xref: /llvm-project/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp (revision d869b188260fc0f76747fb09b02e2bfe98f60a56)
1 //===-- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/Analysis/LoopPass.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
26 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/Transforms/Scalar/LoopPassManager.h"
28 #include "llvm/Transforms/Utils/LoopUtils.h"
29 
30 #define DEBUG_TYPE "simple-loop-unswitch"
31 
32 using namespace llvm;
33 
34 STATISTIC(NumBranches, "Number of branches unswitched");
35 STATISTIC(NumSwitches, "Number of switches unswitched");
36 STATISTIC(NumTrivial, "Number of unswitches that are trivial");
37 
38 static void replaceLoopUsesWithConstant(Loop &L, Value &LIC,
39                                         Constant &Replacement) {
40   assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
41 
42   // Replace uses of LIC in the loop with the given constant.
43   for (auto UI = LIC.use_begin(), UE = LIC.use_end(); UI != UE;) {
44     // Grab the use and walk past it so we can clobber it in the use list.
45     Use *U = &*UI++;
46     Instruction *UserI = dyn_cast<Instruction>(U->getUser());
47     if (!UserI || !L.contains(UserI))
48       continue;
49 
50     // Replace this use within the loop body.
51     *U = &Replacement;
52   }
53 }
54 
55 /// Update the dominator tree after removing one exiting predecessor of a loop
56 /// exit block.
57 static void updateLoopExitIDom(BasicBlock *LoopExitBB, Loop &L,
58                                DominatorTree &DT) {
59   assert(pred_begin(LoopExitBB) != pred_end(LoopExitBB) &&
60          "Cannot have empty predecessors of the loop exit block if we split "
61          "off a block to unswitch!");
62 
63   BasicBlock *IDom = *pred_begin(LoopExitBB);
64   // Walk all of the other predecessors finding the nearest common dominator
65   // until all predecessors are covered or we reach the loop header. The loop
66   // header necessarily dominates all loop exit blocks in loop simplified form
67   // so we can early-exit the moment we hit that block.
68   for (auto PI = std::next(pred_begin(LoopExitBB)), PE = pred_end(LoopExitBB);
69        PI != PE && IDom != L.getHeader(); ++PI)
70     IDom = DT.findNearestCommonDominator(IDom, *PI);
71 
72   DT.changeImmediateDominator(LoopExitBB, IDom);
73 }
74 
75 /// Update the dominator tree after unswitching a particular former exit block.
76 ///
77 /// This handles the full update of the dominator tree after hoisting a block
78 /// that previously was an exit block (or split off of an exit block) up to be
79 /// reached from the new immediate dominator of the preheader.
80 ///
81 /// The common case is simple -- we just move the unswitched block to have an
82 /// immediate dominator of the old preheader. But in complex cases, there may
83 /// be other blocks reachable from the unswitched block that are immediately
84 /// dominated by some node between the unswitched one and the old preheader.
85 /// All of these also need to be hoisted in the dominator tree. We also want to
86 /// minimize queries to the dominator tree because each step of this
87 /// invalidates any DFS numbers that would make queries fast.
88 static void updateDTAfterUnswitch(BasicBlock *UnswitchedBB, BasicBlock *OldPH,
89                                   DominatorTree &DT) {
90   DomTreeNode *OldPHNode = DT[OldPH];
91   DomTreeNode *UnswitchedNode = DT[UnswitchedBB];
92   // If the dominator tree has already been updated for this unswitched node,
93   // we're done. This makes it easier to use this routine if there are multiple
94   // paths to the same unswitched destination.
95   if (UnswitchedNode->getIDom() == OldPHNode)
96     return;
97 
98   // First collect the domtree nodes that we are hoisting over. These are the
99   // set of nodes which may have children that need to be hoisted as well.
100   SmallPtrSet<DomTreeNode *, 4> DomChain;
101   for (auto *IDom = UnswitchedNode->getIDom(); IDom != OldPHNode;
102        IDom = IDom->getIDom())
103     DomChain.insert(IDom);
104 
105   // The unswitched block ends up immediately dominated by the old preheader --
106   // regardless of whether it is the loop exit block or split off of the loop
107   // exit block.
108   DT.changeImmediateDominator(UnswitchedNode, OldPHNode);
109 
110   // Blocks reachable from the unswitched block may need to change their IDom
111   // as well.
112   SmallSetVector<BasicBlock *, 4> Worklist;
113   for (auto *SuccBB : successors(UnswitchedBB))
114     Worklist.insert(SuccBB);
115 
116   // Walk the worklist. We grow the list in the loop and so must recompute size.
117   for (int i = 0; i < (int)Worklist.size(); ++i) {
118     auto *BB = Worklist[i];
119 
120     DomTreeNode *Node = DT[BB];
121     assert(!DomChain.count(Node) &&
122            "Cannot be dominated by a block you can reach!");
123     // If this block doesn't have an immediate dominator somewhere in the chain
124     // we hoisted over, then its position in the domtree hasn't changed. Either
125     // it is above the region hoisted and still valid, or it is below the
126     // hoisted block and so was trivially updated. This also applies to
127     // everything reachable from this block so we're completely done with the
128     // it.
129     if (!DomChain.count(Node->getIDom()))
130       continue;
131 
132     // We need to change the IDom for this node but also walk its successors
133     // which could have similar dominance position.
134     DT.changeImmediateDominator(Node, OldPHNode);
135     for (auto *SuccBB : successors(BB))
136       Worklist.insert(SuccBB);
137   }
138 }
139 
140 /// Check that all the LCSSA PHI nodes in the loop exit block have trivial
141 /// incoming values along this edge.
142 static bool areLoopExitPHIsLoopInvariant(Loop &L, BasicBlock &ExitingBB,
143                                          BasicBlock &ExitBB) {
144   for (Instruction &I : ExitBB) {
145     auto *PN = dyn_cast<PHINode>(&I);
146     if (!PN)
147       // No more PHIs to check.
148       return true;
149 
150     // If the incoming value for this edge isn't loop invariant the unswitch
151     // won't be trivial.
152     if (!L.isLoopInvariant(PN->getIncomingValueForBlock(&ExitingBB)))
153       return false;
154   }
155   llvm_unreachable("Basic blocks should never be empty!");
156 }
157 
158 /// Rewrite the PHI nodes in an unswitched loop exit basic block.
159 ///
160 /// Requires that the loop exit and unswitched basic block are the same, and
161 /// that the exiting block was a unique predecessor of that block. Rewrites the
162 /// PHI nodes in that block such that what were LCSSA PHI nodes become trivial
163 /// PHI nodes from the old preheader that now contains the unswitched
164 /// terminator.
165 static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB,
166                                                   BasicBlock &OldExitingBB,
167                                                   BasicBlock &OldPH) {
168   for (Instruction &I : UnswitchedBB) {
169     auto *PN = dyn_cast<PHINode>(&I);
170     if (!PN)
171       // No more PHIs to check.
172       break;
173 
174     // When the loop exit is directly unswitched we just need to update the
175     // incoming basic block. We loop to handle weird cases with repeated
176     // incoming blocks, but expect to typically only have one operand here.
177     for (auto i : llvm::seq<int>(0, PN->getNumOperands())) {
178       assert(PN->getIncomingBlock(i) == &OldExitingBB &&
179              "Found incoming block different from unique predecessor!");
180       PN->setIncomingBlock(i, &OldPH);
181     }
182   }
183 }
184 
185 /// Rewrite the PHI nodes in the loop exit basic block and the split off
186 /// unswitched block.
187 ///
188 /// Because the exit block remains an exit from the loop, this rewrites the
189 /// LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI
190 /// nodes into the unswitched basic block to select between the value in the
191 /// old preheader and the loop exit.
192 static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock &ExitBB,
193                                                       BasicBlock &UnswitchedBB,
194                                                       BasicBlock &OldExitingBB,
195                                                       BasicBlock &OldPH) {
196   assert(&ExitBB != &UnswitchedBB &&
197          "Must have different loop exit and unswitched blocks!");
198   Instruction *InsertPt = &*UnswitchedBB.begin();
199   for (Instruction &I : ExitBB) {
200     auto *PN = dyn_cast<PHINode>(&I);
201     if (!PN)
202       // No more PHIs to check.
203       break;
204 
205     auto *NewPN = PHINode::Create(PN->getType(), /*NumReservedValues*/ 2,
206                                   PN->getName() + ".split", InsertPt);
207 
208     // Walk backwards over the old PHI node's inputs to minimize the cost of
209     // removing each one. We have to do this weird loop manually so that we
210     // create the same number of new incoming edges in the new PHI as we expect
211     // each case-based edge to be included in the unswitched switch in some
212     // cases.
213     // FIXME: This is really, really gross. It would be much cleaner if LLVM
214     // allowed us to create a single entry for a predecessor block without
215     // having separate entries for each "edge" even though these edges are
216     // required to produce identical results.
217     for (int i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
218       if (PN->getIncomingBlock(i) != &OldExitingBB)
219         continue;
220 
221       Value *Incoming = PN->removeIncomingValue(i);
222       NewPN->addIncoming(Incoming, &OldPH);
223     }
224 
225     // Now replace the old PHI with the new one and wire the old one in as an
226     // input to the new one.
227     PN->replaceAllUsesWith(NewPN);
228     NewPN->addIncoming(PN, &ExitBB);
229   }
230 }
231 
232 /// Unswitch a trivial branch if the condition is loop invariant.
233 ///
234 /// This routine should only be called when loop code leading to the branch has
235 /// been validated as trivial (no side effects). This routine checks if the
236 /// condition is invariant and one of the successors is a loop exit. This
237 /// allows us to unswitch without duplicating the loop, making it trivial.
238 ///
239 /// If this routine fails to unswitch the branch it returns false.
240 ///
241 /// If the branch can be unswitched, this routine splits the preheader and
242 /// hoists the branch above that split. Preserves loop simplified form
243 /// (splitting the exit block as necessary). It simplifies the branch within
244 /// the loop to an unconditional branch but doesn't remove it entirely. Further
245 /// cleanup can be done with some simplify-cfg like pass.
246 static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
247                                   LoopInfo &LI) {
248   assert(BI.isConditional() && "Can only unswitch a conditional branch!");
249   DEBUG(dbgs() << "  Trying to unswitch branch: " << BI << "\n");
250 
251   Value *LoopCond = BI.getCondition();
252 
253   // Need a trivial loop condition to unswitch.
254   if (!L.isLoopInvariant(LoopCond))
255     return false;
256 
257   // FIXME: We should compute this once at the start and update it!
258   SmallVector<BasicBlock *, 16> ExitBlocks;
259   L.getExitBlocks(ExitBlocks);
260   SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(),
261                                              ExitBlocks.end());
262 
263   // Check to see if a successor of the branch is guaranteed to
264   // exit through a unique exit block without having any
265   // side-effects.  If so, determine the value of Cond that causes
266   // it to do this.
267   ConstantInt *CondVal = ConstantInt::getTrue(BI.getContext());
268   ConstantInt *Replacement = ConstantInt::getFalse(BI.getContext());
269   int LoopExitSuccIdx = 0;
270   auto *LoopExitBB = BI.getSuccessor(0);
271   if (!ExitBlockSet.count(LoopExitBB)) {
272     std::swap(CondVal, Replacement);
273     LoopExitSuccIdx = 1;
274     LoopExitBB = BI.getSuccessor(1);
275     if (!ExitBlockSet.count(LoopExitBB))
276       return false;
277   }
278   auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx);
279   assert(L.contains(ContinueBB) &&
280          "Cannot have both successors exit and still be in the loop!");
281 
282   auto *ParentBB = BI.getParent();
283   if (!areLoopExitPHIsLoopInvariant(L, *ParentBB, *LoopExitBB))
284     return false;
285 
286   DEBUG(dbgs() << "    unswitching trivial branch when: " << CondVal
287                << " == " << LoopCond << "\n");
288 
289   // Split the preheader, so that we know that there is a safe place to insert
290   // the conditional branch. We will change the preheader to have a conditional
291   // branch on LoopCond.
292   BasicBlock *OldPH = L.getLoopPreheader();
293   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI);
294 
295   // Now that we have a place to insert the conditional branch, create a place
296   // to branch to: this is the exit block out of the loop that we are
297   // unswitching. We need to split this if there are other loop predecessors.
298   // Because the loop is in simplified form, *any* other predecessor is enough.
299   BasicBlock *UnswitchedBB;
300   if (BasicBlock *PredBB = LoopExitBB->getUniquePredecessor()) {
301     (void)PredBB;
302     assert(PredBB == BI.getParent() &&
303            "A branch's parent isn't a predecessor!");
304     UnswitchedBB = LoopExitBB;
305   } else {
306     UnswitchedBB = SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI);
307   }
308 
309   // Now splice the branch to gate reaching the new preheader and re-point its
310   // successors.
311   OldPH->getInstList().splice(std::prev(OldPH->end()),
312                               BI.getParent()->getInstList(), BI);
313   OldPH->getTerminator()->eraseFromParent();
314   BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB);
315   BI.setSuccessor(1 - LoopExitSuccIdx, NewPH);
316 
317   // Create a new unconditional branch that will continue the loop as a new
318   // terminator.
319   BranchInst::Create(ContinueBB, ParentBB);
320 
321   // Rewrite the relevant PHI nodes.
322   if (UnswitchedBB == LoopExitBB)
323     rewritePHINodesForUnswitchedExitBlock(*UnswitchedBB, *ParentBB, *OldPH);
324   else
325     rewritePHINodesForExitAndUnswitchedBlocks(*LoopExitBB, *UnswitchedBB,
326                                               *ParentBB, *OldPH);
327 
328   // Now we need to update the dominator tree.
329   updateDTAfterUnswitch(UnswitchedBB, OldPH, DT);
330   // But if we split something off of the loop exit block then we also removed
331   // one of the predecessors for the loop exit block and may need to update its
332   // idom.
333   if (UnswitchedBB != LoopExitBB)
334     updateLoopExitIDom(LoopExitBB, L, DT);
335 
336   // Since this is an i1 condition we can also trivially replace uses of it
337   // within the loop with a constant.
338   replaceLoopUsesWithConstant(L, *LoopCond, *Replacement);
339 
340   ++NumTrivial;
341   ++NumBranches;
342   return true;
343 }
344 
345 /// Unswitch a trivial switch if the condition is loop invariant.
346 ///
347 /// This routine should only be called when loop code leading to the switch has
348 /// been validated as trivial (no side effects). This routine checks if the
349 /// condition is invariant and that at least one of the successors is a loop
350 /// exit. This allows us to unswitch without duplicating the loop, making it
351 /// trivial.
352 ///
353 /// If this routine fails to unswitch the switch it returns false.
354 ///
355 /// If the switch can be unswitched, this routine splits the preheader and
356 /// copies the switch above that split. If the default case is one of the
357 /// exiting cases, it copies the non-exiting cases and points them at the new
358 /// preheader. If the default case is not exiting, it copies the exiting cases
359 /// and points the default at the preheader. It preserves loop simplified form
360 /// (splitting the exit blocks as necessary). It simplifies the switch within
361 /// the loop by removing now-dead cases. If the default case is one of those
362 /// unswitched, it replaces its destination with a new basic block containing
363 /// only unreachable. Such basic blocks, while technically loop exits, are not
364 /// considered for unswitching so this is a stable transform and the same
365 /// switch will not be revisited. If after unswitching there is only a single
366 /// in-loop successor, the switch is further simplified to an unconditional
367 /// branch. Still more cleanup can be done with some simplify-cfg like pass.
368 static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
369                                   LoopInfo &LI) {
370   DEBUG(dbgs() << "  Trying to unswitch switch: " << SI << "\n");
371   Value *LoopCond = SI.getCondition();
372 
373   // If this isn't switching on an invariant condition, we can't unswitch it.
374   if (!L.isLoopInvariant(LoopCond))
375     return false;
376 
377   auto *ParentBB = SI.getParent();
378 
379   // FIXME: We should compute this once at the start and update it!
380   SmallVector<BasicBlock *, 16> ExitBlocks;
381   L.getExitBlocks(ExitBlocks);
382   SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(),
383                                              ExitBlocks.end());
384 
385   SmallVector<int, 4> ExitCaseIndices;
386   for (auto Case : SI.cases()) {
387     auto *SuccBB = Case.getCaseSuccessor();
388     if (ExitBlockSet.count(SuccBB) &&
389         areLoopExitPHIsLoopInvariant(L, *ParentBB, *SuccBB))
390       ExitCaseIndices.push_back(Case.getCaseIndex());
391   }
392   BasicBlock *DefaultExitBB = nullptr;
393   if (ExitBlockSet.count(SI.getDefaultDest()) &&
394       areLoopExitPHIsLoopInvariant(L, *ParentBB, *SI.getDefaultDest()) &&
395       !isa<UnreachableInst>(SI.getDefaultDest()->getTerminator()))
396     DefaultExitBB = SI.getDefaultDest();
397   else if (ExitCaseIndices.empty())
398     return false;
399 
400   DEBUG(dbgs() << "    unswitching trivial cases...\n");
401 
402   SmallVector<std::pair<ConstantInt *, BasicBlock *>, 4> ExitCases;
403   ExitCases.reserve(ExitCaseIndices.size());
404   // We walk the case indices backwards so that we remove the last case first
405   // and don't disrupt the earlier indices.
406   for (unsigned Index : reverse(ExitCaseIndices)) {
407     auto CaseI = SI.case_begin() + Index;
408     // Save the value of this case.
409     ExitCases.push_back({CaseI->getCaseValue(), CaseI->getCaseSuccessor()});
410     // Delete the unswitched cases.
411     SI.removeCase(CaseI);
412   }
413 
414   // Check if after this all of the remaining cases point at the same
415   // successor.
416   BasicBlock *CommonSuccBB = nullptr;
417   if (SI.getNumCases() > 0 &&
418       std::all_of(std::next(SI.case_begin()), SI.case_end(),
419                   [&SI](const SwitchInst::CaseHandle &Case) {
420                     return Case.getCaseSuccessor() ==
421                            SI.case_begin()->getCaseSuccessor();
422                   }))
423     CommonSuccBB = SI.case_begin()->getCaseSuccessor();
424 
425   if (DefaultExitBB) {
426     // We can't remove the default edge so replace it with an edge to either
427     // the single common remaining successor (if we have one) or an unreachable
428     // block.
429     if (CommonSuccBB) {
430       SI.setDefaultDest(CommonSuccBB);
431     } else {
432       BasicBlock *UnreachableBB = BasicBlock::Create(
433           ParentBB->getContext(),
434           Twine(ParentBB->getName()) + ".unreachable_default",
435           ParentBB->getParent());
436       new UnreachableInst(ParentBB->getContext(), UnreachableBB);
437       SI.setDefaultDest(UnreachableBB);
438       DT.addNewBlock(UnreachableBB, ParentBB);
439     }
440   } else {
441     // If we're not unswitching the default, we need it to match any cases to
442     // have a common successor or if we have no cases it is the common
443     // successor.
444     if (SI.getNumCases() == 0)
445       CommonSuccBB = SI.getDefaultDest();
446     else if (SI.getDefaultDest() != CommonSuccBB)
447       CommonSuccBB = nullptr;
448   }
449 
450   // Split the preheader, so that we know that there is a safe place to insert
451   // the switch.
452   BasicBlock *OldPH = L.getLoopPreheader();
453   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI);
454   OldPH->getTerminator()->eraseFromParent();
455 
456   // Now add the unswitched switch.
457   auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH);
458 
459   // Rewrite the IR for the unswitched basic blocks. This requires two steps.
460   // First, we split any exit blocks with remaining in-loop predecessors. Then
461   // we update the PHIs in one of two ways depending on if there was a split.
462   // We walk in reverse so that we split in the same order as the cases
463   // appeared. This is purely for convenience of reading the resulting IR, but
464   // it doesn't cost anything really.
465   SmallPtrSet<BasicBlock *, 2> UnswitchedExitBBs;
466   SmallDenseMap<BasicBlock *, BasicBlock *, 2> SplitExitBBMap;
467   // Handle the default exit if necessary.
468   // FIXME: It'd be great if we could merge this with the loop below but LLVM's
469   // ranges aren't quite powerful enough yet.
470   if (DefaultExitBB) {
471     if (pred_empty(DefaultExitBB)) {
472       UnswitchedExitBBs.insert(DefaultExitBB);
473       rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH);
474     } else {
475       auto *SplitBB =
476           SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI);
477       rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB,
478                                                 *ParentBB, *OldPH);
479       updateLoopExitIDom(DefaultExitBB, L, DT);
480       DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB;
481     }
482   }
483   // Note that we must use a reference in the for loop so that we update the
484   // container.
485   for (auto &CasePair : reverse(ExitCases)) {
486     // Grab a reference to the exit block in the pair so that we can update it.
487     BasicBlock *ExitBB = CasePair.second;
488 
489     // If this case is the last edge into the exit block, we can simply reuse it
490     // as it will no longer be a loop exit. No mapping necessary.
491     if (pred_empty(ExitBB)) {
492       // Only rewrite once.
493       if (UnswitchedExitBBs.insert(ExitBB).second)
494         rewritePHINodesForUnswitchedExitBlock(*ExitBB, *ParentBB, *OldPH);
495       continue;
496     }
497 
498     // Otherwise we need to split the exit block so that we retain an exit
499     // block from the loop and a target for the unswitched condition.
500     BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
501     if (!SplitExitBB) {
502       // If this is the first time we see this, do the split and remember it.
503       SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI);
504       rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB,
505                                                 *ParentBB, *OldPH);
506       updateLoopExitIDom(ExitBB, L, DT);
507     }
508     // Update the case pair to point to the split block.
509     CasePair.second = SplitExitBB;
510   }
511 
512   // Now add the unswitched cases. We do this in reverse order as we built them
513   // in reverse order.
514   for (auto CasePair : reverse(ExitCases)) {
515     ConstantInt *CaseVal = CasePair.first;
516     BasicBlock *UnswitchedBB = CasePair.second;
517 
518     NewSI->addCase(CaseVal, UnswitchedBB);
519     updateDTAfterUnswitch(UnswitchedBB, OldPH, DT);
520   }
521 
522   // If the default was unswitched, re-point it and add explicit cases for
523   // entering the loop.
524   if (DefaultExitBB) {
525     NewSI->setDefaultDest(DefaultExitBB);
526     updateDTAfterUnswitch(DefaultExitBB, OldPH, DT);
527 
528     // We removed all the exit cases, so we just copy the cases to the
529     // unswitched switch.
530     for (auto Case : SI.cases())
531       NewSI->addCase(Case.getCaseValue(), NewPH);
532   }
533 
534   // If we ended up with a common successor for every path through the switch
535   // after unswitching, rewrite it to an unconditional branch to make it easy
536   // to recognize. Otherwise we potentially have to recognize the default case
537   // pointing at unreachable and other complexity.
538   if (CommonSuccBB) {
539     BasicBlock *BB = SI.getParent();
540     SI.eraseFromParent();
541     BranchInst::Create(CommonSuccBB, BB);
542   }
543 
544   DT.verifyDomTree();
545   ++NumTrivial;
546   ++NumSwitches;
547   return true;
548 }
549 
550 /// This routine scans the loop to find a branch or switch which occurs before
551 /// any side effects occur. These can potentially be unswitched without
552 /// duplicating the loop. If a branch or switch is successfully unswitched the
553 /// scanning continues to see if subsequent branches or switches have become
554 /// trivial. Once all trivial candidates have been unswitched, this routine
555 /// returns.
556 ///
557 /// The return value indicates whether anything was unswitched (and therefore
558 /// changed).
559 static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT,
560                                          LoopInfo &LI) {
561   bool Changed = false;
562 
563   // If loop header has only one reachable successor we should keep looking for
564   // trivial condition candidates in the successor as well. An alternative is
565   // to constant fold conditions and merge successors into loop header (then we
566   // only need to check header's terminator). The reason for not doing this in
567   // LoopUnswitch pass is that it could potentially break LoopPassManager's
568   // invariants. Folding dead branches could either eliminate the current loop
569   // or make other loops unreachable. LCSSA form might also not be preserved
570   // after deleting branches. The following code keeps traversing loop header's
571   // successors until it finds the trivial condition candidate (condition that
572   // is not a constant). Since unswitching generates branches with constant
573   // conditions, this scenario could be very common in practice.
574   BasicBlock *CurrentBB = L.getHeader();
575   SmallPtrSet<BasicBlock *, 8> Visited;
576   Visited.insert(CurrentBB);
577   do {
578     // Check if there are any side-effecting instructions (e.g. stores, calls,
579     // volatile loads) in the part of the loop that the code *would* execute
580     // without unswitching.
581     if (llvm::any_of(*CurrentBB,
582                      [](Instruction &I) { return I.mayHaveSideEffects(); }))
583       return Changed;
584 
585     TerminatorInst *CurrentTerm = CurrentBB->getTerminator();
586 
587     if (auto *SI = dyn_cast<SwitchInst>(CurrentTerm)) {
588       // Don't bother trying to unswitch past a switch with a constant
589       // condition. This should be removed prior to running this pass by
590       // simplify-cfg.
591       if (isa<Constant>(SI->getCondition()))
592         return Changed;
593 
594       if (!unswitchTrivialSwitch(L, *SI, DT, LI))
595         // Coludn't unswitch this one so we're done.
596         return Changed;
597 
598       // Mark that we managed to unswitch something.
599       Changed = true;
600 
601       // If unswitching turned the terminator into an unconditional branch then
602       // we can continue. The unswitching logic specifically works to fold any
603       // cases it can into an unconditional branch to make it easier to
604       // recognize here.
605       auto *BI = dyn_cast<BranchInst>(CurrentBB->getTerminator());
606       if (!BI || BI->isConditional())
607         return Changed;
608 
609       CurrentBB = BI->getSuccessor(0);
610       continue;
611     }
612 
613     auto *BI = dyn_cast<BranchInst>(CurrentTerm);
614     if (!BI)
615       // We do not understand other terminator instructions.
616       return Changed;
617 
618     // Don't bother trying to unswitch past an unconditional branch or a branch
619     // with a constant value. These should be removed by simplify-cfg prior to
620     // running this pass.
621     if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
622       return Changed;
623 
624     // Found a trivial condition candidate: non-foldable conditional branch. If
625     // we fail to unswitch this, we can't do anything else that is trivial.
626     if (!unswitchTrivialBranch(L, *BI, DT, LI))
627       return Changed;
628 
629     // Mark that we managed to unswitch something.
630     Changed = true;
631 
632     // We unswitched the branch. This should always leave us with an
633     // unconditional branch that we can follow now.
634     BI = cast<BranchInst>(CurrentBB->getTerminator());
635     assert(!BI->isConditional() &&
636            "Cannot form a conditional branch by unswitching1");
637     CurrentBB = BI->getSuccessor(0);
638 
639     // When continuing, if we exit the loop or reach a previous visited block,
640     // then we can not reach any trivial condition candidates (unfoldable
641     // branch instructions or switch instructions) and no unswitch can happen.
642   } while (L.contains(CurrentBB) && Visited.insert(CurrentBB).second);
643 
644   return Changed;
645 }
646 
647 /// Unswitch control flow predicated on loop invariant conditions.
648 ///
649 /// This first hoists all branches or switches which are trivial (IE, do not
650 /// require duplicating any part of the loop) out of the loop body. It then
651 /// looks at other loop invariant control flows and tries to unswitch those as
652 /// well by cloning the loop if the result is small enough.
653 static bool unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI,
654                          AssumptionCache &AC) {
655   assert(L.isLCSSAForm(DT) &&
656          "Loops must be in LCSSA form before unswitching.");
657   bool Changed = false;
658 
659   // Must be in loop simplified form: we need a preheader and dedicated exits.
660   if (!L.isLoopSimplifyForm())
661     return false;
662 
663   // Try trivial unswitch first before loop over other basic blocks in the loop.
664   Changed |= unswitchAllTrivialConditions(L, DT, LI);
665 
666   // FIXME: Add support for non-trivial unswitching by cloning the loop.
667 
668   return Changed;
669 }
670 
671 PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
672                                               LoopStandardAnalysisResults &AR,
673                                               LPMUpdater &U) {
674   Function &F = *L.getHeader()->getParent();
675   (void)F;
676 
677   DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L << "\n");
678 
679   if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC))
680     return PreservedAnalyses::all();
681 
682 #ifndef NDEBUG
683   // Historically this pass has had issues with the dominator tree so verify it
684   // in asserts builds.
685   AR.DT.verifyDomTree();
686 #endif
687   return getLoopPassPreservedAnalyses();
688 }
689 
690 namespace {
691 class SimpleLoopUnswitchLegacyPass : public LoopPass {
692 public:
693   static char ID; // Pass ID, replacement for typeid
694   explicit SimpleLoopUnswitchLegacyPass() : LoopPass(ID) {
695     initializeSimpleLoopUnswitchLegacyPassPass(
696         *PassRegistry::getPassRegistry());
697   }
698 
699   bool runOnLoop(Loop *L, LPPassManager &LPM) override;
700 
701   void getAnalysisUsage(AnalysisUsage &AU) const override {
702     AU.addRequired<AssumptionCacheTracker>();
703     getLoopAnalysisUsage(AU);
704   }
705 };
706 } // namespace
707 
708 bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
709   if (skipLoop(L))
710     return false;
711 
712   Function &F = *L->getHeader()->getParent();
713 
714   DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L << "\n");
715 
716   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
717   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
718   auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
719 
720   bool Changed = unswitchLoop(*L, DT, LI, AC);
721 
722 #ifndef NDEBUG
723   // Historically this pass has had issues with the dominator tree so verify it
724   // in asserts builds.
725   DT.verifyDomTree();
726 #endif
727   return Changed;
728 }
729 
730 char SimpleLoopUnswitchLegacyPass::ID = 0;
731 INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
732                       "Simple unswitch loops", false, false)
733 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
734 INITIALIZE_PASS_DEPENDENCY(LoopPass)
735 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
736 INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
737                     "Simple unswitch loops", false, false)
738 
739 Pass *llvm::createSimpleLoopUnswitchLegacyPass() {
740   return new SimpleLoopUnswitchLegacyPass();
741 }
742