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