| Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// | 
| John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 9 | // | 
|  | 10 | // This file implements the BUDataStructures class, which represents the | 
|  | 11 | // Bottom-Up Interprocedural closure of the data structure graph over the | 
|  | 12 | // program.  This is useful for applications like pool allocation, but **not** | 
| Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 13 | // applications like alias analysis. | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | // | 
|  | 15 | //===----------------------------------------------------------------------===// | 
|  | 16 |  | 
|  | 17 | #include "llvm/Analysis/DataStructure.h" | 
|  | 18 | #include "llvm/Module.h" | 
| Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 19 | #include "Support/Statistic.h" | 
| Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 20 | #include "Support/Debug.h" | 
| Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 21 | #include "DSCallSiteIterator.h" | 
| Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 22 | using namespace llvm; | 
| Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 |  | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 24 | namespace { | 
|  | 25 | Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph"); | 
| Chris Lattner | d391d70 | 2003-07-02 20:24:42 +0000 | [diff] [blame] | 26 | Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined"); | 
| Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 27 | Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges"); | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | RegisterAnalysis<BUDataStructures> | 
| Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 30 | X("budatastructure", "Bottom-up Data Structure Analysis"); | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 31 | } | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 32 |  | 
| Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 33 | using namespace DS; | 
| Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 34 |  | 
| Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 35 | // run - Calculate the bottom up data structure graphs for each function in the | 
|  | 36 | // program. | 
|  | 37 | // | 
|  | 38 | bool BUDataStructures::run(Module &M) { | 
| Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 39 | LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>(); | 
|  | 40 | GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph()); | 
| Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 41 | GlobalsGraph->setPrintAuxCalls(); | 
| Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 42 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 43 | Function *MainFunc = M.getMainFunction(); | 
|  | 44 | if (MainFunc) | 
|  | 45 | calculateReachableGraphs(MainFunc); | 
|  | 46 |  | 
|  | 47 | // Calculate the graphs for any functions that are unreachable from main... | 
| Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 48 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) | 
| Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 49 | if (!I->isExternal() && !DSInfo.count(I)) { | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 50 | #ifndef NDEBUG | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 51 | if (MainFunc) | 
|  | 52 | std::cerr << "*** Function unreachable from main: " | 
|  | 53 | << I->getName() << "\n"; | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 54 | #endif | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 55 | calculateReachableGraphs(I);    // Calculate all graphs... | 
|  | 56 | } | 
| Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 57 |  | 
|  | 58 | NumCallEdges += ActualCallees.size(); | 
| Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 59 |  | 
| Chris Lattner | 28a631d | 2004-02-17 19:06:47 +0000 | [diff] [blame^] | 60 |  | 
|  | 61 | // At the end of the BU phase, clone the BU graph for main into the globals | 
|  | 62 | // graph to make sure it has everything. | 
|  | 63 | if (MainFunc) { | 
|  | 64 | DSGraph &MainGraph = getOrCreateGraph(MainFunc); | 
|  | 65 | DSScalarMap &MainSM = MainGraph.getScalarMap(); | 
|  | 66 | ReachabilityCloner RC(*GlobalsGraph, MainGraph, DSGraph::StripAllocaBit); | 
|  | 67 |  | 
|  | 68 | // Clone everything reachable from globals in the "main" graph into the | 
|  | 69 | // globals graph. | 
|  | 70 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), | 
|  | 71 | E = MainSM.global_end(); I != E; ++I) | 
|  | 72 | RC.getClonedNH(MainSM[*I]); | 
|  | 73 |  | 
|  | 74 |  | 
|  | 75 | } | 
|  | 76 |  | 
| Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 77 | // At the end of the bottom-up pass, the globals graph becomes complete. | 
|  | 78 | // FIXME: This is not the right way to do this, but it is sorta better than | 
| Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 79 | // nothing!  In particular, externally visible globals and unresolvable call | 
|  | 80 | // nodes at the end of the BU phase should make things that they point to | 
|  | 81 | // incomplete in the globals graph. | 
|  | 82 | // | 
| Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 83 | GlobalsGraph->removeTriviallyDeadNodes(); | 
| Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 84 | GlobalsGraph->maskIncompleteMarkers(); | 
| Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 85 | return false; | 
|  | 86 | } | 
| Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 87 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 88 | void BUDataStructures::calculateReachableGraphs(Function *F) { | 
|  | 89 | std::vector<Function*> Stack; | 
| Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 90 | hash_map<Function*, unsigned> ValMap; | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 91 | unsigned NextID = 1; | 
|  | 92 | calculateGraphs(F, Stack, NextID, ValMap); | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { | 
|  | 96 | // Has the graph already been created? | 
|  | 97 | DSGraph *&Graph = DSInfo[F]; | 
|  | 98 | if (Graph) return *Graph; | 
|  | 99 |  | 
|  | 100 | // Copy the local version into DSInfo... | 
|  | 101 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F)); | 
|  | 102 |  | 
|  | 103 | Graph->setGlobalsGraph(GlobalsGraph); | 
|  | 104 | Graph->setPrintAuxCalls(); | 
|  | 105 |  | 
|  | 106 | // Start with a copy of the original call sites... | 
|  | 107 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); | 
|  | 108 | return *Graph; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | unsigned BUDataStructures::calculateGraphs(Function *F, | 
|  | 112 | std::vector<Function*> &Stack, | 
|  | 113 | unsigned &NextID, | 
| Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 114 | hash_map<Function*, unsigned> &ValMap) { | 
| Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 115 | assert(!ValMap.count(F) && "Shouldn't revisit functions!"); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 116 | unsigned Min = NextID++, MyID = Min; | 
|  | 117 | ValMap[F] = Min; | 
|  | 118 | Stack.push_back(F); | 
|  | 119 |  | 
|  | 120 | if (F->isExternal()) {   // sprintf, fprintf, sscanf, etc... | 
|  | 121 | // No callees! | 
|  | 122 | Stack.pop_back(); | 
|  | 123 | ValMap[F] = ~0; | 
|  | 124 | return Min; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | DSGraph &Graph = getOrCreateGraph(F); | 
|  | 128 |  | 
|  | 129 | // The edges out of the current node are the call site targets... | 
| Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 130 | for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph), | 
|  | 131 | E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) { | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 132 | Function *Callee = *I; | 
|  | 133 | unsigned M; | 
|  | 134 | // Have we visited the destination function yet? | 
| Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 135 | hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 136 | if (It == ValMap.end())  // No, visit it now. | 
|  | 137 | M = calculateGraphs(Callee, Stack, NextID, ValMap); | 
|  | 138 | else                    // Yes, get it's number. | 
|  | 139 | M = It->second; | 
|  | 140 | if (M < Min) Min = M; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); | 
|  | 144 | if (Min != MyID) | 
|  | 145 | return Min;         // This is part of a larger SCC! | 
|  | 146 |  | 
|  | 147 | // If this is a new SCC, process it now. | 
|  | 148 | if (Stack.back() == F) {           // Special case the single "SCC" case here. | 
|  | 149 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " | 
|  | 150 | << F->getName() << "\n"); | 
|  | 151 | Stack.pop_back(); | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 152 | DSGraph &G = getDSGraph(*F); | 
|  | 153 | DEBUG(std::cerr << "  [BU] Calculating graph for: " << F->getName()<< "\n"); | 
|  | 154 | calculateGraph(G); | 
|  | 155 | DEBUG(std::cerr << "  [BU] Done inlining: " << F->getName() << " [" | 
|  | 156 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() | 
|  | 157 | << "]\n"); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 158 |  | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 159 | if (MaxSCC < 1) MaxSCC = 1; | 
|  | 160 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 161 | // Should we revisit the graph? | 
| Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 162 | if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) { | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 163 | ValMap.erase(F); | 
|  | 164 | return calculateGraphs(F, Stack, NextID, ValMap); | 
|  | 165 | } else { | 
|  | 166 | ValMap[F] = ~0U; | 
|  | 167 | } | 
|  | 168 | return MyID; | 
|  | 169 |  | 
|  | 170 | } else { | 
|  | 171 | // SCCFunctions - Keep track of the functions in the current SCC | 
|  | 172 | // | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 173 | hash_set<DSGraph*> SCCGraphs; | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 174 |  | 
|  | 175 | Function *NF; | 
|  | 176 | std::vector<Function*>::iterator FirstInSCC = Stack.end(); | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 177 | DSGraph *SCCGraph = 0; | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 178 | do { | 
|  | 179 | NF = *--FirstInSCC; | 
|  | 180 | ValMap[NF] = ~0U; | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 181 |  | 
|  | 182 | // Figure out which graph is the largest one, in order to speed things up | 
|  | 183 | // a bit in situations where functions in the SCC have widely different | 
|  | 184 | // graph sizes. | 
|  | 185 | DSGraph &NFGraph = getDSGraph(*NF); | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 186 | SCCGraphs.insert(&NFGraph); | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 187 | if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize()) | 
|  | 188 | SCCGraph = &NFGraph; | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 189 | } while (NF != F); | 
|  | 190 |  | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 191 | std::cerr << "Calculating graph for SCC #: " << MyID << " of size: " | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 192 | << SCCGraphs.size() << "\n"; | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 193 |  | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 194 | // Compute the Max SCC Size... | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 195 | if (MaxSCC < SCCGraphs.size()) | 
|  | 196 | MaxSCC = SCCGraphs.size(); | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 197 |  | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 198 | // First thing first, collapse all of the DSGraphs into a single graph for | 
|  | 199 | // the entire SCC.  We computed the largest graph, so clone all of the other | 
|  | 200 | // (smaller) graphs into it.  Discard all of the old graphs. | 
|  | 201 | // | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 202 | for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(), | 
|  | 203 | E = SCCGraphs.end(); I != E; ++I) { | 
|  | 204 | DSGraph &G = **I; | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 205 | if (&G != SCCGraph) { | 
|  | 206 | DSGraph::NodeMapTy NodeMap; | 
|  | 207 | SCCGraph->cloneInto(G, SCCGraph->getScalarMap(), | 
| Chris Lattner | 825a02a | 2004-01-27 21:50:41 +0000 | [diff] [blame] | 208 | SCCGraph->getReturnNodes(), NodeMap); | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 209 | // Update the DSInfo map and delete the old graph... | 
| Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 210 | for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(), | 
|  | 211 | E = G.getReturnNodes().end(); I != E; ++I) | 
|  | 212 | DSInfo[I->first] = SCCGraph; | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 213 | delete &G; | 
|  | 214 | } | 
|  | 215 | } | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 216 |  | 
| Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 217 | // Clean up the graph before we start inlining a bunch again... | 
| Chris Lattner | 825a02a | 2004-01-27 21:50:41 +0000 | [diff] [blame] | 218 | SCCGraph->removeDeadNodes(DSGraph::RemoveUnreachableGlobals); | 
| Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 219 |  | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 220 | // Now that we have one big happy family, resolve all of the call sites in | 
|  | 221 | // the graph... | 
|  | 222 | calculateGraph(*SCCGraph); | 
|  | 223 | DEBUG(std::cerr << "  [BU] Done inlining SCC  [" << SCCGraph->getGraphSize() | 
|  | 224 | << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n"); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 225 |  | 
|  | 226 | std::cerr << "DONE with SCC #: " << MyID << "\n"; | 
|  | 227 |  | 
|  | 228 | // We never have to revisit "SCC" processed functions... | 
|  | 229 |  | 
|  | 230 | // Drop the stuff we don't need from the end of the stack | 
|  | 231 | Stack.erase(FirstInSCC, Stack.end()); | 
|  | 232 | return MyID; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | return MyID;  // == Min | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 |  | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 239 | // releaseMemory - If the pass pipeline is done with this pass, we can release | 
|  | 240 | // our memory... here... | 
|  | 241 | // | 
|  | 242 | void BUDataStructures::releaseMemory() { | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 243 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), | 
|  | 244 | E = DSInfo.end(); I != E; ++I) { | 
|  | 245 | I->second->getReturnNodes().erase(I->first); | 
|  | 246 | if (I->second->getReturnNodes().empty()) | 
|  | 247 | delete I->second; | 
|  | 248 | } | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 249 |  | 
|  | 250 | // Empty map so next time memory is released, data structures are not | 
|  | 251 | // re-deleted. | 
|  | 252 | DSInfo.clear(); | 
| Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 253 | delete GlobalsGraph; | 
|  | 254 | GlobalsGraph = 0; | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 255 | } | 
|  | 256 |  | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 257 | void BUDataStructures::calculateGraph(DSGraph &Graph) { | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 258 | // Move our call site list into TempFCs so that inline call sites go into the | 
|  | 259 | // new call site list and doesn't invalidate our iterators! | 
|  | 260 | std::vector<DSCallSite> TempFCs; | 
|  | 261 | std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); | 
|  | 262 | TempFCs.swap(AuxCallsList); | 
| Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 263 |  | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 264 | DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes(); | 
|  | 265 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 266 | // Loop over all of the resolvable call sites | 
|  | 267 | unsigned LastCallSiteIdx = ~0U; | 
| Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 268 | for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs), | 
|  | 269 | E = DSCallSiteIterator::end(TempFCs); I != E; ++I) { | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 270 | // If we skipped over any call sites, they must be unresolvable, copy them | 
|  | 271 | // to the real call site list. | 
|  | 272 | LastCallSiteIdx++; | 
|  | 273 | for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx) | 
|  | 274 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); | 
|  | 275 | LastCallSiteIdx = I.getCallSiteIdx(); | 
| Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 276 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 277 | // Resolve the current call... | 
|  | 278 | Function *Callee = *I; | 
| Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 279 | DSCallSite CS = I.getCallSite(); | 
| Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 280 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 281 | if (Callee->isExternal()) { | 
|  | 282 | // Ignore this case, simple varargs functions we cannot stub out! | 
| Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 283 | } else if (ReturnNodes.count(Callee)) { | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 284 | // Self recursion... simply link up the formal arguments with the | 
|  | 285 | // actual arguments... | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 286 | DEBUG(std::cerr << "    Self Inlining: " << Callee->getName() << "\n"); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 287 |  | 
|  | 288 | // Handle self recursion by resolving the arguments and return value | 
| Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 289 | Graph.mergeInGraph(CS, *Callee, Graph, 0); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | } else { | 
| Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 292 | ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(), | 
|  | 293 | Callee)); | 
| Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 294 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 295 | // Get the data structure graph for the called function. | 
|  | 296 | // | 
|  | 297 | DSGraph &GI = getDSGraph(*Callee);  // Graph to inline | 
|  | 298 |  | 
|  | 299 | DEBUG(std::cerr << "    Inlining graph for " << Callee->getName() | 
| Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 300 | << "[" << GI.getGraphSize() << "+" | 
| Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 301 | << GI.getAuxFunctionCalls().size() << "] into '" | 
|  | 302 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+" | 
| Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 303 | << Graph.getAuxFunctionCalls().size() << "]\n"); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 304 |  | 
| Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 305 | Graph.mergeInGraph(CS, *Callee, GI, | 
| Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 306 | DSGraph::KeepModRefBits | | 
|  | 307 | DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes); | 
| Chris Lattner | d391d70 | 2003-07-02 20:24:42 +0000 | [diff] [blame] | 308 | ++NumBUInlines; | 
| Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 309 |  | 
|  | 310 | #if 0 | 
|  | 311 | Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" + | 
|  | 312 | Callee->getName()); | 
|  | 313 | #endif | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 314 | } | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | // Make sure to catch any leftover unresolvable calls... | 
|  | 318 | for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx) | 
|  | 319 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); | 
|  | 320 |  | 
|  | 321 | TempFCs.clear(); | 
|  | 322 |  | 
| Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 323 | // Re-materialize nodes from the globals graph. | 
|  | 324 | // Do not ignore globals inlined from callees -- they are not up-to-date! | 
| Chris Lattner | 825a02a | 2004-01-27 21:50:41 +0000 | [diff] [blame] | 325 | assert(Graph.getInlinedGlobals().empty()); | 
| Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 326 | Graph.updateFromGlobalGraph(); | 
|  | 327 |  | 
|  | 328 | // Recompute the Incomplete markers | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 329 | Graph.maskIncompleteMarkers(); | 
| Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 330 | Graph.markIncompleteNodes(DSGraph::MarkFormalArgs); | 
| Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 331 |  | 
|  | 332 | // Delete dead nodes.  Treat globals that are unreachable but that can | 
|  | 333 | // reach live nodes as live. | 
| Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 334 | Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 335 |  | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 336 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); | 
| Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 337 | } |