<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/55524>55524</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Failed loop simplification by -O3 (trunk v.s. 11.0.1)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
shao-hua-li
</td>
</tr>
</table>
<pre>
For the following code, clang trunk -O3 failed to optimize the loop and generated a lot of redundant instructions. You can see that clang-11.0.1 generated a much simpler assembly code.
```c
int a=0;
volatile int b[1] = {1};
int main() {
for (; a >= 0; a--)
b[0];
return 0;
}
```
```shell
$ clang-trunk -O3 a.c -S -o a-trunk.s
$ clang-11 -O3 a.c -S -o a-11.s
$ wc a-trunk.s a-11.s
73 187 1508 a-trunk.s
48 117 1047 a-11.s
121 304 2555 total
```
#### a-11.s
```asm
main:
movl a(%rip), %eax
testl %eax, %eax
js .LBB0_4
addl $1, %eax
.LBB0_2:
movl b(%rip), %ecx
addl $-1, %eax
testl %eax, %eax
jg .LBB0_2
movl $-1, a(%rip)
.LBB0_4:
xorl %eax, %eax
retq
```
#### a-trunk.s
```asm
main:
movl a(%rip), %eax
testl %eax, %eax
js .LBB0_9
leal 1(%rax), %edx
movl %eax, %ecx
andl $7, %edx
je .LBB0_5
xorl %esi, %esi
.LBB0_3:
movl b(%rip), %ecx
incl %esi
cmpl %esi, %edx
jne .LBB0_3
movl %eax, %ecx
subl %esi, %ecx
.LBB0_5:
cmpl $7, %eax
jb .LBB0_8
incl %ecx
.LBB0_7:
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
movl b(%rip), %eax
addl $-8, %ecx
jne .LBB0_7
.LBB0_8:
movl $-1, a(%rip)
.LBB0_9:
xorl %eax, %eax
retq
```
</pre>
<img width="1px" height="1px" alt=""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