llvm.org GIT mirror llvm / f5f2c97
[Utils] Cleanup similar cases to MergeBlockIntoPredecessor. Summary: There are two cases where a block is merged into its predecessor and the MergeBlockIntoPredecessor API is not used. Update the API so it can be reused in the other cases, in order to avoid code duplication. Cleanup motivated by D68659. Reviewers: chandlerc, sanjoy.google, george.burgess.iv Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68670 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375050 91177308-0d34-0410-b5e6-96231b3b80d8 Alina Sbirlea 1 year, 1 month ago
4 changed file(s) with 57 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
8282
8383 /// Attempts to merge a block into its predecessor, if possible. The return
8484 /// value indicates success or failure.
85 /// By default do not merge blocks if BB's predecessor has multiple successors.
86 /// If PredecessorWithTwoSuccessors = true, the blocks can only be merged
87 /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single
88 /// successor Sing. In this case the branch will be updated with Sing instead of
89 /// BB, and BB will still be merged into its predecessor and removed.
8590 bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr,
8691 LoopInfo *LI = nullptr,
8792 MemorySSAUpdater *MSSAU = nullptr,
88 MemoryDependenceResults *MemDep = nullptr);
93 MemoryDependenceResults *MemDep = nullptr,
94 bool PredecessorWithTwoSuccessors = false);
8995
9096 /// Replace all uses of an instruction (specified by BI) with a value, then
9197 /// remove and delete the original instruction.
16201620 if (!SinglePred) continue; // Nothing to do.
16211621 assert(SinglePred == Pred && "CFG broken");
16221622
1623 LLVM_DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- "
1624 << Succ->getName() << "\n");
1625
1626 // Resolve any single entry PHI nodes in Succ.
1627 while (PHINode *PN = dyn_cast(Succ->begin()))
1628 ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM,
1629 MSSAU.get());
1630
1631 Instruction *STI = Succ->getTerminator();
1632 Instruction *Start = &*Succ->begin();
1633 // If there's nothing to move, mark the starting instruction as the last
1634 // instruction in the block.
1635 if (Start == STI)
1636 Start = BI;
1637
1638 // Move all of the successor contents from Succ to Pred.
1639 Pred->getInstList().splice(BI->getIterator(), Succ->getInstList(),
1640 Succ->begin(), STI->getIterator());
1641 if (MSSAU)
1642 MSSAU->moveAllAfterMergeBlocks(Succ, Pred, Start);
1643
1644 // Move terminator instruction from Succ now, we're deleting BI below.
1645 // FIXME: remove BI first might be more intuitive.
1646 Pred->getInstList().splice(Pred->end(), Succ->getInstList());
1647
1648 // If Succ has any successors with PHI nodes, update them to have
1649 // entries coming from Pred instead of Succ.
1650 Succ->replaceAllUsesWith(Pred);
1651
1623 // Make the LPM and Worklist updates specific to LoopUnswitch.
16521624 LPM->deleteSimpleAnalysisValue(BI, L);
16531625 RemoveFromWorklist(BI, Worklist);
1654 BI->eraseFromParent();
1655
1656 // Remove Succ from the loop tree.
1657 LI->removeBlock(Succ);
16581626 LPM->deleteSimpleAnalysisValue(Succ, L);
1659 Succ->eraseFromParent();
1627 auto SuccIt = Succ->begin();
1628 while (PHINode *PN = dyn_cast(SuccIt++)) {
1629 for (unsigned It = 0, E = PN->getNumOperands(); It != E; ++It)
1630 if (Instruction *Use = dyn_cast(PN->getOperand(It)))
1631 Worklist.push_back(Use);
1632 for (User *U : PN->users())
1633 Worklist.push_back(cast(U));
1634 LPM->deleteSimpleAnalysisValue(PN, L);
1635 RemoveFromWorklist(PN, Worklist);
1636 ++NumSimplify;
1637 }
1638 // Merge the block and make the remaining analyses updates.
1639 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
1640 MergeBlockIntoPredecessor(Succ, &DTU, LI, MSSAU.get());
16601641 ++NumSimplify;
16611642 continue;
16621643 }
169169
170170 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
171171 LoopInfo *LI, MemorySSAUpdater *MSSAU,
172 MemoryDependenceResults *MemDep) {
172 MemoryDependenceResults *MemDep,
173 bool PredecessorWithTwoSuccessors) {
173174 if (BB->hasAddressTaken())
174175 return false;
175176
184185 return false;
185186
186187 // Can't merge if there are multiple distinct successors.
187 if (PredBB->getUniqueSuccessor() != BB)
188 if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
188189 return false;
190
191 // Currently only allow PredBB to have two predecessors, one being BB.
192 // Update BI to branch to BB's only successor instead of BB.
193 BranchInst *PredBB_BI;
194 BasicBlock *NewSucc = nullptr;
195 unsigned FallThruPath;
196 if (PredecessorWithTwoSuccessors) {
197 if (!(PredBB_BI = dyn_cast(PredBB->getTerminator())))
198 return false;
199 BranchInst *BB_JmpI = dyn_cast(BB->getTerminator());
200 if (!BB_JmpI || !BB_JmpI->isUnconditional())
201 return false;
202 NewSucc = BB_JmpI->getSuccessor(0);
203 FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;
204 }
189205
190206 // Can't merge if there is PHI loop.
191207 for (PHINode &PN : BB->phis())
245261 // source...
246262 BB->replaceAllUsesWith(PredBB);
247263
248 // Delete the unconditional branch from the predecessor...
249 PredBB->getInstList().pop_back();
250
251 // Move terminator instruction and add unreachable to now empty BB.
252 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
264 if (PredecessorWithTwoSuccessors) {
265 // Delete the unconditional branch from BB.
266 BB->getInstList().pop_back();
267
268 // Update branch in the predecessor.
269 PredBB_BI->setSuccessor(FallThruPath, NewSucc);
270 } else {
271 // Delete the unconditional branch from the predecessor.
272 PredBB->getInstList().pop_back();
273
274 // Move terminator instruction.
275 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
276 }
277 // Add unreachable to now empty BB.
253278 new UnreachableInst(BB->getContext(), BB);
254279
255280 // Eliminate duplicate dbg.values describing the entry PHI node post-splice.
614614 LLVM_DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into "
615615 << LastExit->getName() << "\n");
616616
617 Instruction *FirstLatchInst = &*Latch->begin();
618 // If there's nothing to move, mark the starting instruction as the last
619 // instruction in the block.
620 if (FirstLatchInst == Jmp)
621 FirstLatchInst = BI;
622
623 // Hoist the instructions from Latch into LastExit.
624 LastExit->getInstList().splice(BI->getIterator(), Latch->getInstList(),
625 Latch->begin(), Jmp->getIterator());
626
627 // Update MemorySSA
628 if (MSSAU)
629 MSSAU->moveAllAfterMergeBlocks(Latch, LastExit, FirstLatchInst);
630
631 unsigned FallThruPath = BI->getSuccessor(0) == Latch ? 0 : 1;
632 BasicBlock *Header = Jmp->getSuccessor(0);
633 assert(Header == L->getHeader() && "expected a backward branch");
634
635 // Remove Latch from the CFG so that LastExit becomes the new Latch.
636 BI->setSuccessor(FallThruPath, Header);
637 Latch->replaceSuccessorsPhiUsesWith(LastExit);
638 Jmp->eraseFromParent();
639
640 // Nuke the Latch block.
641 assert(Latch->empty() && "unable to evacuate Latch");
642 LI->removeBlock(Latch);
643 if (DT)
644 DT->eraseNode(Latch);
645 Latch->eraseFromParent();
617 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
618 MergeBlockIntoPredecessor(Latch, &DTU, LI, MSSAU, nullptr,
619 /*PredecessorWithTwoSuccessors=*/true);
646620
647621 if (MSSAU && VerifyMemorySSA)
648622 MSSAU->getMemorySSA()->verifyMemorySSA();