blob: d1c96261dccac49c3d048570e902a7fde2ca3c2c [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 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" &&
36 FI.getName() != "sprintf" && FI.getName() != "fputs")
37 return false; // External function found...
38 }
39 return true; // otherwise ok
40}
41
42struct CallSiteIterator {
43 // FCs are the edges out of the current node are the call site targets...
44 std::vector<DSCallSite> *FCs;
45 unsigned CallSite;
46 unsigned CallSiteEntry;
47
48 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
49 CallSite = 0; CallSiteEntry = 0;
50 advanceToNextValid();
51 }
52
53 // End iterator ctor...
54 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
55 CallSite = FCs->size(); CallSiteEntry = 0;
56 }
57
58 void advanceToNextValid() {
59 while (CallSite < FCs->size()) {
60 if (DSNode *CalleeNode = (*FCs)[CallSite].getCallee().getNode()) {
61 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
62 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
63
64 if (CallSiteEntry < Callees.size())
65 return;
66 }
67 CallSiteEntry = 0;
68 ++CallSite;
69 }
70 }
71 }
72public:
73 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
74 static CallSiteIterator end(DSGraph &G) {
75 return CallSiteIterator(G.getAuxFunctionCalls(), true);
76 }
77 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
78 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
79 return CallSiteIterator(CSs, true);
80 }
81 bool operator==(const CallSiteIterator &CSI) const {
82 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
83 }
84 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
85
86 unsigned getCallSiteIdx() const { return CallSite; }
87 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
88
89 Function* operator*() const {
90 DSNode *Node = (*FCs)[CallSite].getCallee().getNode();
91 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
92 }
93
94 CallSiteIterator& operator++() { // Preincrement
95 ++CallSiteEntry;
96 advanceToNextValid();
97 return *this;
98 }
99 CallSiteIterator operator++(int) { // Postincrement
100 CallSiteIterator tmp = *this; ++*this; return tmp;
101 }
102};
103
104
105
Chris Lattneraa0b4682002-11-09 21:12:07 +0000106// run - Calculate the bottom up data structure graphs for each function in the
107// program.
108//
109bool BUDataStructures::run(Module &M) {
110 GlobalsGraph = new DSGraph();
111
Chris Lattnera9c9c022002-11-11 21:35:13 +0000112 Function *MainFunc = M.getMainFunction();
113 if (MainFunc)
114 calculateReachableGraphs(MainFunc);
115
116 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000117 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +0000118 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000119#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000120 if (MainFunc)
121 std::cerr << "*** Function unreachable from main: "
122 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000123#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000124 calculateReachableGraphs(I); // Calculate all graphs...
125 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000126 return false;
127}
Chris Lattner55c10582002-10-03 20:38:41 +0000128
Chris Lattnera9c9c022002-11-11 21:35:13 +0000129void BUDataStructures::calculateReachableGraphs(Function *F) {
130 std::vector<Function*> Stack;
131 std::map<Function*, unsigned> ValMap;
132 unsigned NextID = 1;
133 calculateGraphs(F, Stack, NextID, ValMap);
134}
135
136DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
137 // Has the graph already been created?
138 DSGraph *&Graph = DSInfo[F];
139 if (Graph) return *Graph;
140
141 // Copy the local version into DSInfo...
142 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
143
144 Graph->setGlobalsGraph(GlobalsGraph);
145 Graph->setPrintAuxCalls();
146
147 // Start with a copy of the original call sites...
148 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
149 return *Graph;
150}
151
152unsigned BUDataStructures::calculateGraphs(Function *F,
153 std::vector<Function*> &Stack,
154 unsigned &NextID,
155 std::map<Function*, unsigned> &ValMap) {
156 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
157 unsigned Min = NextID++, MyID = Min;
158 ValMap[F] = Min;
159 Stack.push_back(F);
160
161 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
162 // No callees!
163 Stack.pop_back();
164 ValMap[F] = ~0;
165 return Min;
166 }
167
168 DSGraph &Graph = getOrCreateGraph(F);
169
170 // The edges out of the current node are the call site targets...
171 for (CallSiteIterator I = CallSiteIterator::begin(Graph),
172 E = CallSiteIterator::end(Graph); I != E; ++I) {
173 Function *Callee = *I;
174 unsigned M;
175 // Have we visited the destination function yet?
176 std::map<Function*, unsigned>::iterator It = ValMap.find(Callee);
177 if (It == ValMap.end()) // No, visit it now.
178 M = calculateGraphs(Callee, Stack, NextID, ValMap);
179 else // Yes, get it's number.
180 M = It->second;
181 if (M < Min) Min = M;
182 }
183
184 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
185 if (Min != MyID)
186 return Min; // This is part of a larger SCC!
187
188 // If this is a new SCC, process it now.
189 if (Stack.back() == F) { // Special case the single "SCC" case here.
190 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
191 << F->getName() << "\n");
192 Stack.pop_back();
193 DSGraph &G = calculateGraph(*F);
194
Chris Lattnerae5f6032002-11-17 22:16:28 +0000195 if (MaxSCC < 1) MaxSCC = 1;
196
Chris Lattnera9c9c022002-11-11 21:35:13 +0000197 // Should we revisit the graph?
198 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
199 ValMap.erase(F);
200 return calculateGraphs(F, Stack, NextID, ValMap);
201 } else {
202 ValMap[F] = ~0U;
203 }
204 return MyID;
205
206 } else {
207 // SCCFunctions - Keep track of the functions in the current SCC
208 //
209 std::set<Function*> SCCFunctions;
210
211 Function *NF;
212 std::vector<Function*>::iterator FirstInSCC = Stack.end();
213 do {
214 NF = *--FirstInSCC;
215 ValMap[NF] = ~0U;
216 SCCFunctions.insert(NF);
217 } while (NF != F);
218
219 std::cerr << "Identified SCC #: " << MyID << " of size: "
220 << (Stack.end()-FirstInSCC) << "\n";
221
Chris Lattnerae5f6032002-11-17 22:16:28 +0000222 // Compute the Max SCC Size...
223 if (MaxSCC < unsigned(Stack.end()-FirstInSCC))
224 MaxSCC = Stack.end()-FirstInSCC;
225
Chris Lattnera9c9c022002-11-11 21:35:13 +0000226 std::vector<Function*>::iterator I = Stack.end();
227 do {
228 --I;
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000229#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000230 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
231 << (Stack.end()-FirstInSCC) << " in SCC: "
232 << (*I)->getName());
233 DSGraph &G = getDSGraph(**I);
234 std::cerr << " [" << G.getGraphSize() << "+"
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000235 << G.getAuxFunctionCalls().size() << "] ";
236#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000237
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000238 // Eliminate all call sites in the SCC that are not to functions that are
239 // in the SCC.
240 inlineNonSCCGraphs(**I, SCCFunctions);
241
242#ifndef NDEBUG
243 std::cerr << "after Non-SCC's [" << G.getGraphSize() << "+"
244 << G.getAuxFunctionCalls().size() << "]\n";
245#endif
246 } while (I != FirstInSCC);
247
248 I = Stack.end();
249 do {
250 --I;
251#ifndef NDEBUG
252 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
253 << (Stack.end()-FirstInSCC) << " in SCC: "
254 << (*I)->getName());
255 DSGraph &G = getDSGraph(**I);
256 std::cerr << " [" << G.getGraphSize() << "+"
257 << G.getAuxFunctionCalls().size() << "] ";
258#endif
259 // Inline all graphs into the SCC nodes...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000260 calculateSCCGraph(**I, SCCFunctions);
261
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000262#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000263 std::cerr << "after [" << G.getGraphSize() << "+"
264 << G.getAuxFunctionCalls().size() << "]\n";
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000265#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000266 } while (I != FirstInSCC);
267
268
269 std::cerr << "DONE with SCC #: " << MyID << "\n";
270
271 // We never have to revisit "SCC" processed functions...
272
273 // Drop the stuff we don't need from the end of the stack
274 Stack.erase(FirstInSCC, Stack.end());
275 return MyID;
276 }
277
278 return MyID; // == Min
279}
280
281
Chris Lattner0d9bab82002-07-18 00:12:30 +0000282// releaseMemory - If the pass pipeline is done with this pass, we can release
283// our memory... here...
284//
285void BUDataStructures::releaseMemory() {
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000286 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +0000287 E = DSInfo.end(); I != E; ++I)
288 delete I->second;
289
290 // Empty map so next time memory is released, data structures are not
291 // re-deleted.
292 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000293 delete GlobalsGraph;
294 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000295}
296
Chris Lattnera9c9c022002-11-11 21:35:13 +0000297DSGraph &BUDataStructures::calculateGraph(Function &F) {
298 DSGraph &Graph = getDSGraph(F);
299 DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n");
Chris Lattner8a5db462002-11-11 00:01:34 +0000300
Chris Lattnera9c9c022002-11-11 21:35:13 +0000301 // Move our call site list into TempFCs so that inline call sites go into the
302 // new call site list and doesn't invalidate our iterators!
303 std::vector<DSCallSite> TempFCs;
304 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
305 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000306
Chris Lattnera9c9c022002-11-11 21:35:13 +0000307 // Loop over all of the resolvable call sites
308 unsigned LastCallSiteIdx = ~0U;
309 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
310 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
311 // If we skipped over any call sites, they must be unresolvable, copy them
312 // to the real call site list.
313 LastCallSiteIdx++;
314 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
315 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
316 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000317
Chris Lattnera9c9c022002-11-11 21:35:13 +0000318 // Resolve the current call...
319 Function *Callee = *I;
320 DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000321
Chris Lattnera9c9c022002-11-11 21:35:13 +0000322 if (Callee->isExternal()) {
323 // Ignore this case, simple varargs functions we cannot stub out!
324 } else if (Callee == &F) {
325 // Self recursion... simply link up the formal arguments with the
326 // actual arguments...
327 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
328
329 // Handle self recursion by resolving the arguments and return value
330 Graph.mergeInGraph(CS, Graph, 0);
331
332 } else {
333 // Get the data structure graph for the called function.
334 //
335 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
336
337 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
338 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
339 << GI.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000340
341#if 0
342 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_before_" +
343 Callee->getName());
344#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000345
346 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000347 Graph.mergeInGraph(CS, GI,
348 DSGraph::KeepModRefBits |
349 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerae5f6032002-11-17 22:16:28 +0000350
351#if 0
352 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
353 Callee->getName());
354#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000355 }
356 }
357
358 // Make sure to catch any leftover unresolvable calls...
359 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
360 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
361
362 TempFCs.clear();
363
364 // Recompute the Incomplete markers. If there are any function calls left
365 // now that are complete, we must loop!
366 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000367 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
368 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000369
370 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
Chris Lattner8a5db462002-11-11 00:01:34 +0000371 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
Chris Lattner221c9792002-08-07 21:41:11 +0000372 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000373
Chris Lattnera9c9c022002-11-11 21:35:13 +0000374 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
375
376 return Graph;
377}
378
379
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000380// inlineNonSCCGraphs - This method is almost like the other two calculate graph
381// methods. This one is used to inline function graphs (from functions outside
382// of the SCC) into functions in the SCC. It is not supposed to touch functions
383// IN the SCC at all.
384//
385DSGraph &BUDataStructures::inlineNonSCCGraphs(Function &F,
386 std::set<Function*> &SCCFunctions){
387 DSGraph &Graph = getDSGraph(F);
388 DEBUG(std::cerr << " [BU] Inlining Non-SCC graphs for: "
389 << F.getName() << "\n");
390
391 // Move our call site list into TempFCs so that inline call sites go into the
392 // new call site list and doesn't invalidate our iterators!
393 std::vector<DSCallSite> TempFCs;
394 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
395 TempFCs.swap(AuxCallsList);
396
397 // Loop over all of the resolvable call sites
398 unsigned LastCallSiteIdx = ~0U;
399 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
400 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
401 // If we skipped over any call sites, they must be unresolvable, copy them
402 // to the real call site list.
403 LastCallSiteIdx++;
404 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
405 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
406 LastCallSiteIdx = I.getCallSiteIdx();
407
408 // Resolve the current call...
409 Function *Callee = *I;
410 DSCallSite &CS = I.getCallSite();
411
412 if (Callee->isExternal()) {
413 // Ignore this case, simple varargs functions we cannot stub out!
414 } else if (SCCFunctions.count(Callee)) {
415 // Calling a function in the SCC, ignore it for now!
416 DEBUG(std::cerr << " SCC CallSite for: " << Callee->getName() << "\n");
417 AuxCallsList.push_back(CS);
418 } else {
419 // Get the data structure graph for the called function.
420 //
421 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
422
423 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
424 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
425 << GI.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000426
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000427 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000428 Graph.mergeInGraph(CS, GI,
429 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000430 DSGraph::DontCloneCallNodes);
431 }
432 }
433
434 // Make sure to catch any leftover unresolvable calls...
435 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
436 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
437
438 TempFCs.clear();
439
440 // Recompute the Incomplete markers. If there are any function calls left
441 // now that are complete, we must loop!
442 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000443 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
444 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000445
446 DEBUG(std::cerr << " [BU] Done Non-SCC inlining: " << F.getName() << " ["
447 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
448 << "]\n");
449
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000450 return Graph;
451}
452
453
Chris Lattnera9c9c022002-11-11 21:35:13 +0000454DSGraph &BUDataStructures::calculateSCCGraph(Function &F,
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000455 std::set<Function*> &SCCFunctions){
Chris Lattnera9c9c022002-11-11 21:35:13 +0000456 DSGraph &Graph = getDSGraph(F);
457 DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n");
458
459 std::vector<DSCallSite> UnresolvableCalls;
460 std::map<Function*, DSCallSite> SCCCallSiteMap;
461 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
462
463 while (1) { // Loop until we run out of resolvable call sites!
464 // Move our call site list into TempFCs so that inline call sites go into
465 // the new call site list and doesn't invalidate our iterators!
466 std::vector<DSCallSite> TempFCs;
467 TempFCs.swap(AuxCallsList);
468
469 // Loop over all of the resolvable call sites
470 unsigned LastCallSiteIdx = ~0U;
471 CallSiteIterator I = CallSiteIterator::begin(TempFCs),
472 E = CallSiteIterator::end(TempFCs);
473 if (I == E) {
474 TempFCs.swap(AuxCallsList);
475 break; // Done when no resolvable call sites exist
476 }
477
478 for (; I != E; ++I) {
479 // If we skipped over any call sites, they must be unresolvable, copy them
480 // to the unresolvable site list.
481 LastCallSiteIdx++;
482 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
483 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
484 LastCallSiteIdx = I.getCallSiteIdx();
485
486 // Resolve the current call...
487 Function *Callee = *I;
488 DSCallSite &CS = I.getCallSite();
489
490 if (Callee->isExternal()) {
491 // Ignore this case, simple varargs functions we cannot stub out!
492 } else if (Callee == &F) {
493 // Self recursion... simply link up the formal arguments with the
494 // actual arguments...
495 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
496
497 // Handle self recursion by resolving the arguments and return value
498 Graph.mergeInGraph(CS, Graph, 0);
499 } else if (SCCCallSiteMap.count(Callee)) {
500 // We have already seen a call site in the SCC for this function, just
501 // merge the two call sites together and we are done.
502 SCCCallSiteMap.find(Callee)->second.mergeWith(CS);
503 } else {
504 // Get the data structure graph for the called function.
505 //
506 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
507
508 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
509 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
510 << GI.getAuxFunctionCalls().size() << "]\n");
511
512 // Handle self recursion by resolving the arguments and return value
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000513 Graph.mergeInGraph(CS, GI,
514 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattnera9c9c022002-11-11 21:35:13 +0000515 DSGraph::DontCloneCallNodes);
516
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000517 if (SCCFunctions.count(Callee))
Chris Lattnera9c9c022002-11-11 21:35:13 +0000518 SCCCallSiteMap.insert(std::make_pair(Callee, CS));
519 }
520 }
521
522 // Make sure to catch any leftover unresolvable calls...
523 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
524 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
525 }
526
527 // Reset the SCCCallSiteMap...
528 SCCCallSiteMap.clear();
529
530 AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(),
531 UnresolvableCalls.end());
532 UnresolvableCalls.clear();
533
534
535 // Recompute the Incomplete markers. If there are any function calls left
536 // now that are complete, we must loop!
537 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000538 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
539 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000540
541 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
542 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
543 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000544 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera1079052002-11-10 06:52:47 +0000545
Chris Lattner8a5db462002-11-11 00:01:34 +0000546 return Graph;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000547}