blob: 9ce6cde7a4d8df8f736294862df36261c91a60dc [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"
Chris Lattner24c47c52005-03-22 00:12:00 +000022#include "llvm/Support/Timer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/Statistic.h"
Chris Lattner9a927292003-11-12 23:11:14 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner24c47c52005-03-22 00:12:00 +000026#if 0
27#define TIME_REGION(VARNAME, DESC) \
28 NamedRegionTimer VARNAME(DESC)
29#else
30#define TIME_REGION(VARNAME, DESC)
31#endif
32
Chris Lattner4923d1b2003-02-03 22:51:28 +000033namespace {
34 RegisterAnalysis<TDDataStructures> // Register the pass
Chris Lattner312edd32003-06-28 22:14:55 +000035 Y("tddatastructure", "Top-down Data Structure Analysis");
Chris Lattnera8da51b2003-07-02 04:39:44 +000036
37 Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
38}
39
Chris Lattner3b0a9be2003-09-20 22:24:04 +000040void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
41 hash_set<DSNode*> &Visited) {
Chris Lattner11fc9302003-09-20 23:58:33 +000042 if (!N || Visited.count(N)) return;
Chris Lattner3b0a9be2003-09-20 22:24:04 +000043 Visited.insert(N);
44
45 for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) {
46 DSNodeHandle &NH = N->getLink(i*N->getPointerSize());
47 if (DSNode *NN = NH.getNode()) {
Chris Lattner9454dda2005-03-20 02:39:49 +000048 std::vector<Function*> Functions;
49 NN->addFullFunctionList(Functions);
50 ArgsRemainIncomplete.insert(Functions.begin(), Functions.end());
Chris Lattner3b0a9be2003-09-20 22:24:04 +000051 markReachableFunctionsExternallyAccessible(NN, Visited);
52 }
53 }
Chris Lattner4923d1b2003-02-03 22:51:28 +000054}
Vikram S. Adveaaeee752002-07-30 22:06:40 +000055
Chris Lattner3b0a9be2003-09-20 22:24:04 +000056
Chris Lattneraa0b4682002-11-09 21:12:07 +000057// run - Calculate the top down data structure graphs for each function in the
58// program.
59//
Chris Lattnerb12914b2004-09-20 04:48:05 +000060bool TDDataStructures::runOnModule(Module &M) {
Chris Lattner021decc2005-04-02 19:17:18 +000061 BUInfo = &getAnalysis<BUDataStructures>();
62 GlobalECs = BUInfo->getGlobalECs();
63 GlobalsGraph = new DSGraph(BUInfo->getGlobalsGraph(), GlobalECs);
Chris Lattner11fc9302003-09-20 23:58:33 +000064 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000065
Chris Lattnera8da51b2003-07-02 04:39:44 +000066 // Figure out which functions must not mark their arguments complete because
Chris Lattner3b0a9be2003-09-20 22:24:04 +000067 // they are accessible outside this compilation unit. Currently, these
68 // arguments are functions which are reachable by global variables in the
69 // globals graph.
Chris Lattner6d8f3dc2004-01-28 03:12:48 +000070 const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
Chris Lattner3b0a9be2003-09-20 22:24:04 +000071 hash_set<DSNode*> Visited;
Misha Brukman96a8bd72004-04-29 04:05:30 +000072 for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end();
Chris Lattner275b3012005-03-21 20:31:29 +000073 I != E; ++I) {
74 DSNode *N = GGSM.find(*I)->second.getNode();
75 if (N->isIncomplete())
76 markReachableFunctionsExternallyAccessible(N, Visited);
77 }
Chris Lattner11fc9302003-09-20 23:58:33 +000078
79 // Loop over unresolved call nodes. Any functions passed into (but not
Chris Lattnerf325e392004-01-27 21:53:14 +000080 // returned!) from unresolvable call nodes may be invoked outside of the
Chris Lattner11fc9302003-09-20 23:58:33 +000081 // current module.
Chris Lattnera9548d92005-01-30 23:51:02 +000082 for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(),
83 E = GlobalsGraph->afc_end(); I != E; ++I)
84 for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg)
85 markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(),
Chris Lattner11fc9302003-09-20 23:58:33 +000086 Visited);
Chris Lattner3b0a9be2003-09-20 22:24:04 +000087 Visited.clear();
88
89 // Functions without internal linkage also have unknown incoming arguments!
Chris Lattnera8da51b2003-07-02 04:39:44 +000090 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner3b0a9be2003-09-20 22:24:04 +000091 if (!I->isExternal() && !I->hasInternalLinkage())
Chris Lattnera8da51b2003-07-02 04:39:44 +000092 ArgsRemainIncomplete.insert(I);
93
Chris Lattner6c874612003-07-02 23:42:48 +000094 // We want to traverse the call graph in reverse post-order. To do this, we
95 // calculate a post-order traversal, then reverse it.
96 hash_set<DSGraph*> VisitedGraph;
97 std::vector<DSGraph*> PostOrder;
Chris Lattner6c874612003-07-02 23:42:48 +000098
Chris Lattner24c47c52005-03-22 00:12:00 +000099#if 0
100{TIME_REGION(XXX, "td:Copy graphs");
101
102 // Visit each of the graphs in reverse post-order now!
103 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
104 if (!I->isExternal())
105 getOrCreateDSGraph(*I);
Chris Lattner9308a352005-03-22 01:50:42 +0000106 return false;
Chris Lattner24c47c52005-03-22 00:12:00 +0000107}
Chris Lattner24c47c52005-03-22 00:12:00 +0000108#endif
109
110
111{TIME_REGION(XXX, "td:Compute postorder");
112
Chris Lattneraa0b4682002-11-09 21:12:07 +0000113 // Calculate top-down from main...
114 if (Function *F = M.getMainFunction())
Chris Lattner021decc2005-04-02 19:17:18 +0000115 ComputePostOrder(*F, VisitedGraph, PostOrder);
Chris Lattneraa0b4682002-11-09 21:12:07 +0000116
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000117 // Next calculate the graphs for each unreachable function...
Chris Lattnera8da51b2003-07-02 04:39:44 +0000118 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner021decc2005-04-02 19:17:18 +0000119 ComputePostOrder(*I, VisitedGraph, PostOrder);
Chris Lattner6c874612003-07-02 23:42:48 +0000120
121 VisitedGraph.clear(); // Release memory!
Chris Lattner24c47c52005-03-22 00:12:00 +0000122}
123
124{TIME_REGION(XXX, "td:Inline stuff");
Chris Lattner6c874612003-07-02 23:42:48 +0000125
126 // Visit each of the graphs in reverse post-order now!
127 while (!PostOrder.empty()) {
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000128 InlineCallersIntoGraph(*PostOrder.back());
Chris Lattner6c874612003-07-02 23:42:48 +0000129 PostOrder.pop_back();
130 }
Chris Lattner24c47c52005-03-22 00:12:00 +0000131}
Chris Lattneraa0b4682002-11-09 21:12:07 +0000132
Chris Lattner275b3012005-03-21 20:31:29 +0000133 // Free the IndCallMap.
134 while (!IndCallMap.empty()) {
135 delete IndCallMap.begin()->second;
136 IndCallMap.erase(IndCallMap.begin());
137 }
138
139
Chris Lattnera8da51b2003-07-02 04:39:44 +0000140 ArgsRemainIncomplete.clear();
Chris Lattnerc3f5f772004-02-08 01:51:48 +0000141 GlobalsGraph->removeTriviallyDeadNodes();
142
Chris Lattneraa0b4682002-11-09 21:12:07 +0000143 return false;
144}
145
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000146
Chris Lattner7a211632002-11-08 21:28:37 +0000147DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
148 DSGraph *&G = DSInfo[&F];
149 if (G == 0) { // Not created yet? Clone BU graph...
Chris Lattner24c47c52005-03-22 00:12:00 +0000150 G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs,
151 DSGraph::DontCloneAuxCallNodes);
152 assert(G->getAuxFunctionCalls().empty() && "Cloned aux calls?");
Chris Lattner24d80072003-02-04 00:59:32 +0000153 G->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000154 G->setGlobalsGraph(GlobalsGraph);
Chris Lattner9308a352005-03-22 01:50:42 +0000155
156 // Note that this graph is the graph for ALL of the function in the SCC, not
157 // just F.
158 for (DSGraph::retnodes_iterator RI = G->retnodes_begin(),
159 E = G->retnodes_end(); RI != E; ++RI)
160 if (RI->first != &F)
161 DSInfo[RI->first] = G;
Chris Lattner7a211632002-11-08 21:28:37 +0000162 }
163 return *G;
164}
Vikram S. Adve26b98262002-10-20 21:41:02 +0000165
Chris Lattnerdea81462003-06-29 22:37:07 +0000166
Chris Lattner18f07a12003-07-01 16:28:11 +0000167void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
Chris Lattner021decc2005-04-02 19:17:18 +0000168 std::vector<DSGraph*> &PostOrder) {
Chris Lattner18f07a12003-07-01 16:28:11 +0000169 if (F.isExternal()) return;
170 DSGraph &G = getOrCreateDSGraph(F);
171 if (Visited.count(&G)) return;
172 Visited.insert(&G);
173
174 // Recursively traverse all of the callee graphs.
Chris Lattner1231aa32005-04-02 20:17:09 +0000175 for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE; ++CI){
Chris Lattnera9548d92005-01-30 23:51:02 +0000176 Instruction *CallI = CI->getCallSite().getInstruction();
Chris Lattner1231aa32005-04-02 20:17:09 +0000177 for (BUDataStructures::callee_iterator I = BUInfo->callee_begin(CallI),
178 E = BUInfo->callee_end(CallI); I != E; ++I)
Chris Lattner021decc2005-04-02 19:17:18 +0000179 ComputePostOrder(*I->second, Visited, PostOrder);
Chris Lattner18f07a12003-07-01 16:28:11 +0000180 }
Chris Lattner198be222002-10-21 19:47:18 +0000181
Chris Lattner18f07a12003-07-01 16:28:11 +0000182 PostOrder.push_back(&G);
183}
184
185
186
Chris Lattner18f07a12003-07-01 16:28:11 +0000187
Chris Lattner6c874612003-07-02 23:42:48 +0000188
189// releaseMemory - If the pass pipeline is done with this pass, we can release
190// our memory... here...
191//
192// FIXME: This should be releaseMemory and will work fine, except that LoadVN
193// has no way to extend the lifetime of the pass, which screws up ds-aa.
194//
195void TDDataStructures::releaseMyMemory() {
196 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
197 E = DSInfo.end(); I != E; ++I) {
198 I->second->getReturnNodes().erase(I->first);
199 if (I->second->getReturnNodes().empty())
200 delete I->second;
Chris Lattner18f07a12003-07-01 16:28:11 +0000201 }
Chris Lattner18f07a12003-07-01 16:28:11 +0000202
Chris Lattner6c874612003-07-02 23:42:48 +0000203 // Empty map so next time memory is released, data structures are not
204 // re-deleted.
205 DSInfo.clear();
206 delete GlobalsGraph;
207 GlobalsGraph = 0;
208}
Chris Lattner18f07a12003-07-01 16:28:11 +0000209
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000210/// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph
211/// into it, then recompute completeness of nodes in the resultant graph.
212void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) {
213 // Inline caller graphs into this graph. First step, get the list of call
214 // sites that call into this graph.
215 std::vector<CallerCallEdge> EdgesFromCaller;
216 std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator
217 CEI = CallerEdges.find(&DSG);
218 if (CEI != CallerEdges.end()) {
219 std::swap(CEI->second, EdgesFromCaller);
220 CallerEdges.erase(CEI);
221 }
222
223 // Sort the caller sites to provide a by-caller-graph ordering.
224 std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end());
225
226
Chris Lattnerc2b94802005-03-21 08:43:32 +0000227 // Merge information from the globals graph into this graph. FIXME: This is
228 // stupid. Instead of us cloning information from the GG into this graph,
229 // then having RemoveDeadNodes clone it back, we should do all of this as a
230 // post-pass over all of the graphs. We need to take cloning out of
231 // removeDeadNodes and gut removeDeadNodes at the same time first though. :(
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000232 {
233 DSGraph &GG = *DSG.getGlobalsGraph();
234 ReachabilityCloner RC(DSG, GG,
235 DSGraph::DontCloneCallNodes |
236 DSGraph::DontCloneAuxCallNodes);
237 for (DSScalarMap::global_iterator
238 GI = DSG.getScalarMap().global_begin(),
239 E = DSG.getScalarMap().global_end(); GI != E; ++GI)
240 RC.getClonedNH(GG.getNodeForValue(*GI));
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000241 }
242
243 DEBUG(std::cerr << "[TD] Inlining callers into '" << DSG.getFunctionNames()
244 << "'\n");
245
246 // Iteratively inline caller graphs into this graph.
247 while (!EdgesFromCaller.empty()) {
248 DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph;
249
250 // Iterate through all of the call sites of this graph, cloning and merging
251 // any nodes required by the call.
252 ReachabilityCloner RC(DSG, CallerGraph,
253 DSGraph::DontCloneCallNodes |
254 DSGraph::DontCloneAuxCallNodes);
255
256 // Inline all call sites from this caller graph.
257 do {
258 const DSCallSite &CS = *EdgesFromCaller.back().CS;
259 Function &CF = *EdgesFromCaller.back().CalledFunction;
Chris Lattner275b3012005-03-21 20:31:29 +0000260 DEBUG(std::cerr << " [TD] Inlining graph into Fn '"
261 << CF.getName() << "' from ");
262 if (CallerGraph.getReturnNodes().empty())
263 DEBUG(std::cerr << "SYNTHESIZED INDIRECT GRAPH");
264 else
265 DEBUG (std::cerr << "Fn '"
266 << CS.getCallSite().getInstruction()->
267 getParent()->getParent()->getName() << "'");
268 DEBUG(std::cerr << ": " << CF.getFunctionType()->getNumParams()
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000269 << " args\n");
270
271 // Get the formal argument and return nodes for the called function and
272 // merge them with the cloned subgraph.
Chris Lattner275b3012005-03-21 20:31:29 +0000273 DSCallSite T1 = DSG.getCallSiteForArguments(CF);
274 RC.mergeCallSite(T1, CS);
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000275 ++NumTDInlines;
276
277 EdgesFromCaller.pop_back();
278 } while (!EdgesFromCaller.empty() &&
279 EdgesFromCaller.back().CallerGraph == &CallerGraph);
280 }
281
282
283 // Next, now that this graph is finalized, we need to recompute the
284 // incompleteness markers for this graph and remove unreachable nodes.
285 DSG.maskIncompleteMarkers();
Chris Lattnera8da51b2003-07-02 04:39:44 +0000286
287 // If any of the functions has incomplete incoming arguments, don't mark any
288 // of them as complete.
289 bool HasIncompleteArgs = false;
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000290 for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(),
291 E = DSG.retnodes_end(); I != E; ++I)
Chris Lattnera8da51b2003-07-02 04:39:44 +0000292 if (ArgsRemainIncomplete.count(I->first)) {
293 HasIncompleteArgs = true;
294 break;
295 }
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000296
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000297 // Recompute the Incomplete markers. Depends on whether args are complete
Chris Lattnera8da51b2003-07-02 04:39:44 +0000298 unsigned Flags
299 = HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000300 DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000301
302 // Delete dead nodes. Treat globals that are unreachable as dead also.
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000303 DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
Chris Lattner4f2cfc02003-02-10 18:16:36 +0000304
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000305 // We are done with computing the current TD Graph! Finally, before we can
306 // finish processing this function, we figure out which functions it calls and
307 // records these call graph edges, so that we have them when we process the
308 // callee graphs.
309 if (DSG.fc_begin() == DSG.fc_end()) return;
Chris Lattner18f07a12003-07-01 16:28:11 +0000310
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000311 // Loop over all the call sites and all the callees at each call site, and add
312 // edges to the CallerEdges structure for each callee.
313 for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end();
Chris Lattnera9548d92005-01-30 23:51:02 +0000314 CI != E; ++CI) {
Chris Lattner275b3012005-03-21 20:31:29 +0000315
316 // Handle direct calls efficiently.
317 if (CI->isDirectCall()) {
318 if (!CI->getCalleeFunc()->isExternal() &&
319 !DSG.getReturnNodes().count(CI->getCalleeFunc()))
320 CallerEdges[&getDSGraph(*CI->getCalleeFunc())]
321 .push_back(CallerCallEdge(&DSG, &*CI, CI->getCalleeFunc()));
322 continue;
323 }
324
Chris Lattnera9548d92005-01-30 23:51:02 +0000325 Instruction *CallI = CI->getCallSite().getInstruction();
Vikram S. Adve03e19dd2003-07-16 21:40:28 +0000326 // For each function in the invoked function list at this call site...
Chris Lattner2ccc5f12005-04-02 20:02:41 +0000327 BUDataStructures::callee_iterator IPI =
Chris Lattner021decc2005-04-02 19:17:18 +0000328 BUInfo->callee_begin(CallI), IPE = BUInfo->callee_end(CallI);
Chris Lattner275b3012005-03-21 20:31:29 +0000329
330 // Skip over all calls to this graph (SCC calls).
Chris Lattner021decc2005-04-02 19:17:18 +0000331 while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
332 ++IPI;
Chris Lattner275b3012005-03-21 20:31:29 +0000333
334 // All SCC calls?
Chris Lattner021decc2005-04-02 19:17:18 +0000335 if (IPI == IPE) continue;
Chris Lattner275b3012005-03-21 20:31:29 +0000336
Chris Lattner021decc2005-04-02 19:17:18 +0000337 Function *FirstCallee = IPI->second;
338 ++IPI;
Chris Lattner275b3012005-03-21 20:31:29 +0000339
340 // Skip over more SCC calls.
Chris Lattner021decc2005-04-02 19:17:18 +0000341 while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
342 ++IPI;
Chris Lattner275b3012005-03-21 20:31:29 +0000343
344 // If there is exactly one callee from this call site, remember the edge in
345 // CallerEdges.
Chris Lattner021decc2005-04-02 19:17:18 +0000346 if (IPI == IPE) {
Chris Lattner275b3012005-03-21 20:31:29 +0000347 if (!FirstCallee->isExternal())
348 CallerEdges[&getDSGraph(*FirstCallee)]
349 .push_back(CallerCallEdge(&DSG, &*CI, FirstCallee));
350 continue;
Chris Lattnerf325e392004-01-27 21:53:14 +0000351 }
Chris Lattner275b3012005-03-21 20:31:29 +0000352
353 // Otherwise, there are multiple callees from this call site, so it must be
354 // an indirect call. Chances are that there will be other call sites with
355 // this set of targets. If so, we don't want to do M*N inlining operations,
356 // so we build up a new, private, graph that represents the calls of all
357 // calls to this set of functions.
358 std::vector<Function*> Callees;
Chris Lattner021decc2005-04-02 19:17:18 +0000359 for (BUDataStructures::ActualCalleesTy::const_iterator I =
360 BUInfo->callee_begin(CallI), E = BUInfo->callee_end(CallI);
361 I != E; ++I)
Chris Lattner275b3012005-03-21 20:31:29 +0000362 if (!I->second->isExternal())
363 Callees.push_back(I->second);
364 std::sort(Callees.begin(), Callees.end());
365
366 std::map<std::vector<Function*>, DSGraph*>::iterator IndCallRecI =
367 IndCallMap.lower_bound(Callees);
368
369 DSGraph *IndCallGraph;
370
371 // If we already have this graph, recycle it.
372 if (IndCallRecI != IndCallMap.end() && IndCallRecI->first == Callees) {
373 std::cerr << " [TD] *** Reuse of indcall graph for " << Callees.size()
374 << " callees!\n";
375 IndCallGraph = IndCallRecI->second;
376 } else {
377 // Otherwise, create a new DSGraph to represent this.
378 IndCallGraph = new DSGraph(DSG.getGlobalECs(), DSG.getTargetData());
379
380 // Make a nullary dummy call site, which will eventually get some content
381 // merged into it. The actual callee function doesn't matter here, so we
382 // just pass it something to keep the ctor happy.
383 std::vector<DSNodeHandle> ArgDummyVec;
384 DSCallSite DummyCS(CI->getCallSite(), DSNodeHandle(), Callees[0]/*dummy*/,
385 ArgDummyVec);
386 IndCallGraph->getFunctionCalls().push_back(DummyCS);
387
388 IndCallRecI = IndCallMap.insert(IndCallRecI,
389 std::make_pair(Callees, IndCallGraph));
390
391 // Additionally, make sure that each of the callees inlines this graph
392 // exactly once.
393 DSCallSite *NCS = &IndCallGraph->getFunctionCalls().front();
394 for (unsigned i = 0, e = Callees.size(); i != e; ++i) {
395 DSGraph& CalleeGraph = getDSGraph(*Callees[i]);
396 if (&CalleeGraph != &DSG)
397 CallerEdges[&CalleeGraph].push_back(CallerCallEdge(IndCallGraph, NCS,
398 Callees[i]));
399 }
400 }
401
402 // Now that we know which graph to use for this, merge the caller
403 // information into the graph, based on information from the call site.
404 ReachabilityCloner RC(*IndCallGraph, DSG, 0);
405 RC.mergeCallSite(IndCallGraph->getFunctionCalls().front(), *CI);
Chris Lattnerf325e392004-01-27 21:53:14 +0000406 }
Vikram S. Adveaaeee752002-07-30 22:06:40 +0000407}
Chris Lattner851b5342005-01-24 20:00:14 +0000408
Chris Lattnerd57e55e2005-03-21 04:55:35 +0000409
Chris Lattner851b5342005-01-24 20:00:14 +0000410static const Function *getFnForValue(const Value *V) {
411 if (const Instruction *I = dyn_cast<Instruction>(V))
412 return I->getParent()->getParent();
413 else if (const Argument *A = dyn_cast<Argument>(V))
414 return A->getParent();
415 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
416 return BB->getParent();
417 return 0;
418}
419
420void TDDataStructures::deleteValue(Value *V) {
421 if (const Function *F = getFnForValue(V)) { // Function local value?
422 // If this is a function local value, just delete it from the scalar map!
423 getDSGraph(*F).getScalarMap().eraseIfExists(V);
424 return;
425 }
426
Chris Lattnercff8ac22005-01-31 00:10:45 +0000427 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000428 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
429 "cannot handle scc's");
430 delete DSInfo[F];
431 DSInfo.erase(F);
432 return;
433 }
434
435 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
436}
437
438void TDDataStructures::copyValue(Value *From, Value *To) {
439 if (From == To) return;
440 if (const Function *F = getFnForValue(From)) { // Function local value?
441 // If this is a function local value, just delete it from the scalar map!
442 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
443 return;
444 }
445
446 if (Function *FromF = dyn_cast<Function>(From)) {
447 Function *ToF = cast<Function>(To);
448 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000449 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000450 DSInfo[ToF] = NG;
451 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
452
453 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000454 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000455 NG->getReturnNodes().clear();
456 NG->getReturnNodes()[ToF] = Ret;
457 return;
458 }
459
Chris Lattnerc5132e62005-03-24 04:22:04 +0000460 if (const Function *F = getFnForValue(To)) {
461 DSGraph &G = getDSGraph(*F);
462 G.getScalarMap().copyScalarIfExists(From, To);
463 return;
464 }
465
466 std::cerr << *From;
467 std::cerr << *To;
468 assert(0 && "Do not know how to copy this yet!");
469 abort();
Chris Lattner851b5342005-01-24 20:00:14 +0000470}