blob: 07de2e473cbdd42daa9a215f2940f40964d69780 [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
64 // At the end of the bottom-up pass, the globals graph becomes complete.
65 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000066 // nothing! In particular, externally visible globals and unresolvable call
67 // nodes at the end of the BU phase should make things that they point to
68 // incomplete in the globals graph.
69 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +000070 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +000071 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000072 return false;
73}
Chris Lattner55c10582002-10-03 20:38:41 +000074
Chris Lattnera9c9c022002-11-11 21:35:13 +000075DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
76 // Has the graph already been created?
77 DSGraph *&Graph = DSInfo[F];
78 if (Graph) return *Graph;
79
80 // Copy the local version into DSInfo...
81 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
82
83 Graph->setGlobalsGraph(GlobalsGraph);
84 Graph->setPrintAuxCalls();
85
86 // Start with a copy of the original call sites...
87 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
88 return *Graph;
89}
90
91unsigned BUDataStructures::calculateGraphs(Function *F,
92 std::vector<Function*> &Stack,
93 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +000094 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +000095 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +000096 unsigned Min = NextID++, MyID = Min;
97 ValMap[F] = Min;
98 Stack.push_back(F);
99
Chris Lattner16437ff2004-03-04 17:05:28 +0000100 // FIXME! This test should be generalized to be any function that we have
101 // already processed, in the case when there isn't a main or there are
102 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000103 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
104 // No callees!
105 Stack.pop_back();
106 ValMap[F] = ~0;
107 return Min;
108 }
109
110 DSGraph &Graph = getOrCreateGraph(F);
111
112 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000113 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
114 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000115 Function *Callee = *I;
116 unsigned M;
117 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000118 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000119 if (It == ValMap.end()) // No, visit it now.
120 M = calculateGraphs(Callee, Stack, NextID, ValMap);
121 else // Yes, get it's number.
122 M = It->second;
123 if (M < Min) Min = M;
124 }
125
126 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
127 if (Min != MyID)
128 return Min; // This is part of a larger SCC!
129
130 // If this is a new SCC, process it now.
131 if (Stack.back() == F) { // Special case the single "SCC" case here.
132 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
133 << F->getName() << "\n");
134 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000135 DSGraph &G = getDSGraph(*F);
136 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
137 calculateGraph(G);
138 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
139 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
140 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000141
Chris Lattnerae5f6032002-11-17 22:16:28 +0000142 if (MaxSCC < 1) MaxSCC = 1;
143
Chris Lattnera9c9c022002-11-11 21:35:13 +0000144 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000145 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000146 ValMap.erase(F);
147 return calculateGraphs(F, Stack, NextID, ValMap);
148 } else {
149 ValMap[F] = ~0U;
150 }
151 return MyID;
152
153 } else {
154 // SCCFunctions - Keep track of the functions in the current SCC
155 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000156 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000157
158 Function *NF;
159 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000160 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000161 do {
162 NF = *--FirstInSCC;
163 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000164
165 // Figure out which graph is the largest one, in order to speed things up
166 // a bit in situations where functions in the SCC have widely different
167 // graph sizes.
168 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000169 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000170 // FIXME: If we used a better way of cloning graphs (ie, just splice all
171 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000172 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
173 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000174 } while (NF != F);
175
Chris Lattner0eea6182003-06-30 05:09:58 +0000176 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000177 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000178
Chris Lattnerae5f6032002-11-17 22:16:28 +0000179 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000180 if (MaxSCC < SCCGraphs.size())
181 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000182
Chris Lattner0eea6182003-06-30 05:09:58 +0000183 // First thing first, collapse all of the DSGraphs into a single graph for
184 // the entire SCC. We computed the largest graph, so clone all of the other
185 // (smaller) graphs into it. Discard all of the old graphs.
186 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000187 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
188 E = SCCGraphs.end(); I != E; ++I) {
189 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000190 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000191 {
192 DSGraph::NodeMapTy NodeMap;
193 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
194 SCCGraph->getReturnNodes(), NodeMap);
195 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000196 // Update the DSInfo map and delete the old graph...
Chris Lattnera67138d2004-01-31 21:02:18 +0000197 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
198 E = G.getReturnNodes().end(); I != E; ++I)
199 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000200 delete &G;
201 }
202 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000203
Chris Lattner744f9392003-07-02 04:37:48 +0000204 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000205 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000206
Chris Lattner0eea6182003-06-30 05:09:58 +0000207 // Now that we have one big happy family, resolve all of the call sites in
208 // the graph...
209 calculateGraph(*SCCGraph);
210 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
211 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000212
213 std::cerr << "DONE with SCC #: " << MyID << "\n";
214
215 // We never have to revisit "SCC" processed functions...
216
217 // Drop the stuff we don't need from the end of the stack
218 Stack.erase(FirstInSCC, Stack.end());
219 return MyID;
220 }
221
222 return MyID; // == Min
223}
224
225
Chris Lattner0d9bab82002-07-18 00:12:30 +0000226// releaseMemory - If the pass pipeline is done with this pass, we can release
227// our memory... here...
228//
229void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000230 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
231 E = DSInfo.end(); I != E; ++I) {
232 I->second->getReturnNodes().erase(I->first);
233 if (I->second->getReturnNodes().empty())
234 delete I->second;
235 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000236
237 // Empty map so next time memory is released, data structures are not
238 // re-deleted.
239 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000240 delete GlobalsGraph;
241 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000242}
243
Chris Lattneraf8650e2005-02-01 21:37:27 +0000244static bool isVAHackFn(const Function *F) {
245 return F->getName() == "printf" || F->getName() == "sscanf" ||
246 F->getName() == "fprintf" || F->getName() == "open" ||
247 F->getName() == "sprintf" || F->getName() == "fputs" ||
248 F->getName() == "fscanf";
249}
250
251// isUnresolvableFunction - Return true if this is an unresolvable
252// external function. A direct or indirect call to this cannot be resolved.
253//
254static bool isResolvableFunc(const Function* callee) {
255 return !callee->isExternal() || isVAHackFn(callee);
256}
257
Chris Lattner0eea6182003-06-30 05:09:58 +0000258void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000259 // Move our call site list into TempFCs so that inline call sites go into the
260 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000261 std::list<DSCallSite> TempFCs;
262 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000263 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000264
Chris Lattner0eea6182003-06-30 05:09:58 +0000265 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
266
Chris Lattnerf189bce2005-02-01 17:35:52 +0000267 // Print out multi-call sites.
268 bool Printed = false;
269 for (std::list<DSCallSite>::iterator I = TempFCs.begin(), E = TempFCs.end();
270 I != E; ++I) {
271 if (!I->isDirectCall()) {
272 DSNode *Node = I->getCalleeNode();
273 if (Node->getGlobals().size() > 1) {
274 if (!Printed)
275 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
276 std::cerr << " calls " << Node->getGlobals().size()
Chris Lattneraf8650e2005-02-01 21:37:27 +0000277 << " fns from site: " << I->getCallSite().getInstruction()
278 << " " << *I->getCallSite().getInstruction();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000279 unsigned NumToPrint = Node->getGlobals().size();
280 if (NumToPrint > 5) NumToPrint = 5;
281 std::cerr << " Fns =";
282 for (unsigned i = 0; i != NumToPrint; ++i)
283 std::cerr << " " << Node->getGlobals()[i]->getName();
284 std::cerr << "\n";
285 }
286 }
287 }
288
Chris Lattneraf8650e2005-02-01 21:37:27 +0000289 while (!TempFCs.empty()) {
290 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000291
Chris Lattneraf8650e2005-02-01 21:37:27 +0000292 std::set<Function*> CalledFuncs;
Chris Lattnera9548d92005-01-30 23:51:02 +0000293
Chris Lattneraf8650e2005-02-01 21:37:27 +0000294 if (CS.isDirectCall()) {
295 Function *F = CS.getCalleeFunc();
296 if (isResolvableFunc(F))
297 if (F->isExternal()) { // Call to fprintf, etc.
298 TempFCs.erase(TempFCs.begin());
299 continue;
300 } else {
301 CalledFuncs.insert(F);
302 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000303 } else {
Chris Lattneraf8650e2005-02-01 21:37:27 +0000304 DSNode *Node = CS.getCalleeNode();
Chris Lattner744f9392003-07-02 04:37:48 +0000305
Chris Lattneraf8650e2005-02-01 21:37:27 +0000306 if (!Node->isIncomplete())
307 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
308 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
309 if (isResolvableFunc(CF) && !CF->isExternal())
310 CalledFuncs.insert(CF);
311 }
Chris Lattner0321b682004-02-27 20:05:15 +0000312
Chris Lattneraf8650e2005-02-01 21:37:27 +0000313 if (CalledFuncs.empty()) {
314 // Remember that we could not resolve this yet!
315 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000316 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000317 } else if (CalledFuncs.size() == 1) {
318 Function *Callee = *CalledFuncs.begin();
319
Chris Lattner20cd1362005-02-01 21:49:43 +0000320 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
321 Callee));
Chris Lattneraf8650e2005-02-01 21:37:27 +0000322
Chris Lattner20cd1362005-02-01 21:49:43 +0000323 // Get the data structure graph for the called function.
324 //
325 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
326
327 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
328 << "[" << GI.getGraphSize() << "+"
329 << GI.getAuxFunctionCalls().size() << "] into '"
330 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
331 << Graph.getAuxFunctionCalls().size() << "]\n");
332 Graph.mergeInGraph(CS, *Callee, GI,
333 DSGraph::KeepModRefBits |
334 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
335 ++NumBUInlines;
336
Chris Lattnerae5f6032002-11-17 22:16:28 +0000337#if 0
Chris Lattner20cd1362005-02-01 21:49:43 +0000338 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
339 Callee->getName());
Chris Lattnerae5f6032002-11-17 22:16:28 +0000340#endif
Chris Lattneraf8650e2005-02-01 21:37:27 +0000341 } else {
342 if (!Printed)
343 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
344 std::cerr << " calls " << CalledFuncs.size()
345 << " fns from site: " << CS.getCallSite().getInstruction()
346 << " " << *CS.getCallSite().getInstruction();
347 unsigned NumToPrint = CalledFuncs.size();
348 if (NumToPrint > 8) NumToPrint = 8;
349 std::cerr << " Fns =";
350 for (std::set<Function*>::iterator I = CalledFuncs.begin(),
351 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
352 std::cerr << " " << (*I)->getName();
353 std::cerr << "\n";
Chris Lattnera9548d92005-01-30 23:51:02 +0000354
Chris Lattneraf8650e2005-02-01 21:37:27 +0000355 // Inline all of the called functions.
356 for (std::set<Function*>::iterator I = CalledFuncs.begin(),
357 E = CalledFuncs.end(); I != E; ++I) {
358 Function *Callee = *I;
Chris Lattner20cd1362005-02-01 21:49:43 +0000359 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
360 Callee));
Chris Lattneraf8650e2005-02-01 21:37:27 +0000361
Chris Lattner20cd1362005-02-01 21:49:43 +0000362 // Get the data structure graph for the called function.
363 //
364 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
365
366 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
367 << "[" << GI.getGraphSize() << "+"
368 << GI.getAuxFunctionCalls().size() << "] into '"
369 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
370 << Graph.getAuxFunctionCalls().size() << "]\n");
371 Graph.mergeInGraph(CS, *Callee, GI,
372 DSGraph::KeepModRefBits |
373 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
374 ++NumBUInlines;
375
Chris Lattneraf8650e2005-02-01 21:37:27 +0000376#if 0
Chris Lattner20cd1362005-02-01 21:49:43 +0000377 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
378 Callee->getName());
Chris Lattneraf8650e2005-02-01 21:37:27 +0000379#endif
Chris Lattneraf8650e2005-02-01 21:37:27 +0000380 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000381 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000382 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000383 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000384
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000385 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000386 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000387 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000388 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000389
390 // Delete dead nodes. Treat globals that are unreachable but that can
391 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000392 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000393
Chris Lattner35679372004-02-21 00:30:28 +0000394 // When this graph is finalized, clone the globals in the graph into the
395 // globals graph to make sure it has everything, from all graphs.
396 DSScalarMap &MainSM = Graph.getScalarMap();
397 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
398
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000399 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000400 // globals graph.
401 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
402 E = MainSM.global_end(); I != E; ++I)
403 RC.getClonedNH(MainSM[*I]);
404
Chris Lattnera9c9c022002-11-11 21:35:13 +0000405 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000406}
Chris Lattner851b5342005-01-24 20:00:14 +0000407
408static const Function *getFnForValue(const Value *V) {
409 if (const Instruction *I = dyn_cast<Instruction>(V))
410 return I->getParent()->getParent();
411 else if (const Argument *A = dyn_cast<Argument>(V))
412 return A->getParent();
413 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
414 return BB->getParent();
415 return 0;
416}
417
418/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
419/// These correspond to the interfaces defined in the AliasAnalysis class.
420void BUDataStructures::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 BUDataStructures::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!");
449 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
450 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.
454 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
455 NG->getReturnNodes().clear();
456 NG->getReturnNodes()[ToF] = Ret;
457 return;
458 }
459
460 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
461}