10b57cec5SDimitry Andric //===- LoopSimplify.cpp - Loop Canonicalization Pass ----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass performs several transformations to transform natural loops into a
100b57cec5SDimitry Andric // simpler form, which makes subsequent analyses and transformations simpler and
110b57cec5SDimitry Andric // more effective.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Loop pre-header insertion guarantees that there is a single, non-critical
140b57cec5SDimitry Andric // entry edge from outside of the loop to the loop header.  This simplifies a
150b57cec5SDimitry Andric // number of analyses and transformations, such as LICM.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric // Loop exit-block insertion guarantees that all exit blocks from the loop
180b57cec5SDimitry Andric // (blocks which are outside of the loop that have predecessors inside of the
190b57cec5SDimitry Andric // loop) only have predecessors from inside of the loop (and are thus dominated
200b57cec5SDimitry Andric // by the loop header).  This simplifies transformations such as store-sinking
210b57cec5SDimitry Andric // that are built into LICM.
220b57cec5SDimitry Andric //
230b57cec5SDimitry Andric // This pass also guarantees that loops will have exactly one backedge.
240b57cec5SDimitry Andric //
250b57cec5SDimitry Andric // Indirectbr instructions introduce several complications. If the loop
260b57cec5SDimitry Andric // contains or is entered by an indirectbr instruction, it may not be possible
270b57cec5SDimitry Andric // to transform the loop and make these guarantees. Client code should check
280b57cec5SDimitry Andric // that these conditions are true before relying on them.
290b57cec5SDimitry Andric //
300b57cec5SDimitry Andric // Similar complications arise from callbr instructions, particularly in
310b57cec5SDimitry Andric // asm-goto where blockaddress expressions are used.
320b57cec5SDimitry Andric //
330b57cec5SDimitry Andric // Note that the simplifycfg pass will clean up blocks which are split out but
340b57cec5SDimitry Andric // end up being unnecessary, so usage of this pass should not pessimize
350b57cec5SDimitry Andric // generated code.
360b57cec5SDimitry Andric //
370b57cec5SDimitry Andric // This pass obviously modifies the CFG, but updates loop information and
380b57cec5SDimitry Andric // dominator information.
390b57cec5SDimitry Andric //
400b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopSimplify.h"
430b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
440b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
450b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
460b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
470b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
480b57cec5SDimitry Andric #include "llvm/Analysis/BasicAliasAnalysis.h"
490b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
500b57cec5SDimitry Andric #include "llvm/Analysis/DependenceAnalysis.h"
510b57cec5SDimitry Andric #include "llvm/Analysis/GlobalsModRef.h"
520b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
530b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
540b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
550b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSAUpdater.h"
560b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
570b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
580b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
590b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
600b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
610b57cec5SDimitry Andric #include "llvm/IR/Function.h"
620b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
630b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
640b57cec5SDimitry Andric #include "llvm/IR/Module.h"
65480093f4SDimitry Andric #include "llvm/InitializePasses.h"
660b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
670b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
680b57cec5SDimitry Andric #include "llvm/Transforms/Utils.h"
690b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
700b57cec5SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
710b57cec5SDimitry Andric #include "llvm/Transforms/Utils/LoopUtils.h"
720b57cec5SDimitry Andric using namespace llvm;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric #define DEBUG_TYPE "loop-simplify"
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric STATISTIC(NumNested  , "Number of nested loops split out");
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric // If the block isn't already, move the new block to right after some 'outside
790b57cec5SDimitry Andric // block' block.  This prevents the preheader from being placed inside the loop
800b57cec5SDimitry Andric // body, e.g. when the loop hasn't been rotated.
placeSplitBlockCarefully(BasicBlock * NewBB,SmallVectorImpl<BasicBlock * > & SplitPreds,Loop * L)810b57cec5SDimitry Andric static void placeSplitBlockCarefully(BasicBlock *NewBB,
820b57cec5SDimitry Andric                                      SmallVectorImpl<BasicBlock *> &SplitPreds,
830b57cec5SDimitry Andric                                      Loop *L) {
840b57cec5SDimitry Andric   // Check to see if NewBB is already well placed.
850b57cec5SDimitry Andric   Function::iterator BBI = --NewBB->getIterator();
860b57cec5SDimitry Andric   for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
870b57cec5SDimitry Andric     if (&*BBI == SplitPreds[i])
880b57cec5SDimitry Andric       return;
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   // If it isn't already after an outside block, move it after one.  This is
920b57cec5SDimitry Andric   // always good as it makes the uncond branch from the outside block into a
930b57cec5SDimitry Andric   // fall-through.
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   // Figure out *which* outside block to put this after.  Prefer an outside
960b57cec5SDimitry Andric   // block that neighbors a BB actually in the loop.
970b57cec5SDimitry Andric   BasicBlock *FoundBB = nullptr;
980b57cec5SDimitry Andric   for (unsigned i = 0, e = SplitPreds.size(); i != e; ++i) {
990b57cec5SDimitry Andric     Function::iterator BBI = SplitPreds[i]->getIterator();
1000b57cec5SDimitry Andric     if (++BBI != NewBB->getParent()->end() && L->contains(&*BBI)) {
1010b57cec5SDimitry Andric       FoundBB = SplitPreds[i];
1020b57cec5SDimitry Andric       break;
1030b57cec5SDimitry Andric     }
1040b57cec5SDimitry Andric   }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // If our heuristic for a *good* bb to place this after doesn't find
1070b57cec5SDimitry Andric   // anything, just pick something.  It's likely better than leaving it within
1080b57cec5SDimitry Andric   // the loop.
1090b57cec5SDimitry Andric   if (!FoundBB)
1100b57cec5SDimitry Andric     FoundBB = SplitPreds[0];
1110b57cec5SDimitry Andric   NewBB->moveAfter(FoundBB);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric /// InsertPreheaderForLoop - Once we discover that a loop doesn't have a
1150b57cec5SDimitry Andric /// preheader, this method is called to insert one.  This method has two phases:
1160b57cec5SDimitry Andric /// preheader insertion and analysis updating.
1170b57cec5SDimitry Andric ///
InsertPreheaderForLoop(Loop * L,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)1180b57cec5SDimitry Andric BasicBlock *llvm::InsertPreheaderForLoop(Loop *L, DominatorTree *DT,
1190b57cec5SDimitry Andric                                          LoopInfo *LI, MemorySSAUpdater *MSSAU,
1200b57cec5SDimitry Andric                                          bool PreserveLCSSA) {
1210b57cec5SDimitry Andric   BasicBlock *Header = L->getHeader();
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   // Compute the set of predecessors of the loop that are not in the loop.
1240b57cec5SDimitry Andric   SmallVector<BasicBlock*, 8> OutsideBlocks;
125fe6060f1SDimitry Andric   for (BasicBlock *P : predecessors(Header)) {
1260b57cec5SDimitry Andric     if (!L->contains(P)) {         // Coming in from outside the loop?
1270b57cec5SDimitry Andric       // If the loop is branched to from an indirect terminator, we won't
1280b57cec5SDimitry Andric       // be able to fully transform the loop, because it prohibits
1290b57cec5SDimitry Andric       // edge splitting.
130fcaf7f86SDimitry Andric       if (isa<IndirectBrInst>(P->getTerminator()))
1310b57cec5SDimitry Andric         return nullptr;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       // Keep track of it.
1340b57cec5SDimitry Andric       OutsideBlocks.push_back(P);
1350b57cec5SDimitry Andric     }
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   // Split out the loop pre-header.
1390b57cec5SDimitry Andric   BasicBlock *PreheaderBB;
1400b57cec5SDimitry Andric   PreheaderBB = SplitBlockPredecessors(Header, OutsideBlocks, ".preheader", DT,
1410b57cec5SDimitry Andric                                        LI, MSSAU, PreserveLCSSA);
1420b57cec5SDimitry Andric   if (!PreheaderBB)
1430b57cec5SDimitry Andric     return nullptr;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "LoopSimplify: Creating pre-header "
1460b57cec5SDimitry Andric                     << PreheaderBB->getName() << "\n");
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // Make sure that NewBB is put someplace intelligent, which doesn't mess up
1490b57cec5SDimitry Andric   // code layout too horribly.
1500b57cec5SDimitry Andric   placeSplitBlockCarefully(PreheaderBB, OutsideBlocks, L);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   return PreheaderBB;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric /// Add the specified block, and all of its predecessors, to the specified set,
1560b57cec5SDimitry Andric /// if it's not already in there.  Stop predecessor traversal when we reach
1570b57cec5SDimitry Andric /// StopBlock.
addBlockAndPredsToSet(BasicBlock * InputBB,BasicBlock * StopBlock,SmallPtrSetImpl<BasicBlock * > & Blocks)1580b57cec5SDimitry Andric static void addBlockAndPredsToSet(BasicBlock *InputBB, BasicBlock *StopBlock,
159e8d8bef9SDimitry Andric                                   SmallPtrSetImpl<BasicBlock *> &Blocks) {
1600b57cec5SDimitry Andric   SmallVector<BasicBlock *, 8> Worklist;
1610b57cec5SDimitry Andric   Worklist.push_back(InputBB);
1620b57cec5SDimitry Andric   do {
1630b57cec5SDimitry Andric     BasicBlock *BB = Worklist.pop_back_val();
1640b57cec5SDimitry Andric     if (Blocks.insert(BB).second && BB != StopBlock)
1650b57cec5SDimitry Andric       // If BB is not already processed and it is not a stop block then
1660b57cec5SDimitry Andric       // insert its predecessor in the work list
167e8d8bef9SDimitry Andric       append_range(Worklist, predecessors(BB));
1680b57cec5SDimitry Andric   } while (!Worklist.empty());
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric /// The first part of loop-nestification is to find a PHI node that tells
1720b57cec5SDimitry Andric /// us how to partition the loops.
findPHIToPartitionLoops(Loop * L,DominatorTree * DT,AssumptionCache * AC)1730b57cec5SDimitry Andric static PHINode *findPHIToPartitionLoops(Loop *L, DominatorTree *DT,
1740b57cec5SDimitry Andric                                         AssumptionCache *AC) {
1750b57cec5SDimitry Andric   const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
1760b57cec5SDimitry Andric   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ) {
1770b57cec5SDimitry Andric     PHINode *PN = cast<PHINode>(I);
1780b57cec5SDimitry Andric     ++I;
17981ad6265SDimitry Andric     if (Value *V = simplifyInstruction(PN, {DL, nullptr, DT, AC})) {
1800b57cec5SDimitry Andric       // This is a degenerate PHI already, don't modify it!
1810b57cec5SDimitry Andric       PN->replaceAllUsesWith(V);
1820b57cec5SDimitry Andric       PN->eraseFromParent();
1830b57cec5SDimitry Andric       continue;
1840b57cec5SDimitry Andric     }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric     // Scan this PHI node looking for a use of the PHI node by itself.
1870b57cec5SDimitry Andric     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1880b57cec5SDimitry Andric       if (PN->getIncomingValue(i) == PN &&
1890b57cec5SDimitry Andric           L->contains(PN->getIncomingBlock(i)))
1900b57cec5SDimitry Andric         // We found something tasty to remove.
1910b57cec5SDimitry Andric         return PN;
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric   return nullptr;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric /// If this loop has multiple backedges, try to pull one of them out into
1970b57cec5SDimitry Andric /// a nested loop.
1980b57cec5SDimitry Andric ///
1990b57cec5SDimitry Andric /// This is important for code that looks like
2000b57cec5SDimitry Andric /// this:
2010b57cec5SDimitry Andric ///
2020b57cec5SDimitry Andric ///  Loop:
2030b57cec5SDimitry Andric ///     ...
2040b57cec5SDimitry Andric ///     br cond, Loop, Next
2050b57cec5SDimitry Andric ///     ...
2060b57cec5SDimitry Andric ///     br cond2, Loop, Out
2070b57cec5SDimitry Andric ///
2080b57cec5SDimitry Andric /// To identify this common case, we look at the PHI nodes in the header of the
2090b57cec5SDimitry Andric /// loop.  PHI nodes with unchanging values on one backedge correspond to values
2100b57cec5SDimitry Andric /// that change in the "outer" loop, but not in the "inner" loop.
2110b57cec5SDimitry Andric ///
2120b57cec5SDimitry Andric /// If we are able to separate out a loop, return the new outer loop that was
2130b57cec5SDimitry Andric /// created.
2140b57cec5SDimitry Andric ///
separateNestedLoop(Loop * L,BasicBlock * Preheader,DominatorTree * DT,LoopInfo * LI,ScalarEvolution * SE,bool PreserveLCSSA,AssumptionCache * AC,MemorySSAUpdater * MSSAU)2150b57cec5SDimitry Andric static Loop *separateNestedLoop(Loop *L, BasicBlock *Preheader,
2160b57cec5SDimitry Andric                                 DominatorTree *DT, LoopInfo *LI,
2170b57cec5SDimitry Andric                                 ScalarEvolution *SE, bool PreserveLCSSA,
2180b57cec5SDimitry Andric                                 AssumptionCache *AC, MemorySSAUpdater *MSSAU) {
2190b57cec5SDimitry Andric   // Don't try to separate loops without a preheader.
2200b57cec5SDimitry Andric   if (!Preheader)
2210b57cec5SDimitry Andric     return nullptr;
2220b57cec5SDimitry Andric 
2235ffd83dbSDimitry Andric   // Treat the presence of convergent functions conservatively. The
2245ffd83dbSDimitry Andric   // transformation is invalid if calls to certain convergent
2255ffd83dbSDimitry Andric   // functions (like an AMDGPU barrier) get included in the resulting
2265ffd83dbSDimitry Andric   // inner loop. But blocks meant for the inner loop will be
2275ffd83dbSDimitry Andric   // identified later at a point where it's too late to abort the
2285ffd83dbSDimitry Andric   // transformation. Also, the convergent attribute is not really
2295ffd83dbSDimitry Andric   // sufficient to express the semantics of functions that are
2305ffd83dbSDimitry Andric   // affected by this transformation. So we choose to back off if such
2315ffd83dbSDimitry Andric   // a function call is present until a better alternative becomes
2325ffd83dbSDimitry Andric   // available. This is similar to the conservative treatment of
2335ffd83dbSDimitry Andric   // convergent function calls in GVNHoist and JumpThreading.
234bdd1243dSDimitry Andric   for (auto *BB : L->blocks()) {
2355ffd83dbSDimitry Andric     for (auto &II : *BB) {
2365ffd83dbSDimitry Andric       if (auto CI = dyn_cast<CallBase>(&II)) {
2375ffd83dbSDimitry Andric         if (CI->isConvergent()) {
2385ffd83dbSDimitry Andric           return nullptr;
2395ffd83dbSDimitry Andric         }
2405ffd83dbSDimitry Andric       }
2415ffd83dbSDimitry Andric     }
2425ffd83dbSDimitry Andric   }
2435ffd83dbSDimitry Andric 
2440b57cec5SDimitry Andric   // The header is not a landing pad; preheader insertion should ensure this.
2450b57cec5SDimitry Andric   BasicBlock *Header = L->getHeader();
2460b57cec5SDimitry Andric   assert(!Header->isEHPad() && "Can't insert backedge to EH pad");
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   PHINode *PN = findPHIToPartitionLoops(L, DT, AC);
2490b57cec5SDimitry Andric   if (!PN) return nullptr;  // No known way to partition.
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric   // Pull out all predecessors that have varying values in the loop.  This
2520b57cec5SDimitry Andric   // handles the case when a PHI node has multiple instances of itself as
2530b57cec5SDimitry Andric   // arguments.
2540b57cec5SDimitry Andric   SmallVector<BasicBlock*, 8> OuterLoopPreds;
2550b57cec5SDimitry Andric   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2560b57cec5SDimitry Andric     if (PN->getIncomingValue(i) != PN ||
2570b57cec5SDimitry Andric         !L->contains(PN->getIncomingBlock(i))) {
2580b57cec5SDimitry Andric       // We can't split indirect control flow edges.
259fcaf7f86SDimitry Andric       if (isa<IndirectBrInst>(PN->getIncomingBlock(i)->getTerminator()))
2600b57cec5SDimitry Andric         return nullptr;
2610b57cec5SDimitry Andric       OuterLoopPreds.push_back(PN->getIncomingBlock(i));
2620b57cec5SDimitry Andric     }
2630b57cec5SDimitry Andric   }
2640b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "LoopSimplify: Splitting out a new outer loop\n");
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric   // If ScalarEvolution is around and knows anything about values in
2670b57cec5SDimitry Andric   // this loop, tell it to forget them, because we're about to
2680b57cec5SDimitry Andric   // substantially change it.
2690b57cec5SDimitry Andric   if (SE)
2700b57cec5SDimitry Andric     SE->forgetLoop(L);
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   BasicBlock *NewBB = SplitBlockPredecessors(Header, OuterLoopPreds, ".outer",
2730b57cec5SDimitry Andric                                              DT, LI, MSSAU, PreserveLCSSA);
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   // Make sure that NewBB is put someplace intelligent, which doesn't mess up
2760b57cec5SDimitry Andric   // code layout too horribly.
2770b57cec5SDimitry Andric   placeSplitBlockCarefully(NewBB, OuterLoopPreds, L);
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   // Create the new outer loop.
2800b57cec5SDimitry Andric   Loop *NewOuter = LI->AllocateLoop();
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // Change the parent loop to use the outer loop as its child now.
2830b57cec5SDimitry Andric   if (Loop *Parent = L->getParentLoop())
2840b57cec5SDimitry Andric     Parent->replaceChildLoopWith(L, NewOuter);
2850b57cec5SDimitry Andric   else
2860b57cec5SDimitry Andric     LI->changeTopLevelLoop(L, NewOuter);
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // L is now a subloop of our outer loop.
2890b57cec5SDimitry Andric   NewOuter->addChildLoop(L);
2900b57cec5SDimitry Andric 
2915e801ac6SDimitry Andric   for (BasicBlock *BB : L->blocks())
2925e801ac6SDimitry Andric     NewOuter->addBlockEntry(BB);
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   // Now reset the header in L, which had been moved by
2950b57cec5SDimitry Andric   // SplitBlockPredecessors for the outer loop.
2960b57cec5SDimitry Andric   L->moveToHeader(Header);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   // Determine which blocks should stay in L and which should be moved out to
2990b57cec5SDimitry Andric   // the Outer loop now.
300e8d8bef9SDimitry Andric   SmallPtrSet<BasicBlock *, 4> BlocksInL;
301e8d8bef9SDimitry Andric   for (BasicBlock *P : predecessors(Header)) {
3020b57cec5SDimitry Andric     if (DT->dominates(Header, P))
3030b57cec5SDimitry Andric       addBlockAndPredsToSet(P, Header, BlocksInL);
3040b57cec5SDimitry Andric   }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric   // Scan all of the loop children of L, moving them to OuterLoop if they are
3070b57cec5SDimitry Andric   // not part of the inner loop.
3080b57cec5SDimitry Andric   const std::vector<Loop*> &SubLoops = L->getSubLoops();
3090b57cec5SDimitry Andric   for (size_t I = 0; I != SubLoops.size(); )
3100b57cec5SDimitry Andric     if (BlocksInL.count(SubLoops[I]->getHeader()))
3110b57cec5SDimitry Andric       ++I;   // Loop remains in L
3120b57cec5SDimitry Andric     else
3130b57cec5SDimitry Andric       NewOuter->addChildLoop(L->removeChildLoop(SubLoops.begin() + I));
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   SmallVector<BasicBlock *, 8> OuterLoopBlocks;
3160b57cec5SDimitry Andric   OuterLoopBlocks.push_back(NewBB);
3170b57cec5SDimitry Andric   // Now that we know which blocks are in L and which need to be moved to
3180b57cec5SDimitry Andric   // OuterLoop, move any blocks that need it.
3190b57cec5SDimitry Andric   for (unsigned i = 0; i != L->getBlocks().size(); ++i) {
3200b57cec5SDimitry Andric     BasicBlock *BB = L->getBlocks()[i];
3210b57cec5SDimitry Andric     if (!BlocksInL.count(BB)) {
3220b57cec5SDimitry Andric       // Move this block to the parent, updating the exit blocks sets
3230b57cec5SDimitry Andric       L->removeBlockFromLoop(BB);
3240b57cec5SDimitry Andric       if ((*LI)[BB] == L) {
3250b57cec5SDimitry Andric         LI->changeLoopFor(BB, NewOuter);
3260b57cec5SDimitry Andric         OuterLoopBlocks.push_back(BB);
3270b57cec5SDimitry Andric       }
3280b57cec5SDimitry Andric       --i;
3290b57cec5SDimitry Andric     }
3300b57cec5SDimitry Andric   }
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   // Split edges to exit blocks from the inner loop, if they emerged in the
3330b57cec5SDimitry Andric   // process of separating the outer one.
3340b57cec5SDimitry Andric   formDedicatedExitBlocks(L, DT, LI, MSSAU, PreserveLCSSA);
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   if (PreserveLCSSA) {
3370b57cec5SDimitry Andric     // Fix LCSSA form for L. Some values, which previously were only used inside
3380b57cec5SDimitry Andric     // L, can now be used in NewOuter loop. We need to insert phi-nodes for them
3390b57cec5SDimitry Andric     // in corresponding exit blocks.
3400b57cec5SDimitry Andric     // We don't need to form LCSSA recursively, because there cannot be uses
3410b57cec5SDimitry Andric     // inside a newly created loop of defs from inner loops as those would
3420b57cec5SDimitry Andric     // already be a use of an LCSSA phi node.
3430b57cec5SDimitry Andric     formLCSSA(*L, *DT, LI, SE);
3440b57cec5SDimitry Andric 
3450b57cec5SDimitry Andric     assert(NewOuter->isRecursivelyLCSSAForm(*DT, *LI) &&
3460b57cec5SDimitry Andric            "LCSSA is broken after separating nested loops!");
3470b57cec5SDimitry Andric   }
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   return NewOuter;
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric /// This method is called when the specified loop has more than one
3530b57cec5SDimitry Andric /// backedge in it.
3540b57cec5SDimitry Andric ///
3550b57cec5SDimitry Andric /// If this occurs, revector all of these backedges to target a new basic block
3560b57cec5SDimitry Andric /// and have that block branch to the loop header.  This ensures that loops
3570b57cec5SDimitry Andric /// have exactly one backedge.
insertUniqueBackedgeBlock(Loop * L,BasicBlock * Preheader,DominatorTree * DT,LoopInfo * LI,MemorySSAUpdater * MSSAU)3580b57cec5SDimitry Andric static BasicBlock *insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader,
3590b57cec5SDimitry Andric                                              DominatorTree *DT, LoopInfo *LI,
3600b57cec5SDimitry Andric                                              MemorySSAUpdater *MSSAU) {
3610b57cec5SDimitry Andric   assert(L->getNumBackEdges() > 1 && "Must have > 1 backedge!");
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // Get information about the loop
3640b57cec5SDimitry Andric   BasicBlock *Header = L->getHeader();
3650b57cec5SDimitry Andric   Function *F = Header->getParent();
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric   // Unique backedge insertion currently depends on having a preheader.
3680b57cec5SDimitry Andric   if (!Preheader)
3690b57cec5SDimitry Andric     return nullptr;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   // The header is not an EH pad; preheader insertion should ensure this.
3720b57cec5SDimitry Andric   assert(!Header->isEHPad() && "Can't insert backedge to EH pad");
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric   // Figure out which basic blocks contain back-edges to the loop header.
3750b57cec5SDimitry Andric   std::vector<BasicBlock*> BackedgeBlocks;
376fe6060f1SDimitry Andric   for (BasicBlock *P : predecessors(Header)) {
3770b57cec5SDimitry Andric     // Indirect edges cannot be split, so we must fail if we find one.
378fcaf7f86SDimitry Andric     if (isa<IndirectBrInst>(P->getTerminator()))
3790b57cec5SDimitry Andric       return nullptr;
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric     if (P != Preheader) BackedgeBlocks.push_back(P);
3820b57cec5SDimitry Andric   }
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   // Create and insert the new backedge block...
3850b57cec5SDimitry Andric   BasicBlock *BEBlock = BasicBlock::Create(Header->getContext(),
3860b57cec5SDimitry Andric                                            Header->getName() + ".backedge", F);
3870b57cec5SDimitry Andric   BranchInst *BETerminator = BranchInst::Create(Header, BEBlock);
3880b57cec5SDimitry Andric   BETerminator->setDebugLoc(Header->getFirstNonPHI()->getDebugLoc());
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "LoopSimplify: Inserting unique backedge block "
3910b57cec5SDimitry Andric                     << BEBlock->getName() << "\n");
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   // Move the new backedge block to right after the last backedge block.
3940b57cec5SDimitry Andric   Function::iterator InsertPos = ++BackedgeBlocks.back()->getIterator();
395bdd1243dSDimitry Andric   F->splice(InsertPos, F, BEBlock->getIterator());
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric   // Now that the block has been inserted into the function, create PHI nodes in
3980b57cec5SDimitry Andric   // the backedge block which correspond to any PHI nodes in the header block.
3990b57cec5SDimitry Andric   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
4000b57cec5SDimitry Andric     PHINode *PN = cast<PHINode>(I);
4010b57cec5SDimitry Andric     PHINode *NewPN = PHINode::Create(PN->getType(), BackedgeBlocks.size(),
4020b57cec5SDimitry Andric                                      PN->getName()+".be", BETerminator);
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric     // Loop over the PHI node, moving all entries except the one for the
4050b57cec5SDimitry Andric     // preheader over to the new PHI node.
4060b57cec5SDimitry Andric     unsigned PreheaderIdx = ~0U;
4070b57cec5SDimitry Andric     bool HasUniqueIncomingValue = true;
4080b57cec5SDimitry Andric     Value *UniqueValue = nullptr;
4090b57cec5SDimitry Andric     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4100b57cec5SDimitry Andric       BasicBlock *IBB = PN->getIncomingBlock(i);
4110b57cec5SDimitry Andric       Value *IV = PN->getIncomingValue(i);
4120b57cec5SDimitry Andric       if (IBB == Preheader) {
4130b57cec5SDimitry Andric         PreheaderIdx = i;
4140b57cec5SDimitry Andric       } else {
4150b57cec5SDimitry Andric         NewPN->addIncoming(IV, IBB);
4160b57cec5SDimitry Andric         if (HasUniqueIncomingValue) {
4170b57cec5SDimitry Andric           if (!UniqueValue)
4180b57cec5SDimitry Andric             UniqueValue = IV;
4190b57cec5SDimitry Andric           else if (UniqueValue != IV)
4200b57cec5SDimitry Andric             HasUniqueIncomingValue = false;
4210b57cec5SDimitry Andric         }
4220b57cec5SDimitry Andric       }
4230b57cec5SDimitry Andric     }
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric     // Delete all of the incoming values from the old PN except the preheader's
4260b57cec5SDimitry Andric     assert(PreheaderIdx != ~0U && "PHI has no preheader entry??");
4270b57cec5SDimitry Andric     if (PreheaderIdx != 0) {
4280b57cec5SDimitry Andric       PN->setIncomingValue(0, PN->getIncomingValue(PreheaderIdx));
4290b57cec5SDimitry Andric       PN->setIncomingBlock(0, PN->getIncomingBlock(PreheaderIdx));
4300b57cec5SDimitry Andric     }
4310b57cec5SDimitry Andric     // Nuke all entries except the zero'th.
4325f757f3fSDimitry Andric     PN->removeIncomingValueIf([](unsigned Idx) { return Idx != 0; },
4335f757f3fSDimitry Andric                               /* DeletePHIIfEmpty */ false);
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric     // Finally, add the newly constructed PHI node as the entry for the BEBlock.
4360b57cec5SDimitry Andric     PN->addIncoming(NewPN, BEBlock);
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric     // As an optimization, if all incoming values in the new PhiNode (which is a
4390b57cec5SDimitry Andric     // subset of the incoming values of the old PHI node) have the same value,
4400b57cec5SDimitry Andric     // eliminate the PHI Node.
4410b57cec5SDimitry Andric     if (HasUniqueIncomingValue) {
4420b57cec5SDimitry Andric       NewPN->replaceAllUsesWith(UniqueValue);
443bdd1243dSDimitry Andric       NewPN->eraseFromParent();
4440b57cec5SDimitry Andric     }
4450b57cec5SDimitry Andric   }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric   // Now that all of the PHI nodes have been inserted and adjusted, modify the
4480b57cec5SDimitry Andric   // backedge blocks to jump to the BEBlock instead of the header.
4490b57cec5SDimitry Andric   // If one of the backedges has llvm.loop metadata attached, we remove
4500b57cec5SDimitry Andric   // it from the backedge and add it to BEBlock.
4510b57cec5SDimitry Andric   MDNode *LoopMD = nullptr;
452bdd1243dSDimitry Andric   for (BasicBlock *BB : BackedgeBlocks) {
453bdd1243dSDimitry Andric     Instruction *TI = BB->getTerminator();
4540b57cec5SDimitry Andric     if (!LoopMD)
45506c3fb27SDimitry Andric       LoopMD = TI->getMetadata(LLVMContext::MD_loop);
45606c3fb27SDimitry Andric     TI->setMetadata(LLVMContext::MD_loop, nullptr);
4570b57cec5SDimitry Andric     TI->replaceSuccessorWith(Header, BEBlock);
4580b57cec5SDimitry Andric   }
45906c3fb27SDimitry Andric   BEBlock->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric   //===--- Update all analyses which we must preserve now -----------------===//
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric   // Update Loop Information - we know that this block is now in the current
4640b57cec5SDimitry Andric   // loop and all parent loops.
4650b57cec5SDimitry Andric   L->addBasicBlockToLoop(BEBlock, *LI);
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   // Update dominator information
4680b57cec5SDimitry Andric   DT->splitBlock(BEBlock);
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   if (MSSAU)
4710b57cec5SDimitry Andric     MSSAU->updatePhisWhenInsertingUniqueBackedgeBlock(Header, Preheader,
4720b57cec5SDimitry Andric                                                       BEBlock);
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   return BEBlock;
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric /// Simplify one loop and queue further loops for simplification.
simplifyOneLoop(Loop * L,SmallVectorImpl<Loop * > & Worklist,DominatorTree * DT,LoopInfo * LI,ScalarEvolution * SE,AssumptionCache * AC,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)4780b57cec5SDimitry Andric static bool simplifyOneLoop(Loop *L, SmallVectorImpl<Loop *> &Worklist,
4790b57cec5SDimitry Andric                             DominatorTree *DT, LoopInfo *LI,
4800b57cec5SDimitry Andric                             ScalarEvolution *SE, AssumptionCache *AC,
4810b57cec5SDimitry Andric                             MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
4820b57cec5SDimitry Andric   bool Changed = false;
4830b57cec5SDimitry Andric   if (MSSAU && VerifyMemorySSA)
4840b57cec5SDimitry Andric     MSSAU->getMemorySSA()->verifyMemorySSA();
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric ReprocessLoop:
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   // Check to see that no blocks (other than the header) in this loop have
4890b57cec5SDimitry Andric   // predecessors that are not in the loop.  This is not valid for natural
4900b57cec5SDimitry Andric   // loops, but can occur if the blocks are unreachable.  Since they are
4910b57cec5SDimitry Andric   // unreachable we can just shamelessly delete those CFG edges!
4925e801ac6SDimitry Andric   for (BasicBlock *BB : L->blocks()) {
4935e801ac6SDimitry Andric     if (BB == L->getHeader())
4945e801ac6SDimitry Andric       continue;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric     SmallPtrSet<BasicBlock*, 4> BadPreds;
4975e801ac6SDimitry Andric     for (BasicBlock *P : predecessors(BB))
4980b57cec5SDimitry Andric       if (!L->contains(P))
4990b57cec5SDimitry Andric         BadPreds.insert(P);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric     // Delete each unique out-of-loop (and thus dead) predecessor.
5020b57cec5SDimitry Andric     for (BasicBlock *P : BadPreds) {
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSimplify: Deleting edge from dead predecessor "
5050b57cec5SDimitry Andric                         << P->getName() << "\n");
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric       // Zap the dead pred's terminator and replace it with unreachable.
5080b57cec5SDimitry Andric       Instruction *TI = P->getTerminator();
509fe6060f1SDimitry Andric       changeToUnreachable(TI, PreserveLCSSA,
5100b57cec5SDimitry Andric                           /*DTU=*/nullptr, MSSAU);
5110b57cec5SDimitry Andric       Changed = true;
5120b57cec5SDimitry Andric     }
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric 
5150b57cec5SDimitry Andric   if (MSSAU && VerifyMemorySSA)
5160b57cec5SDimitry Andric     MSSAU->getMemorySSA()->verifyMemorySSA();
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric   // If there are exiting blocks with branches on undef, resolve the undef in
5190b57cec5SDimitry Andric   // the direction which will exit the loop. This will help simplify loop
5200b57cec5SDimitry Andric   // trip count computations.
5210b57cec5SDimitry Andric   SmallVector<BasicBlock*, 8> ExitingBlocks;
5220b57cec5SDimitry Andric   L->getExitingBlocks(ExitingBlocks);
5230b57cec5SDimitry Andric   for (BasicBlock *ExitingBlock : ExitingBlocks)
5240b57cec5SDimitry Andric     if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
5250b57cec5SDimitry Andric       if (BI->isConditional()) {
5260b57cec5SDimitry Andric         if (UndefValue *Cond = dyn_cast<UndefValue>(BI->getCondition())) {
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric           LLVM_DEBUG(dbgs()
5290b57cec5SDimitry Andric                      << "LoopSimplify: Resolving \"br i1 undef\" to exit in "
5300b57cec5SDimitry Andric                      << ExitingBlock->getName() << "\n");
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric           BI->setCondition(ConstantInt::get(Cond->getType(),
5330b57cec5SDimitry Andric                                             !L->contains(BI->getSuccessor(0))));
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric           Changed = true;
5360b57cec5SDimitry Andric         }
5370b57cec5SDimitry Andric       }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   // Does the loop already have a preheader?  If so, don't insert one.
5400b57cec5SDimitry Andric   BasicBlock *Preheader = L->getLoopPreheader();
5410b57cec5SDimitry Andric   if (!Preheader) {
5420b57cec5SDimitry Andric     Preheader = InsertPreheaderForLoop(L, DT, LI, MSSAU, PreserveLCSSA);
5430b57cec5SDimitry Andric     if (Preheader)
5440b57cec5SDimitry Andric       Changed = true;
5450b57cec5SDimitry Andric   }
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   // Next, check to make sure that all exit nodes of the loop only have
5480b57cec5SDimitry Andric   // predecessors that are inside of the loop.  This check guarantees that the
5490b57cec5SDimitry Andric   // loop preheader/header will dominate the exit blocks.  If the exit block has
5500b57cec5SDimitry Andric   // predecessors from outside of the loop, split the edge now.
5510b57cec5SDimitry Andric   if (formDedicatedExitBlocks(L, DT, LI, MSSAU, PreserveLCSSA))
5520b57cec5SDimitry Andric     Changed = true;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   if (MSSAU && VerifyMemorySSA)
5550b57cec5SDimitry Andric     MSSAU->getMemorySSA()->verifyMemorySSA();
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric   // If the header has more than two predecessors at this point (from the
5580b57cec5SDimitry Andric   // preheader and from multiple backedges), we must adjust the loop.
5590b57cec5SDimitry Andric   BasicBlock *LoopLatch = L->getLoopLatch();
5600b57cec5SDimitry Andric   if (!LoopLatch) {
5610b57cec5SDimitry Andric     // If this is really a nested loop, rip it out into a child loop.  Don't do
5620b57cec5SDimitry Andric     // this for loops with a giant number of backedges, just factor them into a
5630b57cec5SDimitry Andric     // common backedge instead.
5640b57cec5SDimitry Andric     if (L->getNumBackEdges() < 8) {
5650b57cec5SDimitry Andric       if (Loop *OuterL = separateNestedLoop(L, Preheader, DT, LI, SE,
5660b57cec5SDimitry Andric                                             PreserveLCSSA, AC, MSSAU)) {
5670b57cec5SDimitry Andric         ++NumNested;
5680b57cec5SDimitry Andric         // Enqueue the outer loop as it should be processed next in our
5690b57cec5SDimitry Andric         // depth-first nest walk.
5700b57cec5SDimitry Andric         Worklist.push_back(OuterL);
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric         // This is a big restructuring change, reprocess the whole loop.
5730b57cec5SDimitry Andric         Changed = true;
5740b57cec5SDimitry Andric         // GCC doesn't tail recursion eliminate this.
5750b57cec5SDimitry Andric         // FIXME: It isn't clear we can't rely on LLVM to TRE this.
5760b57cec5SDimitry Andric         goto ReprocessLoop;
5770b57cec5SDimitry Andric       }
5780b57cec5SDimitry Andric     }
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric     // If we either couldn't, or didn't want to, identify nesting of the loops,
5810b57cec5SDimitry Andric     // insert a new block that all backedges target, then make it jump to the
5820b57cec5SDimitry Andric     // loop header.
5830b57cec5SDimitry Andric     LoopLatch = insertUniqueBackedgeBlock(L, Preheader, DT, LI, MSSAU);
5840b57cec5SDimitry Andric     if (LoopLatch)
5850b57cec5SDimitry Andric       Changed = true;
5860b57cec5SDimitry Andric   }
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   if (MSSAU && VerifyMemorySSA)
5890b57cec5SDimitry Andric     MSSAU->getMemorySSA()->verifyMemorySSA();
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric   // Scan over the PHI nodes in the loop header.  Since they now have only two
5940b57cec5SDimitry Andric   // incoming values (the loop is canonicalized), we may have simplified the PHI
5950b57cec5SDimitry Andric   // down to 'X = phi [X, Y]', which should be replaced with 'Y'.
5960b57cec5SDimitry Andric   PHINode *PN;
5970b57cec5SDimitry Andric   for (BasicBlock::iterator I = L->getHeader()->begin();
5980b57cec5SDimitry Andric        (PN = dyn_cast<PHINode>(I++)); )
59981ad6265SDimitry Andric     if (Value *V = simplifyInstruction(PN, {DL, nullptr, DT, AC})) {
6000b57cec5SDimitry Andric       if (SE) SE->forgetValue(PN);
6010b57cec5SDimitry Andric       if (!PreserveLCSSA || LI->replacementPreservesLCSSAForm(PN, V)) {
6020b57cec5SDimitry Andric         PN->replaceAllUsesWith(V);
6030b57cec5SDimitry Andric         PN->eraseFromParent();
6045ffd83dbSDimitry Andric         Changed = true;
6050b57cec5SDimitry Andric       }
6060b57cec5SDimitry Andric     }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   // If this loop has multiple exits and the exits all go to the same
6090b57cec5SDimitry Andric   // block, attempt to merge the exits. This helps several passes, such
6100b57cec5SDimitry Andric   // as LoopRotation, which do not support loops with multiple exits.
6110b57cec5SDimitry Andric   // SimplifyCFG also does this (and this code uses the same utility
6120b57cec5SDimitry Andric   // function), however this code is loop-aware, where SimplifyCFG is
6130b57cec5SDimitry Andric   // not. That gives it the advantage of being able to hoist
6140b57cec5SDimitry Andric   // loop-invariant instructions out of the way to open up more
6150b57cec5SDimitry Andric   // opportunities, and the disadvantage of having the responsibility
6160b57cec5SDimitry Andric   // to preserve dominator information.
6170b57cec5SDimitry Andric   auto HasUniqueExitBlock = [&]() {
6180b57cec5SDimitry Andric     BasicBlock *UniqueExit = nullptr;
6190b57cec5SDimitry Andric     for (auto *ExitingBB : ExitingBlocks)
6200b57cec5SDimitry Andric       for (auto *SuccBB : successors(ExitingBB)) {
6210b57cec5SDimitry Andric         if (L->contains(SuccBB))
6220b57cec5SDimitry Andric           continue;
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric         if (!UniqueExit)
6250b57cec5SDimitry Andric           UniqueExit = SuccBB;
6260b57cec5SDimitry Andric         else if (UniqueExit != SuccBB)
6270b57cec5SDimitry Andric           return false;
6280b57cec5SDimitry Andric       }
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric     return true;
6310b57cec5SDimitry Andric   };
6320b57cec5SDimitry Andric   if (HasUniqueExitBlock()) {
6330b57cec5SDimitry Andric     for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
6340b57cec5SDimitry Andric       BasicBlock *ExitingBlock = ExitingBlocks[i];
6350b57cec5SDimitry Andric       if (!ExitingBlock->getSinglePredecessor()) continue;
6360b57cec5SDimitry Andric       BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
6370b57cec5SDimitry Andric       if (!BI || !BI->isConditional()) continue;
6380b57cec5SDimitry Andric       CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition());
6390b57cec5SDimitry Andric       if (!CI || CI->getParent() != ExitingBlock) continue;
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric       // Attempt to hoist out all instructions except for the
6420b57cec5SDimitry Andric       // comparison and the branch.
6430b57cec5SDimitry Andric       bool AllInvariant = true;
6440b57cec5SDimitry Andric       bool AnyInvariant = false;
6450b57cec5SDimitry Andric       for (auto I = ExitingBlock->instructionsWithoutDebug().begin(); &*I != BI; ) {
6460b57cec5SDimitry Andric         Instruction *Inst = &*I++;
6470b57cec5SDimitry Andric         if (Inst == CI)
6480b57cec5SDimitry Andric           continue;
6490b57cec5SDimitry Andric         if (!L->makeLoopInvariant(
6500b57cec5SDimitry Andric                 Inst, AnyInvariant,
651bdd1243dSDimitry Andric                 Preheader ? Preheader->getTerminator() : nullptr, MSSAU, SE)) {
6520b57cec5SDimitry Andric           AllInvariant = false;
6530b57cec5SDimitry Andric           break;
6540b57cec5SDimitry Andric         }
6550b57cec5SDimitry Andric       }
656bdd1243dSDimitry Andric       if (AnyInvariant)
6570b57cec5SDimitry Andric         Changed = true;
6580b57cec5SDimitry Andric       if (!AllInvariant) continue;
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric       // The block has now been cleared of all instructions except for
6610b57cec5SDimitry Andric       // a comparison and a conditional branch. SimplifyCFG may be able
6620b57cec5SDimitry Andric       // to fold it now.
663e8d8bef9SDimitry Andric       if (!FoldBranchToCommonDest(BI, /*DTU=*/nullptr, MSSAU))
6640b57cec5SDimitry Andric         continue;
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric       // Success. The block is now dead, so remove it from the loop,
6670b57cec5SDimitry Andric       // update the dominator tree and delete it.
6680b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "LoopSimplify: Eliminating exiting block "
6690b57cec5SDimitry Andric                         << ExitingBlock->getName() << "\n");
6700b57cec5SDimitry Andric 
671e8d8bef9SDimitry Andric       assert(pred_empty(ExitingBlock));
6720b57cec5SDimitry Andric       Changed = true;
6730b57cec5SDimitry Andric       LI->removeBlock(ExitingBlock);
6740b57cec5SDimitry Andric 
6750b57cec5SDimitry Andric       DomTreeNode *Node = DT->getNode(ExitingBlock);
6765ffd83dbSDimitry Andric       while (!Node->isLeaf()) {
6775ffd83dbSDimitry Andric         DomTreeNode *Child = Node->back();
6780b57cec5SDimitry Andric         DT->changeImmediateDominator(Child, Node->getIDom());
6790b57cec5SDimitry Andric       }
6800b57cec5SDimitry Andric       DT->eraseNode(ExitingBlock);
6810b57cec5SDimitry Andric       if (MSSAU) {
6820b57cec5SDimitry Andric         SmallSetVector<BasicBlock *, 8> ExitBlockSet;
6830b57cec5SDimitry Andric         ExitBlockSet.insert(ExitingBlock);
6840b57cec5SDimitry Andric         MSSAU->removeBlocks(ExitBlockSet);
6850b57cec5SDimitry Andric       }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric       BI->getSuccessor(0)->removePredecessor(
6880b57cec5SDimitry Andric           ExitingBlock, /* KeepOneInputPHIs */ PreserveLCSSA);
6890b57cec5SDimitry Andric       BI->getSuccessor(1)->removePredecessor(
6900b57cec5SDimitry Andric           ExitingBlock, /* KeepOneInputPHIs */ PreserveLCSSA);
6910b57cec5SDimitry Andric       ExitingBlock->eraseFromParent();
6920b57cec5SDimitry Andric     }
6930b57cec5SDimitry Andric   }
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric   if (MSSAU && VerifyMemorySSA)
6960b57cec5SDimitry Andric     MSSAU->getMemorySSA()->verifyMemorySSA();
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   return Changed;
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric 
simplifyLoop(Loop * L,DominatorTree * DT,LoopInfo * LI,ScalarEvolution * SE,AssumptionCache * AC,MemorySSAUpdater * MSSAU,bool PreserveLCSSA)7010b57cec5SDimitry Andric bool llvm::simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI,
7020b57cec5SDimitry Andric                         ScalarEvolution *SE, AssumptionCache *AC,
7030b57cec5SDimitry Andric                         MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
7040b57cec5SDimitry Andric   bool Changed = false;
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric #ifndef NDEBUG
7070b57cec5SDimitry Andric   // If we're asked to preserve LCSSA, the loop nest needs to start in LCSSA
7080b57cec5SDimitry Andric   // form.
7090b57cec5SDimitry Andric   if (PreserveLCSSA) {
7100b57cec5SDimitry Andric     assert(DT && "DT not available.");
7110b57cec5SDimitry Andric     assert(LI && "LI not available.");
7120b57cec5SDimitry Andric     assert(L->isRecursivelyLCSSAForm(*DT, *LI) &&
7130b57cec5SDimitry Andric            "Requested to preserve LCSSA, but it's already broken.");
7140b57cec5SDimitry Andric   }
7150b57cec5SDimitry Andric #endif
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric   // Worklist maintains our depth-first queue of loops in this nest to process.
7180b57cec5SDimitry Andric   SmallVector<Loop *, 4> Worklist;
7190b57cec5SDimitry Andric   Worklist.push_back(L);
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric   // Walk the worklist from front to back, pushing newly found sub loops onto
7220b57cec5SDimitry Andric   // the back. This will let us process loops from back to front in depth-first
7230b57cec5SDimitry Andric   // order. We can use this simple process because loops form a tree.
7240b57cec5SDimitry Andric   for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
7250b57cec5SDimitry Andric     Loop *L2 = Worklist[Idx];
7260b57cec5SDimitry Andric     Worklist.append(L2->begin(), L2->end());
7270b57cec5SDimitry Andric   }
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric   while (!Worklist.empty())
7300b57cec5SDimitry Andric     Changed |= simplifyOneLoop(Worklist.pop_back_val(), Worklist, DT, LI, SE,
7310b57cec5SDimitry Andric                                AC, MSSAU, PreserveLCSSA);
7320b57cec5SDimitry Andric 
73306c3fb27SDimitry Andric   // Changing exit conditions for blocks may affect exit counts of this loop and
73406c3fb27SDimitry Andric   // any of its parents, so we must invalidate the entire subtree if we've made
73506c3fb27SDimitry Andric   // any changes. Do this here rather than in simplifyOneLoop() as the top-most
73606c3fb27SDimitry Andric   // loop is going to be the same for all child loops.
73706c3fb27SDimitry Andric   if (Changed && SE)
73806c3fb27SDimitry Andric     SE->forgetTopmostLoop(L);
73906c3fb27SDimitry Andric 
7400b57cec5SDimitry Andric   return Changed;
7410b57cec5SDimitry Andric }
7420b57cec5SDimitry Andric 
7430b57cec5SDimitry Andric namespace {
7440b57cec5SDimitry Andric   struct LoopSimplify : public FunctionPass {
7450b57cec5SDimitry Andric     static char ID; // Pass identification, replacement for typeid
LoopSimplify__anon1adfc6fd0311::LoopSimplify7460b57cec5SDimitry Andric     LoopSimplify() : FunctionPass(ID) {
7470b57cec5SDimitry Andric       initializeLoopSimplifyPass(*PassRegistry::getPassRegistry());
7480b57cec5SDimitry Andric     }
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric     bool runOnFunction(Function &F) override;
7510b57cec5SDimitry Andric 
getAnalysisUsage__anon1adfc6fd0311::LoopSimplify7520b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
7530b57cec5SDimitry Andric       AU.addRequired<AssumptionCacheTracker>();
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric       // We need loop information to identify the loops...
7560b57cec5SDimitry Andric       AU.addRequired<DominatorTreeWrapperPass>();
7570b57cec5SDimitry Andric       AU.addPreserved<DominatorTreeWrapperPass>();
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric       AU.addRequired<LoopInfoWrapperPass>();
7600b57cec5SDimitry Andric       AU.addPreserved<LoopInfoWrapperPass>();
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric       AU.addPreserved<BasicAAWrapperPass>();
7630b57cec5SDimitry Andric       AU.addPreserved<AAResultsWrapperPass>();
7640b57cec5SDimitry Andric       AU.addPreserved<GlobalsAAWrapperPass>();
7650b57cec5SDimitry Andric       AU.addPreserved<ScalarEvolutionWrapperPass>();
7660b57cec5SDimitry Andric       AU.addPreserved<SCEVAAWrapperPass>();
7670b57cec5SDimitry Andric       AU.addPreservedID(LCSSAID);
7680b57cec5SDimitry Andric       AU.addPreserved<DependenceAnalysisWrapperPass>();
7690b57cec5SDimitry Andric       AU.addPreservedID(BreakCriticalEdgesID);  // No critical edges added.
7700b57cec5SDimitry Andric       AU.addPreserved<BranchProbabilityInfoWrapperPass>();
7710b57cec5SDimitry Andric       AU.addPreserved<MemorySSAWrapperPass>();
7720b57cec5SDimitry Andric     }
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric     /// verifyAnalysis() - Verify LoopSimplifyForm's guarantees.
7750b57cec5SDimitry Andric     void verifyAnalysis() const override;
7760b57cec5SDimitry Andric   };
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric char LoopSimplify::ID = 0;
7800b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LoopSimplify, "loop-simplify",
7810b57cec5SDimitry Andric                 "Canonicalize natural loops", false, false)
7820b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
7830b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
7840b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
7850b57cec5SDimitry Andric INITIALIZE_PASS_END(LoopSimplify, "loop-simplify",
7860b57cec5SDimitry Andric                 "Canonicalize natural loops", false, false)
7870b57cec5SDimitry Andric 
7880b57cec5SDimitry Andric // Publicly exposed interface to pass...
7890b57cec5SDimitry Andric char &llvm::LoopSimplifyID = LoopSimplify::ID;
createLoopSimplifyPass()7900b57cec5SDimitry Andric Pass *llvm::createLoopSimplifyPass() { return new LoopSimplify(); }
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric /// runOnFunction - Run down all loops in the CFG (recursively, but we could do
7930b57cec5SDimitry Andric /// it in any convenient order) inserting preheaders...
7940b57cec5SDimitry Andric ///
runOnFunction(Function & F)7950b57cec5SDimitry Andric bool LoopSimplify::runOnFunction(Function &F) {
7960b57cec5SDimitry Andric   bool Changed = false;
7970b57cec5SDimitry Andric   LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
7980b57cec5SDimitry Andric   DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
7990b57cec5SDimitry Andric   auto *SEWP = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
8000b57cec5SDimitry Andric   ScalarEvolution *SE = SEWP ? &SEWP->getSE() : nullptr;
8010b57cec5SDimitry Andric   AssumptionCache *AC =
8020b57cec5SDimitry Andric       &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
8030b57cec5SDimitry Andric   MemorySSA *MSSA = nullptr;
8040b57cec5SDimitry Andric   std::unique_ptr<MemorySSAUpdater> MSSAU;
8050b57cec5SDimitry Andric   auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
8060b57cec5SDimitry Andric   if (MSSAAnalysis) {
8070b57cec5SDimitry Andric     MSSA = &MSSAAnalysis->getMSSA();
8088bcb0991SDimitry Andric     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
8090b57cec5SDimitry Andric   }
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   bool PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric   // Simplify each loop nest in the function.
814e8d8bef9SDimitry Andric   for (auto *L : *LI)
815e8d8bef9SDimitry Andric     Changed |= simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), PreserveLCSSA);
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric #ifndef NDEBUG
8180b57cec5SDimitry Andric   if (PreserveLCSSA) {
8190b57cec5SDimitry Andric     bool InLCSSA = all_of(
8200b57cec5SDimitry Andric         *LI, [&](Loop *L) { return L->isRecursivelyLCSSAForm(*DT, *LI); });
8210b57cec5SDimitry Andric     assert(InLCSSA && "LCSSA is broken after loop-simplify.");
8220b57cec5SDimitry Andric   }
8230b57cec5SDimitry Andric #endif
8240b57cec5SDimitry Andric   return Changed;
8250b57cec5SDimitry Andric }
8260b57cec5SDimitry Andric 
run(Function & F,FunctionAnalysisManager & AM)8270b57cec5SDimitry Andric PreservedAnalyses LoopSimplifyPass::run(Function &F,
8280b57cec5SDimitry Andric                                         FunctionAnalysisManager &AM) {
8290b57cec5SDimitry Andric   bool Changed = false;
8300b57cec5SDimitry Andric   LoopInfo *LI = &AM.getResult<LoopAnalysis>(F);
8310b57cec5SDimitry Andric   DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
8320b57cec5SDimitry Andric   ScalarEvolution *SE = AM.getCachedResult<ScalarEvolutionAnalysis>(F);
8330b57cec5SDimitry Andric   AssumptionCache *AC = &AM.getResult<AssumptionAnalysis>(F);
8348bcb0991SDimitry Andric   auto *MSSAAnalysis = AM.getCachedResult<MemorySSAAnalysis>(F);
8358bcb0991SDimitry Andric   std::unique_ptr<MemorySSAUpdater> MSSAU;
8368bcb0991SDimitry Andric   if (MSSAAnalysis) {
8378bcb0991SDimitry Andric     auto *MSSA = &MSSAAnalysis->getMSSA();
8388bcb0991SDimitry Andric     MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
8398bcb0991SDimitry Andric   }
8408bcb0991SDimitry Andric 
8410b57cec5SDimitry Andric 
8420b57cec5SDimitry Andric   // Note that we don't preserve LCSSA in the new PM, if you need it run LCSSA
8438bcb0991SDimitry Andric   // after simplifying the loops. MemorySSA is preserved if it exists.
844e8d8bef9SDimitry Andric   for (auto *L : *LI)
8450b57cec5SDimitry Andric     Changed |=
846e8d8bef9SDimitry Andric         simplifyLoop(L, DT, LI, SE, AC, MSSAU.get(), /*PreserveLCSSA*/ false);
8470b57cec5SDimitry Andric 
8480b57cec5SDimitry Andric   if (!Changed)
8490b57cec5SDimitry Andric     return PreservedAnalyses::all();
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric   PreservedAnalyses PA;
8520b57cec5SDimitry Andric   PA.preserve<DominatorTreeAnalysis>();
8530b57cec5SDimitry Andric   PA.preserve<LoopAnalysis>();
8540b57cec5SDimitry Andric   PA.preserve<ScalarEvolutionAnalysis>();
8550b57cec5SDimitry Andric   PA.preserve<DependenceAnalysis>();
8568bcb0991SDimitry Andric   if (MSSAAnalysis)
8578bcb0991SDimitry Andric     PA.preserve<MemorySSAAnalysis>();
8580b57cec5SDimitry Andric   // BPI maps conditional terminators to probabilities, LoopSimplify can insert
8590b57cec5SDimitry Andric   // blocks, but it does so only by splitting existing blocks and edges. This
8600b57cec5SDimitry Andric   // results in the interesting property that all new terminators inserted are
8610b57cec5SDimitry Andric   // unconditional branches which do not appear in BPI. All deletions are
8620b57cec5SDimitry Andric   // handled via ValueHandle callbacks w/in BPI.
8630b57cec5SDimitry Andric   PA.preserve<BranchProbabilityAnalysis>();
8640b57cec5SDimitry Andric   return PA;
8650b57cec5SDimitry Andric }
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric // FIXME: Restore this code when we re-enable verification in verifyAnalysis
8680b57cec5SDimitry Andric // below.
8690b57cec5SDimitry Andric #if 0
8700b57cec5SDimitry Andric static void verifyLoop(Loop *L) {
8710b57cec5SDimitry Andric   // Verify subloops.
8720b57cec5SDimitry Andric   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
8730b57cec5SDimitry Andric     verifyLoop(*I);
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   // It used to be possible to just assert L->isLoopSimplifyForm(), however
8760b57cec5SDimitry Andric   // with the introduction of indirectbr, there are now cases where it's
8770b57cec5SDimitry Andric   // not possible to transform a loop as necessary. We can at least check
8780b57cec5SDimitry Andric   // that there is an indirectbr near any time there's trouble.
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric   // Indirectbr can interfere with preheader and unique backedge insertion.
8810b57cec5SDimitry Andric   if (!L->getLoopPreheader() || !L->getLoopLatch()) {
8820b57cec5SDimitry Andric     bool HasIndBrPred = false;
883fe6060f1SDimitry Andric     for (BasicBlock *Pred : predecessors(L->getHeader()))
884fe6060f1SDimitry Andric       if (isa<IndirectBrInst>(Pred->getTerminator())) {
8850b57cec5SDimitry Andric         HasIndBrPred = true;
8860b57cec5SDimitry Andric         break;
8870b57cec5SDimitry Andric       }
8880b57cec5SDimitry Andric     assert(HasIndBrPred &&
8890b57cec5SDimitry Andric            "LoopSimplify has no excuse for missing loop header info!");
8900b57cec5SDimitry Andric     (void)HasIndBrPred;
8910b57cec5SDimitry Andric   }
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   // Indirectbr can interfere with exit block canonicalization.
8940b57cec5SDimitry Andric   if (!L->hasDedicatedExits()) {
8950b57cec5SDimitry Andric     bool HasIndBrExiting = false;
8960b57cec5SDimitry Andric     SmallVector<BasicBlock*, 8> ExitingBlocks;
8970b57cec5SDimitry Andric     L->getExitingBlocks(ExitingBlocks);
8980b57cec5SDimitry Andric     for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
8990b57cec5SDimitry Andric       if (isa<IndirectBrInst>((ExitingBlocks[i])->getTerminator())) {
9000b57cec5SDimitry Andric         HasIndBrExiting = true;
9010b57cec5SDimitry Andric         break;
9020b57cec5SDimitry Andric       }
9030b57cec5SDimitry Andric     }
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric     assert(HasIndBrExiting &&
9060b57cec5SDimitry Andric            "LoopSimplify has no excuse for missing exit block info!");
9070b57cec5SDimitry Andric     (void)HasIndBrExiting;
9080b57cec5SDimitry Andric   }
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric #endif
9110b57cec5SDimitry Andric 
verifyAnalysis() const9120b57cec5SDimitry Andric void LoopSimplify::verifyAnalysis() const {
9130b57cec5SDimitry Andric   // FIXME: This routine is being called mid-way through the loop pass manager
9140b57cec5SDimitry Andric   // as loop passes destroy this analysis. That's actually fine, but we have no
9150b57cec5SDimitry Andric   // way of expressing that here. Once all of the passes that destroy this are
9160b57cec5SDimitry Andric   // hoisted out of the loop pass manager we can add back verification here.
9170b57cec5SDimitry Andric #if 0
9180b57cec5SDimitry Andric   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
9190b57cec5SDimitry Andric     verifyLoop(*I);
9200b57cec5SDimitry Andric #endif
9210b57cec5SDimitry Andric }
922