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