File pr34130.patch of Package gcc41
2007-11-17 Richard Guenther <rguenther@suse.de>
PR middle-end/34130
* fold-const.c (extract_muldiv_1): Do not move negative
constants inside ABS_EXPR.
* gcc.c-torture/execute/pr34130.c: New testcase.
Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c.orig 2009-11-20 13:41:15.000000000 +0100
+++ gcc/fold-const.c 2009-11-20 13:41:37.000000000 +0100
@@ -5339,6 +5339,9 @@ extract_muldiv_1 (tree t, tree c, enum t
}
break;
}
+ /* If the constant is negative, we cannot simplify this. */
+ if (tree_int_cst_sgn (c) == -1)
+ break;
/* FALLTHROUGH */
case NEGATE_EXPR:
if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
Index: gcc/testsuite/gcc.c-torture/execute/pr34130.c
===================================================================
--- /dev/null 1970-01-01 00:00:00.000000000 +0000
+++ gcc/testsuite/gcc.c-torture/execute/pr34130.c 2009-11-20 13:41:37.000000000 +0100
@@ -0,0 +1,12 @@
+extern void abort (void);
+int foo (int i)
+{
+ return -2 * __builtin_abs(i - 2);
+}
+int main()
+{
+ if (foo(1) != -2
+ || foo(3) != -2)
+ abort ();
+ return 0;
+}