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