blob: e895ef3c13559b99402ba7fa113ee87a1cafad7a [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"
Chris Lattner2af8c512005-03-15 17:14:09 +000019#include "llvm/DerivedTypes.h"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000020#include "llvm/Module.h"
21#include "llvm/Pass.h"
Chris Lattnereaef5682004-07-07 06:22:54 +000022#include "llvm/Analysis/DataStructure/DSGraph.h"
23#include "llvm/Analysis/DataStructure/DataStructure.h"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000024#include "llvm/Support/CallSite.h"
Chris Lattnerc9b93802004-10-11 20:53:28 +000025#include "llvm/Support/Debug.h"
26#include "llvm/ADT/SCCIterator.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/EquivalenceClasses.h"
29#include "llvm/ADT/STLExtras.h"
Vikram S. Advec5204fb2004-05-23 07:54:02 +000030using namespace llvm;
31
Vikram S. Advec5204fb2004-05-23 07:54:02 +000032namespace {
Chris Lattneradfd5f12005-03-13 19:51:24 +000033 RegisterAnalysis<EquivClassGraphs> X("eqdatastructure",
Vikram S. Advec5204fb2004-05-23 07:54:02 +000034 "Equivalence-class Bottom-up Data Structure Analysis");
Chris Lattnerab8544a2004-10-31 21:56:11 +000035 Statistic<> NumEquivBUInlines("equivdatastructures",
36 "Number of graphs inlined");
Chris Lattner15d879e2004-10-12 16:52:09 +000037 Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up",
38 "Number of graphs inlined");
Vikram S. Advec5204fb2004-05-23 07:54:02 +000039}
40
Chris Lattner31d3f672004-10-31 23:41:26 +000041#ifndef NDEBUG
42template<typename GT>
43static void CheckAllGraphs(Module *M, GT &ECGraphs) {
44 DSGraph &GG = ECGraphs.getGlobalsGraph();
45
46 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
47 if (!I->isExternal()) {
48 DSGraph &G = ECGraphs.getDSGraph(*I);
Chris Lattnera5f47ea2005-03-15 16:55:04 +000049 if (G.retnodes_begin()->first != I)
Chris Lattnerb2b17bb2005-03-14 19:22:47 +000050 continue; // Only check a graph once.
Chris Lattner31d3f672004-10-31 23:41:26 +000051
52 DSGraph::NodeMapTy GlobalsGraphNodeMapping;
Chris Lattnerb0f92e32005-03-15 00:58:16 +000053 G.computeGToGGMapping(GlobalsGraphNodeMapping);
Chris Lattner31d3f672004-10-31 23:41:26 +000054 }
55}
56#endif
57
Chris Lattner4457f7e2004-11-01 21:07:05 +000058// getSomeCalleeForCallSite - Return any one callee function at a call site.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000059//
Chris Lattneradfd5f12005-03-13 19:51:24 +000060Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
Vikram S. Advec5204fb2004-05-23 07:54:02 +000061 Function *thisFunc = CS.getCaller();
Chris Lattner4457f7e2004-11-01 21:07:05 +000062 assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000063 DSGraph &DSG = getDSGraph(*thisFunc);
64 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000065 std::map<DSNode*, Function *>::const_iterator I =
66 OneCalledFunction.find(calleeNode);
67 return (I == OneCalledFunction.end())? NULL : I->second;
68}
69
Chris Lattnerab8544a2004-10-31 21:56:11 +000070// runOnModule - Calculate the bottom up data structure graphs for each function
71// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000072//
Chris Lattnerb25959a2005-03-12 12:08:52 +000073bool EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000074 CBU = &getAnalysis<CompleteBUDataStructures>();
Chris Lattner04252fe2004-11-11 22:11:17 +000075 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000076
Chris Lattnerab8544a2004-10-31 21:56:11 +000077 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph());
78 GlobalsGraph->setPrintAuxCalls();
79
Chris Lattner31d3f672004-10-31 23:41:26 +000080 ActualCallees = CBU->getActualCallees();
81
Vikram S. Advec5204fb2004-05-23 07:54:02 +000082 // Find equivalence classes of functions called from common call sites.
83 // Fold the CBU graphs for all functions in an equivalence class.
84 buildIndirectFunctionSets(M);
85
86 // Stack of functions used for Tarjan's SCC-finding algorithm.
Chris Lattner033a7d52004-11-02 17:51:11 +000087 std::vector<DSGraph*> Stack;
88 std::map<DSGraph*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000089 unsigned NextID = 1;
90
Chris Lattnera66e3532005-03-13 20:15:06 +000091 Function *MainFunc = M.getMainFunction();
92 if (MainFunc && !MainFunc->isExternal()) {
93 processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000094 } else {
95 std::cerr << "Fold Graphs: No 'main' function found!\n";
96 }
97
98 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner033a7d52004-11-02 17:51:11 +000099 if (!I->isExternal())
100 processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000101
Chris Lattner31d3f672004-10-31 23:41:26 +0000102 DEBUG(CheckAllGraphs(&M, *this));
103
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000104 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattnera66e3532005-03-13 20:15:06 +0000105
106 // Merge the globals variables (not the calls) from the globals graph back
107 // into the main function's graph so that the main function contains all of
108 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +0000109 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +0000110 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
111 const DSGraph &GG = *MainGraph.getGlobalsGraph();
112 ReachabilityCloner RC(MainGraph, GG,
113 DSGraph::DontCloneCallNodes |
114 DSGraph::DontCloneAuxCallNodes);
115
116 // Clone the global nodes into this graph.
117 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
118 E = GG.getScalarMap().global_end(); I != E; ++I)
119 if (isa<GlobalVariable>(*I))
120 RC.getClonedNH(GG.getNodeForValue(*I));
121
Chris Lattner270cf502005-03-13 20:32:26 +0000122 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000123 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
124 DSGraph::IgnoreGlobals);
125 }
126
Chris Lattner2af8c512005-03-15 17:14:09 +0000127 // Final processing. Note that dead node elimination may actually remove
128 // globals from a function graph that are immediately used. If there are no
129 // scalars pointing to the node (e.g. because the only use is a direct store
130 // to a scalar global) we have to make sure to rematerialize the globals back
131 // into the graphs here, or clients will break!
132 for (Module::global_iterator GI = M.global_begin(), E = M.global_end();
133 GI != E; ++GI)
134 // This only happens to first class typed globals.
135 if (GI->getType()->getElementType()->isFirstClassType())
136 for (Value::use_iterator UI = GI->use_begin(), E = GI->use_end();
137 UI != E; ++UI)
138 // This only happens to direct uses by instructions.
139 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
140 DSGraph &DSG = getOrCreateGraph(*User->getParent()->getParent());
141 if (!DSG.getScalarMap().count(GI)) {
142 // If this global does not exist in the graph, but it is immediately
143 // used by an instruction in the graph, clone it over from the
144 // globals graph.
145 ReachabilityCloner RC(DSG, *GlobalsGraph, 0);
146 RC.getClonedNH(GlobalsGraph->getNodeForValue(GI));
147 }
148 }
149
Chris Lattnerab8544a2004-10-31 21:56:11 +0000150 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000151}
152
153
154// buildIndirectFunctionSets - Iterate over the module looking for indirect
155// calls to functions. If a call site can invoke any functions [F1, F2... FN],
156// unify the N functions together in the FuncECs set.
157//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000158void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000159 const ActualCalleesTy& AC = CBU->getActualCallees();
Chris Lattner77408b82004-11-01 20:37:00 +0000160
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000161 // Loop over all of the indirect calls in the program. If a call site can
162 // call multiple different functions, we need to unify all of the callees into
163 // the same equivalence class.
164 Instruction *LastInst = 0;
165 Function *FirstFunc = 0;
166 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
167 if (I->second->isExternal())
168 continue; // Ignore functions we cannot modify
169
170 CallSite CS = CallSite::get(I->first);
171
172 if (CS.getCalledFunction()) { // Direct call:
173 FuncECs.addElement(I->second); // -- Make sure function has equiv class
174 FirstFunc = I->second; // -- First callee at this site
175 } else { // Else indirect call
176 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
177 // << " from : " << I->first);
178 if (I->first != LastInst) {
179 // This is the first callee from this call site.
180 LastInst = I->first;
181 FirstFunc = I->second;
182 // Instead of storing the lastInst For Indirection call Sites we store
183 // the DSNode for the function ptr arguemnt
184 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000185 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
186 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000187 OneCalledFunction[calleeNode] = FirstFunc;
188 FuncECs.addElement(I->second);
189 } else {
190 // This is not the first possible callee from a particular call site.
191 // Union the callee in with the other functions.
192 FuncECs.unionSetsWith(FirstFunc, I->second);
193#ifndef NDEBUG
194 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000195 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
196 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000197 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
198#endif
199 }
200 }
201
202 // Now include all functions that share a graph with any function in the
203 // equivalence class. More precisely, if F is in the class, and G(F) is
204 // its graph, then we include all other functions that are also in G(F).
205 // Currently, that is just the functions in the same call-graph-SCC as F.
206 //
207 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000208 for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(),
209 RE = funcDSGraph.retnodes_end(); RI != RE; ++RI)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000210 FuncECs.unionSetsWith(FirstFunc, RI->first);
211 }
212
213 // Now that all of the equivalences have been built, merge the graphs for
214 // each equivalence class.
215 //
216 std::set<Function*> &leaderSet = FuncECs.getLeaderSet();
217 DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
218 for (std::set<Function*>::iterator LI = leaderSet.begin(),
219 LE = leaderSet.end(); LI != LE; ++LI) {
220
221 Function* LF = *LI;
222 const std::set<Function*>& EqClass = FuncECs.getEqClass(LF);
223
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000224 if (EqClass.size() > 1) {
Chris Lattner983baf42004-11-02 06:38:58 +0000225#ifndef NDEBUG
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000226 DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = ");
227 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
228 EqEnd = EqClass.end(); EqI != EqEnd; ++EqI)
229 DEBUG(std::cerr << " " << (*EqI)->getName() << ",");
230 DEBUG(std::cerr << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000231#endif
232
Chris Lattnerab8544a2004-10-31 21:56:11 +0000233 // This equiv class has multiple functions: merge their graphs. First,
234 // clone the CBU graph for the leader and make it the common graph for the
235 // equivalence graph.
Chris Lattner983baf42004-11-02 06:38:58 +0000236 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000237
Chris Lattner77408b82004-11-01 20:37:00 +0000238 // Record the argument nodes for use in merging later below.
239 std::vector<DSNodeHandle> ArgNodes;
240
Chris Lattnere4d5c442005-03-15 04:54:21 +0000241 for (Function::arg_iterator AI1 = LF->arg_begin(); AI1 != LF->arg_end(); ++AI1)
Chris Lattnerf4985682004-10-31 18:13:19 +0000242 if (DS::isPointerType(AI1->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000243 ArgNodes.push_back(MergedG.getNodeForValue(AI1));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000244
Chris Lattnerab8544a2004-10-31 21:56:11 +0000245 // Merge in the graphs of all other functions in this equiv. class. Note
246 // that two or more functions may have the same graph, and it only needs
Chris Lattner983baf42004-11-02 06:38:58 +0000247 // to be merged in once.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000248 std::set<DSGraph*> GraphsMerged;
Chris Lattner983baf42004-11-02 06:38:58 +0000249 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Chris Lattner113cde82004-10-31 17:47:48 +0000250
Chris Lattner983baf42004-11-02 06:38:58 +0000251 for (std::set<Function*>::const_iterator EqI = EqClass.begin(),
252 E = EqClass.end(); EqI != E; ++EqI) {
253 Function *F = *EqI;
254 DSGraph *&FG = DSInfo[F];
255
256 DSGraph &CBUGraph = CBU->getDSGraph(*F);
257 if (!GraphsMerged.insert(&CBUGraph).second)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000258 continue;
259
260 // Record the "folded" graph for the function.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000261 for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(),
262 E = CBUGraph.retnodes_end(); I != E; ++I) {
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000263 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
264 DSInfo[I->first] = &MergedG;
265 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000266
Chris Lattner983baf42004-11-02 06:38:58 +0000267 // Clone this member of the equivalence class into MergedG.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000268 DSGraph::NodeMapTy NodeMap;
Chris Lattner113cde82004-10-31 17:47:48 +0000269
Chris Lattner983baf42004-11-02 06:38:58 +0000270 MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000271 MergedG.getReturnNodes(), NodeMap, 0);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000272
273 // Merge the return nodes of all functions together.
Chris Lattner983baf42004-11-02 06:38:58 +0000274 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000275
276 // Merge the function arguments with all argument nodes found so far.
277 // If there are extra function args, add them to the vector of argNodes
Chris Lattnere4d5c442005-03-15 04:54:21 +0000278 Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
Chris Lattner77408b82004-11-01 20:37:00 +0000279 for (unsigned arg=0, numArgs = ArgNodes.size();
Chris Lattner113cde82004-10-31 17:47:48 +0000280 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
281 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000282 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Chris Lattner113cde82004-10-31 17:47:48 +0000283
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000284 for ( ; AI2 != AI2end; ++AI2)
Chris Lattner113cde82004-10-31 17:47:48 +0000285 if (DS::isPointerType(AI2->getType()))
Chris Lattner983baf42004-11-02 06:38:58 +0000286 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
287 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000288 }
289 }
290 }
291 DEBUG(std::cerr << "\n");
292}
293
294
Chris Lattnerb25959a2005-03-12 12:08:52 +0000295DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000296 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000297 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000298 if (Graph) return *Graph;
299
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000300 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000301
302 // Copy the CBU graph...
303 Graph = new DSGraph(CBUGraph); // updates the map via reference
304 Graph->setGlobalsGraph(&getGlobalsGraph());
305 Graph->setPrintAuxCalls();
306
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000307 // Make sure to update the DSInfo map for all functions in the graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000308 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
309 I != Graph->retnodes_end(); ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000310 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000311 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000312 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000313 FG = Graph;
314 }
315
Chris Lattner68f96582004-11-01 19:54:06 +0000316 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000317}
318
319
Chris Lattnerb25959a2005-03-12 12:08:52 +0000320unsigned EquivClassGraphs::
Chris Lattner033a7d52004-11-02 17:51:11 +0000321processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
322 std::map<DSGraph*, unsigned> &ValMap) {
323 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
324 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000325 return It->second;
326
Chris Lattner033a7d52004-11-02 17:51:11 +0000327 DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
328 << "\n");
329
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000330 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000331 ValMap[&FG] = Min;
332 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000333
334 // The edges out of the current node are the call site targets...
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000335 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
336 CI != CE; ++CI) {
Chris Lattner6538f422005-01-30 23:51:25 +0000337 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000338
339 // Loop over all of the actually called functions...
340 ActualCalleesTy::const_iterator I, E;
341 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
342 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000343 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000344 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000345 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000346 if (M < Min) Min = M;
347 }
348 }
349
Chris Lattner033a7d52004-11-02 17:51:11 +0000350 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000351 if (Min != MyID)
352 return Min; // This is part of a larger SCC!
353
354 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000355 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000356 while (Stack.back() != &FG) {
357 DSGraph *NG = Stack.back();
358 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000359
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000360 // If the SCC found is not the same as those found in CBU, make sure to
361 // merge the graphs as appropriate.
362 DSGraph::NodeMapTy NodeMap;
363 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
364
365 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000366 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
367 I != NG->retnodes_end(); ++I)
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000368 DSInfo[I->first] = &FG;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000369
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000370 // Remove NG from the ValMap since the pointer may get recycled.
371 ValMap.erase(NG);
372 delete NG;
373 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000374 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000375 }
376
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000377 // Clean up the graph before we start inlining a bunch again.
378 if (MergedGraphs)
379 FG.removeTriviallyDeadNodes();
380
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000381 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000382
Chris Lattner033a7d52004-11-02 17:51:11 +0000383 processGraph(FG);
384 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000385 return MyID;
386}
387
388
389/// processGraph - Process the CBU graphs for the program in bottom-up order on
390/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000391void EquivClassGraphs::processGraph(DSGraph &G) {
Chris Lattner033a7d52004-11-02 17:51:11 +0000392 DEBUG(std::cerr << " ProcessGraph for function "
393 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000394
395 hash_set<Instruction*> calls;
396
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000397 // Else we need to inline some callee graph. Visit all call sites.
398 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000399 unsigned i = 0;
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000400 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattner6538f422005-01-30 23:51:25 +0000401 ++CI, ++i) {
402 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000403 Instruction *TheCall = CS.getCallSite().getInstruction();
404
405 assert(calls.insert(TheCall).second &&
406 "Call instruction occurs multiple times in graph??");
407
408 // Inline the common callee graph into the current graph, if the callee
409 // graph has not changed. Note that all callees should have the same
410 // graph so we only need to do this once.
411 //
412 DSGraph* CalleeGraph = NULL;
413 ActualCalleesTy::const_iterator I, E;
414 tie(I, E) = getActualCallees().equal_range(TheCall);
415 unsigned TNum, Num;
416
417 // Loop over all potential callees to find the first non-external callee.
418 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
419 if (!I->second->isExternal())
420 break;
421
422 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000423 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000424 Function *CalleeFunc = I->second;
425
426 // Merge the callee's graph into this graph, if not already the same.
427 // Callees in the same equivalence class (which subsumes those
428 // in the same SCCs) have the same graph. Note that all recursion
429 // including self-recursion have been folded in the equiv classes.
430 //
431 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000432 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000433 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000434 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
435 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
436 DSGraph::DontCloneCallNodes |
437 DSGraph::DontCloneAuxCallNodes);
Chris Lattner6538f422005-01-30 23:51:25 +0000438 DEBUG(std::cerr << " Inlining graph [" << i << "/"
439 << G.getFunctionCalls().size()-1
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000440 << ":" << TNum << "/" << Num-1 << "] for "
441 << CalleeFunc->getName() << "["
442 << CalleeGraph->getGraphSize() << "+"
443 << CalleeGraph->getAuxFunctionCalls().size()
Chris Lattner033a7d52004-11-02 17:51:11 +0000444 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
445 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000446 << "]\n");
447 }
448 }
449
450#ifndef NDEBUG
451 // Now loop over the rest of the callees and make sure they have the
452 // same graph as the one inlined above.
453 if (CalleeGraph)
454 for (++I, ++TNum; I != E; ++I, ++TNum)
455 if (!I->second->isExternal())
456 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
457 "Callees at a call site have different graphs?");
458#endif
459 }
460
Chris Lattner31d3f672004-10-31 23:41:26 +0000461 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000462 assert(G.getInlinedGlobals().empty());
463 G.maskIncompleteMarkers();
464 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner31d3f672004-10-31 23:41:26 +0000465
466 // Delete dead nodes. Treat globals that are unreachable but that can
467 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000468 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000469
470 // When this graph is finalized, clone the globals in the graph into the
471 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000472 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000473
474 // Clone everything reachable from globals in the function graph into the
475 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000476 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000477 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
478 E = MainSM.global_end(); I != E; ++I)
479 RC.getClonedNH(MainSM[*I]);
480
Chris Lattner31d3f672004-10-31 23:41:26 +0000481 DEBUG(std::cerr << " -- DONE ProcessGraph for function "
Chris Lattner033a7d52004-11-02 17:51:11 +0000482 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000483}