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 Function *MainFunc = M.getMainFunction();
45 calculateReachableGraphs(MainFunc);
47 // Calculate the graphs for any functions that are unreachable from main...
48 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
49 if (!I->isExternal() && !DSInfo.count(I)) {
52 std::cerr << "*** Function unreachable from main: "
53 << I->getName() << "\n";
55 calculateReachableGraphs(I); // Calculate all graphs...
58 NumCallEdges += ActualCallees.size();
60 // At the end of the bottom-up pass, the globals graph becomes complete.
61 // FIXME: This is not the right way to do this, but it is sorta better than
62 // nothing! In particular, externally visible globals and unresolvable call
63 // nodes at the end of the BU phase should make things that they point to
64 // incomplete in the globals graph.
66 GlobalsGraph->removeTriviallyDeadNodes();
67 GlobalsGraph->maskIncompleteMarkers();
71 void BUDataStructures::calculateReachableGraphs(Function *F) {
72 std::vector<Function*> Stack;
73 hash_map<Function*, unsigned> ValMap;
75 calculateGraphs(F, Stack, NextID, ValMap);
78 DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
79 // Has the graph already been created?
80 DSGraph *&Graph = DSInfo[F];
81 if (Graph) return *Graph;
83 // Copy the local version into DSInfo...
84 Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
86 Graph->setGlobalsGraph(GlobalsGraph);
87 Graph->setPrintAuxCalls();
89 // Start with a copy of the original call sites...
90 Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
94 unsigned BUDataStructures::calculateGraphs(Function *F,
95 std::vector<Function*> &Stack,
97 hash_map<Function*, unsigned> &ValMap) {
98 assert(!ValMap.count(F) && "Shouldn't revisit functions!");
99 unsigned Min = NextID++, MyID = Min;
103 // FIXME! This test should be generalized to be any function that we have
104 // already processed, in the case when there isn't a main or there are
105 // unreachable functions!
106 if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
113 DSGraph &Graph = getOrCreateGraph(F);
115 // The edges out of the current node are the call site targets...
116 for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
117 E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
118 Function *Callee = *I;
120 // Have we visited the destination function yet?
121 hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
122 if (It == ValMap.end()) // No, visit it now.
123 M = calculateGraphs(Callee, Stack, NextID, ValMap);
124 else // Yes, get it's number.
126 if (M < Min) Min = M;
129 assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
131 return Min; // This is part of a larger SCC!
133 // If this is a new SCC, process it now.
134 if (Stack.back() == F) { // Special case the single "SCC" case here.
135 DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
136 << F->getName() << "\n");
138 DSGraph &G = getDSGraph(*F);
139 DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
141 DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
142 << G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
145 if (MaxSCC < 1) MaxSCC = 1;
147 // Should we revisit the graph?
148 if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
150 return calculateGraphs(F, Stack, NextID, ValMap);
157 // SCCFunctions - Keep track of the functions in the current SCC
159 hash_set<DSGraph*> SCCGraphs;
162 std::vector<Function*>::iterator FirstInSCC = Stack.end();
163 DSGraph *SCCGraph = 0;
168 // Figure out which graph is the largest one, in order to speed things up
169 // a bit in situations where functions in the SCC have widely different
171 DSGraph &NFGraph = getDSGraph(*NF);
172 SCCGraphs.insert(&NFGraph);
173 // FIXME: If we used a better way of cloning graphs (ie, just splice all
174 // of the nodes into the new graph), this would be completely unneeded!
175 if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
179 std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
180 << SCCGraphs.size() << "\n";
182 // Compute the Max SCC Size...
183 if (MaxSCC < SCCGraphs.size())
184 MaxSCC = SCCGraphs.size();
186 // First thing first, collapse all of the DSGraphs into a single graph for
187 // the entire SCC. We computed the largest graph, so clone all of the other
188 // (smaller) graphs into it. Discard all of the old graphs.
190 for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
191 E = SCCGraphs.end(); I != E; ++I) {
193 if (&G != SCCGraph) {
195 DSGraph::NodeMapTy NodeMap;
196 SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
197 SCCGraph->getReturnNodes(), NodeMap);
199 // Update the DSInfo map and delete the old graph...
200 for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
201 E = G.getReturnNodes().end(); I != E; ++I)
202 DSInfo[I->first] = SCCGraph;
207 // Clean up the graph before we start inlining a bunch again...
208 SCCGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
210 // Now that we have one big happy family, resolve all of the call sites in
212 calculateGraph(*SCCGraph);
213 DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
214 << "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
216 std::cerr << "DONE with SCC #: " << MyID << "\n";
218 // We never have to revisit "SCC" processed functions...
220 // Drop the stuff we don't need from the end of the stack
221 Stack.erase(FirstInSCC, Stack.end());
225 return MyID; // == Min
229 // releaseMemory - If the pass pipeline is done with this pass, we can release
230 // our memory... here...
232 void BUDataStructures::releaseMemory() {
233 for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
234 E = DSInfo.end(); I != E; ++I) {
235 I->second->getReturnNodes().erase(I->first);
236 if (I->second->getReturnNodes().empty())
240 // Empty map so next time memory is released, data structures are not
247 void BUDataStructures::calculateGraph(DSGraph &Graph) {
248 // Move our call site list into TempFCs so that inline call sites go into the
249 // new call site list and doesn't invalidate our iterators!
250 std::list<DSCallSite> TempFCs;
251 std::list<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
252 TempFCs.swap(AuxCallsList);
254 DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
256 // Loop over all of the resolvable call sites
257 DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs);
258 DSCallSiteIterator E = DSCallSiteIterator::end(TempFCs);
260 // If DSCallSiteIterator skipped over any call sites, they are unresolvable:
261 // move them back to the AuxCallsList.
262 std::list<DSCallSite>::iterator LastCallSiteIdx = TempFCs.begin();
263 while (LastCallSiteIdx != I.getCallSiteIdx())
264 AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++);
267 // Resolve the current call...
268 Function *Callee = *I;
269 DSCallSite CS = I.getCallSite();
271 if (Callee->isExternal()) {
272 // Ignore this case, simple varargs functions we cannot stub out!
273 } else if (ReturnNodes.count(Callee)) {
274 // Self recursion... simply link up the formal arguments with the
275 // actual arguments...
276 DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
278 // Handle self recursion by resolving the arguments and return value
279 Graph.mergeInGraph(CS, *Callee, Graph, 0);
282 ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
285 // Get the data structure graph for the called function.
287 DSGraph &GI = getDSGraph(*Callee); // Graph to inline
289 DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
290 << "[" << GI.getGraphSize() << "+"
291 << GI.getAuxFunctionCalls().size() << "] into '"
292 << Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
293 << Graph.getAuxFunctionCalls().size() << "]\n");
294 Graph.mergeInGraph(CS, *Callee, GI,
295 DSGraph::KeepModRefBits |
296 DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
300 Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
305 LastCallSiteIdx = I.getCallSiteIdx();
306 ++I; // Move to the next call site.
308 if (I.getCallSiteIdx() != LastCallSiteIdx) {
309 ++LastCallSiteIdx; // Skip over the site we already processed.
311 // If there are call sites that get skipped over, move them to the aux
312 // calls list: they are not resolvable.
314 while (LastCallSiteIdx != I.getCallSiteIdx())
315 AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++);
317 while (LastCallSiteIdx != TempFCs.end())
318 AuxCallsList.splice(AuxCallsList.end(), TempFCs, LastCallSiteIdx++);
324 // Recompute the Incomplete markers
325 assert(Graph.getInlinedGlobals().empty());
326 Graph.maskIncompleteMarkers();
327 Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
329 // Delete dead nodes. Treat globals that are unreachable but that can
330 // reach live nodes as live.
331 Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
333 // When this graph is finalized, clone the globals in the graph into the
334 // globals graph to make sure it has everything, from all graphs.
335 DSScalarMap &MainSM = Graph.getScalarMap();
336 ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
338 // Clone everything reachable from globals in the function graph into the
340 for (DSScalarMap::global_iterator I = MainSM.global_begin(),
341 E = MainSM.global_end(); I != E; ++I)
342 RC.getClonedNH(MainSM[*I]);
344 //Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
347 static const Function *getFnForValue(const Value *V) {
348 if (const Instruction *I = dyn_cast<Instruction>(V))
349 return I->getParent()->getParent();
350 else if (const Argument *A = dyn_cast<Argument>(V))
351 return A->getParent();
352 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
353 return BB->getParent();
357 /// deleteValue/copyValue - Interfaces to update the DSGraphs in the program.
358 /// These correspond to the interfaces defined in the AliasAnalysis class.
359 void BUDataStructures::deleteValue(Value *V) {
360 if (const Function *F = getFnForValue(V)) { // Function local value?
361 // If this is a function local value, just delete it from the scalar map!
362 getDSGraph(*F).getScalarMap().eraseIfExists(V);
366 if (Function *F = dyn_cast<Function>(F)) {
367 assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
368 "cannot handle scc's");
374 assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
377 void BUDataStructures::copyValue(Value *From, Value *To) {
378 if (From == To) return;
379 if (const Function *F = getFnForValue(From)) { // Function local value?
380 // If this is a function local value, just delete it from the scalar map!
381 getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
385 if (Function *FromF = dyn_cast<Function>(From)) {
386 Function *ToF = cast<Function>(To);
387 assert(!DSInfo.count(ToF) && "New Function already exists!");
388 DSGraph *NG = new DSGraph(getDSGraph(*FromF));
390 assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
392 // Change the Function* is the returnnodes map to the ToF.
393 DSNodeHandle Ret = NG->getReturnNodes().begin()->second;
394 NG->getReturnNodes().clear();
395 NG->getReturnNodes()[ToF] = Ret;
399 assert(!isa<GlobalVariable>(From) && "Do not know how to copy GV's yet!");