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