Index: lib/Transforms/Scalar/LoopUnswitch.cpp =================================================================== --- lib/Transforms/Scalar/LoopUnswitch.cpp +++ lib/Transforms/Scalar/LoopUnswitch.cpp @@ -1060,15 +1060,21 @@ } if (LandingPadInst *LPad = NewExit->getLandingPadInst()) { - PHINode *PN = PHINode::Create(LPad->getType(), 0, "", - &*ExitSucc->getFirstInsertionPt()); - - for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc); - I != E; ++I) { - BasicBlock *BB = *I; - LandingPadInst *LPI = BB->getLandingPadInst(); - LPI->replaceAllUsesWith(PN); - PN->addIncoming(LPI, BB); + auto *OriginalLPad = ExitBlocks[i]->getLandingPadInst(); + if (!OriginalLPad->use_empty()) { + assert(!LPad->getType()->isTokenTy() && + "Loop unswitch cannot be applied if LPad is token type. " + "Otherwise an invalid PHINode of token type would be created."); + PHINode *PN = PHINode::Create(LPad->getType(), 0, "", + &*ExitSucc->getFirstInsertionPt()); + + for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc); + I != E; ++I) { + BasicBlock *BB = *I; + LandingPadInst *LPI = BB->getLandingPadInst(); + LPI->replaceAllUsesWith(PN); + PN->addIncoming(LPI, BB); + } } } } Index: test/Transforms/LoopUnswitch/token.ll =================================================================== --- /dev/null +++ test/Transforms/LoopUnswitch/token.ll @@ -0,0 +1,43 @@ +; RUN: opt < %s -loop-unswitch -S 2>&1 | FileCheck %s + +define void @test1(i1 %cond) personality i32 ()* @"dummy_personality" { +entry: + br label %for.cond + +for.cond: + call void @dummy_call() + br i1 %cond, label %for.body, label %for.end + +for.body: + invoke void @dummy_invoke() + to label %invoke.cont unwind label %lpad + +invoke.cont: + br label %for.inc + +lpad: + %a = landingpad token + cleanup + unreachable + +for.inc: + br label %for.cond + +for.end: + ret void +} + +; CHECK: lpad.us-lcssa.us: ; preds = %for.body.us +; CHECK-NEXT: %lpad.us-lcssa1.us = landingpad token + +; CHECK: lpad.us-lcssa: ; preds = %for.body +; CHECK-NEXT: %lpad.us-lcssa1 = landingpad token + +; CHECK: lpad: ; preds = %lpad.us-lcssa.us, %lpad.us-lcssa +; CHECK-NOT: phi token + +declare void @dummy_call() + +declare void @dummy_invoke() + +declare i32 @"dummy_personality"()