[Bug tree-optimization/112385] `(2 >> a) ^ (5 >> a)` is not optimized to `7 >> a` is 2 and 5 differ in signedness

2023-11-12 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112385

Andrew Pinski  changed:

   What|Removed |Added

 Ever confirmed|0   |1
   Last reconfirmed||2023-11-12
 Status|UNCONFIRMED |ASSIGNED
   Assignee|unassigned at gcc dot gnu.org  |pinskia at gcc dot 
gnu.org

--- Comment #3 from Andrew Pinski  ---
Mine, I will handle this.

[Bug tree-optimization/112385] `(2 >> a) ^ (5 >> a)` is not optimized to `7 >> a` is 2 and 5 differ in signedness

2023-11-04 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112385

--- Comment #2 from Andrew Pinski  ---
We can even do it if we know the unsigned value does not have the last bit set.
That is:
```
int f(int a, unsigned b, int c)
{
  b &= 0x;
  return (c >> a) ^ (b >> a);
}

```
Note clang(LLVM) does not handle the above.

The way LLVM handles the others is it changes ashift to lshift if the value is
known to be non-negative. But since in the gimple level we are dealing with
types we can't do that in a nice way; RTL level on the other hand yes that can
be done (though nonzero bits is limited there).

[Bug tree-optimization/112385] `(2 >> a) ^ (5 >> a)` is not optimized to `7 >> a` is 2 and 5 differ in signedness

2023-11-04 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112385

--- Comment #1 from Andrew Pinski  ---
Note a more complex case like:
```
int f(int a, unsigned b, int c)
{
  c &= 0x;
  return (c >> a) ^ (b >> a);
}
```
Should also be optimized to:
```
int f(int a, unsigned b, int c)
{
  c &= 0x;
  return (c^b) >> a;
}
```

That is if both are known to be non-negative we can do the combining.