public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] [PR tree-optimization/107052] range-ops: Pass nonzero masks through cast.
@ 2022-10-05 12:22 Aldy Hernandez
  2022-10-05 12:22 ` [COMMITTED] [PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount Aldy Hernandez
  0 siblings, 1 reply; 3+ messages in thread
From: Aldy Hernandez @ 2022-10-05 12:22 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, drepper, Aldy Hernandez

Track nonzero masks through a cast in range-ops.

We could also track through a truncating cast if the mask fits in the
outer type.  I will do that as a follow-up patch because I already have
this patchset tested.

	PR tree-optimization/107052

gcc/ChangeLog:

	* range-op.cc (operator_cast::fold_range): Set nonzero mask.
---
 gcc/range-op.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 4f647abd91c..6fa27a8904e 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2515,6 +2515,14 @@ operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
       if (r.varying_p ())
 	return true;
     }
+
+  // Pass nonzero mask through the cast.
+  if (!truncating_cast_p (inner, outer))
+    {
+      wide_int nz = inner.get_nonzero_bits ();
+      nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
+      r.set_nonzero_bits (nz);
+    }
   return true;
 }
 
-- 
2.37.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [COMMITTED] [PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount.
  2022-10-05 12:22 [COMMITTED] [PR tree-optimization/107052] range-ops: Pass nonzero masks through cast Aldy Hernandez
@ 2022-10-05 12:22 ` Aldy Hernandez
  0 siblings, 0 replies; 3+ messages in thread
From: Aldy Hernandez @ 2022-10-05 12:22 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, drepper, Aldy Hernandez

	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                   | 15 ++++++++++++---
 gcc/testsuite/gcc.dg/tree-ssa/pr107052.c | 13 +++++++++++++
 2 files changed, 25 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr107052.c

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" } }
-- 
2.37.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [COMMITTED] [PR tree-optimization/107052] range-ops: Pass nonzero masks through cast.
@ 2022-10-05 12:21 Aldy Hernandez
  0 siblings, 0 replies; 3+ messages in thread
From: Aldy Hernandez @ 2022-10-05 12:21 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, uli, Aldy Hernandez

Track nonzero masks through a cast in range-ops.

We could also track through a truncating cast if the mask fits in the
outer type.  I will do that as a follow-up patch because I already have
this patchset tested.

	PR tree-optimization/107052

gcc/ChangeLog:

	* range-op.cc (operator_cast::fold_range): Set nonzero mask.
---
 gcc/range-op.cc | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 4f647abd91c..6fa27a8904e 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2515,6 +2515,14 @@ operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
       if (r.varying_p ())
 	return true;
     }
+
+  // Pass nonzero mask through the cast.
+  if (!truncating_cast_p (inner, outer))
+    {
+      wide_int nz = inner.get_nonzero_bits ();
+      nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
+      r.set_nonzero_bits (nz);
+    }
   return true;
 }
 
-- 
2.37.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-05 12:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-05 12:22 [COMMITTED] [PR tree-optimization/107052] range-ops: Pass nonzero masks through cast Aldy Hernandez
2022-10-05 12:22 ` [COMMITTED] [PR tree-optimization/107052] range-ops: Take into account nonzero mask in popcount Aldy Hernandez
  -- strict thread matches above, loose matches on Subject: below --
2022-10-05 12:21 [COMMITTED] [PR tree-optimization/107052] range-ops: Pass nonzero masks through cast Aldy Hernandez

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).