1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
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**
6 // applications like alias analysis.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/DataStructure.h"
11 #include "llvm/Analysis/DSGraph.h"
12 #include "llvm/Module.h"
13 #include "Support/Statistic.h"
17 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
19 RegisterAnalysis<BUDataStructures>
20 X("budatastructure", "Bottom-up Data Structure Analysis Closure");
25 // isCompleteNode - Return true if we know all of the targets of this node, and
26 // if the call sites are not external.
28 static 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...
39 return true; // otherwise ok
42 struct CallSiteIterator {
43 // FCs are the edges out of the current node are the call site targets...
44 std::vector<DSCallSite> *FCs;
46 unsigned CallSiteEntry;
48 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
49 CallSite = 0; CallSiteEntry = 0;
53 // End iterator ctor...
54 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
55 CallSite = FCs->size(); CallSiteEntry = 0;
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();
64 if (CallSiteEntry < Callees.size())
73 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
74 static CallSiteIterator end(DSGraph &G) {
75 return CallSiteIterator(G.getAuxFunctionCalls(), true);
77 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
78 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
79 return CallSiteIterator(CSs, true);
81 bool operator==(const CallSiteIterator &CSI) const {
82 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
84 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
86 unsigned getCallSiteIdx() const { return CallSite; }
87 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
89 Function* operator*() const {
90 DSNode *Node = (*FCs)[CallSite].getCallee().getNode();
91 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
94 CallSiteIterator& operator++() { // Preincrement
99 CallSiteIterator operator++(int) { // Postincrement
100 CallSiteIterator tmp = *this; ++*this; return tmp;
106 // run - Calculate the bottom up data structure graphs for each function in the
109 bool BUDataStructures::run(Module &M) {
110 GlobalsGraph = new DSGraph();
112 Function *MainFunc = M.getMainFunction();
114 calculateReachableGraphs(MainFunc);
116 // Calculate the graphs for any functions that are unreachable from main...
117 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
118 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
121 std::cerr << "*** Function unreachable from main: "
122 << I->getName() << "\n";
124 calculateReachableGraphs(I); // Calculate all graphs...
129 void BUDataStructures::calculateReachableGraphs(Function *F) {
130 std::vector<Function*> Stack;
131 std::map<Function*, unsigned> ValMap;
133 calculateGraphs(F, Stack, NextID, ValMap);
136 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
137 // Has the graph already been created?
138 DSGraph *&Graph = DSInfo[F];
139 if (Graph) return *Graph;
141 // Copy the local version into DSInfo...
142 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
144 Graph->setGlobalsGraph(GlobalsGraph);
145 Graph->setPrintAuxCalls();
147 // Start with a copy of the original call sites...
148 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
152 unsigned BUDataStructures::calculateGraphs(Function *F,
153 std::vector<Function*> &Stack,
155 std::map<Function*, unsigned> &ValMap) {
156 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
157 unsigned Min = NextID++, MyID = Min;
161 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
168 DSGraph &Graph = getOrCreateGraph(F);
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;
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.
181 if (M < Min) Min = M;
184 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
186 return Min; // This is part of a larger SCC!
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");
193 DSGraph &G = calculateGraph(*F);
195 if (MaxSCC < 1) MaxSCC = 1;
197 // Should we revisit the graph?
198 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
200 return calculateGraphs(F, Stack, NextID, ValMap);
207 // SCCFunctions - Keep track of the functions in the current SCC
209 std::set<Function*> SCCFunctions;
212 std::vector<Function*>::iterator FirstInSCC = Stack.end();
216 SCCFunctions.insert(NF);
219 std::cerr << "Identified SCC #: " << MyID << " of size: "
220 << (Stack.end()-FirstInSCC) << "\n";
222 // Compute the Max SCC Size...
223 if (MaxSCC < unsigned(Stack.end()-FirstInSCC))
224 MaxSCC = Stack.end()-FirstInSCC;
226 std::vector<Function*>::iterator I = Stack.end();
230 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
231 << (Stack.end()-FirstInSCC) << " in SCC: "
233 DSGraph &G = getDSGraph(**I);
234 std::cerr << " [" << G.getGraphSize() << "+"
235 << G.getAuxFunctionCalls().size() << "] ";
238 // Eliminate all call sites in the SCC that are not to functions that are
240 inlineNonSCCGraphs(**I, SCCFunctions);
243 std::cerr << "after Non-SCC's [" << G.getGraphSize() << "+"
244 << G.getAuxFunctionCalls().size() << "]\n";
246 } while (I != FirstInSCC);
252 /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/"
253 << (Stack.end()-FirstInSCC) << " in SCC: "
255 DSGraph &G = getDSGraph(**I);
256 std::cerr << " [" << G.getGraphSize() << "+"
257 << G.getAuxFunctionCalls().size() << "] ";
259 // Inline all graphs into the SCC nodes...
260 calculateSCCGraph(**I, SCCFunctions);
263 std::cerr << "after [" << G.getGraphSize() << "+"
264 << G.getAuxFunctionCalls().size() << "]\n";
266 } while (I != FirstInSCC);
269 std::cerr << "DONE with SCC #: " << MyID << "\n";
271 // We never have to revisit "SCC" processed functions...
273 // Drop the stuff we don't need from the end of the stack
274 Stack.erase(FirstInSCC, Stack.end());
278 return MyID; // == Min
282 // releaseMemory - If the pass pipeline is done with this pass, we can release
283 // our memory... here...
285 void BUDataStructures::releaseMemory() {
286 for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
287 E = DSInfo.end(); I != E; ++I)
290 // Empty map so next time memory is released, data structures are not
297 DSGraph &BUDataStructures::calculateGraph(Function &F) {
298 DSGraph &Graph = getDSGraph(F);
299 DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n");
301 // 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);
307 // 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.
314 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
315 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
316 LastCallSiteIdx = I.getCallSiteIdx();
318 // Resolve the current call...
319 Function *Callee = *I;
320 DSCallSite &CS = I.getCallSite();
322 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");
329 // Handle self recursion by resolving the arguments and return value
330 Graph.mergeInGraph(CS, Graph, 0);
333 // Get the data structure graph for the called function.
335 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
337 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
338 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
339 << GI.getAuxFunctionCalls().size() << "]\n");
342 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_before_" +
346 // Handle self recursion by resolving the arguments and return value
347 Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit |
348 DSGraph::DontCloneCallNodes);
351 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
357 // Make sure to catch any leftover unresolvable calls...
358 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
359 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
363 // Recompute the Incomplete markers. If there are any function calls left
364 // now that are complete, we must loop!
365 Graph.maskIncompleteMarkers();
366 Graph.markIncompleteNodes();
367 Graph.removeDeadNodes();
369 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
370 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
373 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
379 // inlineNonSCCGraphs - This method is almost like the other two calculate graph
380 // methods. This one is used to inline function graphs (from functions outside
381 // of the SCC) into functions in the SCC. It is not supposed to touch functions
382 // IN the SCC at all.
384 DSGraph &BUDataStructures::inlineNonSCCGraphs(Function &F,
385 std::set<Function*> &SCCFunctions){
386 DSGraph &Graph = getDSGraph(F);
387 DEBUG(std::cerr << " [BU] Inlining Non-SCC graphs for: "
388 << F.getName() << "\n");
390 // Move our call site list into TempFCs so that inline call sites go into the
391 // new call site list and doesn't invalidate our iterators!
392 std::vector<DSCallSite> TempFCs;
393 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
394 TempFCs.swap(AuxCallsList);
396 // Loop over all of the resolvable call sites
397 unsigned LastCallSiteIdx = ~0U;
398 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
399 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
400 // If we skipped over any call sites, they must be unresolvable, copy them
401 // to the real call site list.
403 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
404 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
405 LastCallSiteIdx = I.getCallSiteIdx();
407 // Resolve the current call...
408 Function *Callee = *I;
409 DSCallSite &CS = I.getCallSite();
411 if (Callee->isExternal()) {
412 // Ignore this case, simple varargs functions we cannot stub out!
413 } else if (SCCFunctions.count(Callee)) {
414 // Calling a function in the SCC, ignore it for now!
415 DEBUG(std::cerr << " SCC CallSite for: " << Callee->getName() << "\n");
416 AuxCallsList.push_back(CS);
418 // Get the data structure graph for the called function.
420 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
422 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
423 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
424 << GI.getAuxFunctionCalls().size() << "]\n");
426 // Handle self recursion by resolving the arguments and return value
427 Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit |
428 DSGraph::DontCloneCallNodes);
432 // Make sure to catch any leftover unresolvable calls...
433 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
434 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
438 // Recompute the Incomplete markers. If there are any function calls left
439 // now that are complete, we must loop!
440 Graph.maskIncompleteMarkers();
441 Graph.markIncompleteNodes();
442 Graph.removeDeadNodes();
444 DEBUG(std::cerr << " [BU] Done Non-SCC inlining: " << F.getName() << " ["
445 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
452 DSGraph &BUDataStructures::calculateSCCGraph(Function &F,
453 std::set<Function*> &SCCFunctions){
454 DSGraph &Graph = getDSGraph(F);
455 DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n");
457 std::vector<DSCallSite> UnresolvableCalls;
458 std::map<Function*, DSCallSite> SCCCallSiteMap;
459 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
461 while (1) { // Loop until we run out of resolvable call sites!
462 // Move our call site list into TempFCs so that inline call sites go into
463 // the new call site list and doesn't invalidate our iterators!
464 std::vector<DSCallSite> TempFCs;
465 TempFCs.swap(AuxCallsList);
467 // Loop over all of the resolvable call sites
468 unsigned LastCallSiteIdx = ~0U;
469 CallSiteIterator I = CallSiteIterator::begin(TempFCs),
470 E = CallSiteIterator::end(TempFCs);
472 TempFCs.swap(AuxCallsList);
473 break; // Done when no resolvable call sites exist
476 for (; I != E; ++I) {
477 // If we skipped over any call sites, they must be unresolvable, copy them
478 // to the unresolvable site list.
480 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
481 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
482 LastCallSiteIdx = I.getCallSiteIdx();
484 // Resolve the current call...
485 Function *Callee = *I;
486 DSCallSite &CS = I.getCallSite();
488 if (Callee->isExternal()) {
489 // Ignore this case, simple varargs functions we cannot stub out!
490 } else if (Callee == &F) {
491 // Self recursion... simply link up the formal arguments with the
492 // actual arguments...
493 DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n");
495 // Handle self recursion by resolving the arguments and return value
496 Graph.mergeInGraph(CS, Graph, 0);
497 } else if (SCCCallSiteMap.count(Callee)) {
498 // We have already seen a call site in the SCC for this function, just
499 // merge the two call sites together and we are done.
500 SCCCallSiteMap.find(Callee)->second.mergeWith(CS);
502 // Get the data structure graph for the called function.
504 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
506 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
507 << " in: " << F.getName() << "[" << GI.getGraphSize() << "+"
508 << GI.getAuxFunctionCalls().size() << "]\n");
510 // Handle self recursion by resolving the arguments and return value
511 Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit |
512 DSGraph::DontCloneCallNodes);
514 if (SCCFunctions.count(Callee))
515 SCCCallSiteMap.insert(std::make_pair(Callee, CS));
519 // Make sure to catch any leftover unresolvable calls...
520 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
521 UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]);
524 // Reset the SCCCallSiteMap...
525 SCCCallSiteMap.clear();
527 AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(),
528 UnresolvableCalls.end());
529 UnresolvableCalls.clear();
532 // Recompute the Incomplete markers. If there are any function calls left
533 // now that are complete, we must loop!
534 Graph.maskIncompleteMarkers();
535 Graph.markIncompleteNodes();
536 Graph.removeDeadNodes();
538 DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
539 << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size()
541 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());