Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 1 | //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 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 |
Cameron Zwarich | b703654 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 14 | // dominator trees. |
Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Wei Mi | e04d0ef | 2016-07-22 18:04:25 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/BreakCriticalEdges.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | b5797b6 | 2015-01-18 09:21:15 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Nick Lewycky | 0b68245 | 2013-07-27 01:24:00 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CFG.h" |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/Type.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Wei Mi | e04d0ef | 2016-07-22 18:04:25 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | #define DEBUG_TYPE "break-crit-edges" |
| 34 | |
Chris Lattner | 45f966d | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 35 | STATISTIC(NumBroken, "Number of blocks inserted"); |
Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 45f966d | 2006-12-19 22:17:40 +0000 | [diff] [blame] | 37 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 38 | struct BreakCriticalEdges : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 39 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | BreakCriticalEdges() : FunctionPass(ID) { |
| 41 | initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 43 | |
Kostya Serebryany | e5ea424 | 2014-11-19 00:17:31 +0000 | [diff] [blame] | 44 | bool runOnFunction(Function &F) override { |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 45 | auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 46 | auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 47 | auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>(); |
| 48 | auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; |
| 49 | unsigned N = |
| 50 | SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI)); |
Kostya Serebryany | e5ea424 | 2014-11-19 00:17:31 +0000 | [diff] [blame] | 51 | NumBroken += N; |
| 52 | return N > 0; |
| 53 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 54 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 55 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 56 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Chandler Carruth | 4f8f307 | 2015-01-17 14:16:18 +0000 | [diff] [blame] | 57 | AU.addPreserved<LoopInfoWrapperPass>(); |
Chris Lattner | 72272a7 | 2003-10-12 21:52:28 +0000 | [diff] [blame] | 58 | |
| 59 | // No loop canonicalization guarantees are broken by this pass. |
| 60 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | 4bec665 | 2002-09-24 15:43:12 +0000 | [diff] [blame] | 61 | } |
| 62 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 64 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | char BreakCriticalEdges::ID = 0; |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 66 | INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 67 | "Break critical edges in CFG", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 69 | // Publicly exposed interface to pass... |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 70 | char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID; |
Chris Lattner | 7471b96 | 2004-07-31 10:01:58 +0000 | [diff] [blame] | 71 | FunctionPass *llvm::createBreakCriticalEdgesPass() { |
| 72 | return new BreakCriticalEdges(); |
| 73 | } |
Chris Lattner | b03832d | 2002-09-24 00:08:39 +0000 | [diff] [blame] | 74 | |
Wei Mi | e04d0ef | 2016-07-22 18:04:25 +0000 | [diff] [blame] | 75 | PreservedAnalyses BreakCriticalEdgesPass::run(Function &F, |
| 76 | FunctionAnalysisManager &AM) { |
| 77 | auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F); |
| 78 | auto *LI = AM.getCachedResult<LoopAnalysis>(F); |
| 79 | unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI)); |
| 80 | NumBroken += N; |
| 81 | if (N == 0) |
| 82 | return PreservedAnalyses::all(); |
| 83 | PreservedAnalyses PA; |
| 84 | PA.preserve<DominatorTreeAnalysis>(); |
| 85 | PA.preserve<LoopAnalysis>(); |
| 86 | return PA; |
| 87 | } |
| 88 | |
Chris Lattner | 1e6d305 | 2003-11-10 04:42:42 +0000 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // Implementation of the external critical edge manipulation functions |
| 91 | //===----------------------------------------------------------------------===// |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 92 | |
Sanjay Patel | 85ce0f1 | 2016-04-23 16:31:48 +0000 | [diff] [blame] | 93 | /// When a loop exit edge is split, LCSSA form may require new PHIs in the new |
| 94 | /// exit block. This function inserts the new PHIs, as needed. Preds is a list |
| 95 | /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is |
| 96 | /// the old loop exit, now the successor of SplitBB. |
Bill Wendling | 325e6cd | 2012-04-30 10:25:51 +0000 | [diff] [blame] | 97 | static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds, |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 98 | BasicBlock *SplitBB, |
| 99 | BasicBlock *DestBB) { |
| 100 | // SplitBB shouldn't have anything non-trivial in it yet. |
Bill Wendling | bf4b9af | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 101 | assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() || |
| 102 | SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!"); |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 103 | |
Bill Wendling | bf4b9af | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 104 | // For each PHI in the destination block. |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 105 | for (BasicBlock::iterator I = DestBB->begin(); |
| 106 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 107 | unsigned Idx = PN->getBasicBlockIndex(SplitBB); |
| 108 | Value *V = PN->getIncomingValue(Idx); |
Bill Wendling | bf4b9af | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 109 | |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 110 | // If the input is a PHI which already satisfies LCSSA, don't create |
| 111 | // a new one. |
| 112 | if (const PHINode *VP = dyn_cast<PHINode>(V)) |
| 113 | if (VP->getParent() == SplitBB) |
| 114 | continue; |
Bill Wendling | bf4b9af | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 115 | |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 116 | // Otherwise a new PHI is needed. Create one and populate it. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 117 | PHINode *NewPN = PHINode::Create( |
| 118 | PN->getType(), Preds.size(), "split", |
| 119 | SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator()); |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 120 | for (unsigned i = 0, e = Preds.size(); i != e; ++i) |
| 121 | NewPN->addIncoming(V, Preds[i]); |
Bill Wendling | bf4b9af | 2012-04-30 10:44:54 +0000 | [diff] [blame] | 122 | |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 123 | // Update the original PHI. |
| 124 | PN->setIncomingValue(Idx, NewPN); |
| 125 | } |
| 126 | } |
| 127 | |
Sanjay Patel | 85ce0f1 | 2016-04-23 16:31:48 +0000 | [diff] [blame] | 128 | BasicBlock * |
| 129 | llvm::SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, |
| 130 | const CriticalEdgeSplittingOptions &Options) { |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 131 | if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges)) |
| 132 | return nullptr; |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 133 | |
Chris Lattner | ba364b0 | 2009-10-31 21:51:10 +0000 | [diff] [blame] | 134 | assert(!isa<IndirectBrInst>(TI) && |
| 135 | "Cannot split critical edge from IndirectBrInst"); |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 137 | BasicBlock *TIBB = TI->getParent(); |
Chris Lattner | 12764c8 | 2002-10-31 02:44:36 +0000 | [diff] [blame] | 138 | BasicBlock *DestBB = TI->getSuccessor(SuccNum); |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 139 | |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 140 | // Splitting the critical edge to a pad block is non-trivial. Don't do |
Bill Wendling | 2dfbcc4 | 2011-08-17 21:04:05 +0000 | [diff] [blame] | 141 | // it in this generic function. |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 142 | if (DestBB->isEHPad()) return nullptr; |
Bill Wendling | 2dfbcc4 | 2011-08-17 21:04:05 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 144 | // Create a new basic block, linking it into the CFG. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 145 | BasicBlock *NewBB = BasicBlock::Create(TI->getContext(), |
| 146 | TIBB->getName() + "." + DestBB->getName() + "_crit_edge"); |
Chris Lattner | 72c4dce | 2010-02-13 04:24:19 +0000 | [diff] [blame] | 147 | // Create our unconditional branch. |
Devang Patel | c23bcbc | 2011-05-17 19:43:06 +0000 | [diff] [blame] | 148 | BranchInst *NewBI = BranchInst::Create(DestBB, NewBB); |
| 149 | NewBI->setDebugLoc(TI->getDebugLoc()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 80ea207 | 2006-10-28 06:44:56 +0000 | [diff] [blame] | 151 | // Branch to the new block, breaking the edge. |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 152 | TI->setSuccessor(SuccNum, NewBB); |
| 153 | |
| 154 | // Insert the block into the function... right after the block TI lives in. |
| 155 | Function &F = *TIBB->getParent(); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 156 | Function::iterator FBBI = TIBB->getIterator(); |
Chris Lattner | 233f97a | 2007-04-17 18:09:47 +0000 | [diff] [blame] | 157 | F.getBasicBlockList().insert(++FBBI, NewBB); |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 159 | // If there are any PHI nodes in DestBB, we need to update them so that they |
| 160 | // merge incoming values from NewBB instead of from TIBB. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 161 | { |
| 162 | unsigned BBIdx = 0; |
| 163 | for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) { |
| 164 | // We no longer enter through TIBB, now we come in through NewBB. |
| 165 | // Revector exactly one entry in the PHI node that used to come from |
| 166 | // TIBB to come from NewBB. |
| 167 | PHINode *PN = cast<PHINode>(I); |
| 168 | |
| 169 | // Reuse the previous value of BBIdx if it lines up. In cases where we |
| 170 | // have multiple phi nodes with *lots* of predecessors, this is a speed |
| 171 | // win because we don't have to scan the PHI looking for TIBB. This |
| 172 | // happens because the BB list of PHI nodes are usually in the same |
| 173 | // order. |
| 174 | if (PN->getIncomingBlock(BBIdx) != TIBB) |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 175 | BBIdx = PN->getBasicBlockIndex(TIBB); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 176 | PN->setIncomingBlock(BBIdx, NewBB); |
Chris Lattner | 5e7f705 | 2010-02-13 05:01:14 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 178 | } |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 80ea207 | 2006-10-28 06:44:56 +0000 | [diff] [blame] | 180 | // If there are any other edges from TIBB to DestBB, update those to go |
| 181 | // through the split block, making those edges non-critical as well (and |
| 182 | // reducing the number of phi entries in the DestBB if relevant). |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 183 | if (Options.MergeIdenticalEdges) { |
Chris Lattner | 80ea207 | 2006-10-28 06:44:56 +0000 | [diff] [blame] | 184 | for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) { |
| 185 | if (TI->getSuccessor(i) != DestBB) continue; |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 80ea207 | 2006-10-28 06:44:56 +0000 | [diff] [blame] | 187 | // Remove an entry for TIBB from DestBB phi nodes. |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 188 | DestBB->removePredecessor(TIBB, Options.DontDeleteUselessPHIs); |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 80ea207 | 2006-10-28 06:44:56 +0000 | [diff] [blame] | 190 | // We found another edge to DestBB, go to NewBB instead. |
| 191 | TI->setSuccessor(i, NewBB); |
| 192 | } |
| 193 | } |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 5e7f705 | 2010-02-13 05:01:14 +0000 | [diff] [blame] | 195 | // If we have nothing to update, just return. |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 196 | auto *DT = Options.DT; |
| 197 | auto *LI = Options.LI; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 198 | if (!DT && !LI) |
Chris Lattner | 5e7f705 | 2010-02-13 05:01:14 +0000 | [diff] [blame] | 199 | return NewBB; |
Chris Lattner | 5ac72de | 2002-10-08 21:06:27 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 201 | // Now update analysis information. Since the only predecessor of NewBB is |
| 202 | // the TIBB, TIBB clearly dominates NewBB. TIBB usually doesn't dominate |
| 203 | // anything, as there are other successors of DestBB. However, if all other |
| 204 | // predecessors of DestBB are already dominated by DestBB (e.g. DestBB is a |
| 205 | // loop header) then NewBB dominates DestBB. |
| 206 | SmallVector<BasicBlock*, 8> OtherPreds; |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 207 | |
Chris Lattner | b0ebb65 | 2010-02-13 04:15:26 +0000 | [diff] [blame] | 208 | // If there is a PHI in the block, loop over predecessors with it, which is |
| 209 | // faster than iterating pred_begin/end. |
| 210 | if (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) { |
| 211 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 212 | if (PN->getIncomingBlock(i) != NewBB) |
| 213 | OtherPreds.push_back(PN->getIncomingBlock(i)); |
| 214 | } else { |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 215 | for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); |
| 216 | I != E; ++I) { |
| 217 | BasicBlock *P = *I; |
Gabor Greif | 6d8870f | 2010-07-09 15:25:42 +0000 | [diff] [blame] | 218 | if (P != NewBB) |
Chris Lattner | 90f3a9a | 2011-01-14 04:23:53 +0000 | [diff] [blame] | 219 | OtherPreds.push_back(P); |
Gabor Greif | 6d8870f | 2010-07-09 15:25:42 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | b0ebb65 | 2010-02-13 04:15:26 +0000 | [diff] [blame] | 221 | } |
Gabor Greif | 6d8870f | 2010-07-09 15:25:42 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 223 | bool NewBBDominatesDestBB = true; |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 224 | |
Chris Lattner | bedbd6b | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 225 | // Should we update DominatorTree information? |
Chris Lattner | 5e7f705 | 2010-02-13 05:01:14 +0000 | [diff] [blame] | 226 | if (DT) { |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 227 | DomTreeNode *TINode = DT->getNode(TIBB); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 228 | |
Chris Lattner | bedbd6b | 2002-09-26 16:18:51 +0000 | [diff] [blame] | 229 | // The new block is not the immediate dominator for any other nodes, but |
| 230 | // TINode is the immediate dominator for the new node. |
| 231 | // |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 232 | if (TINode) { // Don't break unreachable code! |
Devang Patel | ebc5b96 | 2007-06-04 16:43:25 +0000 | [diff] [blame] | 233 | DomTreeNode *NewBBNode = DT->addNewBlock(NewBB, TIBB); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 234 | DomTreeNode *DestBBNode = nullptr; |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 236 | // If NewBBDominatesDestBB hasn't been computed yet, do so with DT. |
| 237 | if (!OtherPreds.empty()) { |
| 238 | DestBBNode = DT->getNode(DestBB); |
| 239 | while (!OtherPreds.empty() && NewBBDominatesDestBB) { |
Devang Patel | bdd1aae | 2007-06-04 00:32:22 +0000 | [diff] [blame] | 240 | if (DomTreeNode *OPNode = DT->getNode(OtherPreds.back())) |
Devang Patel | af41e4a | 2007-06-07 17:47:21 +0000 | [diff] [blame] | 241 | NewBBDominatesDestBB = DT->dominates(DestBBNode, OPNode); |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 242 | OtherPreds.pop_back(); |
| 243 | } |
| 244 | OtherPreds.clear(); |
| 245 | } |
Andrew Trick | 411842f | 2011-10-04 03:34:49 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 247 | // If NewBBDominatesDestBB, then NewBB dominates DestBB, otherwise it |
| 248 | // doesn't dominate anything. |
| 249 | if (NewBBDominatesDestBB) { |
| 250 | if (!DestBBNode) DestBBNode = DT->getNode(DestBB); |
| 251 | DT->changeImmediateDominator(DestBBNode, NewBBNode); |
| 252 | } |
| 253 | } |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 12764c8 | 2002-10-31 02:44:36 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 256 | // Update LoopInfo if it is around. |
Chris Lattner | 5e7f705 | 2010-02-13 05:01:14 +0000 | [diff] [blame] | 257 | if (LI) { |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 258 | if (Loop *TIL = LI->getLoopFor(TIBB)) { |
| 259 | // If one or the other blocks were not in a loop, the new block is not |
| 260 | // either, and thus LI doesn't need to be updated. |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 261 | if (Loop *DestLoop = LI->getLoopFor(DestBB)) { |
| 262 | if (TIL == DestLoop) { |
| 263 | // Both in the same loop, the NewBB joins loop. |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 264 | DestLoop->addBasicBlockToLoop(NewBB, *LI); |
Dan Gohman | 18fa568 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 265 | } else if (TIL->contains(DestLoop)) { |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 266 | // Edge from an outer loop to an inner loop. Add to the outer loop. |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 267 | TIL->addBasicBlockToLoop(NewBB, *LI); |
Dan Gohman | 18fa568 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 268 | } else if (DestLoop->contains(TIL)) { |
Chris Lattner | 8aca0ee | 2006-10-03 07:02:02 +0000 | [diff] [blame] | 269 | // Edge from an inner loop to an outer loop. Add to the outer loop. |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 270 | DestLoop->addBasicBlockToLoop(NewBB, *LI); |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 271 | } else { |
| 272 | // Edge from two loops with no containment relation. Because these |
| 273 | // are natural loops, we know that the destination block must be the |
| 274 | // header of its loop (adding a branch into a loop elsewhere would |
| 275 | // create an irreducible loop). |
| 276 | assert(DestLoop->getHeader() == DestBB && |
| 277 | "Should not create irreducible loops!"); |
| 278 | if (Loop *P = DestLoop->getParentLoop()) |
Chandler Carruth | 691addc | 2015-01-18 01:25:51 +0000 | [diff] [blame] | 279 | P->addBasicBlockToLoop(NewBB, *LI); |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Chandler Carruth | ad34d91 | 2015-01-19 10:23:00 +0000 | [diff] [blame] | 282 | |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 283 | // If TIBB is in a loop and DestBB is outside of that loop, we may need |
| 284 | // to update LoopSimplify form and LCSSA form. |
Chandler Carruth | ad34d91 | 2015-01-19 10:23:00 +0000 | [diff] [blame] | 285 | if (!TIL->contains(DestBB)) { |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 286 | assert(!TIL->contains(NewBB) && |
| 287 | "Split point for loop exit is contained in loop!"); |
| 288 | |
| 289 | // Update LCSSA form in the newly created exit block. |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 290 | if (Options.PreserveLCSSA) { |
Bill Wendling | 325e6cd | 2012-04-30 10:25:51 +0000 | [diff] [blame] | 291 | createPHIsForSplitLoopExit(TIBB, NewBB, DestBB); |
Chandler Carruth | ad34d91 | 2015-01-19 10:23:00 +0000 | [diff] [blame] | 292 | } |
Dan Gohman | ec4557f | 2009-09-09 18:18:18 +0000 | [diff] [blame] | 293 | |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 294 | // The only that we can break LoopSimplify form by splitting a critical |
| 295 | // edge is if after the split there exists some edge from TIL to DestBB |
| 296 | // *and* the only edge into DestBB from outside of TIL is that of |
| 297 | // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB |
| 298 | // is the new exit block and it has no non-loop predecessors. If the |
| 299 | // second isn't true, then DestBB was not in LoopSimplify form prior to |
| 300 | // the split as it had a non-loop predecessor. In both of these cases, |
| 301 | // the predecessor must be directly in TIL, not in a subloop, or again |
| 302 | // LoopSimplify doesn't hold. |
| 303 | SmallVector<BasicBlock *, 4> LoopPreds; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 304 | for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E; |
| 305 | ++I) { |
| 306 | BasicBlock *P = *I; |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 307 | if (P == NewBB) |
| 308 | continue; // The new block is known. |
| 309 | if (LI->getLoopFor(P) != TIL) { |
| 310 | // No need to re-simplify, it wasn't to start with. |
| 311 | LoopPreds.clear(); |
| 312 | break; |
Gabor Greif | fd8e7d4 | 2010-07-09 16:31:08 +0000 | [diff] [blame] | 313 | } |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 314 | LoopPreds.push_back(P); |
| 315 | } |
| 316 | if (!LoopPreds.empty()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 317 | assert(!DestBB->isEHPad() && "We don't split edges to EH pads!"); |
Chandler Carruth | b5797b6 | 2015-01-18 09:21:15 +0000 | [diff] [blame] | 318 | BasicBlock *NewExitBB = SplitBlockPredecessors( |
Chandler Carruth | 96ada25 | 2015-07-22 09:52:54 +0000 | [diff] [blame] | 319 | DestBB, LoopPreds, "split", DT, LI, Options.PreserveLCSSA); |
Chandler Carruth | 37df2cf | 2015-01-19 12:09:11 +0000 | [diff] [blame] | 320 | if (Options.PreserveLCSSA) |
Chandler Carruth | d4be9dc | 2014-01-29 13:16:53 +0000 | [diff] [blame] | 321 | createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB); |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | 89c1dfc | 2005-08-13 01:38:43 +0000 | [diff] [blame] | 325 | } |
Dan Gohman | 3ddbc24 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 326 | |
| 327 | return NewBB; |
Chris Lattner | 75f80bd | 2002-09-24 15:51:56 +0000 | [diff] [blame] | 328 | } |