<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56061>56061</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-x86 Missed optimizations with adding and multiplying by constants
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AMS21
</td>
</tr>
</table>
<pre>
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)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1Vl1vmzAU_TXwYjUC8_3AQ7J-SJs6TdukPFbGdoJbgxE2adJfv2tS1KTNFpJmEcHGvvfcc2yuuYVim3xODC1FvURfiVY1-t3VvNXIiWYrwbhyomsHp6UxjXaCqYNv4dqoznSTgkP3-83DKvzpsx_3MJch3ZD2iTNUbZCoDW-5NojUDEk7xpFRiKqqEZIjgqSASbVAfE2qRnKNnktwQFQS4LLkwIIYGK0UDIpam7ajRqhaI1OSGi0pRQvVAo4W1n2AmTjeteNNt_c7sYKgJQdLKdWzFUkV41bI1ir2thdtmu0IsIbY5mFFZMcfMEi3I2urzUlmqOWma2sUYuTgGVrDfYqwE8xg8no38D5MMAYmOAoTjYGJjsLEY2DiozDZGJjsKIzvjcEBqz2gYd92cd9emf0dJ1rzqpCbj7tOdDW4vttx4ALm6PDPwQFyQu-gUw83GEpO-pYTEPfFplTLRK8KWptYe9aEsV3rEO9Pw7K8JxucQzY4mSzIm16GcHQO4ehkwuHFCMfnEI7_1wrb5pOCsnMEZScLSi-2A8P58C_GHwjvOl32nTl7Cw4eWfPSfgrtp-zyh9cI4aAb-OLZId3HjpsT4fEUjwkRfSpEOCZEfHqIvwk4YG97r5SgO4JO9inF6ZgQ-yl0iuSPC3q-5IMJAP5LxQolDZSD9dOhYvN1fqLaJTy9wJ-3327Su8f5AgS5LA9YFmTENcJInvfV49U6jdG9gNRhSDVGVOKFbEvHZ2FKm7J9akFdWnXSiEZu7HOxgeIQ6kxSG-12rczf8QDXrphA_QoPUq6G5qpp1SOnsLq3ELHjGjpR7MW-W-Y0wbgoaMIx9aIk9EJKsoLFieenIU2zhStJwaXOYRlAuity7GEMrrGX4cT3JjT0giL1CAtxWvg8gUOOV0TIiQ1sV8Rt855D0S01TNqSWr9NwuEhljXnAz7pTKnafHr_C_tuTzbvmf4BFu0-TQ">