-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missed simplifications when shl is used instead of mul #46928
Comments
Look like this is a case where we miss hoisting the common operand from the multiplications when |
rewriting as: int foo2(int p) gives: _Z4foo2i: |
Interesting, so failed loop unswitch for original test case? |
Instcombine is definitely missing a factorization for shl with add/sub. I was looking at that fold as part of solving bug 47430. I added some tests here: Let me see if adding that fixes the example in the description. |
With the instcombine improvement, the original example collapses to the optimal code: |
Extended Description
int foo(int p)
{
unsigned i, s = 0;
for (i = 4; i < 20; i++)
{
if (p)
s += i2; // works for i3
else
s += i*4;
}
return s;
}
Clang produces:
foo(int): # @foo(int)
cmp edi, 1
mov cl, 1
adc cl, 0
mov eax, 4
shl eax, cl
mov edx, 5
shl edx, cl
mov esi, 6
shl esi, cl
add edx, eax
add esi, edx
mov eax, 7
shl eax, cl
add eax, esi
mov edx, 8
shl edx, cl
mov esi, 9
shl esi, cl
mov edi, 10
shl edi, cl
add edx, eax
add esi, edx
....
But it should just produce:
foo(int): # @foo(int)
test edi, edi
mov ecx, 736
mov eax, 368
cmove eax, ecx
ret
The text was updated successfully, but these errors were encountered: