Issue 55468
Summary Missed optimization: Bitwise shifts don't backpropagate knowledge about their operands
Labels
Assignees
Reporter josephcsible
    Consider this C code:
```c
extern int x;
int f(int y) {
    x = y % 32;
    return 1 << y;
}
```
At `-O3`, it compiles to this assembly:
```asm
f:                                      # @f
        movl    %edi, %ecx
        leal    31(%rcx), %eax
        testl   %edi, %edi
        cmovnsl %edi, %eax
        andl    $-32, %eax
        movl    %edi, %edx
        subl    %eax, %edx
        movl    %edx, x(%rip)
        movl    $1, %eax
        shll    %cl, %eax
        retq
```
But this would have been more efficient:
```asm
f:                                      # @f
        movl    %edi, %ecx
        movl    %edi, x(%rip)
        movl    $1, %eax
        shll    %cl, %eax
        retq
```
If I manually put `__builtin_assume(y >= 0 && y < 32);` right before `return 1 << y;`, then I do get that more efficient assembly. But we should automatically be assuming that, since `y` gets used as the right operand of a bitwise shift operator.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to