Start using retnodes_* for iteration.
[oota-llvm.git] / lib / Analysis / DataStructure / BottomUpClosure.cpp
1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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**
13 // applications like alias analysis.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/DataStructure/DataStructure.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/Debug.h"
21 #include "DSCallSiteIterator.h"
22 using namespace llvm;
23
24 namespace {
25   Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
26   Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
27   Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
28   
29   RegisterAnalysis<BUDataStructures>
30   X("budatastructure", "Bottom-up Data Structure Analysis");
31 }
32
33 using namespace DS;
34
35 // run - Calculate the bottom up data structure graphs for each function in the
36 // program.
37 //
38 bool BUDataStructures::runOnModule(Module &M) {
39   LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
40   GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
41   GlobalsGraph->setPrintAuxCalls();
42
43   IndCallGraphMap = new std::map<std::vector<Function*>,
44                            std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
45
46   std::vector<Function*> Stack;
47   hash_map<Function*, unsigned> ValMap;
48   unsigned NextID = 1;
49
50   Function *MainFunc = M.getMainFunction();
51   if (MainFunc)
52     calculateGraphs(MainFunc, Stack, NextID, ValMap);
53
54   // Calculate the graphs for any functions that are unreachable from main...
55   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
56     if (!I->isExternal() && !DSInfo.count(I)) {
57 #ifndef NDEBUG
58       if (MainFunc)
59         std::cerr << "*** Function unreachable from main: "
60                   << I->getName() << "\n";
61 #endif
62       calculateGraphs(I, Stack, NextID, ValMap);     // Calculate all graphs.
63     }
64
65   NumCallEdges += ActualCallees.size();
66
67   // If we computed any temporary indcallgraphs, free them now.
68   for (std::map<std::vector<Function*>,
69          std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
70          IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
71     I->second.second.clear();  // Drop arg refs into the graph.
72     delete I->second.first;
73   }
74   delete IndCallGraphMap;
75
76   // At the end of the bottom-up pass, the globals graph becomes complete.
77   // FIXME: This is not the right way to do this, but it is sorta better than
78   // nothing!  In particular, externally visible globals and unresolvable call
79   // nodes at the end of the BU phase should make things that they point to
80   // incomplete in the globals graph.
81   // 
82   GlobalsGraph->removeTriviallyDeadNodes();
83   GlobalsGraph->maskIncompleteMarkers();
84
85   // Merge the globals variables (not the calls) from the globals graph back
86   // into the main function's graph so that the main function contains all of
87   // the information about global pools and GV usage in the program.
88   if (MainFunc) {
89     DSGraph &MainGraph = getOrCreateGraph(MainFunc);
90     const DSGraph &GG = *MainGraph.getGlobalsGraph();
91     ReachabilityCloner RC(MainGraph, GG, 
92                           DSGraph::DontCloneCallNodes |
93                           DSGraph::DontCloneAuxCallNodes);
94
95     // Clone the global nodes into this graph.
96     for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
97            E = GG.getScalarMap().global_end(); I != E; ++I)
98       if (isa<GlobalVariable>(*I))
99         RC.getClonedNH(GG.getNodeForValue(*I));
100
101     MainGraph.maskIncompleteMarkers();
102     MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | 
103                                   DSGraph::IgnoreGlobals);
104   }
105
106   return false;
107 }
108
109 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
110   // Has the graph already been created?
111   DSGraph *&Graph = DSInfo[F];
112   if (Graph) return *Graph;
113
114   // Copy the local version into DSInfo...
115   Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
116
117   Graph->setGlobalsGraph(GlobalsGraph);
118   Graph->setPrintAuxCalls();
119
120   // Start with a copy of the original call sites...
121   Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
122   return *Graph;
123 }
124
125 unsigned BUDataStructures::calculateGraphs(Function *F,
126                                            std::vector<Function*> &Stack,
127                                            unsigned &NextID, 
128                                      hash_map<Function*, unsigned> &ValMap) {
129   assert(!ValMap.count(F) && "Shouldn't revisit functions!");
130   unsigned Min = NextID++, MyID = Min;
131   ValMap[F] = Min;
132   Stack.push_back(F);
133
134   // FIXME!  This test should be generalized to be any function that we have
135   // already processed, in the case when there isn't a main or there are
136   // unreachable functions!
137   if (F->isExternal()) {   // sprintf, fprintf, sscanf, etc...
138     // No callees!
139     Stack.pop_back();
140     ValMap[F] = ~0;
141     return Min;
142   }
143
144   DSGraph &Graph = getOrCreateGraph(F);
145
146   // The edges out of the current node are the call site targets...
147   for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
148          E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
149     Function *Callee = *I;
150     unsigned M;
151     // Have we visited the destination function yet?
152     hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
153     if (It == ValMap.end())  // No, visit it now.
154       M = calculateGraphs(Callee, Stack, NextID, ValMap);
155     else                    // Yes, get it's number.
156       M = It->second;
157     if (M < Min) Min = M;
158   }
159
160   assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
161   if (Min != MyID)
162     return Min;         // This is part of a larger SCC!
163
164   // If this is a new SCC, process it now.
165   if (Stack.back() == F) {           // Special case the single "SCC" case here.
166     DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
167                     << F->getName() << "\n");
168     Stack.pop_back();
169     DSGraph &G = getDSGraph(*F);
170     DEBUG(std::cerr << "  [BU] Calculating graph for: " << F->getName()<< "\n");
171     calculateGraph(G);
172     DEBUG(std::cerr << "  [BU] Done inlining: " << F->getName() << " ["
173                     << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
174                     << "]\n");
175
176     if (MaxSCC < 1) MaxSCC = 1;
177
178     // Should we revisit the graph?
179     if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
180       ValMap.erase(F);
181       return calculateGraphs(F, Stack, NextID, ValMap);
182     } else {
183       ValMap[F] = ~0U;
184     }
185     return MyID;
186
187   } else {
188     // SCCFunctions - Keep track of the functions in the current SCC
189     //
190     hash_set<DSGraph*> SCCGraphs;
191
192     Function *NF;
193     std::vector<Function*>::iterator FirstInSCC = Stack.end();
194     DSGraph *SCCGraph = 0;
195     do {
196       NF = *--FirstInSCC;
197       ValMap[NF] = ~0U;
198
199       // Figure out which graph is the largest one, in order to speed things up
200       // a bit in situations where functions in the SCC have widely different
201       // graph sizes.
202       DSGraph &NFGraph = getDSGraph(*NF);
203       SCCGraphs.insert(&NFGraph);
204       // FIXME: If we used a better way of cloning graphs (ie, just splice all
205       // of the nodes into the new graph), this would be completely unneeded!
206       if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
207         SCCGraph = &NFGraph;
208     } while (NF != F);
209
210     std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
211               << SCCGraphs.size() << "\n";
212
213     // Compute the Max SCC Size...
214     if (MaxSCC < SCCGraphs.size())
215       MaxSCC = SCCGraphs.size();
216
217     // First thing first, collapse all of the DSGraphs into a single graph for
218     // the entire SCC.  We computed the largest graph, so clone all of the other
219     // (smaller) graphs into it.  Discard all of the old graphs.
220     //
221     for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
222            E = SCCGraphs.end(); I != E; ++I) {
223       DSGraph &G = **I;
224       if (&G != SCCGraph) {
225         {
226           DSGraph::NodeMapTy NodeMap;
227           SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
228                               SCCGraph->getReturnNodes(), NodeMap);
229         }
230         // Update the DSInfo map and delete the old graph...
231         for (DSGraph::retnodes_iterator I = G.retnodes_begin(),
232                E = G.retnodes_end(); I != E; ++I)
233           DSInfo[I->first] = SCCGraph;
234         delete &G;
235       }
236     }
237
238     // Clean up the graph before we start inlining a bunch again...
239     SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
240
241     // Now that we have one big happy family, resolve all of the call sites in
242     // the graph...
243     calculateGraph(*SCCGraph);
244     DEBUG(std::cerr << "  [BU] Done inlining SCC  [" << SCCGraph->getGraphSize()
245                     << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
246
247     std::cerr << "DONE with SCC #: " << MyID << "\n";
248
249     // We never have to revisit "SCC" processed functions...
250     
251     // Drop the stuff we don't need from the end of the stack
252     Stack.erase(FirstInSCC, Stack.end());
253     return MyID;
254   }
255
256   return MyID;  // == Min
257 }
258
259
260 // releaseMemory - If the pass pipeline is done with this pass, we can release
261 // our memory... here...
262 //
263 void BUDataStructures::releaseMemory() {
264   for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
265          E = DSInfo.end(); I != E; ++I) {
266     I->second->getReturnNodes().erase(I->first);
267     if (I->second->getReturnNodes().empty())
268       delete I->second;
269   }
270
271   // Empty map so next time memory is released, data structures are not
272   // re-deleted.
273   DSInfo.clear();
274   delete GlobalsGraph;
275   GlobalsGraph = 0;
276 }
277
278 static bool isVAHackFn(const Function *F) {
279   return F->getName() == "printf"  || F->getName() == "sscanf" ||
280     F->getName() == "fprintf" || F->getName() == "open" ||
281     F->getName() == "sprintf" || F->getName() == "fputs" ||
282     F->getName() == "fscanf";
283 }
284
285 // isUnresolvableFunction - Return true if this is an unresolvable
286 // external function.  A direct or indirect call to this cannot be resolved.
287 // 
288 static bool isResolvableFunc(const Function* callee) {
289   return !callee->isExternal() || isVAHackFn(callee);
290 }
291
292 void BUDataStructures::calculateGraph(DSGraph &Graph) {
293   // Move our call site list into TempFCs so that inline call sites go into the
294   // new call site list and doesn't invalidate our iterators!
295   std::list<DSCallSite> TempFCs;
296   std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
297   TempFCs.swap(AuxCallsList);
298
299   DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
300
301   bool Printed = false;
302   std::vector<Function*> CalledFuncs;
303   while (!TempFCs.empty()) {
304     DSCallSite &CS = *TempFCs.begin();
305
306     CalledFuncs.clear();
307
308     if (CS.isDirectCall()) {
309       Function *F = CS.getCalleeFunc();
310       if (isResolvableFunc(F))
311         if (F->isExternal()) {  // Call to fprintf, etc.
312           TempFCs.erase(TempFCs.begin());
313           continue;
314         } else {
315           CalledFuncs.push_back(F);
316         }
317     } else {
318       DSNode *Node = CS.getCalleeNode();
319
320       if (!Node->isIncomplete())
321         for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
322           if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
323             if (isResolvableFunc(CF) && !CF->isExternal())
324               CalledFuncs.push_back(CF);
325     }
326
327     if (CalledFuncs.empty()) {
328       // Remember that we could not resolve this yet!
329       AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
330       continue;
331     } else {
332       DSGraph *GI;
333
334       if (CalledFuncs.size() == 1) {
335         Function *Callee = CalledFuncs[0];
336         ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
337                                             Callee));
338
339         // Get the data structure graph for the called function.
340         GI = &getDSGraph(*Callee);  // Graph to inline
341         DEBUG(std::cerr << "    Inlining graph for " << Callee->getName());
342
343         DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
344               << GI->getAuxFunctionCalls().size() << "] into '"
345               << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
346               << Graph.getAuxFunctionCalls().size() << "]\n");
347         Graph.mergeInGraph(CS, *Callee, *GI,
348                            DSGraph::KeepModRefBits | 
349                            DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
350         ++NumBUInlines;
351       } else {
352         if (!Printed)
353           std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
354         std::cerr << "  calls " << CalledFuncs.size()
355                   << " fns from site: " << CS.getCallSite().getInstruction() 
356                   << "  " << *CS.getCallSite().getInstruction();
357         unsigned NumToPrint = CalledFuncs.size();
358         if (NumToPrint > 8) NumToPrint = 8;
359         std::cerr << "   Fns =";
360         for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
361                E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
362           std::cerr << " " << (*I)->getName();
363         std::cerr << "\n";
364
365         // See if we already computed a graph for this set of callees.
366         std::sort(CalledFuncs.begin(), CalledFuncs.end());
367         std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
368           (*IndCallGraphMap)[CalledFuncs];
369
370         if (IndCallGraph.first == 0) {
371           std::vector<Function*>::iterator I = CalledFuncs.begin(),
372             E = CalledFuncs.end();
373           
374           // Start with a copy of the first graph.
375           GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
376           GI->setGlobalsGraph(Graph.getGlobalsGraph());
377           std::vector<DSNodeHandle> &Args = IndCallGraph.second;
378
379           // Get the argument nodes for the first callee.  The return value is
380           // the 0th index in the vector.
381           GI->getFunctionArgumentsForCall(*I, Args);
382
383           // Merge all of the other callees into this graph.
384           for (++I; I != E; ++I) {
385             // If the graph already contains the nodes for the function, don't
386             // bother merging it in again.
387             if (!GI->containsFunction(*I)) {
388               DSGraph::NodeMapTy NodeMap;
389               GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
390                             GI->getReturnNodes(), NodeMap);
391               ++NumBUInlines;
392             }
393
394             std::vector<DSNodeHandle> NextArgs;
395             GI->getFunctionArgumentsForCall(*I, NextArgs);
396             unsigned i = 0, e = Args.size();
397             for (; i != e; ++i) {
398               if (i == NextArgs.size()) break;
399               Args[i].mergeWith(NextArgs[i]);
400             }
401             for (e = NextArgs.size(); i != e; ++i)
402               Args.push_back(NextArgs[i]);
403           }
404           
405           // Clean up the final graph!
406           GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
407         } else {
408           std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
409         }
410
411         GI = IndCallGraph.first;
412
413         // Merge the unified graph into this graph now.
414         DEBUG(std::cerr << "    Inlining multi callee graph "
415               << "[" << GI->getGraphSize() << "+"
416               << GI->getAuxFunctionCalls().size() << "] into '"
417               << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
418               << Graph.getAuxFunctionCalls().size() << "]\n");
419
420         Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
421                            DSGraph::KeepModRefBits | 
422                            DSGraph::StripAllocaBit |
423                            DSGraph::DontCloneCallNodes);
424         ++NumBUInlines;
425       }
426     }
427     TempFCs.erase(TempFCs.begin());
428   }
429
430   // Recompute the Incomplete markers
431   assert(Graph.getInlinedGlobals().empty());
432   Graph.maskIncompleteMarkers();
433   Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
434
435   // Delete dead nodes.  Treat globals that are unreachable but that can
436   // reach live nodes as live.
437   Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
438
439   // When this graph is finalized, clone the globals in the graph into the
440   // globals graph to make sure it has everything, from all graphs.
441   DSScalarMap &MainSM = Graph.getScalarMap();
442   ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
443
444   // Clone everything reachable from globals in the function graph into the
445   // globals graph.
446   for (DSScalarMap::global_iterator I = MainSM.global_begin(),
447          E = MainSM.global_end(); I != E; ++I) 
448     RC.getClonedNH(MainSM[*I]);
449
450   //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
451 }
452
453 static const Function *getFnForValue(const Value *V) {
454   if (const Instruction *I = dyn_cast<Instruction>(V))
455     return I->getParent()->getParent();
456   else if (const Argument *A = dyn_cast<Argument>(V))
457     return A->getParent();
458   else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
459     return BB->getParent();
460   return 0;
461 }
462
463 /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
464 /// These correspond to the interfaces defined in the AliasAnalysis class.
465 void BUDataStructures::deleteValue(Value *V) {
466   if (const Function *F = getFnForValue(V)) {  // Function local value?
467     // If this is a function local value, just delete it from the scalar map!
468     getDSGraph(*F).getScalarMap().eraseIfExists(V);
469     return;
470   }
471
472   if (Function *F = dyn_cast<Function>(V)) {
473     assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
474            "cannot handle scc's");
475     delete DSInfo[F];
476     DSInfo.erase(F);
477     return;
478   }
479
480   assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
481 }
482
483 void BUDataStructures::copyValue(Value *From, Value *To) {
484   if (From == To) return;
485   if (const Function *F = getFnForValue(From)) {  // Function local value?
486     // If this is a function local value, just delete it from the scalar map!
487     getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
488     return;
489   }
490
491   if (Function *FromF = dyn_cast<Function>(From)) {
492     Function *ToF = cast<Function>(To);
493     assert(!DSInfo.count(ToF) && "New Function already exists!");
494     DSGraph *NG = new DSGraph(getDSGraph(*FromF));
495     DSInfo[ToF] = NG;
496     assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
497
498     // Change the Function* is the returnnodes map to the ToF.
499     DSNodeHandle Ret = NG->retnodes_begin()->second;
500     NG->getReturnNodes().clear();
501     NG->getReturnNodes()[ToF] = Ret;
502     return;
503   }
504
505   assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
506 }