| Issue |
115770
|
| Summary |
Missed optimisation: conditional shift by power-of-two can be replaced with a shift by shifted flag
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
AnthonyMikh
|
Consider this code ([godbolt](https://godbolt.org/z/8on1GdYK9)):
```c
int nibble_to_byte(int nibble, _Bool is_high) {
if (is_high) {
return nibble << 4;
} else {
return nibble;
}
}
int nibble_to_byte2(int nibble, _Bool is_high) {
return nibble << (is_high << 2);
}
```
I expected the compiler to compile both functions the same way. Instead, clang 19.1.0 generates the following asm:
```
nibble_to_byte:
mov eax, edi
shl eax, 4
test esi, esi
cmove eax, edi
ret
nibble_to_byte2:
mov eax, edi
lea ecx, [4*rsi]
shl eax, cl
ret
```
I believe that the second version is more optimal since it touches less bits in flags register and thus is more amendable to instruction reordering in processor.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs