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

    <tr>
        <th>Summary</th>
        <td>
            [x86] Clang >= 13 strips function empty and allows fallthrough in generated Assembly
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            c++,
            clang:codegen
      </td>
    </tr>

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

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

<pre>
    Given the following (arguably sketchy, because it violates (AFAIK) the *"forward progress"* assumption) example code:

```cpp
#include <iostream>

int main()
{
  while (1);
  return 0;
}

int unreachable()
{
  std::cout << "Hello!" << std::endl;
  return 1;
}
```

[Clang 13, 14, and 15 are all affected.](http://godbolt.org/z/nG9qfoYPx) It seems [G++ versions don't "see through" this optimisation "opportunity", even at **`-O3`** keeping the infinite loop](http://godbolt.org/z/4frx4qnno).

At optimisation levels **`-O1`** and higher, the resulting generated Assembly body for `main` is completely empty. The label itself is kept, allowing execution to flow immediately to whatever instruction happens to be following the label. It might even be an invalid instruction or an invalid state (such as [the body of a function expecting parameters on the stack while an emptied parameter-less function was called](http://godbolt.org/z/Wj5aK7n8b)).

```asm
main: # @main
unreachable:                 # @unreachable()
 push     rbx
    mov      edi, offset std::cout
    # ...
```

This is problematic because a dangling label remains in the object file whose contents are poorly controlled. Perhaps Clang should, instead, when realising that a function's body is completely emptied, omit even the label (and thus catch such "sketchy codes" with a potential linker error) or leave an instruction in the body that kills the program when reached?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVk9z2zoO_zT0BROPTPnvwQcnWXc7PWwPndnZIyVBIhuKVAnIjvfTvwHlxE77-qaZjCKREATg94cxRK4LiHu1elSr55kZ2ca0P1tHAybHl1kVm8v-kzthALYIbfQ-nl3oQOmtSd1oKn8BekGu7UXpJ6iwNiMhOIaTi94wkoQejofPX5Te5SRKH5TWbU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