Bug 112104 - loop of ^1 should just be reduced to ^(n&1)
Summary: loop of ^1 should just be reduced to ^(n&1)
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 14.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks: loop-final-value, sccp
  Show dependency treegraph
 
Reported: 2023-10-27 03:47 UTC by Andrew Pinski
Modified: 2024-06-06 08:28 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-10-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2023-10-27 03:47:11 UTC
Take:
```
int
foo(long n3) {
  int j = 0;
  for(int i=0; i<n3; i++)
    j=j^1;
  return j;
}

int
foo1(long n3) {
  int j = 0;
  if (n3>=0)
    j = j ^ (n3&1);
  return j;
}
```

We should figure out that j as the result is just alternating between 0 and 1 (VRP figures that that is the range) in SCCP pattern matching.

I Noticed this while looking into PR 111972
Comment 1 Andrew Pinski 2023-10-27 04:04:50 UTC
This shows up in a really really bad benchmark:
https://github.com/kdlucas/byte-unixbench/blob/master/UnixBench/src/whets.c
Comment 2 Richard Biener 2023-10-27 13:08:11 UTC
Confirmed.  I think it should apply even to

int
foo(long n3, int j, int x) {
  for(int i=0; i<n3; i++)
    j=j^x;
  return j;
}

which alternates between 'j' and 'j^x', thus the result
is n3&1 ? j^x : j?

I wonder if there's some online symbolic calculus service that we can
feed IL during compilation ;)
Comment 3 Hongtao.liu 2023-10-30 06:34:29 UTC
We already have analyze_and_compute_bitop_with_inv_effect, but it only works when inv is an SSA_NAME, it should be extended to constant.
Comment 4 Andrew Pinski 2023-11-14 17:01:13 UTC
Fixed via r14-5428-gfd1596f9962569afff6c9298a7c79686c6950bef .
Comment 5 Hongtao.liu 2023-11-14 23:59:20 UTC
(In reply to Andrew Pinski from comment #4)
> Fixed via r14-5428-gfd1596f9962569afff6c9298a7c79686c6950bef .

Note, my patch only handles constant tripcount for XOR, but not do the transformation when tripcount is variable.
Comment 6 Andrew Pinski 2023-11-15 00:00:16 UTC
(In reply to Hongtao.liu from comment #5)
> (In reply to Andrew Pinski from comment #4)
> > Fixed via r14-5428-gfd1596f9962569afff6c9298a7c79686c6950bef .
> 
> Note, my patch only handles constant tripcount for XOR, but not do the
> transformation when tripcount is variable.

Oh.