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