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>
&& (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)
--- /dev/null
+/* { 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" } } */
--- /dev/null
+/* { 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" } } */