public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/101653] New: Bad code generated when optimizing nested for loops
@ 2021-07-27 22:48 phd at phd dot re
  2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: phd at phd dot re @ 2021-07-27 22:48 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-07-27 23:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-27 22:48 [Bug c/101653] New: Bad code generated when optimizing nested for loops phd at phd dot re
2021-07-27 22:55 ` [Bug tree-optimization/101653] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2021-07-27 23:01 ` jakub at gcc dot gnu.org
2021-07-27 23:09 ` pinskia at gcc dot gnu.org
2021-07-27 23:13 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).