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>(); |
| 40 | GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph()); |
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 | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame^] | 43 | std::vector<Function*> Stack; |
| 44 | hash_map<Function*, unsigned> ValMap; |
| 45 | unsigned NextID = 1; |
| 46 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 47 | Function *MainFunc = M.getMainFunction(); |
| 48 | if (MainFunc) |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame^] | 49 | calculateGraphs(MainFunc, Stack, NextID, ValMap); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 50 | |
| 51 | // Calculate the graphs for any functions that are unreachable from main... |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 52 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 53 | if (!I->isExternal() && !DSInfo.count(I)) { |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 54 | #ifndef NDEBUG |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 55 | if (MainFunc) |
| 56 | std::cerr << "*** Function unreachable from main: " |
| 57 | << I->getName() << "\n"; |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 58 | #endif |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame^] | 59 | calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs. |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 61 | |
| 62 | NumCallEdges += ActualCallees.size(); |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 63 | |
| 64 | // At the end of the bottom-up pass, the globals graph becomes complete. |
| 65 | // 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] | 66 | // nothing! In particular, externally visible globals and unresolvable call |
| 67 | // nodes at the end of the BU phase should make things that they point to |
| 68 | // incomplete in the globals graph. |
| 69 | // |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 70 | GlobalsGraph->removeTriviallyDeadNodes(); |
Chris Lattner | ec157b7 | 2003-09-20 23:27:05 +0000 | [diff] [blame] | 71 | GlobalsGraph->maskIncompleteMarkers(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
Chris Lattner | 55c1058 | 2002-10-03 20:38:41 +0000 | [diff] [blame] | 74 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 75 | void BUDataStructures::calculateReachableGraphs(Function *F) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | DSGraph &BUDataStructures::getOrCreateGraph(Function *F) { |
| 79 | // Has the graph already been created? |
| 80 | DSGraph *&Graph = DSInfo[F]; |
| 81 | if (Graph) return *Graph; |
| 82 | |
| 83 | // Copy the local version into DSInfo... |
| 84 | Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F)); |
| 85 | |
| 86 | Graph->setGlobalsGraph(GlobalsGraph); |
| 87 | Graph->setPrintAuxCalls(); |
| 88 | |
| 89 | // Start with a copy of the original call sites... |
| 90 | Graph->getAuxFunctionCalls() = Graph->getFunctionCalls(); |
| 91 | return *Graph; |
| 92 | } |
| 93 | |
| 94 | unsigned BUDataStructures::calculateGraphs(Function *F, |
| 95 | std::vector<Function*> &Stack, |
| 96 | unsigned &NextID, |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 97 | hash_map<Function*, unsigned> &ValMap) { |
Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 98 | assert(!ValMap.count(F) && "Shouldn't revisit functions!"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 99 | unsigned Min = NextID++, MyID = Min; |
| 100 | ValMap[F] = Min; |
| 101 | Stack.push_back(F); |
| 102 | |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 103 | // FIXME! This test should be generalized to be any function that we have |
| 104 | // already processed, in the case when there isn't a main or there are |
| 105 | // unreachable functions! |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 106 | if (F->isExternal()) { // sprintf, fprintf, sscanf, etc... |
| 107 | // No callees! |
| 108 | Stack.pop_back(); |
| 109 | ValMap[F] = ~0; |
| 110 | return Min; |
| 111 | } |
| 112 | |
| 113 | DSGraph &Graph = getOrCreateGraph(F); |
| 114 | |
| 115 | // The edges out of the current node are the call site targets... |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 116 | for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph), |
| 117 | E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 118 | Function *Callee = *I; |
| 119 | unsigned M; |
| 120 | // Have we visited the destination function yet? |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 121 | hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 122 | if (It == ValMap.end()) // No, visit it now. |
| 123 | M = calculateGraphs(Callee, Stack, NextID, ValMap); |
| 124 | else // Yes, get it's number. |
| 125 | M = It->second; |
| 126 | if (M < Min) Min = M; |
| 127 | } |
| 128 | |
| 129 | assert(ValMap[F] == MyID && "SCC construction assumption wrong!"); |
| 130 | if (Min != MyID) |
| 131 | return Min; // This is part of a larger SCC! |
| 132 | |
| 133 | // If this is a new SCC, process it now. |
| 134 | if (Stack.back() == F) { // Special case the single "SCC" case here. |
| 135 | DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: " |
| 136 | << F->getName() << "\n"); |
| 137 | Stack.pop_back(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 138 | DSGraph &G = getDSGraph(*F); |
| 139 | DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n"); |
| 140 | calculateGraph(G); |
| 141 | DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " [" |
| 142 | << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size() |
| 143 | << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 144 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 145 | if (MaxSCC < 1) MaxSCC = 1; |
| 146 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 147 | // Should we revisit the graph? |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 148 | if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 149 | ValMap.erase(F); |
| 150 | return calculateGraphs(F, Stack, NextID, ValMap); |
| 151 | } else { |
| 152 | ValMap[F] = ~0U; |
| 153 | } |
| 154 | return MyID; |
| 155 | |
| 156 | } else { |
| 157 | // SCCFunctions - Keep track of the functions in the current SCC |
| 158 | // |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 159 | hash_set<DSGraph*> SCCGraphs; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 160 | |
| 161 | Function *NF; |
| 162 | std::vector<Function*>::iterator FirstInSCC = Stack.end(); |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 163 | DSGraph *SCCGraph = 0; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 164 | do { |
| 165 | NF = *--FirstInSCC; |
| 166 | ValMap[NF] = ~0U; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 167 | |
| 168 | // Figure out which graph is the largest one, in order to speed things up |
| 169 | // a bit in situations where functions in the SCC have widely different |
| 170 | // graph sizes. |
| 171 | DSGraph &NFGraph = getDSGraph(*NF); |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 172 | SCCGraphs.insert(&NFGraph); |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 173 | // FIXME: If we used a better way of cloning graphs (ie, just splice all |
| 174 | // of the nodes into the new graph), this would be completely unneeded! |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 175 | if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize()) |
| 176 | SCCGraph = &NFGraph; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 177 | } while (NF != F); |
| 178 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 179 | std::cerr << "Calculating graph for SCC #: " << MyID << " of size: " |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 180 | << SCCGraphs.size() << "\n"; |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 181 | |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 182 | // Compute the Max SCC Size... |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 183 | if (MaxSCC < SCCGraphs.size()) |
| 184 | MaxSCC = SCCGraphs.size(); |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 186 | // First thing first, collapse all of the DSGraphs into a single graph for |
| 187 | // the entire SCC. We computed the largest graph, so clone all of the other |
| 188 | // (smaller) graphs into it. Discard all of the old graphs. |
| 189 | // |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 190 | for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(), |
| 191 | E = SCCGraphs.end(); I != E; ++I) { |
| 192 | DSGraph &G = **I; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 193 | if (&G != SCCGraph) { |
Chris Lattner | 16437ff | 2004-03-04 17:05:28 +0000 | [diff] [blame] | 194 | { |
| 195 | DSGraph::NodeMapTy NodeMap; |
| 196 | SCCGraph->cloneInto(G, SCCGraph->getScalarMap(), |
| 197 | SCCGraph->getReturnNodes(), NodeMap); |
| 198 | } |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 199 | // Update the DSInfo map and delete the old graph... |
Chris Lattner | a67138d | 2004-01-31 21:02:18 +0000 | [diff] [blame] | 200 | for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(), |
| 201 | E = G.getReturnNodes().end(); I != E; ++I) |
| 202 | DSInfo[I->first] = SCCGraph; |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 203 | delete &G; |
| 204 | } |
| 205 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 206 | |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 207 | // Clean up the graph before we start inlining a bunch again... |
Chris Lattner | ac6d485 | 2004-11-08 21:08:46 +0000 | [diff] [blame] | 208 | SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 210 | // Now that we have one big happy family, resolve all of the call sites in |
| 211 | // the graph... |
| 212 | calculateGraph(*SCCGraph); |
| 213 | DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize() |
| 214 | << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 215 | |
| 216 | std::cerr << "DONE with SCC #: " << MyID << "\n"; |
| 217 | |
| 218 | // We never have to revisit "SCC" processed functions... |
| 219 | |
| 220 | // Drop the stuff we don't need from the end of the stack |
| 221 | Stack.erase(FirstInSCC, Stack.end()); |
| 222 | return MyID; |
| 223 | } |
| 224 | |
| 225 | return MyID; // == Min |
| 226 | } |
| 227 | |
| 228 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 229 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 230 | // our memory... here... |
| 231 | // |
| 232 | void BUDataStructures::releaseMemory() { |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 233 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 234 | E = DSInfo.end(); I != E; ++I) { |
| 235 | I->second->getReturnNodes().erase(I->first); |
| 236 | if (I->second->getReturnNodes().empty()) |
| 237 | delete I->second; |
| 238 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 239 | |
| 240 | // Empty map so next time memory is released, data structures are not |
| 241 | // re-deleted. |
| 242 | DSInfo.clear(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 243 | delete GlobalsGraph; |
| 244 | GlobalsGraph = 0; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 247 | void BUDataStructures::calculateGraph(DSGraph &Graph) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 248 | // Move our call site list into TempFCs so that inline call sites go into the |
| 249 | // new call site list and doesn't invalidate our iterators! |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 250 | std::list<DSCallSite> TempFCs; |
| 251 | std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls(); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 252 | TempFCs.swap(AuxCallsList); |
Chris Lattner | 8a5db46 | 2002-11-11 00:01:34 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 254 | DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes(); |
| 255 | |
Chris Lattner | f189bce | 2005-02-01 17:35:52 +0000 | [diff] [blame^] | 256 | // Print out multi-call sites. |
| 257 | bool Printed = false; |
| 258 | for (std::list<DSCallSite>::iterator I = TempFCs.begin(), E = TempFCs.end(); |
| 259 | I != E; ++I) { |
| 260 | if (!I->isDirectCall()) { |
| 261 | DSNode *Node = I->getCalleeNode(); |
| 262 | if (Node->getGlobals().size() > 1) { |
| 263 | if (!Printed) |
| 264 | std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n"; |
| 265 | std::cerr << " calls " << Node->getGlobals().size() |
| 266 | << " fns from site: " << *I->getCallSite().getInstruction(); |
| 267 | unsigned NumToPrint = Node->getGlobals().size(); |
| 268 | if (NumToPrint > 5) NumToPrint = 5; |
| 269 | std::cerr << " Fns ="; |
| 270 | for (unsigned i = 0; i != NumToPrint; ++i) |
| 271 | std::cerr << " " << Node->getGlobals()[i]->getName(); |
| 272 | std::cerr << "\n"; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // Loop over all of the resolvable call sites. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 279 | DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs); |
| 280 | DSCallSiteIterator E = DSCallSiteIterator::end(TempFCs); |
| 281 | |
| 282 | // If DSCallSiteIterator skipped over any call sites, they are unresolvable: |
| 283 | // move them back to the AuxCallsList. |
| 284 | std::list<DSCallSite>::iterator LastCallSiteIdx = TempFCs.begin(); |
| 285 | while (LastCallSiteIdx != I.getCallSiteIdx()) |
| 286 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++); |
Chris Lattner | a107905 | 2002-11-10 06:52:47 +0000 | [diff] [blame] | 287 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 288 | while (I != E) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 289 | // Resolve the current call... |
| 290 | Function *Callee = *I; |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 291 | DSCallSite CS = I.getCallSite(); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 292 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 293 | if (Callee->isExternal()) { |
| 294 | // Ignore this case, simple varargs functions we cannot stub out! |
Chris Lattner | 6acfe92 | 2003-11-13 05:04:19 +0000 | [diff] [blame] | 295 | } else if (ReturnNodes.count(Callee)) { |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 296 | // Self recursion... simply link up the formal arguments with the |
| 297 | // actual arguments... |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 298 | DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n"); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 299 | |
| 300 | // Handle self recursion by resolving the arguments and return value |
Chris Lattner | 0eea618 | 2003-06-30 05:09:58 +0000 | [diff] [blame] | 301 | Graph.mergeInGraph(CS, *Callee, Graph, 0); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 302 | |
| 303 | } else { |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 304 | ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(), |
| 305 | Callee)); |
Chris Lattner | 744f939 | 2003-07-02 04:37:48 +0000 | [diff] [blame] | 306 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 307 | // Get the data structure graph for the called function. |
| 308 | // |
| 309 | DSGraph &GI = getDSGraph(*Callee); // Graph to inline |
Chris Lattner | 0321b68 | 2004-02-27 20:05:15 +0000 | [diff] [blame] | 310 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 311 | DEBUG(std::cerr << " Inlining graph for " << Callee->getName() |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 312 | << "[" << GI.getGraphSize() << "+" |
Chris Lattner | 5d5b6d6 | 2003-07-01 16:04:18 +0000 | [diff] [blame] | 313 | << GI.getAuxFunctionCalls().size() << "] into '" |
| 314 | << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+" |
Chris Lattner | 20167e3 | 2003-02-03 19:11:38 +0000 | [diff] [blame] | 315 | << Graph.getAuxFunctionCalls().size() << "]\n"); |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 316 | Graph.mergeInGraph(CS, *Callee, GI, |
Vikram S. Adve | 61ff029 | 2002-11-27 17:41:13 +0000 | [diff] [blame] | 317 | DSGraph::KeepModRefBits | |
| 318 | DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes); |
Chris Lattner | d391d70 | 2003-07-02 20:24:42 +0000 | [diff] [blame] | 319 | ++NumBUInlines; |
Chris Lattner | ae5f603 | 2002-11-17 22:16:28 +0000 | [diff] [blame] | 320 | |
| 321 | #if 0 |
| 322 | Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" + |
| 323 | Callee->getName()); |
| 324 | #endif |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 325 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 326 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 327 | LastCallSiteIdx = I.getCallSiteIdx(); |
| 328 | ++I; // Move to the next call site. |
| 329 | |
| 330 | if (I.getCallSiteIdx() != LastCallSiteIdx) { |
| 331 | ++LastCallSiteIdx; // Skip over the site we already processed. |
| 332 | |
| 333 | // If there are call sites that get skipped over, move them to the aux |
| 334 | // calls list: they are not resolvable. |
| 335 | if (I != E) |
| 336 | while (LastCallSiteIdx != I.getCallSiteIdx()) |
| 337 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++); |
| 338 | else |
| 339 | while (LastCallSiteIdx != TempFCs.end()) |
| 340 | AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++); |
| 341 | } |
| 342 | } |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 343 | |
| 344 | TempFCs.clear(); |
| 345 | |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 346 | // Recompute the Incomplete markers |
Chris Lattner | d10b5fd | 2004-02-20 23:52:15 +0000 | [diff] [blame] | 347 | assert(Graph.getInlinedGlobals().empty()); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 348 | Graph.maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 349 | Graph.markIncompleteNodes(DSGraph::MarkFormalArgs); |
Vikram S. Adve | 1da1d32 | 2003-07-16 21:42:03 +0000 | [diff] [blame] | 350 | |
| 351 | // Delete dead nodes. Treat globals that are unreachable but that can |
| 352 | // reach live nodes as live. |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 353 | Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 355 | // When this graph is finalized, clone the globals in the graph into the |
| 356 | // globals graph to make sure it has everything, from all graphs. |
| 357 | DSScalarMap &MainSM = Graph.getScalarMap(); |
| 358 | ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit); |
| 359 | |
Chris Lattner | 3b7b81b | 2004-10-31 21:54:51 +0000 | [diff] [blame] | 360 | // Clone everything reachable from globals in the function graph into the |
Chris Lattner | 3567937 | 2004-02-21 00:30:28 +0000 | [diff] [blame] | 361 | // globals graph. |
| 362 | for (DSScalarMap::global_iterator I = MainSM.global_begin(), |
| 363 | E = MainSM.global_end(); I != E; ++I) |
| 364 | RC.getClonedNH(MainSM[*I]); |
| 365 | |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 366 | //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName()); |
Chris Lattner | a9c9c02 | 2002-11-11 21:35:13 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 368 | |
| 369 | static const Function *getFnForValue(const Value *V) { |
| 370 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 371 | return I->getParent()->getParent(); |
| 372 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 373 | return A->getParent(); |
| 374 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 375 | return BB->getParent(); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program. |
| 380 | /// These correspond to the interfaces defined in the AliasAnalysis class. |
| 381 | void BUDataStructures::deleteValue(Value *V) { |
| 382 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 383 | // If this is a function local value, just delete it from the scalar map! |
| 384 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 385 | return; |
| 386 | } |
| 387 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 388 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 389 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 390 | "cannot handle scc's"); |
| 391 | delete DSInfo[F]; |
| 392 | DSInfo.erase(F); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 397 | } |
| 398 | |
| 399 | void BUDataStructures::copyValue(Value *From, Value *To) { |
| 400 | if (From == To) return; |
| 401 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 402 | // If this is a function local value, just delete it from the scalar map! |
| 403 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 408 | Function *ToF = cast<Function>(To); |
| 409 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
| 410 | DSGraph *NG = new DSGraph(getDSGraph(*FromF)); |
| 411 | DSInfo[ToF] = NG; |
| 412 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 413 | |
| 414 | // Change the Function* is the returnnodes map to the ToF. |
| 415 | DSNodeHandle Ret = NG->getReturnNodes().begin()->second; |
| 416 | NG->getReturnNodes().clear(); |
| 417 | NG->getReturnNodes()[ToF] = Ret; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!"); |
| 422 | } |