blob: 0a9994c41164149529ad45ee4db9ab0e48bd22a4 [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 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 Lattneraa0b4682002-11-09 21:12:07 +000033// run - Calculate the bottom up data structure graphs for each function in the
34// program.
35//
Chris Lattnerb12914b2004-09-20 04:48:05 +000036bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +000037 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +000038 GlobalECs = LocalDSA.getGlobalECs();
39
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
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...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000115 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F),
116 GlobalECs);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000117
118 Graph->setGlobalsGraph(GlobalsGraph);
119 Graph->setPrintAuxCalls();
120
121 // Start with a copy of the original call sites...
122 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
123 return *Graph;
124}
125
Chris Lattner6b9eb352005-03-20 02:42:07 +0000126static bool isVAHackFn(const Function *F) {
127 return F->getName() == "printf" || F->getName() == "sscanf" ||
128 F->getName() == "fprintf" || F->getName() == "open" ||
129 F->getName() == "sprintf" || F->getName() == "fputs" ||
130 F->getName() == "fscanf";
131}
132
133static bool isResolvableFunc(const Function* callee) {
134 return !callee->isExternal() || isVAHackFn(callee);
135}
136
137static void GetAllCallees(const DSCallSite &CS,
138 std::vector<Function*> &Callees) {
139 if (CS.isDirectCall()) {
140 if (isResolvableFunc(CS.getCalleeFunc()))
141 Callees.push_back(CS.getCalleeFunc());
142 } else if (!CS.getCalleeNode()->isIncomplete()) {
143 // Get all callees.
144 unsigned OldSize = Callees.size();
145 CS.getCalleeNode()->addFullFunctionList(Callees);
146
147 // If any of the callees are unresolvable, remove the whole batch!
148 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
149 if (!isResolvableFunc(Callees[i])) {
150 Callees.erase(Callees.begin()+OldSize, Callees.end());
151 return;
152 }
153 }
154}
155
156
157/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
158/// the aux list for the specified graph in the Callees vector.
159static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
160 Callees.clear();
161 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
162 GetAllCallees(*I, Callees);
163}
164
Chris Lattnera9c9c022002-11-11 21:35:13 +0000165unsigned BUDataStructures::calculateGraphs(Function *F,
166 std::vector<Function*> &Stack,
167 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000168 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000169 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000170 unsigned Min = NextID++, MyID = Min;
171 ValMap[F] = Min;
172 Stack.push_back(F);
173
Chris Lattner16437ff2004-03-04 17:05:28 +0000174 // FIXME! This test should be generalized to be any function that we have
175 // already processed, in the case when there isn't a main or there are
176 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000177 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
178 // No callees!
179 Stack.pop_back();
180 ValMap[F] = ~0;
181 return Min;
182 }
183
184 DSGraph &Graph = getOrCreateGraph(F);
185
Chris Lattner6b9eb352005-03-20 02:42:07 +0000186 // Find all callee functions.
187 std::vector<Function*> CalleeFunctions;
188 GetAllAuxCallees(Graph, CalleeFunctions);
189
Chris Lattnera9c9c022002-11-11 21:35:13 +0000190 // The edges out of the current node are the call site targets...
Chris Lattner6b9eb352005-03-20 02:42:07 +0000191 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
192 Function *Callee = CalleeFunctions[i];
Chris Lattnera9c9c022002-11-11 21:35:13 +0000193 unsigned M;
194 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000195 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000196 if (It == ValMap.end()) // No, visit it now.
197 M = calculateGraphs(Callee, Stack, NextID, ValMap);
198 else // Yes, get it's number.
199 M = It->second;
200 if (M < Min) Min = M;
201 }
202
203 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
204 if (Min != MyID)
205 return Min; // This is part of a larger SCC!
206
207 // If this is a new SCC, process it now.
208 if (Stack.back() == F) { // Special case the single "SCC" case here.
209 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
210 << F->getName() << "\n");
211 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000212 DSGraph &G = getDSGraph(*F);
213 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
214 calculateGraph(G);
215 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
216 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
217 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000218
Chris Lattnerae5f6032002-11-17 22:16:28 +0000219 if (MaxSCC < 1) MaxSCC = 1;
220
Chris Lattner6b9eb352005-03-20 02:42:07 +0000221 // Should we revisit the graph? Only do it if there are now new resolvable
222 // callees.
223 GetAllAuxCallees(Graph, CalleeFunctions);
224 if (!CalleeFunctions.empty()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000225 ValMap.erase(F);
226 return calculateGraphs(F, Stack, NextID, ValMap);
227 } else {
228 ValMap[F] = ~0U;
229 }
230 return MyID;
231
232 } else {
233 // SCCFunctions - Keep track of the functions in the current SCC
234 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000235 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000236
237 Function *NF;
238 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000239 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000240 do {
241 NF = *--FirstInSCC;
242 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000243
244 // Figure out which graph is the largest one, in order to speed things up
245 // a bit in situations where functions in the SCC have widely different
246 // graph sizes.
247 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000248 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000249 // FIXME: If we used a better way of cloning graphs (ie, just splice all
250 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000251 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
252 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000253 } while (NF != F);
254
Chris Lattner0eea6182003-06-30 05:09:58 +0000255 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000256 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000257
Chris Lattnerae5f6032002-11-17 22:16:28 +0000258 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000259 if (MaxSCC < SCCGraphs.size())
260 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000261
Chris Lattner0eea6182003-06-30 05:09:58 +0000262 // First thing first, collapse all of the DSGraphs into a single graph for
263 // the entire SCC. We computed the largest graph, so clone all of the other
264 // (smaller) graphs into it. Discard all of the old graphs.
265 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000266 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
267 E = SCCGraphs.end(); I != E; ++I) {
268 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000269 if (&G != SCCGraph) {
Chris Lattnera2197132005-03-22 00:36:51 +0000270 SCCGraph->cloneInto(G);
271
Chris Lattner0eea6182003-06-30 05:09:58 +0000272 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000273 for (DSGraph::retnodes_iterator I = G.retnodes_begin(),
274 E = G.retnodes_end(); I != E; ++I)
Chris Lattnera67138d2004-01-31 21:02:18 +0000275 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000276 delete &G;
277 }
278 }
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 Lattnerac6d4852004-11-08 21:08:46 +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...
285 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...
292
293 // Drop the stuff we don't need from the end of the stack
294 Stack.erase(FirstInSCC, Stack.end());
295 return MyID;
296 }
297
298 return MyID; // == Min
299}
300
301
Chris Lattner0d9bab82002-07-18 00:12:30 +0000302// releaseMemory - If the pass pipeline is done with this pass, we can release
303// our memory... here...
304//
305void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000306 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
307 E = DSInfo.end(); I != E; ++I) {
308 I->second->getReturnNodes().erase(I->first);
309 if (I->second->getReturnNodes().empty())
310 delete I->second;
311 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000312
313 // Empty map so next time memory is released, data structures are not
314 // re-deleted.
315 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000316 delete GlobalsGraph;
317 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000318}
319
Chris Lattner0eea6182003-06-30 05:09:58 +0000320void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000321 // Move our call site list into TempFCs so that inline call sites go into the
322 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000323 std::list<DSCallSite> TempFCs;
324 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000325 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000326
Chris Lattner0eea6182003-06-30 05:09:58 +0000327 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
328
Chris Lattnerf189bce2005-02-01 17:35:52 +0000329 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000330 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000331 while (!TempFCs.empty()) {
332 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000333
Chris Lattner86db3642005-02-04 19:59:49 +0000334 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000335
Chris Lattner5021b8c2005-03-18 23:19:47 +0000336 // Fast path for noop calls. Note that we don't care about merging globals
337 // in the callee with nodes in the caller here.
338 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
339 TempFCs.erase(TempFCs.begin());
340 continue;
Chris Lattner6b9eb352005-03-20 02:42:07 +0000341 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
342 TempFCs.erase(TempFCs.begin());
343 continue;
Chris Lattner5021b8c2005-03-18 23:19:47 +0000344 }
345
Chris Lattner6b9eb352005-03-20 02:42:07 +0000346 GetAllCallees(CS, CalledFuncs);
Chris Lattner0321b682004-02-27 20:05:15 +0000347
Chris Lattneraf8650e2005-02-01 21:37:27 +0000348 if (CalledFuncs.empty()) {
349 // Remember that we could not resolve this yet!
350 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000351 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000352 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000353 DSGraph *GI;
Chris Lattnereb144f52005-03-21 20:20:49 +0000354 Instruction *TheCall = CS.getCallSite().getInstruction();
Chris Lattnera9548d92005-01-30 23:51:02 +0000355
Chris Lattner86db3642005-02-04 19:59:49 +0000356 if (CalledFuncs.size() == 1) {
357 Function *Callee = CalledFuncs[0];
Chris Lattnereb144f52005-03-21 20:20:49 +0000358 ActualCallees.insert(std::make_pair(TheCall, Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000359
Chris Lattner20cd1362005-02-01 21:49:43 +0000360 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000361 GI = &getDSGraph(*Callee); // Graph to inline
362 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
363
364 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
365 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000366 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
367 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000368 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000369 DSGraph::KeepModRefBits |
370 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
371 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000372 } else {
373 if (!Printed)
374 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
375 std::cerr << " calls " << CalledFuncs.size()
376 << " fns from site: " << CS.getCallSite().getInstruction()
377 << " " << *CS.getCallSite().getInstruction();
Chris Lattner86db3642005-02-04 19:59:49 +0000378 std::cerr << " Fns =";
Chris Lattnereb144f52005-03-21 20:20:49 +0000379 unsigned NumPrinted = 0;
380
Chris Lattner86db3642005-02-04 19:59:49 +0000381 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
Chris Lattnereb144f52005-03-21 20:20:49 +0000382 E = CalledFuncs.end(); I != E; ++I) {
383 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
384
385 // Add the call edges to the call graph.
386 ActualCallees.insert(std::make_pair(TheCall, *I));
387 }
Chris Lattner86db3642005-02-04 19:59:49 +0000388 std::cerr << "\n";
389
390 // See if we already computed a graph for this set of callees.
391 std::sort(CalledFuncs.begin(), CalledFuncs.end());
392 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000393 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000394
395 if (IndCallGraph.first == 0) {
396 std::vector<Function*>::iterator I = CalledFuncs.begin(),
397 E = CalledFuncs.end();
398
399 // Start with a copy of the first graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000400 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
Chris Lattner86db3642005-02-04 19:59:49 +0000401 GI->setGlobalsGraph(Graph.getGlobalsGraph());
402 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
403
404 // Get the argument nodes for the first callee. The return value is
405 // the 0th index in the vector.
406 GI->getFunctionArgumentsForCall(*I, Args);
407
408 // Merge all of the other callees into this graph.
409 for (++I; I != E; ++I) {
410 // If the graph already contains the nodes for the function, don't
411 // bother merging it in again.
412 if (!GI->containsFunction(*I)) {
Chris Lattnera2197132005-03-22 00:36:51 +0000413 GI->cloneInto(getDSGraph(**I));
Chris Lattner86db3642005-02-04 19:59:49 +0000414 ++NumBUInlines;
415 }
416
417 std::vector<DSNodeHandle> NextArgs;
418 GI->getFunctionArgumentsForCall(*I, NextArgs);
419 unsigned i = 0, e = Args.size();
420 for (; i != e; ++i) {
421 if (i == NextArgs.size()) break;
422 Args[i].mergeWith(NextArgs[i]);
423 }
424 for (e = NextArgs.size(); i != e; ++i)
425 Args.push_back(NextArgs[i]);
426 }
427
428 // Clean up the final graph!
429 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
430 } else {
431 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
432 }
433
434 GI = IndCallGraph.first;
435
436 // Merge the unified graph into this graph now.
437 DEBUG(std::cerr << " Inlining multi callee graph "
438 << "[" << GI->getGraphSize() << "+"
439 << GI->getAuxFunctionCalls().size() << "] into '"
440 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
441 << Graph.getAuxFunctionCalls().size() << "]\n");
442
443 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
444 DSGraph::KeepModRefBits |
445 DSGraph::StripAllocaBit |
446 DSGraph::DontCloneCallNodes);
447 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000448 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000449 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000450 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000451 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000452
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000453 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000454 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000455 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000456
457 // Delete dead nodes. Treat globals that are unreachable but that can
458 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000459 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000460
Chris Lattner35679372004-02-21 00:30:28 +0000461 // When this graph is finalized, clone the globals in the graph into the
462 // globals graph to make sure it has everything, from all graphs.
463 DSScalarMap &MainSM = Graph.getScalarMap();
464 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
465
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000466 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000467 // globals graph.
468 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
469 E = MainSM.global_end(); I != E; ++I)
470 RC.getClonedNH(MainSM[*I]);
471
Chris Lattnera9c9c022002-11-11 21:35:13 +0000472 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000473}
Chris Lattner851b5342005-01-24 20:00:14 +0000474
475static const Function *getFnForValue(const Value *V) {
476 if (const Instruction *I = dyn_cast<Instruction>(V))
477 return I->getParent()->getParent();
478 else if (const Argument *A = dyn_cast<Argument>(V))
479 return A->getParent();
480 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
481 return BB->getParent();
482 return 0;
483}
484
485/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
486/// These correspond to the interfaces defined in the AliasAnalysis class.
487void BUDataStructures::deleteValue(Value *V) {
488 if (const Function *F = getFnForValue(V)) { // Function local value?
489 // If this is a function local value, just delete it from the scalar map!
490 getDSGraph(*F).getScalarMap().eraseIfExists(V);
491 return;
492 }
493
Chris Lattnercff8ac22005-01-31 00:10:45 +0000494 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000495 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
496 "cannot handle scc's");
497 delete DSInfo[F];
498 DSInfo.erase(F);
499 return;
500 }
501
502 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
503}
504
505void BUDataStructures::copyValue(Value *From, Value *To) {
506 if (From == To) return;
507 if (const Function *F = getFnForValue(From)) { // Function local value?
508 // If this is a function local value, just delete it from the scalar map!
509 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
510 return;
511 }
512
513 if (Function *FromF = dyn_cast<Function>(From)) {
514 Function *ToF = cast<Function>(To);
515 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000516 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000517 DSInfo[ToF] = NG;
518 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
519
520 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000521 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000522 NG->getReturnNodes().clear();
523 NG->getReturnNodes()[ToF] = Ret;
524 return;
525 }
526
527 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
528}