]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
middle-end/106070 - bogus cond-expr folding
authorRichard Biener <rguenther@suse.de>
Fri, 24 Jun 2022 11:37:22 +0000 (13:37 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 24 Jun 2022 12:50:05 +0000 (14:50 +0200)
The following fixes up r13-469-g9a53101caadae1b5 by properly
implementing what operand_equal_for_comparison_p did.

2022-06-24  Richard Biener  <rguenther@suse.de>

PR middle-end/106070
* match.pd (a != b ? a : b): Fix translation of
operand_equal_for_comparison_p.

* gcc.dg/torture/pr106070.c: New testcase.

gcc/match.pd
gcc/testsuite/gcc.dg/torture/pr106070.c [new file with mode: 0644]

index 4a570894b2e91643ad5441dfa2d20f845218d2c2..e486b4be282cbbbe9881f130bb9ecc67cd55a4b8 100644 (file)
@@ -4574,12 +4574,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (cond (cmp:c (nop_convert1?@c0 @0) (nop_convert2?@c1 @1))
         (convert3? @0) (convert4? @1))
   (if (!HONOR_SIGNED_ZEROS (type)
-       && ((INTEGRAL_TYPE_P (type)
-           /* Allow widening conversions of the data.  */
+       && (/* Allow widening conversions of the compare operands as data.  */
+          (INTEGRAL_TYPE_P (type)
+           && types_match (TREE_TYPE (@c0), TREE_TYPE (@0))
+           && types_match (TREE_TYPE (@c1), TREE_TYPE (@1))
            && TYPE_PRECISION (TREE_TYPE (@0)) <= TYPE_PRECISION (type)
            && TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (type))
-          || (tree_nop_conversion_p (type, TREE_TYPE (@0))
-              && tree_nop_conversion_p (type, TREE_TYPE (@1)))))
+          /* Or sign conversions for the comparison.  */
+          || (types_match (type, TREE_TYPE (@0))
+              && types_match (type, TREE_TYPE (@1)))))
    (switch
     (if (cmp == EQ_EXPR)
      (if (VECTOR_TYPE_P (type))
diff --git a/gcc/testsuite/gcc.dg/torture/pr106070.c b/gcc/testsuite/gcc.dg/torture/pr106070.c
new file mode 100644 (file)
index 0000000..c36534f
--- /dev/null
@@ -0,0 +1,20 @@
+/* { dg-do run } */
+
+unsigned int var_2 = 1;
+int var_4 = -1;
+int var_10 = 4;
+unsigned long arr_252;
+void __attribute__((noipa)) test() {
+  for (int a = 0; a < var_10; a += 2)
+    arr_252 = var_2 != (int)var_4 ? (unsigned long)var_4 : (unsigned long)var_2;
+}
+
+void test();
+
+int main()
+{
+  test();
+  if (arr_252 != 0xffffffffffffffff)
+    __builtin_abort();
+  return 0;
+}