<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55468>55468</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: Bitwise shifts don't backpropagate knowledge about their operands
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          josephcsible
      </td>
    </tr>
</table>

<pre>
    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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzFVE1vozAQ_TXmMmoEGPJx4NAkW6mH1f6EyoYB3BqbxaZJ9tfvGJJ2mzbS3mo5wvY8v3njmYm01anYWeNUhQP4VjnYQWkrZPyexXsW37NlPM9y3uPR42BAGQ9HxrfzYdjVLF2H74mlG2CrswVoHIHxPZyApTnw9O1SMA3oR2JLCLGjSZcvVrbaXwmYt_ceaH33i4eTdAfKk96uVxodeDuHIJzDTurT5yCE6-aTmmzwX4OlHFgW1--iw-jsq56tOVYqCAmr8vgRpVFMKJ7Q4xBgIEC6uaDFFdqj8_oTJ60-oEpybZy-Ql1zCVOd9WV39OY3UF9HUV2h3CjfUMRwA_WBa0Idz0GrPgR9A5wlt8S5Vl8IS30LRAX0-8s62Y5-LoaDHXUFrXhFkIiGXA8IWNeqVGj8t5fIZ9S3PttjDY_QCTMKrU_Qj1O3PT3JUWmvzBN11tghCaRu5j9CW8fEv6QZ-psaONTaJjTxMoZBNa2nR6_Di9PBl80-d7FvKTOPUFloMORN-Ks8vfX0AkJmD0hxTokVo7ed8KqcBEuESaIyzcQSuJ0y5eT_FEQRv4PRId10we1Zpe1xoJ4BW4MAqfxBueBC1WeTt8MiqgpebfhGRF55jcVP5QKP7b3q1B_SYE0ome2_tx3FZFi6oncQ5Us_2F40wiO8GHvQWDWkV9qpVFENFxUuGgddtN73LtRn-kCzUb4d5YL-62ij9evlc0ecz1hSpA8kZ0RHizzPluuoLUqxrtecy3WSyazO45pnmzoRKEqZ0W8VaSFRu4LlW5bvI1WkcZrGeZLHWZJn8SJL1vk6qQVFvUolcqpx7ITSi-B4YYcmGopJgxwbR0atnHfvRsqEagzihZ9S1dqheLYO-7Z0SmqMJs3FJPgviKXOLg">