xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/LoopPass.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
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 file implements LoopPass and LPPassManager. All loop optimization
100b57cec5SDimitry Andric // and transformation passes are derived from LoopPass. LPPassManager is
110b57cec5SDimitry Andric // responsible for managing LoopPasses.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/Analysis/LoopPass.h"
1681ad6265SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
170b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
180b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
19*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h"
200b57cec5SDimitry Andric #include "llvm/IR/OptBisect.h"
210b57cec5SDimitry Andric #include "llvm/IR/PassTimingInfo.h"
22e8d8bef9SDimitry Andric #include "llvm/IR/PrintPasses.h"
23480093f4SDimitry Andric #include "llvm/InitializePasses.h"
240b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
250b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h"
26480093f4SDimitry Andric #include "llvm/Support/Timer.h"
270b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "loop-pass-manager"
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric namespace {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric /// PrintLoopPass - Print a Function corresponding to a Loop.
350b57cec5SDimitry Andric ///
360b57cec5SDimitry Andric class PrintLoopPassWrapper : public LoopPass {
370b57cec5SDimitry Andric   raw_ostream &OS;
380b57cec5SDimitry Andric   std::string Banner;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric public:
410b57cec5SDimitry Andric   static char ID;
420b57cec5SDimitry Andric   PrintLoopPassWrapper() : LoopPass(ID), OS(dbgs()) {}
430b57cec5SDimitry Andric   PrintLoopPassWrapper(raw_ostream &OS, const std::string &Banner)
440b57cec5SDimitry Andric       : LoopPass(ID), OS(OS), Banner(Banner) {}
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
470b57cec5SDimitry Andric     AU.setPreservesAll();
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   bool runOnLoop(Loop *L, LPPassManager &) override {
510b57cec5SDimitry Andric     auto BBI = llvm::find_if(L->blocks(), [](BasicBlock *BB) { return BB; });
520b57cec5SDimitry Andric     if (BBI != L->blocks().end() &&
530b57cec5SDimitry Andric         isFunctionInPrintList((*BBI)->getParent()->getName())) {
540b57cec5SDimitry Andric       printLoop(*L, OS, Banner);
550b57cec5SDimitry Andric     }
560b57cec5SDimitry Andric     return false;
570b57cec5SDimitry Andric   }
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   StringRef getPassName() const override { return "Print Loop IR"; }
600b57cec5SDimitry Andric };
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric char PrintLoopPassWrapper::ID = 0;
63*0fca6ea1SDimitry Andric } // namespace
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
660b57cec5SDimitry Andric // LPPassManager
670b57cec5SDimitry Andric //
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric char LPPassManager::ID = 0;
700b57cec5SDimitry Andric 
7104eeddc0SDimitry Andric LPPassManager::LPPassManager() : FunctionPass(ID) {
720b57cec5SDimitry Andric   LI = nullptr;
730b57cec5SDimitry Andric   CurrentLoop = nullptr;
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric // Insert loop into loop nest (LoopInfo) and loop queue (LQ).
770b57cec5SDimitry Andric void LPPassManager::addLoop(Loop &L) {
78e8d8bef9SDimitry Andric   if (L.isOutermost()) {
790b57cec5SDimitry Andric     // This is the top level loop.
800b57cec5SDimitry Andric     LQ.push_front(&L);
810b57cec5SDimitry Andric     return;
820b57cec5SDimitry Andric   }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // Insert L into the loop queue after the parent loop.
850b57cec5SDimitry Andric   for (auto I = LQ.begin(), E = LQ.end(); I != E; ++I) {
860b57cec5SDimitry Andric     if (*I == L.getParentLoop()) {
870b57cec5SDimitry Andric       // deque does not support insert after.
880b57cec5SDimitry Andric       ++I;
890b57cec5SDimitry Andric       LQ.insert(I, 1, &L);
900b57cec5SDimitry Andric       return;
910b57cec5SDimitry Andric     }
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric // Recurse through all subloops and all loops  into LQ.
960b57cec5SDimitry Andric static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
970b57cec5SDimitry Andric   LQ.push_back(L);
980b57cec5SDimitry Andric   for (Loop *I : reverse(*L))
990b57cec5SDimitry Andric     addLoopIntoQueue(I, LQ);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric /// Pass Manager itself does not invalidate any analysis info.
1030b57cec5SDimitry Andric void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
1040b57cec5SDimitry Andric   // LPPassManager needs LoopInfo. In the long term LoopInfo class will
1050b57cec5SDimitry Andric   // become part of LPPassManager.
1060b57cec5SDimitry Andric   Info.addRequired<LoopInfoWrapperPass>();
1070b57cec5SDimitry Andric   Info.addRequired<DominatorTreeWrapperPass>();
1080b57cec5SDimitry Andric   Info.setPreservesAll();
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric void LPPassManager::markLoopAsDeleted(Loop &L) {
1120b57cec5SDimitry Andric   assert((&L == CurrentLoop || CurrentLoop->contains(&L)) &&
1130b57cec5SDimitry Andric          "Must not delete loop outside the current loop tree!");
1140b57cec5SDimitry Andric   // If this loop appears elsewhere within the queue, we also need to remove it
1150b57cec5SDimitry Andric   // there. However, we have to be careful to not remove the back of the queue
1160b57cec5SDimitry Andric   // as that is assumed to match the current loop.
1170b57cec5SDimitry Andric   assert(LQ.back() == CurrentLoop && "Loop queue back isn't the current loop!");
1185f757f3fSDimitry Andric   llvm::erase(LQ, &L);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   if (&L == CurrentLoop) {
1210b57cec5SDimitry Andric     CurrentLoopDeleted = true;
1220b57cec5SDimitry Andric     // Add this loop back onto the back of the queue to preserve our invariants.
1230b57cec5SDimitry Andric     LQ.push_back(&L);
1240b57cec5SDimitry Andric   }
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric /// run - Execute all of the passes scheduled for execution.  Keep track of
1280b57cec5SDimitry Andric /// whether any of the passes modifies the function, and if so, return true.
1290b57cec5SDimitry Andric bool LPPassManager::runOnFunction(Function &F) {
1300b57cec5SDimitry Andric   auto &LIWP = getAnalysis<LoopInfoWrapperPass>();
1310b57cec5SDimitry Andric   LI = &LIWP.getLoopInfo();
1320b57cec5SDimitry Andric   Module &M = *F.getParent();
1330b57cec5SDimitry Andric #if 0
1340b57cec5SDimitry Andric   DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1350b57cec5SDimitry Andric #endif
1360b57cec5SDimitry Andric   bool Changed = false;
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   // Collect inherited analysis from Module level pass manager.
1390b57cec5SDimitry Andric   populateInheritedAnalysis(TPM->activeStack);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   // Populate the loop queue in reverse program order. There is no clear need to
1420b57cec5SDimitry Andric   // process sibling loops in either forward or reverse order. There may be some
1430b57cec5SDimitry Andric   // advantage in deleting uses in a later loop before optimizing the
1440b57cec5SDimitry Andric   // definitions in an earlier loop. If we find a clear reason to process in
1450b57cec5SDimitry Andric   // forward order, then a forward variant of LoopPassManager should be created.
1460b57cec5SDimitry Andric   //
1470b57cec5SDimitry Andric   // Note that LoopInfo::iterator visits loops in reverse program
1480b57cec5SDimitry Andric   // order. Here, reverse_iterator gives us a forward order, and the LoopQueue
1490b57cec5SDimitry Andric   // reverses the order a third time by popping from the back.
1500b57cec5SDimitry Andric   for (Loop *L : reverse(*LI))
1510b57cec5SDimitry Andric     addLoopIntoQueue(L, LQ);
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   if (LQ.empty()) // No loops, skip calling finalizers
1540b57cec5SDimitry Andric     return false;
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   // Initialization
1570b57cec5SDimitry Andric   for (Loop *L : LQ) {
1580b57cec5SDimitry Andric     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1590b57cec5SDimitry Andric       LoopPass *P = getContainedPass(Index);
1600b57cec5SDimitry Andric       Changed |= P->doInitialization(L, *this);
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // Walk Loops
1650b57cec5SDimitry Andric   unsigned InstrCount, FunctionSize = 0;
1660b57cec5SDimitry Andric   StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
1670b57cec5SDimitry Andric   bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
1680b57cec5SDimitry Andric   // Collect the initial size of the module and the function we're looking at.
1690b57cec5SDimitry Andric   if (EmitICRemark) {
1700b57cec5SDimitry Andric     InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
1710b57cec5SDimitry Andric     FunctionSize = F.getInstructionCount();
1720b57cec5SDimitry Andric   }
1730b57cec5SDimitry Andric   while (!LQ.empty()) {
1740b57cec5SDimitry Andric     CurrentLoopDeleted = false;
1750b57cec5SDimitry Andric     CurrentLoop = LQ.back();
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric     // Run all passes on the current Loop.
1780b57cec5SDimitry Andric     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1790b57cec5SDimitry Andric       LoopPass *P = getContainedPass(Index);
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric       llvm::TimeTraceScope LoopPassScope("RunLoopPass", P->getPassName());
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric       dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
1840b57cec5SDimitry Andric                    CurrentLoop->getHeader()->getName());
1850b57cec5SDimitry Andric       dumpRequiredSet(P);
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric       initializeAnalysisImpl(P);
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric       bool LocalChanged = false;
1900b57cec5SDimitry Andric       {
1910b57cec5SDimitry Andric         PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader());
1920b57cec5SDimitry Andric         TimeRegion PassTimer(getPassTimer(P));
193e8d8bef9SDimitry Andric #ifdef EXPENSIVE_CHECKS
19481ad6265SDimitry Andric         uint64_t RefHash = P->structuralHash(F);
195e8d8bef9SDimitry Andric #endif
1960b57cec5SDimitry Andric         LocalChanged = P->runOnLoop(CurrentLoop, *this);
197e8d8bef9SDimitry Andric 
198e8d8bef9SDimitry Andric #ifdef EXPENSIVE_CHECKS
19981ad6265SDimitry Andric         if (!LocalChanged && (RefHash != P->structuralHash(F))) {
200e8d8bef9SDimitry Andric           llvm::errs() << "Pass modifies its input and doesn't report it: "
201e8d8bef9SDimitry Andric                        << P->getPassName() << "\n";
202e8d8bef9SDimitry Andric           llvm_unreachable("Pass modifies its input and doesn't report it");
203e8d8bef9SDimitry Andric         }
204e8d8bef9SDimitry Andric #endif
205e8d8bef9SDimitry Andric 
2060b57cec5SDimitry Andric         Changed |= LocalChanged;
2070b57cec5SDimitry Andric         if (EmitICRemark) {
2080b57cec5SDimitry Andric           unsigned NewSize = F.getInstructionCount();
2090b57cec5SDimitry Andric           // Update the size of the function, emit a remark, and update the
2100b57cec5SDimitry Andric           // size of the module.
2110b57cec5SDimitry Andric           if (NewSize != FunctionSize) {
2120b57cec5SDimitry Andric             int64_t Delta = static_cast<int64_t>(NewSize) -
2130b57cec5SDimitry Andric                             static_cast<int64_t>(FunctionSize);
2140b57cec5SDimitry Andric             emitInstrCountChangedRemark(P, M, Delta, InstrCount,
2150b57cec5SDimitry Andric                                         FunctionToInstrCount, &F);
2160b57cec5SDimitry Andric             InstrCount = static_cast<int64_t>(InstrCount) + Delta;
2170b57cec5SDimitry Andric             FunctionSize = NewSize;
2180b57cec5SDimitry Andric           }
2190b57cec5SDimitry Andric         }
2200b57cec5SDimitry Andric       }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric       if (LocalChanged)
2230b57cec5SDimitry Andric         dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
2240b57cec5SDimitry Andric                      CurrentLoopDeleted ? "<deleted loop>"
2250b57cec5SDimitry Andric                                         : CurrentLoop->getName());
2260b57cec5SDimitry Andric       dumpPreservedSet(P);
2270b57cec5SDimitry Andric 
2285ffd83dbSDimitry Andric       if (!CurrentLoopDeleted) {
2290b57cec5SDimitry Andric         // Manually check that this loop is still healthy. This is done
2300b57cec5SDimitry Andric         // instead of relying on LoopInfo::verifyLoop since LoopInfo
2310b57cec5SDimitry Andric         // is a function pass and it's really expensive to verify every
2320b57cec5SDimitry Andric         // loop in the function every time. That level of checking can be
2330b57cec5SDimitry Andric         // enabled with the -verify-loop-info option.
2340b57cec5SDimitry Andric         {
2350b57cec5SDimitry Andric           TimeRegion PassTimer(getPassTimer(&LIWP));
2360b57cec5SDimitry Andric           CurrentLoop->verifyLoop();
2370b57cec5SDimitry Andric         }
2380b57cec5SDimitry Andric         // Here we apply same reasoning as in the above case. Only difference
2390b57cec5SDimitry Andric         // is that LPPassManager might run passes which do not require LCSSA
2400b57cec5SDimitry Andric         // form (LoopPassPrinter for example). We should skip verification for
2410b57cec5SDimitry Andric         // such passes.
2420b57cec5SDimitry Andric         // FIXME: Loop-sink currently break LCSSA. Fix it and reenable the
2430b57cec5SDimitry Andric         // verification!
2440b57cec5SDimitry Andric #if 0
2450b57cec5SDimitry Andric         if (mustPreserveAnalysisID(LCSSAVerificationPass::ID))
2460b57cec5SDimitry Andric           assert(CurrentLoop->isRecursivelyLCSSAForm(*DT, *LI));
2470b57cec5SDimitry Andric #endif
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric         // Then call the regular verifyAnalysis functions.
2500b57cec5SDimitry Andric         verifyPreservedAnalysis(P);
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric         F.getContext().yield();
2530b57cec5SDimitry Andric       }
2540b57cec5SDimitry Andric 
255e8d8bef9SDimitry Andric       if (LocalChanged)
2560b57cec5SDimitry Andric         removeNotPreservedAnalysis(P);
2570b57cec5SDimitry Andric       recordAvailableAnalysis(P);
2580b57cec5SDimitry Andric       removeDeadPasses(P,
2590b57cec5SDimitry Andric                        CurrentLoopDeleted ? "<deleted>"
2600b57cec5SDimitry Andric                                           : CurrentLoop->getHeader()->getName(),
2610b57cec5SDimitry Andric                        ON_LOOP_MSG);
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric       if (CurrentLoopDeleted)
2640b57cec5SDimitry Andric         // Do not run other passes on this loop.
2650b57cec5SDimitry Andric         break;
2660b57cec5SDimitry Andric     }
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric     // If the loop was deleted, release all the loop passes. This frees up
2690b57cec5SDimitry Andric     // some memory, and avoids trouble with the pass manager trying to call
2700b57cec5SDimitry Andric     // verifyAnalysis on them.
2710b57cec5SDimitry Andric     if (CurrentLoopDeleted) {
2720b57cec5SDimitry Andric       for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
2730b57cec5SDimitry Andric         Pass *P = getContainedPass(Index);
2740b57cec5SDimitry Andric         freePass(P, "<deleted>", ON_LOOP_MSG);
2750b57cec5SDimitry Andric       }
2760b57cec5SDimitry Andric     }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric     // Pop the loop from queue after running all passes.
2790b57cec5SDimitry Andric     LQ.pop_back();
2800b57cec5SDimitry Andric   }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric   // Finalization
2830b57cec5SDimitry Andric   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
2840b57cec5SDimitry Andric     LoopPass *P = getContainedPass(Index);
2850b57cec5SDimitry Andric     Changed |= P->doFinalization();
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   return Changed;
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric /// Print passes managed by this manager
2920b57cec5SDimitry Andric void LPPassManager::dumpPassStructure(unsigned Offset) {
2930b57cec5SDimitry Andric   errs().indent(Offset*2) << "Loop Pass Manager\n";
2940b57cec5SDimitry Andric   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
2950b57cec5SDimitry Andric     Pass *P = getContainedPass(Index);
2960b57cec5SDimitry Andric     P->dumpPassStructure(Offset + 1);
2970b57cec5SDimitry Andric     dumpLastUses(P, Offset+1);
2980b57cec5SDimitry Andric   }
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric 
3020b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
3030b57cec5SDimitry Andric // LoopPass
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric Pass *LoopPass::createPrinterPass(raw_ostream &O,
3060b57cec5SDimitry Andric                                   const std::string &Banner) const {
3070b57cec5SDimitry Andric   return new PrintLoopPassWrapper(O, Banner);
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric // Check if this pass is suitable for the current LPPassManager, if
3110b57cec5SDimitry Andric // available. This pass P is not suitable for a LPPassManager if P
3120b57cec5SDimitry Andric // is not preserving higher level analysis info used by other
3130b57cec5SDimitry Andric // LPPassManager passes. In such case, pop LPPassManager from the
3140b57cec5SDimitry Andric // stack. This will force assignPassManager() to create new
3150b57cec5SDimitry Andric // LPPassManger as expected.
3160b57cec5SDimitry Andric void LoopPass::preparePassManager(PMStack &PMS) {
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // Find LPPassManager
3190b57cec5SDimitry Andric   while (!PMS.empty() &&
3200b57cec5SDimitry Andric          PMS.top()->getPassManagerType() > PMT_LoopPassManager)
3210b57cec5SDimitry Andric     PMS.pop();
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // If this pass is destroying high level information that is used
3240b57cec5SDimitry Andric   // by other passes that are managed by LPM then do not insert
3250b57cec5SDimitry Andric   // this pass in current LPM. Use new LPPassManager.
3260b57cec5SDimitry Andric   if (PMS.top()->getPassManagerType() == PMT_LoopPassManager &&
3270b57cec5SDimitry Andric       !PMS.top()->preserveHigherLevelAnalysis(this))
3280b57cec5SDimitry Andric     PMS.pop();
3290b57cec5SDimitry Andric }
3300b57cec5SDimitry Andric 
3310b57cec5SDimitry Andric /// Assign pass manager to manage this pass.
3320b57cec5SDimitry Andric void LoopPass::assignPassManager(PMStack &PMS,
3330b57cec5SDimitry Andric                                  PassManagerType PreferredType) {
3340b57cec5SDimitry Andric   // Find LPPassManager
3350b57cec5SDimitry Andric   while (!PMS.empty() &&
3360b57cec5SDimitry Andric          PMS.top()->getPassManagerType() > PMT_LoopPassManager)
3370b57cec5SDimitry Andric     PMS.pop();
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric   LPPassManager *LPPM;
3400b57cec5SDimitry Andric   if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
3410b57cec5SDimitry Andric     LPPM = (LPPassManager*)PMS.top();
3420b57cec5SDimitry Andric   else {
3430b57cec5SDimitry Andric     // Create new Loop Pass Manager if it does not exist.
3440b57cec5SDimitry Andric     assert (!PMS.empty() && "Unable to create Loop Pass Manager");
3450b57cec5SDimitry Andric     PMDataManager *PMD = PMS.top();
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric     // [1] Create new Loop Pass Manager
3480b57cec5SDimitry Andric     LPPM = new LPPassManager();
3490b57cec5SDimitry Andric     LPPM->populateInheritedAnalysis(PMS);
3500b57cec5SDimitry Andric 
3510b57cec5SDimitry Andric     // [2] Set up new manager's top level manager
3520b57cec5SDimitry Andric     PMTopLevelManager *TPM = PMD->getTopLevelManager();
3530b57cec5SDimitry Andric     TPM->addIndirectPassManager(LPPM);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     // [3] Assign manager to manage this new manager. This may create
3560b57cec5SDimitry Andric     // and push new managers into PMS
3570b57cec5SDimitry Andric     Pass *P = LPPM->getAsPass();
3580b57cec5SDimitry Andric     TPM->schedulePass(P);
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric     // [4] Push new manager into PMS
3610b57cec5SDimitry Andric     PMS.push(LPPM);
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   LPPM->add(this);
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric static std::string getDescription(const Loop &L) {
3680b57cec5SDimitry Andric   return "loop";
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric bool LoopPass::skipLoop(const Loop *L) const {
3720b57cec5SDimitry Andric   const Function *F = L->getHeader()->getParent();
3730b57cec5SDimitry Andric   if (!F)
3740b57cec5SDimitry Andric     return false;
3750b57cec5SDimitry Andric   // Check the opt bisect limit.
3760b57cec5SDimitry Andric   OptPassGate &Gate = F->getContext().getOptPassGate();
377bdd1243dSDimitry Andric   if (Gate.isEnabled() &&
378bdd1243dSDimitry Andric       !Gate.shouldRunPass(this->getPassName(), getDescription(*L)))
3790b57cec5SDimitry Andric     return true;
3800b57cec5SDimitry Andric   // Check for the OptimizeNone attribute.
3810b57cec5SDimitry Andric   if (F->hasOptNone()) {
3820b57cec5SDimitry Andric     // FIXME: Report this to dbgs() only once per function.
3830b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' in function "
3840b57cec5SDimitry Andric                       << F->getName() << "\n");
3850b57cec5SDimitry Andric     // FIXME: Delete loop from pass manager's queue?
3860b57cec5SDimitry Andric     return true;
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric   return false;
3890b57cec5SDimitry Andric }
3900b57cec5SDimitry Andric 
391480093f4SDimitry Andric LCSSAVerificationPass::LCSSAVerificationPass() : FunctionPass(ID) {
392480093f4SDimitry Andric   initializeLCSSAVerificationPassPass(*PassRegistry::getPassRegistry());
393480093f4SDimitry Andric }
394480093f4SDimitry Andric 
3950b57cec5SDimitry Andric char LCSSAVerificationPass::ID = 0;
3960b57cec5SDimitry Andric INITIALIZE_PASS(LCSSAVerificationPass, "lcssa-verification", "LCSSA Verifier",
3970b57cec5SDimitry Andric                 false, false)
398