Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 1 | //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the BUDataStructures class, which represents the |
| 11 | // Bottom-Up Interprocedural closure of the data structure graph over the |
| 12 | // program. This is useful for applications like pool allocation, but **not** |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 13 | // applications like alias analysis. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 8adbec8 | 2004-07-07 06:35:22 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
| 21 | #include "llvm/Support/Debug.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph"); |
Chris Lattner | d391d70 | 2003-07-02 20:24:42 +0000 | [diff] [blame] | 26 | Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined"); |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 27 | Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges"); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 28 | |
| 29 | RegisterAnalysis<BUDataStructures> |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 30 | X("budatastructure", "Bottom-up Data Structure Analysis"); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 31 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 32 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 33 | // run - Calculate the bottom up data structure graphs for each function in the |
| 34 | // program. |
| 35 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 36 | bool BUDataStructures::runOnModule(Module &M) { |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 37 | LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 38 | GlobalECs = LocalDSA.getGlobalECs(); |
| 39 | |
| 40 | GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs); |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 41 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 42 | |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 43 | IndCallGraphMap = new std::map<std::vector<Function*>, |
| 44 | std::pair<DSGraph*, std::vector<DSNodeHandle> > >(); |
| 45 | |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 46 | std::vector<Function*> Stack; |
| 47 | hash_map<Function*, unsigned> ValMap; |
| 48 | unsigned NextID = 1; |
| 49 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 50 | Function *MainFunc = M.getMainFunction(); |
| 51 | if (MainFunc) |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 52 | calculateGraphs(MainFunc, Stack, NextID, ValMap); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 53 | |
| 54 | // Calculate the graphs for any functions that are unreachable from main... |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 55 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 56 | if (!I->isExternal() && !DSInfo.count(I)) { |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 57 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 58 | if (MainFunc) |
| 59 | std::cerr << "*** Function unreachable from main: " |
| 60 | << I->getName() << "\n"; |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 61 | #endif |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 62 | calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs. |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 64 | |
| 65 | NumCallEdges += ActualCallees.size(); |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 67 | // If we computed any temporary indcallgraphs, free them now. |
| 68 | for (std::map<std::vector<Function*>, |
| 69 | std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I = |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 70 | IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) { |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 71 | I->second.second.clear(); // Drop arg refs into the graph. |
| 72 | delete I->second.first; |
| 73 | } |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 74 | delete IndCallGraphMap; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 75 | |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 76 | // At the end of the bottom-up pass, the globals graph becomes complete. |
| 77 | // FIXME: This is not the right way to do this, but it is sorta better than |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 78 | // nothing! In particular, externally visible globals and unresolvable call |
| 79 | // nodes at the end of the BU phase should make things that they point to |
| 80 | // incomplete in the globals graph. |
| 81 | // |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 82 | GlobalsGraph->removeTriviallyDeadNodes(); |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 83 | GlobalsGraph->maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 84 | |
| 85 | // Merge the globals variables (not the calls) from the globals graph back |
| 86 | // into the main function's graph so that the main function contains all of |
| 87 | // the information about global pools and GV usage in the program. |
Chris Lattner | 49e88e8 | 2005-03-15 22:10:04 +0000 | [diff] [blame] | 88 | if (MainFunc && !MainFunc->isExternal()) { |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 89 | DSGraph &MainGraph = getOrCreateGraph(MainFunc); |
| 90 | const DSGraph &GG = *MainGraph.getGlobalsGraph(); |
| 91 | ReachabilityCloner RC(MainGraph, GG, |
| 92 | DSGraph::DontCloneCallNodes | |
| 93 | DSGraph::DontCloneAuxCallNodes); |
| 94 | |
| 95 | // Clone the global nodes into this graph. |
| 96 | for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(), |
| 97 | E = GG.getScalarMap().global_end(); I != E; ++I) |
| 98 | if (isa<GlobalVariable>(*I)) |
| 99 | RC.getClonedNH(GG.getNodeForValue(*I)); |
| 100 | |
Chris Lattner | 270cf50 | 2005-03-13 20:32:26 +0000 | [diff] [blame] | 101 | MainGraph.maskIncompleteMarkers(); |
Chris Lattner | a66e353 | 2005-03-13 20:15:06 +0000 | [diff] [blame] | 102 | MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs | |
| 103 | DSGraph::IgnoreGlobals); |
| 104 | } |
| 105 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 106 | return false; |
| 107 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 108 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 109 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { |
| 110 | // Has the graph already been created? |
| 111 | DSGraph *&Graph = DSInfo[F]; |
| 112 | if (Graph) return *Graph; |
| 113 | |
| 114 | // Copy the local version into DSInfo... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 115 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F), |
| 116 | GlobalECs); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 117 | |
| 118 | Graph->setGlobalsGraph(GlobalsGraph); |
| 119 | Graph->setPrintAuxCalls(); |
| 120 | |
| 121 | // Start with a copy of the original call sites... |
| 122 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); |
| 123 | return *Graph; |
| 124 | } |
| 125 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 126 | static bool isVAHackFn(const Function *F) { |
| 127 | return F->getName() == "printf" || F->getName() == "sscanf" || |
| 128 | F->getName() == "fprintf" || F->getName() == "open" || |
| 129 | F->getName() == "sprintf" || F->getName() == "fputs" || |
| 130 | F->getName() == "fscanf"; |
| 131 | } |
| 132 | |
| 133 | static bool isResolvableFunc(const Function* callee) { |
| 134 | return !callee->isExternal() || isVAHackFn(callee); |
| 135 | } |
| 136 | |
| 137 | static void GetAllCallees(const DSCallSite &CS, |
| 138 | std::vector<Function*> &Callees) { |
| 139 | if (CS.isDirectCall()) { |
| 140 | if (isResolvableFunc(CS.getCalleeFunc())) |
| 141 | Callees.push_back(CS.getCalleeFunc()); |
| 142 | } else if (!CS.getCalleeNode()->isIncomplete()) { |
| 143 | // Get all callees. |
| 144 | unsigned OldSize = Callees.size(); |
| 145 | CS.getCalleeNode()->addFullFunctionList(Callees); |
| 146 | |
| 147 | // If any of the callees are unresolvable, remove the whole batch! |
| 148 | for (unsigned i = OldSize, e = Callees.size(); i != e; ++i) |
| 149 | if (!isResolvableFunc(Callees[i])) { |
| 150 | Callees.erase(Callees.begin()+OldSize, Callees.end()); |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /// GetAllAuxCallees - Return a list containing all of the resolvable callees in |
| 158 | /// the aux list for the specified graph in the Callees vector. |
| 159 | static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) { |
| 160 | Callees.clear(); |
| 161 | for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I) |
| 162 | GetAllCallees(*I, Callees); |
| 163 | } |
| 164 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 165 | unsigned BUDataStructures::calculateGraphs(Function *F, |
| 166 | std::vector<Function*> &Stack, |
| 167 | unsigned &NextID, |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 168 | hash_map<Function*, unsigned> &ValMap) { |
Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 169 | assert(!ValMap.count(F) && "Shouldn't revisit functions!"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 170 | unsigned Min = NextID++, MyID = Min; |
| 171 | ValMap[F] = Min; |
| 172 | Stack.push_back(F); |
| 173 | |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 174 | // FIXME! This test should be generalized to be any function that we have |
| 175 | // already processed, in the case when there isn't a main or there are |
| 176 | // unreachable functions! |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 177 | if (F->isExternal()) { // sprintf, fprintf, sscanf, etc... |
| 178 | // No callees! |
| 179 | Stack.pop_back(); |
| 180 | ValMap[F] = ~0; |
| 181 | return Min; |
| 182 | } |
| 183 | |
| 184 | DSGraph &Graph = getOrCreateGraph(F); |
| 185 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 186 | // Find all callee functions. |
| 187 | std::vector<Function*> CalleeFunctions; |
| 188 | GetAllAuxCallees(Graph, CalleeFunctions); |
| 189 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 190 | // The edges out of the current node are the call site targets... |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 191 | for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) { |
| 192 | Function *Callee = CalleeFunctions[i]; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 193 | unsigned M; |
| 194 | // Have we visited the destination function yet? |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 195 | hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 196 | if (It == ValMap.end()) // No, visit it now. |
| 197 | M = calculateGraphs(Callee, Stack, NextID, ValMap); |
| 198 | else // Yes, get it's number. |
| 199 | M = It->second; |
| 200 | if (M < Min) Min = M; |
| 201 | } |
| 202 | |
| 203 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); |
| 204 | if (Min != MyID) |
| 205 | return Min; // This is part of a larger SCC! |
| 206 | |
| 207 | // If this is a new SCC, process it now. |
| 208 | if (Stack.back() == F) { // Special case the single "SCC" case here. |
| 209 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " |
| 210 | << F->getName() << "\n"); |
| 211 | Stack.pop_back(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 212 | DSGraph &G = getDSGraph(*F); |
| 213 | DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n"); |
| 214 | calculateGraph(G); |
| 215 | DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " [" |
| 216 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
| 217 | << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 218 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 219 | if (MaxSCC < 1) MaxSCC = 1; |
| 220 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 221 | // Should we revisit the graph? Only do it if there are now new resolvable |
| 222 | // callees. |
| 223 | GetAllAuxCallees(Graph, CalleeFunctions); |
| 224 | if (!CalleeFunctions.empty()) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 225 | ValMap.erase(F); |
| 226 | return calculateGraphs(F, Stack, NextID, ValMap); |
| 227 | } else { |
| 228 | ValMap[F] = ~0U; |
| 229 | } |
| 230 | return MyID; |
| 231 | |
| 232 | } else { |
| 233 | // SCCFunctions - Keep track of the functions in the current SCC |
| 234 | // |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 235 | hash_set<DSGraph*> SCCGraphs; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 236 | |
| 237 | Function *NF; |
| 238 | std::vector<Function*>::iterator FirstInSCC = Stack.end(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 239 | DSGraph *SCCGraph = 0; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 240 | do { |
| 241 | NF = *--FirstInSCC; |
| 242 | ValMap[NF] = ~0U; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 243 | |
| 244 | // Figure out which graph is the largest one, in order to speed things up |
| 245 | // a bit in situations where functions in the SCC have widely different |
| 246 | // graph sizes. |
| 247 | DSGraph &NFGraph = getDSGraph(*NF); |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 248 | SCCGraphs.insert(&NFGraph); |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 249 | // FIXME: If we used a better way of cloning graphs (ie, just splice all |
| 250 | // of the nodes into the new graph), this would be completely unneeded! |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 251 | if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize()) |
| 252 | SCCGraph = &NFGraph; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 253 | } while (NF != F); |
| 254 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 255 | std::cerr << "Calculating graph for SCC #: " << MyID << " of size: " |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 256 | << SCCGraphs.size() << "\n"; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 257 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 258 | // Compute the Max SCC Size... |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 259 | if (MaxSCC < SCCGraphs.size()) |
| 260 | MaxSCC = SCCGraphs.size(); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 262 | // First thing first, collapse all of the DSGraphs into a single graph for |
| 263 | // the entire SCC. We computed the largest graph, so clone all of the other |
| 264 | // (smaller) graphs into it. Discard all of the old graphs. |
| 265 | // |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 266 | for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(), |
| 267 | E = SCCGraphs.end(); I != E; ++I) { |
| 268 | DSGraph &G = **I; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 269 | if (&G != SCCGraph) { |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 270 | { |
| 271 | DSGraph::NodeMapTy NodeMap; |
| 272 | SCCGraph->cloneInto(G, SCCGraph->getScalarMap(), |
| 273 | SCCGraph->getReturnNodes(), NodeMap); |
| 274 | } |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 275 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 276 | for (DSGraph::retnodes_iterator I = G.retnodes_begin(), |
| 277 | E = G.retnodes_end(); I != E; ++I) |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 278 | DSInfo[I->first] = SCCGraph; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 279 | delete &G; |
| 280 | } |
| 281 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 283 | // Clean up the graph before we start inlining a bunch again... |
Chris Lattner | ac6d485 | 2004-11-08 21:08:46 +0000 | [diff] [blame] | 284 | SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 286 | // Now that we have one big happy family, resolve all of the call sites in |
| 287 | // the graph... |
| 288 | calculateGraph(*SCCGraph); |
| 289 | DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize() |
| 290 | << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 291 | |
| 292 | std::cerr << "DONE with SCC #: " << MyID << "\n"; |
| 293 | |
| 294 | // We never have to revisit "SCC" processed functions... |
| 295 | |
| 296 | // Drop the stuff we don't need from the end of the stack |
| 297 | Stack.erase(FirstInSCC, Stack.end()); |
| 298 | return MyID; |
| 299 | } |
| 300 | |
| 301 | return MyID; // == Min |
| 302 | } |
| 303 | |
| 304 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 305 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 306 | // our memory... here... |
| 307 | // |
| 308 | void BUDataStructures::releaseMemory() { |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 309 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 310 | E = DSInfo.end(); I != E; ++I) { |
| 311 | I->second->getReturnNodes().erase(I->first); |
| 312 | if (I->second->getReturnNodes().empty()) |
| 313 | delete I->second; |
| 314 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 315 | |
| 316 | // Empty map so next time memory is released, data structures are not |
| 317 | // re-deleted. |
| 318 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 319 | delete GlobalsGraph; |
| 320 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 323 | void BUDataStructures::calculateGraph(DSGraph &Graph) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 324 | // Move our call site list into TempFCs so that inline call sites go into the |
| 325 | // new call site list and doesn't invalidate our iterators! |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 326 | std::list<DSCallSite> TempFCs; |
| 327 | std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 328 | TempFCs.swap(AuxCallsList); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 330 | DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes(); |
| 331 | |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 332 | bool Printed = false; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 333 | std::vector<Function*> CalledFuncs; |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 334 | while (!TempFCs.empty()) { |
| 335 | DSCallSite &CS = *TempFCs.begin(); |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 337 | CalledFuncs.clear(); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 339 | // Fast path for noop calls. Note that we don't care about merging globals |
| 340 | // in the callee with nodes in the caller here. |
| 341 | if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) { |
| 342 | TempFCs.erase(TempFCs.begin()); |
| 343 | continue; |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 344 | } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) { |
| 345 | TempFCs.erase(TempFCs.begin()); |
| 346 | continue; |
Chris Lattner | 5021b8c | 2005-03-18 23:19:47 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | 6b9eb35 | 2005-03-20 02:42:07 +0000 | [diff] [blame] | 349 | GetAllCallees(CS, CalledFuncs); |
Chris Lattner | 0321b68 | 2004-02-27 20:05:15 +0000 | [diff] [blame] | 350 | |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 351 | if (CalledFuncs.empty()) { |
| 352 | // Remember that we could not resolve this yet! |
| 353 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin()); |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 354 | continue; |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 355 | } else { |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 356 | DSGraph *GI; |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 358 | if (CalledFuncs.size() == 1) { |
| 359 | Function *Callee = CalledFuncs[0]; |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 360 | ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(), |
| 361 | Callee)); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 363 | // Get the data structure graph for the called function. |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 364 | GI = &getDSGraph(*Callee); // Graph to inline |
| 365 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName()); |
| 366 | |
| 367 | DEBUG(std::cerr << "[" << GI->getGraphSize() << "+" |
| 368 | << GI->getAuxFunctionCalls().size() << "] into '" |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 369 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+" |
| 370 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 371 | Graph.mergeInGraph(CS, *Callee, *GI, |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 372 | DSGraph::KeepModRefBits | |
| 373 | DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes); |
| 374 | ++NumBUInlines; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 375 | } else { |
| 376 | if (!Printed) |
| 377 | std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n"; |
| 378 | std::cerr << " calls " << CalledFuncs.size() |
| 379 | << " fns from site: " << CS.getCallSite().getInstruction() |
| 380 | << " " << *CS.getCallSite().getInstruction(); |
| 381 | unsigned NumToPrint = CalledFuncs.size(); |
| 382 | if (NumToPrint > 8) NumToPrint = 8; |
| 383 | std::cerr << " Fns ="; |
| 384 | for (std::vector<Function*>::iterator I = CalledFuncs.begin(), |
| 385 | E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint) |
| 386 | std::cerr << " " << (*I)->getName(); |
| 387 | std::cerr << "\n"; |
| 388 | |
| 389 | // See if we already computed a graph for this set of callees. |
| 390 | std::sort(CalledFuncs.begin(), CalledFuncs.end()); |
| 391 | std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph = |
Chris Lattner | bcc70bc | 2005-02-07 16:09:15 +0000 | [diff] [blame] | 392 | (*IndCallGraphMap)[CalledFuncs]; |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 393 | |
| 394 | if (IndCallGraph.first == 0) { |
| 395 | std::vector<Function*>::iterator I = CalledFuncs.begin(), |
| 396 | E = CalledFuncs.end(); |
| 397 | |
| 398 | // Start with a copy of the first graph. |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 399 | GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs); |
Chris Lattner | 86db364 | 2005-02-04 19:59:49 +0000 | [diff] [blame] | 400 | GI->setGlobalsGraph(Graph.getGlobalsGraph()); |
| 401 | std::vector<DSNodeHandle> &Args = IndCallGraph.second; |
| 402 | |
| 403 | // Get the argument nodes for the first callee. The return value is |
| 404 | // the 0th index in the vector. |
| 405 | GI->getFunctionArgumentsForCall(*I, Args); |
| 406 | |
| 407 | // Merge all of the other callees into this graph. |
| 408 | for (++I; I != E; ++I) { |
| 409 | // If the graph already contains the nodes for the function, don't |
| 410 | // bother merging it in again. |
| 411 | if (!GI->containsFunction(*I)) { |
| 412 | DSGraph::NodeMapTy NodeMap; |
| 413 | GI->cloneInto(getDSGraph(**I), GI->getScalarMap(), |
| 414 | GI->getReturnNodes(), NodeMap); |
| 415 | ++NumBUInlines; |
| 416 | } |
| 417 | |
| 418 | std::vector<DSNodeHandle> NextArgs; |
| 419 | GI->getFunctionArgumentsForCall(*I, NextArgs); |
| 420 | unsigned i = 0, e = Args.size(); |
| 421 | for (; i != e; ++i) { |
| 422 | if (i == NextArgs.size()) break; |
| 423 | Args[i].mergeWith(NextArgs[i]); |
| 424 | } |
| 425 | for (e = NextArgs.size(); i != e; ++i) |
| 426 | Args.push_back(NextArgs[i]); |
| 427 | } |
| 428 | |
| 429 | // Clean up the final graph! |
| 430 | GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
| 431 | } else { |
| 432 | std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n"; |
| 433 | } |
| 434 | |
| 435 | GI = IndCallGraph.first; |
| 436 | |
| 437 | // Merge the unified graph into this graph now. |
| 438 | DEBUG(std::cerr << " Inlining multi callee graph " |
| 439 | << "[" << GI->getGraphSize() << "+" |
| 440 | << GI->getAuxFunctionCalls().size() << "] into '" |
| 441 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+" |
| 442 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
| 443 | |
| 444 | Graph.mergeInGraph(CS, IndCallGraph.second, *GI, |
| 445 | DSGraph::KeepModRefBits | |
| 446 | DSGraph::StripAllocaBit | |
| 447 | DSGraph::DontCloneCallNodes); |
| 448 | ++NumBUInlines; |
Chris Lattner | af8650e | 2005-02-01 21:37:27 +0000 | [diff] [blame] | 449 | } |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | 20cd136 | 2005-02-01 21:49:43 +0000 | [diff] [blame] | 451 | TempFCs.erase(TempFCs.begin()); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 452 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 453 | |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 454 | // Recompute the Incomplete markers |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 455 | Graph.maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 456 | Graph.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 457 | |
| 458 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 459 | // reach live nodes as live. |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 460 | Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 462 | // When this graph is finalized, clone the globals in the graph into the |
| 463 | // globals graph to make sure it has everything, from all graphs. |
| 464 | DSScalarMap &MainSM = Graph.getScalarMap(); |
| 465 | ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit); |
| 466 | |
Chris Lattner | 3b7b81b | 2004-10-31 21:54:51 +0000 | [diff] [blame] | 467 | // Clone everything reachable from globals in the function graph into the |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 468 | // globals graph. |
| 469 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
| 470 | E = MainSM.global_end(); I != E; ++I) |
| 471 | RC.getClonedNH(MainSM[*I]); |
| 472 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 473 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 474 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 475 | |
| 476 | static const Function *getFnForValue(const Value *V) { |
| 477 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 478 | return I->getParent()->getParent(); |
| 479 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 480 | return A->getParent(); |
| 481 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 482 | return BB->getParent(); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program. |
| 487 | /// These correspond to the interfaces defined in the AliasAnalysis class. |
| 488 | void BUDataStructures::deleteValue(Value *V) { |
| 489 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 490 | // If this is a function local value, just delete it from the scalar map! |
| 491 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 492 | return; |
| 493 | } |
| 494 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 495 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 496 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 497 | "cannot handle scc's"); |
| 498 | delete DSInfo[F]; |
| 499 | DSInfo.erase(F); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 504 | } |
| 505 | |
| 506 | void BUDataStructures::copyValue(Value *From, Value *To) { |
| 507 | if (From == To) return; |
| 508 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 509 | // If this is a function local value, just delete it from the scalar map! |
| 510 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 511 | return; |
| 512 | } |
| 513 | |
| 514 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 515 | Function *ToF = cast<Function>(To); |
| 516 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 517 | DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs); |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 518 | DSInfo[ToF] = NG; |
| 519 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 520 | |
| 521 | // Change the Function* is the returnnodes map to the ToF. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 522 | DSNodeHandle Ret = NG->retnodes_begin()->second; |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 523 | NG->getReturnNodes().clear(); |
| 524 | NG->getReturnNodes()[ToF] = Ret; |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!"); |
| 529 | } |