blob: 5019d90845069613c58b282fb781a0a119c416a7 [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00002//
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**
Chris Lattner55c10582002-10-03 20:38:41 +00006// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
11#include "llvm/Module.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000012#include "Support/Statistic.h"
Chris Lattner6806f562003-08-01 22:15:03 +000013#include "Support/Debug.h"
Chris Lattner5d5b6d62003-07-01 16:04:18 +000014#include "DSCallSiteIterator.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000015
Chris Lattnerae5f6032002-11-17 22:16:28 +000016namespace {
17 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000018 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000019 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000020
21 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000022 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000023}
Chris Lattner0d9bab82002-07-18 00:12:30 +000024
Chris Lattnerb1060432002-11-07 05:20:53 +000025using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000026
Chris Lattneraa0b4682002-11-09 21:12:07 +000027// run - Calculate the bottom up data structure graphs for each function in the
28// program.
29//
30bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000031 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
32 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000033 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000034
Chris Lattnera9c9c022002-11-11 21:35:13 +000035 Function *MainFunc = M.getMainFunction();
36 if (MainFunc)
37 calculateReachableGraphs(MainFunc);
38
39 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000040 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000041 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000042#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000043 if (MainFunc)
44 std::cerr << "*** Function unreachable from main: "
45 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000046#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000047 calculateReachableGraphs(I); // Calculate all graphs...
48 }
Chris Lattner6c874612003-07-02 23:42:48 +000049
50 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000051
52 // At the end of the bottom-up pass, the globals graph becomes complete.
53 // FIXME: This is not the right way to do this, but it is sorta better than
54 // nothing!
55 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000056 return false;
57}
Chris Lattner55c10582002-10-03 20:38:41 +000058
Chris Lattnera9c9c022002-11-11 21:35:13 +000059void BUDataStructures::calculateReachableGraphs(Function *F) {
60 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000061 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000062 unsigned NextID = 1;
63 calculateGraphs(F, Stack, NextID, ValMap);
64}
65
66DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
67 // Has the graph already been created?
68 DSGraph *&Graph = DSInfo[F];
69 if (Graph) return *Graph;
70
71 // Copy the local version into DSInfo...
72 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
73
74 Graph->setGlobalsGraph(GlobalsGraph);
75 Graph->setPrintAuxCalls();
76
77 // Start with a copy of the original call sites...
78 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
79 return *Graph;
80}
81
82unsigned BUDataStructures::calculateGraphs(Function *F,
83 std::vector<Function*> &Stack,
84 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000085 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000086 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
87 unsigned Min = NextID++, MyID = Min;
88 ValMap[F] = Min;
89 Stack.push_back(F);
90
91 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
92 // No callees!
93 Stack.pop_back();
94 ValMap[F] = ~0;
95 return Min;
96 }
97
98 DSGraph &Graph = getOrCreateGraph(F);
99
100 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000101 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
102 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000103 Function *Callee = *I;
104 unsigned M;
105 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000106 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000107 if (It == ValMap.end()) // No, visit it now.
108 M = calculateGraphs(Callee, Stack, NextID, ValMap);
109 else // Yes, get it's number.
110 M = It->second;
111 if (M < Min) Min = M;
112 }
113
114 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
115 if (Min != MyID)
116 return Min; // This is part of a larger SCC!
117
118 // If this is a new SCC, process it now.
119 if (Stack.back() == F) { // Special case the single "SCC" case here.
120 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
121 << F->getName() << "\n");
122 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000123 DSGraph &G = getDSGraph(*F);
124 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
125 calculateGraph(G);
126 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
127 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
128 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000129
Chris Lattnerae5f6032002-11-17 22:16:28 +0000130 if (MaxSCC < 1) MaxSCC = 1;
131
Chris Lattnera9c9c022002-11-11 21:35:13 +0000132 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000133 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000134 ValMap.erase(F);
135 return calculateGraphs(F, Stack, NextID, ValMap);
136 } else {
137 ValMap[F] = ~0U;
138 }
139 return MyID;
140
141 } else {
142 // SCCFunctions - Keep track of the functions in the current SCC
143 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000144 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000145
146 Function *NF;
147 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000148 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000149 do {
150 NF = *--FirstInSCC;
151 ValMap[NF] = ~0U;
152 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000153
154 // Figure out which graph is the largest one, in order to speed things up
155 // a bit in situations where functions in the SCC have widely different
156 // graph sizes.
157 DSGraph &NFGraph = getDSGraph(*NF);
158 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
159 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000160 } while (NF != F);
161
Chris Lattner0eea6182003-06-30 05:09:58 +0000162 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
163 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000164
Chris Lattnerae5f6032002-11-17 22:16:28 +0000165 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000166 if (MaxSCC < SCCFunctions.size())
167 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000168
Chris Lattner0eea6182003-06-30 05:09:58 +0000169 // First thing first, collapse all of the DSGraphs into a single graph for
170 // the entire SCC. We computed the largest graph, so clone all of the other
171 // (smaller) graphs into it. Discard all of the old graphs.
172 //
173 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
174 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000175 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000176 if (&G != SCCGraph) {
177 DSGraph::NodeMapTy NodeMap;
178 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
179 SCCGraph->getReturnNodes(), NodeMap, 0);
180 // Update the DSInfo map and delete the old graph...
181 DSInfo[*I] = SCCGraph;
182 delete &G;
183 }
184 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000185
Chris Lattner744f9392003-07-02 04:37:48 +0000186 // Clean up the graph before we start inlining a bunch again...
187 SCCGraph->removeTriviallyDeadNodes();
188
Chris Lattner0eea6182003-06-30 05:09:58 +0000189 // Now that we have one big happy family, resolve all of the call sites in
190 // the graph...
191 calculateGraph(*SCCGraph);
192 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
193 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000194
195 std::cerr << "DONE with SCC #: " << MyID << "\n";
196
197 // We never have to revisit "SCC" processed functions...
198
199 // Drop the stuff we don't need from the end of the stack
200 Stack.erase(FirstInSCC, Stack.end());
201 return MyID;
202 }
203
204 return MyID; // == Min
205}
206
207
Chris Lattner0d9bab82002-07-18 00:12:30 +0000208// releaseMemory - If the pass pipeline is done with this pass, we can release
209// our memory... here...
210//
211void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000212 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
213 E = DSInfo.end(); I != E; ++I) {
214 I->second->getReturnNodes().erase(I->first);
215 if (I->second->getReturnNodes().empty())
216 delete I->second;
217 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000218
219 // Empty map so next time memory is released, data structures are not
220 // re-deleted.
221 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000222 delete GlobalsGraph;
223 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000224}
225
Chris Lattner0eea6182003-06-30 05:09:58 +0000226void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000227 // Move our call site list into TempFCs so that inline call sites go into the
228 // new call site list and doesn't invalidate our iterators!
229 std::vector<DSCallSite> TempFCs;
230 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
231 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000232
Chris Lattner0eea6182003-06-30 05:09:58 +0000233 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
234
Chris Lattnera9c9c022002-11-11 21:35:13 +0000235 // Loop over all of the resolvable call sites
236 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000237 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
238 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000239 // If we skipped over any call sites, they must be unresolvable, copy them
240 // to the real call site list.
241 LastCallSiteIdx++;
242 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
243 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
244 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000245
Chris Lattnera9c9c022002-11-11 21:35:13 +0000246 // Resolve the current call...
247 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000248 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000249
Chris Lattnera9c9c022002-11-11 21:35:13 +0000250 if (Callee->isExternal()) {
251 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000252 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000253 // Self recursion... simply link up the formal arguments with the
254 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000255 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000256
257 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000258 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000259
260 } else {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000261 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
262 Callee));
Chris Lattner744f9392003-07-02 04:37:48 +0000263
Chris Lattnera9c9c022002-11-11 21:35:13 +0000264 // Get the data structure graph for the called function.
265 //
266 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
267
268 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000269 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000270 << GI.getAuxFunctionCalls().size() << "] into '"
271 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000272 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000273
274 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000275 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000276 DSGraph::KeepModRefBits |
277 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000278 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000279
280#if 0
281 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
282 Callee->getName());
283#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000284 }
285 }
286
287 // Make sure to catch any leftover unresolvable calls...
288 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
289 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
290
291 TempFCs.clear();
292
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000293 // Re-materialize nodes from the globals graph.
294 // Do not ignore globals inlined from callees -- they are not up-to-date!
295 Graph.getInlinedGlobals().clear();
296 Graph.updateFromGlobalGraph();
297
298 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000299 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000300 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000301
302 // Delete dead nodes. Treat globals that are unreachable but that can
303 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000304 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000305
Chris Lattnera9c9c022002-11-11 21:35:13 +0000306 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000307}
308