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: 142234 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp271072ljj; Tue, 17 Jul 2018 19:19:19 -0700 (PDT) X-Google-Smtp-Source: AAOMgpduVIJ++VDfb59UuFTCBHD6tpY0Jm2ADgt98qWM5AdhWL1F4GnZt+LV99qWQqaZgaCgmI+F X-Received: by 2002:a17:902:b785:: with SMTP id e5-v6mr3957958pls.339.1531880359241; Tue, 17 Jul 2018 19:19:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531880359; cv=none; d=google.com; s=arc-20160816; b=KzfIBK0q2cBjgYyFVpac4nOBCdI6YTzbU/RDNiVqHIYMvK4vuhsK9Z6ArwdDeCrq8b w/OVxhMC4NBxKKMmtiHBYTth+7/rrbcxRpl5jumSzHftO3MgaNZ16ACrVH3ePTu9iu7t UQ+kcoct6LJtM1MRFtRHDgz6geW9GY8JNk/aHT8sqZChmpI52nbhdOXjbfU6DF2wi+fN VOLKH0ndcXjTZUtfAoU4xNW8FXRBorjIDzX0mlmGt1OR5rHQHJqzs1h9gCavDDEqCDzd Sucjpu1XOwbKXIWea7DUmHhezJn7JqaQ+CcA3M+JilPFtXDG/eJDsChZ1rx56018nUZG xmCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:mime-version:dkim-signature :delivered-to:sender:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=Keu45kZ+q7SB8hErjB8EHj8AQTFneEB5J/8YMA9rUtA=; b=owWVYTcvoOWQSH/53oPvVwkvGxEBK5m5cyDkGA8vrlcWpq1YOsYaw+Xd5QNvEI/DLL iZ7dMtBAO8+vQaB9EP/duDcJAKZm4H1nGaKIAA7KjWpYEtGRLD7avnmxL3FifnbQaXci Y6b1e0HvWsUIEtKICKLngMoWznX8ZjnVKKQqCR8KhNG9Tq+h/dvLStSyIVk+UhTeL5s7 1VUh1q9GrXDN+P91PtGPxZaW2DrUFkoxqkP+pvCcm4VAeWRv7llIrN9z2Fhsw/AsurIW gewXJ5cEWE8rjwBwZqeHXY1yEPzqryU+51RNKhWFPJaAthPzQgZI+LVfYCt9UDFdevUr R90w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wgiW+pXY; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=O9tXawG5; spf=pass (google.com: domain of gcc-patches-return-481778-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-481778-patch=linaro.org@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id h91-v6si2097269pld.132.2018.07.17.19.19.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Jul 2018 19:19:19 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-481778-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wgiW+pXY; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=O9tXawG5; spf=pass (google.com: domain of gcc-patches-return-481778-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom="gcc-patches-return-481778-patch=linaro.org@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=MnEXXdmc7kpEJnErmDvN/Wsz5MG2Ca9vZ7IAnEgRgveLPZ PXT9UKlyKm1PnAt3lJnqkSsf3ASRe1kd76rA1/l0Cxwco6uiE7YuWqmBhnP4iqTK NKlHpxzXVHivquN6QYH5DKlP2vVIdS3FA4o2RPRy1AV+La7+MaQANLAa9j8N0= 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=wgiW+pXYbckA+/LOldkq U/UIlcJKvHQCxfyOR/h6QKUBocy+dHA1VxtzRthO0Yci7bbG3eT+O9PPnlUOe8eh nGb5MJQt9HWY1ByT21nkIWRd0bwlEOZBzcxBVhY44wn8T45nhP+LgwRgm0CY+JFy dB4hknbEZ4cODE+J3XA2bV4= 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