<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60622>60622</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Over aggresively optimization on infinite loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          FrankHB
      </td>
    </tr>
</table>

<pre>
    # Case

```c++
// a.cc
#include <iostream>

int main()
{
    while(true)
        ; 
}

void unreachable()
{
    std::cout << "Hello world!" << std::endl;
}
```

# Reproduction
```sh
clang++ -O1 a.cc
./a.out
```

This will show "Hello world!".

Tested on [Godbolt](https://gcc.godbolt.org/z/Gehn483zh).

# Discussions

It might be well-known that [ISO C++ permit an implementation to remove infinite loop without any further reasoning and proof of the termination under the abstraction machine semantics](https://eel.is/c++draft/intro.progress#1). However, there is no wording about assuming it having undefined behavior, so the transformation is limited. The empty loop can be removed, but it should not introduce any differences on [observable behavior](https://eel.is/c++draft/intro.compliance#intro.abstract-5) besides the removal of the loop and such difference (if any; not here because there is no code after the loop) should be considered *unspecifed* but not undefined. In this case, it is valid to transform the translation unit as if it consists of `int main(){}`, but not with the call to `unreachable`.

# Related

A similar issue #60419  was reported. Besides not that "minimal" in its case, the root cause seems the same. However, there are some other subtleties.

The [reply](https://github.com/llvm/llvm-project/issues/60419#issuecomment-1410486119) for that issue is technically incorrect. First, it refers to a proposal of WG14, not the standard; second, it is about C, but C and C++ are still different here, even the [original paper (WG14 N1528)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm) do have concerns on liaison to WG21.

C and C++ are different in the meta level here. In ISO C, if something the language rules should have specified is actually underspecified, it is undefined, as per Clause 4 (same in many editions):

> If  a  ‘‘shall’’ or ‘‘shall  not’’ requirement  that  appears  outside  of  a  constraint  or  runtime-constraint  is  violated,  the  behavior  is  undefined.   Undefined  behavior  is  otherwise indicated  in  this  International  Standard  by  the  words  ‘‘undefined  behavior’’  or  by  the omission of any explicit definition of behavior.  There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’.

As WG14 N1528 has clarified, the permission of removal the empty loops is granted, any further side effects of the unexpected change of the transfomation should not be depend on. Otherwise, it is undefined by "omission of any explicit definition or behavior" in mind. Despite the belief, it is still confusing and potentionlly debatable, especially when compared to ISO C++ rules. Note the transformation on the original case in [WG14 N1509](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1509.pdf) as well as this case is permitted, but it does not have to rely any undefined behavior in C, because the remained program does not contribute to any observable behaviors.

There is no such equivalent rules in ISO C++. Moreover, it should be clear the meaning of "may" in [the C++ rule](https://eel.is/c++draft/basic.exec#intro.progress-1) is clear as per [Table 5 in ISO/IEC directives, part 2](https://www.iso.org/sites/directives/current/part2/index.xhtml#_idTextAnchor090). That is, there is nothing to do with voiding the other requirements on a conforming implementation. As a result, the compiler cannot assume anything as it is undefined. Hence, it is a bug of Clang++. #60419 should be marked "duplicate" rather than "invalid".

Currently G++ does not have the same issue. It simply does not do the transformation. This is about missing-of-optimization, but not conformance.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWE9v4zjy_TTMpRBBpmPHPuQQO-3uHH7TwEz_MMcFRZasmqFILUnZSX_6RVGS_ySexQ52B8HEkegi69WrV4-tYqS9Q3wSi41YvNypPjU-PO2Ccn9-29xV3rw_CTmHrYooyhdRPo__X5bDjxZywz_DU7kTcgeq0Hp6MCenbW8QxHxLPqaAqhXzL5exyCVoFTkhV0KuxzePY0gAgGNDFoVcpdDjaQWM_4n5BqbvvFyGPXgy0LuASjeqygFuh4_JiPmzmD9r3yc-p5hvQUj5Da31cPTBGiFnQsrp3ekL6IwV883H7SdsrgCTc_gVu-BNrxN592FpbIYH2iq3HyCF---zCygLIXeq8H36N5v8aCjCkayF2PjjzRyKq_UYExrwDsRi89WbytskFi9CrpqUushJ5orutS72w-vCh72Qu59C7r5i4x5W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