1 //===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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**
13 // applications like alias analysis.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/DataStructure/DataStructure.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Support/Debug.h"
21 #include "DSCallSiteIterator.h"
25 Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
26 Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
27 Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
29 RegisterAnalysis<BUDataStructures>
30 X("budatastructure", "Bottom-up Data Structure Analysis");
35 // run - Calculate the bottom up data structure graphs for each function in the
38 bool BUDataStructures::runOnModule(Module &M) {
39 LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
40 GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
41 GlobalsGraph->setPrintAuxCalls();
43 std::vector<Function*> Stack;
44 hash_map<Function*, unsigned> ValMap;
47 Function *MainFunc = M.getMainFunction();
49 calculateGraphs(MainFunc, Stack, NextID, ValMap);
51 // Calculate the graphs for any functions that are unreachable from main...
52 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
53 if (!I->isExternal() && !DSInfo.count(I)) {
56 std::cerr << "*** Function unreachable from main: "
57 << I->getName() << "\n";
59 calculateGraphs(I, Stack, NextID, ValMap); // Calculate all graphs.
62 NumCallEdges += ActualCallees.size();
64 // At the end of the bottom-up pass, the globals graph becomes complete.
65 // FIXME: This is not the right way to do this, but it is sorta better than
66 // nothing! In particular, externally visible globals and unresolvable call
67 // nodes at the end of the BU phase should make things that they point to
68 // incomplete in the globals graph.
70 GlobalsGraph->removeTriviallyDeadNodes();
71 GlobalsGraph->maskIncompleteMarkers();
75 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
76 // Has the graph already been created?
77 DSGraph *&Graph = DSInfo[F];
78 if (Graph) return *Graph;
80 // Copy the local version into DSInfo...
81 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
83 Graph->setGlobalsGraph(GlobalsGraph);
84 Graph->setPrintAuxCalls();
86 // Start with a copy of the original call sites...
87 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
91 unsigned BUDataStructures::calculateGraphs(Function *F,
92 std::vector<Function*> &Stack,
94 hash_map<Function*, unsigned> &ValMap) {
95 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
96 unsigned Min = NextID++, MyID = Min;
100 // FIXME! This test should be generalized to be any function that we have
101 // already processed, in the case when there isn't a main or there are
102 // unreachable functions!
103 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
110 DSGraph &Graph = getOrCreateGraph(F);
112 // The edges out of the current node are the call site targets...
113 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
114 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
115 Function *Callee = *I;
117 // Have we visited the destination function yet?
118 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
119 if (It == ValMap.end()) // No, visit it now.
120 M = calculateGraphs(Callee, Stack, NextID, ValMap);
121 else // Yes, get it's number.
123 if (M < Min) Min = M;
126 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
128 return Min; // This is part of a larger SCC!
130 // If this is a new SCC, process it now.
131 if (Stack.back() == F) { // Special case the single "SCC" case here.
132 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
133 << F->getName() << "\n");
135 DSGraph &G = getDSGraph(*F);
136 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
138 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
139 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
142 if (MaxSCC < 1) MaxSCC = 1;
144 // Should we revisit the graph?
145 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
147 return calculateGraphs(F, Stack, NextID, ValMap);
154 // SCCFunctions - Keep track of the functions in the current SCC
156 hash_set<DSGraph*> SCCGraphs;
159 std::vector<Function*>::iterator FirstInSCC = Stack.end();
160 DSGraph *SCCGraph = 0;
165 // Figure out which graph is the largest one, in order to speed things up
166 // a bit in situations where functions in the SCC have widely different
168 DSGraph &NFGraph = getDSGraph(*NF);
169 SCCGraphs.insert(&NFGraph);
170 // FIXME: If we used a better way of cloning graphs (ie, just splice all
171 // of the nodes into the new graph), this would be completely unneeded!
172 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
176 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
177 << SCCGraphs.size() << "\n";
179 // Compute the Max SCC Size...
180 if (MaxSCC < SCCGraphs.size())
181 MaxSCC = SCCGraphs.size();
183 // First thing first, collapse all of the DSGraphs into a single graph for
184 // the entire SCC. We computed the largest graph, so clone all of the other
185 // (smaller) graphs into it. Discard all of the old graphs.
187 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
188 E = SCCGraphs.end(); I != E; ++I) {
190 if (&G != SCCGraph) {
192 DSGraph::NodeMapTy NodeMap;
193 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
194 SCCGraph->getReturnNodes(), NodeMap);
196 // Update the DSInfo map and delete the old graph...
197 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
198 E = G.getReturnNodes().end(); I != E; ++I)
199 DSInfo[I->first] = SCCGraph;
204 // Clean up the graph before we start inlining a bunch again...
205 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
207 // Now that we have one big happy family, resolve all of the call sites in
209 calculateGraph(*SCCGraph);
210 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
211 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
213 std::cerr << "DONE with SCC #: " << MyID << "\n";
215 // We never have to revisit "SCC" processed functions...
217 // Drop the stuff we don't need from the end of the stack
218 Stack.erase(FirstInSCC, Stack.end());
222 return MyID; // == Min
226 // releaseMemory - If the pass pipeline is done with this pass, we can release
227 // our memory... here...
229 void BUDataStructures::releaseMemory() {
230 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
231 E = DSInfo.end(); I != E; ++I) {
232 I->second->getReturnNodes().erase(I->first);
233 if (I->second->getReturnNodes().empty())
237 // Empty map so next time memory is released, data structures are not
244 static bool isVAHackFn(const Function *F) {
245 return F->getName() == "printf" || F->getName() == "sscanf" ||
246 F->getName() == "fprintf" || F->getName() == "open" ||
247 F->getName() == "sprintf" || F->getName() == "fputs" ||
248 F->getName() == "fscanf";
251 // isUnresolvableFunction - Return true if this is an unresolvable
252 // external function. A direct or indirect call to this cannot be resolved.
254 static bool isResolvableFunc(const Function* callee) {
255 return !callee->isExternal() || isVAHackFn(callee);
258 void BUDataStructures::calculateGraph(DSGraph &Graph) {
259 // Move our call site list into TempFCs so that inline call sites go into the
260 // new call site list and doesn't invalidate our iterators!
261 std::list<DSCallSite> TempFCs;
262 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
263 TempFCs.swap(AuxCallsList);
265 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
267 // Print out multi-call sites.
268 bool Printed = false;
269 for (std::list<DSCallSite>::iterator I = TempFCs.begin(), E = TempFCs.end();
271 if (!I->isDirectCall()) {
272 DSNode *Node = I->getCalleeNode();
273 if (Node->getGlobals().size() > 1) {
275 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
276 std::cerr << " calls " << Node->getGlobals().size()
277 << " fns from site: " << I->getCallSite().getInstruction()
278 << " " << *I->getCallSite().getInstruction();
279 unsigned NumToPrint = Node->getGlobals().size();
280 if (NumToPrint > 5) NumToPrint = 5;
281 std::cerr << " Fns =";
282 for (unsigned i = 0; i != NumToPrint; ++i)
283 std::cerr << " " << Node->getGlobals()[i]->getName();
289 while (!TempFCs.empty()) {
290 DSCallSite &CS = *TempFCs.begin();
292 std::set<Function*> CalledFuncs;
294 if (CS.isDirectCall()) {
295 Function *F = CS.getCalleeFunc();
296 if (isResolvableFunc(F))
297 if (F->isExternal()) { // Call to fprintf, etc.
298 TempFCs.erase(TempFCs.begin());
301 CalledFuncs.insert(F);
304 DSNode *Node = CS.getCalleeNode();
306 if (!Node->isIncomplete())
307 for (unsigned i = 0, e = Node->getGlobals().size(); i != e; ++i)
308 if (Function *CF = dyn_cast<Function>(Node->getGlobals()[i]))
309 if (isResolvableFunc(CF) && !CF->isExternal())
310 CalledFuncs.insert(CF);
313 if (CalledFuncs.empty()) {
314 // Remember that we could not resolve this yet!
315 AuxCallsList.splice(AuxCallsList.end(), TempFCs, TempFCs.begin());
317 } else if (CalledFuncs.size() == 1) {
318 Function *Callee = *CalledFuncs.begin();
320 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
323 // Get the data structure graph for the called function.
325 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
327 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
328 << "[" << GI.getGraphSize() << "+"
329 << GI.getAuxFunctionCalls().size() << "] into '"
330 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
331 << Graph.getAuxFunctionCalls().size() << "]\n");
332 Graph.mergeInGraph(CS, *Callee, GI,
333 DSGraph::KeepModRefBits |
334 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
338 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
343 std::cerr << "In Fns: " << Graph.getFunctionNames() << "\n";
344 std::cerr << " calls " << CalledFuncs.size()
345 << " fns from site: " << CS.getCallSite().getInstruction()
346 << " " << *CS.getCallSite().getInstruction();
347 unsigned NumToPrint = CalledFuncs.size();
348 if (NumToPrint > 8) NumToPrint = 8;
349 std::cerr << " Fns =";
350 for (std::set<Function*>::iterator I = CalledFuncs.begin(),
351 E = CalledFuncs.end(); I != E && NumToPrint; ++I, --NumToPrint)
352 std::cerr << " " << (*I)->getName();
355 // Inline all of the called functions.
356 for (std::set<Function*>::iterator I = CalledFuncs.begin(),
357 E = CalledFuncs.end(); I != E; ++I) {
358 Function *Callee = *I;
359 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
362 // Get the data structure graph for the called function.
364 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
366 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
367 << "[" << GI.getGraphSize() << "+"
368 << GI.getAuxFunctionCalls().size() << "] into '"
369 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() <<"+"
370 << Graph.getAuxFunctionCalls().size() << "]\n");
371 Graph.mergeInGraph(CS, *Callee, GI,
372 DSGraph::KeepModRefBits |
373 DSGraph::StripAllocaBit|DSGraph::DontCloneCallNodes);
377 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
382 TempFCs.erase(TempFCs.begin());
385 // Recompute the Incomplete markers
386 assert(Graph.getInlinedGlobals().empty());
387 Graph.maskIncompleteMarkers();
388 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
390 // Delete dead nodes. Treat globals that are unreachable but that can
391 // reach live nodes as live.
392 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
394 // When this graph is finalized, clone the globals in the graph into the
395 // globals graph to make sure it has everything, from all graphs.
396 DSScalarMap &MainSM = Graph.getScalarMap();
397 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
399 // Clone everything reachable from globals in the function graph into the
401 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
402 E = MainSM.global_end(); I != E; ++I)
403 RC.getClonedNH(MainSM[*I]);
405 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
408 static const Function *getFnForValue(const Value *V) {
409 if (const Instruction *I = dyn_cast<Instruction>(V))
410 return I->getParent()->getParent();
411 else if (const Argument *A = dyn_cast<Argument>(V))
412 return A->getParent();
413 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
414 return BB->getParent();
418 /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
419 /// These correspond to the interfaces defined in the AliasAnalysis class.
420 void BUDataStructures::deleteValue(Value *V) {
421 if (const Function *F = getFnForValue(V)) { // Function local value?
422 // If this is a function local value, just delete it from the scalar map!
423 getDSGraph(*F).getScalarMap().eraseIfExists(V);
427 if (Function *F = dyn_cast<Function>(V)) {
428 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
429 "cannot handle scc's");
435 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
438 void BUDataStructures::copyValue(Value *From, Value *To) {
439 if (From == To) return;
440 if (const Function *F = getFnForValue(From)) { // Function local value?
441 // If this is a function local value, just delete it from the scalar map!
442 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
446 if (Function *FromF = dyn_cast<Function>(From)) {
447 Function *ToF = cast<Function>(To);
448 assert(!DSInfo.count(ToF) && "New Function already exists!");
449 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
451 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
453 // Change the Function* is the returnnodes map to the ToF.
454 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
455 NG->getReturnNodes().clear();
456 NG->getReturnNodes()[ToF] = Ret;
460 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");