https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101653
Bug ID: 101653 Summary: Bad code generated when optimizing nested for loops Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: phd at phd dot re Target Milestone: --- Not sure if this was already reported. Bad code is generated when optimizing nested for loops: int main(void) { unsigned a, b, c = 0; // issue only occurs for unsigned for (a = 0; a < 10; a++) // upper limit for variable a doesn't really matter { for (b = 0; b < 2; b++) // bug only for b < 2, no issue for other limits { c++; // first iteration: a == 0, b == 0, c == 1 if (c < a) // c will *never* be smaller than a from now on { return 123; // and yet this is somehow reachable with -O1/2/3 } } } return 0; // with -O0 this returns 0 properly } Affected gcc versions: 9.1+ (including 11.1 and trunk) Not affected versions: 8.5- Bug occurs for -O1, -O2, -O3 (return 123) No issue for -O0 (properly returns 0) Both C and C++ are affected. Live demo: https://godbolt.org/z/nanc9osq5