blob: 6d3c4b0a1a4d33de4a4d47bc66069404f711db4d [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 Lattnerbcc70bc2005-02-07 16:09:15 +000043 IndCallGraphMap = new std::map<std::vector<Function*>,
44 std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
45
Chris Lattnerf189bce2005-02-01 17:35:52 +000046 std::vector<Function*> Stack;
47 hash_map<Function*, unsigned> ValMap;
48 unsigned NextID = 1;
49
Chris Lattnera9c9c022002-11-11 21:35:13 +000050 Function *MainFunc = M.getMainFunction();
51 if (MainFunc)
Chris Lattnerf189bce2005-02-01 17:35:52 +000052 calculateGraphs(MainFunc, Stack, NextID, ValMap);
Chris Lattnera9c9c022002-11-11 21:35:13 +000053
54 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000055 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000056 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000057#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000058 if (MainFunc)
59 std::cerr << "*** Function unreachable from main: "
60 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000061#endif
Chris Lattnerf189bce2005-02-01 17:35:52 +000062 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
Chris Lattnera9c9c022002-11-11 21:35:13 +000063 }
Chris Lattner6c874612003-07-02 23:42:48 +000064
65 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000066
Chris Lattner86db3642005-02-04 19:59:49 +000067 // If we computed any temporary indcallgraphs, free them now.
68 for (std::map<std::vector<Function*>,
69 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +000070 IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
Chris Lattner86db3642005-02-04 19:59:49 +000071 I->second.second.clear(); // Drop arg refs into the graph.
72 delete I->second.first;
73 }
Chris Lattnerbcc70bc2005-02-07 16:09:15 +000074 delete IndCallGraphMap;
Chris Lattner86db3642005-02-04 19:59:49 +000075
Chris Lattnerec157b72003-09-20 23:27:05 +000076 // At the end of the bottom-up pass, the globals graph becomes complete.
77 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000078 // nothing! In particular, externally visible globals and unresolvable call
79 // nodes at the end of the BU phase should make things that they point to
80 // incomplete in the globals graph.
81 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +000082 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +000083 GlobalsGraph->maskIncompleteMarkers();
Chris Lattneraa0b4682002-11-09 21:12:07 +000084 return false;
85}
Chris Lattner55c10582002-10-03 20:38:41 +000086
Chris Lattnera9c9c022002-11-11 21:35:13 +000087DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
88 // Has the graph already been created?
89 DSGraph *&Graph = DSInfo[F];
90 if (Graph) return *Graph;
91
92 // Copy the local version into DSInfo...
93 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
94
95 Graph->setGlobalsGraph(GlobalsGraph);
96 Graph->setPrintAuxCalls();
97
98 // Start with a copy of the original call sites...
99 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
100 return *Graph;
101}
102
103unsigned BUDataStructures::calculateGraphs(Function *F,
104 std::vector<Function*> &Stack,
105 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000106 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000107 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000108 unsigned Min = NextID++, MyID = Min;
109 ValMap[F] = Min;
110 Stack.push_back(F);
111
Chris Lattner16437ff2004-03-04 17:05:28 +0000112 // FIXME! This test should be generalized to be any function that we have
113 // already processed, in the case when there isn't a main or there are
114 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000115 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
116 // No callees!
117 Stack.pop_back();
118 ValMap[F] = ~0;
119 return Min;
120 }
121
122 DSGraph &Graph = getOrCreateGraph(F);
123
124 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000125 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
126 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000127 Function *Callee = *I;
128 unsigned M;
129 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000130 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000131 if (It == ValMap.end()) // No, visit it now.
132 M = calculateGraphs(Callee, Stack, NextID, ValMap);
133 else // Yes, get it's number.
134 M = It->second;
135 if (M < Min) Min = M;
136 }
137
138 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
139 if (Min != MyID)
140 return Min; // This is part of a larger SCC!
141
142 // If this is a new SCC, process it now.
143 if (Stack.back() == F) { // Special case the single "SCC" case here.
144 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
145 << F->getName() << "\n");
146 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000147 DSGraph &G = getDSGraph(*F);
148 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
149 calculateGraph(G);
150 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
151 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
152 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000153
Chris Lattnerae5f6032002-11-17 22:16:28 +0000154 if (MaxSCC < 1) MaxSCC = 1;
155
Chris Lattnera9c9c022002-11-11 21:35:13 +0000156 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000157 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000158 ValMap.erase(F);
159 return calculateGraphs(F, Stack, NextID, ValMap);
160 } else {
161 ValMap[F] = ~0U;
162 }
163 return MyID;
164
165 } else {
166 // SCCFunctions - Keep track of the functions in the current SCC
167 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000168 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000169
170 Function *NF;
171 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000172 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000173 do {
174 NF = *--FirstInSCC;
175 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000176
177 // Figure out which graph is the largest one, in order to speed things up
178 // a bit in situations where functions in the SCC have widely different
179 // graph sizes.
180 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000181 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000182 // FIXME: If we used a better way of cloning graphs (ie, just splice all
183 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000184 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
185 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000186 } while (NF != F);
187
Chris Lattner0eea6182003-06-30 05:09:58 +0000188 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000189 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000190
Chris Lattnerae5f6032002-11-17 22:16:28 +0000191 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000192 if (MaxSCC < SCCGraphs.size())
193 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000194
Chris Lattner0eea6182003-06-30 05:09:58 +0000195 // First thing first, collapse all of the DSGraphs into a single graph for
196 // the entire SCC. We computed the largest graph, so clone all of the other
197 // (smaller) graphs into it. Discard all of the old graphs.
198 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000199 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
200 E = SCCGraphs.end(); I != E; ++I) {
201 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000202 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000203 {
204 DSGraph::NodeMapTy NodeMap;
205 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
206 SCCGraph->getReturnNodes(), NodeMap);
207 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000208 // Update the DSInfo map and delete the old graph...
Chris Lattnera67138d2004-01-31 21:02:18 +0000209 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
210 E = G.getReturnNodes().end(); I != E; ++I)
211 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000212 delete &G;
213 }
214 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000215
Chris Lattner744f9392003-07-02 04:37:48 +0000216 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000217 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000218
Chris Lattner0eea6182003-06-30 05:09:58 +0000219 // Now that we have one big happy family, resolve all of the call sites in
220 // the graph...
221 calculateGraph(*SCCGraph);
222 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
223 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000224
225 std::cerr << "DONE with SCC #: " << MyID << "\n";
226
227 // We never have to revisit "SCC" processed functions...
228
229 // Drop the stuff we don't need from the end of the stack
230 Stack.erase(FirstInSCC, Stack.end());
231 return MyID;
232 }
233
234 return MyID; // == Min
235}
236
237
Chris Lattner0d9bab82002-07-18 00:12:30 +0000238// releaseMemory - If the pass pipeline is done with this pass, we can release
239// our memory... here...
240//
241void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000242 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
243 E = DSInfo.end(); I != E; ++I) {
244 I->second->getReturnNodes().erase(I->first);
245 if (I->second->getReturnNodes().empty())
246 delete I->second;
247 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000248
249 // Empty map so next time memory is released, data structures are not
250 // re-deleted.
251 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000252 delete GlobalsGraph;
253 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000254}
255
Chris Lattneraf8650e2005-02-01 21:37:27 +0000256static bool isVAHackFn(const Function *F) {
257 return F->getName() == "printf" || F->getName() == "sscanf" ||
258 F->getName() == "fprintf" || F->getName() == "open" ||
259 F->getName() == "sprintf" || F->getName() == "fputs" ||
260 F->getName() == "fscanf";
261}
262
263// isUnresolvableFunction - Return true if this is an unresolvable
264// external function. A direct or indirect call to this cannot be resolved.
265//
266static bool isResolvableFunc(const Function* callee) {
267 return !callee->isExternal() || isVAHackFn(callee);
268}
269
Chris Lattner0eea6182003-06-30 05:09:58 +0000270void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000271 // Move our call site list into TempFCs so that inline call sites go into the
272 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000273 std::list<DSCallSite> TempFCs;
274 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000275 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000276
Chris Lattner0eea6182003-06-30 05:09:58 +0000277 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
278
Chris Lattnerf189bce2005-02-01 17:35:52 +0000279 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000280 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000281 while (!TempFCs.empty()) {
282 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000283
Chris Lattner86db3642005-02-04 19:59:49 +0000284 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000285
Chris Lattneraf8650e2005-02-01 21:37:27 +0000286 if (CS.isDirectCall()) {
287 Function *F = CS.getCalleeFunc();
288 if (isResolvableFunc(F))
289 if (F->isExternal()) { // Call to fprintf, etc.
290 TempFCs.erase(TempFCs.begin());
291 continue;
292 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000293 CalledFuncs.push_back(F);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000294 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000295 } else {
Chris Lattneraf8650e2005-02-01 21:37:27 +0000296 DSNode *Node = CS.getCalleeNode();
Chris Lattner744f9392003-07-02 04:37:48 +0000297
Chris Lattneraf8650e2005-02-01 21:37:27 +0000298 if (!Node->isIncomplete())
299 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
300 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
301 if (isResolvableFunc(CF) && !CF->isExternal())
Chris Lattner86db3642005-02-04 19:59:49 +0000302 CalledFuncs.push_back(CF);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000303 }
Chris Lattner0321b682004-02-27 20:05:15 +0000304
Chris Lattneraf8650e2005-02-01 21:37:27 +0000305 if (CalledFuncs.empty()) {
306 // Remember that we could not resolve this yet!
307 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000308 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000309 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000310 DSGraph *GI;
Chris Lattnera9548d92005-01-30 23:51:02 +0000311
Chris Lattner86db3642005-02-04 19:59:49 +0000312 if (CalledFuncs.size() == 1) {
313 Function *Callee = CalledFuncs[0];
Chris Lattner20cd1362005-02-01 21:49:43 +0000314 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
315 Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000316
Chris Lattner20cd1362005-02-01 21:49:43 +0000317 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000318 GI = &getDSGraph(*Callee); // Graph to inline
319 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
320
321 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
322 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000323 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
324 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000325 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000326 DSGraph::KeepModRefBits |
327 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
328 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000329 } else {
330 if (!Printed)
331 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
332 std::cerr << " calls " << CalledFuncs.size()
333 << " fns from site: " << CS.getCallSite().getInstruction()
334 << " " << *CS.getCallSite().getInstruction();
335 unsigned NumToPrint = CalledFuncs.size();
336 if (NumToPrint > 8) NumToPrint = 8;
337 std::cerr << " Fns =";
338 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
339 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
340 std::cerr << " " << (*I)->getName();
341 std::cerr << "\n";
342
343 // See if we already computed a graph for this set of callees.
344 std::sort(CalledFuncs.begin(), CalledFuncs.end());
345 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000346 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000347
348 if (IndCallGraph.first == 0) {
349 std::vector<Function*>::iterator I = CalledFuncs.begin(),
350 E = CalledFuncs.end();
351
352 // Start with a copy of the first graph.
353 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
354 GI->setGlobalsGraph(Graph.getGlobalsGraph());
355 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
356
357 // Get the argument nodes for the first callee. The return value is
358 // the 0th index in the vector.
359 GI->getFunctionArgumentsForCall(*I, Args);
360
361 // Merge all of the other callees into this graph.
362 for (++I; I != E; ++I) {
363 // If the graph already contains the nodes for the function, don't
364 // bother merging it in again.
365 if (!GI->containsFunction(*I)) {
366 DSGraph::NodeMapTy NodeMap;
367 GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
368 GI->getReturnNodes(), NodeMap);
369 ++NumBUInlines;
370 }
371
372 std::vector<DSNodeHandle> NextArgs;
373 GI->getFunctionArgumentsForCall(*I, NextArgs);
374 unsigned i = 0, e = Args.size();
375 for (; i != e; ++i) {
376 if (i == NextArgs.size()) break;
377 Args[i].mergeWith(NextArgs[i]);
378 }
379 for (e = NextArgs.size(); i != e; ++i)
380 Args.push_back(NextArgs[i]);
381 }
382
383 // Clean up the final graph!
384 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
385 } else {
386 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
387 }
388
389 GI = IndCallGraph.first;
390
391 // Merge the unified graph into this graph now.
392 DEBUG(std::cerr << " Inlining multi callee graph "
393 << "[" << GI->getGraphSize() << "+"
394 << GI->getAuxFunctionCalls().size() << "] into '"
395 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
396 << Graph.getAuxFunctionCalls().size() << "]\n");
397
398 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
399 DSGraph::KeepModRefBits |
400 DSGraph::StripAllocaBit |
401 DSGraph::DontCloneCallNodes);
402 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000403 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000404 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000405 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000406 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000407
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000408 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000409 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000410 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000411 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000412
413 // Delete dead nodes. Treat globals that are unreachable but that can
414 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000415 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000416
Chris Lattner35679372004-02-21 00:30:28 +0000417 // When this graph is finalized, clone the globals in the graph into the
418 // globals graph to make sure it has everything, from all graphs.
419 DSScalarMap &MainSM = Graph.getScalarMap();
420 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
421
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000422 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000423 // globals graph.
424 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
425 E = MainSM.global_end(); I != E; ++I)
426 RC.getClonedNH(MainSM[*I]);
427
Chris Lattnera9c9c022002-11-11 21:35:13 +0000428 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000429}
Chris Lattner851b5342005-01-24 20:00:14 +0000430
431static const Function *getFnForValue(const Value *V) {
432 if (const Instruction *I = dyn_cast<Instruction>(V))
433 return I->getParent()->getParent();
434 else if (const Argument *A = dyn_cast<Argument>(V))
435 return A->getParent();
436 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
437 return BB->getParent();
438 return 0;
439}
440
441/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
442/// These correspond to the interfaces defined in the AliasAnalysis class.
443void BUDataStructures::deleteValue(Value *V) {
444 if (const Function *F = getFnForValue(V)) { // Function local value?
445 // If this is a function local value, just delete it from the scalar map!
446 getDSGraph(*F).getScalarMap().eraseIfExists(V);
447 return;
448 }
449
Chris Lattnercff8ac22005-01-31 00:10:45 +0000450 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000451 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
452 "cannot handle scc's");
453 delete DSInfo[F];
454 DSInfo.erase(F);
455 return;
456 }
457
458 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
459}
460
461void BUDataStructures::copyValue(Value *From, Value *To) {
462 if (From == To) return;
463 if (const Function *F = getFnForValue(From)) { // Function local value?
464 // If this is a function local value, just delete it from the scalar map!
465 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
466 return;
467 }
468
469 if (Function *FromF = dyn_cast<Function>(From)) {
470 Function *ToF = cast<Function>(To);
471 assert(!DSInfo.count(ToF) && "New Function already exists!");
472 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
473 DSInfo[ToF] = NG;
474 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
475
476 // Change the Function* is the returnnodes map to the ToF.
477 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
478 NG->getReturnNodes().clear();
479 NG->getReturnNodes()[ToF] = Ret;
480 return;
481 }
482
483 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
484}