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

    <tr>
        <th>Summary</th>
        <td>
            Unused asm blocks do not get optimized away
        </td>
    </tr>

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

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

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

<pre>
    Compiling

`ta.cpp`

```c++
int main()
{
  int val = 42;
  asm("movl %[val], %%eax"
    ::[val]"r"(val):"%eax");
  return 0;
}

```
the given `asm` block is redundant (just moves a register value to EAX without using it).

Building with `clang++ ta.cpp -o test.s -O3 -S` gives

```asm
main:                                   # @main
# %bb.0:
        movl    $42, %ecx
        #APP
        movl    %ecx, %eax
        movl    %ecx, %eax
        movl    %ecx, %eax
        movl    %ecx, %eax
        movl    %ecx, %eax

        #NO_APP
        xorl    %eax, %eax
        retq
                                        # -- End function
        .addrsig

```

I was rather expecting the generated code would instead just be

```asm
main:                                   # @main
        xorl    %eax, %eax
        retq
```

because I did not declare the `asm` block to be `volatile`. Is there an extra optimization flag that I might be missing?

GCC piece on asm volatile:

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Volatile
```
6.47.2.1 Volatile

GCC’s optimizers sometimes discard asm statements if they determine there is no need for the output variables.

[without volatile, ]  optimizers can discard the asm statement, which in turn removes the need for the entire [...]
routine. By omitting the volatile qualifier when it isn’t needed you allow the optimizers to produce the most
efficient code possible. 
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVs1u4zYQfhr6MrAgU5ZtHXyI47jIpbtA0aK3giLHElOK9JJD2-nTF6T8k00NtD0UqCEo4Px-83FmFBGC7izimtUbVm8nIlLv_Potvk1ap97Xz244aKNtx8otK58u70VJopCHA1uUn-TjIxnfpCdLtSUYhLaMrxhvLpbLixIgqY_CAKu2MOesuilEGLILH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