]> gcc.gnu.org Git - gcc.git/commitdiff
re PR tree-optimization/57656 (Wrong constant folding)
authorRichard Biener <rguenther@suse.de>
Tue, 3 Sep 2013 10:00:06 +0000 (10:00 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 3 Sep 2013 10:00:06 +0000 (10:00 +0000)
2013-09-03  Richard Biener  <rguenther@suse.de>

PR middle-end/57656
* fold-const.c (negate_expr_p): Fix division case.
(negate_expr): Likewise.

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

From-SVN: r202204

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/torture/pr57656.c [new file with mode: 0644]

index 25e3fa5a5ae13164cd48ef5d8b3e6726c70d3c64..11b95c70dfb2376194d982c18227d58e7f7645c1 100644 (file)
@@ -1,3 +1,9 @@
+2013-09-03  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/57656
+       * fold-const.c (negate_expr_p): Fix division case.
+       (negate_expr): Likewise.
+
 2013-09-03  Richard Biener  <rguenther@suse.de>
 
        PR lto/58285
index f959f0a14a0d27889f438e73b0c94c1a0f6629e6..9956b2c9f0731c0797c6e2e4e8b33e32ff7c9bec 100644 (file)
@@ -483,11 +483,24 @@ negate_expr_p (tree t)
         and actually traps on some architectures.  But if overflow is
         undefined, we can negate, because - (INT_MIN / 1) is an
         overflow.  */
-      if (INTEGRAL_TYPE_P (TREE_TYPE (t))
-         && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
-        break;
-      return negate_expr_p (TREE_OPERAND (t, 1))
-             || negate_expr_p (TREE_OPERAND (t, 0));
+      if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
+       {
+         if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
+           break;
+         /* If overflow is undefined then we have to be careful because
+            we ask whether it's ok to associate the negate with the
+            division which is not ok for example for
+            -((a - b) / c) where (-(a - b)) / c may invoke undefined
+            overflow because of negating INT_MIN.  So do not use
+            negate_expr_p here but open-code the two important cases.  */
+         if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
+             || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
+                 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
+           return true;
+       }
+      else if (negate_expr_p (TREE_OPERAND (t, 0)))
+       return true;
+      return negate_expr_p (TREE_OPERAND (t, 1));
 
     case NOP_EXPR:
       /* Negate -((double)float) as (double)(-float).  */
@@ -682,16 +695,20 @@ fold_negate_expr (location_t loc, tree t)
              return fold_build2_loc (loc, TREE_CODE (t), type,
                                  TREE_OPERAND (t, 0), negate_expr (tem));
            }
+         /* If overflow is undefined then we have to be careful because
+            we ask whether it's ok to associate the negate with the
+            division which is not ok for example for
+            -((a - b) / c) where (-(a - b)) / c may invoke undefined
+            overflow because of negating INT_MIN.  So do not use
+            negate_expr_p here but open-code the two important cases.  */
           tem = TREE_OPERAND (t, 0);
-          if (negate_expr_p (tem))
-           {
-             if (INTEGRAL_TYPE_P (type)
-                 && (TREE_CODE (tem) != INTEGER_CST
-                     || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
-               fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
-             return fold_build2_loc (loc, TREE_CODE (t), type,
-                                 negate_expr (tem), TREE_OPERAND (t, 1));
-           }
+         if ((INTEGRAL_TYPE_P (type)
+              && (TREE_CODE (tem) == NEGATE_EXPR
+                  || (TREE_CODE (tem) == INTEGER_CST
+                      && may_negate_without_overflow_p (tem))))
+             || !INTEGRAL_TYPE_P (type))
+           return fold_build2_loc (loc, TREE_CODE (t), type,
+                                   negate_expr (tem), TREE_OPERAND (t, 1));
         }
       break;
 
index 7f45feb94d853396bd34628ed84c410d0870dd1d..33282ff31de671b93f7551653784b1d52c1d6561 100644 (file)
@@ -1,3 +1,8 @@
+2013-09-03  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/57656
+       * gcc.dg/torture/pr57656.c: New testcase.
+
 2013-09-03  Richard Biener  <rguenther@suse.de>
 
        PR middle-end/57287
diff --git a/gcc/testsuite/gcc.dg/torture/pr57656.c b/gcc/testsuite/gcc.dg/torture/pr57656.c
new file mode 100644 (file)
index 0000000..4f3645e
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do run } */
+/* { dg-options "-fstrict-overflow" } */
+
+int main (void)
+{
+  int a = -1;
+  int b = __INT_MAX__;
+  int c = 2;
+  int t = 1 - ((a - b) / c);  // t = 1 - ( __INT_MIN__ / 2 )
+  if (t != (1 - (-1 - __INT_MAX__) / 2))
+    __builtin_abort();
+  return 0;
+}
This page took 0.111528 seconds and 5 git commands to generate.