public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-3086] [PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount.
Date: Wed,  5 Oct 2022 12:22:56 +0000 (GMT)	[thread overview]
Message-ID: <20221005122256.06BA4385701D@sourceware.org> (raw)

https://gcc.gnu.org/g:4c451631f722c9939260a5c2fc209802a47e525f

commit r13-3086-g4c451631f722c9939260a5c2fc209802a47e525f
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Tue Oct 4 17:05:10 2022 +0200

    [PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount.
    
            PR tree-optimization/107052
    
    gcc/ChangeLog:
    
            * gimple-range-op.cc (cfn_popcount::fold_range): Take into account
            nonzero bit mask.

Diff:
---
 gcc/gimple-range-op.cc                   | 15 ++++++++++++---
 gcc/testsuite/gcc.dg/tree-ssa/pr107052.c | 13 +++++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc
index 5b71d1cce6d..42ebc7d6ce5 100644
--- a/gcc/gimple-range-op.cc
+++ b/gcc/gimple-range-op.cc
@@ -422,15 +422,24 @@ public:
   virtual bool fold_range (irange &r, tree type, const irange &lh,
 			   const irange &rh, relation_kind rel) const
   {
+    if (lh.undefined_p ())
+      return false;
+    unsigned prec = TYPE_PRECISION (type);
+    wide_int nz = lh.get_nonzero_bits ();
+    wide_int pop = wi::shwi (wi::popcount (nz), prec);
     // 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;
       }
-    return cfn_ffs::fold_range (r, type, lh, rh, rel);
+    if (cfn_ffs::fold_range (r, type, lh, rh, rel))
+      {
+	int_range<2> tmp (type, wi::zero (prec), pop);
+	r.intersect (tmp);
+	return true;
+      }
+    return false;
   }
 } op_cfn_popcount;
 
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr107052.c b/gcc/testsuite/gcc.dg/tree-ssa/pr107052.c
new file mode 100644
index 00000000000..88b5213160d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr107052.c
@@ -0,0 +1,13 @@
+// { dg-do compile }
+// { dg-options "-O2 -fdump-tree-evrp" }
+
+void link_failure();
+void f(int a)
+{
+  a &= 0x300;
+  int b =  __builtin_popcount(a);
+  if (b > 3)
+    link_failure();
+}
+
+// { dg-final { scan-tree-dump-not "link_failure" "evrp" } }

                 reply	other threads:[~2022-10-05 12:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221005122256.06BA4385701D@sourceware.org \
    --to=aldyh@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).