https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115202
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|14.2 |11.5
Status|UNCONFIRMED |NEW
Last reconfirmed| |2024-05-23
Summary|[14/15 Regression] Missed |[11/12/13/14/15 Regression]
|optimization: std::min(f ? |Missed optimization:
|(unsigned short)m : a, ~0) |std::min(f ? (unsigned
| |short)m : a, ~0)
Ever confirmed|0 |1
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
Here is a reduced testcase:
```
unsigned short m;
template<typename T>
const T min(const T a, const T b)
{
if (a < b)
return a;
return b;
}
unsigned short func(int a, int f) {
return min(f ? m : a, ~0);
}
```
The above is a regression from GCC 8 where in GCC 9 adds an early phiopt.
And here is a version which is not a regression:
```
unsigned short m;
template<typename T>
inline const T min(const T a, const T b)
{
return a < b ? a : b;
}
unsigned short func(int a, int f) {
return min(f ? m : a, ~0);
}
```
The real issue is we don't optimize MIN<PHI<>, -1>.
>From the IR:
```
# RANGE [irange] int [0, 65535] MASK 0xffff VALUE 0x0
iftmp.0_6 = (intD.9) m.1_1;
;; succ: 4 [always] count:536870912 (estimated locally, freq 0.5000)
(FALLTHRU,EXECUTABLE)
;; basic block 4, loop depth 0, count 1073741824 (estimated locally, freq
1.0000), maybe hot
;; prev block 3, next block 1, flags: (NEW, REACHABLE, VISITED)
;; pred: 3 [always] count:536870912 (estimated locally, freq 0.5000)
(FALLTHRU,EXECUTABLE)
;; 2 [50.0% (guessed)] count:536870912 (estimated locally, freq
0.5000) (FALSE_VALUE,EXECUTABLE)
# iftmp.0_2 = PHI <iftmp.0_6(3), a_4(D)(2)>
# RANGE [irange] int [-INF, -1]
_7 = MIN_EXPR <iftmp.0_2, -1>;
```
Obviously here `MIN_EXPR <X, -1>` for the iftmp.0_6 part of the PHI, it will be
-1.