Issue 56061
Summary clang-x86 Missed optimizations with adding and multiplying by constants
Labels
Assignees
Reporter AMS21
    Watching Jason Tuners [video](https://youtu.be/NE_v4R1dPMA) sparked my interest and led me to compile a list of examples where clang generates more instructions than gcc for a simple example.

Give the following code:
```cpp
int get_value_2(int x) { return 42 + x * 2; }

int get_value_3(int x) { return 42 + x * 3; }

int get_value_5(int x) { return 42 + x * 5; }

int get_value_6(int x) { return 42 + x * 6; }

int get_value_9(int x) { return 42 + x * 9; }

int get_value_10(int x) { return 42 + x * 10; }
```

generates the following assembly:
```asm
get_value_2(int):                       # @get_value_2(int)
        lea     eax, [rdi + rdi]
        add     eax, 42
        ret
get_value_3(int):                       # @get_value_3(int)
        lea     eax, [rdi + 2*rdi]
        add     eax, 42
        ret
get_value_5(int):                       # @get_value_5(int)
        lea     eax, [rdi + 4*rdi]
        add     eax, 42
        ret
get_value_6(int):                       # @get_value_6(int)
        lea     eax, [rdi + 2*rdi]
        add     eax, eax
        add     eax, 42
        ret
get_value_9(int):                       # @get_value_9(int)
        lea     eax, [rdi + 8*rdi]
        add     eax, 42
        ret
get_value_10(int):                      # @get_value_10(int)
        lea     eax, [rdi + 4*rdi]
        add     eax, eax
        add     eax, 42
        ret
```

While gcc generates the following assembly:
```asm
get_value_2(int):
        lea     eax, [rdi+42+rdi]
        ret
get_value_3(int):
        lea     eax, [rdi+42+rdi*2]
        ret
get_value_5(int):
        lea     eax, [rdi+42+rdi*4]
        ret
get_value_6(int):
        lea     eax, [rdi+rdi*2]
        lea     eax, [rax+42+rax]
        ret
get_value_9(int):
        lea     eax, [rdi+42+rdi*8]
        ret
get_value_10(int):
        lea     eax, [rdi+rdi*4]
        lea     eax, [rax+42+rax]
        ret
```

[godbolt link](https://godbolt.org/z/erKE8GjWf)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to