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"
|
2002-10-17 04:26:54 +00:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-07-30 22:06:40 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-10-01 22:33:50 +00:00
|
|
|
#include "Support/Statistic.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-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
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
// Calculate top-down from main...
|
|
|
|
if (Function *F = M.getMainFunction())
|
|
|
|
calculateGraph(*F);
|
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...
|
2002-07-30 22:06:40 +00:00
|
|
|
for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
|
|
|
|
if (!I->isExternal())
|
|
|
|
calculateGraph(*I);
|
2002-11-09 21:12:07 +00:00
|
|
|
|
|
|
|
GraphDone.clear(); // Free temporary memory...
|
2002-07-30 22:06:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
|
|
// our memory... here...
|
|
|
|
//
|
2003-02-03 22:51:28 +00:00
|
|
|
// 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() {
|
2003-06-22 03:03:52 +00:00
|
|
|
return;
|
2003-02-01 04:52:08 +00:00
|
|
|
for (hash_map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
|
2002-11-09 21:12:07 +00:00
|
|
|
E = DSInfo.end(); I != E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
|
|
|
|
// Empty map so next time memory is released, data structures are not
|
|
|
|
// re-deleted.
|
|
|
|
DSInfo.clear();
|
|
|
|
delete GlobalsGraph;
|
|
|
|
GlobalsGraph = 0;
|
|
|
|
}
|
|
|
|
|
2003-06-30 03:15:25 +00:00
|
|
|
#if 0
|
2002-10-17 04:26:54 +00:00
|
|
|
/// ResolveCallSite - This method is used to link the actual arguments together
|
|
|
|
/// with the formal arguments for a function call in the top-down closure. This
|
|
|
|
/// method assumes that the call site arguments have been mapped into nodes
|
|
|
|
/// local to the specified graph.
|
|
|
|
///
|
|
|
|
void TDDataStructures::ResolveCallSite(DSGraph &Graph,
|
2002-10-20 18:07:37 +00:00
|
|
|
const DSCallSite &CallSite) {
|
2002-10-17 04:26:54 +00:00
|
|
|
// Resolve all of the function formal arguments...
|
|
|
|
Function &F = Graph.getFunction();
|
|
|
|
Function::aiterator AI = F.abegin();
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2002-10-20 21:41:02 +00:00
|
|
|
for (unsigned i = 0, e = CallSite.getNumPtrArgs(); i != e; ++i, ++AI) {
|
2002-10-17 04:26:54 +00:00
|
|
|
// Advance the argument iterator to the first pointer argument...
|
2002-11-07 05:20:53 +00:00
|
|
|
while (!DS::isPointerType(AI->getType())) ++AI;
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2002-10-17 04:26:54 +00:00
|
|
|
// TD ...Merge the formal arg scalar with the actual arg node
|
|
|
|
DSNodeHandle &NodeForFormal = Graph.getNodeForValue(AI);
|
2002-10-21 15:04:18 +00:00
|
|
|
assert(NodeForFormal.getNode() && "Pointer argument has no dest node!");
|
|
|
|
NodeForFormal.mergeWith(CallSite.getPtrArg(i));
|
2002-10-17 04:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge returned node in the caller with the "return" node in callee
|
2002-10-21 02:08:03 +00:00
|
|
|
if (CallSite.getRetVal().getNode() && Graph.getRetNode().getNode())
|
|
|
|
Graph.getRetNode().mergeWith(CallSite.getRetVal());
|
2002-07-30 22:06:40 +00:00
|
|
|
}
|
2003-06-30 03:15:25 +00:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
void TDDataStructures::calculateGraph(Function &F) {
|
|
|
|
// Make sure this graph has not already been calculated, and that we don't get
|
2002-07-30 22:06:40 +00:00
|
|
|
// into an infinite loop with mutually recursive functions.
|
|
|
|
//
|
2002-11-08 21:28:37 +00:00
|
|
|
if (GraphDone.count(&F)) return;
|
|
|
|
GraphDone.insert(&F);
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
// Get the current functions graph...
|
|
|
|
DSGraph &Graph = getOrCreateDSGraph(F);
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2003-02-10 18:16:36 +00:00
|
|
|
// Recompute the Incomplete markers and eliminate unreachable nodes.
|
|
|
|
Graph.maskIncompleteMarkers();
|
2003-06-29 22:37:07 +00:00
|
|
|
unsigned Flags = FunctionHasCompleteArguments(F) ?
|
|
|
|
DSGraph::IgnoreFormalArgs : DSGraph::MarkFormalArgs;
|
2003-02-10 18:16:36 +00:00
|
|
|
Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
|
|
|
|
Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
const std::vector<DSCallSite> &CallSites = Graph.getFunctionCalls();
|
|
|
|
if (CallSites.empty()) {
|
|
|
|
DEBUG(std::cerr << " [TD] No callees for: " << F.getName() << "\n");
|
2003-02-09 18:42:43 +00:00
|
|
|
} else {
|
|
|
|
// Loop over all of the call sites, building a multi-map from Callees to
|
|
|
|
// DSCallSite*'s. With this map we can then loop over each callee, cloning
|
|
|
|
// this graph once into it, then resolving arguments.
|
|
|
|
//
|
|
|
|
std::multimap<Function*, const DSCallSite*> CalleeSites;
|
|
|
|
for (unsigned i = 0, e = CallSites.size(); i != e; ++i) {
|
|
|
|
const DSCallSite &CS = CallSites[i];
|
|
|
|
if (CS.isDirectCall()) {
|
|
|
|
if (!CS.getCalleeFunc()->isExternal()) // If it's not external
|
|
|
|
CalleeSites.insert(std::make_pair(CS.getCalleeFunc(), &CS));// Keep it
|
|
|
|
} else {
|
|
|
|
const std::vector<GlobalValue*> &Callees =
|
|
|
|
CS.getCalleeNode()->getGlobals();
|
|
|
|
|
|
|
|
// Loop over all of the functions that this call may invoke...
|
|
|
|
for (unsigned c = 0, e = Callees.size(); c != e; ++c)
|
|
|
|
if (Function *F = dyn_cast<Function>(Callees[c]))// If this is a fn...
|
|
|
|
if (!F->isExternal()) // If it's not extern
|
|
|
|
CalleeSites.insert(std::make_pair(F, &CS)); // Keep track of it!
|
|
|
|
}
|
2003-02-05 21:59:58 +00:00
|
|
|
}
|
2002-07-30 22:06:40 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Now that we have information about all of the callees, propagate the
|
|
|
|
// current graph into the callees.
|
|
|
|
//
|
|
|
|
DEBUG(std::cerr << " [TD] Inlining '" << F.getName() << "' into "
|
|
|
|
<< CalleeSites.size() << " callees.\n");
|
|
|
|
|
|
|
|
// Loop over all the callees...
|
|
|
|
for (std::multimap<Function*, const DSCallSite*>::iterator
|
|
|
|
I = CalleeSites.begin(), E = CalleeSites.end(); I != E; )
|
|
|
|
if (I->first == &F) { // Bottom-up pass takes care of self loops!
|
|
|
|
++I;
|
|
|
|
} else {
|
|
|
|
// For each callee...
|
2003-06-29 22:37:07 +00:00
|
|
|
Function &Callee = *I->first;
|
|
|
|
DSGraph &CG = getOrCreateDSGraph(Callee); // Get the callee's graph...
|
2002-11-08 21:28:37 +00:00
|
|
|
|
2003-06-29 22:37:07 +00:00
|
|
|
DEBUG(std::cerr << "\t [TD] Inlining into callee '" << Callee.getName()
|
|
|
|
<< "'\n");
|
2002-11-08 21:28:37 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Clone our current graph into the callee...
|
2003-06-30 03:15:25 +00:00
|
|
|
DSGraph::ScalarMapTy OldValMap;
|
|
|
|
DSGraph::NodeMapTy OldNodeMap;
|
|
|
|
DSGraph::ReturnNodesTy ReturnNodes;
|
|
|
|
CG.cloneInto(Graph, OldValMap, ReturnNodes, OldNodeMap,
|
2003-02-09 18:42:43 +00:00
|
|
|
DSGraph::StripModRefBits |
|
2003-02-10 18:16:36 +00:00
|
|
|
DSGraph::KeepAllocaBit | DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
2003-02-09 18:42:43 +00:00
|
|
|
OldValMap.clear(); // We don't care about the ValMap
|
2003-06-30 03:15:25 +00:00
|
|
|
ReturnNodes.clear(); // We don't care about return values either
|
2003-02-09 18:42:43 +00:00
|
|
|
|
|
|
|
// Loop over all of the invocation sites of the callee, resolving
|
|
|
|
// arguments to our graph. This loop may iterate multiple times if the
|
|
|
|
// current function calls this callee multiple times with different
|
|
|
|
// signatures.
|
|
|
|
//
|
2003-06-29 22:37:07 +00:00
|
|
|
for (; I != E && I->first == &Callee; ++I) {
|
2003-02-09 18:42:43 +00:00
|
|
|
// Map call site into callee graph
|
|
|
|
DSCallSite NewCS(*I->second, OldNodeMap);
|
2002-11-08 21:28:37 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Resolve the return values...
|
2003-06-30 03:15:25 +00:00
|
|
|
NewCS.getRetVal().mergeWith(CG.getReturnNodeFor(Callee));
|
2002-11-08 21:28:37 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Resolve all of the arguments...
|
2003-06-29 22:37:07 +00:00
|
|
|
Function::aiterator AI = Callee.abegin();
|
2003-02-09 18:42:43 +00:00
|
|
|
for (unsigned i = 0, e = NewCS.getNumPtrArgs();
|
2003-06-29 22:37:07 +00:00
|
|
|
i != e && AI != Callee.aend(); ++i, ++AI) {
|
2003-02-09 18:42:43 +00:00
|
|
|
// Advance the argument iterator to the first pointer argument...
|
2003-06-29 22:37:07 +00:00
|
|
|
while (AI != Callee.aend() && !DS::isPointerType(AI->getType()))
|
2003-02-09 18:42:43 +00:00
|
|
|
++AI;
|
2003-06-29 22:37:07 +00:00
|
|
|
if (AI == Callee.aend()) break;
|
2003-02-11 23:11:51 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Add the link from the argument scalar to the provided value
|
|
|
|
DSNodeHandle &NH = CG.getNodeForValue(AI);
|
|
|
|
assert(NH.getNode() && "Pointer argument without scalarmap entry?");
|
|
|
|
NH.mergeWith(NewCS.getPtrArg(i));
|
|
|
|
}
|
2002-11-08 21:28:37 +00:00
|
|
|
}
|
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Done with the nodemap...
|
|
|
|
OldNodeMap.clear();
|
2002-11-08 21:28:37 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
// Recompute the Incomplete markers and eliminate unreachable nodes.
|
2003-02-10 18:16:36 +00:00
|
|
|
CG.removeTriviallyDeadNodes();
|
2003-02-09 18:42:43 +00:00
|
|
|
CG.maskIncompleteMarkers();
|
2003-02-10 18:16:36 +00:00
|
|
|
CG.markIncompleteNodes(DSGraph::MarkFormalArgs |DSGraph::IgnoreGlobals);
|
2003-02-09 18:42:43 +00:00
|
|
|
CG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
|
|
|
}
|
2002-10-22 16:01:03 +00:00
|
|
|
|
2003-02-09 18:42:43 +00:00
|
|
|
DEBUG(std::cerr << " [TD] Done inlining into callees for: " << F.getName()
|
|
|
|
<< " [" << Graph.getGraphSize() << "+"
|
|
|
|
<< Graph.getFunctionCalls().size() << "]\n");
|
|
|
|
|
|
|
|
// Loop over all the callees... making sure they are all resolved now...
|
|
|
|
Function *LastFunc = 0;
|
|
|
|
for (std::multimap<Function*, const DSCallSite*>::iterator
|
|
|
|
I = CalleeSites.begin(), E = CalleeSites.end(); I != E; ++I)
|
|
|
|
if (I->first != LastFunc) { // Only visit each callee once...
|
|
|
|
LastFunc = I->first;
|
|
|
|
calculateGraph(*I->first);
|
|
|
|
}
|
|
|
|
}
|
2002-07-30 22:06:40 +00:00
|
|
|
}
|
2003-02-03 22:51:28 +00:00
|
|
|
|