public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115392] New: Missed optimization: fold `3 / (b ^ a)` to `b < 4`
@ 2024-06-08 8:11 zhiwuyazhe154 at gmail dot com
2024-06-08 18:00 ` [Bug tree-optimization/115392] Missed optimization: fold `(a^b) <= 3` to `b < 4` for bool a and know that (a^b) is non zero pinskia at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: zhiwuyazhe154 at gmail dot com @ 2024-06-08 8:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115392
Bug ID: 115392
Summary: Missed optimization: fold `3 / (b ^ a)` to `b < 4`
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: zhiwuyazhe154 at gmail dot com
Target Milestone: ---
Godbolt example: https://godbolt.org/z/7YE4Eq9a7
Code example:
bool m;
bool a;
void fn1(unsigned int b) {
if((b^a)!=0)
m = 3 / (b ^ a); // equals to `m = b < 4`
}
Since the range of a is {0,1}, we can discuss it in different cases:
When a is 0, b^a = b^0 = b;
When a is 1, b^a = b^1 = {b, b is an even number; b - 1, b is an odd number}
In summary, b^a is less than or equal to 3 only when b < 4
GCC -O3:
fn1(unsigned int):
movzx eax, BYTE PTR a[rip]
cmp eax, edi
je .L1
xor eax, edi
cmp eax, 3
setbe BYTE PTR m[rip]
.L1:
ret
a:
.zero 1
m:
.zero 1
Expected code(CLANG -O3):
fn1(unsigned int):
movzx eax, byte ptr [rip + a]
cmp eax, edi
je .LBB0_2
cmp edi, 4
setb byte ptr [rip + m]
.LBB0_2:
ret
m:
.byte 0
a:
.byte 0
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug tree-optimization/115392] Missed optimization: fold `(a^b) <= 3` to `b < 4` for bool a and know that (a^b) is non zero
2024-06-08 8:11 [Bug c++/115392] New: Missed optimization: fold `3 / (b ^ a)` to `b < 4` zhiwuyazhe154 at gmail dot com
@ 2024-06-08 18:00 ` pinskia at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-08 18:00 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115392
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Severity|normal |enhancement
Summary|Missed optimization: fold |Missed optimization: fold
|`3 / (b ^ a)` to `b < 4` |`(a^b) <= 3` to `b < 4` for
| |bool a and know that (a^b)
| |is non zero
Last reconfirmed| |2024-06-08
Component|c++ |tree-optimization
Status|UNCONFIRMED |NEW
Keywords| |missed-optimization
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. Note GCC already transform `3 / (b ^ a)` into `(a^b) <= 3`. But does
not transform that into `b <= 3` when we know that `a` has a [0,1] range.
A simple pattern like:
```
(for cmp (gt le)
(simplify
(cmp:c (bit_xor @0 zero_one_valued_p@1) INTEGER_CST@2)
(if ((wi::to_wide (2) & 1) == 1)
(cmp @0 @2)))
```
Should work.
Note this can be expanded into more if needed
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-06-08 18:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-08 8:11 [Bug c++/115392] New: Missed optimization: fold `3 / (b ^ a)` to `b < 4` zhiwuyazhe154 at gmail dot com
2024-06-08 18:00 ` [Bug tree-optimization/115392] Missed optimization: fold `(a^b) <= 3` to `b < 4` for bool a and know that (a^b) is non zero 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).