blob: 25ddda5932ea75806abb67a0fe02d52dfab20fe1 [file] [log] [blame]
Chris Lattner55c10582002-10-03 20:38:41 +00001//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattner72382102006-01-22 23:19:18 +000023#include <iostream>
Chris Lattner9a927292003-11-12 23:11:14 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattnerae5f6032002-11-17 22:16:28 +000026namespace {
27 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
Chris Lattnerd391d702003-07-02 20:24:42 +000028 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
Chris Lattner6c874612003-07-02 23:42:48 +000029 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
Misha Brukman2b37d7c2005-04-21 21:13:18 +000030
Chris Lattnerae5f6032002-11-17 22:16:28 +000031 RegisterAnalysis<BUDataStructures>
Chris Lattner312edd32003-06-28 22:14:55 +000032 X("budatastructure", "Bottom-up Data Structure Analysis");
Chris Lattnerae5f6032002-11-17 22:16:28 +000033}
Chris Lattner0d9bab82002-07-18 00:12:30 +000034
Chris Lattner33b42762005-03-25 16:45:43 +000035/// BuildGlobalECs - Look at all of the nodes in the globals graph. If any node
36/// contains multiple globals, DSA will never, ever, be able to tell the globals
37/// apart. Instead of maintaining this information in all of the graphs
38/// throughout the entire program, store only a single global (the "leader") in
39/// the graphs, and build equivalence classes for the rest of the globals.
40static void BuildGlobalECs(DSGraph &GG, std::set<GlobalValue*> &ECGlobals) {
41 DSScalarMap &SM = GG.getScalarMap();
42 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
43 for (DSGraph::node_iterator I = GG.node_begin(), E = GG.node_end();
44 I != E; ++I) {
45 if (I->getGlobalsList().size() <= 1) continue;
46
47 // First, build up the equivalence set for this block of globals.
48 const std::vector<GlobalValue*> &GVs = I->getGlobalsList();
49 GlobalValue *First = GVs[0];
50 for (unsigned i = 1, e = GVs.size(); i != e; ++i)
51 GlobalECs.unionSets(First, GVs[i]);
Misha Brukman2b37d7c2005-04-21 21:13:18 +000052
Chris Lattner33b42762005-03-25 16:45:43 +000053 // Next, get the leader element.
54 assert(First == GlobalECs.getLeaderValue(First) &&
55 "First did not end up being the leader?");
Misha Brukman2b37d7c2005-04-21 21:13:18 +000056
Chris Lattner33b42762005-03-25 16:45:43 +000057 // Next, remove all globals from the scalar map that are not the leader.
58 assert(GVs[0] == First && "First had to be at the front!");
59 for (unsigned i = 1, e = GVs.size(); i != e; ++i) {
60 ECGlobals.insert(GVs[i]);
61 SM.erase(SM.find(GVs[i]));
62 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000063
Chris Lattner33b42762005-03-25 16:45:43 +000064 // Finally, change the global node to only contain the leader.
65 I->clearGlobals();
66 I->addGlobal(First);
67 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +000068
Chris Lattner33b42762005-03-25 16:45:43 +000069 DEBUG(GG.AssertGraphOK());
70}
71
72/// EliminateUsesOfECGlobals - Once we have determined that some globals are in
73/// really just equivalent to some other globals, remove the globals from the
74/// specified DSGraph (if present), and merge any nodes with their leader nodes.
75static void EliminateUsesOfECGlobals(DSGraph &G,
76 const std::set<GlobalValue*> &ECGlobals) {
77 DSScalarMap &SM = G.getScalarMap();
78 EquivalenceClasses<GlobalValue*> &GlobalECs = SM.getGlobalECs();
79
80 bool MadeChange = false;
81 for (DSScalarMap::global_iterator GI = SM.global_begin(), E = SM.global_end();
82 GI != E; ) {
83 GlobalValue *GV = *GI++;
84 if (!ECGlobals.count(GV)) continue;
85
86 const DSNodeHandle &GVNH = SM[GV];
87 assert(!GVNH.isNull() && "Global has null NH!?");
88
89 // Okay, this global is in some equivalence class. Start by finding the
90 // leader of the class.
91 GlobalValue *Leader = GlobalECs.getLeaderValue(GV);
92
93 // If the leader isn't already in the graph, insert it into the node
94 // corresponding to GV.
95 if (!SM.global_count(Leader)) {
96 GVNH.getNode()->addGlobal(Leader);
97 SM[Leader] = GVNH;
98 } else {
99 // Otherwise, the leader is in the graph, make sure the nodes are the
100 // merged in the specified graph.
101 const DSNodeHandle &LNH = SM[Leader];
102 if (LNH.getNode() != GVNH.getNode())
103 LNH.mergeWith(GVNH);
104 }
105
106 // Next step, remove the global from the DSNode.
107 GVNH.getNode()->removeGlobal(GV);
108
109 // Finally, remove the global from the ScalarMap.
110 SM.erase(GV);
111 MadeChange = true;
112 }
113
114 DEBUG(if(MadeChange) G.AssertGraphOK());
115}
116
Chris Lattneraa0b4682002-11-09 21:12:07 +0000117// run - Calculate the bottom up data structure graphs for each function in the
118// program.
119//
Chris Lattnerb12914b2004-09-20 04:48:05 +0000120bool BUDataStructures::runOnModule(Module &M) {
Chris Lattner312edd32003-06-28 22:14:55 +0000121 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
Chris Lattnerf4f62272005-03-19 22:23:45 +0000122 GlobalECs = LocalDSA.getGlobalECs();
123
124 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph(), GlobalECs);
Chris Lattner20167e32003-02-03 19:11:38 +0000125 GlobalsGraph->setPrintAuxCalls();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000126
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000127 IndCallGraphMap = new std::map<std::vector<Function*>,
128 std::pair<DSGraph*, std::vector<DSNodeHandle> > >();
129
Chris Lattnerf189bce2005-02-01 17:35:52 +0000130 std::vector<Function*> Stack;
131 hash_map<Function*, unsigned> ValMap;
132 unsigned NextID = 1;
133
Chris Lattnera9c9c022002-11-11 21:35:13 +0000134 Function *MainFunc = M.getMainFunction();
135 if (MainFunc)
Chris Lattnerf189bce2005-02-01 17:35:52 +0000136 calculateGraphs(MainFunc, Stack, NextID, ValMap);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000137
138 // Calculate the graphs for any functions that are unreachable from main...
Chris Lattneraa0b4682002-11-09 21:12:07 +0000139 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner5d5b6d62003-07-01 16:04:18 +0000140 if (!I->isExternal() && !DSInfo.count(I)) {
Chris Lattnerae5f6032002-11-17 22:16:28 +0000141#ifndef NDEBUG
Chris Lattnera9c9c022002-11-11 21:35:13 +0000142 if (MainFunc)
143 std::cerr << "*** Function unreachable from main: "
144 << I->getName() << "\n";
Chris Lattnerae5f6032002-11-17 22:16:28 +0000145#endif
Chris Lattnerf189bce2005-02-01 17:35:52 +0000146 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
Chris Lattnera9c9c022002-11-11 21:35:13 +0000147 }
Chris Lattner6c874612003-07-02 23:42:48 +0000148
149 NumCallEdges += ActualCallees.size();
Chris Lattnerec157b72003-09-20 23:27:05 +0000150
Chris Lattner86db3642005-02-04 19:59:49 +0000151 // If we computed any temporary indcallgraphs, free them now.
152 for (std::map<std::vector<Function*>,
153 std::pair<DSGraph*, std::vector<DSNodeHandle> > >::iterator I =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000154 IndCallGraphMap->begin(), E = IndCallGraphMap->end(); I != E; ++I) {
Chris Lattner86db3642005-02-04 19:59:49 +0000155 I->second.second.clear(); // Drop arg refs into the graph.
156 delete I->second.first;
157 }
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000158 delete IndCallGraphMap;
Chris Lattner86db3642005-02-04 19:59:49 +0000159
Chris Lattnerec157b72003-09-20 23:27:05 +0000160 // At the end of the bottom-up pass, the globals graph becomes complete.
161 // FIXME: This is not the right way to do this, but it is sorta better than
Chris Lattner11fc9302003-09-20 23:58:33 +0000162 // nothing! In particular, externally visible globals and unresolvable call
163 // nodes at the end of the BU phase should make things that they point to
164 // incomplete in the globals graph.
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000165 //
Chris Lattnerc3f5f772004-02-08 01:51:48 +0000166 GlobalsGraph->removeTriviallyDeadNodes();
Chris Lattnerec157b72003-09-20 23:27:05 +0000167 GlobalsGraph->maskIncompleteMarkers();
Chris Lattnera66e3532005-03-13 20:15:06 +0000168
Chris Lattner9547ade2005-03-22 22:10:22 +0000169 // Mark external globals incomplete.
170 GlobalsGraph->markIncompleteNodes(DSGraph::IgnoreGlobals);
171
Chris Lattner33b42762005-03-25 16:45:43 +0000172 // Grow the equivalence classes for the globals to include anything that we
173 // now know to be aliased.
174 std::set<GlobalValue*> ECGlobals;
175 BuildGlobalECs(*GlobalsGraph, ECGlobals);
176 if (!ECGlobals.empty()) {
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000177 NamedRegionTimer X("Bottom-UP EC Cleanup");
178 std::cerr << "Eliminating " << ECGlobals.size() << " EC Globals!\n";
Chris Lattner33b42762005-03-25 16:45:43 +0000179 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
180 E = DSInfo.end(); I != E; ++I)
181 EliminateUsesOfECGlobals(*I->second, ECGlobals);
182 }
183
Chris Lattnera66e3532005-03-13 20:15:06 +0000184 // Merge the globals variables (not the calls) from the globals graph back
185 // into the main function's graph so that the main function contains all of
186 // the information about global pools and GV usage in the program.
Chris Lattner49e88e82005-03-15 22:10:04 +0000187 if (MainFunc && !MainFunc->isExternal()) {
Chris Lattnera66e3532005-03-13 20:15:06 +0000188 DSGraph &MainGraph = getOrCreateGraph(MainFunc);
189 const DSGraph &GG = *MainGraph.getGlobalsGraph();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000190 ReachabilityCloner RC(MainGraph, GG,
Chris Lattnera66e3532005-03-13 20:15:06 +0000191 DSGraph::DontCloneCallNodes |
192 DSGraph::DontCloneAuxCallNodes);
193
194 // Clone the global nodes into this graph.
195 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
196 E = GG.getScalarMap().global_end(); I != E; ++I)
197 if (isa<GlobalVariable>(*I))
198 RC.getClonedNH(GG.getNodeForValue(*I));
199
Chris Lattner270cf502005-03-13 20:32:26 +0000200 MainGraph.maskIncompleteMarkers();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000201 MainGraph.markIncompleteNodes(DSGraph::MarkFormalArgs |
Chris Lattnera66e3532005-03-13 20:15:06 +0000202 DSGraph::IgnoreGlobals);
203 }
204
Chris Lattneraa0b4682002-11-09 21:12:07 +0000205 return false;
206}
Chris Lattner55c10582002-10-03 20:38:41 +0000207
Chris Lattnera9c9c022002-11-11 21:35:13 +0000208DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
209 // Has the graph already been created?
210 DSGraph *&Graph = DSInfo[F];
211 if (Graph) return *Graph;
212
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000213 DSGraph &LocGraph = getAnalysis<LocalDataStructures>().getDSGraph(*F);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000214
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000215 // Steal the local graph.
216 Graph = new DSGraph(GlobalECs, LocGraph.getTargetData());
217 Graph->spliceFrom(LocGraph);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000218
219 Graph->setGlobalsGraph(GlobalsGraph);
220 Graph->setPrintAuxCalls();
221
222 // Start with a copy of the original call sites...
223 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
224 return *Graph;
225}
226
Chris Lattner6b9eb352005-03-20 02:42:07 +0000227static bool isVAHackFn(const Function *F) {
228 return F->getName() == "printf" || F->getName() == "sscanf" ||
229 F->getName() == "fprintf" || F->getName() == "open" ||
230 F->getName() == "sprintf" || F->getName() == "fputs" ||
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000231 F->getName() == "fscanf" || F->getName() == "malloc" ||
232 F->getName() == "free";
Chris Lattner6b9eb352005-03-20 02:42:07 +0000233}
234
235static bool isResolvableFunc(const Function* callee) {
236 return !callee->isExternal() || isVAHackFn(callee);
237}
238
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000239static void GetAllCallees(const DSCallSite &CS,
Chris Lattner6b9eb352005-03-20 02:42:07 +0000240 std::vector<Function*> &Callees) {
241 if (CS.isDirectCall()) {
242 if (isResolvableFunc(CS.getCalleeFunc()))
243 Callees.push_back(CS.getCalleeFunc());
244 } else if (!CS.getCalleeNode()->isIncomplete()) {
245 // Get all callees.
246 unsigned OldSize = Callees.size();
247 CS.getCalleeNode()->addFullFunctionList(Callees);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000248
Chris Lattner6b9eb352005-03-20 02:42:07 +0000249 // If any of the callees are unresolvable, remove the whole batch!
250 for (unsigned i = OldSize, e = Callees.size(); i != e; ++i)
251 if (!isResolvableFunc(Callees[i])) {
252 Callees.erase(Callees.begin()+OldSize, Callees.end());
253 return;
254 }
255 }
256}
257
258
259/// GetAllAuxCallees - Return a list containing all of the resolvable callees in
260/// the aux list for the specified graph in the Callees vector.
261static void GetAllAuxCallees(DSGraph &G, std::vector<Function*> &Callees) {
262 Callees.clear();
263 for (DSGraph::afc_iterator I = G.afc_begin(), E = G.afc_end(); I != E; ++I)
264 GetAllCallees(*I, Callees);
265}
266
Chris Lattnera9c9c022002-11-11 21:35:13 +0000267unsigned BUDataStructures::calculateGraphs(Function *F,
268 std::vector<Function*> &Stack,
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000269 unsigned &NextID,
Chris Lattner41c04f72003-02-01 04:52:08 +0000270 hash_map<Function*, unsigned> &ValMap) {
Chris Lattner6acfe922003-11-13 05:04:19 +0000271 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000272 unsigned Min = NextID++, MyID = Min;
273 ValMap[F] = Min;
274 Stack.push_back(F);
275
Chris Lattner16437ff2004-03-04 17:05:28 +0000276 // FIXME! This test should be generalized to be any function that we have
277 // already processed, in the case when there isn't a main or there are
278 // unreachable functions!
Chris Lattnera9c9c022002-11-11 21:35:13 +0000279 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
280 // No callees!
281 Stack.pop_back();
282 ValMap[F] = ~0;
283 return Min;
284 }
285
286 DSGraph &Graph = getOrCreateGraph(F);
287
Chris Lattner6b9eb352005-03-20 02:42:07 +0000288 // Find all callee functions.
289 std::vector<Function*> CalleeFunctions;
290 GetAllAuxCallees(Graph, CalleeFunctions);
291
Chris Lattnera9c9c022002-11-11 21:35:13 +0000292 // The edges out of the current node are the call site targets...
Chris Lattner6b9eb352005-03-20 02:42:07 +0000293 for (unsigned i = 0, e = CalleeFunctions.size(); i != e; ++i) {
294 Function *Callee = CalleeFunctions[i];
Chris Lattnera9c9c022002-11-11 21:35:13 +0000295 unsigned M;
296 // Have we visited the destination function yet?
Chris Lattner41c04f72003-02-01 04:52:08 +0000297 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000298 if (It == ValMap.end()) // No, visit it now.
299 M = calculateGraphs(Callee, Stack, NextID, ValMap);
300 else // Yes, get it's number.
301 M = It->second;
302 if (M < Min) Min = M;
303 }
304
305 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
306 if (Min != MyID)
307 return Min; // This is part of a larger SCC!
308
309 // If this is a new SCC, process it now.
310 if (Stack.back() == F) { // Special case the single "SCC" case here.
311 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
312 << F->getName() << "\n");
313 Stack.pop_back();
Chris Lattner0eea6182003-06-30 05:09:58 +0000314 DSGraph &G = getDSGraph(*F);
315 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
316 calculateGraph(G);
317 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
318 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
319 << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000320
Chris Lattnerae5f6032002-11-17 22:16:28 +0000321 if (MaxSCC < 1) MaxSCC = 1;
322
Chris Lattner6b9eb352005-03-20 02:42:07 +0000323 // Should we revisit the graph? Only do it if there are now new resolvable
324 // callees.
325 GetAllAuxCallees(Graph, CalleeFunctions);
326 if (!CalleeFunctions.empty()) {
Chris Lattnera9c9c022002-11-11 21:35:13 +0000327 ValMap.erase(F);
328 return calculateGraphs(F, Stack, NextID, ValMap);
329 } else {
330 ValMap[F] = ~0U;
331 }
332 return MyID;
333
334 } else {
335 // SCCFunctions - Keep track of the functions in the current SCC
336 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000337 std::vector<DSGraph*> SCCGraphs;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000338
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000339 unsigned SCCSize = 1;
340 Function *NF = Stack.back();
341 ValMap[NF] = ~0U;
342 DSGraph &SCCGraph = getDSGraph(*NF);
Chris Lattner0eea6182003-06-30 05:09:58 +0000343
Chris Lattner0eea6182003-06-30 05:09:58 +0000344 // First thing first, collapse all of the DSGraphs into a single graph for
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000345 // the entire SCC. Splice all of the graphs into one and discard all of the
346 // old graphs.
Chris Lattner0eea6182003-06-30 05:09:58 +0000347 //
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000348 while (NF != F) {
349 Stack.pop_back();
350 NF = Stack.back();
351 ValMap[NF] = ~0U;
Chris Lattnera2197132005-03-22 00:36:51 +0000352
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000353 DSGraph &NFG = getDSGraph(*NF);
354
355 // Update the Function -> DSG map.
356 for (DSGraph::retnodes_iterator I = NFG.retnodes_begin(),
357 E = NFG.retnodes_end(); I != E; ++I)
358 DSInfo[I->first] = &SCCGraph;
359
360 SCCGraph.spliceFrom(NFG);
361 delete &NFG;
362
363 ++SCCSize;
Chris Lattner0eea6182003-06-30 05:09:58 +0000364 }
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000365 Stack.pop_back();
Chris Lattner20da24c2005-03-25 00:06:09 +0000366
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000367 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
368 << SCCSize << "\n";
369
370 // Compute the Max SCC Size.
371 if (MaxSCC < SCCSize)
372 MaxSCC = SCCSize;
Chris Lattnera9c9c022002-11-11 21:35:13 +0000373
Chris Lattner744f9392003-07-02 04:37:48 +0000374 // Clean up the graph before we start inlining a bunch again...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000375 SCCGraph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner744f9392003-07-02 04:37:48 +0000376
Chris Lattner0eea6182003-06-30 05:09:58 +0000377 // Now that we have one big happy family, resolve all of the call sites in
378 // the graph...
Chris Lattnerb2dbdc12005-03-25 00:05:04 +0000379 calculateGraph(SCCGraph);
380 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph.getGraphSize()
381 << "+" << SCCGraph.getAuxFunctionCalls().size() << "]\n");
Chris Lattnera9c9c022002-11-11 21:35:13 +0000382
383 std::cerr << "DONE with SCC #: " << MyID << "\n";
384
385 // We never have to revisit "SCC" processed functions...
Chris Lattnera9c9c022002-11-11 21:35:13 +0000386 return MyID;
387 }
388
389 return MyID; // == Min
390}
391
392
Chris Lattner0d9bab82002-07-18 00:12:30 +0000393// releaseMemory - If the pass pipeline is done with this pass, we can release
394// our memory... here...
395//
Chris Lattner65512d22005-03-23 21:59:34 +0000396void BUDataStructures::releaseMyMemory() {
Chris Lattner0eea6182003-06-30 05:09:58 +0000397 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
398 E = DSInfo.end(); I != E; ++I) {
399 I->second->getReturnNodes().erase(I->first);
400 if (I->second->getReturnNodes().empty())
401 delete I->second;
402 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000403
404 // Empty map so next time memory is released, data structures are not
405 // re-deleted.
406 DSInfo.clear();
Chris Lattneraa0b4682002-11-09 21:12:07 +0000407 delete GlobalsGraph;
408 GlobalsGraph = 0;
Chris Lattner0d9bab82002-07-18 00:12:30 +0000409}
410
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000411DSGraph &BUDataStructures::CreateGraphForExternalFunction(const Function &Fn) {
412 Function *F = const_cast<Function*>(&Fn);
413 DSGraph *DSG = new DSGraph(GlobalECs, GlobalsGraph->getTargetData());
414 DSInfo[F] = DSG;
415 DSG->setGlobalsGraph(GlobalsGraph);
416 DSG->setPrintAuxCalls();
417
418 // Add function to the graph.
419 DSG->getReturnNodes().insert(std::make_pair(F, DSNodeHandle()));
420
421 if (F->getName() == "free") { // Taking the address of free.
Jeff Cohen00b168892005-07-27 06:12:32 +0000422
Chris Lattnera5ed1bd2005-04-21 16:09:43 +0000423 // Free should take a single pointer argument, mark it as heap memory.
424 DSNode *N = new DSNode(0, DSG);
425 N->setHeapNodeMarker();
426 DSG->getNodeForValue(F->arg_begin()).mergeWith(N);
427
428 } else {
429 std::cerr << "Unrecognized external function: " << F->getName() << "\n";
430 abort();
431 }
432
433 return *DSG;
434}
435
436
Chris Lattner0eea6182003-06-30 05:09:58 +0000437void BUDataStructures::calculateGraph(DSGraph &Graph) {
Chris Lattnera1198b52005-04-25 19:16:31 +0000438 // If this graph contains the main function, clone the globals graph into this
439 // graph before we inline callees and other fun stuff.
440 bool ContainsMain = false;
441 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
442
443 for (DSGraph::ReturnNodesTy::iterator I = ReturnNodes.begin(),
444 E = ReturnNodes.end(); I != E; ++I)
445 if (I->first->hasExternalLinkage() && I->first->getName() == "main") {
446 ContainsMain = true;
447 break;
448 }
449
450 // If this graph contains main, copy the contents of the globals graph over.
451 // Note that this is *required* for correctness. If a callee contains a use
452 // of a global, we have to make sure to link up nodes due to global-argument
453 // bindings.
454 if (ContainsMain) {
455 const DSGraph &GG = *Graph.getGlobalsGraph();
456 ReachabilityCloner RC(Graph, GG,
457 DSGraph::DontCloneCallNodes |
458 DSGraph::DontCloneAuxCallNodes);
459
460 // Clone the global nodes into this graph.
461 for (DSScalarMap::global_iterator I = GG.getScalarMap().global_begin(),
462 E = GG.getScalarMap().global_end(); I != E; ++I)
463 if (isa<GlobalVariable>(*I))
464 RC.getClonedNH(GG.getNodeForValue(*I));
465 }
466
467
Chris Lattnera9c9c022002-11-11 21:35:13 +0000468 // Move our call site list into TempFCs so that inline call sites go into the
469 // new call site list and doesn't invalidate our iterators!
Chris Lattnera9548d92005-01-30 23:51:02 +0000470 std::list<DSCallSite> TempFCs;
471 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
Chris Lattnera9c9c022002-11-11 21:35:13 +0000472 TempFCs.swap(AuxCallsList);
Chris Lattner8a5db462002-11-11 00:01:34 +0000473
Chris Lattnerf189bce2005-02-01 17:35:52 +0000474 bool Printed = false;
Chris Lattner86db3642005-02-04 19:59:49 +0000475 std::vector<Function*> CalledFuncs;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000476 while (!TempFCs.empty()) {
477 DSCallSite &CS = *TempFCs.begin();
Chris Lattnerf189bce2005-02-01 17:35:52 +0000478
Chris Lattner86db3642005-02-04 19:59:49 +0000479 CalledFuncs.clear();
Chris Lattnera9548d92005-01-30 23:51:02 +0000480
Chris Lattner5021b8c2005-03-18 23:19:47 +0000481 // Fast path for noop calls. Note that we don't care about merging globals
482 // in the callee with nodes in the caller here.
483 if (CS.getRetVal().isNull() && CS.getNumPtrArgs() == 0) {
484 TempFCs.erase(TempFCs.begin());
485 continue;
Chris Lattner6b9eb352005-03-20 02:42:07 +0000486 } else if (CS.isDirectCall() && isVAHackFn(CS.getCalleeFunc())) {
487 TempFCs.erase(TempFCs.begin());
488 continue;
Chris Lattner5021b8c2005-03-18 23:19:47 +0000489 }
490
Chris Lattner6b9eb352005-03-20 02:42:07 +0000491 GetAllCallees(CS, CalledFuncs);
Chris Lattner0321b682004-02-27 20:05:15 +0000492
Chris Lattneraf8650e2005-02-01 21:37:27 +0000493 if (CalledFuncs.empty()) {
494 // Remember that we could not resolve this yet!
495 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
Chris Lattner20cd1362005-02-01 21:49:43 +0000496 continue;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000497 } else {
Chris Lattner86db3642005-02-04 19:59:49 +0000498 DSGraph *GI;
Chris Lattnereb144f52005-03-21 20:20:49 +0000499 Instruction *TheCall = CS.getCallSite().getInstruction();
Chris Lattnera9548d92005-01-30 23:51:02 +0000500
Chris Lattner86db3642005-02-04 19:59:49 +0000501 if (CalledFuncs.size() == 1) {
502 Function *Callee = CalledFuncs[0];
Chris Lattnereb144f52005-03-21 20:20:49 +0000503 ActualCallees.insert(std::make_pair(TheCall, Callee));
Chris Lattner86db3642005-02-04 19:59:49 +0000504
Chris Lattner20cd1362005-02-01 21:49:43 +0000505 // Get the data structure graph for the called function.
Chris Lattner86db3642005-02-04 19:59:49 +0000506 GI = &getDSGraph(*Callee); // Graph to inline
507 DEBUG(std::cerr << " Inlining graph for " << Callee->getName());
508
509 DEBUG(std::cerr << "[" << GI->getGraphSize() << "+"
510 << GI->getAuxFunctionCalls().size() << "] into '"
Chris Lattner20cd1362005-02-01 21:49:43 +0000511 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
512 << Graph.getAuxFunctionCalls().size() << "]\n");
Chris Lattner86db3642005-02-04 19:59:49 +0000513 Graph.mergeInGraph(CS, *Callee, *GI,
Chris Lattner20cd1362005-02-01 21:49:43 +0000514 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
515 ++NumBUInlines;
Chris Lattner86db3642005-02-04 19:59:49 +0000516 } else {
517 if (!Printed)
518 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
519 std::cerr << " calls " << CalledFuncs.size()
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000520 << " fns from site: " << CS.getCallSite().getInstruction()
Chris Lattner86db3642005-02-04 19:59:49 +0000521 << " " << *CS.getCallSite().getInstruction();
Chris Lattner86db3642005-02-04 19:59:49 +0000522 std::cerr << " Fns =";
Chris Lattnereb144f52005-03-21 20:20:49 +0000523 unsigned NumPrinted = 0;
524
Chris Lattner86db3642005-02-04 19:59:49 +0000525 for (std::vector<Function*>::iterator I = CalledFuncs.begin(),
Chris Lattnereb144f52005-03-21 20:20:49 +0000526 E = CalledFuncs.end(); I != E; ++I) {
527 if (NumPrinted++ < 8) std::cerr << " " << (*I)->getName();
528
529 // Add the call edges to the call graph.
530 ActualCallees.insert(std::make_pair(TheCall, *I));
531 }
Chris Lattner86db3642005-02-04 19:59:49 +0000532 std::cerr << "\n";
533
534 // See if we already computed a graph for this set of callees.
535 std::sort(CalledFuncs.begin(), CalledFuncs.end());
536 std::pair<DSGraph*, std::vector<DSNodeHandle> > &IndCallGraph =
Chris Lattnerbcc70bc2005-02-07 16:09:15 +0000537 (*IndCallGraphMap)[CalledFuncs];
Chris Lattner86db3642005-02-04 19:59:49 +0000538
539 if (IndCallGraph.first == 0) {
540 std::vector<Function*>::iterator I = CalledFuncs.begin(),
541 E = CalledFuncs.end();
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000542
Chris Lattner86db3642005-02-04 19:59:49 +0000543 // Start with a copy of the first graph.
Chris Lattnerf4f62272005-03-19 22:23:45 +0000544 GI = IndCallGraph.first = new DSGraph(getDSGraph(**I), GlobalECs);
Chris Lattner86db3642005-02-04 19:59:49 +0000545 GI->setGlobalsGraph(Graph.getGlobalsGraph());
546 std::vector<DSNodeHandle> &Args = IndCallGraph.second;
547
548 // Get the argument nodes for the first callee. The return value is
549 // the 0th index in the vector.
550 GI->getFunctionArgumentsForCall(*I, Args);
551
552 // Merge all of the other callees into this graph.
553 for (++I; I != E; ++I) {
554 // If the graph already contains the nodes for the function, don't
555 // bother merging it in again.
556 if (!GI->containsFunction(*I)) {
Chris Lattnera2197132005-03-22 00:36:51 +0000557 GI->cloneInto(getDSGraph(**I));
Chris Lattner86db3642005-02-04 19:59:49 +0000558 ++NumBUInlines;
559 }
560
561 std::vector<DSNodeHandle> NextArgs;
562 GI->getFunctionArgumentsForCall(*I, NextArgs);
563 unsigned i = 0, e = Args.size();
564 for (; i != e; ++i) {
565 if (i == NextArgs.size()) break;
566 Args[i].mergeWith(NextArgs[i]);
567 }
568 for (e = NextArgs.size(); i != e; ++i)
569 Args.push_back(NextArgs[i]);
570 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000571
Chris Lattner86db3642005-02-04 19:59:49 +0000572 // Clean up the final graph!
573 GI->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
574 } else {
575 std::cerr << "***\n*** RECYCLED GRAPH ***\n***\n";
576 }
577
578 GI = IndCallGraph.first;
579
580 // Merge the unified graph into this graph now.
581 DEBUG(std::cerr << " Inlining multi callee graph "
582 << "[" << GI->getGraphSize() << "+"
583 << GI->getAuxFunctionCalls().size() << "] into '"
584 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
585 << Graph.getAuxFunctionCalls().size() << "]\n");
586
587 Graph.mergeInGraph(CS, IndCallGraph.second, *GI,
Chris Lattner86db3642005-02-04 19:59:49 +0000588 DSGraph::StripAllocaBit |
589 DSGraph::DontCloneCallNodes);
590 ++NumBUInlines;
Chris Lattneraf8650e2005-02-01 21:37:27 +0000591 }
Chris Lattnera9548d92005-01-30 23:51:02 +0000592 }
Chris Lattner20cd1362005-02-01 21:49:43 +0000593 TempFCs.erase(TempFCs.begin());
Chris Lattnera9548d92005-01-30 23:51:02 +0000594 }
Chris Lattnera9c9c022002-11-11 21:35:13 +0000595
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000596 // Recompute the Incomplete markers
Chris Lattnera9c9c022002-11-11 21:35:13 +0000597 Graph.maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000598 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
Vikram S. Adve1da1d322003-07-16 21:42:03 +0000599
600 // Delete dead nodes. Treat globals that are unreachable but that can
601 // reach live nodes as live.
Chris Lattner394471f2003-01-23 22:05:33 +0000602 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattnera9c9c022002-11-11 21:35:13 +0000603
Chris Lattner35679372004-02-21 00:30:28 +0000604 // When this graph is finalized, clone the globals in the graph into the
605 // globals graph to make sure it has everything, from all graphs.
606 DSScalarMap &MainSM = Graph.getScalarMap();
607 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
608
Chris Lattner3b7b81b2004-10-31 21:54:51 +0000609 // Clone everything reachable from globals in the function graph into the
Chris Lattner35679372004-02-21 00:30:28 +0000610 // globals graph.
611 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000612 E = MainSM.global_end(); I != E; ++I)
Chris Lattner35679372004-02-21 00:30:28 +0000613 RC.getClonedNH(MainSM[*I]);
614
Chris Lattnera9c9c022002-11-11 21:35:13 +0000615 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
Chris Lattnera9c9c022002-11-11 21:35:13 +0000616}
Chris Lattner851b5342005-01-24 20:00:14 +0000617
618static const Function *getFnForValue(const Value *V) {
619 if (const Instruction *I = dyn_cast<Instruction>(V))
620 return I->getParent()->getParent();
621 else if (const Argument *A = dyn_cast<Argument>(V))
622 return A->getParent();
623 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
624 return BB->getParent();
625 return 0;
626}
627
628/// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
629/// These correspond to the interfaces defined in the AliasAnalysis class.
630void BUDataStructures::deleteValue(Value *V) {
631 if (const Function *F = getFnForValue(V)) { // Function local value?
632 // If this is a function local value, just delete it from the scalar map!
633 getDSGraph(*F).getScalarMap().eraseIfExists(V);
634 return;
635 }
636
Chris Lattnercff8ac22005-01-31 00:10:45 +0000637 if (Function *F = dyn_cast<Function>(V)) {
Chris Lattner851b5342005-01-24 20:00:14 +0000638 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
639 "cannot handle scc's");
640 delete DSInfo[F];
641 DSInfo.erase(F);
642 return;
643 }
644
645 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
646}
647
648void BUDataStructures::copyValue(Value *From, Value *To) {
649 if (From == To) return;
650 if (const Function *F = getFnForValue(From)) { // Function local value?
651 // If this is a function local value, just delete it from the scalar map!
652 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
653 return;
654 }
655
656 if (Function *FromF = dyn_cast<Function>(From)) {
657 Function *ToF = cast<Function>(To);
658 assert(!DSInfo.count(ToF) && "New Function already exists!");
Chris Lattnerf4f62272005-03-19 22:23:45 +0000659 DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
Chris Lattner851b5342005-01-24 20:00:14 +0000660 DSInfo[ToF] = NG;
661 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
662
663 // Change the Function* is the returnnodes map to the ToF.
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000664 DSNodeHandle Ret = NG->retnodes_begin()->second;
Chris Lattner851b5342005-01-24 20:00:14 +0000665 NG->getReturnNodes().clear();
666 NG->getReturnNodes()[ToF] = Ret;
667 return;
668 }
669
Chris Lattnerc5132e62005-03-24 04:22:04 +0000670 if (const Function *F = getFnForValue(To)) {
671 DSGraph &G = getDSGraph(*F);
672 G.getScalarMap().copyScalarIfExists(From, To);
673 return;
674 }
675
676 std::cerr << *From;
677 std::cerr << *To;
678 assert(0 && "Do not know how to copy this yet!");
679 abort();
Chris Lattner851b5342005-01-24 20:00:14 +0000680}