<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56595>56595</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang x86: Unnecessary mov instruction added from local variable?
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
KJP12
</td>
</tr>
</table>
<pre>
## Used to compile
- Source: https://godbolt.org/z/ra41TnPTY (Also under the `input.c` block)
- Use Clang Trunk or 14.0.0 with any of `-O2`, `-O3` or `-Os`
- `clang` and `clang++` both produce this issue.
## Expected Results
All of `a(I)`, `b(I)` and `c(I)` to not have an unnecessary `mov` instruction.
## Actual Results
`b(I)` has an unnecessary `mov` instruction at the beginning, despite the rest of the assembly being reordered copies of `a(I)` and `c(I)`, which do *not* have the unnecessary `mov` instruction.
## Additional Information
<details><summary>input.c</summary>
```c
int a(int i) {
return (i >> 7) + ((i & 127) != 0);
}
int b(int i) {
int a = 0;
if((i & 127) != 0) a = 1;
return (i >> 7) + a;
}
int c(int i) {
int a = i >> 7;
if((i & 127) != 0) a += 1;
return a;
}
```
</details>
<details><summary>objdump from Clang 14.0.6 targeting x86_64-pc-linux-gnu</summary>
```asm
0000000000001140 <a>:
1140: 89 f8 mov %edi,%eax
1142: c1 f8 07 sar $0x7,%eax
1145: 83 e7 7f and $0x7f,%edi
1148: 83 ff 01 cmp $0x1,%edi
114b: 83 d8 ff sbb $0xffffffff,%eax
114e: c3 ret
114f: 90 nop
0000000000001150 <b>:
1150: 89 f8 mov %edi,%eax
1152: 89 f9 mov %edi,%ecx
1154: 83 e1 7f and $0x7f,%ecx
1157: c1 f8 07 sar $0x7,%eax
115a: 83 f9 01 cmp $0x1,%ecx
115d: 83 d8 ff sbb $0xffffffff,%eax
1160: c3 ret
1161: 66 2e 0f 1f 84 00 00 cs nopw 0x0(%rax,%rax,1)
1168: 00 00 00
116b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1)
0000000000001170 <c>:
1170: 89 f8 mov %edi,%eax
1172: c1 f8 07 sar $0x7,%eax
1175: 83 e7 7f and $0x7f,%edi
1178: 83 ff 01 cmp $0x1,%edi
117b: 83 d8 ff sbb $0xffffffff,%eax
117e: c3 ret
```
</details>
This additionally can be reproduced by using Zig, as seen here: https://godbolt.org/z/zEcaadarx
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytV0uT2jgQ_jXm0gXlB7bMgQPDTKqye9jU7uSQvWzpZVBiJMqSZ5j8-m3JHrAZMiEpVMLo-fWn7pbUYka8LKM0wwyfrRTgDHCz26taRvF9FK-m8I9pGy6jbAVb5_YWC1H6AfPGCGZqNzPNBmvf8dfQefKoPz1-gSgtV7U10GohG3BbCVERK71v3YxjCVht-LcoXbzKQNGwrqnewGPT6m9gGkjms3gWw7NyW6D6BUzlMaZ_pfiN0nVXyTwYDg4V63t6QCxyj-f7qRanenrns6dgEHjfGNFyiQyVBWVtK2cdQv_tFPNw2EvuUDl_S9vWznadq7ruSVFc7ke_miMzdmo5ih80oZK1cbClTxK7UUtacmktbV78yJ158oOUtq5puVNGXyK14q6l9ZjSmeQttVfBA3XBRkxulNbKa2kNQtq9cjJ0NNI6v1ZfptbKHatfcDSOxC7ToI1ROdzslbRvVXJBAR7_eav4FoRBX1mhMvDb6cPL-D2FCKF8Hyrlo65Ms6O-1g_J1kI6qmp03wes2Ha3Q3CsvDpltkYHPjWP8JFwyLyrK-3Ar8__K1wQROSu6wFMjXRto_0OUBCEPQAJg9I739i1pwUkad-cRNk9xF4xWQ8TkfuhfC-H_VBeYAMdRjZsr96X1k9KRpPeI0_f48ev4DeA_FWiuGV_xPUSrVeDHY2Pth3Y_ycuYdhX0e72UDVm159K4TAqwNFmI513-0NZ_FfMp3s-rZVuD9ONbq9yIWp3XUs8SEkyj1E3axqYrE5L9B3-4O1SuYCqhLcJt4f_i9JcCtT_2hfoYYSSnlB44lFico6Cm61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