xref: /llvm-project/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp (revision 3f37c517fbc40531571f8b9f951a8610b4789cd6)
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   return true;
337 }
338 
339 bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
340     SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,
341     LoopInfo *LI) {
342   assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
343 
344   bool BlocksHaveBeenMerged = false;
345   while (!MergeBlocks.empty()) {
346     BasicBlock *BB = *MergeBlocks.begin();
347     BasicBlock *Dest = BB->getSingleSuccessor();
348     if (Dest && (!L || L->contains(Dest))) {
349       BasicBlock *Fold = Dest->getUniquePredecessor();
350       (void)Fold;
351       if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {
352         assert(Fold == BB &&
353                "Expecting BB to be unique predecessor of the Dest block");
354         MergeBlocks.erase(Dest);
355         BlocksHaveBeenMerged = true;
356       } else
357         MergeBlocks.erase(BB);
358     } else
359       MergeBlocks.erase(BB);
360   }
361   return BlocksHaveBeenMerged;
362 }
363 
364 /// Remove redundant instructions within sequences of consecutive dbg.value
365 /// instructions. This is done using a backward scan to keep the last dbg.value
366 /// describing a specific variable/fragment.
367 ///
368 /// BackwardScan strategy:
369 /// ----------------------
370 /// Given a sequence of consecutive DbgValueInst like this
371 ///
372 ///   dbg.value ..., "x", FragmentX1  (*)
373 ///   dbg.value ..., "y", FragmentY1
374 ///   dbg.value ..., "x", FragmentX2
375 ///   dbg.value ..., "x", FragmentX1  (**)
376 ///
377 /// then the instruction marked with (*) can be removed (it is guaranteed to be
378 /// obsoleted by the instruction marked with (**) as the latter instruction is
379 /// describing the same variable using the same fragment info).
380 ///
381 /// Possible improvements:
382 /// - Check fully overlapping fragments and not only identical fragments.
383 /// - Support dbg.declare. dbg.label, and possibly other meta instructions being
384 ///   part of the sequence of consecutive instructions.
385 static bool
386 DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
387   SmallVector<DbgVariableRecord *, 8> ToBeRemoved;
388   SmallDenseSet<DebugVariable> VariableSet;
389   for (auto &I : reverse(*BB)) {
390     for (DbgRecord &DR : reverse(I.getDbgRecordRange())) {
391       if (isa<DbgLabelRecord>(DR)) {
392         // Emulate existing behaviour (see comment below for dbg.declares).
393         // FIXME: Don't do this.
394         VariableSet.clear();
395         continue;
396       }
397 
398       DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
399       // Skip declare-type records, as the debug intrinsic method only works
400       // on dbg.value intrinsics.
401       if (DVR.getType() == DbgVariableRecord::LocationType::Declare) {
402         // The debug intrinsic method treats dbg.declares are "non-debug"
403         // instructions (i.e., a break in a consecutive range of debug
404         // intrinsics). Emulate that to create identical outputs. See
405         // "Possible improvements" above.
406         // FIXME: Delete the line below.
407         VariableSet.clear();
408         continue;
409       }
410 
411       DebugVariable Key(DVR.getVariable(), DVR.getExpression(),
412                         DVR.getDebugLoc()->getInlinedAt());
413       auto R = VariableSet.insert(Key);
414       // If the same variable fragment is described more than once it is enough
415       // to keep the last one (i.e. the first found since we for reverse
416       // iteration).
417       if (R.second)
418         continue;
419 
420       if (DVR.isDbgAssign()) {
421         // Don't delete dbg.assign intrinsics that are linked to instructions.
422         if (!at::getAssignmentInsts(&DVR).empty())
423           continue;
424         // Unlinked dbg.assign intrinsics can be treated like dbg.values.
425       }
426 
427       ToBeRemoved.push_back(&DVR);
428       continue;
429     }
430     // Sequence with consecutive dbg.value instrs ended. Clear the map to
431     // restart identifying redundant instructions if case we find another
432     // dbg.value sequence.
433     VariableSet.clear();
434   }
435 
436   for (auto &DVR : ToBeRemoved)
437     DVR->eraseFromParent();
438 
439   return !ToBeRemoved.empty();
440 }
441 
442 static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
443   if (BB->IsNewDbgInfoFormat)
444     return DbgVariableRecordsRemoveRedundantDbgInstrsUsingBackwardScan(BB);
445 
446   SmallVector<DbgValueInst *, 8> ToBeRemoved;
447   SmallDenseSet<DebugVariable> VariableSet;
448   for (auto &I : reverse(*BB)) {
449     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
450       DebugVariable Key(DVI->getVariable(),
451                         DVI->getExpression(),
452                         DVI->getDebugLoc()->getInlinedAt());
453       auto R = VariableSet.insert(Key);
454       // If the variable fragment hasn't been seen before then we don't want
455       // to remove this dbg intrinsic.
456       if (R.second)
457         continue;
458 
459       if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI)) {
460         // Don't delete dbg.assign intrinsics that are linked to instructions.
461         if (!at::getAssignmentInsts(DAI).empty())
462           continue;
463         // Unlinked dbg.assign intrinsics can be treated like dbg.values.
464       }
465 
466       // If the same variable fragment is described more than once it is enough
467       // to keep the last one (i.e. the first found since we for reverse
468       // iteration).
469       ToBeRemoved.push_back(DVI);
470       continue;
471     }
472     // Sequence with consecutive dbg.value instrs ended. Clear the map to
473     // restart identifying redundant instructions if case we find another
474     // dbg.value sequence.
475     VariableSet.clear();
476   }
477 
478   for (auto &Instr : ToBeRemoved)
479     Instr->eraseFromParent();
480 
481   return !ToBeRemoved.empty();
482 }
483 
484 /// Remove redundant dbg.value instructions using a forward scan. This can
485 /// remove a dbg.value instruction that is redundant due to indicating that a
486 /// variable has the same value as already being indicated by an earlier
487 /// dbg.value.
488 ///
489 /// ForwardScan strategy:
490 /// ---------------------
491 /// Given two identical dbg.value instructions, separated by a block of
492 /// instructions that isn't describing the same variable, like this
493 ///
494 ///   dbg.value X1, "x", FragmentX1  (**)
495 ///   <block of instructions, none being "dbg.value ..., "x", ...">
496 ///   dbg.value X1, "x", FragmentX1  (*)
497 ///
498 /// then the instruction marked with (*) can be removed. Variable "x" is already
499 /// described as being mapped to the SSA value X1.
500 ///
501 /// Possible improvements:
502 /// - Keep track of non-overlapping fragments.
503 static bool
504 DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
505   SmallVector<DbgVariableRecord *, 8> ToBeRemoved;
506   SmallDenseMap<DebugVariable,
507                 std::pair<SmallVector<Value *, 4>, DIExpression *>, 4>
508       VariableMap;
509   for (auto &I : *BB) {
510     for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
511       if (DVR.getType() == DbgVariableRecord::LocationType::Declare)
512         continue;
513       DebugVariable Key(DVR.getVariable(), std::nullopt,
514                         DVR.getDebugLoc()->getInlinedAt());
515       auto VMI = VariableMap.find(Key);
516       // A dbg.assign with no linked instructions can be treated like a
517       // dbg.value (i.e. can be deleted).
518       bool IsDbgValueKind =
519           (!DVR.isDbgAssign() || at::getAssignmentInsts(&DVR).empty());
520 
521       // Update the map if we found a new value/expression describing the
522       // variable, or if the variable wasn't mapped already.
523       SmallVector<Value *, 4> Values(DVR.location_ops());
524       if (VMI == VariableMap.end() || VMI->second.first != Values ||
525           VMI->second.second != DVR.getExpression()) {
526         if (IsDbgValueKind)
527           VariableMap[Key] = {Values, DVR.getExpression()};
528         else
529           VariableMap[Key] = {Values, nullptr};
530         continue;
531       }
532       // Don't delete dbg.assign intrinsics that are linked to instructions.
533       if (!IsDbgValueKind)
534         continue;
535       // Found an identical mapping. Remember the instruction for later removal.
536       ToBeRemoved.push_back(&DVR);
537     }
538   }
539 
540   for (auto *DVR : ToBeRemoved)
541     DVR->eraseFromParent();
542 
543   return !ToBeRemoved.empty();
544 }
545 
546 static bool
547 DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock(BasicBlock *BB) {
548   assert(BB->isEntryBlock() && "expected entry block");
549   SmallVector<DbgVariableRecord *, 8> ToBeRemoved;
550   DenseSet<DebugVariable> SeenDefForAggregate;
551   // Returns the DebugVariable for DVI with no fragment info.
552   auto GetAggregateVariable = [](const DbgVariableRecord &DVR) {
553     return DebugVariable(DVR.getVariable(), std::nullopt,
554                          DVR.getDebugLoc().getInlinedAt());
555   };
556 
557   // Remove undef dbg.assign intrinsics that are encountered before
558   // any non-undef intrinsics from the entry block.
559   for (auto &I : *BB) {
560     for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
561       if (!DVR.isDbgValue() && !DVR.isDbgAssign())
562         continue;
563       bool IsDbgValueKind =
564           (DVR.isDbgValue() || at::getAssignmentInsts(&DVR).empty());
565       DebugVariable Aggregate = GetAggregateVariable(DVR);
566       if (!SeenDefForAggregate.contains(Aggregate)) {
567         bool IsKill = DVR.isKillLocation() && IsDbgValueKind;
568         if (!IsKill) {
569           SeenDefForAggregate.insert(Aggregate);
570         } else if (DVR.isDbgAssign()) {
571           ToBeRemoved.push_back(&DVR);
572         }
573       }
574     }
575   }
576 
577   for (DbgVariableRecord *DVR : ToBeRemoved)
578     DVR->eraseFromParent();
579 
580   return !ToBeRemoved.empty();
581 }
582 
583 static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
584   if (BB->IsNewDbgInfoFormat)
585     return DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BB);
586 
587   SmallVector<DbgValueInst *, 8> ToBeRemoved;
588   SmallDenseMap<DebugVariable,
589                 std::pair<SmallVector<Value *, 4>, DIExpression *>, 4>
590       VariableMap;
591   for (auto &I : *BB) {
592     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
593       DebugVariable Key(DVI->getVariable(), std::nullopt,
594                         DVI->getDebugLoc()->getInlinedAt());
595       auto VMI = VariableMap.find(Key);
596       auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
597       // A dbg.assign with no linked instructions can be treated like a
598       // dbg.value (i.e. can be deleted).
599       bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());
600 
601       // Update the map if we found a new value/expression describing the
602       // variable, or if the variable wasn't mapped already.
603       SmallVector<Value *, 4> Values(DVI->getValues());
604       if (VMI == VariableMap.end() || VMI->second.first != Values ||
605           VMI->second.second != DVI->getExpression()) {
606         // Use a sentinel value (nullptr) for the DIExpression when we see a
607         // linked dbg.assign so that the next debug intrinsic will never match
608         // it (i.e. always treat linked dbg.assigns as if they're unique).
609         if (IsDbgValueKind)
610           VariableMap[Key] = {Values, DVI->getExpression()};
611         else
612           VariableMap[Key] = {Values, nullptr};
613         continue;
614       }
615 
616       // Don't delete dbg.assign intrinsics that are linked to instructions.
617       if (!IsDbgValueKind)
618         continue;
619       ToBeRemoved.push_back(DVI);
620     }
621   }
622 
623   for (auto &Instr : ToBeRemoved)
624     Instr->eraseFromParent();
625 
626   return !ToBeRemoved.empty();
627 }
628 
629 /// Remove redundant undef dbg.assign intrinsic from an entry block using a
630 /// forward scan.
631 /// Strategy:
632 /// ---------------------
633 /// Scanning forward, delete dbg.assign intrinsics iff they are undef, not
634 /// linked to an intrinsic, and don't share an aggregate variable with a debug
635 /// intrinsic that didn't meet the criteria. In other words, undef dbg.assigns
636 /// that come before non-undef debug intrinsics for the variable are
637 /// deleted. Given:
638 ///
639 ///   dbg.assign undef, "x", FragmentX1 (*)
640 ///   <block of instructions, none being "dbg.value ..., "x", ...">
641 ///   dbg.value %V, "x", FragmentX2
642 ///   <block of instructions, none being "dbg.value ..., "x", ...">
643 ///   dbg.assign undef, "x", FragmentX1
644 ///
645 /// then (only) the instruction marked with (*) can be removed.
646 /// Possible improvements:
647 /// - Keep track of non-overlapping fragments.
648 static bool removeUndefDbgAssignsFromEntryBlock(BasicBlock *BB) {
649   if (BB->IsNewDbgInfoFormat)
650     return DbgVariableRecordsRemoveUndefDbgAssignsFromEntryBlock(BB);
651 
652   assert(BB->isEntryBlock() && "expected entry block");
653   SmallVector<DbgAssignIntrinsic *, 8> ToBeRemoved;
654   DenseSet<DebugVariable> SeenDefForAggregate;
655   // Returns the DebugVariable for DVI with no fragment info.
656   auto GetAggregateVariable = [](DbgValueInst *DVI) {
657     return DebugVariable(DVI->getVariable(), std::nullopt,
658                          DVI->getDebugLoc()->getInlinedAt());
659   };
660 
661   // Remove undef dbg.assign intrinsics that are encountered before
662   // any non-undef intrinsics from the entry block.
663   for (auto &I : *BB) {
664     DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I);
665     if (!DVI)
666       continue;
667     auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
668     bool IsDbgValueKind = (!DAI || at::getAssignmentInsts(DAI).empty());
669     DebugVariable Aggregate = GetAggregateVariable(DVI);
670     if (!SeenDefForAggregate.contains(Aggregate)) {
671       bool IsKill = DVI->isKillLocation() && IsDbgValueKind;
672       if (!IsKill) {
673         SeenDefForAggregate.insert(Aggregate);
674       } else if (DAI) {
675         ToBeRemoved.push_back(DAI);
676       }
677     }
678   }
679 
680   for (DbgAssignIntrinsic *DAI : ToBeRemoved)
681     DAI->eraseFromParent();
682 
683   return !ToBeRemoved.empty();
684 }
685 
686 bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
687   bool MadeChanges = false;
688   // By using the "backward scan" strategy before the "forward scan" strategy we
689   // can remove both dbg.value (2) and (3) in a situation like this:
690   //
691   //   (1) dbg.value V1, "x", DIExpression()
692   //       ...
693   //   (2) dbg.value V2, "x", DIExpression()
694   //   (3) dbg.value V1, "x", DIExpression()
695   //
696   // The backward scan will remove (2), it is made obsolete by (3). After
697   // getting (2) out of the way, the foward scan will remove (3) since "x"
698   // already is described as having the value V1 at (1).
699   MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);
700   if (BB->isEntryBlock() &&
701       isAssignmentTrackingEnabled(*BB->getParent()->getParent()))
702     MadeChanges |= removeUndefDbgAssignsFromEntryBlock(BB);
703   MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);
704 
705   if (MadeChanges)
706     LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
707                       << BB->getName() << "\n");
708   return MadeChanges;
709 }
710 
711 void llvm::ReplaceInstWithValue(BasicBlock::iterator &BI, Value *V) {
712   Instruction &I = *BI;
713   // Replaces all of the uses of the instruction with uses of the value
714   I.replaceAllUsesWith(V);
715 
716   // Make sure to propagate a name if there is one already.
717   if (I.hasName() && !V->hasName())
718     V->takeName(&I);
719 
720   // Delete the unnecessary instruction now...
721   BI = BI->eraseFromParent();
722 }
723 
724 void llvm::ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI,
725                                Instruction *I) {
726   assert(I->getParent() == nullptr &&
727          "ReplaceInstWithInst: Instruction already inserted into basic block!");
728 
729   // Copy debug location to newly added instruction, if it wasn't already set
730   // by the caller.
731   if (!I->getDebugLoc())
732     I->setDebugLoc(BI->getDebugLoc());
733 
734   // Insert the new instruction into the basic block...
735   BasicBlock::iterator New = I->insertInto(BB, BI);
736 
737   // Replace all uses of the old instruction, and delete it.
738   ReplaceInstWithValue(BI, I);
739 
740   // Move BI back to point to the newly inserted instruction
741   BI = New;
742 }
743 
744 bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {
745   // Remember visited blocks to avoid infinite loop
746   SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
747   unsigned Depth = 0;
748   while (BB && Depth++ < MaxDeoptOrUnreachableSuccessorCheckDepth &&
749          VisitedBlocks.insert(BB).second) {
750     if (isa<UnreachableInst>(BB->getTerminator()) ||
751         BB->getTerminatingDeoptimizeCall())
752       return true;
753     BB = BB->getUniqueSuccessor();
754   }
755   return false;
756 }
757 
758 void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
759   BasicBlock::iterator BI(From);
760   ReplaceInstWithInst(From->getParent(), BI, To);
761 }
762 
763 BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
764                             LoopInfo *LI, MemorySSAUpdater *MSSAU,
765                             const Twine &BBName) {
766   unsigned SuccNum = GetSuccessorNumber(BB, Succ);
767 
768   Instruction *LatchTerm = BB->getTerminator();
769 
770   CriticalEdgeSplittingOptions Options =
771       CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA();
772 
773   if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) {
774     // If it is a critical edge, and the succesor is an exception block, handle
775     // the split edge logic in this specific function
776     if (Succ->isEHPad())
777       return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName);
778 
779     // If this is a critical edge, let SplitKnownCriticalEdge do it.
780     return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);
781   }
782 
783   // If the edge isn't critical, then BB has a single successor or Succ has a
784   // single pred.  Split the block.
785   if (BasicBlock *SP = Succ->getSinglePredecessor()) {
786     // If the successor only has a single pred, split the top of the successor
787     // block.
788     assert(SP == BB && "CFG broken");
789     (void)SP;
790     return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName,
791                       /*Before=*/true);
792   }
793 
794   // Otherwise, if BB has a single successor, split it at the bottom of the
795   // block.
796   assert(BB->getTerminator()->getNumSuccessors() == 1 &&
797          "Should have a single succ!");
798   return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName);
799 }
800 
801 void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) {
802   if (auto *II = dyn_cast<InvokeInst>(TI))
803     II->setUnwindDest(Succ);
804   else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
805     CS->setUnwindDest(Succ);
806   else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
807     CR->setUnwindDest(Succ);
808   else
809     llvm_unreachable("unexpected terminator instruction");
810 }
811 
812 void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
813                           BasicBlock *NewPred, PHINode *Until) {
814   int BBIdx = 0;
815   for (PHINode &PN : DestBB->phis()) {
816     // We manually update the LandingPadReplacement PHINode and it is the last
817     // PHI Node. So, if we find it, we are done.
818     if (Until == &PN)
819       break;
820 
821     // Reuse the previous value of BBIdx if it lines up.  In cases where we
822     // have multiple phi nodes with *lots* of predecessors, this is a speed
823     // win because we don't have to scan the PHI looking for TIBB.  This
824     // happens because the BB list of PHI nodes are usually in the same
825     // order.
826     if (PN.getIncomingBlock(BBIdx) != OldPred)
827       BBIdx = PN.getBasicBlockIndex(OldPred);
828 
829     assert(BBIdx != -1 && "Invalid PHI Index!");
830     PN.setIncomingBlock(BBIdx, NewPred);
831   }
832 }
833 
834 BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
835                                    LandingPadInst *OriginalPad,
836                                    PHINode *LandingPadReplacement,
837                                    const CriticalEdgeSplittingOptions &Options,
838                                    const Twine &BBName) {
839 
840   auto *PadInst = Succ->getFirstNonPHI();
841   if (!LandingPadReplacement && !PadInst->isEHPad())
842     return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);
843 
844   auto *LI = Options.LI;
845   SmallVector<BasicBlock *, 4> LoopPreds;
846   // Check if extra modifications will be required to preserve loop-simplify
847   // form after splitting. If it would require splitting blocks with IndirectBr
848   // terminators, bail out if preserving loop-simplify form is requested.
849   if (Options.PreserveLoopSimplify && LI) {
850     if (Loop *BBLoop = LI->getLoopFor(BB)) {
851 
852       // The only way that we can break LoopSimplify form by splitting a
853       // critical edge is when there exists some edge from BBLoop to Succ *and*
854       // the only edge into Succ from outside of BBLoop is that of NewBB after
855       // the split. If the first isn't true, then LoopSimplify still holds,
856       // NewBB is the new exit block and it has no non-loop predecessors. If the
857       // second isn't true, then Succ was not in LoopSimplify form prior to
858       // the split as it had a non-loop predecessor. In both of these cases,
859       // the predecessor must be directly in BBLoop, not in a subloop, or again
860       // LoopSimplify doesn't hold.
861       for (BasicBlock *P : predecessors(Succ)) {
862         if (P == BB)
863           continue; // The new block is known.
864         if (LI->getLoopFor(P) != BBLoop) {
865           // Loop is not in LoopSimplify form, no need to re simplify after
866           // splitting edge.
867           LoopPreds.clear();
868           break;
869         }
870         LoopPreds.push_back(P);
871       }
872       // Loop-simplify form can be preserved, if we can split all in-loop
873       // predecessors.
874       if (any_of(LoopPreds, [](BasicBlock *Pred) {
875             return isa<IndirectBrInst>(Pred->getTerminator());
876           })) {
877         return nullptr;
878       }
879     }
880   }
881 
882   auto *NewBB =
883       BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);
884   setUnwindEdgeTo(BB->getTerminator(), NewBB);
885   updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
886 
887   if (LandingPadReplacement) {
888     auto *NewLP = OriginalPad->clone();
889     auto *Terminator = BranchInst::Create(Succ, NewBB);
890     NewLP->insertBefore(Terminator);
891     LandingPadReplacement->addIncoming(NewLP, NewBB);
892   } else {
893     Value *ParentPad = nullptr;
894     if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
895       ParentPad = FuncletPad->getParentPad();
896     else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
897       ParentPad = CatchSwitch->getParentPad();
898     else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))
899       ParentPad = CleanupPad->getParentPad();
900     else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))
901       ParentPad = LandingPad->getParent();
902     else
903       llvm_unreachable("handling for other EHPads not implemented yet");
904 
905     auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);
906     CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
907   }
908 
909   auto *DT = Options.DT;
910   auto *MSSAU = Options.MSSAU;
911   if (!DT && !LI)
912     return NewBB;
913 
914   if (DT) {
915     DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
916     SmallVector<DominatorTree::UpdateType, 3> Updates;
917 
918     Updates.push_back({DominatorTree::Insert, BB, NewBB});
919     Updates.push_back({DominatorTree::Insert, NewBB, Succ});
920     Updates.push_back({DominatorTree::Delete, BB, Succ});
921 
922     DTU.applyUpdates(Updates);
923     DTU.flush();
924 
925     if (MSSAU) {
926       MSSAU->applyUpdates(Updates, *DT);
927       if (VerifyMemorySSA)
928         MSSAU->getMemorySSA()->verifyMemorySSA();
929     }
930   }
931 
932   if (LI) {
933     if (Loop *BBLoop = LI->getLoopFor(BB)) {
934       // If one or the other blocks were not in a loop, the new block is not
935       // either, and thus LI doesn't need to be updated.
936       if (Loop *SuccLoop = LI->getLoopFor(Succ)) {
937         if (BBLoop == SuccLoop) {
938           // Both in the same loop, the NewBB joins loop.
939           SuccLoop->addBasicBlockToLoop(NewBB, *LI);
940         } else if (BBLoop->contains(SuccLoop)) {
941           // Edge from an outer loop to an inner loop.  Add to the outer loop.
942           BBLoop->addBasicBlockToLoop(NewBB, *LI);
943         } else if (SuccLoop->contains(BBLoop)) {
944           // Edge from an inner loop to an outer loop.  Add to the outer loop.
945           SuccLoop->addBasicBlockToLoop(NewBB, *LI);
946         } else {
947           // Edge from two loops with no containment relation.  Because these
948           // are natural loops, we know that the destination block must be the
949           // header of its loop (adding a branch into a loop elsewhere would
950           // create an irreducible loop).
951           assert(SuccLoop->getHeader() == Succ &&
952                  "Should not create irreducible loops!");
953           if (Loop *P = SuccLoop->getParentLoop())
954             P->addBasicBlockToLoop(NewBB, *LI);
955         }
956       }
957 
958       // If BB is in a loop and Succ is outside of that loop, we may need to
959       // update LoopSimplify form and LCSSA form.
960       if (!BBLoop->contains(Succ)) {
961         assert(!BBLoop->contains(NewBB) &&
962                "Split point for loop exit is contained in loop!");
963 
964         // Update LCSSA form in the newly created exit block.
965         if (Options.PreserveLCSSA) {
966           createPHIsForSplitLoopExit(BB, NewBB, Succ);
967         }
968 
969         if (!LoopPreds.empty()) {
970           BasicBlock *NewExitBB = SplitBlockPredecessors(
971               Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
972           if (Options.PreserveLCSSA)
973             createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);
974         }
975       }
976     }
977   }
978 
979   return NewBB;
980 }
981 
982 void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
983                                       BasicBlock *SplitBB, BasicBlock *DestBB) {
984   // SplitBB shouldn't have anything non-trivial in it yet.
985   assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
986           SplitBB->isLandingPad()) &&
987          "SplitBB has non-PHI nodes!");
988 
989   // For each PHI in the destination block.
990   for (PHINode &PN : DestBB->phis()) {
991     int Idx = PN.getBasicBlockIndex(SplitBB);
992     assert(Idx >= 0 && "Invalid Block Index");
993     Value *V = PN.getIncomingValue(Idx);
994 
995     // If the input is a PHI which already satisfies LCSSA, don't create
996     // a new one.
997     if (const PHINode *VP = dyn_cast<PHINode>(V))
998       if (VP->getParent() == SplitBB)
999         continue;
1000 
1001     // Otherwise a new PHI is needed. Create one and populate it.
1002     PHINode *NewPN = PHINode::Create(PN.getType(), Preds.size(), "split");
1003     BasicBlock::iterator InsertPos =
1004         SplitBB->isLandingPad() ? SplitBB->begin()
1005                                 : SplitBB->getTerminator()->getIterator();
1006     NewPN->insertBefore(InsertPos);
1007     for (BasicBlock *BB : Preds)
1008       NewPN->addIncoming(V, BB);
1009 
1010     // Update the original PHI.
1011     PN.setIncomingValue(Idx, NewPN);
1012   }
1013 }
1014 
1015 unsigned
1016 llvm::SplitAllCriticalEdges(Function &F,
1017                             const CriticalEdgeSplittingOptions &Options) {
1018   unsigned NumBroken = 0;
1019   for (BasicBlock &BB : F) {
1020     Instruction *TI = BB.getTerminator();
1021     if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI))
1022       for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
1023         if (SplitCriticalEdge(TI, i, Options))
1024           ++NumBroken;
1025   }
1026   return NumBroken;
1027 }
1028 
1029 static BasicBlock *SplitBlockImpl(BasicBlock *Old, BasicBlock::iterator SplitPt,
1030                                   DomTreeUpdater *DTU, DominatorTree *DT,
1031                                   LoopInfo *LI, MemorySSAUpdater *MSSAU,
1032                                   const Twine &BBName, bool Before) {
1033   if (Before) {
1034     DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
1035     return splitBlockBefore(Old, SplitPt,
1036                             DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
1037                             BBName);
1038   }
1039   BasicBlock::iterator SplitIt = SplitPt;
1040   while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
1041     ++SplitIt;
1042     assert(SplitIt != SplitPt->getParent()->end());
1043   }
1044   std::string Name = BBName.str();
1045   BasicBlock *New = Old->splitBasicBlock(
1046       SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
1047 
1048   // The new block lives in whichever loop the old one did. This preserves
1049   // LCSSA as well, because we force the split point to be after any PHI nodes.
1050   if (LI)
1051     if (Loop *L = LI->getLoopFor(Old))
1052       L->addBasicBlockToLoop(New, *LI);
1053 
1054   if (DTU) {
1055     SmallVector<DominatorTree::UpdateType, 8> Updates;
1056     // Old dominates New. New node dominates all other nodes dominated by Old.
1057     SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld;
1058     Updates.push_back({DominatorTree::Insert, Old, New});
1059     Updates.reserve(Updates.size() + 2 * succ_size(New));
1060     for (BasicBlock *SuccessorOfOld : successors(New))
1061       if (UniqueSuccessorsOfOld.insert(SuccessorOfOld).second) {
1062         Updates.push_back({DominatorTree::Insert, New, SuccessorOfOld});
1063         Updates.push_back({DominatorTree::Delete, Old, SuccessorOfOld});
1064       }
1065 
1066     DTU->applyUpdates(Updates);
1067   } else if (DT)
1068     // Old dominates New. New node dominates all other nodes dominated by Old.
1069     if (DomTreeNode *OldNode = DT->getNode(Old)) {
1070       std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
1071 
1072       DomTreeNode *NewNode = DT->addNewBlock(New, Old);
1073       for (DomTreeNode *I : Children)
1074         DT->changeImmediateDominator(I, NewNode);
1075     }
1076 
1077   // Move MemoryAccesses still tracked in Old, but part of New now.
1078   // Update accesses in successor blocks accordingly.
1079   if (MSSAU)
1080     MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
1081 
1082   return New;
1083 }
1084 
1085 BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
1086                              DominatorTree *DT, LoopInfo *LI,
1087                              MemorySSAUpdater *MSSAU, const Twine &BBName,
1088                              bool Before) {
1089   return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
1090                         Before);
1091 }
1092 BasicBlock *llvm::SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt,
1093                              DomTreeUpdater *DTU, LoopInfo *LI,
1094                              MemorySSAUpdater *MSSAU, const Twine &BBName,
1095                              bool Before) {
1096   return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
1097                         Before);
1098 }
1099 
1100 BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt,
1101                                    DomTreeUpdater *DTU, LoopInfo *LI,
1102                                    MemorySSAUpdater *MSSAU,
1103                                    const Twine &BBName) {
1104 
1105   BasicBlock::iterator SplitIt = SplitPt;
1106   while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
1107     ++SplitIt;
1108   std::string Name = BBName.str();
1109   BasicBlock *New = Old->splitBasicBlock(
1110       SplitIt, Name.empty() ? Old->getName() + ".split" : Name,
1111       /* Before=*/true);
1112 
1113   // The new block lives in whichever loop the old one did. This preserves
1114   // LCSSA as well, because we force the split point to be after any PHI nodes.
1115   if (LI)
1116     if (Loop *L = LI->getLoopFor(Old))
1117       L->addBasicBlockToLoop(New, *LI);
1118 
1119   if (DTU) {
1120     SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
1121     // New dominates Old. The predecessor nodes of the Old node dominate
1122     // New node.
1123     SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld;
1124     DTUpdates.push_back({DominatorTree::Insert, New, Old});
1125     DTUpdates.reserve(DTUpdates.size() + 2 * pred_size(New));
1126     for (BasicBlock *PredecessorOfOld : predecessors(New))
1127       if (UniquePredecessorsOfOld.insert(PredecessorOfOld).second) {
1128         DTUpdates.push_back({DominatorTree::Insert, PredecessorOfOld, New});
1129         DTUpdates.push_back({DominatorTree::Delete, PredecessorOfOld, Old});
1130       }
1131 
1132     DTU->applyUpdates(DTUpdates);
1133 
1134     // Move MemoryAccesses still tracked in Old, but part of New now.
1135     // Update accesses in successor blocks accordingly.
1136     if (MSSAU) {
1137       MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
1138       if (VerifyMemorySSA)
1139         MSSAU->getMemorySSA()->verifyMemorySSA();
1140     }
1141   }
1142   return New;
1143 }
1144 
1145 /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
1146 /// Invalidates DFS Numbering when DTU or DT is provided.
1147 static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
1148                                       ArrayRef<BasicBlock *> Preds,
1149                                       DomTreeUpdater *DTU, DominatorTree *DT,
1150                                       LoopInfo *LI, MemorySSAUpdater *MSSAU,
1151                                       bool PreserveLCSSA, bool &HasLoopExit) {
1152   // Update dominator tree if available.
1153   if (DTU) {
1154     // Recalculation of DomTree is needed when updating a forward DomTree and
1155     // the Entry BB is replaced.
1156     if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
1157       // The entry block was removed and there is no external interface for
1158       // the dominator tree to be notified of this change. In this corner-case
1159       // we recalculate the entire tree.
1160       DTU->recalculate(*NewBB->getParent());
1161     } else {
1162       // Split block expects NewBB to have a non-empty set of predecessors.
1163       SmallVector<DominatorTree::UpdateType, 8> Updates;
1164       SmallPtrSet<BasicBlock *, 8> UniquePreds;
1165       Updates.push_back({DominatorTree::Insert, NewBB, OldBB});
1166       Updates.reserve(Updates.size() + 2 * Preds.size());
1167       for (auto *Pred : Preds)
1168         if (UniquePreds.insert(Pred).second) {
1169           Updates.push_back({DominatorTree::Insert, Pred, NewBB});
1170           Updates.push_back({DominatorTree::Delete, Pred, OldBB});
1171         }
1172       DTU->applyUpdates(Updates);
1173     }
1174   } else if (DT) {
1175     if (OldBB == DT->getRootNode()->getBlock()) {
1176       assert(NewBB->isEntryBlock());
1177       DT->setNewRoot(NewBB);
1178     } else {
1179       // Split block expects NewBB to have a non-empty set of predecessors.
1180       DT->splitBlock(NewBB);
1181     }
1182   }
1183 
1184   // Update MemoryPhis after split if MemorySSA is available
1185   if (MSSAU)
1186     MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
1187 
1188   // The rest of the logic is only relevant for updating the loop structures.
1189   if (!LI)
1190     return;
1191 
1192   if (DTU && DTU->hasDomTree())
1193     DT = &DTU->getDomTree();
1194   assert(DT && "DT should be available to update LoopInfo!");
1195   Loop *L = LI->getLoopFor(OldBB);
1196 
1197   // If we need to preserve loop analyses, collect some information about how
1198   // this split will affect loops.
1199   bool IsLoopEntry = !!L;
1200   bool SplitMakesNewLoopHeader = false;
1201   for (BasicBlock *Pred : Preds) {
1202     // Preds that are not reachable from entry should not be used to identify if
1203     // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
1204     // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
1205     // as true and make the NewBB the header of some loop. This breaks LI.
1206     if (!DT->isReachableFromEntry(Pred))
1207       continue;
1208     // If we need to preserve LCSSA, determine if any of the preds is a loop
1209     // exit.
1210     if (PreserveLCSSA)
1211       if (Loop *PL = LI->getLoopFor(Pred))
1212         if (!PL->contains(OldBB))
1213           HasLoopExit = true;
1214 
1215     // If we need to preserve LoopInfo, note whether any of the preds crosses
1216     // an interesting loop boundary.
1217     if (!L)
1218       continue;
1219     if (L->contains(Pred))
1220       IsLoopEntry = false;
1221     else
1222       SplitMakesNewLoopHeader = true;
1223   }
1224 
1225   // Unless we have a loop for OldBB, nothing else to do here.
1226   if (!L)
1227     return;
1228 
1229   if (IsLoopEntry) {
1230     // Add the new block to the nearest enclosing loop (and not an adjacent
1231     // loop). To find this, examine each of the predecessors and determine which
1232     // loops enclose them, and select the most-nested loop which contains the
1233     // loop containing the block being split.
1234     Loop *InnermostPredLoop = nullptr;
1235     for (BasicBlock *Pred : Preds) {
1236       if (Loop *PredLoop = LI->getLoopFor(Pred)) {
1237         // Seek a loop which actually contains the block being split (to avoid
1238         // adjacent loops).
1239         while (PredLoop && !PredLoop->contains(OldBB))
1240           PredLoop = PredLoop->getParentLoop();
1241 
1242         // Select the most-nested of these loops which contains the block.
1243         if (PredLoop && PredLoop->contains(OldBB) &&
1244             (!InnermostPredLoop ||
1245              InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
1246           InnermostPredLoop = PredLoop;
1247       }
1248     }
1249 
1250     if (InnermostPredLoop)
1251       InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
1252   } else {
1253     L->addBasicBlockToLoop(NewBB, *LI);
1254     if (SplitMakesNewLoopHeader)
1255       L->moveToHeader(NewBB);
1256   }
1257 }
1258 
1259 /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
1260 /// This also updates AliasAnalysis, if available.
1261 static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
1262                            ArrayRef<BasicBlock *> Preds, BranchInst *BI,
1263                            bool HasLoopExit) {
1264   // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
1265   SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
1266   for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
1267     PHINode *PN = cast<PHINode>(I++);
1268 
1269     // Check to see if all of the values coming in are the same.  If so, we
1270     // don't need to create a new PHI node, unless it's needed for LCSSA.
1271     Value *InVal = nullptr;
1272     if (!HasLoopExit) {
1273       InVal = PN->getIncomingValueForBlock(Preds[0]);
1274       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1275         if (!PredSet.count(PN->getIncomingBlock(i)))
1276           continue;
1277         if (!InVal)
1278           InVal = PN->getIncomingValue(i);
1279         else if (InVal != PN->getIncomingValue(i)) {
1280           InVal = nullptr;
1281           break;
1282         }
1283       }
1284     }
1285 
1286     if (InVal) {
1287       // If all incoming values for the new PHI would be the same, just don't
1288       // make a new PHI.  Instead, just remove the incoming values from the old
1289       // PHI.
1290       PN->removeIncomingValueIf(
1291           [&](unsigned Idx) {
1292             return PredSet.contains(PN->getIncomingBlock(Idx));
1293           },
1294           /* DeletePHIIfEmpty */ false);
1295 
1296       // Add an incoming value to the PHI node in the loop for the preheader
1297       // edge.
1298       PN->addIncoming(InVal, NewBB);
1299       continue;
1300     }
1301 
1302     // If the values coming into the block are not the same, we need a new
1303     // PHI.
1304     // Create the new PHI node, insert it into NewBB at the end of the block
1305     PHINode *NewPHI =
1306         PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI->getIterator());
1307 
1308     // NOTE! This loop walks backwards for a reason! First off, this minimizes
1309     // the cost of removal if we end up removing a large number of values, and
1310     // second off, this ensures that the indices for the incoming values aren't
1311     // invalidated when we remove one.
1312     for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
1313       BasicBlock *IncomingBB = PN->getIncomingBlock(i);
1314       if (PredSet.count(IncomingBB)) {
1315         Value *V = PN->removeIncomingValue(i, false);
1316         NewPHI->addIncoming(V, IncomingBB);
1317       }
1318     }
1319 
1320     PN->addIncoming(NewPHI, NewBB);
1321   }
1322 }
1323 
1324 static void SplitLandingPadPredecessorsImpl(
1325     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1326     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1327     DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1328     MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
1329 
1330 static BasicBlock *
1331 SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
1332                            const char *Suffix, DomTreeUpdater *DTU,
1333                            DominatorTree *DT, LoopInfo *LI,
1334                            MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1335   // Do not attempt to split that which cannot be split.
1336   if (!BB->canSplitPredecessors())
1337     return nullptr;
1338 
1339   // For the landingpads we need to act a bit differently.
1340   // Delegate this work to the SplitLandingPadPredecessors.
1341   if (BB->isLandingPad()) {
1342     SmallVector<BasicBlock*, 2> NewBBs;
1343     std::string NewName = std::string(Suffix) + ".split-lp";
1344 
1345     SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,
1346                                     DTU, DT, LI, MSSAU, PreserveLCSSA);
1347     return NewBBs[0];
1348   }
1349 
1350   // Create new basic block, insert right before the original block.
1351   BasicBlock *NewBB = BasicBlock::Create(
1352       BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
1353 
1354   // The new block unconditionally branches to the old block.
1355   BranchInst *BI = BranchInst::Create(BB, NewBB);
1356 
1357   Loop *L = nullptr;
1358   BasicBlock *OldLatch = nullptr;
1359   // Splitting the predecessors of a loop header creates a preheader block.
1360   if (LI && LI->isLoopHeader(BB)) {
1361     L = LI->getLoopFor(BB);
1362     // Using the loop start line number prevents debuggers stepping into the
1363     // loop body for this instruction.
1364     BI->setDebugLoc(L->getStartLoc());
1365 
1366     // If BB is the header of the Loop, it is possible that the loop is
1367     // modified, such that the current latch does not remain the latch of the
1368     // loop. If that is the case, the loop metadata from the current latch needs
1369     // to be applied to the new latch.
1370     OldLatch = L->getLoopLatch();
1371   } else
1372     BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
1373 
1374   // Move the edges from Preds to point to NewBB instead of BB.
1375   for (BasicBlock *Pred : Preds) {
1376     // This is slightly more strict than necessary; the minimum requirement
1377     // is that there be no more than one indirectbr branching to BB. And
1378     // all BlockAddress uses would need to be updated.
1379     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1380            "Cannot split an edge from an IndirectBrInst");
1381     Pred->getTerminator()->replaceSuccessorWith(BB, NewBB);
1382   }
1383 
1384   // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
1385   // node becomes an incoming value for BB's phi node.  However, if the Preds
1386   // list is empty, we need to insert dummy entries into the PHI nodes in BB to
1387   // account for the newly created predecessor.
1388   if (Preds.empty()) {
1389     // Insert dummy values as the incoming value.
1390     for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
1391       cast<PHINode>(I)->addIncoming(PoisonValue::get(I->getType()), NewBB);
1392   }
1393 
1394   // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1395   bool HasLoopExit = false;
1396   UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,
1397                             HasLoopExit);
1398 
1399   if (!Preds.empty()) {
1400     // Update the PHI nodes in BB with the values coming from NewBB.
1401     UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
1402   }
1403 
1404   if (OldLatch) {
1405     BasicBlock *NewLatch = L->getLoopLatch();
1406     if (NewLatch != OldLatch) {
1407       MDNode *MD = OldLatch->getTerminator()->getMetadata(LLVMContext::MD_loop);
1408       NewLatch->getTerminator()->setMetadata(LLVMContext::MD_loop, MD);
1409       // It's still possible that OldLatch is the latch of another inner loop,
1410       // in which case we do not remove the metadata.
1411       Loop *IL = LI->getLoopFor(OldLatch);
1412       if (IL && IL->getLoopLatch() != OldLatch)
1413         OldLatch->getTerminator()->setMetadata(LLVMContext::MD_loop, nullptr);
1414     }
1415   }
1416 
1417   return NewBB;
1418 }
1419 
1420 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1421                                          ArrayRef<BasicBlock *> Preds,
1422                                          const char *Suffix, DominatorTree *DT,
1423                                          LoopInfo *LI, MemorySSAUpdater *MSSAU,
1424                                          bool PreserveLCSSA) {
1425   return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,
1426                                     MSSAU, PreserveLCSSA);
1427 }
1428 BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
1429                                          ArrayRef<BasicBlock *> Preds,
1430                                          const char *Suffix,
1431                                          DomTreeUpdater *DTU, LoopInfo *LI,
1432                                          MemorySSAUpdater *MSSAU,
1433                                          bool PreserveLCSSA) {
1434   return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,
1435                                     /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);
1436 }
1437 
1438 static void SplitLandingPadPredecessorsImpl(
1439     BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
1440     const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
1441     DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
1442     MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
1443   assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
1444 
1445   // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
1446   // it right before the original block.
1447   BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
1448                                           OrigBB->getName() + Suffix1,
1449                                           OrigBB->getParent(), OrigBB);
1450   NewBBs.push_back(NewBB1);
1451 
1452   // The new block unconditionally branches to the old block.
1453   BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
1454   BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1455 
1456   // Move the edges from Preds to point to NewBB1 instead of OrigBB.
1457   for (BasicBlock *Pred : Preds) {
1458     // This is slightly more strict than necessary; the minimum requirement
1459     // is that there be no more than one indirectbr branching to BB. And
1460     // all BlockAddress uses would need to be updated.
1461     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1462            "Cannot split an edge from an IndirectBrInst");
1463     Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
1464   }
1465 
1466   bool HasLoopExit = false;
1467   UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,
1468                             PreserveLCSSA, HasLoopExit);
1469 
1470   // Update the PHI nodes in OrigBB with the values coming from NewBB1.
1471   UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
1472 
1473   // Move the remaining edges from OrigBB to point to NewBB2.
1474   SmallVector<BasicBlock*, 8> NewBB2Preds;
1475   for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
1476        i != e; ) {
1477     BasicBlock *Pred = *i++;
1478     if (Pred == NewBB1) continue;
1479     assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
1480            "Cannot split an edge from an IndirectBrInst");
1481     NewBB2Preds.push_back(Pred);
1482     e = pred_end(OrigBB);
1483   }
1484 
1485   BasicBlock *NewBB2 = nullptr;
1486   if (!NewBB2Preds.empty()) {
1487     // Create another basic block for the rest of OrigBB's predecessors.
1488     NewBB2 = BasicBlock::Create(OrigBB->getContext(),
1489                                 OrigBB->getName() + Suffix2,
1490                                 OrigBB->getParent(), OrigBB);
1491     NewBBs.push_back(NewBB2);
1492 
1493     // The new block unconditionally branches to the old block.
1494     BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
1495     BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
1496 
1497     // Move the remaining edges from OrigBB to point to NewBB2.
1498     for (BasicBlock *NewBB2Pred : NewBB2Preds)
1499       NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
1500 
1501     // Update DominatorTree, LoopInfo, and LCCSA analysis information.
1502     HasLoopExit = false;
1503     UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,
1504                               PreserveLCSSA, HasLoopExit);
1505 
1506     // Update the PHI nodes in OrigBB with the values coming from NewBB2.
1507     UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
1508   }
1509 
1510   LandingPadInst *LPad = OrigBB->getLandingPadInst();
1511   Instruction *Clone1 = LPad->clone();
1512   Clone1->setName(Twine("lpad") + Suffix1);
1513   Clone1->insertInto(NewBB1, NewBB1->getFirstInsertionPt());
1514 
1515   if (NewBB2) {
1516     Instruction *Clone2 = LPad->clone();
1517     Clone2->setName(Twine("lpad") + Suffix2);
1518     Clone2->insertInto(NewBB2, NewBB2->getFirstInsertionPt());
1519 
1520     // Create a PHI node for the two cloned landingpad instructions only
1521     // if the original landingpad instruction has some uses.
1522     if (!LPad->use_empty()) {
1523       assert(!LPad->getType()->isTokenTy() &&
1524              "Split cannot be applied if LPad is token type. Otherwise an "
1525              "invalid PHINode of token type would be created.");
1526       PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad->getIterator());
1527       PN->addIncoming(Clone1, NewBB1);
1528       PN->addIncoming(Clone2, NewBB2);
1529       LPad->replaceAllUsesWith(PN);
1530     }
1531     LPad->eraseFromParent();
1532   } else {
1533     // There is no second clone. Just replace the landing pad with the first
1534     // clone.
1535     LPad->replaceAllUsesWith(Clone1);
1536     LPad->eraseFromParent();
1537   }
1538 }
1539 
1540 void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
1541                                        ArrayRef<BasicBlock *> Preds,
1542                                        const char *Suffix1, const char *Suffix2,
1543                                        SmallVectorImpl<BasicBlock *> &NewBBs,
1544                                        DomTreeUpdater *DTU, LoopInfo *LI,
1545                                        MemorySSAUpdater *MSSAU,
1546                                        bool PreserveLCSSA) {
1547   return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,
1548                                          NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,
1549                                          PreserveLCSSA);
1550 }
1551 
1552 ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
1553                                              BasicBlock *Pred,
1554                                              DomTreeUpdater *DTU) {
1555   Instruction *UncondBranch = Pred->getTerminator();
1556   // Clone the return and add it to the end of the predecessor.
1557   Instruction *NewRet = RI->clone();
1558   NewRet->insertInto(Pred, Pred->end());
1559 
1560   // If the return instruction returns a value, and if the value was a
1561   // PHI node in "BB", propagate the right value into the return.
1562   for (Use &Op : NewRet->operands()) {
1563     Value *V = Op;
1564     Instruction *NewBC = nullptr;
1565     if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
1566       // Return value might be bitcasted. Clone and insert it before the
1567       // return instruction.
1568       V = BCI->getOperand(0);
1569       NewBC = BCI->clone();
1570       NewBC->insertInto(Pred, NewRet->getIterator());
1571       Op = NewBC;
1572     }
1573 
1574     Instruction *NewEV = nullptr;
1575     if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
1576       V = EVI->getOperand(0);
1577       NewEV = EVI->clone();
1578       if (NewBC) {
1579         NewBC->setOperand(0, NewEV);
1580         NewEV->insertInto(Pred, NewBC->getIterator());
1581       } else {
1582         NewEV->insertInto(Pred, NewRet->getIterator());
1583         Op = NewEV;
1584       }
1585     }
1586 
1587     if (PHINode *PN = dyn_cast<PHINode>(V)) {
1588       if (PN->getParent() == BB) {
1589         if (NewEV) {
1590           NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
1591         } else if (NewBC)
1592           NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
1593         else
1594           Op = PN->getIncomingValueForBlock(Pred);
1595       }
1596     }
1597   }
1598 
1599   // Update any PHI nodes in the returning block to realize that we no
1600   // longer branch to them.
1601   BB->removePredecessor(Pred);
1602   UncondBranch->eraseFromParent();
1603 
1604   if (DTU)
1605     DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
1606 
1607   return cast<ReturnInst>(NewRet);
1608 }
1609 
1610 Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
1611                                              BasicBlock::iterator SplitBefore,
1612                                              bool Unreachable,
1613                                              MDNode *BranchWeights,
1614                                              DomTreeUpdater *DTU, LoopInfo *LI,
1615                                              BasicBlock *ThenBlock) {
1616   SplitBlockAndInsertIfThenElse(
1617       Cond, SplitBefore, &ThenBlock, /* ElseBlock */ nullptr,
1618       /* UnreachableThen */ Unreachable,
1619       /* UnreachableElse */ false, BranchWeights, DTU, LI);
1620   return ThenBlock->getTerminator();
1621 }
1622 
1623 Instruction *llvm::SplitBlockAndInsertIfElse(Value *Cond,
1624                                              BasicBlock::iterator SplitBefore,
1625                                              bool Unreachable,
1626                                              MDNode *BranchWeights,
1627                                              DomTreeUpdater *DTU, LoopInfo *LI,
1628                                              BasicBlock *ElseBlock) {
1629   SplitBlockAndInsertIfThenElse(
1630       Cond, SplitBefore, /* ThenBlock */ nullptr, &ElseBlock,
1631       /* UnreachableThen */ false,
1632       /* UnreachableElse */ Unreachable, BranchWeights, DTU, LI);
1633   return ElseBlock->getTerminator();
1634 }
1635 
1636 void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, BasicBlock::iterator SplitBefore,
1637                                          Instruction **ThenTerm,
1638                                          Instruction **ElseTerm,
1639                                          MDNode *BranchWeights,
1640                                          DomTreeUpdater *DTU, LoopInfo *LI) {
1641   BasicBlock *ThenBlock = nullptr;
1642   BasicBlock *ElseBlock = nullptr;
1643   SplitBlockAndInsertIfThenElse(
1644       Cond, SplitBefore, &ThenBlock, &ElseBlock, /* UnreachableThen */ false,
1645       /* UnreachableElse */ false, BranchWeights, DTU, LI);
1646 
1647   *ThenTerm = ThenBlock->getTerminator();
1648   *ElseTerm = ElseBlock->getTerminator();
1649 }
1650 
1651 void llvm::SplitBlockAndInsertIfThenElse(
1652     Value *Cond, BasicBlock::iterator SplitBefore, BasicBlock **ThenBlock,
1653     BasicBlock **ElseBlock, bool UnreachableThen, bool UnreachableElse,
1654     MDNode *BranchWeights, DomTreeUpdater *DTU, LoopInfo *LI) {
1655   assert((ThenBlock || ElseBlock) &&
1656          "At least one branch block must be created");
1657   assert((!UnreachableThen || !UnreachableElse) &&
1658          "Split block tail must be reachable");
1659 
1660   SmallVector<DominatorTree::UpdateType, 8> Updates;
1661   SmallPtrSet<BasicBlock *, 8> UniqueOrigSuccessors;
1662   BasicBlock *Head = SplitBefore->getParent();
1663   if (DTU) {
1664     UniqueOrigSuccessors.insert(succ_begin(Head), succ_end(Head));
1665     Updates.reserve(4 + 2 * UniqueOrigSuccessors.size());
1666   }
1667 
1668   LLVMContext &C = Head->getContext();
1669   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
1670   BasicBlock *TrueBlock = Tail;
1671   BasicBlock *FalseBlock = Tail;
1672   bool ThenToTailEdge = false;
1673   bool ElseToTailEdge = false;
1674 
1675   // Encapsulate the logic around creation/insertion/etc of a new block.
1676   auto handleBlock = [&](BasicBlock **PBB, bool Unreachable, BasicBlock *&BB,
1677                          bool &ToTailEdge) {
1678     if (PBB == nullptr)
1679       return; // Do not create/insert a block.
1680 
1681     if (*PBB)
1682       BB = *PBB; // Caller supplied block, use it.
1683     else {
1684       // Create a new block.
1685       BB = BasicBlock::Create(C, "", Head->getParent(), Tail);
1686       if (Unreachable)
1687         (void)new UnreachableInst(C, BB);
1688       else {
1689         (void)BranchInst::Create(Tail, BB);
1690         ToTailEdge = true;
1691       }
1692       BB->getTerminator()->setDebugLoc(SplitBefore->getDebugLoc());
1693       // Pass the new block back to the caller.
1694       *PBB = BB;
1695     }
1696   };
1697 
1698   handleBlock(ThenBlock, UnreachableThen, TrueBlock, ThenToTailEdge);
1699   handleBlock(ElseBlock, UnreachableElse, FalseBlock, ElseToTailEdge);
1700 
1701   Instruction *HeadOldTerm = Head->getTerminator();
1702   BranchInst *HeadNewTerm =
1703       BranchInst::Create(/*ifTrue*/ TrueBlock, /*ifFalse*/ FalseBlock, Cond);
1704   HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
1705   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
1706 
1707   if (DTU) {
1708     Updates.emplace_back(DominatorTree::Insert, Head, TrueBlock);
1709     Updates.emplace_back(DominatorTree::Insert, Head, FalseBlock);
1710     if (ThenToTailEdge)
1711       Updates.emplace_back(DominatorTree::Insert, TrueBlock, Tail);
1712     if (ElseToTailEdge)
1713       Updates.emplace_back(DominatorTree::Insert, FalseBlock, Tail);
1714     for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1715       Updates.emplace_back(DominatorTree::Insert, Tail, UniqueOrigSuccessor);
1716     for (BasicBlock *UniqueOrigSuccessor : UniqueOrigSuccessors)
1717       Updates.emplace_back(DominatorTree::Delete, Head, UniqueOrigSuccessor);
1718     DTU->applyUpdates(Updates);
1719   }
1720 
1721   if (LI) {
1722     if (Loop *L = LI->getLoopFor(Head); L) {
1723       if (ThenToTailEdge)
1724         L->addBasicBlockToLoop(TrueBlock, *LI);
1725       if (ElseToTailEdge)
1726         L->addBasicBlockToLoop(FalseBlock, *LI);
1727       L->addBasicBlockToLoop(Tail, *LI);
1728     }
1729   }
1730 }
1731 
1732 std::pair<Instruction*, Value*>
1733 llvm::SplitBlockAndInsertSimpleForLoop(Value *End, Instruction *SplitBefore) {
1734   BasicBlock *LoopPred = SplitBefore->getParent();
1735   BasicBlock *LoopBody = SplitBlock(SplitBefore->getParent(), SplitBefore);
1736   BasicBlock *LoopExit = SplitBlock(SplitBefore->getParent(), SplitBefore);
1737 
1738   auto *Ty = End->getType();
1739   auto &DL = SplitBefore->getDataLayout();
1740   const unsigned Bitwidth = DL.getTypeSizeInBits(Ty);
1741 
1742   IRBuilder<> Builder(LoopBody->getTerminator());
1743   auto *IV = Builder.CreatePHI(Ty, 2, "iv");
1744   auto *IVNext =
1745     Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",
1746                       /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
1747   auto *IVCheck = Builder.CreateICmpEQ(IVNext, End,
1748                                        IV->getName() + ".check");
1749   Builder.CreateCondBr(IVCheck, LoopExit, LoopBody);
1750   LoopBody->getTerminator()->eraseFromParent();
1751 
1752   // Populate the IV PHI.
1753   IV->addIncoming(ConstantInt::get(Ty, 0), LoopPred);
1754   IV->addIncoming(IVNext, LoopBody);
1755 
1756   return std::make_pair(LoopBody->getFirstNonPHI(), IV);
1757 }
1758 
1759 void llvm::SplitBlockAndInsertForEachLane(ElementCount EC,
1760      Type *IndexTy, Instruction *InsertBefore,
1761      std::function<void(IRBuilderBase&, Value*)> Func) {
1762 
1763   IRBuilder<> IRB(InsertBefore);
1764 
1765   if (EC.isScalable()) {
1766     Value *NumElements = IRB.CreateElementCount(IndexTy, EC);
1767 
1768     auto [BodyIP, Index] =
1769       SplitBlockAndInsertSimpleForLoop(NumElements, InsertBefore);
1770 
1771     IRB.SetInsertPoint(BodyIP);
1772     Func(IRB, Index);
1773     return;
1774   }
1775 
1776   unsigned Num = EC.getFixedValue();
1777   for (unsigned Idx = 0; Idx < Num; ++Idx) {
1778     IRB.SetInsertPoint(InsertBefore);
1779     Func(IRB, ConstantInt::get(IndexTy, Idx));
1780   }
1781 }
1782 
1783 void llvm::SplitBlockAndInsertForEachLane(
1784     Value *EVL, Instruction *InsertBefore,
1785     std::function<void(IRBuilderBase &, Value *)> Func) {
1786 
1787   IRBuilder<> IRB(InsertBefore);
1788   Type *Ty = EVL->getType();
1789 
1790   if (!isa<ConstantInt>(EVL)) {
1791     auto [BodyIP, Index] = SplitBlockAndInsertSimpleForLoop(EVL, InsertBefore);
1792     IRB.SetInsertPoint(BodyIP);
1793     Func(IRB, Index);
1794     return;
1795   }
1796 
1797   unsigned Num = cast<ConstantInt>(EVL)->getZExtValue();
1798   for (unsigned Idx = 0; Idx < Num; ++Idx) {
1799     IRB.SetInsertPoint(InsertBefore);
1800     Func(IRB, ConstantInt::get(Ty, Idx));
1801   }
1802 }
1803 
1804 BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
1805                                  BasicBlock *&IfFalse) {
1806   PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
1807   BasicBlock *Pred1 = nullptr;
1808   BasicBlock *Pred2 = nullptr;
1809 
1810   if (SomePHI) {
1811     if (SomePHI->getNumIncomingValues() != 2)
1812       return nullptr;
1813     Pred1 = SomePHI->getIncomingBlock(0);
1814     Pred2 = SomePHI->getIncomingBlock(1);
1815   } else {
1816     pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
1817     if (PI == PE) // No predecessor
1818       return nullptr;
1819     Pred1 = *PI++;
1820     if (PI == PE) // Only one predecessor
1821       return nullptr;
1822     Pred2 = *PI++;
1823     if (PI != PE) // More than two predecessors
1824       return nullptr;
1825   }
1826 
1827   // We can only handle branches.  Other control flow will be lowered to
1828   // branches if possible anyway.
1829   BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
1830   BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
1831   if (!Pred1Br || !Pred2Br)
1832     return nullptr;
1833 
1834   // Eliminate code duplication by ensuring that Pred1Br is conditional if
1835   // either are.
1836   if (Pred2Br->isConditional()) {
1837     // If both branches are conditional, we don't have an "if statement".  In
1838     // reality, we could transform this case, but since the condition will be
1839     // required anyway, we stand no chance of eliminating it, so the xform is
1840     // probably not profitable.
1841     if (Pred1Br->isConditional())
1842       return nullptr;
1843 
1844     std::swap(Pred1, Pred2);
1845     std::swap(Pred1Br, Pred2Br);
1846   }
1847 
1848   if (Pred1Br->isConditional()) {
1849     // The only thing we have to watch out for here is to make sure that Pred2
1850     // doesn't have incoming edges from other blocks.  If it does, the condition
1851     // doesn't dominate BB.
1852     if (!Pred2->getSinglePredecessor())
1853       return nullptr;
1854 
1855     // If we found a conditional branch predecessor, make sure that it branches
1856     // to BB and Pred2Br.  If it doesn't, this isn't an "if statement".
1857     if (Pred1Br->getSuccessor(0) == BB &&
1858         Pred1Br->getSuccessor(1) == Pred2) {
1859       IfTrue = Pred1;
1860       IfFalse = Pred2;
1861     } else if (Pred1Br->getSuccessor(0) == Pred2 &&
1862                Pred1Br->getSuccessor(1) == BB) {
1863       IfTrue = Pred2;
1864       IfFalse = Pred1;
1865     } else {
1866       // We know that one arm of the conditional goes to BB, so the other must
1867       // go somewhere unrelated, and this must not be an "if statement".
1868       return nullptr;
1869     }
1870 
1871     return Pred1Br;
1872   }
1873 
1874   // Ok, if we got here, both predecessors end with an unconditional branch to
1875   // BB.  Don't panic!  If both blocks only have a single (identical)
1876   // predecessor, and THAT is a conditional branch, then we're all ok!
1877   BasicBlock *CommonPred = Pred1->getSinglePredecessor();
1878   if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
1879     return nullptr;
1880 
1881   // Otherwise, if this is a conditional branch, then we can use it!
1882   BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
1883   if (!BI) return nullptr;
1884 
1885   assert(BI->isConditional() && "Two successors but not conditional?");
1886   if (BI->getSuccessor(0) == Pred1) {
1887     IfTrue = Pred1;
1888     IfFalse = Pred2;
1889   } else {
1890     IfTrue = Pred2;
1891     IfFalse = Pred1;
1892   }
1893   return BI;
1894 }
1895 
1896 void llvm::InvertBranch(BranchInst *PBI, IRBuilderBase &Builder) {
1897   Value *NewCond = PBI->getCondition();
1898   // If this is a "cmp" instruction, only used for branching (and nowhere
1899   // else), then we can simply invert the predicate.
1900   if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
1901     CmpInst *CI = cast<CmpInst>(NewCond);
1902     CI->setPredicate(CI->getInversePredicate());
1903   } else
1904     NewCond = Builder.CreateNot(NewCond, NewCond->getName() + ".not");
1905 
1906   PBI->setCondition(NewCond);
1907   PBI->swapSuccessors();
1908 }
1909 
1910 bool llvm::hasOnlySimpleTerminator(const Function &F) {
1911   for (auto &BB : F) {
1912     auto *Term = BB.getTerminator();
1913     if (!(isa<ReturnInst>(Term) || isa<UnreachableInst>(Term) ||
1914           isa<BranchInst>(Term)))
1915       return false;
1916   }
1917   return true;
1918 }
1919 
1920 bool llvm::isPresplitCoroSuspendExitEdge(const BasicBlock &Src,
1921                                          const BasicBlock &Dest) {
1922   assert(Src.getParent() == Dest.getParent());
1923   if (!Src.getParent()->isPresplitCoroutine())
1924     return false;
1925   if (auto *SW = dyn_cast<SwitchInst>(Src.getTerminator()))
1926     if (auto *Intr = dyn_cast<IntrinsicInst>(SW->getCondition()))
1927       return Intr->getIntrinsicID() == Intrinsic::coro_suspend &&
1928              SW->getDefaultDest() == &Dest;
1929   return false;
1930 }
1931