]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR middle-end/56917 (-ftrapv detects a overflow wrongly.)
authorRichard Biener <rguenther@suse.de>
Thu, 18 Jun 2015 14:47:18 +0000 (14:47 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 18 Jun 2015 14:47:18 +0000 (14:47 +0000)
2015-06-18  Richard Biener  <rguenther@suse.de>

Backport from mainline
2014-12-04  Marek Polacek  <polacek@redhat.com>

PR middle-end/56917
* fold-const.c (fold_unary_loc): Perform the negation in A's type
when transforming ~ (A - 1) or ~ (A + -1) to -A.

* g++.dg/other/const4.C: New testcase.

From-SVN: r224617

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/other/const4.C [new file with mode: 0644]

index 4ee4479e2278f884e6ce9076293e4961af2c0a8d..2bada22e200cf121905f675f011754b09f1cfa70 100644 (file)
@@ -1,3 +1,12 @@
+2015-06-18  Richard Biener  <rguenther@suse.de>
+
+       Backport from mainline
+       2014-12-04  Marek Polacek  <polacek@redhat.com>
+
+       PR middle-end/56917
+       * fold-const.c (fold_unary_loc): Perform the negation in A's type
+       when transforming ~ (A - 1) or ~ (A + -1) to -A.
+
 2015-06-18  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/66233
index f06bef61573604a17006e75cb0449c125adc5359..d3c2569adc355606a11310df6ccc02e0902a9485 100644 (file)
@@ -8249,9 +8249,14 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
                    && integer_onep (TREE_OPERAND (arg0, 1)))
                   || (TREE_CODE (arg0) == PLUS_EXPR
                       && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
-       return fold_build1_loc (loc, NEGATE_EXPR, type,
-                           fold_convert_loc (loc, type,
-                                             TREE_OPERAND (arg0, 0)));
+       {
+         /* Perform the negation in ARG0's type and only then convert
+            to TYPE as to avoid introducing undefined behavior.  */
+         tree t = fold_build1_loc (loc, NEGATE_EXPR,
+                                   TREE_TYPE (TREE_OPERAND (arg0, 0)),
+                                   TREE_OPERAND (arg0, 0));
+         return fold_convert_loc (loc, type, t);
+       }
       /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify.  */
       else if (TREE_CODE (arg0) == BIT_XOR_EXPR
               && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
index 6331bcbdaeafbac6da1b98a424a3c3cc56259e8a..27d71ca069d0344a7010ecb7dcf43f6a2bf1fb76 100644 (file)
@@ -1,3 +1,11 @@
+2015-06-18  Richard Biener  <rguenther@suse.de>
+
+       Backport from mainline
+       2014-12-04  Marek Polacek  <polacek@redhat.com>
+
+       PR middle-end/56917
+       * g++.dg/other/const4.C: New testcase.
+
 2015-06-18  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/66233
diff --git a/gcc/testsuite/g++.dg/other/const4.C b/gcc/testsuite/g++.dg/other/const4.C
new file mode 100644 (file)
index 0000000..6e30d9e
--- /dev/null
@@ -0,0 +1,10 @@
+// { dg-do compile }
+
+int lValue;
+int main()
+{
+  switch (lValue)
+    {
+    case -(int)((2U << (8 * sizeof(int) - 2)) - 1) - 1:;
+    }
+}