1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the BUDataStructures class, which represents the
11 // Bottom-Up Interprocedural closure of the data structure graph over the
12 // program. This is useful for applications like pool allocation, but **not**
13 // applications like alias analysis.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/DataStructure/DataStructure.h"
18 #include "llvm/Analysis/DataStructure/DSGraph.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Support/Debug.h"
25 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
26 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
27 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
29 RegisterAnalysis<BUDataStructures>
30 X("budatastructure", "Bottom-up Data Structure Analysis");
33 // run - Calculate the bottom up data structure graphs for each function in the
36 bool BUDataStructures::runOnModule(Module &M) {
37 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
38 GlobalECs = LocalDSA.getGlobalECs();
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
41 GlobalsGraph->setPrintAuxCalls();
43 IndCallGraphMap = new std::map<std::vector<Function*>,
44 std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
46 std::vector<Function*> Stack;
47 hash_map<Function*, unsigned> ValMap;
50 Function *MainFunc = M.getMainFunction();
52 calculateGraphs(MainFunc, Stack, NextID, ValMap);
54 // Calculate the graphs for any functions that are unreachable from main...
55 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
56 if (!I->isExternal() && !DSInfo.count(I)) {
59 std::cerr << "*** Function unreachable from main: "
60 << I->getName() << "\n";
62 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
65 NumCallEdges += ActualCallees.size();
67 // If we computed any temporary indcallgraphs, free them now.
68 for (std::map<std::vector<Function*>,
69 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
70 IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
71 I->second.second.clear(); // Drop arg refs into the graph.
72 delete I->second.first;
74 delete IndCallGraphMap;
76 // At the end of the bottom-up pass, the globals graph becomes complete.
77 // FIXME: This is not the right way to do this, but it is sorta better than
78 // nothing! In particular, externally visible globals and unresolvable call
79 // nodes at the end of the BU phase should make things that they point to
80 // incomplete in the globals graph.
82 GlobalsGraph->removeTriviallyDeadNodes();
83 GlobalsGraph->maskIncompleteMarkers();
85 // Mark external globals incomplete.
86 GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
88 // Merge the globals variables (not the calls) from the globals graph back
89 // into the main function's graph so that the main function contains all of
90 // the information about global pools and GV usage in the program.
91 if (MainFunc && !MainFunc->isExternal()) {
92 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
93 const DSGraph &GG = *MainGraph.getGlobalsGraph();
94 ReachabilityCloner RC(MainGraph, GG,
95 DSGraph::DontCloneCallNodes |
96 DSGraph::DontCloneAuxCallNodes);
98 // Clone the global nodes into this graph.
99 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
100 E = GG.getScalarMap().global_end(); I != E; ++I)
101 if (isa<GlobalVariable>(*I))
102 RC.getClonedNH(GG.getNodeForValue(*I));
104 MainGraph.maskIncompleteMarkers();
105 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
106 DSGraph::IgnoreGlobals);
112 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
113 // Has the graph already been created?
114 DSGraph *&Graph = DSInfo[F];
115 if (Graph) return *Graph;
117 DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
119 // Steal the local graph.
120 Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
121 Graph->spliceFrom(LocGraph);
123 Graph->setGlobalsGraph(GlobalsGraph);
124 Graph->setPrintAuxCalls();
126 // Start with a copy of the original call sites...
127 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
131 static bool isVAHackFn(const Function *F) {
132 return F->getName() == "printf" || F->getName() == "sscanf" ||
133 F->getName() == "fprintf" || F->getName() == "open" ||
134 F->getName() == "sprintf" || F->getName() == "fputs" ||
135 F->getName() == "fscanf";
138 static bool isResolvableFunc(const Function* callee) {
139 return !callee->isExternal() || isVAHackFn(callee);
142 static void GetAllCallees(const DSCallSite &CS,
143 std::vector<Function*> &Callees) {
144 if (CS.isDirectCall()) {
145 if (isResolvableFunc(CS.getCalleeFunc()))
146 Callees.push_back(CS.getCalleeFunc());
147 } else if (!CS.getCalleeNode()->isIncomplete()) {
149 unsigned OldSize = Callees.size();
150 CS.getCalleeNode()->addFullFunctionList(Callees);
152 // If any of the callees are unresolvable, remove the whole batch!
153 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
154 if (!isResolvableFunc(Callees[i])) {
155 Callees.erase(Callees.begin()+OldSize, Callees.end());
162 /// GetAllAuxCallees - Return a list containing all of the resolvable callees in
163 /// the aux list for the specified graph in the Callees vector.
164 static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
166 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
167 GetAllCallees(*I, Callees);
170 unsigned BUDataStructures::calculateGraphs(Function *F,
171 std::vector<Function*> &Stack,
173 hash_map<Function*, unsigned> &ValMap) {
174 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
175 unsigned Min = NextID++, MyID = Min;
179 // FIXME! This test should be generalized to be any function that we have
180 // already processed, in the case when there isn't a main or there are
181 // unreachable functions!
182 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
189 DSGraph &Graph = getOrCreateGraph(F);
191 // Find all callee functions.
192 std::vector<Function*> CalleeFunctions;
193 GetAllAuxCallees(Graph, CalleeFunctions);
195 // The edges out of the current node are the call site targets...
196 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
197 Function *Callee = CalleeFunctions[i];
199 // Have we visited the destination function yet?
200 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
201 if (It == ValMap.end()) // No, visit it now.
202 M = calculateGraphs(Callee, Stack, NextID, ValMap);
203 else // Yes, get it's number.
205 if (M < Min) Min = M;
208 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
210 return Min; // This is part of a larger SCC!
212 // If this is a new SCC, process it now.
213 if (Stack.back() == F) { // Special case the single "SCC" case here.
214 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
215 << F->getName() << "\n");
217 DSGraph &G = getDSGraph(*F);
218 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
220 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
221 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
224 if (MaxSCC < 1) MaxSCC = 1;
226 // Should we revisit the graph? Only do it if there are now new resolvable
228 GetAllAuxCallees(Graph, CalleeFunctions);
229 if (!CalleeFunctions.empty()) {
231 return calculateGraphs(F, Stack, NextID, ValMap);
238 // SCCFunctions - Keep track of the functions in the current SCC
240 std::vector<DSGraph*> SCCGraphs;
242 unsigned SCCSize = 1;
243 Function *NF = Stack.back();
245 DSGraph &SCCGraph = getDSGraph(*NF);
247 // First thing first, collapse all of the DSGraphs into a single graph for
248 // the entire SCC. Splice all of the graphs into one and discard all of the
256 DSGraph &NFG = getDSGraph(*NF);
258 // Update the Function -> DSG map.
259 for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
260 E = NFG.retnodes_end(); I != E; ++I)
261 DSInfo[I->first] = &SCCGraph;
263 SCCGraph.spliceFrom(NFG);
270 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
273 // Compute the Max SCC Size.
274 if (MaxSCC < SCCSize)
277 // Clean up the graph before we start inlining a bunch again...
278 SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
280 // Now that we have one big happy family, resolve all of the call sites in
282 calculateGraph(SCCGraph);
283 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
284 << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
286 std::cerr << "DONE with SCC #: " << MyID << "\n";
288 // We never have to revisit "SCC" processed functions...
292 return MyID; // == Min
296 // releaseMemory - If the pass pipeline is done with this pass, we can release
297 // our memory... here...
299 void BUDataStructures::releaseMyMemory() {
300 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
301 E = DSInfo.end(); I != E; ++I) {
302 I->second->getReturnNodes().erase(I->first);
303 if (I->second->getReturnNodes().empty())
307 // Empty map so next time memory is released, data structures are not
314 void BUDataStructures::calculateGraph(DSGraph &Graph) {
315 // Move our call site list into TempFCs so that inline call sites go into the
316 // new call site list and doesn't invalidate our iterators!
317 std::list<DSCallSite> TempFCs;
318 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
319 TempFCs.swap(AuxCallsList);
321 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
323 bool Printed = false;
324 std::vector<Function*> CalledFuncs;
325 while (!TempFCs.empty()) {
326 DSCallSite &CS = *TempFCs.begin();
330 // Fast path for noop calls. Note that we don't care about merging globals
331 // in the callee with nodes in the caller here.
332 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
333 TempFCs.erase(TempFCs.begin());
335 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
336 TempFCs.erase(TempFCs.begin());
340 GetAllCallees(CS, CalledFuncs);
342 if (CalledFuncs.empty()) {
343 // Remember that we could not resolve this yet!
344 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
348 Instruction *TheCall = CS.getCallSite().getInstruction();
350 if (CalledFuncs.size() == 1) {
351 Function *Callee = CalledFuncs[0];
352 ActualCallees.insert(std::make_pair(TheCall, Callee));
354 // Get the data structure graph for the called function.
355 GI = &getDSGraph(*Callee); // Graph to inline
356 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
358 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
359 << GI->getAuxFunctionCalls().size() << "] into '"
360 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
361 << Graph.getAuxFunctionCalls().size() << "]\n");
362 Graph.mergeInGraph(CS, *Callee, *GI,
363 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
367 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
368 std::cerr << " calls " << CalledFuncs.size()
369 << " fns from site: " << CS.getCallSite().getInstruction()
370 << " " << *CS.getCallSite().getInstruction();
371 std::cerr << " Fns =";
372 unsigned NumPrinted = 0;
374 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
375 E = CalledFuncs.end(); I != E; ++I) {
376 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
378 // Add the call edges to the call graph.
379 ActualCallees.insert(std::make_pair(TheCall, *I));
383 // See if we already computed a graph for this set of callees.
384 std::sort(CalledFuncs.begin(), CalledFuncs.end());
385 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
386 (*IndCallGraphMap)[CalledFuncs];
388 if (IndCallGraph.first == 0) {
389 std::vector<Function*>::iterator I = CalledFuncs.begin(),
390 E = CalledFuncs.end();
392 // Start with a copy of the first graph.
393 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
394 GI->setGlobalsGraph(Graph.getGlobalsGraph());
395 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
397 // Get the argument nodes for the first callee. The return value is
398 // the 0th index in the vector.
399 GI->getFunctionArgumentsForCall(*I, Args);
401 // Merge all of the other callees into this graph.
402 for (++I; I != E; ++I) {
403 // If the graph already contains the nodes for the function, don't
404 // bother merging it in again.
405 if (!GI->containsFunction(*I)) {
406 GI->cloneInto(getDSGraph(**I));
410 std::vector<DSNodeHandle> NextArgs;
411 GI->getFunctionArgumentsForCall(*I, NextArgs);
412 unsigned i = 0, e = Args.size();
413 for (; i != e; ++i) {
414 if (i == NextArgs.size()) break;
415 Args[i].mergeWith(NextArgs[i]);
417 for (e = NextArgs.size(); i != e; ++i)
418 Args.push_back(NextArgs[i]);
421 // Clean up the final graph!
422 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
424 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
427 GI = IndCallGraph.first;
429 // Merge the unified graph into this graph now.
430 DEBUG(std::cerr << " Inlining multi callee graph "
431 << "[" << GI->getGraphSize() << "+"
432 << GI->getAuxFunctionCalls().size() << "] into '"
433 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
434 << Graph.getAuxFunctionCalls().size() << "]\n");
436 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
437 DSGraph::StripAllocaBit |
438 DSGraph::DontCloneCallNodes);
442 TempFCs.erase(TempFCs.begin());
445 // Recompute the Incomplete markers
446 Graph.maskIncompleteMarkers();
447 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
449 // Delete dead nodes. Treat globals that are unreachable but that can
450 // reach live nodes as live.
451 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
453 // When this graph is finalized, clone the globals in the graph into the
454 // globals graph to make sure it has everything, from all graphs.
455 DSScalarMap &MainSM = Graph.getScalarMap();
456 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
458 // Clone everything reachable from globals in the function graph into the
460 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
461 E = MainSM.global_end(); I != E; ++I)
462 RC.getClonedNH(MainSM[*I]);
464 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
467 static const Function *getFnForValue(const Value *V) {
468 if (const Instruction *I = dyn_cast<Instruction>(V))
469 return I->getParent()->getParent();
470 else if (const Argument *A = dyn_cast<Argument>(V))
471 return A->getParent();
472 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
473 return BB->getParent();
477 /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
478 /// These correspond to the interfaces defined in the AliasAnalysis class.
479 void BUDataStructures::deleteValue(Value *V) {
480 if (const Function *F = getFnForValue(V)) { // Function local value?
481 // If this is a function local value, just delete it from the scalar map!
482 getDSGraph(*F).getScalarMap().eraseIfExists(V);
486 if (Function *F = dyn_cast<Function>(V)) {
487 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
488 "cannot handle scc's");
494 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
497 void BUDataStructures::copyValue(Value *From, Value *To) {
498 if (From == To) return;
499 if (const Function *F = getFnForValue(From)) { // Function local value?
500 // If this is a function local value, just delete it from the scalar map!
501 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
505 if (Function *FromF = dyn_cast<Function>(From)) {
506 Function *ToF = cast<Function>(To);
507 assert(!DSInfo.count(ToF) && "New Function already exists!");
508 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
510 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
512 // Change the Function* is the returnnodes map to the ToF.
513 DSNodeHandle Ret = NG->retnodes_begin()->second;
514 NG->getReturnNodes().clear();
515 NG->getReturnNodes()[ToF] = Ret;
519 if (const Function *F = getFnForValue(To)) {
520 DSGraph &G = getDSGraph(*F);
521 G.getScalarMap().copyScalarIfExists(From, To);
527 assert(0 && "Do not know how to copy this yet!");