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