blob: 47ffc87cc7d5ed31d035dd2792a6fb930ac11808 [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
Chris Lattner9547ade2005-03-22 22:10:22 +000085 // Mark external globals incomplete.
86 GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
87
Chris Lattnera66e3532005-03-13 20:15:06 +000088 // Merge the globals variables (not the calls) from the globals graph back
89 // into the main function's graph so that the main function contains all of
90 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +000091 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +000092 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
93 const DSGraph &GG = *MainGraph.getGlobalsGraph();
94 ReachabilityCloner RC(MainGraph, GG,
95 DSGraph::DontCloneCallNodes |
96 DSGraph::DontCloneAuxCallNodes);
97
98 // Clone the global nodes into this graph.
99 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
100 E = GG.getScalarMap().global_end(); I != E; ++I)
101 if (isa<GlobalVariable>(*I))
102 RC.getClonedNH(GG.getNodeForValue(*I));
103
Chris Lattner270cf502005-03-13 20:32:26 +0000104 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000105 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
106 DSGraph::IgnoreGlobals);
107 }
108
Chris Lattneraa0b4682002-11-09 21:12:07 +0000109 return false;
110}
Chris Lattner55c10582002-10-03 20:38:41 +0000111
Chris Lattnera9c9c022002-11-11 21:35:13 +0000112DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
113 // Has the graph already been created?
114 DSGraph *&Graph = DSInfo[F];
115 if (Graph) return *Graph;
116
117 // Copy the local version into DSInfo...
Chris Lattnerf4f62272005-03-19 22:23:45 +0000118 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F),
119 GlobalECs);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000120
121 Graph->setGlobalsGraph(GlobalsGraph);
122 Graph->setPrintAuxCalls();
123
124 // Start with a copy of the original call sites...
125 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
126 return *Graph;
127}
128
Chris Lattner6b9eb352005-03-20 02:42:07 +0000129static bool isVAHackFn(const Function *F) {
130 return F->getName() == "printf" || F->getName() == "sscanf" ||
131 F->getName() == "fprintf" || F->getName() == "open" ||
132 F->getName() == "sprintf" || F->getName() == "fputs" ||
133 F->getName() == "fscanf";
134}
135
136static bool isResolvableFunc(const Function* callee) {
137 return !callee->isExternal() || isVAHackFn(callee);
138}
139
140static void GetAllCallees(const DSCallSite &CS,
141 std::vector<Function*> &Callees) {
142 if (CS.isDirectCall()) {
143 if (isResolvableFunc(CS.getCalleeFunc()))
144 Callees.push_back(CS.getCalleeFunc());
145 } else if (!CS.getCalleeNode()->isIncomplete()) {
146 // Get all callees.
147 unsigned OldSize = Callees.size();
148 CS.getCalleeNode()->addFullFunctionList(Callees);
149
150 // If any of the callees are unresolvable, remove the whole batch!
151 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
152 if (!isResolvableFunc(Callees[i])) {
153 Callees.erase(Callees.begin()+OldSize, Callees.end());
154 return;
155 }
156 }
157}
158
159
160/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
161/// the aux list for the specified graph in the Callees vector.
162static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
163 Callees.clear();
164 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
165 GetAllCallees(*I, Callees);
166}
167
Chris Lattnera9c9c022002-11-11 21:35:13 +0000168unsigned BUDataStructures::calculateGraphs(Function *F,
169 std::vector<Function*> &Stack,
170 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000171 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000172 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000173 unsigned Min = NextID++, MyID = Min;
174 ValMap[F] = Min;
175 Stack.push_back(F);
176
Chris Lattner16437ff2004-03-04 17:05:28 +0000177 // FIXME! This test should be generalized to be any function that we have
178 // already processed, in the case when there isn't a main or there are
179 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000180 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
181 // No callees!
182 Stack.pop_back();
183 ValMap[F] = ~0;
184 return Min;
185 }
186
187 DSGraph &Graph = getOrCreateGraph(F);
188
Chris Lattner6b9eb352005-03-20 02:42:07 +0000189 // Find all callee functions.
190 std::vector<Function*> CalleeFunctions;
191 GetAllAuxCallees(Graph, CalleeFunctions);
192
Chris Lattnera9c9c022002-11-11 21:35:13 +0000193 // The edges out of the current node are the call site targets...
Chris Lattner6b9eb352005-03-20 02:42:07 +0000194 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
195 Function *Callee = CalleeFunctions[i];
Chris Lattnera9c9c022002-11-11 21:35:13 +0000196 unsigned M;
197 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000198 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000199 if (It == ValMap.end()) // No, visit it now.
200 M = calculateGraphs(Callee, Stack, NextID, ValMap);
201 else // Yes, get it's number.
202 M = It->second;
203 if (M < Min) Min = M;
204 }
205
206 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
207 if (Min != MyID)
208 return Min; // This is part of a larger SCC!
209
210 // If this is a new SCC, process it now.
211 if (Stack.back() == F) { // Special case the single "SCC" case here.
212 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
213 << F->getName() << "\n");
214 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000215 DSGraph &G = getDSGraph(*F);
216 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
217 calculateGraph(G);
218 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
219 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
220 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000221
Chris Lattnerae5f6032002-11-17 22:16:28 +0000222 if (MaxSCC < 1) MaxSCC = 1;
223
Chris Lattner6b9eb352005-03-20 02:42:07 +0000224 // Should we revisit the graph? Only do it if there are now new resolvable
225 // callees.
226 GetAllAuxCallees(Graph, CalleeFunctions);
227 if (!CalleeFunctions.empty()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000228 ValMap.erase(F);
229 return calculateGraphs(F, Stack, NextID, ValMap);
230 } else {
231 ValMap[F] = ~0U;
232 }
233 return MyID;
234
235 } else {
236 // SCCFunctions - Keep track of the functions in the current SCC
237 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000238 hash_set<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000239
240 Function *NF;
241 std::vector<Function*>::iterator FirstInSCC = Stack.end();
Chris Lattner0eea6182003-06-30 05:09:58 +0000242 DSGraph *SCCGraph = 0;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000243 do {
244 NF = *--FirstInSCC;
245 ValMap[NF] = ~0U;
Chris Lattner0eea6182003-06-30 05:09:58 +0000246
247 // Figure out which graph is the largest one, in order to speed things up
248 // a bit in situations where functions in the SCC have widely different
249 // graph sizes.
250 DSGraph &NFGraph = getDSGraph(*NF);
Chris Lattnera67138d2004-01-31 21:02:18 +0000251 SCCGraphs.insert(&NFGraph);
Chris Lattner16437ff2004-03-04 17:05:28 +0000252 // FIXME: If we used a better way of cloning graphs (ie, just splice all
253 // of the nodes into the new graph), this would be completely unneeded!
Chris Lattner0eea6182003-06-30 05:09:58 +0000254 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
255 SCCGraph = &NFGraph;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000256 } while (NF != F);
257
Chris Lattner0eea6182003-06-30 05:09:58 +0000258 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
Chris Lattnera67138d2004-01-31 21:02:18 +0000259 << SCCGraphs.size() << "\n";
Chris Lattnera9c9c022002-11-11 21:35:13 +0000260
Chris Lattnerae5f6032002-11-17 22:16:28 +0000261 // Compute the Max SCC Size...
Chris Lattnera67138d2004-01-31 21:02:18 +0000262 if (MaxSCC < SCCGraphs.size())
263 MaxSCC = SCCGraphs.size();
Chris Lattnerae5f6032002-11-17 22:16:28 +0000264
Chris Lattner0eea6182003-06-30 05:09:58 +0000265 // First thing first, collapse all of the DSGraphs into a single graph for
266 // the entire SCC. We computed the largest graph, so clone all of the other
267 // (smaller) graphs into it. Discard all of the old graphs.
268 //
Chris Lattnera67138d2004-01-31 21:02:18 +0000269 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
270 E = SCCGraphs.end(); I != E; ++I) {
271 DSGraph &G = **I;
Chris Lattner0eea6182003-06-30 05:09:58 +0000272 if (&G != SCCGraph) {
Chris Lattnera2197132005-03-22 00:36:51 +0000273 SCCGraph->cloneInto(G);
274
Chris Lattner0eea6182003-06-30 05:09:58 +0000275 // Update the DSInfo map and delete the old graph...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000276 for (DSGraph::retnodes_iterator I = G.retnodes_begin(),
277 E = G.retnodes_end(); I != E; ++I)
Chris Lattnera67138d2004-01-31 21:02:18 +0000278 DSInfo[I->first] = SCCGraph;
Chris Lattner0eea6182003-06-30 05:09:58 +0000279 delete &G;
280 }
281 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000282
Chris Lattner744f9392003-07-02 04:37:48 +0000283 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerac6d4852004-11-08 21:08:46 +0000284 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000285
Chris Lattner0eea6182003-06-30 05:09:58 +0000286 // Now that we have one big happy family, resolve all of the call sites in
287 // the graph...
288 calculateGraph(*SCCGraph);
289 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
290 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000291
292 std::cerr << "DONE with SCC #: " << MyID << "\n";
293
294 // We never have to revisit "SCC" processed functions...
295
296 // Drop the stuff we don't need from the end of the stack
297 Stack.erase(FirstInSCC, Stack.end());
298 return MyID;
299 }
300
301 return MyID; // == Min
302}
303
304
Chris Lattner0d9bab82002-07-18 00:12:30 +0000305// releaseMemory - If the pass pipeline is done with this pass, we can release
306// our memory... here...
307//
308void BUDataStructures::releaseMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000309 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
310 E = DSInfo.end(); I != E; ++I) {
311 I->second->getReturnNodes().erase(I->first);
312 if (I->second->getReturnNodes().empty())
313 delete I->second;
314 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000315
316 // Empty map so next time memory is released, data structures are not
317 // re-deleted.
318 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000319 delete GlobalsGraph;
320 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000321}
322
Chris Lattner0eea6182003-06-30 05:09:58 +0000323void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000324 // Move our call site list into TempFCs so that inline call sites go into the
325 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000326 std::list<DSCallSite> TempFCs;
327 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000328 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000329
Chris Lattner0eea6182003-06-30 05:09:58 +0000330 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
331
Chris Lattnerf189bce2005-02-01 17:35:52 +0000332 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000333 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000334 while (!TempFCs.empty()) {
335 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000336
Chris Lattner86db3642005-02-04 19:59:49 +0000337 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000338
Chris Lattner5021b8c2005-03-18 23:19:47 +0000339 // Fast path for noop calls. Note that we don't care about merging globals
340 // in the callee with nodes in the caller here.
341 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
342 TempFCs.erase(TempFCs.begin());
343 continue;
Chris Lattner6b9eb352005-03-20 02:42:07 +0000344 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
345 TempFCs.erase(TempFCs.begin());
346 continue;
Chris Lattner5021b8c2005-03-18 23:19:47 +0000347 }
348
Chris Lattner6b9eb352005-03-20 02:42:07 +0000349 GetAllCallees(CS, CalledFuncs);
Chris Lattner0321b682004-02-27 20:05:15 +0000350
Chris Lattneraf8650e2005-02-01 21:37:27 +0000351 if (CalledFuncs.empty()) {
352 // Remember that we could not resolve this yet!
353 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000354 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000355 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000356 DSGraph *GI;
Chris Lattnereb144f52005-03-21 20:20:49 +0000357 Instruction *TheCall = CS.getCallSite().getInstruction();
Chris Lattnera9548d92005-01-30 23:51:02 +0000358
Chris Lattner86db3642005-02-04 19:59:49 +0000359 if (CalledFuncs.size() == 1) {
360 Function *Callee = CalledFuncs[0];
Chris Lattnereb144f52005-03-21 20:20:49 +0000361 ActualCallees.insert(std::make_pair(TheCall, Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000362
Chris Lattner20cd1362005-02-01 21:49:43 +0000363 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000364 GI = &getDSGraph(*Callee); // Graph to inline
365 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
366
367 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
368 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000369 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
370 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000371 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000372 DSGraph::KeepModRefBits |
373 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
374 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000375 } else {
376 if (!Printed)
377 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
378 std::cerr << " calls " << CalledFuncs.size()
379 << " fns from site: " << CS.getCallSite().getInstruction()
380 << " " << *CS.getCallSite().getInstruction();
Chris Lattner86db3642005-02-04 19:59:49 +0000381 std::cerr << " Fns =";
Chris Lattnereb144f52005-03-21 20:20:49 +0000382 unsigned NumPrinted = 0;
383
Chris Lattner86db3642005-02-04 19:59:49 +0000384 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
Chris Lattnereb144f52005-03-21 20:20:49 +0000385 E = CalledFuncs.end(); I != E; ++I) {
386 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
387
388 // Add the call edges to the call graph.
389 ActualCallees.insert(std::make_pair(TheCall, *I));
390 }
Chris Lattner86db3642005-02-04 19:59:49 +0000391 std::cerr << "\n";
392
393 // See if we already computed a graph for this set of callees.
394 std::sort(CalledFuncs.begin(), CalledFuncs.end());
395 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000396 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000397
398 if (IndCallGraph.first == 0) {
399 std::vector<Function*>::iterator I = CalledFuncs.begin(),
400 E = CalledFuncs.end();
401
402 // Start with a copy of the first graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000403 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
Chris Lattner86db3642005-02-04 19:59:49 +0000404 GI->setGlobalsGraph(Graph.getGlobalsGraph());
405 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
406
407 // Get the argument nodes for the first callee. The return value is
408 // the 0th index in the vector.
409 GI->getFunctionArgumentsForCall(*I, Args);
410
411 // Merge all of the other callees into this graph.
412 for (++I; I != E; ++I) {
413 // If the graph already contains the nodes for the function, don't
414 // bother merging it in again.
415 if (!GI->containsFunction(*I)) {
Chris Lattnera2197132005-03-22 00:36:51 +0000416 GI->cloneInto(getDSGraph(**I));
Chris Lattner86db3642005-02-04 19:59:49 +0000417 ++NumBUInlines;
418 }
419
420 std::vector<DSNodeHandle> NextArgs;
421 GI->getFunctionArgumentsForCall(*I, NextArgs);
422 unsigned i = 0, e = Args.size();
423 for (; i != e; ++i) {
424 if (i == NextArgs.size()) break;
425 Args[i].mergeWith(NextArgs[i]);
426 }
427 for (e = NextArgs.size(); i != e; ++i)
428 Args.push_back(NextArgs[i]);
429 }
430
431 // Clean up the final graph!
432 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
433 } else {
434 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
435 }
436
437 GI = IndCallGraph.first;
438
439 // Merge the unified graph into this graph now.
440 DEBUG(std::cerr << " Inlining multi callee graph "
441 << "[" << GI->getGraphSize() << "+"
442 << GI->getAuxFunctionCalls().size() << "] into '"
443 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
444 << Graph.getAuxFunctionCalls().size() << "]\n");
445
446 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
447 DSGraph::KeepModRefBits |
448 DSGraph::StripAllocaBit |
449 DSGraph::DontCloneCallNodes);
450 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000451 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000452 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000453 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000454 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000455
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000456 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000457 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000458 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000459
460 // Delete dead nodes. Treat globals that are unreachable but that can
461 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000462 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000463
Chris Lattner35679372004-02-21 00:30:28 +0000464 // When this graph is finalized, clone the globals in the graph into the
465 // globals graph to make sure it has everything, from all graphs.
466 DSScalarMap &MainSM = Graph.getScalarMap();
467 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
468
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000469 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000470 // globals graph.
471 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
472 E = MainSM.global_end(); I != E; ++I)
473 RC.getClonedNH(MainSM[*I]);
474
Chris Lattnera9c9c022002-11-11 21:35:13 +0000475 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000476}
Chris Lattner851b5342005-01-24 20:00:14 +0000477
478static const Function *getFnForValue(const Value *V) {
479 if (const Instruction *I = dyn_cast<Instruction>(V))
480 return I->getParent()->getParent();
481 else if (const Argument *A = dyn_cast<Argument>(V))
482 return A->getParent();
483 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
484 return BB->getParent();
485 return 0;
486}
487
488/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
489/// These correspond to the interfaces defined in the AliasAnalysis class.
490void BUDataStructures::deleteValue(Value *V) {
491 if (const Function *F = getFnForValue(V)) { // Function local value?
492 // If this is a function local value, just delete it from the scalar map!
493 getDSGraph(*F).getScalarMap().eraseIfExists(V);
494 return;
495 }
496
Chris Lattnercff8ac22005-01-31 00:10:45 +0000497 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000498 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
499 "cannot handle scc's");
500 delete DSInfo[F];
501 DSInfo.erase(F);
502 return;
503 }
504
505 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
506}
507
508void BUDataStructures::copyValue(Value *From, Value *To) {
509 if (From == To) return;
510 if (const Function *F = getFnForValue(From)) { // Function local value?
511 // If this is a function local value, just delete it from the scalar map!
512 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
513 return;
514 }
515
516 if (Function *FromF = dyn_cast<Function>(From)) {
517 Function *ToF = cast<Function>(To);
518 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000519 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000520 DSInfo[ToF] = NG;
521 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
522
523 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000524 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000525 NG->getReturnNodes().clear();
526 NG->getReturnNodes()[ToF] = Ret;
527 return;
528 }
529
530 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");
531}