* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
@ 2011-08-13 1:39 ` jimis at gmx dot net
2011-08-13 2:21 ` jimis at gmx dot net
` (11 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jimis at gmx dot net @ 2011-08-13 1:39 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #2 from jimis <jimis at gmx dot net> 2011-08-13 01:37:14 UTC ---
Created attachment 24996
--> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24996
Make cse.c:preferable() more readable, slightly faster, without affecting its
logic.
2011-08-13 Dimitrios Apostolou <jimis@gmx.net>
* cse.c (preferable): Make it more readable and slightly faster,
without affecting its logic.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
2011-08-13 1:39 ` [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction) jimis at gmx dot net
@ 2011-08-13 2:21 ` jimis at gmx dot net
2012-08-06 11:41 ` rguenth at gcc dot gnu.org
` (10 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jimis at gmx dot net @ 2011-08-13 2:21 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
jimis <jimis at gmx dot net> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jimis at gmx dot net
--- Comment #3 from jimis <jimis at gmx dot net> 2011-08-13 01:39:23 UTC ---
I think that the logic in this function is expressed in complicated manner. It
took me some time to figure out what happens in various corner cases. The
attached patch I think makes the code more readable/maintainable, hopefully it
doesn't affect the function's logic at all. Runtime measured unaffected, if not
a couple milliseconds faster. Bootstrapped and tested on x86_64.
Thanks to Cristophe for pointing me here.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
2011-08-13 1:39 ` [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction) jimis at gmx dot net
2011-08-13 2:21 ` jimis at gmx dot net
@ 2012-08-06 11:41 ` rguenth at gcc dot gnu.org
2023-05-06 21:15 ` pinskia at gcc dot gnu.org
` (9 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-08-06 11:41 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #4 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-08-06 11:41:27 UTC ---
There is no straight-forward place to handle transform of
<bb 2>:
if (i_2(D) != j_3(D))
goto <bb 3>;
else
goto <bb 4>;
<bb 3>:
D.1715_4 = i_2(D) - j_3(D);
<bb 4>:
# D.1715_1 = PHI <D.1715_4(3), 0(2)>
to
<bb 2>:
<bb 3>:
D.1715_4 = i_2(D) - j_3(D);
<bb 4>:
D.1715_1 = D.1715_4;
but it eventually feels like a phi-opt transform.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (2 preceding siblings ...)
2012-08-06 11:41 ` rguenth at gcc dot gnu.org
@ 2023-05-06 21:15 ` pinskia at gcc dot gnu.org
2023-05-06 23:12 ` pinskia at gcc dot gnu.org
` (8 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-06 21:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Assignee|unassigned at gcc dot gnu.org |pinskia at gcc dot gnu.org
Status|NEW |ASSIGNED
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I will implement this match pattern since that is all that is needed now with
phiopt using match.
Something like:
(simplify
(cond (ne @0 @1) (minus@2 @0 @1) (zerop@3))
(@2))
Should work.
Note there might be need to be a check for HONOR_SIGNED_ZEROS and maybe
HONOR_NANS missing.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (3 preceding siblings ...)
2023-05-06 21:15 ` pinskia at gcc dot gnu.org
@ 2023-05-06 23:12 ` pinskia at gcc dot gnu.org
2023-05-06 23:13 ` pinskia at gcc dot gnu.org
` (7 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-06 23:12 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |gabravier at gmail dot com
--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 95729 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (4 preceding siblings ...)
2023-05-06 23:12 ` pinskia at gcc dot gnu.org
@ 2023-05-06 23:13 ` pinskia at gcc dot gnu.org
2023-08-31 1:03 ` pinskia at gcc dot gnu.org
` (6 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-06 23:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #5)
> I will implement this match pattern since that is all that is needed now
> with phiopt using match.
Funny I came up with the same match patterns twice now.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (5 preceding siblings ...)
2023-05-06 23:13 ` pinskia at gcc dot gnu.org
@ 2023-08-31 1:03 ` pinskia at gcc dot gnu.org
2023-08-31 4:48 ` pinskia at gcc dot gnu.org
` (5 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-31 1:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Xor should be handled too:
```
int f_xor(int i, int j)
{
if (i!=j)
return i ^ j;
return 0;
}
``
ior and and should be handled
```
int f_or(int i, int j)
{
if (i!=j)
return i | j;
return i; // could be j not just i
}
int f_and(int i, int j)
{
if (i!=j)
return i & j;
return i; // could be j not just i
}
```
So can plus and multiply:
```
int f_add(int i, int j)
{
if (i!=j)
return i + j;
return i+i;
}
int f_mult(int i, int j)
{
if (i!=j)
return i * j;
return i*i;
}
```
Note clang handles all of these except for f_add. f_mult might be handled via
the pull `i*` out of the conditional and then you have `i!=j?j:i` which then
will be reduced to j (that is they don't pattern match f_mult). They don't have
pattern matching for f_add either and `i+i` will change to `i*2` and not pulled
out of the condition.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (6 preceding siblings ...)
2023-08-31 1:03 ` pinskia at gcc dot gnu.org
@ 2023-08-31 4:48 ` pinskia at gcc dot gnu.org
2023-08-31 18:03 ` pinskia at gcc dot gnu.org
` (4 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-31 4:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55822
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55822&action=edit
Patch which I am testing
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (7 preceding siblings ...)
2023-08-31 4:48 ` pinskia at gcc dot gnu.org
@ 2023-08-31 18:03 ` pinskia at gcc dot gnu.org
2023-09-01 6:58 ` cvs-commit at gcc dot gnu.org
` (3 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-31 18:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #10 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch posted here:
https://gcc.gnu.org/pipermail/gcc-patches/2023-August/628995.html
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (8 preceding siblings ...)
2023-08-31 18:03 ` pinskia at gcc dot gnu.org
@ 2023-09-01 6:58 ` cvs-commit at gcc dot gnu.org
2023-09-01 6:58 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-01 6:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:
https://gcc.gnu.org/g:3d86e7f4a8aef1b864a51660825597eafe9059b1
commit r14-3606-g3d86e7f4a8aef1b864a51660825597eafe9059b1
Author: Andrew Pinski <apinski@marvell.com>
Date: Wed Aug 30 21:21:01 2023 -0700
MATCH [PR19832]: Optimize some `(a != b) ? a OP b : c`
This patch adds the following match patterns to optimize these:
/* (a != b) ? (a - b) : 0 -> (a - b) */
/* (a != b) ? (a ^ b) : 0 -> (a ^ b) */
/* (a != b) ? (a & b) : a -> (a & b) */
/* (a != b) ? (a | b) : a -> (a | b) */
/* (a != b) ? min(a,b) : a -> min(a,b) */
/* (a != b) ? max(a,b) : a -> max(a,b) */
/* (a != b) ? (a * b) : (a * a) -> (a * b) */
/* (a != b) ? (a + b) : (a + a) -> (a + b) */
/* (a != b) ? (a + b) : (2 * a) -> (a + b) */
Note currently only integer types (include vector types)
are handled. Floating point types can be added later on.
OK? Bootstrapped and tested on x86_64-linux-gnu.
The first pattern had still shows up in GCC in cse.c's preferable
function which was the original motivation for this patch.
PR tree-optimization/19832
gcc/ChangeLog:
* match.pd: Add pattern to optimize
`(a != b) ? a OP b : c`.
gcc/testsuite/ChangeLog:
* g++.dg/opt/vectcond-1.C: New test.
* gcc.dg/tree-ssa/phi-opt-same-1.c: New test.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (9 preceding siblings ...)
2023-09-01 6:58 ` cvs-commit at gcc dot gnu.org
@ 2023-09-01 6:58 ` pinskia at gcc dot gnu.org
2023-09-01 12:23 ` cvs-commit at gcc dot gnu.org
2023-12-25 4:02 ` cvs-commit at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-01 6:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|ASSIGNED |RESOLVED
Target Milestone|--- |14.0
--- Comment #12 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (10 preceding siblings ...)
2023-09-01 6:58 ` pinskia at gcc dot gnu.org
@ 2023-09-01 12:23 ` cvs-commit at gcc dot gnu.org
2023-12-25 4:02 ` cvs-commit at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-01 12:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #13 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:c2d3211d580369b75200fbdd7b854d30460e0aba
commit r14-3623-gc2d3211d580369b75200fbdd7b854d30460e0aba
Author: Jakub Jelinek <jakub@redhat.com>
Date: Fri Sep 1 14:22:17 2023 +0200
testsuite: Fix vectcond-1.C FAIL on i686-linux [PR19832]
This test FAILs on i686-linux with
.../gcc/testsuite/g++.dg/opt/vectcond-1.C:8:57: warning: MMX vector return
without MMX enabled changes the ABI [-Wpsabi]
.../gcc/testsuite/g++.dg/opt/vectcond-1.C:17:12: warning: MMX vector
argument without MMX enabled changes the ABI [-Wpsabi]
excess warning. Fixed by using -Wno-psabi.
2023-09-01 Jakub Jelinek <jakub@redhat.com>
PR tree-optimization/19832
* g++.dg/opt/vectcond-1.C: Add -Wno-psabi to dg-options.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug tree-optimization/19832] don't remove an if when we know the value is the same as with the if (subtraction)
[not found] <bug-19832-4@http.gcc.gnu.org/bugzilla/>
` (11 preceding siblings ...)
2023-09-01 12:23 ` cvs-commit at gcc dot gnu.org
@ 2023-12-25 4:02 ` cvs-commit at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-12-25 4:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=19832
--- Comment #14 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:
https://gcc.gnu.org/g:59ecd5ff096f800de17b804f1482055f2d84d629
commit r14-6827-g59ecd5ff096f800de17b804f1482055f2d84d629
Author: Andrew Pinski <quic_apinski@quicinc.com>
Date: Sun Dec 24 15:51:35 2023 -0800
match: Improve `(a != b) ? (a + b) : (2 * a)` pattern [PR19832]
In the testcase provided, we would match f_plus but not g_plus
due to a missing `:c` on the plus operator. This fixes the oversight
there.
Note this was noted in https://github.com/llvm/llvm-project/issues/76318 .
Committed as obvious after bootstrap/test on x86_64-linux-gnu.
PR tree-optimization/19832
gcc/ChangeLog:
* match.pd (`(a != b) ? (a + b) : (2 * a)`): Add `:c`
on the plus operator.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/phi-opt-same-2.c: New test.
Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
^ permalink raw reply [flat|nested] 14+ messages in thread