blob: 67938b7cde59eb3d199f1e75fa72816136ff16d2 [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
Chris Lattner8adbec82004-07-07 06:35:22 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000018#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/Statistic.h"
20#include "llvm/Support/Debug.h"
Chris Lattner5d5b6d62003-07-01 16:04:18 +000021#include "DSCallSiteIterator.h"
Chris Lattner9a927292003-11-12 23:11:14 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerae5f6032002-11-17 22:16:28 +000024namespace {
25 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000026 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000027 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000028
29 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000030 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000031}
Chris Lattner0d9bab82002-07-18 00:12:30 +000032
Chris Lattnerb1060432002-11-07 05:20:53 +000033using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000034
Chris Lattneraa0b4682002-11-09 21:12:07 +000035// run - Calculate the bottom up data structure graphs for each function in the
36// program.
37//
Chris Lattnerb12914b2004-09-20 04:48:05 +000038bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000039 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000041 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000042
Chris Lattnera9c9c022002-11-11 21:35:13 +000043 Function *MainFunc = M.getMainFunction();
44 if (MainFunc)
45 calculateReachableGraphs(MainFunc);
46
47 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000048 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000049 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000050#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000051 if (MainFunc)
52 std::cerr << "*** Function unreachable from main: "
53 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000054#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000055 calculateReachableGraphs(I); // Calculate all graphs...
56 }
Chris Lattner6c874612003-07-02 23:42:48 +000057
58 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000059
60 // At the end of the bottom-up pass, the globals graph becomes complete.
61 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000062 // nothing! In particular, externally visible globals and unresolvable call
63 // nodes at the end of the BU phase should make things that they point to
64 // incomplete in the globals graph.
65 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +000066 GlobalsGraph->removeTriviallyDeadNodes();
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 Lattner6acfe922003-11-13 05:04:19 +000098 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +000099 unsigned Min = NextID++, MyID = Min;
100 ValMap[F] = Min;
101 Stack.push_back(F);
102
Chris Lattner16437ff2004-03-04 17:05:28 +0000103 // FIXME! This test should be generalized to be any function that we have
104 // already processed, in the case when there isn't a main or there are
105 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000106 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
107 // No callees!
108 Stack.pop_back();
109 ValMap[F] = ~0;
110 return Min;
111 }
112
113 DSGraph &Graph = getOrCreateGraph(F);
114
115 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000116 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
117 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000118 Function *Callee = *I;
119 unsigned M;
120 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000121 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000122 if (It == ValMap.end()) // No, visit it now.
123 M = calculateGraphs(Callee, Stack, NextID, ValMap);
124 else // Yes, get it's number.
125 M = It->second;
126 if (M < Min) Min = M;
127 }
128
129 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
130 if (Min != MyID)
131 return Min; // This is part of a larger SCC!
132
133 // If this is a new SCC, process it now.
134 if (Stack.back() == F) { // Special case the single "SCC" case here.
135 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
136 << F->getName() << "\n");
137 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000138 DSGraph &G = getDSGraph(*F);
139 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
140 calculateGraph(G);
141 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
142 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
143 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144
Chris Lattnerae5f6032002-11-17 22:16:28 +0000145 if (MaxSCC < 1) MaxSCC = 1;
146
Chris Lattnera9c9c022002-11-11 21:35:13 +0000147 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000148 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000149 ValMap.erase(F);
150 return calculateGraphs(F, Stack, NextID, ValMap);
151 } else {
152 ValMap[F] = ~0U;
153 }
154 return MyID;
155
156 } else {
157 // SCCFunctions - Keep track of the functions in the current SCC
158 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000159 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000160
161 Function *NF;
162 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000163 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000164 do {
165 NF = *--FirstInSCC;
166 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000167
168 // Figure out which graph is the largest one, in order to speed things up
169 // a bit in situations where functions in the SCC have widely different
170 // graph sizes.
171 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000172 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000173 // FIXME: If we used a better way of cloning graphs (ie, just splice all
174 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000175 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
176 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000177 } while (NF != F);
178
Chris Lattner0eea6182003-06-30 05:09:58 +0000179 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000180 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000181
Chris Lattnerae5f6032002-11-17 22:16:28 +0000182 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000183 if (MaxSCC < SCCGraphs.size())
184 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000185
Chris Lattner0eea6182003-06-30 05:09:58 +0000186 // First thing first, collapse all of the DSGraphs into a single graph for
187 // the entire SCC. We computed the largest graph, so clone all of the other
188 // (smaller) graphs into it. Discard all of the old graphs.
189 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000190 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
191 E = SCCGraphs.end(); I != E; ++I) {
192 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000193 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000194 {
195 DSGraph::NodeMapTy NodeMap;
196 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
197 SCCGraph->getReturnNodes(), NodeMap);
198 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000199 // Update the DSInfo map and delete the old graph...
Chris Lattnera67138d2004-01-31 21:02:18 +0000200 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
201 E = G.getReturnNodes().end(); I != E; ++I)
202 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000203 delete &G;
204 }
205 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000206
Chris Lattner744f9392003-07-02 04:37:48 +0000207 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000208 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000209
Chris Lattner0eea6182003-06-30 05:09:58 +0000210 // Now that we have one big happy family, resolve all of the call sites in
211 // the graph...
212 calculateGraph(*SCCGraph);
213 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
214 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000215
216 std::cerr << "DONE with SCC #: " << MyID << "\n";
217
218 // We never have to revisit "SCC" processed functions...
219
220 // Drop the stuff we don't need from the end of the stack
221 Stack.erase(FirstInSCC, Stack.end());
222 return MyID;
223 }
224
225 return MyID; // == Min
226}
227
228
Chris Lattner0d9bab82002-07-18 00:12:30 +0000229// releaseMemory - If the pass pipeline is done with this pass, we can release
230// our memory... here...
231//
232void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000233 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
234 E = DSInfo.end(); I != E; ++I) {
235 I->second->getReturnNodes().erase(I->first);
236 if (I->second->getReturnNodes().empty())
237 delete I->second;
238 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000239
240 // Empty map so next time memory is released, data structures are not
241 // re-deleted.
242 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000243 delete GlobalsGraph;
244 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000245}
246
Chris Lattner0eea6182003-06-30 05:09:58 +0000247void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000248 // Move our call site list into TempFCs so that inline call sites go into the
249 // new call site list and doesn't invalidate our iterators!
250 std::vector<DSCallSite> TempFCs;
251 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
252 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000253
Chris Lattner0eea6182003-06-30 05:09:58 +0000254 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
255
Chris Lattnera9c9c022002-11-11 21:35:13 +0000256 // Loop over all of the resolvable call sites
257 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000258 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
259 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000260 // If we skipped over any call sites, they must be unresolvable, copy them
261 // to the real call site list.
262 LastCallSiteIdx++;
263 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
264 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
265 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000266
Chris Lattnera9c9c022002-11-11 21:35:13 +0000267 // Resolve the current call...
268 Function *Callee = *I;
Chris Lattner744f9392003-07-02 04:37:48 +0000269 DSCallSite CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000270
Chris Lattnera9c9c022002-11-11 21:35:13 +0000271 if (Callee->isExternal()) {
272 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner6acfe922003-11-13 05:04:19 +0000273 } else if (ReturnNodes.count(Callee)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000274 // Self recursion... simply link up the formal arguments with the
275 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000276 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000277
278 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000279 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000280
281 } else {
Chris Lattner808a7ae2003-09-20 16:34:13 +0000282 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
283 Callee));
Chris Lattner744f9392003-07-02 04:37:48 +0000284
Chris Lattnera9c9c022002-11-11 21:35:13 +0000285 // Get the data structure graph for the called function.
286 //
287 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
Chris Lattner0321b682004-02-27 20:05:15 +0000288
Chris Lattnera9c9c022002-11-11 21:35:13 +0000289 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000290 << "[" << GI.getGraphSize() << "+"
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000291 << GI.getAuxFunctionCalls().size() << "] into '"
292 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000293 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner5a540632003-06-30 03:15:25 +0000294 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000295 DSGraph::KeepModRefBits |
296 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerd391d702003-07-02 20:24:42 +0000297 ++NumBUInlines;
Chris Lattnerae5f6032002-11-17 22:16:28 +0000298
299#if 0
300 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
301 Callee->getName());
302#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000303 }
304 }
305
306 // Make sure to catch any leftover unresolvable calls...
307 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
308 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
309
310 TempFCs.clear();
311
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000312 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000313 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000314 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000315 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000316
317 // Delete dead nodes. Treat globals that are unreachable but that can
318 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000319 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000320
Chris Lattner35679372004-02-21 00:30:28 +0000321 // When this graph is finalized, clone the globals in the graph into the
322 // globals graph to make sure it has everything, from all graphs.
323 DSScalarMap &MainSM = Graph.getScalarMap();
324 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
325
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000326 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000327 // globals graph.
328 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
329 E = MainSM.global_end(); I != E; ++I)
330 RC.getClonedNH(MainSM[*I]);
331
Chris Lattnera9c9c022002-11-11 21:35:13 +0000332 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000333}