Index: lib/Transforms/Scalar/LICM.cpp =================================================================== --- lib/Transforms/Scalar/LICM.cpp +++ lib/Transforms/Scalar/LICM.cpp @@ -597,23 +597,28 @@ // PHI nodes in exit blocks due to LCSSA form. Just RAUW them with clones of // the instruction. while (!I.use_empty()) { - // The user must be a PHI node. - PHINode *PN = cast(I.user_back()); - - BasicBlock *ExitBlock = PN->getParent(); - assert(ExitBlockSet.count(ExitBlock) && - "The LCSSA PHI is not in an exit block!"); - - Instruction *New; - auto It = SunkCopies.find(ExitBlock); - if (It != SunkCopies.end()) - New = It->second; - else - New = SunkCopies[ExitBlock] = - CloneInstructionInExitBlock(I, *ExitBlock, *PN); - - PN->replaceAllUsesWith(New); - PN->eraseFromParent(); + Instruction *User = I.user_back(); + if (DT->isReachableFromEntry(User->getParent())) { + // The user must be a PHI node. + PHINode *PN = cast(User); + + BasicBlock *ExitBlock = PN->getParent(); + assert(ExitBlockSet.count(ExitBlock) && + "The LCSSA PHI is not in an exit block!"); + + Instruction *New; + auto It = SunkCopies.find(ExitBlock); + if (It != SunkCopies.end()) + New = It->second; + else + New = SunkCopies[ExitBlock] = + CloneInstructionInExitBlock(I, *ExitBlock, *PN); + + PN->replaceAllUsesWith(New); + PN->eraseFromParent(); + } else { + User->replaceUsesOfWith(&I, UndefValue::get(I.getType())); + } } CurAST->deleteValue(&I); Index: test/Transforms/LICM/PR19798.ll =================================================================== --- /dev/null +++ test/Transforms/LICM/PR19798.ll @@ -0,0 +1,22 @@ +; RUN: opt -licm -S < %s | FileCheck %s + +define void @f() { +; CHECK-LABEL: @f( +entry: + br label %bb0 + +bb0: + %tobool7 = icmp eq i1 undef, undef + br label %bb1 + +bb1: + br i1 undef, label %bb0, label %bb0 + +unreachable: +; CHECK-LABEL: unreachable: +; CHECK: br i1 undef, label %unreachable, label %unreachable + br i1 %tobool7, label %unreachable, label %unreachable + +bb3: + unreachable +}