-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Description
This example is a simplified version from a case we encountered:
https://godbolt.org/z/af5oWYMzj
We can optimize the switch away by replacing the getelementptrs with the merge.geps in the merge blocks (since #156477 and #158242):
https://godbolt.org/z/MoMnbhM5d
InstCombinePHI can do just that with foldPHIArgGEPIntoPHI
| Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) { |
However, in this case the transform fails because:
- There are multiple users
- The offsets are constant
Both cases prevent the transform due to pessimization. Aborting on constant indices ensures phis into structs do not introduce invalid getelementptrs. Maybe we could relax this restriction on non-struct phis but the multiple users restriction still blocks optimizing our case.
What is the best way to optimize this case? Can we extend foldPHIArgGEPIntoPHi in InstCombine or should we implement this in SimplifyCFG?
Edit 12-01: Further simplified example to have the same switch condition in both switches.