1 //===- IPModRef.h - Compute IP Mod/Ref information --------------*- C++ -*-===//
5 // class IPModRef is an interprocedural analysis pass that computes
6 // flow-insensitive IP Mod and Ref information for every function
7 // (the GMOD and GREF problems) and for every call site (MOD and REF).
9 // In practice, this needs to do NO real interprocedural work because
10 // all that is needed is done by the data structure analysis.
11 // This uses the top-down DS graph for a function and the bottom-up DS graph
12 // for each callee (including the Mod/Ref flags in the bottom-up graph)
13 // to compute the set of nodes that are Mod and Ref for the function and
14 // for each of its call sites.
17 // class FunctionModRefInfo:
19 // The results of IPModRef are encapsulated in the class FunctionModRefInfo.
20 // The results are stored as bit vectors: bit i represents node i
21 // in the TD DSGraph for the current function. (This node numbering is
22 // implemented by class FunctionModRefInfo.) Each FunctionModRefInfo
24 // -- 2 bit vectors for the function (GMOD and GREF), and
25 // -- 2 bit vectors for each call site (MOD and REF).
28 // IPModRef vs. Alias Analysis for Clients:
30 // The IPModRef pass does not provide simpler query interfaces for specific
31 // LLVM values, instructions, or pointers because those results should be
32 // obtained through alias analysis (e.g., class DSAliasAnalysis).
33 // class IPModRef is primarily meant for other analysis passes that need to
34 // use Mod/Ref information efficiently for more complicated purposes;
35 // the bit-vector representations make propagation very efficient.
37 //===----------------------------------------------------------------------===//
39 #ifndef LLVM_ANALYSIS_IPMODREF_H
40 #define LLVM_ANALYSIS_IPMODREF_H
42 #include "llvm/Pass.h"
43 #include "Support/BitSetVector.h"
44 #include "Support/hash_map"
52 class ModRefInfo; // Result of IP Mod/Ref for one entity
53 class FunctionModRefInfo; // ModRefInfo for a func and all calls in it
54 class IPModRef; // Pass that computes IP Mod/Ref info
56 //---------------------------------------------------------------------------
60 // Representation of Mod/Ref information for a single function or callsite.
61 // This is represented as a pair of bit vectors, one each for Mod and Ref.
62 // Each bit vector is indexed by the node id of the DS graph node index.
63 //---------------------------------------------------------------------------
66 BitSetVector modNodeSet; // set of modified nodes
67 BitSetVector refNodeSet; // set of referenced nodes
71 // Methods to construct ModRefInfo objects.
73 ModRefInfo(unsigned int numNodes)
74 : modNodeSet(numNodes),
75 refNodeSet(numNodes) { }
77 unsigned getSize() const {
78 assert(modNodeSet.size() == refNodeSet.size() &&
79 "Mod & Ref different size?");
80 return modNodeSet.size();
83 void setNodeIsMod (unsigned nodeId) { modNodeSet[nodeId] = true; }
84 void setNodeIsRef (unsigned nodeId) { refNodeSet[nodeId] = true; }
87 // Methods to query the mod/ref info
89 bool nodeIsMod (unsigned nodeId) const { return modNodeSet.test(nodeId); }
90 bool nodeIsRef (unsigned nodeId) const { return refNodeSet.test(nodeId); }
91 bool nodeIsKill(unsigned nodeId) const { return false; }
93 const BitSetVector& getModSet() const { return modNodeSet; }
94 BitSetVector& getModSet() { return modNodeSet; }
96 const BitSetVector& getRefSet() const { return refNodeSet; }
97 BitSetVector& getRefSet() { return refNodeSet; }
99 // Debugging support methods
100 void print(std::ostream &O, const std::string& prefix=std::string("")) const;
105 //----------------------------------------------------------------------------
106 // class FunctionModRefInfo
108 // Representation of the results of IP Mod/Ref analysis for a function
109 // and for each of the call sites within the function.
110 // Each of these are represented as bit vectors of size = the number of
111 // nodes in the top-dwon DS graph of the function. Nodes are identified by
112 // their nodeId, in the range [0 .. funcTDGraph.size()-1].
113 //----------------------------------------------------------------------------
115 class FunctionModRefInfo {
116 const Function& F; // The function
117 IPModRef& IPModRefObj; // The IPModRef Object owning this
118 DSGraph* funcTDGraph; // Top-down DS graph for function
119 ModRefInfo funcModRefInfo; // ModRefInfo for the function body
120 std::map<const CallInst*, ModRefInfo*>
121 callSiteModRefInfo; // ModRefInfo for each callsite
122 std::map<const DSNode*, unsigned> NodeIds;
124 friend class IPModRef;
126 void computeModRef (const Function &func);
127 void computeModRef (const CallInst& callInst);
128 DSGraph *ResolveCallSiteModRefInfo(CallInst &CI,
129 hash_map<const DSNode*, DSNodeHandle> &NodeMap);
132 /* ctor */ FunctionModRefInfo (const Function& func,
133 IPModRef& IPModRefObj,
135 /* dtor */ ~FunctionModRefInfo ();
137 // Identify the function and its relevant DS graph
139 const Function& getFunction() const { return F; }
140 const DSGraph& getFuncGraph() const { return *funcTDGraph; }
142 // Retrieve Mod/Ref results for a single call site and for the function body
144 const ModRefInfo* getModRefInfo (const Function& func) const {
145 return &funcModRefInfo;
147 const ModRefInfo* getModRefInfo (const CallInst& callInst) const {
148 std::map<const CallInst*, ModRefInfo*>::const_iterator I =
149 callSiteModRefInfo.find(&callInst);
150 return (I == callSiteModRefInfo.end())? NULL : I->second;
153 // Get the nodeIds used to index all Mod/Ref information for current function
155 unsigned getNodeId (const DSNode* node) const {
156 std::map<const DSNode*, unsigned>::const_iterator iter = NodeIds.find(node);
157 assert(iter != NodeIds.end() && iter->second < funcModRefInfo.getSize());
161 unsigned getNodeId (const Value* value) const;
163 // Debugging support methods
164 void print(std::ostream &O) const;
169 //----------------------------------------------------------------------------
173 // An interprocedural pass that computes IP Mod/Ref info for functions and
174 // for individual call sites.
176 // Given the DSGraph of a function, this class can be queried for
177 // a ModRefInfo object describing all the nodes in the DSGraph that are
178 // (a) modified, and (b) referenced during an execution of the function
179 // from an arbitrary callsite, or during an execution of a single call-site
180 // within the function.
181 //----------------------------------------------------------------------------
183 class IPModRef : public Pass {
184 std::map<const Function*, FunctionModRefInfo*> funcToModRefInfoMap;
187 FunctionModRefInfo& getFuncInfo(const Function& func,
188 bool computeIfMissing = false);
190 IPModRef() : M(NULL) { }
193 // Driver function to run IP Mod/Ref on a Module.
194 // This initializes the module reference, and then computes IPModRef
195 // results immediately if demand-driven analysis was *not* specified.
197 virtual bool run(Module &M);
199 // Retrieve the Mod/Ref information for a single function
201 const FunctionModRefInfo& getFunctionModRefInfo(const Function& func) {
202 return getFuncInfo(func);
205 /// getBUDSGraph - This method returns the BU data structure graph for F
206 /// through the use of the BUDataStructures object.
208 const DSGraph &getBUDSGraph(const Function &F);
210 // Debugging support methods
212 void print(std::ostream &O) const;
215 // Release memory held by this pass when the pass pipeline is done
217 virtual void releaseMemory();
219 // getAnalysisUsage - This pass requires top-down data structure graphs.
220 // It modifies nothing.
222 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
225 //===----------------------------------------------------------------------===//