blob: ef1e01aae4b736fcfc9c800b32a7c4b0c96b82cd [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);
Chris Lattnerb2b17bb2005-03-14 19:22:47 +000048 if (G.getReturnNodes().begin()->first != I)
49 continue; // Only check a graph once.
Chris Lattner31d3f672004-10-31 23:41:26 +000050
51 DSGraph::NodeMapTy GlobalsGraphNodeMapping;
Chris Lattnerb0f92e32005-03-15 00:58:16 +000052 G.computeGToGGMapping(GlobalsGraphNodeMapping);
Chris Lattner31d3f672004-10-31 23:41:26 +000053 }
54}
55#endif
56
Chris Lattner4457f7e2004-11-01 21:07:05 +000057// getSomeCalleeForCallSite - Return any one callee function at a call site.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000058//
Chris Lattneradfd5f12005-03-13 19:51:24 +000059Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
Vikram S. Advec5204fb2004-05-23 07:54:02 +000060 Function *thisFunc = CS.getCaller();
Chris Lattner4457f7e2004-11-01 21:07:05 +000061 assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000062 DSGraph &DSG = getDSGraph(*thisFunc);
63 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000064 std::map<DSNode*, Function *>::const_iterator I =
65 OneCalledFunction.find(calleeNode);
66 return (I == OneCalledFunction.end())? NULL : I->second;
67}
68
Chris Lattnerab8544a2004-10-31 21:56:11 +000069// runOnModule - Calculate the bottom up data structure graphs for each function
70// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000071//
Chris Lattnerb25959a2005-03-12 12:08:52 +000072bool EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000073 CBU = &getAnalysis<CompleteBUDataStructures>();
Chris Lattner04252fe2004-11-11 22:11:17 +000074 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000075
Chris Lattnerab8544a2004-10-31 21:56:11 +000076 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
77 GlobalsGraph->setPrintAuxCalls();
78
Chris Lattner31d3f672004-10-31 23:41:26 +000079 ActualCallees = CBU->getActualCallees();
80
Vikram S. Advec5204fb2004-05-23 07:54:02 +000081 // Find equivalence classes of functions called from common call sites.
82 // Fold the CBU graphs for all functions in an equivalence class.
83 buildIndirectFunctionSets(M);
84
85 // Stack of functions used for Tarjan's SCC-finding algorithm.
Chris Lattner033a7d52004-11-02 17:51:11 +000086 std::vector<DSGraph*> Stack;
87 std::map<DSGraph*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000088 unsigned NextID = 1;
89
Chris Lattnera66e3532005-03-13 20:15:06 +000090 Function *MainFunc = M.getMainFunction();
91 if (MainFunc && !MainFunc->isExternal()) {
92 processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000093 } else {
94 std::cerr << "Fold Graphs: No 'main' function found!\n";
95 }
96
97 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner033a7d52004-11-02 17:51:11 +000098 if (!I->isExternal())
99 processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000100
Chris Lattner31d3f672004-10-31 23:41:26 +0000101 DEBUG(CheckAllGraphs(&M, *this));
102
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000103 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +0000104
105 // Merge the globals variables (not the calls) from the globals graph back
106 // into the main function's graph so that the main function contains all of
107 // the information about global pools and GV usage in the program.
108 if (MainFunc) {
109 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
110 const DSGraph &GG = *MainGraph.getGlobalsGraph();
111 ReachabilityCloner RC(MainGraph, GG,
112 DSGraph::DontCloneCallNodes |
113 DSGraph::DontCloneAuxCallNodes);
114
115 // Clone the global nodes into this graph.
116 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
117 E = GG.getScalarMap().global_end(); I != E; ++I)
118 if (isa<GlobalVariable>(*I))
119 RC.getClonedNH(GG.getNodeForValue(*I));
120
Chris Lattner270cf502005-03-13 20:32:26 +0000121 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000122 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
123 DSGraph::IgnoreGlobals);
124 }
125
Chris Lattnerab8544a2004-10-31 21:56:11 +0000126 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000127}
128
129
130// buildIndirectFunctionSets - Iterate over the module looking for indirect
131// calls to functions. If a call site can invoke any functions [F1, F2... FN],
132// unify the N functions together in the FuncECs set.
133//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000134void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000135 const ActualCalleesTy& AC = CBU->getActualCallees();
Chris Lattner77408b82004-11-01 20:37:00 +0000136
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000137 // Loop over all of the indirect calls in the program. If a call site can
138 // call multiple different functions, we need to unify all of the callees into
139 // the same equivalence class.
140 Instruction *LastInst = 0;
141 Function *FirstFunc = 0;
142 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
143 if (I->second->isExternal())
144 continue; // Ignore functions we cannot modify
145
146 CallSite CS = CallSite::get(I->first);
147
148 if (CS.getCalledFunction()) { // Direct call:
149 FuncECs.addElement(I->second); // -- Make sure function has equiv class
150 FirstFunc = I->second; // -- First callee at this site
151 } else { // Else indirect call
152 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
153 // << " from : " << I->first);
154 if (I->first != LastInst) {
155 // This is the first callee from this call site.
156 LastInst = I->first;
157 FirstFunc = I->second;
158 // Instead of storing the lastInst For Indirection call Sites we store
159 // the DSNode for the function ptr arguemnt
160 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000161 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
162 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000163 OneCalledFunction[calleeNode] = FirstFunc;
164 FuncECs.addElement(I->second);
165 } else {
166 // This is not the first possible callee from a particular call site.
167 // Union the callee in with the other functions.
168 FuncECs.unionSetsWith(FirstFunc, I->second);
169#ifndef NDEBUG
170 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000171 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
172 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000173 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
174#endif
175 }
176 }
177
178 // Now include all functions that share a graph with any function in the
179 // equivalence class. More precisely, if F is in the class, and G(F) is
180 // its graph, then we include all other functions that are also in G(F).
181 // Currently, that is just the functions in the same call-graph-SCC as F.
182 //
183 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
184 const DSGraph::ReturnNodesTy &RetNodes = funcDSGraph.getReturnNodes();
185 for (DSGraph::ReturnNodesTy::const_iterator RI=RetNodes.begin(),
186 RE=RetNodes.end(); RI != RE; ++RI)
187 FuncECs.unionSetsWith(FirstFunc, RI->first);
188 }
189
190 // Now that all of the equivalences have been built, merge the graphs for
191 // each equivalence class.
192 //
193 std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
194 DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
195 for (std::set<Function*>::iterator LI = leaderSet.begin(),
196 LE = leaderSet.end(); LI != LE; ++LI) {
197
198 Function* LF = *LI;
199 const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
200
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000201 if (EqClass.size() > 1) {
Chris Lattner983baf42004-11-02 06:38:58 +0000202#ifndef NDEBUG
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000203 DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = ");
204 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
205 EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
206 DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
207 DEBUG(std::cerr << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000208#endif
209
Chris Lattnerab8544a2004-10-31 21:56:11 +0000210 // This equiv class has multiple functions: merge their graphs. First,
211 // clone the CBU graph for the leader and make it the common graph for the
212 // equivalence graph.
Chris Lattner983baf42004-11-02 06:38:58 +0000213 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000214
Chris Lattner77408b82004-11-01 20:37:00 +0000215 // Record the argument nodes for use in merging later below.
216 std::vector<DSNodeHandle> ArgNodes;
217
Chris Lattnere4d5c442005-03-15 04:54:21 +0000218 for (Function::arg_iterator AI1 = LF->arg_begin(); AI1 != LF->arg_end(); ++AI1)
Chris Lattnerf4985682004-10-31 18:13:19 +0000219 if (DS::isPointerType(AI1->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000220 ArgNodes.push_back(MergedG.getNodeForValue(AI1));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000221
Chris Lattnerab8544a2004-10-31 21:56:11 +0000222 // Merge in the graphs of all other functions in this equiv. class. Note
223 // that two or more functions may have the same graph, and it only needs
Chris Lattner983baf42004-11-02 06:38:58 +0000224 // to be merged in once.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000225 std::set<DSGraph*> GraphsMerged;
Chris Lattner983baf42004-11-02 06:38:58 +0000226 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Chris Lattner113cde82004-10-31 17:47:48 +0000227
Chris Lattner983baf42004-11-02 06:38:58 +0000228 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
229 E = EqClass.end(); EqI != E; ++EqI) {
230 Function *F = *EqI;
231 DSGraph *&FG = DSInfo[F];
232
233 DSGraph &CBUGraph = CBU->getDSGraph(*F);
234 if (!GraphsMerged.insert(&CBUGraph).second)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000235 continue;
236
237 // Record the "folded" graph for the function.
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000238 for (DSGraph::ReturnNodesTy::iterator
239 I = CBUGraph.getReturnNodes().begin(),
240 E = CBUGraph.getReturnNodes().end();
241 I != E; ++I) {
242 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
243 DSInfo[I->first] = &MergedG;
244 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000245
Chris Lattner983baf42004-11-02 06:38:58 +0000246 // Clone this member of the equivalence class into MergedG.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000247 DSGraph::NodeMapTy NodeMap;
Chris Lattner113cde82004-10-31 17:47:48 +0000248
Chris Lattner983baf42004-11-02 06:38:58 +0000249 MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000250 MergedG.getReturnNodes(), NodeMap, 0);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000251
252 // Merge the return nodes of all functions together.
Chris Lattner983baf42004-11-02 06:38:58 +0000253 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000254
255 // Merge the function arguments with all argument nodes found so far.
256 // If there are extra function args, add them to the vector of argNodes
Chris Lattnere4d5c442005-03-15 04:54:21 +0000257 Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
Chris Lattner77408b82004-11-01 20:37:00 +0000258 for (unsigned arg=0, numArgs = ArgNodes.size();
Chris Lattner113cde82004-10-31 17:47:48 +0000259 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
260 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000261 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Chris Lattner113cde82004-10-31 17:47:48 +0000262
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000263 for ( ; AI2 != AI2end; ++AI2)
Chris Lattner113cde82004-10-31 17:47:48 +0000264 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000265 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
266 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000267 }
268 }
269 }
270 DEBUG(std::cerr << "\n");
271}
272
273
Chris Lattnerb25959a2005-03-12 12:08:52 +0000274DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000275 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000276 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000277 if (Graph) return *Graph;
278
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000279 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000280
281 // Copy the CBU graph...
282 Graph = new DSGraph(CBUGraph); // updates the map via reference
283 Graph->setGlobalsGraph(&getGlobalsGraph());
284 Graph->setPrintAuxCalls();
285
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000286 // Make sure to update the DSInfo map for all functions in the graph!
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000287 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
288 I != Graph->getReturnNodes().end(); ++I)
289 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000290 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000291 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000292 FG = Graph;
293 }
294
Chris Lattner68f96582004-11-01 19:54:06 +0000295 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000296}
297
298
Chris Lattnerb25959a2005-03-12 12:08:52 +0000299unsigned EquivClassGraphs::
Chris Lattner033a7d52004-11-02 17:51:11 +0000300processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
301 std::map<DSGraph*, unsigned> &ValMap) {
302 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
303 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000304 return It->second;
305
Chris Lattner033a7d52004-11-02 17:51:11 +0000306 DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
307 << "\n");
308
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000309 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000310 ValMap[&FG] = Min;
311 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000312
313 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000314 for (DSGraph::fc_iterator CI = FG.fc_begin(), E = FG.fc_end(); CI != E; ++CI){
315 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000316
317 // Loop over all of the actually called functions...
318 ActualCalleesTy::const_iterator I, E;
319 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
320 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000321 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000322 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000323 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000324 if (M < Min) Min = M;
325 }
326 }
327
Chris Lattner033a7d52004-11-02 17:51:11 +0000328 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000329 if (Min != MyID)
330 return Min; // This is part of a larger SCC!
331
332 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000333 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000334 while (Stack.back() != &FG) {
335 DSGraph *NG = Stack.back();
336 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000337
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000338 // If the SCC found is not the same as those found in CBU, make sure to
339 // merge the graphs as appropriate.
340 DSGraph::NodeMapTy NodeMap;
341 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
342
343 // Update the DSInfo map and delete the old graph...
344 for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
345 I != NG->getReturnNodes().end(); ++I)
346 DSInfo[I->first] = &FG;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000347
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000348 // Remove NG from the ValMap since the pointer may get recycled.
349 ValMap.erase(NG);
350 delete NG;
351 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000352 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000353 }
354
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000355 // Clean up the graph before we start inlining a bunch again.
356 if (MergedGraphs)
357 FG.removeTriviallyDeadNodes();
358
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000359 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000360
Chris Lattner033a7d52004-11-02 17:51:11 +0000361 processGraph(FG);
362 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000363 return MyID;
364}
365
366
367/// processGraph - Process the CBU graphs for the program in bottom-up order on
368/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000369void EquivClassGraphs::processGraph(DSGraph &G) {
Chris Lattner033a7d52004-11-02 17:51:11 +0000370 DEBUG(std::cerr << " ProcessGraph for function "
371 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000372
373 hash_set<Instruction*> calls;
374
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000375 // Else we need to inline some callee graph. Visit all call sites.
376 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000377 unsigned i = 0;
378 for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E;
379 ++CI, ++i) {
380 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000381 Instruction *TheCall = CS.getCallSite().getInstruction();
382
383 assert(calls.insert(TheCall).second &&
384 "Call instruction occurs multiple times in graph??");
385
386 // Inline the common callee graph into the current graph, if the callee
387 // graph has not changed. Note that all callees should have the same
388 // graph so we only need to do this once.
389 //
390 DSGraph* CalleeGraph = NULL;
391 ActualCalleesTy::const_iterator I, E;
392 tie(I, E) = getActualCallees().equal_range(TheCall);
393 unsigned TNum, Num;
394
395 // Loop over all potential callees to find the first non-external callee.
396 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
397 if (!I->second->isExternal())
398 break;
399
400 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000401 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000402 Function *CalleeFunc = I->second;
403
404 // Merge the callee's graph into this graph, if not already the same.
405 // Callees in the same equivalence class (which subsumes those
406 // in the same SCCs) have the same graph. Note that all recursion
407 // including self-recursion have been folded in the equiv classes.
408 //
409 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000410 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000411 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000412 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
413 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
414 DSGraph::DontCloneCallNodes |
415 DSGraph::DontCloneAuxCallNodes);
Chris Lattner6538f422005-01-30 23:51:25 +0000416 DEBUG(std::cerr << " Inlining graph [" << i << "/"
417 << G.getFunctionCalls().size()-1
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000418 << ":" << TNum << "/" << Num-1 << "] for "
419 << CalleeFunc->getName() << "["
420 << CalleeGraph->getGraphSize() << "+"
421 << CalleeGraph->getAuxFunctionCalls().size()
Chris Lattner033a7d52004-11-02 17:51:11 +0000422 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
423 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000424 << "]\n");
425 }
426 }
427
428#ifndef NDEBUG
429 // Now loop over the rest of the callees and make sure they have the
430 // same graph as the one inlined above.
431 if (CalleeGraph)
432 for (++I, ++TNum; I != E; ++I, ++TNum)
433 if (!I->second->isExternal())
434 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
435 "Callees at a call site have different graphs?");
436#endif
437 }
438
Chris Lattner31d3f672004-10-31 23:41:26 +0000439 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000440 assert(G.getInlinedGlobals().empty());
441 G.maskIncompleteMarkers();
442 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner31d3f672004-10-31 23:41:26 +0000443
444 // Delete dead nodes. Treat globals that are unreachable but that can
445 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000446 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000447
448 // When this graph is finalized, clone the globals in the graph into the
449 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000450 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000451
452 // Clone everything reachable from globals in the function graph into the
453 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000454 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000455 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
456 E = MainSM.global_end(); I != E; ++I)
457 RC.getClonedNH(MainSM[*I]);
458
Chris Lattner31d3f672004-10-31 23:41:26 +0000459 DEBUG(std::cerr << " -- DONE ProcessGraph for function "
Chris Lattner033a7d52004-11-02 17:51:11 +0000460 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000461}