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()) { |
Chris Lattner | 9454dda | 2005-03-20 02:39:49 +0000 | [diff] [blame] | 40 | std::vector<Function*> Functions; |
| 41 | NN->addFullFunctionList(Functions); |
| 42 | ArgsRemainIncomplete.insert(Functions.begin(), Functions.end()); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 43 | markReachableFunctionsExternallyAccessible(NN, Visited); |
| 44 | } |
| 45 | } |
Chris Lattner | 4923d1b | 2003-02-03 22:51:28 +0000 | [diff] [blame] | 46 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 48 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 49 | // run - Calculate the top down data structure graphs for each function in the |
| 50 | // program. |
| 51 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 52 | bool TDDataStructures::runOnModule(Module &M) { |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 53 | BUDataStructures &BU = getAnalysis<BUDataStructures>(); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 54 | GlobalECs = BU.getGlobalECs(); |
| 55 | GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs); |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 56 | GlobalsGraph->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 58 | // Figure out which functions must not mark their arguments complete because |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 59 | // they are accessible outside this compilation unit. Currently, these |
| 60 | // arguments are functions which are reachable by global variables in the |
| 61 | // globals graph. |
Chris Lattner | 6d8f3dc | 2004-01-28 03:12:48 +0000 | [diff] [blame] | 62 | const DSScalarMap &GGSM = GlobalsGraph->getScalarMap(); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 63 | hash_set<DSNode*> Visited; |
Misha Brukman | 96a8bd7 | 2004-04-29 04:05:30 +0000 | [diff] [blame] | 64 | for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end(); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 65 | I != E; ++I) |
Misha Brukman | 96a8bd7 | 2004-04-29 04:05:30 +0000 | [diff] [blame] | 66 | markReachableFunctionsExternallyAccessible(GGSM.find(*I)->second.getNode(), |
| 67 | Visited); |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 68 | |
| 69 | // Loop over unresolved call nodes. Any functions passed into (but not |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 70 | // returned!) from unresolvable call nodes may be invoked outside of the |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 71 | // current module. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 72 | for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(), |
| 73 | E = GlobalsGraph->afc_end(); I != E; ++I) |
| 74 | for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg) |
| 75 | markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(), |
Chris Lattner | 11fc930 | 2003-09-20 23:58:33 +0000 | [diff] [blame] | 76 | Visited); |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 77 | Visited.clear(); |
| 78 | |
| 79 | // Functions without internal linkage also have unknown incoming arguments! |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 80 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 3b0a9be | 2003-09-20 22:24:04 +0000 | [diff] [blame] | 81 | if (!I->isExternal() && !I->hasInternalLinkage()) |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 82 | ArgsRemainIncomplete.insert(I); |
| 83 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 84 | // We want to traverse the call graph in reverse post-order. To do this, we |
| 85 | // calculate a post-order traversal, then reverse it. |
| 86 | hash_set<DSGraph*> VisitedGraph; |
| 87 | std::vector<DSGraph*> PostOrder; |
| 88 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 89 | getAnalysis<BUDataStructures>().getActualCallees(); |
| 90 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 91 | // Calculate top-down from main... |
| 92 | if (Function *F = M.getMainFunction()) |
Chris Lattner | 61691c5 | 2003-07-02 23:44:15 +0000 | [diff] [blame] | 93 | ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 94 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 95 | // Next calculate the graphs for each unreachable function... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 96 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 97 | ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees); |
| 98 | |
| 99 | VisitedGraph.clear(); // Release memory! |
| 100 | |
| 101 | // Visit each of the graphs in reverse post-order now! |
| 102 | while (!PostOrder.empty()) { |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 103 | InlineCallersIntoGraph(*PostOrder.back()); |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 104 | PostOrder.pop_back(); |
| 105 | } |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 106 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 107 | ArgsRemainIncomplete.clear(); |
Chris Lattner | c3f5f77 | 2004-02-08 01:51:48 +0000 | [diff] [blame] | 108 | GlobalsGraph->removeTriviallyDeadNodes(); |
| 109 | |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 114 | DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) { |
| 115 | DSGraph *&G = DSInfo[&F]; |
| 116 | if (G == 0) { // Not created yet? Clone BU graph... |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 117 | G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 118 | G->getAuxFunctionCalls().clear(); |
Chris Lattner | 24d8007 | 2003-02-04 00:59:32 +0000 | [diff] [blame] | 119 | G->setPrintAuxCalls(); |
Chris Lattner | aa0b468 | 2002-11-09 21:12:07 +0000 | [diff] [blame] | 120 | G->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | 7a21163 | 2002-11-08 21:28:37 +0000 | [diff] [blame] | 121 | } |
| 122 | return *G; |
| 123 | } |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 124 | |
Chris Lattner | dea8146 | 2003-06-29 22:37:07 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 126 | void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited, |
| 127 | std::vector<DSGraph*> &PostOrder, |
| 128 | const BUDataStructures::ActualCalleesTy &ActualCallees) { |
| 129 | if (F.isExternal()) return; |
| 130 | DSGraph &G = getOrCreateDSGraph(F); |
| 131 | if (Visited.count(&G)) return; |
| 132 | Visited.insert(&G); |
| 133 | |
| 134 | // Recursively traverse all of the callee graphs. |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 135 | for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E; ++CI) { |
| 136 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 137 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
| 138 | BUDataStructures::ActualCalleesTy::const_iterator> |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 139 | IP = ActualCallees.equal_range(CallI); |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 141 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 142 | I != IP.second; ++I) |
| 143 | ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees); |
| 144 | } |
Chris Lattner | 198be22 | 2002-10-21 19:47:18 +0000 | [diff] [blame] | 145 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 146 | PostOrder.push_back(&G); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 151 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 152 | |
| 153 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 154 | // our memory... here... |
| 155 | // |
| 156 | // FIXME: This should be releaseMemory and will work fine, except that LoadVN |
| 157 | // has no way to extend the lifetime of the pass, which screws up ds-aa. |
| 158 | // |
| 159 | void TDDataStructures::releaseMyMemory() { |
| 160 | for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 161 | E = DSInfo.end(); I != E; ++I) { |
| 162 | I->second->getReturnNodes().erase(I->first); |
| 163 | if (I->second->getReturnNodes().empty()) |
| 164 | delete I->second; |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 6c87461 | 2003-07-02 23:42:48 +0000 | [diff] [blame] | 167 | // Empty map so next time memory is released, data structures are not |
| 168 | // re-deleted. |
| 169 | DSInfo.clear(); |
| 170 | delete GlobalsGraph; |
| 171 | GlobalsGraph = 0; |
| 172 | } |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 173 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 174 | /// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph |
| 175 | /// into it, then recompute completeness of nodes in the resultant graph. |
| 176 | void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) { |
| 177 | // Inline caller graphs into this graph. First step, get the list of call |
| 178 | // sites that call into this graph. |
| 179 | std::vector<CallerCallEdge> EdgesFromCaller; |
| 180 | std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator |
| 181 | CEI = CallerEdges.find(&DSG); |
| 182 | if (CEI != CallerEdges.end()) { |
| 183 | std::swap(CEI->second, EdgesFromCaller); |
| 184 | CallerEdges.erase(CEI); |
| 185 | } |
| 186 | |
| 187 | // Sort the caller sites to provide a by-caller-graph ordering. |
| 188 | std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end()); |
| 189 | |
| 190 | |
Chris Lattner | c2b9480 | 2005-03-21 08:43:32 +0000 | [diff] [blame^] | 191 | // Merge information from the globals graph into this graph. FIXME: This is |
| 192 | // stupid. Instead of us cloning information from the GG into this graph, |
| 193 | // then having RemoveDeadNodes clone it back, we should do all of this as a |
| 194 | // post-pass over all of the graphs. We need to take cloning out of |
| 195 | // removeDeadNodes and gut removeDeadNodes at the same time first though. :( |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 196 | { |
| 197 | DSGraph &GG = *DSG.getGlobalsGraph(); |
| 198 | ReachabilityCloner RC(DSG, GG, |
| 199 | DSGraph::DontCloneCallNodes | |
| 200 | DSGraph::DontCloneAuxCallNodes); |
| 201 | for (DSScalarMap::global_iterator |
| 202 | GI = DSG.getScalarMap().global_begin(), |
| 203 | E = DSG.getScalarMap().global_end(); GI != E; ++GI) |
| 204 | RC.getClonedNH(GG.getNodeForValue(*GI)); |
| 205 | |
| 206 | |
| 207 | } |
| 208 | |
| 209 | DEBUG(std::cerr << "[TD] Inlining callers into '" << DSG.getFunctionNames() |
| 210 | << "'\n"); |
| 211 | |
| 212 | // Iteratively inline caller graphs into this graph. |
| 213 | while (!EdgesFromCaller.empty()) { |
| 214 | DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph; |
| 215 | |
| 216 | // Iterate through all of the call sites of this graph, cloning and merging |
| 217 | // any nodes required by the call. |
| 218 | ReachabilityCloner RC(DSG, CallerGraph, |
| 219 | DSGraph::DontCloneCallNodes | |
| 220 | DSGraph::DontCloneAuxCallNodes); |
| 221 | |
| 222 | // Inline all call sites from this caller graph. |
| 223 | do { |
| 224 | const DSCallSite &CS = *EdgesFromCaller.back().CS; |
| 225 | Function &CF = *EdgesFromCaller.back().CalledFunction; |
| 226 | DEBUG(std::cerr << " [TD] Inlining graph for call to Fn '" |
| 227 | << CF.getName() << "' from Fn '" |
| 228 | << CS.getCallSite().getInstruction()-> |
| 229 | getParent()->getParent()->getName() |
| 230 | << "': " << CF.getFunctionType()->getNumParams() |
| 231 | << " args\n"); |
| 232 | |
| 233 | // Get the formal argument and return nodes for the called function and |
| 234 | // merge them with the cloned subgraph. |
| 235 | RC.mergeCallSite(DSG.getCallSiteForArguments(CF), CS); |
| 236 | ++NumTDInlines; |
| 237 | |
| 238 | EdgesFromCaller.pop_back(); |
| 239 | } while (!EdgesFromCaller.empty() && |
| 240 | EdgesFromCaller.back().CallerGraph == &CallerGraph); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | // Next, now that this graph is finalized, we need to recompute the |
| 245 | // incompleteness markers for this graph and remove unreachable nodes. |
| 246 | DSG.maskIncompleteMarkers(); |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 247 | |
| 248 | // If any of the functions has incomplete incoming arguments, don't mark any |
| 249 | // of them as complete. |
| 250 | bool HasIncompleteArgs = false; |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 251 | for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(), |
| 252 | E = DSG.retnodes_end(); I != E; ++I) |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 253 | if (ArgsRemainIncomplete.count(I->first)) { |
| 254 | HasIncompleteArgs = true; |
| 255 | break; |
| 256 | } |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 257 | |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 258 | // Recompute the Incomplete markers. Depends on whether args are complete |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 259 | unsigned Flags |
| 260 | = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs; |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 261 | DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 262 | |
| 263 | // Delete dead nodes. Treat globals that are unreachable as dead also. |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 264 | DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals); |
Chris Lattner | 4f2cfc0 | 2003-02-10 18:16:36 +0000 | [diff] [blame] | 265 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 266 | // We are done with computing the current TD Graph! Finally, before we can |
| 267 | // finish processing this function, we figure out which functions it calls and |
| 268 | // records these call graph edges, so that we have them when we process the |
| 269 | // callee graphs. |
| 270 | if (DSG.fc_begin() == DSG.fc_end()) return; |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 271 | |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 272 | const BUDataStructures::ActualCalleesTy &ActualCallees = |
| 273 | getAnalysis<BUDataStructures>().getActualCallees(); |
Chris Lattner | 18f07a1 | 2003-07-01 16:28:11 +0000 | [diff] [blame] | 274 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 275 | // Loop over all the call sites and all the callees at each call site, and add |
| 276 | // edges to the CallerEdges structure for each callee. |
| 277 | for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end(); |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 278 | CI != E; ++CI) { |
| 279 | Instruction *CallI = CI->getCallSite().getInstruction(); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 280 | // For each function in the invoked function list at this call site... |
Chris Lattner | a8da51b | 2003-07-02 04:39:44 +0000 | [diff] [blame] | 281 | std::pair<BUDataStructures::ActualCalleesTy::const_iterator, |
Chris Lattner | a9548d9 | 2005-01-30 23:51:02 +0000 | [diff] [blame] | 282 | BUDataStructures::ActualCalleesTy::const_iterator> |
| 283 | IP = ActualCallees.equal_range(CallI); |
Vikram S. Adve | 03e19dd | 2003-07-16 21:40:28 +0000 | [diff] [blame] | 284 | // Loop over each actual callee at this call site |
| 285 | for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first; |
| 286 | I != IP.second; ++I) { |
| 287 | DSGraph& CalleeGraph = getDSGraph(*I->second); |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 288 | if (&CalleeGraph != &DSG) |
| 289 | CallerEdges[&CalleeGraph].push_back(CallerCallEdge(&DSG, &*CI, |
| 290 | I->second)); |
Chris Lattner | f325e39 | 2004-01-27 21:53:14 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Vikram S. Adve | aaeee75 | 2002-07-30 22:06:40 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 294 | |
Chris Lattner | d57e55e | 2005-03-21 04:55:35 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 296 | static const Function *getFnForValue(const Value *V) { |
| 297 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 298 | return I->getParent()->getParent(); |
| 299 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 300 | return A->getParent(); |
| 301 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 302 | return BB->getParent(); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | void TDDataStructures::deleteValue(Value *V) { |
| 307 | if (const Function *F = getFnForValue(V)) { // Function local value? |
| 308 | // If this is a function local value, just delete it from the scalar map! |
| 309 | getDSGraph(*F).getScalarMap().eraseIfExists(V); |
| 310 | return; |
| 311 | } |
| 312 | |
Chris Lattner | cff8ac2 | 2005-01-31 00:10:45 +0000 | [diff] [blame] | 313 | if (Function *F = dyn_cast<Function>(V)) { |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 314 | assert(getDSGraph(*F).getReturnNodes().size() == 1 && |
| 315 | "cannot handle scc's"); |
| 316 | delete DSInfo[F]; |
| 317 | DSInfo.erase(F); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!"); |
| 322 | } |
| 323 | |
| 324 | void TDDataStructures::copyValue(Value *From, Value *To) { |
| 325 | if (From == To) return; |
| 326 | if (const Function *F = getFnForValue(From)) { // Function local value? |
| 327 | // If this is a function local value, just delete it from the scalar map! |
| 328 | getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To); |
| 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (Function *FromF = dyn_cast<Function>(From)) { |
| 333 | Function *ToF = cast<Function>(To); |
| 334 | assert(!DSInfo.count(ToF) && "New Function already exists!"); |
Chris Lattner | f4f6227 | 2005-03-19 22:23:45 +0000 | [diff] [blame] | 335 | DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs); |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 336 | DSInfo[ToF] = NG; |
| 337 | assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!"); |
| 338 | |
| 339 | // Change the Function* is the returnnodes map to the ToF. |
Chris Lattner | a5f47ea | 2005-03-15 16:55:04 +0000 | [diff] [blame] | 340 | DSNodeHandle Ret = NG->retnodes_begin()->second; |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 341 | NG->getReturnNodes().clear(); |
| 342 | NG->getReturnNodes()[ToF] = Ret; |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!"); |
| 347 | } |