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"
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");
35 // run - Calculate the bottom up data structure graphs for each function in the
38 bool BUDataStructures::run(Module &M) {
39 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
41 GlobalsGraph->setPrintAuxCalls();
43 Function *MainFunc = M.getMainFunction();
45 calculateReachableGraphs(MainFunc);
47 // Calculate the graphs for any functions that are unreachable from main...
48 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
49 if (!I->isExternal() && !DSInfo.count(I)) {
52 std::cerr << "*** Function unreachable from main: "
53 << I->getName() << "\n";
55 calculateReachableGraphs(I); // Calculate all graphs...
58 NumCallEdges += ActualCallees.size();
60 // At the end of the bottom-up pass, the globals graph becomes complete.
61 // FIXME: This is not the right way to do this, but it is sorta better than
62 // nothing! In particular, externally visible globals and unresolvable call
63 // nodes at the end of the BU phase should make things that they point to
64 // incomplete in the globals graph.
66 GlobalsGraph->maskIncompleteMarkers();
70 void BUDataStructures::calculateReachableGraphs(Function *F) {
71 std::vector<Function*> Stack;
72 hash_map<Function*, unsigned> ValMap;
74 calculateGraphs(F, Stack, NextID, ValMap);
77 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
78 // Has the graph already been created?
79 DSGraph *&Graph = DSInfo[F];
80 if (Graph) return *Graph;
82 // Copy the local version into DSInfo...
83 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
85 Graph->setGlobalsGraph(GlobalsGraph);
86 Graph->setPrintAuxCalls();
88 // Start with a copy of the original call sites...
89 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
93 unsigned BUDataStructures::calculateGraphs(Function *F,
94 std::vector<Function*> &Stack,
96 hash_map<Function*, unsigned> &ValMap) {
97 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
98 unsigned Min = NextID++, MyID = Min;
102 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
109 DSGraph &Graph = getOrCreateGraph(F);
111 // The edges out of the current node are the call site targets...
112 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
113 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
114 Function *Callee = *I;
116 // Have we visited the destination function yet?
117 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
118 if (It == ValMap.end()) // No, visit it now.
119 M = calculateGraphs(Callee, Stack, NextID, ValMap);
120 else // Yes, get it's number.
122 if (M < Min) Min = M;
125 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
127 return Min; // This is part of a larger SCC!
129 // If this is a new SCC, process it now.
130 if (Stack.back() == F) { // Special case the single "SCC" case here.
131 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
132 << F->getName() << "\n");
134 DSGraph &G = getDSGraph(*F);
135 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
137 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
138 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
141 if (MaxSCC < 1) MaxSCC = 1;
143 // Should we revisit the graph?
144 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
146 return calculateGraphs(F, Stack, NextID, ValMap);
153 // SCCFunctions - Keep track of the functions in the current SCC
155 hash_set<DSGraph*> SCCGraphs;
158 std::vector<Function*>::iterator FirstInSCC = Stack.end();
159 DSGraph *SCCGraph = 0;
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 SCCGraphs.insert(&NFGraph);
169 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
173 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
174 << SCCGraphs.size() << "\n";
176 // Compute the Max SCC Size...
177 if (MaxSCC < SCCGraphs.size())
178 MaxSCC = SCCGraphs.size();
180 // First thing first, collapse all of the DSGraphs into a single graph for
181 // the entire SCC. We computed the largest graph, so clone all of the other
182 // (smaller) graphs into it. Discard all of the old graphs.
184 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
185 E = SCCGraphs.end(); I != E; ++I) {
187 if (&G != SCCGraph) {
188 DSGraph::NodeMapTy NodeMap;
189 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
190 SCCGraph->getReturnNodes(), NodeMap);
191 // Update the DSInfo map and delete the old graph...
192 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
193 E = G.getReturnNodes().end(); I != E; ++I)
194 DSInfo[I->first] = SCCGraph;
199 // Clean up the graph before we start inlining a bunch again...
200 SCCGraph->removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
202 // Now that we have one big happy family, resolve all of the call sites in
204 calculateGraph(*SCCGraph);
205 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
206 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
208 std::cerr << "DONE with SCC #: " << MyID << "\n";
210 // We never have to revisit "SCC" processed functions...
212 // Drop the stuff we don't need from the end of the stack
213 Stack.erase(FirstInSCC, Stack.end());
217 return MyID; // == Min
221 // releaseMemory - If the pass pipeline is done with this pass, we can release
222 // our memory... here...
224 void BUDataStructures::releaseMemory() {
225 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
226 E = DSInfo.end(); I != E; ++I) {
227 I->second->getReturnNodes().erase(I->first);
228 if (I->second->getReturnNodes().empty())
232 // Empty map so next time memory is released, data structures are not
239 void BUDataStructures::calculateGraph(DSGraph &Graph) {
240 // Move our call site list into TempFCs so that inline call sites go into the
241 // new call site list and doesn't invalidate our iterators!
242 std::vector<DSCallSite> TempFCs;
243 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
244 TempFCs.swap(AuxCallsList);
246 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
248 // Loop over all of the resolvable call sites
249 unsigned LastCallSiteIdx = ~0U;
250 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
251 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
252 // If we skipped over any call sites, they must be unresolvable, copy them
253 // to the real call site list.
255 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
256 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
257 LastCallSiteIdx = I.getCallSiteIdx();
259 // Resolve the current call...
260 Function *Callee = *I;
261 DSCallSite CS = I.getCallSite();
263 if (Callee->isExternal()) {
264 // Ignore this case, simple varargs functions we cannot stub out!
265 } else if (ReturnNodes.count(Callee)) {
266 // Self recursion... simply link up the formal arguments with the
267 // actual arguments...
268 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
270 // Handle self recursion by resolving the arguments and return value
271 Graph.mergeInGraph(CS, *Callee, Graph, 0);
274 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
277 // Get the data structure graph for the called function.
279 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
281 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
282 << "[" << GI.getGraphSize() << "+"
283 << GI.getAuxFunctionCalls().size() << "] into '"
284 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
285 << Graph.getAuxFunctionCalls().size() << "]\n");
287 Graph.mergeInGraph(CS, *Callee, GI,
288 DSGraph::KeepModRefBits |
289 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
293 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
299 // Make sure to catch any leftover unresolvable calls...
300 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
301 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
305 // Re-materialize nodes from the globals graph.
306 // Do not ignore globals inlined from callees -- they are not up-to-date!
307 assert(Graph.getInlinedGlobals().empty());
308 Graph.updateFromGlobalGraph();
310 // Recompute the Incomplete markers
311 Graph.maskIncompleteMarkers();
312 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
314 // Delete dead nodes. Treat globals that are unreachable but that can
315 // reach live nodes as live.
316 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
318 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());