blob: caaf43fac65620ca607ea2d66490195bb4815b11 [file] [log] [blame]
Vikram S. Advec5204fb2004-05-23 07:54:02 +00001//===- 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"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000019#include "llvm/Module.h"
20#include "llvm/Pass.h"
Chris Lattnereaef5682004-07-07 06:22:54 +000021#include "llvm/Analysis/DataStructure/DSGraph.h"
22#include "llvm/Analysis/DataStructure/DataStructure.h"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000023#include "llvm/Support/CallSite.h"
Chris Lattnerc9b93802004-10-11 20:53:28 +000024#include "llvm/Support/Debug.h"
25#include "llvm/ADT/SCCIterator.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/EquivalenceClasses.h"
28#include "llvm/ADT/STLExtras.h"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000029using namespace llvm;
30
Vikram S. Advec5204fb2004-05-23 07:54:02 +000031namespace {
Chris Lattner15d879e2004-10-12 16:52:09 +000032 RegisterAnalysis<PA::EquivClassGraphs> X("equivdatastructure",
Vikram S. Advec5204fb2004-05-23 07:54:02 +000033 "Equivalence-class Bottom-up Data Structure Analysis");
Chris Lattnerab8544a2004-10-31 21:56:11 +000034 Statistic<> NumEquivBUInlines("equivdatastructures",
35 "Number of graphs inlined");
Chris Lattner15d879e2004-10-12 16:52:09 +000036 Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up",
37 "Number of graphs inlined");
Vikram S. Advec5204fb2004-05-23 07:54:02 +000038}
39
Vikram S. Advec5204fb2004-05-23 07:54:02 +000040// getDSGraphForCallSite - Return the common data structure graph for
41// callees at the specified call site.
42//
Chris Lattner15d879e2004-10-12 16:52:09 +000043Function *PA::EquivClassGraphs::
44getSomeCalleeForCallSite(const CallSite &CS) const {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000045 Function *thisFunc = CS.getCaller();
46 assert(thisFunc && "getDSGraphForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000047 DSGraph &DSG = getDSGraph(*thisFunc);
48 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000049 std::map<DSNode*, Function *>::const_iterator I =
50 OneCalledFunction.find(calleeNode);
51 return (I == OneCalledFunction.end())? NULL : I->second;
52}
53
Chris Lattnerab8544a2004-10-31 21:56:11 +000054// runOnModule - Calculate the bottom up data structure graphs for each function
55// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000056//
Chris Lattnerab8544a2004-10-31 21:56:11 +000057bool PA::EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000058 CBU = &getAnalysis<CompleteBUDataStructures>();
59
Chris Lattnerab8544a2004-10-31 21:56:11 +000060 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
61 GlobalsGraph->setPrintAuxCalls();
62
Vikram S. Advec5204fb2004-05-23 07:54:02 +000063 // Find equivalence classes of functions called from common call sites.
64 // Fold the CBU graphs for all functions in an equivalence class.
65 buildIndirectFunctionSets(M);
66
67 // Stack of functions used for Tarjan's SCC-finding algorithm.
68 std::vector<Function*> Stack;
Chris Lattner4bbf3df2004-10-31 23:01:34 +000069 std::map<Function*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000070 unsigned NextID = 1;
71
72 if (Function *Main = M.getMainFunction()) {
73 if (!Main->isExternal())
74 processSCC(getOrCreateGraph(*Main), *Main, Stack, NextID, ValMap);
75 } else {
76 std::cerr << "Fold Graphs: No 'main' function found!\n";
77 }
78
79 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
80 if (!I->isExternal() && !FoldedGraphsMap.count(I))
81 processSCC(getOrCreateGraph(*I), *I, Stack, NextID, ValMap);
82
83 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattnerab8544a2004-10-31 21:56:11 +000084 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000085}
86
87
88// buildIndirectFunctionSets - Iterate over the module looking for indirect
89// calls to functions. If a call site can invoke any functions [F1, F2... FN],
90// unify the N functions together in the FuncECs set.
91//
Chris Lattner15d879e2004-10-12 16:52:09 +000092void PA::EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000093 const ActualCalleesTy& AC = CBU->getActualCallees();
94
95 // Loop over all of the indirect calls in the program. If a call site can
96 // call multiple different functions, we need to unify all of the callees into
97 // the same equivalence class.
98 Instruction *LastInst = 0;
99 Function *FirstFunc = 0;
100 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
101 if (I->second->isExternal())
102 continue; // Ignore functions we cannot modify
103
104 CallSite CS = CallSite::get(I->first);
105
106 if (CS.getCalledFunction()) { // Direct call:
107 FuncECs.addElement(I->second); // -- Make sure function has equiv class
108 FirstFunc = I->second; // -- First callee at this site
109 } else { // Else indirect call
110 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
111 // << " from : " << I->first);
112 if (I->first != LastInst) {
113 // This is the first callee from this call site.
114 LastInst = I->first;
115 FirstFunc = I->second;
116 // Instead of storing the lastInst For Indirection call Sites we store
117 // the DSNode for the function ptr arguemnt
118 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000119 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
120 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000121 OneCalledFunction[calleeNode] = FirstFunc;
122 FuncECs.addElement(I->second);
123 } else {
124 // This is not the first possible callee from a particular call site.
125 // Union the callee in with the other functions.
126 FuncECs.unionSetsWith(FirstFunc, I->second);
127#ifndef NDEBUG
128 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000129 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
130 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000131 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) {
Chris Lattnerab8544a2004-10-31 21:56:11 +0000170 // This equiv class has multiple functions: merge their graphs. First,
171 // clone the CBU graph for the leader and make it the common graph for the
172 // equivalence graph.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000173 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)
Chris Lattnerf4985682004-10-31 18:13:19 +0000178 if (DS::isPointerType(AI1->getType()))
179 GraphInfo.argNodes.push_back(mergedG->getNodeForValue(AI1));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000180
Chris Lattnerab8544a2004-10-31 21:56:11 +0000181 // Merge in the graphs of all other functions in this equiv. class. Note
182 // that two or more functions may have the same graph, and it only needs
183 // to be merged in once. Use a set to find repetitions.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000184 std::set<DSGraph*> GraphsMerged;
185 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
186 EqEnd = EqClass.end(); EqI != EqEnd; ++EqI) {
187 Function* F = *EqI;
188 DSGraph*& FG = FoldedGraphsMap[F];
Chris Lattner113cde82004-10-31 17:47:48 +0000189
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000190 if (F == LF || FG == mergedG)
191 continue;
192
193 // Record the "folded" graph for the function.
194 FG = mergedG;
195
196 // Clone this member of the equivalence class into mergedG
197 DSGraph* CBUGraph = &CBU->getDSGraph(*F);
198 if (GraphsMerged.count(CBUGraph) > 0)
199 continue;
200
201 GraphsMerged.insert(CBUGraph);
202 DSGraph::NodeMapTy NodeMap;
Chris Lattner113cde82004-10-31 17:47:48 +0000203
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000204 mergedG->cloneInto(*CBUGraph, mergedG->getScalarMap(),
205 mergedG->getReturnNodes(), NodeMap, 0);
206
207 // Merge the return nodes of all functions together.
208 mergedG->getReturnNodes()[LF].mergeWith(mergedG->getReturnNodes()[F]);
209
210 // Merge the function arguments with all argument nodes found so far.
211 // If there are extra function args, add them to the vector of argNodes
212 Function::aiterator AI2 = F->abegin(), AI2end = F->aend();
213 for (unsigned arg=0, numArgs=GraphInfo.argNodes.size();
Chris Lattner113cde82004-10-31 17:47:48 +0000214 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
215 if (DS::isPointerType(AI2->getType()))
216 GraphInfo.argNodes[arg].mergeWith(mergedG->getNodeForValue(AI2));
217
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000218 for ( ; AI2 != AI2end; ++AI2)
Chris Lattner113cde82004-10-31 17:47:48 +0000219 if (DS::isPointerType(AI2->getType()))
220 GraphInfo.argNodes.push_back(mergedG->getNodeForValue(AI2));
221 DEBUG(mergedG->AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000222 }
223 }
224 }
225 DEBUG(std::cerr << "\n");
226}
227
228
Chris Lattner15d879e2004-10-12 16:52:09 +0000229DSGraph &PA::EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000230 // Has the graph already been created?
231 DSGraph *&Graph = FoldedGraphsMap[&F];
232 if (Graph) return *Graph;
233
Chris Lattnerab8544a2004-10-31 21:56:11 +0000234 return *cloneGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000235}
236
Chris Lattner15d879e2004-10-12 16:52:09 +0000237DSGraph *PA::EquivClassGraphs::cloneGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000238 DSGraph *&Graph = FoldedGraphsMap[&F];
239 DSGraph &CBUGraph = CBU->getDSGraph(F);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000240 assert((Graph == NULL || Graph == &CBUGraph) && "Cloning a graph twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000241
242 // Copy the CBU graph...
243 Graph = new DSGraph(CBUGraph); // updates the map via reference
244 Graph->setGlobalsGraph(&getGlobalsGraph());
245 Graph->setPrintAuxCalls();
246
247 // Make sure to update the FoldedGraphsMap map for all functions in the graph!
248 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
249 I != Graph->getReturnNodes().end(); ++I)
250 if (I->first != &F) {
251 DSGraph*& FG = FoldedGraphsMap[I->first];
252 assert(FG == NULL || FG == &CBU->getDSGraph(*I->first) &&
253 "Merging function in SCC twice?");
254 FG = Graph;
255 }
256
257 return Graph;
258}
259
260
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000261unsigned PA::EquivClassGraphs::processSCC(DSGraph &FG, Function &F,
Chris Lattner15d879e2004-10-12 16:52:09 +0000262 std::vector<Function*> &Stack,
263 unsigned &NextID,
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000264 std::map<Function*,unsigned> &ValMap){
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000265 DEBUG(std::cerr << " ProcessSCC for function " << F.getName() << "\n");
266
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000267 std::map<Function*, unsigned>::iterator It = ValMap.lower_bound(&F);
268 if (It != ValMap.end() && It->first == &F)
269 return It->second;
270
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000271 unsigned Min = NextID++, MyID = Min;
272 ValMap[&F] = Min;
273 Stack.push_back(&F);
274
275 // The edges out of the current node are the call site targets...
276 for (unsigned i = 0, e = FG.getFunctionCalls().size(); i != e; ++i) {
277 Instruction *Call = FG.getFunctionCalls()[i].getCallSite().getInstruction();
278
279 // Loop over all of the actually called functions...
280 ActualCalleesTy::const_iterator I, E;
281 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
282 if (!I->second->isExternal()) {
283 DSGraph &CalleeG = getOrCreateGraph(*I->second);
284
285 // Have we visited the destination function yet?
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000286 std::map<Function*, unsigned>::iterator It = ValMap.find(I->second);
287 unsigned M = processSCC(CalleeG, *I->second, Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000288 if (M < Min) Min = M;
289 }
290 }
291
292 assert(ValMap[&F] == MyID && "SCC construction assumption wrong!");
293 if (Min != MyID)
294 return Min; // This is part of a larger SCC!
295
296 // If this is a new SCC, process it now.
297 bool IsMultiNodeSCC = false;
298 while (Stack.back() != &F) {
299 DSGraph *NG = &getOrCreateGraph(* Stack.back());
300 ValMap[Stack.back()] = ~0U;
301
302 // Since all SCCs must be the same as those found in CBU, we do not need to
303 // do any merging. Make sure all functions in the SCC share the same graph.
304 assert(NG == &FG &&
305 "FoldGraphs: Functions in the same SCC have different graphs?");
306
307 Stack.pop_back();
308 IsMultiNodeSCC = true;
309 }
310
311 // Clean up the graph before we start inlining a bunch again...
312 if (IsMultiNodeSCC)
313 FG.removeTriviallyDeadNodes();
314
315 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000316
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000317 processGraph(FG, F);
318 ValMap[&F] = ~0U;
319 return MyID;
320}
321
322
323/// processGraph - Process the CBU graphs for the program in bottom-up order on
324/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattner15d879e2004-10-12 16:52:09 +0000325void PA::EquivClassGraphs::processGraph(DSGraph &G, Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000326 DEBUG(std::cerr << " ProcessGraph for function " << F.getName() << "\n");
327
328 hash_set<Instruction*> calls;
329
Chris Lattnerab8544a2004-10-31 21:56:11 +0000330 DSGraph* CallerGraph = &getOrCreateGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000331
332 // Else we need to inline some callee graph. Visit all call sites.
333 // The edges out of the current node are the call site targets...
334 for (unsigned i=0, e = CallerGraph->getFunctionCalls().size(); i != e; ++i) {
335 const DSCallSite &CS = CallerGraph->getFunctionCalls()[i];
336 Instruction *TheCall = CS.getCallSite().getInstruction();
337
338 assert(calls.insert(TheCall).second &&
339 "Call instruction occurs multiple times in graph??");
340
341 // Inline the common callee graph into the current graph, if the callee
342 // graph has not changed. Note that all callees should have the same
343 // graph so we only need to do this once.
344 //
345 DSGraph* CalleeGraph = NULL;
346 ActualCalleesTy::const_iterator I, E;
347 tie(I, E) = getActualCallees().equal_range(TheCall);
348 unsigned TNum, Num;
349
350 // Loop over all potential callees to find the first non-external callee.
351 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
352 if (!I->second->isExternal())
353 break;
354
355 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000356 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000357 Function *CalleeFunc = I->second;
358
359 // Merge the callee's graph into this graph, if not already the same.
360 // Callees in the same equivalence class (which subsumes those
361 // in the same SCCs) have the same graph. Note that all recursion
362 // including self-recursion have been folded in the equiv classes.
363 //
364 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
365 if (CalleeGraph != CallerGraph) {
366 ++NumFoldGraphInlines;
367 CallerGraph->mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
368 DSGraph::KeepModRefBits |
369 DSGraph::StripAllocaBit |
370 DSGraph::DontCloneCallNodes |
371 DSGraph::DontCloneAuxCallNodes);
372 DEBUG(std::cerr << " Inlining graph [" << i << "/" << e-1
373 << ":" << TNum << "/" << Num-1 << "] for "
374 << CalleeFunc->getName() << "["
375 << CalleeGraph->getGraphSize() << "+"
376 << CalleeGraph->getAuxFunctionCalls().size()
377 << "] into '" /*<< CallerGraph->getFunctionNames()*/ << "' ["
378 << CallerGraph->getGraphSize() << "+"
379 << CallerGraph->getAuxFunctionCalls().size()
380 << "]\n");
381 }
382 }
383
384#ifndef NDEBUG
385 // Now loop over the rest of the callees and make sure they have the
386 // same graph as the one inlined above.
387 if (CalleeGraph)
388 for (++I, ++TNum; I != E; ++I, ++TNum)
389 if (!I->second->isExternal())
390 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
391 "Callees at a call site have different graphs?");
392#endif
393 }
394
395 // Recompute the Incomplete markers
396 if (CallerGraph != NULL) {
397 assert(CallerGraph->getInlinedGlobals().empty());
398 CallerGraph->maskIncompleteMarkers();
399 CallerGraph->markIncompleteNodes(DSGraph::MarkFormalArgs);
400
401 // Delete dead nodes. Treat globals that are unreachable but that can
402 // reach live nodes as live.
403 CallerGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
404 }
405
Chris Lattnerab8544a2004-10-31 21:56:11 +0000406
407 // When this graph is finalized, clone the globals in the graph into the
408 // globals graph to make sure it has everything, from all graphs.
409 DSScalarMap &MainSM = CallerGraph->getScalarMap();
410 ReachabilityCloner RC(*CallerGraph->getGlobalsGraph(), *CallerGraph,
411 DSGraph::StripAllocaBit);
412
413 // Clone everything reachable from globals in the function graph into the
414 // globals graph.
415 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
416 E = MainSM.global_end(); I != E; ++I)
417 RC.getClonedNH(MainSM[*I]);
418
419
Chris Lattner15d879e2004-10-12 16:52:09 +0000420 DEBUG(std::cerr << " --DONE ProcessGraph for function "
421 << F.getName() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000422}