2002-07-30 22:06:40 +00:00
|
|
|
//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-30 22:06:40 +00:00
|
|
|
//
|
|
|
|
// This file implements the TDDataStructures class, which represents the
|
|
|
|
// Top-down Interprocedural closure of the data structure graph over the
|
|
|
|
// program. This is useful (but not strictly necessary?) for applications
|
|
|
|
// like pointer analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-07 06:32:21 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
2002-07-30 22:06:40 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-07-07 06:32:21 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2003-11-12 23:11:14 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-02-03 22:51:28 +00:00
|
|
|
namespace {
|
|
|
|
RegisterAnalysis<TDDataStructures> // Register the pass
|
2003-06-28 22:14:55 +00:00
|
|
|
Y("tddatastructure", "Top-down Data Structure Analysis");
|
2003-07-02 04:39:44 +00:00
|
|
|
|
|
|
|
Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
|
|
|
|
}
|
|
|
|
|
2003-09-20 22:24:04 +00:00
|
|
|
void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
|
|
|
|
hash_set<DSNode*> &Visited) {
|
2003-09-20 23:58:33 +00:00
|
|
|
if (!N || Visited.count(N)) return;
|
2003-09-20 22:24:04 +00:00
|
|
|
Visited.insert(N);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) {
|
|
|
|
DSNodeHandle &NH = N->getLink(i*N->getPointerSize());
|
|
|
|
if (DSNode *NN = NH.getNode()) {
|
|
|
|
const std::vector<GlobalValue*> &Globals = NN->getGlobals();
|
|
|
|
for (unsigned G = 0, e = Globals.size(); G != e; ++G)
|
|
|
|
if (Function *F = dyn_cast<Function>(Globals[G]))
|
|
|
|
ArgsRemainIncomplete.insert(F);
|
|
|
|
|
|
|
|
markReachableFunctionsExternallyAccessible(NN, Visited);
|
|
|
|
}
|
|
|
|
}
|
2003-02-03 22:51:28 +00:00
|
|
|
}
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2003-09-20 22:24:04 +00:00
|
|
|
|
2002-07-30 22:06:40 +00:00
|
|
|
// run - Calculate the top down data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool TDDataStructures::runOnModule(Module &M) {
|
2002-10-22 16:01:03 +00:00
|
|
|
BUDataStructures &BU = getAnalysis<BUDataStructures>();
|
2003-06-28 22:14:55 +00:00
|
|
|
GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
|
2003-09-20 23:58:33 +00:00
|
|
|
GlobalsGraph->setPrintAuxCalls();
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
// Figure out which functions must not mark their arguments complete because
|
2003-09-20 22:24:04 +00:00
|
|
|
// they are accessible outside this compilation unit. Currently, these
|
|
|
|
// arguments are functions which are reachable by global variables in the
|
|
|
|
// globals graph.
|
2004-01-28 03:12:48 +00:00
|
|
|
const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
|
2003-09-20 22:24:04 +00:00
|
|
|
hash_set<DSNode*> Visited;
|
2004-04-29 04:05:30 +00:00
|
|
|
for (DSScalarMap::global_iterator I=GGSM.global_begin(), E=GGSM.global_end();
|
2003-09-20 22:24:04 +00:00
|
|
|
I != E; ++I)
|
2004-04-29 04:05:30 +00:00
|
|
|
markReachableFunctionsExternallyAccessible(GGSM.find(*I)->second.getNode(),
|
|
|
|
Visited);
|
2003-09-20 23:58:33 +00:00
|
|
|
|
|
|
|
// Loop over unresolved call nodes. Any functions passed into (but not
|
2004-01-27 21:53:14 +00:00
|
|
|
// returned!) from unresolvable call nodes may be invoked outside of the
|
2003-09-20 23:58:33 +00:00
|
|
|
// current module.
|
|
|
|
const std::vector<DSCallSite> &Calls = GlobalsGraph->getAuxFunctionCalls();
|
|
|
|
for (unsigned i = 0, e = Calls.size(); i != e; ++i) {
|
|
|
|
const DSCallSite &CS = Calls[i];
|
|
|
|
for (unsigned arg = 0, e = CS.getNumPtrArgs(); arg != e; ++arg)
|
|
|
|
markReachableFunctionsExternallyAccessible(CS.getPtrArg(arg).getNode(),
|
|
|
|
Visited);
|
|
|
|
}
|
2003-09-20 22:24:04 +00:00
|
|
|
Visited.clear();
|
|
|
|
|
|
|
|
// Functions without internal linkage also have unknown incoming arguments!
|
2003-07-02 04:39:44 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-09-20 22:24:04 +00:00
|
|
|
if (!I->isExternal() && !I->hasInternalLinkage())
|
2003-07-02 04:39:44 +00:00
|
|
|
ArgsRemainIncomplete.insert(I);
|
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
// We want to traverse the call graph in reverse post-order. To do this, we
|
|
|
|
// calculate a post-order traversal, then reverse it.
|
|
|
|
hash_set<DSGraph*> VisitedGraph;
|
|
|
|
std::vector<DSGraph*> PostOrder;
|
|
|
|
const BUDataStructures::ActualCalleesTy &ActualCallees =
|
|
|
|
getAnalysis<BUDataStructures>().getActualCallees();
|
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
// Calculate top-down from main...
|
|
|
|
if (Function *F = M.getMainFunction())
|
2003-07-02 23:44:15 +00:00
|
|
|
ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2003-07-16 21:40:28 +00:00
|
|
|
// Next calculate the graphs for each unreachable function...
|
2003-07-02 04:39:44 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-07-02 23:42:48 +00:00
|
|
|
ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
VisitedGraph.clear(); // Release memory!
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
// Visit each of the graphs in reverse post-order now!
|
|
|
|
while (!PostOrder.empty()) {
|
|
|
|
inlineGraphIntoCallees(*PostOrder.back());
|
|
|
|
PostOrder.pop_back();
|
2003-06-30 04:53:08 +00:00
|
|
|
}
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
ArgsRemainIncomplete.clear();
|
2004-02-08 01:51:48 +00:00
|
|
|
GlobalsGraph->removeTriviallyDeadNodes();
|
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
return false;
|
2002-11-09 21:12:07 +00:00
|
|
|
}
|
|
|
|
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
DSGraph &TDDataStructures::getOrCreateDSGraph(Function &F) {
|
|
|
|
DSGraph *&G = DSInfo[&F];
|
|
|
|
if (G == 0) { // Not created yet? Clone BU graph...
|
|
|
|
G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F));
|
|
|
|
G->getAuxFunctionCalls().clear();
|
2003-02-04 00:59:32 +00:00
|
|
|
G->setPrintAuxCalls();
|
2002-11-09 21:12:07 +00:00
|
|
|
G->setGlobalsGraph(GlobalsGraph);
|
2002-11-08 21:28:37 +00:00
|
|
|
}
|
|
|
|
return *G;
|
|
|
|
}
|
2002-10-20 21:41:02 +00:00
|
|
|
|
2003-06-29 22:37:07 +00:00
|
|
|
|
2003-07-01 16:28:11 +00:00
|
|
|
void TDDataStructures::ComputePostOrder(Function &F,hash_set<DSGraph*> &Visited,
|
|
|
|
std::vector<DSGraph*> &PostOrder,
|
|
|
|
const BUDataStructures::ActualCalleesTy &ActualCallees) {
|
|
|
|
if (F.isExternal()) return;
|
|
|
|
DSGraph &G = getOrCreateDSGraph(F);
|
|
|
|
if (Visited.count(&G)) return;
|
|
|
|
Visited.insert(&G);
|
|
|
|
|
|
|
|
// Recursively traverse all of the callee graphs.
|
|
|
|
const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls();
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
|
2003-09-20 16:34:13 +00:00
|
|
|
Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
|
2003-07-01 16:28:11 +00:00
|
|
|
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
|
|
|
|
BUDataStructures::ActualCalleesTy::const_iterator>
|
2003-09-20 16:34:13 +00:00
|
|
|
IP = ActualCallees.equal_range(CallI);
|
2003-07-01 16:28:11 +00:00
|
|
|
|
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
|
|
|
|
I != IP.second; ++I)
|
|
|
|
ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
|
|
|
|
}
|
2003-06-29 22:37:07 +00:00
|
|
|
|
2003-07-01 16:28:11 +00:00
|
|
|
PostOrder.push_back(&G);
|
|
|
|
}
|
2002-10-22 16:01:03 +00:00
|
|
|
|
|
|
|
|
2003-07-01 16:28:11 +00:00
|
|
|
|
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
|
|
|
|
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
|
|
// our memory... here...
|
|
|
|
//
|
|
|
|
// FIXME: This should be releaseMemory and will work fine, except that LoadVN
|
|
|
|
// has no way to extend the lifetime of the pass, which screws up ds-aa.
|
|
|
|
//
|
|
|
|
void TDDataStructures::releaseMyMemory() {
|
|
|
|
for (hash_map<Function*, DSGraph*>::iterator I = DSInfo.begin(),
|
|
|
|
E = DSInfo.end(); I != E; ++I) {
|
|
|
|
I->second->getReturnNodes().erase(I->first);
|
|
|
|
if (I->second->getReturnNodes().empty())
|
|
|
|
delete I->second;
|
2003-07-01 16:28:11 +00:00
|
|
|
}
|
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
// Empty map so next time memory is released, data structures are not
|
|
|
|
// re-deleted.
|
|
|
|
DSInfo.clear();
|
|
|
|
delete GlobalsGraph;
|
|
|
|
GlobalsGraph = 0;
|
|
|
|
}
|
2003-07-01 16:28:11 +00:00
|
|
|
|
|
|
|
void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
|
2003-02-10 18:16:36 +00:00
|
|
|
// Recompute the Incomplete markers and eliminate unreachable nodes.
|
|
|
|
Graph.maskIncompleteMarkers();
|
2003-07-02 04:39:44 +00:00
|
|
|
|
|
|
|
// If any of the functions has incomplete incoming arguments, don't mark any
|
|
|
|
// of them as complete.
|
|
|
|
bool HasIncompleteArgs = false;
|
|
|
|
const DSGraph::ReturnNodesTy &GraphReturnNodes = Graph.getReturnNodes();
|
|
|
|
for (DSGraph::ReturnNodesTy::const_iterator I = GraphReturnNodes.begin(),
|
|
|
|
E = GraphReturnNodes.end(); I != E; ++I)
|
|
|
|
if (ArgsRemainIncomplete.count(I->first)) {
|
|
|
|
HasIncompleteArgs = true;
|
|
|
|
break;
|
|
|
|
}
|
2003-07-16 21:40:28 +00:00
|
|
|
|
|
|
|
// Now fold in the necessary globals from the GlobalsGraph. A global G
|
|
|
|
// must be folded in if it exists in the current graph (i.e., is not dead)
|
|
|
|
// and it was not inlined from any of my callers. If it was inlined from
|
|
|
|
// a caller, it would have been fully consistent with the GlobalsGraph
|
|
|
|
// in the caller so folding in is not necessary. Otherwise, this node came
|
|
|
|
// solely from this function's BU graph and so has to be made consistent.
|
|
|
|
//
|
|
|
|
Graph.updateFromGlobalGraph();
|
|
|
|
|
|
|
|
// Recompute the Incomplete markers. Depends on whether args are complete
|
2003-07-02 04:39:44 +00:00
|
|
|
unsigned Flags
|
|
|
|
= HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
|
2003-02-10 18:16:36 +00:00
|
|
|
Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
|
2003-07-16 21:40:28 +00:00
|
|
|
|
|
|
|
// Delete dead nodes. Treat globals that are unreachable as dead also.
|
2003-02-10 18:16:36 +00:00
|
|
|
Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
|
|
|
|
2003-07-16 21:40:28 +00:00
|
|
|
// We are done with computing the current TD Graph! Now move on to
|
|
|
|
// inlining the current graph into the graphs for its callees, if any.
|
|
|
|
//
|
2003-07-02 04:39:44 +00:00
|
|
|
const std::vector<DSCallSite> &FunctionCalls = Graph.getFunctionCalls();
|
|
|
|
if (FunctionCalls.empty()) {
|
2003-07-01 16:28:11 +00:00
|
|
|
DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
|
|
|
|
<< "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have information about all of the callees, propagate the
|
2003-07-16 21:40:28 +00:00
|
|
|
// current graph into the callees. Clone only the reachable subgraph at
|
|
|
|
// each call-site, not the entire graph (even though the entire graph
|
|
|
|
// would be cloned only once, this should still be better on average).
|
2003-07-01 16:28:11 +00:00
|
|
|
//
|
|
|
|
DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
|
2003-07-02 04:39:44 +00:00
|
|
|
<< FunctionCalls.size() << " call nodes.\n");
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
const BUDataStructures::ActualCalleesTy &ActualCallees =
|
|
|
|
getAnalysis<BUDataStructures>().getActualCallees();
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2004-01-27 21:53:14 +00:00
|
|
|
// Loop over all the call sites and all the callees at each call site. Build
|
|
|
|
// a mapping from called DSGraph's to the call sites in this function that
|
|
|
|
// invoke them. This is useful because we can be more efficient if there are
|
|
|
|
// multiple call sites to the callees in the graph from this caller.
|
|
|
|
std::multimap<DSGraph*, std::pair<Function*, const DSCallSite*> > CallSites;
|
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
|
2003-09-20 16:34:13 +00:00
|
|
|
Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
|
2003-07-16 21:40:28 +00:00
|
|
|
// For each function in the invoked function list at this call site...
|
2003-07-02 04:39:44 +00:00
|
|
|
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
|
|
|
|
BUDataStructures::ActualCalleesTy::const_iterator>
|
2003-09-20 16:34:13 +00:00
|
|
|
IP = ActualCallees.equal_range(CallI);
|
2003-07-16 21:40:28 +00:00
|
|
|
// Loop over each actual callee at this call site
|
2003-07-02 04:39:44 +00:00
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
|
|
|
|
I != IP.second; ++I) {
|
2003-07-16 21:40:28 +00:00
|
|
|
DSGraph& CalleeGraph = getDSGraph(*I->second);
|
|
|
|
assert(&CalleeGraph != &Graph && "TD need not inline graph into self!");
|
|
|
|
|
2004-01-27 21:53:14 +00:00
|
|
|
CallSites.insert(std::make_pair(&CalleeGraph,
|
|
|
|
std::make_pair(I->second, &FunctionCalls[i])));
|
|
|
|
}
|
|
|
|
}
|
2003-07-16 21:40:28 +00:00
|
|
|
|
2004-01-27 21:53:14 +00:00
|
|
|
// Now that we built the mapping, actually perform the inlining a callee graph
|
|
|
|
// at a time.
|
|
|
|
std::multimap<DSGraph*,std::pair<Function*,const DSCallSite*> >::iterator CSI;
|
|
|
|
for (CSI = CallSites.begin(); CSI != CallSites.end(); ) {
|
|
|
|
DSGraph &CalleeGraph = *CSI->first;
|
|
|
|
// Iterate through all of the call sites of this graph, cloning and merging
|
|
|
|
// any nodes required by the call.
|
|
|
|
ReachabilityCloner RC(CalleeGraph, Graph, DSGraph::StripModRefBits);
|
|
|
|
|
|
|
|
// Clone over any global nodes that appear in both graphs.
|
2004-01-28 03:12:48 +00:00
|
|
|
for (DSScalarMap::global_iterator
|
|
|
|
SI = CalleeGraph.getScalarMap().global_begin(),
|
|
|
|
SE = CalleeGraph.getScalarMap().global_end(); SI != SE; ++SI) {
|
|
|
|
DSScalarMap::const_iterator GI = Graph.getScalarMap().find(*SI);
|
|
|
|
if (GI != Graph.getScalarMap().end())
|
|
|
|
RC.merge(CalleeGraph.getNodeForValue(*SI), GI->second);
|
|
|
|
}
|
2004-01-27 21:53:14 +00:00
|
|
|
|
|
|
|
// Loop over all of the distinct call sites in the caller of the callee.
|
|
|
|
for (; CSI != CallSites.end() && CSI->first == &CalleeGraph; ++CSI) {
|
|
|
|
Function &CF = *CSI->second.first;
|
|
|
|
const DSCallSite &CS = *CSI->second.second;
|
2003-07-16 21:40:28 +00:00
|
|
|
DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
|
|
|
|
<< CalleeGraph.getFunctionNames()
|
2004-01-27 21:53:14 +00:00
|
|
|
<< "': " << CF.getFunctionType()->getNumParams()
|
|
|
|
<< " args\n at call site (DSCallSite*) 0x" << &CS << "\n");
|
2003-07-16 21:40:28 +00:00
|
|
|
|
2004-01-27 21:53:14 +00:00
|
|
|
// Get the formal argument and return nodes for the called function and
|
|
|
|
// merge them with the cloned subgraph.
|
|
|
|
RC.mergeCallSite(CalleeGraph.getCallSiteForArguments(CF), CS);
|
2003-07-16 21:40:28 +00:00
|
|
|
++NumTDInlines;
|
2003-07-02 04:39:44 +00:00
|
|
|
}
|
2003-02-09 18:42:43 +00:00
|
|
|
}
|
2003-07-01 16:28:11 +00:00
|
|
|
|
|
|
|
DEBUG(std::cerr << " [TD] Done inlining into callees for: "
|
|
|
|
<< Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
|
|
|
|
<< Graph.getFunctionCalls().size() << "]\n");
|
2002-07-30 22:06:40 +00:00
|
|
|
}
|