BreakCriticalEdges.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // BreakCriticalEdges pass - Break all of the critical edges in the CFG by
  11. // inserting a dummy basic block. This pass may be "required" by passes that
  12. // cannot deal with critical edges. For this usage, the structure type is
  13. // forward declared. This pass obviously invalidates the CFG, but can update
  14. // dominator trees.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "llvm/Transforms/Scalar.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Analysis/CFG.h"
  22. #include "llvm/Analysis/LoopInfo.h"
  23. #include "llvm/IR/CFG.h"
  24. #include "llvm/IR/Dominators.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/Instructions.h"
  27. #include "llvm/IR/Type.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  30. using namespace llvm;
  31. #define DEBUG_TYPE "break-crit-edges"
  32. STATISTIC(NumBroken, "Number of blocks inserted");
  33. namespace {
  34. struct BreakCriticalEdges : public FunctionPass {
  35. static char ID; // Pass identification, replacement for typeid
  36. BreakCriticalEdges() : FunctionPass(ID) {
  37. initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry());
  38. }
  39. bool runOnFunction(Function &F) override {
  40. auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  41. auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  42. auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
  43. auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
  44. unsigned N =
  45. SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI));
  46. NumBroken += N;
  47. return N > 0;
  48. }
  49. void getAnalysisUsage(AnalysisUsage &AU) const override {
  50. AU.addPreserved<DominatorTreeWrapperPass>();
  51. AU.addPreserved<LoopInfoWrapperPass>();
  52. // No loop canonicalization guarantees are broken by this pass.
  53. AU.addPreservedID(LoopSimplifyID);
  54. }
  55. };
  56. }
  57. char BreakCriticalEdges::ID = 0;
  58. INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges",
  59. "Break critical edges in CFG", false, false)
  60. // Publicly exposed interface to pass...
  61. char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID;
  62. FunctionPass *llvm::createBreakCriticalEdgesPass() {
  63. return new BreakCriticalEdges();
  64. }
  65. //===----------------------------------------------------------------------===//
  66. // Implementation of the external critical edge manipulation functions
  67. //===----------------------------------------------------------------------===//
  68. /// createPHIsForSplitLoopExit - When a loop exit edge is split, LCSSA form
  69. /// may require new PHIs in the new exit block. This function inserts the
  70. /// new PHIs, as needed. Preds is a list of preds inside the loop, SplitBB
  71. /// is the new loop exit block, and DestBB is the old loop exit, now the
  72. /// successor of SplitBB.
  73. static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
  74. BasicBlock *SplitBB,
  75. BasicBlock *DestBB) {
  76. // SplitBB shouldn't have anything non-trivial in it yet.
  77. assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
  78. SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!");
  79. // For each PHI in the destination block.
  80. for (BasicBlock::iterator I = DestBB->begin();
  81. PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  82. unsigned Idx = PN->getBasicBlockIndex(SplitBB);
  83. Value *V = PN->getIncomingValue(Idx);
  84. // If the input is a PHI which already satisfies LCSSA, don't create
  85. // a new one.
  86. if (const PHINode *VP = dyn_cast<PHINode>(V))
  87. if (VP->getParent() == SplitBB)
  88. continue;
  89. // Otherwise a new PHI is needed. Create one and populate it.
  90. PHINode *NewPN =
  91. PHINode::Create(PN->getType(), Preds.size(), "split",
  92. SplitBB->isLandingPad() ?
  93. SplitBB->begin() : SplitBB->getTerminator());
  94. for (unsigned i = 0, e = Preds.size(); i != e; ++i)
  95. NewPN->addIncoming(V, Preds[i]);
  96. // Update the original PHI.
  97. PN->setIncomingValue(Idx, NewPN);
  98. }
  99. }
  100. /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
  101. /// split the critical edge. This will update DominatorTree information if it
  102. /// is available, thus calling this pass will not invalidate either of them.
  103. /// This returns the new block if the edge was split, null otherwise.
  104. ///
  105. /// If MergeIdenticalEdges is true (not the default), *all* edges from TI to the
  106. /// specified successor will be merged into the same critical edge block.
  107. /// This is most commonly interesting with switch instructions, which may
  108. /// have many edges to any one destination. This ensures that all edges to that
  109. /// dest go to one block instead of each going to a different block, but isn't
  110. /// the standard definition of a "critical edge".
  111. ///
  112. /// It is invalid to call this function on a critical edge that starts at an
  113. /// IndirectBrInst. Splitting these edges will almost always create an invalid
  114. /// program because the address of the new block won't be the one that is jumped
  115. /// to.
  116. ///
  117. BasicBlock *llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
  118. const CriticalEdgeSplittingOptions &Options) {
  119. if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges))
  120. return nullptr;
  121. assert(!isa<IndirectBrInst>(TI) &&
  122. "Cannot split critical edge from IndirectBrInst");
  123. BasicBlock *TIBB = TI->getParent();
  124. BasicBlock *DestBB = TI->getSuccessor(SuccNum);
  125. // Splitting the critical edge to a landing pad block is non-trivial. Don't do
  126. // it in this generic function.
  127. if (DestBB->isLandingPad()) return nullptr;
  128. // Create a new basic block, linking it into the CFG.
  129. BasicBlock *NewBB = BasicBlock::Create(TI->getContext(),
  130. TIBB->getName() + "." + DestBB->getName() + "_crit_edge");
  131. // Create our unconditional branch.
  132. BranchInst *NewBI = BranchInst::Create(DestBB, NewBB);
  133. NewBI->setDebugLoc(TI->getDebugLoc());
  134. // Branch to the new block, breaking the edge.
  135. TI->setSuccessor(SuccNum, NewBB);
  136. // Insert the block into the function... right after the block TI lives in.
  137. Function &F = *TIBB->getParent();
  138. Function::iterator FBBI = TIBB;
  139. F.getBasicBlockList().insert(++FBBI, NewBB);
  140. // If there are any PHI nodes in DestBB, we need to update them so that they
  141. // merge incoming values from NewBB instead of from TIBB.
  142. {
  143. unsigned BBIdx = 0;
  144. for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) {
  145. // We no longer enter through TIBB, now we come in through NewBB.
  146. // Revector exactly one entry in the PHI node that used to come from
  147. // TIBB to come from NewBB.
  148. PHINode *PN = cast<PHINode>(I);
  149. // Reuse the previous value of BBIdx if it lines up. In cases where we
  150. // have multiple phi nodes with *lots* of predecessors, this is a speed
  151. // win because we don't have to scan the PHI looking for TIBB. This
  152. // happens because the BB list of PHI nodes are usually in the same
  153. // order.
  154. if (PN->getIncomingBlock(BBIdx) != TIBB)
  155. BBIdx = PN->getBasicBlockIndex(TIBB);
  156. PN->setIncomingBlock(BBIdx, NewBB);
  157. }
  158. }
  159. // If there are any other edges from TIBB to DestBB, update those to go
  160. // through the split block, making those edges non-critical as well (and
  161. // reducing the number of phi entries in the DestBB if relevant).
  162. if (Options.MergeIdenticalEdges) {
  163. for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) {
  164. if (TI->getSuccessor(i) != DestBB) continue;
  165. // Remove an entry for TIBB from DestBB phi nodes.
  166. DestBB->removePredecessor(TIBB, Options.DontDeleteUselessPHIs);
  167. // We found another edge to DestBB, go to NewBB instead.
  168. TI->setSuccessor(i, NewBB);
  169. }
  170. }
  171. // If we have nothing to update, just return.
  172. auto *AA = Options.AA;
  173. auto *DT = Options.DT;
  174. auto *LI = Options.LI;
  175. if (!DT && !LI)
  176. return NewBB;
  177. // Now update analysis information. Since the only predecessor of NewBB is
  178. // the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate
  179. // anything, as there are other successors of DestBB. However, if all other
  180. // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a
  181. // loop header) then NewBB dominates DestBB.
  182. SmallVector<BasicBlock*, 8> OtherPreds;
  183. // If there is a PHI in the block, loop over predecessors with it, which is
  184. // faster than iterating pred_begin/end.
  185. if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
  186. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  187. if (PN->getIncomingBlock(i) != NewBB)
  188. OtherPreds.push_back(PN->getIncomingBlock(i));
  189. } else {
  190. for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB);
  191. I != E; ++I) {
  192. BasicBlock *P = *I;
  193. if (P != NewBB)
  194. OtherPreds.push_back(P);
  195. }
  196. }
  197. bool NewBBDominatesDestBB = true;
  198. // Should we update DominatorTree information?
  199. if (DT) {
  200. DomTreeNode *TINode = DT->getNode(TIBB);
  201. // The new block is not the immediate dominator for any other nodes, but
  202. // TINode is the immediate dominator for the new node.
  203. //
  204. if (TINode) { // Don't break unreachable code!
  205. DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB);
  206. DomTreeNode *DestBBNode = nullptr;
  207. // If NewBBDominatesDestBB hasn't been computed yet, do so with DT.
  208. if (!OtherPreds.empty()) {
  209. DestBBNode = DT->getNode(DestBB);
  210. while (!OtherPreds.empty() && NewBBDominatesDestBB) {
  211. if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back()))
  212. NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode);
  213. OtherPreds.pop_back();
  214. }
  215. OtherPreds.clear();
  216. }
  217. // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it
  218. // doesn't dominate anything.
  219. if (NewBBDominatesDestBB) {
  220. if (!DestBBNode) DestBBNode = DT->getNode(DestBB);
  221. DT->changeImmediateDominator(DestBBNode, NewBBNode);
  222. }
  223. }
  224. }
  225. // Update LoopInfo if it is around.
  226. if (LI) {
  227. if (Loop *TIL = LI->getLoopFor(TIBB)) {
  228. // If one or the other blocks were not in a loop, the new block is not
  229. // either, and thus LI doesn't need to be updated.
  230. if (Loop *DestLoop = LI->getLoopFor(DestBB)) {
  231. if (TIL == DestLoop) {
  232. // Both in the same loop, the NewBB joins loop.
  233. DestLoop->addBasicBlockToLoop(NewBB, *LI);
  234. } else if (TIL->contains(DestLoop)) {
  235. // Edge from an outer loop to an inner loop. Add to the outer loop.
  236. TIL->addBasicBlockToLoop(NewBB, *LI);
  237. } else if (DestLoop->contains(TIL)) {
  238. // Edge from an inner loop to an outer loop. Add to the outer loop.
  239. DestLoop->addBasicBlockToLoop(NewBB, *LI);
  240. } else {
  241. // Edge from two loops with no containment relation. Because these
  242. // are natural loops, we know that the destination block must be the
  243. // header of its loop (adding a branch into a loop elsewhere would
  244. // create an irreducible loop).
  245. assert(DestLoop->getHeader() == DestBB &&
  246. "Should not create irreducible loops!");
  247. if (Loop *P = DestLoop->getParentLoop())
  248. P->addBasicBlockToLoop(NewBB, *LI);
  249. }
  250. }
  251. // If TIBB is in a loop and DestBB is outside of that loop, we may need
  252. // to update LoopSimplify form and LCSSA form.
  253. if (!TIL->contains(DestBB)) {
  254. assert(!TIL->contains(NewBB) &&
  255. "Split point for loop exit is contained in loop!");
  256. // Update LCSSA form in the newly created exit block.
  257. if (Options.PreserveLCSSA) {
  258. createPHIsForSplitLoopExit(TIBB, NewBB, DestBB);
  259. }
  260. // The only that we can break LoopSimplify form by splitting a critical
  261. // edge is if after the split there exists some edge from TIL to DestBB
  262. // *and* the only edge into DestBB from outside of TIL is that of
  263. // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB
  264. // is the new exit block and it has no non-loop predecessors. If the
  265. // second isn't true, then DestBB was not in LoopSimplify form prior to
  266. // the split as it had a non-loop predecessor. In both of these cases,
  267. // the predecessor must be directly in TIL, not in a subloop, or again
  268. // LoopSimplify doesn't hold.
  269. SmallVector<BasicBlock *, 4> LoopPreds;
  270. for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E;
  271. ++I) {
  272. BasicBlock *P = *I;
  273. if (P == NewBB)
  274. continue; // The new block is known.
  275. if (LI->getLoopFor(P) != TIL) {
  276. // No need to re-simplify, it wasn't to start with.
  277. LoopPreds.clear();
  278. break;
  279. }
  280. LoopPreds.push_back(P);
  281. }
  282. if (!LoopPreds.empty()) {
  283. assert(!DestBB->isLandingPad() &&
  284. "We don't split edges to landing pads!");
  285. BasicBlock *NewExitBB = SplitBlockPredecessors(
  286. DestBB, LoopPreds, "split", AA, DT, LI, Options.PreserveLCSSA);
  287. if (Options.PreserveLCSSA)
  288. createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB);
  289. }
  290. }
  291. }
  292. }
  293. return NewBB;
  294. }