Add LoopQueue. This is used by loop pass manager to manage loop nest.
[oota-llvm.git] / lib / Analysis / LoopPass.cpp
1 //===- LoopPass.cpp - Loop Pass and Loop Pass Manager ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/Analysis/LoopPass.h"
17 #include <queue>
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 // LoopQueue
22
23 namespace llvm {
24
25 // Compare Two loops based on their depth in loop nest.
26 class LoopCompare {
27 public:
28   bool operator()( Loop *L1, Loop *L2) const {
29     return L1->getLoopDepth() > L2->getLoopDepth();
30   }
31 };
32
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.
35 class LoopQueue {
36
37   inline void push(Loop *L) { LPQ.push(L); }
38   inline void pop() { LPQ.pop(); }
39   inline Loop *top() { return LPQ.top(); }
40
41 private:
42   std::priority_queue<Loop *, std::vector<Loop *>, LoopCompare> LPQ;
43 };
44
45 } // End of LLVM namespace
46
47 //===----------------------------------------------------------------------===//
48 // LPPassManager
49 //
50 /// LPPassManager manages FPPassManagers and CalLGraphSCCPasses.
51
52 LPPassManager::LPPassManager(int Depth) : PMDataManager(Depth) { 
53   LQ = new LoopQueue(); 
54 }
55
56 LPPassManager::~LPPassManager() {
57   delete LQ;
58 }
59
60 /// run - Execute all of the passes scheduled for execution.  Keep track of
61 /// whether any of the passes modifies the function, and if so, return true.
62 bool LPPassManager::runOnFunction(Function &F) {
63   LoopInfo &LI = getAnalysis<LoopInfo>();
64   bool Changed = false;
65
66   std::string Msg1 = "Executing Pass '";
67   std::string Msg3 = "' Made Modification '";
68
69   // Walk Loops
70   for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I) {
71
72     Loop *L  = *I;
73     // Run all passes on current SCC
74     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
75
76       Pass *P = getContainedPass(Index);
77       AnalysisUsage AnUsage;
78       P->getAnalysisUsage(AnUsage);
79
80       std::string Msg2 = "' on Loop ...\n'";
81       dumpPassInfo(P, Msg1, Msg2);
82       dumpAnalysisSetInfo("Required", P, AnUsage.getRequiredSet());
83
84       initializeAnalysisImpl(P);
85
86       StartPassTimer(P);
87       LoopPass *LP = dynamic_cast<LoopPass *>(P);
88       assert (LP && "Invalid LPPassManager member");
89       LP->runOnLoop(*L, *this);
90       StopPassTimer(P);
91
92       if (Changed)
93         dumpPassInfo(P, Msg3, Msg2);
94       dumpAnalysisSetInfo("Preserved", P, AnUsage.getPreservedSet());
95       
96       removeNotPreservedAnalysis(P);
97       recordAvailableAnalysis(P);
98       removeDeadPasses(P, Msg2);
99     }
100   }
101
102   return Changed;
103 }
104
105