llvm.org GIT mirror llvm / fb75d57
[NFC] BasicBlock: refactor changePhiUses() out of replacePhiUsesWith(), use it Summary: It is a common thing to loop over every `PHINode` in some `BasicBlock` and change old `BasicBlock` incoming block to a new `BasicBlock` incoming block. `replaceSuccessorsPhiUsesWith()` already had code to do that, it just wasn't a function. So outline it into a new function, and use it. Reviewers: chandlerc, craig.topper, spatel, danielcdh Reviewed By: craig.topper Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61013 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359996 91177308-0d34-0410-b5e6-96231b3b80d8 Roman Lebedev 1 year, 5 months ago
5 changed file(s) with 32 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
389389 /// direct branches, switches, etc. to it.
390390 bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
391391
392 /// Update all phi nodes in this basic block to refer to basic block \p New
393 /// instead of basic block \p Old.
394 void replacePhiUsesWith(BasicBlock *Old, BasicBlock *New);
395
392396 /// Update all phi nodes in this basic block's successors to refer to basic
393397 /// block \p New instead of to it.
394398 void replaceSuccessorsPhiUsesWith(BasicBlock *New);
72207220 std::swap(TBB, FBB);
72217221
72227222 // Replace the old BB with the new BB.
7223 for (PHINode &PN : TBB->phis())
7224 PN.replaceIncomingBlockWith(&BB, TmpBB);
7223 TBB->replacePhiUsesWith(&BB, TmpBB);
72257224
72267225 // Add another incoming edge form the new BB.
72277226 for (PHINode &PN : FBB->phis()) {
426426 // successors. If there were PHI nodes in the successors, then they need to
427427 // know that incoming branches will be from New, not from Old.
428428 //
429 for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
430 // Loop over any phi nodes in the basic block, updating the BB field of
431 // incoming values...
432 BasicBlock *Successor = *I;
433 for (auto &PN : Successor->phis())
434 PN.replaceIncomingBlockWith(this, New);
435 }
429 llvm::for_each(successors(New), [this, New](BasicBlock *Succ) {
430 Succ->replacePhiUsesWith(this, New);
431 });
436432 return New;
433 }
434
435 void BasicBlock::replacePhiUsesWith(BasicBlock *Old, BasicBlock *New) {
436 // N.B. This might not be a complete BasicBlock, so don't assume
437 // that it ends with a non-phi instruction.
438 for (iterator II = begin(), IE = end(); II != IE; ++II) {
439 PHINode *PN = dyn_cast(II);
440 if (!PN)
441 break;
442 PN->replaceIncomingBlockWith(Old, New);
443 }
437444 }
438445
439446 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
442449 // Cope with being called on a BasicBlock that doesn't have a terminator
443450 // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
444451 return;
445 for (BasicBlock *Succ : successors(TI)) {
446 // N.B. Succ might not be a complete BasicBlock, so don't assume
447 // that it ends with a non-phi instruction.
448 for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) {
449 PHINode *PN = dyn_cast(II);
450 if (!PN)
451 break;
452 PN->replaceIncomingBlockWith(this, New);
453 }
454 }
452 llvm::for_each(successors(TI), [this, New](BasicBlock *Succ) {
453 Succ->replacePhiUsesWith(this, New);
454 });
455455 }
456456
457457 /// Return true if this basic block is a landing pad. I.e., it's
13241324
13251325 // The latch exit now has a branch from `RRI.ExitSelector' instead of
13261326 // `LS.Latch'. The PHI nodes need to be updated to reflect that.
1327 for (PHINode &PN : LS.LatchExit->phis())
1328 PN.replaceIncomingBlockWith(LS.Latch, RRI.ExitSelector);
1327 LS.LatchExit->replacePhiUsesWith(LS.Latch, RRI.ExitSelector);
13291328
13301329 return RRI;
13311330 }
13481347 BasicBlock *Preheader = BasicBlock::Create(Ctx, Tag, &F, LS.Header);
13491348 BranchInst::Create(LS.Header, Preheader);
13501349
1351 for (PHINode &PN : LS.Header->phis())
1352 PN.replaceIncomingBlockWith(OldPreheader, Preheader);
1350 LS.Header->replacePhiUsesWith(OldPreheader, Preheader);
13531351
13541352 return Preheader;
13551353 }
12781278 FromBB->getTerminator()->getIterator());
12791279 }
12801280
1281 static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred,
1282 BasicBlock *NewPred) {
1283 for (PHINode &PHI : CurrBlock->phis())
1284 PHI.replaceIncomingBlockWith(OldPred, NewPred);
1285 }
1286
12871281 /// Update BI to jump to NewBB instead of OldBB. Records updates to
12881282 /// the dominator tree in DTUpdates, if DT should be preserved.
12891283 static void updateSuccessor(BranchInst *BI, BasicBlock *OldBB,
13481342 // Now adjust the incoming blocks for the LCSSA PHIs.
13491343 // For PHIs moved from Inner's exit block, we need to replace Inner's latch
13501344 // with the new latch.
1351 updateIncomingBlock(InnerLatch, InnerLatch, OuterLatch);
1345 InnerLatch->replacePhiUsesWith(InnerLatch, OuterLatch);
13521346 }
13531347
13541348 bool LoopInterchangeTransform::adjustLoopBranches() {
14151409 InnerLoopHeaderSuccessor, DTUpdates);
14161410
14171411 // Adjust reduction PHI's now that the incoming block has changed.
1418 updateIncomingBlock(InnerLoopHeaderSuccessor, InnerLoopHeader,
1419 OuterLoopHeader);
1412 InnerLoopHeaderSuccessor->replacePhiUsesWith(InnerLoopHeader,
1413 OuterLoopHeader);
14201414
14211415 updateSuccessor(InnerLoopHeaderBI, InnerLoopHeaderSuccessor,
14221416 OuterLoopPreHeader, DTUpdates);
14481442 moveLCSSAPhis(InnerLoopLatchSuccessor, InnerLoopLatch, OuterLoopLatch);
14491443 // For PHIs in the exit block of the outer loop, outer's latch has been
14501444 // replaced by Inners'.
1451 updateIncomingBlock(OuterLoopLatchSuccessor, OuterLoopLatch, InnerLoopLatch);
1445 OuterLoopLatchSuccessor->replacePhiUsesWith(OuterLoopLatch, InnerLoopLatch);
14521446
14531447 // Now update the reduction PHIs in the inner and outer loop headers.
14541448 SmallVector InnerLoopPHIs, OuterLoopPHIs;
14751469 }
14761470
14771471 // Update the incoming blocks for moved PHI nodes.
1478 updateIncomingBlock(OuterLoopHeader, InnerLoopPreHeader, OuterLoopPreHeader);
1479 updateIncomingBlock(OuterLoopHeader, InnerLoopLatch, OuterLoopLatch);
1480 updateIncomingBlock(InnerLoopHeader, OuterLoopPreHeader, InnerLoopPreHeader);
1481 updateIncomingBlock(InnerLoopHeader, OuterLoopLatch, InnerLoopLatch);
1472 OuterLoopHeader->replacePhiUsesWith(InnerLoopPreHeader, OuterLoopPreHeader);
1473 OuterLoopHeader->replacePhiUsesWith(InnerLoopLatch, OuterLoopLatch);
1474 InnerLoopHeader->replacePhiUsesWith(OuterLoopPreHeader, InnerLoopPreHeader);
1475 InnerLoopHeader->replacePhiUsesWith(OuterLoopLatch, InnerLoopLatch);
14821476
14831477 return true;
14841478 }