public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b`
@ 2024-05-09 0:11 pinskia at gcc dot gnu.org
2024-05-09 0:11 ` [Bug tree-optimization/114999] " pinskia at gcc dot gnu.org
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
Bug ID: 114999
Summary: `a - b == b - a` -> `a == b`
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: enhancement
Priority: P3
Component: tree-optimization
Assignee: pinskia at gcc dot gnu.org
Reporter: pinskia at gcc dot gnu.org
Target Milestone: ---
Take:
```
int f(int a, int b, int c)
{
c = a - b;
int d = -c;
int t = c == d;
int t1 = c == 0;
return t == t1;
}
```
This is not able to optimize to 1 as we don't detect that `a - b` and `b - a`
are negative of each other.
```
(for cmp (eq ne)
(simplify
(cmp:c @0 (negate @0))
```
needs to be improved.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] `a - b == b - a` -> `a == b`
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
@ 2024-05-09 0:11 ` pinskia at gcc dot gnu.org
2024-05-09 0:16 ` [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other pinskia at gcc dot gnu.org
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |ASSIGNED
Ever confirmed|0 |1
Last reconfirmed| |2024-05-09
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I have an idea on how to fix this.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
2024-05-09 0:11 ` [Bug tree-optimization/114999] " pinskia at gcc dot gnu.org
@ 2024-05-09 0:16 ` pinskia at gcc dot gnu.org
2024-05-09 0:17 ` pinskia at gcc dot gnu.org
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|`a - b == b - a` -> `a == |A few missing optimizations
|b` |due to `a - b` and `b - a`
| |not being detected as
| |negatives of each other
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Another one:
```
int f(int a, int b, int x)
{
x = a - b;
int y = -x;
return (x >= 0 ? x : 0) + (x <= 0 ? y : 0);
}
```
This should be detected as `abs<a - b>` (just like if the assignment to x is
removed.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
2024-05-09 0:11 ` [Bug tree-optimization/114999] " pinskia at gcc dot gnu.org
2024-05-09 0:16 ` [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other pinskia at gcc dot gnu.org
@ 2024-05-09 0:17 ` pinskia at gcc dot gnu.org
2024-05-09 0:33 ` pinskia at gcc dot gnu.org
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> Another one:
> ```
> int f(int a, int b, int x)
> {
> x = a - b;
> int y = -x;
> return (x >= 0 ? x : 0) + (x <= 0 ? y : 0);
> }
> ```
>
> This should be detected as `abs<a - b>` (just like if the assignment to x is
> removed.
```
int f(int a, int b, int x)
{
x = a - b;
int y = -x;
return (x >= 0 ? x : 0) + (y >= 0 ? y : 0);
}
```
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (2 preceding siblings ...)
2024-05-09 0:17 ` pinskia at gcc dot gnu.org
@ 2024-05-09 0:33 ` pinskia at gcc dot gnu.org
2024-05-09 0:35 ` pinskia at gcc dot gnu.org
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:33 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Blocks| |113265
--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
`X / -X is -1.` is PR 113265
```
int f(int a, int b, int x)
{
x = a - b;
int y = -x;
return (x >= y ? x : y);
}
```
MAX<a, -a> -> ABS<x>
```
int f(int a, int b, int x)
{
x = a - b;
int y = -x;
return (x >= y ? y : x);
}
```
MIN<a, -a> -> -ABS<x>
```
unsigned f(unsigned a, unsigned b, unsigned X, unsigned Y)
{
X = a - b;
unsigned t = -X;
return (X + 1) > Y ? t : 1;
}
```
// `(X + 1) > Y ? -X : 1` -> `X >= Y ? -X : 1` (unsigned)
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113265
[Bug 113265] [11/12/13/14/15 Regression] Missed optimization for redundancy
computation elimination may be due to constant propagation about 0 too late
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (3 preceding siblings ...)
2024-05-09 0:33 ` pinskia at gcc dot gnu.org
@ 2024-05-09 0:35 ` pinskia at gcc dot gnu.org
2024-05-25 2:55 ` pinskia at gcc dot gnu.org
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-09 0:35 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The following patterns could be improved/removed while doing this:
```
/* (A - B) == 0 ? (A - B) : (B - A) same as (B - A) */
/* (A - B) != 0 ? (A - B) : (B - A) same as (A - B) */
/* (A - B) >=/> 0 ? (A - B) : (B - A) same as abs (A - B) */
/* (A - B) <=/< 0 ? (A - B) : (B - A) same as -abs (A - B) */
```
Need to look if we need to improve ctz_table_index match.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (4 preceding siblings ...)
2024-05-09 0:35 ` pinskia at gcc dot gnu.org
@ 2024-05-25 2:55 ` pinskia at gcc dot gnu.org
2024-05-25 5:42 ` pinskia at gcc dot gnu.org
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-25 2:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
`~a` and `a + 1` are also negative of each other too:
```
int f(int a, int b)
{
int t = ~a;
int t1 = -t;
return t1 == t;
}
int f1(int a)
{
return (~a) == (-~a);
}
int f2(int a)
{
return (a) == (-a);
}
int f3(int a)
{
a = ~a;
return (a) == (-a);
}
int f4(int a)
{
a = ~a;
return f2(a);
}
```
Right now I have a patch which handles `a == -a` for `(a-b)`/`b-a`.
I need one that matches `~a` with the corresponding `a + 1` too.
Note gimple_bitwise_inverted_equal_p should also be expanded to support `-a`
with `a + -1` too.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (5 preceding siblings ...)
2024-05-25 2:55 ` pinskia at gcc dot gnu.org
@ 2024-05-25 5:42 ` pinskia at gcc dot gnu.org
2024-05-31 21:29 ` pinskia at gcc dot gnu.org
2024-05-31 21:46 ` pinskia at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-25 5:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
```
inline int func0(int y){
return -~y;
}
int func2(int y){
return (~y)/(-(~y));
}
int func2a(int y){
return (~y)/func0(y);
}
int func1(int y){
return (~y)/(y+1);
}
```
What is interesting clang is able to handle func2 but not the rest while GCC
currently does not handle any of them (folds -~y early to `y + 1` before `y/-y`
to -y).
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (6 preceding siblings ...)
2024-05-25 5:42 ` pinskia at gcc dot gnu.org
@ 2024-05-31 21:29 ` pinskia at gcc dot gnu.org
2024-05-31 21:46 ` pinskia at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-31 21:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
```
int f(int x)
{
x = ~x;
int t = (x >= 0 ? x : 0);
int t1 = (x <= 0 ? -x : 0);
return t + t1;
}
```
abs(~x)
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
` (7 preceding siblings ...)
2024-05-31 21:29 ` pinskia at gcc dot gnu.org
@ 2024-05-31 21:46 ` pinskia at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-31 21:46 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999
--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
```
float f(float a, float b, float x)
{
x = a - b;
float t = 0;
t = t - x;
return t/x;
}
```
! HONOR_NANS (type) && ! HONOR_INFINITIES (type)
```
int f(int a, int b, int A)
{
A = ~A;
int t = 0;
t = t - A;
return A != 0 ? A : t;
}
```
Is not optimized at -O1 until phiopt3 .
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2024-05-31 21:46 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-09 0:11 [Bug tree-optimization/114999] New: `a - b == b - a` -> `a == b` pinskia at gcc dot gnu.org
2024-05-09 0:11 ` [Bug tree-optimization/114999] " pinskia at gcc dot gnu.org
2024-05-09 0:16 ` [Bug tree-optimization/114999] A few missing optimizations due to `a - b` and `b - a` not being detected as negatives of each other pinskia at gcc dot gnu.org
2024-05-09 0:17 ` pinskia at gcc dot gnu.org
2024-05-09 0:33 ` pinskia at gcc dot gnu.org
2024-05-09 0:35 ` pinskia at gcc dot gnu.org
2024-05-25 2:55 ` pinskia at gcc dot gnu.org
2024-05-25 5:42 ` pinskia at gcc dot gnu.org
2024-05-31 21:29 ` pinskia at gcc dot gnu.org
2024-05-31 21:46 ` 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).