From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id B64513860746; Tue, 27 Sep 2022 08:46:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B64513860746 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664268400; bh=XLtnEeII59jxcIaa1oBM06c5dmqj2xnlOo8EMjTX4r8=; h=From:To:Subject:Date:From; b=x86NIJ96Db1zsTanDjdDzKE5Q7BKNOJxWNvHCEsdUPy3BRXNbun1nZzG1kohODHbB xXTYm/YpSNYSaZjobwPj/82NKr1cGKG1g0LXmLlR6WcYlofL2ANNU/QdTIayyE4o/f r9lv7OyoB53T6E2GZZ0v+IVNHWDKZYr/3mDwrUzs= 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-2894] irange: keep better track of powers of 2. X-Act-Checkin: gcc X-Git-Author: Aldy Hernandez X-Git-Refname: refs/heads/master X-Git-Oldrev: 1db05e1a1c1ee3d8a95826d477173fb7c557c002 X-Git-Newrev: be4a6551ed37c1e7dbdfb9400fc2e2b5d40c5be2 Message-Id: <20220927084640.B64513860746@sourceware.org> Date: Tue, 27 Sep 2022 08:46:40 +0000 (GMT) List-Id: https://gcc.gnu.org/g:be4a6551ed37c1e7dbdfb9400fc2e2b5d40c5be2 commit r13-2894-gbe4a6551ed37c1e7dbdfb9400fc2e2b5d40c5be2 Author: Aldy Hernandez Date: Tue Sep 27 08:05:30 2022 +0200 irange: keep better track of powers of 2. When setting the nonzero bits to a mask containing only one bit, set the range immediately, as it can be devined from the mask. This helps us keep better track of powers of two. For example, with this patch a nonzero mask of 0x8000 is set to a range of [0,0][0x8000,0x8000] with a nonzero mask of 0x8000. gcc/ChangeLog: * value-range.cc (irange::set_nonzero_bits): Set range when known. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/popcount6.c: New test. Diff: --- gcc/testsuite/gcc.dg/tree-ssa/popcount6.c | 12 ++++++++++++ gcc/value-range.cc | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c new file mode 100644 index 00000000000..1406ad9d33b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c @@ -0,0 +1,12 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp" } + +int g(int n) +{ + n &= 0x8000; + if (n == 0) + return 1; + return __builtin_popcount(n); +} + +// { dg-final { scan-tree-dump "return 1;" "evrp" } } diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 754379add19..6154d73ccf5 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -2930,6 +2930,19 @@ irange::set_nonzero_bits (const wide_int_ref &bits) set_nonzero_bits (NULL); return; } + // If we have only one bit set in the mask, we can figure out the + // range immediately. + if (wi::popcount (bits) == 1) + { + bool has_zero = contains_p (build_zero_cst (type ())); + set (type (), bits, bits); + if (has_zero) + { + int_range<2> zero; + zero.set_zero (type ()); + union_ (zero); + } + } set_nonzero_bits (wide_int_to_tree (type (), bits)); }