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/Module.h"
12 #include "Support/Statistic.h"
13 #include "DSCallSiteIterator.h"
16 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
17 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
18 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
20 RegisterAnalysis<BUDataStructures>
21 X("budatastructure", "Bottom-up Data Structure Analysis");
26 // run - Calculate the bottom up data structure graphs for each function in the
29 bool BUDataStructures::run(Module &M) {
30 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
31 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
32 GlobalsGraph->setPrintAuxCalls();
34 Function *MainFunc = M.getMainFunction();
36 calculateReachableGraphs(MainFunc);
38 // Calculate the graphs for any functions that are unreachable from main...
39 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
40 if (!I->isExternal() && !DSInfo.count(I)) {
43 std::cerr << "*** Function unreachable from main: "
44 << I->getName() << "\n";
46 calculateReachableGraphs(I); // Calculate all graphs...
49 NumCallEdges += ActualCallees.size();
53 void BUDataStructures::calculateReachableGraphs(Function *F) {
54 std::vector<Function*> Stack;
55 hash_map<Function*, unsigned> ValMap;
57 calculateGraphs(F, Stack, NextID, ValMap);
60 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
61 // Has the graph already been created?
62 DSGraph *&Graph = DSInfo[F];
63 if (Graph) return *Graph;
65 // Copy the local version into DSInfo...
66 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
68 Graph->setGlobalsGraph(GlobalsGraph);
69 Graph->setPrintAuxCalls();
71 // Start with a copy of the original call sites...
72 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
76 unsigned BUDataStructures::calculateGraphs(Function *F,
77 std::vector<Function*> &Stack,
79 hash_map<Function*, unsigned> &ValMap) {
80 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
81 unsigned Min = NextID++, MyID = Min;
85 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
92 DSGraph &Graph = getOrCreateGraph(F);
94 // The edges out of the current node are the call site targets...
95 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
96 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
97 Function *Callee = *I;
99 // Have we visited the destination function yet?
100 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
101 if (It == ValMap.end()) // No, visit it now.
102 M = calculateGraphs(Callee, Stack, NextID, ValMap);
103 else // Yes, get it's number.
105 if (M < Min) Min = M;
108 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
110 return Min; // This is part of a larger SCC!
112 // If this is a new SCC, process it now.
113 if (Stack.back() == F) { // Special case the single "SCC" case here.
114 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
115 << F->getName() << "\n");
117 DSGraph &G = getDSGraph(*F);
118 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
120 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
121 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
124 if (MaxSCC < 1) MaxSCC = 1;
126 // Should we revisit the graph?
127 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
129 return calculateGraphs(F, Stack, NextID, ValMap);
136 // SCCFunctions - Keep track of the functions in the current SCC
138 hash_set<Function*> SCCFunctions;
141 std::vector<Function*>::iterator FirstInSCC = Stack.end();
142 DSGraph *SCCGraph = 0;
146 SCCFunctions.insert(NF);
148 // Figure out which graph is the largest one, in order to speed things up
149 // a bit in situations where functions in the SCC have widely different
151 DSGraph &NFGraph = getDSGraph(*NF);
152 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
156 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
157 << SCCFunctions.size() << "\n";
159 // Compute the Max SCC Size...
160 if (MaxSCC < SCCFunctions.size())
161 MaxSCC = SCCFunctions.size();
163 // First thing first, collapse all of the DSGraphs into a single graph for
164 // the entire SCC. We computed the largest graph, so clone all of the other
165 // (smaller) graphs into it. Discard all of the old graphs.
167 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
168 E = SCCFunctions.end(); I != E; ++I) {
169 DSGraph &G = getDSGraph(**I);
170 if (&G != SCCGraph) {
171 DSGraph::NodeMapTy NodeMap;
172 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
173 SCCGraph->getReturnNodes(), NodeMap, 0);
174 // Update the DSInfo map and delete the old graph...
175 DSInfo[*I] = SCCGraph;
180 // Clean up the graph before we start inlining a bunch again...
181 SCCGraph->removeTriviallyDeadNodes();
183 // Now that we have one big happy family, resolve all of the call sites in
185 calculateGraph(*SCCGraph);
186 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
187 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
189 std::cerr << "DONE with SCC #: " << MyID << "\n";
191 // We never have to revisit "SCC" processed functions...
193 // Drop the stuff we don't need from the end of the stack
194 Stack.erase(FirstInSCC, Stack.end());
198 return MyID; // == Min
202 // releaseMemory - If the pass pipeline is done with this pass, we can release
203 // our memory... here...
205 void BUDataStructures::releaseMemory() {
206 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
207 E = DSInfo.end(); I != E; ++I) {
208 I->second->getReturnNodes().erase(I->first);
209 if (I->second->getReturnNodes().empty())
213 // Empty map so next time memory is released, data structures are not
220 void BUDataStructures::calculateGraph(DSGraph &Graph) {
221 // Move our call site list into TempFCs so that inline call sites go into the
222 // new call site list and doesn't invalidate our iterators!
223 std::vector<DSCallSite> TempFCs;
224 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
225 TempFCs.swap(AuxCallsList);
227 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
229 // Loop over all of the resolvable call sites
230 unsigned LastCallSiteIdx = ~0U;
231 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
232 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
233 // If we skipped over any call sites, they must be unresolvable, copy them
234 // to the real call site list.
236 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
237 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
238 LastCallSiteIdx = I.getCallSiteIdx();
240 // Resolve the current call...
241 Function *Callee = *I;
242 DSCallSite CS = I.getCallSite();
244 if (Callee->isExternal()) {
245 // Ignore this case, simple varargs functions we cannot stub out!
246 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
247 // Self recursion... simply link up the formal arguments with the
248 // actual arguments...
249 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
251 // Handle self recursion by resolving the arguments and return value
252 Graph.mergeInGraph(CS, *Callee, Graph, 0);
255 ActualCallees.insert(std::make_pair(&CS.getCallInst(), Callee));
257 // Get the data structure graph for the called function.
259 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
261 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
262 << "[" << GI.getGraphSize() << "+"
263 << GI.getAuxFunctionCalls().size() << "] into '"
264 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
265 << Graph.getAuxFunctionCalls().size() << "]\n");
267 // Handle self recursion by resolving the arguments and return value
268 Graph.mergeInGraph(CS, *Callee, GI,
269 DSGraph::KeepModRefBits |
270 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
274 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
280 // Make sure to catch any leftover unresolvable calls...
281 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
282 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
286 // Recompute the Incomplete markers. If there are any function calls left
287 // now that are complete, we must loop!
288 Graph.maskIncompleteMarkers();
289 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
290 // FIXME: materialize nodes from the globals graph as neccesary...
291 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
293 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());