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