blob: 8e69291cdbc95566f1b1fcfea4baab53218f562d [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
Chris Lattner270cf502005-03-13 20:32:26 +0000123 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000124 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
125 DSGraph::IgnoreGlobals);
126 }
127
Chris Lattnerab8544a2004-10-31 21:56:11 +0000128 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000129}
130
131
132// buildIndirectFunctionSets - Iterate over the module looking for indirect
133// calls to functions. If a call site can invoke any functions [F1, F2... FN],
134// unify the N functions together in the FuncECs set.
135//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000136void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000137 const ActualCalleesTy& AC = CBU->getActualCallees();
Chris Lattner77408b82004-11-01 20:37:00 +0000138
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000139 // Loop over all of the indirect calls in the program. If a call site can
140 // call multiple different functions, we need to unify all of the callees into
141 // the same equivalence class.
142 Instruction *LastInst = 0;
143 Function *FirstFunc = 0;
144 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
145 if (I->second->isExternal())
146 continue; // Ignore functions we cannot modify
147
148 CallSite CS = CallSite::get(I->first);
149
150 if (CS.getCalledFunction()) { // Direct call:
151 FuncECs.addElement(I->second); // -- Make sure function has equiv class
152 FirstFunc = I->second; // -- First callee at this site
153 } else { // Else indirect call
154 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
155 // << " from : " << I->first);
156 if (I->first != LastInst) {
157 // This is the first callee from this call site.
158 LastInst = I->first;
159 FirstFunc = I->second;
160 // Instead of storing the lastInst For Indirection call Sites we store
161 // the DSNode for the function ptr arguemnt
162 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000163 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
164 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000165 OneCalledFunction[calleeNode] = FirstFunc;
166 FuncECs.addElement(I->second);
167 } else {
168 // This is not the first possible callee from a particular call site.
169 // Union the callee in with the other functions.
170 FuncECs.unionSetsWith(FirstFunc, I->second);
171#ifndef NDEBUG
172 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000173 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
174 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000175 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
176#endif
177 }
178 }
179
180 // Now include all functions that share a graph with any function in the
181 // equivalence class. More precisely, if F is in the class, and G(F) is
182 // its graph, then we include all other functions that are also in G(F).
183 // Currently, that is just the functions in the same call-graph-SCC as F.
184 //
185 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
186 const DSGraph::ReturnNodesTy &RetNodes = funcDSGraph.getReturnNodes();
187 for (DSGraph::ReturnNodesTy::const_iterator RI=RetNodes.begin(),
188 RE=RetNodes.end(); RI != RE; ++RI)
189 FuncECs.unionSetsWith(FirstFunc, RI->first);
190 }
191
192 // Now that all of the equivalences have been built, merge the graphs for
193 // each equivalence class.
194 //
195 std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
196 DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
197 for (std::set<Function*>::iterator LI = leaderSet.begin(),
198 LE = leaderSet.end(); LI != LE; ++LI) {
199
200 Function* LF = *LI;
201 const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
202
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000203 if (EqClass.size() > 1) {
Chris Lattner983baf42004-11-02 06:38:58 +0000204#ifndef NDEBUG
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000205 DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = ");
206 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
207 EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
208 DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
209 DEBUG(std::cerr << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000210#endif
211
Chris Lattnerab8544a2004-10-31 21:56:11 +0000212 // This equiv class has multiple functions: merge their graphs. First,
213 // clone the CBU graph for the leader and make it the common graph for the
214 // equivalence graph.
Chris Lattner983baf42004-11-02 06:38:58 +0000215 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000216
Chris Lattner77408b82004-11-01 20:37:00 +0000217 // Record the argument nodes for use in merging later below.
218 std::vector<DSNodeHandle> ArgNodes;
219
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000220 for (Function::aiterator AI1 = LF->abegin(); AI1 != LF->aend(); ++AI1)
Chris Lattnerf4985682004-10-31 18:13:19 +0000221 if (DS::isPointerType(AI1->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000222 ArgNodes.push_back(MergedG.getNodeForValue(AI1));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000223
Chris Lattnerab8544a2004-10-31 21:56:11 +0000224 // Merge in the graphs of all other functions in this equiv. class. Note
225 // that two or more functions may have the same graph, and it only needs
Chris Lattner983baf42004-11-02 06:38:58 +0000226 // to be merged in once.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000227 std::set<DSGraph*> GraphsMerged;
Chris Lattner983baf42004-11-02 06:38:58 +0000228 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Chris Lattner113cde82004-10-31 17:47:48 +0000229
Chris Lattner983baf42004-11-02 06:38:58 +0000230 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
231 E = EqClass.end(); EqI != E; ++EqI) {
232 Function *F = *EqI;
233 DSGraph *&FG = DSInfo[F];
234
235 DSGraph &CBUGraph = CBU->getDSGraph(*F);
236 if (!GraphsMerged.insert(&CBUGraph).second)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000237 continue;
238
239 // Record the "folded" graph for the function.
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000240 for (DSGraph::ReturnNodesTy::iterator
241 I = CBUGraph.getReturnNodes().begin(),
242 E = CBUGraph.getReturnNodes().end();
243 I != E; ++I) {
244 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
245 DSInfo[I->first] = &MergedG;
246 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000247
Chris Lattner983baf42004-11-02 06:38:58 +0000248 // Clone this member of the equivalence class into MergedG.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000249 DSGraph::NodeMapTy NodeMap;
Chris Lattner113cde82004-10-31 17:47:48 +0000250
Chris Lattner983baf42004-11-02 06:38:58 +0000251 MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000252 MergedG.getReturnNodes(), NodeMap, 0);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000253
254 // Merge the return nodes of all functions together.
Chris Lattner983baf42004-11-02 06:38:58 +0000255 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000256
257 // Merge the function arguments with all argument nodes found so far.
258 // If there are extra function args, add them to the vector of argNodes
259 Function::aiterator AI2 = F->abegin(), AI2end = F->aend();
Chris Lattner77408b82004-11-01 20:37:00 +0000260 for (unsigned arg=0, numArgs = ArgNodes.size();
Chris Lattner113cde82004-10-31 17:47:48 +0000261 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
262 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000263 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Chris Lattner113cde82004-10-31 17:47:48 +0000264
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000265 for ( ; AI2 != AI2end; ++AI2)
Chris Lattner113cde82004-10-31 17:47:48 +0000266 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000267 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
268 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000269 }
270 }
271 }
272 DEBUG(std::cerr << "\n");
273}
274
275
Chris Lattnerb25959a2005-03-12 12:08:52 +0000276DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000277 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000278 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000279 if (Graph) return *Graph;
280
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000281 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000282
283 // Copy the CBU graph...
284 Graph = new DSGraph(CBUGraph); // updates the map via reference
285 Graph->setGlobalsGraph(&getGlobalsGraph());
286 Graph->setPrintAuxCalls();
287
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000288 // Make sure to update the DSInfo map for all functions in the graph!
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000289 for (DSGraph::ReturnNodesTy::iterator I = Graph->getReturnNodes().begin();
290 I != Graph->getReturnNodes().end(); ++I)
291 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000292 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000293 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000294 FG = Graph;
295 }
296
Chris Lattner68f96582004-11-01 19:54:06 +0000297 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000298}
299
300
Chris Lattnerb25959a2005-03-12 12:08:52 +0000301unsigned EquivClassGraphs::
Chris Lattner033a7d52004-11-02 17:51:11 +0000302processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
303 std::map<DSGraph*, unsigned> &ValMap) {
304 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
305 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000306 return It->second;
307
Chris Lattner033a7d52004-11-02 17:51:11 +0000308 DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
309 << "\n");
310
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000311 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000312 ValMap[&FG] = Min;
313 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000314
315 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000316 for (DSGraph::fc_iterator CI = FG.fc_begin(), E = FG.fc_end(); CI != E; ++CI){
317 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000318
319 // Loop over all of the actually called functions...
320 ActualCalleesTy::const_iterator I, E;
321 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
322 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000323 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000324 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000325 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000326 if (M < Min) Min = M;
327 }
328 }
329
Chris Lattner033a7d52004-11-02 17:51:11 +0000330 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000331 if (Min != MyID)
332 return Min; // This is part of a larger SCC!
333
334 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000335 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000336 while (Stack.back() != &FG) {
337 DSGraph *NG = Stack.back();
338 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000339
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000340 // If the SCC found is not the same as those found in CBU, make sure to
341 // merge the graphs as appropriate.
342 DSGraph::NodeMapTy NodeMap;
343 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
344
345 // Update the DSInfo map and delete the old graph...
346 for (DSGraph::ReturnNodesTy::iterator I = NG->getReturnNodes().begin();
347 I != NG->getReturnNodes().end(); ++I)
348 DSInfo[I->first] = &FG;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000349
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000350 // Remove NG from the ValMap since the pointer may get recycled.
351 ValMap.erase(NG);
352 delete NG;
353 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000354 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000355 }
356
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000357 // Clean up the graph before we start inlining a bunch again.
358 if (MergedGraphs)
359 FG.removeTriviallyDeadNodes();
360
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000361 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000362
Chris Lattner033a7d52004-11-02 17:51:11 +0000363 processGraph(FG);
364 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000365 return MyID;
366}
367
368
369/// processGraph - Process the CBU graphs for the program in bottom-up order on
370/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000371void EquivClassGraphs::processGraph(DSGraph &G) {
Chris Lattner033a7d52004-11-02 17:51:11 +0000372 DEBUG(std::cerr << " ProcessGraph for function "
373 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000374
375 hash_set<Instruction*> calls;
376
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000377 // Else we need to inline some callee graph. Visit all call sites.
378 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000379 unsigned i = 0;
380 for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E;
381 ++CI, ++i) {
382 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000383 Instruction *TheCall = CS.getCallSite().getInstruction();
384
385 assert(calls.insert(TheCall).second &&
386 "Call instruction occurs multiple times in graph??");
387
388 // Inline the common callee graph into the current graph, if the callee
389 // graph has not changed. Note that all callees should have the same
390 // graph so we only need to do this once.
391 //
392 DSGraph* CalleeGraph = NULL;
393 ActualCalleesTy::const_iterator I, E;
394 tie(I, E) = getActualCallees().equal_range(TheCall);
395 unsigned TNum, Num;
396
397 // Loop over all potential callees to find the first non-external callee.
398 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
399 if (!I->second->isExternal())
400 break;
401
402 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000403 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000404 Function *CalleeFunc = I->second;
405
406 // Merge the callee's graph into this graph, if not already the same.
407 // Callees in the same equivalence class (which subsumes those
408 // in the same SCCs) have the same graph. Note that all recursion
409 // including self-recursion have been folded in the equiv classes.
410 //
411 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000412 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000413 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000414 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
415 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
416 DSGraph::DontCloneCallNodes |
417 DSGraph::DontCloneAuxCallNodes);
Chris Lattner6538f422005-01-30 23:51:25 +0000418 DEBUG(std::cerr << " Inlining graph [" << i << "/"
419 << G.getFunctionCalls().size()-1
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000420 << ":" << TNum << "/" << Num-1 << "] for "
421 << CalleeFunc->getName() << "["
422 << CalleeGraph->getGraphSize() << "+"
423 << CalleeGraph->getAuxFunctionCalls().size()
Chris Lattner033a7d52004-11-02 17:51:11 +0000424 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
425 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000426 << "]\n");
427 }
428 }
429
430#ifndef NDEBUG
431 // Now loop over the rest of the callees and make sure they have the
432 // same graph as the one inlined above.
433 if (CalleeGraph)
434 for (++I, ++TNum; I != E; ++I, ++TNum)
435 if (!I->second->isExternal())
436 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
437 "Callees at a call site have different graphs?");
438#endif
439 }
440
Chris Lattner31d3f672004-10-31 23:41:26 +0000441 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000442 assert(G.getInlinedGlobals().empty());
443 G.maskIncompleteMarkers();
444 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner31d3f672004-10-31 23:41:26 +0000445
446 // Delete dead nodes. Treat globals that are unreachable but that can
447 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000448 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000449
450 // When this graph is finalized, clone the globals in the graph into the
451 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000452 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000453
454 // Clone everything reachable from globals in the function graph into the
455 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000456 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000457 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
458 E = MainSM.global_end(); I != E; ++I)
459 RC.getClonedNH(MainSM[*I]);
460
Chris Lattner31d3f672004-10-31 23:41:26 +0000461 DEBUG(std::cerr << " -- DONE ProcessGraph for function "
Chris Lattner033a7d52004-11-02 17:51:11 +0000462 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000463}