//===- 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 "Support/StatisticReporter.h"
-using std::map;
+#include "Support/Debug.h"
+#include "Support/Statistic.h"
+#include "DSCallSiteIterator.h"
-static RegisterAnalysis<TDDataStructures>
-Y("tddatastructure", "Top-down Data Structure Analysis Closure");
-AnalysisID TDDataStructures::ID = Y;
+namespace {
+ RegisterAnalysis<TDDataStructures> // Register the pass
+ Y("tddatastructure", "Top-down Data Structure Analysis");
-// releaseMemory - If the pass pipeline is done with this pass, we can release
-// our memory... here...
-//
-void TDDataStructures::releaseMemory() {
- for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
- E = DSInfo.end(); I != E; ++I)
- delete I->second;
+ Statistic<> NumTDInlines("tddatastructures", "Number of graphs inlined");
+}
- // Empty map so next time memory is released, data structures are not
- // re-deleted.
- DSInfo.clear();
+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) {
- // Simply calculate the graphs for each function...
- for (Module::reverse_iterator I = M.rbegin(), E = M.rend(); I != E; ++I)
- if (!I->isExternal())
- calculateGraph(*I);
+ 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. Currently, these
+ // arguments are functions which are reachable by global variables in the
+ // globals graph.
+ const DSGraph::ScalarMapTy &GGSM = GlobalsGraph->getScalarMap();
+ hash_set<DSNode*> Visited;
+ for (DSGraph::ScalarMapTy::const_iterator I = GGSM.begin(), E = GGSM.end();
+ I != E; ++I)
+ if (isa<GlobalValue>(I->first))
+ markReachableFunctionsExternallyAccessible(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 (!I->isExternal() && !I->hasInternalLinkage())
+ ArgsRemainIncomplete.insert(I);
+
+ // 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();
+
+ // Calculate top-down from main...
+ if (Function *F = M.getMainFunction())
+ ComputePostOrder(*F, VisitedGraph, PostOrder, ActualCallees);
+
+ // 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);
+
+ VisitedGraph.clear(); // Release memory!
+
+ // Visit each of the graphs in reverse post-order now!
+ while (!PostOrder.empty()) {
+ inlineGraphIntoCallees(*PostOrder.back());
+ PostOrder.pop_back();
+ }
+
+ ArgsRemainIncomplete.clear();
return false;
}
-// ResolveArguments - Resolve the formal and actual arguments for a function
-// call by merging the nodes for the actual arguments at the call site Call[]
-// (these were copied over from the caller's graph into the callee's graph
-// by cloneInto, and the nodes can be found from OldNodeMap) with the
-// corresponding nodes for the formal arguments in the callee.
-//
-static void ResolveArguments(std::vector<DSNodeHandle> &Call,
- Function &callee,
- std::map<Value*, DSNodeHandle> &CalleeValueMap,
- std::map<const DSNode*, DSNode*> OldNodeMap,
- bool ignoreNodeMap) {
- // Resolve all of the function formal arguments...
- Function::aiterator AI = callee.abegin();
- for (unsigned i = 2, e = Call.size(); i != e; ++i) {
- // Advance the argument iterator to the first pointer argument...
- while (!isa<PointerType>(AI->getType())) ++AI;
-
- // TD ...Merge the formal arg scalar with the actual arg node
- DSNode* actualArg = Call[i];
- DSNode *nodeForActual = ignoreNodeMap? actualArg : OldNodeMap[actualArg];
- assert(nodeForActual && "No node found for actual arg in callee graph!");
-
- DSNode *nodeForFormal = CalleeValueMap[AI]->getLink(0);
- if (nodeForFormal)
- nodeForFormal->mergeWith(nodeForActual);
- ++AI;
+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();
+ G->setPrintAuxCalls();
+ G->setGlobalsGraph(GlobalsGraph);
}
+ return *G;
}
-// MergeGlobalNodes - Merge all existing global nodes with globals
-// inlined from the callee or with globals from the GlobalsGraph.
-//
-static void MergeGlobalNodes(DSGraph& Graph,
- map<Value*, DSNodeHandle> &OldValMap) {
- map<Value*, DSNodeHandle> &ValMap = Graph.getValueMap();
- for (map<Value*, DSNodeHandle>::iterator I = ValMap.begin(), E = ValMap.end();
- I != E; ++I)
- if (GlobalValue* GV = dyn_cast<GlobalValue>(I->first)) {
- map<Value*, DSNodeHandle>:: iterator NHI = OldValMap.find(GV);
- if (NHI != OldValMap.end()) // was it inlined from the callee?
- I->second->mergeWith(NHI->second);
- else // get it from the GlobalsGraph
- I->second->mergeWith(Graph.cloneGlobalInto(GV));
- }
- // Add unused inlined global nodes into the value map
- for (map<Value*, DSNodeHandle>::iterator I = OldValMap.begin(),
- E = OldValMap.end(); I != E; ++I)
- if (isa<GlobalValue>(I->first)) {
- DSNodeHandle &NH = ValMap[I->first]; // If global is not in ValMap...
- if (NH == 0)
- NH = I->second; // Add the one just inlined.
- }
+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) {
+ Instruction *CallI = FunctionCalls[i].getCallSite().getInstruction();
+ std::pair<BUDataStructures::ActualCalleesTy::const_iterator,
+ BUDataStructures::ActualCalleesTy::const_iterator>
+ IP = ActualCallees.equal_range(CallI);
+
+ for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
+ I != IP.second; ++I)
+ ComputePostOrder(*I->second, Visited, PostOrder, ActualCallees);
+ }
+
+ PostOrder.push_back(&G);
}
-// Helper function to push a caller's graph into the calleeGraph,
-// once per call between the caller and the callee.
-// Remove each such resolved call from the OrigFunctionCalls vector.
-// NOTE: This may produce O(N^2) behavior because it uses linear search
-// through the vector OrigFunctionCalls to find all calls to this callee.
-//
-void TDDataStructures::pushGraphIntoCallee(DSGraph &callerGraph,
- DSGraph &calleeGraph,
- std::map<Value*, DSNodeHandle> &OldValMap,
- std::map<const DSNode*, DSNode*> &OldNodeMap) {
-
- Function& caller = callerGraph.getFunction();
-
- // Loop over all function calls in the caller to find those to this callee
- std::vector<std::vector<DSNodeHandle> >& FunctionCalls =
- callerGraph.getOrigFunctionCalls();
-
- for (unsigned i = 0, ei = FunctionCalls.size(); i != ei; ++i) {
-
- std::vector<DSNodeHandle>& Call = FunctionCalls[i];
- assert(Call.size() >= 2 && "No function pointer in Call?");
- DSNodeHandle& callee = Call[1];
- std::vector<GlobalValue*> funcPtrs(callee->getGlobals());
-
- // Loop over the function pointers in the call, looking for the callee
- for (unsigned f = 0; f != funcPtrs.size(); ++f) {
-
- // Must be a function type, so this cast MUST succeed.
- Function &callee = cast<Function>(*funcPtrs[f]);
- if (&callee != &calleeGraph.getFunction())
- continue;
- // Found a call to the callee. Inline its graph
- // copy caller pointer because inlining may modify the callers vector
- // Merge actual arguments from the caller with formals in the callee.
- // Don't use the old->new node map if this is a self-recursive call.
- ResolveArguments(Call, callee, calleeGraph.getValueMap(), OldNodeMap,
- /*ignoreNodeMap*/ &caller == &callee);
- // If its not a self-recursive call, merge global nodes in the inlined
- // graph with the corresponding global nodes in the current graph
- if (&caller != &callee)
- MergeGlobalNodes(calleeGraph, OldValMap);
- // Merge returned node in the caller with the "return" node in callee
- if (Call[0])
- calleeGraph.getRetNode()->mergeWith(OldNodeMap[Call[0]]);
-
- // Erase the entry in the globals vector
- funcPtrs.erase(funcPtrs.begin()+f--);
-
- } // for each function pointer in the call node
- } // for each original call node
+// 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;
+ }
+
+ // Empty map so next time memory is released, data structures are not
+ // re-deleted.
+ DSInfo.clear();
+ delete GlobalsGraph;
+ GlobalsGraph = 0;
}
+void TDDataStructures::inlineGraphIntoCallees(DSGraph &Graph) {
+ // Recompute the Incomplete markers and eliminate unreachable nodes.
+ Graph.removeTriviallyDeadNodes();
+ Graph.maskIncompleteMarkers();
-DSGraph &TDDataStructures::calculateGraph(Function &F) {
- // Make sure this graph has not already been calculated, or that we don't get
- // into an infinite loop with mutually recursive functions.
- //
- DSGraph *&Graph = DSInfo[&F];
- if (Graph) return *Graph;
-
- // Copy the local version into DSInfo...
- DSGraph& BUGraph = getAnalysis<BUDataStructures>().getDSGraph(F);
- Graph = new DSGraph(BUGraph);
-
- // Find the callers of this function recorded during the BU pass
- std::set<Function*> &PendingCallers = BUGraph.getPendingCallers();
-
- DEBUG(std::cerr << " [TD] Inlining callers for: " << F.getName() << "\n");
-
- for (std::set<Function*>::iterator I=PendingCallers.begin(),
- E=PendingCallers.end(); I != E; ++I) {
- Function& caller = **I;
- assert(! caller.isExternal() && "Externals unexpected in callers list");
-
- DEBUG(std::cerr << "\t [TD] Inlining " << caller.getName()
- << " into callee: " << F.getName() << "\n");
-
- // These two maps keep track of where scalars in the old graph _used_
- // to point to, and of new nodes matching nodes of the old graph.
- // These remain empty if no other graph is inlined (i.e., self-recursive).
- std::map<const DSNode*, DSNode*> OldNodeMap;
- std::map<Value*, DSNodeHandle> OldValMap;
-
- if (&caller == &F) {
- // Self-recursive call: this can happen after a cycle of calls is inlined.
- pushGraphIntoCallee(*Graph, *Graph, OldValMap, OldNodeMap);
- }
- else {
- // Recursively compute the graph for the caller. That should
- // be fully resolved except if there is mutual recursion...
- //
- DSGraph &callerGraph = calculateGraph(caller); // Graph to inline
-
- DEBUG(std::cerr << "\t\t[TD] Got graph for " << caller.getName()
- << " in: " << F.getName() << "\n");
-
- // Clone the caller's graph into the current graph, keeping
- // track of where scalars in the old graph _used_ to point...
- // Do this here because it only needs to happens once for each caller!
- // Strip scalars but not allocas since they are visible in callee.
- //
- DSNode *RetVal = Graph->cloneInto(callerGraph, OldValMap, OldNodeMap,
- /*StripScalars*/ true,
- /*StripAllocas*/ false,
- /*CopyCallers*/ true,
- /*CopyOrigCalls*/ false);
-
- pushGraphIntoCallee(callerGraph, *Graph, OldValMap, OldNodeMap);
+ // 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;
}
+
+ // 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()
+ << "\n");
+ return;
}
- // Recompute the Incomplete markers and eliminate unreachable nodes.
- Graph->maskIncompleteMarkers();
- Graph->markIncompleteNodes(/*markFormals*/ ! F.hasInternalLinkage()
- /*&& FIXME: NEED TO CHECK IF ALL CALLERS FOUND!*/);
- Graph->removeDeadNodes(/*KeepAllGlobals*/ false, /*KeepCalls*/ false);
+ // Now that we have information about all of the callees, propagate the
+ // 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();
+
+ // Loop over all the call sites and all the callees at each call site.
+ // Clone and merge the reachable subgraph from the call into callee's graph.
+ //
+ for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
+ 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(CallI);
+
+ // Multiple callees may have the same graph, so try to inline and merge
+ // only once for each <callSite,calleeGraph> pair, not once for each
+ // <callSite,calleeFunction> pair; the latter will be correct but slower.
+ hash_set<DSGraph*> GraphsSeen;
+
+ // Loop over each actual callee at this call site
+ for (BUDataStructures::ActualCalleesTy::const_iterator I = IP.first;
+ I != IP.second; ++I) {
+ DSGraph& CalleeGraph = getDSGraph(*I->second);
+ assert(&CalleeGraph != &Graph && "TD need not inline graph into self!");
+
+ // if this callee graph is already done at this site, skip this callee
+ if (GraphsSeen.find(&CalleeGraph) != GraphsSeen.end())
+ continue;
+ GraphsSeen.insert(&CalleeGraph);
+
+ // Get the root nodes for cloning the reachable subgraph into each callee:
+ // -- all global nodes that appear in both the caller and the callee
+ // -- return value at this call site, if any
+ // -- actual arguments passed at this call site
+ // -- callee node at this call site, if this is an indirect call (this may
+ // not be needed for merging, but allows us to create CS and therefore
+ // simplify the merging below).
+ hash_set<const DSNode*> RootNodeSet;
+ for (DSGraph::ScalarMapTy::const_iterator
+ SI = CalleeGraph.getScalarMap().begin(),
+ SE = CalleeGraph.getScalarMap().end(); SI != SE; ++SI)
+ if (GlobalValue* GV = dyn_cast<GlobalValue>(SI->first)) {
+ DSGraph::ScalarMapTy::const_iterator GI=Graph.getScalarMap().find(GV);
+ if (GI != Graph.getScalarMap().end())
+ RootNodeSet.insert(GI->second.getNode());
+ }
+
+ if (const DSNode* RetNode = FunctionCalls[i].getRetVal().getNode())
+ RootNodeSet.insert(RetNode);
- DEBUG(std::cerr << " [TD] Done inlining callers for: " << F.getName() << " ["
- << Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
- << "]\n");
+ for (unsigned j=0, N=FunctionCalls[i].getNumPtrArgs(); j < N; ++j)
+ if (const DSNode* ArgTarget = FunctionCalls[i].getPtrArg(j).getNode())
+ RootNodeSet.insert(ArgTarget);
- return *Graph;
+ if (FunctionCalls[i].isIndirectCall())
+ RootNodeSet.insert(FunctionCalls[i].getCalleeNode());
+
+ DEBUG(std::cerr << " [TD] Resolving arguments for callee graph '"
+ << CalleeGraph.getFunctionNames()
+ << "': " << I->second->getFunctionType()->getNumParams()
+ << " args\n at call site (DSCallSite*) 0x"
+ << &FunctionCalls[i] << "\n");
+
+ DSGraph::NodeMapTy NodeMapInCallee; // map from nodes to clones in callee
+ DSGraph::NodeMapTy CompletedMap; // unused map for nodes not to do
+ CalleeGraph.cloneReachableSubgraph(Graph, RootNodeSet,
+ NodeMapInCallee, CompletedMap,
+ DSGraph::StripModRefBits |
+ DSGraph::KeepAllocaBit);
+
+ // Transform our call site info into the cloned version for CalleeGraph
+ DSCallSite CS(FunctionCalls[i], NodeMapInCallee);
+
+ // Get the formal argument and return nodes for the called function
+ // and merge them with the cloned subgraph. Global nodes were merged
+ // already by cloneReachableSubgraph() above.
+ CalleeGraph.getCallSiteForArguments(*I->second).mergeWith(CS);
+
+ ++NumTDInlines;
+ }
+ }
+
+ DEBUG(std::cerr << " [TD] Done inlining into callees for: "
+ << Graph.getFunctionNames() << " [" << Graph.getGraphSize() << "+"
+ << Graph.getFunctionCalls().size() << "]\n");
}
+