blob: 1d3397578e10459b260bc72c53ba7c55818bef2c [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 Lattner41c04f72003-02-01 04:52:08 +000014#include "Support/hash_map"
Chris Lattner0d9bab82002-07-18 00:12:30 +000015
Chris Lattnerae5f6032002-11-17 22:16:28 +000016namespace {
17 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
18
19 RegisterAnalysis<BUDataStructures>
20 X("budatastructure", "Bottom-up Data Structure Analysis Closure");
21}
Chris Lattner0d9bab82002-07-18 00:12:30 +000022
Chris Lattnerb1060432002-11-07 05:20:53 +000023using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000024
Chris Lattnera9c9c022002-11-11 21:35:13 +000025// isCompleteNode - Return true if we know all of the targets of this node, and
26// if the call sites are not external.
27//
28static inline bool isCompleteNode(DSNode *N) {
29 if (N->NodeType & DSNode::Incomplete) return false;
30 const std::vector<GlobalValue*> &Callees = N->getGlobals();
31 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
32 if (Callees[i]->isExternal()) {
33 GlobalValue &FI = cast<Function>(*Callees[i]);
34 if (FI.getName() != "printf" && FI.getName() != "sscanf" &&
35 FI.getName() != "fprintf" && FI.getName() != "open" &&
Chris Lattner11d71ed2003-01-31 23:57:10 +000036 FI.getName() != "sprintf" && FI.getName() != "fputs" &&
37 FI.getName() != "fscanf")
Chris Lattnera9c9c022002-11-11 21:35:13 +000038 return false; // External function found...
39 }
40 return true; // otherwise ok
41}
42
43struct CallSiteIterator {
44 // FCs are the edges out of the current node are the call site targets...
45 std::vector<DSCallSite> *FCs;
46 unsigned CallSite;
47 unsigned CallSiteEntry;
48
49 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
50 CallSite = 0; CallSiteEntry = 0;
51 advanceToNextValid();
52 }
53
54 // End iterator ctor...
55 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
56 CallSite = FCs->size(); CallSiteEntry = 0;
57 }
58
59 void advanceToNextValid() {
60 while (CallSite < FCs->size()) {
61 if (DSNode *CalleeNode = (*FCs)[CallSite].getCallee().getNode()) {
62 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
63 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
64
65 if (CallSiteEntry < Callees.size())
66 return;
67 }
68 CallSiteEntry = 0;
69 ++CallSite;
70 }
71 }
72 }
73public:
74 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
75 static CallSiteIterator end(DSGraph &G) {
76 return CallSiteIterator(G.getAuxFunctionCalls(), true);
77 }
78 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
79 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
80 return CallSiteIterator(CSs, true);
81 }
82 bool operator==(const CallSiteIterator &CSI) const {
83 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
84 }
85 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
86
87 unsigned getCallSiteIdx() const { return CallSite; }
88 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
89
90 Function* operator*() const {
91 DSNode *Node = (*FCs)[CallSite].getCallee().getNode();
92 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
93 }
94
95 CallSiteIterator& operator++() { // Preincrement
96 ++CallSiteEntry;
97 advanceToNextValid();
98 return *this;
99 }
100 CallSiteIterator operator++(int) { // Postincrement
101 CallSiteIterator tmp = *this; ++*this; return tmp;
102 }
103};
104
105
106
Chris Lattneraa0b4682002-11-09 21:12:07 +0000107// run - Calculate the bottom up data structure graphs for each function in the
108// program.
109//
110bool BUDataStructures::run(Module &M) {
111 GlobalsGraph = new DSGraph();
Chris Lattner20167e32003-02-03 19:11:38 +0000112 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000113
Chris Lattnera9c9c022002-11-11 21:35:13 +0000114 Function *MainFunc = M.getMainFunction();
115 if (MainFunc)
116 calculateReachableGraphs(MainFunc);
117
118 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000119 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +0000120 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000121#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000122 if (MainFunc)
123 std::cerr << "*** Function unreachable from main: "
124 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000125#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000126 calculateReachableGraphs(I); // Calculate all graphs...
127 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000128 return false;
129}
Chris Lattner55c10582002-10-03 20:38:41 +0000130
Chris Lattnera9c9c022002-11-11 21:35:13 +0000131void BUDataStructures::calculateReachableGraphs(Function *F) {
132 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +0000133 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000134 unsigned NextID = 1;
135 calculateGraphs(F, Stack, NextID, ValMap);
136}
137
138DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
139 // Has the graph already been created?
140 DSGraph *&Graph = DSInfo[F];
141 if (Graph) return *Graph;
142
143 // Copy the local version into DSInfo...
144 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
145
146 Graph->setGlobalsGraph(GlobalsGraph);
147 Graph->setPrintAuxCalls();
148
149 // Start with a copy of the original call sites...
150 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
151 return *Graph;
152}
153
154unsigned BUDataStructures::calculateGraphs(Function *F,
155 std::vector<Function*> &Stack,
156 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000157 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000158 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
159 unsigned Min = NextID++, MyID = Min;
160 ValMap[F] = Min;
161 Stack.push_back(F);
162
163 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
164 // No callees!
165 Stack.pop_back();
166 ValMap[F] = ~0;
167 return Min;
168 }
169
170 DSGraph &Graph = getOrCreateGraph(F);
171
172 // The edges out of the current node are the call site targets...
173 for (CallSiteIterator I = CallSiteIterator::begin(Graph),
174 E = CallSiteIterator::end(Graph); I != E; ++I) {
175 Function *Callee = *I;
176 unsigned M;
177 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000178 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000179 if (It == ValMap.end()) // No, visit it now.
180 M = calculateGraphs(Callee, Stack, NextID, ValMap);
181 else // Yes, get it's number.
182 M = It->second;
183 if (M < Min) Min = M;
184 }
185
186 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
187 if (Min != MyID)
188 return Min; // This is part of a larger SCC!
189
190 // If this is a new SCC, process it now.
191 if (Stack.back() == F) { // Special case the single "SCC" case here.
192 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
193 << F->getName() << "\n");
194 Stack.pop_back();
195 DSGraph &G = calculateGraph(*F);
196
Chris Lattnerae5f6032002-11-17 22:16:28 +0000197 if (MaxSCC < 1) MaxSCC = 1;
198
Chris Lattnera9c9c022002-11-11 21:35:13 +0000199 // Should we revisit the graph?
200 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
201 ValMap.erase(F);
202 return calculateGraphs(F, Stack, NextID, ValMap);
203 } else {
204 ValMap[F] = ~0U;
205 }
206 return MyID;
207
208 } else {
209 // SCCFunctions - Keep track of the functions in the current SCC
210 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000211 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000212
213 Function *NF;
214 std::vector<Function*>::iterator FirstInSCC = Stack.end();
215 do {
216 NF = *--FirstInSCC;
217 ValMap[NF] = ~0U;
218 SCCFunctions.insert(NF);
219 } while (NF != F);
220
221 std::cerr << "Identified SCC #: " << MyID << " of size: "
222 << (Stack.end()-FirstInSCC) << "\n";
223
Chris Lattnerae5f6032002-11-17 22:16:28 +0000224 // Compute the Max SCC Size...
225 if (MaxSCC < unsigned(Stack.end()-FirstInSCC))
226 MaxSCC = Stack.end()-FirstInSCC;
227
Chris Lattnera9c9c022002-11-11 21:35:13 +0000228 std::vector<Function*>::iterator I = Stack.end();
229 do {
230 --I;
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000231#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000232 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
233 << (Stack.end()-FirstInSCC) << " in SCC: "
234 << (*I)->getName());
235 DSGraph &G = getDSGraph(**I);
236 std::cerr << " [" << G.getGraphSize() << "+"
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000237 << G.getAuxFunctionCalls().size() << "] ";
238#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000239
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000240 // Eliminate all call sites in the SCC that are not to functions that are
241 // in the SCC.
242 inlineNonSCCGraphs(**I, SCCFunctions);
243
244#ifndef NDEBUG
245 std::cerr << "after Non-SCC's [" << G.getGraphSize() << "+"
246 << G.getAuxFunctionCalls().size() << "]\n";
247#endif
248 } while (I != FirstInSCC);
249
250 I = Stack.end();
251 do {
252 --I;
253#ifndef NDEBUG
254 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
255 << (Stack.end()-FirstInSCC) << " in SCC: "
256 << (*I)->getName());
257 DSGraph &G = getDSGraph(**I);
258 std::cerr << " [" << G.getGraphSize() << "+"
259 << G.getAuxFunctionCalls().size() << "] ";
260#endif
261 // Inline all graphs into the SCC nodes...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000262 calculateSCCGraph(**I, SCCFunctions);
263
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000264#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000265 std::cerr << "after [" << G.getGraphSize() << "+"
266 << G.getAuxFunctionCalls().size() << "]\n";
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000267#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000268 } while (I != FirstInSCC);
269
270
271 std::cerr << "DONE with SCC #: " << MyID << "\n";
272
273 // We never have to revisit "SCC" processed functions...
274
275 // Drop the stuff we don't need from the end of the stack
276 Stack.erase(FirstInSCC, Stack.end());
277 return MyID;
278 }
279
280 return MyID; // == Min
281}
282
283
Chris Lattner0d9bab82002-07-18 00:12:30 +0000284// releaseMemory - If the pass pipeline is done with this pass, we can release
285// our memory... here...
286//
287void BUDataStructures::releaseMemory() {
Chris Lattner41c04f72003-02-01 04:52:08 +0000288 for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +0000289 E = DSInfo.end(); I != E; ++I)
290 delete I->second;
291
292 // Empty map so next time memory is released, data structures are not
293 // re-deleted.
294 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000295 delete GlobalsGraph;
296 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000297}
298
Chris Lattnera9c9c022002-11-11 21:35:13 +0000299DSGraph &BUDataStructures::calculateGraph(Function &F) {
300 DSGraph &Graph = getDSGraph(F);
301 DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n");
Chris Lattner8a5db462002-11-11 00:01:34 +0000302
Chris Lattnera9c9c022002-11-11 21:35:13 +0000303 // Move our call site list into TempFCs so that inline call sites go into the
304 // new call site list and doesn't invalidate our iterators!
305 std::vector<DSCallSite> TempFCs;
306 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
307 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000308
Chris Lattnera9c9c022002-11-11 21:35:13 +0000309 // Loop over all of the resolvable call sites
310 unsigned LastCallSiteIdx = ~0U;
311 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
312 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
313 // If we skipped over any call sites, they must be unresolvable, copy them
314 // to the real call site list.
315 LastCallSiteIdx++;
316 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
317 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
318 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000319
Chris Lattnera9c9c022002-11-11 21:35:13 +0000320 // Resolve the current call...
321 Function *Callee = *I;
322 DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000323
Chris Lattnera9c9c022002-11-11 21:35:13 +0000324 if (Callee->isExternal()) {
325 // Ignore this case, simple varargs functions we cannot stub out!
326 } else if (Callee == &F) {
327 // Self recursion... simply link up the formal arguments with the
328 // actual arguments...
329 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
330
331 // Handle self recursion by resolving the arguments and return value
332 Graph.mergeInGraph(CS, Graph, 0);
333
334 } else {
335 // Get the data structure graph for the called function.
336 //
337 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
338
339 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000340 << "[" << GI.getGraphSize() << "+"
341 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
342 << "[" << Graph.getGraphSize() << "+"
343 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000344#if 0
345 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_before_" +
346 Callee->getName());
347#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000348
349 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000350 Graph.mergeInGraph(CS, GI,
351 DSGraph::KeepModRefBits |
352 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerae5f6032002-11-17 22:16:28 +0000353
354#if 0
355 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
356 Callee->getName());
357#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000358 }
359 }
360
361 // Make sure to catch any leftover unresolvable calls...
362 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
363 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
364
365 TempFCs.clear();
366
367 // Recompute the Incomplete markers. If there are any function calls left
368 // now that are complete, we must loop!
369 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000370 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000371 // FIXME: materialize nodes from the globals graph as neccesary...
Chris Lattner394471f2003-01-23 22:05:33 +0000372 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000373
374 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
Chris Lattner8a5db462002-11-11 00:01:34 +0000375 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
Chris Lattner221c9792002-08-07 21:41:11 +0000376 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000377
Chris Lattnera9c9c022002-11-11 21:35:13 +0000378 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
379
380 return Graph;
381}
382
383
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000384// inlineNonSCCGraphs - This method is almost like the other two calculate graph
385// methods. This one is used to inline function graphs (from functions outside
386// of the SCC) into functions in the SCC. It is not supposed to touch functions
387// IN the SCC at all.
388//
389DSGraph &BUDataStructures::inlineNonSCCGraphs(Function &F,
Chris Lattner41c04f72003-02-01 04:52:08 +0000390 hash_set<Function*> &SCCFunctions){
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000391 DSGraph &Graph = getDSGraph(F);
392 DEBUG(std::cerr << " [BU] Inlining Non-SCC graphs for: "
393 << F.getName() << "\n");
394
395 // Move our call site list into TempFCs so that inline call sites go into the
396 // new call site list and doesn't invalidate our iterators!
397 std::vector<DSCallSite> TempFCs;
398 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
399 TempFCs.swap(AuxCallsList);
400
401 // Loop over all of the resolvable call sites
402 unsigned LastCallSiteIdx = ~0U;
403 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
404 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
405 // If we skipped over any call sites, they must be unresolvable, copy them
406 // to the real call site list.
407 LastCallSiteIdx++;
408 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
409 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
410 LastCallSiteIdx = I.getCallSiteIdx();
411
412 // Resolve the current call...
413 Function *Callee = *I;
414 DSCallSite &CS = I.getCallSite();
415
416 if (Callee->isExternal()) {
417 // Ignore this case, simple varargs functions we cannot stub out!
418 } else if (SCCFunctions.count(Callee)) {
419 // Calling a function in the SCC, ignore it for now!
420 DEBUG(std::cerr << " SCC CallSite for: " << Callee->getName() << "\n");
421 AuxCallsList.push_back(CS);
422 } else {
423 // Get the data structure graph for the called function.
424 //
425 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
Chris Lattner20167e32003-02-03 19:11:38 +0000426
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000427 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000428 << "[" << GI.getGraphSize() << "+"
429 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
430 << "[" << Graph.getGraphSize() << "+"
431 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000432
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000433 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000434 Graph.mergeInGraph(CS, GI,
435 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000436 DSGraph::DontCloneCallNodes);
437 }
438 }
439
440 // Make sure to catch any leftover unresolvable calls...
441 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
442 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
443
444 TempFCs.clear();
445
446 // Recompute the Incomplete markers. If there are any function calls left
447 // now that are complete, we must loop!
448 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000449 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
450 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000451
452 DEBUG(std::cerr << " [BU] Done Non-SCC inlining: " << F.getName() << " ["
453 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
454 << "]\n");
Chris Lattner20167e32003-02-03 19:11:38 +0000455 //Graph.writeGraphToFile(std::cerr, "nscc_" + F.getName());
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000456 return Graph;
457}
458
459
Chris Lattnera9c9c022002-11-11 21:35:13 +0000460DSGraph &BUDataStructures::calculateSCCGraph(Function &F,
Chris Lattner41c04f72003-02-01 04:52:08 +0000461 hash_set<Function*> &SCCFunctions){
Chris Lattnera9c9c022002-11-11 21:35:13 +0000462 DSGraph &Graph = getDSGraph(F);
463 DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n");
464
465 std::vector<DSCallSite> UnresolvableCalls;
Chris Lattner41c04f72003-02-01 04:52:08 +0000466 hash_map<Function*, DSCallSite> SCCCallSiteMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000467 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
468
469 while (1) { // Loop until we run out of resolvable call sites!
470 // Move our call site list into TempFCs so that inline call sites go into
471 // the new call site list and doesn't invalidate our iterators!
472 std::vector<DSCallSite> TempFCs;
473 TempFCs.swap(AuxCallsList);
Chris Lattner20167e32003-02-03 19:11:38 +0000474
Chris Lattnera9c9c022002-11-11 21:35:13 +0000475 // Loop over all of the resolvable call sites
476 unsigned LastCallSiteIdx = ~0U;
477 CallSiteIterator I = CallSiteIterator::begin(TempFCs),
478 E = CallSiteIterator::end(TempFCs);
479 if (I == E) {
480 TempFCs.swap(AuxCallsList);
481 break; // Done when no resolvable call sites exist
482 }
483
484 for (; I != E; ++I) {
485 // If we skipped over any call sites, they must be unresolvable, copy them
486 // to the unresolvable site list.
487 LastCallSiteIdx++;
488 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
489 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
490 LastCallSiteIdx = I.getCallSiteIdx();
491
492 // Resolve the current call...
493 Function *Callee = *I;
494 DSCallSite &CS = I.getCallSite();
495
496 if (Callee->isExternal()) {
497 // Ignore this case, simple varargs functions we cannot stub out!
498 } else if (Callee == &F) {
499 // Self recursion... simply link up the formal arguments with the
500 // actual arguments...
501 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
502
503 // Handle self recursion by resolving the arguments and return value
504 Graph.mergeInGraph(CS, Graph, 0);
505 } else if (SCCCallSiteMap.count(Callee)) {
506 // We have already seen a call site in the SCC for this function, just
507 // merge the two call sites together and we are done.
508 SCCCallSiteMap.find(Callee)->second.mergeWith(CS);
509 } else {
510 // Get the data structure graph for the called function.
511 //
512 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
Chris Lattnera9c9c022002-11-11 21:35:13 +0000513 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000514 << "[" << GI.getGraphSize() << "+"
515 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
516 << "[" << Graph.getGraphSize() << "+"
517 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000518
519 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000520 Graph.mergeInGraph(CS, GI,
521 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattnera9c9c022002-11-11 21:35:13 +0000522 DSGraph::DontCloneCallNodes);
523
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000524 if (SCCFunctions.count(Callee))
Chris Lattnera9c9c022002-11-11 21:35:13 +0000525 SCCCallSiteMap.insert(std::make_pair(Callee, CS));
526 }
527 }
528
529 // Make sure to catch any leftover unresolvable calls...
530 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
531 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
532 }
533
534 // Reset the SCCCallSiteMap...
535 SCCCallSiteMap.clear();
536
537 AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(),
538 UnresolvableCalls.end());
539 UnresolvableCalls.clear();
540
541
542 // Recompute the Incomplete markers. If there are any function calls left
543 // now that are complete, we must loop!
544 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000545 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000546
547 // FIXME: materialize nodes from the globals graph as neccesary...
548
Chris Lattner394471f2003-01-23 22:05:33 +0000549 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000550
551 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
552 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
553 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000554 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattner8a5db462002-11-11 00:01:34 +0000555 return Graph;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000556}