[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-31 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #9 from Andrew Pinski  ---
```
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 .

[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-31 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #8 from Andrew Pinski  ---
```
int f(int x)
{
x = ~x;
int t = (x >= 0 ? x : 0);
int t1 = (x <= 0 ? -x : 0);
return  t + t1;
}
```

abs(~x)

[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-24 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #7 from Andrew Pinski  ---
```
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).

[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-24 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #6 from Andrew Pinski  ---
`~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.

[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-08 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #5 from Andrew Pinski  ---
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.

[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-08 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

Andrew Pinski  changed:

   What|Removed |Added

 Blocks||113265

--- Comment #4 from Andrew Pinski  ---
`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 -> ABS

```
int f(int a, int b, int x)
{
  x = a - b;
  int y = -x;
  return (x >= y ? y : x);
}
```
MIN -> -ABS

```
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

[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-08 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

--- Comment #3 from Andrew Pinski  ---
(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` (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);
}
```

[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-08 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114999

Andrew Pinski  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  ---
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` (just like if the assignment to x is
removed.