blob: ba559bb0e6723009a6804459190fc1ec980cf52e [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 Lattnera66e3532005-03-13 20:15:06 +000084
85 // Merge the globals variables (not the calls) from the globals graph back
86 // into the main function's graph so that the main function contains all of
87 // the information about global pools and GV usage in the program.
88 if (MainFunc) {
89 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
90 const DSGraph &GG = *MainGraph.getGlobalsGraph();
91 ReachabilityCloner RC(MainGraph, GG,
92 DSGraph::DontCloneCallNodes |
93 DSGraph::DontCloneAuxCallNodes);
94
95 // Clone the global nodes into this graph.
96 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
97 E = GG.getScalarMap().global_end(); I != E; ++I)
98 if (isa<GlobalVariable>(*I))
99 RC.getClonedNH(GG.getNodeForValue(*I));
100
101 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
102 DSGraph::IgnoreGlobals);
103 }
104
Chris Lattneraa0b4682002-11-09 21:12:07 +0000105 return false;
106}
Chris Lattner55c10582002-10-03 20:38:41 +0000107
Chris Lattnera9c9c022002-11-11 21:35:13 +0000108DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
109 // Has the graph already been created?
110 DSGraph *&Graph = DSInfo[F];
111 if (Graph) return *Graph;
112
113 // Copy the local version into DSInfo...
114 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
115
116 Graph->setGlobalsGraph(GlobalsGraph);
117 Graph->setPrintAuxCalls();
118
119 // Start with a copy of the original call sites...
120 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
121 return *Graph;
122}
123
124unsigned BUDataStructures::calculateGraphs(Function *F,
125 std::vector<Function*> &Stack,
126 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000127 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000128 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000129 unsigned Min = NextID++, MyID = Min;
130 ValMap[F] = Min;
131 Stack.push_back(F);
132
Chris Lattner16437ff2004-03-04 17:05:28 +0000133 // FIXME! This test should be generalized to be any function that we have
134 // already processed, in the case when there isn't a main or there are
135 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000136 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
137 // No callees!
138 Stack.pop_back();
139 ValMap[F] = ~0;
140 return Min;
141 }
142
143 DSGraph &Graph = getOrCreateGraph(F);
144
145 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000146 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
147 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000148 Function *Callee = *I;
149 unsigned M;
150 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000151 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000152 if (It == ValMap.end()) // No, visit it now.
153 M = calculateGraphs(Callee, Stack, NextID, ValMap);
154 else // Yes, get it's number.
155 M = It->second;
156 if (M < Min) Min = M;
157 }
158
159 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
160 if (Min != MyID)
161 return Min; // This is part of a larger SCC!
162
163 // If this is a new SCC, process it now.
164 if (Stack.back() == F) { // Special case the single "SCC" case here.
165 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
166 << F->getName() << "\n");
167 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000168 DSGraph &G = getDSGraph(*F);
169 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
170 calculateGraph(G);
171 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
172 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
173 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000174
Chris Lattnerae5f6032002-11-17 22:16:28 +0000175 if (MaxSCC < 1) MaxSCC = 1;
176
Chris Lattnera9c9c022002-11-11 21:35:13 +0000177 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000178 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000179 ValMap.erase(F);
180 return calculateGraphs(F, Stack, NextID, ValMap);
181 } else {
182 ValMap[F] = ~0U;
183 }
184 return MyID;
185
186 } else {
187 // SCCFunctions - Keep track of the functions in the current SCC
188 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000189 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000190
191 Function *NF;
192 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000193 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000194 do {
195 NF = *--FirstInSCC;
196 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000197
198 // Figure out which graph is the largest one, in order to speed things up
199 // a bit in situations where functions in the SCC have widely different
200 // graph sizes.
201 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000202 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000203 // FIXME: If we used a better way of cloning graphs (ie, just splice all
204 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000205 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
206 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000207 } while (NF != F);
208
Chris Lattner0eea6182003-06-30 05:09:58 +0000209 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000210 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000211
Chris Lattnerae5f6032002-11-17 22:16:28 +0000212 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000213 if (MaxSCC < SCCGraphs.size())
214 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000215
Chris Lattner0eea6182003-06-30 05:09:58 +0000216 // First thing first, collapse all of the DSGraphs into a single graph for
217 // the entire SCC. We computed the largest graph, so clone all of the other
218 // (smaller) graphs into it. Discard all of the old graphs.
219 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000220 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
221 E = SCCGraphs.end(); I != E; ++I) {
222 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000223 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000224 {
225 DSGraph::NodeMapTy NodeMap;
226 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
227 SCCGraph->getReturnNodes(), NodeMap);
228 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000229 // Update the DSInfo map and delete the old graph...
Chris Lattnera67138d2004-01-31 21:02:18 +0000230 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
231 E = G.getReturnNodes().end(); I != E; ++I)
232 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000233 delete &G;
234 }
235 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000236
Chris Lattner744f9392003-07-02 04:37:48 +0000237 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000238 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000239
Chris Lattner0eea6182003-06-30 05:09:58 +0000240 // Now that we have one big happy family, resolve all of the call sites in
241 // the graph...
242 calculateGraph(*SCCGraph);
243 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
244 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000245
246 std::cerr << "DONE with SCC #: " << MyID << "\n";
247
248 // We never have to revisit "SCC" processed functions...
249
250 // Drop the stuff we don't need from the end of the stack
251 Stack.erase(FirstInSCC, Stack.end());
252 return MyID;
253 }
254
255 return MyID; // == Min
256}
257
258
Chris Lattner0d9bab82002-07-18 00:12:30 +0000259// releaseMemory - If the pass pipeline is done with this pass, we can release
260// our memory... here...
261//
262void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000263 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
264 E = DSInfo.end(); I != E; ++I) {
265 I->second->getReturnNodes().erase(I->first);
266 if (I->second->getReturnNodes().empty())
267 delete I->second;
268 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000269
270 // Empty map so next time memory is released, data structures are not
271 // re-deleted.
272 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000273 delete GlobalsGraph;
274 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000275}
276
Chris Lattneraf8650e2005-02-01 21:37:27 +0000277static bool isVAHackFn(const Function *F) {
278 return F->getName() == "printf" || F->getName() == "sscanf" ||
279 F->getName() == "fprintf" || F->getName() == "open" ||
280 F->getName() == "sprintf" || F->getName() == "fputs" ||
281 F->getName() == "fscanf";
282}
283
284// isUnresolvableFunction - Return true if this is an unresolvable
285// external function. A direct or indirect call to this cannot be resolved.
286//
287static bool isResolvableFunc(const Function* callee) {
288 return !callee->isExternal() || isVAHackFn(callee);
289}
290
Chris Lattner0eea6182003-06-30 05:09:58 +0000291void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000292 // Move our call site list into TempFCs so that inline call sites go into the
293 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000294 std::list<DSCallSite> TempFCs;
295 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000296 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000297
Chris Lattner0eea6182003-06-30 05:09:58 +0000298 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
299
Chris Lattnerf189bce2005-02-01 17:35:52 +0000300 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000301 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000302 while (!TempFCs.empty()) {
303 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000304
Chris Lattner86db3642005-02-04 19:59:49 +0000305 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000306
Chris Lattneraf8650e2005-02-01 21:37:27 +0000307 if (CS.isDirectCall()) {
308 Function *F = CS.getCalleeFunc();
309 if (isResolvableFunc(F))
310 if (F->isExternal()) { // Call to fprintf, etc.
311 TempFCs.erase(TempFCs.begin());
312 continue;
313 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000314 CalledFuncs.push_back(F);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000315 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000316 } else {
Chris Lattneraf8650e2005-02-01 21:37:27 +0000317 DSNode *Node = CS.getCalleeNode();
Chris Lattner744f9392003-07-02 04:37:48 +0000318
Chris Lattneraf8650e2005-02-01 21:37:27 +0000319 if (!Node->isIncomplete())
320 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
321 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
322 if (isResolvableFunc(CF) && !CF->isExternal())
Chris Lattner86db3642005-02-04 19:59:49 +0000323 CalledFuncs.push_back(CF);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000324 }
Chris Lattner0321b682004-02-27 20:05:15 +0000325
Chris Lattneraf8650e2005-02-01 21:37:27 +0000326 if (CalledFuncs.empty()) {
327 // Remember that we could not resolve this yet!
328 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000329 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000330 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000331 DSGraph *GI;
Chris Lattnera9548d92005-01-30 23:51:02 +0000332
Chris Lattner86db3642005-02-04 19:59:49 +0000333 if (CalledFuncs.size() == 1) {
334 Function *Callee = CalledFuncs[0];
Chris Lattner20cd1362005-02-01 21:49:43 +0000335 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
336 Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000337
Chris Lattner20cd1362005-02-01 21:49:43 +0000338 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000339 GI = &getDSGraph(*Callee); // Graph to inline
340 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
341
342 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
343 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000344 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
345 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000346 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000347 DSGraph::KeepModRefBits |
348 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
349 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000350 } else {
351 if (!Printed)
352 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
353 std::cerr << " calls " << CalledFuncs.size()
354 << " fns from site: " << CS.getCallSite().getInstruction()
355 << " " << *CS.getCallSite().getInstruction();
356 unsigned NumToPrint = CalledFuncs.size();
357 if (NumToPrint > 8) NumToPrint = 8;
358 std::cerr << " Fns =";
359 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
360 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
361 std::cerr << " " << (*I)->getName();
362 std::cerr << "\n";
363
364 // See if we already computed a graph for this set of callees.
365 std::sort(CalledFuncs.begin(), CalledFuncs.end());
366 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000367 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000368
369 if (IndCallGraph.first == 0) {
370 std::vector<Function*>::iterator I = CalledFuncs.begin(),
371 E = CalledFuncs.end();
372
373 // Start with a copy of the first graph.
374 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
375 GI->setGlobalsGraph(Graph.getGlobalsGraph());
376 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
377
378 // Get the argument nodes for the first callee. The return value is
379 // the 0th index in the vector.
380 GI->getFunctionArgumentsForCall(*I, Args);
381
382 // Merge all of the other callees into this graph.
383 for (++I; I != E; ++I) {
384 // If the graph already contains the nodes for the function, don't
385 // bother merging it in again.
386 if (!GI->containsFunction(*I)) {
387 DSGraph::NodeMapTy NodeMap;
388 GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
389 GI->getReturnNodes(), NodeMap);
390 ++NumBUInlines;
391 }
392
393 std::vector<DSNodeHandle> NextArgs;
394 GI->getFunctionArgumentsForCall(*I, NextArgs);
395 unsigned i = 0, e = Args.size();
396 for (; i != e; ++i) {
397 if (i == NextArgs.size()) break;
398 Args[i].mergeWith(NextArgs[i]);
399 }
400 for (e = NextArgs.size(); i != e; ++i)
401 Args.push_back(NextArgs[i]);
402 }
403
404 // Clean up the final graph!
405 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
406 } else {
407 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
408 }
409
410 GI = IndCallGraph.first;
411
412 // Merge the unified graph into this graph now.
413 DEBUG(std::cerr << " Inlining multi callee graph "
414 << "[" << GI->getGraphSize() << "+"
415 << GI->getAuxFunctionCalls().size() << "] into '"
416 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
417 << Graph.getAuxFunctionCalls().size() << "]\n");
418
419 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
420 DSGraph::KeepModRefBits |
421 DSGraph::StripAllocaBit |
422 DSGraph::DontCloneCallNodes);
423 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000424 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000425 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000426 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000427 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000428
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000429 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000430 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000431 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000432 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000433
434 // Delete dead nodes. Treat globals that are unreachable but that can
435 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000436 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000437
Chris Lattner35679372004-02-21 00:30:28 +0000438 // When this graph is finalized, clone the globals in the graph into the
439 // globals graph to make sure it has everything, from all graphs.
440 DSScalarMap &MainSM = Graph.getScalarMap();
441 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
442
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000443 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000444 // globals graph.
445 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
446 E = MainSM.global_end(); I != E; ++I)
447 RC.getClonedNH(MainSM[*I]);
448
Chris Lattnera9c9c022002-11-11 21:35:13 +0000449 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000450}
Chris Lattner851b5342005-01-24 20:00:14 +0000451
452static const Function *getFnForValue(const Value *V) {
453 if (const Instruction *I = dyn_cast<Instruction>(V))
454 return I->getParent()->getParent();
455 else if (const Argument *A = dyn_cast<Argument>(V))
456 return A->getParent();
457 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
458 return BB->getParent();
459 return 0;
460}
461
462/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
463/// These correspond to the interfaces defined in the AliasAnalysis class.
464void BUDataStructures::deleteValue(Value *V) {
465 if (const Function *F = getFnForValue(V)) { // Function local value?
466 // If this is a function local value, just delete it from the scalar map!
467 getDSGraph(*F).getScalarMap().eraseIfExists(V);
468 return;
469 }
470
Chris Lattnercff8ac22005-01-31 00:10:45 +0000471 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000472 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
473 "cannot handle scc's");
474 delete DSInfo[F];
475 DSInfo.erase(F);
476 return;
477 }
478
479 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
480}
481
482void BUDataStructures::copyValue(Value *From, Value *To) {
483 if (From == To) return;
484 if (const Function *F = getFnForValue(From)) { // Function local value?
485 // If this is a function local value, just delete it from the scalar map!
486 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
487 return;
488 }
489
490 if (Function *FromF = dyn_cast<Function>(From)) {
491 Function *ToF = cast<Function>(To);
492 assert(!DSInfo.count(ToF) && "New Function already exists!");
493 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
494 DSInfo[ToF] = NG;
495 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
496
497 // Change the Function* is the returnnodes map to the ToF.
498 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
499 NG->getReturnNodes().clear();
500 NG->getReturnNodes()[ToF] = Ret;
501 return;
502 }
503
504 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
505}