blob: 3631514b5576b59c3fe25d6caf141a423d335346 [file] [log] [blame]
Vikram S. Adve895c0bd2002-11-06 17:02:03 +00001//===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===//
2//
3// See high-level comments in include/llvm/Analysis/IPModRef.h
4//
5//===----------------------------------------------------------------------===//
6
Chris Lattnerfc928242002-11-06 18:38:18 +00007#include "llvm/Analysis/IPModRef.h"
Vikram S. Adve895c0bd2002-11-06 17:02:03 +00008#include "llvm/Analysis/DataStructure.h"
9#include "llvm/Analysis/DSGraph.h"
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000010#include "llvm/Module.h"
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000011#include "llvm/iOther.h"
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000012#include "Support/Statistic.h"
13#include "Support/STLExtras.h"
14#include "Support/StringExtras.h"
15
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000016//----------------------------------------------------------------------------
17// Private constants and data
18//----------------------------------------------------------------------------
19
20static RegisterAnalysis<IPModRef>
21Z("ipmodref", "Interprocedural mod/ref analysis");
22
23
24//----------------------------------------------------------------------------
25// class ModRefInfo
26//----------------------------------------------------------------------------
27
28void ModRefInfo::print(std::ostream &O) const
29{
30 O << std::endl << "Modified nodes = " << modNodeSet;
31 O << "Referenced nodes = " << refNodeSet << std::endl;
32}
33
34void 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//
46FunctionModRefInfo::FunctionModRefInfo(const Function& func,
Chris Lattner21108082002-11-06 19:07:13 +000047 IPModRef& ipmro,
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000048 const DSGraph& tdg,
49 const DSGraph& ldg)
Chris Lattner21108082002-11-06 19:07:13 +000050 : F(func), IPModRefObj(ipmro),
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000051 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
60FunctionModRefInfo::~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
Chris Lattnerfc928242002-11-06 18:38:18 +000070unsigned FunctionModRefInfo::getNodeId(const Value* value) const {
71 return getNodeId(funcTDGraph.getNodeForValue(const_cast<Value*>(value))
72 .getNode());
73}
74
75
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000076
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000077// Compute Mod/Ref bit vectors for the entire function.
78// These are simply copies of the Read/Write flags from the nodes of
79// the top-down DS graph.
80//
81void FunctionModRefInfo::computeModRef(const Function &func)
82{
83 // Mark all nodes in the graph that are marked MOD as being mod
84 // and all those marked REF as being ref.
85 for (unsigned i = 0, N = funcTDGraph.getGraphSize(); i < N; ++i)
86 {
87 if (funcTDGraph.getNodes()[i]->isModified())
88 funcModRefInfo.setNodeIsMod(i);
89 if (funcTDGraph.getNodes()[i]->isRead())
90 funcModRefInfo.setNodeIsRef(i);
91 }
92
93 // Compute the Mod/Ref info for all call sites within the function
94 // Use the Local DSgraph, which includes all the call sites in the
95 // original program.
96 const std::vector<DSCallSite>& callSites = funcLocalGraph.getFunctionCalls();
97 for (unsigned i = 0, N = callSites.size(); i < N; ++i)
98 computeModRef(callSites[i].getCallInst());
99}
100
Chris Lattner268748a2002-11-06 19:38:43 +0000101// ResolveCallSiteModRefInfo - This method performs the following actions:
102//
103// 1. It clones the top-down graph for the current function
104// 2. It clears all of the mod/ref bits in the cloned graph
105// 3. It then merges the bottom-up graph(s) for the specified call-site into
106// the clone (bringing new mod/ref bits).
Chris Lattner4476ceb2002-11-06 19:59:33 +0000107// 4. It returns the clone, and a mapping of nodes from the original TDGraph to
108// the cloned graph with Mod/Ref info for the callsite.
Chris Lattner268748a2002-11-06 19:38:43 +0000109//
110// NOTE: Because this clones a dsgraph and returns it, the caller is responsible
111// for deleting the returned graph!
Chris Lattnered8e6492002-11-07 07:12:23 +0000112// NOTE: This method may return a null pointer if it is unable to determine the
113// requested information (because the call site calls an external
114// function or we cannot determine the complete set of functions invoked).
Chris Lattner268748a2002-11-06 19:38:43 +0000115//
Chris Lattnered8e6492002-11-07 07:12:23 +0000116DSGraph *FunctionModRefInfo::ResolveCallSiteModRefInfo(CallInst &CI,
Chris Lattner4476ceb2002-11-06 19:59:33 +0000117 std::map<const DSNode*, DSNodeHandle> &NodeMap) {
Chris Lattner268748a2002-11-06 19:38:43 +0000118
Chris Lattner4476ceb2002-11-06 19:59:33 +0000119 // Step #1: Clone the top-down graph...
120 std::map<const DSNode*, DSNode*> RawNodeMap;
121 DSGraph *Result = new DSGraph(funcTDGraph, RawNodeMap);
122
123 // Convert the NodeMap from a map to DSNode* to be a map to DSNodeHandle's
124 NodeMap.insert(RawNodeMap.begin(), RawNodeMap.end());
125
126 // We are now done with the old map... so free it's memory...
127 RawNodeMap.clear();
128
129 // Step #2: Clear Mod/Ref information...
130 Result->maskNodeTypes(~(DSNode::Modified | DSNode::Read));
131
Chris Lattnered8e6492002-11-07 07:12:23 +0000132 // Step #3: clone the bottom up graphs for the callees into the caller graph
133 if (const Function *F = CI.getCalledFunction()) {
134 if (F->isExternal()) {
135 delete Result;
136 return 0; // We cannot compute Mod/Ref info for this callsite...
137 }
Chris Lattner268748a2002-11-06 19:38:43 +0000138
Chris Lattnered8e6492002-11-07 07:12:23 +0000139 // Build up a DSCallSite for our invocation point here...
140
141 // If the call returns a value, make sure to merge the nodes...
142 DSNodeHandle RetVal;
143 if (DS::isPointerType(CI.getType()))
144 RetVal = Result->getNodeForValue(&CI);
145
146 // Populate the arguments list...
147 std::vector<DSNodeHandle> Args;
148 for (unsigned i = 1, e = CI.getNumOperands(); i != e; ++i)
149 if (DS::isPointerType(CI.getOperand(i)->getType()))
150 Args.push_back(Result->getNodeForValue(CI.getOperand(i)));
151
152 // Build the call site...
153 DSCallSite CS(CI, RetVal, 0, Args);
154
155 // Perform the merging now of the graph for the callee, which will come with
156 // mod/ref bits set...
157 Result->mergeInGraph(CS, IPModRefObj.getBUDSGraph(*F),
158 DSGraph::StripAllocaBit);
159
160 } else {
161 std::cerr << "IP Mod/Ref indirect call not implemented yet: "
162 << "Being conservative\n";
163 delete Result;
164 return 0;
165 }
166
167 // Remove trivial dead nodes... don't aggressively prune graph though... the
168 // graph is short lived anyway.
169 Result->removeTriviallyDeadNodes(false);
170
171 // Step #4: Return the clone + the mapping (by ref)
Chris Lattner268748a2002-11-06 19:38:43 +0000172 return Result;
173}
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000174
175// Compute Mod/Ref bit vectors for a single call site.
176// These are copies of the Read/Write flags from the nodes of
177// the graph produced by clearing all flags in teh caller's TD graph
178// and then inlining the callee's BU graph into the caller's TD graph.
179//
180void
181FunctionModRefInfo::computeModRef(const CallInst& callInst)
182{
183 // Allocate the mod/ref info for the call site. Bits automatically cleared.
184 ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
185 callSiteModRefInfo[&callInst] = callModRefInfo;
186
187 // Get a copy of the graph for the callee with the callee inlined
Chris Lattner4476ceb2002-11-06 19:59:33 +0000188 std::map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattnered8e6492002-11-07 07:12:23 +0000189 DSGraph* csgp =
190 ResolveCallSiteModRefInfo(const_cast<CallInst&>(callInst), NodeMap);
191
192 assert(csgp && "FIXME: Cannot handle case where call site mod/ref info"
193 " is not available yet!");
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000194
195 // For all nodes in the graph, extract the mod/ref information
196 const std::vector<DSNode*>& csgNodes = csgp->getNodes();
197 const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes();
198 assert(csgNodes.size() == origNodes.size());
199 for (unsigned i=0, N = csgNodes.size(); i < N; ++i)
200 {
201 if (csgNodes[i]->isModified())
202 callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
203 if (csgNodes[i]->isRead())
204 callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
205 }
Chris Lattnere83cb532002-11-07 05:00:35 +0000206
207 // Drop nodemap before we delete the graph...
208 NodeMap.clear();
Chris Lattner268748a2002-11-06 19:38:43 +0000209 delete csgp;
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000210}
211
212
213// Print the results of the pass.
214// Currently this just prints bit-vectors and is not very readable.
215//
216void FunctionModRefInfo::print(std::ostream &O) const
217{
218 O << "---------- Mod/ref information for function "
219 << F.getName() << "---------- \n\n";
220
221 O << "Mod/ref info for function body:\n";
222 funcModRefInfo.print(O);
223
224 for (std::map<const CallInst*, ModRefInfo*>::const_iterator
225 CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
226 CI != CE; ++CI)
227 {
228 O << "Mod/ref info for call site " << CI->first << ":\n";
229 CI->second->print(O);
230 }
231
232 O << "\n";
233}
234
235void FunctionModRefInfo::dump() const
236{
237 print(std::cerr);
238}
239
240
241//----------------------------------------------------------------------------
242// class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
243//----------------------------------------------------------------------------
244
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000245// Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
246//
247void IPModRef::releaseMemory()
248{
249 for(std::map<const Function*, FunctionModRefInfo*>::iterator
250 I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
251 delete(I->second);
252
253 // Clear map so memory is not re-released if we are called again
254 funcToModRefInfoMap.clear();
255}
256
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000257// Run the "interprocedural" pass on each function. This needs to do
258// NO real interprocedural work because all that has been done the
259// data structure analysis.
260//
261bool IPModRef::run(Module &theModule)
262{
263 M = &theModule;
Chris Lattnere83cb532002-11-07 05:00:35 +0000264
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000265 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
266 if (! FI->isExternal())
267 getFuncInfo(*FI, /*computeIfMissing*/ true);
268 return true;
269}
270
271
Chris Lattnerfc928242002-11-06 18:38:18 +0000272FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
273 bool computeIfMissing)
274{
275 FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func];
276 assert (funcInfo != NULL || computeIfMissing);
Chris Lattner21108082002-11-06 19:07:13 +0000277 if (funcInfo == NULL)
Chris Lattnerfc928242002-11-06 18:38:18 +0000278 { // Create a new FunctionModRefInfo object
Chris Lattner21108082002-11-06 19:07:13 +0000279 funcInfo = new FunctionModRefInfo(func, *this, // inserts into map
Chris Lattnerfc928242002-11-06 18:38:18 +0000280 getAnalysis<TDDataStructures>().getDSGraph(func),
281 getAnalysis<LocalDataStructures>().getDSGraph(func));
282 funcInfo->computeModRef(func); // computes the mod/ref info
283 }
284 return *funcInfo;
285}
286
Chris Lattnered8e6492002-11-07 07:12:23 +0000287/// getBUDSGraph - This method returns the BU data structure graph for F through
288/// the use of the BUDataStructures object.
289///
290const DSGraph &IPModRef::getBUDSGraph(const Function &F) {
291 return getAnalysis<BUDataStructures>().getDSGraph(F);
292}
293
294
Chris Lattnerfc928242002-11-06 18:38:18 +0000295// getAnalysisUsage - This pass requires top-down data structure graphs.
296// It modifies nothing.
297//
298void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
299 AU.setPreservesAll();
300 AU.addRequired<LocalDataStructures>();
Chris Lattner21108082002-11-06 19:07:13 +0000301 AU.addRequired<BUDataStructures>();
Chris Lattnerfc928242002-11-06 18:38:18 +0000302 AU.addRequired<TDDataStructures>();
303}
304
305
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000306void IPModRef::print(std::ostream &O) const
307{
308 O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
309
310 for (std::map<const Function*, FunctionModRefInfo*>::const_iterator
311 mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end();
312 mapI != mapE; ++mapI)
313 mapI->second->print(O);
314
315 O << "\n";
316}
317
318
319void IPModRef::dump() const
320{
321 print(std::cerr);
322}