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