https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128474
Bug ID: 128474
Summary: transform `a/b >= CST` into `a >= b*CST` if `b*CST`
does not overflow (unsigned only?)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Take:
```
#define CST 2
unsigned src(unsigned long long a, unsigned y)
{
unsigned long long b = y;
unsigned long long d = a/b;
return d >= CST; // `d > (CST-1)`
}
unsigned tgt(unsigned long long a, unsigned y)
{
unsigned long long b = y;
unsigned long long t = b*CST; // won't overflow
return a >= t;
}
```
src can be translated to tgt as b*CST does not overflow.
We already do this for a const y via the following pattern:
```
/* X / C1 op C2 into a simple range test. */
(for cmp (simple_comparison)
(simplify
(cmp (trunc_div:s @0 INTEGER_CST@1) INTEGER_CST@2)
```
But this could be generalized too.
Note this is a generalized form from
https://github.com/llvm/llvm-project/issues/238952 .