Issue 64729
Summary Negative effects of subtraction on optimization
Labels new issue
Assignees
Reporter ZY546
    Hello, I've noticed that there are often missed optimizations that occur when there is a subtraction or a minus sign in an _expression_. For example, in the following example, `var_4 + var_1` should ideally be computed only once:

https://godbolt.org/z/dPa75Enaq
```c
extern int var_23;
extern int var_24;

void test(int var_1, int var_3, int var_4) {
    var_23 = var_4 + var_1;
    var_24 = -var_3 + var_4 + var_1;
}
```
clang16 -O3:
```asm
test(int, int, int): # @test(int, int, int)
        lea     eax, [rdx + rdi]
        mov     rcx, qword ptr [rip + var_23@GOTPCREL]
        mov dword ptr [rcx], eax
        sub     edi, esi
        add     edi, edx
        mov     rax, qword ptr [rip + var_24@GOTPCREL]
        mov dword ptr [rax], edi
        ret
```
```
define dso_local void @test(int, int, int)(i32 noundef %var_1, i32 noundef %var_3, i32 noundef %var_4) local_unnamed_addr {
entry:
  %add = add nsw i32 %var_4, %var_1
  store i32 %add, ptr @var_23, align 4
  %add1 = sub i32 %var_1, %var_3
  %add2 = add i32 %add1, %var_4
  store i32 %add2, ptr @var_24, align 4
  ret void
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to