blob: 4b1eaa9ea0157d326388aeab96ddf8ed8928b3ce [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"
Chris Lattner72382102006-01-22 23:19:18 +000029#include <iostream>
Vikram S. Advec5204fb2004-05-23 07:54:02 +000030using namespace llvm;
31
Vikram S. Advec5204fb2004-05-23 07:54:02 +000032namespace {
Chris Lattner5d8925c2006-08-27 22:30:17 +000033 RegisterPass<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) {
Chris Lattner31d3f672004-10-31 23:41:26 +000044 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
45 if (!I->isExternal()) {
46 DSGraph &G = ECGraphs.getDSGraph(*I);
Chris Lattnera5f47ea2005-03-15 16:55:04 +000047 if (G.retnodes_begin()->first != I)
Chris Lattnerb2b17bb2005-03-14 19:22:47 +000048 continue; // Only check a graph once.
Chris Lattner31d3f672004-10-31 23:41:26 +000049
50 DSGraph::NodeMapTy GlobalsGraphNodeMapping;
Chris Lattnerb0f92e32005-03-15 00:58:16 +000051 G.computeGToGGMapping(GlobalsGraphNodeMapping);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000052 }
Chris Lattner31d3f672004-10-31 23:41:26 +000053}
54#endif
55
Chris Lattner4457f7e2004-11-01 21:07:05 +000056// getSomeCalleeForCallSite - Return any one callee function at a call site.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000057//
Chris Lattneradfd5f12005-03-13 19:51:24 +000058Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{
Vikram S. Advec5204fb2004-05-23 07:54:02 +000059 Function *thisFunc = CS.getCaller();
Chris Lattner4457f7e2004-11-01 21:07:05 +000060 assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?");
Chris Lattnerab8544a2004-10-31 21:56:11 +000061 DSGraph &DSG = getDSGraph(*thisFunc);
62 DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +000063 std::map<DSNode*, Function *>::const_iterator I =
64 OneCalledFunction.find(calleeNode);
65 return (I == OneCalledFunction.end())? NULL : I->second;
66}
67
Chris Lattnerab8544a2004-10-31 21:56:11 +000068// runOnModule - Calculate the bottom up data structure graphs for each function
69// in the program.
Vikram S. Advec5204fb2004-05-23 07:54:02 +000070//
Chris Lattnerb25959a2005-03-12 12:08:52 +000071bool EquivClassGraphs::runOnModule(Module &M) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +000072 CBU = &getAnalysis<CompleteBUDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000073 GlobalECs = CBU->getGlobalECs();
Chris Lattner04252fe2004-11-11 22:11:17 +000074 DEBUG(CheckAllGraphs(&M, *CBU));
Vikram S. Advec5204fb2004-05-23 07:54:02 +000075
Chris Lattnerf4f62272005-03-19 22:23:45 +000076 GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs);
Chris Lattnerab8544a2004-10-31 21:56:11 +000077 GlobalsGraph->setPrintAuxCalls();
78
Chris Lattner31d3f672004-10-31 23:41:26 +000079 ActualCallees = CBU->getActualCallees();
80
Vikram S. Advec5204fb2004-05-23 07:54:02 +000081 // Find equivalence classes of functions called from common call sites.
82 // Fold the CBU graphs for all functions in an equivalence class.
83 buildIndirectFunctionSets(M);
84
85 // Stack of functions used for Tarjan's SCC-finding algorithm.
Chris Lattner033a7d52004-11-02 17:51:11 +000086 std::vector<DSGraph*> Stack;
87 std::map<DSGraph*, unsigned> ValMap;
Vikram S. Advec5204fb2004-05-23 07:54:02 +000088 unsigned NextID = 1;
89
Chris Lattnera66e3532005-03-13 20:15:06 +000090 Function *MainFunc = M.getMainFunction();
91 if (MainFunc && !MainFunc->isExternal()) {
92 processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +000093 } else {
94 std::cerr << "Fold Graphs: No 'main' function found!\n";
95 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000096
Vikram S. Advec5204fb2004-05-23 07:54:02 +000097 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner033a7d52004-11-02 17:51:11 +000098 if (!I->isExternal())
99 processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000100
Chris Lattner31d3f672004-10-31 23:41:26 +0000101 DEBUG(CheckAllGraphs(&M, *this));
102
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000103 getGlobalsGraph().removeTriviallyDeadNodes();
Chris Lattner38065a72005-03-15 22:47:18 +0000104 getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals);
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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000112 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +0000113 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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000123 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +0000124 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();
Misha Brukman2b37d7c2005-04-21 21:13:18 +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:
Chris Lattner605a87c2005-03-19 05:15:27 +0000173 FuncECs.insert(I->second); // -- Make sure function has equiv class
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000174 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;
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000182 // 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);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000186 DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000187 OneCalledFunction[calleeNode] = FirstFunc;
Chris Lattner605a87c2005-03-19 05:15:27 +0000188 FuncECs.insert(I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000189 } else {
190 // This is not the first possible callee from a particular call site.
191 // Union the callee in with the other functions.
Chris Lattner605a87c2005-03-19 05:15:27 +0000192 FuncECs.unionSets(FirstFunc, I->second);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000193#ifndef NDEBUG
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000194 Function *thisFunc = LastInst->getParent()->getParent();
Chris Lattner15d879e2004-10-12 16:52:09 +0000195 DSGraph &TFG = CBU->getDSGraph(*thisFunc);
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000196 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000206 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000207 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)
Chris Lattner605a87c2005-03-19 05:15:27 +0000210 FuncECs.unionSets(FirstFunc, RI->first);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000211 }
212
213 // Now that all of the equivalences have been built, merge the graphs for
214 // each equivalence class.
215 //
Bill Wendling5294fb02006-11-17 07:33:59 +0000216 DOUT << "\nIndirect Function Equivalence Sets:\n";
Chris Lattner605a87c2005-03-19 05:15:27 +0000217 for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E =
218 FuncECs.end(); EQSI != E; ++EQSI) {
219 if (!EQSI->isLeader()) continue;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000220
Chris Lattner605a87c2005-03-19 05:15:27 +0000221 EquivalenceClasses<Function*>::member_iterator SI =
222 FuncECs.member_begin(EQSI);
223 assert(SI != FuncECs.member_end() && "Empty equiv set??");
224 EquivalenceClasses<Function*>::member_iterator SN = SI;
225 ++SN;
226 if (SN == FuncECs.member_end())
227 continue; // Single function equivalence set, no merging to do.
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000228
Chris Lattner605a87c2005-03-19 05:15:27 +0000229 Function* LF = *SI;
230
Chris Lattner983baf42004-11-02 06:38:58 +0000231#ifndef NDEBUG
Bill Wendling5294fb02006-11-17 07:33:59 +0000232 DOUT <<" Equivalence set for leader " << LF->getName() <<" = ";
Chris Lattner605a87c2005-03-19 05:15:27 +0000233 for (SN = SI; SN != FuncECs.member_end(); ++SN)
Bill Wendling5294fb02006-11-17 07:33:59 +0000234 DOUT << " " << (*SN)->getName() << "," ;
235 DOUT << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000236#endif
237
Chris Lattner605a87c2005-03-19 05:15:27 +0000238 // This equiv class has multiple functions: merge their graphs. First,
239 // clone the CBU graph for the leader and make it the common graph for the
240 // equivalence graph.
241 DSGraph &MergedG = getOrCreateGraph(*LF);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000242
Chris Lattner605a87c2005-03-19 05:15:27 +0000243 // Record the argument nodes for use in merging later below.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000244 std::vector<DSNodeHandle> ArgNodes;
Chris Lattner77408b82004-11-01 20:37:00 +0000245
Chris Lattner605a87c2005-03-19 05:15:27 +0000246 for (Function::arg_iterator AI = LF->arg_begin(), E = LF->arg_end();
247 AI != E; ++AI)
248 if (DS::isPointerType(AI->getType()))
249 ArgNodes.push_back(MergedG.getNodeForValue(AI));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000250
Chris Lattner605a87c2005-03-19 05:15:27 +0000251 // Merge in the graphs of all other functions in this equiv. class. Note
252 // that two or more functions may have the same graph, and it only needs
253 // to be merged in once.
254 std::set<DSGraph*> GraphsMerged;
255 GraphsMerged.insert(&CBU->getDSGraph(*LF));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000256
Chris Lattner605a87c2005-03-19 05:15:27 +0000257 for (++SI; SI != FuncECs.member_end(); ++SI) {
258 Function *F = *SI;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000259 DSGraph &CBUGraph = CBU->getDSGraph(*F);
Chris Lattner605a87c2005-03-19 05:15:27 +0000260 if (GraphsMerged.insert(&CBUGraph).second) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000261 // Record the "folded" graph for the function.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000262 for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(),
263 E = CBUGraph.retnodes_end(); I != E; ++I) {
Chris Lattnerf1de30a2004-11-02 20:31:06 +0000264 assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!");
265 DSInfo[I->first] = &MergedG;
266 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000267
Chris Lattner983baf42004-11-02 06:38:58 +0000268 // Clone this member of the equivalence class into MergedG.
Chris Lattnera2197132005-03-22 00:36:51 +0000269 MergedG.cloneInto(CBUGraph);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000270 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000271
Chris Lattner605a87c2005-03-19 05:15:27 +0000272 // Merge the return nodes of all functions together.
273 MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000274
Chris Lattner605a87c2005-03-19 05:15:27 +0000275 // Merge the function arguments with all argument nodes found so far.
276 // If there are extra function args, add them to the vector of argNodes
277 Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end();
278 for (unsigned arg = 0, numArgs = ArgNodes.size();
279 arg != numArgs && AI2 != AI2end; ++AI2, ++arg)
280 if (DS::isPointerType(AI2->getType()))
281 ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2));
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000282
Chris Lattner605a87c2005-03-19 05:15:27 +0000283 for ( ; AI2 != AI2end; ++AI2)
284 if (DS::isPointerType(AI2->getType()))
285 ArgNodes.push_back(MergedG.getNodeForValue(AI2));
286 DEBUG(MergedG.AssertGraphOK());
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000287 }
288 }
Bill Wendling5294fb02006-11-17 07:33:59 +0000289 DOUT << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000290}
291
292
Chris Lattnerb25959a2005-03-12 12:08:52 +0000293DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000294 // Has the graph already been created?
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000295 DSGraph *&Graph = DSInfo[&F];
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000296 if (Graph) return *Graph;
297
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000298 DSGraph &CBUGraph = CBU->getDSGraph(F);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000299
300 // Copy the CBU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000301 Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000302 Graph->setGlobalsGraph(&getGlobalsGraph());
303 Graph->setPrintAuxCalls();
304
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000305 // Make sure to update the DSInfo map for all functions in the graph!
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000306 for (DSGraph::retnodes_iterator I = Graph->retnodes_begin();
307 I != Graph->retnodes_end(); ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000308 if (I->first != &F) {
Chris Lattnerfcb7d952004-11-01 21:02:23 +0000309 DSGraph *&FG = DSInfo[I->first];
Chris Lattner983baf42004-11-02 06:38:58 +0000310 assert(FG == 0 && "Merging function in SCC twice?");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000311 FG = Graph;
312 }
313
Chris Lattner68f96582004-11-01 19:54:06 +0000314 return *Graph;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000315}
316
317
Chris Lattnerb25959a2005-03-12 12:08:52 +0000318unsigned EquivClassGraphs::
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000319processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID,
Chris Lattner033a7d52004-11-02 17:51:11 +0000320 std::map<DSGraph*, unsigned> &ValMap) {
321 std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG);
322 if (It != ValMap.end() && It->first == &FG)
Chris Lattner4bbf3df2004-10-31 23:01:34 +0000323 return It->second;
324
Bill Wendling5294fb02006-11-17 07:33:59 +0000325 DOUT << " ProcessSCC for function " << FG.getFunctionNames() << "\n";
Chris Lattner033a7d52004-11-02 17:51:11 +0000326
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000327 unsigned Min = NextID++, MyID = Min;
Chris Lattner033a7d52004-11-02 17:51:11 +0000328 ValMap[&FG] = Min;
329 Stack.push_back(&FG);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000330
331 // The edges out of the current node are the call site targets...
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000332 for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end();
333 CI != CE; ++CI) {
Chris Lattner6538f422005-01-30 23:51:25 +0000334 Instruction *Call = CI->getCallSite().getInstruction();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000335
336 // Loop over all of the actually called functions...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000337 for (callee_iterator I = callee_begin(Call), E = callee_end(Call);
338 I != E; ++I)
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000339 if (!I->second->isExternal()) {
Chris Lattner31d3f672004-10-31 23:41:26 +0000340 // Process the callee as necessary.
Chris Lattner033a7d52004-11-02 17:51:11 +0000341 unsigned M = processSCC(getOrCreateGraph(*I->second),
Chris Lattner31d3f672004-10-31 23:41:26 +0000342 Stack, NextID, ValMap);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000343 if (M < Min) Min = M;
344 }
345 }
346
Chris Lattner033a7d52004-11-02 17:51:11 +0000347 assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!");
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000348 if (Min != MyID)
349 return Min; // This is part of a larger SCC!
350
351 // If this is a new SCC, process it now.
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000352 bool MergedGraphs = false;
Chris Lattner033a7d52004-11-02 17:51:11 +0000353 while (Stack.back() != &FG) {
354 DSGraph *NG = Stack.back();
355 ValMap[NG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000356
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000357 // If the SCC found is not the same as those found in CBU, make sure to
358 // merge the graphs as appropriate.
Chris Lattnera2197132005-03-22 00:36:51 +0000359 FG.cloneInto(*NG);
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000360
361 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000362 for (DSGraph::retnodes_iterator I = NG->retnodes_begin();
363 I != NG->retnodes_end(); ++I)
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000364 DSInfo[I->first] = &FG;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000365
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000366 // Remove NG from the ValMap since the pointer may get recycled.
367 ValMap.erase(NG);
368 delete NG;
369 MergedGraphs = true;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000370 Stack.pop_back();
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000371 }
372
Chris Lattnercaa35bc2004-11-02 19:29:59 +0000373 // Clean up the graph before we start inlining a bunch again.
374 if (MergedGraphs)
375 FG.removeTriviallyDeadNodes();
376
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000377 Stack.pop_back();
Chris Lattner113cde82004-10-31 17:47:48 +0000378
Chris Lattner033a7d52004-11-02 17:51:11 +0000379 processGraph(FG);
380 ValMap[&FG] = ~0U;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000381 return MyID;
382}
383
384
385/// processGraph - Process the CBU graphs for the program in bottom-up order on
386/// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs.
Chris Lattnerb25959a2005-03-12 12:08:52 +0000387void EquivClassGraphs::processGraph(DSGraph &G) {
Bill Wendling5294fb02006-11-17 07:33:59 +0000388 DOUT << " ProcessGraph for function " << G.getFunctionNames() << "\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000389
390 hash_set<Instruction*> calls;
391
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000392 // Else we need to inline some callee graph. Visit all call sites.
393 // The edges out of the current node are the call site targets...
Chris Lattner6538f422005-01-30 23:51:25 +0000394 unsigned i = 0;
Chris Lattner5d85f8f2005-03-15 06:29:12 +0000395 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE;
Chris Lattner6538f422005-01-30 23:51:25 +0000396 ++CI, ++i) {
397 const DSCallSite &CS = *CI;
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000398 Instruction *TheCall = CS.getCallSite().getInstruction();
399
400 assert(calls.insert(TheCall).second &&
401 "Call instruction occurs multiple times in graph??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000402
Chris Lattner5021b8c2005-03-18 23:19:47 +0000403 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0)
404 continue;
405
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000406 // Inline the common callee graph into the current graph, if the callee
407 // graph has not changed. Note that all callees should have the same
408 // graph so we only need to do this once.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000409 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000410 DSGraph* CalleeGraph = NULL;
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000411 callee_iterator I = callee_begin(TheCall), E = callee_end(TheCall);
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000412 unsigned TNum, Num;
413
414 // Loop over all potential callees to find the first non-external callee.
415 for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum)
416 if (!I->second->isExternal())
417 break;
418
419 // Now check if the graph has changed and if so, clone and inline it.
Chris Lattnerab8544a2004-10-31 21:56:11 +0000420 if (I != E) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000421 Function *CalleeFunc = I->second;
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000422
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000423 // Merge the callee's graph into this graph, if not already the same.
424 // Callees in the same equivalence class (which subsumes those
425 // in the same SCCs) have the same graph. Note that all recursion
426 // including self-recursion have been folded in the equiv classes.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000427 //
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000428 CalleeGraph = &getOrCreateGraph(*CalleeFunc);
Chris Lattner033a7d52004-11-02 17:51:11 +0000429 if (CalleeGraph != &G) {
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000430 ++NumFoldGraphInlines;
Chris Lattner033a7d52004-11-02 17:51:11 +0000431 G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph,
Chris Lattner0d397bd2005-03-24 18:42:51 +0000432 DSGraph::StripAllocaBit |
Chris Lattner033a7d52004-11-02 17:51:11 +0000433 DSGraph::DontCloneCallNodes |
434 DSGraph::DontCloneAuxCallNodes);
Bill Wendling5294fb02006-11-17 07:33:59 +0000435 DOUT << " Inlining graph [" << i << "/"
436 << G.getFunctionCalls().size()-1
437 << ":" << TNum << "/" << Num-1 << "] for "
438 << CalleeFunc->getName() << "["
439 << CalleeGraph->getGraphSize() << "+"
440 << CalleeGraph->getAuxFunctionCalls().size()
441 << "] into '" /*<< G.getFunctionNames()*/ << "' ["
442 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
443 << "]\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000444 }
445 }
446
447#ifndef NDEBUG
448 // Now loop over the rest of the callees and make sure they have the
449 // same graph as the one inlined above.
450 if (CalleeGraph)
451 for (++I, ++TNum; I != E; ++I, ++TNum)
452 if (!I->second->isExternal())
453 assert(CalleeGraph == &getOrCreateGraph(*I->second) &&
454 "Callees at a call site have different graphs?");
455#endif
456 }
457
Chris Lattner31d3f672004-10-31 23:41:26 +0000458 // Recompute the Incomplete markers.
Chris Lattner033a7d52004-11-02 17:51:11 +0000459 G.maskIncompleteMarkers();
460 G.markIncompleteNodes(DSGraph::MarkFormalArgs);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000461
Chris Lattner31d3f672004-10-31 23:41:26 +0000462 // Delete dead nodes. Treat globals that are unreachable but that can
463 // reach live nodes as live.
Chris Lattner033a7d52004-11-02 17:51:11 +0000464 G.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000465
466 // When this graph is finalized, clone the globals in the graph into the
467 // globals graph to make sure it has everything, from all graphs.
Chris Lattner033a7d52004-11-02 17:51:11 +0000468 ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit);
Chris Lattnerab8544a2004-10-31 21:56:11 +0000469
470 // Clone everything reachable from globals in the function graph into the
471 // globals graph.
Chris Lattner033a7d52004-11-02 17:51:11 +0000472 DSScalarMap &MainSM = G.getScalarMap();
Chris Lattnerab8544a2004-10-31 21:56:11 +0000473 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000474 E = MainSM.global_end(); I != E; ++I)
Chris Lattnerab8544a2004-10-31 21:56:11 +0000475 RC.getClonedNH(MainSM[*I]);
476
Bill Wendling5294fb02006-11-17 07:33:59 +0000477 DOUT << " -- DONE ProcessGraph for function " << G.getFunctionNames() <<"\n";
Vikram S. Advec5204fb2004-05-23 07:54:02 +0000478}