blob: c32f4425cfa1f606e26f6baf41b569b13485a3ef [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
77// Dummy function that will be replaced with one that inlines
78// the callee's BU graph into the caller's TD graph.
79//
Chris Lattner21108082002-11-06 19:07:13 +000080static const DSGraph* ResolveGraphForCallSite(const DSGraph& funcTDGraph,
Vikram S. Adve895c0bd2002-11-06 17:02:03 +000081 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//
91void 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//
117void
118FunctionModRefInfo::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//
145void 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
164void 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//
177void 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//
192bool 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
Chris Lattnerfc928242002-11-06 18:38:18 +0000202FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
203 bool computeIfMissing)
204{
205 FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func];
206 assert (funcInfo != NULL || computeIfMissing);
Chris Lattner21108082002-11-06 19:07:13 +0000207 if (funcInfo == NULL)
Chris Lattnerfc928242002-11-06 18:38:18 +0000208 { // Create a new FunctionModRefInfo object
Chris Lattner21108082002-11-06 19:07:13 +0000209 funcInfo = new FunctionModRefInfo(func, *this, // inserts into map
Chris Lattnerfc928242002-11-06 18:38:18 +0000210 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//
220void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
221 AU.setPreservesAll();
222 AU.addRequired<LocalDataStructures>();
Chris Lattner21108082002-11-06 19:07:13 +0000223 AU.addRequired<BUDataStructures>();
Chris Lattnerfc928242002-11-06 18:38:18 +0000224 AU.addRequired<TDDataStructures>();
225}
226
227
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000228void 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
241void IPModRef::dump() const
242{
243 print(std::cerr);
244}