2002-07-30 22:06:40 +00:00
|
|
|
//===- TopDownClosure.cpp - Compute the top-down interprocedure closure ---===//
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
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.
|
2005-04-21 21:13:18 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2005-03-22 00:12:00 +00:00
|
|
|
#include "llvm/Support/Timer.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-01-22 23:19:18 +00:00
|
|
|
#include <iostream>
|
2003-11-12 23:11:14 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2005-03-22 00:12:00 +00:00
|
|
|
#if 0
|
|
|
|
#define TIME_REGION(VARNAME, DESC) \
|
|
|
|
NamedRegionTimer VARNAME(DESC)
|
|
|
|
#else
|
|
|
|
#define TIME_REGION(VARNAME, DESC)
|
|
|
|
#endif
|
|
|
|
|
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()) {
|
2005-03-20 02:39:49 +00:00
|
|
|
std::vector<Function*> Functions;
|
|
|
|
NN->addFullFunctionList(Functions);
|
|
|
|
ArgsRemainIncomplete.insert(Functions.begin(), Functions.end());
|
2003-09-20 22:24:04 +00:00
|
|
|
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) {
|
2005-04-02 19:17:18 +00:00
|
|
|
BUInfo = &getAnalysis<BUDataStructures>();
|
|
|
|
GlobalECs = BUInfo->getGlobalECs();
|
|
|
|
GlobalsGraph = new DSGraph(BUInfo->getGlobalsGraph(), GlobalECs);
|
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();
|
2005-03-21 20:31:29 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
DSNode *N = GGSM.find(*I)->second.getNode();
|
|
|
|
if (N->isIncomplete())
|
|
|
|
markReachableFunctionsExternallyAccessible(N, 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.
|
2005-01-30 23:51:02 +00:00
|
|
|
for (DSGraph::afc_iterator I = GlobalsGraph->afc_begin(),
|
|
|
|
E = GlobalsGraph->afc_end(); I != E; ++I)
|
|
|
|
for (unsigned arg = 0, e = I->getNumPtrArgs(); arg != e; ++arg)
|
|
|
|
markReachableFunctionsExternallyAccessible(I->getPtrArg(arg).getNode(),
|
2003-09-20 23:58:33 +00:00
|
|
|
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;
|
|
|
|
|
2005-03-22 00:12:00 +00:00
|
|
|
#if 0
|
|
|
|
{TIME_REGION(XXX, "td:Copy graphs");
|
|
|
|
|
|
|
|
// Visit each of the graphs in reverse post-order now!
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isExternal())
|
|
|
|
getOrCreateDSGraph(*I);
|
2005-03-22 01:50:42 +00:00
|
|
|
return false;
|
2005-03-22 00:12:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
{TIME_REGION(XXX, "td:Compute postorder");
|
|
|
|
|
2002-11-08 21:28:37 +00:00
|
|
|
// Calculate top-down from main...
|
|
|
|
if (Function *F = M.getMainFunction())
|
2005-04-02 19:17:18 +00:00
|
|
|
ComputePostOrder(*F, VisitedGraph, PostOrder);
|
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)
|
2005-04-02 19:17:18 +00:00
|
|
|
ComputePostOrder(*I, VisitedGraph, PostOrder);
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2003-07-02 23:42:48 +00:00
|
|
|
VisitedGraph.clear(); // Release memory!
|
2005-03-22 00:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{TIME_REGION(XXX, "td:Inline stuff");
|
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()) {
|
2005-03-21 04:55:35 +00:00
|
|
|
InlineCallersIntoGraph(*PostOrder.back());
|
2003-07-02 23:42:48 +00:00
|
|
|
PostOrder.pop_back();
|
2003-06-30 04:53:08 +00:00
|
|
|
}
|
2005-03-22 00:12:00 +00:00
|
|
|
}
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2005-03-21 20:31:29 +00:00
|
|
|
// Free the IndCallMap.
|
|
|
|
while (!IndCallMap.empty()) {
|
|
|
|
delete IndCallMap.begin()->second;
|
|
|
|
IndCallMap.erase(IndCallMap.begin());
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-21 20:31:29 +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...
|
2005-03-22 00:12:00 +00:00
|
|
|
G = new DSGraph(getAnalysis<BUDataStructures>().getDSGraph(F), GlobalECs,
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
|
|
|
assert(G->getAuxFunctionCalls().empty() && "Cloned aux calls?");
|
2003-02-04 00:59:32 +00:00
|
|
|
G->setPrintAuxCalls();
|
2002-11-09 21:12:07 +00:00
|
|
|
G->setGlobalsGraph(GlobalsGraph);
|
2005-03-22 01:50:42 +00:00
|
|
|
|
|
|
|
// Note that this graph is the graph for ALL of the function in the SCC, not
|
|
|
|
// just F.
|
|
|
|
for (DSGraph::retnodes_iterator RI = G->retnodes_begin(),
|
|
|
|
E = G->retnodes_end(); RI != E; ++RI)
|
|
|
|
if (RI->first != &F)
|
|
|
|
DSInfo[RI->first] = G;
|
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,
|
2005-04-02 19:17:18 +00:00
|
|
|
std::vector<DSGraph*> &PostOrder) {
|
2003-07-01 16:28:11 +00:00
|
|
|
if (F.isExternal()) return;
|
|
|
|
DSGraph &G = getOrCreateDSGraph(F);
|
|
|
|
if (Visited.count(&G)) return;
|
|
|
|
Visited.insert(&G);
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2003-07-01 16:28:11 +00:00
|
|
|
// Recursively traverse all of the callee graphs.
|
2005-04-02 20:17:09 +00:00
|
|
|
for (DSGraph::fc_iterator CI = G.fc_begin(), CE = G.fc_end(); CI != CE; ++CI){
|
2005-01-30 23:51:02 +00:00
|
|
|
Instruction *CallI = CI->getCallSite().getInstruction();
|
2005-04-02 20:17:09 +00:00
|
|
|
for (BUDataStructures::callee_iterator I = BUInfo->callee_begin(CallI),
|
|
|
|
E = BUInfo->callee_end(CallI); I != E; ++I)
|
2005-04-02 19:17:18 +00:00
|
|
|
ComputePostOrder(*I->second, Visited, PostOrder);
|
2003-07-01 16:28:11 +00:00
|
|
|
}
|
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
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
/// InlineCallersIntoGraph - Inline all of the callers of the specified DS graph
|
|
|
|
/// into it, then recompute completeness of nodes in the resultant graph.
|
|
|
|
void TDDataStructures::InlineCallersIntoGraph(DSGraph &DSG) {
|
|
|
|
// Inline caller graphs into this graph. First step, get the list of call
|
|
|
|
// sites that call into this graph.
|
|
|
|
std::vector<CallerCallEdge> EdgesFromCaller;
|
|
|
|
std::map<DSGraph*, std::vector<CallerCallEdge> >::iterator
|
2005-04-21 21:13:18 +00:00
|
|
|
CEI = CallerEdges.find(&DSG);
|
2005-03-21 04:55:35 +00:00
|
|
|
if (CEI != CallerEdges.end()) {
|
|
|
|
std::swap(CEI->second, EdgesFromCaller);
|
|
|
|
CallerEdges.erase(CEI);
|
|
|
|
}
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
// Sort the caller sites to provide a by-caller-graph ordering.
|
|
|
|
std::sort(EdgesFromCaller.begin(), EdgesFromCaller.end());
|
|
|
|
|
|
|
|
|
2005-03-21 08:43:32 +00:00
|
|
|
// Merge information from the globals graph into this graph. FIXME: This is
|
|
|
|
// stupid. Instead of us cloning information from the GG into this graph,
|
|
|
|
// then having RemoveDeadNodes clone it back, we should do all of this as a
|
|
|
|
// post-pass over all of the graphs. We need to take cloning out of
|
|
|
|
// removeDeadNodes and gut removeDeadNodes at the same time first though. :(
|
2005-03-21 04:55:35 +00:00
|
|
|
{
|
|
|
|
DSGraph &GG = *DSG.getGlobalsGraph();
|
|
|
|
ReachabilityCloner RC(DSG, GG,
|
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
|
|
|
for (DSScalarMap::global_iterator
|
|
|
|
GI = DSG.getScalarMap().global_begin(),
|
|
|
|
E = DSG.getScalarMap().global_end(); GI != E; ++GI)
|
|
|
|
RC.getClonedNH(GG.getNodeForValue(*GI));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(std::cerr << "[TD] Inlining callers into '" << DSG.getFunctionNames()
|
|
|
|
<< "'\n");
|
|
|
|
|
|
|
|
// Iteratively inline caller graphs into this graph.
|
|
|
|
while (!EdgesFromCaller.empty()) {
|
|
|
|
DSGraph &CallerGraph = *EdgesFromCaller.back().CallerGraph;
|
|
|
|
|
|
|
|
// Iterate through all of the call sites of this graph, cloning and merging
|
|
|
|
// any nodes required by the call.
|
|
|
|
ReachabilityCloner RC(DSG, CallerGraph,
|
|
|
|
DSGraph::DontCloneCallNodes |
|
|
|
|
DSGraph::DontCloneAuxCallNodes);
|
|
|
|
|
|
|
|
// Inline all call sites from this caller graph.
|
|
|
|
do {
|
|
|
|
const DSCallSite &CS = *EdgesFromCaller.back().CS;
|
|
|
|
Function &CF = *EdgesFromCaller.back().CalledFunction;
|
2005-03-21 20:31:29 +00:00
|
|
|
DEBUG(std::cerr << " [TD] Inlining graph into Fn '"
|
|
|
|
<< CF.getName() << "' from ");
|
|
|
|
if (CallerGraph.getReturnNodes().empty())
|
|
|
|
DEBUG(std::cerr << "SYNTHESIZED INDIRECT GRAPH");
|
|
|
|
else
|
|
|
|
DEBUG (std::cerr << "Fn '"
|
|
|
|
<< CS.getCallSite().getInstruction()->
|
|
|
|
getParent()->getParent()->getName() << "'");
|
|
|
|
DEBUG(std::cerr << ": " << CF.getFunctionType()->getNumParams()
|
2005-03-21 04:55:35 +00:00
|
|
|
<< " args\n");
|
2005-04-21 21:13:18 +00:00
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
// Get the formal argument and return nodes for the called function and
|
|
|
|
// merge them with the cloned subgraph.
|
2005-03-21 20:31:29 +00:00
|
|
|
DSCallSite T1 = DSG.getCallSiteForArguments(CF);
|
|
|
|
RC.mergeCallSite(T1, CS);
|
2005-03-21 04:55:35 +00:00
|
|
|
++NumTDInlines;
|
|
|
|
|
|
|
|
EdgesFromCaller.pop_back();
|
|
|
|
} while (!EdgesFromCaller.empty() &&
|
|
|
|
EdgesFromCaller.back().CallerGraph == &CallerGraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Next, now that this graph is finalized, we need to recompute the
|
|
|
|
// incompleteness markers for this graph and remove unreachable nodes.
|
|
|
|
DSG.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;
|
2005-03-21 04:55:35 +00:00
|
|
|
for (DSGraph::retnodes_iterator I = DSG.retnodes_begin(),
|
|
|
|
E = DSG.retnodes_end(); I != E; ++I)
|
2003-07-02 04:39:44 +00:00
|
|
|
if (ArgsRemainIncomplete.count(I->first)) {
|
|
|
|
HasIncompleteArgs = true;
|
|
|
|
break;
|
|
|
|
}
|
2003-07-16 21:40:28 +00:00
|
|
|
|
|
|
|
// 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;
|
2005-03-21 04:55:35 +00:00
|
|
|
DSG.markIncompleteNodes(Flags | DSGraph::IgnoreGlobals);
|
2003-07-16 21:40:28 +00:00
|
|
|
|
|
|
|
// Delete dead nodes. Treat globals that are unreachable as dead also.
|
2005-03-21 04:55:35 +00:00
|
|
|
DSG.removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
// We are done with computing the current TD Graph! Finally, before we can
|
|
|
|
// finish processing this function, we figure out which functions it calls and
|
|
|
|
// records these call graph edges, so that we have them when we process the
|
|
|
|
// callee graphs.
|
|
|
|
if (DSG.fc_begin() == DSG.fc_end()) return;
|
2003-07-01 16:28:11 +00:00
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
// Loop over all the call sites and all the callees at each call site, and add
|
|
|
|
// edges to the CallerEdges structure for each callee.
|
|
|
|
for (DSGraph::fc_iterator CI = DSG.fc_begin(), E = DSG.fc_end();
|
2005-01-30 23:51:02 +00:00
|
|
|
CI != E; ++CI) {
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// Handle direct calls efficiently.
|
|
|
|
if (CI->isDirectCall()) {
|
|
|
|
if (!CI->getCalleeFunc()->isExternal() &&
|
|
|
|
!DSG.getReturnNodes().count(CI->getCalleeFunc()))
|
|
|
|
CallerEdges[&getDSGraph(*CI->getCalleeFunc())]
|
|
|
|
.push_back(CallerCallEdge(&DSG, &*CI, CI->getCalleeFunc()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-30 23:51:02 +00:00
|
|
|
Instruction *CallI = CI->getCallSite().getInstruction();
|
2003-07-16 21:40:28 +00:00
|
|
|
// For each function in the invoked function list at this call site...
|
2005-04-02 20:02:41 +00:00
|
|
|
BUDataStructures::callee_iterator IPI =
|
2005-04-02 19:17:18 +00:00
|
|
|
BUInfo->callee_begin(CallI), IPE = BUInfo->callee_end(CallI);
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// Skip over all calls to this graph (SCC calls).
|
2005-04-02 19:17:18 +00:00
|
|
|
while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
|
|
|
|
++IPI;
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// All SCC calls?
|
2005-04-02 19:17:18 +00:00
|
|
|
if (IPI == IPE) continue;
|
2005-03-21 20:31:29 +00:00
|
|
|
|
2005-04-02 19:17:18 +00:00
|
|
|
Function *FirstCallee = IPI->second;
|
|
|
|
++IPI;
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// Skip over more SCC calls.
|
2005-04-02 19:17:18 +00:00
|
|
|
while (IPI != IPE && &getDSGraph(*IPI->second) == &DSG)
|
|
|
|
++IPI;
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// If there is exactly one callee from this call site, remember the edge in
|
|
|
|
// CallerEdges.
|
2005-04-02 19:17:18 +00:00
|
|
|
if (IPI == IPE) {
|
2005-03-21 20:31:29 +00:00
|
|
|
if (!FirstCallee->isExternal())
|
|
|
|
CallerEdges[&getDSGraph(*FirstCallee)]
|
|
|
|
.push_back(CallerCallEdge(&DSG, &*CI, FirstCallee));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, there are multiple callees from this call site, so it must be
|
|
|
|
// an indirect call. Chances are that there will be other call sites with
|
|
|
|
// this set of targets. If so, we don't want to do M*N inlining operations,
|
|
|
|
// so we build up a new, private, graph that represents the calls of all
|
|
|
|
// calls to this set of functions.
|
|
|
|
std::vector<Function*> Callees;
|
2005-04-21 21:13:18 +00:00
|
|
|
for (BUDataStructures::ActualCalleesTy::const_iterator I =
|
2005-04-02 19:17:18 +00:00
|
|
|
BUInfo->callee_begin(CallI), E = BUInfo->callee_end(CallI);
|
|
|
|
I != E; ++I)
|
2005-03-21 20:31:29 +00:00
|
|
|
if (!I->second->isExternal())
|
|
|
|
Callees.push_back(I->second);
|
|
|
|
std::sort(Callees.begin(), Callees.end());
|
|
|
|
|
|
|
|
std::map<std::vector<Function*>, DSGraph*>::iterator IndCallRecI =
|
|
|
|
IndCallMap.lower_bound(Callees);
|
|
|
|
|
|
|
|
DSGraph *IndCallGraph;
|
|
|
|
|
|
|
|
// If we already have this graph, recycle it.
|
|
|
|
if (IndCallRecI != IndCallMap.end() && IndCallRecI->first == Callees) {
|
|
|
|
std::cerr << " [TD] *** Reuse of indcall graph for " << Callees.size()
|
|
|
|
<< " callees!\n";
|
|
|
|
IndCallGraph = IndCallRecI->second;
|
|
|
|
} else {
|
|
|
|
// Otherwise, create a new DSGraph to represent this.
|
|
|
|
IndCallGraph = new DSGraph(DSG.getGlobalECs(), DSG.getTargetData());
|
|
|
|
|
|
|
|
// Make a nullary dummy call site, which will eventually get some content
|
|
|
|
// merged into it. The actual callee function doesn't matter here, so we
|
|
|
|
// just pass it something to keep the ctor happy.
|
|
|
|
std::vector<DSNodeHandle> ArgDummyVec;
|
|
|
|
DSCallSite DummyCS(CI->getCallSite(), DSNodeHandle(), Callees[0]/*dummy*/,
|
|
|
|
ArgDummyVec);
|
|
|
|
IndCallGraph->getFunctionCalls().push_back(DummyCS);
|
|
|
|
|
|
|
|
IndCallRecI = IndCallMap.insert(IndCallRecI,
|
|
|
|
std::make_pair(Callees, IndCallGraph));
|
|
|
|
|
|
|
|
// Additionally, make sure that each of the callees inlines this graph
|
|
|
|
// exactly once.
|
|
|
|
DSCallSite *NCS = &IndCallGraph->getFunctionCalls().front();
|
|
|
|
for (unsigned i = 0, e = Callees.size(); i != e; ++i) {
|
|
|
|
DSGraph& CalleeGraph = getDSGraph(*Callees[i]);
|
|
|
|
if (&CalleeGraph != &DSG)
|
|
|
|
CallerEdges[&CalleeGraph].push_back(CallerCallEdge(IndCallGraph, NCS,
|
|
|
|
Callees[i]));
|
|
|
|
}
|
2003-07-02 04:39:44 +00:00
|
|
|
}
|
2005-03-21 20:31:29 +00:00
|
|
|
|
|
|
|
// Now that we know which graph to use for this, merge the caller
|
|
|
|
// information into the graph, based on information from the call site.
|
|
|
|
ReachabilityCloner RC(*IndCallGraph, DSG, 0);
|
|
|
|
RC.mergeCallSite(IndCallGraph->getFunctionCalls().front(), *CI);
|
2003-02-09 18:42:43 +00:00
|
|
|
}
|
2002-07-30 22:06:40 +00:00
|
|
|
}
|
2005-01-24 20:00:14 +00:00
|
|
|
|
2005-03-21 04:55:35 +00:00
|
|
|
|
2005-01-24 20:00:14 +00:00
|
|
|
static const Function *getFnForValue(const Value *V) {
|
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
return I->getParent()->getParent();
|
|
|
|
else if (const Argument *A = dyn_cast<Argument>(V))
|
|
|
|
return A->getParent();
|
|
|
|
else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
|
|
|
return BB->getParent();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TDDataStructures::deleteValue(Value *V) {
|
|
|
|
if (const Function *F = getFnForValue(V)) { // Function local value?
|
|
|
|
// If this is a function local value, just delete it from the scalar map!
|
|
|
|
getDSGraph(*F).getScalarMap().eraseIfExists(V);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-01-31 00:10:45 +00:00
|
|
|
if (Function *F = dyn_cast<Function>(V)) {
|
2005-01-24 20:00:14 +00:00
|
|
|
assert(getDSGraph(*F).getReturnNodes().size() == 1 &&
|
|
|
|
"cannot handle scc's");
|
|
|
|
delete DSInfo[F];
|
|
|
|
DSInfo.erase(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!isa<GlobalVariable>(V) && "Do not know how to delete GV's yet!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TDDataStructures::copyValue(Value *From, Value *To) {
|
|
|
|
if (From == To) return;
|
|
|
|
if (const Function *F = getFnForValue(From)) { // Function local value?
|
|
|
|
// If this is a function local value, just delete it from the scalar map!
|
|
|
|
getDSGraph(*F).getScalarMap().copyScalarIfExists(From, To);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *FromF = dyn_cast<Function>(From)) {
|
|
|
|
Function *ToF = cast<Function>(To);
|
|
|
|
assert(!DSInfo.count(ToF) && "New Function already exists!");
|
Create an equivalence class of global variables that DSA will never be able
to tell apart anyway, and only track the leader for of these equivalence
classes in our graphs.
This dramatically reduces the number of GlobalValue*'s that appear in scalar
maps, which A) reduces memory usage, by eliminating many many scalarmap entries
and B) reduces time for operations that need to execute an operation for each
global in the scalar map.
As an example, this reduces the memory used to analyze 176.gcc from 1GB to
511MB, which (while it's still way too much) is better because it doesn't hit
swap anymore. On eon, this shrinks the local graphs from 14MB to 6.8MB,
shrinks the bu+td graphs of povray from 50M to 40M, shrinks the TD graphs of
130.li from 8.8M to 3.6M, etc.
This change also speeds up DSA on large programs where this makes a big
difference. For example, 130.li goes from 1.17s -> 0.56s, 134.perl goes
from 2.14 -> 0.93s, povray goes from 15.63s->7.99s (!!!).
This also apparently either fixes the problem that caused DSA to crash on
perlbmk and gcc, or it hides it, because DSA now works on these. These
both take entirely too much time in the TD pass (147s for perl, 538s for
gcc, vs 7.67/5.9s in the bu pass for either one), but this is a known
problem that I'll deal with later.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@20696 91177308-0d34-0410-b5e6-96231b3b80d8
2005-03-19 22:23:45 +00:00
|
|
|
DSGraph *NG = new DSGraph(getDSGraph(*FromF), GlobalECs);
|
2005-01-24 20:00:14 +00:00
|
|
|
DSInfo[ToF] = NG;
|
|
|
|
assert(NG->getReturnNodes().size() == 1 && "Cannot copy SCC's yet!");
|
|
|
|
|
|
|
|
// Change the Function* is the returnnodes map to the ToF.
|
2005-03-15 16:55:04 +00:00
|
|
|
DSNodeHandle Ret = NG->retnodes_begin()->second;
|
2005-01-24 20:00:14 +00:00
|
|
|
NG->getReturnNodes().clear();
|
|
|
|
NG->getReturnNodes()[ToF] = Ret;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-03-24 04:22:04 +00:00
|
|
|
if (const Function *F = getFnForValue(To)) {
|
|
|
|
DSGraph &G = getDSGraph(*F);
|
|
|
|
G.getScalarMap().copyScalarIfExists(From, To);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << *From;
|
|
|
|
std::cerr << *To;
|
|
|
|
assert(0 && "Do not know how to copy this yet!");
|
|
|
|
abort();
|
2005-01-24 20:00:14 +00:00
|
|
|
}
|