Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 1 | //===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 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 Lattner | 0700123 | 2005-04-02 20:08:17 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Chris Lattner | 2af8c51 | 2005-03-15 17:14:09 +0000 | [diff] [blame] | 19 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | eaef568 | 2004-07-07 06:22:54 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Chris Lattner | c9b9380 | 2004-10-11 20:53:28 +0000 | [diff] [blame] | 24 | #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 Lattner | 7238210 | 2006-01-22 23:19:18 +0000 | [diff] [blame] | 29 | #include <iostream> |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame^] | 33 | RegisterPass<EquivClassGraphs> X("eqdatastructure", |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 34 | "Equivalence-class Bottom-up Data Structure Analysis"); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 35 | Statistic<> NumEquivBUInlines("equivdatastructures", |
| 36 | "Number of graphs inlined"); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 37 | Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up", |
| 38 | "Number of graphs inlined"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 41 | #ifndef NDEBUG |
| 42 | template<typename GT> |
| 43 | static void CheckAllGraphs(Module *M, GT &ECGraphs) { |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 44 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 45 | if (!I->isExternal()) { |
| 46 | DSGraph &G = ECGraphs.getDSGraph(*I); |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 47 | if (G.retnodes_begin()->first != I) |
Chris Lattner | b2b17bb | 2005-03-14 19:22:47 +0000 | [diff] [blame] | 48 | continue; // Only check a graph once. |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 49 | |
| 50 | DSGraph::NodeMapTy GlobalsGraphNodeMapping; |
Chris Lattner | b0f92e3 | 2005-03-15 00:58:16 +0000 | [diff] [blame] | 51 | G.computeGToGGMapping(GlobalsGraphNodeMapping); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 53 | } |
| 54 | #endif |
| 55 | |
Chris Lattner | 4457f7e | 2004-11-01 21:07:05 +0000 | [diff] [blame] | 56 | // getSomeCalleeForCallSite - Return any one callee function at a call site. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 57 | // |
Chris Lattner | adfd5f1 | 2005-03-13 19:51:24 +0000 | [diff] [blame] | 58 | Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{ |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 59 | Function *thisFunc = CS.getCaller(); |
Chris Lattner | 4457f7e | 2004-11-01 21:07:05 +0000 | [diff] [blame] | 60 | assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?"); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 61 | DSGraph &DSG = getDSGraph(*thisFunc); |
| 62 | DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 63 | std::map<DSNode*, Function *>::const_iterator I = |
| 64 | OneCalledFunction.find(calleeNode); |
| 65 | return (I == OneCalledFunction.end())? NULL : I->second; |
| 66 | } |
| 67 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 68 | // runOnModule - Calculate the bottom up data structure graphs for each function |
| 69 | // in the program. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 70 | // |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 71 | bool EquivClassGraphs::runOnModule(Module &M) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 72 | CBU = &getAnalysis<CompleteBUDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 73 | GlobalECs = CBU->getGlobalECs(); |
Chris Lattner | 04252fe | 2004-11-11 22:11:17 +0000 | [diff] [blame] | 74 | DEBUG(CheckAllGraphs(&M, *CBU)); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 75 | |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 76 | GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 77 | GlobalsGraph->setPrintAuxCalls(); |
| 78 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 79 | ActualCallees = CBU->getActualCallees(); |
| 80 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 81 | // 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 Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 86 | std::vector<DSGraph*> Stack; |
| 87 | std::map<DSGraph*, unsigned> ValMap; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 88 | unsigned NextID = 1; |
| 89 | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 90 | Function *MainFunc = M.getMainFunction(); |
| 91 | if (MainFunc && !MainFunc->isExternal()) { |
| 92 | processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 93 | } else { |
| 94 | std::cerr << "Fold Graphs: No 'main' function found!\n"; |
| 95 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 96 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 97 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 98 | if (!I->isExternal()) |
| 99 | processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 101 | DEBUG(CheckAllGraphs(&M, *this)); |
| 102 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 103 | getGlobalsGraph().removeTriviallyDeadNodes(); |
Chris Lattner | 38065a7 | 2005-03-15 22:47:18 +0000 | [diff] [blame] | 104 | getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 105 | |
| 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 Lattner | 49e88e8 | 2005-03-15 22:10:04 +0000 | [diff] [blame] | 109 | if (MainFunc && !MainFunc->isExternal()) { |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 110 | DSGraph &MainGraph = getOrCreateGraph(*MainFunc); |
| 111 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 112 | ReachabilityCloner RC(MainGraph, GG, |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 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 Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 122 | MainGraph.maskIncompleteMarkers(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 123 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 124 | DSGraph::IgnoreGlobals); |
| 125 | } |
| 126 | |
Chris Lattner | 2af8c51 | 2005-03-15 17:14:09 +0000 | [diff] [blame] | 127 | // 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 Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 150 | return false; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 151 | } |
| 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 Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 158 | void EquivClassGraphs::buildIndirectFunctionSets(Module &M) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 159 | const ActualCalleesTy& AC = CBU->getActualCallees(); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 160 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 161 | // 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 Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 173 | FuncECs.insert(I->second); // -- Make sure function has equiv class |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 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; |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 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 Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 185 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 186 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 187 | OneCalledFunction[calleeNode] = FirstFunc; |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 188 | FuncECs.insert(I->second); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 189 | } else { |
| 190 | // This is not the first possible callee from a particular call site. |
| 191 | // Union the callee in with the other functions. |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 192 | FuncECs.unionSets(FirstFunc, I->second); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 193 | #ifndef NDEBUG |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 194 | Function *thisFunc = LastInst->getParent()->getParent(); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 195 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
Misha Brukman | dedf2bd | 2005-04-22 04:01:18 +0000 | [diff] [blame] | 196 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 197 | 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 Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 206 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 207 | DSGraph& funcDSGraph = CBU->getDSGraph(*I->second); |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 208 | for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(), |
| 209 | RE = funcDSGraph.retnodes_end(); RI != RE; ++RI) |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 210 | FuncECs.unionSets(FirstFunc, RI->first); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Now that all of the equivalences have been built, merge the graphs for |
| 214 | // each equivalence class. |
| 215 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 216 | DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n"); |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 217 | for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E = |
| 218 | FuncECs.end(); EQSI != E; ++EQSI) { |
| 219 | if (!EQSI->isLeader()) continue; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 221 | 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. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 228 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 229 | Function* LF = *SI; |
| 230 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 231 | #ifndef NDEBUG |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 232 | DEBUG(std::cerr <<" Equivalence set for leader " << LF->getName() <<" = "); |
| 233 | for (SN = SI; SN != FuncECs.member_end(); ++SN) |
| 234 | DEBUG(std::cerr << " " << (*SN)->getName() << "," ); |
| 235 | DEBUG(std::cerr << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 236 | #endif |
| 237 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 238 | // 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. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 243 | // Record the argument nodes for use in merging later below. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 244 | std::vector<DSNodeHandle> ArgNodes; |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 246 | 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 Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 251 | // 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 Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 257 | for (++SI; SI != FuncECs.member_end(); ++SI) { |
| 258 | Function *F = *SI; |
| 259 | DSGraph *&FG = DSInfo[F]; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 260 | |
| 261 | DSGraph &CBUGraph = CBU->getDSGraph(*F); |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 262 | if (GraphsMerged.insert(&CBUGraph).second) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 263 | // Record the "folded" graph for the function. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 264 | for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(), |
| 265 | E = CBUGraph.retnodes_end(); I != E; ++I) { |
Chris Lattner | f1de30a | 2004-11-02 20:31:06 +0000 | [diff] [blame] | 266 | assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!"); |
| 267 | DSInfo[I->first] = &MergedG; |
| 268 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 270 | // Clone this member of the equivalence class into MergedG. |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 271 | MergedG.cloneInto(CBUGraph); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 272 | } |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 274 | // Merge the return nodes of all functions together. |
| 275 | MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 277 | // Merge the function arguments with all argument nodes found so far. |
| 278 | // If there are extra function args, add them to the vector of argNodes |
| 279 | Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end(); |
| 280 | for (unsigned arg = 0, numArgs = ArgNodes.size(); |
| 281 | arg != numArgs && AI2 != AI2end; ++AI2, ++arg) |
| 282 | if (DS::isPointerType(AI2->getType())) |
| 283 | ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2)); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 285 | for ( ; AI2 != AI2end; ++AI2) |
| 286 | if (DS::isPointerType(AI2->getType())) |
| 287 | ArgNodes.push_back(MergedG.getNodeForValue(AI2)); |
| 288 | DEBUG(MergedG.AssertGraphOK()); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | DEBUG(std::cerr << "\n"); |
| 292 | } |
| 293 | |
| 294 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 295 | DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 296 | // Has the graph already been created? |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 297 | DSGraph *&Graph = DSInfo[&F]; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 298 | if (Graph) return *Graph; |
| 299 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 300 | DSGraph &CBUGraph = CBU->getDSGraph(F); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 301 | |
| 302 | // Copy the CBU graph... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 303 | Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 304 | Graph->setGlobalsGraph(&getGlobalsGraph()); |
| 305 | Graph->setPrintAuxCalls(); |
| 306 | |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 307 | // Make sure to update the DSInfo map for all functions in the graph! |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 308 | for (DSGraph::retnodes_iterator I = Graph->retnodes_begin(); |
| 309 | I != Graph->retnodes_end(); ++I) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 310 | if (I->first != &F) { |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 311 | DSGraph *&FG = DSInfo[I->first]; |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 312 | assert(FG == 0 && "Merging function in SCC twice?"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 313 | FG = Graph; |
| 314 | } |
| 315 | |
Chris Lattner | 68f9658 | 2004-11-01 19:54:06 +0000 | [diff] [blame] | 316 | return *Graph; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 320 | unsigned EquivClassGraphs:: |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 321 | processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID, |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 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 Lattner | 4bbf3df | 2004-10-31 23:01:34 +0000 | [diff] [blame] | 325 | return It->second; |
| 326 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 327 | DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames() |
| 328 | << "\n"); |
| 329 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 330 | unsigned Min = NextID++, MyID = Min; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 331 | ValMap[&FG] = Min; |
| 332 | Stack.push_back(&FG); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 333 | |
| 334 | // The edges out of the current node are the call site targets... |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 335 | for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end(); |
| 336 | CI != CE; ++CI) { |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 337 | Instruction *Call = CI->getCallSite().getInstruction(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 338 | |
| 339 | // Loop over all of the actually called functions... |
Chris Lattner | 2ccc5f1 | 2005-04-02 20:02:41 +0000 | [diff] [blame] | 340 | for (callee_iterator I = callee_begin(Call), E = callee_end(Call); |
| 341 | I != E; ++I) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 342 | if (!I->second->isExternal()) { |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 343 | // Process the callee as necessary. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 344 | unsigned M = processSCC(getOrCreateGraph(*I->second), |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 345 | Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 346 | if (M < Min) Min = M; |
| 347 | } |
| 348 | } |
| 349 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 350 | assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 351 | 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 Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 355 | bool MergedGraphs = false; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 356 | while (Stack.back() != &FG) { |
| 357 | DSGraph *NG = Stack.back(); |
| 358 | ValMap[NG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 359 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 360 | // If the SCC found is not the same as those found in CBU, make sure to |
| 361 | // merge the graphs as appropriate. |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 362 | FG.cloneInto(*NG); |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 363 | |
| 364 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 365 | for (DSGraph::retnodes_iterator I = NG->retnodes_begin(); |
| 366 | I != NG->retnodes_end(); ++I) |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 367 | DSInfo[I->first] = &FG; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 368 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 369 | // Remove NG from the ValMap since the pointer may get recycled. |
| 370 | ValMap.erase(NG); |
| 371 | delete NG; |
| 372 | MergedGraphs = true; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 373 | Stack.pop_back(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 376 | // Clean up the graph before we start inlining a bunch again. |
| 377 | if (MergedGraphs) |
| 378 | FG.removeTriviallyDeadNodes(); |
| 379 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 380 | Stack.pop_back(); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 382 | processGraph(FG); |
| 383 | ValMap[&FG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 384 | return MyID; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /// processGraph - Process the CBU graphs for the program in bottom-up order on |
| 389 | /// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs. |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 390 | void EquivClassGraphs::processGraph(DSGraph &G) { |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 391 | DEBUG(std::cerr << " ProcessGraph for function " |
| 392 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 393 | |
| 394 | hash_set<Instruction*> calls; |
| 395 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 396 | // Else we need to inline some callee graph. Visit all call sites. |
| 397 | // The edges out of the current node are the call site targets... |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 398 | unsigned i = 0; |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 399 | for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE; |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 400 | ++CI, ++i) { |
| 401 | const DSCallSite &CS = *CI; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 402 | Instruction *TheCall = CS.getCallSite().getInstruction(); |
| 403 | |
| 404 | assert(calls.insert(TheCall).second && |
| 405 | "Call instruction occurs multiple times in graph??"); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 407 | if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) |
| 408 | continue; |
| 409 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 410 | // Inline the common callee graph into the current graph, if the callee |
| 411 | // graph has not changed. Note that all callees should have the same |
| 412 | // graph so we only need to do this once. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 413 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 414 | DSGraph* CalleeGraph = NULL; |
Chris Lattner | 2ccc5f1 | 2005-04-02 20:02:41 +0000 | [diff] [blame] | 415 | callee_iterator I = callee_begin(TheCall), E = callee_end(TheCall); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 416 | unsigned TNum, Num; |
| 417 | |
| 418 | // Loop over all potential callees to find the first non-external callee. |
| 419 | for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum) |
| 420 | if (!I->second->isExternal()) |
| 421 | break; |
| 422 | |
| 423 | // Now check if the graph has changed and if so, clone and inline it. |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 424 | if (I != E) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 425 | Function *CalleeFunc = I->second; |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 426 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 427 | // Merge the callee's graph into this graph, if not already the same. |
| 428 | // Callees in the same equivalence class (which subsumes those |
| 429 | // in the same SCCs) have the same graph. Note that all recursion |
| 430 | // including self-recursion have been folded in the equiv classes. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 431 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 432 | CalleeGraph = &getOrCreateGraph(*CalleeFunc); |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 433 | if (CalleeGraph != &G) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 434 | ++NumFoldGraphInlines; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 435 | G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph, |
Chris Lattner | 0d397bd | 2005-03-24 18:42:51 +0000 | [diff] [blame] | 436 | DSGraph::StripAllocaBit | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 437 | DSGraph::DontCloneCallNodes | |
| 438 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 439 | DEBUG(std::cerr << " Inlining graph [" << i << "/" |
| 440 | << G.getFunctionCalls().size()-1 |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 441 | << ":" << TNum << "/" << Num-1 << "] for " |
| 442 | << CalleeFunc->getName() << "[" |
| 443 | << CalleeGraph->getGraphSize() << "+" |
| 444 | << CalleeGraph->getAuxFunctionCalls().size() |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 445 | << "] into '" /*<< G.getFunctionNames()*/ << "' [" |
| 446 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 447 | << "]\n"); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | #ifndef NDEBUG |
| 452 | // Now loop over the rest of the callees and make sure they have the |
| 453 | // same graph as the one inlined above. |
| 454 | if (CalleeGraph) |
| 455 | for (++I, ++TNum; I != E; ++I, ++TNum) |
| 456 | if (!I->second->isExternal()) |
| 457 | assert(CalleeGraph == &getOrCreateGraph(*I->second) && |
| 458 | "Callees at a call site have different graphs?"); |
| 459 | #endif |
| 460 | } |
| 461 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 462 | // Recompute the Incomplete markers. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 463 | G.maskIncompleteMarkers(); |
| 464 | G.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 466 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 467 | // reach live nodes as live. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 468 | G.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 469 | |
| 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 Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 472 | ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 473 | |
| 474 | // Clone everything reachable from globals in the function graph into the |
| 475 | // globals graph. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 476 | DSScalarMap &MainSM = G.getScalarMap(); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 477 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 478 | E = MainSM.global_end(); I != E; ++I) |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 479 | RC.getClonedNH(MainSM[*I]); |
| 480 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 481 | DEBUG(std::cerr << " -- DONE ProcessGraph for function " |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 482 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 483 | } |