https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112629

            Bug ID: 112629
           Summary: Missed-optimization: constant << (b + signed_constant)
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

The following C code:

```
int add_shift(signed shift) {
    return 1 << (shift + 4);
}

add_shift:
        lea     ecx, [rdi+4]
        mov     eax, 1
        sal     eax, cl
        ret
```

Could be optimized to:

```
int optimized_shift(signed shift) {
    return 16 << shift;
}

optimized_shift:
        mov     ecx, edi
        mov     eax, 16
        sal     eax, cl
        ret
```

We always know we are shifting by at least 4, since the added value is a signed
integer and overflow is undefined. Also note that this optimization works the
same for right shifts.

Reply via email to