BreakCriticalEdges.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // BreakCriticalEdges pass - Break all of the critical edges in the CFG by
  10. // inserting a dummy basic block. This pass may be "required" by passes that
  11. // cannot deal with critical edges. For this usage, the structure type is
  12. // forward declared. This pass obviously invalidates the CFG, but can update
  13. // dominator trees.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Transforms/Utils/BreakCriticalEdges.h"
  17. #include "llvm/ADT/SetVector.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/BlockFrequencyInfo.h"
  21. #include "llvm/Analysis/BranchProbabilityInfo.h"
  22. #include "llvm/Analysis/CFG.h"
  23. #include "llvm/Analysis/LoopInfo.h"
  24. #include "llvm/Analysis/MemorySSAUpdater.h"
  25. #include "llvm/Analysis/PostDominators.h"
  26. #include "llvm/IR/CFG.h"
  27. #include "llvm/IR/Dominators.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/Type.h"
  30. #include "llvm/InitializePasses.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Transforms/Utils.h"
  33. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  34. #include "llvm/Transforms/Utils/Cloning.h"
  35. #include "llvm/Transforms/Utils/ValueMapper.h"
  36. using namespace llvm;
  37. #define DEBUG_TYPE "break-crit-edges"
  38. STATISTIC(NumBroken, "Number of blocks inserted");
  39. namespace {
  40. struct BreakCriticalEdges : public FunctionPass {
  41. static char ID; // Pass identification, replacement for typeid
  42. BreakCriticalEdges() : FunctionPass(ID) {
  43. initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
  44. }
  45. bool runOnFunction(Function &F) override {
  46. auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  47. auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  48. auto *PDTWP = getAnalysisIfAvailable<PostDominatorTreeWrapperPass>();
  49. auto *PDT = PDTWP ? &PDTWP->getPostDomTree() : nullptr;
  50. auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
  51. auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
  52. unsigned N =
  53. SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI, nullptr, PDT));
  54. NumBroken += N;
  55. return N > 0;
  56. }
  57. void getAnalysisUsage(AnalysisUsage &AU) const override {
  58. AU.addPreserved<DominatorTreeWrapperPass>();
  59. AU.addPreserved<LoopInfoWrapperPass>();
  60. // No loop canonicalization guarantees are broken by this pass.
  61. AU.addPreservedID(LoopSimplifyID);
  62. }
  63. };
  64. }
  65. char BreakCriticalEdges::ID = 0;
  66. INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
  67. "Break critical edges in CFG", false, false)
  68. // Publicly exposed interface to pass...
  69. char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
  70. FunctionPass *llvm::createBreakCriticalEdgesPass() {
  71. return new BreakCriticalEdges();
  72. }
  73. PreservedAnalyses BreakCriticalEdgesPass::run(Function &F,
  74. FunctionAnalysisManager &AM) {
  75. auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
  76. auto *LI = AM.getCachedResult<LoopAnalysis>(F);
  77. unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
  78. NumBroken += N;
  79. if (N == 0)
  80. return PreservedAnalyses::all();
  81. PreservedAnalyses PA;
  82. PA.preserve<DominatorTreeAnalysis>();
  83. PA.preserve<LoopAnalysis>();
  84. return PA;
  85. }
  86. //===----------------------------------------------------------------------===//
  87. // Implementation of the external critical edge manipulation functions
  88. //===----------------------------------------------------------------------===//
  89. BasicBlock *llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum,
  90. const CriticalEdgeSplittingOptions &Options,
  91. const Twine &BBName) {
  92. if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges))
  93. return nullptr;
  94. return SplitKnownCriticalEdge(TI, SuccNum, Options, BBName);
  95. }
  96. BasicBlock *
  97. llvm::SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum,
  98. const CriticalEdgeSplittingOptions &Options,
  99. const Twine &BBName) {
  100. assert(!isa<IndirectBrInst>(TI) &&
  101. "Cannot split critical edge from IndirectBrInst");
  102. BasicBlock *TIBB = TI->getParent();
  103. BasicBlock *DestBB = TI->getSuccessor(SuccNum);
  104. // Splitting the critical edge to a pad block is non-trivial. Don't do
  105. // it in this generic function.
  106. if (DestBB->isEHPad()) return nullptr;
  107. if (Options.IgnoreUnreachableDests &&
  108. isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime()))
  109. return nullptr;
  110. auto *LI = Options.LI;
  111. SmallVector<BasicBlock *, 4> LoopPreds;
  112. // Check if extra modifications will be required to preserve loop-simplify
  113. // form after splitting. If it would require splitting blocks with IndirectBr
  114. // or CallBr terminators, bail out if preserving loop-simplify form is
  115. // requested.
  116. if (LI) {
  117. if (Loop *TIL = LI->getLoopFor(TIBB)) {
  118. // The only way that we can break LoopSimplify form by splitting a
  119. // critical edge is if after the split there exists some edge from TIL to
  120. // DestBB *and* the only edge into DestBB from outside of TIL is that of
  121. // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
  122. // is the new exit block and it has no non-loop predecessors. If the
  123. // second isn't true, then DestBB was not in LoopSimplify form prior to
  124. // the split as it had a non-loop predecessor. In both of these cases,
  125. // the predecessor must be directly in TIL, not in a subloop, or again
  126. // LoopSimplify doesn't hold.
  127. for (BasicBlock *P : predecessors(DestBB)) {
  128. if (P == TIBB)
  129. continue; // The new block is known.
  130. if (LI->getLoopFor(P) != TIL) {
  131. // No need to re-simplify, it wasn't to start with.
  132. LoopPreds.clear();
  133. break;
  134. }
  135. LoopPreds.push_back(P);
  136. }
  137. // Loop-simplify form can be preserved, if we can split all in-loop
  138. // predecessors.
  139. if (any_of(LoopPreds, [](BasicBlock *Pred) {
  140. const Instruction *T = Pred->getTerminator();
  141. if (const auto *CBR = dyn_cast<CallBrInst>(T))
  142. return CBR->getDefaultDest() != Pred;
  143. return isa<IndirectBrInst>(T);
  144. })) {
  145. if (Options.PreserveLoopSimplify)
  146. return nullptr;
  147. LoopPreds.clear();
  148. }
  149. }
  150. }
  151. // Create a new basic block, linking it into the CFG.
  152. BasicBlock *NewBB = nullptr;
  153. if (BBName.str() != "")
  154. NewBB = BasicBlock::Create(TI->getContext(), BBName);
  155. else
  156. NewBB = BasicBlock::Create(TI->getContext(), TIBB->getName() + "." +
  157. DestBB->getName() +
  158. "_crit_edge");
  159. // Create our unconditional branch.
  160. BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
  161. NewBI->setDebugLoc(TI->getDebugLoc());
  162. // Insert the block into the function... right after the block TI lives in.
  163. Function &F = *TIBB->getParent();
  164. Function::iterator FBBI = TIBB->getIterator();
  165. F.getBasicBlockList().insert(++FBBI, NewBB);
  166. // Branch to the new block, breaking the edge.
  167. TI->setSuccessor(SuccNum, NewBB);
  168. // If there are any PHI nodes in DestBB, we need to update them so that they
  169. // merge incoming values from NewBB instead of from TIBB.
  170. {
  171. unsigned BBIdx = 0;
  172. for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
  173. // We no longer enter through TIBB, now we come in through NewBB.
  174. // Revector exactly one entry in the PHI node that used to come from
  175. // TIBB to come from NewBB.
  176. PHINode *PN = cast<PHINode>(I);
  177. // Reuse the previous value of BBIdx if it lines up. In cases where we
  178. // have multiple phi nodes with *lots* of predecessors, this is a speed
  179. // win because we don't have to scan the PHI looking for TIBB. This
  180. // happens because the BB list of PHI nodes are usually in the same
  181. // order.
  182. if (PN->getIncomingBlock(BBIdx) != TIBB)
  183. BBIdx = PN->getBasicBlockIndex(TIBB);
  184. PN->setIncomingBlock(BBIdx, NewBB);
  185. }
  186. }
  187. // If there are any other edges from TIBB to DestBB, update those to go
  188. // through the split block, making those edges non-critical as well (and
  189. // reducing the number of phi entries in the DestBB if relevant).
  190. if (Options.MergeIdenticalEdges) {
  191. for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
  192. if (TI->getSuccessor(i) != DestBB) continue;
  193. // Remove an entry for TIBB from DestBB phi nodes.
  194. DestBB->removePredecessor(TIBB, Options.KeepOneInputPHIs);
  195. // We found another edge to DestBB, go to NewBB instead.
  196. TI->setSuccessor(i, NewBB);
  197. }
  198. }
  199. // If we have nothing to update, just return.
  200. auto *DT = Options.DT;
  201. auto *PDT = Options.PDT;
  202. auto *MSSAU = Options.MSSAU;
  203. if (MSSAU)
  204. MSSAU->wireOldPredecessorsToNewImmediatePredecessor(
  205. DestBB, NewBB, {TIBB}, Options.MergeIdenticalEdges);
  206. if (!DT && !PDT && !LI)
  207. return NewBB;
  208. if (DT || PDT) {
  209. // Update the DominatorTree.
  210. // ---> NewBB -----\
  211. // / V
  212. // TIBB -------\\------> DestBB
  213. //
  214. // First, inform the DT about the new path from TIBB to DestBB via NewBB,
  215. // then delete the old edge from TIBB to DestBB. By doing this in that order
  216. // DestBB stays reachable in the DT the whole time and its subtree doesn't
  217. // get disconnected.
  218. SmallVector<DominatorTree::UpdateType, 3> Updates;
  219. Updates.push_back({DominatorTree::Insert, TIBB, NewBB});
  220. Updates.push_back({DominatorTree::Insert, NewBB, DestBB});
  221. if (!llvm::is_contained(successors(TIBB), DestBB))
  222. Updates.push_back({DominatorTree::Delete, TIBB, DestBB});
  223. if (DT)
  224. DT->applyUpdates(Updates);
  225. if (PDT)
  226. PDT->applyUpdates(Updates);
  227. }
  228. // Update LoopInfo if it is around.
  229. if (LI) {
  230. if (Loop *TIL = LI->getLoopFor(TIBB)) {
  231. // If one or the other blocks were not in a loop, the new block is not
  232. // either, and thus LI doesn't need to be updated.
  233. if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
  234. if (TIL == DestLoop) {
  235. // Both in the same loop, the NewBB joins loop.
  236. DestLoop->addBasicBlockToLoop(NewBB, *LI);
  237. } else if (TIL->contains(DestLoop)) {
  238. // Edge from an outer loop to an inner loop. Add to the outer loop.
  239. TIL->addBasicBlockToLoop(NewBB, *LI);
  240. } else if (DestLoop->contains(TIL)) {
  241. // Edge from an inner loop to an outer loop. Add to the outer loop.
  242. DestLoop->addBasicBlockToLoop(NewBB, *LI);
  243. } else {
  244. // Edge from two loops with no containment relation. Because these
  245. // are natural loops, we know that the destination block must be the
  246. // header of its loop (adding a branch into a loop elsewhere would
  247. // create an irreducible loop).
  248. assert(DestLoop->getHeader() == DestBB &&
  249. "Should not create irreducible loops!");
  250. if (Loop *P = DestLoop->getParentLoop())
  251. P->addBasicBlockToLoop(NewBB, *LI);
  252. }
  253. }
  254. // If TIBB is in a loop and DestBB is outside of that loop, we may need
  255. // to update LoopSimplify form and LCSSA form.
  256. if (!TIL->contains(DestBB)) {
  257. assert(!TIL->contains(NewBB) &&
  258. "Split point for loop exit is contained in loop!");
  259. // Update LCSSA form in the newly created exit block.
  260. if (Options.PreserveLCSSA) {
  261. createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
  262. }
  263. if (!LoopPreds.empty()) {
  264. assert(!DestBB->isEHPad() && "We don't split edges to EH pads!");
  265. BasicBlock *NewExitBB = SplitBlockPredecessors(
  266. DestBB, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
  267. if (Options.PreserveLCSSA)
  268. createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);
  269. }
  270. }
  271. }
  272. }
  273. return NewBB;
  274. }
  275. // Return the unique indirectbr predecessor of a block. This may return null
  276. // even if such a predecessor exists, if it's not useful for splitting.
  277. // If a predecessor is found, OtherPreds will contain all other (non-indirectbr)
  278. // predecessors of BB.
  279. static BasicBlock *
  280. findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) {
  281. // If the block doesn't have any PHIs, we don't care about it, since there's
  282. // no point in splitting it.
  283. PHINode *PN = dyn_cast<PHINode>(BB->begin());
  284. if (!PN)
  285. return nullptr;
  286. // Verify we have exactly one IBR predecessor.
  287. // Conservatively bail out if one of the other predecessors is not a "regular"
  288. // terminator (that is, not a switch or a br).
  289. BasicBlock *IBB = nullptr;
  290. for (unsigned Pred = 0, E = PN->getNumIncomingValues(); Pred != E; ++Pred) {
  291. BasicBlock *PredBB = PN->getIncomingBlock(Pred);
  292. Instruction *PredTerm = PredBB->getTerminator();
  293. switch (PredTerm->getOpcode()) {
  294. case Instruction::IndirectBr:
  295. if (IBB)
  296. return nullptr;
  297. IBB = PredBB;
  298. break;
  299. case Instruction::Br:
  300. case Instruction::Switch:
  301. OtherPreds.push_back(PredBB);
  302. continue;
  303. default:
  304. return nullptr;
  305. }
  306. }
  307. return IBB;
  308. }
  309. bool llvm::SplitIndirectBrCriticalEdges(Function &F,
  310. BranchProbabilityInfo *BPI,
  311. BlockFrequencyInfo *BFI) {
  312. // Check whether the function has any indirectbrs, and collect which blocks
  313. // they may jump to. Since most functions don't have indirect branches,
  314. // this lowers the common case's overhead to O(Blocks) instead of O(Edges).
  315. SmallSetVector<BasicBlock *, 16> Targets;
  316. for (auto &BB : F) {
  317. auto *IBI = dyn_cast<IndirectBrInst>(BB.getTerminator());
  318. if (!IBI)
  319. continue;
  320. for (unsigned Succ = 0, E = IBI->getNumSuccessors(); Succ != E; ++Succ)
  321. Targets.insert(IBI->getSuccessor(Succ));
  322. }
  323. if (Targets.empty())
  324. return false;
  325. bool ShouldUpdateAnalysis = BPI && BFI;
  326. bool Changed = false;
  327. for (BasicBlock *Target : Targets) {
  328. SmallVector<BasicBlock *, 16> OtherPreds;
  329. BasicBlock *IBRPred = findIBRPredecessor(Target, OtherPreds);
  330. // If we did not found an indirectbr, or the indirectbr is the only
  331. // incoming edge, this isn't the kind of edge we're looking for.
  332. if (!IBRPred || OtherPreds.empty())
  333. continue;
  334. // Don't even think about ehpads/landingpads.
  335. Instruction *FirstNonPHI = Target->getFirstNonPHI();
  336. if (FirstNonPHI->isEHPad() || Target->isLandingPad())
  337. continue;
  338. // Remember edge probabilities if needed.
  339. SmallVector<BranchProbability, 4> EdgeProbabilities;
  340. if (ShouldUpdateAnalysis) {
  341. EdgeProbabilities.reserve(Target->getTerminator()->getNumSuccessors());
  342. for (unsigned I = 0, E = Target->getTerminator()->getNumSuccessors();
  343. I < E; ++I)
  344. EdgeProbabilities.emplace_back(BPI->getEdgeProbability(Target, I));
  345. BPI->eraseBlock(Target);
  346. }
  347. BasicBlock *BodyBlock = Target->splitBasicBlock(FirstNonPHI, ".split");
  348. if (ShouldUpdateAnalysis) {
  349. // Copy the BFI/BPI from Target to BodyBlock.
  350. BPI->setEdgeProbability(BodyBlock, EdgeProbabilities);
  351. BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(Target).getFrequency());
  352. }
  353. // It's possible Target was its own successor through an indirectbr.
  354. // In this case, the indirectbr now comes from BodyBlock.
  355. if (IBRPred == Target)
  356. IBRPred = BodyBlock;
  357. // At this point Target only has PHIs, and BodyBlock has the rest of the
  358. // block's body. Create a copy of Target that will be used by the "direct"
  359. // preds.
  360. ValueToValueMapTy VMap;
  361. BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);
  362. BlockFrequency BlockFreqForDirectSucc;
  363. for (BasicBlock *Pred : OtherPreds) {
  364. // If the target is a loop to itself, then the terminator of the split
  365. // block (BodyBlock) needs to be updated.
  366. BasicBlock *Src = Pred != Target ? Pred : BodyBlock;
  367. Src->getTerminator()->replaceUsesOfWith(Target, DirectSucc);
  368. if (ShouldUpdateAnalysis)
  369. BlockFreqForDirectSucc += BFI->getBlockFreq(Src) *
  370. BPI->getEdgeProbability(Src, DirectSucc);
  371. }
  372. if (ShouldUpdateAnalysis) {
  373. BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc.getFrequency());
  374. BlockFrequency NewBlockFreqForTarget =
  375. BFI->getBlockFreq(Target) - BlockFreqForDirectSucc;
  376. BFI->setBlockFreq(Target, NewBlockFreqForTarget.getFrequency());
  377. }
  378. // Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that
  379. // they are clones, so the number of PHIs are the same.
  380. // (a) Remove the edge coming from IBRPred from the "Direct" PHI
  381. // (b) Leave that as the only edge in the "Indirect" PHI.
  382. // (c) Merge the two in the body block.
  383. BasicBlock::iterator Indirect = Target->begin(),
  384. End = Target->getFirstNonPHI()->getIterator();
  385. BasicBlock::iterator Direct = DirectSucc->begin();
  386. BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt();
  387. assert(&*End == Target->getTerminator() &&
  388. "Block was expected to only contain PHIs");
  389. while (Indirect != End) {
  390. PHINode *DirPHI = cast<PHINode>(Direct);
  391. PHINode *IndPHI = cast<PHINode>(Indirect);
  392. // Now, clean up - the direct block shouldn't get the indirect value,
  393. // and vice versa.
  394. DirPHI->removeIncomingValue(IBRPred);
  395. Direct++;
  396. // Advance the pointer here, to avoid invalidation issues when the old
  397. // PHI is erased.
  398. Indirect++;
  399. PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", IndPHI);
  400. NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
  401. IBRPred);
  402. // Create a PHI in the body block, to merge the direct and indirect
  403. // predecessors.
  404. PHINode *MergePHI =
  405. PHINode::Create(IndPHI->getType(), 2, "merge", &*MergeInsert);
  406. MergePHI->addIncoming(NewIndPHI, Target);
  407. MergePHI->addIncoming(DirPHI, DirectSucc);
  408. IndPHI->replaceAllUsesWith(MergePHI);
  409. IndPHI->eraseFromParent();
  410. }
  411. Changed = true;
  412. }
  413. return Changed;
  414. }