Fix problem with dangling referrers
[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 // 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 // 
81 void 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
101 // 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, and a mapping of nodes from the original TDGraph to
108 //     the cloned graph with Mod/Ref info for the callsite.
109 //
110 // NOTE: Because this clones a dsgraph and returns it, the caller is responsible
111 //       for deleting the returned graph!
112 //
113 DSGraph *FunctionModRefInfo::ResolveCallSiteModRefInfo(const CallInst &CI,
114                                std::map<const DSNode*, DSNodeHandle> &NodeMap) {
115
116   // Step #1: Clone the top-down graph...
117   std::map<const DSNode*, DSNode*> RawNodeMap;
118   DSGraph *Result = new DSGraph(funcTDGraph, RawNodeMap);
119
120   // Convert the NodeMap from a map to DSNode* to be a map to DSNodeHandle's
121   NodeMap.insert(RawNodeMap.begin(), RawNodeMap.end());
122
123   // We are now done with the old map... so free it's memory...
124   RawNodeMap.clear();
125
126   // Step #2: Clear Mod/Ref information...
127   Result->maskNodeTypes(~(DSNode::Modified | DSNode::Read));
128
129
130   
131   return Result;
132 }
133
134 // Compute Mod/Ref bit vectors for a single call site.
135 // These are copies of the Read/Write flags from the nodes of
136 // the graph produced by clearing all flags in teh caller's TD graph
137 // and then inlining the callee's BU graph into the caller's TD graph.
138 // 
139 void
140 FunctionModRefInfo::computeModRef(const CallInst& callInst)
141 {
142   // Allocate the mod/ref info for the call site.  Bits automatically cleared.
143   ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize());
144   callSiteModRefInfo[&callInst] = callModRefInfo;
145
146   // Get a copy of the graph for the callee with the callee inlined
147   std::map<const DSNode*, DSNodeHandle> NodeMap;
148   DSGraph* csgp = ResolveCallSiteModRefInfo(callInst, NodeMap);
149
150   // For all nodes in the graph, extract the mod/ref information
151   const std::vector<DSNode*>& csgNodes = csgp->getNodes();
152   const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes();
153   assert(csgNodes.size() == origNodes.size());
154   for (unsigned i=0, N = csgNodes.size(); i < N; ++i)
155     { 
156       if (csgNodes[i]->isModified())
157         callModRefInfo->setNodeIsMod(getNodeId(origNodes[i]));
158       if (csgNodes[i]->isRead())
159         callModRefInfo->setNodeIsRef(getNodeId(origNodes[i]));
160     }
161
162   // Drop nodemap before we delete the graph...
163   NodeMap.clear();
164   delete csgp;
165 }
166
167
168 // Print the results of the pass.
169 // Currently this just prints bit-vectors and is not very readable.
170 // 
171 void FunctionModRefInfo::print(std::ostream &O) const
172 {
173   O << "---------- Mod/ref information for function "
174     << F.getName() << "---------- \n\n";
175
176   O << "Mod/ref info for function body:\n";
177   funcModRefInfo.print(O);
178
179   for (std::map<const CallInst*, ModRefInfo*>::const_iterator
180          CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end();
181        CI != CE; ++CI)
182     {
183       O << "Mod/ref info for call site " << CI->first << ":\n";
184       CI->second->print(O);
185     }
186
187   O << "\n";
188 }
189
190 void FunctionModRefInfo::dump() const
191 {
192   print(std::cerr);
193 }
194
195
196 //----------------------------------------------------------------------------
197 // class IPModRef: An interprocedural pass that computes IP Mod/Ref info.
198 //----------------------------------------------------------------------------
199
200 // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap.
201 // 
202 void IPModRef::releaseMemory()
203 {
204   for(std::map<const Function*, FunctionModRefInfo*>::iterator
205         I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I)
206     delete(I->second);
207
208   // Clear map so memory is not re-released if we are called again
209   funcToModRefInfoMap.clear();
210 }
211
212
213 // Run the "interprocedural" pass on each function.  This needs to do
214 // NO real interprocedural work because all that has been done the
215 // data structure analysis.
216 // 
217 bool IPModRef::run(Module &theModule)
218 {
219   M = &theModule;
220
221   for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
222     if (! FI->isExternal())
223       getFuncInfo(*FI, /*computeIfMissing*/ true);
224   return true;
225 }
226
227
228 FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func,
229                                           bool computeIfMissing)
230 {
231   FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func];
232   assert (funcInfo != NULL || computeIfMissing);
233   if (funcInfo == NULL)
234     { // Create a new FunctionModRefInfo object
235       funcInfo = new FunctionModRefInfo(func, *this, // inserts into map
236                               getAnalysis<TDDataStructures>().getDSGraph(func),
237                           getAnalysis<LocalDataStructures>().getDSGraph(func));
238       funcInfo->computeModRef(func);            // computes the mod/ref info
239     }
240   return *funcInfo;
241 }
242
243 // getAnalysisUsage - This pass requires top-down data structure graphs.
244 // It modifies nothing.
245 // 
246 void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const {
247   AU.setPreservesAll();
248   AU.addRequired<LocalDataStructures>();
249   AU.addRequired<BUDataStructures>();
250   AU.addRequired<TDDataStructures>();
251 }
252
253
254 void IPModRef::print(std::ostream &O) const
255 {
256   O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n";
257   
258   for (std::map<const Function*, FunctionModRefInfo*>::const_iterator
259          mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end();
260        mapI != mapE; ++mapI)
261     mapI->second->print(O);
262
263   O << "\n";
264 }
265
266
267 void IPModRef::dump() const
268 {
269   print(std::cerr);
270 }