middle-end/106414 - fix mistake in ~(x ^ y) -> x == y pattern
authorRichard Biener <rguenther@suse.de>
Mon, 25 Jul 2022 10:10:48 +0000 (12:10 +0200)
committerRichard Biener <rguenther@suse.de>
Mon, 25 Jul 2022 11:24:47 +0000 (13:24 +0200)
When compares are integer typed the inversion with ~ isn't properly
preserved by the equality comparison even when converting the
result properly.  The following fixes this by restricting the
input precisions accordingly.

PR middle-end/106414
* match.pd (~(x ^ y) -> x == y): Restrict to single bit
precision types.

* gcc.dg/torture/pr106414-1.c: New testcase.
* gcc.dg/torture/pr106414-2.c: Likewise.

gcc/match.pd
gcc/testsuite/gcc.dg/torture/pr106414-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/pr106414-2.c [new file with mode: 0644]

index 9736393..330c1db 100644 (file)
@@ -1946,7 +1946,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 /* ~(a ^ b) is a == b for truth valued a and b.  */
 (simplify
  (bit_not (bit_xor:s truth_valued_p@0 truth_valued_p@1))
- (if (!VECTOR_TYPE_P (type))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && TYPE_PRECISION (TREE_TYPE (@0)) == 1)
   (convert (eq @0 @1))))
 
 /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */
diff --git a/gcc/testsuite/gcc.dg/torture/pr106414-1.c b/gcc/testsuite/gcc.dg/torture/pr106414-1.c
new file mode 100644 (file)
index 0000000..0974716
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+
+int a, c, e;
+const int b = 1;
+char d;
+int main() {
+  a = ~(e || 0) ^ b & ~d;
+  d = ~(a | ~2);
+  if (d)
+    __builtin_abort();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr106414-2.c b/gcc/testsuite/gcc.dg/torture/pr106414-2.c
new file mode 100644 (file)
index 0000000..bed6a40
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do run } */
+
+int a, b, c, d;
+unsigned e;
+int main() {
+  c = e = -((a && 1) ^ ~(b || 0));
+  if (e < -1)
+    d = c;
+  if (!d)
+    __builtin_abort();
+  return 0;
+}