Fix #includes of i*.h => Instructions.h as per PR403.
[oota-llvm.git] / lib / Analysis / DataStructure / EquivClassGraphs.cpp
1 //===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===//
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 pass is the same as the complete bottom-up graphs, but
11 // with functions partitioned into equivalence classes and a single merged
12 // DS graph for all functions in an equivalence class.  After this merging,
13 // graphs are inlined bottom-up on the SCCs of the final (CBU) call graph.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #define DEBUG_TYPE "ECGraphs"
18 #include "EquivClassGraphs.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Analysis/DataStructure/DSGraph.h"
22 #include "llvm/Analysis/DataStructure/DataStructure.h"
23 #include "llvm/Support/CallSite.h"
24 #include "Support/Debug.h"
25 #include "Support/SCCIterator.h"
26 #include "Support/Statistic.h"
27 #include "Support/EquivalenceClasses.h"
28 #include "Support/STLExtras.h"
29 using namespace llvm;
30
31 namespace llvm {
32   namespace PA {
33     Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up",
34                                     "Number of graphs inlined");
35     
36   } // End PA namespace
37 } // End llvm namespace
38
39
40 namespace {
41   RegisterAnalysis<llvm::PA::EquivClassGraphs> X("equivdatastructure",
42                     "Equivalence-class Bottom-up Data Structure Analysis");
43   Statistic<> NumEquivBUInlines("equivdatastructures", "Number of graphs inlined");
44 }
45
46
47 // getDSGraphForCallSite - Return the common data structure graph for
48 // callees at the specified call site.
49 // 
50 Function *llvm::PA::EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const {
51   Function *thisFunc = CS.getCaller();
52   assert(thisFunc && "getDSGraphForCallSite(): Not a valid call site?");
53   DSNode *calleeNode = CBU->getDSGraph(*thisFunc).
54     getNodeForValue(CS.getCalledValue()).getNode();
55   std::map<DSNode*, Function *>::const_iterator I =
56     OneCalledFunction.find(calleeNode);
57   return (I == OneCalledFunction.end())? NULL : I->second;
58 }
59
60 // computeFoldedGraphs - Calculate the bottom up data structure
61 // graphs for each function in the program.
62 //
63 void llvm::PA::EquivClassGraphs::computeFoldedGraphs(Module &M) {
64   CBU = &getAnalysis<CompleteBUDataStructures>();
65
66   // Find equivalence classes of functions called from common call sites.
67   // Fold the CBU graphs for all functions in an equivalence class.
68   buildIndirectFunctionSets(M);
69
70   // Stack of functions used for Tarjan's SCC-finding algorithm.
71   std::vector<Function*> Stack;
72   hash_map<Function*, unsigned> ValMap;
73   unsigned NextID = 1;
74
75   if (Function *Main = M.getMainFunction()) {
76     if (!Main->isExternal())
77       processSCC(getOrCreateGraph(*Main), *Main, Stack, NextID, ValMap);
78   } else {
79     std::cerr << "Fold Graphs: No 'main' function found!\n";
80   }
81   
82   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
83     if (!I->isExternal() && !FoldedGraphsMap.count(I))
84       processSCC(getOrCreateGraph(*I), *I, Stack, NextID, ValMap);
85
86   getGlobalsGraph().removeTriviallyDeadNodes();
87 }
88
89
90 // buildIndirectFunctionSets - Iterate over the module looking for indirect
91 // calls to functions.  If a call site can invoke any functions [F1, F2... FN],
92 // unify the N functions together in the FuncECs set.
93 //
94 void llvm::PA::EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
95   const ActualCalleesTy& AC = CBU->getActualCallees();
96
97   // Loop over all of the indirect calls in the program.  If a call site can
98   // call multiple different functions, we need to unify all of the callees into
99   // the same equivalence class.
100   Instruction *LastInst = 0;
101   Function *FirstFunc = 0;
102   for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
103     if (I->second->isExternal())
104       continue;                         // Ignore functions we cannot modify
105
106     CallSite CS = CallSite::get(I->first);
107
108     if (CS.getCalledFunction()) {       // Direct call:
109       FuncECs.addElement(I->second);    // -- Make sure function has equiv class
110       FirstFunc = I->second;            // -- First callee at this site
111     } else {                            // Else indirect call
112       // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
113       //       << " from : " << I->first);
114       if (I->first != LastInst) {
115         // This is the first callee from this call site.
116         LastInst = I->first;
117         FirstFunc = I->second;
118         // Instead of storing the lastInst For Indirection call Sites we store
119         // the DSNode for the function ptr arguemnt
120         Function *thisFunc = LastInst->getParent()->getParent();
121         DSNode *calleeNode = CBU->getDSGraph(*thisFunc).getNodeForValue(CS.getCalledValue()).getNode();
122         OneCalledFunction[calleeNode] = FirstFunc;
123         FuncECs.addElement(I->second);
124       } else {
125         // This is not the first possible callee from a particular call site.
126         // Union the callee in with the other functions.
127         FuncECs.unionSetsWith(FirstFunc, I->second);
128 #ifndef NDEBUG
129         Function *thisFunc = LastInst->getParent()->getParent();
130         DSNode *calleeNode = CBU->getDSGraph(*thisFunc).getNodeForValue(CS.getCalledValue()).getNode();
131         assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
132 #endif
133       }
134     }
135
136     // Now include all functions that share a graph with any function in the
137     // equivalence class.  More precisely, if F is in the class, and G(F) is
138     // its graph, then we include all other functions that are also in G(F).
139     // Currently, that is just the functions in the same call-graph-SCC as F.
140     // 
141     DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
142     const DSGraph::ReturnNodesTy &RetNodes = funcDSGraph.getReturnNodes();
143     for (DSGraph::ReturnNodesTy::const_iterator RI=RetNodes.begin(),
144            RE=RetNodes.end(); RI != RE; ++RI)
145       FuncECs.unionSetsWith(FirstFunc, RI->first);
146   }
147
148   // Now that all of the equivalences have been built, merge the graphs for
149   // each equivalence class.
150   //
151   std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
152   DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
153   for (std::set<Function*>::iterator LI = leaderSet.begin(),
154          LE = leaderSet.end(); LI != LE; ++LI) {
155
156     Function* LF = *LI;
157     const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
158
159 #ifndef NDEBUG
160     if (EqClass.size() > 1) {
161       DEBUG(std::cerr <<"  Equivalence set for leader " <<LF->getName()<<" = ");
162       for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
163              EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
164         DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
165       DEBUG(std::cerr << "\n");
166     }
167 #endif
168
169     if (EqClass.size() > 1) {
170       // This equiv class has multiple functions: merge their graphs.
171       // First, clone the CBU graph for the leader and make it the 
172       // common graph for the equivalence graph.
173       DSGraph* mergedG = cloneGraph(*LF);
174
175       // Record the argument nodes for use in merging later below
176       EquivClassGraphArgsInfo& GraphInfo = getECGraphInfo(mergedG);
177       for (Function::aiterator AI1 = LF->abegin(); AI1 != LF->aend(); ++AI1)
178         GraphInfo.argNodes.push_back(mergedG->getNodeForValue(AI1));
179       
180       // Merge in the graphs of all other functions in this equiv. class.
181       // Note that two or more functions may have the same graph, and it
182       // only needs to be merged in once.  Use a set to find repetitions.
183       std::set<DSGraph*> GraphsMerged;
184       for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
185              EqEnd = EqClass.end(); EqI != EqEnd; ++EqI) {
186         Function* F = *EqI;
187         DSGraph*& FG = FoldedGraphsMap[F];
188         if (F == LF || FG == mergedG)
189           continue;
190         
191         // Record the "folded" graph for the function.
192         FG = mergedG;
193         
194         // Clone this member of the equivalence class into mergedG
195         DSGraph* CBUGraph = &CBU->getDSGraph(*F); 
196         if (GraphsMerged.count(CBUGraph) > 0)
197           continue;
198         
199         GraphsMerged.insert(CBUGraph);
200         DSGraph::NodeMapTy NodeMap;    
201         mergedG->cloneInto(*CBUGraph, mergedG->getScalarMap(),
202                            mergedG->getReturnNodes(), NodeMap, 0);
203
204         // Merge the return nodes of all functions together.
205         mergedG->getReturnNodes()[LF].mergeWith(mergedG->getReturnNodes()[F]);
206
207         // Merge the function arguments with all argument nodes found so far.
208         // If there are extra function args, add them to the vector of argNodes
209         Function::aiterator AI2 = F->abegin(), AI2end = F->aend();
210         for (unsigned arg=0, numArgs=GraphInfo.argNodes.size();
211              arg < numArgs && AI2 != AI2end; ++AI2, ++arg)
212           GraphInfo.argNodes[arg].mergeWith(mergedG->getNodeForValue(AI2));
213         for ( ; AI2 != AI2end; ++AI2)
214           GraphInfo.argNodes.push_back(mergedG->getNodeForValue(AI2));
215       }
216     }
217   }
218   DEBUG(std::cerr << "\n");
219 }
220
221
222 DSGraph &llvm::PA::EquivClassGraphs::getOrCreateGraph(Function &F) {
223   // Has the graph already been created?
224   DSGraph *&Graph = FoldedGraphsMap[&F];
225   if (Graph) return *Graph;
226
227   // Use the CBU graph directly without copying it.
228   // This automatically updates the FoldedGraphsMap via the reference.
229   Graph = &CBU->getDSGraph(F);
230
231   return *Graph;
232 }
233
234 DSGraph* llvm::PA::EquivClassGraphs::cloneGraph(Function &F) {
235   DSGraph *&Graph = FoldedGraphsMap[&F];
236   DSGraph &CBUGraph = CBU->getDSGraph(F);
237   assert(Graph == NULL || Graph == &CBUGraph && "Cloning a graph twice?");
238
239   // Copy the CBU graph...
240   Graph = new DSGraph(CBUGraph);           // updates the map via reference
241   Graph->setGlobalsGraph(&getGlobalsGraph());
242   Graph->setPrintAuxCalls();
243
244   // Make sure to update the FoldedGraphsMap map for all functions in the graph!
245   for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
246        I != Graph->getReturnNodes().end(); ++I)
247     if (I->first != &F) {
248       DSGraph*& FG = FoldedGraphsMap[I->first];
249       assert(FG == NULL || FG == &CBU->getDSGraph(*I->first) &&
250              "Merging function in SCC twice?");
251       FG = Graph;
252     }
253
254   return Graph;
255 }
256
257
258 unsigned llvm::PA::EquivClassGraphs::processSCC(DSGraph &FG, Function& F,
259                                         std::vector<Function*> &Stack,
260                                         unsigned &NextID, 
261                                         hash_map<Function*, unsigned> &ValMap) {
262   DEBUG(std::cerr << "    ProcessSCC for function " << F.getName() << "\n");
263
264   assert(!ValMap.count(&F) && "Shouldn't revisit functions!");
265   unsigned Min = NextID++, MyID = Min;
266   ValMap[&F] = Min;
267   Stack.push_back(&F);
268
269   // The edges out of the current node are the call site targets...
270   for (unsigned i = 0, e = FG.getFunctionCalls().size(); i != e; ++i) {
271     Instruction *Call = FG.getFunctionCalls()[i].getCallSite().getInstruction();
272
273     // Loop over all of the actually called functions...
274     ActualCalleesTy::const_iterator I, E;
275     for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
276       if (!I->second->isExternal()) {
277         DSGraph &CalleeG = getOrCreateGraph(*I->second);
278
279         // Have we visited the destination function yet?
280         hash_map<Function*, unsigned>::iterator It = ValMap.find(I->second);
281         unsigned M = (It == ValMap.end())  // No, visit it now.
282           ? processSCC(CalleeG, *I->second, Stack, NextID, ValMap)
283           : It->second;                    // Yes, get it's number.
284         
285         if (M < Min) Min = M;
286       }
287   }
288
289   assert(ValMap[&F] == MyID && "SCC construction assumption wrong!");
290   if (Min != MyID)
291     return Min;         // This is part of a larger SCC!
292
293   // If this is a new SCC, process it now.
294   bool IsMultiNodeSCC = false;
295   while (Stack.back() != &F) {
296     DSGraph *NG = &getOrCreateGraph(* Stack.back());
297     ValMap[Stack.back()] = ~0U;
298
299     // Since all SCCs must be the same as those found in CBU, we do not need to
300     // do any merging.  Make sure all functions in the SCC share the same graph.
301     assert(NG == &FG &&
302            "FoldGraphs: Functions in the same SCC have different graphs?");
303     
304     Stack.pop_back();
305     IsMultiNodeSCC = true;
306   }
307
308   // Clean up the graph before we start inlining a bunch again...
309   if (IsMultiNodeSCC)
310     FG.removeTriviallyDeadNodes();
311
312   Stack.pop_back();
313   processGraph(FG, F);
314   ValMap[&F] = ~0U;
315   return MyID;
316 }
317
318
319 /// processGraph - Process the CBU graphs for the program in bottom-up order on
320 /// the SCC of the __ACTUAL__ call graph.  This builds final folded CBU graphs.
321 void llvm::PA::EquivClassGraphs::processGraph(DSGraph &G, Function& F) {
322   DEBUG(std::cerr << "    ProcessGraph for function " << F.getName() << "\n");
323
324   hash_set<Instruction*> calls;
325
326   DSGraph* CallerGraph = sameAsCBUGraph(F)? NULL : &getOrCreateGraph(F);
327
328   // If the function has not yet been cloned, let's check if any callees
329   // need to be inlined before cloning it.
330   // 
331   for (unsigned i=0, e=G.getFunctionCalls().size(); i!=e && !CallerGraph; ++i) {
332     const DSCallSite &CS = G.getFunctionCalls()[i];
333     Instruction *TheCall = CS.getCallSite().getInstruction();
334     
335     // Loop over all potential callees to find the first non-external callee.
336     // Some inlining is needed if there is such a callee and it has changed.
337     ActualCalleesTy::const_iterator I, E;
338     for (tie(I, E) = getActualCallees().equal_range(TheCall); I != E; ++I)
339       if (!I->second->isExternal() && !sameAsCBUGraph(*I->second)) {
340         // Ok, the caller does need to be cloned... go ahead and do it now.
341         // clone the CBU graph for F now because we have not cloned it so far
342         CallerGraph = cloneGraph(F);
343         break;
344       }
345   }
346
347   if (!CallerGraph) {                   // No inlining is needed.
348     DEBUG(std::cerr << "  --DONE ProcessGraph for function " << F.getName()
349           << " (NO INLINING NEEDED)\n");
350     return;
351   }
352
353   // Else we need to inline some callee graph.  Visit all call sites.
354   // The edges out of the current node are the call site targets...
355   for (unsigned i=0, e = CallerGraph->getFunctionCalls().size(); i != e; ++i) {
356     const DSCallSite &CS = CallerGraph->getFunctionCalls()[i];
357     Instruction *TheCall = CS.getCallSite().getInstruction();
358
359     assert(calls.insert(TheCall).second &&
360            "Call instruction occurs multiple times in graph??");
361     
362     // Inline the common callee graph into the current graph, if the callee
363     // graph has not changed.  Note that all callees should have the same
364     // graph so we only need to do this once.
365     // 
366     DSGraph* CalleeGraph = NULL;
367     ActualCalleesTy::const_iterator I, E;
368     tie(I, E) = getActualCallees().equal_range(TheCall);
369     unsigned TNum, Num;
370
371     // Loop over all potential callees to find the first non-external callee.
372     for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
373       if (!I->second->isExternal())
374         break;
375
376     // Now check if the graph has changed and if so, clone and inline it.
377     if (I != E && !sameAsCBUGraph(*I->second)) {
378       Function *CalleeFunc = I->second;
379       
380       // Merge the callee's graph into this graph, if not already the same.
381       // Callees in the same equivalence class (which subsumes those
382       // in the same SCCs) have the same graph.  Note that all recursion
383       // including self-recursion have been folded in the equiv classes.
384       // 
385       CalleeGraph = &getOrCreateGraph(*CalleeFunc);
386       if (CalleeGraph != CallerGraph) {
387         ++NumFoldGraphInlines;
388         CallerGraph->mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
389                                   DSGraph::KeepModRefBits |
390                                   DSGraph::StripAllocaBit |
391                                   DSGraph::DontCloneCallNodes |
392                                   DSGraph::DontCloneAuxCallNodes);
393         DEBUG(std::cerr << "    Inlining graph [" << i << "/" << e-1
394               << ":" << TNum << "/" << Num-1 << "] for "
395               << CalleeFunc->getName() << "["
396               << CalleeGraph->getGraphSize() << "+"
397               << CalleeGraph->getAuxFunctionCalls().size()
398               << "] into '" /*<< CallerGraph->getFunctionNames()*/ << "' ["
399               << CallerGraph->getGraphSize() << "+"
400               << CallerGraph->getAuxFunctionCalls().size()
401               << "]\n");
402       }
403     }
404
405 #ifndef NDEBUG
406     // Now loop over the rest of the callees and make sure they have the
407     // same graph as the one inlined above.
408     if (CalleeGraph)
409       for (++I, ++TNum; I != E; ++I, ++TNum)
410         if (!I->second->isExternal())
411           assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
412                  "Callees at a call site have different graphs?");
413 #endif
414   }
415
416   // Recompute the Incomplete markers
417   if (CallerGraph != NULL) {
418     assert(CallerGraph->getInlinedGlobals().empty());
419     CallerGraph->maskIncompleteMarkers();
420     CallerGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
421
422     // Delete dead nodes.  Treat globals that are unreachable but that can
423     // reach live nodes as live.
424     CallerGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
425   }
426
427   DEBUG(std::cerr << "  --DONE ProcessGraph for function " << F.getName() << "\n");
428 }