From patchwork Wed Jul 18 02:18:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kugan Vivekanandarajah X-Patchwork-Id: 945465 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-481778-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="eMTjb41K"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="O9tXawG5"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41Vgnb5Fytz9s3R for ; Wed, 18 Jul 2018 12:19:12 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=HzSEz8wanSkerCngM9j+Z5M71w5VnEXJ92w85vGU7QOgT4 koPpRvZsF2f0G5Yaf780MM3le9Tri/8Or0Ni49NPs+j43U+3/xNGoYppLXUSmkHi 8IogLWVVl52Ojw98DJPCEBmgTJYUfChefiS1mQlAyfEzneQ49j5hM/xaSpG7o= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=ORcCfGl85JkRqdKuZ2dcXJMbQG8=; b=eMTjb41KVxLlLkS200eP XemglyNtSTbp/4M8uvelqr7NKZ13AyVebE1pdxdZhGrWwBW1qsaCAVK/WMi5b6El Jk0juT80vbHYHjLvmUlfK88Qc2WbG7r6jIJ4sVClslmc5cOunYkegrbPFVk92mJ3 C7dnByeOoHK9+cIYlDPdMU0= Received: (qmail 92242 invoked by alias); 18 Jul 2018 02:19:03 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 92224 invoked by uid 89); 18 Jul 2018 02:19:02 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-lf0-f42.google.com Received: from mail-lf0-f42.google.com (HELO mail-lf0-f42.google.com) (209.85.215.42) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 18 Jul 2018 02:19:00 +0000 Received: by mail-lf0-f42.google.com with SMTP id j143-v6so81938lfj.12 for ; Tue, 17 Jul 2018 19:19:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:from:date:message-id:subject:to; bh=R11paaRt7mknJDrPulO0ImQFSSRPyko1Wz56xyWCNTA=; b=O9tXawG5tAprFr16Lk3xM0i99oHYGgb09xn2w4w/avBmILjnPMWrFVmVNotkbapez+ irY3q3yxo3dUfyKrEVMv4aBj7ASZIqyqBKAcCap4h06OqdqKL0YZf4tvy/6Tkv5md3uO R2gdNra0M7ZjsaQI4E5cjM3OywSDdEbYIE9eM= MIME-Version: 1.0 Received: by 2002:a2e:48b:0:0:0:0:0 with HTTP; Tue, 17 Jul 2018 19:18:17 -0700 (PDT) From: Kugan Vivekanandarajah Date: Wed, 18 Jul 2018 12:18:17 +1000 Message-ID: Subject: [PR86544] Fix Popcount detection generates different code on C and C++ To: GCC Patches X-IsSubscribed: yes Attached patch fixes phi-opt not optimizing c++ testcase where we have if (b_4(D) == 0) instead of if (b_4(D) != 0) as shown in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86544 Patch bootstrapped and regression tested on x86_64-linux-gnu with no new regressions. Is this OK for trunk? Thanks, Kugan gcc/ChangeLog: 2018-07-18 Kugan Vivekanandarajah PR middle-end/86544 * tree-ssa-phiopt.c (cond_removal_in_popcount_pattern): Handle comparison with EQ_EXPR in last stmt. gcc/testsuite/ChangeLog: 2018-07-18 Kugan Vivekanandarajah PR middle-end/86544 * g++.dg/tree-ssa/pr86544.C: New test. From c482a4225764e0b338abafb7ccea4553f273f5d5 Mon Sep 17 00:00:00 2001 From: Kugan Vivekanandarajah Date: Wed, 18 Jul 2018 08:14:16 +1000 Subject: [PATCH] fix cpp testcase Change-Id: Icd59b31faef2ac66beb42990cb69cbbe38c238aa --- gcc/testsuite/g++.dg/tree-ssa/pr86544.C | 15 +++++++++++++++ gcc/tree-ssa-phiopt.c | 26 +++++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr86544.C diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr86544.C b/gcc/testsuite/g++.dg/tree-ssa/pr86544.C new file mode 100644 index 0000000..8a90089 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr86544.C @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-phiopt3 -fdump-tree-optimized" } */ + +int PopCount (long b) { + int c = 0; + + while (b) { + b &= b - 1; + c++; + } + return c; +} + +/* { dg-final { scan-tree-dump-times "__builtin_popcount" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if" 0 "phiopt3" } } */ diff --git a/gcc/tree-ssa-phiopt.c b/gcc/tree-ssa-phiopt.c index 656f840..1667bad 100644 --- a/gcc/tree-ssa-phiopt.c +++ b/gcc/tree-ssa-phiopt.c @@ -1614,8 +1614,22 @@ cond_removal_in_popcount_pattern (basic_block cond_bb, basic_block middle_bb, arg = gimple_assign_rhs1 (cast); } + cond = last_stmt (cond_bb); + + /* Cond_bb has a check for b_4 [!=|==] 0 before calling the popcount + builtin. */ + if (gimple_code (cond) != GIMPLE_COND + || (gimple_cond_code (cond) != NE_EXPR + && gimple_cond_code (cond) != EQ_EXPR) + || !integer_zerop (gimple_cond_rhs (cond)) + || arg != gimple_cond_lhs (cond)) + return false; + /* Canonicalize. */ - if (e2->flags & EDGE_TRUE_VALUE) + if ((e2->flags & EDGE_TRUE_VALUE + && gimple_cond_code (cond) == NE_EXPR) + || (e1->flags & EDGE_TRUE_VALUE + && gimple_cond_code (cond) == EQ_EXPR)) { std::swap (arg0, arg1); std::swap (e1, e2); @@ -1625,16 +1639,6 @@ cond_removal_in_popcount_pattern (basic_block cond_bb, basic_block middle_bb, if (lhs != arg0 || !integer_zerop (arg1)) return false; - cond = last_stmt (cond_bb); - - /* Cond_bb has a check for b_4 != 0 before calling the popcount - builtin. */ - if (gimple_code (cond) != GIMPLE_COND - || gimple_cond_code (cond) != NE_EXPR - || !integer_zerop (gimple_cond_rhs (cond)) - || arg != gimple_cond_lhs (cond)) - return false; - /* And insert the popcount builtin and cast stmt before the cond_bb. */ gsi = gsi_last_bb (cond_bb); if (cast) -- 2.7.4