2002-10-03 20:38:41 +00:00
|
|
|
//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-18 00:12:30 +00:00
|
|
|
//
|
|
|
|
// This file implements the BUDataStructures class, which represents the
|
|
|
|
// Bottom-Up Interprocedural closure of the data structure graph over the
|
|
|
|
// program. This is useful for applications like pool allocation, but **not**
|
2002-10-03 20:38:41 +00:00
|
|
|
// applications like alias analysis.
|
2002-07-18 00:12:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-07 06:35:22 +00:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
2002-07-18 00:12:30 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2003-07-01 16:04:18 +00:00
|
|
|
#include "DSCallSiteIterator.h"
|
2003-11-12 23:11:14 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-11-17 22:16:28 +00:00
|
|
|
namespace {
|
|
|
|
Statistic<> MaxSCC("budatastructure", "Maximum SCC Size in Call Graph");
|
2003-07-02 20:24:42 +00:00
|
|
|
Statistic<> NumBUInlines("budatastructures", "Number of graphs inlined");
|
2003-07-02 23:42:48 +00:00
|
|
|
Statistic<> NumCallEdges("budatastructures", "Number of 'actual' call edges");
|
2002-11-17 22:16:28 +00:00
|
|
|
|
|
|
|
RegisterAnalysis<BUDataStructures>
|
2003-06-28 22:14:55 +00:00
|
|
|
X("budatastructure", "Bottom-up Data Structure Analysis");
|
2002-11-17 22:16:28 +00:00
|
|
|
}
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-11-07 05:20:53 +00:00
|
|
|
using namespace DS;
|
2002-10-03 20:38:41 +00:00
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
// run - Calculate the bottom up data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 04:48:05 +00:00
|
|
|
bool BUDataStructures::runOnModule(Module &M) {
|
2003-06-28 22:14:55 +00:00
|
|
|
LocalDataStructures &LocalDSA = getAnalysis<LocalDataStructures>();
|
|
|
|
GlobalsGraph = new DSGraph(LocalDSA.getGlobalsGraph());
|
2003-02-03 19:11:38 +00:00
|
|
|
GlobalsGraph->setPrintAuxCalls();
|
2002-11-09 21:12:07 +00:00
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
if (MainFunc)
|
|
|
|
calculateReachableGraphs(MainFunc);
|
|
|
|
|
|
|
|
// Calculate the graphs for any functions that are unreachable from main...
|
2002-11-09 21:12:07 +00:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2003-07-01 16:04:18 +00:00
|
|
|
if (!I->isExternal() && !DSInfo.count(I)) {
|
2002-11-17 22:16:28 +00:00
|
|
|
#ifndef NDEBUG
|
2002-11-11 21:35:13 +00:00
|
|
|
if (MainFunc)
|
|
|
|
std::cerr << "*** Function unreachable from main: "
|
|
|
|
<< I->getName() << "\n";
|
2002-11-17 22:16:28 +00:00
|
|
|
#endif
|
2002-11-11 21:35:13 +00:00
|
|
|
calculateReachableGraphs(I); // Calculate all graphs...
|
|
|
|
}
|
2003-07-02 23:42:48 +00:00
|
|
|
|
|
|
|
NumCallEdges += ActualCallees.size();
|
2003-09-20 23:27:05 +00:00
|
|
|
|
|
|
|
// At the end of the bottom-up pass, the globals graph becomes complete.
|
|
|
|
// FIXME: This is not the right way to do this, but it is sorta better than
|
2003-09-20 23:58:33 +00:00
|
|
|
// nothing! In particular, externally visible globals and unresolvable call
|
|
|
|
// nodes at the end of the BU phase should make things that they point to
|
|
|
|
// incomplete in the globals graph.
|
|
|
|
//
|
2004-02-08 01:51:48 +00:00
|
|
|
GlobalsGraph->removeTriviallyDeadNodes();
|
2003-09-20 23:27:05 +00:00
|
|
|
GlobalsGraph->maskIncompleteMarkers();
|
2002-11-09 21:12:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-10-03 20:38:41 +00:00
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
void BUDataStructures::calculateReachableGraphs(Function *F) {
|
|
|
|
std::vector<Function*> Stack;
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<Function*, unsigned> ValMap;
|
2002-11-11 21:35:13 +00:00
|
|
|
unsigned NextID = 1;
|
|
|
|
calculateGraphs(F, Stack, NextID, ValMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
DSGraph &BUDataStructures::getOrCreateGraph(Function *F) {
|
|
|
|
// Has the graph already been created?
|
|
|
|
DSGraph *&Graph = DSInfo[F];
|
|
|
|
if (Graph) return *Graph;
|
|
|
|
|
|
|
|
// Copy the local version into DSInfo...
|
|
|
|
Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(*F));
|
|
|
|
|
|
|
|
Graph->setGlobalsGraph(GlobalsGraph);
|
|
|
|
Graph->setPrintAuxCalls();
|
|
|
|
|
|
|
|
// Start with a copy of the original call sites...
|
|
|
|
Graph->getAuxFunctionCalls() = Graph->getFunctionCalls();
|
|
|
|
return *Graph;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BUDataStructures::calculateGraphs(Function *F,
|
|
|
|
std::vector<Function*> &Stack,
|
|
|
|
unsigned &NextID,
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<Function*, unsigned> &ValMap) {
|
2003-11-13 05:04:19 +00:00
|
|
|
assert(!ValMap.count(F) && "Shouldn't revisit functions!");
|
2002-11-11 21:35:13 +00:00
|
|
|
unsigned Min = NextID++, MyID = Min;
|
|
|
|
ValMap[F] = Min;
|
|
|
|
Stack.push_back(F);
|
|
|
|
|
2004-03-04 17:05:28 +00:00
|
|
|
// FIXME! This test should be generalized to be any function that we have
|
|
|
|
// already processed, in the case when there isn't a main or there are
|
|
|
|
// unreachable functions!
|
2002-11-11 21:35:13 +00:00
|
|
|
if (F->isExternal()) { // sprintf, fprintf, sscanf, etc...
|
|
|
|
// No callees!
|
|
|
|
Stack.pop_back();
|
|
|
|
ValMap[F] = ~0;
|
|
|
|
return Min;
|
|
|
|
}
|
|
|
|
|
|
|
|
DSGraph &Graph = getOrCreateGraph(F);
|
|
|
|
|
|
|
|
// The edges out of the current node are the call site targets...
|
2003-06-30 05:27:53 +00:00
|
|
|
for (DSCallSiteIterator I = DSCallSiteIterator::begin_aux(Graph),
|
|
|
|
E = DSCallSiteIterator::end_aux(Graph); I != E; ++I) {
|
2002-11-11 21:35:13 +00:00
|
|
|
Function *Callee = *I;
|
|
|
|
unsigned M;
|
|
|
|
// Have we visited the destination function yet?
|
2003-02-01 04:52:08 +00:00
|
|
|
hash_map<Function*, unsigned>::iterator It = ValMap.find(Callee);
|
2002-11-11 21:35:13 +00:00
|
|
|
if (It == ValMap.end()) // No, visit it now.
|
|
|
|
M = calculateGraphs(Callee, Stack, NextID, ValMap);
|
|
|
|
else // Yes, get it's number.
|
|
|
|
M = It->second;
|
|
|
|
if (M < Min) Min = M;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(ValMap[F] == MyID && "SCC construction assumption wrong!");
|
|
|
|
if (Min != MyID)
|
|
|
|
return Min; // This is part of a larger SCC!
|
|
|
|
|
|
|
|
// If this is a new SCC, process it now.
|
|
|
|
if (Stack.back() == F) { // Special case the single "SCC" case here.
|
|
|
|
DEBUG(std::cerr << "Visiting single node SCC #: " << MyID << " fn: "
|
|
|
|
<< F->getName() << "\n");
|
|
|
|
Stack.pop_back();
|
2003-06-30 05:09:58 +00:00
|
|
|
DSGraph &G = getDSGraph(*F);
|
|
|
|
DEBUG(std::cerr << " [BU] Calculating graph for: " << F->getName()<< "\n");
|
|
|
|
calculateGraph(G);
|
|
|
|
DEBUG(std::cerr << " [BU] Done inlining: " << F->getName() << " ["
|
|
|
|
<< G.getGraphSize() << "+" << G.getAuxFunctionCalls().size()
|
|
|
|
<< "]\n");
|
2002-11-11 21:35:13 +00:00
|
|
|
|
2002-11-17 22:16:28 +00:00
|
|
|
if (MaxSCC < 1) MaxSCC = 1;
|
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
// Should we revisit the graph?
|
2003-06-30 05:27:53 +00:00
|
|
|
if (DSCallSiteIterator::begin_aux(G) != DSCallSiteIterator::end_aux(G)) {
|
2002-11-11 21:35:13 +00:00
|
|
|
ValMap.erase(F);
|
|
|
|
return calculateGraphs(F, Stack, NextID, ValMap);
|
|
|
|
} else {
|
|
|
|
ValMap[F] = ~0U;
|
|
|
|
}
|
|
|
|
return MyID;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// SCCFunctions - Keep track of the functions in the current SCC
|
|
|
|
//
|
2004-01-31 21:02:18 +00:00
|
|
|
hash_set<DSGraph*> SCCGraphs;
|
2002-11-11 21:35:13 +00:00
|
|
|
|
|
|
|
Function *NF;
|
|
|
|
std::vector<Function*>::iterator FirstInSCC = Stack.end();
|
2003-06-30 05:09:58 +00:00
|
|
|
DSGraph *SCCGraph = 0;
|
2002-11-11 21:35:13 +00:00
|
|
|
do {
|
|
|
|
NF = *--FirstInSCC;
|
|
|
|
ValMap[NF] = ~0U;
|
2003-06-30 05:09:58 +00:00
|
|
|
|
|
|
|
// Figure out which graph is the largest one, in order to speed things up
|
|
|
|
// a bit in situations where functions in the SCC have widely different
|
|
|
|
// graph sizes.
|
|
|
|
DSGraph &NFGraph = getDSGraph(*NF);
|
2004-01-31 21:02:18 +00:00
|
|
|
SCCGraphs.insert(&NFGraph);
|
2004-03-04 17:05:28 +00:00
|
|
|
// FIXME: If we used a better way of cloning graphs (ie, just splice all
|
|
|
|
// of the nodes into the new graph), this would be completely unneeded!
|
2003-06-30 05:09:58 +00:00
|
|
|
if (!SCCGraph || SCCGraph->getGraphSize() < NFGraph.getGraphSize())
|
|
|
|
SCCGraph = &NFGraph;
|
2002-11-11 21:35:13 +00:00
|
|
|
} while (NF != F);
|
|
|
|
|
2003-06-30 05:09:58 +00:00
|
|
|
std::cerr << "Calculating graph for SCC #: " << MyID << " of size: "
|
2004-01-31 21:02:18 +00:00
|
|
|
<< SCCGraphs.size() << "\n";
|
2002-11-11 21:35:13 +00:00
|
|
|
|
2002-11-17 22:16:28 +00:00
|
|
|
// Compute the Max SCC Size...
|
2004-01-31 21:02:18 +00:00
|
|
|
if (MaxSCC < SCCGraphs.size())
|
|
|
|
MaxSCC = SCCGraphs.size();
|
2002-11-12 15:58:08 +00:00
|
|
|
|
2003-06-30 05:09:58 +00:00
|
|
|
// First thing first, collapse all of the DSGraphs into a single graph for
|
|
|
|
// the entire SCC. We computed the largest graph, so clone all of the other
|
|
|
|
// (smaller) graphs into it. Discard all of the old graphs.
|
|
|
|
//
|
2004-01-31 21:02:18 +00:00
|
|
|
for (hash_set<DSGraph*>::iterator I = SCCGraphs.begin(),
|
|
|
|
E = SCCGraphs.end(); I != E; ++I) {
|
|
|
|
DSGraph &G = **I;
|
2003-06-30 05:09:58 +00:00
|
|
|
if (&G != SCCGraph) {
|
2004-03-04 17:05:28 +00:00
|
|
|
{
|
|
|
|
DSGraph::NodeMapTy NodeMap;
|
|
|
|
SCCGraph->cloneInto(G, SCCGraph->getScalarMap(),
|
|
|
|
SCCGraph->getReturnNodes(), NodeMap);
|
|
|
|
}
|
2003-06-30 05:09:58 +00:00
|
|
|
// Update the DSInfo map and delete the old graph...
|
2004-01-31 21:02:18 +00:00
|
|
|
for (DSGraph::ReturnNodesTy::iterator I = G.getReturnNodes().begin(),
|
|
|
|
E = G.getReturnNodes().end(); I != E; ++I)
|
|
|
|
DSInfo[I->first] = SCCGraph;
|
2003-06-30 05:09:58 +00:00
|
|
|
delete &G;
|
|
|
|
}
|
|
|
|
}
|
2002-11-11 21:35:13 +00:00
|
|
|
|
2003-07-02 04:37:48 +00:00
|
|
|
// Clean up the graph before we start inlining a bunch again...
|
2004-01-27 21:50:41 +00:00
|
|
|
SCCGraph->removeDeadNodes(DSGraph::RemoveUnreachableGlobals);
|
2003-07-02 04:37:48 +00:00
|
|
|
|
2003-06-30 05:09:58 +00:00
|
|
|
// Now that we have one big happy family, resolve all of the call sites in
|
|
|
|
// the graph...
|
|
|
|
calculateGraph(*SCCGraph);
|
|
|
|
DEBUG(std::cerr << " [BU] Done inlining SCC [" << SCCGraph->getGraphSize()
|
|
|
|
<< "+" << SCCGraph->getAuxFunctionCalls().size() << "]\n");
|
2002-11-11 21:35:13 +00:00
|
|
|
|
|
|
|
std::cerr << "DONE with SCC #: " << MyID << "\n";
|
|
|
|
|
|
|
|
// We never have to revisit "SCC" processed functions...
|
|
|
|
|
|
|
|
// Drop the stuff we don't need from the end of the stack
|
|
|
|
Stack.erase(FirstInSCC, Stack.end());
|
|
|
|
return MyID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MyID; // == Min
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
// releaseMemory - If the pass pipeline is done with this pass, we can release
|
|
|
|
// our memory... here...
|
|
|
|
//
|
|
|
|
void BUDataStructures::releaseMemory() {
|
2003-06-30 05:09:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
// Empty map so next time memory is released, data structures are not
|
|
|
|
// re-deleted.
|
|
|
|
DSInfo.clear();
|
2002-11-09 21:12:07 +00:00
|
|
|
delete GlobalsGraph;
|
|
|
|
GlobalsGraph = 0;
|
2002-07-18 00:12:30 +00:00
|
|
|
}
|
|
|
|
|
2003-06-30 05:09:58 +00:00
|
|
|
void BUDataStructures::calculateGraph(DSGraph &Graph) {
|
2002-11-11 21:35:13 +00:00
|
|
|
// Move our call site list into TempFCs so that inline call sites go into the
|
|
|
|
// new call site list and doesn't invalidate our iterators!
|
|
|
|
std::vector<DSCallSite> TempFCs;
|
|
|
|
std::vector<DSCallSite> &AuxCallsList = Graph.getAuxFunctionCalls();
|
|
|
|
TempFCs.swap(AuxCallsList);
|
|
|
|
|
2003-06-30 05:09:58 +00:00
|
|
|
DSGraph::ReturnNodesTy &ReturnNodes = Graph.getReturnNodes();
|
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
// Loop over all of the resolvable call sites
|
|
|
|
unsigned LastCallSiteIdx = ~0U;
|
2003-06-30 05:27:53 +00:00
|
|
|
for (DSCallSiteIterator I = DSCallSiteIterator::begin(TempFCs),
|
|
|
|
E = DSCallSiteIterator::end(TempFCs); I != E; ++I) {
|
2002-11-11 21:35:13 +00:00
|
|
|
// If we skipped over any call sites, they must be unresolvable, copy them
|
|
|
|
// to the real call site list.
|
|
|
|
LastCallSiteIdx++;
|
|
|
|
for (; LastCallSiteIdx < I.getCallSiteIdx(); ++LastCallSiteIdx)
|
|
|
|
AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
|
|
|
|
LastCallSiteIdx = I.getCallSiteIdx();
|
|
|
|
|
|
|
|
// Resolve the current call...
|
|
|
|
Function *Callee = *I;
|
2003-07-02 04:37:48 +00:00
|
|
|
DSCallSite CS = I.getCallSite();
|
2002-11-11 21:35:13 +00:00
|
|
|
|
|
|
|
if (Callee->isExternal()) {
|
|
|
|
// Ignore this case, simple varargs functions we cannot stub out!
|
2003-11-13 05:04:19 +00:00
|
|
|
} else if (ReturnNodes.count(Callee)) {
|
2002-11-11 21:35:13 +00:00
|
|
|
// Self recursion... simply link up the formal arguments with the
|
|
|
|
// actual arguments...
|
2003-06-30 05:09:58 +00:00
|
|
|
DEBUG(std::cerr << " Self Inlining: " << Callee->getName() << "\n");
|
2002-11-11 21:35:13 +00:00
|
|
|
|
|
|
|
// Handle self recursion by resolving the arguments and return value
|
2003-06-30 05:09:58 +00:00
|
|
|
Graph.mergeInGraph(CS, *Callee, Graph, 0);
|
2002-11-11 00:01:34 +00:00
|
|
|
|
|
|
|
} else {
|
2003-09-20 16:34:13 +00:00
|
|
|
ActualCallees.insert(std::make_pair(CS.getCallSite().getInstruction(),
|
|
|
|
Callee));
|
2003-07-02 04:37:48 +00:00
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
// Get the data structure graph for the called function.
|
2002-11-11 00:01:34 +00:00
|
|
|
//
|
2002-11-11 21:35:13 +00:00
|
|
|
DSGraph &GI = getDSGraph(*Callee); // Graph to inline
|
2004-02-27 20:05:15 +00:00
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
DEBUG(std::cerr << " Inlining graph for " << Callee->getName()
|
2003-02-03 19:11:38 +00:00
|
|
|
<< "[" << GI.getGraphSize() << "+"
|
2003-07-01 16:04:18 +00:00
|
|
|
<< GI.getAuxFunctionCalls().size() << "] into '"
|
|
|
|
<< Graph.getFunctionNames() << "' [" << Graph.getGraphSize() << "+"
|
2003-02-03 19:11:38 +00:00
|
|
|
<< Graph.getAuxFunctionCalls().size() << "]\n");
|
2003-06-30 03:15:25 +00:00
|
|
|
Graph.mergeInGraph(CS, *Callee, GI,
|
2002-11-27 17:41:13 +00:00
|
|
|
DSGraph::KeepModRefBits |
|
|
|
|
DSGraph::StripAllocaBit | DSGraph::DontCloneCallNodes);
|
2003-07-02 20:24:42 +00:00
|
|
|
++NumBUInlines;
|
2002-11-17 22:16:28 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
Graph.writeGraphToFile(std::cerr, "bu_" + F.getName() + "_after_" +
|
|
|
|
Callee->getName());
|
|
|
|
#endif
|
2002-11-11 00:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
// Make sure to catch any leftover unresolvable calls...
|
|
|
|
for (++LastCallSiteIdx; LastCallSiteIdx < TempFCs.size(); ++LastCallSiteIdx)
|
|
|
|
AuxCallsList.push_back(TempFCs[LastCallSiteIdx]);
|
2002-11-11 00:01:34 +00:00
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
TempFCs.clear();
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2003-07-16 21:42:03 +00:00
|
|
|
// Recompute the Incomplete markers
|
2004-02-20 23:52:15 +00:00
|
|
|
assert(Graph.getInlinedGlobals().empty());
|
2002-11-11 21:35:13 +00:00
|
|
|
Graph.maskIncompleteMarkers();
|
2003-01-23 22:05:33 +00:00
|
|
|
Graph.markIncompleteNodes(DSGraph::MarkFormalArgs);
|
2003-07-16 21:42:03 +00:00
|
|
|
|
|
|
|
// Delete dead nodes. Treat globals that are unreachable but that can
|
|
|
|
// reach live nodes as live.
|
2003-01-23 22:05:33 +00:00
|
|
|
Graph.removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2002-11-11 00:01:34 +00:00
|
|
|
|
2004-02-21 00:30:28 +00:00
|
|
|
// When this graph is finalized, clone the globals in the graph into the
|
|
|
|
// globals graph to make sure it has everything, from all graphs.
|
|
|
|
DSScalarMap &MainSM = Graph.getScalarMap();
|
|
|
|
ReachabilityCloner RC(*GlobalsGraph, Graph, DSGraph::StripAllocaBit);
|
|
|
|
|
|
|
|
// Clone everything reachable from globals in the "main" graph into the
|
|
|
|
// globals graph.
|
|
|
|
for (DSScalarMap::global_iterator I = MainSM.global_begin(),
|
|
|
|
E = MainSM.global_end(); I != E; ++I)
|
|
|
|
RC.getClonedNH(MainSM[*I]);
|
|
|
|
|
2002-11-11 21:35:13 +00:00
|
|
|
//Graph.writeGraphToFile(std::cerr, "bu_" + F.getName());
|
2002-11-12 15:58:08 +00:00
|
|
|
}
|