Introduce llvm::SplitAllCriticalEdges
[oota-llvm.git] / lib / Transforms / Utils / BasicBlockUtils.cpp
1 //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
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 // This family of functions perform manipulations on basic blocks, and
11 // instructions contained within basic blocks.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/CFG.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include <algorithm>
32 using namespace llvm;
33
34 /// DeleteDeadBlock - Delete the specified block, which must have no
35 /// predecessors.
36 void llvm::DeleteDeadBlock(BasicBlock *BB) {
37   assert((pred_begin(BB) == pred_end(BB) ||
38          // Can delete self loop.
39          BB->getSinglePredecessor() == BB) && "Block is not dead!");
40   TerminatorInst *BBTerm = BB->getTerminator();
41
42   // Loop through all of our successors and make sure they know that one
43   // of their predecessors is going away.
44   for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
45     BBTerm->getSuccessor(i)->removePredecessor(BB);
46
47   // Zap all the instructions in the block.
48   while (!BB->empty()) {
49     Instruction &I = BB->back();
50     // If this instruction is used, replace uses with an arbitrary value.
51     // Because control flow can't get here, we don't care what we replace the
52     // value with.  Note that since this block is unreachable, and all values
53     // contained within it must dominate their uses, that all uses will
54     // eventually be removed (they are themselves dead).
55     if (!I.use_empty())
56       I.replaceAllUsesWith(UndefValue::get(I.getType()));
57     BB->getInstList().pop_back();
58   }
59
60   // Zap the block!
61   BB->eraseFromParent();
62 }
63
64 /// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
65 /// any single-entry PHI nodes in it, fold them away.  This handles the case
66 /// when all entries to the PHI nodes in a block are guaranteed equal, such as
67 /// when the block has exactly one predecessor.
68 void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P) {
69   if (!isa<PHINode>(BB->begin())) return;
70
71   AliasAnalysis *AA = nullptr;
72   MemoryDependenceAnalysis *MemDep = nullptr;
73   if (P) {
74     AA = P->getAnalysisIfAvailable<AliasAnalysis>();
75     MemDep = P->getAnalysisIfAvailable<MemoryDependenceAnalysis>();
76   }
77
78   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
79     if (PN->getIncomingValue(0) != PN)
80       PN->replaceAllUsesWith(PN->getIncomingValue(0));
81     else
82       PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
83
84     if (MemDep)
85       MemDep->removeInstruction(PN);  // Memdep updates AA itself.
86     else if (AA && isa<PointerType>(PN->getType()))
87       AA->deleteValue(PN);
88
89     PN->eraseFromParent();
90   }
91 }
92
93
94 /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
95 /// is dead. Also recursively delete any operands that become dead as
96 /// a result. This includes tracing the def-use list from the PHI to see if
97 /// it is ultimately unused or if it reaches an unused cycle.
98 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) {
99   // Recursively deleting a PHI may cause multiple PHIs to be deleted
100   // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
101   SmallVector<WeakVH, 8> PHIs;
102   for (BasicBlock::iterator I = BB->begin();
103        PHINode *PN = dyn_cast<PHINode>(I); ++I)
104     PHIs.push_back(PN);
105
106   bool Changed = false;
107   for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
108     if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
109       Changed |= RecursivelyDeleteDeadPHINode(PN, TLI);
110
111   return Changed;
112 }
113
114 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
115 /// if possible.  The return value indicates success or failure.
116 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) {
117   // Don't merge away blocks who have their address taken.
118   if (BB->hasAddressTaken()) return false;
119
120   // Can't merge if there are multiple predecessors, or no predecessors.
121   BasicBlock *PredBB = BB->getUniquePredecessor();
122   if (!PredBB) return false;
123
124   // Don't break self-loops.
125   if (PredBB == BB) return false;
126   // Don't break invokes.
127   if (isa<InvokeInst>(PredBB->getTerminator())) return false;
128
129   succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
130   BasicBlock *OnlySucc = BB;
131   for (; SI != SE; ++SI)
132     if (*SI != OnlySucc) {
133       OnlySucc = nullptr;     // There are multiple distinct successors!
134       break;
135     }
136
137   // Can't merge if there are multiple successors.
138   if (!OnlySucc) return false;
139
140   // Can't merge if there is PHI loop.
141   for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {
142     if (PHINode *PN = dyn_cast<PHINode>(BI)) {
143       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
144         if (PN->getIncomingValue(i) == PN)
145           return false;
146     } else
147       break;
148   }
149
150   // Begin by getting rid of unneeded PHIs.
151   if (isa<PHINode>(BB->front()))
152     FoldSingleEntryPHINodes(BB, P);
153
154   // Delete the unconditional branch from the predecessor...
155   PredBB->getInstList().pop_back();
156
157   // Make all PHI nodes that referred to BB now refer to Pred as their
158   // source...
159   BB->replaceAllUsesWith(PredBB);
160
161   // Move all definitions in the successor to the predecessor...
162   PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
163
164   // Inherit predecessors name if it exists.
165   if (!PredBB->hasName())
166     PredBB->takeName(BB);
167
168   // Finally, erase the old block and update dominator info.
169   if (P) {
170     if (DominatorTreeWrapperPass *DTWP =
171             P->getAnalysisIfAvailable<DominatorTreeWrapperPass>()) {
172       DominatorTree &DT = DTWP->getDomTree();
173       if (DomTreeNode *DTN = DT.getNode(BB)) {
174         DomTreeNode *PredDTN = DT.getNode(PredBB);
175         SmallVector<DomTreeNode*, 8> Children(DTN->begin(), DTN->end());
176         for (SmallVectorImpl<DomTreeNode *>::iterator DI = Children.begin(),
177              DE = Children.end(); DI != DE; ++DI)
178           DT.changeImmediateDominator(*DI, PredDTN);
179
180         DT.eraseNode(BB);
181       }
182
183       if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
184         LI->removeBlock(BB);
185
186       if (MemoryDependenceAnalysis *MD =
187             P->getAnalysisIfAvailable<MemoryDependenceAnalysis>())
188         MD->invalidateCachedPredecessors();
189     }
190   }
191
192   BB->eraseFromParent();
193   return true;
194 }
195
196 /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
197 /// with a value, then remove and delete the original instruction.
198 ///
199 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
200                                 BasicBlock::iterator &BI, Value *V) {
201   Instruction &I = *BI;
202   // Replaces all of the uses of the instruction with uses of the value
203   I.replaceAllUsesWith(V);
204
205   // Make sure to propagate a name if there is one already.
206   if (I.hasName() && !V->hasName())
207     V->takeName(&I);
208
209   // Delete the unnecessary instruction now...
210   BI = BIL.erase(BI);
211 }
212
213
214 /// ReplaceInstWithInst - Replace the instruction specified by BI with the
215 /// instruction specified by I.  The original instruction is deleted and BI is
216 /// updated to point to the new instruction.
217 ///
218 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
219                                BasicBlock::iterator &BI, Instruction *I) {
220   assert(I->getParent() == nullptr &&
221          "ReplaceInstWithInst: Instruction already inserted into basic block!");
222
223   // Insert the new instruction into the basic block...
224   BasicBlock::iterator New = BIL.insert(BI, I);
225
226   // Replace all uses of the old instruction, and delete it.
227   ReplaceInstWithValue(BIL, BI, I);
228
229   // Move BI back to point to the newly inserted instruction
230   BI = New;
231 }
232
233 /// ReplaceInstWithInst - Replace the instruction specified by From with the
234 /// instruction specified by To.
235 ///
236 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
237   BasicBlock::iterator BI(From);
238   ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
239 }
240
241 /// SplitEdge -  Split the edge connecting specified block. Pass P must
242 /// not be NULL.
243 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) {
244   unsigned SuccNum = GetSuccessorNumber(BB, Succ);
245
246   // If this is a critical edge, let SplitCriticalEdge do it.
247   TerminatorInst *LatchTerm = BB->getTerminator();
248   if (SplitCriticalEdge(LatchTerm, SuccNum, P))
249     return LatchTerm->getSuccessor(SuccNum);
250
251   // If the edge isn't critical, then BB has a single successor or Succ has a
252   // single pred.  Split the block.
253   if (BasicBlock *SP = Succ->getSinglePredecessor()) {
254     // If the successor only has a single pred, split the top of the successor
255     // block.
256     assert(SP == BB && "CFG broken");
257     SP = nullptr;
258     return SplitBlock(Succ, Succ->begin(), P);
259   }
260
261   // Otherwise, if BB has a single successor, split it at the bottom of the
262   // block.
263   assert(BB->getTerminator()->getNumSuccessors() == 1 &&
264          "Should have a single succ!");
265   return SplitBlock(BB, BB->getTerminator(), P);
266 }
267
268 unsigned llvm::SplitAllCriticalEdges(Function &F, Pass *P) {
269   unsigned NumBroken = 0;
270   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
271     TerminatorInst *TI = I->getTerminator();
272     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
273       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
274         if (SplitCriticalEdge(TI, i, P))
275           ++NumBroken;
276   }
277   return NumBroken;
278 }
279
280 /// SplitBlock - Split the specified block at the specified instruction - every
281 /// thing before SplitPt stays in Old and everything starting with SplitPt moves
282 /// to a new block.  The two blocks are joined by an unconditional branch and
283 /// the loop info is updated.
284 ///
285 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) {
286   BasicBlock::iterator SplitIt = SplitPt;
287   while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt))
288     ++SplitIt;
289   BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
290
291   // The new block lives in whichever loop the old one did. This preserves
292   // LCSSA as well, because we force the split point to be after any PHI nodes.
293   if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
294     if (Loop *L = LI->getLoopFor(Old))
295       L->addBasicBlockToLoop(New, LI->getBase());
296
297   if (DominatorTreeWrapperPass *DTWP =
298           P->getAnalysisIfAvailable<DominatorTreeWrapperPass>()) {
299     DominatorTree &DT = DTWP->getDomTree();
300     // Old dominates New. New node dominates all other nodes dominated by Old.
301     if (DomTreeNode *OldNode = DT.getNode(Old)) {
302       std::vector<DomTreeNode *> Children;
303       for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end();
304            I != E; ++I)
305         Children.push_back(*I);
306
307       DomTreeNode *NewNode = DT.addNewBlock(New, Old);
308       for (std::vector<DomTreeNode *>::iterator I = Children.begin(),
309              E = Children.end(); I != E; ++I)
310         DT.changeImmediateDominator(*I, NewNode);
311     }
312   }
313
314   return New;
315 }
316
317 /// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA
318 /// analysis information.
319 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
320                                       ArrayRef<BasicBlock *> Preds,
321                                       Pass *P, bool &HasLoopExit) {
322   if (!P) return;
323
324   LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
325   Loop *L = LI ? LI->getLoopFor(OldBB) : nullptr;
326
327   // If we need to preserve loop analyses, collect some information about how
328   // this split will affect loops.
329   bool IsLoopEntry = !!L;
330   bool SplitMakesNewLoopHeader = false;
331   if (LI) {
332     bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID);
333     for (ArrayRef<BasicBlock*>::iterator
334            i = Preds.begin(), e = Preds.end(); i != e; ++i) {
335       BasicBlock *Pred = *i;
336
337       // If we need to preserve LCSSA, determine if any of the preds is a loop
338       // exit.
339       if (PreserveLCSSA)
340         if (Loop *PL = LI->getLoopFor(Pred))
341           if (!PL->contains(OldBB))
342             HasLoopExit = true;
343
344       // If we need to preserve LoopInfo, note whether any of the preds crosses
345       // an interesting loop boundary.
346       if (!L) continue;
347       if (L->contains(Pred))
348         IsLoopEntry = false;
349       else
350         SplitMakesNewLoopHeader = true;
351     }
352   }
353
354   // Update dominator tree if available.
355   if (DominatorTreeWrapperPass *DTWP =
356           P->getAnalysisIfAvailable<DominatorTreeWrapperPass>())
357     DTWP->getDomTree().splitBlock(NewBB);
358
359   if (!L) return;
360
361   if (IsLoopEntry) {
362     // Add the new block to the nearest enclosing loop (and not an adjacent
363     // loop). To find this, examine each of the predecessors and determine which
364     // loops enclose them, and select the most-nested loop which contains the
365     // loop containing the block being split.
366     Loop *InnermostPredLoop = nullptr;
367     for (ArrayRef<BasicBlock*>::iterator
368            i = Preds.begin(), e = Preds.end(); i != e; ++i) {
369       BasicBlock *Pred = *i;
370       if (Loop *PredLoop = LI->getLoopFor(Pred)) {
371         // Seek a loop which actually contains the block being split (to avoid
372         // adjacent loops).
373         while (PredLoop && !PredLoop->contains(OldBB))
374           PredLoop = PredLoop->getParentLoop();
375
376         // Select the most-nested of these loops which contains the block.
377         if (PredLoop && PredLoop->contains(OldBB) &&
378             (!InnermostPredLoop ||
379              InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
380           InnermostPredLoop = PredLoop;
381       }
382     }
383
384     if (InnermostPredLoop)
385       InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase());
386   } else {
387     L->addBasicBlockToLoop(NewBB, LI->getBase());
388     if (SplitMakesNewLoopHeader)
389       L->moveToHeader(NewBB);
390   }
391 }
392
393 /// UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming
394 /// from NewBB. This also updates AliasAnalysis, if available.
395 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
396                            ArrayRef<BasicBlock*> Preds, BranchInst *BI,
397                            Pass *P, bool HasLoopExit) {
398   // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
399   AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : nullptr;
400   SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
401   for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
402     PHINode *PN = cast<PHINode>(I++);
403
404     // Check to see if all of the values coming in are the same.  If so, we
405     // don't need to create a new PHI node, unless it's needed for LCSSA.
406     Value *InVal = nullptr;
407     if (!HasLoopExit) {
408       InVal = PN->getIncomingValueForBlock(Preds[0]);
409       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
410         if (!PredSet.count(PN->getIncomingBlock(i)))
411           continue;
412         if (!InVal)
413           InVal = PN->getIncomingValue(i);
414         else if (InVal != PN->getIncomingValue(i)) {
415           InVal = nullptr;
416           break;
417         }
418       }
419     }
420
421     if (InVal) {
422       // If all incoming values for the new PHI would be the same, just don't
423       // make a new PHI.  Instead, just remove the incoming values from the old
424       // PHI.
425
426       // NOTE! This loop walks backwards for a reason! First off, this minimizes
427       // the cost of removal if we end up removing a large number of values, and
428       // second off, this ensures that the indices for the incoming values
429       // aren't invalidated when we remove one.
430       for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
431         if (PredSet.count(PN->getIncomingBlock(i)))
432           PN->removeIncomingValue(i, false);
433
434       // Add an incoming value to the PHI node in the loop for the preheader
435       // edge.
436       PN->addIncoming(InVal, NewBB);
437       continue;
438     }
439
440     // If the values coming into the block are not the same, we need a new
441     // PHI.
442     // Create the new PHI node, insert it into NewBB at the end of the block
443     PHINode *NewPHI =
444         PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
445     if (AA)
446       AA->copyValue(PN, NewPHI);
447
448     // NOTE! This loop walks backwards for a reason! First off, this minimizes
449     // the cost of removal if we end up removing a large number of values, and
450     // second off, this ensures that the indices for the incoming values aren't
451     // invalidated when we remove one.
452     for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
453       BasicBlock *IncomingBB = PN->getIncomingBlock(i);
454       if (PredSet.count(IncomingBB)) {
455         Value *V = PN->removeIncomingValue(i, false);
456         NewPHI->addIncoming(V, IncomingBB);
457       }
458     }
459
460     PN->addIncoming(NewPHI, NewBB);
461   }
462 }
463
464 /// SplitBlockPredecessors - This method transforms BB by introducing a new
465 /// basic block into the function, and moving some of the predecessors of BB to
466 /// be predecessors of the new block.  The new predecessors are indicated by the
467 /// Preds array, which has NumPreds elements in it.  The new block is given a
468 /// suffix of 'Suffix'.
469 ///
470 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
471 /// LoopInfo, and LCCSA but no other analyses. In particular, it does not
472 /// preserve LoopSimplify (because it's complicated to handle the case where one
473 /// of the edges being split is an exit of a loop with other exits).
474 ///
475 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
476                                          ArrayRef<BasicBlock*> Preds,
477                                          const char *Suffix, Pass *P) {
478   // Create new basic block, insert right before the original block.
479   BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix,
480                                          BB->getParent(), BB);
481
482   // The new block unconditionally branches to the old block.
483   BranchInst *BI = BranchInst::Create(BB, NewBB);
484
485   // Move the edges from Preds to point to NewBB instead of BB.
486   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
487     // This is slightly more strict than necessary; the minimum requirement
488     // is that there be no more than one indirectbr branching to BB. And
489     // all BlockAddress uses would need to be updated.
490     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
491            "Cannot split an edge from an IndirectBrInst");
492     Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
493   }
494
495   // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
496   // node becomes an incoming value for BB's phi node.  However, if the Preds
497   // list is empty, we need to insert dummy entries into the PHI nodes in BB to
498   // account for the newly created predecessor.
499   if (Preds.size() == 0) {
500     // Insert dummy values as the incoming value.
501     for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
502       cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
503     return NewBB;
504   }
505
506   // Update DominatorTree, LoopInfo, and LCCSA analysis information.
507   bool HasLoopExit = false;
508   UpdateAnalysisInformation(BB, NewBB, Preds, P, HasLoopExit);
509
510   // Update the PHI nodes in BB with the values coming from NewBB.
511   UpdatePHINodes(BB, NewBB, Preds, BI, P, HasLoopExit);
512   return NewBB;
513 }
514
515 /// SplitLandingPadPredecessors - This method transforms the landing pad,
516 /// OrigBB, by introducing two new basic blocks into the function. One of those
517 /// new basic blocks gets the predecessors listed in Preds. The other basic
518 /// block gets the remaining predecessors of OrigBB. The landingpad instruction
519 /// OrigBB is clone into both of the new basic blocks. The new blocks are given
520 /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
521 ///
522 /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
523 /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
524 /// it does not preserve LoopSimplify (because it's complicated to handle the
525 /// case where one of the edges being split is an exit of a loop with other
526 /// exits).
527 ///
528 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
529                                        ArrayRef<BasicBlock*> Preds,
530                                        const char *Suffix1, const char *Suffix2,
531                                        Pass *P,
532                                        SmallVectorImpl<BasicBlock*> &NewBBs) {
533   assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
534
535   // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
536   // it right before the original block.
537   BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
538                                           OrigBB->getName() + Suffix1,
539                                           OrigBB->getParent(), OrigBB);
540   NewBBs.push_back(NewBB1);
541
542   // The new block unconditionally branches to the old block.
543   BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
544
545   // Move the edges from Preds to point to NewBB1 instead of OrigBB.
546   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
547     // This is slightly more strict than necessary; the minimum requirement
548     // is that there be no more than one indirectbr branching to BB. And
549     // all BlockAddress uses would need to be updated.
550     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
551            "Cannot split an edge from an IndirectBrInst");
552     Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
553   }
554
555   // Update DominatorTree, LoopInfo, and LCCSA analysis information.
556   bool HasLoopExit = false;
557   UpdateAnalysisInformation(OrigBB, NewBB1, Preds, P, HasLoopExit);
558
559   // Update the PHI nodes in OrigBB with the values coming from NewBB1.
560   UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, P, HasLoopExit);
561
562   // Move the remaining edges from OrigBB to point to NewBB2.
563   SmallVector<BasicBlock*, 8> NewBB2Preds;
564   for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
565        i != e; ) {
566     BasicBlock *Pred = *i++;
567     if (Pred == NewBB1) continue;
568     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
569            "Cannot split an edge from an IndirectBrInst");
570     NewBB2Preds.push_back(Pred);
571     e = pred_end(OrigBB);
572   }
573
574   BasicBlock *NewBB2 = nullptr;
575   if (!NewBB2Preds.empty()) {
576     // Create another basic block for the rest of OrigBB's predecessors.
577     NewBB2 = BasicBlock::Create(OrigBB->getContext(),
578                                 OrigBB->getName() + Suffix2,
579                                 OrigBB->getParent(), OrigBB);
580     NewBBs.push_back(NewBB2);
581
582     // The new block unconditionally branches to the old block.
583     BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
584
585     // Move the remaining edges from OrigBB to point to NewBB2.
586     for (SmallVectorImpl<BasicBlock*>::iterator
587            i = NewBB2Preds.begin(), e = NewBB2Preds.end(); i != e; ++i)
588       (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
589
590     // Update DominatorTree, LoopInfo, and LCCSA analysis information.
591     HasLoopExit = false;
592     UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, P, HasLoopExit);
593
594     // Update the PHI nodes in OrigBB with the values coming from NewBB2.
595     UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, P, HasLoopExit);
596   }
597
598   LandingPadInst *LPad = OrigBB->getLandingPadInst();
599   Instruction *Clone1 = LPad->clone();
600   Clone1->setName(Twine("lpad") + Suffix1);
601   NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
602
603   if (NewBB2) {
604     Instruction *Clone2 = LPad->clone();
605     Clone2->setName(Twine("lpad") + Suffix2);
606     NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
607
608     // Create a PHI node for the two cloned landingpad instructions.
609     PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
610     PN->addIncoming(Clone1, NewBB1);
611     PN->addIncoming(Clone2, NewBB2);
612     LPad->replaceAllUsesWith(PN);
613     LPad->eraseFromParent();
614   } else {
615     // There is no second clone. Just replace the landing pad with the first
616     // clone.
617     LPad->replaceAllUsesWith(Clone1);
618     LPad->eraseFromParent();
619   }
620 }
621
622 /// FoldReturnIntoUncondBranch - This method duplicates the specified return
623 /// instruction into a predecessor which ends in an unconditional branch. If
624 /// the return instruction returns a value defined by a PHI, propagate the
625 /// right value into the return. It returns the new return instruction in the
626 /// predecessor.
627 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
628                                              BasicBlock *Pred) {
629   Instruction *UncondBranch = Pred->getTerminator();
630   // Clone the return and add it to the end of the predecessor.
631   Instruction *NewRet = RI->clone();
632   Pred->getInstList().push_back(NewRet);
633
634   // If the return instruction returns a value, and if the value was a
635   // PHI node in "BB", propagate the right value into the return.
636   for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
637        i != e; ++i) {
638     Value *V = *i;
639     Instruction *NewBC = nullptr;
640     if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
641       // Return value might be bitcasted. Clone and insert it before the
642       // return instruction.
643       V = BCI->getOperand(0);
644       NewBC = BCI->clone();
645       Pred->getInstList().insert(NewRet, NewBC);
646       *i = NewBC;
647     }
648     if (PHINode *PN = dyn_cast<PHINode>(V)) {
649       if (PN->getParent() == BB) {
650         if (NewBC)
651           NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
652         else
653           *i = PN->getIncomingValueForBlock(Pred);
654       }
655     }
656   }
657
658   // Update any PHI nodes in the returning block to realize that we no
659   // longer branch to them.
660   BB->removePredecessor(Pred);
661   UncondBranch->eraseFromParent();
662   return cast<ReturnInst>(NewRet);
663 }
664
665 /// SplitBlockAndInsertIfThen - Split the containing block at the
666 /// specified instruction - everything before and including SplitBefore stays
667 /// in the old basic block, and everything after SplitBefore is moved to a
668 /// new block. The two blocks are connected by a conditional branch
669 /// (with value of Cmp being the condition).
670 /// Before:
671 ///   Head
672 ///   SplitBefore
673 ///   Tail
674 /// After:
675 ///   Head
676 ///   if (Cond)
677 ///     ThenBlock
678 ///   SplitBefore
679 ///   Tail
680 ///
681 /// If Unreachable is true, then ThenBlock ends with
682 /// UnreachableInst, otherwise it branches to Tail.
683 /// Returns the NewBasicBlock's terminator.
684
685 TerminatorInst *llvm::SplitBlockAndInsertIfThen(Value *Cond,
686                                                 Instruction *SplitBefore,
687                                                 bool Unreachable,
688                                                 MDNode *BranchWeights,
689                                                 DominatorTree *DT) {
690   BasicBlock *Head = SplitBefore->getParent();
691   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
692   TerminatorInst *HeadOldTerm = Head->getTerminator();
693   LLVMContext &C = Head->getContext();
694   BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
695   TerminatorInst *CheckTerm;
696   if (Unreachable)
697     CheckTerm = new UnreachableInst(C, ThenBlock);
698   else
699     CheckTerm = BranchInst::Create(Tail, ThenBlock);
700   CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
701   BranchInst *HeadNewTerm =
702     BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cond);
703   HeadNewTerm->setDebugLoc(SplitBefore->getDebugLoc());
704   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
705   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
706
707   if (DT) {
708     if (DomTreeNode *OldNode = DT->getNode(Head)) {
709       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
710
711       DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
712       for (auto Child : Children)
713         DT->changeImmediateDominator(Child, NewNode);
714
715       // Head dominates ThenBlock.
716       DT->addNewBlock(ThenBlock, Head);
717     }
718   }
719
720   return CheckTerm;
721 }
722
723 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
724 /// but also creates the ElseBlock.
725 /// Before:
726 ///   Head
727 ///   SplitBefore
728 ///   Tail
729 /// After:
730 ///   Head
731 ///   if (Cond)
732 ///     ThenBlock
733 ///   else
734 ///     ElseBlock
735 ///   SplitBefore
736 ///   Tail
737 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
738                                          TerminatorInst **ThenTerm,
739                                          TerminatorInst **ElseTerm,
740                                          MDNode *BranchWeights) {
741   BasicBlock *Head = SplitBefore->getParent();
742   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
743   TerminatorInst *HeadOldTerm = Head->getTerminator();
744   LLVMContext &C = Head->getContext();
745   BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
746   BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
747   *ThenTerm = BranchInst::Create(Tail, ThenBlock);
748   (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
749   *ElseTerm = BranchInst::Create(Tail, ElseBlock);
750   (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
751   BranchInst *HeadNewTerm =
752     BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
753   HeadNewTerm->setDebugLoc(SplitBefore->getDebugLoc());
754   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
755   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
756 }
757
758
759 /// GetIfCondition - Given a basic block (BB) with two predecessors,
760 /// check to see if the merge at this block is due
761 /// to an "if condition".  If so, return the boolean condition that determines
762 /// which entry into BB will be taken.  Also, return by references the block
763 /// that will be entered from if the condition is true, and the block that will
764 /// be entered if the condition is false.
765 ///
766 /// This does no checking to see if the true/false blocks have large or unsavory
767 /// instructions in them.
768 Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
769                              BasicBlock *&IfFalse) {
770   PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
771   BasicBlock *Pred1 = nullptr;
772   BasicBlock *Pred2 = nullptr;
773
774   if (SomePHI) {
775     if (SomePHI->getNumIncomingValues() != 2)
776       return nullptr;
777     Pred1 = SomePHI->getIncomingBlock(0);
778     Pred2 = SomePHI->getIncomingBlock(1);
779   } else {
780     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
781     if (PI == PE) // No predecessor
782       return nullptr;
783     Pred1 = *PI++;
784     if (PI == PE) // Only one predecessor
785       return nullptr;
786     Pred2 = *PI++;
787     if (PI != PE) // More than two predecessors
788       return nullptr;
789   }
790
791   // We can only handle branches.  Other control flow will be lowered to
792   // branches if possible anyway.
793   BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
794   BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
795   if (!Pred1Br || !Pred2Br)
796     return nullptr;
797
798   // Eliminate code duplication by ensuring that Pred1Br is conditional if
799   // either are.
800   if (Pred2Br->isConditional()) {
801     // If both branches are conditional, we don't have an "if statement".  In
802     // reality, we could transform this case, but since the condition will be
803     // required anyway, we stand no chance of eliminating it, so the xform is
804     // probably not profitable.
805     if (Pred1Br->isConditional())
806       return nullptr;
807
808     std::swap(Pred1, Pred2);
809     std::swap(Pred1Br, Pred2Br);
810   }
811
812   if (Pred1Br->isConditional()) {
813     // The only thing we have to watch out for here is to make sure that Pred2
814     // doesn't have incoming edges from other blocks.  If it does, the condition
815     // doesn't dominate BB.
816     if (!Pred2->getSinglePredecessor())
817       return nullptr;
818
819     // If we found a conditional branch predecessor, make sure that it branches
820     // to BB and Pred2Br.  If it doesn't, this isn't an "if statement".
821     if (Pred1Br->getSuccessor(0) == BB &&
822         Pred1Br->getSuccessor(1) == Pred2) {
823       IfTrue = Pred1;
824       IfFalse = Pred2;
825     } else if (Pred1Br->getSuccessor(0) == Pred2 &&
826                Pred1Br->getSuccessor(1) == BB) {
827       IfTrue = Pred2;
828       IfFalse = Pred1;
829     } else {
830       // We know that one arm of the conditional goes to BB, so the other must
831       // go somewhere unrelated, and this must not be an "if statement".
832       return nullptr;
833     }
834
835     return Pred1Br->getCondition();
836   }
837
838   // Ok, if we got here, both predecessors end with an unconditional branch to
839   // BB.  Don't panic!  If both blocks only have a single (identical)
840   // predecessor, and THAT is a conditional branch, then we're all ok!
841   BasicBlock *CommonPred = Pred1->getSinglePredecessor();
842   if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
843     return nullptr;
844
845   // Otherwise, if this is a conditional branch, then we can use it!
846   BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
847   if (!BI) return nullptr;
848
849   assert(BI->isConditional() && "Two successors but not conditional?");
850   if (BI->getSuccessor(0) == Pred1) {
851     IfTrue = Pred1;
852     IfFalse = Pred2;
853   } else {
854     IfTrue = Pred2;
855     IfFalse = Pred1;
856   }
857   return BI->getCondition();
858 }