blob: 7ab7d8056435e931087545aebf7735ae9a21a45e [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.
Chris Lattner49e88e82005-03-15 22:10:04 +000088 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000089 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
Chris Lattner270cf502005-03-13 20:32:26 +0000101 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000102 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
103 DSGraph::IgnoreGlobals);
104 }
105
Chris Lattneraa0b4682002-11-09 21:12:07 +0000106 return false;
107}
Chris Lattner55c10582002-10-03 20:38:41 +0000108
Chris Lattnera9c9c022002-11-11 21:35:13 +0000109DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
110 // Has the graph already been created?
111 DSGraph *&Graph = DSInfo[F];
112 if (Graph) return *Graph;
113
114 // Copy the local version into DSInfo...
115 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
116
117 Graph->setGlobalsGraph(GlobalsGraph);
118 Graph->setPrintAuxCalls();
119
120 // Start with a copy of the original call sites...
121 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
122 return *Graph;
123}
124
125unsigned BUDataStructures::calculateGraphs(Function *F,
126 std::vector<Function*> &Stack,
127 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000128 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000129 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000130 unsigned Min = NextID++, MyID = Min;
131 ValMap[F] = Min;
132 Stack.push_back(F);
133
Chris Lattner16437ff2004-03-04 17:05:28 +0000134 // FIXME! This test should be generalized to be any function that we have
135 // already processed, in the case when there isn't a main or there are
136 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000137 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
138 // No callees!
139 Stack.pop_back();
140 ValMap[F] = ~0;
141 return Min;
142 }
143
144 DSGraph &Graph = getOrCreateGraph(F);
145
146 // The edges out of the current node are the call site targets...
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000147 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
148 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000149 Function *Callee = *I;
150 unsigned M;
151 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000152 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000153 if (It == ValMap.end()) // No, visit it now.
154 M = calculateGraphs(Callee, Stack, NextID, ValMap);
155 else // Yes, get it's number.
156 M = It->second;
157 if (M < Min) Min = M;
158 }
159
160 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
161 if (Min != MyID)
162 return Min; // This is part of a larger SCC!
163
164 // If this is a new SCC, process it now.
165 if (Stack.back() == F) { // Special case the single "SCC" case here.
166 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
167 << F->getName() << "\n");
168 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000169 DSGraph &G = getDSGraph(*F);
170 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
171 calculateGraph(G);
172 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
173 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
174 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000175
Chris Lattnerae5f6032002-11-17 22:16:28 +0000176 if (MaxSCC < 1) MaxSCC = 1;
177
Chris Lattnera9c9c022002-11-11 21:35:13 +0000178 // Should we revisit the graph?
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000179 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000180 ValMap.erase(F);
181 return calculateGraphs(F, Stack, NextID, ValMap);
182 } else {
183 ValMap[F] = ~0U;
184 }
185 return MyID;
186
187 } else {
188 // SCCFunctions - Keep track of the functions in the current SCC
189 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000190 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000191
192 Function *NF;
193 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000194 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000195 do {
196 NF = *--FirstInSCC;
197 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000198
199 // Figure out which graph is the largest one, in order to speed things up
200 // a bit in situations where functions in the SCC have widely different
201 // graph sizes.
202 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000203 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000204 // FIXME: If we used a better way of cloning graphs (ie, just splice all
205 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000206 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
207 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000208 } while (NF != F);
209
Chris Lattner0eea6182003-06-30 05:09:58 +0000210 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000211 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000212
Chris Lattnerae5f6032002-11-17 22:16:28 +0000213 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000214 if (MaxSCC < SCCGraphs.size())
215 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000216
Chris Lattner0eea6182003-06-30 05:09:58 +0000217 // First thing first, collapse all of the DSGraphs into a single graph for
218 // the entire SCC. We computed the largest graph, so clone all of the other
219 // (smaller) graphs into it. Discard all of the old graphs.
220 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000221 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
222 E = SCCGraphs.end(); I != E; ++I) {
223 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000224 if (&G != SCCGraph) {
Chris Lattner16437ff2004-03-04 17:05:28 +0000225 {
226 DSGraph::NodeMapTy NodeMap;
227 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
228 SCCGraph->getReturnNodes(), NodeMap);
229 }
Chris Lattner0eea6182003-06-30 05:09:58 +0000230 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000231 for (DSGraph::retnodes_iterator I = G.retnodes_begin(),
232 E = G.retnodes_end(); I != E; ++I)
Chris Lattnera67138d2004-01-31 21:02:18 +0000233 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000234 delete &G;
235 }
236 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000237
Chris Lattner744f9392003-07-02 04:37:48 +0000238 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000239 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000240
Chris Lattner0eea6182003-06-30 05:09:58 +0000241 // Now that we have one big happy family, resolve all of the call sites in
242 // the graph...
243 calculateGraph(*SCCGraph);
244 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
245 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000246
247 std::cerr << "DONE with SCC #: " << MyID << "\n";
248
249 // We never have to revisit "SCC" processed functions...
250
251 // Drop the stuff we don't need from the end of the stack
252 Stack.erase(FirstInSCC, Stack.end());
253 return MyID;
254 }
255
256 return MyID; // == Min
257}
258
259
Chris Lattner0d9bab82002-07-18 00:12:30 +0000260// releaseMemory - If the pass pipeline is done with this pass, we can release
261// our memory... here...
262//
263void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000264 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
265 E = DSInfo.end(); I != E; ++I) {
266 I->second->getReturnNodes().erase(I->first);
267 if (I->second->getReturnNodes().empty())
268 delete I->second;
269 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000270
271 // Empty map so next time memory is released, data structures are not
272 // re-deleted.
273 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000274 delete GlobalsGraph;
275 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000276}
277
Chris Lattneraf8650e2005-02-01 21:37:27 +0000278static bool isVAHackFn(const Function *F) {
279 return F->getName() == "printf" || F->getName() == "sscanf" ||
280 F->getName() == "fprintf" || F->getName() == "open" ||
281 F->getName() == "sprintf" || F->getName() == "fputs" ||
282 F->getName() == "fscanf";
283}
284
285// isUnresolvableFunction - Return true if this is an unresolvable
286// external function. A direct or indirect call to this cannot be resolved.
287//
288static bool isResolvableFunc(const Function* callee) {
289 return !callee->isExternal() || isVAHackFn(callee);
290}
291
Chris Lattner0eea6182003-06-30 05:09:58 +0000292void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000293 // Move our call site list into TempFCs so that inline call sites go into the
294 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000295 std::list<DSCallSite> TempFCs;
296 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000297 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000298
Chris Lattner0eea6182003-06-30 05:09:58 +0000299 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
300
Chris Lattnerf189bce2005-02-01 17:35:52 +0000301 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000302 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000303 while (!TempFCs.empty()) {
304 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000305
Chris Lattner86db3642005-02-04 19:59:49 +0000306 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000307
Chris Lattner5021b8c2005-03-18 23:19:47 +0000308 // Fast path for noop calls. Note that we don't care about merging globals
309 // in the callee with nodes in the caller here.
310 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
311 TempFCs.erase(TempFCs.begin());
312 continue;
313 }
314
Chris Lattneraf8650e2005-02-01 21:37:27 +0000315 if (CS.isDirectCall()) {
316 Function *F = CS.getCalleeFunc();
317 if (isResolvableFunc(F))
318 if (F->isExternal()) { // Call to fprintf, etc.
319 TempFCs.erase(TempFCs.begin());
320 continue;
321 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000322 CalledFuncs.push_back(F);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000323 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000324 } else {
Chris Lattneraf8650e2005-02-01 21:37:27 +0000325 DSNode *Node = CS.getCalleeNode();
Chris Lattner744f9392003-07-02 04:37:48 +0000326
Chris Lattneraf8650e2005-02-01 21:37:27 +0000327 if (!Node->isIncomplete())
328 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
329 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
330 if (isResolvableFunc(CF) && !CF->isExternal())
Chris Lattner86db3642005-02-04 19:59:49 +0000331 CalledFuncs.push_back(CF);
Chris Lattneraf8650e2005-02-01 21:37:27 +0000332 }
Chris Lattner0321b682004-02-27 20:05:15 +0000333
Chris Lattneraf8650e2005-02-01 21:37:27 +0000334 if (CalledFuncs.empty()) {
335 // Remember that we could not resolve this yet!
336 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000337 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000338 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000339 DSGraph *GI;
Chris Lattnera9548d92005-01-30 23:51:02 +0000340
Chris Lattner86db3642005-02-04 19:59:49 +0000341 if (CalledFuncs.size() == 1) {
342 Function *Callee = CalledFuncs[0];
Chris Lattner20cd1362005-02-01 21:49:43 +0000343 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
344 Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000345
Chris Lattner20cd1362005-02-01 21:49:43 +0000346 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000347 GI = &getDSGraph(*Callee); // Graph to inline
348 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
349
350 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
351 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000352 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
353 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000354 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000355 DSGraph::KeepModRefBits |
356 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
357 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000358 } else {
359 if (!Printed)
360 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
361 std::cerr << " calls " << CalledFuncs.size()
362 << " fns from site: " << CS.getCallSite().getInstruction()
363 << " " << *CS.getCallSite().getInstruction();
364 unsigned NumToPrint = CalledFuncs.size();
365 if (NumToPrint > 8) NumToPrint = 8;
366 std::cerr << " Fns =";
367 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
368 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
369 std::cerr << " " << (*I)->getName();
370 std::cerr << "\n";
371
372 // See if we already computed a graph for this set of callees.
373 std::sort(CalledFuncs.begin(), CalledFuncs.end());
374 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000375 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000376
377 if (IndCallGraph.first == 0) {
378 std::vector<Function*>::iterator I = CalledFuncs.begin(),
379 E = CalledFuncs.end();
380
381 // Start with a copy of the first graph.
382 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I));
383 GI->setGlobalsGraph(Graph.getGlobalsGraph());
384 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
385
386 // Get the argument nodes for the first callee. The return value is
387 // the 0th index in the vector.
388 GI->getFunctionArgumentsForCall(*I, Args);
389
390 // Merge all of the other callees into this graph.
391 for (++I; I != E; ++I) {
392 // If the graph already contains the nodes for the function, don't
393 // bother merging it in again.
394 if (!GI->containsFunction(*I)) {
395 DSGraph::NodeMapTy NodeMap;
396 GI->cloneInto(getDSGraph(**I), GI->getScalarMap(),
397 GI->getReturnNodes(), NodeMap);
398 ++NumBUInlines;
399 }
400
401 std::vector<DSNodeHandle> NextArgs;
402 GI->getFunctionArgumentsForCall(*I, NextArgs);
403 unsigned i = 0, e = Args.size();
404 for (; i != e; ++i) {
405 if (i == NextArgs.size()) break;
406 Args[i].mergeWith(NextArgs[i]);
407 }
408 for (e = NextArgs.size(); i != e; ++i)
409 Args.push_back(NextArgs[i]);
410 }
411
412 // Clean up the final graph!
413 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
414 } else {
415 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
416 }
417
418 GI = IndCallGraph.first;
419
420 // Merge the unified graph into this graph now.
421 DEBUG(std::cerr << " Inlining multi callee graph "
422 << "[" << GI->getGraphSize() << "+"
423 << GI->getAuxFunctionCalls().size() << "] into '"
424 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
425 << Graph.getAuxFunctionCalls().size() << "]\n");
426
427 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
428 DSGraph::KeepModRefBits |
429 DSGraph::StripAllocaBit |
430 DSGraph::DontCloneCallNodes);
431 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000432 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000433 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000434 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000435 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000436
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000437 // Recompute the Incomplete markers
Chris Lattnerd10b5fd2004-02-20 23:52:15 +0000438 assert(Graph.getInlinedGlobals().empty());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000439 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000440 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000441
442 // Delete dead nodes. Treat globals that are unreachable but that can
443 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000444 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000445
Chris Lattner35679372004-02-21 00:30:28 +0000446 // When this graph is finalized, clone the globals in the graph into the
447 // globals graph to make sure it has everything, from all graphs.
448 DSScalarMap &MainSM = Graph.getScalarMap();
449 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
450
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000451 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000452 // globals graph.
453 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
454 E = MainSM.global_end(); I != E; ++I)
455 RC.getClonedNH(MainSM[*I]);
456
Chris Lattnera9c9c022002-11-11 21:35:13 +0000457 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000458}
Chris Lattner851b5342005-01-24 20:00:14 +0000459
460static const Function *getFnForValue(const Value *V) {
461 if (const Instruction *I = dyn_cast<Instruction>(V))
462 return I->getParent()->getParent();
463 else if (const Argument *A = dyn_cast<Argument>(V))
464 return A->getParent();
465 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
466 return BB->getParent();
467 return 0;
468}
469
470/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
471/// These correspond to the interfaces defined in the AliasAnalysis class.
472void BUDataStructures::deleteValue(Value *V) {
473 if (const Function *F = getFnForValue(V)) { // Function local value?
474 // If this is a function local value, just delete it from the scalar map!
475 getDSGraph(*F).getScalarMap().eraseIfExists(V);
476 return;
477 }
478
Chris Lattnercff8ac22005-01-31 00:10:45 +0000479 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000480 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
481 "cannot handle scc's");
482 delete DSInfo[F];
483 DSInfo.erase(F);
484 return;
485 }
486
487 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
488}
489
490void BUDataStructures::copyValue(Value *From, Value *To) {
491 if (From == To) return;
492 if (const Function *F = getFnForValue(From)) { // Function local value?
493 // If this is a function local value, just delete it from the scalar map!
494 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
495 return;
496 }
497
498 if (Function *FromF = dyn_cast<Function>(From)) {
499 Function *ToF = cast<Function>(To);
500 assert(!DSInfo.count(ToF) && "New Function already exists!");
501 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
502 DSInfo[ToF] = NG;
503 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
504
505 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000506 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000507 NG->getReturnNodes().clear();
508 NG->getReturnNodes()[ToF] = Ret;
509 return;
510 }
511
512 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
513}