blob: 8dc2064810436489dba38fc7ef34728aa00780c5 [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"
Chris Lattner7aed7172005-03-12 11:51:30 +000018#include "llvm/Analysis/DataStructure/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 Lattneradfd5f12005-03-13 19:51:24 +000032 RegisterAnalysis<EquivClassGraphs> X("eqdatastructure",
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
Chris Lattner31d3f672004-10-31 23:41:26 +000040#ifndef NDEBUG
41template<typename GT>
42static void CheckAllGraphs(Module *M, GT &ECGraphs) {
43 DSGraph &GG = ECGraphs.getGlobalsGraph();
44
45 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
46 if (!I->isExternal()) {
47 DSGraph &G = ECGraphs.getDSGraph(*I);
48
49 DSGraph::NodeMapTy GlobalsGraphNodeMapping;
50 for (DSScalarMap::global_iterator I = G.getScalarMap().global_begin(),
51 E = G.getScalarMap().global_end(); I != E; ++I)
52 DSGraph::computeNodeMapping(G.getNodeForValue(*I),
53 GG.getNodeForValue(*I),
54 GlobalsGraphNodeMapping);
55 }
56}
57#endif
58
Chris Lattner4457f7e2004-11-01 21:07:05 +000059// getSomeCalleeForCallSite - Return any one callee function at a call site.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000060//
Chris Lattneradfd5f12005-03-13 19:51:24 +000061Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
Vikram S. Advec5204fb2004-05-23 07:54:02 +000062 Function *thisFunc = CS.getCaller();
Chris Lattner4457f7e2004-11-01 21:07:05 +000063 assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000064 DSGraph &DSG = getDSGraph(*thisFunc);
65 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000066 std::map<DSNode*, Function *>::const_iterator I =
67 OneCalledFunction.find(calleeNode);
68 return (I == OneCalledFunction.end())? NULL : I->second;
69}
70
Chris Lattnerab8544a2004-10-31 21:56:11 +000071// runOnModule - Calculate the bottom up data structure graphs for each function
72// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000073//
Chris Lattnerb25959a2005-03-12 12:08:52 +000074bool EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000075 CBU = &getAnalysis<CompleteBUDataStructures>();
Chris Lattner04252fe2004-11-11 22:11:17 +000076 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000077
Chris Lattnerab8544a2004-10-31 21:56:11 +000078 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
79 GlobalsGraph->setPrintAuxCalls();
80
Chris Lattner31d3f672004-10-31 23:41:26 +000081 ActualCallees = CBU->getActualCallees();
82
Vikram S. Advec5204fb2004-05-23 07:54:02 +000083 // Find equivalence classes of functions called from common call sites.
84 // Fold the CBU graphs for all functions in an equivalence class.
85 buildIndirectFunctionSets(M);
86
87 // Stack of functions used for Tarjan's SCC-finding algorithm.
Chris Lattner033a7d52004-11-02 17:51:11 +000088 std::vector<DSGraph*> Stack;
89 std::map<DSGraph*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000090 unsigned NextID = 1;
91
Chris Lattnera66e3532005-03-13 20:15:06 +000092 Function *MainFunc = M.getMainFunction();
93 if (MainFunc && !MainFunc->isExternal()) {
94 processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000095 } else {
96 std::cerr << "Fold Graphs: No 'main' function found!\n";
97 }
98
99 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner033a7d52004-11-02 17:51:11 +0000100 if (!I->isExternal())
101 processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000102
Chris Lattner31d3f672004-10-31 23:41:26 +0000103 DEBUG(CheckAllGraphs(&M, *this));
104
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000105 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +0000106
107 // Merge the globals variables (not the calls) from the globals graph back
108 // into the main function's graph so that the main function contains all of
109 // the information about global pools and GV usage in the program.
110 if (MainFunc) {
111 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
112 const DSGraph &GG = *MainGraph.getGlobalsGraph();
113 ReachabilityCloner RC(MainGraph, GG,
114 DSGraph::DontCloneCallNodes |
115 DSGraph::DontCloneAuxCallNodes);
116
117 // Clone the global nodes into this graph.
118 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
119 E = GG.getScalarMap().global_end(); I != E; ++I)
120 if (isa<GlobalVariable>(*I))
121 RC.getClonedNH(GG.getNodeForValue(*I));
122
123 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
124 DSGraph::IgnoreGlobals);
125 }
126
Chris Lattnerab8544a2004-10-31 21:56:11 +0000127 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000128}
129
130
131// buildIndirectFunctionSets - Iterate over the module looking for indirect
132// calls to functions. If a call site can invoke any functions [F1, F2... FN],
133// unify the N functions together in the FuncECs set.
134//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000135void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000136 const ActualCalleesTy& AC = CBU->getActualCallees();
Chris Lattner77408b82004-11-01 20:37:00 +0000137
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000138 // Loop over all of the indirect calls in the program. If a call site can
139 // call multiple different functions, we need to unify all of the callees into
140 // the same equivalence class.
141 Instruction *LastInst = 0;
142 Function *FirstFunc = 0;
143 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
144 if (I->second->isExternal())
145 continue; // Ignore functions we cannot modify
146
147 CallSite CS = CallSite::get(I->first);
148
149 if (CS.getCalledFunction()) { // Direct call:
150 FuncECs.addElement(I->second); // -- Make sure function has equiv class
151 FirstFunc = I->second; // -- First callee at this site
152 } else { // Else indirect call
153 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
154 // << " from : " << I->first);
155 if (I->first != LastInst) {
156 // This is the first callee from this call site.
157 LastInst = I->first;
158 FirstFunc = I->second;
159 // Instead of storing the lastInst For Indirection call Sites we store
160 // the DSNode for the function ptr arguemnt
161 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000162 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
163 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000164 OneCalledFunction[calleeNode] = FirstFunc;
165 FuncECs.addElement(I->second);
166 } else {
167 // This is not the first possible callee from a particular call site.
168 // Union the callee in with the other functions.
169 FuncECs.unionSetsWith(FirstFunc, I->second);
170#ifndef NDEBUG
171 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000172 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
173 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000174 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
175#endif
176 }
177 }
178
179 // Now include all functions that share a graph with any function in the
180 // equivalence class. More precisely, if F is in the class, and G(F) is
181 // its graph, then we include all other functions that are also in G(F).
182 // Currently, that is just the functions in the same call-graph-SCC as F.
183 //
184 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
185 const DSGraph::ReturnNodesTy &RetNodes = funcDSGraph.getReturnNodes();
186 for (DSGraph::ReturnNodesTy::const_iterator RI=RetNodes.begin(),
187 RE=RetNodes.end(); RI != RE; ++RI)
188 FuncECs.unionSetsWith(FirstFunc, RI->first);
189 }
190
191 // Now that all of the equivalences have been built, merge the graphs for
192 // each equivalence class.
193 //
194 std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
195 DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
196 for (std::set<Function*>::iterator LI = leaderSet.begin(),
197 LE = leaderSet.end(); LI != LE; ++LI) {
198
199 Function* LF = *LI;
200 const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
201
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000202 if (EqClass.size() > 1) {
Chris Lattner983baf42004-11-02 06:38:58 +0000203#ifndef NDEBUG
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000204 DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = ");
205 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
206 EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
207 DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
208 DEBUG(std::cerr << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000209#endif
210
Chris Lattnerab8544a2004-10-31 21:56:11 +0000211 // This equiv class has multiple functions: merge their graphs. First,
212 // clone the CBU graph for the leader and make it the common graph for the
213 // equivalence graph.
Chris Lattner983baf42004-11-02 06:38:58 +0000214 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000215
Chris Lattner77408b82004-11-01 20:37:00 +0000216 // Record the argument nodes for use in merging later below.
217 std::vector<DSNodeHandle> ArgNodes;
218
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000219 for (Function::aiterator AI1 = LF->abegin(); AI1 != LF->aend(); ++AI1)
Chris Lattnerf4985682004-10-31 18:13:19 +0000220 if (DS::isPointerType(AI1->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000221 ArgNodes.push_back(MergedG.getNodeForValue(AI1));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000222
Chris Lattnerab8544a2004-10-31 21:56:11 +0000223 // Merge in the graphs of all other functions in this equiv. class. Note
224 // that two or more functions may have the same graph, and it only needs
Chris Lattner983baf42004-11-02 06:38:58 +0000225 // to be merged in once.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000226 std::set<DSGraph*> GraphsMerged;
Chris Lattner983baf42004-11-02 06:38:58 +0000227 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Chris Lattner113cde82004-10-31 17:47:48 +0000228
Chris Lattner983baf42004-11-02 06:38:58 +0000229 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
230 E = EqClass.end(); EqI != E; ++EqI) {
231 Function *F = *EqI;
232 DSGraph *&FG = DSInfo[F];
233
234 DSGraph &CBUGraph = CBU->getDSGraph(*F);
235 if (!GraphsMerged.insert(&CBUGraph).second)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000236 continue;
237
238 // Record the "folded" graph for the function.
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000239 for (DSGraph::ReturnNodesTy::iterator
240 I = CBUGraph.getReturnNodes().begin(),
241 E = CBUGraph.getReturnNodes().end();
242 I != E; ++I) {
243 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
244 DSInfo[I->first] = &MergedG;
245 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000246
Chris Lattner983baf42004-11-02 06:38:58 +0000247 // Clone this member of the equivalence class into MergedG.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000248 DSGraph::NodeMapTy NodeMap;
Chris Lattner113cde82004-10-31 17:47:48 +0000249
Chris Lattner983baf42004-11-02 06:38:58 +0000250 MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000251 MergedG.getReturnNodes(), NodeMap, 0);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000252
253 // Merge the return nodes of all functions together.
Chris Lattner983baf42004-11-02 06:38:58 +0000254 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000255
256 // Merge the function arguments with all argument nodes found so far.
257 // If there are extra function args, add them to the vector of argNodes
258 Function::aiterator AI2 = F->abegin(), AI2end = F->aend();
Chris Lattner77408b82004-11-01 20:37:00 +0000259 for (unsigned arg=0, numArgs = ArgNodes.size();
Chris Lattner113cde82004-10-31 17:47:48 +0000260 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
261 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000262 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Chris Lattner113cde82004-10-31 17:47:48 +0000263
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000264 for ( ; AI2 != AI2end; ++AI2)
Chris Lattner113cde82004-10-31 17:47:48 +0000265 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000266 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
267 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000268 }
269 }
270 }
271 DEBUG(std::cerr << "\n");
272}
273
274
Chris Lattnerb25959a2005-03-12 12:08:52 +0000275DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000276 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000277 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000278 if (Graph) return *Graph;
279
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000280 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000281
282 // Copy the CBU graph...
283 Graph = new DSGraph(CBUGraph); // updates the map via reference
284 Graph->setGlobalsGraph(&getGlobalsGraph());
285 Graph->setPrintAuxCalls();
286
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000287 // Make sure to update the DSInfo map for all functions in the graph!
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000288 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
289 I != Graph->getReturnNodes().end(); ++I)
290 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000291 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000292 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000293 FG = Graph;
294 }
295
Chris Lattner68f96582004-11-01 19:54:06 +0000296 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000297}
298
299
Chris Lattnerb25959a2005-03-12 12:08:52 +0000300unsigned EquivClassGraphs::
Chris Lattner033a7d52004-11-02 17:51:11 +0000301processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
302 std::map<DSGraph*, unsigned> &ValMap) {
303 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
304 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000305 return It->second;
306
Chris Lattner033a7d52004-11-02 17:51:11 +0000307 DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
308 << "\n");
309
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000310 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000311 ValMap[&FG] = Min;
312 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000313
314 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000315 for (DSGraph::fc_iterator CI = FG.fc_begin(), E = FG.fc_end(); CI != E; ++CI){
316 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000317
318 // Loop over all of the actually called functions...
319 ActualCalleesTy::const_iterator I, E;
320 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
321 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000322 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000323 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000324 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000325 if (M < Min) Min = M;
326 }
327 }
328
Chris Lattner033a7d52004-11-02 17:51:11 +0000329 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000330 if (Min != MyID)
331 return Min; // This is part of a larger SCC!
332
333 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000334 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000335 while (Stack.back() != &FG) {
336 DSGraph *NG = Stack.back();
337 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000338
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000339 // If the SCC found is not the same as those found in CBU, make sure to
340 // merge the graphs as appropriate.
341 DSGraph::NodeMapTy NodeMap;
342 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
343
344 // Update the DSInfo map and delete the old graph...
345 for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
346 I != NG->getReturnNodes().end(); ++I)
347 DSInfo[I->first] = &FG;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000348
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000349 // Remove NG from the ValMap since the pointer may get recycled.
350 ValMap.erase(NG);
351 delete NG;
352 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000353 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000354 }
355
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000356 // Clean up the graph before we start inlining a bunch again.
357 if (MergedGraphs)
358 FG.removeTriviallyDeadNodes();
359
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000360 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000361
Chris Lattner033a7d52004-11-02 17:51:11 +0000362 processGraph(FG);
363 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000364 return MyID;
365}
366
367
368/// processGraph - Process the CBU graphs for the program in bottom-up order on
369/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000370void EquivClassGraphs::processGraph(DSGraph &G) {
Chris Lattner033a7d52004-11-02 17:51:11 +0000371 DEBUG(std::cerr << " ProcessGraph for function "
372 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000373
374 hash_set<Instruction*> calls;
375
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000376 // Else we need to inline some callee graph. Visit all call sites.
377 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000378 unsigned i = 0;
379 for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E;
380 ++CI, ++i) {
381 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000382 Instruction *TheCall = CS.getCallSite().getInstruction();
383
384 assert(calls.insert(TheCall).second &&
385 "Call instruction occurs multiple times in graph??");
386
387 // Inline the common callee graph into the current graph, if the callee
388 // graph has not changed. Note that all callees should have the same
389 // graph so we only need to do this once.
390 //
391 DSGraph* CalleeGraph = NULL;
392 ActualCalleesTy::const_iterator I, E;
393 tie(I, E) = getActualCallees().equal_range(TheCall);
394 unsigned TNum, Num;
395
396 // Loop over all potential callees to find the first non-external callee.
397 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
398 if (!I->second->isExternal())
399 break;
400
401 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000402 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000403 Function *CalleeFunc = I->second;
404
405 // Merge the callee's graph into this graph, if not already the same.
406 // Callees in the same equivalence class (which subsumes those
407 // in the same SCCs) have the same graph. Note that all recursion
408 // including self-recursion have been folded in the equiv classes.
409 //
410 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000411 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000412 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000413 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
414 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
415 DSGraph::DontCloneCallNodes |
416 DSGraph::DontCloneAuxCallNodes);
Chris Lattner6538f422005-01-30 23:51:25 +0000417 DEBUG(std::cerr << " Inlining graph [" << i << "/"
418 << G.getFunctionCalls().size()-1
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000419 << ":" << TNum << "/" << Num-1 << "] for "
420 << CalleeFunc->getName() << "["
421 << CalleeGraph->getGraphSize() << "+"
422 << CalleeGraph->getAuxFunctionCalls().size()
Chris Lattner033a7d52004-11-02 17:51:11 +0000423 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
424 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000425 << "]\n");
426 }
427 }
428
429#ifndef NDEBUG
430 // Now loop over the rest of the callees and make sure they have the
431 // same graph as the one inlined above.
432 if (CalleeGraph)
433 for (++I, ++TNum; I != E; ++I, ++TNum)
434 if (!I->second->isExternal())
435 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
436 "Callees at a call site have different graphs?");
437#endif
438 }
439
Chris Lattner31d3f672004-10-31 23:41:26 +0000440 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000441 assert(G.getInlinedGlobals().empty());
442 G.maskIncompleteMarkers();
443 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner31d3f672004-10-31 23:41:26 +0000444
445 // Delete dead nodes. Treat globals that are unreachable but that can
446 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000447 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000448
449 // When this graph is finalized, clone the globals in the graph into the
450 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000451 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000452
453 // Clone everything reachable from globals in the function graph into the
454 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000455 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000456 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
457 E = MainSM.global_end(); I != E; ++I)
458 RC.getClonedNH(MainSM[*I]);
459
Chris Lattner31d3f672004-10-31 23:41:26 +0000460 DEBUG(std::cerr << " -- DONE ProcessGraph for function "
Chris Lattner033a7d52004-11-02 17:51:11 +0000461 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000462}