Skip to content

[SimplifyCFG] Failure to sink if one branch simplifies #118548

@nikic

Description

@nikic

https://alive2.llvm.org/ce/z/7GaUcf

define ptr @src(ptr %p, i64 %x) {
entry:
  switch i64 %x, label %unreachable [
    i64 0, label %exit
    i64 1, label %bb1
    i64 2, label %bb2
  ]

bb1:
  %gep1 = getelementptr inbounds nuw i8, ptr %p, i64 4
  br label %exit

bb2:
  %gep2 = getelementptr inbounds nuw i8, ptr %p, i64 8
  br label %exit

unreachable:
  unreachable

exit:
  %phi = phi ptr [ %gep1, %bb1 ], [ %gep2, %bb2 ], [ %p, %entry ]
  ret ptr %phi
}

define ptr @tgt(ptr %p, i64 %x) {
entry:
  switch i64 %x, label %unreachable [
    i64 0, label %exit
    i64 1, label %bb1
    i64 2, label %bb2
  ]

bb1:
  br label %exit

bb2:
  br label %exit

unreachable:
  unreachable

exit:
  %phi = phi i64 [ 4, %bb1 ], [ 8, %bb2 ], [ 0, %entry ]
  %gep = getelementptr inbounds nuw i8, ptr %p, i64 %phi
  ret ptr %gep
}

We can sink this if we treat the default branch as a getelementptr inbounds nuw i8, ptr %p, i64 0 and subsequently fold the switch to arithmetic. Not sure how profitable this would be in general, but it's certainly profitable if it allows us to fold away the switch.

This is kind of similar to the copyable element problem in SLP.

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