Issue 55524
Summary Failed loop simplification by -O3 (trunk v.s. 11.0.1)
Labels new issue
Assignees
Reporter shao-hua-li
    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
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to