]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
backport: re PR tree-optimization/56250 (Wrong constant folding on unsigned int)
authorJakub Jelinek <jakub@redhat.com>
Tue, 19 Feb 2013 17:26:04 +0000 (18:26 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 19 Feb 2013 17:26:04 +0000 (18:26 +0100)
Backported from mainline
2013-02-08  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/56250
* fold-const.c (extract_muldiv_1) <case NEGATE_EXPR>: Don't optimize
if type is unsigned and code isn't MULT_EXPR.

* gcc.c-torture/execute/pr56250.c: New test.

From-SVN: r196147

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

index 561723e4d5431a6fd2791925f6d1cafc6919faac..d8b06bd5e57cd9259f12ca2cc7703d724cc33cfb 100644 (file)
@@ -1,6 +1,12 @@
 2013-02-19  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2013-02-08  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/56250
+       * fold-const.c (extract_muldiv_1) <case NEGATE_EXPR>: Don't optimize
+       if type is unsigned and code isn't MULT_EXPR.
+
        2013-02-06  Jakub Jelinek  <jakub@redhat.com>
 
        PR middle-end/56217
index 7f17b89b6d3c19139810def16432c1e334463a36..1f864a78ce9524de9222c0dd8555c555f9829b77 100644 (file)
@@ -5716,6 +5716,11 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
         break;
       /* FALLTHROUGH */
     case NEGATE_EXPR:
+      /* For division and modulus, type can't be unsigned, as e.g.
+        (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
+        For signed types, even with wrapping overflow, this is fine.  */
+      if (code != MULT_EXPR && TYPE_UNSIGNED (type))
+       break;
       if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
          != 0)
        return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
index cdc9e6195bb007ebf7e6c18b6d8537a3d1102ded..a8a2fe3a76b6e9b24d0b1103f73b5cf325679bd6 100644 (file)
@@ -1,6 +1,11 @@
 2013-02-19  Jakub Jelinek  <jakub@redhat.com>
 
        Backported from mainline
+       2013-02-08  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/56250
+       * gcc.c-torture/execute/pr56250.c: New test.
+
        2013-02-07  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/56241
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr56250.c b/gcc/testsuite/gcc.c-torture/execute/pr56250.c
new file mode 100644 (file)
index 0000000..8da36f8
--- /dev/null
@@ -0,0 +1,13 @@
+/* PR tree-optimization/56250 */
+
+extern void abort (void);
+
+int
+main ()
+{
+  unsigned int x = 2;
+  unsigned int y = (0U - x / 2) / 2;
+  if (-1U / x != y)
+    abort ();
+  return 0;
+}