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 | using std::map; |
| 15 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 16 | static RegisterAnalysis<BUDataStructures> |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 17 | X("budatastructure", "Bottom-up Data Structure Analysis Closure"); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 18 | |
Chris Lattner | b106043 | 2002-11-07 05:20:53 +0000 | [diff] [blame] | 19 | using namespace DS; |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 20 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 21 | // isCompleteNode - Return true if we know all of the targets of this node, and |
| 22 | // if the call sites are not external. |
| 23 | // |
| 24 | static inline bool isCompleteNode(DSNode *N) { |
| 25 | if (N->NodeType & DSNode::Incomplete) return false; |
| 26 | const std::vector<GlobalValue*> &Callees = N->getGlobals(); |
| 27 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) |
| 28 | if (Callees[i]->isExternal()) { |
| 29 | GlobalValue &FI = cast<Function>(*Callees[i]); |
| 30 | if (FI.getName() != "printf" && FI.getName() != "sscanf" && |
| 31 | FI.getName() != "fprintf" && FI.getName() != "open" && |
| 32 | FI.getName() != "sprintf" && FI.getName() != "fputs") |
| 33 | return false; // External function found... |
| 34 | } |
| 35 | return true; // otherwise ok |
| 36 | } |
| 37 | |
| 38 | struct CallSiteIterator { |
| 39 | // FCs are the edges out of the current node are the call site targets... |
| 40 | std::vector<DSCallSite> *FCs; |
| 41 | unsigned CallSite; |
| 42 | unsigned CallSiteEntry; |
| 43 | |
| 44 | CallSiteIterator(std::vector<DSCallSite> &CS) : FCs(&CS) { |
| 45 | CallSite = 0; CallSiteEntry = 0; |
| 46 | advanceToNextValid(); |
| 47 | } |
| 48 | |
| 49 | // End iterator ctor... |
| 50 | CallSiteIterator(std::vector<DSCallSite> &CS, bool) : FCs(&CS) { |
| 51 | CallSite = FCs->size(); CallSiteEntry = 0; |
| 52 | } |
| 53 | |
| 54 | void advanceToNextValid() { |
| 55 | while (CallSite < FCs->size()) { |
| 56 | if (DSNode *CalleeNode = (*FCs)[CallSite].getCallee().getNode()) { |
| 57 | if (CallSiteEntry || isCompleteNode(CalleeNode)) { |
| 58 | const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals(); |
| 59 | |
| 60 | if (CallSiteEntry < Callees.size()) |
| 61 | return; |
| 62 | } |
| 63 | CallSiteEntry = 0; |
| 64 | ++CallSite; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | public: |
| 69 | static CallSiteIterator begin(DSGraph &G) { return G.getAuxFunctionCalls(); } |
| 70 | static CallSiteIterator end(DSGraph &G) { |
| 71 | return CallSiteIterator(G.getAuxFunctionCalls(), true); |
| 72 | } |
| 73 | static CallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; } |
| 74 | static CallSiteIterator end(std::vector<DSCallSite> &CSs) { |
| 75 | return CallSiteIterator(CSs, true); |
| 76 | } |
| 77 | bool operator==(const CallSiteIterator &CSI) const { |
| 78 | return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry; |
| 79 | } |
| 80 | bool operator!=(const CallSiteIterator &CSI) const { return !operator==(CSI);} |
| 81 | |
| 82 | unsigned getCallSiteIdx() const { return CallSite; } |
| 83 | DSCallSite &getCallSite() const { return (*FCs)[CallSite]; } |
| 84 | |
| 85 | Function* operator*() const { |
| 86 | DSNode *Node = (*FCs)[CallSite].getCallee().getNode(); |
| 87 | return cast<Function>(Node->getGlobals()[CallSiteEntry]); |
| 88 | } |
| 89 | |
| 90 | CallSiteIterator& operator++() { // Preincrement |
| 91 | ++CallSiteEntry; |
| 92 | advanceToNextValid(); |
| 93 | return *this; |
| 94 | } |
| 95 | CallSiteIterator operator++(int) { // Postincrement |
| 96 | CallSiteIterator tmp = *this; ++*this; return tmp; |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 102 | // run - Calculate the bottom up data structure graphs for each function in the |
| 103 | // program. |
| 104 | // |
| 105 | bool BUDataStructures::run(Module &M) { |
| 106 | GlobalsGraph = new DSGraph(); |
| 107 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 108 | Function *MainFunc = M.getMainFunction(); |
| 109 | if (MainFunc) |
| 110 | calculateReachableGraphs(MainFunc); |
| 111 | |
| 112 | // Calculate the graphs for any functions that are unreachable from main... |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 113 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 114 | if (!I->isExternal() && DSInfo.find(I) == DSInfo.end()) { |
| 115 | if (MainFunc) |
| 116 | std::cerr << "*** Function unreachable from main: " |
| 117 | << I->getName() << "\n"; |
| 118 | calculateReachableGraphs(I); // Calculate all graphs... |
| 119 | } |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 120 | return false; |
| 121 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 122 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 123 | void BUDataStructures::calculateReachableGraphs(Function *F) { |
| 124 | std::vector<Function*> Stack; |
| 125 | std::map<Function*, unsigned> ValMap; |
| 126 | unsigned NextID = 1; |
| 127 | calculateGraphs(F, Stack, NextID, ValMap); |
| 128 | } |
| 129 | |
| 130 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { |
| 131 | // Has the graph already been created? |
| 132 | DSGraph *&Graph = DSInfo[F]; |
| 133 | if (Graph) return *Graph; |
| 134 | |
| 135 | // Copy the local version into DSInfo... |
| 136 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F)); |
| 137 | |
| 138 | Graph->setGlobalsGraph(GlobalsGraph); |
| 139 | Graph->setPrintAuxCalls(); |
| 140 | |
| 141 | // Start with a copy of the original call sites... |
| 142 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); |
| 143 | return *Graph; |
| 144 | } |
| 145 | |
| 146 | unsigned BUDataStructures::calculateGraphs(Function *F, |
| 147 | std::vector<Function*> &Stack, |
| 148 | unsigned &NextID, |
| 149 | std::map<Function*, unsigned> &ValMap) { |
| 150 | assert(ValMap.find(F) == ValMap.end() && "Shouldn't revisit functions!"); |
| 151 | unsigned Min = NextID++, MyID = Min; |
| 152 | ValMap[F] = Min; |
| 153 | Stack.push_back(F); |
| 154 | |
| 155 | if (F->isExternal()) { // sprintf, fprintf, sscanf, etc... |
| 156 | // No callees! |
| 157 | Stack.pop_back(); |
| 158 | ValMap[F] = ~0; |
| 159 | return Min; |
| 160 | } |
| 161 | |
| 162 | DSGraph &Graph = getOrCreateGraph(F); |
| 163 | |
| 164 | // The edges out of the current node are the call site targets... |
| 165 | for (CallSiteIterator I = CallSiteIterator::begin(Graph), |
| 166 | E = CallSiteIterator::end(Graph); I != E; ++I) { |
| 167 | Function *Callee = *I; |
| 168 | unsigned M; |
| 169 | // Have we visited the destination function yet? |
| 170 | std::map<Function*, unsigned>::iterator It = ValMap.find(Callee); |
| 171 | if (It == ValMap.end()) // No, visit it now. |
| 172 | M = calculateGraphs(Callee, Stack, NextID, ValMap); |
| 173 | else // Yes, get it's number. |
| 174 | M = It->second; |
| 175 | if (M < Min) Min = M; |
| 176 | } |
| 177 | |
| 178 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); |
| 179 | if (Min != MyID) |
| 180 | return Min; // This is part of a larger SCC! |
| 181 | |
| 182 | // If this is a new SCC, process it now. |
| 183 | if (Stack.back() == F) { // Special case the single "SCC" case here. |
| 184 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " |
| 185 | << F->getName() << "\n"); |
| 186 | Stack.pop_back(); |
| 187 | DSGraph &G = calculateGraph(*F); |
| 188 | |
| 189 | // Should we revisit the graph? |
| 190 | if (CallSiteIterator::begin(G) != CallSiteIterator::end(G)) { |
| 191 | ValMap.erase(F); |
| 192 | return calculateGraphs(F, Stack, NextID, ValMap); |
| 193 | } else { |
| 194 | ValMap[F] = ~0U; |
| 195 | } |
| 196 | return MyID; |
| 197 | |
| 198 | } else { |
| 199 | // SCCFunctions - Keep track of the functions in the current SCC |
| 200 | // |
| 201 | std::set<Function*> SCCFunctions; |
| 202 | |
| 203 | Function *NF; |
| 204 | std::vector<Function*>::iterator FirstInSCC = Stack.end(); |
| 205 | do { |
| 206 | NF = *--FirstInSCC; |
| 207 | ValMap[NF] = ~0U; |
| 208 | SCCFunctions.insert(NF); |
| 209 | } while (NF != F); |
| 210 | |
| 211 | std::cerr << "Identified SCC #: " << MyID << " of size: " |
| 212 | << (Stack.end()-FirstInSCC) << "\n"; |
| 213 | |
| 214 | std::vector<Function*>::iterator I = Stack.end(); |
| 215 | do { |
| 216 | --I; |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 217 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 218 | /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/" |
| 219 | << (Stack.end()-FirstInSCC) << " in SCC: " |
| 220 | << (*I)->getName()); |
| 221 | DSGraph &G = getDSGraph(**I); |
| 222 | std::cerr << " [" << G.getGraphSize() << "+" |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 223 | << G.getAuxFunctionCalls().size() << "] "; |
| 224 | #endif |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 226 | // Eliminate all call sites in the SCC that are not to functions that are |
| 227 | // in the SCC. |
| 228 | inlineNonSCCGraphs(**I, SCCFunctions); |
| 229 | |
| 230 | #ifndef NDEBUG |
| 231 | std::cerr << "after Non-SCC's [" << G.getGraphSize() << "+" |
| 232 | << G.getAuxFunctionCalls().size() << "]\n"; |
| 233 | #endif |
| 234 | } while (I != FirstInSCC); |
| 235 | |
| 236 | I = Stack.end(); |
| 237 | do { |
| 238 | --I; |
| 239 | #ifndef NDEBUG |
| 240 | /*DEBUG*/(std::cerr << " Fn #" << (Stack.end()-I) << "/" |
| 241 | << (Stack.end()-FirstInSCC) << " in SCC: " |
| 242 | << (*I)->getName()); |
| 243 | DSGraph &G = getDSGraph(**I); |
| 244 | std::cerr << " [" << G.getGraphSize() << "+" |
| 245 | << G.getAuxFunctionCalls().size() << "] "; |
| 246 | #endif |
| 247 | // Inline all graphs into the SCC nodes... |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 248 | calculateSCCGraph(**I, SCCFunctions); |
| 249 | |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 250 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 251 | std::cerr << "after [" << G.getGraphSize() << "+" |
| 252 | << G.getAuxFunctionCalls().size() << "]\n"; |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 253 | #endif |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 254 | } while (I != FirstInSCC); |
| 255 | |
| 256 | |
| 257 | std::cerr << "DONE with SCC #: " << MyID << "\n"; |
| 258 | |
| 259 | // We never have to revisit "SCC" processed functions... |
| 260 | |
| 261 | // Drop the stuff we don't need from the end of the stack |
| 262 | Stack.erase(FirstInSCC, Stack.end()); |
| 263 | return MyID; |
| 264 | } |
| 265 | |
| 266 | return MyID; // == Min |
| 267 | } |
| 268 | |
| 269 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 270 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 271 | // our memory... here... |
| 272 | // |
| 273 | void BUDataStructures::releaseMemory() { |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 274 | for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 275 | E = DSInfo.end(); I != E; ++I) |
| 276 | delete I->second; |
| 277 | |
| 278 | // Empty map so next time memory is released, data structures are not |
| 279 | // re-deleted. |
| 280 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 281 | delete GlobalsGraph; |
| 282 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 285 | DSGraph &BUDataStructures::calculateGraph(Function &F) { |
| 286 | DSGraph &Graph = getDSGraph(F); |
| 287 | DEBUG(std::cerr << " [BU] Calculating graph for: " << F.getName() << "\n"); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 288 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 289 | // Move our call site list into TempFCs so that inline call sites go into the |
| 290 | // new call site list and doesn't invalidate our iterators! |
| 291 | std::vector<DSCallSite> TempFCs; |
| 292 | std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
| 293 | TempFCs.swap(AuxCallsList); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 294 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 295 | // Loop over all of the resolvable call sites |
| 296 | unsigned LastCallSiteIdx = ~0U; |
| 297 | for (CallSiteIterator I = CallSiteIterator::begin(TempFCs), |
| 298 | E = CallSiteIterator::end(TempFCs); I != E; ++I) { |
| 299 | // If we skipped over any call sites, they must be unresolvable, copy them |
| 300 | // to the real call site list. |
| 301 | LastCallSiteIdx++; |
| 302 | for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx) |
| 303 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 304 | LastCallSiteIdx = I.getCallSiteIdx(); |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 305 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 306 | // Resolve the current call... |
| 307 | Function *Callee = *I; |
| 308 | DSCallSite &CS = I.getCallSite(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 309 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 310 | if (Callee->isExternal()) { |
| 311 | // Ignore this case, simple varargs functions we cannot stub out! |
| 312 | } else if (Callee == &F) { |
| 313 | // Self recursion... simply link up the formal arguments with the |
| 314 | // actual arguments... |
| 315 | DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n"); |
| 316 | |
| 317 | // Handle self recursion by resolving the arguments and return value |
| 318 | Graph.mergeInGraph(CS, Graph, 0); |
| 319 | |
| 320 | } else { |
| 321 | // Get the data structure graph for the called function. |
| 322 | // |
| 323 | DSGraph &GI = getDSGraph(*Callee); // Graph to inline |
| 324 | |
| 325 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName() |
| 326 | << " in: " << F.getName() << "[" << GI.getGraphSize() << "+" |
| 327 | << GI.getAuxFunctionCalls().size() << "]\n"); |
| 328 | |
| 329 | // Handle self recursion by resolving the arguments and return value |
| 330 | Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit | |
| 331 | DSGraph::DontCloneCallNodes); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Make sure to catch any leftover unresolvable calls... |
| 336 | for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx) |
| 337 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 338 | |
| 339 | TempFCs.clear(); |
| 340 | |
| 341 | // Recompute the Incomplete markers. If there are any function calls left |
| 342 | // now that are complete, we must loop! |
| 343 | Graph.maskIncompleteMarkers(); |
| 344 | Graph.markIncompleteNodes(); |
| 345 | Graph.removeDeadNodes(); |
| 346 | |
| 347 | DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " [" |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 348 | << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size() |
Chris Lattner | 221c979 | 2002-08-07 21:41:11 +0000 | [diff] [blame] | 349 | << "]\n"); |
Vikram S. Adve | 355e2ca | 2002-07-30 22:05:22 +0000 | [diff] [blame] | 350 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 351 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
| 352 | |
| 353 | return Graph; |
| 354 | } |
| 355 | |
| 356 | |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 357 | // inlineNonSCCGraphs - This method is almost like the other two calculate graph |
| 358 | // methods. This one is used to inline function graphs (from functions outside |
| 359 | // of the SCC) into functions in the SCC. It is not supposed to touch functions |
| 360 | // IN the SCC at all. |
| 361 | // |
| 362 | DSGraph &BUDataStructures::inlineNonSCCGraphs(Function &F, |
| 363 | std::set<Function*> &SCCFunctions){ |
| 364 | DSGraph &Graph = getDSGraph(F); |
| 365 | DEBUG(std::cerr << " [BU] Inlining Non-SCC graphs for: " |
| 366 | << F.getName() << "\n"); |
| 367 | |
| 368 | // Move our call site list into TempFCs so that inline call sites go into the |
| 369 | // new call site list and doesn't invalidate our iterators! |
| 370 | std::vector<DSCallSite> TempFCs; |
| 371 | std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
| 372 | TempFCs.swap(AuxCallsList); |
| 373 | |
| 374 | // Loop over all of the resolvable call sites |
| 375 | unsigned LastCallSiteIdx = ~0U; |
| 376 | for (CallSiteIterator I = CallSiteIterator::begin(TempFCs), |
| 377 | E = CallSiteIterator::end(TempFCs); I != E; ++I) { |
| 378 | // If we skipped over any call sites, they must be unresolvable, copy them |
| 379 | // to the real call site list. |
| 380 | LastCallSiteIdx++; |
| 381 | for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx) |
| 382 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 383 | LastCallSiteIdx = I.getCallSiteIdx(); |
| 384 | |
| 385 | // Resolve the current call... |
| 386 | Function *Callee = *I; |
| 387 | DSCallSite &CS = I.getCallSite(); |
| 388 | |
| 389 | if (Callee->isExternal()) { |
| 390 | // Ignore this case, simple varargs functions we cannot stub out! |
| 391 | } else if (SCCFunctions.count(Callee)) { |
| 392 | // Calling a function in the SCC, ignore it for now! |
| 393 | DEBUG(std::cerr << " SCC CallSite for: " << Callee->getName() << "\n"); |
| 394 | AuxCallsList.push_back(CS); |
| 395 | } else { |
| 396 | // Get the data structure graph for the called function. |
| 397 | // |
| 398 | DSGraph &GI = getDSGraph(*Callee); // Graph to inline |
| 399 | |
| 400 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName() |
| 401 | << " in: " << F.getName() << "[" << GI.getGraphSize() << "+" |
| 402 | << GI.getAuxFunctionCalls().size() << "]\n"); |
| 403 | |
| 404 | // Handle self recursion by resolving the arguments and return value |
| 405 | Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit | |
| 406 | DSGraph::DontCloneCallNodes); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Make sure to catch any leftover unresolvable calls... |
| 411 | for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx) |
| 412 | AuxCallsList.push_back(TempFCs[LastCallSiteIdx]); |
| 413 | |
| 414 | TempFCs.clear(); |
| 415 | |
| 416 | // Recompute the Incomplete markers. If there are any function calls left |
| 417 | // now that are complete, we must loop! |
| 418 | Graph.maskIncompleteMarkers(); |
| 419 | Graph.markIncompleteNodes(); |
| 420 | Graph.removeDeadNodes(); |
| 421 | |
| 422 | DEBUG(std::cerr << " [BU] Done Non-SCC inlining: " << F.getName() << " [" |
| 423 | << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size() |
| 424 | << "]\n"); |
| 425 | |
| 426 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
| 427 | |
| 428 | return Graph; |
| 429 | } |
| 430 | |
| 431 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 432 | DSGraph &BUDataStructures::calculateSCCGraph(Function &F, |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 433 | std::set<Function*> &SCCFunctions){ |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 434 | DSGraph &Graph = getDSGraph(F); |
| 435 | DEBUG(std::cerr << " [BU] Calculating SCC graph for: " << F.getName()<<"\n"); |
| 436 | |
| 437 | std::vector<DSCallSite> UnresolvableCalls; |
| 438 | std::map<Function*, DSCallSite> SCCCallSiteMap; |
| 439 | std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
| 440 | |
| 441 | while (1) { // Loop until we run out of resolvable call sites! |
| 442 | // Move our call site list into TempFCs so that inline call sites go into |
| 443 | // the new call site list and doesn't invalidate our iterators! |
| 444 | std::vector<DSCallSite> TempFCs; |
| 445 | TempFCs.swap(AuxCallsList); |
| 446 | |
| 447 | // Loop over all of the resolvable call sites |
| 448 | unsigned LastCallSiteIdx = ~0U; |
| 449 | CallSiteIterator I = CallSiteIterator::begin(TempFCs), |
| 450 | E = CallSiteIterator::end(TempFCs); |
| 451 | if (I == E) { |
| 452 | TempFCs.swap(AuxCallsList); |
| 453 | break; // Done when no resolvable call sites exist |
| 454 | } |
| 455 | |
| 456 | for (; I != E; ++I) { |
| 457 | // If we skipped over any call sites, they must be unresolvable, copy them |
| 458 | // to the unresolvable site list. |
| 459 | LastCallSiteIdx++; |
| 460 | for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx) |
| 461 | UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]); |
| 462 | LastCallSiteIdx = I.getCallSiteIdx(); |
| 463 | |
| 464 | // Resolve the current call... |
| 465 | Function *Callee = *I; |
| 466 | DSCallSite &CS = I.getCallSite(); |
| 467 | |
| 468 | if (Callee->isExternal()) { |
| 469 | // Ignore this case, simple varargs functions we cannot stub out! |
| 470 | } else if (Callee == &F) { |
| 471 | // Self recursion... simply link up the formal arguments with the |
| 472 | // actual arguments... |
| 473 | DEBUG(std::cerr << " Self Inlining: " << F.getName() << "\n"); |
| 474 | |
| 475 | // Handle self recursion by resolving the arguments and return value |
| 476 | Graph.mergeInGraph(CS, Graph, 0); |
| 477 | } else if (SCCCallSiteMap.count(Callee)) { |
| 478 | // We have already seen a call site in the SCC for this function, just |
| 479 | // merge the two call sites together and we are done. |
| 480 | SCCCallSiteMap.find(Callee)->second.mergeWith(CS); |
| 481 | } else { |
| 482 | // Get the data structure graph for the called function. |
| 483 | // |
| 484 | DSGraph &GI = getDSGraph(*Callee); // Graph to inline |
| 485 | |
| 486 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName() |
| 487 | << " in: " << F.getName() << "[" << GI.getGraphSize() << "+" |
| 488 | << GI.getAuxFunctionCalls().size() << "]\n"); |
| 489 | |
| 490 | // Handle self recursion by resolving the arguments and return value |
| 491 | Graph.mergeInGraph(CS, GI, DSGraph::StripAllocaBit | |
| 492 | DSGraph::DontCloneCallNodes); |
| 493 | |
Chris Lattner | 5f1f2c6 | 2002-11-12 15:58:08 +0000 | [diff] [blame] | 494 | if (SCCFunctions.count(Callee)) |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 495 | SCCCallSiteMap.insert(std::make_pair(Callee, CS)); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // Make sure to catch any leftover unresolvable calls... |
| 500 | for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx) |
| 501 | UnresolvableCalls.push_back(TempFCs[LastCallSiteIdx]); |
| 502 | } |
| 503 | |
| 504 | // Reset the SCCCallSiteMap... |
| 505 | SCCCallSiteMap.clear(); |
| 506 | |
| 507 | AuxCallsList.insert(AuxCallsList.end(), UnresolvableCalls.begin(), |
| 508 | UnresolvableCalls.end()); |
| 509 | UnresolvableCalls.clear(); |
| 510 | |
| 511 | |
| 512 | // Recompute the Incomplete markers. If there are any function calls left |
| 513 | // now that are complete, we must loop! |
| 514 | Graph.maskIncompleteMarkers(); |
| 515 | Graph.markIncompleteNodes(); |
| 516 | Graph.removeDeadNodes(); |
| 517 | |
| 518 | DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " [" |
| 519 | << Graph.getGraphSize() << "+" << Graph.getAuxFunctionCalls().size() |
| 520 | << "]\n"); |
| 521 | |
| 522 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 524 | return Graph; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 525 | } |