Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 1 | //===- EquivClassGraphs.cpp - Merge equiv-class graphs & inline bottom-up -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass is the same as the complete bottom-up graphs, but |
| 11 | // with functions partitioned into equivalence classes and a single merged |
| 12 | // DS graph for all functions in an equivalence class. After this merging, |
| 13 | // graphs are inlined bottom-up on the SCCs of the final (CBU) call graph. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "ECGraphs" |
Chris 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" |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 31 | namespace { |
Chris Lattner | adfd5f1 | 2005-03-13 19:51:24 +0000 | [diff] [blame] | 32 | RegisterAnalysis<EquivClassGraphs> X("eqdatastructure", |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 33 | "Equivalence-class Bottom-up Data Structure Analysis"); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 34 | Statistic<> NumEquivBUInlines("equivdatastructures", |
| 35 | "Number of graphs inlined"); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 36 | Statistic<> NumFoldGraphInlines("Inline equiv-class graphs bottom up", |
| 37 | "Number of graphs inlined"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 40 | #ifndef NDEBUG |
| 41 | template<typename GT> |
| 42 | static void CheckAllGraphs(Module *M, GT &ECGraphs) { |
| 43 | DSGraph &GG = ECGraphs.getGlobalsGraph(); |
| 44 | |
| 45 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 46 | if (!I->isExternal()) { |
| 47 | DSGraph &G = ECGraphs.getDSGraph(*I); |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 48 | if (G.retnodes_begin()->first != I) |
Chris Lattner | b2b17bb | 2005-03-14 19:22:47 +0000 | [diff] [blame] | 49 | continue; // Only check a graph once. |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 50 | |
| 51 | DSGraph::NodeMapTy GlobalsGraphNodeMapping; |
Chris Lattner | b0f92e3 | 2005-03-15 00:58:16 +0000 | [diff] [blame] | 52 | G.computeGToGGMapping(GlobalsGraphNodeMapping); |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | #endif |
| 56 | |
Chris Lattner | 4457f7e | 2004-11-01 21:07:05 +0000 | [diff] [blame] | 57 | // getSomeCalleeForCallSite - Return any one callee function at a call site. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 58 | // |
Chris Lattner | adfd5f1 | 2005-03-13 19:51:24 +0000 | [diff] [blame] | 59 | Function *EquivClassGraphs::getSomeCalleeForCallSite(const CallSite &CS) const{ |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 60 | Function *thisFunc = CS.getCaller(); |
Chris Lattner | 4457f7e | 2004-11-01 21:07:05 +0000 | [diff] [blame] | 61 | assert(thisFunc && "getSomeCalleeForCallSite(): Not a valid call site?"); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 62 | DSGraph &DSG = getDSGraph(*thisFunc); |
| 63 | DSNode *calleeNode = DSG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 64 | std::map<DSNode*, Function *>::const_iterator I = |
| 65 | OneCalledFunction.find(calleeNode); |
| 66 | return (I == OneCalledFunction.end())? NULL : I->second; |
| 67 | } |
| 68 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 69 | // runOnModule - Calculate the bottom up data structure graphs for each function |
| 70 | // in the program. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 71 | // |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 72 | bool EquivClassGraphs::runOnModule(Module &M) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 73 | CBU = &getAnalysis<CompleteBUDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 74 | GlobalECs = CBU->getGlobalECs(); |
Chris Lattner | 04252fe | 2004-11-11 22:11:17 +0000 | [diff] [blame] | 75 | DEBUG(CheckAllGraphs(&M, *CBU)); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 76 | |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 77 | GlobalsGraph = new DSGraph(CBU->getGlobalsGraph(), GlobalECs); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 78 | GlobalsGraph->setPrintAuxCalls(); |
| 79 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 80 | ActualCallees = CBU->getActualCallees(); |
| 81 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 82 | // Find equivalence classes of functions called from common call sites. |
| 83 | // Fold the CBU graphs for all functions in an equivalence class. |
| 84 | buildIndirectFunctionSets(M); |
| 85 | |
| 86 | // Stack of functions used for Tarjan's SCC-finding algorithm. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 87 | std::vector<DSGraph*> Stack; |
| 88 | std::map<DSGraph*, unsigned> ValMap; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 89 | unsigned NextID = 1; |
| 90 | |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 91 | Function *MainFunc = M.getMainFunction(); |
| 92 | if (MainFunc && !MainFunc->isExternal()) { |
| 93 | processSCC(getOrCreateGraph(*MainFunc), Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 94 | } else { |
| 95 | std::cerr << "Fold Graphs: No 'main' function found!\n"; |
| 96 | } |
| 97 | |
| 98 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 99 | if (!I->isExternal()) |
| 100 | processSCC(getOrCreateGraph(*I), Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 102 | DEBUG(CheckAllGraphs(&M, *this)); |
| 103 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 104 | getGlobalsGraph().removeTriviallyDeadNodes(); |
Chris Lattner | 38065a7 | 2005-03-15 22:47:18 +0000 | [diff] [blame] | 105 | getGlobalsGraph().markIncompleteNodes(DSGraph::IgnoreGlobals); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 106 | |
| 107 | // Merge the globals variables (not the calls) from the globals graph back |
| 108 | // into the main function's graph so that the main function contains all of |
| 109 | // the information about global pools and GV usage in the program. |
Chris Lattner | 49e88e8 | 2005-03-15 22:10:04 +0000 | [diff] [blame] | 110 | if (MainFunc && !MainFunc->isExternal()) { |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 111 | DSGraph &MainGraph = getOrCreateGraph(*MainFunc); |
| 112 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
| 113 | ReachabilityCloner RC(MainGraph, GG, |
| 114 | DSGraph::DontCloneCallNodes | |
| 115 | DSGraph::DontCloneAuxCallNodes); |
| 116 | |
| 117 | // Clone the global nodes into this graph. |
| 118 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 119 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 120 | if (isa<GlobalVariable>(*I)) |
| 121 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 122 | |
Chris Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 123 | MainGraph.maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 124 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
| 125 | DSGraph::IgnoreGlobals); |
| 126 | } |
| 127 | |
Chris Lattner | 2af8c51 | 2005-03-15 17:14:09 +0000 | [diff] [blame] | 128 | // Final processing. Note that dead node elimination may actually remove |
| 129 | // globals from a function graph that are immediately used. If there are no |
| 130 | // scalars pointing to the node (e.g. because the only use is a direct store |
| 131 | // to a scalar global) we have to make sure to rematerialize the globals back |
| 132 | // into the graphs here, or clients will break! |
| 133 | for (Module::global_iterator GI = M.global_begin(), E = M.global_end(); |
| 134 | GI != E; ++GI) |
| 135 | // This only happens to first class typed globals. |
| 136 | if (GI->getType()->getElementType()->isFirstClassType()) |
| 137 | for (Value::use_iterator UI = GI->use_begin(), E = GI->use_end(); |
| 138 | UI != E; ++UI) |
| 139 | // This only happens to direct uses by instructions. |
| 140 | if (Instruction *User = dyn_cast<Instruction>(*UI)) { |
| 141 | DSGraph &DSG = getOrCreateGraph(*User->getParent()->getParent()); |
| 142 | if (!DSG.getScalarMap().count(GI)) { |
| 143 | // If this global does not exist in the graph, but it is immediately |
| 144 | // used by an instruction in the graph, clone it over from the |
| 145 | // globals graph. |
| 146 | ReachabilityCloner RC(DSG, *GlobalsGraph, 0); |
| 147 | RC.getClonedNH(GlobalsGraph->getNodeForValue(GI)); |
| 148 | } |
| 149 | } |
| 150 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 151 | return false; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | // buildIndirectFunctionSets - Iterate over the module looking for indirect |
| 156 | // calls to functions. If a call site can invoke any functions [F1, F2... FN], |
| 157 | // unify the N functions together in the FuncECs set. |
| 158 | // |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 159 | void EquivClassGraphs::buildIndirectFunctionSets(Module &M) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 160 | const ActualCalleesTy& AC = CBU->getActualCallees(); |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 161 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 162 | // Loop over all of the indirect calls in the program. If a call site can |
| 163 | // call multiple different functions, we need to unify all of the callees into |
| 164 | // the same equivalence class. |
| 165 | Instruction *LastInst = 0; |
| 166 | Function *FirstFunc = 0; |
| 167 | for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) { |
| 168 | if (I->second->isExternal()) |
| 169 | continue; // Ignore functions we cannot modify |
| 170 | |
| 171 | CallSite CS = CallSite::get(I->first); |
| 172 | |
| 173 | if (CS.getCalledFunction()) { // Direct call: |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 174 | FuncECs.insert(I->second); // -- Make sure function has equiv class |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 175 | FirstFunc = I->second; // -- First callee at this site |
| 176 | } else { // Else indirect call |
| 177 | // DEBUG(std::cerr << "CALLEE: " << I->second->getName() |
| 178 | // << " from : " << I->first); |
| 179 | if (I->first != LastInst) { |
| 180 | // This is the first callee from this call site. |
| 181 | LastInst = I->first; |
| 182 | FirstFunc = I->second; |
| 183 | // Instead of storing the lastInst For Indirection call Sites we store |
| 184 | // the DSNode for the function ptr arguemnt |
| 185 | Function *thisFunc = LastInst->getParent()->getParent(); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 186 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
| 187 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 188 | OneCalledFunction[calleeNode] = FirstFunc; |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 189 | FuncECs.insert(I->second); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 190 | } else { |
| 191 | // This is not the first possible callee from a particular call site. |
| 192 | // Union the callee in with the other functions. |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 193 | FuncECs.unionSets(FirstFunc, I->second); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 194 | #ifndef NDEBUG |
| 195 | Function *thisFunc = LastInst->getParent()->getParent(); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 196 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
| 197 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 198 | assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?"); |
| 199 | #endif |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Now include all functions that share a graph with any function in the |
| 204 | // equivalence class. More precisely, if F is in the class, and G(F) is |
| 205 | // its graph, then we include all other functions that are also in G(F). |
| 206 | // Currently, that is just the functions in the same call-graph-SCC as F. |
| 207 | // |
| 208 | DSGraph& funcDSGraph = CBU->getDSGraph(*I->second); |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 209 | for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(), |
| 210 | RE = funcDSGraph.retnodes_end(); RI != RE; ++RI) |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 211 | FuncECs.unionSets(FirstFunc, RI->first); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Now that all of the equivalences have been built, merge the graphs for |
| 215 | // each equivalence class. |
| 216 | // |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 217 | DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n"); |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 218 | for (EquivalenceClasses<Function*>::iterator EQSI = FuncECs.begin(), E = |
| 219 | FuncECs.end(); EQSI != E; ++EQSI) { |
| 220 | if (!EQSI->isLeader()) continue; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 221 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 222 | EquivalenceClasses<Function*>::member_iterator SI = |
| 223 | FuncECs.member_begin(EQSI); |
| 224 | assert(SI != FuncECs.member_end() && "Empty equiv set??"); |
| 225 | EquivalenceClasses<Function*>::member_iterator SN = SI; |
| 226 | ++SN; |
| 227 | if (SN == FuncECs.member_end()) |
| 228 | continue; // Single function equivalence set, no merging to do. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 229 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 230 | Function* LF = *SI; |
| 231 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 232 | #ifndef NDEBUG |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 233 | DEBUG(std::cerr <<" Equivalence set for leader " << LF->getName() <<" = "); |
| 234 | for (SN = SI; SN != FuncECs.member_end(); ++SN) |
| 235 | DEBUG(std::cerr << " " << (*SN)->getName() << "," ); |
| 236 | DEBUG(std::cerr << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 237 | #endif |
| 238 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 239 | // This equiv class has multiple functions: merge their graphs. First, |
| 240 | // clone the CBU graph for the leader and make it the common graph for the |
| 241 | // equivalence graph. |
| 242 | DSGraph &MergedG = getOrCreateGraph(*LF); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 244 | // Record the argument nodes for use in merging later below. |
| 245 | std::vector<DSNodeHandle> ArgNodes; |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 247 | for (Function::arg_iterator AI = LF->arg_begin(), E = LF->arg_end(); |
| 248 | AI != E; ++AI) |
| 249 | if (DS::isPointerType(AI->getType())) |
| 250 | ArgNodes.push_back(MergedG.getNodeForValue(AI)); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 251 | |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 252 | // Merge in the graphs of all other functions in this equiv. class. Note |
| 253 | // that two or more functions may have the same graph, and it only needs |
| 254 | // to be merged in once. |
| 255 | std::set<DSGraph*> GraphsMerged; |
| 256 | GraphsMerged.insert(&CBU->getDSGraph(*LF)); |
| 257 | |
| 258 | for (++SI; SI != FuncECs.member_end(); ++SI) { |
| 259 | Function *F = *SI; |
| 260 | DSGraph *&FG = DSInfo[F]; |
| 261 | |
| 262 | DSGraph &CBUGraph = CBU->getDSGraph(*F); |
| 263 | if (GraphsMerged.insert(&CBUGraph).second) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 264 | // Record the "folded" graph for the function. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 265 | for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(), |
| 266 | E = CBUGraph.retnodes_end(); I != E; ++I) { |
Chris Lattner | f1de30a | 2004-11-02 20:31:06 +0000 | [diff] [blame] | 267 | assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!"); |
| 268 | DSInfo[I->first] = &MergedG; |
| 269 | } |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 271 | // Clone this member of the equivalence class into MergedG. |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 272 | MergedG.cloneInto(CBUGraph); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | 605a87c | 2005-03-19 05:15:27 +0000 | [diff] [blame] | 274 | |
| 275 | // Merge the return nodes of all functions together. |
| 276 | MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]); |
| 277 | |
| 278 | // Merge the function arguments with all argument nodes found so far. |
| 279 | // If there are extra function args, add them to the vector of argNodes |
| 280 | Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end(); |
| 281 | for (unsigned arg = 0, numArgs = ArgNodes.size(); |
| 282 | arg != numArgs && AI2 != AI2end; ++AI2, ++arg) |
| 283 | if (DS::isPointerType(AI2->getType())) |
| 284 | ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2)); |
| 285 | |
| 286 | for ( ; AI2 != AI2end; ++AI2) |
| 287 | if (DS::isPointerType(AI2->getType())) |
| 288 | ArgNodes.push_back(MergedG.getNodeForValue(AI2)); |
| 289 | DEBUG(MergedG.AssertGraphOK()); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | DEBUG(std::cerr << "\n"); |
| 293 | } |
| 294 | |
| 295 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 296 | DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 297 | // Has the graph already been created? |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 298 | DSGraph *&Graph = DSInfo[&F]; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 299 | if (Graph) return *Graph; |
| 300 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 301 | DSGraph &CBUGraph = CBU->getDSGraph(F); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 302 | |
| 303 | // Copy the CBU graph... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 304 | Graph = new DSGraph(CBUGraph, GlobalECs); // updates the map via reference |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 305 | Graph->setGlobalsGraph(&getGlobalsGraph()); |
| 306 | Graph->setPrintAuxCalls(); |
| 307 | |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 308 | // 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] | 309 | for (DSGraph::retnodes_iterator I = Graph->retnodes_begin(); |
| 310 | I != Graph->retnodes_end(); ++I) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 311 | if (I->first != &F) { |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 312 | DSGraph *&FG = DSInfo[I->first]; |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 313 | assert(FG == 0 && "Merging function in SCC twice?"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 314 | FG = Graph; |
| 315 | } |
| 316 | |
Chris Lattner | 68f9658 | 2004-11-01 19:54:06 +0000 | [diff] [blame] | 317 | return *Graph; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 321 | unsigned EquivClassGraphs:: |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 322 | processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID, |
| 323 | std::map<DSGraph*, unsigned> &ValMap) { |
| 324 | std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG); |
| 325 | if (It != ValMap.end() && It->first == &FG) |
Chris Lattner | 4bbf3df | 2004-10-31 23:01:34 +0000 | [diff] [blame] | 326 | return It->second; |
| 327 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 328 | DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames() |
| 329 | << "\n"); |
| 330 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 331 | unsigned Min = NextID++, MyID = Min; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 332 | ValMap[&FG] = Min; |
| 333 | Stack.push_back(&FG); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 334 | |
| 335 | // The edges out of the current node are the call site targets... |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 336 | for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end(); |
| 337 | CI != CE; ++CI) { |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 338 | Instruction *Call = CI->getCallSite().getInstruction(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 339 | |
| 340 | // Loop over all of the actually called functions... |
Chris Lattner | 2ccc5f1 | 2005-04-02 20:02:41 +0000 | [diff] [blame] | 341 | for (callee_iterator I = callee_begin(Call), E = callee_end(Call); |
| 342 | I != E; ++I) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 343 | if (!I->second->isExternal()) { |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 344 | // Process the callee as necessary. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 345 | unsigned M = processSCC(getOrCreateGraph(*I->second), |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 346 | Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 347 | if (M < Min) Min = M; |
| 348 | } |
| 349 | } |
| 350 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 351 | assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 352 | if (Min != MyID) |
| 353 | return Min; // This is part of a larger SCC! |
| 354 | |
| 355 | // If this is a new SCC, process it now. |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 356 | bool MergedGraphs = false; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 357 | while (Stack.back() != &FG) { |
| 358 | DSGraph *NG = Stack.back(); |
| 359 | ValMap[NG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 360 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 361 | // If the SCC found is not the same as those found in CBU, make sure to |
| 362 | // merge the graphs as appropriate. |
Chris Lattner | a219713 | 2005-03-22 00:36:51 +0000 | [diff] [blame] | 363 | FG.cloneInto(*NG); |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 364 | |
| 365 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 366 | for (DSGraph::retnodes_iterator I = NG->retnodes_begin(); |
| 367 | I != NG->retnodes_end(); ++I) |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 368 | DSInfo[I->first] = &FG; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 369 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 370 | // Remove NG from the ValMap since the pointer may get recycled. |
| 371 | ValMap.erase(NG); |
| 372 | delete NG; |
| 373 | MergedGraphs = true; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 374 | Stack.pop_back(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 377 | // Clean up the graph before we start inlining a bunch again. |
| 378 | if (MergedGraphs) |
| 379 | FG.removeTriviallyDeadNodes(); |
| 380 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 381 | Stack.pop_back(); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 383 | processGraph(FG); |
| 384 | ValMap[&FG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 385 | return MyID; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /// processGraph - Process the CBU graphs for the program in bottom-up order on |
| 390 | /// the SCC of the __ACTUAL__ call graph. This builds final folded CBU graphs. |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 391 | void EquivClassGraphs::processGraph(DSGraph &G) { |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 392 | DEBUG(std::cerr << " ProcessGraph for function " |
| 393 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 394 | |
| 395 | hash_set<Instruction*> calls; |
| 396 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 397 | // Else we need to inline some callee graph. Visit all call sites. |
| 398 | // The edges out of the current node are the call site targets... |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 399 | unsigned i = 0; |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 400 | 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] | 401 | ++CI, ++i) { |
| 402 | const DSCallSite &CS = *CI; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 403 | Instruction *TheCall = CS.getCallSite().getInstruction(); |
| 404 | |
| 405 | assert(calls.insert(TheCall).second && |
| 406 | "Call instruction occurs multiple times in graph??"); |
| 407 | |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 408 | if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) |
| 409 | continue; |
| 410 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 411 | // Inline the common callee graph into the current graph, if the callee |
| 412 | // graph has not changed. Note that all callees should have the same |
| 413 | // graph so we only need to do this once. |
| 414 | // |
| 415 | DSGraph* CalleeGraph = NULL; |
Chris Lattner | 2ccc5f1 | 2005-04-02 20:02:41 +0000 | [diff] [blame] | 416 | callee_iterator I = callee_begin(TheCall), E = callee_end(TheCall); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 417 | unsigned TNum, Num; |
| 418 | |
| 419 | // Loop over all potential callees to find the first non-external callee. |
| 420 | for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum) |
| 421 | if (!I->second->isExternal()) |
| 422 | break; |
| 423 | |
| 424 | // 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] | 425 | if (I != E) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 426 | Function *CalleeFunc = I->second; |
| 427 | |
| 428 | // Merge the callee's graph into this graph, if not already the same. |
| 429 | // Callees in the same equivalence class (which subsumes those |
| 430 | // in the same SCCs) have the same graph. Note that all recursion |
| 431 | // including self-recursion have been folded in the equiv classes. |
| 432 | // |
| 433 | CalleeGraph = &getOrCreateGraph(*CalleeFunc); |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 434 | if (CalleeGraph != &G) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 435 | ++NumFoldGraphInlines; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 436 | G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph, |
Chris Lattner | 0d397bd | 2005-03-24 18:42:51 +0000 | [diff] [blame] | 437 | DSGraph::StripAllocaBit | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 438 | DSGraph::DontCloneCallNodes | |
| 439 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 440 | DEBUG(std::cerr << " Inlining graph [" << i << "/" |
| 441 | << G.getFunctionCalls().size()-1 |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 442 | << ":" << TNum << "/" << Num-1 << "] for " |
| 443 | << CalleeFunc->getName() << "[" |
| 444 | << CalleeGraph->getGraphSize() << "+" |
| 445 | << CalleeGraph->getAuxFunctionCalls().size() |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 446 | << "] into '" /*<< G.getFunctionNames()*/ << "' [" |
| 447 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 448 | << "]\n"); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | #ifndef NDEBUG |
| 453 | // Now loop over the rest of the callees and make sure they have the |
| 454 | // same graph as the one inlined above. |
| 455 | if (CalleeGraph) |
| 456 | for (++I, ++TNum; I != E; ++I, ++TNum) |
| 457 | if (!I->second->isExternal()) |
| 458 | assert(CalleeGraph == &getOrCreateGraph(*I->second) && |
| 459 | "Callees at a call site have different graphs?"); |
| 460 | #endif |
| 461 | } |
| 462 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 463 | // Recompute the Incomplete markers. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 464 | G.maskIncompleteMarkers(); |
| 465 | G.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 466 | |
| 467 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 468 | // reach live nodes as live. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 469 | G.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 470 | |
| 471 | // When this graph is finalized, clone the globals in the graph into the |
| 472 | // globals graph to make sure it has everything, from all graphs. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 473 | ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 474 | |
| 475 | // Clone everything reachable from globals in the function graph into the |
| 476 | // globals graph. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 477 | DSScalarMap &MainSM = G.getScalarMap(); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 478 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
| 479 | E = MainSM.global_end(); I != E; ++I) |
| 480 | RC.getClonedNH(MainSM[*I]); |
| 481 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 482 | DEBUG(std::cerr << " -- DONE ProcessGraph for function " |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 483 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 484 | } |