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/PseudoProbe.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/User.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Transforms/Utils/Local.h"
46 #include <cassert>
47 #include <cstdint>
48 #include <string>
49 #include <utility>
50 #include <vector>
51 
52 using namespace llvm;
53 
54 #define DEBUG_TYPE "basicblock-utils"
55 
56 static cl::opt<unsigned> MaxDeoptimizingCheckDepth(
57     "max-deopt-check-depth", cl::init(8), cl::Hidden,
58     cl::desc("Set the maximum path length when checking whether a basic block "
59              "is deoptimizing"));
60 
61 void llvm::DetatchDeadBlocks(
62     ArrayRef<BasicBlock *> BBs,
63     SmallVectorImpl<DominatorTree::UpdateType> *Updates,
64     bool KeepOneInputPHIs) {
65   for (auto *BB : BBs) {
66     // Loop through all of our successors and make sure they know that one
67     // of their predecessors is going away.
68     SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
69     for (BasicBlock *Succ : successors(BB)) {
70       Succ->removePredecessor(BB, KeepOneInputPHIs);
71       if (Updates && UniqueSuccessors.insert(Succ).second)
72         Updates->push_back({DominatorTree::Delete, BB, Succ});
73     }
74 
75     // Zap all the instructions in the block.
76     while (!BB->empty()) {
77       Instruction &I = BB->back();
78       // If this instruction is used, replace uses with an arbitrary value.
79       // Because control flow can't get here, we don't care what we replace the
80       // value with.  Note that since this block is unreachable, and all values
81       // contained within it must dominate their uses, that all uses will
82       // eventually be removed (they are themselves dead).
83       if (!I.use_empty())
84         I.replaceAllUsesWith(UndefValue::get(I.getType()));
85       BB->getInstList().pop_back();
86     }
87     new UnreachableInst(BB->getContext(), BB);
88     assert(BB->getInstList().size() == 1 &&
89            isa<UnreachableInst>(BB->getTerminator()) &&
90            "The successor list of BB isn't empty before "
91            "applying corresponding DTU updates.");
92   }
93 }
94 
95 void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU,
96                            bool KeepOneInputPHIs) {
97   DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);
98 }
99 
100 void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU,
101                             bool KeepOneInputPHIs) {
102 #ifndef NDEBUG
103   // Make sure that all predecessors of each dead block is also dead.
104   SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());
105   assert(Dead.size() == BBs.size() && "Duplicating blocks?");
106   for (auto *BB : Dead)
107     for (BasicBlock *Pred : predecessors(BB))
108       assert(Dead.count(Pred) && "All predecessors must be dead!");
109 #endif
110 
111   SmallVector<DominatorTree::UpdateType, 4> Updates;
112   DetatchDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);
113 
114   if (DTU)
115     DTU->applyUpdates(Updates);
116 
117   for (BasicBlock *BB : BBs)
118     if (DTU)
119       DTU->deleteBB(BB);
120     else
121       BB->eraseFromParent();
122 }
123 
124 bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
125                                       bool KeepOneInputPHIs) {
126   df_iterator_default_set<BasicBlock*> Reachable;
127 
128   // Mark all reachable blocks.
129   for (BasicBlock *BB : depth_first_ext(&F, Reachable))
130     (void)BB/* Mark all reachable blocks */;
131 
132   // Collect all dead blocks.
133   std::vector<BasicBlock*> DeadBlocks;
134   for (BasicBlock &BB : F)
135     if (!Reachable.count(&BB))
136       DeadBlocks.push_back(&BB);
137 
138   // Delete the dead blocks.
139   DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);
140 
141   return !DeadBlocks.empty();
142 }
143 
144 bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
145                                    MemoryDependenceResults *MemDep) {
146   if (!isa<PHINode>(BB->begin()))
147     return false;
148 
149   while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
150     if (PN->getIncomingValue(0) != PN)
151       PN->replaceAllUsesWith(PN->getIncomingValue(0));
152     else
153       PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
154 
155     if (MemDep)
156       MemDep->removeInstruction(PN);  // Memdep updates AA itself.
157 
158     PN->eraseFromParent();
159   }
160   return true;
161 }
162 
163 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI,
164                           MemorySSAUpdater *MSSAU) {
165   // Recursively deleting a PHI may cause multiple PHIs to be deleted
166   // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
167   SmallVector<WeakTrackingVH, 8> PHIs;
168   for (PHINode &PN : BB->phis())
169     PHIs.push_back(&PN);
170 
171   bool Changed = false;
172   for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
173     if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
174       Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU);
175 
176   return Changed;
177 }
178 
179 bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
180                                      LoopInfo *LI, MemorySSAUpdater *MSSAU,
181                                      MemoryDependenceResults *MemDep,
182                                      bool PredecessorWithTwoSuccessors) {
183   if (BB->hasAddressTaken())
184     return false;
185 
186   // Can't merge if there are multiple predecessors, or no predecessors.
187   BasicBlock *PredBB = BB->getUniquePredecessor();
188   if (!PredBB) return false;
189 
190   // Don't break self-loops.
191   if (PredBB == BB) return false;
192   // Don't break unwinding instructions.
193   if (PredBB->getTerminator()->isExceptionalTerminator())
194     return false;
195 
196   // Can't merge if there are multiple distinct successors.
197   if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
198     return false;
199 
200   // Currently only allow PredBB to have two predecessors, one being BB.
201   // Update BI to branch to BB's only successor instead of BB.
202   BranchInst *PredBB_BI;
203   BasicBlock *NewSucc = nullptr;
204   unsigned FallThruPath;
205   if (PredecessorWithTwoSuccessors) {
206     if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator())))
207       return false;
208     BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator());
209     if (!BB_JmpI || !BB_JmpI->isUnconditional())
210       return false;
211     NewSucc = BB_JmpI->getSuccessor(0);
212     FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;
213   }
214 
215   // Can't merge if there is PHI loop.
216   for (PHINode &PN : BB->phis())
217     if (llvm::is_contained(PN.incoming_values(), &PN))
218       return false;
219 
220   LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
221                     << PredBB->getName() << "\n");
222 
223   // Begin by getting rid of unneeded PHIs.
224   SmallVector<AssertingVH<Value>, 4> IncomingValues;
225   if (isa<PHINode>(BB->front())) {
226     for (PHINode &PN : BB->phis())
227       if (!isa<PHINode>(PN.getIncomingValue(0)) ||
228           cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)
229         IncomingValues.push_back(PN.getIncomingValue(0));
230     FoldSingleEntryPHINodes(BB, MemDep);
231   }
232 
233   // DTU update: Collect all the edges that exit BB.
234   // These dominator edges will be redirected from Pred.
235   std::vector<DominatorTree::UpdateType> Updates;
236   if (DTU) {
237     SmallPtrSet<BasicBlock *, 2> SuccsOfBB(succ_begin(BB), succ_end(BB));
238     SmallPtrSet<BasicBlock *, 2> SuccsOfPredBB(succ_begin(PredBB),
239                                                succ_end(PredBB));
240     Updates.reserve(Updates.size() + 2 * SuccsOfBB.size() + 1);
241     // Add insert edges first. Experimentally, for the particular case of two
242     // blocks that can be merged, with a single successor and single predecessor
243     // respectively, it is beneficial to have all insert updates first. Deleting
244     // edges first may lead to unreachable blocks, followed by inserting edges
245     // making the blocks reachable again. Such DT updates lead to high compile
246     // times. We add inserts before deletes here to reduce compile time.
247     for (BasicBlock *SuccOfBB : SuccsOfBB)
248       // This successor of BB may already be a PredBB's successor.
249       if (!SuccsOfPredBB.contains(SuccOfBB))
250         Updates.push_back({DominatorTree::Insert, PredBB, SuccOfBB});
251     for (BasicBlock *SuccOfBB : SuccsOfBB)
252       Updates.push_back({DominatorTree::Delete, BB, SuccOfBB});
253     Updates.push_back({DominatorTree::Delete, PredBB, BB});
254   }
255 
256   Instruction *PTI = PredBB->getTerminator();
257   Instruction *STI = BB->getTerminator();
258   Instruction *Start = &*BB->begin();
259   // If there's nothing to move, mark the starting instruction as the last
260   // instruction in the block. Terminator instruction is handled separately.
261   if (Start == STI)
262     Start = PTI;
263 
264   // Move all definitions in the successor to the predecessor...
265   PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(),
266                                BB->begin(), STI->getIterator());
267 
268   if (MSSAU)
269     MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
270 
271   // Make all PHI nodes that referred to BB now refer to Pred as their
272   // source...
273   BB->replaceAllUsesWith(PredBB);
274 
275   if (PredecessorWithTwoSuccessors) {
276     // Delete the unconditional branch from BB.
277     BB->getInstList().pop_back();
278 
279     // Update branch in the predecessor.
280     PredBB_BI->setSuccessor(FallThruPath, NewSucc);
281   } else {
282     // Delete the unconditional branch from the predecessor.
283     PredBB->getInstList().pop_back();
284 
285     // Move terminator instruction.
286     PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
287 
288     // Terminator may be a memory accessing instruction too.
289     if (MSSAU)
290       if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>(
291               MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator())))
292         MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End);
293   }
294   // Add unreachable to now empty BB.
295   new UnreachableInst(BB->getContext(), BB);
296 
297   // Inherit predecessors name if it exists.
298   if (!PredBB->hasName())
299     PredBB->takeName(BB);
300 
301   if (LI)
302     LI->removeBlock(BB);
303 
304   if (MemDep)
305     MemDep->invalidateCachedPredecessors();
306 
307   if (DTU)
308     DTU->applyUpdates(Updates);
309 
310   // Finally, erase the old block and update dominator info.
311   DeleteDeadBlock(BB, DTU);
312 
313   return true;
314 }
315 
316 bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
317     SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,
318     LoopInfo *LI) {
319   assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
320 
321   bool BlocksHaveBeenMerged = false;
322   while (!MergeBlocks.empty()) {
323     BasicBlock *BB = *MergeBlocks.begin();
324     BasicBlock *Dest = BB->getSingleSuccessor();
325     if (Dest && (!L || L->contains(Dest))) {
326       BasicBlock *Fold = Dest->getUniquePredecessor();
327       (void)Fold;
328       if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {
329         assert(Fold == BB &&
330                "Expecting BB to be unique predecessor of the Dest block");
331         MergeBlocks.erase(Dest);
332         BlocksHaveBeenMerged = true;
333       } else
334         MergeBlocks.erase(BB);
335     } else
336       MergeBlocks.erase(BB);
337   }
338   return BlocksHaveBeenMerged;
339 }
340 
341 /// Remove redundant instructions within sequences of consecutive dbg.value
342 /// instructions. This is done using a backward scan to keep the last dbg.value
343 /// describing a specific variable/fragment.
344 ///
345 /// BackwardScan strategy:
346 /// ----------------------
347 /// Given a sequence of consecutive DbgValueInst like this
348 ///
349 ///   dbg.value ..., "x", FragmentX1  (*)
350 ///   dbg.value ..., "y", FragmentY1
351 ///   dbg.value ..., "x", FragmentX2
352 ///   dbg.value ..., "x", FragmentX1  (**)
353 ///
354 /// then the instruction marked with (*) can be removed (it is guaranteed to be
355 /// obsoleted by the instruction marked with (**) as the latter instruction is
356 /// describing the same variable using the same fragment info).
357 ///
358 /// Possible improvements:
359 /// - Check fully overlapping fragments and not only identical fragments.
360 /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta
361 ///   instructions being part of the sequence of consecutive instructions.
362 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
363   SmallVector<DbgValueInst *, 8> ToBeRemoved;
364   SmallDenseSet<DebugVariable> VariableSet;
365   for (auto &I : reverse(*BB)) {
366     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
367       DebugVariable Key(DVI->getVariable(),
368                         DVI->getExpression(),
369                         DVI->getDebugLoc()->getInlinedAt());
370       auto R = VariableSet.insert(Key);
371       // If the same variable fragment is described more than once it is enough
372       // to keep the last one (i.e. the first found since we for reverse
373       // iteration).
374       if (!R.second)
375         ToBeRemoved.push_back(DVI);
376       continue;
377     }
378     // Sequence with consecutive dbg.value instrs ended. Clear the map to
379     // restart identifying redundant instructions if case we find another
380     // dbg.value sequence.
381     VariableSet.clear();
382   }
383 
384   for (auto &Instr : ToBeRemoved)
385     Instr->eraseFromParent();
386 
387   return !ToBeRemoved.empty();
388 }
389 
390 /// Remove redundant dbg.value instructions using a forward scan. This can
391 /// remove a dbg.value instruction that is redundant due to indicating that a
392 /// variable has the same value as already being indicated by an earlier
393 /// dbg.value.
394 ///
395 /// ForwardScan strategy:
396 /// ---------------------
397 /// Given two identical dbg.value instructions, separated by a block of
398 /// instructions that isn't describing the same variable, like this
399 ///
400 ///   dbg.value X1, "x", FragmentX1  (**)
401 ///   <block of instructions, none being "dbg.value ..., "x", ...">
402 ///   dbg.value X1, "x", FragmentX1  (*)
403 ///
404 /// then the instruction marked with (*) can be removed. Variable "x" is already
405 /// described as being mapped to the SSA value X1.
406 ///
407 /// Possible improvements:
408 /// - Keep track of non-overlapping fragments.
409 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
410   SmallVector<DbgValueInst *, 8> ToBeRemoved;
411   DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>
412       VariableMap;
413   for (auto &I : *BB) {
414     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
415       DebugVariable Key(DVI->getVariable(),
416                         NoneType(),
417                         DVI->getDebugLoc()->getInlinedAt());
418       auto VMI = VariableMap.find(Key);
419       // Update the map if we found a new value/expression describing the
420       // variable, or if the variable wasn't mapped already.
421       SmallVector<Value *, 4> Values(DVI->getValues());
422       if (VMI == VariableMap.end() || VMI->second.first != Values ||
423           VMI->second.second != DVI->getExpression()) {
424         VariableMap[Key] = {Values, DVI->getExpression()};
425         continue;
426       }
427       // Found an identical mapping. Remember the instruction for later removal.
428       ToBeRemoved.push_back(DVI);
429     }
430   }
431 
432   for (auto &Instr : ToBeRemoved)
433     Instr->eraseFromParent();
434 
435   return !ToBeRemoved.empty();
436 }
437 
438 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
439   bool MadeChanges = false;
440   // By using the "backward scan" strategy before the "forward scan" strategy we
441   // can remove both dbg.value (2) and (3) in a situation like this:
442   //
443   //   (1) dbg.value V1, "x", DIExpression()
444   //       ...
445   //   (2) dbg.value V2, "x", DIExpression()
446   //   (3) dbg.value V1, "x", DIExpression()
447   //
448   // The backward scan will remove (2), it is made obsolete by (3). After
449   // getting (2) out of the way, the foward scan will remove (3) since "x"
450   // already is described as having the value V1 at (1).
451   MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);
452   MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);
453 
454   if (MadeChanges)
455     LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
456                       << BB->getName() << "\n");
457   return MadeChanges;
458 }
459 
460 void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
461                                 BasicBlock::iterator &BI, Value *V) {
462   Instruction &I = *BI;
463   // Replaces all of the uses of the instruction with uses of the value
464   I.replaceAllUsesWith(V);
465 
466   // Make sure to propagate a name if there is one already.
467   if (I.hasName() && !V->hasName())
468     V->takeName(&I);
469 
470   // Delete the unnecessary instruction now...
471   BI = BIL.erase(BI);
472 }
473 
474 void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
475                                BasicBlock::iterator &BI, Instruction *I) {
476   assert(I->getParent() == nullptr &&
477          "ReplaceInstWithInst: Instruction already inserted into basic block!");
478 
479   // Copy debug location to newly added instruction, if it wasn't already set
480   // by the caller.
481   if (!I->getDebugLoc())
482     I->setDebugLoc(BI->getDebugLoc());
483 
484   // Insert the new instruction into the basic block...
485   BasicBlock::iterator New = BIL.insert(BI, I);
486 
487   // Replace all uses of the old instruction, and delete it.
488   ReplaceInstWithValue(BIL, BI, I);
489 
490   // Move BI back to point to the newly inserted instruction
491   BI = New;
492 }
493 
494 bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {
495   // Remember visited blocks to avoid infinite loop
496   SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
497   unsigned Depth = 0;
498   while (BB && Depth++ < MaxDeoptimizingCheckDepth &&
499          VisitedBlocks.insert(BB).second) {
500     if (BB->getTerminatingDeoptimizeCall() ||
501         isa<UnreachableInst>(BB->getTerminator()))
502       return true;
503     BB = BB->getSingleSuccessor();
504   }
505   return false;
506 }
507 
508 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
509   BasicBlock::iterator BI(From);
510   ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
511 }
512 
513 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
514                             LoopInfo *LI, MemorySSAUpdater *MSSAU,
515                             const Twine &BBName) {
516   unsigned SuccNum = GetSuccessorNumber(BB, Succ);
517 
518   Instruction *LatchTerm = BB->getTerminator();
519 
520   CriticalEdgeSplittingOptions Options =
521       CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA();
522 
523   if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) {
524     // If it is a critical edge, and the succesor is an exception block, handle
525     // the split edge logic in this specific function
526     if (Succ->isEHPad())
527       return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName);
528 
529     // If this is a critical edge, let SplitKnownCriticalEdge do it.
530     return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);
531   }
532 
533   // If the edge isn't critical, then BB has a single successor or Succ has a
534   // single pred.  Split the block.
535   if (BasicBlock *SP = Succ->getSinglePredecessor()) {
536     // If the successor only has a single pred, split the top of the successor
537     // block.
538     assert(SP == BB && "CFG broken");
539     SP = nullptr;
540     return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName,
541                       /*Before=*/true);
542   }
543 
544   // Otherwise, if BB has a single successor, split it at the bottom of the
545   // block.
546   assert(BB->getTerminator()->getNumSuccessors() == 1 &&
547          "Should have a single succ!");
548   return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName);
549 }
550 
551 void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) {
552   if (auto *II = dyn_cast<InvokeInst>(TI))
553     II->setUnwindDest(Succ);
554   else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
555     CS->setUnwindDest(Succ);
556   else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
557     CR->setUnwindDest(Succ);
558   else
559     llvm_unreachable("unexpected terminator instruction");
560 }
561 
562 void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
563                           BasicBlock *NewPred, PHINode *Until) {
564   int BBIdx = 0;
565   for (PHINode &PN : DestBB->phis()) {
566     // We manually update the LandingPadReplacement PHINode and it is the last
567     // PHI Node. So, if we find it, we are done.
568     if (Until == &PN)
569       break;
570 
571     // Reuse the previous value of BBIdx if it lines up.  In cases where we
572     // have multiple phi nodes with *lots* of predecessors, this is a speed
573     // win because we don't have to scan the PHI looking for TIBB.  This
574     // happens because the BB list of PHI nodes are usually in the same
575     // order.
576     if (PN.getIncomingBlock(BBIdx) != OldPred)
577       BBIdx = PN.getBasicBlockIndex(OldPred);
578 
579     assert(BBIdx != -1 && "Invalid PHI Index!");
580     PN.setIncomingBlock(BBIdx, NewPred);
581   }
582 }
583 
584 BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
585                                    LandingPadInst *OriginalPad,
586                                    PHINode *LandingPadReplacement,
587                                    const CriticalEdgeSplittingOptions &Options,
588                                    const Twine &BBName) {
589 
590   auto *PadInst = Succ->getFirstNonPHI();
591   if (!LandingPadReplacement && !PadInst->isEHPad())
592     return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);
593 
594   auto *LI = Options.LI;
595   SmallVector<BasicBlock *, 4> LoopPreds;
596   // Check if extra modifications will be required to preserve loop-simplify
597   // form after splitting. If it would require splitting blocks with IndirectBr
598   // terminators, bail out if preserving loop-simplify form is requested.
599   if (Options.PreserveLoopSimplify && LI) {
600     if (Loop *BBLoop = LI->getLoopFor(BB)) {
601 
602       // The only way that we can break LoopSimplify form by splitting a
603       // critical edge is when there exists some edge from BBLoop to Succ *and*
604       // the only edge into Succ from outside of BBLoop is that of NewBB after
605       // the split. If the first isn't true, then LoopSimplify still holds,
606       // NewBB is the new exit block and it has no non-loop predecessors. If the
607       // second isn't true, then Succ was not in LoopSimplify form prior to
608       // the split as it had a non-loop predecessor. In both of these cases,
609       // the predecessor must be directly in BBLoop, not in a subloop, or again
610       // LoopSimplify doesn't hold.
611       for (BasicBlock *P : predecessors(Succ)) {
612         if (P == BB)
613           continue; // The new block is known.
614         if (LI->getLoopFor(P) != BBLoop) {
615           // Loop is not in LoopSimplify form, no need to re simplify after
616           // splitting edge.
617           LoopPreds.clear();
618           break;
619         }
620         LoopPreds.push_back(P);
621       }
622       // Loop-simplify form can be preserved, if we can split all in-loop
623       // predecessors.
624       if (any_of(LoopPreds, [](BasicBlock *Pred) {
625             return isa<IndirectBrInst>(Pred->getTerminator());
626           })) {
627         return nullptr;
628       }
629     }
630   }
631 
632   auto *NewBB =
633       BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);
634   setUnwindEdgeTo(BB->getTerminator(), NewBB);
635   updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
636 
637   if (LandingPadReplacement) {
638     auto *NewLP = OriginalPad->clone();
639     auto *Terminator = BranchInst::Create(Succ, NewBB);
640     NewLP->insertBefore(Terminator);
641     LandingPadReplacement->addIncoming(NewLP, NewBB);
642   } else {
643     Value *ParentPad = nullptr;
644     if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
645       ParentPad = FuncletPad->getParentPad();
646     else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
647       ParentPad = CatchSwitch->getParentPad();
648     else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))
649       ParentPad = CleanupPad->getParentPad();
650     else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))
651       ParentPad = LandingPad->getParent();
652     else
653       llvm_unreachable("handling for other EHPads not implemented yet");
654 
655     auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);
656     CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
657   }
658 
659   auto *DT = Options.DT;
660   auto *MSSAU = Options.MSSAU;
661   if (!DT && !LI)
662     return NewBB;
663 
664   if (DT) {
665     DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
666     SmallVector<DominatorTree::UpdateType, 3> Updates;
667 
668     Updates.push_back({DominatorTree::Insert, BB, NewBB});
669     Updates.push_back({DominatorTree::Insert, NewBB, Succ});
670     Updates.push_back({DominatorTree::Delete, BB, Succ});
671 
672     DTU.applyUpdates(Updates);
673     DTU.flush();
674 
675     if (MSSAU) {
676       MSSAU->applyUpdates(Updates, *DT);
677       if (VerifyMemorySSA)
678         MSSAU->getMemorySSA()->verifyMemorySSA();
679     }
680   }
681 
682   if (LI) {
683     if (Loop *BBLoop = LI->getLoopFor(BB)) {
684       // If one or the other blocks were not in a loop, the new block is not
685       // either, and thus LI doesn't need to be updated.
686       if (Loop *SuccLoop = LI->getLoopFor(Succ)) {
687         if (BBLoop == SuccLoop) {
688           // Both in the same loop, the NewBB joins loop.
689           SuccLoop->addBasicBlockToLoop(NewBB, *LI);
690         } else if (BBLoop->contains(SuccLoop)) {
691           // Edge from an outer loop to an inner loop.  Add to the outer loop.
692           BBLoop->addBasicBlockToLoop(NewBB, *LI);
693         } else if (SuccLoop->contains(BBLoop)) {
694           // Edge from an inner loop to an outer loop.  Add to the outer loop.
695           SuccLoop->addBasicBlockToLoop(NewBB, *LI);
696         } else {
697           // Edge from two loops with no containment relation.  Because these
698           // are natural loops, we know that the destination block must be the
699           // header of its loop (adding a branch into a loop elsewhere would
700           // create an irreducible loop).
701           assert(SuccLoop->getHeader() == Succ &&
702                  "Should not create irreducible loops!");
703           if (Loop *P = SuccLoop->getParentLoop())
704             P->addBasicBlockToLoop(NewBB, *LI);
705         }
706       }
707 
708       // If BB is in a loop and Succ is outside of that loop, we may need to
709       // update LoopSimplify form and LCSSA form.
710       if (!BBLoop->contains(Succ)) {
711         assert(!BBLoop->contains(NewBB) &&
712                "Split point for loop exit is contained in loop!");
713 
714         // Update LCSSA form in the newly created exit block.
715         if (Options.PreserveLCSSA) {
716           createPHIsForSplitLoopExit(BB, NewBB, Succ);
717         }
718 
719         if (!LoopPreds.empty()) {
720           BasicBlock *NewExitBB = SplitBlockPredecessors(
721               Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
722           if (Options.PreserveLCSSA)
723             createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);
724         }
725       }
726     }
727   }
728 
729   return NewBB;
730 }
731 
732 void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
733                                       BasicBlock *SplitBB, BasicBlock *DestBB) {
734   // SplitBB shouldn't have anything non-trivial in it yet.
735   assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
736           SplitBB->isLandingPad()) &&
737          "SplitBB has non-PHI nodes!");
738 
739   // For each PHI in the destination block.
740   for (PHINode &PN : DestBB->phis()) {
741     int Idx = PN.getBasicBlockIndex(SplitBB);
742     assert(Idx >= 0 && "Invalid Block Index");
743     Value *V = PN.getIncomingValue(Idx);
744 
745     // If the input is a PHI which already satisfies LCSSA, don't create
746     // a new one.
747     if (const PHINode *VP = dyn_cast<PHINode>(V))
748       if (VP->getParent() == SplitBB)
749         continue;
750 
751     // Otherwise a new PHI is needed. Create one and populate it.
752     PHINode *NewPN = PHINode::Create(
753         PN.getType(), Preds.size(), "split",
754         SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator());
755     for (BasicBlock *BB : Preds)
756       NewPN->addIncoming(V, BB);
757 
758     // Update the original PHI.
759     PN.setIncomingValue(Idx, NewPN);
760   }
761 }
762 
763 unsigned
764 llvm::SplitAllCriticalEdges(Function &F,
765                             const CriticalEdgeSplittingOptions &Options) {
766   unsigned NumBroken = 0;
767   for (BasicBlock &BB : F) {
768     Instruction *TI = BB.getTerminator();
769     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) &&
770         !isa<CallBrInst>(TI))
771       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
772         if (SplitCriticalEdge(TI, i, Options))
773           ++NumBroken;
774   }
775   return NumBroken;
776 }
777 
778 static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
779                                   DomTreeUpdater *DTU, DominatorTree *DT,
780                                   LoopInfo *LI, MemorySSAUpdater *MSSAU,
781                                   const Twine &BBName, bool Before) {
782   if (Before) {
783     DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
784     return splitBlockBefore(Old, SplitPt,
785                             DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
786                             BBName);
787   }
788   BasicBlock::iterator SplitIt = SplitPt->getIterator();
789   while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
790     ++SplitIt;
791     assert(SplitIt != SplitPt->getParent()->end());
792   }
793   std::string Name = BBName.str();
794   BasicBlock *New = Old->splitBasicBlock(
795       SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
796 
797   // The new block lives in whichever loop the old one did. This preserves
798   // LCSSA as well, because we force the split point to be after any PHI nodes.
799   if (LI)
800     if (Loop *L = LI->getLoopFor(Old))
801       L->addBasicBlockToLoop(New, *LI);
802 
803   if (DTU) {
804     SmallVector<DominatorTree::UpdateType, 8> Updates;
805     // Old dominates New. New node dominates all other nodes dominated by Old.
806     SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld(succ_begin(New),
807                                                        succ_end(New));
808     Updates.push_back({DominatorTree::Insert, Old, New});
809     Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfOld.size());
810     for (BasicBlock *UniqueSuccessorOfOld : UniqueSuccessorsOfOld) {
811       Updates.push_back({DominatorTree::Insert, New, UniqueSuccessorOfOld});
812       Updates.push_back({DominatorTree::Delete, Old, UniqueSuccessorOfOld});
813     }
814 
815     DTU->applyUpdates(Updates);
816   } else if (DT)
817     // Old dominates New. New node dominates all other nodes dominated by Old.
818     if (DomTreeNode *OldNode = DT->getNode(Old)) {
819       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
820 
821       DomTreeNode *NewNode = DT->addNewBlock(New, Old);
822       for (DomTreeNode *I : Children)
823         DT->changeImmediateDominator(I, NewNode);
824     }
825 
826   // Move MemoryAccesses still tracked in Old, but part of New now.
827   // Update accesses in successor blocks accordingly.
828   if (MSSAU)
829     MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
830 
831   return New;
832 }
833 
834 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
835                              DominatorTree *DT, LoopInfo *LI,
836                              MemorySSAUpdater *MSSAU, const Twine &BBName,
837                              bool Before) {
838   return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
839                         Before);
840 }
841 BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
842                              DomTreeUpdater *DTU, LoopInfo *LI,
843                              MemorySSAUpdater *MSSAU, const Twine &BBName,
844                              bool Before) {
845   return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
846                         Before);
847 }
848 
849 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
850                                    DomTreeUpdater *DTU, LoopInfo *LI,
851                                    MemorySSAUpdater *MSSAU,
852                                    const Twine &BBName) {
853 
854   BasicBlock::iterator SplitIt = SplitPt->getIterator();
855   while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
856     ++SplitIt;
857   std::string Name = BBName.str();
858   BasicBlock *New = Old->splitBasicBlock(
859       SplitIt, Name.empty() ? Old->getName() + ".split" : Name,
860       /* Before=*/true);
861 
862   // The new block lives in whichever loop the old one did. This preserves
863   // LCSSA as well, because we force the split point to be after any PHI nodes.
864   if (LI)
865     if (Loop *L = LI->getLoopFor(Old))
866       L->addBasicBlockToLoop(New, *LI);
867 
868   if (DTU) {
869     SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
870     // New dominates Old. The predecessor nodes of the Old node dominate
871     // New node.
872     SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld(pred_begin(New),
873                                                          pred_end(New));
874     DTUpdates.push_back({DominatorTree::Insert, New, Old});
875     DTUpdates.reserve(DTUpdates.size() + 2 * UniquePredecessorsOfOld.size());
876     for (BasicBlock *UniquePredecessorOfOld : UniquePredecessorsOfOld) {
877       DTUpdates.push_back({DominatorTree::Insert, UniquePredecessorOfOld, New});
878       DTUpdates.push_back({DominatorTree::Delete, UniquePredecessorOfOld, Old});
879     }
880 
881     DTU->applyUpdates(DTUpdates);
882 
883     // Move MemoryAccesses still tracked in Old, but part of New now.
884     // Update accesses in successor blocks accordingly.
885     if (MSSAU) {
886       MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
887       if (VerifyMemorySSA)
888         MSSAU->getMemorySSA()->verifyMemorySSA();
889     }
890   }
891   return New;
892 }
893 
894 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
895 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
896                                       ArrayRef<BasicBlock *> Preds,
897                                       DomTreeUpdater *DTU, DominatorTree *DT,
898                                       LoopInfo *LI, MemorySSAUpdater *MSSAU,
899                                       bool PreserveLCSSA, bool &HasLoopExit) {
900   // Update dominator tree if available.
901   if (DTU) {
902     // Recalculation of DomTree is needed when updating a forward DomTree and
903     // the Entry BB is replaced.
904     if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
905       // The entry block was removed and there is no external interface for
906       // the dominator tree to be notified of this change. In this corner-case
907       // we recalculate the entire tree.
908       DTU->recalculate(*NewBB->getParent());
909     } else {
910       // Split block expects NewBB to have a non-empty set of predecessors.
911       SmallVector<DominatorTree::UpdateType, 8> Updates;
912       SmallPtrSet<BasicBlock *, 8> UniquePreds(Preds.begin(), Preds.end());
913       Updates.push_back({DominatorTree::Insert, NewBB, OldBB});
914       Updates.reserve(Updates.size() + 2 * UniquePreds.size());
915       for (auto *UniquePred : UniquePreds) {
916         Updates.push_back({DominatorTree::Insert, UniquePred, NewBB});
917         Updates.push_back({DominatorTree::Delete, UniquePred, OldBB});
918       }
919       DTU->applyUpdates(Updates);
920     }
921   } else if (DT) {
922     if (OldBB == DT->getRootNode()->getBlock()) {
923       assert(NewBB->isEntryBlock());
924       DT->setNewRoot(NewBB);
925     } else {
926       // Split block expects NewBB to have a non-empty set of predecessors.
927       DT->splitBlock(NewBB);
928     }
929   }
930 
931   // Update MemoryPhis after split if MemorySSA is available
932   if (MSSAU)
933     MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
934 
935   // The rest of the logic is only relevant for updating the loop structures.
936   if (!LI)
937     return;
938 
939   if (DTU && DTU->hasDomTree())
940     DT = &DTU->getDomTree();
941   assert(DT && "DT should be available to update LoopInfo!");
942   Loop *L = LI->getLoopFor(OldBB);
943 
944   // If we need to preserve loop analyses, collect some information about how
945   // this split will affect loops.
946   bool IsLoopEntry = !!L;
947   bool SplitMakesNewLoopHeader = false;
948   for (BasicBlock *Pred : Preds) {
949     // Preds that are not reachable from entry should not be used to identify if
950     // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
951     // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
952     // as true and make the NewBB the header of some loop. This breaks LI.
953     if (!DT->isReachableFromEntry(Pred))
954       continue;
955     // If we need to preserve LCSSA, determine if any of the preds is a loop
956     // exit.
957     if (PreserveLCSSA)
958       if (Loop *PL = LI->getLoopFor(Pred))
959         if (!PL->contains(OldBB))
960           HasLoopExit = true;
961 
962     // If we need to preserve LoopInfo, note whether any of the preds crosses
963     // an interesting loop boundary.
964     if (!L)
965       continue;
966     if (L->contains(Pred))
967       IsLoopEntry = false;
968     else
969       SplitMakesNewLoopHeader = true;
970   }
971 
972   // Unless we have a loop for OldBB, nothing else to do here.
973   if (!L)
974     return;
975 
976   if (IsLoopEntry) {
977     // Add the new block to the nearest enclosing loop (and not an adjacent
978     // loop). To find this, examine each of the predecessors and determine which
979     // loops enclose them, and select the most-nested loop which contains the
980     // loop containing the block being split.
981     Loop *InnermostPredLoop = nullptr;
982     for (BasicBlock *Pred : Preds) {
983       if (Loop *PredLoop = LI->getLoopFor(Pred)) {
984         // Seek a loop which actually contains the block being split (to avoid
985         // adjacent loops).
986         while (PredLoop && !PredLoop->contains(OldBB))
987           PredLoop = PredLoop->getParentLoop();
988 
989         // Select the most-nested of these loops which contains the block.
990         if (PredLoop && PredLoop->contains(OldBB) &&
991             (!InnermostPredLoop ||
992              InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
993           InnermostPredLoop = PredLoop;
994       }
995     }
996 
997     if (InnermostPredLoop)
998       InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
999   } else {
1000     L->addBasicBlockToLoop(NewBB, *LI);
1001     if (SplitMakesNewLoopHeader)
1002       L->moveToHeader(NewBB);
1003   }
1004 }
1005 
1006 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
1007 /// This also updates AliasAnalysis, if available.
1008 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
1009                            ArrayRef<BasicBlock *> Preds, BranchInst *BI,
1010                            bool HasLoopExit) {
1011   // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
1012   SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
1013   for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
1014     PHINode *PN = cast<PHINode>(I++);
1015 
1016     // Check to see if all of the values coming in are the same.  If so, we
1017     // don't need to create a new PHI node, unless it's needed for LCSSA.
1018     Value *InVal = nullptr;
1019     if (!HasLoopExit) {
1020       InVal = PN->getIncomingValueForBlock(Preds[0]);
1021       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1022         if (!PredSet.count(PN->getIncomingBlock(i)))
1023           continue;
1024         if (!InVal)
1025           InVal = PN->getIncomingValue(i);
1026         else if (InVal != PN->getIncomingValue(i)) {
1027           InVal = nullptr;
1028           break;
1029         }
1030       }
1031     }
1032 
1033     if (InVal) {
1034       // If all incoming values for the new PHI would be the same, just don't
1035       // make a new PHI.  Instead, just remove the incoming values from the old
1036       // PHI.
1037 
1038       // NOTE! This loop walks backwards for a reason! First off, this minimizes
1039       // the cost of removal if we end up removing a large number of values, and
1040       // second off, this ensures that the indices for the incoming values
1041       // aren't invalidated when we remove one.
1042       for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
1043         if (PredSet.count(PN->getIncomingBlock(i)))
1044           PN->removeIncomingValue(i, false);
1045 
1046       // Add an incoming value to the PHI node in the loop for the preheader
1047       // edge.
1048       PN->addIncoming(InVal, NewBB);
1049       continue;
1050     }
1051 
1052     // If the values coming into the block are not the same, we need a new
1053     // PHI.
1054     // Create the new PHI node, insert it into NewBB at the end of the block
1055     PHINode *NewPHI =
1056         PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
1057 
1058     // NOTE! This loop walks backwards for a reason! First off, this minimizes
1059     // the cost of removal if we end up removing a large number of values, and
1060     // second off, this ensures that the indices for the incoming values aren't
1061     // invalidated when we remove one.
1062     for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
1063       BasicBlock *IncomingBB = PN->getIncomingBlock(i);
1064       if (PredSet.count(IncomingBB)) {
1065         Value *V = PN->removeIncomingValue(i, false);
1066         NewPHI->addIncoming(V, IncomingBB);
1067       }
1068     }
1069 
1070     PN->addIncoming(NewPHI, NewBB);
1071   }
1072 }
1073 
1074 static void SplitLandingPadPredecessorsImpl(
1075     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1076     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1077     DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1078     MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
1079 
1080 static BasicBlock *
1081 SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
1082                            const char *Suffix, DomTreeUpdater *DTU,
1083                            DominatorTree *DT, LoopInfo *LI,
1084                            MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1085   // Do not attempt to split that which cannot be split.
1086   if (!BB->canSplitPredecessors())
1087     return nullptr;
1088 
1089   // For the landingpads we need to act a bit differently.
1090   // Delegate this work to the SplitLandingPadPredecessors.
1091   if (BB->isLandingPad()) {
1092     SmallVector<BasicBlock*, 2> NewBBs;
1093     std::string NewName = std::string(Suffix) + ".split-lp";
1094 
1095     SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,
1096                                     DTU, DT, LI, MSSAU, PreserveLCSSA);
1097     return NewBBs[0];
1098   }
1099 
1100   // Create new basic block, insert right before the original block.
1101   BasicBlock *NewBB = BasicBlock::Create(
1102       BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
1103 
1104   // The new block unconditionally branches to the old block.
1105   BranchInst *BI = BranchInst::Create(BB, NewBB);
1106 
1107   Loop *L = nullptr;
1108   BasicBlock *OldLatch = nullptr;
1109   // Splitting the predecessors of a loop header creates a preheader block.
1110   if (LI && LI->isLoopHeader(BB)) {
1111     L = LI->getLoopFor(BB);
1112     // Using the loop start line number prevents debuggers stepping into the
1113     // loop body for this instruction.
1114     BI->setDebugLoc(L->getStartLoc());
1115 
1116     // If BB is the header of the Loop, it is possible that the loop is
1117     // modified, such that the current latch does not remain the latch of the
1118     // loop. If that is the case, the loop metadata from the current latch needs
1119     // to be applied to the new latch.
1120     OldLatch = L->getLoopLatch();
1121   } else
1122     BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
1123 
1124   // Move the edges from Preds to point to NewBB instead of BB.
1125   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1126     // This is slightly more strict than necessary; the minimum requirement
1127     // is that there be no more than one indirectbr branching to BB. And
1128     // all BlockAddress uses would need to be updated.
1129     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
1130            "Cannot split an edge from an IndirectBrInst");
1131     assert(!isa<CallBrInst>(Preds[i]->getTerminator()) &&
1132            "Cannot split an edge from a CallBrInst");
1133     Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
1134   }
1135 
1136   // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
1137   // node becomes an incoming value for BB's phi node.  However, if the Preds
1138   // list is empty, we need to insert dummy entries into the PHI nodes in BB to
1139   // account for the newly created predecessor.
1140   if (Preds.empty()) {
1141     // Insert dummy values as the incoming value.
1142     for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
1143       cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
1144   }
1145 
1146   // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1147   bool HasLoopExit = false;
1148   UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,
1149                             HasLoopExit);
1150 
1151   if (!Preds.empty()) {
1152     // Update the PHI nodes in BB with the values coming from NewBB.
1153     UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
1154   }
1155 
1156   if (OldLatch) {
1157     BasicBlock *NewLatch = L->getLoopLatch();
1158     if (NewLatch != OldLatch) {
1159       MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
1160       NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
1161       OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
1162     }
1163   }
1164 
1165   return NewBB;
1166 }
1167 
1168 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1169                                          ArrayRef<BasicBlock *> Preds,
1170                                          const char *Suffix, DominatorTree *DT,
1171                                          LoopInfo *LI, MemorySSAUpdater *MSSAU,
1172                                          bool PreserveLCSSA) {
1173   return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,
1174                                     MSSAU, PreserveLCSSA);
1175 }
1176 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1177                                          ArrayRef<BasicBlock *> Preds,
1178                                          const char *Suffix,
1179                                          DomTreeUpdater *DTU, LoopInfo *LI,
1180                                          MemorySSAUpdater *MSSAU,
1181                                          bool PreserveLCSSA) {
1182   return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,
1183                                     /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);
1184 }
1185 
1186 static void SplitLandingPadPredecessorsImpl(
1187     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1188     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1189     DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1190     MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1191   assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
1192 
1193   // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
1194   // it right before the original block.
1195   BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
1196                                           OrigBB->getName() + Suffix1,
1197                                           OrigBB->getParent(), OrigBB);
1198   NewBBs.push_back(NewBB1);
1199 
1200   // The new block unconditionally branches to the old block.
1201   BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
1202   BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1203 
1204   // Move the edges from Preds to point to NewBB1 instead of OrigBB.
1205   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1206     // This is slightly more strict than necessary; the minimum requirement
1207     // is that there be no more than one indirectbr branching to BB. And
1208     // all BlockAddress uses would need to be updated.
1209     assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
1210            "Cannot split an edge from an IndirectBrInst");
1211     Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
1212   }
1213 
1214   bool HasLoopExit = false;
1215   UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,
1216                             PreserveLCSSA, HasLoopExit);
1217 
1218   // Update the PHI nodes in OrigBB with the values coming from NewBB1.
1219   UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
1220 
1221   // Move the remaining edges from OrigBB to point to NewBB2.
1222   SmallVector<BasicBlock*, 8> NewBB2Preds;
1223   for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
1224        i != e; ) {
1225     BasicBlock *Pred = *i++;
1226     if (Pred == NewBB1) continue;
1227     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1228            "Cannot split an edge from an IndirectBrInst");
1229     NewBB2Preds.push_back(Pred);
1230     e = pred_end(OrigBB);
1231   }
1232 
1233   BasicBlock *NewBB2 = nullptr;
1234   if (!NewBB2Preds.empty()) {
1235     // Create another basic block for the rest of OrigBB's predecessors.
1236     NewBB2 = BasicBlock::Create(OrigBB->getContext(),
1237                                 OrigBB->getName() + Suffix2,
1238                                 OrigBB->getParent(), OrigBB);
1239     NewBBs.push_back(NewBB2);
1240 
1241     // The new block unconditionally branches to the old block.
1242     BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
1243     BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1244 
1245     // Move the remaining edges from OrigBB to point to NewBB2.
1246     for (BasicBlock *NewBB2Pred : NewBB2Preds)
1247       NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
1248 
1249     // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1250     HasLoopExit = false;
1251     UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,
1252                               PreserveLCSSA, HasLoopExit);
1253 
1254     // Update the PHI nodes in OrigBB with the values coming from NewBB2.
1255     UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
1256   }
1257 
1258   LandingPadInst *LPad = OrigBB->getLandingPadInst();
1259   Instruction *Clone1 = LPad->clone();
1260   Clone1->setName(Twine("lpad") + Suffix1);
1261   NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
1262 
1263   if (NewBB2) {
1264     Instruction *Clone2 = LPad->clone();
1265     Clone2->setName(Twine("lpad") + Suffix2);
1266     NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
1267 
1268     // Create a PHI node for the two cloned landingpad instructions only
1269     // if the original landingpad instruction has some uses.
1270     if (!LPad->use_empty()) {
1271       assert(!LPad->getType()->isTokenTy() &&
1272              "Split cannot be applied if LPad is token type. Otherwise an "
1273              "invalid PHINode of token type would be created.");
1274       PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
1275       PN->addIncoming(Clone1, NewBB1);
1276       PN->addIncoming(Clone2, NewBB2);
1277       LPad->replaceAllUsesWith(PN);
1278     }
1279     LPad->eraseFromParent();
1280   } else {
1281     // There is no second clone. Just replace the landing pad with the first
1282     // clone.
1283     LPad->replaceAllUsesWith(Clone1);
1284     LPad->eraseFromParent();
1285   }
1286 }
1287 
1288 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
1289                                        ArrayRef<BasicBlock *> Preds,
1290                                        const char *Suffix1, const char *Suffix2,
1291                                        SmallVectorImpl<BasicBlock *> &NewBBs,
1292                                        DominatorTree *DT, LoopInfo *LI,
1293                                        MemorySSAUpdater *MSSAU,
1294                                        bool PreserveLCSSA) {
1295   return SplitLandingPadPredecessorsImpl(
1296       OrigBB, Preds, Suffix1, Suffix2, NewBBs,
1297       /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA);
1298 }
1299 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
1300                                        ArrayRef<BasicBlock *> Preds,
1301                                        const char *Suffix1, const char *Suffix2,
1302                                        SmallVectorImpl<BasicBlock *> &NewBBs,
1303                                        DomTreeUpdater *DTU, LoopInfo *LI,
1304                                        MemorySSAUpdater *MSSAU,
1305                                        bool PreserveLCSSA) {
1306   return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,
1307                                          NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,
1308                                          PreserveLCSSA);
1309 }
1310 
1311 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
1312                                              BasicBlock *Pred,
1313                                              DomTreeUpdater *DTU) {
1314   Instruction *UncondBranch = Pred->getTerminator();
1315   // Clone the return and add it to the end of the predecessor.
1316   Instruction *NewRet = RI->clone();
1317   Pred->getInstList().push_back(NewRet);
1318 
1319   // If the return instruction returns a value, and if the value was a
1320   // PHI node in "BB", propagate the right value into the return.
1321   for (Use &Op : NewRet->operands()) {
1322     Value *V = Op;
1323     Instruction *NewBC = nullptr;
1324     if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
1325       // Return value might be bitcasted. Clone and insert it before the
1326       // return instruction.
1327       V = BCI->getOperand(0);
1328       NewBC = BCI->clone();
1329       Pred->getInstList().insert(NewRet->getIterator(), NewBC);
1330       Op = NewBC;
1331     }
1332 
1333     Instruction *NewEV = nullptr;
1334     if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
1335       V = EVI->getOperand(0);
1336       NewEV = EVI->clone();
1337       if (NewBC) {
1338         NewBC->setOperand(0, NewEV);
1339         Pred->getInstList().insert(NewBC->getIterator(), NewEV);
1340       } else {
1341         Pred->getInstList().insert(NewRet->getIterator(), NewEV);
1342         Op = NewEV;
1343       }
1344     }
1345 
1346     if (PHINode *PN = dyn_cast<PHINode>(V)) {
1347       if (PN->getParent() == BB) {
1348         if (NewEV) {
1349           NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
1350         } else if (NewBC)
1351           NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
1352         else
1353           Op = PN->getIncomingValueForBlock(Pred);
1354       }
1355     }
1356   }
1357 
1358   // Update any PHI nodes in the returning block to realize that we no
1359   // longer branch to them.
1360   BB->removePredecessor(Pred);
1361   UncondBranch->eraseFromParent();
1362 
1363   if (DTU)
1364     DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
1365 
1366   return cast<ReturnInst>(NewRet);
1367 }
1368 
1369 static Instruction *
1370 SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,
1371                               bool Unreachable, MDNode *BranchWeights,
1372                               DomTreeUpdater *DTU, DominatorTree *DT,
1373                               LoopInfo *LI, BasicBlock *ThenBlock) {
1374   SmallVector<DominatorTree::UpdateType, 8> Updates;
1375   BasicBlock *Head = SplitBefore->getParent();
1376   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1377   if (DTU) {
1378     SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfHead(succ_begin(Tail),
1379                                                         succ_end(Tail));
1380     Updates.push_back({DominatorTree::Insert, Head, Tail});
1381     Updates.reserve(Updates.size() + 2 * UniqueSuccessorsOfHead.size());
1382     for (BasicBlock *UniqueSuccessorOfHead : UniqueSuccessorsOfHead) {
1383       Updates.push_back({DominatorTree::Insert, Tail, UniqueSuccessorOfHead});
1384       Updates.push_back({DominatorTree::Delete, Head, UniqueSuccessorOfHead});
1385     }
1386   }
1387   Instruction *HeadOldTerm = Head->getTerminator();
1388   LLVMContext &C = Head->getContext();
1389   Instruction *CheckTerm;
1390   bool CreateThenBlock = (ThenBlock == nullptr);
1391   if (CreateThenBlock) {
1392     ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1393     if (Unreachable)
1394       CheckTerm = new UnreachableInst(C, ThenBlock);
1395     else {
1396       CheckTerm = BranchInst::Create(Tail, ThenBlock);
1397       if (DTU)
1398         Updates.push_back({DominatorTree::Insert, ThenBlock, Tail});
1399     }
1400     CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
1401   } else
1402     CheckTerm = ThenBlock->getTerminator();
1403   BranchInst *HeadNewTerm =
1404       BranchInst::Create(/*ifTrue*/ ThenBlock, /*ifFalse*/ Tail, Cond);
1405   if (DTU)
1406     Updates.push_back({DominatorTree::Insert, Head, ThenBlock});
1407   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1408   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1409 
1410   if (DTU)
1411     DTU->applyUpdates(Updates);
1412   else if (DT) {
1413     if (DomTreeNode *OldNode = DT->getNode(Head)) {
1414       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
1415 
1416       DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
1417       for (DomTreeNode *Child : Children)
1418         DT->changeImmediateDominator(Child, NewNode);
1419 
1420       // Head dominates ThenBlock.
1421       if (CreateThenBlock)
1422         DT->addNewBlock(ThenBlock, Head);
1423       else
1424         DT->changeImmediateDominator(ThenBlock, Head);
1425     }
1426   }
1427 
1428   if (LI) {
1429     if (Loop *L = LI->getLoopFor(Head)) {
1430       L->addBasicBlockToLoop(ThenBlock, *LI);
1431       L->addBasicBlockToLoop(Tail, *LI);
1432     }
1433   }
1434 
1435   return CheckTerm;
1436 }
1437 
1438 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
1439                                              Instruction *SplitBefore,
1440                                              bool Unreachable,
1441                                              MDNode *BranchWeights,
1442                                              DominatorTree *DT, LoopInfo *LI,
1443                                              BasicBlock *ThenBlock) {
1444   return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
1445                                        BranchWeights,
1446                                        /*DTU=*/nullptr, DT, LI, ThenBlock);
1447 }
1448 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
1449                                              Instruction *SplitBefore,
1450                                              bool Unreachable,
1451                                              MDNode *BranchWeights,
1452                                              DomTreeUpdater *DTU, LoopInfo *LI,
1453                                              BasicBlock *ThenBlock) {
1454   return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
1455                                        BranchWeights, DTU, /*DT=*/nullptr, LI,
1456                                        ThenBlock);
1457 }
1458 
1459 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
1460                                          Instruction **ThenTerm,
1461                                          Instruction **ElseTerm,
1462                                          MDNode *BranchWeights) {
1463   BasicBlock *Head = SplitBefore->getParent();
1464   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
1465   Instruction *HeadOldTerm = Head->getTerminator();
1466   LLVMContext &C = Head->getContext();
1467   BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1468   BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
1469   *ThenTerm = BranchInst::Create(Tail, ThenBlock);
1470   (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1471   *ElseTerm = BranchInst::Create(Tail, ElseBlock);
1472   (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
1473   BranchInst *HeadNewTerm =
1474     BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
1475   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1476   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1477 }
1478 
1479 BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1480                                  BasicBlock *&IfFalse) {
1481   PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
1482   BasicBlock *Pred1 = nullptr;
1483   BasicBlock *Pred2 = nullptr;
1484 
1485   if (SomePHI) {
1486     if (SomePHI->getNumIncomingValues() != 2)
1487       return nullptr;
1488     Pred1 = SomePHI->getIncomingBlock(0);
1489     Pred2 = SomePHI->getIncomingBlock(1);
1490   } else {
1491     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1492     if (PI == PE) // No predecessor
1493       return nullptr;
1494     Pred1 = *PI++;
1495     if (PI == PE) // Only one predecessor
1496       return nullptr;
1497     Pred2 = *PI++;
1498     if (PI != PE) // More than two predecessors
1499       return nullptr;
1500   }
1501 
1502   // We can only handle branches.  Other control flow will be lowered to
1503   // branches if possible anyway.
1504   BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
1505   BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
1506   if (!Pred1Br || !Pred2Br)
1507     return nullptr;
1508 
1509   // Eliminate code duplication by ensuring that Pred1Br is conditional if
1510   // either are.
1511   if (Pred2Br->isConditional()) {
1512     // If both branches are conditional, we don't have an "if statement".  In
1513     // reality, we could transform this case, but since the condition will be
1514     // required anyway, we stand no chance of eliminating it, so the xform is
1515     // probably not profitable.
1516     if (Pred1Br->isConditional())
1517       return nullptr;
1518 
1519     std::swap(Pred1, Pred2);
1520     std::swap(Pred1Br, Pred2Br);
1521   }
1522 
1523   if (Pred1Br->isConditional()) {
1524     // The only thing we have to watch out for here is to make sure that Pred2
1525     // doesn't have incoming edges from other blocks.  If it does, the condition
1526     // doesn't dominate BB.
1527     if (!Pred2->getSinglePredecessor())
1528       return nullptr;
1529 
1530     // If we found a conditional branch predecessor, make sure that it branches
1531     // to BB and Pred2Br.  If it doesn't, this isn't an "if statement".
1532     if (Pred1Br->getSuccessor(0) == BB &&
1533         Pred1Br->getSuccessor(1) == Pred2) {
1534       IfTrue = Pred1;
1535       IfFalse = Pred2;
1536     } else if (Pred1Br->getSuccessor(0) == Pred2 &&
1537                Pred1Br->getSuccessor(1) == BB) {
1538       IfTrue = Pred2;
1539       IfFalse = Pred1;
1540     } else {
1541       // We know that one arm of the conditional goes to BB, so the other must
1542       // go somewhere unrelated, and this must not be an "if statement".
1543       return nullptr;
1544     }
1545 
1546     return Pred1Br;
1547   }
1548 
1549   // Ok, if we got here, both predecessors end with an unconditional branch to
1550   // BB.  Don't panic!  If both blocks only have a single (identical)
1551   // predecessor, and THAT is a conditional branch, then we're all ok!
1552   BasicBlock *CommonPred = Pred1->getSinglePredecessor();
1553   if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
1554     return nullptr;
1555 
1556   // Otherwise, if this is a conditional branch, then we can use it!
1557   BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
1558   if (!BI) return nullptr;
1559 
1560   assert(BI->isConditional() && "Two successors but not conditional?");
1561   if (BI->getSuccessor(0) == Pred1) {
1562     IfTrue = Pred1;
1563     IfFalse = Pred2;
1564   } else {
1565     IfTrue = Pred2;
1566     IfFalse = Pred1;
1567   }
1568   return BI;
1569 }
1570 
1571 // After creating a control flow hub, the operands of PHINodes in an outgoing
1572 // block Out no longer match the predecessors of that block. Predecessors of Out
1573 // that are incoming blocks to the hub are now replaced by just one edge from
1574 // the hub. To match this new control flow, the corresponding values from each
1575 // PHINode must now be moved a new PHINode in the first guard block of the hub.
1576 //
1577 // This operation cannot be performed with SSAUpdater, because it involves one
1578 // new use: If the block Out is in the list of Incoming blocks, then the newly
1579 // created PHI in the Hub will use itself along that edge from Out to Hub.
1580 static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
1581                           const SetVector<BasicBlock *> &Incoming,
1582                           BasicBlock *FirstGuardBlock) {
1583   auto I = Out->begin();
1584   while (I != Out->end() && isa<PHINode>(I)) {
1585     auto Phi = cast<PHINode>(I);
1586     auto NewPhi =
1587         PHINode::Create(Phi->getType(), Incoming.size(),
1588                         Phi->getName() + ".moved", &FirstGuardBlock->back());
1589     for (auto In : Incoming) {
1590       Value *V = UndefValue::get(Phi->getType());
1591       if (In == Out) {
1592         V = NewPhi;
1593       } else if (Phi->getBasicBlockIndex(In) != -1) {
1594         V = Phi->removeIncomingValue(In, false);
1595       }
1596       NewPhi->addIncoming(V, In);
1597     }
1598     assert(NewPhi->getNumIncomingValues() == Incoming.size());
1599     if (Phi->getNumOperands() == 0) {
1600       Phi->replaceAllUsesWith(NewPhi);
1601       I = Phi->eraseFromParent();
1602       continue;
1603     }
1604     Phi->addIncoming(NewPhi, GuardBlock);
1605     ++I;
1606   }
1607 }
1608 
1609 using BBPredicates = DenseMap<BasicBlock *, PHINode *>;
1610 using BBSetVector = SetVector<BasicBlock *>;
1611 
1612 // Redirects the terminator of the incoming block to the first guard
1613 // block in the hub. The condition of the original terminator (if it
1614 // was conditional) and its original successors are returned as a
1615 // tuple <condition, succ0, succ1>. The function additionally filters
1616 // out successors that are not in the set of outgoing blocks.
1617 //
1618 // - condition is non-null iff the branch is conditional.
1619 // - Succ1 is non-null iff the sole/taken target is an outgoing block.
1620 // - Succ2 is non-null iff condition is non-null and the fallthrough
1621 //         target is an outgoing block.
1622 static std::tuple<Value *, BasicBlock *, BasicBlock *>
1623 redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,
1624               const BBSetVector &Outgoing) {
1625   auto Branch = cast<BranchInst>(BB->getTerminator());
1626   auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;
1627 
1628   BasicBlock *Succ0 = Branch->getSuccessor(0);
1629   BasicBlock *Succ1 = nullptr;
1630   Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;
1631 
1632   if (Branch->isUnconditional()) {
1633     Branch->setSuccessor(0, FirstGuardBlock);
1634     assert(Succ0);
1635   } else {
1636     Succ1 = Branch->getSuccessor(1);
1637     Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
1638     assert(Succ0 || Succ1);
1639     if (Succ0 && !Succ1) {
1640       Branch->setSuccessor(0, FirstGuardBlock);
1641     } else if (Succ1 && !Succ0) {
1642       Branch->setSuccessor(1, FirstGuardBlock);
1643     } else {
1644       Branch->eraseFromParent();
1645       BranchInst::Create(FirstGuardBlock, BB);
1646     }
1647   }
1648 
1649   assert(Succ0 || Succ1);
1650   return std::make_tuple(Condition, Succ0, Succ1);
1651 }
1652 
1653 // Capture the existing control flow as guard predicates, and redirect
1654 // control flow from every incoming block to the first guard block in
1655 // the hub.
1656 //
1657 // There is one guard predicate for each outgoing block OutBB. The
1658 // predicate is a PHINode with one input for each InBB which
1659 // represents whether the hub should transfer control flow to OutBB if
1660 // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub
1661 // evaluates them in the same order as the Outgoing set-vector, and
1662 // control branches to the first outgoing block whose predicate
1663 // evaluates to true.
1664 static void convertToGuardPredicates(
1665     BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates,
1666     SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming,
1667     const BBSetVector &Outgoing) {
1668   auto &Context = Incoming.front()->getContext();
1669   auto BoolTrue = ConstantInt::getTrue(Context);
1670   auto BoolFalse = ConstantInt::getFalse(Context);
1671 
1672   // The predicate for the last outgoing is trivially true, and so we
1673   // process only the first N-1 successors.
1674   for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1675     auto Out = Outgoing[i];
1676     LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");
1677     auto Phi =
1678         PHINode::Create(Type::getInt1Ty(Context), Incoming.size(),
1679                         StringRef("Guard.") + Out->getName(), FirstGuardBlock);
1680     GuardPredicates[Out] = Phi;
1681   }
1682 
1683   for (auto In : Incoming) {
1684     Value *Condition;
1685     BasicBlock *Succ0;
1686     BasicBlock *Succ1;
1687     std::tie(Condition, Succ0, Succ1) =
1688         redirectToHub(In, FirstGuardBlock, Outgoing);
1689 
1690     // Optimization: Consider an incoming block A with both successors
1691     // Succ0 and Succ1 in the set of outgoing blocks. The predicates
1692     // for Succ0 and Succ1 complement each other. If Succ0 is visited
1693     // first in the loop below, control will branch to Succ0 using the
1694     // corresponding predicate. But if that branch is not taken, then
1695     // control must reach Succ1, which means that the predicate for
1696     // Succ1 is always true.
1697     bool OneSuccessorDone = false;
1698     for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
1699       auto Out = Outgoing[i];
1700       auto Phi = GuardPredicates[Out];
1701       if (Out != Succ0 && Out != Succ1) {
1702         Phi->addIncoming(BoolFalse, In);
1703         continue;
1704       }
1705       // Optimization: When only one successor is an outgoing block,
1706       // the predicate is always true.
1707       if (!Succ0 || !Succ1 || OneSuccessorDone) {
1708         Phi->addIncoming(BoolTrue, In);
1709         continue;
1710       }
1711       assert(Succ0 && Succ1);
1712       OneSuccessorDone = true;
1713       if (Out == Succ0) {
1714         Phi->addIncoming(Condition, In);
1715         continue;
1716       }
1717       auto Inverted = invertCondition(Condition);
1718       DeletionCandidates.push_back(Condition);
1719       Phi->addIncoming(Inverted, In);
1720     }
1721   }
1722 }
1723 
1724 // For each outgoing block OutBB, create a guard block in the Hub. The
1725 // first guard block was already created outside, and available as the
1726 // first element in the vector of guard blocks.
1727 //
1728 // Each guard block terminates in a conditional branch that transfers
1729 // control to the corresponding outgoing block or the next guard
1730 // block. The last guard block has two outgoing blocks as successors
1731 // since the condition for the final outgoing block is trivially
1732 // true. So we create one less block (including the first guard block)
1733 // than the number of outgoing blocks.
1734 static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks,
1735                               Function *F, const BBSetVector &Outgoing,
1736                               BBPredicates &GuardPredicates, StringRef Prefix) {
1737   for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) {
1738     GuardBlocks.push_back(
1739         BasicBlock::Create(F->getContext(), Prefix + ".guard", F));
1740   }
1741   assert(GuardBlocks.size() == GuardPredicates.size());
1742 
1743   // To help keep the loop simple, temporarily append the last
1744   // outgoing block to the list of guard blocks.
1745   GuardBlocks.push_back(Outgoing.back());
1746 
1747   for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {
1748     auto Out = Outgoing[i];
1749     assert(GuardPredicates.count(Out));
1750     BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],
1751                        GuardBlocks[i]);
1752   }
1753 
1754   // Remove the last block from the guard list.
1755   GuardBlocks.pop_back();
1756 }
1757 
1758 BasicBlock *llvm::CreateControlFlowHub(
1759     DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
1760     const BBSetVector &Incoming, const BBSetVector &Outgoing,
1761     const StringRef Prefix) {
1762   auto F = Incoming.front()->getParent();
1763   auto FirstGuardBlock =
1764       BasicBlock::Create(F->getContext(), Prefix + ".guard", F);
1765 
1766   SmallVector<DominatorTree::UpdateType, 16> Updates;
1767   if (DTU) {
1768     for (auto In : Incoming) {
1769       Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});
1770       for (auto Succ : successors(In)) {
1771         if (Outgoing.count(Succ))
1772           Updates.push_back({DominatorTree::Delete, In, Succ});
1773       }
1774     }
1775   }
1776 
1777   BBPredicates GuardPredicates;
1778   SmallVector<WeakVH, 8> DeletionCandidates;
1779   convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates,
1780                            Incoming, Outgoing);
1781 
1782   GuardBlocks.push_back(FirstGuardBlock);
1783   createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix);
1784 
1785   // Update the PHINodes in each outgoing block to match the new control flow.
1786   for (int i = 0, e = GuardBlocks.size(); i != e; ++i) {
1787     reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);
1788   }
1789   reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);
1790 
1791   if (DTU) {
1792     int NumGuards = GuardBlocks.size();
1793     assert((int)Outgoing.size() == NumGuards + 1);
1794     for (int i = 0; i != NumGuards - 1; ++i) {
1795       Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});
1796       Updates.push_back(
1797           {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});
1798     }
1799     Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
1800                        Outgoing[NumGuards - 1]});
1801     Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
1802                        Outgoing[NumGuards]});
1803     DTU->applyUpdates(Updates);
1804   }
1805 
1806   for (auto I : DeletionCandidates) {
1807     if (I->use_empty())
1808       if (auto Inst = dyn_cast_or_null<Instruction>(I))
1809         Inst->eraseFromParent();
1810   }
1811 
1812   return FirstGuardBlock;
1813 }
1814