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

            Bug ID: 108737
           Summary: Apparent miscompile of infinite loop on gcc trunk in
                    cddce2 pass
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: njs at pobox dot com
  Target Milestone: ---

There's a meme going around about strange compilation outputs caused
infinite-loop-related UB, and while discussing it a friend stumbled on this
weird behavior:

https://godbolt.org/z/qfj1jabMW

C++ code is:

extern int foo();

void blah() {
    int x = foo();
    while (1) {
        if(x) foo();
    }
}


When compiled with whatever "gcc trunk" means on compiler explorer, and with
-O3 (but not -O2), as C++ (but not C), then gcc reduces this to a single call
to `foo()` followed by an empty infinite loop:

blah():
        sub     rsp, 8
        call    foo()
.L2:
        jmp     .L2

As far as I can tell, there's no UB here -- 'foo' being extern means it might
do anything, and infinite loops with side-effects are well-defined in C++. So
this seems like a straight-up optimizer bug?

Poking through the pass analysis in CE, it looks like the output of "unrolljam
(tree)" is still correct, but then the output of the next pass "cddce2 (tree)"
has deleted everything. Somehow it concludes that the hoisted `if` can be
eliminated? idgi.

Reply via email to