blob: 447b36d7808117d15912582891263c4be0e7bdfc [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 Lattnerf4f62272005-03-19 22:23:45 +000075 GlobalECs = CBU->getGlobalECs();
Chris Lattner04252fe2004-11-11 22:11:17 +000076 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000077
Chris Lattnerf4f62272005-03-19 22:23:45 +000078 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs);
Chris Lattnerab8544a2004-10-31 21:56:11 +000079 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 Lattner38065a72005-03-15 22:47:18 +0000106 getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals);
Chris Lattnera66e3532005-03-13 20:15:06 +0000107
108 // Merge the globals variables (not the calls) from the globals graph back
109 // into the main function's graph so that the main function contains all of
110 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +0000111 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +0000112 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
113 const DSGraph &GG = *MainGraph.getGlobalsGraph();
114 ReachabilityCloner RC(MainGraph, GG,
115 DSGraph::DontCloneCallNodes |
116 DSGraph::DontCloneAuxCallNodes);
117
118 // Clone the global nodes into this graph.
119 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
120 E = GG.getScalarMap().global_end(); I != E; ++I)
121 if (isa<GlobalVariable>(*I))
122 RC.getClonedNH(GG.getNodeForValue(*I));
123
Chris Lattner270cf502005-03-13 20:32:26 +0000124 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000125 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
126 DSGraph::IgnoreGlobals);
127 }
128
Chris Lattner2af8c512005-03-15 17:14:09 +0000129 // Final processing. Note that dead node elimination may actually remove
130 // globals from a function graph that are immediately used. If there are no
131 // scalars pointing to the node (e.g. because the only use is a direct store
132 // to a scalar global) we have to make sure to rematerialize the globals back
133 // into the graphs here, or clients will break!
134 for (Module::global_iterator GI = M.global_begin(), E = M.global_end();
135 GI != E; ++GI)
136 // This only happens to first class typed globals.
137 if (GI->getType()->getElementType()->isFirstClassType())
138 for (Value::use_iterator UI = GI->use_begin(), E = GI->use_end();
139 UI != E; ++UI)
140 // This only happens to direct uses by instructions.
141 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
142 DSGraph &DSG = getOrCreateGraph(*User->getParent()->getParent());
143 if (!DSG.getScalarMap().count(GI)) {
144 // If this global does not exist in the graph, but it is immediately
145 // used by an instruction in the graph, clone it over from the
146 // globals graph.
147 ReachabilityCloner RC(DSG, *GlobalsGraph, 0);
148 RC.getClonedNH(GlobalsGraph->getNodeForValue(GI));
149 }
150 }
151
Chris Lattnerab8544a2004-10-31 21:56:11 +0000152 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000153}
154
155
156// buildIndirectFunctionSets - Iterate over the module looking for indirect
157// calls to functions. If a call site can invoke any functions [F1, F2... FN],
158// unify the N functions together in the FuncECs set.
159//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000160void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000161 const ActualCalleesTy& AC = CBU->getActualCallees();
Chris Lattner77408b82004-11-01 20:37:00 +0000162
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000163 // Loop over all of the indirect calls in the program. If a call site can
164 // call multiple different functions, we need to unify all of the callees into
165 // the same equivalence class.
166 Instruction *LastInst = 0;
167 Function *FirstFunc = 0;
168 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
169 if (I->second->isExternal())
170 continue; // Ignore functions we cannot modify
171
172 CallSite CS = CallSite::get(I->first);
173
174 if (CS.getCalledFunction()) { // Direct call:
Chris Lattner605a87c2005-03-19 05:15:27 +0000175 FuncECs.insert(I->second); // -- Make sure function has equiv class
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000176 FirstFunc = I->second; // -- First callee at this site
177 } else { // Else indirect call
178 // DEBUG(std::cerr << "CALLEE: " << I->second->getName()
179 // << " from : " << I->first);
180 if (I->first != LastInst) {
181 // This is the first callee from this call site.
182 LastInst = I->first;
183 FirstFunc = I->second;
184 // Instead of storing the lastInst For Indirection call Sites we store
185 // the DSNode for the function ptr arguemnt
186 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000187 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
188 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000189 OneCalledFunction[calleeNode] = FirstFunc;
Chris Lattner605a87c2005-03-19 05:15:27 +0000190 FuncECs.insert(I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000191 } else {
192 // This is not the first possible callee from a particular call site.
193 // Union the callee in with the other functions.
Chris Lattner605a87c2005-03-19 05:15:27 +0000194 FuncECs.unionSets(FirstFunc, I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000195#ifndef NDEBUG
196 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000197 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
198 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000199 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
200#endif
201 }
202 }
203
204 // Now include all functions that share a graph with any function in the
205 // equivalence class. More precisely, if F is in the class, and G(F) is
206 // its graph, then we include all other functions that are also in G(F).
207 // Currently, that is just the functions in the same call-graph-SCC as F.
208 //
209 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000210 for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(),
211 RE = funcDSGraph.retnodes_end(); RI != RE; ++RI)
Chris Lattner605a87c2005-03-19 05:15:27 +0000212 FuncECs.unionSets(FirstFunc, RI->first);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000213 }
214
215 // Now that all of the equivalences have been built, merge the graphs for
216 // each equivalence class.
217 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000218 DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n");
Chris Lattner605a87c2005-03-19 05:15:27 +0000219 for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E =
220 FuncECs.end(); EQSI != E; ++EQSI) {
221 if (!EQSI->isLeader()) continue;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000222
Chris Lattner605a87c2005-03-19 05:15:27 +0000223 EquivalenceClasses<Function*>::member_iterator SI =
224 FuncECs.member_begin(EQSI);
225 assert(SI != FuncECs.member_end() && "Empty equiv set??");
226 EquivalenceClasses<Function*>::member_iterator SN = SI;
227 ++SN;
228 if (SN == FuncECs.member_end())
229 continue; // Single function equivalence set, no merging to do.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000230
Chris Lattner605a87c2005-03-19 05:15:27 +0000231 Function* LF = *SI;
232
Chris Lattner983baf42004-11-02 06:38:58 +0000233#ifndef NDEBUG
Chris Lattner605a87c2005-03-19 05:15:27 +0000234 DEBUG(std::cerr <<" Equivalence set for leader " << LF->getName() <<" = ");
235 for (SN = SI; SN != FuncECs.member_end(); ++SN)
236 DEBUG(std::cerr << " " << (*SN)->getName() << "," );
237 DEBUG(std::cerr << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000238#endif
239
Chris Lattner605a87c2005-03-19 05:15:27 +0000240 // This equiv class has multiple functions: merge their graphs. First,
241 // clone the CBU graph for the leader and make it the common graph for the
242 // equivalence graph.
243 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000244
Chris Lattner605a87c2005-03-19 05:15:27 +0000245 // Record the argument nodes for use in merging later below.
246 std::vector<DSNodeHandle> ArgNodes;
Chris Lattner77408b82004-11-01 20:37:00 +0000247
Chris Lattner605a87c2005-03-19 05:15:27 +0000248 for (Function::arg_iterator AI = LF->arg_begin(), E = LF->arg_end();
249 AI != E; ++AI)
250 if (DS::isPointerType(AI->getType()))
251 ArgNodes.push_back(MergedG.getNodeForValue(AI));
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000252
Chris Lattner605a87c2005-03-19 05:15:27 +0000253 // Merge in the graphs of all other functions in this equiv. class. Note
254 // that two or more functions may have the same graph, and it only needs
255 // to be merged in once.
256 std::set<DSGraph*> GraphsMerged;
257 GraphsMerged.insert(&CBU->getDSGraph(*LF));
258
259 for (++SI; SI != FuncECs.member_end(); ++SI) {
260 Function *F = *SI;
261 DSGraph *&FG = DSInfo[F];
262
263 DSGraph &CBUGraph = CBU->getDSGraph(*F);
264 if (GraphsMerged.insert(&CBUGraph).second) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000265 // Record the "folded" graph for the function.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000266 for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(),
267 E = CBUGraph.retnodes_end(); I != E; ++I) {
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000268 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
269 DSInfo[I->first] = &MergedG;
270 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000271
Chris Lattner983baf42004-11-02 06:38:58 +0000272 // Clone this member of the equivalence class into MergedG.
Chris Lattner605a87c2005-03-19 05:15:27 +0000273 {
274 DSGraph::NodeMapTy NodeMap;
275 MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(),
276 MergedG.getReturnNodes(), NodeMap, 0);
277 }
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000278 }
Chris Lattner605a87c2005-03-19 05:15:27 +0000279
280 // Merge the return nodes of all functions together.
281 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
282
283 // Merge the function arguments with all argument nodes found so far.
284 // If there are extra function args, add them to the vector of argNodes
285 Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
286 for (unsigned arg = 0, numArgs = ArgNodes.size();
287 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
288 if (DS::isPointerType(AI2->getType()))
289 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
290
291 for ( ; AI2 != AI2end; ++AI2)
292 if (DS::isPointerType(AI2->getType()))
293 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
294 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000295 }
296 }
297 DEBUG(std::cerr << "\n");
298}
299
300
Chris Lattnerb25959a2005-03-12 12:08:52 +0000301DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000302 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000303 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000304 if (Graph) return *Graph;
305
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000306 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000307
308 // Copy the CBU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000309 Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000310 Graph->setGlobalsGraph(&getGlobalsGraph());
311 Graph->setPrintAuxCalls();
312
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000313 // Make sure to update the DSInfo map for all functions in the graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000314 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
315 I != Graph->retnodes_end(); ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000316 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000317 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000318 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000319 FG = Graph;
320 }
321
Chris Lattner68f96582004-11-01 19:54:06 +0000322 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000323}
324
325
Chris Lattnerb25959a2005-03-12 12:08:52 +0000326unsigned EquivClassGraphs::
Chris Lattner033a7d52004-11-02 17:51:11 +0000327processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
328 std::map<DSGraph*, unsigned> &ValMap) {
329 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
330 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000331 return It->second;
332
Chris Lattner033a7d52004-11-02 17:51:11 +0000333 DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames()
334 << "\n");
335
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000336 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000337 ValMap[&FG] = Min;
338 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000339
340 // The edges out of the current node are the call site targets...
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000341 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
342 CI != CE; ++CI) {
Chris Lattner6538f422005-01-30 23:51:25 +0000343 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000344
345 // Loop over all of the actually called functions...
346 ActualCalleesTy::const_iterator I, E;
347 for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I)
348 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000349 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000350 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000351 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000352 if (M < Min) Min = M;
353 }
354 }
355
Chris Lattner033a7d52004-11-02 17:51:11 +0000356 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000357 if (Min != MyID)
358 return Min; // This is part of a larger SCC!
359
360 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000361 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000362 while (Stack.back() != &FG) {
363 DSGraph *NG = Stack.back();
364 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000365
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000366 // If the SCC found is not the same as those found in CBU, make sure to
367 // merge the graphs as appropriate.
368 DSGraph::NodeMapTy NodeMap;
369 FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap);
370
371 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000372 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
373 I != NG->retnodes_end(); ++I)
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000374 DSInfo[I->first] = &FG;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000375
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000376 // Remove NG from the ValMap since the pointer may get recycled.
377 ValMap.erase(NG);
378 delete NG;
379 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000380 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000381 }
382
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000383 // Clean up the graph before we start inlining a bunch again.
384 if (MergedGraphs)
385 FG.removeTriviallyDeadNodes();
386
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000387 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000388
Chris Lattner033a7d52004-11-02 17:51:11 +0000389 processGraph(FG);
390 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000391 return MyID;
392}
393
394
395/// processGraph - Process the CBU graphs for the program in bottom-up order on
396/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000397void EquivClassGraphs::processGraph(DSGraph &G) {
Chris Lattner033a7d52004-11-02 17:51:11 +0000398 DEBUG(std::cerr << " ProcessGraph for function "
399 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000400
401 hash_set<Instruction*> calls;
402
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000403 // Else we need to inline some callee graph. Visit all call sites.
404 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000405 unsigned i = 0;
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000406 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattner6538f422005-01-30 23:51:25 +0000407 ++CI, ++i) {
408 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000409 Instruction *TheCall = CS.getCallSite().getInstruction();
410
411 assert(calls.insert(TheCall).second &&
412 "Call instruction occurs multiple times in graph??");
413
Chris Lattner5021b8c2005-03-18 23:19:47 +0000414 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
415 continue;
416
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000417 // Inline the common callee graph into the current graph, if the callee
418 // graph has not changed. Note that all callees should have the same
419 // graph so we only need to do this once.
420 //
421 DSGraph* CalleeGraph = NULL;
422 ActualCalleesTy::const_iterator I, E;
423 tie(I, E) = getActualCallees().equal_range(TheCall);
424 unsigned TNum, Num;
425
426 // Loop over all potential callees to find the first non-external callee.
427 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
428 if (!I->second->isExternal())
429 break;
430
431 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000432 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000433 Function *CalleeFunc = I->second;
434
435 // Merge the callee's graph into this graph, if not already the same.
436 // Callees in the same equivalence class (which subsumes those
437 // in the same SCCs) have the same graph. Note that all recursion
438 // including self-recursion have been folded in the equiv classes.
439 //
440 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000441 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000442 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000443 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
444 DSGraph::KeepModRefBits | DSGraph::StripAllocaBit |
445 DSGraph::DontCloneCallNodes |
446 DSGraph::DontCloneAuxCallNodes);
Chris Lattner6538f422005-01-30 23:51:25 +0000447 DEBUG(std::cerr << " Inlining graph [" << i << "/"
448 << G.getFunctionCalls().size()-1
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000449 << ":" << TNum << "/" << Num-1 << "] for "
450 << CalleeFunc->getName() << "["
451 << CalleeGraph->getGraphSize() << "+"
452 << CalleeGraph->getAuxFunctionCalls().size()
Chris Lattner033a7d52004-11-02 17:51:11 +0000453 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
454 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000455 << "]\n");
456 }
457 }
458
459#ifndef NDEBUG
460 // Now loop over the rest of the callees and make sure they have the
461 // same graph as the one inlined above.
462 if (CalleeGraph)
463 for (++I, ++TNum; I != E; ++I, ++TNum)
464 if (!I->second->isExternal())
465 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
466 "Callees at a call site have different graphs?");
467#endif
468 }
469
Chris Lattner31d3f672004-10-31 23:41:26 +0000470 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000471 assert(G.getInlinedGlobals().empty());
472 G.maskIncompleteMarkers();
473 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Chris Lattner31d3f672004-10-31 23:41:26 +0000474
475 // Delete dead nodes. Treat globals that are unreachable but that can
476 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000477 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000478
479 // When this graph is finalized, clone the globals in the graph into the
480 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000481 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000482
483 // Clone everything reachable from globals in the function graph into the
484 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000485 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000486 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
487 E = MainSM.global_end(); I != E; ++I)
488 RC.getClonedNH(MainSM[*I]);
489
Chris Lattner31d3f672004-10-31 23:41:26 +0000490 DEBUG(std::cerr << " -- DONE ProcessGraph for function "
Chris Lattner033a7d52004-11-02 17:51:11 +0000491 << G.getFunctionNames() << "\n");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000492}