https://gcc.gnu.org/g:80c77d9a1cfbfde3a437361fa95be326fcdf3e58

commit r16-6752-g80c77d9a1cfbfde3a437361fa95be326fcdf3e58
Author: Andrew Pinski <[email protected]>
Date:   Mon Jan 12 18:58:47 2026 -0800

    match: Add simplification of `(a*zero_one_valued_p) & b` if `a & b` 
simplifies [PR119402]
    
    This is a small reassociation for `a*bool & b` into `(a & b) * bool` 
checking if
    `a & b` simplifies. Since it could be the case `b` is `~a` or `a` or 
something
    else that might simplify when anding with `a`.
    
    Note this fixes a regression for aarch64 where the cost of a multiply vs 
`&-` changed
    in GCC 14 and can no longer optimize some cases at the RTL level.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/119402
    gcc/ChangeLog:
    
            * match.pd (`(a*zero_one_valued_p) & b`): New pattern.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/bitops-14.c: New test.
            * gcc.dg/tree-ssa/bitops-15.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/match.pd                              |  5 +++++
 gcc/testsuite/gcc.dg/tree-ssa/bitops-14.c | 24 ++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bitops-15.c | 24 ++++++++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index cc33a972b982..2be1c5d83f1a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5137,6 +5137,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && (GIMPLE || !TREE_SIDE_EFFECTS (@1)))
   (cond (convert:boolean_type_node @2) @1 @0)))
 
+/* Transform A & (B*cmp) into (A&B)*cmp.  */
+(simplify
+ (bit_and:c (mult:cs zero_one_valued_p@0 @1) @2)
+ (mult @0 (bit_and! @1 @2)))
+
 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
 (simplify
  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-14.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bitops-14.c
new file mode 100644
index 000000000000..4fe794672871
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-14.c
@@ -0,0 +1,24 @@
+/* { dg-do compile }  */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+
+__attribute__((noipa))
+int x(_Bool iftmp, unsigned _6)
+{
+        return ((-iftmp) & _6) & (~_6);
+}
+__attribute__((noipa))
+int y(_Bool iftmp, unsigned _6)
+{
+        return (iftmp * _6) & (~_6);
+}
+__attribute__((noipa))
+int z(_Bool iftmp, unsigned _6)
+{
+        unsigned t = ~_6;
+        unsigned t1 = (iftmp ? _6 : 0);
+        return t1 & t;
+}
+
+/* In this case AND should be removed.  */
+/* { dg-final { scan-tree-dump-not " & " "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-15.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bitops-15.c
new file mode 100644
index 000000000000..37608bb35f0d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-15.c
@@ -0,0 +1,24 @@
+/* { dg-do compile }  */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+
+__attribute__((noipa))
+int x(_Bool iftmp, unsigned _6)
+{
+        return ((-iftmp) & _6) & (_6);
+}
+__attribute__((noipa))
+int y(_Bool iftmp, unsigned _6)
+{
+        return (iftmp * _6) & (_6);
+}
+__attribute__((noipa))
+int z(_Bool iftmp, unsigned _6)
+{
+        unsigned t = _6;
+        unsigned t1 = (iftmp ? _6 : 0);
+        return t1 & t;
+}
+
+/* In this case AND should be removed.  */
+/* { dg-final { scan-tree-dump-not " & " "optimized" } } */

Reply via email to