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