blob: 31b8021613171965be896913024fdca8bce13fc0 [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
Chris Lattner2b4c8df2003-06-30 05:27:53 +000010#include "DSCallSiteIterator.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000011#include "llvm/Analysis/DataStructure.h"
12#include "llvm/Module.h"
Chris Lattnerfccd06f2002-10-01 22:33:50 +000013#include "Support/Statistic.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");
17
18 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000019 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000020}
Chris Lattner0d9bab82002-07-18 00:12:30 +000021
Chris Lattnerb1060432002-11-07 05:20:53 +000022using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000023
Chris Lattneraa0b4682002-11-09 21:12:07 +000024// run - Calculate the bottom up data structure graphs for each function in the
25// program.
26//
27bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000028 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
29 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000030 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000031
Chris Lattnera9c9c022002-11-11 21:35:13 +000032 Function *MainFunc = M.getMainFunction();
33 if (MainFunc)
34 calculateReachableGraphs(MainFunc);
35
36 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000037 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +000038 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000039#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000040 if (MainFunc)
41 std::cerr << "*** Function unreachable from main: "
42 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000043#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +000044 calculateReachableGraphs(I); // Calculate all graphs...
45 }
Chris Lattneraa0b4682002-11-09 21:12:07 +000046 return false;
47}
Chris Lattner55c10582002-10-03 20:38:41 +000048
Chris Lattnera9c9c022002-11-11 21:35:13 +000049void BUDataStructures::calculateReachableGraphs(Function *F) {
50 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +000051 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +000052 unsigned NextID = 1;
53 calculateGraphs(F, Stack, NextID, ValMap);
54}
55
56DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
57 // Has the graph already been created?
58 DSGraph *&Graph = DSInfo[F];
59 if (Graph) return *Graph;
60
61 // Copy the local version into DSInfo...
62 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
63
64 Graph->setGlobalsGraph(GlobalsGraph);
65 Graph->setPrintAuxCalls();
66
67 // Start with a copy of the original call sites...
68 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
69 return *Graph;
70}
71
72unsigned BUDataStructures::calculateGraphs(Function *F,
73 std::vector<Function*> &Stack,
74 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000075 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000076 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
77 unsigned Min = NextID++, MyID = Min;
78 ValMap[F] = Min;
79 Stack.push_back(F);
80
81 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
82 // No callees!
83 Stack.pop_back();
84 ValMap[F] = ~0;
85 return Min;
86 }
87
88 DSGraph &Graph = getOrCreateGraph(F);
89
90 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000091 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
92 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +000093 Function *Callee = *I;
94 unsigned M;
95 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +000096 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +000097 if (It == ValMap.end()) // No, visit it now.
98 M = calculateGraphs(Callee, Stack, NextID, ValMap);
99 else // Yes, get it's number.
100 M = It->second;
101 if (M < Min) Min = M;
102 }
103
104 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
105 if (Min != MyID)
106 return Min; // This is part of a larger SCC!
107
108 // If this is a new SCC, process it now.
109 if (Stack.back() == F) { // Special case the single "SCC" case here.
110 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
111 << F->getName() << "\n");
112 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000113 DSGraph &G = getDSGraph(*F);
114 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
115 calculateGraph(G);
116 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
117 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
118 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000119
Chris Lattnerae5f6032002-11-17 22:16:28 +0000120 if (MaxSCC < 1) MaxSCC = 1;
121
Chris Lattnera9c9c022002-11-11 21:35:13 +0000122 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000123 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000124 ValMap.erase(F);
125 return calculateGraphs(F, Stack, NextID, ValMap);
126 } else {
127 ValMap[F] = ~0U;
128 }
129 return MyID;
130
131 } else {
132 // SCCFunctions - Keep track of the functions in the current SCC
133 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000134 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000135
136 Function *NF;
137 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000138 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000139 do {
140 NF = *--FirstInSCC;
141 ValMap[NF] = ~0U;
142 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000143
144 // Figure out which graph is the largest one, in order to speed things up
145 // a bit in situations where functions in the SCC have widely different
146 // graph sizes.
147 DSGraph &NFGraph = getDSGraph(*NF);
148 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
149 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000150 } while (NF != F);
151
Chris Lattner0eea6182003-06-30 05:09:58 +0000152 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
153 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000154
Chris Lattnerae5f6032002-11-17 22:16:28 +0000155 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000156 if (MaxSCC < SCCFunctions.size())
157 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000158
Chris Lattner0eea6182003-06-30 05:09:58 +0000159 // First thing first, collapse all of the DSGraphs into a single graph for
160 // the entire SCC. We computed the largest graph, so clone all of the other
161 // (smaller) graphs into it. Discard all of the old graphs.
162 //
163 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
164 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000165 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000166 if (&G != SCCGraph) {
167 DSGraph::NodeMapTy NodeMap;
168 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
169 SCCGraph->getReturnNodes(), NodeMap, 0);
170 // Update the DSInfo map and delete the old graph...
171 DSInfo[*I] = SCCGraph;
172 delete &G;
173 }
174 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000175
Chris Lattner0eea6182003-06-30 05:09:58 +0000176 // Now that we have one big happy family, resolve all of the call sites in
177 // the graph...
178 calculateGraph(*SCCGraph);
179 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
180 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000181
182 std::cerr << "DONE with SCC #: " << MyID << "\n";
183
184 // We never have to revisit "SCC" processed functions...
185
186 // Drop the stuff we don't need from the end of the stack
187 Stack.erase(FirstInSCC, Stack.end());
188 return MyID;
189 }
190
191 return MyID; // == Min
192}
193
194
Chris Lattner0d9bab82002-07-18 00:12:30 +0000195// releaseMemory - If the pass pipeline is done with this pass, we can release
196// our memory... here...
197//
198void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000199 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
200 E = DSInfo.end(); I != E; ++I) {
201 I->second->getReturnNodes().erase(I->first);
202 if (I->second->getReturnNodes().empty())
203 delete I->second;
204 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000205
206 // Empty map so next time memory is released, data structures are not
207 // re-deleted.
208 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000209 delete GlobalsGraph;
210 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000211}
212
Chris Lattner0eea6182003-06-30 05:09:58 +0000213void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000214 // Move our call site list into TempFCs so that inline call sites go into the
215 // new call site list and doesn't invalidate our iterators!
216 std::vector<DSCallSite> TempFCs;
217 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
218 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000219
Chris Lattner0eea6182003-06-30 05:09:58 +0000220 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
221
Chris Lattnera9c9c022002-11-11 21:35:13 +0000222 // Loop over all of the resolvable call sites
223 unsigned LastCallSiteIdx = ~0U;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000224 for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
225 E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000226 // If we skipped over any call sites, they must be unresolvable, copy them
227 // to the real call site list.
228 LastCallSiteIdx++;
229 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
230 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
231 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000232
Chris Lattnera9c9c022002-11-11 21:35:13 +0000233 // Resolve the current call...
234 Function *Callee = *I;
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000235 const DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000236
Chris Lattnera9c9c022002-11-11 21:35:13 +0000237 if (Callee->isExternal()) {
238 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000239 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000240 // Self recursion... simply link up the formal arguments with the
241 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000242 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000243
244 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000245 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000246
247 } else {
248 // Get the data structure graph for the called function.
249 //
250 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
251
252 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000253 << "[" << GI.getGraphSize() << "+"
Chris Lattner0eea6182003-06-30 05:09:58 +0000254 << GI.getAuxFunctionCalls().size() << "] into ["
255 << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000256 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000257
258 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000259 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000260 DSGraph::KeepModRefBits |
261 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerae5f6032002-11-17 22:16:28 +0000262
263#if 0
264 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
265 Callee->getName());
266#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000267 }
268 }
269
270 // Make sure to catch any leftover unresolvable calls...
271 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
272 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
273
274 TempFCs.clear();
275
276 // Recompute the Incomplete markers. If there are any function calls left
277 // now that are complete, we must loop!
278 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000279 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000280 // FIXME: materialize nodes from the globals graph as neccesary...
Chris Lattner394471f2003-01-23 22:05:33 +0000281 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000282
Chris Lattnera9c9c022002-11-11 21:35:13 +0000283 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000284}
285