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