llvm.org GIT mirror llvm / 14d59d2
[NFC] PHINode: introduce replaceIncomingBlockWith() function, use it Summary: There is `PHINode::getBasicBlockIndex()`, `PHINode::setIncomingBlock()` and `PHINode::getNumOperands()`, but no function to replace every specified `BasicBlock*` predecessor with some other specified `BasicBlock*`. Clearly, there are a lot of places that could use that functionality. Reviewers: chandlerc, craig.topper, spatel, danielcdh Reviewed By: craig.topper Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61011 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359995 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev 5 months ago
5 changed file(s) with 17 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
27372737 block_begin()[i] = BB;
27382738 }
27392739
2740 /// Replace every incoming basic block \p Old to basic block \p New.
2741 void replaceIncomingBlockWith(BasicBlock *Old, BasicBlock *New) {
2742 assert(New && Old && "PHI node got a null basic block!");
2743 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2744 if (getIncomingBlock(Op) == Old)
2745 setIncomingBlock(Op, New);
2746 }
2747
27402748 /// Add an incoming value to the end of the PHI list
27412749 ///
27422750 void addIncoming(Value *V, BasicBlock *BB) {
72207220 std::swap(TBB, FBB);
72217221
72227222 // Replace the old BB with the new BB.
7223 for (PHINode &PN : TBB->phis()) {
7224 int i;
7225 while ((i = PN.getBasicBlockIndex(&BB)) >= 0)
7226 PN.setIncomingBlock(i, TmpBB);
7227 }
7223 for (PHINode &PN : TBB->phis())
7224 PN.replaceIncomingBlockWith(&BB, TmpBB);
72287225
72297226 // Add another incoming edge form the new BB.
72307227 for (PHINode &PN : FBB->phis()) {
430430 // Loop over any phi nodes in the basic block, updating the BB field of
431431 // incoming values...
432432 BasicBlock *Successor = *I;
433 for (auto &PN : Successor->phis()) {
434 int Idx = PN.getBasicBlockIndex(this);
435 while (Idx != -1) {
436 PN.setIncomingBlock((unsigned)Idx, New);
437 Idx = PN.getBasicBlockIndex(this);
438 }
439 }
433 for (auto &PN : Successor->phis())
434 PN.replaceIncomingBlockWith(this, New);
440435 }
441436 return New;
442437 }
454449 PHINode *PN = dyn_cast(II);
455450 if (!PN)
456451 break;
457 int i;
458 while ((i = PN->getBasicBlockIndex(this)) >= 0)
459 PN->setIncomingBlock(i, New);
452 PN->replaceIncomingBlockWith(this, New);
460453 }
461454 }
462455 }
535535 Optional HighLimit;
536536 };
537537
538 // A utility function that does a `replaceUsesOfWith' on the incoming block
539 // set of a `PHINode' -- replaces instances of `Block' in the `PHINode's
540 // incoming block list with `ReplaceBy'.
541 static void replacePHIBlock(PHINode *PN, BasicBlock *Block,
542 BasicBlock *ReplaceBy);
543
544538 // Compute a safe set of limits for the main loop to run in -- effectively the
545539 // intersection of `Range' and the iteration space of the original loop.
546540 // Return None if unable to compute the set of subranges.
641635 };
642636
643637 } // end anonymous namespace
644
645 void LoopConstrainer::replacePHIBlock(PHINode *PN, BasicBlock *Block,
646 BasicBlock *ReplaceBy) {
647 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
648 if (PN->getIncomingBlock(i) == Block)
649 PN->setIncomingBlock(i, ReplaceBy);
650 }
651638
652639 /// Given a loop with an deccreasing induction variable, is it possible to
653640 /// safely calculate the bounds of a new loop using the given Predicate.
13381325 // The latch exit now has a branch from `RRI.ExitSelector' instead of
13391326 // `LS.Latch'. The PHI nodes need to be updated to reflect that.
13401327 for (PHINode &PN : LS.LatchExit->phis())
1341 replacePHIBlock(&PN, LS.Latch, RRI.ExitSelector);
1328 PN.replaceIncomingBlockWith(LS.Latch, RRI.ExitSelector);
13421329
13431330 return RRI;
13441331 }
13621349 BranchInst::Create(LS.Header, Preheader);
13631350
13641351 for (PHINode &PN : LS.Header->phis())
1365 for (unsigned i = 0, e = PN.getNumIncomingValues(); i < e; ++i)
1366 replacePHIBlock(&PN, OldPreheader, Preheader);
1352 PN.replaceIncomingBlockWith(OldPreheader, Preheader);
13671353
13681354 return Preheader;
13691355 }
12801280
12811281 static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred,
12821282 BasicBlock *NewPred) {
1283 for (PHINode &PHI : CurrBlock->phis()) {
1284 unsigned Num = PHI.getNumIncomingValues();
1285 for (unsigned i = 0; i < Num; ++i) {
1286 if (PHI.getIncomingBlock(i) == OldPred)
1287 PHI.setIncomingBlock(i, NewPred);
1288 }
1289 }
1283 for (PHINode &PHI : CurrBlock->phis())
1284 PHI.replaceIncomingBlockWith(OldPred, NewPred);
12901285 }
12911286
12921287 /// Update BI to jump to NewBB instead of OldBB. Records updates to