Give a back pointer to the IPModRef object to the FunctionModRefInfo object
[oota-llvm.git] / lib / Analysis / DataStructure / IPModRef.cpp
1 //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===//
2 //
3 // See high-level comments in include/llvm/Analysis/IPModRef.h
4 // 
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Analysis/IPModRef.h"
8 #include "llvm/Analysis/DataStructure.h"
9 #include "llvm/Analysis/DSGraph.h"
10 #include "llvm/Module.h"
11 #include "llvm/iOther.h"
12 #include "Support/Statistic.h"
13 #include "Support/STLExtras.h"
14 #include "Support/StringExtras.h"
15
16 //----------------------------------------------------------------------------
17 // Private constants and data
18 //----------------------------------------------------------------------------
19
20 static RegisterAnalysis<IPModRef>
21 Z("ipmodref", "Interprocedural mod/ref analysis");
22
23
24 //----------------------------------------------------------------------------
25 // class ModRefInfo
26 //----------------------------------------------------------------------------
27
28 void ModRefInfo::print(std::ostream &O) const
29 {
30   O << std::endl << "Modified   nodes = " << modNodeSet;
31   O              << "Referenced nodes = " << refNodeSet << std::endl;
32 }
33
34 void ModRefInfo::dump() const
35 {
36   print(std::cerr);
37 }
38
39 //----------------------------------------------------------------------------
40 // class FunctionModRefInfo
41 //----------------------------------------------------------------------------
42
43
44 // This constructor computes a node numbering for the TD graph.
45 // 
46 FunctionModRefInfo::FunctionModRefInfo(const Function& func,
47                                        IPModRef& ipmro,
48                                        const DSGraph& tdg,
49                                        const DSGraph& ldg)
50   : F(func), IPModRefObj(ipmro), 
51     funcTDGraph(tdg),
52     funcLocalGraph(ldg),
53     funcModRefInfo(tdg.getGraphSize())
54 {
55   for (unsigned i=0, N = funcTDGraph.getGraphSize(); i < N; ++i)
56     NodeIds[funcTDGraph.getNodes()[i]] = i;
57 }
58
59
60 FunctionModRefInfo::~FunctionModRefInfo()
61 {
62   for(std::map<const CallInst*, ModRefInfo*>::iterator
63         I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I)
64     delete(I->second);
65
66   // Empty map just to make problems easier to track down
67   callSiteModRefInfo.clear();
68 }
69
70 unsigned FunctionModRefInfo::getNodeId(const Value* value) const {
71   return getNodeId(funcTDGraph.getNodeForValue(const_cast<Value*>(value))
72                    .getNode());
73 }
74
75
76
77 // Dummy function that will be replaced with one that inlines
78 // the callee's BU graph into the caller's TD graph.
79 // 
80 static const DSGraph* ResolveGraphForCallSite(const DSGraph& funcTDGraph,
81                                        const CallInst& callInst)
82 {
83   return &funcTDGraph;                    // TEMPORARY
84 }
85
86
87 // Compute Mod/Ref bit vectors for the entire function.
88 // These are simply copies of the Read/Write flags from the nodes of
89 // the top-down DS graph.
90 // 
91 void FunctionModRefInfo::computeModRef(const Function &func)
92 {
93   // Mark all nodes in the graph that are marked MOD as being mod
94   // and all those marked REF as being ref.
95   for (unsigned i = 0, N = funcTDGraph.getGraphSize(); i < N; ++i)
96     {
97       if (funcTDGraph.getNodes()[i]->isModified())
98         funcModRefInfo.setNodeIsMod(i);
99       if (funcTDGraph.getNodes()[i]->isRead())
100         funcModRefInfo.setNodeIsRef(i);
101     }
102
103   // Compute the Mod/Ref info for all call sites within the function
104   // Use the Local DSgraph, which includes all the call sites in the
105   // original program.
106   const std::vector<DSCallSite>& callSites = funcLocalGraph.getFunctionCalls();
107   for (unsigned i = 0, N = callSites.size(); i < N; ++i)
108     computeModRef(callSites[i].getCallInst());
109 }
110
111
112 // Compute Mod/Ref bit vectors for a single call site.
113 // These are copies of the Read/Write flags from the nodes of
114 // the graph produced by clearing all flags in teh caller's TD graph
115 // and then inlining the callee's BU graph into the caller's TD graph.
116 // 
117 void
118 FunctionModRefInfo::computeModRef(const CallInst& callInst)
119 {
120   // Allocate the mod/ref info for the call site.  Bits automatically cleared.
121   ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
122   callSiteModRefInfo[&callInst] = callModRefInfo;
123
124   // Get a copy of the graph for the callee with the callee inlined
125   const DSGraph* csgp = ResolveGraphForCallSite(funcTDGraph, callInst);
126   assert(csgp && "Unable to compute callee mod/ref information");
127
128   // For all nodes in the graph, extract the mod/ref information
129   const std::vector<DSNode*>& csgNodes = csgp->getNodes();
130   const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes();
131   assert(csgNodes.size() == origNodes.size());
132   for (unsigned i=0, N = csgNodes.size(); i < N; ++i)
133     { 
134       if (csgNodes[i]->isModified())
135         callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
136       if (csgNodes[i]->isRead())
137         callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
138     }
139 }
140
141
142 // Print the results of the pass.
143 // Currently this just prints bit-vectors and is not very readable.
144 // 
145 void FunctionModRefInfo::print(std::ostream &O) const
146 {
147   O << "---------- Mod/ref information for function "
148     << F.getName() << "---------- \n\n";
149
150   O << "Mod/ref info for function body:\n";
151   funcModRefInfo.print(O);
152
153   for (std::map<const CallInst*, ModRefInfo*>::const_iterator
154          CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
155        CI != CE; ++CI)
156     {
157       O << "Mod/ref info for call site " << CI->first << ":\n";
158       CI->second->print(O);
159     }
160
161   O << "\n";
162 }
163
164 void FunctionModRefInfo::dump() const
165 {
166   print(std::cerr);
167 }
168
169
170 //----------------------------------------------------------------------------
171 // class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
172 //----------------------------------------------------------------------------
173
174
175 // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
176 // 
177 void IPModRef::releaseMemory()
178 {
179   for(std::map<const Function*, FunctionModRefInfo*>::iterator
180         I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
181     delete(I->second);
182
183   // Clear map so memory is not re-released if we are called again
184   funcToModRefInfoMap.clear();
185 }
186
187
188 // Run the "interprocedural" pass on each function.  This needs to do
189 // NO real interprocedural work because all that has been done the
190 // data structure analysis.
191 // 
192 bool IPModRef::run(Module &theModule)
193 {
194   M = &theModule;
195   for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
196     if (! FI->isExternal())
197       getFuncInfo(*FI, /*computeIfMissing*/ true);
198   return true;
199 }
200
201
202 FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
203                                           bool computeIfMissing)
204 {
205   FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func];
206   assert (funcInfo != NULL || computeIfMissing);
207   if (funcInfo == NULL)
208     { // Create a new FunctionModRefInfo object
209       funcInfo = new FunctionModRefInfo(func, *this, // inserts into map
210                               getAnalysis<TDDataStructures>().getDSGraph(func),
211                           getAnalysis<LocalDataStructures>().getDSGraph(func));
212       funcInfo->computeModRef(func);            // computes the mod/ref info
213     }
214   return *funcInfo;
215 }
216
217 // getAnalysisUsage - This pass requires top-down data structure graphs.
218 // It modifies nothing.
219 // 
220 void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
221   AU.setPreservesAll();
222   AU.addRequired<LocalDataStructures>();
223   AU.addRequired<BUDataStructures>();
224   AU.addRequired<TDDataStructures>();
225 }
226
227
228 void IPModRef::print(std::ostream &O) const
229 {
230   O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
231   
232   for (std::map<const Function*, FunctionModRefInfo*>::const_iterator
233          mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end();
234        mapI != mapE; ++mapI)
235     mapI->second->print(O);
236
237   O << "\n";
238 }
239
240
241 void IPModRef::dump() const
242 {
243   print(std::cerr);
244 }