blob: aff74ac92064070a64b75b9b205b2964ecb13749 [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 Lattner5d5b6d62003-07-01 16:04:18 +000013#include "DSCallSiteIterator.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000014
Chris Lattnerae5f6032002-11-17 22:16:28 +000015namespace {
16 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000017 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000018 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000019
20 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000021 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000022}
Chris Lattner0d9bab82002-07-18 00:12:30 +000023
Chris Lattnerb1060432002-11-07 05:20:53 +000024using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000025
Chris Lattneraa0b4682002-11-09 21:12:07 +000026// run - Calculate the bottom up data structure graphs for each function in the
27// program.
28//
29bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000030 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
31 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000032 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000033
Chris Lattnera9c9c022002-11-11 21:35:13 +000034 Function *MainFunc = M.getMainFunction();
35 if (MainFunc)
36 calculateReachableGraphs(MainFunc);
37
38 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000039 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000040 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000041#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000042 if (MainFunc)
43 std::cerr << "*** Function unreachable from main: "
44 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000045#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000046 calculateReachableGraphs(I); // Calculate all graphs...
47 }
Chris Lattner6c874612003-07-02 23:42:48 +000048
49 NumCallEdges += ActualCallees.size();
Chris Lattneraa0b4682002-11-09 21:12:07 +000050 return false;
51}
Chris Lattner55c10582002-10-03 20:38:41 +000052
Chris Lattnera9c9c022002-11-11 21:35:13 +000053void BUDataStructures::calculateReachableGraphs(Function *F) {
54 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000055 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000056 unsigned NextID = 1;
57 calculateGraphs(F, Stack, NextID, ValMap);
58}
59
60DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
61 // Has the graph already been created?
62 DSGraph *&Graph = DSInfo[F];
63 if (Graph) return *Graph;
64
65 // Copy the local version into DSInfo...
66 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
67
68 Graph->setGlobalsGraph(GlobalsGraph);
69 Graph->setPrintAuxCalls();
70
71 // Start with a copy of the original call sites...
72 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
73 return *Graph;
74}
75
76unsigned BUDataStructures::calculateGraphs(Function *F,
77 std::vector<Function*> &Stack,
78 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000079 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000080 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
81 unsigned Min = NextID++, MyID = Min;
82 ValMap[F] = Min;
83 Stack.push_back(F);
84
85 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
86 // No callees!
87 Stack.pop_back();
88 ValMap[F] = ~0;
89 return Min;
90 }
91
92 DSGraph &Graph = getOrCreateGraph(F);
93
94 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000095 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
96 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000097 Function *Callee = *I;
98 unsigned M;
99 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000100 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000101 if (It == ValMap.end()) // No, visit it now.
102 M = calculateGraphs(Callee, Stack, NextID, ValMap);
103 else // Yes, get it's number.
104 M = It->second;
105 if (M < Min) Min = M;
106 }
107
108 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
109 if (Min != MyID)
110 return Min; // This is part of a larger SCC!
111
112 // If this is a new SCC, process it now.
113 if (Stack.back() == F) { // Special case the single "SCC" case here.
114 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
115 << F->getName() << "\n");
116 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000117 DSGraph &G = getDSGraph(*F);
118 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
119 calculateGraph(G);
120 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
121 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
122 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000123
Chris Lattnerae5f6032002-11-17 22:16:28 +0000124 if (MaxSCC < 1) MaxSCC = 1;
125
Chris Lattnera9c9c022002-11-11 21:35:13 +0000126 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000127 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000128 ValMap.erase(F);
129 return calculateGraphs(F, Stack, NextID, ValMap);
130 } else {
131 ValMap[F] = ~0U;
132 }
133 return MyID;
134
135 } else {
136 // SCCFunctions - Keep track of the functions in the current SCC
137 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000138 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000139
140 Function *NF;
141 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000142 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000143 do {
144 NF = *--FirstInSCC;
145 ValMap[NF] = ~0U;
146 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000147
148 // Figure out which graph is the largest one, in order to speed things up
149 // a bit in situations where functions in the SCC have widely different
150 // graph sizes.
151 DSGraph &NFGraph = getDSGraph(*NF);
152 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
153 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000154 } while (NF != F);
155
Chris Lattner0eea6182003-06-30 05:09:58 +0000156 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
157 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000158
Chris Lattnerae5f6032002-11-17 22:16:28 +0000159 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000160 if (MaxSCC < SCCFunctions.size())
161 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000162
Chris Lattner0eea6182003-06-30 05:09:58 +0000163 // First thing first, collapse all of the DSGraphs into a single graph for
164 // the entire SCC. We computed the largest graph, so clone all of the other
165 // (smaller) graphs into it. Discard all of the old graphs.
166 //
167 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
168 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000169 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000170 if (&G != SCCGraph) {
171 DSGraph::NodeMapTy NodeMap;
172 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
173 SCCGraph->getReturnNodes(), NodeMap, 0);
174 // Update the DSInfo map and delete the old graph...
175 DSInfo[*I] = SCCGraph;
176 delete &G;
177 }
178 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000179
Chris Lattner744f9392003-07-02 04:37:48 +0000180 // Clean up the graph before we start inlining a bunch again...
181 SCCGraph->removeTriviallyDeadNodes();
182
Chris Lattner0eea6182003-06-30 05:09:58 +0000183 // Now that we have one big happy family, resolve all of the call sites in
184 // the graph...
185 calculateGraph(*SCCGraph);
186 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
187 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000188
189 std::cerr << "DONE with SCC #: " << MyID << "\n";
190
191 // We never have to revisit "SCC" processed functions...
192
193 // Drop the stuff we don't need from the end of the stack
194 Stack.erase(FirstInSCC, Stack.end());
195 return MyID;
196 }
197
198 return MyID; // == Min
199}
200
201
Chris Lattner0d9bab82002-07-18 00:12:30 +0000202// releaseMemory - If the pass pipeline is done with this pass, we can release
203// our memory... here...
204//
205void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000206 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
207 E = DSInfo.end(); I != E; ++I) {
208 I->second->getReturnNodes().erase(I->first);
209 if (I->second->getReturnNodes().empty())
210 delete I->second;
211 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000212
213 // Empty map so next time memory is released, data structures are not
214 // re-deleted.
215 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000216 delete GlobalsGraph;
217 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000218}
219
Chris Lattner0eea6182003-06-30 05:09:58 +0000220void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000221 // Move our call site list into TempFCs so that inline call sites go into the
222 // new call site list and doesn't invalidate our iterators!
223 std::vector<DSCallSite> TempFCs;
224 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
225 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000226
Chris Lattner0eea6182003-06-30 05:09:58 +0000227 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
228
Chris Lattnera9c9c022002-11-11 21:35:13 +0000229 // Loop over all of the resolvable call sites
230 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000231 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
232 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000233 // If we skipped over any call sites, they must be unresolvable, copy them
234 // to the real call site list.
235 LastCallSiteIdx++;
236 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
237 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
238 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000239
Chris Lattnera9c9c022002-11-11 21:35:13 +0000240 // Resolve the current call...
241 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000242 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000243
Chris Lattnera9c9c022002-11-11 21:35:13 +0000244 if (Callee->isExternal()) {
245 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000246 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000247 // Self recursion... simply link up the formal arguments with the
248 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000249 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000250
251 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000252 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000253
254 } else {
Chris Lattner744f9392003-07-02 04:37:48 +0000255 ActualCallees.insert(std::make_pair(&CS.getCallInst(), Callee));
256
Chris Lattnera9c9c022002-11-11 21:35:13 +0000257 // Get the data structure graph for the called function.
258 //
259 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
260
261 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000262 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000263 << GI.getAuxFunctionCalls().size() << "] into '"
264 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000265 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000266
267 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000268 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000269 DSGraph::KeepModRefBits |
270 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000271 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000272
273#if 0
274 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
275 Callee->getName());
276#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000277 }
278 }
279
280 // Make sure to catch any leftover unresolvable calls...
281 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
282 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
283
284 TempFCs.clear();
285
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000286 // Re-materialize nodes from the globals graph.
287 // Do not ignore globals inlined from callees -- they are not up-to-date!
288 Graph.getInlinedGlobals().clear();
289 Graph.updateFromGlobalGraph();
290
291 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000292 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000293 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000294
295 // Delete dead nodes. Treat globals that are unreachable but that can
296 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000297 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000298
Chris Lattnera9c9c022002-11-11 21:35:13 +0000299 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000300}
301