1 //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LoopPass and LPPassManager. All loop optimization
11 // and transformation passes are derived from LoopPass. LPPassManager is
12 // responsible for managing LoopPasses.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/LoopPass.h"
20 //===----------------------------------------------------------------------===//
25 // Compare Two loops based on their depth in loop nest.
28 bool operator()( Loop *L1, Loop *L2) const {
29 return L1->getLoopDepth() > L2->getLoopDepth();
33 // Loop queue used by Loop Pass Manager. This is a wrapper class
34 // that hides implemenation detail (use of priority_queue) inside .cpp file.
37 inline void push(Loop *L) { LPQ.push(L); }
38 inline void pop() { LPQ.pop(); }
39 inline Loop *top() { return LPQ.top(); }
40 inline bool empty() { return LPQ.empty(); }
42 std::priority_queue<Loop *, std::vector<Loop *>, LoopCompare> LPQ;
45 } // End of LLVM namespace
47 //===----------------------------------------------------------------------===//
50 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
52 LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) {
58 LPPassManager::~LPPassManager() {
62 /// Delete loop from the loop queue. This is used by Loop pass to inform
63 /// Loop Pass Manager that it should skip rest of the passes for this loop.
64 void LPPassManager::deleteLoopFromQueue(Loop *L) {
65 // Do not pop loop from LQ here. It will be done by runOnFunction while loop.
69 // Reoptimize this loop. LPPassManager will re-insert this loop into the
70 // queue. This allows LoopPass to change loop nest for the loop. This
71 // utility may send LPPassManager into infinite loops so use caution.
72 void LPPassManager::redoLoop(Loop *L) {
76 // Recurse through all subloops and all loops into LQ.
77 static void addLoopIntoQueue(Loop *L, LoopQueue *LQ) {
78 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
79 addLoopIntoQueue(*I, LQ);
83 /// run - Execute all of the passes scheduled for execution. Keep track of
84 /// whether any of the passes modifies the function, and if so, return true.
85 bool LPPassManager::runOnFunction(Function &F) {
86 LoopInfo &LI = getAnalysis<LoopInfo>();
89 // Populate Loop Queue
90 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
91 addLoopIntoQueue(*I, LQ);
93 std::string Msg1 = "Executing Pass '";
94 std::string Msg3 = "' Made Modification '";
97 while (!LQ->empty()) {
100 skipThisLoop = false;
101 redoThisLoop = false;
103 // Run all passes on current SCC
104 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
106 Pass *P = getContainedPass(Index);
107 AnalysisUsage AnUsage;
108 P->getAnalysisUsage(AnUsage);
110 std::string Msg2 = "' on Loop ...\n'";
111 dumpPassInfo(P, Msg1, Msg2);
112 dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
114 initializeAnalysisImpl(P);
117 LoopPass *LP = dynamic_cast<LoopPass *>(P);
118 assert (LP && "Invalid LPPassManager member");
119 LP->runOnLoop(*L, *this);
123 dumpPassInfo(P, Msg3, Msg2);
124 dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
126 removeNotPreservedAnalysis(P);
127 recordAvailableAnalysis(P);
128 removeDeadPasses(P, Msg2);
131 // Do not run other passes on this loop.
135 // Pop the loop from queue after running all passes.
146 //===----------------------------------------------------------------------===//
149 /// Assign pass manager to manage this pass.
150 void LoopPass::assignPassManager(PMStack &PMS,
151 PassManagerType PreferredType) {
152 // Find LPPassManager
153 while (!PMS.empty()) {
154 if (PMS.top()->getPassManagerType() > PMT_LoopPassManager)
160 LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());
162 // Create new Loop Pass Manager if it does not exist.
165 assert (!PMS.empty() && "Unable to create Loop Pass Manager");
166 PMDataManager *PMD = PMS.top();
168 // [1] Create new Call Graph Pass Manager
169 LPPM = new LPPassManager(PMD->getDepth() + 1);
171 // [2] Set up new manager's top level manager
172 PMTopLevelManager *TPM = PMD->getTopLevelManager();
173 TPM->addIndirectPassManager(LPPM);
175 // [3] Assign manager to manage this new manager. This may create
176 // and push new managers into PMS
177 Pass *P = dynamic_cast<Pass *>(LPPM);
178 P->assignPassManager(PMS);
180 // [4] Push new manager into PMS