]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
irange: keep better track of powers of 2.
authorAldy Hernandez <aldyh@redhat.com>
Tue, 27 Sep 2022 06:05:30 +0000 (08:05 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Tue, 27 Sep 2022 08:43:11 +0000 (10:43 +0200)
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.

gcc/testsuite/gcc.dg/tree-ssa/popcount6.c [new file with mode: 0644]
gcc/value-range.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c b/gcc/testsuite/gcc.dg/tree-ssa/popcount6.c
new file mode 100644 (file)
index 0000000..1406ad9
--- /dev/null
@@ -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" } }
index 754379add190746ea51a9d2c8a2db9a3dc6e6752..6154d73ccf5dccb0b89e5fb8a4937090ea0a441c 100644 (file)
@@ -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));
 }