public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112884] New: Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0
@ 2023-12-06 16:47 xxs_chy at outlook dot com
2023-12-06 18:41 ` [Bug tree-optimization/112884] " pinskia at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: xxs_chy at outlook dot com @ 2023-12-06 16:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112884
Bug ID: 112884
Summary: Missing optimization: fold a%2==0 ? a/2*2 : 0 to
a%2==0 ? a : 0
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: xxs_chy at outlook dot com
Target Milestone: ---
Godbolt example: https://godbolt.org/z/Ec1ax79r8
For the select arm "a / 2 * 2" in:
unsigned src(unsigned a) {
return a % 2 == 0 ? (a / 2 * 2) : 0;
}
it's equivalent to "a", so the program could be folded to:
unsigned tgt(unsigned a) {
return a % 2 == 0 ? a : 0;
}
Both GCC and LLVM missed such optimization in select arms.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/112884] Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0
2023-12-06 16:47 [Bug tree-optimization/112884] New: Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0 xxs_chy at outlook dot com
@ 2023-12-06 18:41 ` pinskia at gcc dot gnu.org
2023-12-07 8:10 ` rguenth at gcc dot gnu.org
2023-12-07 8:26 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-06 18:41 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112884
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Last reconfirmed| |2023-12-06
Severity|normal |enhancement
CC| |pinskia at gcc dot gnu.org
Status|UNCONFIRMED |NEW
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.
VRP does have the information:
=========== BB 3 ============
a_3(D) [irange] unsigned int [0, 0][2, 4294967294] MASK 0xfffffffe VALUE 0x0
<bb 3> :
iftmp.0_5 = a_3(D) & 4294967294;
But it does not do the folding ...
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/112884] Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0
2023-12-06 16:47 [Bug tree-optimization/112884] New: Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0 xxs_chy at outlook dot com
2023-12-06 18:41 ` [Bug tree-optimization/112884] " pinskia at gcc dot gnu.org
@ 2023-12-07 8:10 ` rguenth at gcc dot gnu.org
2023-12-07 8:26 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-12-07 8:10 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112884
--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
I'd rather not do it in match.pd - well, maybe improve A / CST * CST
folding to consider nonzero bits, that is.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug tree-optimization/112884] Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0
2023-12-06 16:47 [Bug tree-optimization/112884] New: Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0 xxs_chy at outlook dot com
2023-12-06 18:41 ` [Bug tree-optimization/112884] " pinskia at gcc dot gnu.org
2023-12-07 8:10 ` rguenth at gcc dot gnu.org
@ 2023-12-07 8:26 ` pinskia at gcc dot gnu.org
2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-07 8:26 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112884
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #2)
> I'd rather not do it in match.pd - well, maybe improve A / CST * CST
> folding to consider nonzero bits, that is.
I don't think there is anything to do in match.pd really.
Since `(A / CST) * CST` already simplifies to `A & ~(CST1)` for power of 2.
Rather VRP just needs to simplify `a_3(D) & 4294967294` to `a_3(D)` when it
knows that `a_3(D) & 1` is 0 already (which it knows at that point:
```
a_3(D) [irange] unsigned int [0, 0][2, 4294967294] MASK 0xfffffffe VALUE 0x0
```
).
Note we already handle:
```
unsigned src(unsigned a) {
if (a % 2 != 0) __builtin_unreachable();
return (a / 2 * 2);
}
```
Via match:
```
Folding statement: _3 = a_2(D) & 4294967294;
Applying pattern match.pd:1572, gimple-match-6.cc:16797
gimple_simplified to _3 = a_2(D);
Folded into: _3 = a_2(D);
```
This pattern uses get_nonzero_bits. get_nonzero_bits uses the global ranger to
find out the nonzero bits. We could add a similar optimization directly to the
folding part of VRP that uses the local ranger to find the nonzero bits.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-12-07 8:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-06 16:47 [Bug tree-optimization/112884] New: Missing optimization: fold a%2==0 ? a/2*2 : 0 to a%2==0 ? a : 0 xxs_chy at outlook dot com
2023-12-06 18:41 ` [Bug tree-optimization/112884] " pinskia at gcc dot gnu.org
2023-12-07 8:10 ` rguenth at gcc dot gnu.org
2023-12-07 8:26 ` 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).