blob: d515de6c350520fe5198ddda4cf599d2780e7546 [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"
Chris Lattner55c10582002-10-03 20:38:41 +000011#include "llvm/Analysis/DSGraph.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000012#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 Lattner923fc052003-02-05 21:59:58 +000024static bool isVAHackFn(const Function *F) {
25 return F->getName() == "printf" || F->getName() == "sscanf" ||
26 F->getName() == "fprintf" || F->getName() == "open" ||
27 F->getName() == "sprintf" || F->getName() == "fputs" ||
28 F->getName() == "fscanf";
29}
30
Chris Lattnera9c9c022002-11-11 21:35:13 +000031// isCompleteNode - Return true if we know all of the targets of this node, and
32// if the call sites are not external.
33//
34static inline bool isCompleteNode(DSNode *N) {
Chris Lattnerbd92b732003-06-19 21:15:11 +000035 if (N->isIncomplete()) return false;
Chris Lattnera9c9c022002-11-11 21:35:13 +000036 const std::vector<GlobalValue*> &Callees = N->getGlobals();
37 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
Chris Lattner923fc052003-02-05 21:59:58 +000038 if (Callees[i]->isExternal())
39 if (!isVAHackFn(cast<Function>(Callees[i])))
Chris Lattnera9c9c022002-11-11 21:35:13 +000040 return false; // External function found...
Chris Lattnera9c9c022002-11-11 21:35:13 +000041 return true; // otherwise ok
42}
43
44struct CallSiteIterator {
45 // FCs are the edges out of the current node are the call site targets...
46 std::vector<DSCallSite> *FCs;
47 unsigned CallSite;
48 unsigned CallSiteEntry;
49
50 CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) {
51 CallSite = 0; CallSiteEntry = 0;
Chris Lattner923fc052003-02-05 21:59:58 +000052 advanceToValidCallee();
Chris Lattnera9c9c022002-11-11 21:35:13 +000053 }
54
55 // End iterator ctor...
56 CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
57 CallSite = FCs->size(); CallSiteEntry = 0;
58 }
59
Chris Lattner923fc052003-02-05 21:59:58 +000060 void advanceToValidCallee() {
Chris Lattnera9c9c022002-11-11 21:35:13 +000061 while (CallSite < FCs->size()) {
Chris Lattner923fc052003-02-05 21:59:58 +000062 if ((*FCs)[CallSite].isDirectCall()) {
63 if (CallSiteEntry == 0 && // direct call only has one target...
64 (!(*FCs)[CallSite].getCalleeFunc()->isExternal() ||
65 isVAHackFn((*FCs)[CallSite].getCalleeFunc()))) // If not external
66 return;
67 } else {
68 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
Chris Lattnera9c9c022002-11-11 21:35:13 +000069 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
70 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
71
72 if (CallSiteEntry < Callees.size())
73 return;
74 }
Chris Lattnera9c9c022002-11-11 21:35:13 +000075 }
Chris Lattner923fc052003-02-05 21:59:58 +000076 CallSiteEntry = 0;
77 ++CallSite;
Chris Lattnera9c9c022002-11-11 21:35:13 +000078 }
79 }
80public:
81 static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); }
82 static CallSiteIterator end(DSGraph &G) {
83 return CallSiteIterator(G.getAuxFunctionCalls(), true);
84 }
85 static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
86 static CallSiteIterator end(std::vector<DSCallSite> &CSs) {
87 return CallSiteIterator(CSs, true);
88 }
89 bool operator==(const CallSiteIterator &CSI) const {
90 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
91 }
92 bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);}
93
94 unsigned getCallSiteIdx() const { return CallSite; }
95 DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
96
Chris Lattner923fc052003-02-05 21:59:58 +000097 Function *operator*() const {
98 if ((*FCs)[CallSite].isDirectCall()) {
99 return (*FCs)[CallSite].getCalleeFunc();
100 } else {
101 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
102 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
103 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000104 }
105
106 CallSiteIterator& operator++() { // Preincrement
107 ++CallSiteEntry;
Chris Lattner923fc052003-02-05 21:59:58 +0000108 advanceToValidCallee();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000109 return *this;
110 }
111 CallSiteIterator operator++(int) { // Postincrement
112 CallSiteIterator tmp = *this; ++*this; return tmp;
113 }
114};
115
116
117
Chris Lattneraa0b4682002-11-09 21:12:07 +0000118// run - Calculate the bottom up data structure graphs for each function in the
119// program.
120//
121bool BUDataStructures::run(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +0000122 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
123 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +0000124 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000125
Chris Lattnera9c9c022002-11-11 21:35:13 +0000126 Function *MainFunc = M.getMainFunction();
127 if (MainFunc)
128 calculateReachableGraphs(MainFunc);
129
130 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000131 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattnera9c9c022002-11-11 21:35:13 +0000132 if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000133#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000134 if (MainFunc)
135 std::cerr << "*** Function unreachable from main: "
136 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000137#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000138 calculateReachableGraphs(I); // Calculate all graphs...
139 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000140 return false;
141}
Chris Lattner55c10582002-10-03 20:38:41 +0000142
Chris Lattnera9c9c022002-11-11 21:35:13 +0000143void BUDataStructures::calculateReachableGraphs(Function *F) {
144 std::vector<Function*> Stack;
Chris Lattner41c04f72003-02-01 04:52:08 +0000145 hash_map<Function*, unsigned> ValMap;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000146 unsigned NextID = 1;
147 calculateGraphs(F, Stack, NextID, ValMap);
148}
149
150DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
151 // Has the graph already been created?
152 DSGraph *&Graph = DSInfo[F];
153 if (Graph) return *Graph;
154
155 // Copy the local version into DSInfo...
156 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
157
158 Graph->setGlobalsGraph(GlobalsGraph);
159 Graph->setPrintAuxCalls();
160
161 // Start with a copy of the original call sites...
162 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
163 return *Graph;
164}
165
166unsigned BUDataStructures::calculateGraphs(Function *F,
167 std::vector<Function*> &Stack,
168 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000169 hash_map<Function*, unsigned> &ValMap) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000170 assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
171 unsigned Min = NextID++, MyID = Min;
172 ValMap[F] = Min;
173 Stack.push_back(F);
174
175 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
176 // No callees!
177 Stack.pop_back();
178 ValMap[F] = ~0;
179 return Min;
180 }
181
182 DSGraph &Graph = getOrCreateGraph(F);
183
184 // The edges out of the current node are the call site targets...
185 for (CallSiteIterator I = CallSiteIterator::begin(Graph),
186 E = CallSiteIterator::end(Graph); I != E; ++I) {
187 Function *Callee = *I;
188 unsigned M;
189 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000190 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000191 if (It == ValMap.end()) // No, visit it now.
192 M = calculateGraphs(Callee, Stack, NextID, ValMap);
193 else // Yes, get it's number.
194 M = It->second;
195 if (M < Min) Min = M;
196 }
197
198 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
199 if (Min != MyID)
200 return Min; // This is part of a larger SCC!
201
202 // If this is a new SCC, process it now.
203 if (Stack.back() == F) { // Special case the single "SCC" case here.
204 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
205 << F->getName() << "\n");
206 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000207 DSGraph &G = getDSGraph(*F);
208 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
209 calculateGraph(G);
210 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
211 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
212 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000213
Chris Lattnerae5f6032002-11-17 22:16:28 +0000214 if (MaxSCC < 1) MaxSCC = 1;
215
Chris Lattnera9c9c022002-11-11 21:35:13 +0000216 // Should we revisit the graph?
217 if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) {
218 ValMap.erase(F);
219 return calculateGraphs(F, Stack, NextID, ValMap);
220 } else {
221 ValMap[F] = ~0U;
222 }
223 return MyID;
224
225 } else {
226 // SCCFunctions - Keep track of the functions in the current SCC
227 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000228 hash_set<Function*> SCCFunctions;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000229
230 Function *NF;
231 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000232 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000233 do {
234 NF = *--FirstInSCC;
235 ValMap[NF] = ~0U;
236 SCCFunctions.insert(NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000237
238 // Figure out which graph is the largest one, in order to speed things up
239 // a bit in situations where functions in the SCC have widely different
240 // graph sizes.
241 DSGraph &NFGraph = getDSGraph(*NF);
242 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
243 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000244 } while (NF != F);
245
Chris Lattner0eea6182003-06-30 05:09:58 +0000246 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
247 << SCCFunctions.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000248
Chris Lattnerae5f6032002-11-17 22:16:28 +0000249 // Compute the Max SCC Size...
Chris Lattner0eea6182003-06-30 05:09:58 +0000250 if (MaxSCC < SCCFunctions.size())
251 MaxSCC = SCCFunctions.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000252
Chris Lattner0eea6182003-06-30 05:09:58 +0000253 // First thing first, collapse all of the DSGraphs into a single graph for
254 // the entire SCC. We computed the largest graph, so clone all of the other
255 // (smaller) graphs into it. Discard all of the old graphs.
256 //
257 for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
258 E = SCCFunctions.end(); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000259 DSGraph &G = getDSGraph(**I);
Chris Lattner0eea6182003-06-30 05:09:58 +0000260 if (&G != SCCGraph) {
261 DSGraph::NodeMapTy NodeMap;
262 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
263 SCCGraph->getReturnNodes(), NodeMap, 0);
264 // Update the DSInfo map and delete the old graph...
265 DSInfo[*I] = SCCGraph;
266 delete &G;
267 }
268 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000269
Chris Lattner0eea6182003-06-30 05:09:58 +0000270 // Now that we have one big happy family, resolve all of the call sites in
271 // the graph...
272 calculateGraph(*SCCGraph);
273 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
274 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000275
276 std::cerr << "DONE with SCC #: " << MyID << "\n";
277
278 // We never have to revisit "SCC" processed functions...
279
280 // Drop the stuff we don't need from the end of the stack
281 Stack.erase(FirstInSCC, Stack.end());
282 return MyID;
283 }
284
285 return MyID; // == Min
286}
287
288
Chris Lattner0d9bab82002-07-18 00:12:30 +0000289// releaseMemory - If the pass pipeline is done with this pass, we can release
290// our memory... here...
291//
292void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000293 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
294 E = DSInfo.end(); I != E; ++I) {
295 I->second->getReturnNodes().erase(I->first);
296 if (I->second->getReturnNodes().empty())
297 delete I->second;
298 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000299
300 // Empty map so next time memory is released, data structures are not
301 // re-deleted.
302 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000303 delete GlobalsGraph;
304 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000305}
306
Chris Lattner0eea6182003-06-30 05:09:58 +0000307void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000308 // Move our call site list into TempFCs so that inline call sites go into the
309 // new call site list and doesn't invalidate our iterators!
310 std::vector<DSCallSite> TempFCs;
311 std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
312 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000313
Chris Lattner0eea6182003-06-30 05:09:58 +0000314 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
315
Chris Lattnera9c9c022002-11-11 21:35:13 +0000316 // Loop over all of the resolvable call sites
317 unsigned LastCallSiteIdx = ~0U;
318 for (CallSiteIterator I = CallSiteIterator::begin(TempFCs),
319 E = CallSiteIterator::end(TempFCs); I != E; ++I) {
320 // If we skipped over any call sites, they must be unresolvable, copy them
321 // to the real call site list.
322 LastCallSiteIdx++;
323 for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
324 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
325 LastCallSiteIdx = I.getCallSiteIdx();
Chris Lattnera1079052002-11-10 06:52:47 +0000326
Chris Lattnera9c9c022002-11-11 21:35:13 +0000327 // Resolve the current call...
328 Function *Callee = *I;
329 DSCallSite &CS = I.getCallSite();
Chris Lattner0d9bab82002-07-18 00:12:30 +0000330
Chris Lattnera9c9c022002-11-11 21:35:13 +0000331 if (Callee->isExternal()) {
332 // Ignore this case, simple varargs functions we cannot stub out!
Chris Lattner0eea6182003-06-30 05:09:58 +0000333 } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000334 // Self recursion... simply link up the formal arguments with the
335 // actual arguments...
Chris Lattner0eea6182003-06-30 05:09:58 +0000336 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000337
338 // Handle self recursion by resolving the arguments and return value
Chris Lattner0eea6182003-06-30 05:09:58 +0000339 Graph.mergeInGraph(CS, *Callee, Graph, 0);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000340
341 } else {
342 // Get the data structure graph for the called function.
343 //
344 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
345
346 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
Chris Lattner20167e32003-02-03 19:11:38 +0000347 << "[" << GI.getGraphSize() << "+"
Chris Lattner0eea6182003-06-30 05:09:58 +0000348 << GI.getAuxFunctionCalls().size() << "] into ["
349 << Graph.getGraphSize() << "+"
Chris Lattner20167e32003-02-03 19:11:38 +0000350 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000351
352 // Handle self recursion by resolving the arguments and return value
Chris Lattner5a540632003-06-30 03:15:25 +0000353 Graph.mergeInGraph(CS, *Callee, GI,
Vikram S. Adve61ff0292002-11-27 17:41:13 +0000354 DSGraph::KeepModRefBits |
355 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
Chris Lattnerae5f6032002-11-17 22:16:28 +0000356
357#if 0
358 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
359 Callee->getName());
360#endif
Chris Lattnera9c9c022002-11-11 21:35:13 +0000361 }
362 }
363
364 // Make sure to catch any leftover unresolvable calls...
365 for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
366 AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
367
368 TempFCs.clear();
369
370 // Recompute the Incomplete markers. If there are any function calls left
371 // now that are complete, we must loop!
372 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000373 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner20167e32003-02-03 19:11:38 +0000374 // FIXME: materialize nodes from the globals graph as neccesary...
Chris Lattner394471f2003-01-23 22:05:33 +0000375 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000376
Chris Lattnera9c9c022002-11-11 21:35:13 +0000377 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000378}
379