1 //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===//
3 // See high-level comments in include/llvm/Analysis/IPModRef.h
5 //===----------------------------------------------------------------------===//
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"
16 //----------------------------------------------------------------------------
17 // Private constants and data
18 //----------------------------------------------------------------------------
20 static RegisterAnalysis<IPModRef>
21 Z("ipmodref", "Interprocedural mod/ref analysis");
24 //----------------------------------------------------------------------------
26 //----------------------------------------------------------------------------
28 void ModRefInfo::print(std::ostream &O) const
30 O << std::endl << "Modified nodes = " << modNodeSet;
31 O << "Referenced nodes = " << refNodeSet << std::endl;
34 void ModRefInfo::dump() const
39 //----------------------------------------------------------------------------
40 // class FunctionModRefInfo
41 //----------------------------------------------------------------------------
44 // This constructor computes a node numbering for the TD graph.
46 FunctionModRefInfo::FunctionModRefInfo(const Function& func,
50 : F(func), IPModRefObj(ipmro),
53 funcModRefInfo(tdg.getGraphSize())
55 for (unsigned i=0, N = funcTDGraph.getGraphSize(); i < N; ++i)
56 NodeIds[funcTDGraph.getNodes()[i]] = i;
60 FunctionModRefInfo::~FunctionModRefInfo()
62 for(std::map<const CallInst*, ModRefInfo*>::iterator
63 I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I)
66 // Empty map just to make problems easier to track down
67 callSiteModRefInfo.clear();
70 unsigned FunctionModRefInfo::getNodeId(const Value* value) const {
71 return getNodeId(funcTDGraph.getNodeForValue(const_cast<Value*>(value))
77 // Dummy function that will be replaced with one that inlines
78 // the callee's BU graph into the caller's TD graph.
80 static const DSGraph* ResolveGraphForCallSite(const DSGraph& funcTDGraph,
81 const CallInst& callInst)
83 return &funcTDGraph; // TEMPORARY
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.
91 void FunctionModRefInfo::computeModRef(const Function &func)
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)
97 if (funcTDGraph.getNodes()[i]->isModified())
98 funcModRefInfo.setNodeIsMod(i);
99 if (funcTDGraph.getNodes()[i]->isRead())
100 funcModRefInfo.setNodeIsRef(i);
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
106 const std::vector<DSCallSite>& callSites = funcLocalGraph.getFunctionCalls();
107 for (unsigned i = 0, N = callSites.size(); i < N; ++i)
108 computeModRef(callSites[i].getCallInst());
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.
118 FunctionModRefInfo::computeModRef(const CallInst& callInst)
120 // Allocate the mod/ref info for the call site. Bits automatically cleared.
121 ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
122 callSiteModRefInfo[&callInst] = callModRefInfo;
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");
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)
134 if (csgNodes[i]->isModified())
135 callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
136 if (csgNodes[i]->isRead())
137 callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
142 // Print the results of the pass.
143 // Currently this just prints bit-vectors and is not very readable.
145 void FunctionModRefInfo::print(std::ostream &O) const
147 O << "---------- Mod/ref information for function "
148 << F.getName() << "---------- \n\n";
150 O << "Mod/ref info for function body:\n";
151 funcModRefInfo.print(O);
153 for (std::map<const CallInst*, ModRefInfo*>::const_iterator
154 CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
157 O << "Mod/ref info for call site " << CI->first << ":\n";
158 CI->second->print(O);
164 void FunctionModRefInfo::dump() const
170 //----------------------------------------------------------------------------
171 // class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
172 //----------------------------------------------------------------------------
175 // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
177 void IPModRef::releaseMemory()
179 for(std::map<const Function*, FunctionModRefInfo*>::iterator
180 I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
183 // Clear map so memory is not re-released if we are called again
184 funcToModRefInfoMap.clear();
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.
192 bool IPModRef::run(Module &theModule)
195 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
196 if (! FI->isExternal())
197 getFuncInfo(*FI, /*computeIfMissing*/ true);
202 FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
203 bool computeIfMissing)
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
217 // getAnalysisUsage - This pass requires top-down data structure graphs.
218 // It modifies nothing.
220 void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
221 AU.setPreservesAll();
222 AU.addRequired<LocalDataStructures>();
223 AU.addRequired<BUDataStructures>();
224 AU.addRequired<TDDataStructures>();
228 void IPModRef::print(std::ostream &O) const
230 O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
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);
241 void IPModRef::dump() const