1f22ef01cSRoman Divacky //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements LoopPass and LPPassManager. All loop optimization
11f22ef01cSRoman Divacky // and transformation passes are derived from LoopPass. LPPassManager is
12f22ef01cSRoman Divacky // responsible for managing LoopPasses.
13f22ef01cSRoman Divacky //
14f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
15f22ef01cSRoman Divacky
16f22ef01cSRoman Divacky #include "llvm/Analysis/LoopPass.h"
17f1a29dd3SDimitry Andric #include "llvm/Analysis/LoopAnalysisManager.h"
18d88c1a5aSDimitry Andric #include "llvm/IR/Dominators.h"
1991bc56edSDimitry Andric #include "llvm/IR/IRPrintingPasses.h"
2091bc56edSDimitry Andric #include "llvm/IR/LLVMContext.h"
213ca95b02SDimitry Andric #include "llvm/IR/OptBisect.h"
227d523365SDimitry Andric #include "llvm/IR/PassManager.h"
23*b5893f02SDimitry Andric #include "llvm/IR/PassTimingInfo.h"
24f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
25f22ef01cSRoman Divacky #include "llvm/Support/Timer.h"
26ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
27f22ef01cSRoman Divacky using namespace llvm;
28f22ef01cSRoman Divacky
2991bc56edSDimitry Andric #define DEBUG_TYPE "loop-pass-manager"
3091bc56edSDimitry Andric
31f22ef01cSRoman Divacky namespace {
32f22ef01cSRoman Divacky
33f22ef01cSRoman Divacky /// PrintLoopPass - Print a Function corresponding to a Loop.
34f22ef01cSRoman Divacky ///
357d523365SDimitry Andric class PrintLoopPassWrapper : public LoopPass {
36f1a29dd3SDimitry Andric raw_ostream &OS;
37f1a29dd3SDimitry Andric std::string Banner;
38f22ef01cSRoman Divacky
39f22ef01cSRoman Divacky public:
40f22ef01cSRoman Divacky static char ID;
PrintLoopPassWrapper()41f1a29dd3SDimitry Andric PrintLoopPassWrapper() : LoopPass(ID), OS(dbgs()) {}
PrintLoopPassWrapper(raw_ostream & OS,const std::string & Banner)427d523365SDimitry Andric PrintLoopPassWrapper(raw_ostream &OS, const std::string &Banner)
43f1a29dd3SDimitry Andric : LoopPass(ID), OS(OS), Banner(Banner) {}
44f22ef01cSRoman Divacky
getAnalysisUsage(AnalysisUsage & AU) const4591bc56edSDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
46f22ef01cSRoman Divacky AU.setPreservesAll();
47f22ef01cSRoman Divacky }
48f22ef01cSRoman Divacky
runOnLoop(Loop * L,LPPassManager &)4991bc56edSDimitry Andric bool runOnLoop(Loop *L, LPPassManager &) override {
502cab237bSDimitry Andric auto BBI = llvm::find_if(L->blocks(), [](BasicBlock *BB) { return BB; });
51444ed5c5SDimitry Andric if (BBI != L->blocks().end() &&
523ca95b02SDimitry Andric isFunctionInPrintList((*BBI)->getParent()->getName())) {
53f1a29dd3SDimitry Andric printLoop(*L, OS, Banner);
543ca95b02SDimitry Andric }
55f22ef01cSRoman Divacky return false;
56f22ef01cSRoman Divacky }
577a7e6055SDimitry Andric
getPassName() const587a7e6055SDimitry Andric StringRef getPassName() const override { return "Print Loop IR"; }
59f22ef01cSRoman Divacky };
60f22ef01cSRoman Divacky
617d523365SDimitry Andric char PrintLoopPassWrapper::ID = 0;
623dac3a9bSDimitry Andric }
63f22ef01cSRoman Divacky
64f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
65f22ef01cSRoman Divacky // LPPassManager
66f22ef01cSRoman Divacky //
67f22ef01cSRoman Divacky
68f22ef01cSRoman Divacky char LPPassManager::ID = 0;
69f22ef01cSRoman Divacky
LPPassManager()706122f3e6SDimitry Andric LPPassManager::LPPassManager()
716122f3e6SDimitry Andric : FunctionPass(ID), PMDataManager() {
7291bc56edSDimitry Andric LI = nullptr;
7391bc56edSDimitry Andric CurrentLoop = nullptr;
74f22ef01cSRoman Divacky }
75f22ef01cSRoman Divacky
76302affcbSDimitry Andric // Insert loop into loop nest (LoopInfo) and loop queue (LQ).
addLoop(Loop & L)77302affcbSDimitry Andric void LPPassManager::addLoop(Loop &L) {
78302affcbSDimitry Andric if (!L.getParentLoop()) {
797d523365SDimitry Andric // This is the top level loop.
80302affcbSDimitry Andric LQ.push_front(&L);
81302affcbSDimitry Andric return;
82f22ef01cSRoman Divacky }
83f22ef01cSRoman Divacky
847d523365SDimitry Andric // Insert L into the loop queue after the parent loop.
857d523365SDimitry Andric for (auto I = LQ.begin(), E = LQ.end(); I != E; ++I) {
86302affcbSDimitry Andric if (*I == L.getParentLoop()) {
87f22ef01cSRoman Divacky // deque does not support insert after.
88f22ef01cSRoman Divacky ++I;
89302affcbSDimitry Andric LQ.insert(I, 1, &L);
90302affcbSDimitry Andric return;
91f22ef01cSRoman Divacky }
92f22ef01cSRoman Divacky }
93f22ef01cSRoman Divacky }
94f22ef01cSRoman Divacky
95f22ef01cSRoman Divacky /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
96f22ef01cSRoman Divacky /// all loop passes.
cloneBasicBlockSimpleAnalysis(BasicBlock * From,BasicBlock * To,Loop * L)97f22ef01cSRoman Divacky void LPPassManager::cloneBasicBlockSimpleAnalysis(BasicBlock *From,
98f22ef01cSRoman Divacky BasicBlock *To, Loop *L) {
99f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
100e580952dSDimitry Andric LoopPass *LP = getContainedPass(Index);
101f22ef01cSRoman Divacky LP->cloneBasicBlockAnalysis(From, To, L);
102f22ef01cSRoman Divacky }
103f22ef01cSRoman Divacky }
104f22ef01cSRoman Divacky
105f22ef01cSRoman Divacky /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes.
deleteSimpleAnalysisValue(Value * V,Loop * L)106f22ef01cSRoman Divacky void LPPassManager::deleteSimpleAnalysisValue(Value *V, Loop *L) {
107f22ef01cSRoman Divacky if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
1083ca95b02SDimitry Andric for (Instruction &I : *BB) {
109f22ef01cSRoman Divacky deleteSimpleAnalysisValue(&I, L);
110f22ef01cSRoman Divacky }
111f22ef01cSRoman Divacky }
112f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
113e580952dSDimitry Andric LoopPass *LP = getContainedPass(Index);
114f22ef01cSRoman Divacky LP->deleteAnalysisValue(V, L);
115f22ef01cSRoman Divacky }
116f22ef01cSRoman Divacky }
117f22ef01cSRoman Divacky
11839d628a0SDimitry Andric /// Invoke deleteAnalysisLoop hook for all passes.
deleteSimpleAnalysisLoop(Loop * L)11939d628a0SDimitry Andric void LPPassManager::deleteSimpleAnalysisLoop(Loop *L) {
12039d628a0SDimitry Andric for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
12139d628a0SDimitry Andric LoopPass *LP = getContainedPass(Index);
12239d628a0SDimitry Andric LP->deleteAnalysisLoop(L);
12339d628a0SDimitry Andric }
12439d628a0SDimitry Andric }
12539d628a0SDimitry Andric
126f22ef01cSRoman Divacky
127f22ef01cSRoman Divacky // Recurse through all subloops and all loops into LQ.
addLoopIntoQueue(Loop * L,std::deque<Loop * > & LQ)128f22ef01cSRoman Divacky static void addLoopIntoQueue(Loop *L, std::deque<Loop *> &LQ) {
129f22ef01cSRoman Divacky LQ.push_back(L);
130d88c1a5aSDimitry Andric for (Loop *I : reverse(*L))
131d88c1a5aSDimitry Andric addLoopIntoQueue(I, LQ);
132f22ef01cSRoman Divacky }
133f22ef01cSRoman Divacky
134f22ef01cSRoman Divacky /// Pass Manager itself does not invalidate any analysis info.
getAnalysisUsage(AnalysisUsage & Info) const135f22ef01cSRoman Divacky void LPPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
136f22ef01cSRoman Divacky // LPPassManager needs LoopInfo. In the long term LoopInfo class will
137f22ef01cSRoman Divacky // become part of LPPassManager.
138ff0cc061SDimitry Andric Info.addRequired<LoopInfoWrapperPass>();
139d88c1a5aSDimitry Andric Info.addRequired<DominatorTreeWrapperPass>();
140f22ef01cSRoman Divacky Info.setPreservesAll();
141f22ef01cSRoman Divacky }
142f22ef01cSRoman Divacky
markLoopAsDeleted(Loop & L)1432cab237bSDimitry Andric void LPPassManager::markLoopAsDeleted(Loop &L) {
1442cab237bSDimitry Andric assert((&L == CurrentLoop || CurrentLoop->contains(&L)) &&
1452cab237bSDimitry Andric "Must not delete loop outside the current loop tree!");
1464ba319b5SDimitry Andric // If this loop appears elsewhere within the queue, we also need to remove it
1474ba319b5SDimitry Andric // there. However, we have to be careful to not remove the back of the queue
1484ba319b5SDimitry Andric // as that is assumed to match the current loop.
1494ba319b5SDimitry Andric assert(LQ.back() == CurrentLoop && "Loop queue back isn't the current loop!");
1504ba319b5SDimitry Andric LQ.erase(std::remove(LQ.begin(), LQ.end(), &L), LQ.end());
1514ba319b5SDimitry Andric
1524ba319b5SDimitry Andric if (&L == CurrentLoop) {
1532cab237bSDimitry Andric CurrentLoopDeleted = true;
1544ba319b5SDimitry Andric // Add this loop back onto the back of the queue to preserve our invariants.
1554ba319b5SDimitry Andric LQ.push_back(&L);
1564ba319b5SDimitry Andric }
1572cab237bSDimitry Andric }
1582cab237bSDimitry Andric
159f22ef01cSRoman Divacky /// run - Execute all of the passes scheduled for execution. Keep track of
160f22ef01cSRoman Divacky /// whether any of the passes modifies the function, and if so, return true.
runOnFunction(Function & F)161f22ef01cSRoman Divacky bool LPPassManager::runOnFunction(Function &F) {
162ff0cc061SDimitry Andric auto &LIWP = getAnalysis<LoopInfoWrapperPass>();
163ff0cc061SDimitry Andric LI = &LIWP.getLoopInfo();
1644ba319b5SDimitry Andric Module &M = *F.getParent();
1654ba319b5SDimitry Andric #if 0
166d88c1a5aSDimitry Andric DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1674ba319b5SDimitry Andric #endif
168f22ef01cSRoman Divacky bool Changed = false;
169f22ef01cSRoman Divacky
170f22ef01cSRoman Divacky // Collect inherited analysis from Module level pass manager.
171f22ef01cSRoman Divacky populateInheritedAnalysis(TPM->activeStack);
172f22ef01cSRoman Divacky
1737ae0e2c9SDimitry Andric // Populate the loop queue in reverse program order. There is no clear need to
1747ae0e2c9SDimitry Andric // process sibling loops in either forward or reverse order. There may be some
1757ae0e2c9SDimitry Andric // advantage in deleting uses in a later loop before optimizing the
1767ae0e2c9SDimitry Andric // definitions in an earlier loop. If we find a clear reason to process in
1777ae0e2c9SDimitry Andric // forward order, then a forward variant of LoopPassManager should be created.
178f785676fSDimitry Andric //
179f785676fSDimitry Andric // Note that LoopInfo::iterator visits loops in reverse program
180f785676fSDimitry Andric // order. Here, reverse_iterator gives us a forward order, and the LoopQueue
181f785676fSDimitry Andric // reverses the order a third time by popping from the back.
182d88c1a5aSDimitry Andric for (Loop *L : reverse(*LI))
183d88c1a5aSDimitry Andric addLoopIntoQueue(L, LQ);
184f22ef01cSRoman Divacky
185f22ef01cSRoman Divacky if (LQ.empty()) // No loops, skip calling finalizers
186f22ef01cSRoman Divacky return false;
187f22ef01cSRoman Divacky
188f22ef01cSRoman Divacky // Initialization
189d88c1a5aSDimitry Andric for (Loop *L : LQ) {
190f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
191e580952dSDimitry Andric LoopPass *P = getContainedPass(Index);
192f22ef01cSRoman Divacky Changed |= P->doInitialization(L, *this);
193f22ef01cSRoman Divacky }
194f22ef01cSRoman Divacky }
195f22ef01cSRoman Divacky
196f22ef01cSRoman Divacky // Walk Loops
197*b5893f02SDimitry Andric unsigned InstrCount, FunctionSize = 0;
198*b5893f02SDimitry Andric StringMap<std::pair<unsigned, unsigned>> FunctionToInstrCount;
1994ba319b5SDimitry Andric bool EmitICRemark = M.shouldEmitInstrCountChangedRemark();
200*b5893f02SDimitry Andric // Collect the initial size of the module and the function we're looking at.
201*b5893f02SDimitry Andric if (EmitICRemark) {
202*b5893f02SDimitry Andric InstrCount = initSizeRemarkInfo(M, FunctionToInstrCount);
203*b5893f02SDimitry Andric FunctionSize = F.getInstructionCount();
204*b5893f02SDimitry Andric }
205f22ef01cSRoman Divacky while (!LQ.empty()) {
2062cab237bSDimitry Andric CurrentLoopDeleted = false;
207f22ef01cSRoman Divacky CurrentLoop = LQ.back();
208444ed5c5SDimitry Andric
209f22ef01cSRoman Divacky // Run all passes on the current Loop.
210f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
211e580952dSDimitry Andric LoopPass *P = getContainedPass(Index);
212dff0c46cSDimitry Andric
213f22ef01cSRoman Divacky dumpPassInfo(P, EXECUTION_MSG, ON_LOOP_MSG,
214f22ef01cSRoman Divacky CurrentLoop->getHeader()->getName());
215f22ef01cSRoman Divacky dumpRequiredSet(P);
216f22ef01cSRoman Divacky
217f22ef01cSRoman Divacky initializeAnalysisImpl(P);
218dff0c46cSDimitry Andric
219*b5893f02SDimitry Andric bool LocalChanged = false;
220f22ef01cSRoman Divacky {
221f22ef01cSRoman Divacky PassManagerPrettyStackEntry X(P, *CurrentLoop->getHeader());
222f22ef01cSRoman Divacky TimeRegion PassTimer(getPassTimer(P));
223*b5893f02SDimitry Andric LocalChanged = P->runOnLoop(CurrentLoop, *this);
224*b5893f02SDimitry Andric Changed |= LocalChanged;
225*b5893f02SDimitry Andric if (EmitICRemark) {
226*b5893f02SDimitry Andric unsigned NewSize = F.getInstructionCount();
227*b5893f02SDimitry Andric // Update the size of the function, emit a remark, and update the
228*b5893f02SDimitry Andric // size of the module.
229*b5893f02SDimitry Andric if (NewSize != FunctionSize) {
230*b5893f02SDimitry Andric int64_t Delta = static_cast<int64_t>(NewSize) -
231*b5893f02SDimitry Andric static_cast<int64_t>(FunctionSize);
232*b5893f02SDimitry Andric emitInstrCountChangedRemark(P, M, Delta, InstrCount,
233*b5893f02SDimitry Andric FunctionToInstrCount, &F);
234*b5893f02SDimitry Andric InstrCount = static_cast<int64_t>(InstrCount) + Delta;
235*b5893f02SDimitry Andric FunctionSize = NewSize;
236*b5893f02SDimitry Andric }
237*b5893f02SDimitry Andric }
238f22ef01cSRoman Divacky }
239f22ef01cSRoman Divacky
240*b5893f02SDimitry Andric if (LocalChanged)
241f22ef01cSRoman Divacky dumpPassInfo(P, MODIFICATION_MSG, ON_LOOP_MSG,
2422cab237bSDimitry Andric CurrentLoopDeleted ? "<deleted loop>"
2432cab237bSDimitry Andric : CurrentLoop->getName());
244f22ef01cSRoman Divacky dumpPreservedSet(P);
245f22ef01cSRoman Divacky
2462cab237bSDimitry Andric if (CurrentLoopDeleted) {
2477d523365SDimitry Andric // Notify passes that the loop is being deleted.
2487d523365SDimitry Andric deleteSimpleAnalysisLoop(CurrentLoop);
2497d523365SDimitry Andric } else {
250f22ef01cSRoman Divacky // Manually check that this loop is still healthy. This is done
251f22ef01cSRoman Divacky // instead of relying on LoopInfo::verifyLoop since LoopInfo
252f22ef01cSRoman Divacky // is a function pass and it's really expensive to verify every
253f22ef01cSRoman Divacky // loop in the function every time. That level of checking can be
254f22ef01cSRoman Divacky // enabled with the -verify-loop-info option.
255f22ef01cSRoman Divacky {
256ff0cc061SDimitry Andric TimeRegion PassTimer(getPassTimer(&LIWP));
257f22ef01cSRoman Divacky CurrentLoop->verifyLoop();
258f22ef01cSRoman Divacky }
259d88c1a5aSDimitry Andric // Here we apply same reasoning as in the above case. Only difference
260d88c1a5aSDimitry Andric // is that LPPassManager might run passes which do not require LCSSA
261d88c1a5aSDimitry Andric // form (LoopPassPrinter for example). We should skip verification for
262d88c1a5aSDimitry Andric // such passes.
2634ba319b5SDimitry Andric // FIXME: Loop-sink currently break LCSSA. Fix it and reenable the
2644ba319b5SDimitry Andric // verification!
2654ba319b5SDimitry Andric #if 0
266d88c1a5aSDimitry Andric if (mustPreserveAnalysisID(LCSSAVerificationPass::ID))
2674ba319b5SDimitry Andric assert(CurrentLoop->isRecursivelyLCSSAForm(*DT, *LI));
2684ba319b5SDimitry Andric #endif
269f22ef01cSRoman Divacky
270f22ef01cSRoman Divacky // Then call the regular verifyAnalysis functions.
271f22ef01cSRoman Divacky verifyPreservedAnalysis(P);
27291bc56edSDimitry Andric
27391bc56edSDimitry Andric F.getContext().yield();
274f22ef01cSRoman Divacky }
275f22ef01cSRoman Divacky
276f22ef01cSRoman Divacky removeNotPreservedAnalysis(P);
277f22ef01cSRoman Divacky recordAvailableAnalysis(P);
2782cab237bSDimitry Andric removeDeadPasses(P,
2792cab237bSDimitry Andric CurrentLoopDeleted ? "<deleted>"
2807d523365SDimitry Andric : CurrentLoop->getHeader()->getName(),
281f22ef01cSRoman Divacky ON_LOOP_MSG);
282f22ef01cSRoman Divacky
2832cab237bSDimitry Andric if (CurrentLoopDeleted)
284f22ef01cSRoman Divacky // Do not run other passes on this loop.
285f22ef01cSRoman Divacky break;
286f22ef01cSRoman Divacky }
287f22ef01cSRoman Divacky
288f22ef01cSRoman Divacky // If the loop was deleted, release all the loop passes. This frees up
289f22ef01cSRoman Divacky // some memory, and avoids trouble with the pass manager trying to call
290f22ef01cSRoman Divacky // verifyAnalysis on them.
2912cab237bSDimitry Andric if (CurrentLoopDeleted) {
292f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
293f22ef01cSRoman Divacky Pass *P = getContainedPass(Index);
294f22ef01cSRoman Divacky freePass(P, "<deleted>", ON_LOOP_MSG);
295f22ef01cSRoman Divacky }
2967d523365SDimitry Andric }
297f22ef01cSRoman Divacky
298f22ef01cSRoman Divacky // Pop the loop from queue after running all passes.
299f22ef01cSRoman Divacky LQ.pop_back();
300f22ef01cSRoman Divacky }
301f22ef01cSRoman Divacky
302f22ef01cSRoman Divacky // Finalization
303f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
304e580952dSDimitry Andric LoopPass *P = getContainedPass(Index);
305f22ef01cSRoman Divacky Changed |= P->doFinalization();
306f22ef01cSRoman Divacky }
307f22ef01cSRoman Divacky
308f22ef01cSRoman Divacky return Changed;
309f22ef01cSRoman Divacky }
310f22ef01cSRoman Divacky
311f22ef01cSRoman Divacky /// Print passes managed by this manager
dumpPassStructure(unsigned Offset)312f22ef01cSRoman Divacky void LPPassManager::dumpPassStructure(unsigned Offset) {
313f22ef01cSRoman Divacky errs().indent(Offset*2) << "Loop Pass Manager\n";
314f22ef01cSRoman Divacky for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
315f22ef01cSRoman Divacky Pass *P = getContainedPass(Index);
316f22ef01cSRoman Divacky P->dumpPassStructure(Offset + 1);
317f22ef01cSRoman Divacky dumpLastUses(P, Offset+1);
318f22ef01cSRoman Divacky }
319f22ef01cSRoman Divacky }
320f22ef01cSRoman Divacky
321f22ef01cSRoman Divacky
322f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
323f22ef01cSRoman Divacky // LoopPass
324f22ef01cSRoman Divacky
createPrinterPass(raw_ostream & O,const std::string & Banner) const325f22ef01cSRoman Divacky Pass *LoopPass::createPrinterPass(raw_ostream &O,
326f22ef01cSRoman Divacky const std::string &Banner) const {
3277d523365SDimitry Andric return new PrintLoopPassWrapper(O, Banner);
328f22ef01cSRoman Divacky }
329f22ef01cSRoman Divacky
330f22ef01cSRoman Divacky // Check if this pass is suitable for the current LPPassManager, if
331f22ef01cSRoman Divacky // available. This pass P is not suitable for a LPPassManager if P
332f22ef01cSRoman Divacky // is not preserving higher level analysis info used by other
333f22ef01cSRoman Divacky // LPPassManager passes. In such case, pop LPPassManager from the
334f22ef01cSRoman Divacky // stack. This will force assignPassManager() to create new
335f22ef01cSRoman Divacky // LPPassManger as expected.
preparePassManager(PMStack & PMS)336f22ef01cSRoman Divacky void LoopPass::preparePassManager(PMStack &PMS) {
337f22ef01cSRoman Divacky
338f22ef01cSRoman Divacky // Find LPPassManager
339f22ef01cSRoman Divacky while (!PMS.empty() &&
340f22ef01cSRoman Divacky PMS.top()->getPassManagerType() > PMT_LoopPassManager)
341f22ef01cSRoman Divacky PMS.pop();
342f22ef01cSRoman Divacky
343f22ef01cSRoman Divacky // If this pass is destroying high level information that is used
344f22ef01cSRoman Divacky // by other passes that are managed by LPM then do not insert
345f22ef01cSRoman Divacky // this pass in current LPM. Use new LPPassManager.
346f22ef01cSRoman Divacky if (PMS.top()->getPassManagerType() == PMT_LoopPassManager &&
347f22ef01cSRoman Divacky !PMS.top()->preserveHigherLevelAnalysis(this))
348f22ef01cSRoman Divacky PMS.pop();
349f22ef01cSRoman Divacky }
350f22ef01cSRoman Divacky
351f22ef01cSRoman Divacky /// Assign pass manager to manage this pass.
assignPassManager(PMStack & PMS,PassManagerType PreferredType)352f22ef01cSRoman Divacky void LoopPass::assignPassManager(PMStack &PMS,
353f22ef01cSRoman Divacky PassManagerType PreferredType) {
354f22ef01cSRoman Divacky // Find LPPassManager
355f22ef01cSRoman Divacky while (!PMS.empty() &&
356f22ef01cSRoman Divacky PMS.top()->getPassManagerType() > PMT_LoopPassManager)
357f22ef01cSRoman Divacky PMS.pop();
358f22ef01cSRoman Divacky
359f22ef01cSRoman Divacky LPPassManager *LPPM;
360f22ef01cSRoman Divacky if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
361f22ef01cSRoman Divacky LPPM = (LPPassManager*)PMS.top();
362f22ef01cSRoman Divacky else {
363f22ef01cSRoman Divacky // Create new Loop Pass Manager if it does not exist.
364f22ef01cSRoman Divacky assert (!PMS.empty() && "Unable to create Loop Pass Manager");
365f22ef01cSRoman Divacky PMDataManager *PMD = PMS.top();
366f22ef01cSRoman Divacky
3676122f3e6SDimitry Andric // [1] Create new Loop Pass Manager
3686122f3e6SDimitry Andric LPPM = new LPPassManager();
369f22ef01cSRoman Divacky LPPM->populateInheritedAnalysis(PMS);
370f22ef01cSRoman Divacky
371f22ef01cSRoman Divacky // [2] Set up new manager's top level manager
372f22ef01cSRoman Divacky PMTopLevelManager *TPM = PMD->getTopLevelManager();
373f22ef01cSRoman Divacky TPM->addIndirectPassManager(LPPM);
374f22ef01cSRoman Divacky
375f22ef01cSRoman Divacky // [3] Assign manager to manage this new manager. This may create
376f22ef01cSRoman Divacky // and push new managers into PMS
377f22ef01cSRoman Divacky Pass *P = LPPM->getAsPass();
378f22ef01cSRoman Divacky TPM->schedulePass(P);
379f22ef01cSRoman Divacky
380f22ef01cSRoman Divacky // [4] Push new manager into PMS
381f22ef01cSRoman Divacky PMS.push(LPPM);
382f22ef01cSRoman Divacky }
383f22ef01cSRoman Divacky
384f22ef01cSRoman Divacky LPPM->add(this);
385f22ef01cSRoman Divacky }
38691bc56edSDimitry Andric
skipLoop(const Loop * L) const3873ca95b02SDimitry Andric bool LoopPass::skipLoop(const Loop *L) const {
38891bc56edSDimitry Andric const Function *F = L->getHeader()->getParent();
3893ca95b02SDimitry Andric if (!F)
3903ca95b02SDimitry Andric return false;
3913ca95b02SDimitry Andric // Check the opt bisect limit.
3923ca95b02SDimitry Andric LLVMContext &Context = F->getContext();
3934ba319b5SDimitry Andric if (!Context.getOptPassGate().shouldRunPass(this, *L))
3943ca95b02SDimitry Andric return true;
3953ca95b02SDimitry Andric // Check for the OptimizeNone attribute.
3963ca95b02SDimitry Andric if (F->hasFnAttribute(Attribute::OptimizeNone)) {
39791bc56edSDimitry Andric // FIXME: Report this to dbgs() only once per function.
3984ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' in function "
3994ba319b5SDimitry Andric << F->getName() << "\n");
40091bc56edSDimitry Andric // FIXME: Delete loop from pass manager's queue?
40191bc56edSDimitry Andric return true;
40291bc56edSDimitry Andric }
40391bc56edSDimitry Andric return false;
40491bc56edSDimitry Andric }
405d88c1a5aSDimitry Andric
406d88c1a5aSDimitry Andric char LCSSAVerificationPass::ID = 0;
407d88c1a5aSDimitry Andric INITIALIZE_PASS(LCSSAVerificationPass, "lcssa-verification", "LCSSA Verifier",
408d88c1a5aSDimitry Andric false, false)
409