From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id E5FDC38582BC; Tue, 27 Sep 2022 15:02:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5FDC38582BC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664290942; bh=ZzbYJEL5Gm9IfkWmftWcrvM1RJSiDBvrxhdAM1E5z5Y=; h=From:To:Subject:Date:From; b=j2I2yeXGEmVsA0uvnVQqhF/fDZ2yVuUOM6Wbn//6YfgwaSSZ24yAuo3SPuIwUkNqU Shos07Um5VGfRmCsLbA5akOqjMaANA3nhQdxgqc3BlBVyJuF/jfugurcLYNyDmHRih J2xKeCiT4Dcq5aS1qWWblehOQRtEQyEuYEhLouzw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-2900] range-ops: Calculate the popcount of a singleton. X-Act-Checkin: gcc X-Git-Author: Aldy Hernandez X-Git-Refname: refs/heads/master X-Git-Oldrev: 971bc0aae9cf52abe9a6fcab3b7c25d1fa94ad1e X-Git-Newrev: 001c60ccfeaf9a480a56203418af804ad42e88b9 Message-Id: <20220927150222.E5FDC38582BC@sourceware.org> Date: Tue, 27 Sep 2022 15:02:22 +0000 (GMT) List-Id: https://gcc.gnu.org/g:001c60ccfeaf9a480a56203418af804ad42e88b9 commit r13-2900-g001c60ccfeaf9a480a56203418af804ad42e88b9 Author: Aldy Hernandez Date: Tue Sep 27 10:30:00 2022 +0200 range-ops: Calculate the popcount of a singleton. The legacy popcount folding didn't actually fold singleton ranges. I don't think anyone noticed because there are match.pd patterns that fold __builtin_popcount using the global nonzero bits set by CCP. It's good form to handle this, even without CCP's help. Tested on x86-64 Linux. gcc/ChangeLog: * gimple-range-op.cc (cfn_popcount): Calculate the popcount of a singleton. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/popcount6b.c: New test. Diff: --- gcc/gimple-range-op.cc | 8 ++++++++ gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index d7c6dfa933d..3f5e5852e5a 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -397,6 +397,14 @@ public: { if (lh.undefined_p ()) return false; + // Calculating the popcount of a singleton is trivial. + if (lh.singleton_p ()) + { + wide_int nz = lh.get_nonzero_bits (); + wide_int pop = wi::shwi (wi::popcount (nz), TYPE_PRECISION (type)); + r.set (type, pop, pop); + return true; + } // __builtin_ffs* and __builtin_popcount* return [0, prec]. int prec = TYPE_PRECISION (lh.type ()); // If arg is non-zero, then ffs or popcount are non-zero. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c new file mode 100644 index 00000000000..90336ecb070 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount6b.c @@ -0,0 +1,6 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp -fno-tree-ccp" } + +#include "popcount6.c" + +// { dg-final { scan-tree-dump "return 1;" "evrp" } }