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

            Bug ID: 126761
           Summary: missing detection of umin(a,~c) with
                    __builtin_add_overflow
           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 f(unsigned a, unsigned c)
{
    unsigned add;
    if (__builtin_add_overflow (a, c, &add))
      add = -1u;
    add -= c;
    return add;
}
```

This is the same as doing Sat.add(a,c) - c. when a+c overflows it becomes
UINT_MAX - c which is the same as doing ~c. So this is umin(a,~c).

Note cst being being 0 or -1u is already handled. But we can handle the
non-constant cast.

Reply via email to