blob: e3c8c49d4210d6f3e2352484a10176bc82189617 [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural 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//===----------------------------------------------------------------------===//
Chris Lattner0d9bab82002-07-18 00:12:30 +00009//
10// This file implements the BUDataStructures class, which represents the
11// Bottom-Up Interprocedural closure of the data structure graph over the
12// program. This is useful for applications like pool allocation, but **not**
Chris Lattner55c10582002-10-03 20:38:41 +000013// applications like alias analysis.
Chris Lattner0d9bab82002-07-18 00:12:30 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner8adbec82004-07-07 06:35:22 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000018#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include "llvm/ADT/Statistic.h"
20#include "llvm/Support/Debug.h"
Chris Lattner5d5b6d62003-07-01 16:04:18 +000021#include "DSCallSiteIterator.h"
Chris Lattner9a927292003-11-12 23:11:14 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerae5f6032002-11-17 22:16:28 +000024namespace {
25 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000026 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000027 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000028
29 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000030 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000031}
Chris Lattner0d9bab82002-07-18 00:12:30 +000032
Chris Lattnerb1060432002-11-07 05:20:53 +000033using namespace DS;
Chris Lattner55c10582002-10-03 20:38:41 +000034
Chris Lattneraa0b4682002-11-09 21:12:07 +000035// run - Calculate the bottom up data structure graphs for each function in the
36// program.
37//
Chris Lattnerb12914b2004-09-20 04:48:05 +000038bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000039 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
Chris Lattner20167e32003-02-03 19:11:38 +000041 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000042
Chris Lattnerf189bce2005-02-01 17:35:52 +000043 std::vector<Function*> Stack;
44 hash_map<Function*, unsigned> ValMap;
45 unsigned NextID = 1;
46
Chris Lattnera9c9c022002-11-11 21:35:13 +000047 Function *MainFunc = M.getMainFunction();
48 if (MainFunc)
Chris Lattnerf189bce2005-02-01 17:35:52 +000049 calculateGraphs(MainFunc, Stack, NextID, ValMap);
Chris Lattnera9c9c022002-11-11 21:35:13 +000050
51 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000052 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000053 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000054#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000055 if (MainFunc)
56 std::cerr << "*** Function unreachable from main: "
57 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000058#endif
Chris Lattnerf189bce2005-02-01 17:35:52 +000059 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
Chris Lattnera9c9c022002-11-11 21:35:13 +000060 }
Chris Lattner6c874612003-07-02 23:42:48 +000061
62 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000063
Chris Lattner86db3642005-02-04 19:59:49 +000064 // If we computed any temporary indcallgraphs, free them now.
65 for (std::map<std::vector<Function*>,
66 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
67 IndCallGraphMap.begin(), E = IndCallGraphMap.end(); I != E; ++I) {
68 I->second.second.clear(); // Drop arg refs into the graph.
69 delete I->second.first;
70 }
71 IndCallGraphMap.clear();
72
Chris Lattnerec157b72003-09-20 23:27:05 +000073 // At the end of the bottom-up pass, the globals graph becomes complete.
74 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000075 // nothing! In particular, externally visible globals and unresolvable call
76 // nodes at the end of the BU phase should make things that they point to
77 // incomplete in the globals graph.
78 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +000079 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +000080 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000081 return false;
82}
Chris Lattner55c10582002-10-03 20:38:41 +000083
Chris Lattnera9c9c022002-11-11 21:35:13 +000084DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
85 // Has the graph already been created?
86 DSGraph *&Graph = DSInfo[F];
87 if (Graph) return *Graph;
88
89 // Copy the local version into DSInfo...
90 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
91
92 Graph->setGlobalsGraph(GlobalsGraph);
93 Graph->setPrintAuxCalls();
94
95 // Start with a copy of the original call sites...
96 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
97 return *Graph;
98}
99
100unsigned BUDataStructures::calculateGraphs(Function *F,
101 std::vector<Function*> &Stack,
102 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000103 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000104 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000105 unsigned Min = NextID++, MyID = Min;
106 ValMap[F] = Min;
107 Stack.push_back(F);
108
Chris Lattner16437ff2004-03-04 17:05:28 +0000109 // FIXME! This test should be generalized to be any function that we have
110 // already processed, in the case when there isn't a main or there are
111 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000112 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
113 // No callees!
114 Stack.pop_back();
115 ValMap[F] = ~0;
116 return Min;
117 }
118
119 DSGraph &Graph = getOrCreateGraph(F);
120
121 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000122 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
123 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000124 Function *Callee = *I;
125 unsigned M;
126 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000127 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000128 if (It == ValMap.end()) // No, visit it now.
129 M = calculateGraphs(Callee, Stack, NextID, ValMap);
130 else // Yes, get it's number.
131 M = It->second;
132 if (M < Min) Min = M;
133 }
134
135 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
136 if (Min != MyID)
137 return Min; // This is part of a larger SCC!
138
139 // If this is a new SCC, process it now.
140 if (Stack.back() == F) { // Special case the single "SCC" case here.
141 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
142 << F->getName() << "\n");
143 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000144 DSGraph &G = getDSGraph(*F);
145 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
146 calculateGraph(G);
147 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
148 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
149 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000150
Chris Lattnerae5f6032002-11-17 22:16:28 +0000151 if (MaxSCC < 1) MaxSCC = 1;
152
Chris Lattnera9c9c022002-11-11 21:35:13 +0000153 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000154 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000155 ValMap.erase(F);
156 return calculateGraphs(F, Stack, NextID, ValMap);
157 } else {
158 ValMap[F] = ~0U;
159 }
160 return MyID;
161
162 } else {
163 // SCCFunctions - Keep track of the functions in the current SCC
164 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000165 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000166
167 Function *NF;
168 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000169 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000170 do {
171 NF = *--FirstInSCC;
172 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000173
174 // Figure out which graph is the largest one, in order to speed things up
175 // a bit in situations where functions in the SCC have widely different
176 // graph sizes.
177 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000178 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000179 // FIXME: If we used a better way of cloning graphs (ie, just splice all
180 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000181 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
182 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000183 } while (NF != F);
184
Chris Lattner0eea6182003-06-30 05:09:58 +0000185 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000186 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000187
Chris Lattnerae5f6032002-11-17 22:16:28 +0000188 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000189 if (MaxSCC < SCCGraphs.size())
190 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000191
Chris Lattner0eea6182003-06-30 05:09:58 +0000192 // First thing first, collapse all of the DSGraphs into a single graph for
193 // the entire SCC. We computed the largest graph, so clone all of the other
194 // (smaller) graphs into it. Discard all of the old graphs.
195 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000196 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
197 E = SCCGraphs.end(); I != E; ++I) {
198 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000199 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000200 {
201 DSGraph::NodeMapTy NodeMap;
202 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
203 SCCGraph->getReturnNodes(), NodeMap);
204 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000205 // Update the DSInfo map and delete the old graph...
Chris Lattnera67138d2004-01-31 21:02:18 +0000206 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
207 E = G.getReturnNodes().end(); I != E; ++I)
208 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000209 delete &G;
210 }
211 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000212
Chris Lattner744f9392003-07-02 04:37:48 +0000213 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000214 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000215
Chris Lattner0eea6182003-06-30 05:09:58 +0000216 // Now that we have one big happy family, resolve all of the call sites in
217 // the graph...
218 calculateGraph(*SCCGraph);
219 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
220 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000221
222 std::cerr << "DONE with SCC #: " << MyID << "\n";
223
224 // We never have to revisit "SCC" processed functions...
225
226 // Drop the stuff we don't need from the end of the stack
227 Stack.erase(FirstInSCC, Stack.end());
228 return MyID;
229 }
230
231 return MyID; // == Min
232}
233
234
Chris Lattner0d9bab82002-07-18 00:12:30 +0000235// releaseMemory - If the pass pipeline is done with this pass, we can release
236// our memory... here...
237//
238void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000239 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
240 E = DSInfo.end(); I != E; ++I) {
241 I->second->getReturnNodes().erase(I->first);
242 if (I->second->getReturnNodes().empty())
243 delete I->second;
244 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000245
246 // Empty map so next time memory is released, data structures are not
247 // re-deleted.
248 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000249 delete GlobalsGraph;
250 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000251}
252
Chris Lattneraf8650e2005-02-01 21:37:27 +0000253static bool isVAHackFn(const Function *F) {
254 return F->getName() == "printf" || F->getName() == "sscanf" ||
255 F->getName() == "fprintf" || F->getName() == "open" ||
256 F->getName() == "sprintf" || F->getName() == "fputs" ||
257 F->getName() == "fscanf";
258}
259
260// isUnresolvableFunction - Return true if this is an unresolvable
261// external function. A direct or indirect call to this cannot be resolved.
262//
263static bool isResolvableFunc(const Function* callee) {
264 return !callee->isExternal() || isVAHackFn(callee);
265}
266
Chris Lattner0eea6182003-06-30 05:09:58 +0000267void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000268 // Move our call site list into TempFCs so that inline call sites go into the
269 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000270 std::list<DSCallSite> TempFCs;
271 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000272 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000273
Chris Lattner0eea6182003-06-30 05:09:58 +0000274 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
275
Chris Lattnerf189bce2005-02-01 17:35:52 +0000276 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000277 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000278 while (!TempFCs.empty()) {
279 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000280
Chris Lattner86db3642005-02-04 19:59:49 +0000281 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000282
Chris Lattneraf8650e2005-02-01 21:37:27 +0000283 if (CS.isDirectCall()) {
284 Function *F = CS.getCalleeFunc();
285 if (isResolvableFunc(F))
286 if (F->isExternal()) { // Call to fprintf, etc.
287 TempFCs.erase(TempFCs.begin());
288 continue;
289 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000290 CalledFuncs.push_back(F);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000291 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000292 } else {
Chris Lattneraf8650e2005-02-01 21:37:27 +0000293 DSNode *Node = CS.getCalleeNode();
Chris Lattner744f9392003-07-02 04:37:48 +0000294
Chris Lattneraf8650e2005-02-01 21:37:27 +0000295 if (!Node->isIncomplete())
296 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
297 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
298 if (isResolvableFunc(CF) && !CF->isExternal())
Chris Lattner86db3642005-02-04 19:59:49 +0000299 CalledFuncs.push_back(CF);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000300 }
Chris Lattner0321b682004-02-27 20:05:15 +0000301
Chris Lattneraf8650e2005-02-01 21:37:27 +0000302 if (CalledFuncs.empty()) {
303 // Remember that we could not resolve this yet!
304 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000305 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000306 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000307 DSGraph *GI;
Chris Lattnera9548d92005-01-30 23:51:02 +0000308
Chris Lattner86db3642005-02-04 19:59:49 +0000309 if (CalledFuncs.size() == 1) {
310 Function *Callee = CalledFuncs[0];
Chris Lattner20cd1362005-02-01 21:49:43 +0000311 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
312 Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000313
Chris Lattner20cd1362005-02-01 21:49:43 +0000314 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000315 GI = &getDSGraph(*Callee); // Graph to inline
316 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
317
318 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
319 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000320 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
321 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000322 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000323 DSGraph::KeepModRefBits |
324 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
325 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000326 } else {
327 if (!Printed)
328 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
329 std::cerr << " calls " << CalledFuncs.size()
330 << " fns from site: " << CS.getCallSite().getInstruction()
331 << " " << *CS.getCallSite().getInstruction();
332 unsigned NumToPrint = CalledFuncs.size();
333 if (NumToPrint > 8) NumToPrint = 8;
334 std::cerr << " Fns =";
335 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
336 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
337 std::cerr << " " << (*I)->getName();
338 std::cerr << "\n";
339
340 // See if we already computed a graph for this set of callees.
341 std::sort(CalledFuncs.begin(), CalledFuncs.end());
342 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
343 IndCallGraphMap[CalledFuncs];
344
345 if (IndCallGraph.first == 0) {
346 std::vector<Function*>::iterator I = CalledFuncs.begin(),
347 E = CalledFuncs.end();
348
349 // Start with a copy of the first graph.
350 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
351 GI->setGlobalsGraph(Graph.getGlobalsGraph());
352 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
353
354 // Get the argument nodes for the first callee. The return value is
355 // the 0th index in the vector.
356 GI->getFunctionArgumentsForCall(*I, Args);
357
358 // Merge all of the other callees into this graph.
359 for (++I; I != E; ++I) {
360 // If the graph already contains the nodes for the function, don't
361 // bother merging it in again.
362 if (!GI->containsFunction(*I)) {
363 DSGraph::NodeMapTy NodeMap;
364 GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
365 GI->getReturnNodes(), NodeMap);
366 ++NumBUInlines;
367 }
368
369 std::vector<DSNodeHandle> NextArgs;
370 GI->getFunctionArgumentsForCall(*I, NextArgs);
371 unsigned i = 0, e = Args.size();
372 for (; i != e; ++i) {
373 if (i == NextArgs.size()) break;
374 Args[i].mergeWith(NextArgs[i]);
375 }
376 for (e = NextArgs.size(); i != e; ++i)
377 Args.push_back(NextArgs[i]);
378 }
379
380 // Clean up the final graph!
381 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
382 } else {
383 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
384 }
385
386 GI = IndCallGraph.first;
387
388 // Merge the unified graph into this graph now.
389 DEBUG(std::cerr << " Inlining multi callee graph "
390 << "[" << GI->getGraphSize() << "+"
391 << GI->getAuxFunctionCalls().size() << "] into '"
392 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
393 << Graph.getAuxFunctionCalls().size() << "]\n");
394
395 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
396 DSGraph::KeepModRefBits |
397 DSGraph::StripAllocaBit |
398 DSGraph::DontCloneCallNodes);
399 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000400 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000401 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000402 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000403 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000404
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000405 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000406 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000407 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000408 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000409
410 // Delete dead nodes. Treat globals that are unreachable but that can
411 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000412 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000413
Chris Lattner35679372004-02-21 00:30:28 +0000414 // When this graph is finalized, clone the globals in the graph into the
415 // globals graph to make sure it has everything, from all graphs.
416 DSScalarMap &MainSM = Graph.getScalarMap();
417 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
418
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000419 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000420 // globals graph.
421 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
422 E = MainSM.global_end(); I != E; ++I)
423 RC.getClonedNH(MainSM[*I]);
424
Chris Lattnera9c9c022002-11-11 21:35:13 +0000425 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000426}
Chris Lattner851b5342005-01-24 20:00:14 +0000427
428static const Function *getFnForValue(const Value *V) {
429 if (const Instruction *I = dyn_cast<Instruction>(V))
430 return I->getParent()->getParent();
431 else if (const Argument *A = dyn_cast<Argument>(V))
432 return A->getParent();
433 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
434 return BB->getParent();
435 return 0;
436}
437
438/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
439/// These correspond to the interfaces defined in the AliasAnalysis class.
440void BUDataStructures::deleteValue(Value *V) {
441 if (const Function *F = getFnForValue(V)) { // Function local value?
442 // If this is a function local value, just delete it from the scalar map!
443 getDSGraph(*F).getScalarMap().eraseIfExists(V);
444 return;
445 }
446
Chris Lattnercff8ac22005-01-31 00:10:45 +0000447 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000448 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
449 "cannot handle scc's");
450 delete DSInfo[F];
451 DSInfo.erase(F);
452 return;
453 }
454
455 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
456}
457
458void BUDataStructures::copyValue(Value *From, Value *To) {
459 if (From == To) return;
460 if (const Function *F = getFnForValue(From)) { // Function local value?
461 // If this is a function local value, just delete it from the scalar map!
462 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
463 return;
464 }
465
466 if (Function *FromF = dyn_cast<Function>(From)) {
467 Function *ToF = cast<Function>(To);
468 assert(!DSInfo.count(ToF) && "New Function already exists!");
469 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
470 DSInfo[ToF] = NG;
471 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
472
473 // Change the Function* is the returnnodes map to the ToF.
474 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
475 NG->getReturnNodes().clear();
476 NG->getReturnNodes()[ToF] = Ret;
477 return;
478 }
479
480 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
481}