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