blob: 591cefb15b9dd54aa228cd705ffda520e1ccc42b [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>
Chris Lattner312edd32003-06-28 22:14:55 +000020 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000021}
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 Lattner923fc052003-02-05 21:59:58 +000025static bool isVAHackFn(const Function *F) {
26 return F->getName() == "printf" || F->getName() == "sscanf" ||
27 F->getName() == "fprintf" || F->getName() == "open" ||
28 F->getName() == "sprintf" || F->getName() == "fputs" ||
29 F->getName() == "fscanf";
30}
31
Chris Lattnera9c9c022002-11-11 21:35:13 +000032// isCompleteNode - Return true if we know all of the targets of this node, and
33// if the call sites are not external.
34//
35static inline bool isCompleteNode(DSNode *N) {
Chris Lattnerbd92b732003-06-19 21:15:11 +000036 if (N->isIncomplete()) return false;
Chris Lattnera9c9c022002-11-11 21:35:13 +000037 const std::vector<GlobalValue*> &Callees = N->getGlobals();
38 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
Chris Lattner923fc052003-02-05 21:59:58 +000039 if (Callees[i]->isExternal())
40 if (!isVAHackFn(cast<Function>(Callees[i])))
Chris Lattnera9c9c022002-11-11 21:35:13 +000041 return false; // External function found...
Chris Lattnera9c9c022002-11-11 21:35:13 +000042 return true; // otherwise ok
43}
44
45struct CallSiteIterator {
46 // FCs are the edges out of the current node are the call site targets...
47 std::vector<DSCallSite> *FCs;
48 unsigned CallSite;
49 unsigned CallSiteEntry;
50
51 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
52 CallSite = 0; CallSiteEntry = 0;
Chris Lattner923fc052003-02-05 21:59:58 +000053 advanceToValidCallee();
Chris Lattnera9c9c022002-11-11 21:35:13 +000054 }
55
56 // End iterator ctor...
57 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
58 CallSite = FCs->size(); CallSiteEntry = 0;
59 }
60
Chris Lattner923fc052003-02-05 21:59:58 +000061 void advanceToValidCallee() {
Chris Lattnera9c9c022002-11-11 21:35:13 +000062 while (CallSite < FCs->size()) {
Chris Lattner923fc052003-02-05 21:59:58 +000063 if ((*FCs)[CallSite].isDirectCall()) {
64 if (CallSiteEntry == 0 && // direct call only has one target...
65 (!(*FCs)[CallSite].getCalleeFunc()->isExternal() ||
66 isVAHackFn((*FCs)[CallSite].getCalleeFunc()))) // If not external
67 return;
68 } else {
69 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
Chris Lattnera9c9c022002-11-11 21:35:13 +000070 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
71 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
72
73 if (CallSiteEntry < Callees.size())
74 return;
75 }
Chris Lattnera9c9c022002-11-11 21:35:13 +000076 }
Chris Lattner923fc052003-02-05 21:59:58 +000077 CallSiteEntry = 0;
78 ++CallSite;
Chris Lattnera9c9c022002-11-11 21:35:13 +000079 }
80 }
81public:
82 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
83 static CallSiteIterator end(DSGraph &G) {
84 return CallSiteIterator(G.getAuxFunctionCalls(), true);
85 }
86 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
87 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
88 return CallSiteIterator(CSs, true);
89 }
90 bool operator==(const CallSiteIterator &CSI) const {
91 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
92 }
93 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
94
95 unsigned getCallSiteIdx() const { return CallSite; }
96 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
97
Chris Lattner923fc052003-02-05 21:59:58 +000098 Function *operator*() const {
99 if ((*FCs)[CallSite].isDirectCall()) {
100 return (*FCs)[CallSite].getCalleeFunc();
101 } else {
102 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
103 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
104 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000105 }
106
107 CallSiteIterator& operator++() { // Preincrement
108 ++CallSiteEntry;
Chris Lattner923fc052003-02-05 21:59:58 +0000109 advanceToValidCallee();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000110 return *this;
111 }
112 CallSiteIterator operator++(int) { // Postincrement
113 CallSiteIterator tmp = *this; ++*this; return tmp;
114 }
115};
116
117
118
Chris Lattneraa0b4682002-11-09 21:12:07 +0000119// run - Calculate the bottom up data structure graphs for each function in the
120// program.
121//
122bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +0000123 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
124 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +0000125 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000126
Chris Lattnera9c9c022002-11-11 21:35:13 +0000127 Function *MainFunc = M.getMainFunction();
128 if (MainFunc)
129 calculateReachableGraphs(MainFunc);
130
131 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000132 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +0000133 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000134#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000135 if (MainFunc)
136 std::cerr << "*** Function unreachable from main: "
137 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000138#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000139 calculateReachableGraphs(I); // Calculate all graphs...
140 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000141 return false;
142}
Chris Lattner55c10582002-10-03 20:38:41 +0000143
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144void BUDataStructures::calculateReachableGraphs(Function *F) {
145 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +0000146 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000147 unsigned NextID = 1;
148 calculateGraphs(F, Stack, NextID, ValMap);
149}
150
151DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
152 // Has the graph already been created?
153 DSGraph *&Graph = DSInfo[F];
154 if (Graph) return *Graph;
155
156 // Copy the local version into DSInfo...
157 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
158
159 Graph->setGlobalsGraph(GlobalsGraph);
160 Graph->setPrintAuxCalls();
161
162 // Start with a copy of the original call sites...
163 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
164 return *Graph;
165}
166
167unsigned BUDataStructures::calculateGraphs(Function *F,
168 std::vector<Function*> &Stack,
169 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000170 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000171 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
172 unsigned Min = NextID++, MyID = Min;
173 ValMap[F] = Min;
174 Stack.push_back(F);
175
176 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
177 // No callees!
178 Stack.pop_back();
179 ValMap[F] = ~0;
180 return Min;
181 }
182
183 DSGraph &Graph = getOrCreateGraph(F);
184
185 // The edges out of the current node are the call site targets...
186 for (CallSiteIterator I = CallSiteIterator::begin(Graph),
187 E = CallSiteIterator::end(Graph); I != E; ++I) {
188 Function *Callee = *I;
189 unsigned M;
190 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000191 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000192 if (It == ValMap.end()) // No, visit it now.
193 M = calculateGraphs(Callee, Stack, NextID, ValMap);
194 else // Yes, get it's number.
195 M = It->second;
196 if (M < Min) Min = M;
197 }
198
199 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
200 if (Min != MyID)
201 return Min; // This is part of a larger SCC!
202
203 // If this is a new SCC, process it now.
204 if (Stack.back() == F) { // Special case the single "SCC" case here.
205 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
206 << F->getName() << "\n");
207 Stack.pop_back();
208 DSGraph &G = calculateGraph(*F);
209
Chris Lattnerae5f6032002-11-17 22:16:28 +0000210 if (MaxSCC < 1) MaxSCC = 1;
211
Chris Lattnera9c9c022002-11-11 21:35:13 +0000212 // Should we revisit the graph?
213 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
214 ValMap.erase(F);
215 return calculateGraphs(F, Stack, NextID, ValMap);
216 } else {
217 ValMap[F] = ~0U;
218 }
219 return MyID;
220
221 } else {
222 // SCCFunctions - Keep track of the functions in the current SCC
223 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000224 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000225
226 Function *NF;
227 std::vector<Function*>::iterator FirstInSCC = Stack.end();
228 do {
229 NF = *--FirstInSCC;
230 ValMap[NF] = ~0U;
231 SCCFunctions.insert(NF);
232 } while (NF != F);
233
234 std::cerr << "Identified SCC #: " << MyID << " of size: "
235 << (Stack.end()-FirstInSCC) << "\n";
236
Chris Lattnerae5f6032002-11-17 22:16:28 +0000237 // Compute the Max SCC Size...
238 if (MaxSCC < unsigned(Stack.end()-FirstInSCC))
239 MaxSCC = Stack.end()-FirstInSCC;
240
Chris Lattnera9c9c022002-11-11 21:35:13 +0000241 std::vector<Function*>::iterator I = Stack.end();
242 do {
243 --I;
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000244#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000245 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
246 << (Stack.end()-FirstInSCC) << " in SCC: "
247 << (*I)->getName());
248 DSGraph &G = getDSGraph(**I);
249 std::cerr << " [" << G.getGraphSize() << "+"
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000250 << G.getAuxFunctionCalls().size() << "] ";
251#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000252
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000253 // Eliminate all call sites in the SCC that are not to functions that are
254 // in the SCC.
255 inlineNonSCCGraphs(**I, SCCFunctions);
256
257#ifndef NDEBUG
258 std::cerr << "after Non-SCC's [" << G.getGraphSize() << "+"
259 << G.getAuxFunctionCalls().size() << "]\n";
260#endif
261 } while (I != FirstInSCC);
262
263 I = Stack.end();
264 do {
265 --I;
266#ifndef NDEBUG
267 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
268 << (Stack.end()-FirstInSCC) << " in SCC: "
269 << (*I)->getName());
270 DSGraph &G = getDSGraph(**I);
271 std::cerr << " [" << G.getGraphSize() << "+"
272 << G.getAuxFunctionCalls().size() << "] ";
273#endif
274 // Inline all graphs into the SCC nodes...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000275 calculateSCCGraph(**I, SCCFunctions);
276
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000277#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000278 std::cerr << "after [" << G.getGraphSize() << "+"
279 << G.getAuxFunctionCalls().size() << "]\n";
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000280#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000281 } while (I != FirstInSCC);
282
283
284 std::cerr << "DONE with SCC #: " << MyID << "\n";
285
286 // We never have to revisit "SCC" processed functions...
287
288 // Drop the stuff we don't need from the end of the stack
289 Stack.erase(FirstInSCC, Stack.end());
290 return MyID;
291 }
292
293 return MyID; // == Min
294}
295
296
Chris Lattner0d9bab82002-07-18 00:12:30 +0000297// releaseMemory - If the pass pipeline is done with this pass, we can release
298// our memory... here...
299//
300void BUDataStructures::releaseMemory() {
Chris Lattner41c04f72003-02-01 04:52:08 +0000301 for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
Chris Lattner0d9bab82002-07-18 00:12:30 +0000302 E = DSInfo.end(); I != E; ++I)
303 delete I->second;
304
305 // Empty map so next time memory is released, data structures are not
306 // re-deleted.
307 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000308 delete GlobalsGraph;
309 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000310}
311
Chris Lattnera9c9c022002-11-11 21:35:13 +0000312DSGraph &BUDataStructures::calculateGraph(Function &F) {
313 DSGraph &Graph = getDSGraph(F);
314 DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n");
Chris Lattner8a5db462002-11-11 00:01:34 +0000315
Chris Lattnera9c9c022002-11-11 21:35:13 +0000316 // Move our call site list into TempFCs so that inline call sites go into the
317 // new call site list and doesn't invalidate our iterators!
318 std::vector<DSCallSite> TempFCs;
319 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
320 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000321
Chris Lattnera9c9c022002-11-11 21:35:13 +0000322 // Loop over all of the resolvable call sites
323 unsigned LastCallSiteIdx = ~0U;
324 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
325 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
326 // If we skipped over any call sites, they must be unresolvable, copy them
327 // to the real call site list.
328 LastCallSiteIdx++;
329 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
330 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
331 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000332
Chris Lattnera9c9c022002-11-11 21:35:13 +0000333 // Resolve the current call...
334 Function *Callee = *I;
335 DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000336
Chris Lattnera9c9c022002-11-11 21:35:13 +0000337 if (Callee->isExternal()) {
338 // Ignore this case, simple varargs functions we cannot stub out!
339 } else if (Callee == &F) {
340 // Self recursion... simply link up the formal arguments with the
341 // actual arguments...
342 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
343
344 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000345 Graph.mergeInGraph(CS, F, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000346
347 } else {
348 // Get the data structure graph for the called function.
349 //
350 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
351
352 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000353 << "[" << GI.getGraphSize() << "+"
354 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
355 << "[" << Graph.getGraphSize() << "+"
356 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000357#if 0
358 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_before_" +
359 Callee->getName());
360#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000361
362 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000363 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000364 DSGraph::KeepModRefBits |
365 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerae5f6032002-11-17 22:16:28 +0000366
367#if 0
368 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
369 Callee->getName());
370#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000371 }
372 }
373
374 // Make sure to catch any leftover unresolvable calls...
375 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
376 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
377
378 TempFCs.clear();
379
380 // Recompute the Incomplete markers. If there are any function calls left
381 // now that are complete, we must loop!
382 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000383 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000384 // FIXME: materialize nodes from the globals graph as neccesary...
Chris Lattner394471f2003-01-23 22:05:33 +0000385 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000386
387 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
Chris Lattner8a5db462002-11-11 00:01:34 +0000388 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
Chris Lattner221c9792002-08-07 21:41:11 +0000389 << "]\n");
Vikram S. Adve355e2ca2002-07-30 22:05:22 +0000390
Chris Lattnera9c9c022002-11-11 21:35:13 +0000391 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
392
393 return Graph;
394}
395
396
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000397// inlineNonSCCGraphs - This method is almost like the other two calculate graph
398// methods. This one is used to inline function graphs (from functions outside
399// of the SCC) into functions in the SCC. It is not supposed to touch functions
400// IN the SCC at all.
401//
402DSGraph &BUDataStructures::inlineNonSCCGraphs(Function &F,
Chris Lattner41c04f72003-02-01 04:52:08 +0000403 hash_set<Function*> &SCCFunctions){
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000404 DSGraph &Graph = getDSGraph(F);
405 DEBUG(std::cerr << " [BU] Inlining Non-SCC graphs for: "
406 << F.getName() << "\n");
407
408 // Move our call site list into TempFCs so that inline call sites go into the
409 // new call site list and doesn't invalidate our iterators!
410 std::vector<DSCallSite> TempFCs;
411 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
412 TempFCs.swap(AuxCallsList);
413
414 // Loop over all of the resolvable call sites
415 unsigned LastCallSiteIdx = ~0U;
416 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
417 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
418 // If we skipped over any call sites, they must be unresolvable, copy them
419 // to the real call site list.
420 LastCallSiteIdx++;
421 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
422 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
423 LastCallSiteIdx = I.getCallSiteIdx();
424
425 // Resolve the current call...
Chris Lattner5a540632003-06-30 03:15:25 +0000426 Function &Callee = **I;
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000427 DSCallSite &CS = I.getCallSite();
428
Chris Lattner5a540632003-06-30 03:15:25 +0000429 if (Callee.isExternal()) {
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000430 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner5a540632003-06-30 03:15:25 +0000431 } else if (SCCFunctions.count(&Callee)) {
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000432 // Calling a function in the SCC, ignore it for now!
Chris Lattner5a540632003-06-30 03:15:25 +0000433 DEBUG(std::cerr << " SCC CallSite for: " << Callee.getName() << "\n");
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000434 AuxCallsList.push_back(CS);
435 } else {
436 // Get the data structure graph for the called function.
437 //
Chris Lattner5a540632003-06-30 03:15:25 +0000438 DSGraph &GI = getDSGraph(Callee); // Graph to inline
Chris Lattner20167e32003-02-03 19:11:38 +0000439
Chris Lattner5a540632003-06-30 03:15:25 +0000440 DEBUG(std::cerr << " Inlining graph for " << Callee.getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000441 << "[" << GI.getGraphSize() << "+"
442 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
443 << "[" << Graph.getGraphSize() << "+"
444 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000445
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000446 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000447 Graph.mergeInGraph(CS, Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000448 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000449 DSGraph::DontCloneCallNodes);
450 }
451 }
452
453 // Make sure to catch any leftover unresolvable calls...
454 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
455 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
456
457 TempFCs.clear();
458
459 // Recompute the Incomplete markers. If there are any function calls left
460 // now that are complete, we must loop!
461 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000462 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
463 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000464
465 DEBUG(std::cerr << " [BU] Done Non-SCC inlining: " << F.getName() << " ["
466 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
467 << "]\n");
Chris Lattner20167e32003-02-03 19:11:38 +0000468 //Graph.writeGraphToFile(std::cerr, "nscc_" + F.getName());
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000469 return Graph;
470}
471
472
Chris Lattnera9c9c022002-11-11 21:35:13 +0000473DSGraph &BUDataStructures::calculateSCCGraph(Function &F,
Chris Lattner41c04f72003-02-01 04:52:08 +0000474 hash_set<Function*> &SCCFunctions){
Chris Lattnera9c9c022002-11-11 21:35:13 +0000475 DSGraph &Graph = getDSGraph(F);
476 DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n");
477
478 std::vector<DSCallSite> UnresolvableCalls;
Chris Lattner41c04f72003-02-01 04:52:08 +0000479 hash_map<Function*, DSCallSite> SCCCallSiteMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000480 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
481
482 while (1) { // Loop until we run out of resolvable call sites!
483 // Move our call site list into TempFCs so that inline call sites go into
484 // the new call site list and doesn't invalidate our iterators!
485 std::vector<DSCallSite> TempFCs;
486 TempFCs.swap(AuxCallsList);
Chris Lattner20167e32003-02-03 19:11:38 +0000487
Chris Lattnera9c9c022002-11-11 21:35:13 +0000488 // Loop over all of the resolvable call sites
489 unsigned LastCallSiteIdx = ~0U;
490 CallSiteIterator I = CallSiteIterator::begin(TempFCs),
491 E = CallSiteIterator::end(TempFCs);
492 if (I == E) {
493 TempFCs.swap(AuxCallsList);
494 break; // Done when no resolvable call sites exist
495 }
496
497 for (; I != E; ++I) {
498 // If we skipped over any call sites, they must be unresolvable, copy them
499 // to the unresolvable site list.
500 LastCallSiteIdx++;
501 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
502 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
503 LastCallSiteIdx = I.getCallSiteIdx();
504
505 // Resolve the current call...
506 Function *Callee = *I;
507 DSCallSite &CS = I.getCallSite();
508
509 if (Callee->isExternal()) {
510 // Ignore this case, simple varargs functions we cannot stub out!
511 } else if (Callee == &F) {
512 // Self recursion... simply link up the formal arguments with the
513 // actual arguments...
514 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
515
516 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000517 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000518 } else if (SCCCallSiteMap.count(Callee)) {
519 // We have already seen a call site in the SCC for this function, just
520 // merge the two call sites together and we are done.
521 SCCCallSiteMap.find(Callee)->second.mergeWith(CS);
522 } else {
523 // Get the data structure graph for the called function.
524 //
525 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
Chris Lattnera9c9c022002-11-11 21:35:13 +0000526 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000527 << "[" << GI.getGraphSize() << "+"
528 << GI.getAuxFunctionCalls().size() << "] into: " << F.getName()
529 << "[" << Graph.getGraphSize() << "+"
530 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000531
532 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000533 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000534 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
Chris Lattnera9c9c022002-11-11 21:35:13 +0000535 DSGraph::DontCloneCallNodes);
536
Chris Lattner5f1f2c62002-11-12 15:58:08 +0000537 if (SCCFunctions.count(Callee))
Chris Lattnera9c9c022002-11-11 21:35:13 +0000538 SCCCallSiteMap.insert(std::make_pair(Callee, CS));
539 }
540 }
541
542 // Make sure to catch any leftover unresolvable calls...
543 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
544 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
545 }
546
547 // Reset the SCCCallSiteMap...
548 SCCCallSiteMap.clear();
549
550 AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(),
551 UnresolvableCalls.end());
552 UnresolvableCalls.clear();
553
554
555 // Recompute the Incomplete markers. If there are any function calls left
556 // now that are complete, we must loop!
557 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000558 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000559
560 // FIXME: materialize nodes from the globals graph as neccesary...
561
Chris Lattner394471f2003-01-23 22:05:33 +0000562 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000563
564 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
565 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
566 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000567 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattner8a5db462002-11-11 00:01:34 +0000568 return Graph;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000569}