blob: b97e8649075827ccc9f7089138895d42a969b12c [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 Lattner6b9eb352005-03-20 02:42:07 +000018#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattner0d9bab82002-07-18 00:12:30 +000019#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/Statistic.h"
21#include "llvm/Support/Debug.h"
Chris Lattnerb2dbdc12005-03-25 00:05:04 +000022#include "llvm/Support/Timer.h"
Chris Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Chris Lattnerae5f6032002-11-17 22:16:28 +000025namespace {
26 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000027 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000028 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Chris Lattnerae5f6032002-11-17 22:16:28 +000029
30 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000031 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000032}
Chris Lattner0d9bab82002-07-18 00:12:30 +000033
Chris Lattneraa0b4682002-11-09 21:12:07 +000034// run - Calculate the bottom up data structure graphs for each function in the
35// program.
36//
Chris Lattnerb12914b2004-09-20 04:48:05 +000037bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000038 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000039 GlobalECs = LocalDSA.getGlobalECs();
40
41 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
Chris Lattner20167e32003-02-03 19:11:38 +000042 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +000043
Chris Lattnerbcc70bc2005-02-07 16:09:15 +000044 IndCallGraphMap = new std::map<std::vector<Function*>,
45 std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
46
Chris Lattnerf189bce2005-02-01 17:35:52 +000047 std::vector<Function*> Stack;
48 hash_map<Function*, unsigned> ValMap;
49 unsigned NextID = 1;
50
Chris Lattnera9c9c022002-11-11 21:35:13 +000051 Function *MainFunc = M.getMainFunction();
52 if (MainFunc)
Chris Lattnerf189bce2005-02-01 17:35:52 +000053 calculateGraphs(MainFunc, Stack, NextID, ValMap);
Chris Lattnera9c9c022002-11-11 21:35:13 +000054
55 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +000056 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +000057 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +000058#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +000059 if (MainFunc)
60 std::cerr << "*** Function unreachable from main: "
61 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +000062#endif
Chris Lattnerf189bce2005-02-01 17:35:52 +000063 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
Chris Lattnera9c9c022002-11-11 21:35:13 +000064 }
Chris Lattner6c874612003-07-02 23:42:48 +000065
66 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +000067
Chris Lattner86db3642005-02-04 19:59:49 +000068 // If we computed any temporary indcallgraphs, free them now.
69 for (std::map<std::vector<Function*>,
70 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +000071 IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
Chris Lattner86db3642005-02-04 19:59:49 +000072 I->second.second.clear(); // Drop arg refs into the graph.
73 delete I->second.first;
74 }
Chris Lattnerbcc70bc2005-02-07 16:09:15 +000075 delete IndCallGraphMap;
Chris Lattner86db3642005-02-04 19:59:49 +000076
Chris Lattnerec157b72003-09-20 23:27:05 +000077 // At the end of the bottom-up pass, the globals graph becomes complete.
78 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +000079 // nothing! In particular, externally visible globals and unresolvable call
80 // nodes at the end of the BU phase should make things that they point to
81 // incomplete in the globals graph.
82 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +000083 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +000084 GlobalsGraph->maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +000085
Chris Lattner9547ade2005-03-22 22:10:22 +000086 // Mark external globals incomplete.
87 GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
88
Chris Lattnera66e3532005-03-13 20:15:06 +000089 // Merge the globals variables (not the calls) from the globals graph back
90 // into the main function's graph so that the main function contains all of
91 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000092 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000093 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
94 const DSGraph &GG = *MainGraph.getGlobalsGraph();
95 ReachabilityCloner RC(MainGraph, GG,
96 DSGraph::DontCloneCallNodes |
97 DSGraph::DontCloneAuxCallNodes);
98
99 // Clone the global nodes into this graph.
100 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
101 E = GG.getScalarMap().global_end(); I != E; ++I)
102 if (isa<GlobalVariable>(*I))
103 RC.getClonedNH(GG.getNodeForValue(*I));
104
Chris Lattner270cf502005-03-13 20:32:26 +0000105 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000106 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
107 DSGraph::IgnoreGlobals);
108 }
109
Chris Lattneraa0b4682002-11-09 21:12:07 +0000110 return false;
111}
Chris Lattner55c10582002-10-03 20:38:41 +0000112
Chris Lattnera9c9c022002-11-11 21:35:13 +0000113DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
114 // Has the graph already been created?
115 DSGraph *&Graph = DSInfo[F];
116 if (Graph) return *Graph;
117
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000118 DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
119
120 // Steal the local graph.
121 Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
122 Graph->spliceFrom(LocGraph);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000123
124 Graph->setGlobalsGraph(GlobalsGraph);
125 Graph->setPrintAuxCalls();
126
127 // Start with a copy of the original call sites...
128 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
129 return *Graph;
130}
131
Chris Lattner6b9eb352005-03-20 02:42:07 +0000132static bool isVAHackFn(const Function *F) {
133 return F->getName() == "printf" || F->getName() == "sscanf" ||
134 F->getName() == "fprintf" || F->getName() == "open" ||
135 F->getName() == "sprintf" || F->getName() == "fputs" ||
136 F->getName() == "fscanf";
137}
138
139static bool isResolvableFunc(const Function* callee) {
140 return !callee->isExternal() || isVAHackFn(callee);
141}
142
143static void GetAllCallees(const DSCallSite &CS,
144 std::vector<Function*> &Callees) {
145 if (CS.isDirectCall()) {
146 if (isResolvableFunc(CS.getCalleeFunc()))
147 Callees.push_back(CS.getCalleeFunc());
148 } else if (!CS.getCalleeNode()->isIncomplete()) {
149 // Get all callees.
150 unsigned OldSize = Callees.size();
151 CS.getCalleeNode()->addFullFunctionList(Callees);
152
153 // If any of the callees are unresolvable, remove the whole batch!
154 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
155 if (!isResolvableFunc(Callees[i])) {
156 Callees.erase(Callees.begin()+OldSize, Callees.end());
157 return;
158 }
159 }
160}
161
162
163/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
164/// the aux list for the specified graph in the Callees vector.
165static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
166 Callees.clear();
167 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
168 GetAllCallees(*I, Callees);
169}
170
Chris Lattnera9c9c022002-11-11 21:35:13 +0000171unsigned BUDataStructures::calculateGraphs(Function *F,
172 std::vector<Function*> &Stack,
173 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000174 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000175 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000176 unsigned Min = NextID++, MyID = Min;
177 ValMap[F] = Min;
178 Stack.push_back(F);
179
Chris Lattner16437ff2004-03-04 17:05:28 +0000180 // FIXME! This test should be generalized to be any function that we have
181 // already processed, in the case when there isn't a main or there are
182 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000183 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
184 // No callees!
185 Stack.pop_back();
186 ValMap[F] = ~0;
187 return Min;
188 }
189
190 DSGraph &Graph = getOrCreateGraph(F);
191
Chris Lattner6b9eb352005-03-20 02:42:07 +0000192 // Find all callee functions.
193 std::vector<Function*> CalleeFunctions;
194 GetAllAuxCallees(Graph, CalleeFunctions);
195
Chris Lattnera9c9c022002-11-11 21:35:13 +0000196 // The edges out of the current node are the call site targets...
Chris Lattner6b9eb352005-03-20 02:42:07 +0000197 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
198 Function *Callee = CalleeFunctions[i];
Chris Lattnera9c9c022002-11-11 21:35:13 +0000199 unsigned M;
200 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000201 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000202 if (It == ValMap.end()) // No, visit it now.
203 M = calculateGraphs(Callee, Stack, NextID, ValMap);
204 else // Yes, get it's number.
205 M = It->second;
206 if (M < Min) Min = M;
207 }
208
209 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
210 if (Min != MyID)
211 return Min; // This is part of a larger SCC!
212
213 // If this is a new SCC, process it now.
214 if (Stack.back() == F) { // Special case the single "SCC" case here.
215 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
216 << F->getName() << "\n");
217 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000218 DSGraph &G = getDSGraph(*F);
219 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
220 calculateGraph(G);
221 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
222 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
223 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000224
Chris Lattnerae5f6032002-11-17 22:16:28 +0000225 if (MaxSCC < 1) MaxSCC = 1;
226
Chris Lattner6b9eb352005-03-20 02:42:07 +0000227 // Should we revisit the graph? Only do it if there are now new resolvable
228 // callees.
229 GetAllAuxCallees(Graph, CalleeFunctions);
230 if (!CalleeFunctions.empty()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000231 ValMap.erase(F);
232 return calculateGraphs(F, Stack, NextID, ValMap);
233 } else {
234 ValMap[F] = ~0U;
235 }
236 return MyID;
237
238 } else {
239 // SCCFunctions - Keep track of the functions in the current SCC
240 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000241 std::vector<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000242
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000243 unsigned SCCSize = 1;
244 Function *NF = Stack.back();
245 ValMap[NF] = ~0U;
246 DSGraph &SCCGraph = getDSGraph(*NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000247
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000248 { NamedRegionTimer XX("asldkfjasdf");
Chris Lattnerae5f6032002-11-17 22:16:28 +0000249
Chris Lattner0eea6182003-06-30 05:09:58 +0000250 // First thing first, collapse all of the DSGraphs into a single graph for
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000251 // the entire SCC. Splice all of the graphs into one and discard all of the
252 // old graphs.
Chris Lattner0eea6182003-06-30 05:09:58 +0000253 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000254 while (NF != F) {
255 Stack.pop_back();
256 NF = Stack.back();
257 ValMap[NF] = ~0U;
Chris Lattnera2197132005-03-22 00:36:51 +0000258
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000259 DSGraph &NFG = getDSGraph(*NF);
260
261 // Update the Function -> DSG map.
262 for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
263 E = NFG.retnodes_end(); I != E; ++I)
264 DSInfo[I->first] = &SCCGraph;
265
266 SCCGraph.spliceFrom(NFG);
267 delete &NFG;
268
269 ++SCCSize;
Chris Lattner0eea6182003-06-30 05:09:58 +0000270 }
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000271 Stack.pop_back();
272 }
273 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
274 << SCCSize << "\n";
275
276 // Compute the Max SCC Size.
277 if (MaxSCC < SCCSize)
278 MaxSCC = SCCSize;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000279
Chris Lattner744f9392003-07-02 04:37:48 +0000280 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000281 SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000282
Chris Lattner0eea6182003-06-30 05:09:58 +0000283 // Now that we have one big happy family, resolve all of the call sites in
284 // the graph...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000285 calculateGraph(SCCGraph);
286 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
287 << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000288
289 std::cerr << "DONE with SCC #: " << MyID << "\n";
290
291 // We never have to revisit "SCC" processed functions...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000292 return MyID;
293 }
294
295 return MyID; // == Min
296}
297
298
Chris Lattner0d9bab82002-07-18 00:12:30 +0000299// releaseMemory - If the pass pipeline is done with this pass, we can release
300// our memory... here...
301//
Chris Lattner65512d22005-03-23 21:59:34 +0000302void BUDataStructures::releaseMyMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000303 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
304 E = DSInfo.end(); I != E; ++I) {
305 I->second->getReturnNodes().erase(I->first);
306 if (I->second->getReturnNodes().empty())
307 delete I->second;
308 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000309
310 // Empty map so next time memory is released, data structures are not
311 // re-deleted.
312 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000313 delete GlobalsGraph;
314 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000315}
316
Chris Lattner0eea6182003-06-30 05:09:58 +0000317void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000318 // Move our call site list into TempFCs so that inline call sites go into the
319 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000320 std::list<DSCallSite> TempFCs;
321 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000322 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000323
Chris Lattner0eea6182003-06-30 05:09:58 +0000324 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
325
Chris Lattnerf189bce2005-02-01 17:35:52 +0000326 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000327 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000328 while (!TempFCs.empty()) {
329 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000330
Chris Lattner86db3642005-02-04 19:59:49 +0000331 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000332
Chris Lattner5021b8c2005-03-18 23:19:47 +0000333 // Fast path for noop calls. Note that we don't care about merging globals
334 // in the callee with nodes in the caller here.
335 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
336 TempFCs.erase(TempFCs.begin());
337 continue;
Chris Lattner6b9eb352005-03-20 02:42:07 +0000338 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
339 TempFCs.erase(TempFCs.begin());
340 continue;
Chris Lattner5021b8c2005-03-18 23:19:47 +0000341 }
342
Chris Lattner6b9eb352005-03-20 02:42:07 +0000343 GetAllCallees(CS, CalledFuncs);
Chris Lattner0321b682004-02-27 20:05:15 +0000344
Chris Lattneraf8650e2005-02-01 21:37:27 +0000345 if (CalledFuncs.empty()) {
346 // Remember that we could not resolve this yet!
347 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000348 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000349 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000350 DSGraph *GI;
Chris Lattnereb144f52005-03-21 20:20:49 +0000351 Instruction *TheCall = CS.getCallSite().getInstruction();
Chris Lattnera9548d92005-01-30 23:51:02 +0000352
Chris Lattner86db3642005-02-04 19:59:49 +0000353 if (CalledFuncs.size() == 1) {
354 Function *Callee = CalledFuncs[0];
Chris Lattnereb144f52005-03-21 20:20:49 +0000355 ActualCallees.insert(std::make_pair(TheCall, Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000356
Chris Lattner20cd1362005-02-01 21:49:43 +0000357 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000358 GI = &getDSGraph(*Callee); // Graph to inline
359 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
360
361 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
362 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000363 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
364 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000365 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000366 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
367 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000368 } else {
369 if (!Printed)
370 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
371 std::cerr << " calls " << CalledFuncs.size()
372 << " fns from site: " << CS.getCallSite().getInstruction()
373 << " " << *CS.getCallSite().getInstruction();
Chris Lattner86db3642005-02-04 19:59:49 +0000374 std::cerr << " Fns =";
Chris Lattnereb144f52005-03-21 20:20:49 +0000375 unsigned NumPrinted = 0;
376
Chris Lattner86db3642005-02-04 19:59:49 +0000377 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
Chris Lattnereb144f52005-03-21 20:20:49 +0000378 E = CalledFuncs.end(); I != E; ++I) {
379 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
380
381 // Add the call edges to the call graph.
382 ActualCallees.insert(std::make_pair(TheCall, *I));
383 }
Chris Lattner86db3642005-02-04 19:59:49 +0000384 std::cerr << "\n";
385
386 // See if we already computed a graph for this set of callees.
387 std::sort(CalledFuncs.begin(), CalledFuncs.end());
388 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000389 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000390
391 if (IndCallGraph.first == 0) {
392 std::vector<Function*>::iterator I = CalledFuncs.begin(),
393 E = CalledFuncs.end();
394
395 // Start with a copy of the first graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000396 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
Chris Lattner86db3642005-02-04 19:59:49 +0000397 GI->setGlobalsGraph(Graph.getGlobalsGraph());
398 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
399
400 // Get the argument nodes for the first callee. The return value is
401 // the 0th index in the vector.
402 GI->getFunctionArgumentsForCall(*I, Args);
403
404 // Merge all of the other callees into this graph.
405 for (++I; I != E; ++I) {
406 // If the graph already contains the nodes for the function, don't
407 // bother merging it in again.
408 if (!GI->containsFunction(*I)) {
Chris Lattnera2197132005-03-22 00:36:51 +0000409 GI->cloneInto(getDSGraph(**I));
Chris Lattner86db3642005-02-04 19:59:49 +0000410 ++NumBUInlines;
411 }
412
413 std::vector<DSNodeHandle> NextArgs;
414 GI->getFunctionArgumentsForCall(*I, NextArgs);
415 unsigned i = 0, e = Args.size();
416 for (; i != e; ++i) {
417 if (i == NextArgs.size()) break;
418 Args[i].mergeWith(NextArgs[i]);
419 }
420 for (e = NextArgs.size(); i != e; ++i)
421 Args.push_back(NextArgs[i]);
422 }
423
424 // Clean up the final graph!
425 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
426 } else {
427 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
428 }
429
430 GI = IndCallGraph.first;
431
432 // Merge the unified graph into this graph now.
433 DEBUG(std::cerr << " Inlining multi callee graph "
434 << "[" << GI->getGraphSize() << "+"
435 << GI->getAuxFunctionCalls().size() << "] into '"
436 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
437 << Graph.getAuxFunctionCalls().size() << "]\n");
438
439 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
Chris Lattner86db3642005-02-04 19:59:49 +0000440 DSGraph::StripAllocaBit |
441 DSGraph::DontCloneCallNodes);
442 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000443 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000444 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000445 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000446 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000447
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000448 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000449 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000450 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000451
452 // Delete dead nodes. Treat globals that are unreachable but that can
453 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000454 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000455
Chris Lattner35679372004-02-21 00:30:28 +0000456 // When this graph is finalized, clone the globals in the graph into the
457 // globals graph to make sure it has everything, from all graphs.
458 DSScalarMap &MainSM = Graph.getScalarMap();
459 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
460
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000461 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000462 // globals graph.
463 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
464 E = MainSM.global_end(); I != E; ++I)
465 RC.getClonedNH(MainSM[*I]);
466
Chris Lattnera9c9c022002-11-11 21:35:13 +0000467 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000468}
Chris Lattner851b5342005-01-24 20:00:14 +0000469
470static const Function *getFnForValue(const Value *V) {
471 if (const Instruction *I = dyn_cast<Instruction>(V))
472 return I->getParent()->getParent();
473 else if (const Argument *A = dyn_cast<Argument>(V))
474 return A->getParent();
475 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
476 return BB->getParent();
477 return 0;
478}
479
480/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
481/// These correspond to the interfaces defined in the AliasAnalysis class.
482void BUDataStructures::deleteValue(Value *V) {
483 if (const Function *F = getFnForValue(V)) { // Function local value?
484 // If this is a function local value, just delete it from the scalar map!
485 getDSGraph(*F).getScalarMap().eraseIfExists(V);
486 return;
487 }
488
Chris Lattnercff8ac22005-01-31 00:10:45 +0000489 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000490 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
491 "cannot handle scc's");
492 delete DSInfo[F];
493 DSInfo.erase(F);
494 return;
495 }
496
497 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
498}
499
500void BUDataStructures::copyValue(Value *From, Value *To) {
501 if (From == To) return;
502 if (const Function *F = getFnForValue(From)) { // Function local value?
503 // If this is a function local value, just delete it from the scalar map!
504 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
505 return;
506 }
507
508 if (Function *FromF = dyn_cast<Function>(From)) {
509 Function *ToF = cast<Function>(To);
510 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000511 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000512 DSInfo[ToF] = NG;
513 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
514
515 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000516 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000517 NG->getReturnNodes().clear();
518 NG->getReturnNodes()[ToF] = Ret;
519 return;
520 }
521
Chris Lattnerc5132e62005-03-24 04:22:04 +0000522 if (const Function *F = getFnForValue(To)) {
523 DSGraph &G = getDSGraph(*F);
524 G.getScalarMap().copyScalarIfExists(From, To);
525 return;
526 }
527
528 std::cerr << *From;
529 std::cerr << *To;
530 assert(0 && "Do not know how to copy this yet!");
531 abort();
Chris Lattner851b5342005-01-24 20:00:14 +0000532}