Skip to content

Commit e9205ca

Browse files
authored
[SimplifyCFG] Remove all incoming values from OtherDest if OtherDest is unreachable (#162677)
Fixes #162585. #161000 changed `br i1 true, label %if, label %else` to `br label %if`, so we should remove one more incoming value.
1 parent ab897ae commit e9205ca

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5959,7 +5959,11 @@ bool SimplifyCFGOpt::turnSwitchRangeIntoICmp(SwitchInst *SI,
59595959
unsigned PreviousEdges = OtherCases->size();
59605960
if (OtherDest == SI->getDefaultDest())
59615961
++PreviousEdges;
5962-
for (unsigned I = 0, E = PreviousEdges - 1; I != E; ++I)
5962+
unsigned E = PreviousEdges - 1;
5963+
// Remove all incoming values from OtherDest if OtherDest is unreachable.
5964+
if (NewBI->isUnconditional())
5965+
++E;
5966+
for (unsigned I = 0; I != E; ++I)
59635967
cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
59645968
}
59655969

llvm/test/Transforms/SimplifyCFG/switch-range-to-icmp.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,27 @@ b:
401401
ret i32 %1
402402
}
403403

404+
define i32 @else_will_be_unreachable(i1 %arg) {
405+
; CHECK-LABEL: @else_will_be_unreachable(
406+
; CHECK-NEXT: entry:
407+
; CHECK-NEXT: [[I:%.*]] = select i1 [[ARG:%.*]], i32 0, i32 1
408+
; CHECK-NEXT: ret i32 [[I]]
409+
;
410+
entry:
411+
switch i1 %arg, label %else [
412+
i1 false, label %if
413+
i1 true, label %if
414+
]
415+
416+
if:
417+
br i1 %arg, label %else, label %bb
418+
419+
bb:
420+
br label %else
421+
422+
else:
423+
%i = phi i32 [ 0, %entry ], [ 0, %if ], [ 1, %bb ]
424+
ret i32 %i
425+
}
426+
404427
declare void @bar(ptr nonnull dereferenceable(4))

0 commit comments

Comments
 (0)