public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
@ 2024-05-09 12:59 652023330028 at smail dot nju.edu.cn
  2024-05-09 14:27 ` [Bug tree-optimization/115011] " hjl.tools at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-05-09 12:59 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115011
           Summary: [14/15 Regression] Missed optimization: (bool) (f ? 1:
                    t) ==> 1 when bool t = (0 >= f) + x;
           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 as stated in the title,
but gcc -O3 seems to have missed it.


Reduced code:
https://godbolt.org/z/xE6GEEn7j

bool x;
bool fn3(unsigned long long int f) {
    bool t = (0 >= f) + x;
    return f ? 1: t;
}

GCC -O3:
fn3(unsigned long long):
        movzx   eax, BYTE PTR x[rip]
        cmp     rdi, 1
        adc     eax, 0
        test    eax, eax
        setne   al
        test    rdi, rdi
        setne   dl
        or      eax, edx
        ret

Expected code (GCC-13.2):
fn3(unsigned long long):
        mov     eax, 1
        ret

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

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

* [Bug tree-optimization/115011] [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
  2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
@ 2024-05-09 14:27 ` hjl.tools at gmail dot com
  2024-05-09 14:28 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: hjl.tools at gmail dot com @ 2024-05-09 14:27 UTC (permalink / raw)
  To: gcc-bugs

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

H.J. Lu <hjl.tools at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu.org
   Target Milestone|---                         |14.2
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-05-09
     Ever confirmed|0                           |1

--- Comment #1 from H.J. Lu <hjl.tools at gmail dot com> ---
It is caused by r14-1597.

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

* [Bug tree-optimization/115011] [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
  2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
  2024-05-09 14:27 ` [Bug tree-optimization/115011] " hjl.tools at gmail dot com
@ 2024-05-09 14:28 ` pinskia at gcc dot gnu.org
  2024-05-09 14:50 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 14:28 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2024-05-09 00:00:00         |
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. this was "exposed/caused" by my r14-3486-gd9a0d692ffc695 .

We miss this in previous releases too (LLVM is only able to optimize it for
LLVM 18+):
```
bool x;
bool fn3(unsigned long long int f) {
    bool t = (0 >= f) + x;
    bool t1 = t | (f != 0);
    return t1;
}
```

I will look into this in a few weeks since I suspect it is a minor regression.

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

* [Bug tree-optimization/115011] [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
  2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
  2024-05-09 14:27 ` [Bug tree-optimization/115011] " hjl.tools at gmail dot com
  2024-05-09 14:28 ` pinskia at gcc dot gnu.org
@ 2024-05-09 14:50 ` pinskia at gcc dot gnu.org
  2024-05-09 14:50 ` pinskia at gcc dot gnu.org
  2024-05-10 12:27 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 14:50 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |104292

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The easiest way to fix this is to optimize t_12 in:
```
  _1 = f_10(D) == 0;
  _2 = (int) _1; //[0,1]
  x.0_3 = x;
  _4 = (int) x.0_3; //[0,1]
  _5 = _2 + _4;
  t_12 = _5 != 0;
  _7 = f_10(D) != 0;
  _14 = _7 | t_12;
```

Into:
``
  x.0_3 = x;
  _1 = f_10(D) == 0;
  t_12 = _1 | x.0_3; // <-- this
  _7 = f_10(D) != 0;
  _14 = _7 | t_12;
```
Which then will be optimized into true as _1 and _7 are opposites.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104292
[Bug 104292] (bool)(bool0 + bool1) should be simplified into bool0 | bool1

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

* [Bug tree-optimization/115011] [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
  2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
                   ` (2 preceding siblings ...)
  2024-05-09 14:50 ` pinskia at gcc dot gnu.org
@ 2024-05-09 14:50 ` pinskia at gcc dot gnu.org
  2024-05-10 12:27 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 14:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So depends on PR 104292.

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

* [Bug tree-optimization/115011] [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x;
  2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
                   ` (3 preceding siblings ...)
  2024-05-09 14:50 ` pinskia at gcc dot gnu.org
@ 2024-05-10 12:27 ` rguenth at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-10 12:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-09 12:59 [Bug tree-optimization/115011] New: [14/15 Regression] Missed optimization: (bool) (f ? 1: t) ==> 1 when bool t = (0 >= f) + x; 652023330028 at smail dot nju.edu.cn
2024-05-09 14:27 ` [Bug tree-optimization/115011] " hjl.tools at gmail dot com
2024-05-09 14:28 ` pinskia at gcc dot gnu.org
2024-05-09 14:50 ` pinskia at gcc dot gnu.org
2024-05-09 14:50 ` pinskia at gcc dot gnu.org
2024-05-10 12:27 ` rguenth 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).