1a369a457SEugene Zelenko //===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ---------===//
21353f9a4SChandler Carruth //
31353f9a4SChandler Carruth //                     The LLVM Compiler Infrastructure
41353f9a4SChandler Carruth //
51353f9a4SChandler Carruth // This file is distributed under the University of Illinois Open Source
61353f9a4SChandler Carruth // License. See LICENSE.TXT for details.
71353f9a4SChandler Carruth //
81353f9a4SChandler Carruth //===----------------------------------------------------------------------===//
91353f9a4SChandler Carruth 
10a369a457SEugene Zelenko #include "llvm/ADT/DenseMap.h"
11a369a457SEugene Zelenko #include "llvm/ADT/Sequence.h"
12a369a457SEugene Zelenko #include "llvm/ADT/SetVector.h"
131353f9a4SChandler Carruth #include "llvm/ADT/SmallPtrSet.h"
14a369a457SEugene Zelenko #include "llvm/ADT/SmallVector.h"
151353f9a4SChandler Carruth #include "llvm/ADT/Statistic.h"
16a369a457SEugene Zelenko #include "llvm/ADT/STLExtras.h"
17a369a457SEugene Zelenko #include "llvm/ADT/Twine.h"
181353f9a4SChandler Carruth #include "llvm/Analysis/AssumptionCache.h"
19a369a457SEugene Zelenko #include "llvm/Analysis/LoopAnalysisManager.h"
201353f9a4SChandler Carruth #include "llvm/Analysis/LoopInfo.h"
211353f9a4SChandler Carruth #include "llvm/Analysis/LoopPass.h"
22a369a457SEugene Zelenko #include "llvm/IR/BasicBlock.h"
23a369a457SEugene Zelenko #include "llvm/IR/Constant.h"
241353f9a4SChandler Carruth #include "llvm/IR/Constants.h"
251353f9a4SChandler Carruth #include "llvm/IR/Dominators.h"
261353f9a4SChandler Carruth #include "llvm/IR/Function.h"
27a369a457SEugene Zelenko #include "llvm/IR/InstrTypes.h"
28a369a457SEugene Zelenko #include "llvm/IR/Instruction.h"
291353f9a4SChandler Carruth #include "llvm/IR/Instructions.h"
30a369a457SEugene Zelenko #include "llvm/IR/Use.h"
31a369a457SEugene Zelenko #include "llvm/IR/Value.h"
32a369a457SEugene Zelenko #include "llvm/Pass.h"
33a369a457SEugene Zelenko #include "llvm/Support/Casting.h"
341353f9a4SChandler Carruth #include "llvm/Support/Debug.h"
35a369a457SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
36a369a457SEugene Zelenko #include "llvm/Support/GenericDomTree.h"
371353f9a4SChandler Carruth #include "llvm/Support/raw_ostream.h"
381353f9a4SChandler Carruth #include "llvm/Transforms/Utils/BasicBlockUtils.h"
391353f9a4SChandler Carruth #include "llvm/Transforms/Utils/LoopUtils.h"
40a369a457SEugene Zelenko #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
41a369a457SEugene Zelenko #include <algorithm>
42a369a457SEugene Zelenko #include <cassert>
43a369a457SEugene Zelenko #include <iterator>
44a369a457SEugene Zelenko #include <utility>
451353f9a4SChandler Carruth 
461353f9a4SChandler Carruth #define DEBUG_TYPE "simple-loop-unswitch"
471353f9a4SChandler Carruth 
481353f9a4SChandler Carruth using namespace llvm;
491353f9a4SChandler Carruth 
501353f9a4SChandler Carruth STATISTIC(NumBranches, "Number of branches unswitched");
511353f9a4SChandler Carruth STATISTIC(NumSwitches, "Number of switches unswitched");
521353f9a4SChandler Carruth STATISTIC(NumTrivial, "Number of unswitches that are trivial");
531353f9a4SChandler Carruth 
541353f9a4SChandler Carruth static void replaceLoopUsesWithConstant(Loop &L, Value &LIC,
551353f9a4SChandler Carruth                                         Constant &Replacement) {
561353f9a4SChandler Carruth   assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
571353f9a4SChandler Carruth 
581353f9a4SChandler Carruth   // Replace uses of LIC in the loop with the given constant.
591353f9a4SChandler Carruth   for (auto UI = LIC.use_begin(), UE = LIC.use_end(); UI != UE;) {
601353f9a4SChandler Carruth     // Grab the use and walk past it so we can clobber it in the use list.
611353f9a4SChandler Carruth     Use *U = &*UI++;
621353f9a4SChandler Carruth     Instruction *UserI = dyn_cast<Instruction>(U->getUser());
631353f9a4SChandler Carruth     if (!UserI || !L.contains(UserI))
641353f9a4SChandler Carruth       continue;
651353f9a4SChandler Carruth 
661353f9a4SChandler Carruth     // Replace this use within the loop body.
671353f9a4SChandler Carruth     *U = &Replacement;
681353f9a4SChandler Carruth   }
691353f9a4SChandler Carruth }
701353f9a4SChandler Carruth 
711353f9a4SChandler Carruth /// Update the dominator tree after removing one exiting predecessor of a loop
721353f9a4SChandler Carruth /// exit block.
731353f9a4SChandler Carruth static void updateLoopExitIDom(BasicBlock *LoopExitBB, Loop &L,
741353f9a4SChandler Carruth                                DominatorTree &DT) {
751353f9a4SChandler Carruth   assert(pred_begin(LoopExitBB) != pred_end(LoopExitBB) &&
761353f9a4SChandler Carruth          "Cannot have empty predecessors of the loop exit block if we split "
771353f9a4SChandler Carruth          "off a block to unswitch!");
781353f9a4SChandler Carruth 
791353f9a4SChandler Carruth   BasicBlock *IDom = *pred_begin(LoopExitBB);
801353f9a4SChandler Carruth   // Walk all of the other predecessors finding the nearest common dominator
811353f9a4SChandler Carruth   // until all predecessors are covered or we reach the loop header. The loop
821353f9a4SChandler Carruth   // header necessarily dominates all loop exit blocks in loop simplified form
831353f9a4SChandler Carruth   // so we can early-exit the moment we hit that block.
841353f9a4SChandler Carruth   for (auto PI = std::next(pred_begin(LoopExitBB)), PE = pred_end(LoopExitBB);
851353f9a4SChandler Carruth        PI != PE && IDom != L.getHeader(); ++PI)
861353f9a4SChandler Carruth     IDom = DT.findNearestCommonDominator(IDom, *PI);
871353f9a4SChandler Carruth 
881353f9a4SChandler Carruth   DT.changeImmediateDominator(LoopExitBB, IDom);
891353f9a4SChandler Carruth }
901353f9a4SChandler Carruth 
911353f9a4SChandler Carruth /// Update the dominator tree after unswitching a particular former exit block.
921353f9a4SChandler Carruth ///
931353f9a4SChandler Carruth /// This handles the full update of the dominator tree after hoisting a block
941353f9a4SChandler Carruth /// that previously was an exit block (or split off of an exit block) up to be
951353f9a4SChandler Carruth /// reached from the new immediate dominator of the preheader.
961353f9a4SChandler Carruth ///
971353f9a4SChandler Carruth /// The common case is simple -- we just move the unswitched block to have an
981353f9a4SChandler Carruth /// immediate dominator of the old preheader. But in complex cases, there may
991353f9a4SChandler Carruth /// be other blocks reachable from the unswitched block that are immediately
1001353f9a4SChandler Carruth /// dominated by some node between the unswitched one and the old preheader.
1011353f9a4SChandler Carruth /// All of these also need to be hoisted in the dominator tree. We also want to
1021353f9a4SChandler Carruth /// minimize queries to the dominator tree because each step of this
1031353f9a4SChandler Carruth /// invalidates any DFS numbers that would make queries fast.
1041353f9a4SChandler Carruth static void updateDTAfterUnswitch(BasicBlock *UnswitchedBB, BasicBlock *OldPH,
1051353f9a4SChandler Carruth                                   DominatorTree &DT) {
1061353f9a4SChandler Carruth   DomTreeNode *OldPHNode = DT[OldPH];
1071353f9a4SChandler Carruth   DomTreeNode *UnswitchedNode = DT[UnswitchedBB];
1081353f9a4SChandler Carruth   // If the dominator tree has already been updated for this unswitched node,
1091353f9a4SChandler Carruth   // we're done. This makes it easier to use this routine if there are multiple
1101353f9a4SChandler Carruth   // paths to the same unswitched destination.
1111353f9a4SChandler Carruth   if (UnswitchedNode->getIDom() == OldPHNode)
1121353f9a4SChandler Carruth     return;
1131353f9a4SChandler Carruth 
1141353f9a4SChandler Carruth   // First collect the domtree nodes that we are hoisting over. These are the
1151353f9a4SChandler Carruth   // set of nodes which may have children that need to be hoisted as well.
1161353f9a4SChandler Carruth   SmallPtrSet<DomTreeNode *, 4> DomChain;
1171353f9a4SChandler Carruth   for (auto *IDom = UnswitchedNode->getIDom(); IDom != OldPHNode;
1181353f9a4SChandler Carruth        IDom = IDom->getIDom())
1191353f9a4SChandler Carruth     DomChain.insert(IDom);
1201353f9a4SChandler Carruth 
1211353f9a4SChandler Carruth   // The unswitched block ends up immediately dominated by the old preheader --
1221353f9a4SChandler Carruth   // regardless of whether it is the loop exit block or split off of the loop
1231353f9a4SChandler Carruth   // exit block.
1241353f9a4SChandler Carruth   DT.changeImmediateDominator(UnswitchedNode, OldPHNode);
1251353f9a4SChandler Carruth 
126*dd2e275aSChandler Carruth   // For everything that moves up the dominator tree, we need to examine the
127*dd2e275aSChandler Carruth   // dominator frontier to see if it additionally should move up the dominator
128*dd2e275aSChandler Carruth   // tree. This lambda appends the dominator frontier for a node on the
129*dd2e275aSChandler Carruth   // worklist.
130*dd2e275aSChandler Carruth   //
131*dd2e275aSChandler Carruth   // Note that we don't currently use the IDFCalculator here for two reasons:
132*dd2e275aSChandler Carruth   // 1) It computes dominator tree levels for the entire function on each run
133*dd2e275aSChandler Carruth   //    of 'compute'. While this isn't terrible, given that we expect to update
134*dd2e275aSChandler Carruth   //    relatively small subtrees of the domtree, it isn't necessarily the right
135*dd2e275aSChandler Carruth   //    tradeoff.
136*dd2e275aSChandler Carruth   // 2) The interface doesn't fit this usage well. It doesn't operate in
137*dd2e275aSChandler Carruth   //    append-only, and builds several sets that we don't need.
138*dd2e275aSChandler Carruth   //
139*dd2e275aSChandler Carruth   // FIXME: Neither of these issues are a big deal and could be addressed with
140*dd2e275aSChandler Carruth   // some amount of refactoring of IDFCalculator. That would allow us to share
141*dd2e275aSChandler Carruth   // the core logic here (which is solving the same core problem).
1421353f9a4SChandler Carruth   SmallSetVector<BasicBlock *, 4> Worklist;
143*dd2e275aSChandler Carruth   SmallVector<DomTreeNode *, 4> DomNodes;
144*dd2e275aSChandler Carruth   SmallPtrSet<BasicBlock *, 4> DomSet;
145*dd2e275aSChandler Carruth   auto AppendDomFrontier = [&](DomTreeNode *Node) {
146*dd2e275aSChandler Carruth     assert(DomNodes.empty() && "Must start with no dominator nodes.");
147*dd2e275aSChandler Carruth     assert(DomSet.empty() && "Must start with an empty dominator set.");
148*dd2e275aSChandler Carruth 
149*dd2e275aSChandler Carruth     // First flatten this subtree into sequence of nodes by doing a pre-order
150*dd2e275aSChandler Carruth     // walk.
151*dd2e275aSChandler Carruth     DomNodes.push_back(Node);
152*dd2e275aSChandler Carruth     // We intentionally re-evaluate the size as each node can add new children.
153*dd2e275aSChandler Carruth     // Because this is a tree walk, this cannot add any duplicates.
154*dd2e275aSChandler Carruth     for (int i = 0; i < (int)DomNodes.size(); ++i)
155*dd2e275aSChandler Carruth       DomNodes.insert(DomNodes.end(), DomNodes[i]->begin(), DomNodes[i]->end());
156*dd2e275aSChandler Carruth 
157*dd2e275aSChandler Carruth     // Now create a set of the basic blocks so we can quickly test for
158*dd2e275aSChandler Carruth     // dominated successors. We could in theory use the DFS numbers of the
159*dd2e275aSChandler Carruth     // dominator tree for this, but we want this to remain predictably fast
160*dd2e275aSChandler Carruth     // even while we mutate the dominator tree in ways that would invalidate
161*dd2e275aSChandler Carruth     // the DFS numbering.
162*dd2e275aSChandler Carruth     for (DomTreeNode *InnerN : DomNodes)
163*dd2e275aSChandler Carruth       DomSet.insert(InnerN->getBlock());
164*dd2e275aSChandler Carruth 
165*dd2e275aSChandler Carruth     // Now re-walk the nodes, appending every successor of every node that isn't
166*dd2e275aSChandler Carruth     // in the set. Note that we don't append the node itself, even though if it
167*dd2e275aSChandler Carruth     // is a successor it does not strictly dominate itself and thus it would be
168*dd2e275aSChandler Carruth     // part of the dominance frontier. The reason we don't append it is that
169*dd2e275aSChandler Carruth     // the node passed in came *from* the worklist and so it has already been
170*dd2e275aSChandler Carruth     // processed.
171*dd2e275aSChandler Carruth     for (DomTreeNode *InnerN : DomNodes)
172*dd2e275aSChandler Carruth       for (BasicBlock *SuccBB : successors(InnerN->getBlock()))
173*dd2e275aSChandler Carruth         if (!DomSet.count(SuccBB))
1741353f9a4SChandler Carruth           Worklist.insert(SuccBB);
1751353f9a4SChandler Carruth 
176*dd2e275aSChandler Carruth     DomNodes.clear();
177*dd2e275aSChandler Carruth     DomSet.clear();
178*dd2e275aSChandler Carruth   };
179*dd2e275aSChandler Carruth 
180*dd2e275aSChandler Carruth   // Append the initial dom frontier nodes.
181*dd2e275aSChandler Carruth   AppendDomFrontier(UnswitchedNode);
182*dd2e275aSChandler Carruth 
1831353f9a4SChandler Carruth   // Walk the worklist. We grow the list in the loop and so must recompute size.
1841353f9a4SChandler Carruth   for (int i = 0; i < (int)Worklist.size(); ++i) {
1851353f9a4SChandler Carruth     auto *BB = Worklist[i];
1861353f9a4SChandler Carruth 
1871353f9a4SChandler Carruth     DomTreeNode *Node = DT[BB];
1881353f9a4SChandler Carruth     assert(!DomChain.count(Node) &&
1891353f9a4SChandler Carruth            "Cannot be dominated by a block you can reach!");
190*dd2e275aSChandler Carruth 
191*dd2e275aSChandler Carruth     // If this block had an immediate dominator somewhere in the chain
192*dd2e275aSChandler Carruth     // we hoisted over, then its position in the domtree needs to move as it is
193*dd2e275aSChandler Carruth     // reachable from a node hoisted over this chain.
1941353f9a4SChandler Carruth     if (!DomChain.count(Node->getIDom()))
1951353f9a4SChandler Carruth       continue;
1961353f9a4SChandler Carruth 
1971353f9a4SChandler Carruth     DT.changeImmediateDominator(Node, OldPHNode);
198*dd2e275aSChandler Carruth 
199*dd2e275aSChandler Carruth     // Now add this node's dominator frontier to the worklist as well.
200*dd2e275aSChandler Carruth     AppendDomFrontier(Node);
2011353f9a4SChandler Carruth   }
2021353f9a4SChandler Carruth }
2031353f9a4SChandler Carruth 
204d869b188SChandler Carruth /// Check that all the LCSSA PHI nodes in the loop exit block have trivial
205d869b188SChandler Carruth /// incoming values along this edge.
206d869b188SChandler Carruth static bool areLoopExitPHIsLoopInvariant(Loop &L, BasicBlock &ExitingBB,
207d869b188SChandler Carruth                                          BasicBlock &ExitBB) {
208d869b188SChandler Carruth   for (Instruction &I : ExitBB) {
209d869b188SChandler Carruth     auto *PN = dyn_cast<PHINode>(&I);
210d869b188SChandler Carruth     if (!PN)
211d869b188SChandler Carruth       // No more PHIs to check.
212d869b188SChandler Carruth       return true;
213d869b188SChandler Carruth 
214d869b188SChandler Carruth     // If the incoming value for this edge isn't loop invariant the unswitch
215d869b188SChandler Carruth     // won't be trivial.
216d869b188SChandler Carruth     if (!L.isLoopInvariant(PN->getIncomingValueForBlock(&ExitingBB)))
217d869b188SChandler Carruth       return false;
218d869b188SChandler Carruth   }
219d869b188SChandler Carruth   llvm_unreachable("Basic blocks should never be empty!");
220d869b188SChandler Carruth }
221d869b188SChandler Carruth 
222d869b188SChandler Carruth /// Rewrite the PHI nodes in an unswitched loop exit basic block.
223d869b188SChandler Carruth ///
224d869b188SChandler Carruth /// Requires that the loop exit and unswitched basic block are the same, and
225d869b188SChandler Carruth /// that the exiting block was a unique predecessor of that block. Rewrites the
226d869b188SChandler Carruth /// PHI nodes in that block such that what were LCSSA PHI nodes become trivial
227d869b188SChandler Carruth /// PHI nodes from the old preheader that now contains the unswitched
228d869b188SChandler Carruth /// terminator.
229d869b188SChandler Carruth static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB,
230d869b188SChandler Carruth                                                   BasicBlock &OldExitingBB,
231d869b188SChandler Carruth                                                   BasicBlock &OldPH) {
232d869b188SChandler Carruth   for (Instruction &I : UnswitchedBB) {
233d869b188SChandler Carruth     auto *PN = dyn_cast<PHINode>(&I);
234d869b188SChandler Carruth     if (!PN)
235d869b188SChandler Carruth       // No more PHIs to check.
236d869b188SChandler Carruth       break;
237d869b188SChandler Carruth 
238d869b188SChandler Carruth     // When the loop exit is directly unswitched we just need to update the
239d869b188SChandler Carruth     // incoming basic block. We loop to handle weird cases with repeated
240d869b188SChandler Carruth     // incoming blocks, but expect to typically only have one operand here.
241a369a457SEugene Zelenko     for (auto i : seq<int>(0, PN->getNumOperands())) {
242d869b188SChandler Carruth       assert(PN->getIncomingBlock(i) == &OldExitingBB &&
243d869b188SChandler Carruth              "Found incoming block different from unique predecessor!");
244d869b188SChandler Carruth       PN->setIncomingBlock(i, &OldPH);
245d869b188SChandler Carruth     }
246d869b188SChandler Carruth   }
247d869b188SChandler Carruth }
248d869b188SChandler Carruth 
249d869b188SChandler Carruth /// Rewrite the PHI nodes in the loop exit basic block and the split off
250d869b188SChandler Carruth /// unswitched block.
251d869b188SChandler Carruth ///
252d869b188SChandler Carruth /// Because the exit block remains an exit from the loop, this rewrites the
253d869b188SChandler Carruth /// LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI
254d869b188SChandler Carruth /// nodes into the unswitched basic block to select between the value in the
255d869b188SChandler Carruth /// old preheader and the loop exit.
256d869b188SChandler Carruth static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock &ExitBB,
257d869b188SChandler Carruth                                                       BasicBlock &UnswitchedBB,
258d869b188SChandler Carruth                                                       BasicBlock &OldExitingBB,
259d869b188SChandler Carruth                                                       BasicBlock &OldPH) {
260d869b188SChandler Carruth   assert(&ExitBB != &UnswitchedBB &&
261d869b188SChandler Carruth          "Must have different loop exit and unswitched blocks!");
262d869b188SChandler Carruth   Instruction *InsertPt = &*UnswitchedBB.begin();
263d869b188SChandler Carruth   for (Instruction &I : ExitBB) {
264d869b188SChandler Carruth     auto *PN = dyn_cast<PHINode>(&I);
265d869b188SChandler Carruth     if (!PN)
266d869b188SChandler Carruth       // No more PHIs to check.
267d869b188SChandler Carruth       break;
268d869b188SChandler Carruth 
269d869b188SChandler Carruth     auto *NewPN = PHINode::Create(PN->getType(), /*NumReservedValues*/ 2,
270d869b188SChandler Carruth                                   PN->getName() + ".split", InsertPt);
271d869b188SChandler Carruth 
272d869b188SChandler Carruth     // Walk backwards over the old PHI node's inputs to minimize the cost of
273d869b188SChandler Carruth     // removing each one. We have to do this weird loop manually so that we
274d869b188SChandler Carruth     // create the same number of new incoming edges in the new PHI as we expect
275d869b188SChandler Carruth     // each case-based edge to be included in the unswitched switch in some
276d869b188SChandler Carruth     // cases.
277d869b188SChandler Carruth     // FIXME: This is really, really gross. It would be much cleaner if LLVM
278d869b188SChandler Carruth     // allowed us to create a single entry for a predecessor block without
279d869b188SChandler Carruth     // having separate entries for each "edge" even though these edges are
280d869b188SChandler Carruth     // required to produce identical results.
281d869b188SChandler Carruth     for (int i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
282d869b188SChandler Carruth       if (PN->getIncomingBlock(i) != &OldExitingBB)
283d869b188SChandler Carruth         continue;
284d869b188SChandler Carruth 
285d869b188SChandler Carruth       Value *Incoming = PN->removeIncomingValue(i);
286d869b188SChandler Carruth       NewPN->addIncoming(Incoming, &OldPH);
287d869b188SChandler Carruth     }
288d869b188SChandler Carruth 
289d869b188SChandler Carruth     // Now replace the old PHI with the new one and wire the old one in as an
290d869b188SChandler Carruth     // input to the new one.
291d869b188SChandler Carruth     PN->replaceAllUsesWith(NewPN);
292d869b188SChandler Carruth     NewPN->addIncoming(PN, &ExitBB);
293d869b188SChandler Carruth   }
294d869b188SChandler Carruth }
295d869b188SChandler Carruth 
2961353f9a4SChandler Carruth /// Unswitch a trivial branch if the condition is loop invariant.
2971353f9a4SChandler Carruth ///
2981353f9a4SChandler Carruth /// This routine should only be called when loop code leading to the branch has
2991353f9a4SChandler Carruth /// been validated as trivial (no side effects). This routine checks if the
3001353f9a4SChandler Carruth /// condition is invariant and one of the successors is a loop exit. This
3011353f9a4SChandler Carruth /// allows us to unswitch without duplicating the loop, making it trivial.
3021353f9a4SChandler Carruth ///
3031353f9a4SChandler Carruth /// If this routine fails to unswitch the branch it returns false.
3041353f9a4SChandler Carruth ///
3051353f9a4SChandler Carruth /// If the branch can be unswitched, this routine splits the preheader and
3061353f9a4SChandler Carruth /// hoists the branch above that split. Preserves loop simplified form
3071353f9a4SChandler Carruth /// (splitting the exit block as necessary). It simplifies the branch within
3081353f9a4SChandler Carruth /// the loop to an unconditional branch but doesn't remove it entirely. Further
3091353f9a4SChandler Carruth /// cleanup can be done with some simplify-cfg like pass.
3101353f9a4SChandler Carruth static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT,
3111353f9a4SChandler Carruth                                   LoopInfo &LI) {
3121353f9a4SChandler Carruth   assert(BI.isConditional() && "Can only unswitch a conditional branch!");
3131353f9a4SChandler Carruth   DEBUG(dbgs() << "  Trying to unswitch branch: " << BI << "\n");
3141353f9a4SChandler Carruth 
3151353f9a4SChandler Carruth   Value *LoopCond = BI.getCondition();
3161353f9a4SChandler Carruth 
3171353f9a4SChandler Carruth   // Need a trivial loop condition to unswitch.
3181353f9a4SChandler Carruth   if (!L.isLoopInvariant(LoopCond))
3191353f9a4SChandler Carruth     return false;
3201353f9a4SChandler Carruth 
3211353f9a4SChandler Carruth   // FIXME: We should compute this once at the start and update it!
3221353f9a4SChandler Carruth   SmallVector<BasicBlock *, 16> ExitBlocks;
3231353f9a4SChandler Carruth   L.getExitBlocks(ExitBlocks);
3241353f9a4SChandler Carruth   SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(),
3251353f9a4SChandler Carruth                                              ExitBlocks.end());
3261353f9a4SChandler Carruth 
3271353f9a4SChandler Carruth   // Check to see if a successor of the branch is guaranteed to
3281353f9a4SChandler Carruth   // exit through a unique exit block without having any
3291353f9a4SChandler Carruth   // side-effects.  If so, determine the value of Cond that causes
3301353f9a4SChandler Carruth   // it to do this.
3311353f9a4SChandler Carruth   ConstantInt *CondVal = ConstantInt::getTrue(BI.getContext());
3321353f9a4SChandler Carruth   ConstantInt *Replacement = ConstantInt::getFalse(BI.getContext());
3331353f9a4SChandler Carruth   int LoopExitSuccIdx = 0;
3341353f9a4SChandler Carruth   auto *LoopExitBB = BI.getSuccessor(0);
3351353f9a4SChandler Carruth   if (!ExitBlockSet.count(LoopExitBB)) {
3361353f9a4SChandler Carruth     std::swap(CondVal, Replacement);
3371353f9a4SChandler Carruth     LoopExitSuccIdx = 1;
3381353f9a4SChandler Carruth     LoopExitBB = BI.getSuccessor(1);
3391353f9a4SChandler Carruth     if (!ExitBlockSet.count(LoopExitBB))
3401353f9a4SChandler Carruth       return false;
3411353f9a4SChandler Carruth   }
3421353f9a4SChandler Carruth   auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx);
3431353f9a4SChandler Carruth   assert(L.contains(ContinueBB) &&
3441353f9a4SChandler Carruth          "Cannot have both successors exit and still be in the loop!");
3451353f9a4SChandler Carruth 
346d869b188SChandler Carruth   auto *ParentBB = BI.getParent();
347d869b188SChandler Carruth   if (!areLoopExitPHIsLoopInvariant(L, *ParentBB, *LoopExitBB))
3481353f9a4SChandler Carruth     return false;
3491353f9a4SChandler Carruth 
3501353f9a4SChandler Carruth   DEBUG(dbgs() << "    unswitching trivial branch when: " << CondVal
3511353f9a4SChandler Carruth                << " == " << LoopCond << "\n");
3521353f9a4SChandler Carruth 
3531353f9a4SChandler Carruth   // Split the preheader, so that we know that there is a safe place to insert
3541353f9a4SChandler Carruth   // the conditional branch. We will change the preheader to have a conditional
3551353f9a4SChandler Carruth   // branch on LoopCond.
3561353f9a4SChandler Carruth   BasicBlock *OldPH = L.getLoopPreheader();
3571353f9a4SChandler Carruth   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI);
3581353f9a4SChandler Carruth 
3591353f9a4SChandler Carruth   // Now that we have a place to insert the conditional branch, create a place
3601353f9a4SChandler Carruth   // to branch to: this is the exit block out of the loop that we are
3611353f9a4SChandler Carruth   // unswitching. We need to split this if there are other loop predecessors.
3621353f9a4SChandler Carruth   // Because the loop is in simplified form, *any* other predecessor is enough.
3631353f9a4SChandler Carruth   BasicBlock *UnswitchedBB;
3641353f9a4SChandler Carruth   if (BasicBlock *PredBB = LoopExitBB->getUniquePredecessor()) {
3651353f9a4SChandler Carruth     (void)PredBB;
366d869b188SChandler Carruth     assert(PredBB == BI.getParent() &&
367d869b188SChandler Carruth            "A branch's parent isn't a predecessor!");
3681353f9a4SChandler Carruth     UnswitchedBB = LoopExitBB;
3691353f9a4SChandler Carruth   } else {
3701353f9a4SChandler Carruth     UnswitchedBB = SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI);
3711353f9a4SChandler Carruth   }
3721353f9a4SChandler Carruth 
3731353f9a4SChandler Carruth   // Now splice the branch to gate reaching the new preheader and re-point its
3741353f9a4SChandler Carruth   // successors.
3751353f9a4SChandler Carruth   OldPH->getInstList().splice(std::prev(OldPH->end()),
3761353f9a4SChandler Carruth                               BI.getParent()->getInstList(), BI);
3771353f9a4SChandler Carruth   OldPH->getTerminator()->eraseFromParent();
3781353f9a4SChandler Carruth   BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB);
3791353f9a4SChandler Carruth   BI.setSuccessor(1 - LoopExitSuccIdx, NewPH);
3801353f9a4SChandler Carruth 
3811353f9a4SChandler Carruth   // Create a new unconditional branch that will continue the loop as a new
3821353f9a4SChandler Carruth   // terminator.
3831353f9a4SChandler Carruth   BranchInst::Create(ContinueBB, ParentBB);
3841353f9a4SChandler Carruth 
385d869b188SChandler Carruth   // Rewrite the relevant PHI nodes.
386d869b188SChandler Carruth   if (UnswitchedBB == LoopExitBB)
387d869b188SChandler Carruth     rewritePHINodesForUnswitchedExitBlock(*UnswitchedBB, *ParentBB, *OldPH);
388d869b188SChandler Carruth   else
389d869b188SChandler Carruth     rewritePHINodesForExitAndUnswitchedBlocks(*LoopExitBB, *UnswitchedBB,
390d869b188SChandler Carruth                                               *ParentBB, *OldPH);
391d869b188SChandler Carruth 
3921353f9a4SChandler Carruth   // Now we need to update the dominator tree.
3931353f9a4SChandler Carruth   updateDTAfterUnswitch(UnswitchedBB, OldPH, DT);
3941353f9a4SChandler Carruth   // But if we split something off of the loop exit block then we also removed
3951353f9a4SChandler Carruth   // one of the predecessors for the loop exit block and may need to update its
3961353f9a4SChandler Carruth   // idom.
3971353f9a4SChandler Carruth   if (UnswitchedBB != LoopExitBB)
3981353f9a4SChandler Carruth     updateLoopExitIDom(LoopExitBB, L, DT);
3991353f9a4SChandler Carruth 
4001353f9a4SChandler Carruth   // Since this is an i1 condition we can also trivially replace uses of it
4011353f9a4SChandler Carruth   // within the loop with a constant.
4021353f9a4SChandler Carruth   replaceLoopUsesWithConstant(L, *LoopCond, *Replacement);
4031353f9a4SChandler Carruth 
4041353f9a4SChandler Carruth   ++NumTrivial;
4051353f9a4SChandler Carruth   ++NumBranches;
4061353f9a4SChandler Carruth   return true;
4071353f9a4SChandler Carruth }
4081353f9a4SChandler Carruth 
4091353f9a4SChandler Carruth /// Unswitch a trivial switch if the condition is loop invariant.
4101353f9a4SChandler Carruth ///
4111353f9a4SChandler Carruth /// This routine should only be called when loop code leading to the switch has
4121353f9a4SChandler Carruth /// been validated as trivial (no side effects). This routine checks if the
4131353f9a4SChandler Carruth /// condition is invariant and that at least one of the successors is a loop
4141353f9a4SChandler Carruth /// exit. This allows us to unswitch without duplicating the loop, making it
4151353f9a4SChandler Carruth /// trivial.
4161353f9a4SChandler Carruth ///
4171353f9a4SChandler Carruth /// If this routine fails to unswitch the switch it returns false.
4181353f9a4SChandler Carruth ///
4191353f9a4SChandler Carruth /// If the switch can be unswitched, this routine splits the preheader and
4201353f9a4SChandler Carruth /// copies the switch above that split. If the default case is one of the
4211353f9a4SChandler Carruth /// exiting cases, it copies the non-exiting cases and points them at the new
4221353f9a4SChandler Carruth /// preheader. If the default case is not exiting, it copies the exiting cases
4231353f9a4SChandler Carruth /// and points the default at the preheader. It preserves loop simplified form
4241353f9a4SChandler Carruth /// (splitting the exit blocks as necessary). It simplifies the switch within
4251353f9a4SChandler Carruth /// the loop by removing now-dead cases. If the default case is one of those
4261353f9a4SChandler Carruth /// unswitched, it replaces its destination with a new basic block containing
4271353f9a4SChandler Carruth /// only unreachable. Such basic blocks, while technically loop exits, are not
4281353f9a4SChandler Carruth /// considered for unswitching so this is a stable transform and the same
4291353f9a4SChandler Carruth /// switch will not be revisited. If after unswitching there is only a single
4301353f9a4SChandler Carruth /// in-loop successor, the switch is further simplified to an unconditional
4311353f9a4SChandler Carruth /// branch. Still more cleanup can be done with some simplify-cfg like pass.
4321353f9a4SChandler Carruth static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT,
4331353f9a4SChandler Carruth                                   LoopInfo &LI) {
4341353f9a4SChandler Carruth   DEBUG(dbgs() << "  Trying to unswitch switch: " << SI << "\n");
4351353f9a4SChandler Carruth   Value *LoopCond = SI.getCondition();
4361353f9a4SChandler Carruth 
4371353f9a4SChandler Carruth   // If this isn't switching on an invariant condition, we can't unswitch it.
4381353f9a4SChandler Carruth   if (!L.isLoopInvariant(LoopCond))
4391353f9a4SChandler Carruth     return false;
4401353f9a4SChandler Carruth 
441d869b188SChandler Carruth   auto *ParentBB = SI.getParent();
442d869b188SChandler Carruth 
4431353f9a4SChandler Carruth   // FIXME: We should compute this once at the start and update it!
4441353f9a4SChandler Carruth   SmallVector<BasicBlock *, 16> ExitBlocks;
4451353f9a4SChandler Carruth   L.getExitBlocks(ExitBlocks);
4461353f9a4SChandler Carruth   SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(),
4471353f9a4SChandler Carruth                                              ExitBlocks.end());
4481353f9a4SChandler Carruth 
4491353f9a4SChandler Carruth   SmallVector<int, 4> ExitCaseIndices;
4501353f9a4SChandler Carruth   for (auto Case : SI.cases()) {
4511353f9a4SChandler Carruth     auto *SuccBB = Case.getCaseSuccessor();
452d869b188SChandler Carruth     if (ExitBlockSet.count(SuccBB) &&
453d869b188SChandler Carruth         areLoopExitPHIsLoopInvariant(L, *ParentBB, *SuccBB))
4541353f9a4SChandler Carruth       ExitCaseIndices.push_back(Case.getCaseIndex());
4551353f9a4SChandler Carruth   }
4561353f9a4SChandler Carruth   BasicBlock *DefaultExitBB = nullptr;
4571353f9a4SChandler Carruth   if (ExitBlockSet.count(SI.getDefaultDest()) &&
458d869b188SChandler Carruth       areLoopExitPHIsLoopInvariant(L, *ParentBB, *SI.getDefaultDest()) &&
4591353f9a4SChandler Carruth       !isa<UnreachableInst>(SI.getDefaultDest()->getTerminator()))
4601353f9a4SChandler Carruth     DefaultExitBB = SI.getDefaultDest();
4611353f9a4SChandler Carruth   else if (ExitCaseIndices.empty())
4621353f9a4SChandler Carruth     return false;
4631353f9a4SChandler Carruth 
4641353f9a4SChandler Carruth   DEBUG(dbgs() << "    unswitching trivial cases...\n");
4651353f9a4SChandler Carruth 
4661353f9a4SChandler Carruth   SmallVector<std::pair<ConstantInt *, BasicBlock *>, 4> ExitCases;
4671353f9a4SChandler Carruth   ExitCases.reserve(ExitCaseIndices.size());
4681353f9a4SChandler Carruth   // We walk the case indices backwards so that we remove the last case first
4691353f9a4SChandler Carruth   // and don't disrupt the earlier indices.
4701353f9a4SChandler Carruth   for (unsigned Index : reverse(ExitCaseIndices)) {
4711353f9a4SChandler Carruth     auto CaseI = SI.case_begin() + Index;
4721353f9a4SChandler Carruth     // Save the value of this case.
4731353f9a4SChandler Carruth     ExitCases.push_back({CaseI->getCaseValue(), CaseI->getCaseSuccessor()});
4741353f9a4SChandler Carruth     // Delete the unswitched cases.
4751353f9a4SChandler Carruth     SI.removeCase(CaseI);
4761353f9a4SChandler Carruth   }
4771353f9a4SChandler Carruth 
4781353f9a4SChandler Carruth   // Check if after this all of the remaining cases point at the same
4791353f9a4SChandler Carruth   // successor.
4801353f9a4SChandler Carruth   BasicBlock *CommonSuccBB = nullptr;
4811353f9a4SChandler Carruth   if (SI.getNumCases() > 0 &&
4821353f9a4SChandler Carruth       std::all_of(std::next(SI.case_begin()), SI.case_end(),
4831353f9a4SChandler Carruth                   [&SI](const SwitchInst::CaseHandle &Case) {
4841353f9a4SChandler Carruth                     return Case.getCaseSuccessor() ==
4851353f9a4SChandler Carruth                            SI.case_begin()->getCaseSuccessor();
4861353f9a4SChandler Carruth                   }))
4871353f9a4SChandler Carruth     CommonSuccBB = SI.case_begin()->getCaseSuccessor();
4881353f9a4SChandler Carruth 
4891353f9a4SChandler Carruth   if (DefaultExitBB) {
4901353f9a4SChandler Carruth     // We can't remove the default edge so replace it with an edge to either
4911353f9a4SChandler Carruth     // the single common remaining successor (if we have one) or an unreachable
4921353f9a4SChandler Carruth     // block.
4931353f9a4SChandler Carruth     if (CommonSuccBB) {
4941353f9a4SChandler Carruth       SI.setDefaultDest(CommonSuccBB);
4951353f9a4SChandler Carruth     } else {
4961353f9a4SChandler Carruth       BasicBlock *UnreachableBB = BasicBlock::Create(
4971353f9a4SChandler Carruth           ParentBB->getContext(),
4981353f9a4SChandler Carruth           Twine(ParentBB->getName()) + ".unreachable_default",
4991353f9a4SChandler Carruth           ParentBB->getParent());
5001353f9a4SChandler Carruth       new UnreachableInst(ParentBB->getContext(), UnreachableBB);
5011353f9a4SChandler Carruth       SI.setDefaultDest(UnreachableBB);
5021353f9a4SChandler Carruth       DT.addNewBlock(UnreachableBB, ParentBB);
5031353f9a4SChandler Carruth     }
5041353f9a4SChandler Carruth   } else {
5051353f9a4SChandler Carruth     // If we're not unswitching the default, we need it to match any cases to
5061353f9a4SChandler Carruth     // have a common successor or if we have no cases it is the common
5071353f9a4SChandler Carruth     // successor.
5081353f9a4SChandler Carruth     if (SI.getNumCases() == 0)
5091353f9a4SChandler Carruth       CommonSuccBB = SI.getDefaultDest();
5101353f9a4SChandler Carruth     else if (SI.getDefaultDest() != CommonSuccBB)
5111353f9a4SChandler Carruth       CommonSuccBB = nullptr;
5121353f9a4SChandler Carruth   }
5131353f9a4SChandler Carruth 
5141353f9a4SChandler Carruth   // Split the preheader, so that we know that there is a safe place to insert
5151353f9a4SChandler Carruth   // the switch.
5161353f9a4SChandler Carruth   BasicBlock *OldPH = L.getLoopPreheader();
5171353f9a4SChandler Carruth   BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI);
5181353f9a4SChandler Carruth   OldPH->getTerminator()->eraseFromParent();
5191353f9a4SChandler Carruth 
5201353f9a4SChandler Carruth   // Now add the unswitched switch.
5211353f9a4SChandler Carruth   auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH);
5221353f9a4SChandler Carruth 
523d869b188SChandler Carruth   // Rewrite the IR for the unswitched basic blocks. This requires two steps.
524d869b188SChandler Carruth   // First, we split any exit blocks with remaining in-loop predecessors. Then
525d869b188SChandler Carruth   // we update the PHIs in one of two ways depending on if there was a split.
526d869b188SChandler Carruth   // We walk in reverse so that we split in the same order as the cases
527d869b188SChandler Carruth   // appeared. This is purely for convenience of reading the resulting IR, but
528d869b188SChandler Carruth   // it doesn't cost anything really.
529d869b188SChandler Carruth   SmallPtrSet<BasicBlock *, 2> UnswitchedExitBBs;
5301353f9a4SChandler Carruth   SmallDenseMap<BasicBlock *, BasicBlock *, 2> SplitExitBBMap;
5311353f9a4SChandler Carruth   // Handle the default exit if necessary.
5321353f9a4SChandler Carruth   // FIXME: It'd be great if we could merge this with the loop below but LLVM's
5331353f9a4SChandler Carruth   // ranges aren't quite powerful enough yet.
534d869b188SChandler Carruth   if (DefaultExitBB) {
535d869b188SChandler Carruth     if (pred_empty(DefaultExitBB)) {
536d869b188SChandler Carruth       UnswitchedExitBBs.insert(DefaultExitBB);
537d869b188SChandler Carruth       rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH);
538d869b188SChandler Carruth     } else {
5391353f9a4SChandler Carruth       auto *SplitBB =
5401353f9a4SChandler Carruth           SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI);
541d869b188SChandler Carruth       rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB,
542d869b188SChandler Carruth                                                 *ParentBB, *OldPH);
5431353f9a4SChandler Carruth       updateLoopExitIDom(DefaultExitBB, L, DT);
5441353f9a4SChandler Carruth       DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB;
5451353f9a4SChandler Carruth     }
546d869b188SChandler Carruth   }
5471353f9a4SChandler Carruth   // Note that we must use a reference in the for loop so that we update the
5481353f9a4SChandler Carruth   // container.
5491353f9a4SChandler Carruth   for (auto &CasePair : reverse(ExitCases)) {
5501353f9a4SChandler Carruth     // Grab a reference to the exit block in the pair so that we can update it.
551d869b188SChandler Carruth     BasicBlock *ExitBB = CasePair.second;
5521353f9a4SChandler Carruth 
5531353f9a4SChandler Carruth     // If this case is the last edge into the exit block, we can simply reuse it
5541353f9a4SChandler Carruth     // as it will no longer be a loop exit. No mapping necessary.
555d869b188SChandler Carruth     if (pred_empty(ExitBB)) {
556d869b188SChandler Carruth       // Only rewrite once.
557d869b188SChandler Carruth       if (UnswitchedExitBBs.insert(ExitBB).second)
558d869b188SChandler Carruth         rewritePHINodesForUnswitchedExitBlock(*ExitBB, *ParentBB, *OldPH);
5591353f9a4SChandler Carruth       continue;
560d869b188SChandler Carruth     }
5611353f9a4SChandler Carruth 
5621353f9a4SChandler Carruth     // Otherwise we need to split the exit block so that we retain an exit
5631353f9a4SChandler Carruth     // block from the loop and a target for the unswitched condition.
5641353f9a4SChandler Carruth     BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB];
5651353f9a4SChandler Carruth     if (!SplitExitBB) {
5661353f9a4SChandler Carruth       // If this is the first time we see this, do the split and remember it.
5671353f9a4SChandler Carruth       SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI);
568d869b188SChandler Carruth       rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB,
569d869b188SChandler Carruth                                                 *ParentBB, *OldPH);
5701353f9a4SChandler Carruth       updateLoopExitIDom(ExitBB, L, DT);
5711353f9a4SChandler Carruth     }
572d869b188SChandler Carruth     // Update the case pair to point to the split block.
573d869b188SChandler Carruth     CasePair.second = SplitExitBB;
5741353f9a4SChandler Carruth   }
5751353f9a4SChandler Carruth 
5761353f9a4SChandler Carruth   // Now add the unswitched cases. We do this in reverse order as we built them
5771353f9a4SChandler Carruth   // in reverse order.
5781353f9a4SChandler Carruth   for (auto CasePair : reverse(ExitCases)) {
5791353f9a4SChandler Carruth     ConstantInt *CaseVal = CasePair.first;
5801353f9a4SChandler Carruth     BasicBlock *UnswitchedBB = CasePair.second;
5811353f9a4SChandler Carruth 
5821353f9a4SChandler Carruth     NewSI->addCase(CaseVal, UnswitchedBB);
5831353f9a4SChandler Carruth     updateDTAfterUnswitch(UnswitchedBB, OldPH, DT);
5841353f9a4SChandler Carruth   }
5851353f9a4SChandler Carruth 
5861353f9a4SChandler Carruth   // If the default was unswitched, re-point it and add explicit cases for
5871353f9a4SChandler Carruth   // entering the loop.
5881353f9a4SChandler Carruth   if (DefaultExitBB) {
5891353f9a4SChandler Carruth     NewSI->setDefaultDest(DefaultExitBB);
5901353f9a4SChandler Carruth     updateDTAfterUnswitch(DefaultExitBB, OldPH, DT);
5911353f9a4SChandler Carruth 
5921353f9a4SChandler Carruth     // We removed all the exit cases, so we just copy the cases to the
5931353f9a4SChandler Carruth     // unswitched switch.
5941353f9a4SChandler Carruth     for (auto Case : SI.cases())
5951353f9a4SChandler Carruth       NewSI->addCase(Case.getCaseValue(), NewPH);
5961353f9a4SChandler Carruth   }
5971353f9a4SChandler Carruth 
5981353f9a4SChandler Carruth   // If we ended up with a common successor for every path through the switch
5991353f9a4SChandler Carruth   // after unswitching, rewrite it to an unconditional branch to make it easy
6001353f9a4SChandler Carruth   // to recognize. Otherwise we potentially have to recognize the default case
6011353f9a4SChandler Carruth   // pointing at unreachable and other complexity.
6021353f9a4SChandler Carruth   if (CommonSuccBB) {
6031353f9a4SChandler Carruth     BasicBlock *BB = SI.getParent();
6041353f9a4SChandler Carruth     SI.eraseFromParent();
6051353f9a4SChandler Carruth     BranchInst::Create(CommonSuccBB, BB);
6061353f9a4SChandler Carruth   }
6071353f9a4SChandler Carruth 
6081353f9a4SChandler Carruth   DT.verifyDomTree();
6091353f9a4SChandler Carruth   ++NumTrivial;
6101353f9a4SChandler Carruth   ++NumSwitches;
6111353f9a4SChandler Carruth   return true;
6121353f9a4SChandler Carruth }
6131353f9a4SChandler Carruth 
6141353f9a4SChandler Carruth /// This routine scans the loop to find a branch or switch which occurs before
6151353f9a4SChandler Carruth /// any side effects occur. These can potentially be unswitched without
6161353f9a4SChandler Carruth /// duplicating the loop. If a branch or switch is successfully unswitched the
6171353f9a4SChandler Carruth /// scanning continues to see if subsequent branches or switches have become
6181353f9a4SChandler Carruth /// trivial. Once all trivial candidates have been unswitched, this routine
6191353f9a4SChandler Carruth /// returns.
6201353f9a4SChandler Carruth ///
6211353f9a4SChandler Carruth /// The return value indicates whether anything was unswitched (and therefore
6221353f9a4SChandler Carruth /// changed).
6231353f9a4SChandler Carruth static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT,
6241353f9a4SChandler Carruth                                          LoopInfo &LI) {
6251353f9a4SChandler Carruth   bool Changed = false;
6261353f9a4SChandler Carruth 
6271353f9a4SChandler Carruth   // If loop header has only one reachable successor we should keep looking for
6281353f9a4SChandler Carruth   // trivial condition candidates in the successor as well. An alternative is
6291353f9a4SChandler Carruth   // to constant fold conditions and merge successors into loop header (then we
6301353f9a4SChandler Carruth   // only need to check header's terminator). The reason for not doing this in
6311353f9a4SChandler Carruth   // LoopUnswitch pass is that it could potentially break LoopPassManager's
6321353f9a4SChandler Carruth   // invariants. Folding dead branches could either eliminate the current loop
6331353f9a4SChandler Carruth   // or make other loops unreachable. LCSSA form might also not be preserved
6341353f9a4SChandler Carruth   // after deleting branches. The following code keeps traversing loop header's
6351353f9a4SChandler Carruth   // successors until it finds the trivial condition candidate (condition that
6361353f9a4SChandler Carruth   // is not a constant). Since unswitching generates branches with constant
6371353f9a4SChandler Carruth   // conditions, this scenario could be very common in practice.
6381353f9a4SChandler Carruth   BasicBlock *CurrentBB = L.getHeader();
6391353f9a4SChandler Carruth   SmallPtrSet<BasicBlock *, 8> Visited;
6401353f9a4SChandler Carruth   Visited.insert(CurrentBB);
6411353f9a4SChandler Carruth   do {
6421353f9a4SChandler Carruth     // Check if there are any side-effecting instructions (e.g. stores, calls,
6431353f9a4SChandler Carruth     // volatile loads) in the part of the loop that the code *would* execute
6441353f9a4SChandler Carruth     // without unswitching.
6451353f9a4SChandler Carruth     if (llvm::any_of(*CurrentBB,
6461353f9a4SChandler Carruth                      [](Instruction &I) { return I.mayHaveSideEffects(); }))
6471353f9a4SChandler Carruth       return Changed;
6481353f9a4SChandler Carruth 
6491353f9a4SChandler Carruth     TerminatorInst *CurrentTerm = CurrentBB->getTerminator();
6501353f9a4SChandler Carruth 
6511353f9a4SChandler Carruth     if (auto *SI = dyn_cast<SwitchInst>(CurrentTerm)) {
6521353f9a4SChandler Carruth       // Don't bother trying to unswitch past a switch with a constant
6531353f9a4SChandler Carruth       // condition. This should be removed prior to running this pass by
6541353f9a4SChandler Carruth       // simplify-cfg.
6551353f9a4SChandler Carruth       if (isa<Constant>(SI->getCondition()))
6561353f9a4SChandler Carruth         return Changed;
6571353f9a4SChandler Carruth 
6581353f9a4SChandler Carruth       if (!unswitchTrivialSwitch(L, *SI, DT, LI))
6591353f9a4SChandler Carruth         // Coludn't unswitch this one so we're done.
6601353f9a4SChandler Carruth         return Changed;
6611353f9a4SChandler Carruth 
6621353f9a4SChandler Carruth       // Mark that we managed to unswitch something.
6631353f9a4SChandler Carruth       Changed = true;
6641353f9a4SChandler Carruth 
6651353f9a4SChandler Carruth       // If unswitching turned the terminator into an unconditional branch then
6661353f9a4SChandler Carruth       // we can continue. The unswitching logic specifically works to fold any
6671353f9a4SChandler Carruth       // cases it can into an unconditional branch to make it easier to
6681353f9a4SChandler Carruth       // recognize here.
6691353f9a4SChandler Carruth       auto *BI = dyn_cast<BranchInst>(CurrentBB->getTerminator());
6701353f9a4SChandler Carruth       if (!BI || BI->isConditional())
6711353f9a4SChandler Carruth         return Changed;
6721353f9a4SChandler Carruth 
6731353f9a4SChandler Carruth       CurrentBB = BI->getSuccessor(0);
6741353f9a4SChandler Carruth       continue;
6751353f9a4SChandler Carruth     }
6761353f9a4SChandler Carruth 
6771353f9a4SChandler Carruth     auto *BI = dyn_cast<BranchInst>(CurrentTerm);
6781353f9a4SChandler Carruth     if (!BI)
6791353f9a4SChandler Carruth       // We do not understand other terminator instructions.
6801353f9a4SChandler Carruth       return Changed;
6811353f9a4SChandler Carruth 
6821353f9a4SChandler Carruth     // Don't bother trying to unswitch past an unconditional branch or a branch
6831353f9a4SChandler Carruth     // with a constant value. These should be removed by simplify-cfg prior to
6841353f9a4SChandler Carruth     // running this pass.
6851353f9a4SChandler Carruth     if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
6861353f9a4SChandler Carruth       return Changed;
6871353f9a4SChandler Carruth 
6881353f9a4SChandler Carruth     // Found a trivial condition candidate: non-foldable conditional branch. If
6891353f9a4SChandler Carruth     // we fail to unswitch this, we can't do anything else that is trivial.
6901353f9a4SChandler Carruth     if (!unswitchTrivialBranch(L, *BI, DT, LI))
6911353f9a4SChandler Carruth       return Changed;
6921353f9a4SChandler Carruth 
6931353f9a4SChandler Carruth     // Mark that we managed to unswitch something.
6941353f9a4SChandler Carruth     Changed = true;
6951353f9a4SChandler Carruth 
6961353f9a4SChandler Carruth     // We unswitched the branch. This should always leave us with an
6971353f9a4SChandler Carruth     // unconditional branch that we can follow now.
6981353f9a4SChandler Carruth     BI = cast<BranchInst>(CurrentBB->getTerminator());
6991353f9a4SChandler Carruth     assert(!BI->isConditional() &&
7001353f9a4SChandler Carruth            "Cannot form a conditional branch by unswitching1");
7011353f9a4SChandler Carruth     CurrentBB = BI->getSuccessor(0);
7021353f9a4SChandler Carruth 
7031353f9a4SChandler Carruth     // When continuing, if we exit the loop or reach a previous visited block,
7041353f9a4SChandler Carruth     // then we can not reach any trivial condition candidates (unfoldable
7051353f9a4SChandler Carruth     // branch instructions or switch instructions) and no unswitch can happen.
7061353f9a4SChandler Carruth   } while (L.contains(CurrentBB) && Visited.insert(CurrentBB).second);
7071353f9a4SChandler Carruth 
7081353f9a4SChandler Carruth   return Changed;
7091353f9a4SChandler Carruth }
7101353f9a4SChandler Carruth 
7111353f9a4SChandler Carruth /// Unswitch control flow predicated on loop invariant conditions.
7121353f9a4SChandler Carruth ///
7131353f9a4SChandler Carruth /// This first hoists all branches or switches which are trivial (IE, do not
7141353f9a4SChandler Carruth /// require duplicating any part of the loop) out of the loop body. It then
7151353f9a4SChandler Carruth /// looks at other loop invariant control flows and tries to unswitch those as
7161353f9a4SChandler Carruth /// well by cloning the loop if the result is small enough.
7171353f9a4SChandler Carruth static bool unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI,
7181353f9a4SChandler Carruth                          AssumptionCache &AC) {
7191353f9a4SChandler Carruth   assert(L.isLCSSAForm(DT) &&
7201353f9a4SChandler Carruth          "Loops must be in LCSSA form before unswitching.");
7211353f9a4SChandler Carruth   bool Changed = false;
7221353f9a4SChandler Carruth 
7231353f9a4SChandler Carruth   // Must be in loop simplified form: we need a preheader and dedicated exits.
7241353f9a4SChandler Carruth   if (!L.isLoopSimplifyForm())
7251353f9a4SChandler Carruth     return false;
7261353f9a4SChandler Carruth 
7271353f9a4SChandler Carruth   // Try trivial unswitch first before loop over other basic blocks in the loop.
7281353f9a4SChandler Carruth   Changed |= unswitchAllTrivialConditions(L, DT, LI);
7291353f9a4SChandler Carruth 
7301353f9a4SChandler Carruth   // FIXME: Add support for non-trivial unswitching by cloning the loop.
7311353f9a4SChandler Carruth 
7321353f9a4SChandler Carruth   return Changed;
7331353f9a4SChandler Carruth }
7341353f9a4SChandler Carruth 
7351353f9a4SChandler Carruth PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
7361353f9a4SChandler Carruth                                               LoopStandardAnalysisResults &AR,
7371353f9a4SChandler Carruth                                               LPMUpdater &U) {
7381353f9a4SChandler Carruth   Function &F = *L.getHeader()->getParent();
7391353f9a4SChandler Carruth   (void)F;
7401353f9a4SChandler Carruth 
7411353f9a4SChandler Carruth   DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L << "\n");
7421353f9a4SChandler Carruth 
7431353f9a4SChandler Carruth   if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC))
7441353f9a4SChandler Carruth     return PreservedAnalyses::all();
7451353f9a4SChandler Carruth 
7461353f9a4SChandler Carruth #ifndef NDEBUG
7471353f9a4SChandler Carruth   // Historically this pass has had issues with the dominator tree so verify it
7481353f9a4SChandler Carruth   // in asserts builds.
7491353f9a4SChandler Carruth   AR.DT.verifyDomTree();
7501353f9a4SChandler Carruth #endif
7511353f9a4SChandler Carruth   return getLoopPassPreservedAnalyses();
7521353f9a4SChandler Carruth }
7531353f9a4SChandler Carruth 
7541353f9a4SChandler Carruth namespace {
755a369a457SEugene Zelenko 
7561353f9a4SChandler Carruth class SimpleLoopUnswitchLegacyPass : public LoopPass {
7571353f9a4SChandler Carruth public:
7581353f9a4SChandler Carruth   static char ID; // Pass ID, replacement for typeid
759a369a457SEugene Zelenko 
7601353f9a4SChandler Carruth   explicit SimpleLoopUnswitchLegacyPass() : LoopPass(ID) {
7611353f9a4SChandler Carruth     initializeSimpleLoopUnswitchLegacyPassPass(
7621353f9a4SChandler Carruth         *PassRegistry::getPassRegistry());
7631353f9a4SChandler Carruth   }
7641353f9a4SChandler Carruth 
7651353f9a4SChandler Carruth   bool runOnLoop(Loop *L, LPPassManager &LPM) override;
7661353f9a4SChandler Carruth 
7671353f9a4SChandler Carruth   void getAnalysisUsage(AnalysisUsage &AU) const override {
7681353f9a4SChandler Carruth     AU.addRequired<AssumptionCacheTracker>();
7691353f9a4SChandler Carruth     getLoopAnalysisUsage(AU);
7701353f9a4SChandler Carruth   }
7711353f9a4SChandler Carruth };
772a369a457SEugene Zelenko 
773a369a457SEugene Zelenko } // end anonymous namespace
7741353f9a4SChandler Carruth 
7751353f9a4SChandler Carruth bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
7761353f9a4SChandler Carruth   if (skipLoop(L))
7771353f9a4SChandler Carruth     return false;
7781353f9a4SChandler Carruth 
7791353f9a4SChandler Carruth   Function &F = *L->getHeader()->getParent();
7801353f9a4SChandler Carruth 
7811353f9a4SChandler Carruth   DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L << "\n");
7821353f9a4SChandler Carruth 
7831353f9a4SChandler Carruth   auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
7841353f9a4SChandler Carruth   auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
7851353f9a4SChandler Carruth   auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
7861353f9a4SChandler Carruth 
7871353f9a4SChandler Carruth   bool Changed = unswitchLoop(*L, DT, LI, AC);
7881353f9a4SChandler Carruth 
7891353f9a4SChandler Carruth #ifndef NDEBUG
7901353f9a4SChandler Carruth   // Historically this pass has had issues with the dominator tree so verify it
7911353f9a4SChandler Carruth   // in asserts builds.
7921353f9a4SChandler Carruth   DT.verifyDomTree();
7931353f9a4SChandler Carruth #endif
7941353f9a4SChandler Carruth   return Changed;
7951353f9a4SChandler Carruth }
7961353f9a4SChandler Carruth 
7971353f9a4SChandler Carruth char SimpleLoopUnswitchLegacyPass::ID = 0;
7981353f9a4SChandler Carruth INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
7991353f9a4SChandler Carruth                       "Simple unswitch loops", false, false)
8001353f9a4SChandler Carruth INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
8011353f9a4SChandler Carruth INITIALIZE_PASS_DEPENDENCY(LoopPass)
8021353f9a4SChandler Carruth INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
8031353f9a4SChandler Carruth INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch",
8041353f9a4SChandler Carruth                     "Simple unswitch loops", false, false)
8051353f9a4SChandler Carruth 
8061353f9a4SChandler Carruth Pass *llvm::createSimpleLoopUnswitchLegacyPass() {
8071353f9a4SChandler Carruth   return new SimpleLoopUnswitchLegacyPass();
8081353f9a4SChandler Carruth }
809