95de8e45e9b6eba7119bf93f9e2451ce988c293d
[oota-llvm.git] / lib / Analysis / DataStructure / BottomUpClosure.cpp
1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
2 //
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**
6 // applications like alias analysis.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Analysis/DataStructure.h"
11 #include "llvm/Module.h"
12 #include "Support/Statistic.h"
13 #include "Support/Debug.h"
14 #include "DSCallSiteIterator.h"
15
16 namespace {
17   Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
18   Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
19   Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
20   
21   RegisterAnalysis<BUDataStructures>
22   X("budatastructure", "Bottom-up Data Structure Analysis");
23 }
24
25 using namespace DS;
26
27 // run - Calculate the bottom up data structure graphs for each function in the
28 // program.
29 //
30 bool BUDataStructures::run(Module &M) {
31   LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
32   GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
33   GlobalsGraph->setPrintAuxCalls();
34
35   Function *MainFunc = M.getMainFunction();
36   if (MainFunc)
37     calculateReachableGraphs(MainFunc);
38
39   // Calculate the graphs for any functions that are unreachable from main...
40   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
41     if (!I->isExternal() && !DSInfo.count(I)) {
42 #ifndef NDEBUG
43       if (MainFunc)
44         std::cerr << "*** Function unreachable from main: "
45                   << I->getName() << "\n";
46 #endif
47       calculateReachableGraphs(I);    // Calculate all graphs...
48     }
49
50   NumCallEdges += ActualCallees.size();
51
52   // At the end of the bottom-up pass, the globals graph becomes complete.
53   // FIXME: This is not the right way to do this, but it is sorta better than
54   // nothing!  In particular, externally visible globals and unresolvable call
55   // nodes at the end of the BU phase should make things that they point to
56   // incomplete in the globals graph.
57   // 
58   GlobalsGraph->maskIncompleteMarkers();
59   return false;
60 }
61
62 void BUDataStructures::calculateReachableGraphs(Function *F) {
63   std::vector<Function*> Stack;
64   hash_map<Function*, unsigned> ValMap;
65   unsigned NextID = 1;
66   calculateGraphs(F, Stack, NextID, ValMap);
67 }
68
69 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
70   // Has the graph already been created?
71   DSGraph *&Graph = DSInfo[F];
72   if (Graph) return *Graph;
73
74   // Copy the local version into DSInfo...
75   Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
76
77   Graph->setGlobalsGraph(GlobalsGraph);
78   Graph->setPrintAuxCalls();
79
80   // Start with a copy of the original call sites...
81   Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
82   return *Graph;
83 }
84
85 unsigned BUDataStructures::calculateGraphs(Function *F,
86                                            std::vector<Function*> &Stack,
87                                            unsigned &NextID, 
88                                      hash_map<Function*, unsigned> &ValMap) {
89   assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!");
90   unsigned Min = NextID++, MyID = Min;
91   ValMap[F] = Min;
92   Stack.push_back(F);
93
94   if (F->isExternal()) {   // sprintf, fprintf, sscanf, etc...
95     // No callees!
96     Stack.pop_back();
97     ValMap[F] = ~0;
98     return Min;
99   }
100
101   DSGraph &Graph = getOrCreateGraph(F);
102
103   // The edges out of the current node are the call site targets...
104   for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
105          E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
106     Function *Callee = *I;
107     unsigned M;
108     // Have we visited the destination function yet?
109     hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
110     if (It == ValMap.end())  // No, visit it now.
111       M = calculateGraphs(Callee, Stack, NextID, ValMap);
112     else                    // Yes, get it's number.
113       M = It->second;
114     if (M < Min) Min = M;
115   }
116
117   assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
118   if (Min != MyID)
119     return Min;         // This is part of a larger SCC!
120
121   // If this is a new SCC, process it now.
122   if (Stack.back() == F) {           // Special case the single "SCC" case here.
123     DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
124                     << F->getName() << "\n");
125     Stack.pop_back();
126     DSGraph &G = getDSGraph(*F);
127     DEBUG(std::cerr << "  [BU] Calculating graph for: " << F->getName()<< "\n");
128     calculateGraph(G);
129     DEBUG(std::cerr << "  [BU] Done inlining: " << F->getName() << " ["
130                     << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
131                     << "]\n");
132
133     if (MaxSCC < 1) MaxSCC = 1;
134
135     // Should we revisit the graph?
136     if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
137       ValMap.erase(F);
138       return calculateGraphs(F, Stack, NextID, ValMap);
139     } else {
140       ValMap[F] = ~0U;
141     }
142     return MyID;
143
144   } else {
145     // SCCFunctions - Keep track of the functions in the current SCC
146     //
147     hash_set<Function*> SCCFunctions;
148
149     Function *NF;
150     std::vector<Function*>::iterator FirstInSCC = Stack.end();
151     DSGraph *SCCGraph = 0;
152     do {
153       NF = *--FirstInSCC;
154       ValMap[NF] = ~0U;
155       SCCFunctions.insert(NF);
156
157       // Figure out which graph is the largest one, in order to speed things up
158       // a bit in situations where functions in the SCC have widely different
159       // graph sizes.
160       DSGraph &NFGraph = getDSGraph(*NF);
161       if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
162         SCCGraph = &NFGraph;
163     } while (NF != F);
164
165     std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
166               << SCCFunctions.size() << "\n";
167
168     // Compute the Max SCC Size...
169     if (MaxSCC < SCCFunctions.size())
170       MaxSCC = SCCFunctions.size();
171
172     // First thing first, collapse all of the DSGraphs into a single graph for
173     // the entire SCC.  We computed the largest graph, so clone all of the other
174     // (smaller) graphs into it.  Discard all of the old graphs.
175     //
176     for (hash_set<Function*>::iterator I = SCCFunctions.begin(),
177            E = SCCFunctions.end(); I != E; ++I) {
178       DSGraph &G = getDSGraph(**I);
179       if (&G != SCCGraph) {
180         DSGraph::NodeMapTy NodeMap;
181         SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
182                             SCCGraph->getReturnNodes(), NodeMap, 0);
183         // Update the DSInfo map and delete the old graph...
184         DSInfo[*I] = SCCGraph;
185         delete &G;
186       }
187     }
188
189     // Clean up the graph before we start inlining a bunch again...
190     SCCGraph->removeTriviallyDeadNodes();
191
192     // Now that we have one big happy family, resolve all of the call sites in
193     // the graph...
194     calculateGraph(*SCCGraph);
195     DEBUG(std::cerr << "  [BU] Done inlining SCC  [" << SCCGraph->getGraphSize()
196                     << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
197
198     std::cerr << "DONE with SCC #: " << MyID << "\n";
199
200     // We never have to revisit "SCC" processed functions...
201     
202     // Drop the stuff we don't need from the end of the stack
203     Stack.erase(FirstInSCC, Stack.end());
204     return MyID;
205   }
206
207   return MyID;  // == Min
208 }
209
210
211 // releaseMemory - If the pass pipeline is done with this pass, we can release
212 // our memory... here...
213 //
214 void BUDataStructures::releaseMemory() {
215   for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
216          E = DSInfo.end(); I != E; ++I) {
217     I->second->getReturnNodes().erase(I->first);
218     if (I->second->getReturnNodes().empty())
219       delete I->second;
220   }
221
222   // Empty map so next time memory is released, data structures are not
223   // re-deleted.
224   DSInfo.clear();
225   delete GlobalsGraph;
226   GlobalsGraph = 0;
227 }
228
229 void BUDataStructures::calculateGraph(DSGraph &Graph) {
230   // Move our call site list into TempFCs so that inline call sites go into the
231   // new call site list and doesn't invalidate our iterators!
232   std::vector<DSCallSite> TempFCs;
233   std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
234   TempFCs.swap(AuxCallsList);
235
236   DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
237
238   // Loop over all of the resolvable call sites
239   unsigned LastCallSiteIdx = ~0U;
240   for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
241          E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
242     // If we skipped over any call sites, they must be unresolvable, copy them
243     // to the real call site list.
244     LastCallSiteIdx++;
245     for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
246       AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
247     LastCallSiteIdx = I.getCallSiteIdx();
248     
249     // Resolve the current call...
250     Function *Callee = *I;
251     DSCallSite CS = I.getCallSite();
252
253     if (Callee->isExternal()) {
254       // Ignore this case, simple varargs functions we cannot stub out!
255     } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) {
256       // Self recursion... simply link up the formal arguments with the
257       // actual arguments...
258       DEBUG(std::cerr << "    Self Inlining: " << Callee->getName() << "\n");
259
260       // Handle self recursion by resolving the arguments and return value
261       Graph.mergeInGraph(CS, *Callee, Graph, 0);
262
263     } else {
264       ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
265                                           Callee));
266
267       // Get the data structure graph for the called function.
268       //
269       DSGraph &GI = getDSGraph(*Callee);  // Graph to inline
270       
271       DEBUG(std::cerr << "    Inlining graph for " << Callee->getName()
272             << "[" << GI.getGraphSize() << "+"
273             << GI.getAuxFunctionCalls().size() << "] into '"
274             << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
275             << Graph.getAuxFunctionCalls().size() << "]\n");
276       
277       // Handle self recursion by resolving the arguments and return value
278       Graph.mergeInGraph(CS, *Callee, GI,
279                          DSGraph::KeepModRefBits | 
280                          DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
281       ++NumBUInlines;
282
283 #if 0
284       Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
285                              Callee->getName());
286 #endif
287     }
288   }
289
290   // Make sure to catch any leftover unresolvable calls...
291   for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
292     AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
293
294   TempFCs.clear();
295
296   // Re-materialize nodes from the globals graph.
297   // Do not ignore globals inlined from callees -- they are not up-to-date!
298   Graph.getInlinedGlobals().clear();
299   Graph.updateFromGlobalGraph();
300
301   // Recompute the Incomplete markers
302   Graph.maskIncompleteMarkers();
303   Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
304
305   // Delete dead nodes.  Treat globals that are unreachable but that can
306   // reach live nodes as live.
307   Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
308
309   //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
310 }
311