Index: include/llvm/IR/Instructions.h =================================================================== --- include/llvm/IR/Instructions.h +++ include/llvm/IR/Instructions.h @@ -2738,6 +2738,16 @@ block_begin()[i] = BB; } + /// Replace every incoming basic block \p Old to basic block \p New. + void replaceIncomingBlockWith(BasicBlock *Old, BasicBlock *New) { + assert(New && Old && "PHI node got a null basic block!"); + for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) { + BasicBlock *Curr = getIncomingBlock(Op); + if (Curr == Old) + setIncomingBlock(Op, New); + } + } + /// Add an incoming value to the end of the PHI list /// void addIncoming(Value *V, BasicBlock *BB) { Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -7226,11 +7226,8 @@ std::swap(TBB, FBB); // Replace the old BB with the new BB. - for (PHINode &PN : TBB->phis()) { - int i; - while ((i = PN.getBasicBlockIndex(&BB)) >= 0) - PN.setIncomingBlock(i, TmpBB); - } + for (PHINode &PN : TBB->phis()) + PN.replaceIncomingBlockWith(&BB, TmpBB); // Add another incoming edge form the new BB. for (PHINode &PN : FBB->phis()) { Index: lib/IR/BasicBlock.cpp =================================================================== --- lib/IR/BasicBlock.cpp +++ lib/IR/BasicBlock.cpp @@ -431,13 +431,8 @@ // Loop over any phi nodes in the basic block, updating the BB field of // incoming values... BasicBlock *Successor = *I; - for (auto &PN : Successor->phis()) { - int Idx = PN.getBasicBlockIndex(this); - while (Idx != -1) { - PN.setIncomingBlock((unsigned)Idx, New); - Idx = PN.getBasicBlockIndex(this); - } - } + for (auto &PN : Successor->phis()) + PN.replaceIncomingBlockWith(this, New); } return New; } @@ -455,9 +450,7 @@ PHINode *PN = dyn_cast(II); if (!PN) break; - int i; - while ((i = PN->getBasicBlockIndex(this)) >= 0) - PN->setIncomingBlock(i, New); + PN->replaceIncomingBlockWith(this, New); } } } Index: lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp =================================================================== --- lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp +++ lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp @@ -645,9 +645,7 @@ void LoopConstrainer::replacePHIBlock(PHINode *PN, BasicBlock *Block, BasicBlock *ReplaceBy) { - for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) - if (PN->getIncomingBlock(i) == Block) - PN->setIncomingBlock(i, ReplaceBy); + PN->replaceIncomingBlockWith(Block, ReplaceBy); } /// Given a loop with an deccreasing induction variable, is it possible to Index: lib/Transforms/Scalar/LoopInterchange.cpp =================================================================== --- lib/Transforms/Scalar/LoopInterchange.cpp +++ lib/Transforms/Scalar/LoopInterchange.cpp @@ -1281,13 +1281,8 @@ static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred, BasicBlock *NewPred) { - for (PHINode &PHI : CurrBlock->phis()) { - unsigned Num = PHI.getNumIncomingValues(); - for (unsigned i = 0; i < Num; ++i) { - if (PHI.getIncomingBlock(i) == OldPred) - PHI.setIncomingBlock(i, NewPred); - } - } + for (PHINode &PHI : CurrBlock->phis()) + PHI.replaceIncomingBlockWith(OldPred, NewPred); } /// Update BI to jump to NewBB instead of OldBB. Records updates to