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

            Bug ID: 128819
           Summary: `(zero_one + CST) >> N)` can be optimized to either
                    `CST>>N` or `(CST>>N) + zero_one`
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

take:
```
unsigned src(unsigned a)
{
    return ((a&1)+9)>>1;
}

unsigned tgt(unsigned a)
{
    return (a&1)+4;
}
unsigned src1(unsigned a)
{
    return ((a&1)+8)>>1;
}

unsigned tgt1(unsigned a)
{
    return 4;
}
```

src should be able to transform over to tgt.
In the case of 9, there is a carry bit that is needed still; hence the `a&1`
term is still needed.

This is true for `(zero_one + CST)>>N` -> `zero_one + (CST>>N)` where
`CST&((1<<N)-1)` is a lower mask.
For the false case then it is just `CST>>N` as the zero_one does not cause a
carry outside of the first N bits.

Reply via email to