Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 1 | //===- TopDownClosure.cpp - Compute the top-down interprocedure 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 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the TDDataStructures class, which represents the |
| 11 | // Top-down Interprocedural closure of the data structure graph over the |
| 12 | // program. This is useful (but not strictly necessary?) for applications |
| 13 | // like pointer analysis. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
| 22 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | RegisterAnalysis<TDDataStructures> // Register the pass |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 27 | Y("tddatastructure", "Top-down Data Structure Analysis"); |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 28 | |
| 29 | Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined"); |
| 30 | } |
| 31 | |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 32 | void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N, |
| 33 | hash_set<DSNode*> &Visited) { |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 34 | if (!N || Visited.count(N)) return; |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 35 | Visited.insert(N); |
| 36 | |
| 37 | for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) { |
| 38 | DSNodeHandle &NH = N->getLink(i*N->getPointerSize()); |
| 39 | if (DSNode *NN = NH.getNode()) { |
| 40 | const std::vector<GlobalValue*> &Globals = NN->getGlobals(); |
| 41 | for (unsigned G = 0, e = Globals.size(); G != e; ++G) |
| 42 | if (Function *F = dyn_cast<Function>(Globals[G])) |
| 43 | ArgsRemainIncomplete.insert(F); |
| 44 | |
| 45 | markReachableFunctionsExternallyAccessible(NN, Visited); |
| 46 | } |
| 47 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 48 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 50 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 51 | // run - Calculate the top down data structure graphs for each function in the |
| 52 | // program. |
| 53 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 54 | bool TDDataStructures::runOnModule(Module &M) { |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 55 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
Chris Lattner | 312edd3 | 2003-06-28 22:14:55 +0000 | [diff] [blame] | 56 | GlobalsGraph = new DSGraph(BU.getGlobalsGraph()); |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 57 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 58 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 59 | // Figure out which functions must not mark their arguments complete because |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 60 | // they are accessible outside this compilation unit. Currently, these |
| 61 | // arguments are functions which are reachable by global variables in the |
| 62 | // globals graph. |
Chris Lattner | 6d8f3dc | 2004-01-28 03:12:48 +0000 | [diff] [blame] | 63 | const DSScalarMap &GGSM = GlobalsGraph->getScalarMap(); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 64 | hash_set<DSNode*> Visited; |
Misha Brukman | 96a8bd7 | 2004-04-29 04:05:30 +0000 | [diff] [blame] | 65 | for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end(); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 66 | I != E; ++I) |
Misha Brukman | 96a8bd7 | 2004-04-29 04:05:30 +0000 | [diff] [blame] | 67 | markReachableFunctionsExternallyAccessible(GGSM.find(*I)->second.getNode(), |
| 68 | Visited); |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 69 | |
| 70 | // Loop over unresolved call nodes. Any functions passed into (but not |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 71 | // returned!) from unresolvable call nodes may be invoked outside of the |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 72 | // current module. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 73 | for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(), |
| 74 | E = GlobalsGraph->afc_end(); I != E; ++I) |
| 75 | for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg) |
| 76 | markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(), |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 77 | Visited); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 78 | Visited.clear(); |
| 79 | |
| 80 | // Functions without internal linkage also have unknown incoming arguments! |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 81 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 82 | if (!I->isExternal() && !I->hasInternalLinkage()) |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 83 | ArgsRemainIncomplete.insert(I); |
| 84 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 85 | // We want to traverse the call graph in reverse post-order. To do this, we |
| 86 | // calculate a post-order traversal, then reverse it. |
| 87 | hash_set<DSGraph*> VisitedGraph; |
| 88 | std::vector<DSGraph*> PostOrder; |
| 89 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 90 | getAnalysis<BUDataStructures>().getActualCallees(); |
| 91 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 92 | // Calculate top-down from main... |
| 93 | if (Function *F = M.getMainFunction()) |
Chris Lattner | 61691c5 | 2003-07-02 23:44:15 +0000 | [diff] [blame] | 94 | ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 95 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 96 | // Next calculate the graphs for each unreachable function... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 97 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 98 | ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees); |
| 99 | |
| 100 | VisitedGraph.clear(); // Release memory! |
| 101 | |
| 102 | // Visit each of the graphs in reverse post-order now! |
| 103 | while (!PostOrder.empty()) { |
| 104 | inlineGraphIntoCallees(*PostOrder.back()); |
| 105 | PostOrder.pop_back(); |
| 106 | } |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 107 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 108 | ArgsRemainIncomplete.clear(); |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 109 | GlobalsGraph->removeTriviallyDeadNodes(); |
| 110 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 115 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 116 | DSGraph *&G = DSInfo[&F]; |
| 117 | if (G == 0) { // Not created yet? Clone BU graph... |
| 118 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F)); |
| 119 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 120 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 121 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 122 | } |
| 123 | return *G; |
| 124 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 125 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 127 | void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited, |
| 128 | std::vector<DSGraph*> &PostOrder, |
| 129 | const BUDataStructures::ActualCalleesTy &ActualCallees) { |
| 130 | if (F.isExternal()) return; |
| 131 | DSGraph &G = getOrCreateDSGraph(F); |
| 132 | if (Visited.count(&G)) return; |
| 133 | Visited.insert(&G); |
| 134 | |
| 135 | // Recursively traverse all of the callee graphs. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 136 | for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E; ++CI) { |
| 137 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 138 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
| 139 | BUDataStructures::ActualCalleesTy::const_iterator> |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 140 | IP = ActualCallees.equal_range(CallI); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 142 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 143 | I != IP.second; ++I) |
| 144 | ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees); |
| 145 | } |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 147 | PostOrder.push_back(&G); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 153 | |
| 154 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 155 | // our memory... here... |
| 156 | // |
| 157 | // FIXME: This should be releaseMemory and will work fine, except that LoadVN |
| 158 | // has no way to extend the lifetime of the pass, which screws up ds-aa. |
| 159 | // |
| 160 | void TDDataStructures::releaseMyMemory() { |
| 161 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 162 | E = DSInfo.end(); I != E; ++I) { |
| 163 | I->second->getReturnNodes().erase(I->first); |
| 164 | if (I->second->getReturnNodes().empty()) |
| 165 | delete I->second; |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 168 | // Empty map so next time memory is released, data structures are not |
| 169 | // re-deleted. |
| 170 | DSInfo.clear(); |
| 171 | delete GlobalsGraph; |
| 172 | GlobalsGraph = 0; |
| 173 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 174 | |
| 175 | void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) { |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 176 | // Recompute the Incomplete markers and eliminate unreachable nodes. |
| 177 | Graph.maskIncompleteMarkers(); |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 178 | |
| 179 | // If any of the functions has incomplete incoming arguments, don't mark any |
| 180 | // of them as complete. |
| 181 | bool HasIncompleteArgs = false; |
| 182 | const DSGraph::ReturnNodesTy &GraphReturnNodes = Graph.getReturnNodes(); |
| 183 | for (DSGraph::ReturnNodesTy::const_iterator I = GraphReturnNodes.begin(), |
| 184 | E = GraphReturnNodes.end(); I != E; ++I) |
| 185 | if (ArgsRemainIncomplete.count(I->first)) { |
| 186 | HasIncompleteArgs = true; |
| 187 | break; |
| 188 | } |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 189 | |
| 190 | // Now fold in the necessary globals from the GlobalsGraph. A global G |
| 191 | // must be folded in if it exists in the current graph (i.e., is not dead) |
| 192 | // and it was not inlined from any of my callers. If it was inlined from |
| 193 | // a caller, it would have been fully consistent with the GlobalsGraph |
| 194 | // in the caller so folding in is not necessary. Otherwise, this node came |
| 195 | // solely from this function's BU graph and so has to be made consistent. |
| 196 | // |
| 197 | Graph.updateFromGlobalGraph(); |
| 198 | |
| 199 | // Recompute the Incomplete markers. Depends on whether args are complete |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 200 | unsigned Flags |
| 201 | = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs; |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 202 | Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 203 | |
| 204 | // Delete dead nodes. Treat globals that are unreachable as dead also. |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 205 | Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
| 206 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 207 | // We are done with computing the current TD Graph! Now move on to |
| 208 | // inlining the current graph into the graphs for its callees, if any. |
| 209 | // |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 210 | if (Graph.fc_begin() == Graph.fc_end()) { |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 211 | DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames() |
| 212 | << "\n"); |
| 213 | return; |
| 214 | } |
| 215 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 216 | // Now that we have information about all of the callees, propagate the |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 217 | // current graph into the callees. Clone only the reachable subgraph at |
| 218 | // each call-site, not the entire graph (even though the entire graph |
| 219 | // would be cloned only once, this should still be better on average). |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 220 | // |
| 221 | DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into " |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 222 | << Graph.getFunctionCalls().size() << " call nodes.\n"); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 223 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 224 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 225 | getAnalysis<BUDataStructures>().getActualCallees(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 226 | |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 227 | // Loop over all the call sites and all the callees at each call site. Build |
| 228 | // a mapping from called DSGraph's to the call sites in this function that |
| 229 | // invoke them. This is useful because we can be more efficient if there are |
| 230 | // multiple call sites to the callees in the graph from this caller. |
| 231 | std::multimap<DSGraph*, std::pair<Function*, const DSCallSite*> > CallSites; |
| 232 | |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 233 | for (DSGraph::fc_iterator CI = Graph.fc_begin(), E = Graph.fc_end(); |
| 234 | CI != E; ++CI) { |
| 235 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 236 | // For each function in the invoked function list at this call site... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 237 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 238 | BUDataStructures::ActualCalleesTy::const_iterator> |
| 239 | IP = ActualCallees.equal_range(CallI); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 240 | // Loop over each actual callee at this call site |
| 241 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 242 | I != IP.second; ++I) { |
| 243 | DSGraph& CalleeGraph = getDSGraph(*I->second); |
Chris Lattner | d7be188 | 2005-02-04 18:58:04 +0000 | [diff] [blame^] | 244 | if (&CalleeGraph != &Graph) |
| 245 | CallSites.insert(std::make_pair(&CalleeGraph, |
| 246 | std::make_pair(I->second, &*CI))); |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 249 | |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 250 | // Now that we built the mapping, actually perform the inlining a callee graph |
| 251 | // at a time. |
| 252 | std::multimap<DSGraph*,std::pair<Function*,const DSCallSite*> >::iterator CSI; |
| 253 | for (CSI = CallSites.begin(); CSI != CallSites.end(); ) { |
| 254 | DSGraph &CalleeGraph = *CSI->first; |
| 255 | // Iterate through all of the call sites of this graph, cloning and merging |
| 256 | // any nodes required by the call. |
| 257 | ReachabilityCloner RC(CalleeGraph, Graph, DSGraph::StripModRefBits); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 258 | |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 259 | // Clone over any global nodes that appear in both graphs. |
Chris Lattner | 6d8f3dc | 2004-01-28 03:12:48 +0000 | [diff] [blame] | 260 | for (DSScalarMap::global_iterator |
| 261 | SI = CalleeGraph.getScalarMap().global_begin(), |
| 262 | SE = CalleeGraph.getScalarMap().global_end(); SI != SE; ++SI) { |
| 263 | DSScalarMap::const_iterator GI = Graph.getScalarMap().find(*SI); |
| 264 | if (GI != Graph.getScalarMap().end()) |
| 265 | RC.merge(CalleeGraph.getNodeForValue(*SI), GI->second); |
| 266 | } |
Chris Lattner | 0e74412 | 2002-10-17 04:26:54 +0000 | [diff] [blame] | 267 | |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 268 | // Loop over all of the distinct call sites in the caller of the callee. |
| 269 | for (; CSI != CallSites.end() && CSI->first == &CalleeGraph; ++CSI) { |
| 270 | Function &CF = *CSI->second.first; |
| 271 | const DSCallSite &CS = *CSI->second.second; |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 272 | DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '" |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 273 | << CalleeGraph.getFunctionNames() |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 274 | << "': " << CF.getFunctionType()->getNumParams() |
| 275 | << " args\n at call site (DSCallSite*) 0x" << &CS << "\n"); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 276 | |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 277 | // Get the formal argument and return nodes for the called function and |
| 278 | // merge them with the cloned subgraph. |
| 279 | RC.mergeCallSite(CalleeGraph.getCallSiteForArguments(CF), CS); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 280 | ++NumTDInlines; |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | e0fbd48 | 2003-02-09 18:42:43 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 283 | |
| 284 | DEBUG(std::cerr << " [TD] Done inlining into callees for: " |
| 285 | << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+" |
| 286 | << Graph.getFunctionCalls().size() << "]\n"); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 287 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 288 | |
| 289 | static const Function *getFnForValue(const Value *V) { |
| 290 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 291 | return I->getParent()->getParent(); |
| 292 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 293 | return A->getParent(); |
| 294 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 295 | return BB->getParent(); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | void TDDataStructures::deleteValue(Value *V) { |
| 300 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 301 | // If this is a function local value, just delete it from the scalar map! |
| 302 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 303 | return; |
| 304 | } |
| 305 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 306 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 307 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 308 | "cannot handle scc's"); |
| 309 | delete DSInfo[F]; |
| 310 | DSInfo.erase(F); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 315 | } |
| 316 | |
| 317 | void TDDataStructures::copyValue(Value *From, Value *To) { |
| 318 | if (From == To) return; |
| 319 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 320 | // If this is a function local value, just delete it from the scalar map! |
| 321 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 326 | Function *ToF = cast<Function>(To); |
| 327 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
| 328 | DSGraph *NG = new DSGraph(getDSGraph(*FromF)); |
| 329 | DSInfo[ToF] = NG; |
| 330 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 331 | |
| 332 | // Change the Function* is the returnnodes map to the ToF. |
| 333 | DSNodeHandle Ret = NG->getReturnNodes().begin()->second; |
| 334 | NG->getReturnNodes().clear(); |
| 335 | NG->getReturnNodes()[ToF] = Ret; |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!"); |
| 340 | } |