blob: 66990fd9230ff66b01937385175f55b1274f14c3 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000023namespace llvm {
24
Chris Lattnerae5f6032002-11-17 22:16:28 +000025namespace {
26 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000027 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000028 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000029
30 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000031 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000032}
Chris Lattner0d9bab82002-07-18 00:12:30 +000033
Chris Lattnerb1060432002-11-07 05:20:53 +000034using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000035
Chris Lattneraa0b4682002-11-09 21:12:07 +000036// run - Calculate the bottom up data structure graphs for each function in the
37// program.
38//
39bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000040 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
41 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000042 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000043
Chris Lattnera9c9c022002-11-11 21:35:13 +000044 Function *MainFunc = M.getMainFunction();
45 if (MainFunc)
46 calculateReachableGraphs(MainFunc);
47
48 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000049 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000050 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000051#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000052 if (MainFunc)
53 std::cerr << "*** Function unreachable from main: "
54 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000055#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000056 calculateReachableGraphs(I); // Calculate all graphs...
57 }
Chris Lattner6c874612003-07-02 23:42:48 +000058
59 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000060
61 // At the end of the bottom-up pass, the globals graph becomes complete.
62 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000063 // nothing! In particular, externally visible globals and unresolvable call
64 // nodes at the end of the BU phase should make things that they point to
65 // incomplete in the globals graph.
66 //
Chris Lattnerec157b72003-09-20 23:27:05 +000067 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000068 return false;
69}
Chris Lattner55c10582002-10-03 20:38:41 +000070
Chris Lattnera9c9c022002-11-11 21:35:13 +000071void BUDataStructures::calculateReachableGraphs(Function *F) {
72 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000073 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000074 unsigned NextID = 1;
75 calculateGraphs(F, Stack, NextID, ValMap);
76}
77
78DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
79 // Has the graph already been created?
80 DSGraph *&Graph = DSInfo[F];
81 if (Graph) return *Graph;
82
83 // Copy the local version into DSInfo...
84 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
85
86 Graph->setGlobalsGraph(GlobalsGraph);
87 Graph->setPrintAuxCalls();
88
89 // Start with a copy of the original call sites...
90 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
91 return *Graph;
92}
93
94unsigned BUDataStructures::calculateGraphs(Function *F,
95 std::vector<Function*> &Stack,
96 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000097 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000098 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
99 unsigned Min = NextID++, MyID = Min;
100 ValMap[F] = Min;
101 Stack.push_back(F);
102
103 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
104 // No callees!
105 Stack.pop_back();
106 ValMap[F] = ~0;
107 return Min;
108 }
109
110 DSGraph &Graph = getOrCreateGraph(F);
111
112 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000113 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
114 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000115 Function *Callee = *I;
116 unsigned M;
117 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000118 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000119 if (It == ValMap.end()) // No, visit it now.
120 M = calculateGraphs(Callee, Stack, NextID, ValMap);
121 else // Yes, get it's number.
122 M = It->second;
123 if (M < Min) Min = M;
124 }
125
126 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
127 if (Min != MyID)
128 return Min; // This is part of a larger SCC!
129
130 // If this is a new SCC, process it now.
131 if (Stack.back() == F) { // Special case the single "SCC" case here.
132 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
133 << F->getName() << "\n");
134 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000135 DSGraph &G = getDSGraph(*F);
136 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
137 calculateGraph(G);
138 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
139 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
140 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000141
Chris Lattnerae5f6032002-11-17 22:16:28 +0000142 if (MaxSCC < 1) MaxSCC = 1;
143
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000145 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000146 ValMap.erase(F);
147 return calculateGraphs(F, Stack, NextID, ValMap);
148 } else {
149 ValMap[F] = ~0U;
150 }
151 return MyID;
152
153 } else {
154 // SCCFunctions - Keep track of the functions in the current SCC
155 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000156 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000157
158 Function *NF;
159 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000160 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000161 do {
162 NF = *--FirstInSCC;
163 ValMap[NF] = ~0U;
164 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000165
166 // Figure out which graph is the largest one, in order to speed things up
167 // a bit in situations where functions in the SCC have widely different
168 // graph sizes.
169 DSGraph &NFGraph = getDSGraph(*NF);
170 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
171 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000172 } while (NF != F);
173
Chris Lattner0eea6182003-06-30 05:09:58 +0000174 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
175 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000176
Chris Lattnerae5f6032002-11-17 22:16:28 +0000177 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000178 if (MaxSCC < SCCFunctions.size())
179 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000180
Chris Lattner0eea6182003-06-30 05:09:58 +0000181 // First thing first, collapse all of the DSGraphs into a single graph for
182 // the entire SCC. We computed the largest graph, so clone all of the other
183 // (smaller) graphs into it. Discard all of the old graphs.
184 //
185 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
186 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000187 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000188 if (&G != SCCGraph) {
189 DSGraph::NodeMapTy NodeMap;
190 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
191 SCCGraph->getReturnNodes(), NodeMap, 0);
192 // Update the DSInfo map and delete the old graph...
193 DSInfo[*I] = SCCGraph;
194 delete &G;
195 }
196 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000197
Chris Lattner744f9392003-07-02 04:37:48 +0000198 // Clean up the graph before we start inlining a bunch again...
199 SCCGraph->removeTriviallyDeadNodes();
200
Chris Lattner0eea6182003-06-30 05:09:58 +0000201 // Now that we have one big happy family, resolve all of the call sites in
202 // the graph...
203 calculateGraph(*SCCGraph);
204 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
205 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000206
207 std::cerr << "DONE with SCC #: " << MyID << "\n";
208
209 // We never have to revisit "SCC" processed functions...
210
211 // Drop the stuff we don't need from the end of the stack
212 Stack.erase(FirstInSCC, Stack.end());
213 return MyID;
214 }
215
216 return MyID; // == Min
217}
218
219
Chris Lattner0d9bab82002-07-18 00:12:30 +0000220// releaseMemory - If the pass pipeline is done with this pass, we can release
221// our memory... here...
222//
223void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000224 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
225 E = DSInfo.end(); I != E; ++I) {
226 I->second->getReturnNodes().erase(I->first);
227 if (I->second->getReturnNodes().empty())
228 delete I->second;
229 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000230
231 // Empty map so next time memory is released, data structures are not
232 // re-deleted.
233 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000234 delete GlobalsGraph;
235 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000236}
237
Chris Lattner0eea6182003-06-30 05:09:58 +0000238void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000239 // Move our call site list into TempFCs so that inline call sites go into the
240 // new call site list and doesn't invalidate our iterators!
241 std::vector<DSCallSite> TempFCs;
242 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
243 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000244
Chris Lattner0eea6182003-06-30 05:09:58 +0000245 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
246
Chris Lattnera9c9c022002-11-11 21:35:13 +0000247 // Loop over all of the resolvable call sites
248 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000249 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
250 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000251 // If we skipped over any call sites, they must be unresolvable, copy them
252 // to the real call site list.
253 LastCallSiteIdx++;
254 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
255 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
256 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000257
Chris Lattnera9c9c022002-11-11 21:35:13 +0000258 // Resolve the current call...
259 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000260 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000261
Chris Lattnera9c9c022002-11-11 21:35:13 +0000262 if (Callee->isExternal()) {
263 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000264 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000265 // Self recursion... simply link up the formal arguments with the
266 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000267 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000268
269 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000270 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000271
272 } else {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000273 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
274 Callee));
Chris Lattner744f9392003-07-02 04:37:48 +0000275
Chris Lattnera9c9c022002-11-11 21:35:13 +0000276 // Get the data structure graph for the called function.
277 //
278 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
279
280 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000281 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000282 << GI.getAuxFunctionCalls().size() << "] into '"
283 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000284 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000285
286 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000287 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000288 DSGraph::KeepModRefBits |
289 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000290 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000291
292#if 0
293 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
294 Callee->getName());
295#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000296 }
297 }
298
299 // Make sure to catch any leftover unresolvable calls...
300 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
301 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
302
303 TempFCs.clear();
304
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000305 // Re-materialize nodes from the globals graph.
306 // Do not ignore globals inlined from callees -- they are not up-to-date!
307 Graph.getInlinedGlobals().clear();
308 Graph.updateFromGlobalGraph();
309
310 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000311 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000312 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000313
314 // Delete dead nodes. Treat globals that are unreachable but that can
315 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000316 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000317
Chris Lattnera9c9c022002-11-11 21:35:13 +0000318 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000319}
320
Brian Gaeked0fde302003-11-11 22:41:34 +0000321} // End llvm namespace