re PR middle-end/33779 (folds unsigned multiplication == 0 to true)
authorRichard Guenther <rguenther@suse.de>
Wed, 31 Oct 2007 12:33:05 +0000 (12:33 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 31 Oct 2007 12:33:05 +0000 (12:33 +0000)
2007-10-31  Richard Guenther  <rguenther@suse.de>

PR middle-end/33779
* fold-const.c (extract_muldiv_1): Make sure to not introduce
new undefined integer overflow.
(fold_binary): Avoid useless conversion.

* gcc.c-torture/execute/pr33779-1.c: New testcase.
* gcc.c-torture/execute/pr33779-2.c: Likewise.

From-SVN: r129796

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

index 2808ae787c321facce096ce7724b98dfc8f58324..9e46938b0d0b4950ba6e2a01acb7196bbccd2afb 100644 (file)
@@ -1,3 +1,10 @@
+2007-10-31  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/33779
+       * fold-const.c (extract_muldiv_1): Make sure to not introduce
+       new undefined integer overflow.
+       (fold_binary): Avoid useless conversion.
+
 2007-10-31  Richard Sandiford  <rsandifo@nildram.co.uk>
 
        PR target/33948
index 2598ec1e18fb779af0b82b74826f4b1517c5f183..a7d2756116b991cb82d0bbaa59867c1477db7db3 100644 (file)
@@ -6060,7 +6060,12 @@ extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
                 then we cannot pass through this conversion.  */
              || (code != MULT_EXPR
                  && (TYPE_UNSIGNED (ctype)
-                     != TYPE_UNSIGNED (TREE_TYPE (op0))))))
+                     != TYPE_UNSIGNED (TREE_TYPE (op0))))
+             /* ... or has undefined overflow while the converted to
+                type has not, we cannot do the operation in the inner type
+                as that would introduce undefined overflow.  */
+             || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
+                 && !TYPE_OVERFLOW_UNDEFINED (type))))
        break;
 
       /* Pass the constant down and see if we can make a simplification.  If
@@ -10266,9 +10271,7 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
 
          strict_overflow_p = false;
          if (TREE_CODE (arg1) == INTEGER_CST
-             && 0 != (tem = extract_muldiv (op0,
-                                            fold_convert (type, arg1),
-                                            code, NULL_TREE,
+             && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
                                             &strict_overflow_p)))
            {
              if (strict_overflow_p)
index 59ae5ef3d10fc312dc08e65dea579858dd6fdce5..385b384e4c64faaf239b4ca1d34397fec2f14834 100644 (file)
@@ -1,3 +1,9 @@
+2007-10-31  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/33779
+       * gcc.c-torture/execute/pr33779-1.c: New testcase.
+       * gcc.c-torture/execute/pr33779-2.c: Likewise.
+
 2007-10-31  Paul Thomas  <pault@gcc.gnu.org>
 
        PR fortran/33897
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr33779-1.c b/gcc/testsuite/gcc.c-torture/execute/pr33779-1.c
new file mode 100644 (file)
index 0000000..da08e01
--- /dev/null
@@ -0,0 +1,14 @@
+int foo(int i)
+{
+  if (((unsigned)(i + 1)) * 4 == 0)
+    return 1;
+  return 0;
+}
+
+extern void abort(void);
+int main()
+{
+  if (foo(0x3fffffff) == 0)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr33779-2.c b/gcc/testsuite/gcc.c-torture/execute/pr33779-2.c
new file mode 100644 (file)
index 0000000..16c34b6
--- /dev/null
@@ -0,0 +1,12 @@
+int foo(int i)
+{
+  return ((int)((unsigned)(i + 1) * 4)) / 4;
+}
+
+extern void abort(void);
+int main()
+{
+  if (foo(0x3fffffff) != 0)
+    abort ();
+  return 0;
+}