Take this valid testcase: int main () { static int a[] = { 0, 1, 2 }; int *i = a+3; if (i-- > a) return 0; return 0; } In the IR (in .orginal) we get: if ( --i > &a - 4B) which is incorrect and not really defined.
Note: I found this while implementing PTR_PLUS_EXPR but that is because I did not fix the part which does this optimization.
The comment from the code: For pointer types we assume overflow doesn't happen. That is true but you can cause an overflow to happen when you transform the code this way. Mine, I have a fix.
Before we would produce: i = &a + 12B; i = i - 4B; D.1616 = &a - 4B; if (i > D.1616) Now we produce: i = &a + 12B; D.1620 = i > &a; i = i - 4B; if (D.1620) Which is better because we can actually fold it down to return 0.
Subject: Bug 29715 Author: pinskia Date: Wed May 2 17:47:50 2007 New Revision: 124354 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=124354 Log: Forgot to add the PR number to the last changelog entry: 2007-05-02 Andrew Pinski <andrew_pinski@playstation.sony.com> PR middle-end/29715 * fold-const.c (fold_comparision): Remove the "foo++ == CONST" transformation. Modified: trunk/gcc/ChangeLog
Fixed.