//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
//
// This file implements the TDDataStructures class, which represents the
// Top-down Interprocedural closure of the data structure graph over the
#include "llvm/Analysis/DataStructure.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/Analysis/DSGraph.h"
+#include "Support/Debug.h"
#include "Support/Statistic.h"
-#include "DSCallSiteIterator.h"
+using namespace llvm;
namespace {
RegisterAnalysis<TDDataStructures> // Register the pass
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();
+void TDDataStructures::markReachableFunctionsExternallyAccessible(DSNode *N,
+ hash_set<DSNode*> &Visited) {
+ if (!N || Visited.count(N)) return;
+ 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);
+ }
+ }
}
+
// run - Calculate the top down data structure graphs for each function in the
// program.
//
bool TDDataStructures::run(Module &M) {
BUDataStructures &BU = getAnalysis<BUDataStructures>();
GlobalsGraph = new DSGraph(BU.getGlobalsGraph());
+ GlobalsGraph->setPrintAuxCalls();
// Figure out which functions must not mark their arguments complete because
- // they are accessible outside this compilation unit.
+ // they are accessible outside this compilation unit. Currently, these
+ // arguments are functions which are reachable by global variables in the
+ // globals graph.
+ const DSScalarMap &GGSM = GlobalsGraph->getScalarMap();
+ hash_set<DSNode*> Visited;
+ for (DSScalarMap::global_iterator I = GGSM.global_begin(), E = GGSM.global_end();
+ I != E; ++I)
+ markReachableFunctionsExternallyAccessible(GGSM.find(*I)->second.getNode(), Visited);
+
+ // Loop over unresolved call nodes. Any functions passed into (but not
+ // returned!) from unresolvable call nodes may be invoked outside of the
+ // 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);
+ }
+ Visited.clear();
+
+ // Functions without internal linkage also have unknown incoming arguments!
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
- if (!FunctionHasCompleteArguments(*I))
+ if (!I->isExternal() && !I->hasInternalLinkage())
ArgsRemainIncomplete.insert(I);
// We want to traverse the call graph in reverse post-order. To do this, we
// Calculate top-down from main...
if (Function *F = M.getMainFunction())
- ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
+ ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
- // Next calculate the graphs for each function unreachable function...
+ // Next calculate the graphs for each unreachable function...
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
ComputePostOrder(*I, VisitedGraph, PostOrder, ActualCallees);
}
ArgsRemainIncomplete.clear();
+ GlobalsGraph->removeTriviallyDeadNodes();
+
return false;
}
const std::vector<DSCallSite> &FunctionCalls = G.getFunctionCalls();
for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
+ Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
BUDataStructures::ActualCalleesTy::const_iterator>
- IP = ActualCallees.equal_range(&FunctionCalls[i].getCallInst());
+ IP = ActualCallees.equal_range(CallI);
for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
I != IP.second; ++I)
void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
// Recompute the Incomplete markers and eliminate unreachable nodes.
- Graph.removeTriviallyDeadNodes();
Graph.maskIncompleteMarkers();
// If any of the functions has incomplete incoming arguments, don't mark any
HasIncompleteArgs = true;
break;
}
-
+
+ // 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
unsigned Flags
= HasIncompleteArgs ? DSGraph::MarkFormalArgs : DSGraph::IgnoreFormalArgs;
Graph.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
+
+ // Delete dead nodes. Treat globals that are unreachable as dead also.
Graph.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
+ // 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.
+ //
const std::vector<DSCallSite> &FunctionCalls = Graph.getFunctionCalls();
if (FunctionCalls.empty()) {
DEBUG(std::cerr << " [TD] No callees for: " << Graph.getFunctionNames()
}
// Now that we have information about all of the callees, propagate the
- // current graph into the callees.
+ // 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).
//
DEBUG(std::cerr << " [TD] Inlining '" << Graph.getFunctionNames() <<"' into "
<< FunctionCalls.size() << " call nodes.\n");
const BUDataStructures::ActualCalleesTy &ActualCallees =
getAnalysis<BUDataStructures>().getActualCallees();
- // 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 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;
- // 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.
+ Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
+ // For each function in the invoked function list at this call site...
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;
- }
- }
-
- if (NumArgs == -1) {
- std::cerr << "ERROR: NONSAME NUMBER OF ARGUMENTS TO CALLEES\n";
- }
-
+ IP = ActualCallees.equal_range(CallI);
+ // Loop over each actual callee at this call site
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;
- }
+ DSGraph& CalleeGraph = getDSGraph(*I->second);
+ assert(&CalleeGraph != &Graph && "TD need not inline graph into self!");
+
+ CallSites.insert(std::make_pair(&CalleeGraph,
+ std::make_pair(I->second, &FunctionCalls[i])));
}
}
- // 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");
-
- // Transform our call site information into the cloned version for CG
- DSCallSite CS(FunctionCalls[i], InlinedSites[&CG]);
+ // 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.
+ 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);
+ }
- // Get the arguments bindings for the called function in CG... and merge
- // them with the cloned graph.
- CG.getCallSiteForArguments(*I->second).mergeWith(CS);
+ // 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;
+ DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
+ << CalleeGraph.getFunctionNames()
+ << "': " << CF.getFunctionType()->getNumParams()
+ << " args\n at call site (DSCallSite*) 0x" << &CS << "\n");
+
+ // Get the formal argument and return nodes for the called function and
+ // merge them with the cloned subgraph.
+ RC.mergeCallSite(CalleeGraph.getCallSiteForArguments(CF), CS);
+ ++NumTDInlines;
}
}
<< Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
<< Graph.getFunctionCalls().size() << "]\n");
}
-