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