public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115202] New: [14/15 Regression] Missed optimization: std::min(f ?  (unsigned short)m : a, ~0)
@ 2024-05-23  7:44 652023330028 at smail dot nju.edu.cn
  2024-05-23  9:02 ` [Bug tree-optimization/115202] " rguenth at gcc dot gnu.org
  2024-05-23 12:18 ` [Bug tree-optimization/115202] [11/12/13/14/15 " pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-05-23  7:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115202
           Summary: [14/15 Regression] Missed optimization: std::min(f ?
                    (unsigned short)m : a, ~0)
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we noticed that the code below can be optimized to be independent of
'm', but gcc -O3 seems to have missed it.

https://godbolt.org/z/a6MoofoYK

unsigned short m;
#include <algorithm>
unsigned short func(int a, int f) {
    return std::min(f ? m : a, ~0);
}

GCC-trunk -O3:
func(int, int):
        movzx   eax, WORD PTR m[rip]
        test    esi, esi
        mov     edx, -1
        cmove   eax, edi
        test    eax, eax
        cmovns  eax, edx
        ret


Expected code (gcc-13.2):
func(int, int):
        mov     eax, edi
        test    edi, edi
        jns     .L4
        test    esi, esi
        jne     .L4
        ret
.L4:
        mov     eax, 65535
        ret

If we did it correctly, it's caused by
https://github.com/gcc-mirror/gcc/commit/7b34cacc5735385e7e2855d7c0a6fad60ef4a99b

Thank you very much for your time and effort! We look forward to hearing from
you.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/115202] [14/15 Regression] Missed optimization: std::min(f ?  (unsigned short)m : a, ~0)
  2024-05-23  7:44 [Bug tree-optimization/115202] New: [14/15 Regression] Missed optimization: std::min(f ? (unsigned short)m : a, ~0) 652023330028 at smail dot nju.edu.cn
@ 2024-05-23  9:02 ` rguenth at gcc dot gnu.org
  2024-05-23 12:18 ` [Bug tree-optimization/115202] [11/12/13/14/15 " pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-23  9:02 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
   Target Milestone|---                         |14.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug tree-optimization/115202] [11/12/13/14/15 Regression] Missed optimization: std::min(f ?  (unsigned short)m : a, ~0)
  2024-05-23  7:44 [Bug tree-optimization/115202] New: [14/15 Regression] Missed optimization: std::min(f ? (unsigned short)m : a, ~0) 652023330028 at smail dot nju.edu.cn
  2024-05-23  9:02 ` [Bug tree-optimization/115202] " rguenth at gcc dot gnu.org
@ 2024-05-23 12:18 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-23 12:18 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-23 12:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-23  7:44 [Bug tree-optimization/115202] New: [14/15 Regression] Missed optimization: std::min(f ? (unsigned short)m : a, ~0) 652023330028 at smail dot nju.edu.cn
2024-05-23  9:02 ` [Bug tree-optimization/115202] " rguenth at gcc dot gnu.org
2024-05-23 12:18 ` [Bug tree-optimization/115202] [11/12/13/14/15 " pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).