blob: d43f9fecbfccf0f22878ccceb9e5a556fb88e17d [file] [log] [blame]
Vikram S. Adveaaeee752002-07-30 22:06:40 +00001//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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. Adveaaeee752002-07-30 22:06:40 +00009//
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 Lattner4dabb2c2004-07-07 06:32:21 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
Vikram S. Adveaaeee752002-07-30 22:06:40 +000018#include "llvm/Module.h"
19#include "llvm/DerivedTypes.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000020#include "llvm/Analysis/DataStructure/DSGraph.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/Debug.h"
22#include "llvm/ADT/Statistic.h"
Chris Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattner4923d1b2003-02-03 22:51:28 +000025namespace {
26 RegisterAnalysis<TDDataStructures> // Register the pass
Chris Lattner312edd32003-06-28 22:14:55 +000027 Y("tddatastructure", "Top-down Data Structure Analysis");
Chris Lattnera8da51b2003-07-02 04:39:44 +000028
29 Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
30}
31
Chris Lattner3b0a9be2003-09-20 22:24:04 +000032void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
33 hash_set<DSNode*> &Visited) {
Chris Lattner11fc9302003-09-20 23:58:33 +000034 if (!N || Visited.count(N)) return;
Chris Lattner3b0a9be2003-09-20 22:24:04 +000035 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 Lattner9454dda2005-03-20 02:39:49 +000040 std::vector<Function*> Functions;
41 NN->addFullFunctionList(Functions);
42 ArgsRemainIncomplete.insert(Functions.begin(), Functions.end());
Chris Lattner3b0a9be2003-09-20 22:24:04 +000043 markReachableFunctionsExternallyAccessible(NN, Visited);
44 }
45 }
Chris Lattner4923d1b2003-02-03 22:51:28 +000046}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000047
Chris Lattner3b0a9be2003-09-20 22:24:04 +000048
Chris Lattneraa0b4682002-11-09 21:12:07 +000049// run - Calculate the top down data structure graphs for each function in the
50// program.
51//
Chris Lattnerb12914b2004-09-20 04:48:05 +000052bool TDDataStructures::runOnModule(Module &M) {
Chris Lattneraa0b4682002-11-09 21:12:07 +000053 BUDataStructures &BU = getAnalysis<BUDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000054 GlobalECs = BU.getGlobalECs();
55 GlobalsGraph = new DSGraph(BU.getGlobalsGraph(), GlobalECs);
Chris Lattner11fc9302003-09-20 23:58:33 +000056 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000057
Chris Lattnera8da51b2003-07-02 04:39:44 +000058 // Figure out which functions must not mark their arguments complete because
Chris Lattner3b0a9be2003-09-20 22:24:04 +000059 // 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 Lattner6d8f3dc2004-01-28 03:12:48 +000062 const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
Chris Lattner3b0a9be2003-09-20 22:24:04 +000063 hash_set<DSNode*> Visited;
Misha Brukman96a8bd72004-04-29 04:05:30 +000064 for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end();
Chris Lattner275b3012005-03-21 20:31:29 +000065 I != E; ++I) {
66 DSNode *N = GGSM.find(*I)->second.getNode();
67 if (N->isIncomplete())
68 markReachableFunctionsExternallyAccessible(N, Visited);
69 }
Chris Lattner11fc9302003-09-20 23:58:33 +000070
71 // Loop over unresolved call nodes. Any functions passed into (but not
Chris Lattnerf325e392004-01-27 21:53:14 +000072 // returned!) from unresolvable call nodes may be invoked outside of the
Chris Lattner11fc9302003-09-20 23:58:33 +000073 // current module.
Chris Lattnera9548d92005-01-30 23:51:02 +000074 for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(),
75 E = GlobalsGraph->afc_end(); I != E; ++I)
76 for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg)
77 markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(),
Chris Lattner11fc9302003-09-20 23:58:33 +000078 Visited);
Chris Lattner3b0a9be2003-09-20 22:24:04 +000079 Visited.clear();
80
81 // Functions without internal linkage also have unknown incoming arguments!
Chris Lattnera8da51b2003-07-02 04:39:44 +000082 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner3b0a9be2003-09-20 22:24:04 +000083 if (!I->isExternal() && !I->hasInternalLinkage())
Chris Lattnera8da51b2003-07-02 04:39:44 +000084 ArgsRemainIncomplete.insert(I);
85
Chris Lattner6c874612003-07-02 23:42:48 +000086 // We want to traverse the call graph in reverse post-order. To do this, we
87 // calculate a post-order traversal, then reverse it.
88 hash_set<DSGraph*> VisitedGraph;
89 std::vector<DSGraph*> PostOrder;
90 const BUDataStructures::ActualCalleesTy &ActualCallees =
91 getAnalysis<BUDataStructures>().getActualCallees();
92
Chris Lattneraa0b4682002-11-09 21:12:07 +000093 // Calculate top-down from main...
94 if (Function *F = M.getMainFunction())
Chris Lattner61691c52003-07-02 23:44:15 +000095 ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
Chris Lattneraa0b4682002-11-09 21:12:07 +000096
Vikram S. Adve03e19dd2003-07-16 21:40:28 +000097 // Next calculate the graphs for each unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +000098 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner6c874612003-07-02 23:42:48 +000099 ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
100
101 VisitedGraph.clear(); // Release memory!
102
103 // Visit each of the graphs in reverse post-order now!
104 while (!PostOrder.empty()) {
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000105 InlineCallersIntoGraph(*PostOrder.back());
Chris Lattner6c874612003-07-02 23:42:48 +0000106 PostOrder.pop_back();
107 }
Chris Lattneraa0b4682002-11-09 21:12:07 +0000108
Chris Lattner275b3012005-03-21 20:31:29 +0000109 // Free the IndCallMap.
110 while (!IndCallMap.empty()) {
111 delete IndCallMap.begin()->second;
112 IndCallMap.erase(IndCallMap.begin());
113 }
114
115
Chris Lattnera8da51b2003-07-02 04:39:44 +0000116 ArgsRemainIncomplete.clear();
Chris Lattnerc3f5f772004-02-08 01:51:48 +0000117 GlobalsGraph->removeTriviallyDeadNodes();
118
Chris Lattneraa0b4682002-11-09 21:12:07 +0000119 return false;
120}
121
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000122
Chris Lattner7a211632002-11-08 21:28:37 +0000123DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
124 DSGraph *&G = DSInfo[&F];
125 if (G == 0) { // Not created yet? Clone BU graph...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000126 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs);
Chris Lattner7a211632002-11-08 21:28:37 +0000127 G->getAuxFunctionCalls().clear();
Chris Lattner24d80072003-02-04 00:59:32 +0000128 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000129 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner7a211632002-11-08 21:28:37 +0000130 }
131 return *G;
132}
Vikram S. Adve26b98262002-10-20 21:41:02 +0000133
Chris Lattnerdea81462003-06-29 22:37:07 +0000134
Chris Lattner18f07a12003-07-01 16:28:11 +0000135void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
136 std::vector<DSGraph*> &PostOrder,
137 const BUDataStructures::ActualCalleesTy &ActualCallees) {
138 if (F.isExternal()) return;
139 DSGraph &G = getOrCreateDSGraph(F);
140 if (Visited.count(&G)) return;
141 Visited.insert(&G);
142
143 // Recursively traverse all of the callee graphs.
Chris Lattnera9548d92005-01-30 23:51:02 +0000144 for (DSGraph::fc_iterator CI = G.fc_begin(), E = G.fc_end(); CI != E; ++CI) {
145 Instruction *CallI = CI->getCallSite().getInstruction();
Chris Lattner18f07a12003-07-01 16:28:11 +0000146 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
147 BUDataStructures::ActualCalleesTy::const_iterator>
Chris Lattner808a7ae2003-09-20 16:34:13 +0000148 IP = ActualCallees.equal_range(CallI);
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000149
Chris Lattner18f07a12003-07-01 16:28:11 +0000150 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
151 I != IP.second; ++I)
152 ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
153 }
Chris Lattner198be222002-10-21 19:47:18 +0000154
Chris Lattner18f07a12003-07-01 16:28:11 +0000155 PostOrder.push_back(&G);
156}
157
158
159
Chris Lattner18f07a12003-07-01 16:28:11 +0000160
Chris Lattner6c874612003-07-02 23:42:48 +0000161
162// releaseMemory - If the pass pipeline is done with this pass, we can release
163// our memory... here...
164//
165// FIXME: This should be releaseMemory and will work fine, except that LoadVN
166// has no way to extend the lifetime of the pass, which screws up ds-aa.
167//
168void TDDataStructures::releaseMyMemory() {
169 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
170 E = DSInfo.end(); I != E; ++I) {
171 I->second->getReturnNodes().erase(I->first);
172 if (I->second->getReturnNodes().empty())
173 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000174 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000175
Chris Lattner6c874612003-07-02 23:42:48 +0000176 // Empty map so next time memory is released, data structures are not
177 // re-deleted.
178 DSInfo.clear();
179 delete GlobalsGraph;
180 GlobalsGraph = 0;
181}
Chris Lattner18f07a12003-07-01 16:28:11 +0000182
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000183/// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph
184/// into it, then recompute completeness of nodes in the resultant graph.
185void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) {
186 // Inline caller graphs into this graph. First step, get the list of call
187 // sites that call into this graph.
188 std::vector<CallerCallEdge> EdgesFromCaller;
189 std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator
190 CEI = CallerEdges.find(&DSG);
191 if (CEI != CallerEdges.end()) {
192 std::swap(CEI->second, EdgesFromCaller);
193 CallerEdges.erase(CEI);
194 }
195
196 // Sort the caller sites to provide a by-caller-graph ordering.
197 std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end());
198
199
Chris Lattnerc2b94802005-03-21 08:43:32 +0000200 // Merge information from the globals graph into this graph. FIXME: This is
201 // stupid. Instead of us cloning information from the GG into this graph,
202 // then having RemoveDeadNodes clone it back, we should do all of this as a
203 // post-pass over all of the graphs. We need to take cloning out of
204 // removeDeadNodes and gut removeDeadNodes at the same time first though. :(
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000205 {
206 DSGraph &GG = *DSG.getGlobalsGraph();
207 ReachabilityCloner RC(DSG, GG,
208 DSGraph::DontCloneCallNodes |
209 DSGraph::DontCloneAuxCallNodes);
210 for (DSScalarMap::global_iterator
211 GI = DSG.getScalarMap().global_begin(),
212 E = DSG.getScalarMap().global_end(); GI != E; ++GI)
213 RC.getClonedNH(GG.getNodeForValue(*GI));
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000214 }
215
216 DEBUG(std::cerr << "[TD] Inlining callers into '" << DSG.getFunctionNames()
217 << "'\n");
218
219 // Iteratively inline caller graphs into this graph.
220 while (!EdgesFromCaller.empty()) {
221 DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph;
222
223 // Iterate through all of the call sites of this graph, cloning and merging
224 // any nodes required by the call.
225 ReachabilityCloner RC(DSG, CallerGraph,
226 DSGraph::DontCloneCallNodes |
227 DSGraph::DontCloneAuxCallNodes);
228
229 // Inline all call sites from this caller graph.
230 do {
231 const DSCallSite &CS = *EdgesFromCaller.back().CS;
232 Function &CF = *EdgesFromCaller.back().CalledFunction;
Chris Lattner275b3012005-03-21 20:31:29 +0000233 DEBUG(std::cerr << " [TD] Inlining graph into Fn '"
234 << CF.getName() << "' from ");
235 if (CallerGraph.getReturnNodes().empty())
236 DEBUG(std::cerr << "SYNTHESIZED INDIRECT GRAPH");
237 else
238 DEBUG (std::cerr << "Fn '"
239 << CS.getCallSite().getInstruction()->
240 getParent()->getParent()->getName() << "'");
241 DEBUG(std::cerr << ": " << CF.getFunctionType()->getNumParams()
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000242 << " args\n");
243
244 // Get the formal argument and return nodes for the called function and
245 // merge them with the cloned subgraph.
Chris Lattner275b3012005-03-21 20:31:29 +0000246 DSCallSite T1 = DSG.getCallSiteForArguments(CF);
247 RC.mergeCallSite(T1, CS);
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000248 ++NumTDInlines;
249
250 EdgesFromCaller.pop_back();
251 } while (!EdgesFromCaller.empty() &&
252 EdgesFromCaller.back().CallerGraph == &CallerGraph);
253 }
254
255
256 // Next, now that this graph is finalized, we need to recompute the
257 // incompleteness markers for this graph and remove unreachable nodes.
258 DSG.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000259
260 // If any of the functions has incomplete incoming arguments, don't mark any
261 // of them as complete.
262 bool HasIncompleteArgs = false;
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000263 for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(),
264 E = DSG.retnodes_end(); I != E; ++I)
Chris Lattnera8da51b2003-07-02 04:39:44 +0000265 if (ArgsRemainIncomplete.count(I->first)) {
266 HasIncompleteArgs = true;
267 break;
268 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000269
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000270 // Recompute the Incomplete markers. Depends on whether args are complete
Chris Lattnera8da51b2003-07-02 04:39:44 +0000271 unsigned Flags
272 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000273 DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000274
275 // Delete dead nodes. Treat globals that are unreachable as dead also.
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000276 DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000277
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000278 // We are done with computing the current TD Graph! Finally, before we can
279 // finish processing this function, we figure out which functions it calls and
280 // records these call graph edges, so that we have them when we process the
281 // callee graphs.
282 if (DSG.fc_begin() == DSG.fc_end()) return;
Chris Lattner18f07a12003-07-01 16:28:11 +0000283
Chris Lattnera8da51b2003-07-02 04:39:44 +0000284 const BUDataStructures::ActualCalleesTy &ActualCallees =
285 getAnalysis<BUDataStructures>().getActualCallees();
Chris Lattner18f07a12003-07-01 16:28:11 +0000286
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000287 // Loop over all the call sites and all the callees at each call site, and add
288 // edges to the CallerEdges structure for each callee.
289 for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end();
Chris Lattnera9548d92005-01-30 23:51:02 +0000290 CI != E; ++CI) {
Chris Lattner275b3012005-03-21 20:31:29 +0000291
292 // Handle direct calls efficiently.
293 if (CI->isDirectCall()) {
294 if (!CI->getCalleeFunc()->isExternal() &&
295 !DSG.getReturnNodes().count(CI->getCalleeFunc()))
296 CallerEdges[&getDSGraph(*CI->getCalleeFunc())]
297 .push_back(CallerCallEdge(&DSG, &*CI, CI->getCalleeFunc()));
298 continue;
299 }
300
Chris Lattnera9548d92005-01-30 23:51:02 +0000301 Instruction *CallI = CI->getCallSite().getInstruction();
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000302 // For each function in the invoked function list at this call site...
Chris Lattnera8da51b2003-07-02 04:39:44 +0000303 std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
Chris Lattnera9548d92005-01-30 23:51:02 +0000304 BUDataStructures::ActualCalleesTy::const_iterator>
305 IP = ActualCallees.equal_range(CallI);
Chris Lattner275b3012005-03-21 20:31:29 +0000306
307 // Skip over all calls to this graph (SCC calls).
308 while (IP.first != IP.second && &getDSGraph(*IP.first->second) == &DSG)
309 ++IP.first;
310
311 // All SCC calls?
312 if (IP.first == IP.second) continue;
313
314 Function *FirstCallee = IP.first->second;
315 ++IP.first;
316
317 // Skip over more SCC calls.
318 while (IP.first != IP.second && &getDSGraph(*IP.first->second) == &DSG)
319 ++IP.first;
320
321 // If there is exactly one callee from this call site, remember the edge in
322 // CallerEdges.
323 if (IP.first == IP.second) {
324 if (!FirstCallee->isExternal())
325 CallerEdges[&getDSGraph(*FirstCallee)]
326 .push_back(CallerCallEdge(&DSG, &*CI, FirstCallee));
327 continue;
Chris Lattnerf325e392004-01-27 21:53:14 +0000328 }
Chris Lattner275b3012005-03-21 20:31:29 +0000329
330 // Otherwise, there are multiple callees from this call site, so it must be
331 // an indirect call. Chances are that there will be other call sites with
332 // this set of targets. If so, we don't want to do M*N inlining operations,
333 // so we build up a new, private, graph that represents the calls of all
334 // calls to this set of functions.
335 std::vector<Function*> Callees;
336 IP = ActualCallees.equal_range(CallI);
337 for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
338 I != IP.second; ++I)
339 if (!I->second->isExternal())
340 Callees.push_back(I->second);
341 std::sort(Callees.begin(), Callees.end());
342
343 std::map<std::vector<Function*>, DSGraph*>::iterator IndCallRecI =
344 IndCallMap.lower_bound(Callees);
345
346 DSGraph *IndCallGraph;
347
348 // If we already have this graph, recycle it.
349 if (IndCallRecI != IndCallMap.end() && IndCallRecI->first == Callees) {
350 std::cerr << " [TD] *** Reuse of indcall graph for " << Callees.size()
351 << " callees!\n";
352 IndCallGraph = IndCallRecI->second;
353 } else {
354 // Otherwise, create a new DSGraph to represent this.
355 IndCallGraph = new DSGraph(DSG.getGlobalECs(), DSG.getTargetData());
356
357 // Make a nullary dummy call site, which will eventually get some content
358 // merged into it. The actual callee function doesn't matter here, so we
359 // just pass it something to keep the ctor happy.
360 std::vector<DSNodeHandle> ArgDummyVec;
361 DSCallSite DummyCS(CI->getCallSite(), DSNodeHandle(), Callees[0]/*dummy*/,
362 ArgDummyVec);
363 IndCallGraph->getFunctionCalls().push_back(DummyCS);
364
365 IndCallRecI = IndCallMap.insert(IndCallRecI,
366 std::make_pair(Callees, IndCallGraph));
367
368 // Additionally, make sure that each of the callees inlines this graph
369 // exactly once.
370 DSCallSite *NCS = &IndCallGraph->getFunctionCalls().front();
371 for (unsigned i = 0, e = Callees.size(); i != e; ++i) {
372 DSGraph& CalleeGraph = getDSGraph(*Callees[i]);
373 if (&CalleeGraph != &DSG)
374 CallerEdges[&CalleeGraph].push_back(CallerCallEdge(IndCallGraph, NCS,
375 Callees[i]));
376 }
377 }
378
379 // Now that we know which graph to use for this, merge the caller
380 // information into the graph, based on information from the call site.
381 ReachabilityCloner RC(*IndCallGraph, DSG, 0);
382 RC.mergeCallSite(IndCallGraph->getFunctionCalls().front(), *CI);
Chris Lattnerf325e392004-01-27 21:53:14 +0000383 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000384}
Chris Lattner851b5342005-01-24 20:00:14 +0000385
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000386
Chris Lattner851b5342005-01-24 20:00:14 +0000387static const Function *getFnForValue(const Value *V) {
388 if (const Instruction *I = dyn_cast<Instruction>(V))
389 return I->getParent()->getParent();
390 else if (const Argument *A = dyn_cast<Argument>(V))
391 return A->getParent();
392 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
393 return BB->getParent();
394 return 0;
395}
396
397void TDDataStructures::deleteValue(Value *V) {
398 if (const Function *F = getFnForValue(V)) { // Function local value?
399 // If this is a function local value, just delete it from the scalar map!
400 getDSGraph(*F).getScalarMap().eraseIfExists(V);
401 return;
402 }
403
Chris Lattnercff8ac22005-01-31 00:10:45 +0000404 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000405 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
406 "cannot handle scc's");
407 delete DSInfo[F];
408 DSInfo.erase(F);
409 return;
410 }
411
412 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
413}
414
415void TDDataStructures::copyValue(Value *From, Value *To) {
416 if (From == To) return;
417 if (const Function *F = getFnForValue(From)) { // Function local value?
418 // If this is a function local value, just delete it from the scalar map!
419 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
420 return;
421 }
422
423 if (Function *FromF = dyn_cast<Function>(From)) {
424 Function *ToF = cast<Function>(To);
425 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000426 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000427 DSInfo[ToF] = NG;
428 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
429
430 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000431 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000432 NG->getReturnNodes().clear();
433 NG->getReturnNodes()[ToF] = Ret;
434 return;
435 }
436
437 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
438}