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

            Bug ID: 127221
           Summary: (A &mask) + (B & mask) -> (A+B) & mask for unsigned
                    types or no overflow possible for `A+B`
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
unsigned ff(unsigned f, unsigned m)
{
  unsigned t = m;
  t&=1;
  f&=1;
  return t+f;
}
unsigned ff1(unsigned f, unsigned m)
{
  unsigned t = m;
  return (t+f) & 1;
}
```
ff can be optimized to ff1.


This is because there is no carry in for the lowest bit so the add does not
change the lower bit.  This works with any lower full mask; that is
bits0...bitsN is set.


This can be used to optimize:
```
unsigned g(unsigned f)
{
  unsigned t = ~f;
  t&=1;
  f&=1;
  return t+f;
}
unsigned g1(unsigned f)
{
  unsigned t = ~f;
  return (t+f) & 1;
}
```

I don't know if we should do this for RTL level or gimple or both.

Reply via email to