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 | 7aed717 | 2005-03-12 11:51:30 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/EquivClassGraphs.h" |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Pass.h" |
Chris Lattner | eaef568 | 2004-07-07 06:22:54 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
| 22 | #include "llvm/Analysis/DataStructure/DataStructure.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 | 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 | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 76 | GlobalsGraph = new DSGraph(CBU->getGlobalsGraph()); |
| 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 | } |
| 96 | |
| 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 | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 104 | |
| 105 | // Merge the globals variables (not the calls) from the globals graph back |
| 106 | // into the main function's graph so that the main function contains all of |
| 107 | // the information about global pools and GV usage in the program. |
| 108 | if (MainFunc) { |
| 109 | DSGraph &MainGraph = getOrCreateGraph(*MainFunc); |
| 110 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
| 111 | ReachabilityCloner RC(MainGraph, GG, |
| 112 | DSGraph::DontCloneCallNodes | |
| 113 | DSGraph::DontCloneAuxCallNodes); |
| 114 | |
| 115 | // Clone the global nodes into this graph. |
| 116 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 117 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 118 | if (isa<GlobalVariable>(*I)) |
| 119 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 120 | |
Chris Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 121 | MainGraph.maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 122 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
| 123 | DSGraph::IgnoreGlobals); |
| 124 | } |
| 125 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 126 | return false; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
| 130 | // buildIndirectFunctionSets - Iterate over the module looking for indirect |
| 131 | // calls to functions. If a call site can invoke any functions [F1, F2... FN], |
| 132 | // unify the N functions together in the FuncECs set. |
| 133 | // |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 134 | void EquivClassGraphs::buildIndirectFunctionSets(Module &M) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 135 | const ActualCalleesTy& AC = CBU->getActualCallees(); |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 136 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 137 | // Loop over all of the indirect calls in the program. If a call site can |
| 138 | // call multiple different functions, we need to unify all of the callees into |
| 139 | // the same equivalence class. |
| 140 | Instruction *LastInst = 0; |
| 141 | Function *FirstFunc = 0; |
| 142 | for (ActualCalleesTy::const_iterator I=AC.begin(), E=AC.end(); I != E; ++I) { |
| 143 | if (I->second->isExternal()) |
| 144 | continue; // Ignore functions we cannot modify |
| 145 | |
| 146 | CallSite CS = CallSite::get(I->first); |
| 147 | |
| 148 | if (CS.getCalledFunction()) { // Direct call: |
| 149 | FuncECs.addElement(I->second); // -- Make sure function has equiv class |
| 150 | FirstFunc = I->second; // -- First callee at this site |
| 151 | } else { // Else indirect call |
| 152 | // DEBUG(std::cerr << "CALLEE: " << I->second->getName() |
| 153 | // << " from : " << I->first); |
| 154 | if (I->first != LastInst) { |
| 155 | // This is the first callee from this call site. |
| 156 | LastInst = I->first; |
| 157 | FirstFunc = I->second; |
| 158 | // Instead of storing the lastInst For Indirection call Sites we store |
| 159 | // the DSNode for the function ptr arguemnt |
| 160 | Function *thisFunc = LastInst->getParent()->getParent(); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 161 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
| 162 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 163 | OneCalledFunction[calleeNode] = FirstFunc; |
| 164 | FuncECs.addElement(I->second); |
| 165 | } else { |
| 166 | // This is not the first possible callee from a particular call site. |
| 167 | // Union the callee in with the other functions. |
| 168 | FuncECs.unionSetsWith(FirstFunc, I->second); |
| 169 | #ifndef NDEBUG |
| 170 | Function *thisFunc = LastInst->getParent()->getParent(); |
Chris Lattner | 15d879e | 2004-10-12 16:52:09 +0000 | [diff] [blame] | 171 | DSGraph &TFG = CBU->getDSGraph(*thisFunc); |
| 172 | DSNode *calleeNode = TFG.getNodeForValue(CS.getCalledValue()).getNode(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 173 | assert(OneCalledFunction.count(calleeNode) > 0 && "Missed a call?"); |
| 174 | #endif |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Now include all functions that share a graph with any function in the |
| 179 | // equivalence class. More precisely, if F is in the class, and G(F) is |
| 180 | // its graph, then we include all other functions that are also in G(F). |
| 181 | // Currently, that is just the functions in the same call-graph-SCC as F. |
| 182 | // |
| 183 | DSGraph& funcDSGraph = CBU->getDSGraph(*I->second); |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame^] | 184 | for (DSGraph::retnodes_iterator RI = funcDSGraph.retnodes_begin(), |
| 185 | RE = funcDSGraph.retnodes_end(); RI != RE; ++RI) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 186 | FuncECs.unionSetsWith(FirstFunc, RI->first); |
| 187 | } |
| 188 | |
| 189 | // Now that all of the equivalences have been built, merge the graphs for |
| 190 | // each equivalence class. |
| 191 | // |
| 192 | std::set<Function*> &leaderSet = FuncECs.getLeaderSet(); |
| 193 | DEBUG(std::cerr << "\nIndirect Function Equivalence Sets:\n"); |
| 194 | for (std::set<Function*>::iterator LI = leaderSet.begin(), |
| 195 | LE = leaderSet.end(); LI != LE; ++LI) { |
| 196 | |
| 197 | Function* LF = *LI; |
| 198 | const std::set<Function*>& EqClass = FuncECs.getEqClass(LF); |
| 199 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 200 | if (EqClass.size() > 1) { |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 201 | #ifndef NDEBUG |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 202 | DEBUG(std::cerr <<" Equivalence set for leader " <<LF->getName()<<" = "); |
| 203 | for (std::set<Function*>::const_iterator EqI = EqClass.begin(), |
| 204 | EqEnd = EqClass.end(); EqI != EqEnd; ++EqI) |
| 205 | DEBUG(std::cerr << " " << (*EqI)->getName() << ","); |
| 206 | DEBUG(std::cerr << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 207 | #endif |
| 208 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 209 | // This equiv class has multiple functions: merge their graphs. First, |
| 210 | // clone the CBU graph for the leader and make it the common graph for the |
| 211 | // equivalence graph. |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 212 | DSGraph &MergedG = getOrCreateGraph(*LF); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 214 | // Record the argument nodes for use in merging later below. |
| 215 | std::vector<DSNodeHandle> ArgNodes; |
| 216 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 217 | for (Function::arg_iterator AI1 = LF->arg_begin(); AI1 != LF->arg_end(); ++AI1) |
Chris Lattner | f498568 | 2004-10-31 18:13:19 +0000 | [diff] [blame] | 218 | if (DS::isPointerType(AI1->getType())) |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 219 | ArgNodes.push_back(MergedG.getNodeForValue(AI1)); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 220 | |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 221 | // Merge in the graphs of all other functions in this equiv. class. Note |
| 222 | // that two or more functions may have the same graph, and it only needs |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 223 | // to be merged in once. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 224 | std::set<DSGraph*> GraphsMerged; |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 225 | GraphsMerged.insert(&CBU->getDSGraph(*LF)); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 227 | for (std::set<Function*>::const_iterator EqI = EqClass.begin(), |
| 228 | E = EqClass.end(); EqI != E; ++EqI) { |
| 229 | Function *F = *EqI; |
| 230 | DSGraph *&FG = DSInfo[F]; |
| 231 | |
| 232 | DSGraph &CBUGraph = CBU->getDSGraph(*F); |
| 233 | if (!GraphsMerged.insert(&CBUGraph).second) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 234 | continue; |
| 235 | |
| 236 | // Record the "folded" graph for the function. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame^] | 237 | for (DSGraph::retnodes_iterator I = CBUGraph.retnodes_begin(), |
| 238 | E = CBUGraph.retnodes_end(); I != E; ++I) { |
Chris Lattner | f1de30a | 2004-11-02 20:31:06 +0000 | [diff] [blame] | 239 | assert(DSInfo[I->first] == 0 && "Graph already exists for Fn!"); |
| 240 | DSInfo[I->first] = &MergedG; |
| 241 | } |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 243 | // Clone this member of the equivalence class into MergedG. |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 244 | DSGraph::NodeMapTy NodeMap; |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 246 | MergedG.cloneInto(CBUGraph, MergedG.getScalarMap(), |
Chris Lattner | f1de30a | 2004-11-02 20:31:06 +0000 | [diff] [blame] | 247 | MergedG.getReturnNodes(), NodeMap, 0); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 248 | |
| 249 | // Merge the return nodes of all functions together. |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 250 | MergedG.getReturnNodes()[LF].mergeWith(MergedG.getReturnNodes()[F]); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 251 | |
| 252 | // Merge the function arguments with all argument nodes found so far. |
| 253 | // If there are extra function args, add them to the vector of argNodes |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 254 | Function::arg_iterator AI2 = F->arg_begin(), AI2end = F->arg_end(); |
Chris Lattner | 77408b8 | 2004-11-01 20:37:00 +0000 | [diff] [blame] | 255 | for (unsigned arg=0, numArgs = ArgNodes.size(); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 256 | arg != numArgs && AI2 != AI2end; ++AI2, ++arg) |
| 257 | if (DS::isPointerType(AI2->getType())) |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 258 | ArgNodes[arg].mergeWith(MergedG.getNodeForValue(AI2)); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 259 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 260 | for ( ; AI2 != AI2end; ++AI2) |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 261 | if (DS::isPointerType(AI2->getType())) |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 262 | ArgNodes.push_back(MergedG.getNodeForValue(AI2)); |
| 263 | DEBUG(MergedG.AssertGraphOK()); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | DEBUG(std::cerr << "\n"); |
| 268 | } |
| 269 | |
| 270 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 271 | DSGraph &EquivClassGraphs::getOrCreateGraph(Function &F) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 272 | // Has the graph already been created? |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 273 | DSGraph *&Graph = DSInfo[&F]; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 274 | if (Graph) return *Graph; |
| 275 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 276 | DSGraph &CBUGraph = CBU->getDSGraph(F); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 277 | |
| 278 | // Copy the CBU graph... |
| 279 | Graph = new DSGraph(CBUGraph); // updates the map via reference |
| 280 | Graph->setGlobalsGraph(&getGlobalsGraph()); |
| 281 | Graph->setPrintAuxCalls(); |
| 282 | |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 283 | // 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^] | 284 | for (DSGraph::retnodes_iterator I = Graph->retnodes_begin(); |
| 285 | I != Graph->retnodes_end(); ++I) |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 286 | if (I->first != &F) { |
Chris Lattner | fcb7d95 | 2004-11-01 21:02:23 +0000 | [diff] [blame] | 287 | DSGraph *&FG = DSInfo[I->first]; |
Chris Lattner | 983baf4 | 2004-11-02 06:38:58 +0000 | [diff] [blame] | 288 | assert(FG == 0 && "Merging function in SCC twice?"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 289 | FG = Graph; |
| 290 | } |
| 291 | |
Chris Lattner | 68f9658 | 2004-11-01 19:54:06 +0000 | [diff] [blame] | 292 | return *Graph; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Chris Lattner | b25959a | 2005-03-12 12:08:52 +0000 | [diff] [blame] | 296 | unsigned EquivClassGraphs:: |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 297 | processSCC(DSGraph &FG, std::vector<DSGraph*> &Stack, unsigned &NextID, |
| 298 | std::map<DSGraph*, unsigned> &ValMap) { |
| 299 | std::map<DSGraph*, unsigned>::iterator It = ValMap.lower_bound(&FG); |
| 300 | if (It != ValMap.end() && It->first == &FG) |
Chris Lattner | 4bbf3df | 2004-10-31 23:01:34 +0000 | [diff] [blame] | 301 | return It->second; |
| 302 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 303 | DEBUG(std::cerr << " ProcessSCC for function " << FG.getFunctionNames() |
| 304 | << "\n"); |
| 305 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 306 | unsigned Min = NextID++, MyID = Min; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 307 | ValMap[&FG] = Min; |
| 308 | Stack.push_back(&FG); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 309 | |
| 310 | // The edges out of the current node are the call site targets... |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 311 | for (DSGraph::fc_iterator CI = FG.fc_begin(), CE = FG.fc_end(); |
| 312 | CI != CE; ++CI) { |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 313 | Instruction *Call = CI->getCallSite().getInstruction(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 314 | |
| 315 | // Loop over all of the actually called functions... |
| 316 | ActualCalleesTy::const_iterator I, E; |
| 317 | for (tie(I, E) = getActualCallees().equal_range(Call); I != E; ++I) |
| 318 | if (!I->second->isExternal()) { |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 319 | // Process the callee as necessary. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 320 | unsigned M = processSCC(getOrCreateGraph(*I->second), |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 321 | Stack, NextID, ValMap); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 322 | if (M < Min) Min = M; |
| 323 | } |
| 324 | } |
| 325 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 326 | assert(ValMap[&FG] == MyID && "SCC construction assumption wrong!"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 327 | if (Min != MyID) |
| 328 | return Min; // This is part of a larger SCC! |
| 329 | |
| 330 | // If this is a new SCC, process it now. |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 331 | bool MergedGraphs = false; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 332 | while (Stack.back() != &FG) { |
| 333 | DSGraph *NG = Stack.back(); |
| 334 | ValMap[NG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 335 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 336 | // If the SCC found is not the same as those found in CBU, make sure to |
| 337 | // merge the graphs as appropriate. |
| 338 | DSGraph::NodeMapTy NodeMap; |
| 339 | FG.cloneInto(*NG, FG.getScalarMap(), FG.getReturnNodes(), NodeMap); |
| 340 | |
| 341 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame^] | 342 | for (DSGraph::retnodes_iterator I = NG->retnodes_begin(); |
| 343 | I != NG->retnodes_end(); ++I) |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 344 | DSInfo[I->first] = &FG; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 345 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 346 | // Remove NG from the ValMap since the pointer may get recycled. |
| 347 | ValMap.erase(NG); |
| 348 | delete NG; |
| 349 | MergedGraphs = true; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 350 | Stack.pop_back(); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Chris Lattner | caa35bc | 2004-11-02 19:29:59 +0000 | [diff] [blame] | 353 | // Clean up the graph before we start inlining a bunch again. |
| 354 | if (MergedGraphs) |
| 355 | FG.removeTriviallyDeadNodes(); |
| 356 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 357 | Stack.pop_back(); |
Chris Lattner | 113cde8 | 2004-10-31 17:47:48 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 359 | processGraph(FG); |
| 360 | ValMap[&FG] = ~0U; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 361 | return MyID; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | /// processGraph - Process the CBU graphs for the program in bottom-up order on |
| 366 | /// 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] | 367 | void EquivClassGraphs::processGraph(DSGraph &G) { |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 368 | DEBUG(std::cerr << " ProcessGraph for function " |
| 369 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 370 | |
| 371 | hash_set<Instruction*> calls; |
| 372 | |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 373 | // Else we need to inline some callee graph. Visit all call sites. |
| 374 | // The edges out of the current node are the call site targets... |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 375 | unsigned i = 0; |
Chris Lattner | 5d85f8f | 2005-03-15 06:29:12 +0000 | [diff] [blame] | 376 | 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] | 377 | ++CI, ++i) { |
| 378 | const DSCallSite &CS = *CI; |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 379 | Instruction *TheCall = CS.getCallSite().getInstruction(); |
| 380 | |
| 381 | assert(calls.insert(TheCall).second && |
| 382 | "Call instruction occurs multiple times in graph??"); |
| 383 | |
| 384 | // Inline the common callee graph into the current graph, if the callee |
| 385 | // graph has not changed. Note that all callees should have the same |
| 386 | // graph so we only need to do this once. |
| 387 | // |
| 388 | DSGraph* CalleeGraph = NULL; |
| 389 | ActualCalleesTy::const_iterator I, E; |
| 390 | tie(I, E) = getActualCallees().equal_range(TheCall); |
| 391 | unsigned TNum, Num; |
| 392 | |
| 393 | // Loop over all potential callees to find the first non-external callee. |
| 394 | for (TNum = 0, Num = std::distance(I, E); I != E; ++I, ++TNum) |
| 395 | if (!I->second->isExternal()) |
| 396 | break; |
| 397 | |
| 398 | // 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] | 399 | if (I != E) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 400 | Function *CalleeFunc = I->second; |
| 401 | |
| 402 | // Merge the callee's graph into this graph, if not already the same. |
| 403 | // Callees in the same equivalence class (which subsumes those |
| 404 | // in the same SCCs) have the same graph. Note that all recursion |
| 405 | // including self-recursion have been folded in the equiv classes. |
| 406 | // |
| 407 | CalleeGraph = &getOrCreateGraph(*CalleeFunc); |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 408 | if (CalleeGraph != &G) { |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 409 | ++NumFoldGraphInlines; |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 410 | G.mergeInGraph(CS, *CalleeFunc, *CalleeGraph, |
| 411 | DSGraph::KeepModRefBits | DSGraph::StripAllocaBit | |
| 412 | DSGraph::DontCloneCallNodes | |
| 413 | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | 6538f42 | 2005-01-30 23:51:25 +0000 | [diff] [blame] | 414 | DEBUG(std::cerr << " Inlining graph [" << i << "/" |
| 415 | << G.getFunctionCalls().size()-1 |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 416 | << ":" << TNum << "/" << Num-1 << "] for " |
| 417 | << CalleeFunc->getName() << "[" |
| 418 | << CalleeGraph->getGraphSize() << "+" |
| 419 | << CalleeGraph->getAuxFunctionCalls().size() |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 420 | << "] into '" /*<< G.getFunctionNames()*/ << "' [" |
| 421 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 422 | << "]\n"); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | #ifndef NDEBUG |
| 427 | // Now loop over the rest of the callees and make sure they have the |
| 428 | // same graph as the one inlined above. |
| 429 | if (CalleeGraph) |
| 430 | for (++I, ++TNum; I != E; ++I, ++TNum) |
| 431 | if (!I->second->isExternal()) |
| 432 | assert(CalleeGraph == &getOrCreateGraph(*I->second) && |
| 433 | "Callees at a call site have different graphs?"); |
| 434 | #endif |
| 435 | } |
| 436 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 437 | // Recompute the Incomplete markers. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 438 | assert(G.getInlinedGlobals().empty()); |
| 439 | G.maskIncompleteMarkers(); |
| 440 | G.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 441 | |
| 442 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 443 | // reach live nodes as live. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 444 | G.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 445 | |
| 446 | // When this graph is finalized, clone the globals in the graph into the |
| 447 | // globals graph to make sure it has everything, from all graphs. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 448 | ReachabilityCloner RC(*G.getGlobalsGraph(), G, DSGraph::StripAllocaBit); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 449 | |
| 450 | // Clone everything reachable from globals in the function graph into the |
| 451 | // globals graph. |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 452 | DSScalarMap &MainSM = G.getScalarMap(); |
Chris Lattner | ab8544a | 2004-10-31 21:56:11 +0000 | [diff] [blame] | 453 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
| 454 | E = MainSM.global_end(); I != E; ++I) |
| 455 | RC.getClonedNH(MainSM[*I]); |
| 456 | |
Chris Lattner | 31d3f67 | 2004-10-31 23:41:26 +0000 | [diff] [blame] | 457 | DEBUG(std::cerr << " -- DONE ProcessGraph for function " |
Chris Lattner | 033a7d5 | 2004-11-02 17:51:11 +0000 | [diff] [blame] | 458 | << G.getFunctionNames() << "\n"); |
Vikram S. Adve | c5204fb | 2004-05-23 07:54:02 +0000 | [diff] [blame] | 459 | } |