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.h"
18 #include "llvm/Module.h"
19 #include "Support/Statistic.h"
20 #include "Support/Debug.h"
21 #include "DSCallSiteIterator.h"
24 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
25 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
26 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
28 RegisterAnalysis<BUDataStructures>
29 X("budatastructure", "Bottom-up Data Structure Analysis");
34 // run - Calculate the bottom up data structure graphs for each function in the
37 bool BUDataStructures::run(Module &M) {
38 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
39 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
40 GlobalsGraph->setPrintAuxCalls();
42 Function *MainFunc = M.getMainFunction();
44 calculateReachableGraphs(MainFunc);
46 // Calculate the graphs for any functions that are unreachable from main...
47 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
48 if (!I->isExternal() && !DSInfo.count(I)) {
51 std::cerr << "*** Function unreachable from main: "
52 << I->getName() << "\n";
54 calculateReachableGraphs(I); // Calculate all graphs...
57 NumCallEdges += ActualCallees.size();
59 // At the end of the bottom-up pass, the globals graph becomes complete.
60 // FIXME: This is not the right way to do this, but it is sorta better than
61 // nothing! In particular, externally visible globals and unresolvable call
62 // nodes at the end of the BU phase should make things that they point to
63 // incomplete in the globals graph.
65 GlobalsGraph->maskIncompleteMarkers();
69 void BUDataStructures::calculateReachableGraphs(Function *F) {
70 std::vector<Function*> Stack;
71 hash_map<Function*, unsigned> ValMap;
73 calculateGraphs(F, Stack, NextID, ValMap);
76 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
77 // Has the graph already been created?
78 DSGraph *&Graph = DSInfo[F];
79 if (Graph) return *Graph;
81 // Copy the local version into DSInfo...
82 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
84 Graph->setGlobalsGraph(GlobalsGraph);
85 Graph->setPrintAuxCalls();
87 // Start with a copy of the original call sites...
88 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
92 unsigned BUDataStructures::calculateGraphs(Function *F,
93 std::vector<Function*> &Stack,
95 hash_map<Function*, unsigned> &ValMap) {
96 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
97 unsigned Min = NextID++, MyID = Min;
101 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
108 DSGraph &Graph = getOrCreateGraph(F);
110 // The edges out of the current node are the call site targets...
111 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
112 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
113 Function *Callee = *I;
115 // Have we visited the destination function yet?
116 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
117 if (It == ValMap.end()) // No, visit it now.
118 M = calculateGraphs(Callee, Stack, NextID, ValMap);
119 else // Yes, get it's number.
121 if (M < Min) Min = M;
124 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
126 return Min; // This is part of a larger SCC!
128 // If this is a new SCC, process it now.
129 if (Stack.back() == F) { // Special case the single "SCC" case here.
130 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
131 << F->getName() << "\n");
133 DSGraph &G = getDSGraph(*F);
134 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
136 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
137 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
140 if (MaxSCC < 1) MaxSCC = 1;
142 // Should we revisit the graph?
143 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
145 return calculateGraphs(F, Stack, NextID, ValMap);
152 // SCCFunctions - Keep track of the functions in the current SCC
154 hash_set<Function*> SCCFunctions;
157 std::vector<Function*>::iterator FirstInSCC = Stack.end();
158 DSGraph *SCCGraph = 0;
162 SCCFunctions.insert(NF);
164 // Figure out which graph is the largest one, in order to speed things up
165 // a bit in situations where functions in the SCC have widely different
167 DSGraph &NFGraph = getDSGraph(*NF);
168 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
172 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
173 << SCCFunctions.size() << "\n";
175 // Compute the Max SCC Size...
176 if (MaxSCC < SCCFunctions.size())
177 MaxSCC = SCCFunctions.size();
179 // First thing first, collapse all of the DSGraphs into a single graph for
180 // the entire SCC. We computed the largest graph, so clone all of the other
181 // (smaller) graphs into it. Discard all of the old graphs.
183 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
184 E = SCCFunctions.end(); I != E; ++I) {
185 DSGraph &G = getDSGraph(**I);
186 if (&G != SCCGraph) {
187 DSGraph::NodeMapTy NodeMap;
188 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
189 SCCGraph->getReturnNodes(), NodeMap, 0);
190 // Update the DSInfo map and delete the old graph...
191 DSInfo[*I] = SCCGraph;
196 // Clean up the graph before we start inlining a bunch again...
197 SCCGraph->removeTriviallyDeadNodes();
199 // Now that we have one big happy family, resolve all of the call sites in
201 calculateGraph(*SCCGraph);
202 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
203 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
205 std::cerr << "DONE with SCC #: " << MyID << "\n";
207 // We never have to revisit "SCC" processed functions...
209 // Drop the stuff we don't need from the end of the stack
210 Stack.erase(FirstInSCC, Stack.end());
214 return MyID; // == Min
218 // releaseMemory - If the pass pipeline is done with this pass, we can release
219 // our memory... here...
221 void BUDataStructures::releaseMemory() {
222 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
223 E = DSInfo.end(); I != E; ++I) {
224 I->second->getReturnNodes().erase(I->first);
225 if (I->second->getReturnNodes().empty())
229 // Empty map so next time memory is released, data structures are not
236 void BUDataStructures::calculateGraph(DSGraph &Graph) {
237 // Move our call site list into TempFCs so that inline call sites go into the
238 // new call site list and doesn't invalidate our iterators!
239 std::vector<DSCallSite> TempFCs;
240 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
241 TempFCs.swap(AuxCallsList);
243 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
245 // Loop over all of the resolvable call sites
246 unsigned LastCallSiteIdx = ~0U;
247 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
248 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
249 // If we skipped over any call sites, they must be unresolvable, copy them
250 // to the real call site list.
252 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
253 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
254 LastCallSiteIdx = I.getCallSiteIdx();
256 // Resolve the current call...
257 Function *Callee = *I;
258 DSCallSite CS = I.getCallSite();
260 if (Callee->isExternal()) {
261 // Ignore this case, simple varargs functions we cannot stub out!
262 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
263 // Self recursion... simply link up the formal arguments with the
264 // actual arguments...
265 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
267 // Handle self recursion by resolving the arguments and return value
268 Graph.mergeInGraph(CS, *Callee, Graph, 0);
271 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
274 // Get the data structure graph for the called function.
276 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
278 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
279 << "[" << GI.getGraphSize() << "+"
280 << GI.getAuxFunctionCalls().size() << "] into '"
281 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
282 << Graph.getAuxFunctionCalls().size() << "]\n");
284 // Handle self recursion by resolving the arguments and return value
285 Graph.mergeInGraph(CS, *Callee, GI,
286 DSGraph::KeepModRefBits |
287 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
291 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
297 // Make sure to catch any leftover unresolvable calls...
298 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
299 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
303 // Re-materialize nodes from the globals graph.
304 // Do not ignore globals inlined from callees -- they are not up-to-date!
305 Graph.getInlinedGlobals().clear();
306 Graph.updateFromGlobalGraph();
308 // Recompute the Incomplete markers
309 Graph.maskIncompleteMarkers();
310 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
312 // Delete dead nodes. Treat globals that are unreachable but that can
313 // reach live nodes as live.
314 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
316 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());