public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/115474] New: Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b)
@ 2024-06-13 10:47 zhiwuyazhe154 at gmail dot com
  2024-06-13 14:05 ` [Bug tree-optimization/115474] " pinskia at gcc dot gnu.org
  2024-06-13 14:44 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: zhiwuyazhe154 at gmail dot com @ 2024-06-13 10:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115474
           Summary: Missed optimization: fold `std::max(a > b, a)` to `a`
                    (bool a, bool b)
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zhiwuyazhe154 at gmail dot com
  Target Milestone: ---

godbolt example: https://godbolt.org/z/KG1fhaPvM

Code Example:
```
int func(bool a, bool b) {
    return std::max(a > b, a); // equals to a
}
```

In this case, when a == 0, a > b == 0(a); when a == 1, a > b <= 1(a). So it can
be folded to a.

GCC -O3:
func(bool, bool):
        cmp     sil, dil
        setb    dl
        cmp     dl, dil
        setb    al
        or      eax, edx
        movzx   eax, al
        ret

Expected code(CLANG -O3):
func(bool, bool):                             
        mov     eax, edi
        ret

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

* [Bug tree-optimization/115474] Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b)
  2024-06-13 10:47 [Bug tree-optimization/115474] New: Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b) zhiwuyazhe154 at gmail dot com
@ 2024-06-13 14:05 ` pinskia at gcc dot gnu.org
  2024-06-13 14:44 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-13 14:05 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org
                 CC|                            |pinskia at gcc dot gnu.org
   Last reconfirmed|                            |2024-06-13

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed, there are at least 2 forms that need to be optimized (note MIN/MAX
here is gone already due to zero_one optimizations):

-O2:
  _2 = b_5(D) < a_7(D); // a & !b
  _1 = _2 < a_7(D); // !(a & !b) & a -> (!a | b) & a -> a & b
  _8 = _1 | _2; // (a & b) | (a & !b) -> a

-O1:
  _2 = b_4(D) < a_7(D);  // a & !b
  _6 = _2 | a_7(D); // (a & !b) | a -> a


Let me handle those.

Note there is some more with <=/>/>= too.

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

* [Bug tree-optimization/115474] Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b)
  2024-06-13 10:47 [Bug tree-optimization/115474] New: Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b) zhiwuyazhe154 at gmail dot com
  2024-06-13 14:05 ` [Bug tree-optimization/115474] " pinskia at gcc dot gnu.org
@ 2024-06-13 14:44 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-13 14:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here is a few more:
```
int func(bool a, bool b) {
  bool t = b < a;
  return t | a; // a
}
int func1(bool a, bool b) {
  bool t = b < a;
  bool t1 = t < a;
  return t | t1; // a
}


int func2(bool a, bool b) {
  bool t = b > a;
  return t | a; // b | a
}
int func3(bool a, bool b) {
  bool t = b > a;
  bool t1 = t < a;
  return t | t1; // b | a
}
```

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

end of thread, other threads:[~2024-06-13 14:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-13 10:47 [Bug tree-optimization/115474] New: Missed optimization: fold `std::max(a > b, a)` to `a` (bool a, bool b) zhiwuyazhe154 at gmail dot com
2024-06-13 14:05 ` [Bug tree-optimization/115474] " pinskia at gcc dot gnu.org
2024-06-13 14:44 ` 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).