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