blob: 38aaf0b93c0a8e61663771cb13bb7559d13af315 [file] [log] [blame]
Vikram S. Advec5204fb2004-05-23 07:54:02 +00001//===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Vikram S. Advec5204fb2004-05-23 07:54:02 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Vikram S. Advec5204fb2004-05-23 07:54:02 +00008//===----------------------------------------------------------------------===//
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 Lattner07001232005-04-02 20:08:17 +000018#include "llvm/Analysis/DataStructure/DataStructure.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"
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 Lattner5d8925c2006-08-27 22:30:17 +000032 RegisterPass<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) {
Chris Lattner31d3f672004-10-31 23:41:26 +000043 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
44 if (!I->isExternal()) {
45 DSGraph &G = ECGraphs.getDSGraph(*I);
Chris Lattnera5f47ea2005-03-15 16:55:04 +000046 if (G.retnodes_begin()->first != I)
Chris Lattnerb2b17bb2005-03-14 19:22:47 +000047 continue; // Only check a graph once.
Chris Lattner31d3f672004-10-31 23:41:26 +000048
49 DSGraph::NodeMapTy GlobalsGraphNodeMapping;
Chris Lattnerb0f92e32005-03-15 00:58:16 +000050 G.computeGToGGMapping(GlobalsGraphNodeMapping);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000051 }
Chris Lattner31d3f672004-10-31 23:41:26 +000052}
53#endif
54
Chris Lattner4457f7e2004-11-01 21:07:05 +000055// getSomeCalleeForCallSite - Return any one callee function at a call site.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000056//
Chris Lattneradfd5f12005-03-13 19:51:24 +000057Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
Vikram S. Advec5204fb2004-05-23 07:54:02 +000058 Function *thisFunc = CS.getCaller();
Chris Lattner4457f7e2004-11-01 21:07:05 +000059 assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000060 DSGraph &DSG = getDSGraph(*thisFunc);
61 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000062 std::map<DSNode*, Function *>::const_iterator I =
63 OneCalledFunction.find(calleeNode);
64 return (I == OneCalledFunction.end())? NULL : I->second;
65}
66
Chris Lattnerab8544a2004-10-31 21:56:11 +000067// runOnModule - Calculate the bottom up data structure graphs for each function
68// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000069//
Chris Lattnerb25959a2005-03-12 12:08:52 +000070bool EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000071 CBU = &getAnalysis<CompleteBUDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000072 GlobalECs = CBU->getGlobalECs();
Chris Lattner04252fe2004-11-11 22:11:17 +000073 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000074
Chris Lattnerf4f62272005-03-19 22:23:45 +000075 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs);
Chris Lattnerab8544a2004-10-31 21:56:11 +000076 GlobalsGraph->setPrintAuxCalls();
77
Chris Lattner31d3f672004-10-31 23:41:26 +000078 ActualCallees = CBU->getActualCallees();
79
Vikram S. Advec5204fb2004-05-23 07:54:02 +000080 // Find equivalence classes of functions called from common call sites.
81 // Fold the CBU graphs for all functions in an equivalence class.
82 buildIndirectFunctionSets(M);
83
84 // Stack of functions used for Tarjan's SCC-finding algorithm.
Chris Lattner033a7d52004-11-02 17:51:11 +000085 std::vector<DSGraph*> Stack;
86 std::map<DSGraph*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000087 unsigned NextID = 1;
88
Chris Lattnera66e3532005-03-13 20:15:06 +000089 Function *MainFunc = M.getMainFunction();
90 if (MainFunc && !MainFunc->isExternal()) {
91 processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000092 } else {
Bill Wendlinga5b31ca2006-11-28 23:33:06 +000093 llvm_cerr << "Fold Graphs: No 'main' function found!\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +000094 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000095
Vikram S. Advec5204fb2004-05-23 07:54:02 +000096 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner033a7d52004-11-02 17:51:11 +000097 if (!I->isExternal())
98 processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000099
Chris Lattner31d3f672004-10-31 23:41:26 +0000100 DEBUG(CheckAllGraphs(&M, *this));
101
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000102 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattner38065a72005-03-15 22:47:18 +0000103 getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals);
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.
Chris Lattner49e88e82005-03-15 22:10:04 +0000108 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +0000109 DSGraph &MainGraph = getOrCreateGraph(*MainFunc);
110 const DSGraph &GG = *MainGraph.getGlobalsGraph();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000111 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +0000112 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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000122 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +0000123 DSGraph::IgnoreGlobals);
124 }
125
Chris Lattner2af8c512005-03-15 17:14:09 +0000126 // Final processing. Note that dead node elimination may actually remove
127 // globals from a function graph that are immediately used. If there are no
128 // scalars pointing to the node (e.g. because the only use is a direct store
129 // to a scalar global) we have to make sure to rematerialize the globals back
130 // into the graphs here, or clients will break!
131 for (Module::global_iterator GI = M.global_begin(), E = M.global_end();
132 GI != E; ++GI)
133 // This only happens to first class typed globals.
134 if (GI->getType()->getElementType()->isFirstClassType())
135 for (Value::use_iterator UI = GI->use_begin(), E = GI->use_end();
136 UI != E; ++UI)
137 // This only happens to direct uses by instructions.
138 if (Instruction *User = dyn_cast<Instruction>(*UI)) {
139 DSGraph &DSG = getOrCreateGraph(*User->getParent()->getParent());
140 if (!DSG.getScalarMap().count(GI)) {
141 // If this global does not exist in the graph, but it is immediately
142 // used by an instruction in the graph, clone it over from the
143 // globals graph.
144 ReachabilityCloner RC(DSG, *GlobalsGraph, 0);
145 RC.getClonedNH(GlobalsGraph->getNodeForValue(GI));
146 }
147 }
148
Chris Lattnerab8544a2004-10-31 21:56:11 +0000149 return false;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000150}
151
152
153// buildIndirectFunctionSets - Iterate over the module looking for indirect
154// calls to functions. If a call site can invoke any functions [F1, F2... FN],
155// unify the N functions together in the FuncECs set.
156//
Chris Lattnerb25959a2005-03-12 12:08:52 +0000157void EquivClassGraphs::buildIndirectFunctionSets(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000158 const ActualCalleesTy& AC = CBU->getActualCallees();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000159
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000160 // Loop over all of the indirect calls in the program. If a call site can
161 // call multiple different functions, we need to unify all of the callees into
162 // the same equivalence class.
163 Instruction *LastInst = 0;
164 Function *FirstFunc = 0;
165 for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) {
166 if (I->second->isExternal())
167 continue; // Ignore functions we cannot modify
168
169 CallSite CS = CallSite::get(I->first);
170
171 if (CS.getCalledFunction()) { // Direct call:
Chris Lattner605a87c2005-03-19 05:15:27 +0000172 FuncECs.insert(I->second); // -- Make sure function has equiv class
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000173 FirstFunc = I->second; // -- First callee at this site
174 } else { // Else indirect call
Bill Wendlinga5b31ca2006-11-28 23:33:06 +0000175 // DOUT << "CALLEE: " << I->second->getName()
176 // << " from : " << I->first;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000177 if (I->first != LastInst) {
178 // This is the first callee from this call site.
179 LastInst = I->first;
180 FirstFunc = I->second;
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000181 // Instead of storing the lastInst For Indirection call Sites we store
182 // the DSNode for the function ptr arguemnt
183 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000184 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000185 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000186 OneCalledFunction[calleeNode] = FirstFunc;
Chris Lattner605a87c2005-03-19 05:15:27 +0000187 FuncECs.insert(I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000188 } else {
189 // This is not the first possible callee from a particular call site.
190 // Union the callee in with the other functions.
Chris Lattner605a87c2005-03-19 05:15:27 +0000191 FuncECs.unionSets(FirstFunc, I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000192#ifndef NDEBUG
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000193 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000194 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000195 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000196 assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?");
197#endif
198 }
199 }
200
201 // Now include all functions that share a graph with any function in the
202 // equivalence class. More precisely, if F is in the class, and G(F) is
203 // its graph, then we include all other functions that are also in G(F).
204 // Currently, that is just the functions in the same call-graph-SCC as F.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000205 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000206 DSGraph& funcDSGraph = CBU->getDSGraph(*I->second);
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000207 for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(),
208 RE = funcDSGraph.retnodes_end(); RI != RE; ++RI)
Chris Lattner605a87c2005-03-19 05:15:27 +0000209 FuncECs.unionSets(FirstFunc, RI->first);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000210 }
211
212 // Now that all of the equivalences have been built, merge the graphs for
213 // each equivalence class.
214 //
Bill Wendling5294fb02006-11-17 07:33:59 +0000215 DOUT << "\nIndirect Function Equivalence Sets:\n";
Chris Lattner605a87c2005-03-19 05:15:27 +0000216 for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E =
217 FuncECs.end(); EQSI != E; ++EQSI) {
218 if (!EQSI->isLeader()) continue;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000219
Chris Lattner605a87c2005-03-19 05:15:27 +0000220 EquivalenceClasses<Function*>::member_iterator SI =
221 FuncECs.member_begin(EQSI);
222 assert(SI != FuncECs.member_end() && "Empty equiv set??");
223 EquivalenceClasses<Function*>::member_iterator SN = SI;
224 ++SN;
225 if (SN == FuncECs.member_end())
226 continue; // Single function equivalence set, no merging to do.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000227
Chris Lattner605a87c2005-03-19 05:15:27 +0000228 Function* LF = *SI;
229
Chris Lattner983baf42004-11-02 06:38:58 +0000230#ifndef NDEBUG
Bill Wendling5294fb02006-11-17 07:33:59 +0000231 DOUT <<" Equivalence set for leader " << LF->getName() <<" = ";
Chris Lattner605a87c2005-03-19 05:15:27 +0000232 for (SN = SI; SN != FuncECs.member_end(); ++SN)
Bill Wendling5294fb02006-11-17 07:33:59 +0000233 DOUT << " " << (*SN)->getName() << "," ;
234 DOUT << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000235#endif
236
Chris Lattner605a87c2005-03-19 05:15:27 +0000237 // This equiv class has multiple functions: merge their graphs. First,
238 // clone the CBU graph for the leader and make it the common graph for the
239 // equivalence graph.
240 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000241
Chris Lattner605a87c2005-03-19 05:15:27 +0000242 // Record the argument nodes for use in merging later below.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000243 std::vector<DSNodeHandle> ArgNodes;
Chris Lattner77408b82004-11-01 20:37:00 +0000244
Chris Lattner605a87c2005-03-19 05:15:27 +0000245 for (Function::arg_iterator AI = LF->arg_begin(), E = LF->arg_end();
246 AI != E; ++AI)
247 if (DS::isPointerType(AI->getType()))
248 ArgNodes.push_back(MergedG.getNodeForValue(AI));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000249
Chris Lattner605a87c2005-03-19 05:15:27 +0000250 // Merge in the graphs of all other functions in this equiv. class. Note
251 // that two or more functions may have the same graph, and it only needs
252 // to be merged in once.
253 std::set<DSGraph*> GraphsMerged;
254 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000255
Chris Lattner605a87c2005-03-19 05:15:27 +0000256 for (++SI; SI != FuncECs.member_end(); ++SI) {
257 Function *F = *SI;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000258 DSGraph &CBUGraph = CBU->getDSGraph(*F);
Chris Lattner605a87c2005-03-19 05:15:27 +0000259 if (GraphsMerged.insert(&CBUGraph).second) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000260 // 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 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000266
Chris Lattner983baf42004-11-02 06:38:58 +0000267 // Clone this member of the equivalence class into MergedG.
Chris Lattnera2197132005-03-22 00:36:51 +0000268 MergedG.cloneInto(CBUGraph);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000269 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000270
Chris Lattner605a87c2005-03-19 05:15:27 +0000271 // Merge the return nodes of all functions together.
272 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000273
Chris Lattner605a87c2005-03-19 05:15:27 +0000274 // Merge the function arguments with all argument nodes found so far.
275 // If there are extra function args, add them to the vector of argNodes
276 Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
277 for (unsigned arg = 0, numArgs = ArgNodes.size();
278 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
279 if (DS::isPointerType(AI2->getType()))
280 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000281
Chris Lattner605a87c2005-03-19 05:15:27 +0000282 for ( ; AI2 != AI2end; ++AI2)
283 if (DS::isPointerType(AI2->getType()))
284 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
285 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000286 }
287 }
Bill Wendling5294fb02006-11-17 07:33:59 +0000288 DOUT << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000289}
290
291
Chris Lattnerb25959a2005-03-12 12:08:52 +0000292DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000293 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000294 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000295 if (Graph) return *Graph;
296
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000297 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000298
299 // Copy the CBU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000300 Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000301 Graph->setGlobalsGraph(&getGlobalsGraph());
302 Graph->setPrintAuxCalls();
303
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000304 // Make sure to update the DSInfo map for all functions in the graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000305 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
306 I != Graph->retnodes_end(); ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000307 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000308 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000309 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000310 FG = Graph;
311 }
312
Chris Lattner68f96582004-11-01 19:54:06 +0000313 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000314}
315
316
Chris Lattnerb25959a2005-03-12 12:08:52 +0000317unsigned EquivClassGraphs::
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000318processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
Chris Lattner033a7d52004-11-02 17:51:11 +0000319 std::map<DSGraph*, unsigned> &ValMap) {
320 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
321 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000322 return It->second;
323
Bill Wendling5294fb02006-11-17 07:33:59 +0000324 DOUT << " ProcessSCC for function " << FG.getFunctionNames() << "\n";
Chris Lattner033a7d52004-11-02 17:51:11 +0000325
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000326 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000327 ValMap[&FG] = Min;
328 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000329
330 // The edges out of the current node are the call site targets...
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000331 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
332 CI != CE; ++CI) {
Chris Lattner6538f422005-01-30 23:51:25 +0000333 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000334
335 // Loop over all of the actually called functions...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000336 for (callee_iterator I = callee_begin(Call), E = callee_end(Call);
337 I != E; ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000338 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000339 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000340 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000341 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000342 if (M < Min) Min = M;
343 }
344 }
345
Chris Lattner033a7d52004-11-02 17:51:11 +0000346 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000347 if (Min != MyID)
348 return Min; // This is part of a larger SCC!
349
350 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000351 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000352 while (Stack.back() != &FG) {
353 DSGraph *NG = Stack.back();
354 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000355
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000356 // If the SCC found is not the same as those found in CBU, make sure to
357 // merge the graphs as appropriate.
Chris Lattnera2197132005-03-22 00:36:51 +0000358 FG.cloneInto(*NG);
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000359
360 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000361 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
362 I != NG->retnodes_end(); ++I)
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000363 DSInfo[I->first] = &FG;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000364
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000365 // Remove NG from the ValMap since the pointer may get recycled.
366 ValMap.erase(NG);
367 delete NG;
368 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000369 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000370 }
371
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000372 // Clean up the graph before we start inlining a bunch again.
373 if (MergedGraphs)
374 FG.removeTriviallyDeadNodes();
375
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000376 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000377
Chris Lattner033a7d52004-11-02 17:51:11 +0000378 processGraph(FG);
379 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000380 return MyID;
381}
382
383
384/// processGraph - Process the CBU graphs for the program in bottom-up order on
385/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000386void EquivClassGraphs::processGraph(DSGraph &G) {
Bill Wendling5294fb02006-11-17 07:33:59 +0000387 DOUT << " ProcessGraph for function " << G.getFunctionNames() << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000388
389 hash_set<Instruction*> calls;
390
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000391 // Else we need to inline some callee graph. Visit all call sites.
392 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000393 unsigned i = 0;
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000394 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattner6538f422005-01-30 23:51:25 +0000395 ++CI, ++i) {
396 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000397 Instruction *TheCall = CS.getCallSite().getInstruction();
398
399 assert(calls.insert(TheCall).second &&
400 "Call instruction occurs multiple times in graph??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000401
Chris Lattner5021b8c2005-03-18 23:19:47 +0000402 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
403 continue;
404
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000405 // Inline the common callee graph into the current graph, if the callee
406 // graph has not changed. Note that all callees should have the same
407 // graph so we only need to do this once.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000408 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000409 DSGraph* CalleeGraph = NULL;
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000410 callee_iterator I = callee_begin(TheCall), E = callee_end(TheCall);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000411 unsigned TNum, Num;
412
413 // Loop over all potential callees to find the first non-external callee.
414 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
415 if (!I->second->isExternal())
416 break;
417
418 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000419 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000420 Function *CalleeFunc = I->second;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000421
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000422 // Merge the callee's graph into this graph, if not already the same.
423 // Callees in the same equivalence class (which subsumes those
424 // in the same SCCs) have the same graph. Note that all recursion
425 // including self-recursion have been folded in the equiv classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000426 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000427 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000428 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000429 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000430 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
Chris Lattner0d397bd2005-03-24 18:42:51 +0000431 DSGraph::StripAllocaBit |
Chris Lattner033a7d52004-11-02 17:51:11 +0000432 DSGraph::DontCloneCallNodes |
433 DSGraph::DontCloneAuxCallNodes);
Bill Wendling5294fb02006-11-17 07:33:59 +0000434 DOUT << " Inlining graph [" << i << "/"
435 << G.getFunctionCalls().size()-1
436 << ":" << TNum << "/" << Num-1 << "] for "
437 << CalleeFunc->getName() << "["
438 << CalleeGraph->getGraphSize() << "+"
439 << CalleeGraph->getAuxFunctionCalls().size()
440 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
441 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
442 << "]\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000443 }
444 }
445
446#ifndef NDEBUG
447 // Now loop over the rest of the callees and make sure they have the
448 // same graph as the one inlined above.
449 if (CalleeGraph)
450 for (++I, ++TNum; I != E; ++I, ++TNum)
451 if (!I->second->isExternal())
452 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
453 "Callees at a call site have different graphs?");
454#endif
455 }
456
Chris Lattner31d3f672004-10-31 23:41:26 +0000457 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000458 G.maskIncompleteMarkers();
459 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000460
Chris Lattner31d3f672004-10-31 23:41:26 +0000461 // Delete dead nodes. Treat globals that are unreachable but that can
462 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000463 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000464
465 // When this graph is finalized, clone the globals in the graph into the
466 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000467 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000468
469 // Clone everything reachable from globals in the function graph into the
470 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000471 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000472 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000473 E = MainSM.global_end(); I != E; ++I)
Chris Lattnerab8544a2004-10-31 21:56:11 +0000474 RC.getClonedNH(MainSM[*I]);
475
Bill Wendling5294fb02006-11-17 07:33:59 +0000476 DOUT << " -- DONE ProcessGraph for function " << G.getFunctionNames() <<"\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000477}