Skip to content

Missed optimization for switches using getelementptr #169919

@OutOfCache

Description

@OutOfCache

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:

  1. There are multiple users
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions