public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/114052] New: Wrong code at -O2 for well-defined infinite loop
@ 2024-02-22 10:46 maxdamantus at gmail dot com
  2024-02-22 10:56 ` [Bug c/114052] [11/12/13/14 Regression] " jakub at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: maxdamantus at gmail dot com @ 2024-02-22 10:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114052

            Bug ID: 114052
           Summary: Wrong code at -O2 for well-defined infinite loop
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: maxdamantus at gmail dot com
  Target Milestone: ---

int foo(void) {
        int counter = 0;
        while (1) {
                if(counter >= 2)
                        continue;
                printf("%i\n", counter++);
        }
        return 0;
}

This is actually from a reddit post I came across:
https://www.reddit.com/r/C_Programming/comments/yugk1y/gcc_optimization_behavior/

There are theories in the comments above about UB due to integer overflow or
non-terminating loops, but I'm pretty sure they don't apply (correct me if I'm
wrong).

The controlling expression of the loop is a constant expression, so it can't be
assumed to terminate, and the loop should repeat infinitely with `counter ==
2`, so no overflow should occur. Instead, part of the loop is presumably DCEd,
so control continues outside of the function.

Versions before gcc-13 had slightly different incorrect behaviours occur when
replacing `2` in the condition with `3`, `4`, `5`, etc. These larger numbers
appear to behave correctly now.

godbolt link:
https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAJlIOAMngMmABy7gBGmMQSAJykAA6oCoS2DM5uHnoJSTYCAUGhLBFRXFwWmFY5DEIETMQEae6ePpaY1inVtQR5IeGRMRY1dQ0ZpQqDXYE9hX0lAJQWqK7EyOwcAKReAMyByG5YANRr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reproducible from git
version, `basepoints/gcc-14-9077-g52490278466`.

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

end of thread, other threads:[~2024-04-05 12:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-22 10:46 [Bug c/114052] New: Wrong code at -O2 for well-defined infinite loop maxdamantus at gmail dot com
2024-02-22 10:56 ` [Bug c/114052] [11/12/13/14 Regression] " jakub at gcc dot gnu.org
2024-02-22 10:56 ` jakub at gcc dot gnu.org
2024-02-22 11:11 ` sjames at gcc dot gnu.org
2024-02-22 12:18 ` [Bug tree-optimization/114052] " rguenth at gcc dot gnu.org
2024-02-22 13:08 ` rguenth at gcc dot gnu.org
2024-02-22 13:54 ` hubicka at gcc dot gnu.org
2024-02-22 14:08 ` rguenth at gcc dot gnu.org
2024-02-22 14:20 ` hubicka at ucw dot cz
2024-04-05 12:03 ` rguenth 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).