blob: f5d435fe45d4ad130745c9d06118198dd9d4572a [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 Lattneraa0b4682002-11-09 21:12:07 +000051 return false;
52}
Chris Lattner55c10582002-10-03 20:38:41 +000053
Chris Lattnera9c9c022002-11-11 21:35:13 +000054void BUDataStructures::calculateReachableGraphs(Function *F) {
55 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000056 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000057 unsigned NextID = 1;
58 calculateGraphs(F, Stack, NextID, ValMap);
59}
60
61DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
62 // Has the graph already been created?
63 DSGraph *&Graph = DSInfo[F];
64 if (Graph) return *Graph;
65
66 // Copy the local version into DSInfo...
67 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
68
69 Graph->setGlobalsGraph(GlobalsGraph);
70 Graph->setPrintAuxCalls();
71
72 // Start with a copy of the original call sites...
73 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
74 return *Graph;
75}
76
77unsigned BUDataStructures::calculateGraphs(Function *F,
78 std::vector<Function*> &Stack,
79 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000080 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000081 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
82 unsigned Min = NextID++, MyID = Min;
83 ValMap[F] = Min;
84 Stack.push_back(F);
85
86 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
87 // No callees!
88 Stack.pop_back();
89 ValMap[F] = ~0;
90 return Min;
91 }
92
93 DSGraph &Graph = getOrCreateGraph(F);
94
95 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000096 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
97 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000098 Function *Callee = *I;
99 unsigned M;
100 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000101 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000102 if (It == ValMap.end()) // No, visit it now.
103 M = calculateGraphs(Callee, Stack, NextID, ValMap);
104 else // Yes, get it's number.
105 M = It->second;
106 if (M < Min) Min = M;
107 }
108
109 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
110 if (Min != MyID)
111 return Min; // This is part of a larger SCC!
112
113 // If this is a new SCC, process it now.
114 if (Stack.back() == F) { // Special case the single "SCC" case here.
115 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
116 << F->getName() << "\n");
117 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000118 DSGraph &G = getDSGraph(*F);
119 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
120 calculateGraph(G);
121 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
122 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
123 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000124
Chris Lattnerae5f6032002-11-17 22:16:28 +0000125 if (MaxSCC < 1) MaxSCC = 1;
126
Chris Lattnera9c9c022002-11-11 21:35:13 +0000127 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000128 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000129 ValMap.erase(F);
130 return calculateGraphs(F, Stack, NextID, ValMap);
131 } else {
132 ValMap[F] = ~0U;
133 }
134 return MyID;
135
136 } else {
137 // SCCFunctions - Keep track of the functions in the current SCC
138 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000139 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000140
141 Function *NF;
142 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000143 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144 do {
145 NF = *--FirstInSCC;
146 ValMap[NF] = ~0U;
147 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000148
149 // Figure out which graph is the largest one, in order to speed things up
150 // a bit in situations where functions in the SCC have widely different
151 // graph sizes.
152 DSGraph &NFGraph = getDSGraph(*NF);
153 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
154 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000155 } while (NF != F);
156
Chris Lattner0eea6182003-06-30 05:09:58 +0000157 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
158 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000159
Chris Lattnerae5f6032002-11-17 22:16:28 +0000160 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000161 if (MaxSCC < SCCFunctions.size())
162 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000163
Chris Lattner0eea6182003-06-30 05:09:58 +0000164 // First thing first, collapse all of the DSGraphs into a single graph for
165 // the entire SCC. We computed the largest graph, so clone all of the other
166 // (smaller) graphs into it. Discard all of the old graphs.
167 //
168 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
169 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000170 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000171 if (&G != SCCGraph) {
172 DSGraph::NodeMapTy NodeMap;
173 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
174 SCCGraph->getReturnNodes(), NodeMap, 0);
175 // Update the DSInfo map and delete the old graph...
176 DSInfo[*I] = SCCGraph;
177 delete &G;
178 }
179 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000180
Chris Lattner744f9392003-07-02 04:37:48 +0000181 // Clean up the graph before we start inlining a bunch again...
182 SCCGraph->removeTriviallyDeadNodes();
183
Chris Lattner0eea6182003-06-30 05:09:58 +0000184 // Now that we have one big happy family, resolve all of the call sites in
185 // the graph...
186 calculateGraph(*SCCGraph);
187 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
188 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000189
190 std::cerr << "DONE with SCC #: " << MyID << "\n";
191
192 // We never have to revisit "SCC" processed functions...
193
194 // Drop the stuff we don't need from the end of the stack
195 Stack.erase(FirstInSCC, Stack.end());
196 return MyID;
197 }
198
199 return MyID; // == Min
200}
201
202
Chris Lattner0d9bab82002-07-18 00:12:30 +0000203// releaseMemory - If the pass pipeline is done with this pass, we can release
204// our memory... here...
205//
206void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000207 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
208 E = DSInfo.end(); I != E; ++I) {
209 I->second->getReturnNodes().erase(I->first);
210 if (I->second->getReturnNodes().empty())
211 delete I->second;
212 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000213
214 // Empty map so next time memory is released, data structures are not
215 // re-deleted.
216 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000217 delete GlobalsGraph;
218 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000219}
220
Chris Lattner0eea6182003-06-30 05:09:58 +0000221void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000222 // Move our call site list into TempFCs so that inline call sites go into the
223 // new call site list and doesn't invalidate our iterators!
224 std::vector<DSCallSite> TempFCs;
225 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
226 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000227
Chris Lattner0eea6182003-06-30 05:09:58 +0000228 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
229
Chris Lattnera9c9c022002-11-11 21:35:13 +0000230 // Loop over all of the resolvable call sites
231 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000232 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
233 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000234 // If we skipped over any call sites, they must be unresolvable, copy them
235 // to the real call site list.
236 LastCallSiteIdx++;
237 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
238 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
239 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000240
Chris Lattnera9c9c022002-11-11 21:35:13 +0000241 // Resolve the current call...
242 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000243 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000244
Chris Lattnera9c9c022002-11-11 21:35:13 +0000245 if (Callee->isExternal()) {
246 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000247 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000248 // Self recursion... simply link up the formal arguments with the
249 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000250 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000251
252 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000253 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000254
255 } else {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000256 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
257 Callee));
Chris Lattner744f9392003-07-02 04:37:48 +0000258
Chris Lattnera9c9c022002-11-11 21:35:13 +0000259 // Get the data structure graph for the called function.
260 //
261 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
262
263 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000264 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000265 << GI.getAuxFunctionCalls().size() << "] into '"
266 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000267 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000268
269 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000270 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000271 DSGraph::KeepModRefBits |
272 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000273 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000274
275#if 0
276 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
277 Callee->getName());
278#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000279 }
280 }
281
282 // Make sure to catch any leftover unresolvable calls...
283 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
284 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
285
286 TempFCs.clear();
287
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000288 // Re-materialize nodes from the globals graph.
289 // Do not ignore globals inlined from callees -- they are not up-to-date!
290 Graph.getInlinedGlobals().clear();
291 Graph.updateFromGlobalGraph();
292
293 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000294 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000295 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000296
297 // Delete dead nodes. Treat globals that are unreachable but that can
298 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000299 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000300
Chris Lattnera9c9c022002-11-11 21:35:13 +0000301 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000302}
303