Index: include/llvm/IR/BasicBlock.h =================================================================== --- include/llvm/IR/BasicBlock.h +++ include/llvm/IR/BasicBlock.h @@ -390,6 +390,10 @@ /// direct branches, switches, etc. to it. bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } + /// Update all phi nodes in this basic block to refer to basic block \p New + /// instead of basic block \p Old. + void changePhiUses(BasicBlock *Old, BasicBlock *New); + /// Update all phi nodes in this basic block's successors to refer to basic /// block \p New instead of to it. void replaceSuccessorsPhiUsesWith(BasicBlock *New); Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -7226,8 +7226,7 @@ std::swap(TBB, FBB); // Replace the old BB with the new BB. - for (PHINode &PN : TBB->phis()) - PN.changeIncomingBlock(&BB, TmpBB); + TBB->changePhiUses(&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 @@ -437,22 +437,26 @@ return New; } +void BasicBlock::changePhiUses(BasicBlock *Old, BasicBlock *New) { + // N.B. This might not be a complete BasicBlock, so don't assume + // that it ends with a non-phi instruction. + for (iterator II = begin(), IE = end(); II != IE; ++II) { + PHINode *PN = dyn_cast(II); + if (!PN) + break; + PN->changeIncomingBlock(Old, New); + } +} + void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { Instruction *TI = getTerminator(); if (!TI) // Cope with being called on a BasicBlock that doesn't have a terminator // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this. return; - for (BasicBlock *Succ : successors(TI)) { - // N.B. Succ might not be a complete BasicBlock, so don't assume - // that it ends with a non-phi instruction. - for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) { - PHINode *PN = dyn_cast(II); - if (!PN) - break; - PN->changeIncomingBlock(this, New); - } - } + llvm::for_each(successors(TI), [this, New](BasicBlock *Succ) { + Succ->changePhiUses(this, New); + }); } /// Return true if this basic block is a landing pad. I.e., it's Index: lib/Transforms/Scalar/LoopInterchange.cpp =================================================================== --- lib/Transforms/Scalar/LoopInterchange.cpp +++ lib/Transforms/Scalar/LoopInterchange.cpp @@ -1281,8 +1281,7 @@ static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred, BasicBlock *NewPred) { - for (PHINode &PHI : CurrBlock->phis()) - PHI.changeIncomingBlock(OldPred, NewPred); + CurrBlock->changePhiUses(OldPred, NewPred); } /// Update BI to jump to NewBB instead of OldBB. Records updates to