]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Add pattern for `(x | y) & (x & z)`
authorAndrew Pinski <apinski@marvell.com>
Sun, 3 Sep 2023 17:17:29 +0000 (10:17 -0700)
committerAndrew Pinski <apinski@marvell.com>
Tue, 5 Sep 2023 21:14:54 +0000 (14:14 -0700)
Like the pattern already there for `(x | y) & x`,
this adds a simple pattern to optimize `(x | y) & (x & z)`
to just `x & z`.

OK? Bootstrapped and tested on x86-64-linux-gnu with no regressions.

gcc/ChangeLog:

PR tree-optimization/103536
* match.pd (`(x | y) & (x & z)`,
`(x & y) | (x | z)`): New patterns.

gcc/testsuite/ChangeLog:

PR tree-optimization/103536
* gcc.dg/tree-ssa/andor-6.c: New test.
* gcc.dg/tree-ssa/andor-bool-1.c: New test.

gcc/match.pd
gcc/testsuite/gcc.dg/tree-ssa/andor-6.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c [new file with mode: 0644]

index 41382f73a2f9c1d12a5ff5f90e4c361fc28edc42..bc9ddd65fe3114ea8a50c1ddf15f793d319f54e6 100644 (file)
@@ -2070,7 +2070,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (with { bool wascmp; }
    (if (bitwise_inverted_equal_p (@0, @2, wascmp)
        && (!wascmp || element_precision (type) == 1))
-    (bitop @0 @1)))))
+    (bitop @0 @1))))
+  /* (x | y) & (x & z) -> (x & z) */
+  /* (x & y) | (x | z) -> (x | z) */
+ (simplify
+  (bitop:c (rbitop:c @0 @1) (bitop:c@3 @0 @2))
+  @3))
 
 /* ((x | y) & z) | x -> (z & y) | x
    ((x ^ y) & z) | x -> (z & y) | x  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-6.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-6.c
new file mode 100644 (file)
index 0000000..32e1173
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+/* PR tree-optimization/103536 */
+
+int
+orand(int a, int b, int c)
+{
+    return (a | b) & (a & c); // a & c
+}
+
+/* { dg-final { scan-tree-dump "return a \& c;" "original" } } */
+
+int
+andor(int d, int e, int f)
+{
+    return (d & e) | (d | f); // d | f
+}
+
+/* { dg-final { scan-tree-dump "return d \\| f;" "original" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c b/gcc/testsuite/gcc.dg/tree-ssa/andor-bool-1.c
new file mode 100644 (file)
index 0000000..a1b974f
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/103536 */
+
+_Bool
+src_1 (_Bool a, _Bool b)
+{
+    return (a || b) && (a && b);
+}
+
+/* { dg-final { scan-tree-dump "a_\[0-9\]+.D. \& b_\[0-9\]+.D." "optimized" } } */
+/* { dg-final { scan-tree-dump-not "a_\[0-9\]+.D. \\\| b_\[0-9\]+.D." "optimized" } } */
+/* { dg-final { scan-tree-dump-not "if " "optimized" } } */