llvm.org GIT mirror llvm / 9254ae1
LowerSwitch: Fix producing invalid IR on unreachable code If a switch was in an unreachable block that branched to a block with a phi, it would leave phis with missing predecessors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301064 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 2 years ago
2 changed file(s) with 48 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
402402 Value *Val = SI->getCondition(); // The value we are switching on...
403403 BasicBlock* Default = SI->getDefaultDest();
404404
405 // Don't handle unreachable blocks. If there are successors with phis, this
406 // would leave them behind with missing predecessors.
407 if ((CurBlock != &F->getEntryBlock() && pred_empty(CurBlock)) ||
408 CurBlock->getSinglePredecessor() == CurBlock) {
409 DeleteList.insert(CurBlock);
410 return;
411 }
412
405413 // If there is only the default destination, just branch.
406414 if (!SI->getNumCases()) {
407415 BranchInst::Create(Default, CurBlock);
0 ; RUN: opt -S -lowerswitch %s | FileCheck %s
1
2 ; CHECK-LABEL: @phi_in_dead_block(
3 ; CHECK-NOT: switch
4 define void @phi_in_dead_block() {
5 bb:
6 br i1 undef, label %bb2, label %bb3
7
8 bb1: ; No predecessors!
9 switch i32 undef, label %bb2 [
10 i32 9, label %bb3
11 ]
12
13 bb2: ; preds = %bb1, %bb
14 %tmp = phi i64 [ undef, %bb1 ], [ undef, %bb ]
15 unreachable
16
17 bb3: ; preds = %bb1, %bb
18 unreachable
19 }
20
21 ; CHECK-LABEL: @phi_in_dead_block_br_to_self(
22 ; CHECK-NOT: switch
23 define void @phi_in_dead_block_br_to_self() {
24 bb:
25 br i1 undef, label %bb2, label %bb3
26
27 bb1: ; No predecessors!
28 switch i32 undef, label %bb2 [
29 i32 9, label %bb3
30 i32 10, label %bb1
31 ]
32
33 bb2: ; preds = %bb1, %bb
34 %tmp = phi i64 [ undef, %bb1 ], [ undef, %bb ]
35 unreachable
36
37 bb3: ; preds = %bb1, %bb
38 unreachable
39 }