public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] Optimize [0 = x & MASK] in range-ops.
@ 2022-09-26 17:24 Aldy Hernandez
  2022-09-26 23:27 ` Jeff Law
  2022-09-27  9:26 ` Mikael Morin
  0 siblings, 2 replies; 3+ messages in thread
From: Aldy Hernandez @ 2022-09-26 17:24 UTC (permalink / raw)
  To: GCC patches; +Cc: Andrew MacLeod, Aldy Hernandez

For [0 = x & MASK], we can determine that x is ~MASK.  This is
something we're picking up in DOM thanks to maybe_set_nonzero_bits,
but is something we should handle natively.

This is a good example of how much easier to maintain the range-ops
entries are versus the ad-hoc pattern matching stuff we had to do
before.  For the curious, compare the changes to range-op here,
versus maybe_set_nonzero_bits.

I'm leaving the call to maybe_set_nonzero_bits until I can properly
audit it to make sure we're catching it all in range-ops.  It won't
hurt, since both set_range_info() and set_nonzero_bits() are
intersect operations, so we'll never lose information if we do both.

Tested on x86-64 Linux.

	PR tree-optimization/107009

gcc/ChangeLog:

	* range-op.cc (operator_bitwise_and::op1_range): Optimize 0 = x & MASK.
	(range_op_bitwise_and_tests): New test.
---
 gcc/range-op.cc | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 072ebd32109..fc930f4d613 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -2951,6 +2951,15 @@ operator_bitwise_and::op1_range (irange &r, tree type,
     }
   if (r.undefined_p ())
     set_nonzero_range_from_mask (r, type, lhs);
+
+  // For 0 = op1 & MASK, op1 is ~MASK.
+  if (lhs.zero_p () && op2.singleton_p ())
+    {
+      wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
+      int_range<2> tmp (type);
+      tmp.set_nonzero_bits (nz);
+      r.intersect (tmp);
+    }
   return true;
 }
 
@@ -4612,6 +4621,15 @@ range_op_bitwise_and_tests ()
   op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
   ASSERT_TRUE (res == int_range<1> (integer_type_node));
 
+  // For 0 = x & MASK, x is ~MASK.
+  {
+    int_range<2> zero (integer_zero_node, integer_zero_node);
+    int_range<2> mask = int_range<2> (INT (7), INT (7));
+    op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
+    wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
+    ASSERT_TRUE (res.get_nonzero_bits () == inv);
+  }
+
   // (NONZERO | X) is nonzero.
   i1.set_nonzero (integer_type_node);
   i2.set_varying (integer_type_node);
-- 
2.37.1


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

* Re: [COMMITTED] Optimize [0 = x & MASK] in range-ops.
  2022-09-26 17:24 [COMMITTED] Optimize [0 = x & MASK] in range-ops Aldy Hernandez
@ 2022-09-26 23:27 ` Jeff Law
  2022-09-27  9:26 ` Mikael Morin
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff Law @ 2022-09-26 23:27 UTC (permalink / raw)
  To: gcc-patches


On 9/26/22 11:24, Aldy Hernandez via Gcc-patches wrote:
> For [0 = x & MASK], we can determine that x is ~MASK.  This is
> something we're picking up in DOM thanks to maybe_set_nonzero_bits,
> but is something we should handle natively.
>
> This is a good example of how much easier to maintain the range-ops
> entries are versus the ad-hoc pattern matching stuff we had to do
> before.  For the curious, compare the changes to range-op here,
> versus maybe_set_nonzero_bits.
>
> I'm leaving the call to maybe_set_nonzero_bits until I can properly
> audit it to make sure we're catching it all in range-ops.  It won't
> hurt, since both set_range_info() and set_nonzero_bits() are
> intersect operations, so we'll never lose information if we do both.
>
> Tested on x86-64 Linux.
>
> 	PR tree-optimization/107009
>
> gcc/ChangeLog:
>
> 	* range-op.cc (operator_bitwise_and::op1_range): Optimize 0 = x & MASK.
> 	(range_op_bitwise_and_tests): New test.

Umm,


0 = x & MASK;


Just means that X has no bits set in MASK.   So you can use it to set 
nonzero-bits to ~MASK like your patch does and you can use that to 
refine a result.  So it's really the comment that is misleading/wrong.


jeff


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

* Re: [COMMITTED] Optimize [0 = x & MASK] in range-ops.
  2022-09-26 17:24 [COMMITTED] Optimize [0 = x & MASK] in range-ops Aldy Hernandez
  2022-09-26 23:27 ` Jeff Law
@ 2022-09-27  9:26 ` Mikael Morin
  1 sibling, 0 replies; 3+ messages in thread
From: Mikael Morin @ 2022-09-27  9:26 UTC (permalink / raw)
  To: gcc-patches

Le 26/09/2022 à 19:24, Aldy Hernandez via Gcc-patches a écrit :
> For [0 = x & MASK], we can determine that x is ~MASK.
> 
Suggestion: as AND is a bitwise operator, a X non-zero bit can be 
cleared for every bit at which the result is cleared and the MASK is 
set, so what you do here can be extended to non-zero result values.


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

end of thread, other threads:[~2022-09-27  9:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-26 17:24 [COMMITTED] Optimize [0 = x & MASK] in range-ops Aldy Hernandez
2022-09-26 23:27 ` Jeff Law
2022-09-27  9:26 ` Mikael Morin

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