xref: /llvm-project/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp (revision 1d82c765eff0ddf11d017bd833d352bda0a1fb3c)
1 ///===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ---------===//
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 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/Sequence.h"
13 #include "llvm/ADT/SetVector.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/Analysis/AssumptionCache.h"
19 #include "llvm/Analysis/BlockFrequencyInfo.h"
20 #include "llvm/Analysis/CFG.h"
21 #include "llvm/Analysis/CodeMetrics.h"
22 #include "llvm/Analysis/DomTreeUpdater.h"
23 #include "llvm/Analysis/GuardUtils.h"
24 #include "llvm/Analysis/LoopAnalysisManager.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/LoopIterator.h"
27 #include "llvm/Analysis/LoopPass.h"
28 #include "llvm/Analysis/MemorySSA.h"
29 #include "llvm/Analysis/MemorySSAUpdater.h"
30 #include "llvm/Analysis/MustExecute.h"
31 #include "llvm/Analysis/ProfileSummaryInfo.h"
32 #include "llvm/Analysis/ScalarEvolution.h"
33 #include "llvm/Analysis/TargetTransformInfo.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/Constant.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/Dominators.h"
39 #include "llvm/IR/Function.h"
40 #include "llvm/IR/IRBuilder.h"
41 #include "llvm/IR/InstrTypes.h"
42 #include "llvm/IR/Instruction.h"
43 #include "llvm/IR/Instructions.h"
44 #include "llvm/IR/IntrinsicInst.h"
45 #include "llvm/IR/PatternMatch.h"
46 #include "llvm/IR/ProfDataUtils.h"
47 #include "llvm/IR/Use.h"
48 #include "llvm/IR/Value.h"
49 #include "llvm/InitializePasses.h"
50 #include "llvm/Pass.h"
51 #include "llvm/Support/Casting.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/ErrorHandling.h"
55 #include "llvm/Support/GenericDomTree.h"
56 #include "llvm/Support/InstructionCost.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/Scalar/LoopPassManager.h"
59 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
60 #include "llvm/Transforms/Utils/Cloning.h"
61 #include "llvm/Transforms/Utils/Local.h"
62 #include "llvm/Transforms/Utils/LoopUtils.h"
63 #include "llvm/Transforms/Utils/ValueMapper.h"
64 #include <algorithm>
65 #include <cassert>
66 #include <iterator>
67 #include <numeric>
68 #include <optional>
69 #include <utility>
70 
71 #define DEBUG_TYPE "simple-loop-unswitch"
72 
73 using namespace llvm;
74 using namespace llvm::PatternMatch;
75 
76 STATISTIC(NumBranches, "Number of branches unswitched");
77 STATISTIC(NumSwitches, "Number of switches unswitched");
78 STATISTIC(NumSelects, "Number of selects turned into branches for unswitching");
79 STATISTIC(NumGuards, "Number of guards turned into branches for unswitching");
80 STATISTIC(NumTrivial, "Number of unswitches that are trivial");
81 STATISTIC(
82     NumCostMultiplierSkipped,
83     "Number of unswitch candidates that had their cost multiplier skipped");
84 STATISTIC(NumInvariantConditionsInjected,
85           "Number of invariant conditions injected and unswitched");
86 
87 static cl::opt<bool> EnableNonTrivialUnswitch(
88     "enable-nontrivial-unswitch", cl::init(false), cl::Hidden,
89     cl::desc("Forcibly enables non-trivial loop unswitching rather than "
90              "following the configuration passed into the pass."));
91 
92 static cl::opt<int>
93     UnswitchThreshold("unswitch-threshold", cl::init(50), cl::Hidden,
94                       cl::desc("The cost threshold for unswitching a loop."));
95 
96 static cl::opt<bool> EnableUnswitchCostMultiplier(
97     "enable-unswitch-cost-multiplier", cl::init(true), cl::Hidden,
98     cl::desc("Enable unswitch cost multiplier that prohibits exponential "
99              "explosion in nontrivial unswitch."));
100 static cl::opt<int> UnswitchSiblingsToplevelDiv(
101     "unswitch-siblings-toplevel-div", cl::init(2), cl::Hidden,
102     cl::desc("Toplevel siblings divisor for cost multiplier."));
103 static cl::opt<int> UnswitchNumInitialUnscaledCandidates(
104     "unswitch-num-initial-unscaled-candidates", cl::init(8), cl::Hidden,
105     cl::desc("Number of unswitch candidates that are ignored when calculating "
106              "cost multiplier."));
107 static cl::opt<bool> UnswitchGuards(
108     "simple-loop-unswitch-guards", cl::init(true), cl::Hidden,
109     cl::desc("If enabled, simple loop unswitching will also consider "
110              "llvm.experimental.guard intrinsics as unswitch candidates."));
111 static cl::opt<bool> DropNonTrivialImplicitNullChecks(
112     "simple-loop-unswitch-drop-non-trivial-implicit-null-checks",
113     cl::init(false), cl::Hidden,
114     cl::desc("If enabled, drop make.implicit metadata in unswitched implicit "
115              "null checks to save time analyzing if we can keep it."));
116 static cl::opt<unsigned>
117     MSSAThreshold("simple-loop-unswitch-memoryssa-threshold",
118                   cl::desc("Max number of memory uses to explore during "
119                            "partial unswitching analysis"),
120                   cl::init(100), cl::Hidden);
121 static cl::opt<bool> FreezeLoopUnswitchCond(
122     "freeze-loop-unswitch-cond", cl::init(true), cl::Hidden,
123     cl::desc("If enabled, the freeze instruction will be added to condition "
124              "of loop unswitch to prevent miscompilation."));
125 
126 static cl::opt<bool> InjectInvariantConditions(
127     "simple-loop-unswitch-inject-invariant-conditions", cl::Hidden,
128     cl::desc("Whether we should inject new invariants and unswitch them to "
129              "eliminate some existing (non-invariant) conditions."),
130     cl::init(true));
131 
132 static cl::opt<unsigned> InjectInvariantConditionHotnesThreshold(
133     "simple-loop-unswitch-inject-invariant-condition-hotness-threshold",
134     cl::Hidden, cl::desc("Only try to inject loop invariant conditions and "
135                          "unswitch on them to eliminate branches that are "
136                          "not-taken 1/<this option> times or less."),
137     cl::init(16));
138 
139 namespace {
140 struct CompareDesc {
141   BranchInst *Term;
142   Value *Invariant;
143   BasicBlock *InLoopSucc;
144 
145   CompareDesc(BranchInst *Term, Value *Invariant, BasicBlock *InLoopSucc)
146       : Term(Term), Invariant(Invariant), InLoopSucc(InLoopSucc) {}
147 };
148 
149 struct InjectedInvariant {
150   ICmpInst::Predicate Pred;
151   Value *LHS;
152   Value *RHS;
153   BasicBlock *InLoopSucc;
154 
155   InjectedInvariant(ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
156                     BasicBlock *InLoopSucc)
157       : Pred(Pred), LHS(LHS), RHS(RHS), InLoopSucc(InLoopSucc) {}
158 };
159 
160 struct NonTrivialUnswitchCandidate {
161   Instruction *TI = nullptr;
162   TinyPtrVector<Value *> Invariants;
163   std::optional<InstructionCost> Cost;
164   std::optional<InjectedInvariant> PendingInjection;
165   NonTrivialUnswitchCandidate(
166       Instruction *TI, ArrayRef<Value *> Invariants,
167       std::optional<InstructionCost> Cost = std::nullopt,
168       std::optional<InjectedInvariant> PendingInjection = std::nullopt)
169       : TI(TI), Invariants(Invariants), Cost(Cost),
170         PendingInjection(PendingInjection) {};
171 
172   bool hasPendingInjection() const { return PendingInjection.has_value(); }
173 };
174 } // end anonymous namespace.
175 
176 // Helper to skip (select x, true, false), which matches both a logical AND and
177 // OR and can confuse code that tries to determine if \p Cond is either a
178 // logical AND or OR but not both.
179 static Value *skipTrivialSelect(Value *Cond) {
180   Value *CondNext;
181   while (match(Cond, m_Select(m_Value(CondNext), m_One(), m_Zero())))
182     Cond = CondNext;
183   return Cond;
184 }
185 
186 /// Collect all of the loop invariant input values transitively used by the
187 /// homogeneous instruction graph from a given root.
188 ///
189 /// This essentially walks from a root recursively through loop variant operands
190 /// which have perform the same logical operation (AND or OR) and finds all
191 /// inputs which are loop invariant. For some operations these can be
192 /// re-associated and unswitched out of the loop entirely.
193 static TinyPtrVector<Value *>
194 collectHomogenousInstGraphLoopInvariants(const Loop &L, Instruction &Root,
195                                          const LoopInfo &LI) {
196   assert(!L.isLoopInvariant(&Root) &&
197          "Only need to walk the graph if root itself is not invariant.");
198   TinyPtrVector<Value *> Invariants;
199 
200   bool IsRootAnd = match(&Root, m_LogicalAnd());
201   bool IsRootOr  = match(&Root, m_LogicalOr());
202 
203   // Build a worklist and recurse through operators collecting invariants.
204   SmallVector<Instruction *, 4> Worklist;
205   SmallPtrSet<Instruction *, 8> Visited;
206   Worklist.push_back(&Root);
207   Visited.insert(&Root);
208   do {
209     Instruction &I = *Worklist.pop_back_val();
210     for (Value *OpV : I.operand_values()) {
211       // Skip constants as unswitching isn't interesting for them.
212       if (isa<Constant>(OpV))
213         continue;
214 
215       // Add it to our result if loop invariant.
216       if (L.isLoopInvariant(OpV)) {
217         Invariants.push_back(OpV);
218         continue;
219       }
220 
221       // If not an instruction with the same opcode, nothing we can do.
222       Instruction *OpI = dyn_cast<Instruction>(skipTrivialSelect(OpV));
223 
224       if (OpI && ((IsRootAnd && match(OpI, m_LogicalAnd())) ||
225                   (IsRootOr  && match(OpI, m_LogicalOr())))) {
226         // Visit this operand.
227         if (Visited.insert(OpI).second)
228           Worklist.push_back(OpI);
229       }
230     }
231   } while (!Worklist.empty());
232 
233   return Invariants;
234 }
235 
236 static void replaceLoopInvariantUses(const Loop &L, Value *Invariant,
237                                      Constant &Replacement) {
238   assert(!isa<Constant>(Invariant) && "Why are we unswitching on a constant?");
239 
240   // Replace uses of LIC in the loop with the given constant.
241   // We use make_early_inc_range as set invalidates the iterator.
242   for (Use &U : llvm::make_early_inc_range(Invariant->uses())) {
243     Instruction *UserI = dyn_cast<Instruction>(U.getUser());
244 
245     // Replace this use within the loop body.
246     if (UserI && L.contains(UserI))
247       U.set(&Replacement);
248   }
249 }
250 
251 /// Check that all the LCSSA PHI nodes in the loop exit block have trivial
252 /// incoming values along this edge.
253 static bool areLoopExitPHIsLoopInvariant(const Loop &L,
254                                          const BasicBlock &ExitingBB,
255                                          const BasicBlock &ExitBB) {
256   for (const Instruction &I : ExitBB) {
257     auto *PN = dyn_cast<PHINode>(&I);
258     if (!PN)
259       // No more PHIs to check.
260       return true;
261 
262     // If the incoming value for this edge isn't loop invariant the unswitch
263     // won't be trivial.
264     if (!L.isLoopInvariant(PN->getIncomingValueForBlock(&ExitingBB)))
265       return false;
266   }
267   llvm_unreachable("Basic blocks should never be empty!");
268 }
269 
270 /// Copy a set of loop invariant values \p ToDuplicate and insert them at the
271 /// end of \p BB and conditionally branch on the copied condition. We only
272 /// branch on a single value.
273 static void buildPartialUnswitchConditionalBranch(
274     BasicBlock &BB, ArrayRef<Value *> Invariants, bool Direction,
275     BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, bool InsertFreeze,
276     const Instruction *I, AssumptionCache *AC, const DominatorTree &DT) {
277   IRBuilder<> IRB(&BB);
278 
279   SmallVector<Value *> FrozenInvariants;
280   for (Value *Inv : Invariants) {
281     if (InsertFreeze && !isGuaranteedNotToBeUndefOrPoison(Inv, AC, I, &DT))
282       Inv = IRB.CreateFreeze(Inv, Inv->getName() + ".fr");
283     FrozenInvariants.push_back(Inv);
284   }
285 
286   Value *Cond = Direction ? IRB.CreateOr(FrozenInvariants)
287                           : IRB.CreateAnd(FrozenInvariants);
288   IRB.CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc,
289                    Direction ? &NormalSucc : &UnswitchedSucc);
290 }
291 
292 /// Copy a set of loop invariant values, and conditionally branch on them.
293 static void buildPartialInvariantUnswitchConditionalBranch(
294     BasicBlock &BB, ArrayRef<Value *> ToDuplicate, bool Direction,
295     BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, Loop &L,
296     MemorySSAUpdater *MSSAU) {
297   ValueToValueMapTy VMap;
298   for (auto *Val : reverse(ToDuplicate)) {
299     Instruction *Inst = cast<Instruction>(Val);
300     Instruction *NewInst = Inst->clone();
301     NewInst->insertInto(&BB, BB.end());
302     RemapInstruction(NewInst, VMap,
303                      RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
304     VMap[Val] = NewInst;
305 
306     if (!MSSAU)
307       continue;
308 
309     MemorySSA *MSSA = MSSAU->getMemorySSA();
310     if (auto *MemUse =
311             dyn_cast_or_null<MemoryUse>(MSSA->getMemoryAccess(Inst))) {
312       auto *DefiningAccess = MemUse->getDefiningAccess();
313       // Get the first defining access before the loop.
314       while (L.contains(DefiningAccess->getBlock())) {
315         // If the defining access is a MemoryPhi, get the incoming
316         // value for the pre-header as defining access.
317         if (auto *MemPhi = dyn_cast<MemoryPhi>(DefiningAccess))
318           DefiningAccess =
319               MemPhi->getIncomingValueForBlock(L.getLoopPreheader());
320         else
321           DefiningAccess = cast<MemoryDef>(DefiningAccess)->getDefiningAccess();
322       }
323       MSSAU->createMemoryAccessInBB(NewInst, DefiningAccess,
324                                     NewInst->getParent(),
325                                     MemorySSA::BeforeTerminator);
326     }
327   }
328 
329   IRBuilder<> IRB(&BB);
330   Value *Cond = VMap[ToDuplicate[0]];
331   IRB.CreateCondBr(Cond, Direction ? &UnswitchedSucc : &NormalSucc,
332                    Direction ? &NormalSucc : &UnswitchedSucc);
333 }
334 
335 /// Rewrite the PHI nodes in an unswitched loop exit basic block.
336 ///
337 /// Requires that the loop exit and unswitched basic block are the same, and
338 /// that the exiting block was a unique predecessor of that block. Rewrites the
339 /// PHI nodes in that block such that what were LCSSA PHI nodes become trivial
340 /// PHI nodes from the old preheader that now contains the unswitched
341 /// terminator.
342 static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB,
343                                                   BasicBlock &OldExitingBB,
344                                                   BasicBlock &OldPH) {
345   for (PHINode &PN : UnswitchedBB.phis()) {
346     // When the loop exit is directly unswitched we just need to update the
347     // incoming basic block. We loop to handle weird cases with repeated
348     // incoming blocks, but expect to typically only have one operand here.
349     for (auto i : seq<int>(0, PN.getNumOperands())) {
350       assert(PN.getIncomingBlock(i) == &OldExitingBB &&
351              "Found incoming block different from unique predecessor!");
352       PN.setIncomingBlock(i, &OldPH);
353     }
354   }
355 }
356 
357 /// Rewrite the PHI nodes in the loop exit basic block and the split off
358 /// unswitched block.
359 ///
360 /// Because the exit block remains an exit from the loop, this rewrites the
361 /// LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI
362 /// nodes into the unswitched basic block to select between the value in the
363 /// old preheader and the loop exit.
364 static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock &ExitBB,
365                                                       BasicBlock &UnswitchedBB,
366                                                       BasicBlock &OldExitingBB,
367                                                       BasicBlock &OldPH,
368                                                       bool FullUnswitch) {
369   assert(&ExitBB != &UnswitchedBB &&
370          "Must have different loop exit and unswitched blocks!");
371   BasicBlock::iterator InsertPt = UnswitchedBB.begin();
372   for (PHINode &PN : ExitBB.phis()) {
373     auto *NewPN = PHINode::Create(PN.getType(), /*NumReservedValues*/ 2,
374                                   PN.getName() + ".split");
375     NewPN->insertBefore(InsertPt);
376 
377     // Walk backwards over the old PHI node's inputs to minimize the cost of
378     // removing each one. We have to do this weird loop manually so that we
379     // create the same number of new incoming edges in the new PHI as we expect
380     // each case-based edge to be included in the unswitched switch in some
381     // cases.
382     // FIXME: This is really, really gross. It would be much cleaner if LLVM
383     // allowed us to create a single entry for a predecessor block without
384     // having separate entries for each "edge" even though these edges are
385     // required to produce identical results.
386     for (int i = PN.getNumIncomingValues() - 1; i >= 0; --i) {
387       if (PN.getIncomingBlock(i) != &OldExitingBB)
388         continue;
389 
390       Value *Incoming = PN.getIncomingValue(i);
391       if (FullUnswitch)
392         // No more edge from the old exiting block to the exit block.
393         PN.removeIncomingValue(i);
394 
395       NewPN->addIncoming(Incoming, &OldPH);
396     }
397 
398     // Now replace the old PHI with the new one and wire the old one in as an
399     // input to the new one.
400     PN.replaceAllUsesWith(NewPN);
401     NewPN->addIncoming(&PN, &ExitBB);
402   }
403 }
404 
405 /// Hoist the current loop up to the innermost loop containing a remaining exit.
406 ///
407 /// Because we've removed an exit from the loop, we may have changed the set of
408 /// loops reachable and need to move the current loop up the loop nest or even
409 /// to an entirely separate nest.
410 static void hoistLoopToNewParent(Loop &L, BasicBlock &Preheader,
411                                  DominatorTree &DT, LoopInfo &LI,
412                                  MemorySSAUpdater *MSSAU, ScalarEvolution *SE) {
413   // If the loop is already at the top level, we can't hoist it anywhere.
414   Loop *OldParentL = L.getParentLoop();
415   if (!OldParentL)
416     return;
417 
418   SmallVector<BasicBlock *, 4> Exits;
419   L.getExitBlocks(Exits);
420   Loop *NewParentL = nullptr;
421   for (auto *ExitBB : Exits)
422     if (Loop *ExitL = LI.getLoopFor(ExitBB))
423       if (!NewParentL || NewParentL->contains(ExitL))
424         NewParentL = ExitL;
425 
426   if (NewParentL == OldParentL)
427     return;
428 
429   // The new parent loop (if different) should always contain the old one.
430   if (NewParentL)
431     assert(NewParentL->contains(OldParentL) &&
432            "Can only hoist this loop up the nest!");
433 
434   // The preheader will need to move with the body of this loop. However,
435   // because it isn't in this loop we also need to update the primary loop map.
436   assert(OldParentL == LI.getLoopFor(&Preheader) &&
437          "Parent loop of this loop should contain this loop's preheader!");
438   LI.changeLoopFor(&Preheader, NewParentL);
439 
440   // Remove this loop from its old parent.
441   OldParentL->removeChildLoop(&L);
442 
443   // Add the loop either to the new parent or as a top-level loop.
444   if (NewParentL)
445     NewParentL->addChildLoop(&L);
446   else
447     LI.addTopLevelLoop(&L);
448 
449   // Remove this loops blocks from the old parent and every other loop up the
450   // nest until reaching the new parent. Also update all of these
451   // no-longer-containing loops to reflect the nesting change.
452   for (Loop *OldContainingL = OldParentL; OldContainingL != NewParentL;
453        OldContainingL = OldContainingL->getParentLoop()) {
454     llvm::erase_if(OldContainingL->getBlocksVector(),
455                    [&](const BasicBlock *BB) {
456                      return BB == &Preheader || L.contains(BB);
457                    });
458 
459     OldContainingL->getBlocksSet().erase(&Preheader);
460     for (BasicBlock *BB : L.blocks())
461       OldContainingL->getBlocksSet().erase(BB);
462 
463     // Because we just hoisted a loop out of this one, we have essentially
464     // created new exit paths from it. That means we need to form LCSSA PHI
465     // nodes for values used in the no-longer-nested loop.
466     formLCSSA(*OldContainingL, DT, &LI, SE);
467 
468     // We shouldn't need to form dedicated exits because the exit introduced
469     // here is the (just split by unswitching) preheader. However, after trivial
470     // unswitching it is possible to get new non-dedicated exits out of parent
471     // loop so let's conservatively form dedicated exit blocks and figure out
472     // if we can optimize later.
473     formDedicatedExitBlocks(OldContainingL, &DT, &LI, MSSAU,
474                             /*PreserveLCSSA*/ true);
475   }
476 }
477 
478 // Return the top-most loop containing ExitBB and having ExitBB as exiting block
479 // or the loop containing ExitBB, if there is no parent loop containing ExitBB
480 // as exiting block.
481 static Loop *getTopMostExitingLoop(const BasicBlock *ExitBB,
482                                    const LoopInfo &LI) {
483   Loop *TopMost = LI.getLoopFor(ExitBB);
484   Loop *Current = TopMost;
485   while (Current) {
486     if (Current->isLoopExiting(ExitBB))
487       TopMost = Current;
488     Current = Current->getParentLoop();
489   }
490   return TopMost;
491 }
492 
493 /// Unswitch a trivial branch if the condition is loop invariant.
494 ///
495 /// This routine should only be called when loop code leading to the branch has
496 /// been validated as trivial (no side effects). This routine checks if the
497 /// condition is invariant and one of the successors is a loop exit. This
498 /// allows us to unswitch without duplicating the loop, making it trivial.
499 ///
500 /// If this routine fails to unswitch the branch it returns false.
501 ///
502 /// If the branch can be unswitched, this routine splits the preheader and
503 /// hoists the branch above that split. Preserves loop simplified form
504 /// (splitting the exit block as necessary). It simplifies the branch within
505 /// the loop to an unconditional branch but doesn't remove it entirely. Further
506 /// cleanup can be done with some simplifycfg like pass.
507 ///
508 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
509 /// invalidated by this.
510 static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
511                                   LoopInfo &LI, ScalarEvolution *SE,
512                                   MemorySSAUpdater *MSSAU) {
513   assert(BI.isConditional() && "Can only unswitch a conditional branch!");
514   LLVM_DEBUG(dbgs() << "  Trying to unswitch branch: " << BI << "\n");
515 
516   // The loop invariant values that we want to unswitch.
517   TinyPtrVector<Value *> Invariants;
518 
519   // When true, we're fully unswitching the branch rather than just unswitching
520   // some input conditions to the branch.
521   bool FullUnswitch = false;
522 
523   Value *Cond = skipTrivialSelect(BI.getCondition());
524   if (L.isLoopInvariant(Cond)) {
525     Invariants.push_back(Cond);
526     FullUnswitch = true;
527   } else {
528     if (auto *CondInst = dyn_cast<Instruction>(Cond))
529       Invariants = collectHomogenousInstGraphLoopInvariants(L, *CondInst, LI);
530     if (Invariants.empty()) {
531       LLVM_DEBUG(dbgs() << "   Couldn't find invariant inputs!\n");
532       return false;
533     }
534   }
535 
536   // Check that one of the branch's successors exits, and which one.
537   bool ExitDirection = true;
538   int LoopExitSuccIdx = 0;
539   auto *LoopExitBB = BI.getSuccessor(0);
540   if (L.contains(LoopExitBB)) {
541     ExitDirection = false;
542     LoopExitSuccIdx = 1;
543     LoopExitBB = BI.getSuccessor(1);
544     if (L.contains(LoopExitBB)) {
545       LLVM_DEBUG(dbgs() << "   Branch doesn't exit the loop!\n");
546       return false;
547     }
548   }
549   auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx);
550   auto *ParentBB = BI.getParent();
551   if (!areLoopExitPHIsLoopInvariant(L, *ParentBB, *LoopExitBB)) {
552     LLVM_DEBUG(dbgs() << "   Loop exit PHI's aren't loop-invariant!\n");
553     return false;
554   }
555 
556   // When unswitching only part of the branch's condition, we need the exit
557   // block to be reached directly from the partially unswitched input. This can
558   // be done when the exit block is along the true edge and the branch condition
559   // is a graph of `or` operations, or the exit block is along the false edge
560   // and the condition is a graph of `and` operations.
561   if (!FullUnswitch) {
562     if (ExitDirection ? !match(Cond, m_LogicalOr())
563                       : !match(Cond, m_LogicalAnd())) {
564       LLVM_DEBUG(dbgs() << "   Branch condition is in improper form for "
565                            "non-full unswitch!\n");
566       return false;
567     }
568   }
569 
570   LLVM_DEBUG({
571     dbgs() << "    unswitching trivial invariant conditions for: " << BI
572            << "\n";
573     for (Value *Invariant : Invariants) {
574       dbgs() << "      " << *Invariant << " == true";
575       if (Invariant != Invariants.back())
576         dbgs() << " ||";
577       dbgs() << "\n";
578     }
579   });
580 
581   // If we have scalar evolutions, we need to invalidate them including this
582   // loop, the loop containing the exit block and the topmost parent loop
583   // exiting via LoopExitBB.
584   if (SE) {
585     if (const Loop *ExitL = getTopMostExitingLoop(LoopExitBB, LI))
586       SE->forgetLoop(ExitL);
587     else
588       // Forget the entire nest as this exits the entire nest.
589       SE->forgetTopmostLoop(&L);
590     SE->forgetBlockAndLoopDispositions();
591   }
592 
593   if (MSSAU && VerifyMemorySSA)
594     MSSAU->getMemorySSA()->verifyMemorySSA();
595 
596   // Split the preheader, so that we know that there is a safe place to insert
597   // the conditional branch. We will change the preheader to have a conditional
598   // branch on LoopCond.
599   BasicBlock *OldPH = L.getLoopPreheader();
600   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI, MSSAU);
601 
602   // Now that we have a place to insert the conditional branch, create a place
603   // to branch to: this is the exit block out of the loop that we are
604   // unswitching. We need to split this if there are other loop predecessors.
605   // Because the loop is in simplified form, *any* other predecessor is enough.
606   BasicBlock *UnswitchedBB;
607   if (FullUnswitch && LoopExitBB->getUniquePredecessor()) {
608     assert(LoopExitBB->getUniquePredecessor() == BI.getParent() &&
609            "A branch's parent isn't a predecessor!");
610     UnswitchedBB = LoopExitBB;
611   } else {
612     UnswitchedBB =
613         SplitBlock(LoopExitBB, LoopExitBB->begin(), &DT, &LI, MSSAU, "", false);
614   }
615 
616   if (MSSAU && VerifyMemorySSA)
617     MSSAU->getMemorySSA()->verifyMemorySSA();
618 
619   // Actually move the invariant uses into the unswitched position. If possible,
620   // we do this by moving the instructions, but when doing partial unswitching
621   // we do it by building a new merge of the values in the unswitched position.
622   OldPH->getTerminator()->eraseFromParent();
623   if (FullUnswitch) {
624     // If fully unswitching, we can use the existing branch instruction.
625     // Splice it into the old PH to gate reaching the new preheader and re-point
626     // its successors.
627     BI.moveBefore(*OldPH, OldPH->end());
628     BI.setCondition(Cond);
629     if (MSSAU) {
630       // Temporarily clone the terminator, to make MSSA update cheaper by
631       // separating "insert edge" updates from "remove edge" ones.
632       BI.clone()->insertInto(ParentBB, ParentBB->end());
633     } else {
634       // Create a new unconditional branch that will continue the loop as a new
635       // terminator.
636       BranchInst::Create(ContinueBB, ParentBB);
637     }
638     BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB);
639     BI.setSuccessor(1 - LoopExitSuccIdx, NewPH);
640   } else {
641     // Only unswitching a subset of inputs to the condition, so we will need to
642     // build a new branch that merges the invariant inputs.
643     if (ExitDirection)
644       assert(match(skipTrivialSelect(BI.getCondition()), m_LogicalOr()) &&
645              "Must have an `or` of `i1`s or `select i1 X, true, Y`s for the "
646              "condition!");
647     else
648       assert(match(skipTrivialSelect(BI.getCondition()), m_LogicalAnd()) &&
649              "Must have an `and` of `i1`s or `select i1 X, Y, false`s for the"
650              " condition!");
651     buildPartialUnswitchConditionalBranch(
652         *OldPH, Invariants, ExitDirection, *UnswitchedBB, *NewPH,
653         FreezeLoopUnswitchCond, OldPH->getTerminator(), nullptr, DT);
654   }
655 
656   // Update the dominator tree with the added edge.
657   DT.insertEdge(OldPH, UnswitchedBB);
658 
659   // After the dominator tree was updated with the added edge, update MemorySSA
660   // if available.
661   if (MSSAU) {
662     SmallVector<CFGUpdate, 1> Updates;
663     Updates.push_back({cfg::UpdateKind::Insert, OldPH, UnswitchedBB});
664     MSSAU->applyInsertUpdates(Updates, DT);
665   }
666 
667   // Finish updating dominator tree and memory ssa for full unswitch.
668   if (FullUnswitch) {
669     if (MSSAU) {
670       // Remove the cloned branch instruction.
671       ParentBB->getTerminator()->eraseFromParent();
672       // Create unconditional branch now.
673       BranchInst::Create(ContinueBB, ParentBB);
674       MSSAU->removeEdge(ParentBB, LoopExitBB);
675     }
676     DT.deleteEdge(ParentBB, LoopExitBB);
677   }
678 
679   if (MSSAU && VerifyMemorySSA)
680     MSSAU->getMemorySSA()->verifyMemorySSA();
681 
682   // Rewrite the relevant PHI nodes.
683   if (UnswitchedBB == LoopExitBB)
684     rewritePHINodesForUnswitchedExitBlock(*UnswitchedBB, *ParentBB, *OldPH);
685   else
686     rewritePHINodesForExitAndUnswitchedBlocks(*LoopExitBB, *UnswitchedBB,
687                                               *ParentBB, *OldPH, FullUnswitch);
688 
689   // The constant we can replace all of our invariants with inside the loop
690   // body. If any of the invariants have a value other than this the loop won't
691   // be entered.
692   ConstantInt *Replacement = ExitDirection
693                                  ? ConstantInt::getFalse(BI.getContext())
694                                  : ConstantInt::getTrue(BI.getContext());
695 
696   // Since this is an i1 condition we can also trivially replace uses of it
697   // within the loop with a constant.
698   for (Value *Invariant : Invariants)
699     replaceLoopInvariantUses(L, Invariant, *Replacement);
700 
701   // If this was full unswitching, we may have changed the nesting relationship
702   // for this loop so hoist it to its correct parent if needed.
703   if (FullUnswitch)
704     hoistLoopToNewParent(L, *NewPH, DT, LI, MSSAU, SE);
705 
706   if (MSSAU && VerifyMemorySSA)
707     MSSAU->getMemorySSA()->verifyMemorySSA();
708 
709   LLVM_DEBUG(dbgs() << "    done: unswitching trivial branch...\n");
710   ++NumTrivial;
711   ++NumBranches;
712   return true;
713 }
714 
715 /// Unswitch a trivial switch if the condition is loop invariant.
716 ///
717 /// This routine should only be called when loop code leading to the switch has
718 /// been validated as trivial (no side effects). This routine checks if the
719 /// condition is invariant and that at least one of the successors is a loop
720 /// exit. This allows us to unswitch without duplicating the loop, making it
721 /// trivial.
722 ///
723 /// If this routine fails to unswitch the switch it returns false.
724 ///
725 /// If the switch can be unswitched, this routine splits the preheader and
726 /// copies the switch above that split. If the default case is one of the
727 /// exiting cases, it copies the non-exiting cases and points them at the new
728 /// preheader. If the default case is not exiting, it copies the exiting cases
729 /// and points the default at the preheader. It preserves loop simplified form
730 /// (splitting the exit blocks as necessary). It simplifies the switch within
731 /// the loop by removing now-dead cases. If the default case is one of those
732 /// unswitched, it replaces its destination with a new basic block containing
733 /// only unreachable. Such basic blocks, while technically loop exits, are not
734 /// considered for unswitching so this is a stable transform and the same
735 /// switch will not be revisited. If after unswitching there is only a single
736 /// in-loop successor, the switch is further simplified to an unconditional
737 /// branch. Still more cleanup can be done with some simplifycfg like pass.
738 ///
739 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
740 /// invalidated by this.
741 static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
742                                   LoopInfo &LI, ScalarEvolution *SE,
743                                   MemorySSAUpdater *MSSAU) {
744   LLVM_DEBUG(dbgs() << "  Trying to unswitch switch: " << SI << "\n");
745   Value *LoopCond = SI.getCondition();
746 
747   // If this isn't switching on an invariant condition, we can't unswitch it.
748   if (!L.isLoopInvariant(LoopCond))
749     return false;
750 
751   auto *ParentBB = SI.getParent();
752 
753   // The same check must be used both for the default and the exit cases. We
754   // should never leave edges from the switch instruction to a basic block that
755   // we are unswitching, hence the condition used to determine the default case
756   // needs to also be used to populate ExitCaseIndices, which is then used to
757   // remove cases from the switch.
758   auto IsTriviallyUnswitchableExitBlock = [&](BasicBlock &BBToCheck) {
759     // BBToCheck is not an exit block if it is inside loop L.
760     if (L.contains(&BBToCheck))
761       return false;
762     // BBToCheck is not trivial to unswitch if its phis aren't loop invariant.
763     if (!areLoopExitPHIsLoopInvariant(L, *ParentBB, BBToCheck))
764       return false;
765     // We do not unswitch a block that only has an unreachable statement, as
766     // it's possible this is a previously unswitched block. Only unswitch if
767     // either the terminator is not unreachable, or, if it is, it's not the only
768     // instruction in the block.
769     auto *TI = BBToCheck.getTerminator();
770     bool isUnreachable = isa<UnreachableInst>(TI);
771     return !isUnreachable ||
772            (isUnreachable && (BBToCheck.getFirstNonPHIOrDbg() != TI));
773   };
774 
775   SmallVector<int, 4> ExitCaseIndices;
776   for (auto Case : SI.cases())
777     if (IsTriviallyUnswitchableExitBlock(*Case.getCaseSuccessor()))
778       ExitCaseIndices.push_back(Case.getCaseIndex());
779   BasicBlock *DefaultExitBB = nullptr;
780   SwitchInstProfUpdateWrapper::CaseWeightOpt DefaultCaseWeight =
781       SwitchInstProfUpdateWrapper::getSuccessorWeight(SI, 0);
782   if (IsTriviallyUnswitchableExitBlock(*SI.getDefaultDest())) {
783     DefaultExitBB = SI.getDefaultDest();
784   } else if (ExitCaseIndices.empty())
785     return false;
786 
787   LLVM_DEBUG(dbgs() << "    unswitching trivial switch...\n");
788 
789   if (MSSAU && VerifyMemorySSA)
790     MSSAU->getMemorySSA()->verifyMemorySSA();
791 
792   // We may need to invalidate SCEVs for the outermost loop reached by any of
793   // the exits.
794   Loop *OuterL = &L;
795 
796   if (DefaultExitBB) {
797     // Check the loop containing this exit.
798     Loop *ExitL = getTopMostExitingLoop(DefaultExitBB, LI);
799     if (!ExitL || ExitL->contains(OuterL))
800       OuterL = ExitL;
801   }
802   for (unsigned Index : ExitCaseIndices) {
803     auto CaseI = SI.case_begin() + Index;
804     // Compute the outer loop from this exit.
805     Loop *ExitL = getTopMostExitingLoop(CaseI->getCaseSuccessor(), LI);
806     if (!ExitL || ExitL->contains(OuterL))
807       OuterL = ExitL;
808   }
809 
810   if (SE) {
811     if (OuterL)
812       SE->forgetLoop(OuterL);
813     else
814       SE->forgetTopmostLoop(&L);
815   }
816 
817   if (DefaultExitBB) {
818     // Clear out the default destination temporarily to allow accurate
819     // predecessor lists to be examined below.
820     SI.setDefaultDest(nullptr);
821   }
822 
823   // Store the exit cases into a separate data structure and remove them from
824   // the switch.
825   SmallVector<std::tuple<ConstantInt *, BasicBlock *,
826                          SwitchInstProfUpdateWrapper::CaseWeightOpt>,
827               4> ExitCases;
828   ExitCases.reserve(ExitCaseIndices.size());
829   SwitchInstProfUpdateWrapper SIW(SI);
830   // We walk the case indices backwards so that we remove the last case first
831   // and don't disrupt the earlier indices.
832   for (unsigned Index : reverse(ExitCaseIndices)) {
833     auto CaseI = SI.case_begin() + Index;
834     // Save the value of this case.
835     auto W = SIW.getSuccessorWeight(CaseI->getSuccessorIndex());
836     ExitCases.emplace_back(CaseI->getCaseValue(), CaseI->getCaseSuccessor(), W);
837     // Delete the unswitched cases.
838     SIW.removeCase(CaseI);
839   }
840 
841   // Check if after this all of the remaining cases point at the same
842   // successor.
843   BasicBlock *CommonSuccBB = nullptr;
844   if (SI.getNumCases() > 0 &&
845       all_of(drop_begin(SI.cases()), [&SI](const SwitchInst::CaseHandle &Case) {
846         return Case.getCaseSuccessor() == SI.case_begin()->getCaseSuccessor();
847       }))
848     CommonSuccBB = SI.case_begin()->getCaseSuccessor();
849   if (!DefaultExitBB) {
850     // If we're not unswitching the default, we need it to match any cases to
851     // have a common successor or if we have no cases it is the common
852     // successor.
853     if (SI.getNumCases() == 0)
854       CommonSuccBB = SI.getDefaultDest();
855     else if (SI.getDefaultDest() != CommonSuccBB)
856       CommonSuccBB = nullptr;
857   }
858 
859   // Split the preheader, so that we know that there is a safe place to insert
860   // the switch.
861   BasicBlock *OldPH = L.getLoopPreheader();
862   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI, MSSAU);
863   OldPH->getTerminator()->eraseFromParent();
864 
865   // Now add the unswitched switch.
866   auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH);
867   SwitchInstProfUpdateWrapper NewSIW(*NewSI);
868 
869   // Rewrite the IR for the unswitched basic blocks. This requires two steps.
870   // First, we split any exit blocks with remaining in-loop predecessors. Then
871   // we update the PHIs in one of two ways depending on if there was a split.
872   // We walk in reverse so that we split in the same order as the cases
873   // appeared. This is purely for convenience of reading the resulting IR, but
874   // it doesn't cost anything really.
875   SmallPtrSet<BasicBlock *, 2> UnswitchedExitBBs;
876   SmallDenseMap<BasicBlock *, BasicBlock *, 2> SplitExitBBMap;
877   // Handle the default exit if necessary.
878   // FIXME: It'd be great if we could merge this with the loop below but LLVM's
879   // ranges aren't quite powerful enough yet.
880   if (DefaultExitBB) {
881     if (pred_empty(DefaultExitBB)) {
882       UnswitchedExitBBs.insert(DefaultExitBB);
883       rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH);
884     } else {
885       auto *SplitBB =
886           SplitBlock(DefaultExitBB, DefaultExitBB->begin(), &DT, &LI, MSSAU);
887       rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB,
888                                                 *ParentBB, *OldPH,
889                                                 /*FullUnswitch*/ true);
890       DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB;
891     }
892   }
893   // Note that we must use a reference in the for loop so that we update the
894   // container.
895   for (auto &ExitCase : reverse(ExitCases)) {
896     // Grab a reference to the exit block in the pair so that we can update it.
897     BasicBlock *ExitBB = std::get<1>(ExitCase);
898 
899     // If this case is the last edge into the exit block, we can simply reuse it
900     // as it will no longer be a loop exit. No mapping necessary.
901     if (pred_empty(ExitBB)) {
902       // Only rewrite once.
903       if (UnswitchedExitBBs.insert(ExitBB).second)
904         rewritePHINodesForUnswitchedExitBlock(*ExitBB, *ParentBB, *OldPH);
905       continue;
906     }
907 
908     // Otherwise we need to split the exit block so that we retain an exit
909     // block from the loop and a target for the unswitched condition.
910     BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
911     if (!SplitExitBB) {
912       // If this is the first time we see this, do the split and remember it.
913       SplitExitBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU);
914       rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB,
915                                                 *ParentBB, *OldPH,
916                                                 /*FullUnswitch*/ true);
917     }
918     // Update the case pair to point to the split block.
919     std::get<1>(ExitCase) = SplitExitBB;
920   }
921 
922   // Now add the unswitched cases. We do this in reverse order as we built them
923   // in reverse order.
924   for (auto &ExitCase : reverse(ExitCases)) {
925     ConstantInt *CaseVal = std::get<0>(ExitCase);
926     BasicBlock *UnswitchedBB = std::get<1>(ExitCase);
927 
928     NewSIW.addCase(CaseVal, UnswitchedBB, std::get<2>(ExitCase));
929   }
930 
931   // If the default was unswitched, re-point it and add explicit cases for
932   // entering the loop.
933   if (DefaultExitBB) {
934     NewSIW->setDefaultDest(DefaultExitBB);
935     NewSIW.setSuccessorWeight(0, DefaultCaseWeight);
936 
937     // We removed all the exit cases, so we just copy the cases to the
938     // unswitched switch.
939     for (const auto &Case : SI.cases())
940       NewSIW.addCase(Case.getCaseValue(), NewPH,
941                      SIW.getSuccessorWeight(Case.getSuccessorIndex()));
942   } else if (DefaultCaseWeight) {
943     // We have to set branch weight of the default case.
944     uint64_t SW = *DefaultCaseWeight;
945     for (const auto &Case : SI.cases()) {
946       auto W = SIW.getSuccessorWeight(Case.getSuccessorIndex());
947       assert(W &&
948              "case weight must be defined as default case weight is defined");
949       SW += *W;
950     }
951     NewSIW.setSuccessorWeight(0, SW);
952   }
953 
954   // If we ended up with a common successor for every path through the switch
955   // after unswitching, rewrite it to an unconditional branch to make it easy
956   // to recognize. Otherwise we potentially have to recognize the default case
957   // pointing at unreachable and other complexity.
958   if (CommonSuccBB) {
959     BasicBlock *BB = SI.getParent();
960     // We may have had multiple edges to this common successor block, so remove
961     // them as predecessors. We skip the first one, either the default or the
962     // actual first case.
963     bool SkippedFirst = DefaultExitBB == nullptr;
964     for (auto Case : SI.cases()) {
965       assert(Case.getCaseSuccessor() == CommonSuccBB &&
966              "Non-common successor!");
967       (void)Case;
968       if (!SkippedFirst) {
969         SkippedFirst = true;
970         continue;
971       }
972       CommonSuccBB->removePredecessor(BB,
973                                       /*KeepOneInputPHIs*/ true);
974     }
975     // Now nuke the switch and replace it with a direct branch.
976     SIW.eraseFromParent();
977     BranchInst::Create(CommonSuccBB, BB);
978   } else if (DefaultExitBB) {
979     assert(SI.getNumCases() > 0 &&
980            "If we had no cases we'd have a common successor!");
981     // Move the last case to the default successor. This is valid as if the
982     // default got unswitched it cannot be reached. This has the advantage of
983     // being simple and keeping the number of edges from this switch to
984     // successors the same, and avoiding any PHI update complexity.
985     auto LastCaseI = std::prev(SI.case_end());
986 
987     SI.setDefaultDest(LastCaseI->getCaseSuccessor());
988     SIW.setSuccessorWeight(
989         0, SIW.getSuccessorWeight(LastCaseI->getSuccessorIndex()));
990     SIW.removeCase(LastCaseI);
991   }
992 
993   // Walk the unswitched exit blocks and the unswitched split blocks and update
994   // the dominator tree based on the CFG edits. While we are walking unordered
995   // containers here, the API for applyUpdates takes an unordered list of
996   // updates and requires them to not contain duplicates.
997   SmallVector<DominatorTree::UpdateType, 4> DTUpdates;
998   for (auto *UnswitchedExitBB : UnswitchedExitBBs) {
999     DTUpdates.push_back({DT.Delete, ParentBB, UnswitchedExitBB});
1000     DTUpdates.push_back({DT.Insert, OldPH, UnswitchedExitBB});
1001   }
1002   for (auto SplitUnswitchedPair : SplitExitBBMap) {
1003     DTUpdates.push_back({DT.Delete, ParentBB, SplitUnswitchedPair.first});
1004     DTUpdates.push_back({DT.Insert, OldPH, SplitUnswitchedPair.second});
1005   }
1006 
1007   if (MSSAU) {
1008     MSSAU->applyUpdates(DTUpdates, DT, /*UpdateDT=*/true);
1009     if (VerifyMemorySSA)
1010       MSSAU->getMemorySSA()->verifyMemorySSA();
1011   } else {
1012     DT.applyUpdates(DTUpdates);
1013   }
1014 
1015   assert(DT.verify(DominatorTree::VerificationLevel::Fast));
1016 
1017   // We may have changed the nesting relationship for this loop so hoist it to
1018   // its correct parent if needed.
1019   hoistLoopToNewParent(L, *NewPH, DT, LI, MSSAU, SE);
1020 
1021   if (MSSAU && VerifyMemorySSA)
1022     MSSAU->getMemorySSA()->verifyMemorySSA();
1023 
1024   ++NumTrivial;
1025   ++NumSwitches;
1026   LLVM_DEBUG(dbgs() << "    done: unswitching trivial switch...\n");
1027   return true;
1028 }
1029 
1030 /// This routine scans the loop to find a branch or switch which occurs before
1031 /// any side effects occur. These can potentially be unswitched without
1032 /// duplicating the loop. If a branch or switch is successfully unswitched the
1033 /// scanning continues to see if subsequent branches or switches have become
1034 /// trivial. Once all trivial candidates have been unswitched, this routine
1035 /// returns.
1036 ///
1037 /// The return value indicates whether anything was unswitched (and therefore
1038 /// changed).
1039 ///
1040 /// If `SE` is not null, it will be updated based on the potential loop SCEVs
1041 /// invalidated by this.
1042 static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT,
1043                                          LoopInfo &LI, ScalarEvolution *SE,
1044                                          MemorySSAUpdater *MSSAU) {
1045   bool Changed = false;
1046 
1047   // If loop header has only one reachable successor we should keep looking for
1048   // trivial condition candidates in the successor as well. An alternative is
1049   // to constant fold conditions and merge successors into loop header (then we
1050   // only need to check header's terminator). The reason for not doing this in
1051   // LoopUnswitch pass is that it could potentially break LoopPassManager's
1052   // invariants. Folding dead branches could either eliminate the current loop
1053   // or make other loops unreachable. LCSSA form might also not be preserved
1054   // after deleting branches. The following code keeps traversing loop header's
1055   // successors until it finds the trivial condition candidate (condition that
1056   // is not a constant). Since unswitching generates branches with constant
1057   // conditions, this scenario could be very common in practice.
1058   BasicBlock *CurrentBB = L.getHeader();
1059   SmallPtrSet<BasicBlock *, 8> Visited;
1060   Visited.insert(CurrentBB);
1061   do {
1062     // Check if there are any side-effecting instructions (e.g. stores, calls,
1063     // volatile loads) in the part of the loop that the code *would* execute
1064     // without unswitching.
1065     if (MSSAU) // Possible early exit with MSSA
1066       if (auto *Defs = MSSAU->getMemorySSA()->getBlockDefs(CurrentBB))
1067         if (!isa<MemoryPhi>(*Defs->begin()) || (++Defs->begin() != Defs->end()))
1068           return Changed;
1069     if (llvm::any_of(*CurrentBB,
1070                      [](Instruction &I) { return I.mayHaveSideEffects(); }))
1071       return Changed;
1072 
1073     Instruction *CurrentTerm = CurrentBB->getTerminator();
1074 
1075     if (auto *SI = dyn_cast<SwitchInst>(CurrentTerm)) {
1076       // Don't bother trying to unswitch past a switch with a constant
1077       // condition. This should be removed prior to running this pass by
1078       // simplifycfg.
1079       if (isa<Constant>(SI->getCondition()))
1080         return Changed;
1081 
1082       if (!unswitchTrivialSwitch(L, *SI, DT, LI, SE, MSSAU))
1083         // Couldn't unswitch this one so we're done.
1084         return Changed;
1085 
1086       // Mark that we managed to unswitch something.
1087       Changed = true;
1088 
1089       // If unswitching turned the terminator into an unconditional branch then
1090       // we can continue. The unswitching logic specifically works to fold any
1091       // cases it can into an unconditional branch to make it easier to
1092       // recognize here.
1093       auto *BI = dyn_cast<BranchInst>(CurrentBB->getTerminator());
1094       if (!BI || BI->isConditional())
1095         return Changed;
1096 
1097       CurrentBB = BI->getSuccessor(0);
1098       continue;
1099     }
1100 
1101     auto *BI = dyn_cast<BranchInst>(CurrentTerm);
1102     if (!BI)
1103       // We do not understand other terminator instructions.
1104       return Changed;
1105 
1106     // Don't bother trying to unswitch past an unconditional branch or a branch
1107     // with a constant value. These should be removed by simplifycfg prior to
1108     // running this pass.
1109     if (!BI->isConditional() ||
1110         isa<Constant>(skipTrivialSelect(BI->getCondition())))
1111       return Changed;
1112 
1113     // Found a trivial condition candidate: non-foldable conditional branch. If
1114     // we fail to unswitch this, we can't do anything else that is trivial.
1115     if (!unswitchTrivialBranch(L, *BI, DT, LI, SE, MSSAU))
1116       return Changed;
1117 
1118     // Mark that we managed to unswitch something.
1119     Changed = true;
1120 
1121     // If we only unswitched some of the conditions feeding the branch, we won't
1122     // have collapsed it to a single successor.
1123     BI = cast<BranchInst>(CurrentBB->getTerminator());
1124     if (BI->isConditional())
1125       return Changed;
1126 
1127     // Follow the newly unconditional branch into its successor.
1128     CurrentBB = BI->getSuccessor(0);
1129 
1130     // When continuing, if we exit the loop or reach a previous visited block,
1131     // then we can not reach any trivial condition candidates (unfoldable
1132     // branch instructions or switch instructions) and no unswitch can happen.
1133   } while (L.contains(CurrentBB) && Visited.insert(CurrentBB).second);
1134 
1135   return Changed;
1136 }
1137 
1138 /// Build the cloned blocks for an unswitched copy of the given loop.
1139 ///
1140 /// The cloned blocks are inserted before the loop preheader (`LoopPH`) and
1141 /// after the split block (`SplitBB`) that will be used to select between the
1142 /// cloned and original loop.
1143 ///
1144 /// This routine handles cloning all of the necessary loop blocks and exit
1145 /// blocks including rewriting their instructions and the relevant PHI nodes.
1146 /// Any loop blocks or exit blocks which are dominated by a different successor
1147 /// than the one for this clone of the loop blocks can be trivially skipped. We
1148 /// use the `DominatingSucc` map to determine whether a block satisfies that
1149 /// property with a simple map lookup.
1150 ///
1151 /// It also correctly creates the unconditional branch in the cloned
1152 /// unswitched parent block to only point at the unswitched successor.
1153 ///
1154 /// This does not handle most of the necessary updates to `LoopInfo`. Only exit
1155 /// block splitting is correctly reflected in `LoopInfo`, essentially all of
1156 /// the cloned blocks (and their loops) are left without full `LoopInfo`
1157 /// updates. This also doesn't fully update `DominatorTree`. It adds the cloned
1158 /// blocks to them but doesn't create the cloned `DominatorTree` structure and
1159 /// instead the caller must recompute an accurate DT. It *does* correctly
1160 /// update the `AssumptionCache` provided in `AC`.
1161 static BasicBlock *buildClonedLoopBlocks(
1162     Loop &L, BasicBlock *LoopPH, BasicBlock *SplitBB,
1163     ArrayRef<BasicBlock *> ExitBlocks, BasicBlock *ParentBB,
1164     BasicBlock *UnswitchedSuccBB, BasicBlock *ContinueSuccBB,
1165     const SmallDenseMap<BasicBlock *, BasicBlock *, 16> &DominatingSucc,
1166     ValueToValueMapTy &VMap,
1167     SmallVectorImpl<DominatorTree::UpdateType> &DTUpdates, AssumptionCache &AC,
1168     DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU,
1169     ScalarEvolution *SE) {
1170   SmallVector<BasicBlock *, 4> NewBlocks;
1171   NewBlocks.reserve(L.getNumBlocks() + ExitBlocks.size());
1172 
1173   // We will need to clone a bunch of blocks, wrap up the clone operation in
1174   // a helper.
1175   auto CloneBlock = [&](BasicBlock *OldBB) {
1176     // Clone the basic block and insert it before the new preheader.
1177     BasicBlock *NewBB = CloneBasicBlock(OldBB, VMap, ".us", OldBB->getParent());
1178     NewBB->moveBefore(LoopPH);
1179 
1180     // Record this block and the mapping.
1181     NewBlocks.push_back(NewBB);
1182     VMap[OldBB] = NewBB;
1183 
1184     return NewBB;
1185   };
1186 
1187   // We skip cloning blocks when they have a dominating succ that is not the
1188   // succ we are cloning for.
1189   auto SkipBlock = [&](BasicBlock *BB) {
1190     auto It = DominatingSucc.find(BB);
1191     return It != DominatingSucc.end() && It->second != UnswitchedSuccBB;
1192   };
1193 
1194   // First, clone the preheader.
1195   auto *ClonedPH = CloneBlock(LoopPH);
1196 
1197   // Then clone all the loop blocks, skipping the ones that aren't necessary.
1198   for (auto *LoopBB : L.blocks())
1199     if (!SkipBlock(LoopBB))
1200       CloneBlock(LoopBB);
1201 
1202   // Split all the loop exit edges so that when we clone the exit blocks, if
1203   // any of the exit blocks are *also* a preheader for some other loop, we
1204   // don't create multiple predecessors entering the loop header.
1205   for (auto *ExitBB : ExitBlocks) {
1206     if (SkipBlock(ExitBB))
1207       continue;
1208 
1209     // When we are going to clone an exit, we don't need to clone all the
1210     // instructions in the exit block and we want to ensure we have an easy
1211     // place to merge the CFG, so split the exit first. This is always safe to
1212     // do because there cannot be any non-loop predecessors of a loop exit in
1213     // loop simplified form.
1214     auto *MergeBB = SplitBlock(ExitBB, ExitBB->begin(), &DT, &LI, MSSAU);
1215 
1216     // Rearrange the names to make it easier to write test cases by having the
1217     // exit block carry the suffix rather than the merge block carrying the
1218     // suffix.
1219     MergeBB->takeName(ExitBB);
1220     ExitBB->setName(Twine(MergeBB->getName()) + ".split");
1221 
1222     // Now clone the original exit block.
1223     auto *ClonedExitBB = CloneBlock(ExitBB);
1224     assert(ClonedExitBB->getTerminator()->getNumSuccessors() == 1 &&
1225            "Exit block should have been split to have one successor!");
1226     assert(ClonedExitBB->getTerminator()->getSuccessor(0) == MergeBB &&
1227            "Cloned exit block has the wrong successor!");
1228 
1229     // Remap any cloned instructions and create a merge phi node for them.
1230     for (auto ZippedInsts : llvm::zip_first(
1231              llvm::make_range(ExitBB->begin(), std::prev(ExitBB->end())),
1232              llvm::make_range(ClonedExitBB->begin(),
1233                               std::prev(ClonedExitBB->end())))) {
1234       Instruction &I = std::get<0>(ZippedInsts);
1235       Instruction &ClonedI = std::get<1>(ZippedInsts);
1236 
1237       // The only instructions in the exit block should be PHI nodes and
1238       // potentially a landing pad.
1239       assert(
1240           (isa<PHINode>(I) || isa<LandingPadInst>(I) || isa<CatchPadInst>(I)) &&
1241           "Bad instruction in exit block!");
1242       // We should have a value map between the instruction and its clone.
1243       assert(VMap.lookup(&I) == &ClonedI && "Mismatch in the value map!");
1244 
1245       // Forget SCEVs based on exit phis in case SCEV looked through the phi.
1246       if (SE && isa<PHINode>(I))
1247         SE->forgetValue(&I);
1248 
1249       auto *MergePN =
1250           PHINode::Create(I.getType(), /*NumReservedValues*/ 2, ".us-phi");
1251       MergePN->insertBefore(MergeBB->getFirstInsertionPt());
1252       I.replaceAllUsesWith(MergePN);
1253       MergePN->addIncoming(&I, ExitBB);
1254       MergePN->addIncoming(&ClonedI, ClonedExitBB);
1255     }
1256   }
1257 
1258   // Rewrite the instructions in the cloned blocks to refer to the instructions
1259   // in the cloned blocks. We have to do this as a second pass so that we have
1260   // everything available. Also, we have inserted new instructions which may
1261   // include assume intrinsics, so we update the assumption cache while
1262   // processing this.
1263   for (auto *ClonedBB : NewBlocks)
1264     for (Instruction &I : *ClonedBB) {
1265       RemapInstruction(&I, VMap,
1266                        RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
1267       if (auto *II = dyn_cast<AssumeInst>(&I))
1268         AC.registerAssumption(II);
1269     }
1270 
1271   // Update any PHI nodes in the cloned successors of the skipped blocks to not
1272   // have spurious incoming values.
1273   for (auto *LoopBB : L.blocks())
1274     if (SkipBlock(LoopBB))
1275       for (auto *SuccBB : successors(LoopBB))
1276         if (auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.lookup(SuccBB)))
1277           for (PHINode &PN : ClonedSuccBB->phis())
1278             PN.removeIncomingValue(LoopBB, /*DeletePHIIfEmpty*/ false);
1279 
1280   // Remove the cloned parent as a predecessor of any successor we ended up
1281   // cloning other than the unswitched one.
1282   auto *ClonedParentBB = cast<BasicBlock>(VMap.lookup(ParentBB));
1283   for (auto *SuccBB : successors(ParentBB)) {
1284     if (SuccBB == UnswitchedSuccBB)
1285       continue;
1286 
1287     auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.lookup(SuccBB));
1288     if (!ClonedSuccBB)
1289       continue;
1290 
1291     ClonedSuccBB->removePredecessor(ClonedParentBB,
1292                                     /*KeepOneInputPHIs*/ true);
1293   }
1294 
1295   // Replace the cloned branch with an unconditional branch to the cloned
1296   // unswitched successor.
1297   auto *ClonedSuccBB = cast<BasicBlock>(VMap.lookup(UnswitchedSuccBB));
1298   Instruction *ClonedTerminator = ClonedParentBB->getTerminator();
1299   // Trivial Simplification. If Terminator is a conditional branch and
1300   // condition becomes dead - erase it.
1301   Value *ClonedConditionToErase = nullptr;
1302   if (auto *BI = dyn_cast<BranchInst>(ClonedTerminator))
1303     ClonedConditionToErase = BI->getCondition();
1304   else if (auto *SI = dyn_cast<SwitchInst>(ClonedTerminator))
1305     ClonedConditionToErase = SI->getCondition();
1306 
1307   ClonedTerminator->eraseFromParent();
1308   BranchInst::Create(ClonedSuccBB, ClonedParentBB);
1309 
1310   if (ClonedConditionToErase)
1311     RecursivelyDeleteTriviallyDeadInstructions(ClonedConditionToErase, nullptr,
1312                                                MSSAU);
1313 
1314   // If there are duplicate entries in the PHI nodes because of multiple edges
1315   // to the unswitched successor, we need to nuke all but one as we replaced it
1316   // with a direct branch.
1317   for (PHINode &PN : ClonedSuccBB->phis()) {
1318     bool Found = false;
1319     // Loop over the incoming operands backwards so we can easily delete as we
1320     // go without invalidating the index.
1321     for (int i = PN.getNumOperands() - 1; i >= 0; --i) {
1322       if (PN.getIncomingBlock(i) != ClonedParentBB)
1323         continue;
1324       if (!Found) {
1325         Found = true;
1326         continue;
1327       }
1328       PN.removeIncomingValue(i, /*DeletePHIIfEmpty*/ false);
1329     }
1330   }
1331 
1332   // Record the domtree updates for the new blocks.
1333   SmallPtrSet<BasicBlock *, 4> SuccSet;
1334   for (auto *ClonedBB : NewBlocks) {
1335     for (auto *SuccBB : successors(ClonedBB))
1336       if (SuccSet.insert(SuccBB).second)
1337         DTUpdates.push_back({DominatorTree::Insert, ClonedBB, SuccBB});
1338     SuccSet.clear();
1339   }
1340 
1341   return ClonedPH;
1342 }
1343 
1344 /// Recursively clone the specified loop and all of its children.
1345 ///
1346 /// The target parent loop for the clone should be provided, or can be null if
1347 /// the clone is a top-level loop. While cloning, all the blocks are mapped
1348 /// with the provided value map. The entire original loop must be present in
1349 /// the value map. The cloned loop is returned.
1350 static Loop *cloneLoopNest(Loop &OrigRootL, Loop *RootParentL,
1351                            const ValueToValueMapTy &VMap, LoopInfo &LI) {
1352   auto AddClonedBlocksToLoop = [&](Loop &OrigL, Loop &ClonedL) {
1353     assert(ClonedL.getBlocks().empty() && "Must start with an empty loop!");
1354     ClonedL.reserveBlocks(OrigL.getNumBlocks());
1355     for (auto *BB : OrigL.blocks()) {
1356       auto *ClonedBB = cast<BasicBlock>(VMap.lookup(BB));
1357       ClonedL.addBlockEntry(ClonedBB);
1358       if (LI.getLoopFor(BB) == &OrigL)
1359         LI.changeLoopFor(ClonedBB, &ClonedL);
1360     }
1361   };
1362 
1363   // We specially handle the first loop because it may get cloned into
1364   // a different parent and because we most commonly are cloning leaf loops.
1365   Loop *ClonedRootL = LI.AllocateLoop();
1366   if (RootParentL)
1367     RootParentL->addChildLoop(ClonedRootL);
1368   else
1369     LI.addTopLevelLoop(ClonedRootL);
1370   AddClonedBlocksToLoop(OrigRootL, *ClonedRootL);
1371 
1372   if (OrigRootL.isInnermost())
1373     return ClonedRootL;
1374 
1375   // If we have a nest, we can quickly clone the entire loop nest using an
1376   // iterative approach because it is a tree. We keep the cloned parent in the
1377   // data structure to avoid repeatedly querying through a map to find it.
1378   SmallVector<std::pair<Loop *, Loop *>, 16> LoopsToClone;
1379   // Build up the loops to clone in reverse order as we'll clone them from the
1380   // back.
1381   for (Loop *ChildL : llvm::reverse(OrigRootL))
1382     LoopsToClone.push_back({ClonedRootL, ChildL});
1383   do {
1384     Loop *ClonedParentL, *L;
1385     std::tie(ClonedParentL, L) = LoopsToClone.pop_back_val();
1386     Loop *ClonedL = LI.AllocateLoop();
1387     ClonedParentL->addChildLoop(ClonedL);
1388     AddClonedBlocksToLoop(*L, *ClonedL);
1389     for (Loop *ChildL : llvm::reverse(*L))
1390       LoopsToClone.push_back({ClonedL, ChildL});
1391   } while (!LoopsToClone.empty());
1392 
1393   return ClonedRootL;
1394 }
1395 
1396 /// Build the cloned loops of an original loop from unswitching.
1397 ///
1398 /// Because unswitching simplifies the CFG of the loop, this isn't a trivial
1399 /// operation. We need to re-verify that there even is a loop (as the backedge
1400 /// may not have been cloned), and even if there are remaining backedges the
1401 /// backedge set may be different. However, we know that each child loop is
1402 /// undisturbed, we only need to find where to place each child loop within
1403 /// either any parent loop or within a cloned version of the original loop.
1404 ///
1405 /// Because child loops may end up cloned outside of any cloned version of the
1406 /// original loop, multiple cloned sibling loops may be created. All of them
1407 /// are returned so that the newly introduced loop nest roots can be
1408 /// identified.
1409 static void buildClonedLoops(Loop &OrigL, ArrayRef<BasicBlock *> ExitBlocks,
1410                              const ValueToValueMapTy &VMap, LoopInfo &LI,
1411                              SmallVectorImpl<Loop *> &NonChildClonedLoops) {
1412   Loop *ClonedL = nullptr;
1413 
1414   auto *OrigPH = OrigL.getLoopPreheader();
1415   auto *OrigHeader = OrigL.getHeader();
1416 
1417   auto *ClonedPH = cast<BasicBlock>(VMap.lookup(OrigPH));
1418   auto *ClonedHeader = cast<BasicBlock>(VMap.lookup(OrigHeader));
1419 
1420   // We need to know the loops of the cloned exit blocks to even compute the
1421   // accurate parent loop. If we only clone exits to some parent of the
1422   // original parent, we want to clone into that outer loop. We also keep track
1423   // of the loops that our cloned exit blocks participate in.
1424   Loop *ParentL = nullptr;
1425   SmallVector<BasicBlock *, 4> ClonedExitsInLoops;
1426   SmallDenseMap<BasicBlock *, Loop *, 16> ExitLoopMap;
1427   ClonedExitsInLoops.reserve(ExitBlocks.size());
1428   for (auto *ExitBB : ExitBlocks)
1429     if (auto *ClonedExitBB = cast_or_null<BasicBlock>(VMap.lookup(ExitBB)))
1430       if (Loop *ExitL = LI.getLoopFor(ExitBB)) {
1431         ExitLoopMap[ClonedExitBB] = ExitL;
1432         ClonedExitsInLoops.push_back(ClonedExitBB);
1433         if (!ParentL || (ParentL != ExitL && ParentL->contains(ExitL)))
1434           ParentL = ExitL;
1435       }
1436   assert((!ParentL || ParentL == OrigL.getParentLoop() ||
1437           ParentL->contains(OrigL.getParentLoop())) &&
1438          "The computed parent loop should always contain (or be) the parent of "
1439          "the original loop.");
1440 
1441   // We build the set of blocks dominated by the cloned header from the set of
1442   // cloned blocks out of the original loop. While not all of these will
1443   // necessarily be in the cloned loop, it is enough to establish that they
1444   // aren't in unreachable cycles, etc.
1445   SmallSetVector<BasicBlock *, 16> ClonedLoopBlocks;
1446   for (auto *BB : OrigL.blocks())
1447     if (auto *ClonedBB = cast_or_null<BasicBlock>(VMap.lookup(BB)))
1448       ClonedLoopBlocks.insert(ClonedBB);
1449 
1450   // Rebuild the set of blocks that will end up in the cloned loop. We may have
1451   // skipped cloning some region of this loop which can in turn skip some of
1452   // the backedges so we have to rebuild the blocks in the loop based on the
1453   // backedges that remain after cloning.
1454   SmallVector<BasicBlock *, 16> Worklist;
1455   SmallPtrSet<BasicBlock *, 16> BlocksInClonedLoop;
1456   for (auto *Pred : predecessors(ClonedHeader)) {
1457     // The only possible non-loop header predecessor is the preheader because
1458     // we know we cloned the loop in simplified form.
1459     if (Pred == ClonedPH)
1460       continue;
1461 
1462     // Because the loop was in simplified form, the only non-loop predecessor
1463     // should be the preheader.
1464     assert(ClonedLoopBlocks.count(Pred) && "Found a predecessor of the loop "
1465                                            "header other than the preheader "
1466                                            "that is not part of the loop!");
1467 
1468     // Insert this block into the loop set and on the first visit (and if it
1469     // isn't the header we're currently walking) put it into the worklist to
1470     // recurse through.
1471     if (BlocksInClonedLoop.insert(Pred).second && Pred != ClonedHeader)
1472       Worklist.push_back(Pred);
1473   }
1474 
1475   // If we had any backedges then there *is* a cloned loop. Put the header into
1476   // the loop set and then walk the worklist backwards to find all the blocks
1477   // that remain within the loop after cloning.
1478   if (!BlocksInClonedLoop.empty()) {
1479     BlocksInClonedLoop.insert(ClonedHeader);
1480 
1481     while (!Worklist.empty()) {
1482       BasicBlock *BB = Worklist.pop_back_val();
1483       assert(BlocksInClonedLoop.count(BB) &&
1484              "Didn't put block into the loop set!");
1485 
1486       // Insert any predecessors that are in the possible set into the cloned
1487       // set, and if the insert is successful, add them to the worklist. Note
1488       // that we filter on the blocks that are definitely reachable via the
1489       // backedge to the loop header so we may prune out dead code within the
1490       // cloned loop.
1491       for (auto *Pred : predecessors(BB))
1492         if (ClonedLoopBlocks.count(Pred) &&
1493             BlocksInClonedLoop.insert(Pred).second)
1494           Worklist.push_back(Pred);
1495     }
1496 
1497     ClonedL = LI.AllocateLoop();
1498     if (ParentL) {
1499       ParentL->addBasicBlockToLoop(ClonedPH, LI);
1500       ParentL->addChildLoop(ClonedL);
1501     } else {
1502       LI.addTopLevelLoop(ClonedL);
1503     }
1504     NonChildClonedLoops.push_back(ClonedL);
1505 
1506     ClonedL->reserveBlocks(BlocksInClonedLoop.size());
1507     // We don't want to just add the cloned loop blocks based on how we
1508     // discovered them. The original order of blocks was carefully built in
1509     // a way that doesn't rely on predecessor ordering. Rather than re-invent
1510     // that logic, we just re-walk the original blocks (and those of the child
1511     // loops) and filter them as we add them into the cloned loop.
1512     for (auto *BB : OrigL.blocks()) {
1513       auto *ClonedBB = cast_or_null<BasicBlock>(VMap.lookup(BB));
1514       if (!ClonedBB || !BlocksInClonedLoop.count(ClonedBB))
1515         continue;
1516 
1517       // Directly add the blocks that are only in this loop.
1518       if (LI.getLoopFor(BB) == &OrigL) {
1519         ClonedL->addBasicBlockToLoop(ClonedBB, LI);
1520         continue;
1521       }
1522 
1523       // We want to manually add it to this loop and parents.
1524       // Registering it with LoopInfo will happen when we clone the top
1525       // loop for this block.
1526       for (Loop *PL = ClonedL; PL; PL = PL->getParentLoop())
1527         PL->addBlockEntry(ClonedBB);
1528     }
1529 
1530     // Now add each child loop whose header remains within the cloned loop. All
1531     // of the blocks within the loop must satisfy the same constraints as the
1532     // header so once we pass the header checks we can just clone the entire
1533     // child loop nest.
1534     for (Loop *ChildL : OrigL) {
1535       auto *ClonedChildHeader =
1536           cast_or_null<BasicBlock>(VMap.lookup(ChildL->getHeader()));
1537       if (!ClonedChildHeader || !BlocksInClonedLoop.count(ClonedChildHeader))
1538         continue;
1539 
1540 #ifndef NDEBUG
1541       // We should never have a cloned child loop header but fail to have
1542       // all of the blocks for that child loop.
1543       for (auto *ChildLoopBB : ChildL->blocks())
1544         assert(BlocksInClonedLoop.count(
1545                    cast<BasicBlock>(VMap.lookup(ChildLoopBB))) &&
1546                "Child cloned loop has a header within the cloned outer "
1547                "loop but not all of its blocks!");
1548 #endif
1549 
1550       cloneLoopNest(*ChildL, ClonedL, VMap, LI);
1551     }
1552   }
1553 
1554   // Now that we've handled all the components of the original loop that were
1555   // cloned into a new loop, we still need to handle anything from the original
1556   // loop that wasn't in a cloned loop.
1557 
1558   // Figure out what blocks are left to place within any loop nest containing
1559   // the unswitched loop. If we never formed a loop, the cloned PH is one of
1560   // them.
1561   SmallPtrSet<BasicBlock *, 16> UnloopedBlockSet;
1562   if (BlocksInClonedLoop.empty())
1563     UnloopedBlockSet.insert(ClonedPH);
1564   for (auto *ClonedBB : ClonedLoopBlocks)
1565     if (!BlocksInClonedLoop.count(ClonedBB))
1566       UnloopedBlockSet.insert(ClonedBB);
1567 
1568   // Copy the cloned exits and sort them in ascending loop depth, we'll work
1569   // backwards across these to process them inside out. The order shouldn't
1570   // matter as we're just trying to build up the map from inside-out; we use
1571   // the map in a more stably ordered way below.
1572   auto OrderedClonedExitsInLoops = ClonedExitsInLoops;
1573   llvm::sort(OrderedClonedExitsInLoops, [&](BasicBlock *LHS, BasicBlock *RHS) {
1574     return ExitLoopMap.lookup(LHS)->getLoopDepth() <
1575            ExitLoopMap.lookup(RHS)->getLoopDepth();
1576   });
1577 
1578   // Populate the existing ExitLoopMap with everything reachable from each
1579   // exit, starting from the inner most exit.
1580   while (!UnloopedBlockSet.empty() && !OrderedClonedExitsInLoops.empty()) {
1581     assert(Worklist.empty() && "Didn't clear worklist!");
1582 
1583     BasicBlock *ExitBB = OrderedClonedExitsInLoops.pop_back_val();
1584     Loop *ExitL = ExitLoopMap.lookup(ExitBB);
1585 
1586     // Walk the CFG back until we hit the cloned PH adding everything reachable
1587     // and in the unlooped set to this exit block's loop.
1588     Worklist.push_back(ExitBB);
1589     do {
1590       BasicBlock *BB = Worklist.pop_back_val();
1591       // We can stop recursing at the cloned preheader (if we get there).
1592       if (BB == ClonedPH)
1593         continue;
1594 
1595       for (BasicBlock *PredBB : predecessors(BB)) {
1596         // If this pred has already been moved to our set or is part of some
1597         // (inner) loop, no update needed.
1598         if (!UnloopedBlockSet.erase(PredBB)) {
1599           assert(
1600               (BlocksInClonedLoop.count(PredBB) || ExitLoopMap.count(PredBB)) &&
1601               "Predecessor not mapped to a loop!");
1602           continue;
1603         }
1604 
1605         // We just insert into the loop set here. We'll add these blocks to the
1606         // exit loop after we build up the set in an order that doesn't rely on
1607         // predecessor order (which in turn relies on use list order).
1608         bool Inserted = ExitLoopMap.insert({PredBB, ExitL}).second;
1609         (void)Inserted;
1610         assert(Inserted && "Should only visit an unlooped block once!");
1611 
1612         // And recurse through to its predecessors.
1613         Worklist.push_back(PredBB);
1614       }
1615     } while (!Worklist.empty());
1616   }
1617 
1618   // Now that the ExitLoopMap gives as  mapping for all the non-looping cloned
1619   // blocks to their outer loops, walk the cloned blocks and the cloned exits
1620   // in their original order adding them to the correct loop.
1621 
1622   // We need a stable insertion order. We use the order of the original loop
1623   // order and map into the correct parent loop.
1624   for (auto *BB : llvm::concat<BasicBlock *const>(
1625            ArrayRef(ClonedPH), ClonedLoopBlocks, ClonedExitsInLoops))
1626     if (Loop *OuterL = ExitLoopMap.lookup(BB))
1627       OuterL->addBasicBlockToLoop(BB, LI);
1628 
1629 #ifndef NDEBUG
1630   for (auto &BBAndL : ExitLoopMap) {
1631     auto *BB = BBAndL.first;
1632     auto *OuterL = BBAndL.second;
1633     assert(LI.getLoopFor(BB) == OuterL &&
1634            "Failed to put all blocks into outer loops!");
1635   }
1636 #endif
1637 
1638   // Now that all the blocks are placed into the correct containing loop in the
1639   // absence of child loops, find all the potentially cloned child loops and
1640   // clone them into whatever outer loop we placed their header into.
1641   for (Loop *ChildL : OrigL) {
1642     auto *ClonedChildHeader =
1643         cast_or_null<BasicBlock>(VMap.lookup(ChildL->getHeader()));
1644     if (!ClonedChildHeader || BlocksInClonedLoop.count(ClonedChildHeader))
1645       continue;
1646 
1647 #ifndef NDEBUG
1648     for (auto *ChildLoopBB : ChildL->blocks())
1649       assert(VMap.count(ChildLoopBB) &&
1650              "Cloned a child loop header but not all of that loops blocks!");
1651 #endif
1652 
1653     NonChildClonedLoops.push_back(cloneLoopNest(
1654         *ChildL, ExitLoopMap.lookup(ClonedChildHeader), VMap, LI));
1655   }
1656 }
1657 
1658 static void
1659 deleteDeadClonedBlocks(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
1660                        ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps,
1661                        DominatorTree &DT, MemorySSAUpdater *MSSAU) {
1662   // Find all the dead clones, and remove them from their successors.
1663   SmallVector<BasicBlock *, 16> DeadBlocks;
1664   for (BasicBlock *BB : llvm::concat<BasicBlock *const>(L.blocks(), ExitBlocks))
1665     for (const auto &VMap : VMaps)
1666       if (BasicBlock *ClonedBB = cast_or_null<BasicBlock>(VMap->lookup(BB)))
1667         if (!DT.isReachableFromEntry(ClonedBB)) {
1668           for (BasicBlock *SuccBB : successors(ClonedBB))
1669             SuccBB->removePredecessor(ClonedBB);
1670           DeadBlocks.push_back(ClonedBB);
1671         }
1672 
1673   // Remove all MemorySSA in the dead blocks
1674   if (MSSAU) {
1675     SmallSetVector<BasicBlock *, 8> DeadBlockSet(DeadBlocks.begin(),
1676                                                  DeadBlocks.end());
1677     MSSAU->removeBlocks(DeadBlockSet);
1678   }
1679 
1680   // Drop any remaining references to break cycles.
1681   for (BasicBlock *BB : DeadBlocks)
1682     BB->dropAllReferences();
1683   // Erase them from the IR.
1684   for (BasicBlock *BB : DeadBlocks)
1685     BB->eraseFromParent();
1686 }
1687 
1688 static void
1689 deleteDeadBlocksFromLoop(Loop &L,
1690                          SmallVectorImpl<BasicBlock *> &ExitBlocks,
1691                          DominatorTree &DT, LoopInfo &LI,
1692                          MemorySSAUpdater *MSSAU,
1693                          ScalarEvolution *SE,
1694                          function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
1695   // Find all the dead blocks tied to this loop, and remove them from their
1696   // successors.
1697   SmallSetVector<BasicBlock *, 8> DeadBlockSet;
1698 
1699   // Start with loop/exit blocks and get a transitive closure of reachable dead
1700   // blocks.
1701   SmallVector<BasicBlock *, 16> DeathCandidates(ExitBlocks.begin(),
1702                                                 ExitBlocks.end());
1703   DeathCandidates.append(L.blocks().begin(), L.blocks().end());
1704   while (!DeathCandidates.empty()) {
1705     auto *BB = DeathCandidates.pop_back_val();
1706     if (!DeadBlockSet.count(BB) && !DT.isReachableFromEntry(BB)) {
1707       for (BasicBlock *SuccBB : successors(BB)) {
1708         SuccBB->removePredecessor(BB);
1709         DeathCandidates.push_back(SuccBB);
1710       }
1711       DeadBlockSet.insert(BB);
1712     }
1713   }
1714 
1715   // Remove all MemorySSA in the dead blocks
1716   if (MSSAU)
1717     MSSAU->removeBlocks(DeadBlockSet);
1718 
1719   // Filter out the dead blocks from the exit blocks list so that it can be
1720   // used in the caller.
1721   llvm::erase_if(ExitBlocks,
1722                  [&](BasicBlock *BB) { return DeadBlockSet.count(BB); });
1723 
1724   // Walk from this loop up through its parents removing all of the dead blocks.
1725   for (Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop()) {
1726     for (auto *BB : DeadBlockSet)
1727       ParentL->getBlocksSet().erase(BB);
1728     llvm::erase_if(ParentL->getBlocksVector(),
1729                    [&](BasicBlock *BB) { return DeadBlockSet.count(BB); });
1730   }
1731 
1732   // Now delete the dead child loops. This raw delete will clear them
1733   // recursively.
1734   llvm::erase_if(L.getSubLoopsVector(), [&](Loop *ChildL) {
1735     if (!DeadBlockSet.count(ChildL->getHeader()))
1736       return false;
1737 
1738     assert(llvm::all_of(ChildL->blocks(),
1739                         [&](BasicBlock *ChildBB) {
1740                           return DeadBlockSet.count(ChildBB);
1741                         }) &&
1742            "If the child loop header is dead all blocks in the child loop must "
1743            "be dead as well!");
1744     DestroyLoopCB(*ChildL, ChildL->getName());
1745     if (SE)
1746       SE->forgetBlockAndLoopDispositions();
1747     LI.destroy(ChildL);
1748     return true;
1749   });
1750 
1751   // Remove the loop mappings for the dead blocks and drop all the references
1752   // from these blocks to others to handle cyclic references as we start
1753   // deleting the blocks themselves.
1754   for (auto *BB : DeadBlockSet) {
1755     // Check that the dominator tree has already been updated.
1756     assert(!DT.getNode(BB) && "Should already have cleared domtree!");
1757     LI.changeLoopFor(BB, nullptr);
1758     // Drop all uses of the instructions to make sure we won't have dangling
1759     // uses in other blocks.
1760     for (auto &I : *BB)
1761       if (!I.use_empty())
1762         I.replaceAllUsesWith(PoisonValue::get(I.getType()));
1763     BB->dropAllReferences();
1764   }
1765 
1766   // Actually delete the blocks now that they've been fully unhooked from the
1767   // IR.
1768   for (auto *BB : DeadBlockSet)
1769     BB->eraseFromParent();
1770 }
1771 
1772 /// Recompute the set of blocks in a loop after unswitching.
1773 ///
1774 /// This walks from the original headers predecessors to rebuild the loop. We
1775 /// take advantage of the fact that new blocks can't have been added, and so we
1776 /// filter by the original loop's blocks. This also handles potentially
1777 /// unreachable code that we don't want to explore but might be found examining
1778 /// the predecessors of the header.
1779 ///
1780 /// If the original loop is no longer a loop, this will return an empty set. If
1781 /// it remains a loop, all the blocks within it will be added to the set
1782 /// (including those blocks in inner loops).
1783 static SmallPtrSet<const BasicBlock *, 16> recomputeLoopBlockSet(Loop &L,
1784                                                                  LoopInfo &LI) {
1785   SmallPtrSet<const BasicBlock *, 16> LoopBlockSet;
1786 
1787   auto *PH = L.getLoopPreheader();
1788   auto *Header = L.getHeader();
1789 
1790   // A worklist to use while walking backwards from the header.
1791   SmallVector<BasicBlock *, 16> Worklist;
1792 
1793   // First walk the predecessors of the header to find the backedges. This will
1794   // form the basis of our walk.
1795   for (auto *Pred : predecessors(Header)) {
1796     // Skip the preheader.
1797     if (Pred == PH)
1798       continue;
1799 
1800     // Because the loop was in simplified form, the only non-loop predecessor
1801     // is the preheader.
1802     assert(L.contains(Pred) && "Found a predecessor of the loop header other "
1803                                "than the preheader that is not part of the "
1804                                "loop!");
1805 
1806     // Insert this block into the loop set and on the first visit and, if it
1807     // isn't the header we're currently walking, put it into the worklist to
1808     // recurse through.
1809     if (LoopBlockSet.insert(Pred).second && Pred != Header)
1810       Worklist.push_back(Pred);
1811   }
1812 
1813   // If no backedges were found, we're done.
1814   if (LoopBlockSet.empty())
1815     return LoopBlockSet;
1816 
1817   // We found backedges, recurse through them to identify the loop blocks.
1818   while (!Worklist.empty()) {
1819     BasicBlock *BB = Worklist.pop_back_val();
1820     assert(LoopBlockSet.count(BB) && "Didn't put block into the loop set!");
1821 
1822     // No need to walk past the header.
1823     if (BB == Header)
1824       continue;
1825 
1826     // Because we know the inner loop structure remains valid we can use the
1827     // loop structure to jump immediately across the entire nested loop.
1828     // Further, because it is in loop simplified form, we can directly jump
1829     // to its preheader afterward.
1830     if (Loop *InnerL = LI.getLoopFor(BB))
1831       if (InnerL != &L) {
1832         assert(L.contains(InnerL) &&
1833                "Should not reach a loop *outside* this loop!");
1834         // The preheader is the only possible predecessor of the loop so
1835         // insert it into the set and check whether it was already handled.
1836         auto *InnerPH = InnerL->getLoopPreheader();
1837         assert(L.contains(InnerPH) && "Cannot contain an inner loop block "
1838                                       "but not contain the inner loop "
1839                                       "preheader!");
1840         if (!LoopBlockSet.insert(InnerPH).second)
1841           // The only way to reach the preheader is through the loop body
1842           // itself so if it has been visited the loop is already handled.
1843           continue;
1844 
1845         // Insert all of the blocks (other than those already present) into
1846         // the loop set. We expect at least the block that led us to find the
1847         // inner loop to be in the block set, but we may also have other loop
1848         // blocks if they were already enqueued as predecessors of some other
1849         // outer loop block.
1850         for (auto *InnerBB : InnerL->blocks()) {
1851           if (InnerBB == BB) {
1852             assert(LoopBlockSet.count(InnerBB) &&
1853                    "Block should already be in the set!");
1854             continue;
1855           }
1856 
1857           LoopBlockSet.insert(InnerBB);
1858         }
1859 
1860         // Add the preheader to the worklist so we will continue past the
1861         // loop body.
1862         Worklist.push_back(InnerPH);
1863         continue;
1864       }
1865 
1866     // Insert any predecessors that were in the original loop into the new
1867     // set, and if the insert is successful, add them to the worklist.
1868     for (auto *Pred : predecessors(BB))
1869       if (L.contains(Pred) && LoopBlockSet.insert(Pred).second)
1870         Worklist.push_back(Pred);
1871   }
1872 
1873   assert(LoopBlockSet.count(Header) && "Cannot fail to add the header!");
1874 
1875   // We've found all the blocks participating in the loop, return our completed
1876   // set.
1877   return LoopBlockSet;
1878 }
1879 
1880 /// Rebuild a loop after unswitching removes some subset of blocks and edges.
1881 ///
1882 /// The removal may have removed some child loops entirely but cannot have
1883 /// disturbed any remaining child loops. However, they may need to be hoisted
1884 /// to the parent loop (or to be top-level loops). The original loop may be
1885 /// completely removed.
1886 ///
1887 /// The sibling loops resulting from this update are returned. If the original
1888 /// loop remains a valid loop, it will be the first entry in this list with all
1889 /// of the newly sibling loops following it.
1890 ///
1891 /// Returns true if the loop remains a loop after unswitching, and false if it
1892 /// is no longer a loop after unswitching (and should not continue to be
1893 /// referenced).
1894 static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
1895                                      LoopInfo &LI,
1896                                      SmallVectorImpl<Loop *> &HoistedLoops,
1897                                      ScalarEvolution *SE) {
1898   auto *PH = L.getLoopPreheader();
1899 
1900   // Compute the actual parent loop from the exit blocks. Because we may have
1901   // pruned some exits the loop may be different from the original parent.
1902   Loop *ParentL = nullptr;
1903   SmallVector<Loop *, 4> ExitLoops;
1904   SmallVector<BasicBlock *, 4> ExitsInLoops;
1905   ExitsInLoops.reserve(ExitBlocks.size());
1906   for (auto *ExitBB : ExitBlocks)
1907     if (Loop *ExitL = LI.getLoopFor(ExitBB)) {
1908       ExitLoops.push_back(ExitL);
1909       ExitsInLoops.push_back(ExitBB);
1910       if (!ParentL || (ParentL != ExitL && ParentL->contains(ExitL)))
1911         ParentL = ExitL;
1912     }
1913 
1914   // Recompute the blocks participating in this loop. This may be empty if it
1915   // is no longer a loop.
1916   auto LoopBlockSet = recomputeLoopBlockSet(L, LI);
1917 
1918   // If we still have a loop, we need to re-set the loop's parent as the exit
1919   // block set changing may have moved it within the loop nest. Note that this
1920   // can only happen when this loop has a parent as it can only hoist the loop
1921   // *up* the nest.
1922   if (!LoopBlockSet.empty() && L.getParentLoop() != ParentL) {
1923     // Remove this loop's (original) blocks from all of the intervening loops.
1924     for (Loop *IL = L.getParentLoop(); IL != ParentL;
1925          IL = IL->getParentLoop()) {
1926       IL->getBlocksSet().erase(PH);
1927       for (auto *BB : L.blocks())
1928         IL->getBlocksSet().erase(BB);
1929       llvm::erase_if(IL->getBlocksVector(), [&](BasicBlock *BB) {
1930         return BB == PH || L.contains(BB);
1931       });
1932     }
1933 
1934     LI.changeLoopFor(PH, ParentL);
1935     L.getParentLoop()->removeChildLoop(&L);
1936     if (ParentL)
1937       ParentL->addChildLoop(&L);
1938     else
1939       LI.addTopLevelLoop(&L);
1940   }
1941 
1942   // Now we update all the blocks which are no longer within the loop.
1943   auto &Blocks = L.getBlocksVector();
1944   auto BlocksSplitI =
1945       LoopBlockSet.empty()
1946           ? Blocks.begin()
1947           : std::stable_partition(
1948                 Blocks.begin(), Blocks.end(),
1949                 [&](BasicBlock *BB) { return LoopBlockSet.count(BB); });
1950 
1951   // Before we erase the list of unlooped blocks, build a set of them.
1952   SmallPtrSet<BasicBlock *, 16> UnloopedBlocks(BlocksSplitI, Blocks.end());
1953   if (LoopBlockSet.empty())
1954     UnloopedBlocks.insert(PH);
1955 
1956   // Now erase these blocks from the loop.
1957   for (auto *BB : make_range(BlocksSplitI, Blocks.end()))
1958     L.getBlocksSet().erase(BB);
1959   Blocks.erase(BlocksSplitI, Blocks.end());
1960 
1961   // Sort the exits in ascending loop depth, we'll work backwards across these
1962   // to process them inside out.
1963   llvm::stable_sort(ExitsInLoops, [&](BasicBlock *LHS, BasicBlock *RHS) {
1964     return LI.getLoopDepth(LHS) < LI.getLoopDepth(RHS);
1965   });
1966 
1967   // We'll build up a set for each exit loop.
1968   SmallPtrSet<BasicBlock *, 16> NewExitLoopBlocks;
1969   Loop *PrevExitL = L.getParentLoop(); // The deepest possible exit loop.
1970 
1971   auto RemoveUnloopedBlocksFromLoop =
1972       [](Loop &L, SmallPtrSetImpl<BasicBlock *> &UnloopedBlocks) {
1973         for (auto *BB : UnloopedBlocks)
1974           L.getBlocksSet().erase(BB);
1975         llvm::erase_if(L.getBlocksVector(), [&](BasicBlock *BB) {
1976           return UnloopedBlocks.count(BB);
1977         });
1978       };
1979 
1980   SmallVector<BasicBlock *, 16> Worklist;
1981   while (!UnloopedBlocks.empty() && !ExitsInLoops.empty()) {
1982     assert(Worklist.empty() && "Didn't clear worklist!");
1983     assert(NewExitLoopBlocks.empty() && "Didn't clear loop set!");
1984 
1985     // Grab the next exit block, in decreasing loop depth order.
1986     BasicBlock *ExitBB = ExitsInLoops.pop_back_val();
1987     Loop &ExitL = *LI.getLoopFor(ExitBB);
1988     assert(ExitL.contains(&L) && "Exit loop must contain the inner loop!");
1989 
1990     // Erase all of the unlooped blocks from the loops between the previous
1991     // exit loop and this exit loop. This works because the ExitInLoops list is
1992     // sorted in increasing order of loop depth and thus we visit loops in
1993     // decreasing order of loop depth.
1994     for (; PrevExitL != &ExitL; PrevExitL = PrevExitL->getParentLoop())
1995       RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
1996 
1997     // Walk the CFG back until we hit the cloned PH adding everything reachable
1998     // and in the unlooped set to this exit block's loop.
1999     Worklist.push_back(ExitBB);
2000     do {
2001       BasicBlock *BB = Worklist.pop_back_val();
2002       // We can stop recursing at the cloned preheader (if we get there).
2003       if (BB == PH)
2004         continue;
2005 
2006       for (BasicBlock *PredBB : predecessors(BB)) {
2007         // If this pred has already been moved to our set or is part of some
2008         // (inner) loop, no update needed.
2009         if (!UnloopedBlocks.erase(PredBB)) {
2010           assert((NewExitLoopBlocks.count(PredBB) ||
2011                   ExitL.contains(LI.getLoopFor(PredBB))) &&
2012                  "Predecessor not in a nested loop (or already visited)!");
2013           continue;
2014         }
2015 
2016         // We just insert into the loop set here. We'll add these blocks to the
2017         // exit loop after we build up the set in a deterministic order rather
2018         // than the predecessor-influenced visit order.
2019         bool Inserted = NewExitLoopBlocks.insert(PredBB).second;
2020         (void)Inserted;
2021         assert(Inserted && "Should only visit an unlooped block once!");
2022 
2023         // And recurse through to its predecessors.
2024         Worklist.push_back(PredBB);
2025       }
2026     } while (!Worklist.empty());
2027 
2028     // If blocks in this exit loop were directly part of the original loop (as
2029     // opposed to a child loop) update the map to point to this exit loop. This
2030     // just updates a map and so the fact that the order is unstable is fine.
2031     for (auto *BB : NewExitLoopBlocks)
2032       if (Loop *BBL = LI.getLoopFor(BB))
2033         if (BBL == &L || !L.contains(BBL))
2034           LI.changeLoopFor(BB, &ExitL);
2035 
2036     // We will remove the remaining unlooped blocks from this loop in the next
2037     // iteration or below.
2038     NewExitLoopBlocks.clear();
2039   }
2040 
2041   // Any remaining unlooped blocks are no longer part of any loop unless they
2042   // are part of some child loop.
2043   for (; PrevExitL; PrevExitL = PrevExitL->getParentLoop())
2044     RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks);
2045   for (auto *BB : UnloopedBlocks)
2046     if (Loop *BBL = LI.getLoopFor(BB))
2047       if (BBL == &L || !L.contains(BBL))
2048         LI.changeLoopFor(BB, nullptr);
2049 
2050   // Sink all the child loops whose headers are no longer in the loop set to
2051   // the parent (or to be top level loops). We reach into the loop and directly
2052   // update its subloop vector to make this batch update efficient.
2053   auto &SubLoops = L.getSubLoopsVector();
2054   auto SubLoopsSplitI =
2055       LoopBlockSet.empty()
2056           ? SubLoops.begin()
2057           : std::stable_partition(
2058                 SubLoops.begin(), SubLoops.end(), [&](Loop *SubL) {
2059                   return LoopBlockSet.count(SubL->getHeader());
2060                 });
2061   for (auto *HoistedL : make_range(SubLoopsSplitI, SubLoops.end())) {
2062     HoistedLoops.push_back(HoistedL);
2063     HoistedL->setParentLoop(nullptr);
2064 
2065     // To compute the new parent of this hoisted loop we look at where we
2066     // placed the preheader above. We can't lookup the header itself because we
2067     // retained the mapping from the header to the hoisted loop. But the
2068     // preheader and header should have the exact same new parent computed
2069     // based on the set of exit blocks from the original loop as the preheader
2070     // is a predecessor of the header and so reached in the reverse walk. And
2071     // because the loops were all in simplified form the preheader of the
2072     // hoisted loop can't be part of some *other* loop.
2073     if (auto *NewParentL = LI.getLoopFor(HoistedL->getLoopPreheader()))
2074       NewParentL->addChildLoop(HoistedL);
2075     else
2076       LI.addTopLevelLoop(HoistedL);
2077   }
2078   SubLoops.erase(SubLoopsSplitI, SubLoops.end());
2079 
2080   // Actually delete the loop if nothing remained within it.
2081   if (Blocks.empty()) {
2082     assert(SubLoops.empty() &&
2083            "Failed to remove all subloops from the original loop!");
2084     if (Loop *ParentL = L.getParentLoop())
2085       ParentL->removeChildLoop(llvm::find(*ParentL, &L));
2086     else
2087       LI.removeLoop(llvm::find(LI, &L));
2088     // markLoopAsDeleted for L should be triggered by the caller (it is typically
2089     // done by using the UnswitchCB callback).
2090     if (SE)
2091       SE->forgetBlockAndLoopDispositions();
2092     LI.destroy(&L);
2093     return false;
2094   }
2095 
2096   return true;
2097 }
2098 
2099 /// Helper to visit a dominator subtree, invoking a callable on each node.
2100 ///
2101 /// Returning false at any point will stop walking past that node of the tree.
2102 template <typename CallableT>
2103 void visitDomSubTree(DominatorTree &DT, BasicBlock *BB, CallableT Callable) {
2104   SmallVector<DomTreeNode *, 4> DomWorklist;
2105   DomWorklist.push_back(DT[BB]);
2106 #ifndef NDEBUG
2107   SmallPtrSet<DomTreeNode *, 4> Visited;
2108   Visited.insert(DT[BB]);
2109 #endif
2110   do {
2111     DomTreeNode *N = DomWorklist.pop_back_val();
2112 
2113     // Visit this node.
2114     if (!Callable(N->getBlock()))
2115       continue;
2116 
2117     // Accumulate the child nodes.
2118     for (DomTreeNode *ChildN : *N) {
2119       assert(Visited.insert(ChildN).second &&
2120              "Cannot visit a node twice when walking a tree!");
2121       DomWorklist.push_back(ChildN);
2122     }
2123   } while (!DomWorklist.empty());
2124 }
2125 
2126 static void unswitchNontrivialInvariants(
2127     Loop &L, Instruction &TI, ArrayRef<Value *> Invariants,
2128     IVConditionInfo &PartialIVInfo, DominatorTree &DT, LoopInfo &LI,
2129     AssumptionCache &AC,
2130     function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
2131     ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2132     function_ref<void(Loop &, StringRef)> DestroyLoopCB, bool InsertFreeze) {
2133   auto *ParentBB = TI.getParent();
2134   BranchInst *BI = dyn_cast<BranchInst>(&TI);
2135   SwitchInst *SI = BI ? nullptr : cast<SwitchInst>(&TI);
2136 
2137   // We can only unswitch switches, conditional branches with an invariant
2138   // condition, or combining invariant conditions with an instruction or
2139   // partially invariant instructions.
2140   assert((SI || (BI && BI->isConditional())) &&
2141          "Can only unswitch switches and conditional branch!");
2142   bool PartiallyInvariant = !PartialIVInfo.InstToDuplicate.empty();
2143   bool FullUnswitch =
2144       SI || (skipTrivialSelect(BI->getCondition()) == Invariants[0] &&
2145              !PartiallyInvariant);
2146   if (FullUnswitch)
2147     assert(Invariants.size() == 1 &&
2148            "Cannot have other invariants with full unswitching!");
2149   else
2150     assert(isa<Instruction>(skipTrivialSelect(BI->getCondition())) &&
2151            "Partial unswitching requires an instruction as the condition!");
2152 
2153   if (MSSAU && VerifyMemorySSA)
2154     MSSAU->getMemorySSA()->verifyMemorySSA();
2155 
2156   // Constant and BBs tracking the cloned and continuing successor. When we are
2157   // unswitching the entire condition, this can just be trivially chosen to
2158   // unswitch towards `true`. However, when we are unswitching a set of
2159   // invariants combined with `and` or `or` or partially invariant instructions,
2160   // the combining operation determines the best direction to unswitch: we want
2161   // to unswitch the direction that will collapse the branch.
2162   bool Direction = true;
2163   int ClonedSucc = 0;
2164   if (!FullUnswitch) {
2165     Value *Cond = skipTrivialSelect(BI->getCondition());
2166     (void)Cond;
2167     assert(((match(Cond, m_LogicalAnd()) ^ match(Cond, m_LogicalOr())) ||
2168             PartiallyInvariant) &&
2169            "Only `or`, `and`, an `select`, partially invariant instructions "
2170            "can combine invariants being unswitched.");
2171     if (!match(Cond, m_LogicalOr())) {
2172       if (match(Cond, m_LogicalAnd()) ||
2173           (PartiallyInvariant && !PartialIVInfo.KnownValue->isOneValue())) {
2174         Direction = false;
2175         ClonedSucc = 1;
2176       }
2177     }
2178   }
2179 
2180   BasicBlock *RetainedSuccBB =
2181       BI ? BI->getSuccessor(1 - ClonedSucc) : SI->getDefaultDest();
2182   SmallSetVector<BasicBlock *, 4> UnswitchedSuccBBs;
2183   if (BI)
2184     UnswitchedSuccBBs.insert(BI->getSuccessor(ClonedSucc));
2185   else
2186     for (auto Case : SI->cases())
2187       if (Case.getCaseSuccessor() != RetainedSuccBB)
2188         UnswitchedSuccBBs.insert(Case.getCaseSuccessor());
2189 
2190   assert(!UnswitchedSuccBBs.count(RetainedSuccBB) &&
2191          "Should not unswitch the same successor we are retaining!");
2192 
2193   // The branch should be in this exact loop. Any inner loop's invariant branch
2194   // should be handled by unswitching that inner loop. The caller of this
2195   // routine should filter out any candidates that remain (but were skipped for
2196   // whatever reason).
2197   assert(LI.getLoopFor(ParentBB) == &L && "Branch in an inner loop!");
2198 
2199   // Compute the parent loop now before we start hacking on things.
2200   Loop *ParentL = L.getParentLoop();
2201   // Get blocks in RPO order for MSSA update, before changing the CFG.
2202   LoopBlocksRPO LBRPO(&L);
2203   if (MSSAU)
2204     LBRPO.perform(&LI);
2205 
2206   // Compute the outer-most loop containing one of our exit blocks. This is the
2207   // furthest up our loopnest which can be mutated, which we will use below to
2208   // update things.
2209   Loop *OuterExitL = &L;
2210   SmallVector<BasicBlock *, 4> ExitBlocks;
2211   L.getUniqueExitBlocks(ExitBlocks);
2212   for (auto *ExitBB : ExitBlocks) {
2213     // ExitBB can be an exit block for several levels in the loop nest. Make
2214     // sure we find the top most.
2215     Loop *NewOuterExitL = getTopMostExitingLoop(ExitBB, LI);
2216     if (!NewOuterExitL) {
2217       // We exited the entire nest with this block, so we're done.
2218       OuterExitL = nullptr;
2219       break;
2220     }
2221     if (NewOuterExitL != OuterExitL && NewOuterExitL->contains(OuterExitL))
2222       OuterExitL = NewOuterExitL;
2223   }
2224 
2225   // At this point, we're definitely going to unswitch something so invalidate
2226   // any cached information in ScalarEvolution for the outer most loop
2227   // containing an exit block and all nested loops.
2228   if (SE) {
2229     if (OuterExitL)
2230       SE->forgetLoop(OuterExitL);
2231     else
2232       SE->forgetTopmostLoop(&L);
2233     SE->forgetBlockAndLoopDispositions();
2234   }
2235 
2236   // If the edge from this terminator to a successor dominates that successor,
2237   // store a map from each block in its dominator subtree to it. This lets us
2238   // tell when cloning for a particular successor if a block is dominated by
2239   // some *other* successor with a single data structure. We use this to
2240   // significantly reduce cloning.
2241   SmallDenseMap<BasicBlock *, BasicBlock *, 16> DominatingSucc;
2242   for (auto *SuccBB : llvm::concat<BasicBlock *const>(ArrayRef(RetainedSuccBB),
2243                                                       UnswitchedSuccBBs))
2244     if (SuccBB->getUniquePredecessor() ||
2245         llvm::all_of(predecessors(SuccBB), [&](BasicBlock *PredBB) {
2246           return PredBB == ParentBB || DT.dominates(SuccBB, PredBB);
2247         }))
2248       visitDomSubTree(DT, SuccBB, [&](BasicBlock *BB) {
2249         DominatingSucc[BB] = SuccBB;
2250         return true;
2251       });
2252 
2253   // Split the preheader, so that we know that there is a safe place to insert
2254   // the conditional branch. We will change the preheader to have a conditional
2255   // branch on LoopCond. The original preheader will become the split point
2256   // between the unswitched versions, and we will have a new preheader for the
2257   // original loop.
2258   BasicBlock *SplitBB = L.getLoopPreheader();
2259   BasicBlock *LoopPH = SplitEdge(SplitBB, L.getHeader(), &DT, &LI, MSSAU);
2260 
2261   // Keep track of the dominator tree updates needed.
2262   SmallVector<DominatorTree::UpdateType, 4> DTUpdates;
2263 
2264   // Clone the loop for each unswitched successor.
2265   SmallVector<std::unique_ptr<ValueToValueMapTy>, 4> VMaps;
2266   VMaps.reserve(UnswitchedSuccBBs.size());
2267   SmallDenseMap<BasicBlock *, BasicBlock *, 4> ClonedPHs;
2268   for (auto *SuccBB : UnswitchedSuccBBs) {
2269     VMaps.emplace_back(new ValueToValueMapTy());
2270     ClonedPHs[SuccBB] = buildClonedLoopBlocks(
2271         L, LoopPH, SplitBB, ExitBlocks, ParentBB, SuccBB, RetainedSuccBB,
2272         DominatingSucc, *VMaps.back(), DTUpdates, AC, DT, LI, MSSAU, SE);
2273   }
2274 
2275   // Drop metadata if we may break its semantics by moving this instr into the
2276   // split block.
2277   if (TI.getMetadata(LLVMContext::MD_make_implicit)) {
2278     if (DropNonTrivialImplicitNullChecks)
2279       // Do not spend time trying to understand if we can keep it, just drop it
2280       // to save compile time.
2281       TI.setMetadata(LLVMContext::MD_make_implicit, nullptr);
2282     else {
2283       // It is only legal to preserve make.implicit metadata if we are
2284       // guaranteed no reach implicit null check after following this branch.
2285       ICFLoopSafetyInfo SafetyInfo;
2286       SafetyInfo.computeLoopSafetyInfo(&L);
2287       if (!SafetyInfo.isGuaranteedToExecute(TI, &DT, &L))
2288         TI.setMetadata(LLVMContext::MD_make_implicit, nullptr);
2289     }
2290   }
2291 
2292   // The stitching of the branched code back together depends on whether we're
2293   // doing full unswitching or not with the exception that we always want to
2294   // nuke the initial terminator placed in the split block.
2295   SplitBB->getTerminator()->eraseFromParent();
2296   if (FullUnswitch) {
2297     // Splice the terminator from the original loop and rewrite its
2298     // successors.
2299     TI.moveBefore(*SplitBB, SplitBB->end());
2300 
2301     // Keep a clone of the terminator for MSSA updates.
2302     Instruction *NewTI = TI.clone();
2303     NewTI->insertInto(ParentBB, ParentBB->end());
2304 
2305     // First wire up the moved terminator to the preheaders.
2306     if (BI) {
2307       BasicBlock *ClonedPH = ClonedPHs.begin()->second;
2308       BI->setSuccessor(ClonedSucc, ClonedPH);
2309       BI->setSuccessor(1 - ClonedSucc, LoopPH);
2310       Value *Cond = skipTrivialSelect(BI->getCondition());
2311       if (InsertFreeze)
2312         Cond = new FreezeInst(
2313             Cond, Cond->getName() + ".fr", BI);
2314       BI->setCondition(Cond);
2315       DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH});
2316     } else {
2317       assert(SI && "Must either be a branch or switch!");
2318 
2319       // Walk the cases and directly update their successors.
2320       assert(SI->getDefaultDest() == RetainedSuccBB &&
2321              "Not retaining default successor!");
2322       SI->setDefaultDest(LoopPH);
2323       for (const auto &Case : SI->cases())
2324         if (Case.getCaseSuccessor() == RetainedSuccBB)
2325           Case.setSuccessor(LoopPH);
2326         else
2327           Case.setSuccessor(ClonedPHs.find(Case.getCaseSuccessor())->second);
2328 
2329       if (InsertFreeze)
2330         SI->setCondition(new FreezeInst(
2331             SI->getCondition(), SI->getCondition()->getName() + ".fr", SI));
2332 
2333       // We need to use the set to populate domtree updates as even when there
2334       // are multiple cases pointing at the same successor we only want to
2335       // remove and insert one edge in the domtree.
2336       for (BasicBlock *SuccBB : UnswitchedSuccBBs)
2337         DTUpdates.push_back(
2338             {DominatorTree::Insert, SplitBB, ClonedPHs.find(SuccBB)->second});
2339     }
2340 
2341     if (MSSAU) {
2342       DT.applyUpdates(DTUpdates);
2343       DTUpdates.clear();
2344 
2345       // Remove all but one edge to the retained block and all unswitched
2346       // blocks. This is to avoid having duplicate entries in the cloned Phis,
2347       // when we know we only keep a single edge for each case.
2348       MSSAU->removeDuplicatePhiEdgesBetween(ParentBB, RetainedSuccBB);
2349       for (BasicBlock *SuccBB : UnswitchedSuccBBs)
2350         MSSAU->removeDuplicatePhiEdgesBetween(ParentBB, SuccBB);
2351 
2352       for (auto &VMap : VMaps)
2353         MSSAU->updateForClonedLoop(LBRPO, ExitBlocks, *VMap,
2354                                    /*IgnoreIncomingWithNoClones=*/true);
2355       MSSAU->updateExitBlocksForClonedLoop(ExitBlocks, VMaps, DT);
2356 
2357       // Remove all edges to unswitched blocks.
2358       for (BasicBlock *SuccBB : UnswitchedSuccBBs)
2359         MSSAU->removeEdge(ParentBB, SuccBB);
2360     }
2361 
2362     // Now unhook the successor relationship as we'll be replacing
2363     // the terminator with a direct branch. This is much simpler for branches
2364     // than switches so we handle those first.
2365     if (BI) {
2366       // Remove the parent as a predecessor of the unswitched successor.
2367       assert(UnswitchedSuccBBs.size() == 1 &&
2368              "Only one possible unswitched block for a branch!");
2369       BasicBlock *UnswitchedSuccBB = *UnswitchedSuccBBs.begin();
2370       UnswitchedSuccBB->removePredecessor(ParentBB,
2371                                           /*KeepOneInputPHIs*/ true);
2372       DTUpdates.push_back({DominatorTree::Delete, ParentBB, UnswitchedSuccBB});
2373     } else {
2374       // Note that we actually want to remove the parent block as a predecessor
2375       // of *every* case successor. The case successor is either unswitched,
2376       // completely eliminating an edge from the parent to that successor, or it
2377       // is a duplicate edge to the retained successor as the retained successor
2378       // is always the default successor and as we'll replace this with a direct
2379       // branch we no longer need the duplicate entries in the PHI nodes.
2380       SwitchInst *NewSI = cast<SwitchInst>(NewTI);
2381       assert(NewSI->getDefaultDest() == RetainedSuccBB &&
2382              "Not retaining default successor!");
2383       for (const auto &Case : NewSI->cases())
2384         Case.getCaseSuccessor()->removePredecessor(
2385             ParentBB,
2386             /*KeepOneInputPHIs*/ true);
2387 
2388       // We need to use the set to populate domtree updates as even when there
2389       // are multiple cases pointing at the same successor we only want to
2390       // remove and insert one edge in the domtree.
2391       for (BasicBlock *SuccBB : UnswitchedSuccBBs)
2392         DTUpdates.push_back({DominatorTree::Delete, ParentBB, SuccBB});
2393     }
2394 
2395     // After MSSAU update, remove the cloned terminator instruction NewTI.
2396     ParentBB->getTerminator()->eraseFromParent();
2397 
2398     // Create a new unconditional branch to the continuing block (as opposed to
2399     // the one cloned).
2400     BranchInst::Create(RetainedSuccBB, ParentBB);
2401   } else {
2402     assert(BI && "Only branches have partial unswitching.");
2403     assert(UnswitchedSuccBBs.size() == 1 &&
2404            "Only one possible unswitched block for a branch!");
2405     BasicBlock *ClonedPH = ClonedPHs.begin()->second;
2406     // When doing a partial unswitch, we have to do a bit more work to build up
2407     // the branch in the split block.
2408     if (PartiallyInvariant)
2409       buildPartialInvariantUnswitchConditionalBranch(
2410           *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU);
2411     else {
2412       buildPartialUnswitchConditionalBranch(
2413           *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH,
2414           FreezeLoopUnswitchCond, BI, &AC, DT);
2415     }
2416     DTUpdates.push_back({DominatorTree::Insert, SplitBB, ClonedPH});
2417 
2418     if (MSSAU) {
2419       DT.applyUpdates(DTUpdates);
2420       DTUpdates.clear();
2421 
2422       // Perform MSSA cloning updates.
2423       for (auto &VMap : VMaps)
2424         MSSAU->updateForClonedLoop(LBRPO, ExitBlocks, *VMap,
2425                                    /*IgnoreIncomingWithNoClones=*/true);
2426       MSSAU->updateExitBlocksForClonedLoop(ExitBlocks, VMaps, DT);
2427     }
2428   }
2429 
2430   // Apply the updates accumulated above to get an up-to-date dominator tree.
2431   DT.applyUpdates(DTUpdates);
2432 
2433   // Now that we have an accurate dominator tree, first delete the dead cloned
2434   // blocks so that we can accurately build any cloned loops. It is important to
2435   // not delete the blocks from the original loop yet because we still want to
2436   // reference the original loop to understand the cloned loop's structure.
2437   deleteDeadClonedBlocks(L, ExitBlocks, VMaps, DT, MSSAU);
2438 
2439   // Build the cloned loop structure itself. This may be substantially
2440   // different from the original structure due to the simplified CFG. This also
2441   // handles inserting all the cloned blocks into the correct loops.
2442   SmallVector<Loop *, 4> NonChildClonedLoops;
2443   for (std::unique_ptr<ValueToValueMapTy> &VMap : VMaps)
2444     buildClonedLoops(L, ExitBlocks, *VMap, LI, NonChildClonedLoops);
2445 
2446   // Now that our cloned loops have been built, we can update the original loop.
2447   // First we delete the dead blocks from it and then we rebuild the loop
2448   // structure taking these deletions into account.
2449   deleteDeadBlocksFromLoop(L, ExitBlocks, DT, LI, MSSAU, SE,DestroyLoopCB);
2450 
2451   if (MSSAU && VerifyMemorySSA)
2452     MSSAU->getMemorySSA()->verifyMemorySSA();
2453 
2454   SmallVector<Loop *, 4> HoistedLoops;
2455   bool IsStillLoop =
2456       rebuildLoopAfterUnswitch(L, ExitBlocks, LI, HoistedLoops, SE);
2457 
2458   if (MSSAU && VerifyMemorySSA)
2459     MSSAU->getMemorySSA()->verifyMemorySSA();
2460 
2461   // This transformation has a high risk of corrupting the dominator tree, and
2462   // the below steps to rebuild loop structures will result in hard to debug
2463   // errors in that case so verify that the dominator tree is sane first.
2464   // FIXME: Remove this when the bugs stop showing up and rely on existing
2465   // verification steps.
2466   assert(DT.verify(DominatorTree::VerificationLevel::Fast));
2467 
2468   if (BI && !PartiallyInvariant) {
2469     // If we unswitched a branch which collapses the condition to a known
2470     // constant we want to replace all the uses of the invariants within both
2471     // the original and cloned blocks. We do this here so that we can use the
2472     // now updated dominator tree to identify which side the users are on.
2473     assert(UnswitchedSuccBBs.size() == 1 &&
2474            "Only one possible unswitched block for a branch!");
2475     BasicBlock *ClonedPH = ClonedPHs.begin()->second;
2476 
2477     // When considering multiple partially-unswitched invariants
2478     // we cant just go replace them with constants in both branches.
2479     //
2480     // For 'AND' we infer that true branch ("continue") means true
2481     // for each invariant operand.
2482     // For 'OR' we can infer that false branch ("continue") means false
2483     // for each invariant operand.
2484     // So it happens that for multiple-partial case we dont replace
2485     // in the unswitched branch.
2486     bool ReplaceUnswitched =
2487         FullUnswitch || (Invariants.size() == 1) || PartiallyInvariant;
2488 
2489     ConstantInt *UnswitchedReplacement =
2490         Direction ? ConstantInt::getTrue(BI->getContext())
2491                   : ConstantInt::getFalse(BI->getContext());
2492     ConstantInt *ContinueReplacement =
2493         Direction ? ConstantInt::getFalse(BI->getContext())
2494                   : ConstantInt::getTrue(BI->getContext());
2495     for (Value *Invariant : Invariants) {
2496       assert(!isa<Constant>(Invariant) &&
2497              "Should not be replacing constant values!");
2498       // Use make_early_inc_range here as set invalidates the iterator.
2499       for (Use &U : llvm::make_early_inc_range(Invariant->uses())) {
2500         Instruction *UserI = dyn_cast<Instruction>(U.getUser());
2501         if (!UserI)
2502           continue;
2503 
2504         // Replace it with the 'continue' side if in the main loop body, and the
2505         // unswitched if in the cloned blocks.
2506         if (DT.dominates(LoopPH, UserI->getParent()))
2507           U.set(ContinueReplacement);
2508         else if (ReplaceUnswitched &&
2509                  DT.dominates(ClonedPH, UserI->getParent()))
2510           U.set(UnswitchedReplacement);
2511       }
2512     }
2513   }
2514 
2515   // We can change which blocks are exit blocks of all the cloned sibling
2516   // loops, the current loop, and any parent loops which shared exit blocks
2517   // with the current loop. As a consequence, we need to re-form LCSSA for
2518   // them. But we shouldn't need to re-form LCSSA for any child loops.
2519   // FIXME: This could be made more efficient by tracking which exit blocks are
2520   // new, and focusing on them, but that isn't likely to be necessary.
2521   //
2522   // In order to reasonably rebuild LCSSA we need to walk inside-out across the
2523   // loop nest and update every loop that could have had its exits changed. We
2524   // also need to cover any intervening loops. We add all of these loops to
2525   // a list and sort them by loop depth to achieve this without updating
2526   // unnecessary loops.
2527   auto UpdateLoop = [&](Loop &UpdateL) {
2528 #ifndef NDEBUG
2529     UpdateL.verifyLoop();
2530     for (Loop *ChildL : UpdateL) {
2531       ChildL->verifyLoop();
2532       assert(ChildL->isRecursivelyLCSSAForm(DT, LI) &&
2533              "Perturbed a child loop's LCSSA form!");
2534     }
2535 #endif
2536     // First build LCSSA for this loop so that we can preserve it when
2537     // forming dedicated exits. We don't want to perturb some other loop's
2538     // LCSSA while doing that CFG edit.
2539     formLCSSA(UpdateL, DT, &LI, SE);
2540 
2541     // For loops reached by this loop's original exit blocks we may
2542     // introduced new, non-dedicated exits. At least try to re-form dedicated
2543     // exits for these loops. This may fail if they couldn't have dedicated
2544     // exits to start with.
2545     formDedicatedExitBlocks(&UpdateL, &DT, &LI, MSSAU, /*PreserveLCSSA*/ true);
2546   };
2547 
2548   // For non-child cloned loops and hoisted loops, we just need to update LCSSA
2549   // and we can do it in any order as they don't nest relative to each other.
2550   //
2551   // Also check if any of the loops we have updated have become top-level loops
2552   // as that will necessitate widening the outer loop scope.
2553   for (Loop *UpdatedL :
2554        llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops)) {
2555     UpdateLoop(*UpdatedL);
2556     if (UpdatedL->isOutermost())
2557       OuterExitL = nullptr;
2558   }
2559   if (IsStillLoop) {
2560     UpdateLoop(L);
2561     if (L.isOutermost())
2562       OuterExitL = nullptr;
2563   }
2564 
2565   // If the original loop had exit blocks, walk up through the outer most loop
2566   // of those exit blocks to update LCSSA and form updated dedicated exits.
2567   if (OuterExitL != &L)
2568     for (Loop *OuterL = ParentL; OuterL != OuterExitL;
2569          OuterL = OuterL->getParentLoop())
2570       UpdateLoop(*OuterL);
2571 
2572 #ifndef NDEBUG
2573   // Verify the entire loop structure to catch any incorrect updates before we
2574   // progress in the pass pipeline.
2575   LI.verify(DT);
2576 #endif
2577 
2578   // Now that we've unswitched something, make callbacks to report the changes.
2579   // For that we need to merge together the updated loops and the cloned loops
2580   // and check whether the original loop survived.
2581   SmallVector<Loop *, 4> SibLoops;
2582   for (Loop *UpdatedL : llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops))
2583     if (UpdatedL->getParentLoop() == ParentL)
2584       SibLoops.push_back(UpdatedL);
2585   UnswitchCB(IsStillLoop, PartiallyInvariant, SibLoops);
2586 
2587   if (MSSAU && VerifyMemorySSA)
2588     MSSAU->getMemorySSA()->verifyMemorySSA();
2589 
2590   if (BI)
2591     ++NumBranches;
2592   else
2593     ++NumSwitches;
2594 }
2595 
2596 /// Recursively compute the cost of a dominator subtree based on the per-block
2597 /// cost map provided.
2598 ///
2599 /// The recursive computation is memozied into the provided DT-indexed cost map
2600 /// to allow querying it for most nodes in the domtree without it becoming
2601 /// quadratic.
2602 static InstructionCost computeDomSubtreeCost(
2603     DomTreeNode &N,
2604     const SmallDenseMap<BasicBlock *, InstructionCost, 4> &BBCostMap,
2605     SmallDenseMap<DomTreeNode *, InstructionCost, 4> &DTCostMap) {
2606   // Don't accumulate cost (or recurse through) blocks not in our block cost
2607   // map and thus not part of the duplication cost being considered.
2608   auto BBCostIt = BBCostMap.find(N.getBlock());
2609   if (BBCostIt == BBCostMap.end())
2610     return 0;
2611 
2612   // Lookup this node to see if we already computed its cost.
2613   auto DTCostIt = DTCostMap.find(&N);
2614   if (DTCostIt != DTCostMap.end())
2615     return DTCostIt->second;
2616 
2617   // If not, we have to compute it. We can't use insert above and update
2618   // because computing the cost may insert more things into the map.
2619   InstructionCost Cost = std::accumulate(
2620       N.begin(), N.end(), BBCostIt->second,
2621       [&](InstructionCost Sum, DomTreeNode *ChildN) -> InstructionCost {
2622         return Sum + computeDomSubtreeCost(*ChildN, BBCostMap, DTCostMap);
2623       });
2624   bool Inserted = DTCostMap.insert({&N, Cost}).second;
2625   (void)Inserted;
2626   assert(Inserted && "Should not insert a node while visiting children!");
2627   return Cost;
2628 }
2629 
2630 /// Turns a select instruction into implicit control flow branch,
2631 /// making the following replacement:
2632 ///
2633 /// head:
2634 ///   --code before select--
2635 ///   select %cond, %trueval, %falseval
2636 ///   --code after select--
2637 ///
2638 /// into
2639 ///
2640 /// head:
2641 ///   --code before select--
2642 ///   br i1 %cond, label %then, label %tail
2643 ///
2644 /// then:
2645 ///   br %tail
2646 ///
2647 /// tail:
2648 ///   phi [ %trueval, %then ], [ %falseval, %head]
2649 ///   unreachable
2650 ///
2651 /// It also makes all relevant DT and LI updates, so that all structures are in
2652 /// valid state after this transform.
2653 static BranchInst *turnSelectIntoBranch(SelectInst *SI, DominatorTree &DT,
2654                                         LoopInfo &LI, MemorySSAUpdater *MSSAU,
2655                                         AssumptionCache *AC) {
2656   LLVM_DEBUG(dbgs() << "Turning " << *SI << " into a branch.\n");
2657   BasicBlock *HeadBB = SI->getParent();
2658 
2659   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2660   SplitBlockAndInsertIfThen(SI->getCondition(), SI, false,
2661                             SI->getMetadata(LLVMContext::MD_prof), &DTU, &LI);
2662   auto *CondBr = cast<BranchInst>(HeadBB->getTerminator());
2663   BasicBlock *ThenBB = CondBr->getSuccessor(0),
2664              *TailBB = CondBr->getSuccessor(1);
2665   if (MSSAU)
2666     MSSAU->moveAllAfterSpliceBlocks(HeadBB, TailBB, SI);
2667 
2668   PHINode *Phi = PHINode::Create(SI->getType(), 2, "unswitched.select", SI);
2669   Phi->addIncoming(SI->getTrueValue(), ThenBB);
2670   Phi->addIncoming(SI->getFalseValue(), HeadBB);
2671   SI->replaceAllUsesWith(Phi);
2672   SI->eraseFromParent();
2673 
2674   if (MSSAU && VerifyMemorySSA)
2675     MSSAU->getMemorySSA()->verifyMemorySSA();
2676 
2677   ++NumSelects;
2678   return CondBr;
2679 }
2680 
2681 /// Turns a llvm.experimental.guard intrinsic into implicit control flow branch,
2682 /// making the following replacement:
2683 ///
2684 ///   --code before guard--
2685 ///   call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ]
2686 ///   --code after guard--
2687 ///
2688 /// into
2689 ///
2690 ///   --code before guard--
2691 ///   br i1 %cond, label %guarded, label %deopt
2692 ///
2693 /// guarded:
2694 ///   --code after guard--
2695 ///
2696 /// deopt:
2697 ///   call void (i1, ...) @llvm.experimental.guard(i1 false) [ "deopt"() ]
2698 ///   unreachable
2699 ///
2700 /// It also makes all relevant DT and LI updates, so that all structures are in
2701 /// valid state after this transform.
2702 static BranchInst *turnGuardIntoBranch(IntrinsicInst *GI, Loop &L,
2703                                        DominatorTree &DT, LoopInfo &LI,
2704                                        MemorySSAUpdater *MSSAU) {
2705   SmallVector<DominatorTree::UpdateType, 4> DTUpdates;
2706   LLVM_DEBUG(dbgs() << "Turning " << *GI << " into a branch.\n");
2707   BasicBlock *CheckBB = GI->getParent();
2708 
2709   if (MSSAU && VerifyMemorySSA)
2710      MSSAU->getMemorySSA()->verifyMemorySSA();
2711 
2712   DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
2713   Instruction *DeoptBlockTerm =
2714       SplitBlockAndInsertIfThen(GI->getArgOperand(0), GI, true,
2715                                 GI->getMetadata(LLVMContext::MD_prof), &DTU, &LI);
2716   BranchInst *CheckBI = cast<BranchInst>(CheckBB->getTerminator());
2717   // SplitBlockAndInsertIfThen inserts control flow that branches to
2718   // DeoptBlockTerm if the condition is true.  We want the opposite.
2719   CheckBI->swapSuccessors();
2720 
2721   BasicBlock *GuardedBlock = CheckBI->getSuccessor(0);
2722   GuardedBlock->setName("guarded");
2723   CheckBI->getSuccessor(1)->setName("deopt");
2724   BasicBlock *DeoptBlock = CheckBI->getSuccessor(1);
2725 
2726   if (MSSAU)
2727     MSSAU->moveAllAfterSpliceBlocks(CheckBB, GuardedBlock, GI);
2728 
2729   GI->moveBefore(DeoptBlockTerm);
2730   GI->setArgOperand(0, ConstantInt::getFalse(GI->getContext()));
2731 
2732   if (MSSAU) {
2733     MemoryDef *MD = cast<MemoryDef>(MSSAU->getMemorySSA()->getMemoryAccess(GI));
2734     MSSAU->moveToPlace(MD, DeoptBlock, MemorySSA::BeforeTerminator);
2735     if (VerifyMemorySSA)
2736       MSSAU->getMemorySSA()->verifyMemorySSA();
2737   }
2738 
2739   if (VerifyLoopInfo)
2740     LI.verify(DT);
2741   ++NumGuards;
2742   return CheckBI;
2743 }
2744 
2745 /// Cost multiplier is a way to limit potentially exponential behavior
2746 /// of loop-unswitch. Cost is multipied in proportion of 2^number of unswitch
2747 /// candidates available. Also accounting for the number of "sibling" loops with
2748 /// the idea to account for previous unswitches that already happened on this
2749 /// cluster of loops. There was an attempt to keep this formula simple,
2750 /// just enough to limit the worst case behavior. Even if it is not that simple
2751 /// now it is still not an attempt to provide a detailed heuristic size
2752 /// prediction.
2753 ///
2754 /// TODO: Make a proper accounting of "explosion" effect for all kinds of
2755 /// unswitch candidates, making adequate predictions instead of wild guesses.
2756 /// That requires knowing not just the number of "remaining" candidates but
2757 /// also costs of unswitching for each of these candidates.
2758 static int CalculateUnswitchCostMultiplier(
2759     const Instruction &TI, const Loop &L, const LoopInfo &LI,
2760     const DominatorTree &DT,
2761     ArrayRef<NonTrivialUnswitchCandidate> UnswitchCandidates) {
2762 
2763   // Guards and other exiting conditions do not contribute to exponential
2764   // explosion as soon as they dominate the latch (otherwise there might be
2765   // another path to the latch remaining that does not allow to eliminate the
2766   // loop copy on unswitch).
2767   const BasicBlock *Latch = L.getLoopLatch();
2768   const BasicBlock *CondBlock = TI.getParent();
2769   if (DT.dominates(CondBlock, Latch) &&
2770       (isGuard(&TI) ||
2771        (TI.isTerminator() &&
2772         llvm::count_if(successors(&TI), [&L](const BasicBlock *SuccBB) {
2773           return L.contains(SuccBB);
2774         }) <= 1))) {
2775     NumCostMultiplierSkipped++;
2776     return 1;
2777   }
2778 
2779   auto *ParentL = L.getParentLoop();
2780   int SiblingsCount = (ParentL ? ParentL->getSubLoopsVector().size()
2781                                : std::distance(LI.begin(), LI.end()));
2782   // Count amount of clones that all the candidates might cause during
2783   // unswitching. Branch/guard/select counts as 1, switch counts as log2 of its
2784   // cases.
2785   int UnswitchedClones = 0;
2786   for (const auto &Candidate : UnswitchCandidates) {
2787     const Instruction *CI = Candidate.TI;
2788     const BasicBlock *CondBlock = CI->getParent();
2789     bool SkipExitingSuccessors = DT.dominates(CondBlock, Latch);
2790     if (isa<SelectInst>(CI)) {
2791       UnswitchedClones++;
2792       continue;
2793     }
2794     if (isGuard(CI)) {
2795       if (!SkipExitingSuccessors)
2796         UnswitchedClones++;
2797       continue;
2798     }
2799     int NonExitingSuccessors =
2800         llvm::count_if(successors(CondBlock),
2801                        [SkipExitingSuccessors, &L](const BasicBlock *SuccBB) {
2802           return !SkipExitingSuccessors || L.contains(SuccBB);
2803         });
2804     UnswitchedClones += Log2_32(NonExitingSuccessors);
2805   }
2806 
2807   // Ignore up to the "unscaled candidates" number of unswitch candidates
2808   // when calculating the power-of-two scaling of the cost. The main idea
2809   // with this control is to allow a small number of unswitches to happen
2810   // and rely more on siblings multiplier (see below) when the number
2811   // of candidates is small.
2812   unsigned ClonesPower =
2813       std::max(UnswitchedClones - (int)UnswitchNumInitialUnscaledCandidates, 0);
2814 
2815   // Allowing top-level loops to spread a bit more than nested ones.
2816   int SiblingsMultiplier =
2817       std::max((ParentL ? SiblingsCount
2818                         : SiblingsCount / (int)UnswitchSiblingsToplevelDiv),
2819                1);
2820   // Compute the cost multiplier in a way that won't overflow by saturating
2821   // at an upper bound.
2822   int CostMultiplier;
2823   if (ClonesPower > Log2_32(UnswitchThreshold) ||
2824       SiblingsMultiplier > UnswitchThreshold)
2825     CostMultiplier = UnswitchThreshold;
2826   else
2827     CostMultiplier = std::min(SiblingsMultiplier * (1 << ClonesPower),
2828                               (int)UnswitchThreshold);
2829 
2830   LLVM_DEBUG(dbgs() << "  Computed multiplier  " << CostMultiplier
2831                     << " (siblings " << SiblingsMultiplier << " * clones "
2832                     << (1 << ClonesPower) << ")"
2833                     << " for unswitch candidate: " << TI << "\n");
2834   return CostMultiplier;
2835 }
2836 
2837 static bool collectUnswitchCandidates(
2838     SmallVectorImpl<NonTrivialUnswitchCandidate> &UnswitchCandidates,
2839     IVConditionInfo &PartialIVInfo, Instruction *&PartialIVCondBranch,
2840     const Loop &L, const LoopInfo &LI, AAResults &AA,
2841     const MemorySSAUpdater *MSSAU) {
2842   assert(UnswitchCandidates.empty() && "Should be!");
2843 
2844   auto AddUnswitchCandidatesForInst = [&](Instruction *I, Value *Cond) {
2845     Cond = skipTrivialSelect(Cond);
2846     if (isa<Constant>(Cond))
2847       return;
2848     if (L.isLoopInvariant(Cond)) {
2849       UnswitchCandidates.push_back({I, {Cond}});
2850       return;
2851     }
2852     if (match(Cond, m_CombineOr(m_LogicalAnd(), m_LogicalOr()))) {
2853       TinyPtrVector<Value *> Invariants =
2854           collectHomogenousInstGraphLoopInvariants(
2855               L, *static_cast<Instruction *>(Cond), LI);
2856       if (!Invariants.empty())
2857         UnswitchCandidates.push_back({I, std::move(Invariants)});
2858     }
2859   };
2860 
2861   // Whether or not we should also collect guards in the loop.
2862   bool CollectGuards = false;
2863   if (UnswitchGuards) {
2864     auto *GuardDecl = L.getHeader()->getParent()->getParent()->getFunction(
2865         Intrinsic::getName(Intrinsic::experimental_guard));
2866     if (GuardDecl && !GuardDecl->use_empty())
2867       CollectGuards = true;
2868   }
2869 
2870   for (auto *BB : L.blocks()) {
2871     if (LI.getLoopFor(BB) != &L)
2872       continue;
2873 
2874     for (auto &I : *BB) {
2875       if (auto *SI = dyn_cast<SelectInst>(&I)) {
2876         auto *Cond = SI->getCondition();
2877         // Do not unswitch vector selects and logical and/or selects
2878         if (Cond->getType()->isIntegerTy(1) && !SI->getType()->isIntegerTy(1))
2879           AddUnswitchCandidatesForInst(SI, Cond);
2880       } else if (CollectGuards && isGuard(&I)) {
2881         auto *Cond =
2882             skipTrivialSelect(cast<IntrinsicInst>(&I)->getArgOperand(0));
2883         // TODO: Support AND, OR conditions and partial unswitching.
2884         if (!isa<Constant>(Cond) && L.isLoopInvariant(Cond))
2885           UnswitchCandidates.push_back({&I, {Cond}});
2886       }
2887     }
2888 
2889     if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
2890       // We can only consider fully loop-invariant switch conditions as we need
2891       // to completely eliminate the switch after unswitching.
2892       if (!isa<Constant>(SI->getCondition()) &&
2893           L.isLoopInvariant(SI->getCondition()) && !BB->getUniqueSuccessor())
2894         UnswitchCandidates.push_back({SI, {SI->getCondition()}});
2895       continue;
2896     }
2897 
2898     auto *BI = dyn_cast<BranchInst>(BB->getTerminator());
2899     if (!BI || !BI->isConditional() ||
2900         BI->getSuccessor(0) == BI->getSuccessor(1))
2901       continue;
2902 
2903     AddUnswitchCandidatesForInst(BI, BI->getCondition());
2904   }
2905 
2906   if (MSSAU && !findOptionMDForLoop(&L, "llvm.loop.unswitch.partial.disable") &&
2907       !any_of(UnswitchCandidates, [&L](auto &TerminatorAndInvariants) {
2908          return TerminatorAndInvariants.TI == L.getHeader()->getTerminator();
2909        })) {
2910     MemorySSA *MSSA = MSSAU->getMemorySSA();
2911     if (auto Info = hasPartialIVCondition(L, MSSAThreshold, *MSSA, AA)) {
2912       LLVM_DEBUG(
2913           dbgs() << "simple-loop-unswitch: Found partially invariant condition "
2914                  << *Info->InstToDuplicate[0] << "\n");
2915       PartialIVInfo = *Info;
2916       PartialIVCondBranch = L.getHeader()->getTerminator();
2917       TinyPtrVector<Value *> ValsToDuplicate;
2918       llvm::append_range(ValsToDuplicate, Info->InstToDuplicate);
2919       UnswitchCandidates.push_back(
2920           {L.getHeader()->getTerminator(), std::move(ValsToDuplicate)});
2921     }
2922   }
2923   return !UnswitchCandidates.empty();
2924 }
2925 
2926 /// Tries to canonicalize condition described by:
2927 ///
2928 ///   br (LHS pred RHS), label IfTrue, label IfFalse
2929 ///
2930 /// into its equivalent where `Pred` is something that we support for injected
2931 /// invariants (so far it is limited to ult), LHS in canonicalized form is
2932 /// non-invariant and RHS is an invariant.
2933 static void canonicalizeForInvariantConditionInjection(
2934     ICmpInst::Predicate &Pred, Value *&LHS, Value *&RHS, BasicBlock *&IfTrue,
2935     BasicBlock *&IfFalse, const Loop &L) {
2936   if (!L.contains(IfTrue)) {
2937     Pred = ICmpInst::getInversePredicate(Pred);
2938     std::swap(IfTrue, IfFalse);
2939   }
2940 
2941   // Move loop-invariant argument to RHS position.
2942   if (L.isLoopInvariant(LHS)) {
2943     Pred = ICmpInst::getSwappedPredicate(Pred);
2944     std::swap(LHS, RHS);
2945   }
2946 
2947   if (Pred == ICmpInst::ICMP_SGE && match(RHS, m_Zero())) {
2948     // Turn "x >=s 0" into "x <u UMIN_INT"
2949     Pred = ICmpInst::ICMP_ULT;
2950     RHS = ConstantInt::get(
2951         RHS->getContext(),
2952         APInt::getSignedMinValue(RHS->getType()->getIntegerBitWidth()));
2953   }
2954 }
2955 
2956 /// Returns true, if predicate described by ( \p Pred, \p LHS, \p RHS )
2957 /// succeeding into blocks ( \p IfTrue, \p IfFalse) can be optimized by
2958 /// injecting a loop-invariant condition.
2959 static bool shouldTryInjectInvariantCondition(
2960     const ICmpInst::Predicate Pred, const Value *LHS, const Value *RHS,
2961     const BasicBlock *IfTrue, const BasicBlock *IfFalse, const Loop &L) {
2962   if (L.isLoopInvariant(LHS) || !L.isLoopInvariant(RHS))
2963     return false;
2964   // TODO: Support other predicates.
2965   if (Pred != ICmpInst::ICMP_ULT)
2966     return false;
2967   // TODO: Support non-loop-exiting branches?
2968   if (!L.contains(IfTrue) || L.contains(IfFalse))
2969     return false;
2970   // FIXME: For some reason this causes problems with MSSA updates, need to
2971   // investigate why. So far, just don't unswitch latch.
2972   if (L.getHeader() == IfTrue)
2973     return false;
2974   return true;
2975 }
2976 
2977 /// Returns true, if metadata on \p BI allows us to optimize branching into \p
2978 /// TakenSucc via injection of invariant conditions. The branch should be not
2979 /// enough and not previously unswitched, the information about this comes from
2980 /// the metadata.
2981 bool shouldTryInjectBasingOnMetadata(const BranchInst *BI,
2982                                      const BasicBlock *TakenSucc) {
2983   // Skip branches that have already been unswithed this way. After successful
2984   // unswitching of injected condition, we will still have a copy of this loop
2985   // which looks exactly the same as original one. To prevent the 2nd attempt
2986   // of unswitching it in the same pass, mark this branch as "nothing to do
2987   // here".
2988   if (BI->hasMetadata("llvm.invariant.condition.injection.disabled"))
2989     return false;
2990   SmallVector<uint32_t> Weights;
2991   if (!extractBranchWeights(*BI, Weights))
2992     return false;
2993   unsigned T = InjectInvariantConditionHotnesThreshold;
2994   BranchProbability LikelyTaken(T - 1, T);
2995 
2996   assert(Weights.size() == 2 && "Unexpected profile data!");
2997   size_t Idx = BI->getSuccessor(0) == TakenSucc ? 0 : 1;
2998   auto Num = Weights[Idx];
2999   auto Denom = Weights[0] + Weights[1];
3000   // Degenerate or overflowed metadata.
3001   if (Denom == 0 || Num > Denom)
3002     return false;
3003   BranchProbability ActualTaken(Num, Denom);
3004   if (LikelyTaken > ActualTaken)
3005     return false;
3006   return true;
3007 }
3008 
3009 /// Materialize pending invariant condition of the given candidate into IR. The
3010 /// injected loop-invariant condition implies the original loop-variant branch
3011 /// condition, so the materialization turns
3012 ///
3013 /// loop_block:
3014 ///   ...
3015 ///   br i1 %variant_cond, label InLoopSucc, label OutOfLoopSucc
3016 ///
3017 /// into
3018 ///
3019 /// preheader:
3020 ///   %invariant_cond = LHS pred RHS
3021 /// ...
3022 /// loop_block:
3023 ///   br i1 %invariant_cond, label InLoopSucc, label OriginalCheck
3024 /// OriginalCheck:
3025 ///   br i1 %variant_cond, label InLoopSucc, label OutOfLoopSucc
3026 /// ...
3027 static NonTrivialUnswitchCandidate
3028 injectPendingInvariantConditions(NonTrivialUnswitchCandidate Candidate, Loop &L,
3029                                  DominatorTree &DT, LoopInfo &LI,
3030                                  AssumptionCache &AC, MemorySSAUpdater *MSSAU) {
3031   assert(Candidate.hasPendingInjection() && "Nothing to inject!");
3032   BasicBlock *Preheader = L.getLoopPreheader();
3033   assert(Preheader && "Loop is not in simplified form?");
3034   assert(LI.getLoopFor(Candidate.TI->getParent()) == &L &&
3035          "Unswitching branch of inner loop!");
3036 
3037   auto Pred = Candidate.PendingInjection->Pred;
3038   auto *LHS = Candidate.PendingInjection->LHS;
3039   auto *RHS = Candidate.PendingInjection->RHS;
3040   auto *InLoopSucc = Candidate.PendingInjection->InLoopSucc;
3041   auto *TI = cast<BranchInst>(Candidate.TI);
3042   auto *BB = Candidate.TI->getParent();
3043   auto *OutOfLoopSucc = InLoopSucc == TI->getSuccessor(0) ? TI->getSuccessor(1)
3044                                                           : TI->getSuccessor(0);
3045   // FIXME: Remove this once limitation on successors is lifted.
3046   assert(L.contains(InLoopSucc) && "Not supported yet!");
3047   assert(!L.contains(OutOfLoopSucc) && "Not supported yet!");
3048   auto &Ctx = BB->getContext();
3049 
3050   IRBuilder<> Builder(Preheader->getTerminator());
3051   assert(ICmpInst::isUnsigned(Pred) && "Not supported yet!");
3052   if (LHS->getType() != RHS->getType()) {
3053     if (LHS->getType()->getIntegerBitWidth() <
3054         RHS->getType()->getIntegerBitWidth())
3055       LHS = Builder.CreateZExt(LHS, RHS->getType(), LHS->getName() + ".wide");
3056     else
3057       RHS = Builder.CreateZExt(RHS, LHS->getType(), RHS->getName() + ".wide");
3058   }
3059   // Do not use builder here: CreateICmp may simplify this into a constant and
3060   // unswitching will break. Better optimize it away later.
3061   auto *InjectedCond =
3062       ICmpInst::Create(Instruction::ICmp, Pred, LHS, RHS, "injected.cond",
3063                        Preheader->getTerminator());
3064   auto *OldCond = TI->getCondition();
3065 
3066   BasicBlock *CheckBlock = BasicBlock::Create(Ctx, BB->getName() + ".check",
3067                                               BB->getParent(), InLoopSucc);
3068   Builder.SetInsertPoint(TI);
3069   auto *InvariantBr =
3070       Builder.CreateCondBr(InjectedCond, InLoopSucc, CheckBlock);
3071 
3072   Builder.SetInsertPoint(CheckBlock);
3073   auto *NewTerm = Builder.CreateCondBr(OldCond, InLoopSucc, OutOfLoopSucc);
3074 
3075   TI->eraseFromParent();
3076   // Prevent infinite unswitching.
3077   NewTerm->setMetadata("llvm.invariant.condition.injection.disabled",
3078                        MDNode::get(BB->getContext(), {}));
3079 
3080   // Fixup phis.
3081   for (auto &I : *InLoopSucc) {
3082     auto *PN = dyn_cast<PHINode>(&I);
3083     if (!PN)
3084       break;
3085     auto *Inc = PN->getIncomingValueForBlock(BB);
3086     PN->addIncoming(Inc, CheckBlock);
3087   }
3088   OutOfLoopSucc->replacePhiUsesWith(BB, CheckBlock);
3089 
3090   SmallVector<DominatorTree::UpdateType, 4> DTUpdates = {
3091     { DominatorTree::Insert, BB, CheckBlock },
3092     { DominatorTree::Insert, CheckBlock, InLoopSucc },
3093     { DominatorTree::Insert, CheckBlock, OutOfLoopSucc },
3094     { DominatorTree::Delete, BB, OutOfLoopSucc }
3095   };
3096 
3097   DT.applyUpdates(DTUpdates);
3098   if (MSSAU)
3099     MSSAU->applyUpdates(DTUpdates, DT);
3100   L.addBasicBlockToLoop(CheckBlock, LI);
3101 
3102 #ifndef NDEBUG
3103   DT.verify();
3104   LI.verify(DT);
3105   if (MSSAU && VerifyMemorySSA)
3106     MSSAU->getMemorySSA()->verifyMemorySSA();
3107 #endif
3108 
3109   // TODO: In fact, cost of unswitching a new invariant candidate is *slightly*
3110   // higher because we have just inserted a new block. Need to think how to
3111   // adjust the cost of injected candidates when it was first computed.
3112   LLVM_DEBUG(dbgs() << "Injected a new loop-invariant branch " << *InvariantBr
3113                     << " and considering it for unswitching.");
3114   ++NumInvariantConditionsInjected;
3115   return NonTrivialUnswitchCandidate(InvariantBr, { InjectedCond },
3116                                      Candidate.Cost);
3117 }
3118 
3119 /// Given chain of loop branch conditions looking like:
3120 ///   br (Variant < Invariant1)
3121 ///   br (Variant < Invariant2)
3122 ///   br (Variant < Invariant3)
3123 ///   ...
3124 /// collect set of invariant conditions on which we want to unswitch, which
3125 /// look like:
3126 ///   Invariant1 <= Invariant2
3127 ///   Invariant2 <= Invariant3
3128 ///   ...
3129 /// Though they might not immediately exist in the IR, we can still inject them.
3130 static bool insertCandidatesWithPendingInjections(
3131     SmallVectorImpl<NonTrivialUnswitchCandidate> &UnswitchCandidates, Loop &L,
3132     ICmpInst::Predicate Pred, ArrayRef<CompareDesc> Compares,
3133     const DominatorTree &DT) {
3134 
3135   assert(ICmpInst::isRelational(Pred));
3136   assert(ICmpInst::isStrictPredicate(Pred));
3137   if (Compares.size() < 2)
3138     return false;
3139   ICmpInst::Predicate NonStrictPred = ICmpInst::getNonStrictPredicate(Pred);
3140   for (auto Prev = Compares.begin(), Next = Compares.begin() + 1;
3141        Next != Compares.end(); ++Prev, ++Next) {
3142     Value *LHS = Next->Invariant;
3143     Value *RHS = Prev->Invariant;
3144     BasicBlock *InLoopSucc = Prev->InLoopSucc;
3145     InjectedInvariant ToInject(NonStrictPred, LHS, RHS, InLoopSucc);
3146     NonTrivialUnswitchCandidate Candidate(Prev->Term, { LHS, RHS },
3147                                           std::nullopt, std::move(ToInject));
3148     UnswitchCandidates.push_back(std::move(Candidate));
3149   }
3150   return true;
3151 }
3152 
3153 /// Collect unswitch candidates by invariant conditions that are not immediately
3154 /// present in the loop. However, they can be injected into the code if we
3155 /// decide it's profitable.
3156 /// An example of such conditions is following:
3157 ///
3158 ///   for (...) {
3159 ///     x = load ...
3160 ///     if (! x <u C1) break;
3161 ///     if (! x <u C2) break;
3162 ///     <do something>
3163 ///   }
3164 ///
3165 /// We can unswitch by condition "C1 <=u C2". If that is true, then "x <u C1 <=
3166 /// C2" automatically implies "x <u C2", so we can get rid of one of
3167 /// loop-variant checks in unswitched loop version.
3168 static bool collectUnswitchCandidatesWithInjections(
3169     SmallVectorImpl<NonTrivialUnswitchCandidate> &UnswitchCandidates,
3170     IVConditionInfo &PartialIVInfo, Instruction *&PartialIVCondBranch, Loop &L,
3171     const DominatorTree &DT, const LoopInfo &LI, AAResults &AA,
3172     const MemorySSAUpdater *MSSAU) {
3173   if (!InjectInvariantConditions)
3174     return false;
3175 
3176   if (!DT.isReachableFromEntry(L.getHeader()))
3177     return false;
3178   auto *Latch = L.getLoopLatch();
3179   // Need to have a single latch and a preheader.
3180   if (!Latch)
3181     return false;
3182   assert(L.getLoopPreheader() && "Must have a preheader!");
3183 
3184   DenseMap<Value *, SmallVector<CompareDesc, 4> > CandidatesULT;
3185   // Traverse the conditions that dominate latch (and therefore dominate each
3186   // other).
3187   for (auto *DTN = DT.getNode(Latch); L.contains(DTN->getBlock());
3188        DTN = DTN->getIDom()) {
3189     ICmpInst::Predicate Pred;
3190     Value *LHS = nullptr, *RHS = nullptr;
3191     BasicBlock *IfTrue = nullptr, *IfFalse = nullptr;
3192     auto *BB = DTN->getBlock();
3193     // Ignore inner loops.
3194     if (LI.getLoopFor(BB) != &L)
3195       continue;
3196     auto *Term = BB->getTerminator();
3197     if (!match(Term, m_Br(m_ICmp(Pred, m_Value(LHS), m_Value(RHS)),
3198                           m_BasicBlock(IfTrue), m_BasicBlock(IfFalse))))
3199       continue;
3200     if (!LHS->getType()->isIntegerTy())
3201       continue;
3202     canonicalizeForInvariantConditionInjection(Pred, LHS, RHS, IfTrue, IfFalse,
3203                                                L);
3204     if (!shouldTryInjectInvariantCondition(Pred, LHS, RHS, IfTrue, IfFalse, L))
3205       continue;
3206     if (!shouldTryInjectBasingOnMetadata(cast<BranchInst>(Term), IfTrue))
3207       continue;
3208     // Strip ZEXT for unsigned predicate.
3209     // TODO: once signed predicates are supported, also strip SEXT.
3210     CompareDesc Desc(cast<BranchInst>(Term), RHS, IfTrue);
3211     while (auto *Zext = dyn_cast<ZExtInst>(LHS))
3212       LHS = Zext->getOperand(0);
3213     CandidatesULT[LHS].push_back(Desc);
3214   }
3215 
3216   bool Found = false;
3217   for (auto &It : CandidatesULT)
3218     Found |= insertCandidatesWithPendingInjections(
3219         UnswitchCandidates, L, ICmpInst::ICMP_ULT, It.second, DT);
3220   return Found;
3221 }
3222 
3223 static bool isSafeForNoNTrivialUnswitching(Loop &L, LoopInfo &LI) {
3224   if (!L.isSafeToClone())
3225     return false;
3226   for (auto *BB : L.blocks())
3227     for (auto &I : *BB) {
3228       if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB))
3229         return false;
3230       if (auto *CB = dyn_cast<CallBase>(&I)) {
3231         assert(!CB->cannotDuplicate() && "Checked by L.isSafeToClone().");
3232         if (CB->isConvergent())
3233           return false;
3234       }
3235     }
3236 
3237   // Check if there are irreducible CFG cycles in this loop. If so, we cannot
3238   // easily unswitch non-trivial edges out of the loop. Doing so might turn the
3239   // irreducible control flow into reducible control flow and introduce new
3240   // loops "out of thin air". If we ever discover important use cases for doing
3241   // this, we can add support to loop unswitch, but it is a lot of complexity
3242   // for what seems little or no real world benefit.
3243   LoopBlocksRPO RPOT(&L);
3244   RPOT.perform(&LI);
3245   if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI))
3246     return false;
3247 
3248   SmallVector<BasicBlock *, 4> ExitBlocks;
3249   L.getUniqueExitBlocks(ExitBlocks);
3250   // We cannot unswitch if exit blocks contain a cleanuppad/catchswitch
3251   // instruction as we don't know how to split those exit blocks.
3252   // FIXME: We should teach SplitBlock to handle this and remove this
3253   // restriction.
3254   for (auto *ExitBB : ExitBlocks) {
3255     auto *I = ExitBB->getFirstNonPHI();
3256     if (isa<CleanupPadInst>(I) || isa<CatchSwitchInst>(I)) {
3257       LLVM_DEBUG(dbgs() << "Cannot unswitch because of cleanuppad/catchswitch "
3258                            "in exit block\n");
3259       return false;
3260     }
3261   }
3262 
3263   return true;
3264 }
3265 
3266 static NonTrivialUnswitchCandidate findBestNonTrivialUnswitchCandidate(
3267     ArrayRef<NonTrivialUnswitchCandidate> UnswitchCandidates, const Loop &L,
3268     const DominatorTree &DT, const LoopInfo &LI, AssumptionCache &AC,
3269     const TargetTransformInfo &TTI, const IVConditionInfo &PartialIVInfo) {
3270   // Given that unswitching these terminators will require duplicating parts of
3271   // the loop, so we need to be able to model that cost. Compute the ephemeral
3272   // values and set up a data structure to hold per-BB costs. We cache each
3273   // block's cost so that we don't recompute this when considering different
3274   // subsets of the loop for duplication during unswitching.
3275   SmallPtrSet<const Value *, 4> EphValues;
3276   CodeMetrics::collectEphemeralValues(&L, &AC, EphValues);
3277   SmallDenseMap<BasicBlock *, InstructionCost, 4> BBCostMap;
3278 
3279   // Compute the cost of each block, as well as the total loop cost. Also, bail
3280   // out if we see instructions which are incompatible with loop unswitching
3281   // (convergent, noduplicate, or cross-basic-block tokens).
3282   // FIXME: We might be able to safely handle some of these in non-duplicated
3283   // regions.
3284   TargetTransformInfo::TargetCostKind CostKind =
3285       L.getHeader()->getParent()->hasMinSize()
3286       ? TargetTransformInfo::TCK_CodeSize
3287       : TargetTransformInfo::TCK_SizeAndLatency;
3288   InstructionCost LoopCost = 0;
3289   for (auto *BB : L.blocks()) {
3290     InstructionCost Cost = 0;
3291     for (auto &I : *BB) {
3292       if (EphValues.count(&I))
3293         continue;
3294       Cost += TTI.getInstructionCost(&I, CostKind);
3295     }
3296     assert(Cost >= 0 && "Must not have negative costs!");
3297     LoopCost += Cost;
3298     assert(LoopCost >= 0 && "Must not have negative loop costs!");
3299     BBCostMap[BB] = Cost;
3300   }
3301   LLVM_DEBUG(dbgs() << "  Total loop cost: " << LoopCost << "\n");
3302 
3303   // Now we find the best candidate by searching for the one with the following
3304   // properties in order:
3305   //
3306   // 1) An unswitching cost below the threshold
3307   // 2) The smallest number of duplicated unswitch candidates (to avoid
3308   //    creating redundant subsequent unswitching)
3309   // 3) The smallest cost after unswitching.
3310   //
3311   // We prioritize reducing fanout of unswitch candidates provided the cost
3312   // remains below the threshold because this has a multiplicative effect.
3313   //
3314   // This requires memoizing each dominator subtree to avoid redundant work.
3315   //
3316   // FIXME: Need to actually do the number of candidates part above.
3317   SmallDenseMap<DomTreeNode *, InstructionCost, 4> DTCostMap;
3318   // Given a terminator which might be unswitched, computes the non-duplicated
3319   // cost for that terminator.
3320   auto ComputeUnswitchedCost = [&](Instruction &TI,
3321                                    bool FullUnswitch) -> InstructionCost {
3322     // Unswitching selects unswitches the entire loop.
3323     if (isa<SelectInst>(TI))
3324       return LoopCost;
3325 
3326     BasicBlock &BB = *TI.getParent();
3327     SmallPtrSet<BasicBlock *, 4> Visited;
3328 
3329     InstructionCost Cost = 0;
3330     for (BasicBlock *SuccBB : successors(&BB)) {
3331       // Don't count successors more than once.
3332       if (!Visited.insert(SuccBB).second)
3333         continue;
3334 
3335       // If this is a partial unswitch candidate, then it must be a conditional
3336       // branch with a condition of either `or`, `and`, their corresponding
3337       // select forms or partially invariant instructions. In that case, one of
3338       // the successors is necessarily duplicated, so don't even try to remove
3339       // its cost.
3340       if (!FullUnswitch) {
3341         auto &BI = cast<BranchInst>(TI);
3342         Value *Cond = skipTrivialSelect(BI.getCondition());
3343         if (match(Cond, m_LogicalAnd())) {
3344           if (SuccBB == BI.getSuccessor(1))
3345             continue;
3346         } else if (match(Cond, m_LogicalOr())) {
3347           if (SuccBB == BI.getSuccessor(0))
3348             continue;
3349         } else if ((PartialIVInfo.KnownValue->isOneValue() &&
3350                     SuccBB == BI.getSuccessor(0)) ||
3351                    (!PartialIVInfo.KnownValue->isOneValue() &&
3352                     SuccBB == BI.getSuccessor(1)))
3353           continue;
3354       }
3355 
3356       // This successor's domtree will not need to be duplicated after
3357       // unswitching if the edge to the successor dominates it (and thus the
3358       // entire tree). This essentially means there is no other path into this
3359       // subtree and so it will end up live in only one clone of the loop.
3360       if (SuccBB->getUniquePredecessor() ||
3361           llvm::all_of(predecessors(SuccBB), [&](BasicBlock *PredBB) {
3362             return PredBB == &BB || DT.dominates(SuccBB, PredBB);
3363           })) {
3364         Cost += computeDomSubtreeCost(*DT[SuccBB], BBCostMap, DTCostMap);
3365         assert(Cost <= LoopCost &&
3366                "Non-duplicated cost should never exceed total loop cost!");
3367       }
3368     }
3369 
3370     // Now scale the cost by the number of unique successors minus one. We
3371     // subtract one because there is already at least one copy of the entire
3372     // loop. This is computing the new cost of unswitching a condition.
3373     // Note that guards always have 2 unique successors that are implicit and
3374     // will be materialized if we decide to unswitch it.
3375     int SuccessorsCount = isGuard(&TI) ? 2 : Visited.size();
3376     assert(SuccessorsCount > 1 &&
3377            "Cannot unswitch a condition without multiple distinct successors!");
3378     return (LoopCost - Cost) * (SuccessorsCount - 1);
3379   };
3380 
3381   std::optional<NonTrivialUnswitchCandidate> Best;
3382   for (auto &Candidate : UnswitchCandidates) {
3383     Instruction &TI = *Candidate.TI;
3384     ArrayRef<Value *> Invariants = Candidate.Invariants;
3385     BranchInst *BI = dyn_cast<BranchInst>(&TI);
3386     bool FullUnswitch =
3387         !BI || Candidate.hasPendingInjection() ||
3388         (Invariants.size() == 1 &&
3389          Invariants[0] == skipTrivialSelect(BI->getCondition()));
3390     InstructionCost CandidateCost = ComputeUnswitchedCost(TI, FullUnswitch);
3391     // Calculate cost multiplier which is a tool to limit potentially
3392     // exponential behavior of loop-unswitch.
3393     if (EnableUnswitchCostMultiplier) {
3394       int CostMultiplier =
3395           CalculateUnswitchCostMultiplier(TI, L, LI, DT, UnswitchCandidates);
3396       assert(
3397           (CostMultiplier > 0 && CostMultiplier <= UnswitchThreshold) &&
3398           "cost multiplier needs to be in the range of 1..UnswitchThreshold");
3399       CandidateCost *= CostMultiplier;
3400       LLVM_DEBUG(dbgs() << "  Computed cost of " << CandidateCost
3401                         << " (multiplier: " << CostMultiplier << ")"
3402                         << " for unswitch candidate: " << TI << "\n");
3403     } else {
3404       LLVM_DEBUG(dbgs() << "  Computed cost of " << CandidateCost
3405                         << " for unswitch candidate: " << TI << "\n");
3406     }
3407 
3408     if (!Best || CandidateCost < Best->Cost) {
3409       Best = Candidate;
3410       Best->Cost = CandidateCost;
3411     }
3412   }
3413   assert(Best && "Must be!");
3414   return *Best;
3415 }
3416 
3417 // Insert a freeze on an unswitched branch if all is true:
3418 // 1. freeze-loop-unswitch-cond option is true
3419 // 2. The branch may not execute in the loop pre-transformation. If a branch may
3420 // not execute and could cause UB, it would always cause UB if it is hoisted outside
3421 // of the loop. Insert a freeze to prevent this case.
3422 // 3. The branch condition may be poison or undef
3423 static bool shouldInsertFreeze(Loop &L, Instruction &TI, DominatorTree &DT,
3424                                AssumptionCache &AC) {
3425   assert(isa<BranchInst>(TI) || isa<SwitchInst>(TI));
3426   if (!FreezeLoopUnswitchCond)
3427     return false;
3428 
3429   ICFLoopSafetyInfo SafetyInfo;
3430   SafetyInfo.computeLoopSafetyInfo(&L);
3431   if (SafetyInfo.isGuaranteedToExecute(TI, &DT, &L))
3432     return false;
3433 
3434   Value *Cond;
3435   if (BranchInst *BI = dyn_cast<BranchInst>(&TI))
3436     Cond = skipTrivialSelect(BI->getCondition());
3437   else
3438     Cond = skipTrivialSelect(cast<SwitchInst>(&TI)->getCondition());
3439   return !isGuaranteedNotToBeUndefOrPoison(
3440       Cond, &AC, L.getLoopPreheader()->getTerminator(), &DT);
3441 }
3442 
3443 static bool unswitchBestCondition(
3444     Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
3445     AAResults &AA, TargetTransformInfo &TTI,
3446     function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
3447     ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
3448     function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
3449   // Collect all invariant conditions within this loop (as opposed to an inner
3450   // loop which would be handled when visiting that inner loop).
3451   SmallVector<NonTrivialUnswitchCandidate, 4> UnswitchCandidates;
3452   IVConditionInfo PartialIVInfo;
3453   Instruction *PartialIVCondBranch = nullptr;
3454   collectUnswitchCandidates(UnswitchCandidates, PartialIVInfo,
3455                             PartialIVCondBranch, L, LI, AA, MSSAU);
3456   collectUnswitchCandidatesWithInjections(UnswitchCandidates, PartialIVInfo,
3457                                           PartialIVCondBranch, L, DT, LI, AA,
3458                                           MSSAU);
3459   // If we didn't find any candidates, we're done.
3460   if (UnswitchCandidates.empty())
3461     return false;
3462 
3463   LLVM_DEBUG(
3464       dbgs() << "Considering " << UnswitchCandidates.size()
3465              << " non-trivial loop invariant conditions for unswitching.\n");
3466 
3467   NonTrivialUnswitchCandidate Best = findBestNonTrivialUnswitchCandidate(
3468       UnswitchCandidates, L, DT, LI, AC, TTI, PartialIVInfo);
3469 
3470   assert(Best.TI && "Failed to find loop unswitch candidate");
3471   assert(Best.Cost && "Failed to compute cost");
3472 
3473   if (*Best.Cost >= UnswitchThreshold) {
3474     LLVM_DEBUG(dbgs() << "Cannot unswitch, lowest cost found: " << *Best.Cost
3475                       << "\n");
3476     return false;
3477   }
3478 
3479   if (Best.hasPendingInjection())
3480     Best = injectPendingInvariantConditions(Best, L, DT, LI, AC, MSSAU);
3481   assert(!Best.hasPendingInjection() &&
3482          "All injections should have been done by now!");
3483 
3484   if (Best.TI != PartialIVCondBranch)
3485     PartialIVInfo.InstToDuplicate.clear();
3486 
3487   bool InsertFreeze;
3488   if (auto *SI = dyn_cast<SelectInst>(Best.TI)) {
3489     // If the best candidate is a select, turn it into a branch. Select
3490     // instructions with a poison conditional do not propagate poison, but
3491     // branching on poison causes UB. Insert a freeze on the select
3492     // conditional to prevent UB after turning the select into a branch.
3493     InsertFreeze = !isGuaranteedNotToBeUndefOrPoison(
3494         SI->getCondition(), &AC, L.getLoopPreheader()->getTerminator(), &DT);
3495     Best.TI = turnSelectIntoBranch(SI, DT, LI, MSSAU, &AC);
3496   } else {
3497     // If the best candidate is a guard, turn it into a branch.
3498     if (isGuard(Best.TI))
3499       Best.TI =
3500           turnGuardIntoBranch(cast<IntrinsicInst>(Best.TI), L, DT, LI, MSSAU);
3501     InsertFreeze = shouldInsertFreeze(L, *Best.TI, DT, AC);
3502   }
3503 
3504   LLVM_DEBUG(dbgs() << "  Unswitching non-trivial (cost = " << Best.Cost
3505                     << ") terminator: " << *Best.TI << "\n");
3506   unswitchNontrivialInvariants(L, *Best.TI, Best.Invariants, PartialIVInfo, DT,
3507                                LI, AC, UnswitchCB, SE, MSSAU, DestroyLoopCB,
3508                                InsertFreeze);
3509   return true;
3510 }
3511 
3512 /// Unswitch control flow predicated on loop invariant conditions.
3513 ///
3514 /// This first hoists all branches or switches which are trivial (IE, do not
3515 /// require duplicating any part of the loop) out of the loop body. It then
3516 /// looks at other loop invariant control flows and tries to unswitch those as
3517 /// well by cloning the loop if the result is small enough.
3518 ///
3519 /// The `DT`, `LI`, `AC`, `AA`, `TTI` parameters are required analyses that are
3520 /// also updated based on the unswitch. The `MSSA` analysis is also updated if
3521 /// valid (i.e. its use is enabled).
3522 ///
3523 /// If either `NonTrivial` is true or the flag `EnableNonTrivialUnswitch` is
3524 /// true, we will attempt to do non-trivial unswitching as well as trivial
3525 /// unswitching.
3526 ///
3527 /// The `UnswitchCB` callback provided will be run after unswitching is
3528 /// complete, with the first parameter set to `true` if the provided loop
3529 /// remains a loop, and a list of new sibling loops created.
3530 ///
3531 /// If `SE` is non-null, we will update that analysis based on the unswitching
3532 /// done.
3533 static bool
3534 unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
3535              AAResults &AA, TargetTransformInfo &TTI, bool Trivial,
3536              bool NonTrivial,
3537              function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
3538              ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
3539              ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI,
3540              function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
3541   assert(L.isRecursivelyLCSSAForm(DT, LI) &&
3542          "Loops must be in LCSSA form before unswitching.");
3543 
3544   // Must be in loop simplified form: we need a preheader and dedicated exits.
3545   if (!L.isLoopSimplifyForm())
3546     return false;
3547 
3548   // Try trivial unswitch first before loop over other basic blocks in the loop.
3549   if (Trivial && unswitchAllTrivialConditions(L, DT, LI, SE, MSSAU)) {
3550     // If we unswitched successfully we will want to clean up the loop before
3551     // processing it further so just mark it as unswitched and return.
3552     UnswitchCB(/*CurrentLoopValid*/ true, false, {});
3553     return true;
3554   }
3555 
3556   const Function *F = L.getHeader()->getParent();
3557 
3558   // Check whether we should continue with non-trivial conditions.
3559   // EnableNonTrivialUnswitch: Global variable that forces non-trivial
3560   //                           unswitching for testing and debugging.
3561   // NonTrivial: Parameter that enables non-trivial unswitching for this
3562   //             invocation of the transform. But this should be allowed only
3563   //             for targets without branch divergence.
3564   //
3565   // FIXME: If divergence analysis becomes available to a loop
3566   // transform, we should allow unswitching for non-trivial uniform
3567   // branches even on targets that have divergence.
3568   // https://bugs.llvm.org/show_bug.cgi?id=48819
3569   bool ContinueWithNonTrivial =
3570       EnableNonTrivialUnswitch || (NonTrivial && !TTI.hasBranchDivergence(F));
3571   if (!ContinueWithNonTrivial)
3572     return false;
3573 
3574   // Skip non-trivial unswitching for optsize functions.
3575   if (F->hasOptSize())
3576     return false;
3577 
3578   // Returns true if Loop L's loop nest is cold, i.e. if the headers of L,
3579   // of the loops L is nested in, and of the loops nested in L are all cold.
3580   auto IsLoopNestCold = [&](const Loop *L) {
3581     // Check L and all of its parent loops.
3582     auto *Parent = L;
3583     while (Parent) {
3584       if (!PSI->isColdBlock(Parent->getHeader(), BFI))
3585         return false;
3586       Parent = Parent->getParentLoop();
3587     }
3588     // Next check all loops nested within L.
3589     SmallVector<const Loop *, 4> Worklist;
3590     Worklist.insert(Worklist.end(), L->getSubLoops().begin(),
3591                     L->getSubLoops().end());
3592     while (!Worklist.empty()) {
3593       auto *CurLoop = Worklist.pop_back_val();
3594       if (!PSI->isColdBlock(CurLoop->getHeader(), BFI))
3595         return false;
3596       Worklist.insert(Worklist.end(), CurLoop->getSubLoops().begin(),
3597                       CurLoop->getSubLoops().end());
3598     }
3599     return true;
3600   };
3601 
3602   // Skip cold loops in cold loop nests, as unswitching them brings little
3603   // benefit but increases the code size
3604   if (PSI && PSI->hasProfileSummary() && BFI && IsLoopNestCold(&L)) {
3605     LLVM_DEBUG(dbgs() << " Skip cold loop: " << L << "\n");
3606     return false;
3607   }
3608 
3609   // Perform legality checks.
3610   if (!isSafeForNoNTrivialUnswitching(L, LI))
3611     return false;
3612 
3613   // For non-trivial unswitching, because it often creates new loops, we rely on
3614   // the pass manager to iterate on the loops rather than trying to immediately
3615   // reach a fixed point. There is no substantial advantage to iterating
3616   // internally, and if any of the new loops are simplified enough to contain
3617   // trivial unswitching we want to prefer those.
3618 
3619   // Try to unswitch the best invariant condition. We prefer this full unswitch to
3620   // a partial unswitch when possible below the threshold.
3621   if (unswitchBestCondition(L, DT, LI, AC, AA, TTI, UnswitchCB, SE, MSSAU,
3622                             DestroyLoopCB))
3623     return true;
3624 
3625   // No other opportunities to unswitch.
3626   return false;
3627 }
3628 
3629 PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
3630                                               LoopStandardAnalysisResults &AR,
3631                                               LPMUpdater &U) {
3632   Function &F = *L.getHeader()->getParent();
3633   (void)F;
3634   ProfileSummaryInfo *PSI = nullptr;
3635   if (auto OuterProxy =
3636           AM.getResult<FunctionAnalysisManagerLoopProxy>(L, AR)
3637               .getCachedResult<ModuleAnalysisManagerFunctionProxy>(F))
3638     PSI = OuterProxy->getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
3639   LLVM_DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L
3640                     << "\n");
3641 
3642   // Save the current loop name in a variable so that we can report it even
3643   // after it has been deleted.
3644   std::string LoopName = std::string(L.getName());
3645 
3646   auto UnswitchCB = [&L, &U, &LoopName](bool CurrentLoopValid,
3647                                         bool PartiallyInvariant,
3648                                         ArrayRef<Loop *> NewLoops) {
3649     // If we did a non-trivial unswitch, we have added new (cloned) loops.
3650     if (!NewLoops.empty())
3651       U.addSiblingLoops(NewLoops);
3652 
3653     // If the current loop remains valid, we should revisit it to catch any
3654     // other unswitch opportunities. Otherwise, we need to mark it as deleted.
3655     if (CurrentLoopValid) {
3656       if (PartiallyInvariant) {
3657         // Mark the new loop as partially unswitched, to avoid unswitching on
3658         // the same condition again.
3659         auto &Context = L.getHeader()->getContext();
3660         MDNode *DisableUnswitchMD = MDNode::get(
3661             Context,
3662             MDString::get(Context, "llvm.loop.unswitch.partial.disable"));
3663         MDNode *NewLoopID = makePostTransformationMetadata(
3664             Context, L.getLoopID(), {"llvm.loop.unswitch.partial"},
3665             {DisableUnswitchMD});
3666         L.setLoopID(NewLoopID);
3667       } else
3668         U.revisitCurrentLoop();
3669     } else
3670       U.markLoopAsDeleted(L, LoopName);
3671   };
3672 
3673   auto DestroyLoopCB = [&U](Loop &L, StringRef Name) {
3674     U.markLoopAsDeleted(L, Name);
3675   };
3676 
3677   std::optional<MemorySSAUpdater> MSSAU;
3678   if (AR.MSSA) {
3679     MSSAU = MemorySSAUpdater(AR.MSSA);
3680     if (VerifyMemorySSA)
3681       AR.MSSA->verifyMemorySSA();
3682   }
3683   if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC, AR.AA, AR.TTI, Trivial, NonTrivial,
3684                     UnswitchCB, &AR.SE, MSSAU ? &*MSSAU : nullptr, PSI, AR.BFI,
3685                     DestroyLoopCB))
3686     return PreservedAnalyses::all();
3687 
3688   if (AR.MSSA && VerifyMemorySSA)
3689     AR.MSSA->verifyMemorySSA();
3690 
3691   // Historically this pass has had issues with the dominator tree so verify it
3692   // in asserts builds.
3693   assert(AR.DT.verify(DominatorTree::VerificationLevel::Fast));
3694 
3695   auto PA = getLoopPassPreservedAnalyses();
3696   if (AR.MSSA)
3697     PA.preserve<MemorySSAAnalysis>();
3698   return PA;
3699 }
3700 
3701 void SimpleLoopUnswitchPass::printPipeline(
3702     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
3703   static_cast<PassInfoMixin<SimpleLoopUnswitchPass> *>(this)->printPipeline(
3704       OS, MapClassName2PassName);
3705 
3706   OS << '<';
3707   OS << (NonTrivial ? "" : "no-") << "nontrivial;";
3708   OS << (Trivial ? "" : "no-") << "trivial";
3709   OS << '>';
3710 }
3711 
3712 namespace {
3713 
3714 class SimpleLoopUnswitchLegacyPass : public LoopPass {
3715   bool NonTrivial;
3716 
3717 public:
3718   static char ID; // Pass ID, replacement for typeid
3719 
3720   explicit SimpleLoopUnswitchLegacyPass(bool NonTrivial = false)
3721       : LoopPass(ID), NonTrivial(NonTrivial) {
3722     initializeSimpleLoopUnswitchLegacyPassPass(
3723         *PassRegistry::getPassRegistry());
3724   }
3725 
3726   bool runOnLoop(Loop *L, LPPassManager &LPM) override;
3727 
3728   void getAnalysisUsage(AnalysisUsage &AU) const override {
3729     AU.addRequired<AssumptionCacheTracker>();
3730     AU.addRequired<TargetTransformInfoWrapperPass>();
3731     AU.addRequired<MemorySSAWrapperPass>();
3732     AU.addPreserved<MemorySSAWrapperPass>();
3733     getLoopAnalysisUsage(AU);
3734   }
3735 };
3736 
3737 } // end anonymous namespace
3738 
3739 bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
3740   if (skipLoop(L))
3741     return false;
3742 
3743   Function &F = *L->getHeader()->getParent();
3744 
3745   LLVM_DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L
3746                     << "\n");
3747   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
3748   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
3749   auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
3750   auto &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
3751   auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
3752   MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
3753   MemorySSAUpdater MSSAU(MSSA);
3754 
3755   auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
3756   auto *SE = SEWP ? &SEWP->getSE() : nullptr;
3757 
3758   auto UnswitchCB = [&L, &LPM](bool CurrentLoopValid, bool PartiallyInvariant,
3759                                ArrayRef<Loop *> NewLoops) {
3760     // If we did a non-trivial unswitch, we have added new (cloned) loops.
3761     for (auto *NewL : NewLoops)
3762       LPM.addLoop(*NewL);
3763 
3764     // If the current loop remains valid, re-add it to the queue. This is
3765     // a little wasteful as we'll finish processing the current loop as well,
3766     // but it is the best we can do in the old PM.
3767     if (CurrentLoopValid) {
3768       // If the current loop has been unswitched using a partially invariant
3769       // condition, we should not re-add the current loop to avoid unswitching
3770       // on the same condition again.
3771       if (!PartiallyInvariant)
3772         LPM.addLoop(*L);
3773     } else
3774       LPM.markLoopAsDeleted(*L);
3775   };
3776 
3777   auto DestroyLoopCB = [&LPM](Loop &L, StringRef /* Name */) {
3778     LPM.markLoopAsDeleted(L);
3779   };
3780 
3781   if (VerifyMemorySSA)
3782     MSSA->verifyMemorySSA();
3783   bool Changed =
3784       unswitchLoop(*L, DT, LI, AC, AA, TTI, true, NonTrivial, UnswitchCB, SE,
3785                    &MSSAU, nullptr, nullptr, DestroyLoopCB);
3786 
3787   if (VerifyMemorySSA)
3788     MSSA->verifyMemorySSA();
3789 
3790   // Historically this pass has had issues with the dominator tree so verify it
3791   // in asserts builds.
3792   assert(DT.verify(DominatorTree::VerificationLevel::Fast));
3793 
3794   return Changed;
3795 }
3796 
3797 char SimpleLoopUnswitchLegacyPass::ID = 0;
3798 INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
3799                       "Simple unswitch loops", false, false)
3800 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
3801 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
3802 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
3803 INITIALIZE_PASS_DEPENDENCY(LoopPass)
3804 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
3805 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
3806 INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
3807                     "Simple unswitch loops", false, false)
3808 
3809 Pass *llvm::createSimpleLoopUnswitchLegacyPass(bool NonTrivial) {
3810   return new SimpleLoopUnswitchLegacyPass(NonTrivial);
3811 }
3812