Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // This file implements the BUDataStructures class, which represents the |
| 4 | // Bottom-Up Interprocedural closure of the data structure graph over the |
| 5 | // program. This is useful for applications like pool allocation, but **not** |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 6 | // applications like alias analysis. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/DSGraph.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 12 | #include "llvm/Module.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 13 | #include "Support/Statistic.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph"); |
| 17 | |
| 18 | RegisterAnalysis<BUDataStructures> |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 19 | X("budatastructure", "Bottom-up Data Structure Analysis"); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 20 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 21 | |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 22 | using namespace DS; |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 24 | static bool isVAHackFn(const Function *F) { |
| 25 | return F->getName() == "printf" || F->getName() == "sscanf" || |
| 26 | F->getName() == "fprintf" || F->getName() == "open" || |
| 27 | F->getName() == "sprintf" || F->getName() == "fputs" || |
| 28 | F->getName() == "fscanf"; |
| 29 | } |
| 30 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 31 | // isCompleteNode - Return true if we know all of the targets of this node, and |
| 32 | // if the call sites are not external. |
| 33 | // |
| 34 | static inline bool isCompleteNode(DSNode *N) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 35 | if (N->isIncomplete()) return false; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 36 | const std::vector<GlobalValue*> &Callees = N->getGlobals(); |
| 37 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 38 | if (Callees[i]->isExternal()) |
| 39 | if (!isVAHackFn(cast<Function>(Callees[i]))) |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 40 | return false; // External function found... |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 41 | return true; // otherwise ok |
| 42 | } |
| 43 | |
| 44 | struct CallSiteIterator { |
| 45 | // FCs are the edges out of the current node are the call site targets... |
| 46 | std::vector<DSCallSite> *FCs; |
| 47 | unsigned CallSite; |
| 48 | unsigned CallSiteEntry; |
| 49 | |
| 50 | CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) { |
| 51 | CallSite = 0; CallSiteEntry = 0; |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 52 | advanceToValidCallee(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // End iterator ctor... |
| 56 | CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) { |
| 57 | CallSite = FCs->size(); CallSiteEntry = 0; |
| 58 | } |
| 59 | |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 60 | void advanceToValidCallee() { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 61 | while (CallSite < FCs->size()) { |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 62 | if ((*FCs)[CallSite].isDirectCall()) { |
| 63 | if (CallSiteEntry == 0 && // direct call only has one target... |
| 64 | (!(*FCs)[CallSite].getCalleeFunc()->isExternal() || |
| 65 | isVAHackFn((*FCs)[CallSite].getCalleeFunc()))) // If not external |
| 66 | return; |
| 67 | } else { |
| 68 | DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 69 | if (CallSiteEntry || isCompleteNode(CalleeNode)) { |
| 70 | const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals(); |
| 71 | |
| 72 | if (CallSiteEntry < Callees.size()) |
| 73 | return; |
| 74 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 76 | CallSiteEntry = 0; |
| 77 | ++CallSite; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | public: |
| 81 | static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); } |
| 82 | static CallSiteIterator end(DSGraph &G) { |
| 83 | return CallSiteIterator(G.getAuxFunctionCalls(), true); |
| 84 | } |
| 85 | static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; } |
| 86 | static CallSiteIterator end(std::vector<DSCallSite> &CSs) { |
| 87 | return CallSiteIterator(CSs, true); |
| 88 | } |
| 89 | bool operator==(const CallSiteIterator &CSI) const { |
| 90 | return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry; |
| 91 | } |
| 92 | bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);} |
| 93 | |
| 94 | unsigned getCallSiteIdx() const { return CallSite; } |
| 95 | DSCallSite &getCallSite() const { return (*FCs)[CallSite]; } |
| 96 | |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 97 | Function *operator*() const { |
| 98 | if ((*FCs)[CallSite].isDirectCall()) { |
| 99 | return (*FCs)[CallSite].getCalleeFunc(); |
| 100 | } else { |
| 101 | DSNode *Node = (*FCs)[CallSite].getCalleeNode(); |
| 102 | return cast<Function>(Node->getGlobals()[CallSiteEntry]); |
| 103 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | CallSiteIterator& operator++() { // Preincrement |
| 107 | ++CallSiteEntry; |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 108 | advanceToValidCallee(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 109 | return *this; |
| 110 | } |
| 111 | CallSiteIterator operator++(int) { // Postincrement |
| 112 | CallSiteIterator tmp = *this; ++*this; return tmp; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | |
| 117 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 118 | // run - Calculate the bottom up data structure graphs for each function in the |
| 119 | // program. |
| 120 | // |
| 121 | bool BUDataStructures::run(Module &M) { |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 122 | LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>(); |
| 123 | GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph()); |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 124 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 125 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 126 | Function *MainFunc = M.getMainFunction(); |
| 127 | if (MainFunc) |
| 128 | calculateReachableGraphs(MainFunc); |
| 129 | |
| 130 | // Calculate the graphs for any functions that are unreachable from main... |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 131 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 132 | if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) { |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 133 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 134 | if (MainFunc) |
| 135 | std::cerr << "*** Function unreachable from main: " |
| 136 | << I->getName() << "\n"; |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 137 | #endif |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 138 | calculateReachableGraphs(I); // Calculate all graphs... |
| 139 | } |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 140 | return false; |
| 141 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 142 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 143 | void BUDataStructures::calculateReachableGraphs(Function *F) { |
| 144 | std::vector<Function*> Stack; |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 145 | hash_map<Function*, unsigned> ValMap; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 146 | unsigned NextID = 1; |
| 147 | calculateGraphs(F, Stack, NextID, ValMap); |
| 148 | } |
| 149 | |
| 150 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { |
| 151 | // Has the graph already been created? |
| 152 | DSGraph *&Graph = DSInfo[F]; |
| 153 | if (Graph) return *Graph; |
| 154 | |
| 155 | // Copy the local version into DSInfo... |
| 156 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F)); |
| 157 | |
| 158 | Graph->setGlobalsGraph(GlobalsGraph); |
| 159 | Graph->setPrintAuxCalls(); |
| 160 | |
| 161 | // Start with a copy of the original call sites... |
| 162 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); |
| 163 | return *Graph; |
| 164 | } |
| 165 | |
| 166 | unsigned BUDataStructures::calculateGraphs(Function *F, |
| 167 | std::vector<Function*> &Stack, |
| 168 | unsigned &NextID, |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 169 | hash_map<Function*, unsigned> &ValMap) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 170 | assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!"); |
| 171 | unsigned Min = NextID++, MyID = Min; |
| 172 | ValMap[F] = Min; |
| 173 | Stack.push_back(F); |
| 174 | |
| 175 | if (F->isExternal()) { // sprintf, fprintf, sscanf, etc... |
| 176 | // No callees! |
| 177 | Stack.pop_back(); |
| 178 | ValMap[F] = ~0; |
| 179 | return Min; |
| 180 | } |
| 181 | |
| 182 | DSGraph &Graph = getOrCreateGraph(F); |
| 183 | |
| 184 | // The edges out of the current node are the call site targets... |
| 185 | for (CallSiteIterator I = CallSiteIterator::begin(Graph), |
| 186 | E = CallSiteIterator::end(Graph); I != E; ++I) { |
| 187 | Function *Callee = *I; |
| 188 | unsigned M; |
| 189 | // Have we visited the destination function yet? |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 190 | hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 191 | if (It == ValMap.end()) // No, visit it now. |
| 192 | M = calculateGraphs(Callee, Stack, NextID, ValMap); |
| 193 | else // Yes, get it's number. |
| 194 | M = It->second; |
| 195 | if (M < Min) Min = M; |
| 196 | } |
| 197 | |
| 198 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); |
| 199 | if (Min != MyID) |
| 200 | return Min; // This is part of a larger SCC! |
| 201 | |
| 202 | // If this is a new SCC, process it now. |
| 203 | if (Stack.back() == F) { // Special case the single "SCC" case here. |
| 204 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " |
| 205 | << F->getName() << "\n"); |
| 206 | Stack.pop_back(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 207 | DSGraph &G = getDSGraph(*F); |
| 208 | DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n"); |
| 209 | calculateGraph(G); |
| 210 | DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " [" |
| 211 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
| 212 | << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 213 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 214 | if (MaxSCC < 1) MaxSCC = 1; |
| 215 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 216 | // Should we revisit the graph? |
| 217 | if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) { |
| 218 | ValMap.erase(F); |
| 219 | return calculateGraphs(F, Stack, NextID, ValMap); |
| 220 | } else { |
| 221 | ValMap[F] = ~0U; |
| 222 | } |
| 223 | return MyID; |
| 224 | |
| 225 | } else { |
| 226 | // SCCFunctions - Keep track of the functions in the current SCC |
| 227 | // |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 228 | hash_set<Function*> SCCFunctions; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 229 | |
| 230 | Function *NF; |
| 231 | std::vector<Function*>::iterator FirstInSCC = Stack.end(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 232 | DSGraph *SCCGraph = 0; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 233 | do { |
| 234 | NF = *--FirstInSCC; |
| 235 | ValMap[NF] = ~0U; |
| 236 | SCCFunctions.insert(NF); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 237 | |
| 238 | // Figure out which graph is the largest one, in order to speed things up |
| 239 | // a bit in situations where functions in the SCC have widely different |
| 240 | // graph sizes. |
| 241 | DSGraph &NFGraph = getDSGraph(*NF); |
| 242 | if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize()) |
| 243 | SCCGraph = &NFGraph; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 244 | } while (NF != F); |
| 245 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 246 | std::cerr << "Calculating graph for SCC #: " << MyID << " of size: " |
| 247 | << SCCFunctions.size() << "\n"; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 248 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 249 | // Compute the Max SCC Size... |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 250 | if (MaxSCC < SCCFunctions.size()) |
| 251 | MaxSCC = SCCFunctions.size(); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 253 | // First thing first, collapse all of the DSGraphs into a single graph for |
| 254 | // the entire SCC. We computed the largest graph, so clone all of the other |
| 255 | // (smaller) graphs into it. Discard all of the old graphs. |
| 256 | // |
| 257 | for (hash_set<Function*>::iterator I = SCCFunctions.begin(), |
| 258 | E = SCCFunctions.end(); I != E; ++I) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 259 | DSGraph &G = getDSGraph(**I); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 260 | if (&G != SCCGraph) { |
| 261 | DSGraph::NodeMapTy NodeMap; |
| 262 | SCCGraph->cloneInto(G, SCCGraph->getScalarMap(), |
| 263 | SCCGraph->getReturnNodes(), NodeMap, 0); |
| 264 | // Update the DSInfo map and delete the old graph... |
| 265 | DSInfo[*I] = SCCGraph; |
| 266 | delete &G; |
| 267 | } |
| 268 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 269 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 270 | // Now that we have one big happy family, resolve all of the call sites in |
| 271 | // the graph... |
| 272 | calculateGraph(*SCCGraph); |
| 273 | DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize() |
| 274 | << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 275 | |
| 276 | std::cerr << "DONE with SCC #: " << MyID << "\n"; |
| 277 | |
| 278 | // We never have to revisit "SCC" processed functions... |
| 279 | |
| 280 | // Drop the stuff we don't need from the end of the stack |
| 281 | Stack.erase(FirstInSCC, Stack.end()); |
| 282 | return MyID; |
| 283 | } |
| 284 | |
| 285 | return MyID; // == Min |
| 286 | } |
| 287 | |
| 288 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 289 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 290 | // our memory... here... |
| 291 | // |
| 292 | void BUDataStructures::releaseMemory() { |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 293 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 294 | E = DSInfo.end(); I != E; ++I) { |
| 295 | I->second->getReturnNodes().erase(I->first); |
| 296 | if (I->second->getReturnNodes().empty()) |
| 297 | delete I->second; |
| 298 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 299 | |
| 300 | // Empty map so next time memory is released, data structures are not |
| 301 | // re-deleted. |
| 302 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 303 | delete GlobalsGraph; |
| 304 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 307 | void BUDataStructures::calculateGraph(DSGraph &Graph) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 308 | // Move our call site list into TempFCs so that inline call sites go into the |
| 309 | // new call site list and doesn't invalidate our iterators! |
| 310 | std::vector<DSCallSite> TempFCs; |
| 311 | std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
| 312 | TempFCs.swap(AuxCallsList); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 313 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 314 | DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes(); |
| 315 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 316 | // Loop over all of the resolvable call sites |
| 317 | unsigned LastCallSiteIdx = ~0U; |
| 318 | for (CallSiteIterator I = CallSiteIterator::begin(TempFCs), |
| 319 | E = CallSiteIterator::end(TempFCs); I != E; ++I) { |
| 320 | // If we skipped over any call sites, they must be unresolvable, copy them |
| 321 | // to the real call site list. |
| 322 | LastCallSiteIdx++; |
| 323 | for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx) |
| 324 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 325 | LastCallSiteIdx = I.getCallSiteIdx(); |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 326 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 327 | // Resolve the current call... |
| 328 | Function *Callee = *I; |
| 329 | DSCallSite &CS = I.getCallSite(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 330 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 331 | if (Callee->isExternal()) { |
| 332 | // Ignore this case, simple varargs functions we cannot stub out! |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 333 | } else if (ReturnNodes.find(Callee) != ReturnNodes.end()) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 334 | // Self recursion... simply link up the formal arguments with the |
| 335 | // actual arguments... |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 336 | DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 337 | |
| 338 | // Handle self recursion by resolving the arguments and return value |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 339 | Graph.mergeInGraph(CS, *Callee, Graph, 0); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 340 | |
| 341 | } else { |
| 342 | // Get the data structure graph for the called function. |
| 343 | // |
| 344 | DSGraph &GI = getDSGraph(*Callee); // Graph to inline |
| 345 | |
| 346 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName() |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 347 | << "[" << GI.getGraphSize() << "+" |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 348 | << GI.getAuxFunctionCalls().size() << "] into [" |
| 349 | << Graph.getGraphSize() << "+" |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 350 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 351 | |
| 352 | // Handle self recursion by resolving the arguments and return value |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 353 | Graph.mergeInGraph(CS, *Callee, GI, |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 354 | DSGraph::KeepModRefBits | |
| 355 | DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 356 | |
| 357 | #if 0 |
| 358 | Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" + |
| 359 | Callee->getName()); |
| 360 | #endif |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | // Make sure to catch any leftover unresolvable calls... |
| 365 | for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx) |
| 366 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 367 | |
| 368 | TempFCs.clear(); |
| 369 | |
| 370 | // Recompute the Incomplete markers. If there are any function calls left |
| 371 | // now that are complete, we must loop! |
| 372 | Graph.maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 373 | Graph.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 374 | // FIXME: materialize nodes from the globals graph as neccesary... |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 375 | Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 376 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 377 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 378 | } |
| 379 | |