2015-10-28 Richard Biener <rguenther@suse.de>
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 28 Oct 2015 13:41:25 +0000 (13:41 +0000)
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 28 Oct 2015 13:41:25 +0000 (13:41 +0000)
* fold-const.c (negate_expr_p): Adjust the division case to
properly avoid introducing undefined overflow.
(fold_negate_expr): Likewise.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@229484 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/fold-const.c

index 883f1b6..410537b 100644 (file)
@@ -1,5 +1,11 @@
 2015-10-28  Richard Biener  <rguenther@suse.de>
 
+       * fold-const.c (negate_expr_p): Adjust the division case to
+       properly avoid introducing undefined overflow.
+       (fold_negate_expr): Likewise.
+
+2015-10-28  Richard Biener  <rguenther@suse.de>
+
        PR tree-optimization/65962
        * tree-ssa-pre.c (eliminate_dom_walker::before_dom_children):
        Avoid creating loop carried dependences also for outer loops
index 61801cb..016c0dd 100644 (file)
@@ -488,29 +488,19 @@ negate_expr_p (tree t)
     case TRUNC_DIV_EXPR:
     case ROUND_DIV_EXPR:
     case EXACT_DIV_EXPR:
-      /* In general we can't negate A / B, because if A is INT_MIN and
-        B is 1, we may turn this into INT_MIN / -1 which is undefined
-        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)))
-       {
-         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)))
+      if (TYPE_UNSIGNED (type))
+       break;
+      if (negate_expr_p (TREE_OPERAND (t, 0)))
        return true;
-      return negate_expr_p (TREE_OPERAND (t, 1));
+      /* In general we can't negate B in A / B, because if A is INT_MIN and
+        B is 1, we may turn this into INT_MIN / -1 which is undefined
+        and actually traps on some architectures.  */
+      if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
+         || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
+         || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
+             && ! integer_onep (TREE_OPERAND (t, 1))))
+       return negate_expr_p (TREE_OPERAND (t, 1));
+      break;
 
     case NOP_EXPR:
       /* Negate -((double)float) as (double)(-float).  */
@@ -680,40 +670,23 @@ fold_negate_expr (location_t loc, tree t)
     case TRUNC_DIV_EXPR:
     case ROUND_DIV_EXPR:
     case EXACT_DIV_EXPR:
-      /* In general we can't negate A / B, because if A is INT_MIN and
+      if (TYPE_UNSIGNED (type))
+       break;
+      if (negate_expr_p (TREE_OPERAND (t, 0)))
+       return fold_build2_loc (loc, TREE_CODE (t), type,
+                               negate_expr (TREE_OPERAND (t, 0)),
+                               TREE_OPERAND (t, 1));
+      /* In general we can't negate B in A / B, because if A is INT_MIN and
         B is 1, we may turn this into INT_MIN / -1 which is undefined
-        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 (type) || TYPE_OVERFLOW_UNDEFINED (type))
-        {
-         const char * const warnmsg = G_("assuming signed overflow does not "
-                                         "occur when negating a division");
-          tem = TREE_OPERAND (t, 1);
-          if (negate_expr_p (tem))
-           {
-             if (INTEGRAL_TYPE_P (type)
-                 && (TREE_CODE (tem) != INTEGER_CST
-                     || integer_onep (tem)))
-               fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
-             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 ((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));
-        }
+        and actually traps on some architectures.  */
+      if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
+          || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
+          || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
+              && ! integer_onep (TREE_OPERAND (t, 1))))
+         && negate_expr_p (TREE_OPERAND (t, 1)))
+       return fold_build2_loc (loc, TREE_CODE (t), type,
+                               TREE_OPERAND (t, 0),
+                               negate_expr (TREE_OPERAND (t, 1)));
       break;
 
     case NOP_EXPR: