]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount.
authorAldy Hernandez <aldyh@redhat.com>
Tue, 4 Oct 2022 15:05:10 +0000 (17:05 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Wed, 5 Oct 2022 12:21:04 +0000 (14:21 +0200)
PR tree-optimization/107052

gcc/ChangeLog:

* gimple-range-op.cc (cfn_popcount::fold_range): Take into account
nonzero bit mask.

gcc/gimple-range-op.cc
gcc/testsuite/gcc.dg/tree-ssa/pr107052.c [new file with mode: 0644]

index 5b71d1cce6d39ba68afd5e53a6264b9252611462..42ebc7d6ce5690880d344aefc630db70a27320b0 100644 (file)
@@ -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 (file)
index 0000000..88b5213
--- /dev/null
@@ -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" } }