blob: b4b2e48b4efedb2717450a7ac4f8f17de6b6037a [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00009//
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**
Chris Lattner55c10582002-10-03 20:38:41 +000013// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/Analysis/DataStructure.h"
18#include "llvm/Module.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000019#include "Support/Statistic.h"
Chris Lattner6806f562003-08-01 22:15:03 +000020#include "Support/Debug.h"
Chris Lattner5d5b6d62003-07-01 16:04:18 +000021#include "DSCallSiteIterator.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000022
Chris Lattnerae5f6032002-11-17 22:16:28 +000023namespace {
24 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000025 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000026 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000027
28 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000029 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000030}
Chris Lattner0d9bab82002-07-18 00:12:30 +000031
Chris Lattnerb1060432002-11-07 05:20:53 +000032using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000033
Chris Lattneraa0b4682002-11-09 21:12:07 +000034// run - Calculate the bottom up data structure graphs for each function in the
35// program.
36//
37bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000038 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
39 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000040 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000041
Chris Lattnera9c9c022002-11-11 21:35:13 +000042 Function *MainFunc = M.getMainFunction();
43 if (MainFunc)
44 calculateReachableGraphs(MainFunc);
45
46 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000047 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000048 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000049#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000050 if (MainFunc)
51 std::cerr << "*** Function unreachable from main: "
52 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000053#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000054 calculateReachableGraphs(I); // Calculate all graphs...
55 }
Chris Lattner6c874612003-07-02 23:42:48 +000056
57 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000058
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
Chris Lattner11fc9302003-09-20 23:58:33 +000061 // 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.
64 //
Chris Lattnerec157b72003-09-20 23:27:05 +000065 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000066 return false;
67}
Chris Lattner55c10582002-10-03 20:38:41 +000068
Chris Lattnera9c9c022002-11-11 21:35:13 +000069void BUDataStructures::calculateReachableGraphs(Function *F) {
70 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000071 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000072 unsigned NextID = 1;
73 calculateGraphs(F, Stack, NextID, ValMap);
74}
75
76DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
77 // Has the graph already been created?
78 DSGraph *&Graph = DSInfo[F];
79 if (Graph) return *Graph;
80
81 // Copy the local version into DSInfo...
82 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
83
84 Graph->setGlobalsGraph(GlobalsGraph);
85 Graph->setPrintAuxCalls();
86
87 // Start with a copy of the original call sites...
88 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
89 return *Graph;
90}
91
92unsigned BUDataStructures::calculateGraphs(Function *F,
93 std::vector<Function*> &Stack,
94 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000095 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000096 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
97 unsigned Min = NextID++, MyID = Min;
98 ValMap[F] = Min;
99 Stack.push_back(F);
100
101 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
102 // No callees!
103 Stack.pop_back();
104 ValMap[F] = ~0;
105 return Min;
106 }
107
108 DSGraph &Graph = getOrCreateGraph(F);
109
110 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000111 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
112 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000113 Function *Callee = *I;
114 unsigned M;
115 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000116 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000117 if (It == ValMap.end()) // No, visit it now.
118 M = calculateGraphs(Callee, Stack, NextID, ValMap);
119 else // Yes, get it's number.
120 M = It->second;
121 if (M < Min) Min = M;
122 }
123
124 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
125 if (Min != MyID)
126 return Min; // This is part of a larger SCC!
127
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");
132 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000133 DSGraph &G = getDSGraph(*F);
134 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
135 calculateGraph(G);
136 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
137 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
138 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000139
Chris Lattnerae5f6032002-11-17 22:16:28 +0000140 if (MaxSCC < 1) MaxSCC = 1;
141
Chris Lattnera9c9c022002-11-11 21:35:13 +0000142 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000143 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144 ValMap.erase(F);
145 return calculateGraphs(F, Stack, NextID, ValMap);
146 } else {
147 ValMap[F] = ~0U;
148 }
149 return MyID;
150
151 } else {
152 // SCCFunctions - Keep track of the functions in the current SCC
153 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000154 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000155
156 Function *NF;
157 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000158 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000159 do {
160 NF = *--FirstInSCC;
161 ValMap[NF] = ~0U;
162 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000163
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
166 // graph sizes.
167 DSGraph &NFGraph = getDSGraph(*NF);
168 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
169 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000170 } while (NF != F);
171
Chris Lattner0eea6182003-06-30 05:09:58 +0000172 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
173 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000174
Chris Lattnerae5f6032002-11-17 22:16:28 +0000175 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000176 if (MaxSCC < SCCFunctions.size())
177 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000178
Chris Lattner0eea6182003-06-30 05:09:58 +0000179 // 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.
182 //
183 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
184 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000185 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000186 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;
192 delete &G;
193 }
194 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000195
Chris Lattner744f9392003-07-02 04:37:48 +0000196 // Clean up the graph before we start inlining a bunch again...
197 SCCGraph->removeTriviallyDeadNodes();
198
Chris Lattner0eea6182003-06-30 05:09:58 +0000199 // Now that we have one big happy family, resolve all of the call sites in
200 // the graph...
201 calculateGraph(*SCCGraph);
202 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
203 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000204
205 std::cerr << "DONE with SCC #: " << MyID << "\n";
206
207 // We never have to revisit "SCC" processed functions...
208
209 // Drop the stuff we don't need from the end of the stack
210 Stack.erase(FirstInSCC, Stack.end());
211 return MyID;
212 }
213
214 return MyID; // == Min
215}
216
217
Chris Lattner0d9bab82002-07-18 00:12:30 +0000218// releaseMemory - If the pass pipeline is done with this pass, we can release
219// our memory... here...
220//
221void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000222 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())
226 delete I->second;
227 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000228
229 // Empty map so next time memory is released, data structures are not
230 // re-deleted.
231 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000232 delete GlobalsGraph;
233 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000234}
235
Chris Lattner0eea6182003-06-30 05:09:58 +0000236void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000237 // 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);
Chris Lattner8a5db462002-11-11 00:01:34 +0000242
Chris Lattner0eea6182003-06-30 05:09:58 +0000243 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
244
Chris Lattnera9c9c022002-11-11 21:35:13 +0000245 // Loop over all of the resolvable call sites
246 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000247 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
248 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000249 // If we skipped over any call sites, they must be unresolvable, copy them
250 // to the real call site list.
251 LastCallSiteIdx++;
252 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
253 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
254 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000255
Chris Lattnera9c9c022002-11-11 21:35:13 +0000256 // Resolve the current call...
257 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000258 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000259
Chris Lattnera9c9c022002-11-11 21:35:13 +0000260 if (Callee->isExternal()) {
261 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000262 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000263 // Self recursion... simply link up the formal arguments with the
264 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000265 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000266
267 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000268 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000269
270 } else {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000271 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
272 Callee));
Chris Lattner744f9392003-07-02 04:37:48 +0000273
Chris Lattnera9c9c022002-11-11 21:35:13 +0000274 // Get the data structure graph for the called function.
275 //
276 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
277
278 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000279 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000280 << GI.getAuxFunctionCalls().size() << "] into '"
281 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000282 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000283
284 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000285 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000286 DSGraph::KeepModRefBits |
287 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000288 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000289
290#if 0
291 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
292 Callee->getName());
293#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000294 }
295 }
296
297 // Make sure to catch any leftover unresolvable calls...
298 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
299 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
300
301 TempFCs.clear();
302
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000303 // 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();
307
308 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000309 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000310 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000311
312 // Delete dead nodes. Treat globals that are unreachable but that can
313 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000314 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000315
Chris Lattnera9c9c022002-11-11 21:35:13 +0000316 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000317}
318