Added LLVM copyright header (for lack of a better term).
[oota-llvm.git] / include / llvm / Analysis / IPModRef.h
1 //===- IPModRef.h - Compute IP Mod/Ref information --------------*- 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 // class IPModRef:
11 // 
12 // class IPModRef is an interprocedural analysis pass that computes
13 // flow-insensitive IP Mod and Ref information for every function
14 // (the GMOD and GREF problems) and for every call site (MOD and REF).
15 // 
16 // In practice, this needs to do NO real interprocedural work because
17 // all that is needed is done by the data structure analysis.
18 // This uses the top-down DS graph for a function and the bottom-up DS graph
19 // for each callee (including the Mod/Ref flags in the bottom-up graph)
20 // to compute the set of nodes that are Mod and Ref for the function and
21 // for each of its call sites.
22 //
23 // 
24 // class FunctionModRefInfo:
25 // 
26 // The results of IPModRef are encapsulated in the class FunctionModRefInfo.
27 // The results are stored as bit vectors: bit i represents node i
28 // in the TD DSGraph for the current function.  (This node numbering is
29 // implemented by class FunctionModRefInfo.)  Each FunctionModRefInfo
30 // includes:
31 // -- 2 bit vectors for the function (GMOD and GREF), and
32 // -- 2 bit vectors for each call site (MOD and REF).
33 //
34 // 
35 // IPModRef vs. Alias Analysis for Clients:
36 // 
37 // The IPModRef pass does not provide simpler query interfaces for specific
38 // LLVM values, instructions, or pointers because those results should be
39 // obtained through alias analysis (e.g., class DSAliasAnalysis).
40 // class IPModRef is primarily meant for other analysis passes that need to
41 // use Mod/Ref information efficiently for more complicated purposes;
42 // the bit-vector representations make propagation very efficient.
43 //
44 //===----------------------------------------------------------------------===//
45
46 #ifndef LLVM_ANALYSIS_IPMODREF_H
47 #define LLVM_ANALYSIS_IPMODREF_H
48
49 #include "llvm/Pass.h"
50 #include "Support/BitSetVector.h"
51 #include "Support/hash_map"
52
53 class Module;
54 class Function;
55 class CallSite;
56 class Instruction;
57 class CallInst;
58 class InvokeInst;
59 class DSNode;
60 class DSGraph;
61 class DSNodeHandle;
62 class ModRefInfo;               // Result of IP Mod/Ref for one entity
63 class FunctionModRefInfo;       // ModRefInfo for a func and all calls in it
64 class IPModRef;                 // Pass that computes IP Mod/Ref info
65
66 //---------------------------------------------------------------------------
67 // class ModRefInfo 
68 // 
69 // Purpose:
70 //   Representation of Mod/Ref information for a single function or callsite.
71 //   This is represented as a pair of bit vectors, one each for Mod and Ref.
72 //   Each bit vector is indexed by the node id of the DS graph node index.
73 //---------------------------------------------------------------------------
74
75 class ModRefInfo {
76   BitSetVector   modNodeSet;            // set of modified nodes
77   BitSetVector   refNodeSet;            // set of referenced nodes
78   
79 public:
80   // 
81   // Methods to construct ModRefInfo objects.
82   // 
83   ModRefInfo(unsigned int numNodes)
84     : modNodeSet(numNodes),
85       refNodeSet(numNodes) { }
86
87   unsigned getSize() const {
88     assert(modNodeSet.size() == refNodeSet.size() &&
89            "Mod & Ref different size?");
90     return modNodeSet.size();
91   }
92
93   void setNodeIsMod (unsigned nodeId)   { modNodeSet[nodeId] = true; }
94   void setNodeIsRef (unsigned nodeId)   { refNodeSet[nodeId] = true; }
95
96   //
97   // Methods to query the mod/ref info
98   // 
99   bool nodeIsMod (unsigned nodeId) const  { return modNodeSet.test(nodeId); }
100   bool nodeIsRef (unsigned nodeId) const  { return refNodeSet.test(nodeId); }
101   bool nodeIsKill(unsigned nodeId) const  { return false; }
102
103   const BitSetVector&  getModSet() const  { return modNodeSet; }
104         BitSetVector&  getModSet()        { return modNodeSet; }
105
106   const BitSetVector&  getRefSet() const  { return refNodeSet; }
107         BitSetVector&  getRefSet()        { return refNodeSet; }
108
109   // Debugging support methods
110   void print(std::ostream &O, const std::string& prefix=std::string("")) const;
111   void dump() const;
112 };
113
114
115 //----------------------------------------------------------------------------
116 // class FunctionModRefInfo
117 // 
118 // Representation of the results of IP Mod/Ref analysis for a function
119 // and for each of the call sites within the function.
120 // Each of these are represented as bit vectors of size = the number of
121 // nodes in the top-dwon DS graph of the function.  Nodes are identified by
122 // their nodeId, in the range [0 .. funcTDGraph.size()-1].
123 //----------------------------------------------------------------------------
124
125 class FunctionModRefInfo {
126   const Function&       F;                  // The function
127   IPModRef&             IPModRefObj;        // The IPModRef Object owning this
128   DSGraph*              funcTDGraph;        // Top-down DS graph for function
129   ModRefInfo            funcModRefInfo;     // ModRefInfo for the function body
130   std::map<const Instruction*, ModRefInfo*>
131                         callSiteModRefInfo; // ModRefInfo for each callsite
132   std::map<const DSNode*, unsigned> NodeIds;
133
134   friend class IPModRef;
135
136   void          computeModRef   (const Function &func);
137   void          computeModRef   (CallSite call);
138   DSGraph *ResolveCallSiteModRefInfo(CallSite CS,
139                                 hash_map<const DSNode*, DSNodeHandle> &NodeMap);
140
141 public:
142   /* ctor */    FunctionModRefInfo      (const Function& func,
143                                          IPModRef&       IPModRefObj,
144                                          DSGraph*        tdgClone);
145   /* dtor */    ~FunctionModRefInfo     ();
146
147   // Identify the function and its relevant DS graph
148   // 
149   const Function& getFunction() const   { return F; }
150   const DSGraph&  getFuncGraph() const  { return *funcTDGraph; }
151
152   // Retrieve Mod/Ref results for a single call site and for the function body
153   // 
154   const ModRefInfo*     getModRefInfo  (const Function& func) const {
155     return &funcModRefInfo;
156   }
157   const ModRefInfo*     getModRefInfo  (const CallInst& callInst) const {
158     std::map<const Instruction*, ModRefInfo*>::const_iterator I = 
159       callSiteModRefInfo.find((Instruction*)&callInst);
160     return (I == callSiteModRefInfo.end()) ? NULL : I->second;
161   }
162   const ModRefInfo*     getModRefInfo  (const InvokeInst& II) const {
163     std::map<const Instruction*, ModRefInfo*>::const_iterator I = 
164       callSiteModRefInfo.find((Instruction*)&II);
165     return (I == callSiteModRefInfo.end()) ? NULL : I->second;
166   }
167
168   // Get the nodeIds used to index all Mod/Ref information for current function
169   //
170   unsigned              getNodeId       (const DSNode* node) const {
171     std::map<const DSNode*, unsigned>::const_iterator iter = NodeIds.find(node);
172     assert(iter != NodeIds.end() && iter->second < funcModRefInfo.getSize());
173     return iter->second;
174   }
175
176   unsigned              getNodeId       (const Value* value) const;
177
178   // Debugging support methods
179   void print(std::ostream &O) const;
180   void dump() const;
181 };
182
183
184 //----------------------------------------------------------------------------
185 // class IPModRef
186 // 
187 // Purpose:
188 // An interprocedural pass that computes IP Mod/Ref info for functions and
189 // for individual call sites.
190 // 
191 // Given the DSGraph of a function, this class can be queried for
192 // a ModRefInfo object describing all the nodes in the DSGraph that are
193 // (a) modified, and (b) referenced during an execution of the function
194 // from an arbitrary callsite, or during an execution of a single call-site
195 // within the function.
196 //----------------------------------------------------------------------------
197
198 class IPModRef : public Pass {
199   std::map<const Function*, FunctionModRefInfo*> funcToModRefInfoMap;
200   Module* M;
201
202   FunctionModRefInfo& getFuncInfo(const Function& func,
203                                   bool computeIfMissing = false);
204 public:
205   IPModRef() : M(NULL)  { }
206   ~IPModRef()           { }
207
208   // Driver function to run IP Mod/Ref on a Module.
209   // This initializes the module reference, and then computes IPModRef
210   // results immediately if demand-driven analysis was *not* specified.
211   // 
212   virtual bool run(Module &M);
213
214   // Retrieve the Mod/Ref information for a single function
215   // 
216   const FunctionModRefInfo& getFunctionModRefInfo(const Function& func) {
217     return getFuncInfo(func);
218   }
219
220   /// getBUDSGraph - This method returns the BU data structure graph for F
221   /// through the use of the BUDataStructures object.
222   ///
223   const DSGraph &getBUDSGraph(const Function &F);
224
225   // Debugging support methods
226   // 
227   void print(std::ostream &O) const;
228   void dump() const;
229
230   // Release memory held by this pass when the pass pipeline is done
231   // 
232   virtual void releaseMemory();
233
234   // getAnalysisUsage - This pass requires top-down data structure graphs.
235   // It modifies nothing.
236   // 
237   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
238 };
239
240 //===----------------------------------------------------------------------===//
241
242 #endif