Bug 115055 - reassociation should use match-and-simplify
Summary: reassociation should use match-and-simplify
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 15.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2024-05-12 21:59 UTC by Andrew Pinski
Modified: 2024-05-13 09:49 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-05-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2024-05-12 21:59:52 UTC
Take:
```
int f(int a, int c)
{
        int b = a & c;
        return (a == 0) & b;
}
```

This should be optimized to 0 but currently does not. 

There is a match pattern (via PR 111431) which is able to handle `a & (a == 0)` into `0`. reassociation knows how to handle `a & a` but it seems like if while doing reassociation, we should be to try to use match-and-simplify to simplify the f here.
Comment 1 Richard Biener 2024-05-13 09:49:05 UTC
  b_5 = a_3(D) & c_4(D);
  _1 = a_3(D) == 0;
  _2 = (int) _1;
  _6 = b_5 & _2;
  return _6;

I believe reassoc doesn't see that a and (int)(a == 0) are "related" when
ranking ops of the AND, so it fails to appropriately order them and thus
trigger the simplification.  We definitely want to avoid doing n^2
matching combos of all AND operands.

So confirmed, but not necessarily confirming the proposed solution.