]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match: Fix (a != b) | ((a|b) != 0) and (a == b) & ((a|b) == 0) match pattern [PR122296]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 17 Oct 2025 00:02:52 +0000 (17:02 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 17 Oct 2025 05:00:45 +0000 (22:00 -0700)
There are 2 fixes for these 2 patterns.
1) Reuse the (a|b) expression instead of recreating it
   Fixed by capturing the bit_ior expression and using that instead
   of a new expression.
2) Use the correct 0. Fixed by capturing the integer_zerop and using that
   instead of integer_zero_node.

2) could be fuxed by using `build_cst_zero (TREE_TYPE (@0))` But since
we already have the correct 0, capturing it would be faster.

Pushed as obvious after a bootstrap/test on x86_64-linux-gnu.

PR tree-optimization/122296

gcc/ChangeLog:

* match.pd (`(a != b) | ((a|b) != 0)`): Reuse both
the ior and zero instead of recreating them.
(`(a == b) & ((a|b) == 0)`): Likewise

gcc/testsuite/ChangeLog:

* gcc.dg/torture/int-bwise-opt-1.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/match.pd
gcc/testsuite/gcc.dg/torture/int-bwise-opt-1.c [new file with mode: 0644]

index ab5b51bacaf638c76c0b5ae5951cae7da02a05e9..bfc51e6579a958e55582178560f99a5d8d2564cd 100644 (file)
@@ -6844,11 +6844,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_and:c (ne:c @0 @1) (ne (bit_ior @0 @1) integer_zerop))
  (ne @0 @1))
 (simplify
- (bit_ior:c (ne:c @0 @1) (ne (bit_ior @0 @1) integer_zerop))
- (ne (bit_ior @0 @1) { integer_zero_node; }))
+ (bit_ior:c (ne:c @0 @1) (ne (bit_ior@2 @0 @1) integer_zerop@3))
+ (ne @2 @3))
 (simplify
- (bit_and:c (eq:c @0 @1) (eq (bit_ior @0 @1) integer_zerop))
- (eq (bit_ior @0 @1) { integer_zero_node; }))
+ (bit_and:c (eq:c @0 @1) (eq (bit_ior@2 @0 @1) integer_zerop@3))
+ (eq @2 @3))
 (simplify
  (bit_ior:c (eq:c @0 @1) (eq (bit_ior @0 @1) integer_zerop))
  (eq @0 @1))
diff --git a/gcc/testsuite/gcc.dg/torture/int-bwise-opt-1.c b/gcc/testsuite/gcc.dg/torture/int-bwise-opt-1.c
new file mode 100644 (file)
index 0000000..ceea95b
--- /dev/null
@@ -0,0 +1,32 @@
+/* { dg-do compile } */
+
+/* PR tree-optimization/122296 */
+
+typedef unsigned type1;
+typedef unsigned __attribute__((vector_size(sizeof(unsigned) ))) type2;
+type1 g(type1 a, type1 b)
+{
+  type1 c = a == b;
+  type1 d = (a|b) == 0;
+  return c & d;
+}
+
+type1 f(type1 a, type1 b)
+{
+  type1 c = a != b;
+  type1 d = (a|b) != 0;
+  return c | d;
+}
+type2 g2(type2 a, type2 b)
+{
+  type2 c = a == b;
+  type2 d = (a|b) == 0;
+  return c & d;
+}
+
+type2 f2(type2 a, type2 b)
+{
+  type2 c = a != b;
+  type2 d = (a|b) != 0;
+  return c | d;
+}