7355e85def07cc7483043271098288589e0542ae
[oota-llvm.git] / include / llvm / PassAnalysisSupport.h
1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
2 //
3 // This file defines stuff that is used to define and "use" Analysis Passes.
4 // This file is automatically #included by Pass.h, so:
5 //
6 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
7 //
8 // Instead, #include Pass.h
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
13 #define LLVM_PASS_ANALYSIS_SUPPORT_H
14
15 // No need to include Pass.h, we are being included by it!
16
17 //===----------------------------------------------------------------------===//
18 // AnalysisUsage - Represent the analysis usage information of a pass.  This
19 // tracks analyses that the pass REQUIRES (must available when the pass runs),
20 // and analyses that the pass PRESERVES (the pass does not invalidate the
21 // results of these analyses).  This information is provided by a pass to the
22 // Pass infrastructure through the getAnalysisUsage virtual function.
23 //
24 class AnalysisUsage {
25   // Sets of analyses required and preserved by a pass
26   std::vector<AnalysisID> Required, Preserved;
27   bool PreservesAll;
28 public:
29   AnalysisUsage() : PreservesAll(false) {}
30   
31   // addRequired - Add the specified ID to the required set of the usage info
32   // for a pass.
33   //
34   AnalysisUsage &addRequiredID(AnalysisID ID) {
35     Required.push_back(ID);
36     return *this;
37   }
38   template<class PassClass>
39   AnalysisUsage &addRequired() {
40     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
41     Required.push_back(Pass::getClassPassInfo<PassClass>());
42     return *this;
43   }
44
45   // addPreserved - Add the specified ID to the set of analyses preserved by
46   // this pass
47   //
48   AnalysisUsage &addPreservedID(AnalysisID ID) {
49     Preserved.push_back(ID);
50     return *this;
51   }
52
53   template<class PassClass>
54   AnalysisUsage &addPreserved() {
55     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
56     Preserved.push_back(Pass::getClassPassInfo<PassClass>());
57     return *this;
58   }
59
60   // setPreservesAll - Set by analyses that do not transform their input at all
61   void setPreservesAll() { PreservesAll = true; }
62   bool getPreservesAll() const { return PreservesAll; }
63
64   /// setPreservesCFG - This function should be called by the pass, iff they do
65   /// not:
66   ///
67   ///  1. Add or remove basic blocks from the function
68   ///  2. Modify terminator instructions in any way.
69   ///
70   /// This function annotates the AnalysisUsage info object to say that analyses
71   /// that only depend on the CFG are preserved by this pass.
72   ///
73   void setPreservesCFG();
74
75   const std::vector<AnalysisID> &getRequiredSet() const { return Required; }
76   const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; }
77 };
78
79
80
81 //===----------------------------------------------------------------------===//
82 // AnalysisResolver - Simple interface implemented by PassManager objects that
83 // is used to pull analysis information out of them.
84 //
85 struct AnalysisResolver {
86   virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
87   virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
88   virtual void addPass(ImmutablePass *IP, AnalysisUsage &AU) = 0;
89   Pass *getAnalysis(AnalysisID ID) const {
90     Pass *Result = getAnalysisOrNullUp(ID);
91     assert(Result && "Pass has an incorrect analysis uses set!");
92     return Result;
93   }
94
95   // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
96   Pass *getAnalysisToUpdate(AnalysisID ID) const {
97     return getAnalysisOrNullUp(ID);
98   }
99
100   // Methods for introspecting into pass manager objects...
101   virtual unsigned getDepth() const = 0;
102   virtual unsigned getNumContainedPasses() const = 0;
103   virtual const Pass *getContainedPass(unsigned N) const = 0;
104
105   virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
106
107   void startPass(Pass *P) {}
108   void endPass(Pass *P) {}
109 protected:
110   void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
111 };
112
113 /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
114 /// to get to the analysis information that might be around that needs to be
115 /// updated.  This is different than getAnalysis in that it can fail (ie the
116 /// analysis results haven't been computed), so should only be used if you
117 /// provide the capability to update an analysis that exists.  This method is
118 /// often used by transformation APIs to update analysis results for a pass
119 /// automatically as the transform is performed.
120 ///
121 template<typename AnalysisType>
122 AnalysisType *Pass::getAnalysisToUpdate() const {
123   assert(Resolver && "Pass not resident in a PassManager object!");
124   const PassInfo *PI = getClassPassInfo<AnalysisType>();
125   if (PI == 0) return 0;
126   return dynamic_cast<AnalysisType*>(Resolver->getAnalysisToUpdate(PI));
127 }
128
129 #endif