blob: e2d6f6f0f426bd51d1be564fca26eada70d1fcd1 [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 Lattnerae5f6032002-11-17 22:16:28 +000018
19 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000020 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000021}
Chris Lattner0d9bab82002-07-18 00:12:30 +000022
Chris Lattnerb1060432002-11-07 05:20:53 +000023using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000024
Chris Lattneraa0b4682002-11-09 21:12:07 +000025// run - Calculate the bottom up data structure graphs for each function in the
26// program.
27//
28bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000029 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
30 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000031 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000032
Chris Lattnera9c9c022002-11-11 21:35:13 +000033 Function *MainFunc = M.getMainFunction();
34 if (MainFunc)
35 calculateReachableGraphs(MainFunc);
36
37 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000038 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000039 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000040#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000041 if (MainFunc)
42 std::cerr << "*** Function unreachable from main: "
43 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000044#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000045 calculateReachableGraphs(I); // Calculate all graphs...
46 }
Chris Lattneraa0b4682002-11-09 21:12:07 +000047 return false;
48}
Chris Lattner55c10582002-10-03 20:38:41 +000049
Chris Lattnera9c9c022002-11-11 21:35:13 +000050void BUDataStructures::calculateReachableGraphs(Function *F) {
51 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000052 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000053 unsigned NextID = 1;
54 calculateGraphs(F, Stack, NextID, ValMap);
55}
56
57DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
58 // Has the graph already been created?
59 DSGraph *&Graph = DSInfo[F];
60 if (Graph) return *Graph;
61
62 // Copy the local version into DSInfo...
63 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
64
65 Graph->setGlobalsGraph(GlobalsGraph);
66 Graph->setPrintAuxCalls();
67
68 // Start with a copy of the original call sites...
69 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
70 return *Graph;
71}
72
73unsigned BUDataStructures::calculateGraphs(Function *F,
74 std::vector<Function*> &Stack,
75 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000076 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000077 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
78 unsigned Min = NextID++, MyID = Min;
79 ValMap[F] = Min;
80 Stack.push_back(F);
81
82 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
83 // No callees!
84 Stack.pop_back();
85 ValMap[F] = ~0;
86 return Min;
87 }
88
89 DSGraph &Graph = getOrCreateGraph(F);
90
91 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000092 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
93 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000094 Function *Callee = *I;
95 unsigned M;
96 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +000097 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +000098 if (It == ValMap.end()) // No, visit it now.
99 M = calculateGraphs(Callee, Stack, NextID, ValMap);
100 else // Yes, get it's number.
101 M = It->second;
102 if (M < Min) Min = M;
103 }
104
105 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
106 if (Min != MyID)
107 return Min; // This is part of a larger SCC!
108
109 // If this is a new SCC, process it now.
110 if (Stack.back() == F) { // Special case the single "SCC" case here.
111 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
112 << F->getName() << "\n");
113 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000114 DSGraph &G = getDSGraph(*F);
115 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
116 calculateGraph(G);
117 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
118 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
119 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000120
Chris Lattnerae5f6032002-11-17 22:16:28 +0000121 if (MaxSCC < 1) MaxSCC = 1;
122
Chris Lattnera9c9c022002-11-11 21:35:13 +0000123 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000124 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000125 ValMap.erase(F);
126 return calculateGraphs(F, Stack, NextID, ValMap);
127 } else {
128 ValMap[F] = ~0U;
129 }
130 return MyID;
131
132 } else {
133 // SCCFunctions - Keep track of the functions in the current SCC
134 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000135 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000136
137 Function *NF;
138 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000139 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000140 do {
141 NF = *--FirstInSCC;
142 ValMap[NF] = ~0U;
143 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000144
145 // Figure out which graph is the largest one, in order to speed things up
146 // a bit in situations where functions in the SCC have widely different
147 // graph sizes.
148 DSGraph &NFGraph = getDSGraph(*NF);
149 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
150 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000151 } while (NF != F);
152
Chris Lattner0eea6182003-06-30 05:09:58 +0000153 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
154 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000155
Chris Lattnerae5f6032002-11-17 22:16:28 +0000156 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000157 if (MaxSCC < SCCFunctions.size())
158 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000159
Chris Lattner0eea6182003-06-30 05:09:58 +0000160 // First thing first, collapse all of the DSGraphs into a single graph for
161 // the entire SCC. We computed the largest graph, so clone all of the other
162 // (smaller) graphs into it. Discard all of the old graphs.
163 //
164 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
165 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000166 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000167 if (&G != SCCGraph) {
168 DSGraph::NodeMapTy NodeMap;
169 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
170 SCCGraph->getReturnNodes(), NodeMap, 0);
171 // Update the DSInfo map and delete the old graph...
172 DSInfo[*I] = SCCGraph;
173 delete &G;
174 }
175 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000176
Chris Lattner744f9392003-07-02 04:37:48 +0000177 // Clean up the graph before we start inlining a bunch again...
178 SCCGraph->removeTriviallyDeadNodes();
179
Chris Lattner0eea6182003-06-30 05:09:58 +0000180 // Now that we have one big happy family, resolve all of the call sites in
181 // the graph...
182 calculateGraph(*SCCGraph);
183 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
184 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000185
186 std::cerr << "DONE with SCC #: " << MyID << "\n";
187
188 // We never have to revisit "SCC" processed functions...
189
190 // Drop the stuff we don't need from the end of the stack
191 Stack.erase(FirstInSCC, Stack.end());
192 return MyID;
193 }
194
195 return MyID; // == Min
196}
197
198
Chris Lattner0d9bab82002-07-18 00:12:30 +0000199// releaseMemory - If the pass pipeline is done with this pass, we can release
200// our memory... here...
201//
202void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000203 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
204 E = DSInfo.end(); I != E; ++I) {
205 I->second->getReturnNodes().erase(I->first);
206 if (I->second->getReturnNodes().empty())
207 delete I->second;
208 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000209
210 // Empty map so next time memory is released, data structures are not
211 // re-deleted.
212 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000213 delete GlobalsGraph;
214 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000215}
216
Chris Lattner0eea6182003-06-30 05:09:58 +0000217void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000218 // Move our call site list into TempFCs so that inline call sites go into the
219 // new call site list and doesn't invalidate our iterators!
220 std::vector<DSCallSite> TempFCs;
221 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
222 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000223
Chris Lattner0eea6182003-06-30 05:09:58 +0000224 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
225
Chris Lattnera9c9c022002-11-11 21:35:13 +0000226 // Loop over all of the resolvable call sites
227 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000228 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
229 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000230 // If we skipped over any call sites, they must be unresolvable, copy them
231 // to the real call site list.
232 LastCallSiteIdx++;
233 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
234 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
235 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000236
Chris Lattnera9c9c022002-11-11 21:35:13 +0000237 // Resolve the current call...
238 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000239 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000240
Chris Lattnera9c9c022002-11-11 21:35:13 +0000241 if (Callee->isExternal()) {
242 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000243 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000244 // Self recursion... simply link up the formal arguments with the
245 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000246 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000247
248 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000249 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000250
251 } else {
Chris Lattner744f9392003-07-02 04:37:48 +0000252 ActualCallees.insert(std::make_pair(&CS.getCallInst(), Callee));
253
Chris Lattnera9c9c022002-11-11 21:35:13 +0000254 // Get the data structure graph for the called function.
255 //
256 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
257
258 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000259 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000260 << GI.getAuxFunctionCalls().size() << "] into '"
261 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000262 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000263
264 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000265 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000266 DSGraph::KeepModRefBits |
267 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000268 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000269
270#if 0
271 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
272 Callee->getName());
273#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000274 }
275 }
276
277 // Make sure to catch any leftover unresolvable calls...
278 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
279 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
280
281 TempFCs.clear();
282
283 // Recompute the Incomplete markers. If there are any function calls left
284 // now that are complete, we must loop!
285 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000286 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000287 // FIXME: materialize nodes from the globals graph as neccesary...
Chris Lattner394471f2003-01-23 22:05:33 +0000288 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000289
Chris Lattnera9c9c022002-11-11 21:35:13 +0000290 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000291}
292