https://gcc.gnu.org/g:cee985872c4976c6253c9caeae476760fcf18bec

commit r17-1543-gcee985872c4976c6253c9caeae476760fcf18bec
Author: Souradipto Das <[email protected]>
Date:   Mon Jun 8 09:15:24 2026 +0530

    tree-optimization: Add bitop reduction simplifications against zero
    
    This patch introduces a simplification rule in match.pd to reduce bitwise
    expressions against zero. Specifically, it simplifies patterns where a
    variable checked against zero is combined via bitwise AND/OR with a 
compounded
    bitwise OR check against zero.
    
            PR tree-optimization/125442
    
    gcc/ChangeLog:
    
            * match.pd: Add simplification rules for
            (a == 0) | ((a | b) == 0) -> (a == 0) and
            (a != 0) & ((a | b) != 0) -> (a != 0).
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/int-bwise-opt-3.c: New test.
            * gcc.dg/int-bwise-opt-4.c: New test.
    
    Suggested-by: Andrew Pinski <[email protected]>
    Signed-off-by: Souradipto Das <[email protected]>

Diff:
---
 gcc/match.pd                           | 9 +++++++++
 gcc/testsuite/gcc.dg/int-bwise-opt-3.c | 9 +++++++++
 gcc/testsuite/gcc.dg/int-bwise-opt-4.c | 9 +++++++++
 3 files changed, 27 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 926071896194..d52ea89af2b6 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6685,6 +6685,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (simplify
   (bitop (neeql @0 @1) (neeqr (bit_ior @0 @1) integer_zerop))
   { constant_boolean_node (bitop == BIT_IOR_EXPR, type); }))
+
+/* (a == 0) | ((a | b) == 0) -> (a == 0) -- PR125442
+   (a != 0) & ((a | b) != 0) -> (a != 0) -- PR125442 */
+
+(for bitop (bit_and bit_ior)
+     neeq  (ne      eq)
+(simplify
+  (bitop:c (neeq@2 @0 integer_zerop) (neeq (bit_ior:c @0 @1) integer_zerop))
+  @2))
 #endif
 
 /* These was part of minmax phiopt.  */
diff --git a/gcc/testsuite/gcc.dg/int-bwise-opt-3.c 
b/gcc/testsuite/gcc.dg/int-bwise-opt-3.c
new file mode 100644
index 000000000000..199e85dbe3c8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/int-bwise-opt-3.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f1(int a, int b)
+{
+  return (a == 0) | ((a | b) == 0);
+}
+
+/* { dg-final { scan-tree-dump-times "\\\|" 0 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/int-bwise-opt-4.c 
b/gcc/testsuite/gcc.dg/int-bwise-opt-4.c
new file mode 100644
index 000000000000..6989f71b8fd7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/int-bwise-opt-4.c
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int f2(int a, int b)
+{
+  return (a != 0) & ((a | b) != 0);
+}
+
+/* { dg-final { scan-tree-dump-times "\&" 0 "optimized" } } */

Reply via email to