]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match: Add simplification of `(a*zero_one_valued_p) & b` if `a & b` simplifies [PR119402]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Tue, 13 Jan 2026 02:58:47 +0000 (18:58 -0800)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Tue, 13 Jan 2026 20:42:29 +0000 (12:42 -0800)
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 <andrew.pinski@oss.qualcomm.com>
gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/bitops-14.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/bitops-15.c [new file with mode: 0644]

index cc33a972b982eba7018e8c48fae9c741f675a221..2be1c5d83f1a42ae8f99ddfbe022dc22f495b970 100644 (file)
@@ -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 (file)
index 0000000..4fe7946
--- /dev/null
@@ -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 (file)
index 0000000..37608bb
--- /dev/null
@@ -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" } } */