2002-07-30 22:06:40 +00:00
|
|
|
//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-10-01 22:33:50 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-07-01 16:28:11 +00:00
|
|
|
#include "DSCallSiteIterator.h"
|
2002-07-30 22:06:40 +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");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// FunctionHasCompleteArguments - This function returns true if it is safe not
|
|
|
|
/// to mark arguments to the function complete.
|
|
|
|
///
|
|
|
|
/// FIXME: Need to check if all callers have been found, or rather if a
|
|
|
|
/// funcpointer escapes!
|
|
|
|
///
|
|
|
|
static bool FunctionHasCompleteArguments(Function &F) {
|
|
|
|
return F.hasInternalLinkage();
|
2003-02-03 22:51:28 +00:00
|
|
|
}
|
2002-07-30 22:06:40 +00:00
|
|
|
|
|
|
|
// run - Calculate the top down data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
|
|
|
bool TDDataStructures::run(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());
|
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
|
|
|
|
// they are accessible outside this compilation unit.
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!FunctionHasCompleteArguments(*I))
|
|
|
|
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:42:48 +00:00
|
|
|
ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
// Next calculate the graphs for each function 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();
|
|
|
|
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) {
|
|
|
|
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
|
|
|
|
BUDataStructures::ActualCalleesTy::const_iterator>
|
2003-07-02 04:39:44 +00:00
|
|
|
IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
|
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.
|
2003-07-02 19:49:11 +00:00
|
|
|
Graph.removeTriviallyDeadNodes();
|
2003-02-10 18:16:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Flags
|
|
|
|
= HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
|
2003-02-10 18:16:36 +00:00
|
|
|
Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
|
|
|
|
Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
|
|
|
|
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
|
|
|
|
// current graph into the callees.
|
|
|
|
//
|
|
|
|
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
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
// Only inline this function into each real callee once. After that, just
|
|
|
|
// merge information into arguments...
|
|
|
|
hash_map<DSGraph*, DSGraph::NodeMapTy> InlinedSites;
|
|
|
|
|
|
|
|
// Loop over all the callees... cloning this graph into each one exactly once,
|
|
|
|
// keeping track of the node mapping information...
|
|
|
|
for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
|
|
|
|
// Inline this graph into each function in the invoked function list.
|
|
|
|
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
|
|
|
|
BUDataStructures::ActualCalleesTy::const_iterator>
|
|
|
|
IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
|
|
|
|
|
|
|
|
int NumArgs = 0;
|
|
|
|
if (IP.first != IP.second) {
|
|
|
|
NumArgs = IP.first->second->getFunctionType()->getNumParams();
|
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
|
|
|
|
I != IP.second; ++I)
|
|
|
|
if (NumArgs != (int)I->second->getFunctionType()->getNumParams()) {
|
|
|
|
NumArgs = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
if (NumArgs == -1) {
|
|
|
|
std::cerr << "ERROR: NONSAME NUMBER OF ARGUMENTS TO CALLEES\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
|
|
|
|
I != IP.second; ++I) {
|
|
|
|
DSGraph &CG = getDSGraph(*I->second);
|
|
|
|
assert(&CG != &Graph && "TD need not inline graph into self!");
|
|
|
|
|
|
|
|
if (!InlinedSites.count(&CG)) { // If we haven't already inlined into CG
|
|
|
|
DEBUG(std::cerr << " [TD] Inlining graph into callee graph '"
|
|
|
|
<< CG.getFunctionNames() << "': " << I->second->getFunctionType()->getNumParams() << " args\n");
|
|
|
|
DSGraph::ScalarMapTy OldScalarMap;
|
|
|
|
DSGraph::ReturnNodesTy ReturnNodes;
|
|
|
|
CG.cloneInto(Graph, OldScalarMap, ReturnNodes, InlinedSites[&CG],
|
|
|
|
DSGraph::StripModRefBits | DSGraph::KeepAllocaBit |
|
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
|
|
|
++NumTDInlines;
|
2003-02-09 18:42:43 +00:00
|
|
|
}
|
2003-07-01 16:28:11 +00:00
|
|
|
}
|
2003-07-02 04:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all the callees...
|
|
|
|
for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
|
|
|
|
// Inline this graph into each function in the invoked function list.
|
|
|
|
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
|
|
|
|
BUDataStructures::ActualCalleesTy::const_iterator>
|
|
|
|
IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
|
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
|
|
|
|
I != IP.second; ++I) {
|
|
|
|
DSGraph &CG = getDSGraph(*I->second);
|
|
|
|
DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
|
|
|
|
<< CG.getFunctionNames() << "'\n");
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
// Transform our call site information into the cloned version for CG
|
|
|
|
DSCallSite CS(FunctionCalls[i], InlinedSites[&CG]);
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2003-07-02 04:39:44 +00:00
|
|
|
// Get the arguments bindings for the called function in CG... and merge
|
|
|
|
// them with the cloned graph.
|
|
|
|
CG.getCallSiteForArguments(*I->second).mergeWith(CS);
|
|
|
|
}
|
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
|
|
|
}
|
2003-02-03 22:51:28 +00:00
|
|
|
|