* Standardize how analysis results/passes as printed with the print() virtual
[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
19 //===----------------------------------------------------------------------===//
20 // AnalysisUsage - Represent the analysis usage information of a pass.  This
21 // tracks analyses that the pass REQUIRES (must available when the pass runs),
22 // and analyses that the pass PRESERVES (the pass does not invalidate the
23 // results of these analyses).  This information is provided by a pass to the
24 // Pass infrastructure through the getAnalysisUsage virtual function.
25 //
26 class AnalysisUsage {
27   // Sets of analyses required and preserved by a pass
28   std::vector<AnalysisID> Required, Preserved, Provided;
29   bool PreservesAll;
30 public:
31   AnalysisUsage() : PreservesAll(false) {}
32   
33   // addRequires - Add the specified ID to the required set of the usage info
34   // for a pass.
35   //
36   AnalysisUsage &addRequired(AnalysisID ID) {
37     Required.push_back(ID);
38     return *this;
39   }
40
41   // addPreserves - Add the specified ID to the set of analyses preserved by
42   // this pass
43   //
44   AnalysisUsage &addPreserved(AnalysisID ID) {
45     Preserved.push_back(ID);
46     return *this;
47   }
48
49   void addProvided(AnalysisID ID) {
50     Provided.push_back(ID);
51   }
52
53   // PreservesAll - Set by analyses that do not transform their input at all
54   void setPreservesAll() { PreservesAll = true; }
55   bool preservesAll() const { return PreservesAll; }
56
57   // preservesCFG - This function should be called to by the pass, iff they do
58   // not:
59   //
60   //  1. Add or remove basic blocks from the function
61   //  2. Modify terminator instructions in any way.
62   //
63   // This function annotates the AnalysisUsage info object to say that analyses
64   // that only depend on the CFG are preserved by this pass.
65   //
66   void preservesCFG();
67
68   const std::vector<AnalysisID> &getRequiredSet() const { return Required; }
69   const std::vector<AnalysisID> &getPreservedSet() const { return Preserved; }
70   const std::vector<AnalysisID> &getProvidedSet() const { return Provided; }
71 };
72
73
74
75 //===----------------------------------------------------------------------===//
76 // AnalysisResolver - Simple interface implemented by PassManagers objects that
77 // is used to pull analysis information out of them.
78 //
79 struct AnalysisResolver {
80   virtual Pass *getAnalysisOrNullUp(AnalysisID ID) const = 0;
81   virtual Pass *getAnalysisOrNullDown(AnalysisID ID) const = 0;
82   Pass *getAnalysis(AnalysisID ID) {
83     Pass *Result = getAnalysisOrNullUp(ID);
84     assert(Result && "Pass has an incorrect analysis uses set!");
85     return Result;
86   }
87
88   // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
89   Pass *getAnalysisToUpdate(AnalysisID ID) {
90     Pass *Result = getAnalysisOrNullUp(ID);
91     return Result;
92   }
93
94   virtual unsigned getDepth() const = 0;
95
96   virtual void markPassUsed(AnalysisID P, Pass *User) = 0;
97
98   void startPass(Pass *P) {}
99   void endPass(Pass *P) {}
100 protected:
101   void setAnalysisResolver(Pass *P, AnalysisResolver *AR);
102 };
103
104 #endif