From: Richard Guenther Date: Wed, 3 Aug 2011 13:33:28 +0000 (+0000) Subject: re PR middle-end/49958 (fold performs invalid association) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a130fb24411061b0edf14203bd76ff1c3aad89b8;p=gcc.git re PR middle-end/49958 (fold performs invalid association) 2011-08-03 Richard Guenther PR middle-end/49958 * fold-const.c (fold_binary_loc): Only associate (+ (+ (* a b) c) (* d e)) as (+ (+ (* a b) (* d e)) c) if overflow wraps. * gcc.dg/torture/pr49958.c: New testcase. From-SVN: r177270 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 72902da53dd..886b360da4f 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2011-08-03 Richard Guenther + + PR middle-end/49958 + * fold-const.c (fold_binary_loc): Only associate + (+ (+ (* a b) c) (* d e)) as (+ (+ (* a b) (* d e)) c) if + overflow wraps. + 2011-08-03 Alan Modra PR rtl-optimization/49941 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 8ad806f9eee..0cdf682ae38 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -9711,12 +9711,13 @@ fold_binary_loc (location_t loc, /* Reassociate (plus (plus (mult) (foo)) (mult)) as (plus (plus (mult) (mult)) (foo)) so that we can take advantage of the factoring cases below. */ - if (((TREE_CODE (arg0) == PLUS_EXPR - || TREE_CODE (arg0) == MINUS_EXPR) - && TREE_CODE (arg1) == MULT_EXPR) - || ((TREE_CODE (arg1) == PLUS_EXPR - || TREE_CODE (arg1) == MINUS_EXPR) - && TREE_CODE (arg0) == MULT_EXPR)) + if (TYPE_OVERFLOW_WRAPS (type) + && (((TREE_CODE (arg0) == PLUS_EXPR + || TREE_CODE (arg0) == MINUS_EXPR) + && TREE_CODE (arg1) == MULT_EXPR) + || ((TREE_CODE (arg1) == PLUS_EXPR + || TREE_CODE (arg1) == MINUS_EXPR) + && TREE_CODE (arg0) == MULT_EXPR))) { tree parg0, parg1, parg, marg; enum tree_code pcode; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e16b1a07d56..1e70bbdfe06 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-08-03 Richard Guenther + + PR middle-end/49958 + * gcc.dg/torture/pr49958.c: New testcase. + 2011-08-03 Richard Guenther PR tree-optimization/49938 diff --git a/gcc/testsuite/gcc.dg/torture/pr49958.c b/gcc/testsuite/gcc.dg/torture/pr49958.c new file mode 100644 index 00000000000..de689bcde50 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr49958.c @@ -0,0 +1,11 @@ +/* { dg-do run } */ +/* { dg-options "-fstrict-overflow" } */ + +extern void abort (void); +int foo (int i, int j, int o, int m) { return i*o + 1 + j*m > 1; } +int main() +{ + if (foo (- __INT_MAX__ - 1, -1, 1, 1)) + abort (); + return 0; +}