blob: 9b33a3c27c8b7c9ea72e33a7cbd318a4afef517c [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).
107// 4. It returns the clone.
108//
109// NOTE: Because this clones a dsgraph and returns it, the caller is responsible
110// for deleting the returned graph!
111//
112DSGraph *FunctionModRefInfo::ResolveCallSiteModRefInfo(const CallInst &CI) {
113 // Step #1: Clone the top-down graph...
114 DSGraph *Result = new DSGraph(funcTDGraph);
115
116 //const Function &F = *CI.getParent()->getParent();
117 //DSGraph &TDGraph = IPModRefObj.getAnalysis<TDDataStructures>().getDSGraph(F);
118
119
120 return Result;
121}
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000122
123// Compute Mod/Ref bit vectors for a single call site.
124// These are copies of the Read/Write flags from the nodes of
125// the graph produced by clearing all flags in teh caller's TD graph
126// and then inlining the callee's BU graph into the caller's TD graph.
127//
128void
129FunctionModRefInfo::computeModRef(const CallInst& callInst)
130{
131 // Allocate the mod/ref info for the call site. Bits automatically cleared.
132 ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
133 callSiteModRefInfo[&callInst] = callModRefInfo;
134
135 // Get a copy of the graph for the callee with the callee inlined
Chris Lattner268748a2002-11-06 19:38:43 +0000136 DSGraph* csgp = ResolveCallSiteModRefInfo(callInst);
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000137
138 // For all nodes in the graph, extract the mod/ref information
139 const std::vector<DSNode*>& csgNodes = csgp->getNodes();
140 const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes();
141 assert(csgNodes.size() == origNodes.size());
142 for (unsigned i=0, N = csgNodes.size(); i < N; ++i)
143 {
144 if (csgNodes[i]->isModified())
145 callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
146 if (csgNodes[i]->isRead())
147 callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
148 }
Chris Lattner268748a2002-11-06 19:38:43 +0000149 delete csgp;
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000150}
151
152
153// Print the results of the pass.
154// Currently this just prints bit-vectors and is not very readable.
155//
156void FunctionModRefInfo::print(std::ostream &O) const
157{
158 O << "---------- Mod/ref information for function "
159 << F.getName() << "---------- \n\n";
160
161 O << "Mod/ref info for function body:\n";
162 funcModRefInfo.print(O);
163
164 for (std::map<const CallInst*, ModRefInfo*>::const_iterator
165 CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
166 CI != CE; ++CI)
167 {
168 O << "Mod/ref info for call site " << CI->first << ":\n";
169 CI->second->print(O);
170 }
171
172 O << "\n";
173}
174
175void FunctionModRefInfo::dump() const
176{
177 print(std::cerr);
178}
179
180
181//----------------------------------------------------------------------------
182// class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
183//----------------------------------------------------------------------------
184
185
186// Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
187//
188void IPModRef::releaseMemory()
189{
190 for(std::map<const Function*, FunctionModRefInfo*>::iterator
191 I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
192 delete(I->second);
193
194 // Clear map so memory is not re-released if we are called again
195 funcToModRefInfoMap.clear();
196}
197
198
199// Run the "interprocedural" pass on each function. This needs to do
200// NO real interprocedural work because all that has been done the
201// data structure analysis.
202//
203bool IPModRef::run(Module &theModule)
204{
205 M = &theModule;
206 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
207 if (! FI->isExternal())
208 getFuncInfo(*FI, /*computeIfMissing*/ true);
209 return true;
210}
211
212
Chris Lattnerfc928242002-11-06 18:38:18 +0000213FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
214 bool computeIfMissing)
215{
216 FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func];
217 assert (funcInfo != NULL || computeIfMissing);
Chris Lattner21108082002-11-06 19:07:13 +0000218 if (funcInfo == NULL)
Chris Lattnerfc928242002-11-06 18:38:18 +0000219 { // Create a new FunctionModRefInfo object
Chris Lattner21108082002-11-06 19:07:13 +0000220 funcInfo = new FunctionModRefInfo(func, *this, // inserts into map
Chris Lattnerfc928242002-11-06 18:38:18 +0000221 getAnalysis<TDDataStructures>().getDSGraph(func),
222 getAnalysis<LocalDataStructures>().getDSGraph(func));
223 funcInfo->computeModRef(func); // computes the mod/ref info
224 }
225 return *funcInfo;
226}
227
228// getAnalysisUsage - This pass requires top-down data structure graphs.
229// It modifies nothing.
230//
231void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
232 AU.setPreservesAll();
233 AU.addRequired<LocalDataStructures>();
Chris Lattner21108082002-11-06 19:07:13 +0000234 AU.addRequired<BUDataStructures>();
Chris Lattnerfc928242002-11-06 18:38:18 +0000235 AU.addRequired<TDDataStructures>();
236}
237
238
Vikram S. Adve895c0bd2002-11-06 17:02:03 +0000239void IPModRef::print(std::ostream &O) const
240{
241 O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
242
243 for (std::map<const Function*, FunctionModRefInfo*>::const_iterator
244 mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end();
245 mapI != mapE; ++mapI)
246 mapI->second->print(O);
247
248 O << "\n";
249}
250
251
252void IPModRef::dump() const
253{
254 print(std::cerr);
255}