Skip to content

[SimplifyCFG] eliminateDeadSwitchCases does not eliminate dead branches. #78281

Open
@dianqk

Description

@dianqk

I tried the following code, which I expect to be optimized.

define i32 @src(i32 noundef %0) {
  %2 = icmp eq i32 %0, 0
  %3 = icmp eq i32 %0, 1
  %foo = zext i1 %3 to i32
  %bar = select i1 %2, i32 -1, i32 %foo
  switch i32 %bar, label %4 [
    i32 0, label %7
    i32 -1, label %7
  ]

4:                                                ; preds = %1
  %5 = icmp eq i32 %0, 2
  %6 = zext i1 %5 to i32
  br label %7

7:                                                ; preds = %4, %1, %1
  %8 = phi i32 [ %6, %4 ], [ 1, %1 ], [ 1, %1 ]
  ret i32 %8
}

define i32 @tgt(i32 noundef %0) {
  %.not = icmp ne i32 %0, 1
  %2 = zext i1 %.not to i32
  ret i32 %2
}

Alive2: https://alive2.llvm.org/ce/z/zyMuXt

Similar to #73446. But it's more complicated.
This should be caused by KnownBits.

We know that this %bar can only be -1, 0 and 1. So we can transform the default branch to a branch with a specific value.
There are several variants of this issue, such as replacing -1 with some other value and increasing the number of cases.

From: rust-lang/rust#119520.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions