4f449952d9449f28c27c6f05ad889a230d50eb4d
[oota-llvm.git] / include / llvm / Analysis / LoopPass.h
1 //===- LoopPass.h - LoopPass class ----------------------------------------===//
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 defines LoopPass class. All loop optimization
11 // and transformation passes are derived from LoopPass.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LOOP_PASS_H
16 #define LLVM_LOOP_PASS_H
17
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Pass.h"
20 #include "llvm/PassManagers.h"
21 #include "llvm/Function.h"
22
23 namespace llvm {
24
25 class LPPassManager;
26 class Loop;
27 class Function;
28
29 class LoopPass : public Pass {
30
31  public:
32   // runOnLoop - THis method should be implemented by the subclass to perform
33   // whatever action is necessary for the specfied Loop. 
34   virtual bool runOnLoop (Loop *L, LPPassManager &LPM) = 0;
35   virtual bool runOnFunctionBody (Function &F, LPPassManager &LPM) { 
36     return false; 
37   }
38
39   // Initialization and finalization hooks.
40   virtual bool doInitialization(Loop *L, LPPassManager &LPM) { 
41     return false; 
42   }
43
44   // Finalization hook does not supply Loop because at this time
45   // loop nest is completely different.
46   virtual bool doFinalization() { return false; }
47  
48   /// Assign pass manager to manager this pass
49   virtual void assignPassManager(PMStack &PMS,
50                                  PassManagerType PMT = PMT_LoopPassManager);
51
52 };
53
54 class LPPassManager : public FunctionPass, public PMDataManager {
55
56 public:
57   LPPassManager(int Depth);
58
59   /// run - Execute all of the passes scheduled for execution.  Keep track of
60   /// whether any of the passes modifies the module, and if so, return true.
61   bool runOnFunction(Function &F);
62
63   /// Pass Manager itself does not invalidate any analysis info.
64   void getAnalysisUsage(AnalysisUsage &Info) const {
65     // LPPassManager needs LoopInfo. In the long term LoopInfo class will 
66     // be consumed by LPPassManager.
67     Info.addRequired<LoopInfo>();
68     Info.setPreservesAll();
69   }
70   
71   virtual const char *getPassName() const {
72     return "Loop Pass Manager";
73   }
74   
75   // Print passes managed by this manager
76   void dumpPassStructure(unsigned Offset) {
77     llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
78     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
79       Pass *P = getContainedPass(Index);
80       P->dumpPassStructure(Offset + 1);
81       dumpLastUses(P, Offset+1);
82     }
83   }
84   
85   Pass *getContainedPass(unsigned N) {
86     assert ( N < PassVector.size() && "Pass number out of range!");
87     Pass *FP = static_cast<Pass *>(PassVector[N]);
88     return FP;
89   }
90
91   virtual PassManagerType getPassManagerType() const { 
92     return PMT_LoopPassManager; 
93   }
94
95 public:
96   // Delete loop from the loop queue. This is used by Loop pass to inform
97   // Loop Pass Manager that it should skip rest of the passes for this loop.
98   void deleteLoopFromQueue(Loop *L);
99
100   // Reoptimize this loop. LPPassManager will re-insert this loop into the
101   // queue. This allows LoopPass to change loop nest for the loop. This
102   // utility may send LPPassManager into infinite loops so use caution.
103   void redoLoop(Loop *L);
104 private:
105   std::deque<Loop *> LQ;
106   bool skipThisLoop;
107   bool redoThisLoop;
108 };
109
110 } // End llvm namespace
111
112 #endif