Index: lib/Transforms/Scalar/LoopSimplifyCFG.cpp =================================================================== --- lib/Transforms/Scalar/LoopSimplifyCFG.cpp +++ lib/Transforms/Scalar/LoopSimplifyCFG.cpp @@ -242,8 +242,11 @@ SmallPtrSet DeadSuccessors; // Remove all BB's successors except for the live one. for (auto *Succ : successors(BB)) - if (Succ != TheOnlySucc) + if (Succ != TheOnlySucc) { DeadSuccessors.insert(Succ); + for (PHINode &Phi : Succ->phis()) + Phi.removeIncomingValue(BB); + } IRBuilder<> Builder(BB->getContext()); Instruction *Term = BB->getTerminator(); Index: test/Transforms/LoopSimplifyCFG/fold-terminator-phis.ll =================================================================== --- /dev/null +++ test/Transforms/LoopSimplifyCFG/fold-terminator-phis.ll @@ -0,0 +1,27 @@ +; RUN: opt -loop-simplifycfg -S < %s | FileCheck %s + +; CHECK-LABEL: repo +; CHECK: %inc = add i32 %i, 2 +define void @repo(i32 %size) { +entry: + br label %loop + +loop: + %i = phi i32 [ 0, %entry ], [ %inc, %loop2 ] + br i1 true, label %loop1, label %loop2 + +loop1: + call void @something() + br label %loop2 + +loop2: + %phi = phi i32 [ 1, %loop ], [ 2, %loop1 ] + %inc = add i32 %i, %phi + %cmp = icmp ult i32 %inc, 3900 + br i1 %cmp, label %loop, label %end + +end: + ret void +} + +declare void @something()