blob: e096dde249165e43652cfa0b6dc84dd41777a5d4 [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00002//
3// This file implements the BUDataStructures class, which represents the
4// Bottom-Up Interprocedural closure of the data structure graph over the
5// program. This is useful for applications like pool allocation, but **not**
Chris Lattner55c10582002-10-03 20:38:41 +00006// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
Chris Lattner55c10582002-10-03 20:38:41 +000011#include "llvm/Analysis/DSGraph.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000012#include "llvm/Module.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000014using std::map;
15
Chris Lattner1e435162002-07-26 21:12:44 +000016static RegisterAnalysis<BUDataStructures>
Vikram S. Adve355e2ca2002-07-30 22:05:22 +000017X("budatastructure", "Bottom-up Data Structure Analysis Closure");
Chris Lattner0d9bab82002-07-18 00:12:30 +000018
Chris Lattnerb1060432002-11-07 05:20:53 +000019using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000020
Chris Lattnera9c9c022002-11-11 21:35:13 +000021// isCompleteNode - Return true if we know all of the targets of this node, and
22// if the call sites are not external.
23//
24static inline bool isCompleteNode(DSNode *N) {
25 if (N->NodeType & DSNode::Incomplete) return false;
26 const std::vector<GlobalValue*> &Callees = N->getGlobals();
27 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
28 if (Callees[i]->isExternal()) {
29 GlobalValue &FI = cast<Function>(*Callees[i]);
30 if (FI.getName() != "printf" && FI.getName() != "sscanf" &&
31 FI.getName() != "fprintf" && FI.getName() != "open" &&
32 FI.getName() != "sprintf" && FI.getName() != "fputs")
33 return false; // External function found...
34 }
35 return true; // otherwise ok
36}
37
38struct CallSiteIterator {
39 // FCs are the edges out of the current node are the call site targets...
40 std::vector<DSCallSite> *FCs;
41 unsigned CallSite;
42 unsigned CallSiteEntry;
43
44 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
45 CallSite = 0; CallSiteEntry = 0;
46 advanceToNextValid();
47 }
48
49 // End iterator ctor...
50 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
51 CallSite = FCs->size(); CallSiteEntry = 0;
52 }
53
54 void advanceToNextValid() {
55 while (CallSite < FCs->size()) {
56 if (DSNode *CalleeNode = (*FCs)[CallSite].getCallee().getNode()) {
57 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
58 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
59
60 if (CallSiteEntry < Callees.size())
61 return;
62 }
63 CallSiteEntry = 0;
64 ++CallSite;
65 }
66 }
67 }
68public:
69 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
70 static CallSiteIterator end(DSGraph &G) {
71 return CallSiteIterator(G.getAuxFunctionCalls(), true);
72 }
73 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
74 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
75 return CallSiteIterator(CSs, true);
76 }
77 bool operator==(const CallSiteIterator &CSI) const {
78 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
79 }
80 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
81
82 unsigned getCallSiteIdx() const { return CallSite; }
83 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
84
85 Function* operator*() const {
86 DSNode *Node = (*FCs)[CallSite].getCallee().getNode();
87 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
88 }
89
90 CallSiteIterator& operator++() { // Preincrement
91 ++CallSiteEntry;
92 advanceToNextValid();
93 return *this;
94 }
95 CallSiteIterator operator++(int) { // Postincrement
96 CallSiteIterator tmp = *this; ++*this; return tmp;
97 }
98};
99
100
101
Chris Lattneraa0b4682002-11-09 21:12:07 +0000102// run - Calculate the bottom up data structure graphs for each function in the
103// program.
104//
105bool BUDataStructures::run(Module &M) {
106 GlobalsGraph = new DSGraph();
107
Chris Lattnera9c9c022002-11-11 21:35:13 +0000108 Function *MainFunc = M.getMainFunction();
109 if (MainFunc)
110 calculateReachableGraphs(MainFunc);
111
112 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000113 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +0000114 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
115 if (MainFunc)
116 std::cerr << "*** Function unreachable from main: "
117 << I->getName() << "\n";
118 calculateReachableGraphs(I); // Calculate all graphs...
119 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000120 return false;
121}
Chris Lattner55c10582002-10-03 20:38:41 +0000122
Chris Lattnera9c9c022002-11-11 21:35:13 +0000123void BUDataStructures::calculateReachableGraphs(Function *F) {
124 std::vector<Function*> Stack;
125 std::map<Function*, unsigned> ValMap;
126 unsigned NextID = 1;
127 calculateGraphs(F, Stack, NextID, ValMap);
128}
129
130DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
131 // Has the graph already been created?
132 DSGraph *&Graph = DSInfo[F];
133 if (Graph) return *Graph;
134
135 // Copy the local version into DSInfo...
136 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
137
138 Graph->setGlobalsGraph(GlobalsGraph);
139 Graph->setPrintAuxCalls();
140
141 // Start with a copy of the original call sites...
142 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
143 return *Graph;
144}
145
146unsigned BUDataStructures::calculateGraphs(Function *F,
147 std::vector<Function*> &Stack,
148 unsigned &NextID,
149 std::map<Function*, unsigned> &ValMap) {
150 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
151 unsigned Min = NextID++, MyID = Min;
152 ValMap[F] = Min;
153 Stack.push_back(F);
154
155 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
156 // No callees!
157 Stack.pop_back();
158 ValMap[F] = ~0;
159 return Min;
160 }
161
162 DSGraph &Graph = getOrCreateGraph(F);
163
164 // The edges out of the current node are the call site targets...
165 for (CallSiteIterator I = CallSiteIterator::begin(Graph),
166 E = CallSiteIterator::end(Graph); I != E; ++I) {
167 Function *Callee = *I;
168 unsigned M;
169 // Have we visited the destination function yet?
170 std::map<Function*, unsigned>::iterator It = ValMap.find(Callee);
171 if (It == ValMap.end()) // No, visit it now.
172 M = calculateGraphs(Callee, Stack, NextID, ValMap);
173 else // Yes, get it's number.
174 M = It->second;
175 if (M < Min) Min = M;
176 }
177
178 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
179 if (Min != MyID)
180 return Min; // This is part of a larger SCC!
181
182 // If this is a new SCC, process it now.
183 if (Stack.back() == F) { // Special case the single "SCC" case here.
184 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
185 << F->getName() << "\n");
186 Stack.pop_back();
187 DSGraph &G = calculateGraph(*F);
188
189 // Should we revisit the graph?
190 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
191 ValMap.erase(F);
192 return calculateGraphs(F, Stack, NextID, ValMap);
193 } else {
194 ValMap[F] = ~0U;
195 }
196 return MyID;
197
198 } else {
199 // SCCFunctions - Keep track of the functions in the current SCC
200 //
201 std::set<Function*> SCCFunctions;
202
203 Function *NF;
204 std::vector<Function*>::iterator FirstInSCC = Stack.end();
205 do {
206 NF = *--FirstInSCC;
207 ValMap[NF] = ~0U;
208 SCCFunctions.insert(NF);
209 } while (NF != F);
210
211 std::cerr << "Identified SCC #: " << MyID << " of size: "
212 << (Stack.end()-FirstInSCC) << "\n";
213
214 std::vector<Function*>::iterator I = Stack.end();
215 do {
216 --I;
217 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
218 << (Stack.end()-FirstInSCC) << " in SCC: "
219 << (*I)->getName());
220 DSGraph &G = getDSGraph(**I);
221 std::cerr << " [" << G.getGraphSize() << "+"
222 << G.getAuxFunctionCalls().size() << "] " << std::flush;
223
224 // Inline all graphs into the last (highest numbered) node in the SCC
225 calculateSCCGraph(**I, SCCFunctions);
226
227 std::cerr << "after [" << G.getGraphSize() << "+"
228 << G.getAuxFunctionCalls().size() << "]\n";
229 } while (I != FirstInSCC);
230
231
232 std::cerr << "DONE with SCC #: " << MyID << "\n";
233
234 // We never have to revisit "SCC" processed functions...
235
236 // Drop the stuff we don't need from the end of the stack
237 Stack.erase(FirstInSCC, Stack.end());
238 return MyID;
239 }
240
241 return MyID; // == Min
242}
243
244
Chris Lattner0d9bab82002-07-18 00:12:30 +0000245// releaseMemory - If the pass pipeline is done with this pass, we can release
246// our memory... here...
247//
248void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000249 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +0000250 E = DSInfo.end(); I != E; ++I)
251 delete I->second;
252
253 // Empty map so next time memory is released, data structures are not
254 // re-deleted.
255 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000256 delete GlobalsGraph;
257 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000258}
259
Chris Lattnera9c9c022002-11-11 21:35:13 +0000260DSGraph &BUDataStructures::calculateGraph(Function &F) {
261 DSGraph &Graph = getDSGraph(F);
262 DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n");
Chris Lattner8a5db462002-11-11 00:01:34 +0000263
Chris Lattnera9c9c022002-11-11 21:35:13 +0000264 // Move our call site list into TempFCs so that inline call sites go into the
265 // new call site list and doesn't invalidate our iterators!
266 std::vector<DSCallSite> TempFCs;
267 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
268 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000269
Chris Lattnera9c9c022002-11-11 21:35:13 +0000270 // Loop over all of the resolvable call sites
271 unsigned LastCallSiteIdx = ~0U;
272 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
273 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
274 // If we skipped over any call sites, they must be unresolvable, copy them
275 // to the real call site list.
276 LastCallSiteIdx++;
277 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
278 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
279 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000280
Chris Lattnera9c9c022002-11-11 21:35:13 +0000281 // Resolve the current call...
282 Function *Callee = *I;
283 DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000284
Chris Lattnera9c9c022002-11-11 21:35:13 +0000285 if (Callee->isExternal()) {
286 // Ignore this case, simple varargs functions we cannot stub out!
287 } else if (Callee == &F) {
288 // Self recursion... simply link up the formal arguments with the
289 // actual arguments...
290 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
291
292 // Handle self recursion by resolving the arguments and return value
293 Graph.mergeInGraph(CS, Graph, 0);
294
295 } else {
296 // Get the data structure graph for the called function.
297 //
298 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
299
300 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
301 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
302 << GI.getAuxFunctionCalls().size() << "]\n");
303
304 // Handle self recursion by resolving the arguments and return value
305 Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit |
306 DSGraph::DontCloneCallNodes);
307 }
308 }
309
310 // Make sure to catch any leftover unresolvable calls...
311 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
312 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
313
314 TempFCs.clear();
315
316 // Recompute the Incomplete markers. If there are any function calls left
317 // now that are complete, we must loop!
318 Graph.maskIncompleteMarkers();
319 Graph.markIncompleteNodes();
320 Graph.removeDeadNodes();
321
322 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
Chris Lattner8a5db462002-11-11 00:01:34 +0000323 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
Chris Lattner221c9792002-08-07 21:41:11 +0000324 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000325
Chris Lattnera9c9c022002-11-11 21:35:13 +0000326 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
327
328 return Graph;
329}
330
331
332DSGraph &BUDataStructures::calculateSCCGraph(Function &F,
333 std::set<Function*> &InlinedSCCFunctions) {
334 DSGraph &Graph = getDSGraph(F);
335 DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n");
336
337 std::vector<DSCallSite> UnresolvableCalls;
338 std::map<Function*, DSCallSite> SCCCallSiteMap;
339 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
340
341 while (1) { // Loop until we run out of resolvable call sites!
342 // Move our call site list into TempFCs so that inline call sites go into
343 // the new call site list and doesn't invalidate our iterators!
344 std::vector<DSCallSite> TempFCs;
345 TempFCs.swap(AuxCallsList);
346
347 // Loop over all of the resolvable call sites
348 unsigned LastCallSiteIdx = ~0U;
349 CallSiteIterator I = CallSiteIterator::begin(TempFCs),
350 E = CallSiteIterator::end(TempFCs);
351 if (I == E) {
352 TempFCs.swap(AuxCallsList);
353 break; // Done when no resolvable call sites exist
354 }
355
356 for (; I != E; ++I) {
357 // If we skipped over any call sites, they must be unresolvable, copy them
358 // to the unresolvable site list.
359 LastCallSiteIdx++;
360 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
361 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
362 LastCallSiteIdx = I.getCallSiteIdx();
363
364 // Resolve the current call...
365 Function *Callee = *I;
366 DSCallSite &CS = I.getCallSite();
367
368 if (Callee->isExternal()) {
369 // Ignore this case, simple varargs functions we cannot stub out!
370 } else if (Callee == &F) {
371 // Self recursion... simply link up the formal arguments with the
372 // actual arguments...
373 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
374
375 // Handle self recursion by resolving the arguments and return value
376 Graph.mergeInGraph(CS, Graph, 0);
377 } else if (SCCCallSiteMap.count(Callee)) {
378 // We have already seen a call site in the SCC for this function, just
379 // merge the two call sites together and we are done.
380 SCCCallSiteMap.find(Callee)->second.mergeWith(CS);
381 } else {
382 // Get the data structure graph for the called function.
383 //
384 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
385
386 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
387 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
388 << GI.getAuxFunctionCalls().size() << "]\n");
389
390 // Handle self recursion by resolving the arguments and return value
391 Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit |
392 DSGraph::DontCloneCallNodes);
393
394 if (InlinedSCCFunctions.count(Callee))
395 SCCCallSiteMap.insert(std::make_pair(Callee, CS));
396 }
397 }
398
399 // Make sure to catch any leftover unresolvable calls...
400 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
401 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
402 }
403
404 // Reset the SCCCallSiteMap...
405 SCCCallSiteMap.clear();
406
407 AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(),
408 UnresolvableCalls.end());
409 UnresolvableCalls.clear();
410
411
412 // Recompute the Incomplete markers. If there are any function calls left
413 // now that are complete, we must loop!
414 Graph.maskIncompleteMarkers();
415 Graph.markIncompleteNodes();
416 Graph.removeDeadNodes();
417
418 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
419 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
420 << "]\n");
421
422 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera1079052002-11-10 06:52:47 +0000423
Chris Lattner8a5db462002-11-11 00:01:34 +0000424 return Graph;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000425}