blob: 4f244b3a364089229a916a9c128c94a051a558fb [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 Lattnera5ed1bd2005-04-21 16:09:43 +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 Lattner33b42762005-03-25 16:45:43 +000034/// BuildGlobalECs - Look at all of the nodes in the globals graph. If any node
35/// contains multiple globals, DSA will never, ever, be able to tell the globals
36/// apart. Instead of maintaining this information in all of the graphs
37/// throughout the entire program, store only a single global (the "leader") in
38/// the graphs, and build equivalence classes for the rest of the globals.
39static void BuildGlobalECs(DSGraph &GG, std::set<GlobalValue*> &ECGlobals) {
40 DSScalarMap &SM = GG.getScalarMap();
41 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
42 for (DSGraph::node_iterator I = GG.node_begin(), E = GG.node_end();
43 I != E; ++I) {
44 if (I->getGlobalsList().size() <= 1) continue;
45
46 // First, build up the equivalence set for this block of globals.
47 const std::vector<GlobalValue*> &GVs = I->getGlobalsList();
48 GlobalValue *First = GVs[0];
49 for (unsigned i = 1, e = GVs.size(); i != e; ++i)
50 GlobalECs.unionSets(First, GVs[i]);
51
52 // Next, get the leader element.
53 assert(First == GlobalECs.getLeaderValue(First) &&
54 "First did not end up being the leader?");
55
56 // Next, remove all globals from the scalar map that are not the leader.
57 assert(GVs[0] == First && "First had to be at the front!");
58 for (unsigned i = 1, e = GVs.size(); i != e; ++i) {
59 ECGlobals.insert(GVs[i]);
60 SM.erase(SM.find(GVs[i]));
61 }
62
63 // Finally, change the global node to only contain the leader.
64 I->clearGlobals();
65 I->addGlobal(First);
66 }
67
68 DEBUG(GG.AssertGraphOK());
69}
70
71/// EliminateUsesOfECGlobals - Once we have determined that some globals are in
72/// really just equivalent to some other globals, remove the globals from the
73/// specified DSGraph (if present), and merge any nodes with their leader nodes.
74static void EliminateUsesOfECGlobals(DSGraph &G,
75 const std::set<GlobalValue*> &ECGlobals) {
76 DSScalarMap &SM = G.getScalarMap();
77 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
78
79 bool MadeChange = false;
80 for (DSScalarMap::global_iterator GI = SM.global_begin(), E = SM.global_end();
81 GI != E; ) {
82 GlobalValue *GV = *GI++;
83 if (!ECGlobals.count(GV)) continue;
84
85 const DSNodeHandle &GVNH = SM[GV];
86 assert(!GVNH.isNull() && "Global has null NH!?");
87
88 // Okay, this global is in some equivalence class. Start by finding the
89 // leader of the class.
90 GlobalValue *Leader = GlobalECs.getLeaderValue(GV);
91
92 // If the leader isn't already in the graph, insert it into the node
93 // corresponding to GV.
94 if (!SM.global_count(Leader)) {
95 GVNH.getNode()->addGlobal(Leader);
96 SM[Leader] = GVNH;
97 } else {
98 // Otherwise, the leader is in the graph, make sure the nodes are the
99 // merged in the specified graph.
100 const DSNodeHandle &LNH = SM[Leader];
101 if (LNH.getNode() != GVNH.getNode())
102 LNH.mergeWith(GVNH);
103 }
104
105 // Next step, remove the global from the DSNode.
106 GVNH.getNode()->removeGlobal(GV);
107
108 // Finally, remove the global from the ScalarMap.
109 SM.erase(GV);
110 MadeChange = true;
111 }
112
113 DEBUG(if(MadeChange) G.AssertGraphOK());
114}
115
Chris Lattneraa0b4682002-11-09 21:12:07 +0000116// run - Calculate the bottom up data structure graphs for each function in the
117// program.
118//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000119bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +0000120 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +0000121 GlobalECs = LocalDSA.getGlobalECs();
122
123 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
Chris Lattner20167e32003-02-03 19:11:38 +0000124 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000125
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000126 IndCallGraphMap = new std::map<std::vector<Function*>,
127 std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
128
Chris Lattnerf189bce2005-02-01 17:35:52 +0000129 std::vector<Function*> Stack;
130 hash_map<Function*, unsigned> ValMap;
131 unsigned NextID = 1;
132
Chris Lattnera9c9c022002-11-11 21:35:13 +0000133 Function *MainFunc = M.getMainFunction();
134 if (MainFunc)
Chris Lattnerf189bce2005-02-01 17:35:52 +0000135 calculateGraphs(MainFunc, Stack, NextID, ValMap);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000136
137 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000138 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000139 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000140#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000141 if (MainFunc)
142 std::cerr << "*** Function unreachable from main: "
143 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000144#endif
Chris Lattnerf189bce2005-02-01 17:35:52 +0000145 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
Chris Lattnera9c9c022002-11-11 21:35:13 +0000146 }
Chris Lattner6c874612003-07-02 23:42:48 +0000147
148 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +0000149
Chris Lattner86db3642005-02-04 19:59:49 +0000150 // If we computed any temporary indcallgraphs, free them now.
151 for (std::map<std::vector<Function*>,
152 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000153 IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
Chris Lattner86db3642005-02-04 19:59:49 +0000154 I->second.second.clear(); // Drop arg refs into the graph.
155 delete I->second.first;
156 }
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000157 delete IndCallGraphMap;
Chris Lattner86db3642005-02-04 19:59:49 +0000158
Chris Lattnerec157b72003-09-20 23:27:05 +0000159 // At the end of the bottom-up pass, the globals graph becomes complete.
160 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +0000161 // nothing! In particular, externally visible globals and unresolvable call
162 // nodes at the end of the BU phase should make things that they point to
163 // incomplete in the globals graph.
164 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +0000165 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +0000166 GlobalsGraph->maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000167
Chris Lattner9547ade2005-03-22 22:10:22 +0000168 // Mark external globals incomplete.
169 GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
170
Chris Lattner33b42762005-03-25 16:45:43 +0000171 // Grow the equivalence classes for the globals to include anything that we
172 // now know to be aliased.
173 std::set<GlobalValue*> ECGlobals;
174 BuildGlobalECs(*GlobalsGraph, ECGlobals);
175 if (!ECGlobals.empty()) {
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000176 NamedRegionTimer X("Bottom-UP EC Cleanup");
177 std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n";
Chris Lattner33b42762005-03-25 16:45:43 +0000178 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
179 E = DSInfo.end(); I != E; ++I)
180 EliminateUsesOfECGlobals(*I->second, ECGlobals);
181 }
182
Chris Lattnera66e3532005-03-13 20:15:06 +0000183 // Merge the globals variables (not the calls) from the globals graph back
184 // into the main function's graph so that the main function contains all of
185 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +0000186 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +0000187 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
188 const DSGraph &GG = *MainGraph.getGlobalsGraph();
189 ReachabilityCloner RC(MainGraph, GG,
190 DSGraph::DontCloneCallNodes |
191 DSGraph::DontCloneAuxCallNodes);
192
193 // Clone the global nodes into this graph.
194 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
195 E = GG.getScalarMap().global_end(); I != E; ++I)
196 if (isa<GlobalVariable>(*I))
197 RC.getClonedNH(GG.getNodeForValue(*I));
198
Chris Lattner270cf502005-03-13 20:32:26 +0000199 MainGraph.maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000200 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
201 DSGraph::IgnoreGlobals);
202 }
203
Chris Lattneraa0b4682002-11-09 21:12:07 +0000204 return false;
205}
Chris Lattner55c10582002-10-03 20:38:41 +0000206
Chris Lattnera9c9c022002-11-11 21:35:13 +0000207DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
208 // Has the graph already been created?
209 DSGraph *&Graph = DSInfo[F];
210 if (Graph) return *Graph;
211
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000212 DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
213
214 // Steal the local graph.
215 Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
216 Graph->spliceFrom(LocGraph);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000217
218 Graph->setGlobalsGraph(GlobalsGraph);
219 Graph->setPrintAuxCalls();
220
221 // Start with a copy of the original call sites...
222 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
223 return *Graph;
224}
225
Chris Lattner6b9eb352005-03-20 02:42:07 +0000226static bool isVAHackFn(const Function *F) {
227 return F->getName() == "printf" || F->getName() == "sscanf" ||
228 F->getName() == "fprintf" || F->getName() == "open" ||
229 F->getName() == "sprintf" || F->getName() == "fputs" ||
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000230 F->getName() == "fscanf" || F->getName() == "malloc" ||
231 F->getName() == "free";
Chris Lattner6b9eb352005-03-20 02:42:07 +0000232}
233
234static bool isResolvableFunc(const Function* callee) {
235 return !callee->isExternal() || isVAHackFn(callee);
236}
237
238static void GetAllCallees(const DSCallSite &CS,
239 std::vector<Function*> &Callees) {
240 if (CS.isDirectCall()) {
241 if (isResolvableFunc(CS.getCalleeFunc()))
242 Callees.push_back(CS.getCalleeFunc());
243 } else if (!CS.getCalleeNode()->isIncomplete()) {
244 // Get all callees.
245 unsigned OldSize = Callees.size();
246 CS.getCalleeNode()->addFullFunctionList(Callees);
247
248 // If any of the callees are unresolvable, remove the whole batch!
249 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
250 if (!isResolvableFunc(Callees[i])) {
251 Callees.erase(Callees.begin()+OldSize, Callees.end());
252 return;
253 }
254 }
255}
256
257
258/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
259/// the aux list for the specified graph in the Callees vector.
260static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
261 Callees.clear();
262 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
263 GetAllCallees(*I, Callees);
264}
265
Chris Lattnera9c9c022002-11-11 21:35:13 +0000266unsigned BUDataStructures::calculateGraphs(Function *F,
267 std::vector<Function*> &Stack,
268 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000269 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000270 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000271 unsigned Min = NextID++, MyID = Min;
272 ValMap[F] = Min;
273 Stack.push_back(F);
274
Chris Lattner16437ff2004-03-04 17:05:28 +0000275 // FIXME! This test should be generalized to be any function that we have
276 // already processed, in the case when there isn't a main or there are
277 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000278 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
279 // No callees!
280 Stack.pop_back();
281 ValMap[F] = ~0;
282 return Min;
283 }
284
285 DSGraph &Graph = getOrCreateGraph(F);
286
Chris Lattner6b9eb352005-03-20 02:42:07 +0000287 // Find all callee functions.
288 std::vector<Function*> CalleeFunctions;
289 GetAllAuxCallees(Graph, CalleeFunctions);
290
Chris Lattnera9c9c022002-11-11 21:35:13 +0000291 // The edges out of the current node are the call site targets...
Chris Lattner6b9eb352005-03-20 02:42:07 +0000292 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
293 Function *Callee = CalleeFunctions[i];
Chris Lattnera9c9c022002-11-11 21:35:13 +0000294 unsigned M;
295 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000296 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000297 if (It == ValMap.end()) // No, visit it now.
298 M = calculateGraphs(Callee, Stack, NextID, ValMap);
299 else // Yes, get it's number.
300 M = It->second;
301 if (M < Min) Min = M;
302 }
303
304 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
305 if (Min != MyID)
306 return Min; // This is part of a larger SCC!
307
308 // If this is a new SCC, process it now.
309 if (Stack.back() == F) { // Special case the single "SCC" case here.
310 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
311 << F->getName() << "\n");
312 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000313 DSGraph &G = getDSGraph(*F);
314 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
315 calculateGraph(G);
316 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
317 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
318 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000319
Chris Lattnerae5f6032002-11-17 22:16:28 +0000320 if (MaxSCC < 1) MaxSCC = 1;
321
Chris Lattner6b9eb352005-03-20 02:42:07 +0000322 // Should we revisit the graph? Only do it if there are now new resolvable
323 // callees.
324 GetAllAuxCallees(Graph, CalleeFunctions);
325 if (!CalleeFunctions.empty()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000326 ValMap.erase(F);
327 return calculateGraphs(F, Stack, NextID, ValMap);
328 } else {
329 ValMap[F] = ~0U;
330 }
331 return MyID;
332
333 } else {
334 // SCCFunctions - Keep track of the functions in the current SCC
335 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000336 std::vector<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000337
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000338 unsigned SCCSize = 1;
339 Function *NF = Stack.back();
340 ValMap[NF] = ~0U;
341 DSGraph &SCCGraph = getDSGraph(*NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000342
Chris Lattner0eea6182003-06-30 05:09:58 +0000343 // First thing first, collapse all of the DSGraphs into a single graph for
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000344 // the entire SCC. Splice all of the graphs into one and discard all of the
345 // old graphs.
Chris Lattner0eea6182003-06-30 05:09:58 +0000346 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000347 while (NF != F) {
348 Stack.pop_back();
349 NF = Stack.back();
350 ValMap[NF] = ~0U;
Chris Lattnera2197132005-03-22 00:36:51 +0000351
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000352 DSGraph &NFG = getDSGraph(*NF);
353
354 // Update the Function -> DSG map.
355 for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
356 E = NFG.retnodes_end(); I != E; ++I)
357 DSInfo[I->first] = &SCCGraph;
358
359 SCCGraph.spliceFrom(NFG);
360 delete &NFG;
361
362 ++SCCSize;
Chris Lattner0eea6182003-06-30 05:09:58 +0000363 }
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000364 Stack.pop_back();
Chris Lattner20da24c2005-03-25 00:06:09 +0000365
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000366 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
367 << SCCSize << "\n";
368
369 // Compute the Max SCC Size.
370 if (MaxSCC < SCCSize)
371 MaxSCC = SCCSize;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000372
Chris Lattner744f9392003-07-02 04:37:48 +0000373 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000374 SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000375
Chris Lattner0eea6182003-06-30 05:09:58 +0000376 // Now that we have one big happy family, resolve all of the call sites in
377 // the graph...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000378 calculateGraph(SCCGraph);
379 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
380 << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000381
382 std::cerr << "DONE with SCC #: " << MyID << "\n";
383
384 // We never have to revisit "SCC" processed functions...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000385 return MyID;
386 }
387
388 return MyID; // == Min
389}
390
391
Chris Lattner0d9bab82002-07-18 00:12:30 +0000392// releaseMemory - If the pass pipeline is done with this pass, we can release
393// our memory... here...
394//
Chris Lattner65512d22005-03-23 21:59:34 +0000395void BUDataStructures::releaseMyMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000396 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
397 E = DSInfo.end(); I != E; ++I) {
398 I->second->getReturnNodes().erase(I->first);
399 if (I->second->getReturnNodes().empty())
400 delete I->second;
401 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000402
403 // Empty map so next time memory is released, data structures are not
404 // re-deleted.
405 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000406 delete GlobalsGraph;
407 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000408}
409
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000410DSGraph &BUDataStructures::CreateGraphForExternalFunction(const Function &Fn) {
411 Function *F = const_cast<Function*>(&Fn);
412 DSGraph *DSG = new DSGraph(GlobalECs, GlobalsGraph->getTargetData());
413 DSInfo[F] = DSG;
414 DSG->setGlobalsGraph(GlobalsGraph);
415 DSG->setPrintAuxCalls();
416
417 // Add function to the graph.
418 DSG->getReturnNodes().insert(std::make_pair(F, DSNodeHandle()));
419
420 if (F->getName() == "free") { // Taking the address of free.
421
422 // Free should take a single pointer argument, mark it as heap memory.
423 DSNode *N = new DSNode(0, DSG);
424 N->setHeapNodeMarker();
425 DSG->getNodeForValue(F->arg_begin()).mergeWith(N);
426
427 } else {
428 std::cerr << "Unrecognized external function: " << F->getName() << "\n";
429 abort();
430 }
431
432 return *DSG;
433}
434
435
Chris Lattner0eea6182003-06-30 05:09:58 +0000436void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000437 // Move our call site list into TempFCs so that inline call sites go into the
438 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000439 std::list<DSCallSite> TempFCs;
440 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000441 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000442
Chris Lattner0eea6182003-06-30 05:09:58 +0000443 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
444
Chris Lattnerf189bce2005-02-01 17:35:52 +0000445 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000446 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000447 while (!TempFCs.empty()) {
448 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000449
Chris Lattner86db3642005-02-04 19:59:49 +0000450 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000451
Chris Lattner5021b8c2005-03-18 23:19:47 +0000452 // Fast path for noop calls. Note that we don't care about merging globals
453 // in the callee with nodes in the caller here.
454 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
455 TempFCs.erase(TempFCs.begin());
456 continue;
Chris Lattner6b9eb352005-03-20 02:42:07 +0000457 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
458 TempFCs.erase(TempFCs.begin());
459 continue;
Chris Lattner5021b8c2005-03-18 23:19:47 +0000460 }
461
Chris Lattner6b9eb352005-03-20 02:42:07 +0000462 GetAllCallees(CS, CalledFuncs);
Chris Lattner0321b682004-02-27 20:05:15 +0000463
Chris Lattneraf8650e2005-02-01 21:37:27 +0000464 if (CalledFuncs.empty()) {
465 // Remember that we could not resolve this yet!
466 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000467 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000468 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000469 DSGraph *GI;
Chris Lattnereb144f52005-03-21 20:20:49 +0000470 Instruction *TheCall = CS.getCallSite().getInstruction();
Chris Lattnera9548d92005-01-30 23:51:02 +0000471
Chris Lattner86db3642005-02-04 19:59:49 +0000472 if (CalledFuncs.size() == 1) {
473 Function *Callee = CalledFuncs[0];
Chris Lattnereb144f52005-03-21 20:20:49 +0000474 ActualCallees.insert(std::make_pair(TheCall, Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000475
Chris Lattner20cd1362005-02-01 21:49:43 +0000476 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000477 GI = &getDSGraph(*Callee); // Graph to inline
478 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
479
480 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
481 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000482 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
483 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000484 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000485 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
486 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000487 } else {
488 if (!Printed)
489 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
490 std::cerr << " calls " << CalledFuncs.size()
491 << " fns from site: " << CS.getCallSite().getInstruction()
492 << " " << *CS.getCallSite().getInstruction();
Chris Lattner86db3642005-02-04 19:59:49 +0000493 std::cerr << " Fns =";
Chris Lattnereb144f52005-03-21 20:20:49 +0000494 unsigned NumPrinted = 0;
495
Chris Lattner86db3642005-02-04 19:59:49 +0000496 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
Chris Lattnereb144f52005-03-21 20:20:49 +0000497 E = CalledFuncs.end(); I != E; ++I) {
498 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
499
500 // Add the call edges to the call graph.
501 ActualCallees.insert(std::make_pair(TheCall, *I));
502 }
Chris Lattner86db3642005-02-04 19:59:49 +0000503 std::cerr << "\n";
504
505 // See if we already computed a graph for this set of callees.
506 std::sort(CalledFuncs.begin(), CalledFuncs.end());
507 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000508 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000509
510 if (IndCallGraph.first == 0) {
511 std::vector<Function*>::iterator I = CalledFuncs.begin(),
512 E = CalledFuncs.end();
513
514 // Start with a copy of the first graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000515 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
Chris Lattner86db3642005-02-04 19:59:49 +0000516 GI->setGlobalsGraph(Graph.getGlobalsGraph());
517 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
518
519 // Get the argument nodes for the first callee. The return value is
520 // the 0th index in the vector.
521 GI->getFunctionArgumentsForCall(*I, Args);
522
523 // Merge all of the other callees into this graph.
524 for (++I; I != E; ++I) {
525 // If the graph already contains the nodes for the function, don't
526 // bother merging it in again.
527 if (!GI->containsFunction(*I)) {
Chris Lattnera2197132005-03-22 00:36:51 +0000528 GI->cloneInto(getDSGraph(**I));
Chris Lattner86db3642005-02-04 19:59:49 +0000529 ++NumBUInlines;
530 }
531
532 std::vector<DSNodeHandle> NextArgs;
533 GI->getFunctionArgumentsForCall(*I, NextArgs);
534 unsigned i = 0, e = Args.size();
535 for (; i != e; ++i) {
536 if (i == NextArgs.size()) break;
537 Args[i].mergeWith(NextArgs[i]);
538 }
539 for (e = NextArgs.size(); i != e; ++i)
540 Args.push_back(NextArgs[i]);
541 }
542
543 // Clean up the final graph!
544 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
545 } else {
546 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
547 }
548
549 GI = IndCallGraph.first;
550
551 // Merge the unified graph into this graph now.
552 DEBUG(std::cerr << " Inlining multi callee graph "
553 << "[" << GI->getGraphSize() << "+"
554 << GI->getAuxFunctionCalls().size() << "] into '"
555 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
556 << Graph.getAuxFunctionCalls().size() << "]\n");
557
558 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
Chris Lattner86db3642005-02-04 19:59:49 +0000559 DSGraph::StripAllocaBit |
560 DSGraph::DontCloneCallNodes);
561 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000562 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000563 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000564 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000565 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000566
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000567 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000568 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000569 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000570
571 // Delete dead nodes. Treat globals that are unreachable but that can
572 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000573 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000574
Chris Lattner35679372004-02-21 00:30:28 +0000575 // When this graph is finalized, clone the globals in the graph into the
576 // globals graph to make sure it has everything, from all graphs.
577 DSScalarMap &MainSM = Graph.getScalarMap();
578 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
579
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000580 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000581 // globals graph.
582 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
583 E = MainSM.global_end(); I != E; ++I)
584 RC.getClonedNH(MainSM[*I]);
585
Chris Lattnera9c9c022002-11-11 21:35:13 +0000586 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000587}
Chris Lattner851b5342005-01-24 20:00:14 +0000588
589static const Function *getFnForValue(const Value *V) {
590 if (const Instruction *I = dyn_cast<Instruction>(V))
591 return I->getParent()->getParent();
592 else if (const Argument *A = dyn_cast<Argument>(V))
593 return A->getParent();
594 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
595 return BB->getParent();
596 return 0;
597}
598
599/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
600/// These correspond to the interfaces defined in the AliasAnalysis class.
601void BUDataStructures::deleteValue(Value *V) {
602 if (const Function *F = getFnForValue(V)) { // Function local value?
603 // If this is a function local value, just delete it from the scalar map!
604 getDSGraph(*F).getScalarMap().eraseIfExists(V);
605 return;
606 }
607
Chris Lattnercff8ac22005-01-31 00:10:45 +0000608 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000609 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
610 "cannot handle scc's");
611 delete DSInfo[F];
612 DSInfo.erase(F);
613 return;
614 }
615
616 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
617}
618
619void BUDataStructures::copyValue(Value *From, Value *To) {
620 if (From == To) return;
621 if (const Function *F = getFnForValue(From)) { // Function local value?
622 // If this is a function local value, just delete it from the scalar map!
623 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
624 return;
625 }
626
627 if (Function *FromF = dyn_cast<Function>(From)) {
628 Function *ToF = cast<Function>(To);
629 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000630 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000631 DSInfo[ToF] = NG;
632 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
633
634 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000635 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000636 NG->getReturnNodes().clear();
637 NG->getReturnNodes()[ToF] = Ret;
638 return;
639 }
640
Chris Lattnerc5132e62005-03-24 04:22:04 +0000641 if (const Function *F = getFnForValue(To)) {
642 DSGraph &G = getDSGraph(*F);
643 G.getScalarMap().copyScalarIfExists(From, To);
644 return;
645 }
646
647 std::cerr << *From;
648 std::cerr << *To;
649 assert(0 && "Do not know how to copy this yet!");
650 abort();
Chris Lattner851b5342005-01-24 20:00:14 +0000651}