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