[Bug tree-optimization/126761] New: missing detection of umin(a,~c) with __builtin_add_overflow

"pinskia at gcc dot gnu.org via Gcc-bugs" <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.