2002-10-03 20:38:41 +00:00
|
|
|
//===- BottomUpClosure.cpp - Compute bottom-up interprocedural closure ----===//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
2002-10-03 20:38:41 +00:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-07-18 00:12:30 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-10-01 22:33:50 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-07-18 00:12:30 +00:00
|
|
|
using std::map;
|
|
|
|
|
2002-07-26 21:12:44 +00:00
|
|
|
static RegisterAnalysis<BUDataStructures>
|
2002-07-30 22:05:22 +00:00
|
|
|
X("budatastructure", "Bottom-up Data Structure Analysis Closure");
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
// TODO: FIXME
|
|
|
|
namespace DataStructureAnalysis {
|
|
|
|
// isPointerType - Return true if this first class type is big enough to hold
|
|
|
|
// a pointer.
|
|
|
|
//
|
|
|
|
bool isPointerType(const Type *Ty);
|
|
|
|
}
|
|
|
|
using namespace DataStructureAnalysis;
|
|
|
|
|
|
|
|
|
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() {
|
2002-10-17 04:24:08 +00:00
|
|
|
// Delete all call site information
|
|
|
|
CallSites.clear();
|
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
for (map<const Function*, DSGraph*>::iterator I = DSInfo.begin(),
|
2002-07-18 00:12:30 +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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// run - Calculate the bottom up data structure graphs for each function in the
|
|
|
|
// program.
|
|
|
|
//
|
|
|
|
bool BUDataStructures::run(Module &M) {
|
|
|
|
// Simply calculate the graphs for each function...
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isExternal())
|
|
|
|
calculateGraph(*I);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveArguments - Resolve the formal and actual arguments for a function
|
|
|
|
// call.
|
|
|
|
//
|
2002-10-20 18:07:37 +00:00
|
|
|
static void ResolveArguments(DSCallSite &Call, Function &F,
|
2002-07-18 00:12:30 +00:00
|
|
|
map<Value*, DSNodeHandle> &ValueMap) {
|
|
|
|
// Resolve all of the function arguments...
|
|
|
|
Function::aiterator AI = F.abegin();
|
|
|
|
for (unsigned i = 2, e = Call.size(); i != e; ++i) {
|
|
|
|
// Advance the argument iterator to the first pointer argument...
|
2002-10-03 20:38:41 +00:00
|
|
|
while (!isPointerType(AI->getType())) ++AI;
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
// Add the link from the argument scalar to the provided value
|
2002-10-03 20:38:41 +00:00
|
|
|
DSNodeHandle &NN = ValueMap[AI];
|
|
|
|
NN.addEdgeTo(Call[i]);
|
2002-07-18 00:12:30 +00:00
|
|
|
++AI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DSGraph &BUDataStructures::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...
|
|
|
|
Graph = new DSGraph(getAnalysis<LocalDataStructures>().getDSGraph(F));
|
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
#if 0
|
2002-07-30 22:05:22 +00:00
|
|
|
// Populate the GlobalsGraph with globals from this one.
|
|
|
|
Graph->GlobalsGraph->cloneGlobals(*Graph, /*cloneCalls*/ false);
|
2002-10-03 20:38:41 +00:00
|
|
|
#endif
|
2002-07-30 22:05:22 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
// Start resolving calls...
|
2002-10-20 18:07:37 +00:00
|
|
|
std::vector<DSCallSite> &FCs = Graph->getFunctionCalls();
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
DEBUG(std::cerr << " [BU] Inlining: " << F.getName() << "\n");
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
bool Inlined;
|
|
|
|
do {
|
|
|
|
Inlined = false;
|
2002-07-30 22:05:22 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
for (unsigned i = 0; i != FCs.size(); ++i) {
|
|
|
|
// Copy the call, because inlining graphs may invalidate the FCs vector.
|
2002-10-20 18:07:37 +00:00
|
|
|
DSCallSite Call = FCs[i];
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
// If the function list is complete...
|
2002-10-20 18:07:37 +00:00
|
|
|
if ((Call.getCalleeNode().getNode()->NodeType & DSNode::Incomplete)==0) {
|
2002-07-18 00:12:30 +00:00
|
|
|
// Start inlining all of the functions we can... some may not be
|
|
|
|
// inlinable if they are external...
|
|
|
|
//
|
2002-10-20 18:07:37 +00:00
|
|
|
std::vector<GlobalValue*> Callees(Call.getCalleeNode().getNode()->getGlobals());
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
// Loop over the functions, inlining whatever we can...
|
2002-07-30 22:05:22 +00:00
|
|
|
for (unsigned c = 0; c != Callees.size(); ++c) {
|
2002-07-18 00:12:30 +00:00
|
|
|
// Must be a function type, so this cast MUST succeed.
|
2002-07-30 22:05:22 +00:00
|
|
|
Function &FI = cast<Function>(*Callees[c]);
|
2002-10-17 04:24:08 +00:00
|
|
|
|
|
|
|
// Record that this is a call site of FI.
|
2002-10-20 18:07:37 +00:00
|
|
|
assert(&Call.getCaller() == &F && "Invalid caller in DSCallSite?");
|
|
|
|
CallSites[&FI].push_back(DSCallSite(Call));
|
2002-10-17 04:24:08 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
if (&FI == &F) {
|
|
|
|
// Self recursion... simply link up the formal arguments with the
|
|
|
|
// actual arguments...
|
2002-07-30 22:05:22 +00:00
|
|
|
|
|
|
|
DEBUG(std::cerr << "\t[BU] Self Inlining: " << F.getName() << "\n");
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-10-20 18:07:37 +00:00
|
|
|
if (Call.getReturnValueNode().getNode()) // Handle the return value if present...
|
|
|
|
Graph->getRetNode().mergeWith(Call.getReturnValueNode());
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
// Resolve the arguments in the call to the actual values...
|
|
|
|
ResolveArguments(Call, F, Graph->getValueMap());
|
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
// Erase the entry in the callees vector
|
|
|
|
Callees.erase(Callees.begin()+c--);
|
2002-10-17 04:24:08 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
} else if (!FI.isExternal()) {
|
2002-07-30 22:05:22 +00:00
|
|
|
DEBUG(std::cerr << "\t[BU] In " << F.getName() << " inlining: "
|
2002-07-18 00:12:30 +00:00
|
|
|
<< FI.getName() << "\n");
|
2002-07-18 16:13:52 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
// Get the data structure graph for the called function, closing it
|
|
|
|
// if possible (which is only impossible in the case of mutual
|
|
|
|
// recursion...
|
|
|
|
//
|
|
|
|
DSGraph &GI = calculateGraph(FI); // Graph to inline
|
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
DEBUG(std::cerr << "\t\t[BU] Got graph for " << FI.getName()
|
|
|
|
<< " in: " << F.getName() << "\n");
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-07-18 16:13:52 +00:00
|
|
|
// Clone the callee's graph into the current graph, keeping
|
2002-07-30 22:05:22 +00:00
|
|
|
// track of where scalars in the old graph _used_ to point,
|
|
|
|
// and of the new nodes matching nodes of the old graph.
|
2002-10-03 20:38:41 +00:00
|
|
|
map<Value*, DSNodeHandle> OldValMap;
|
|
|
|
map<const DSNode*, DSNode*> OldNodeMap;
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
// The clone call may invalidate any of the vectors in the data
|
2002-07-30 22:05:22 +00:00
|
|
|
// structure graph. Strip locals and don't copy the list of callers
|
2002-10-03 20:38:41 +00:00
|
|
|
DSNodeHandle RetVal = Graph->cloneInto(GI, OldValMap, OldNodeMap,
|
|
|
|
/*StripScalars*/ true,
|
|
|
|
/*StripAllocas*/ true,
|
|
|
|
/*CopyCallers*/ false,
|
|
|
|
/*CopyOrigCalls*/ false);
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
// Resolve the arguments in the call to the actual values...
|
2002-07-18 00:12:30 +00:00
|
|
|
ResolveArguments(Call, FI, OldValMap);
|
|
|
|
|
2002-10-20 18:07:37 +00:00
|
|
|
if (Call.getReturnValueNode().getNode()) // Handle the return value if present
|
|
|
|
RetVal.mergeWith(Call.getReturnValueNode());
|
2002-07-30 22:05:22 +00:00
|
|
|
|
|
|
|
// Erase the entry in the Callees vector
|
|
|
|
Callees.erase(Callees.begin()+c--);
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-07-19 18:11:43 +00:00
|
|
|
} else if (FI.getName() == "printf" || FI.getName() == "sscanf" ||
|
|
|
|
FI.getName() == "fprintf" || FI.getName() == "open" ||
|
|
|
|
FI.getName() == "sprintf") {
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
// Erase the entry in the globals vector
|
2002-07-30 22:05:22 +00:00
|
|
|
Callees.erase(Callees.begin()+c--);
|
2002-07-18 00:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
if (Callees.empty()) { // Inlined all of the function calls?
|
2002-07-18 00:12:30 +00:00
|
|
|
// Erase the call if it is resolvable...
|
|
|
|
FCs.erase(FCs.begin()+i--); // Don't skip a the next call...
|
|
|
|
Inlined = true;
|
2002-10-20 18:07:37 +00:00
|
|
|
} else if (Callees.size() != Call.getCalleeNode().getNode()->getGlobals().size()) {
|
2002-07-18 00:12:30 +00:00
|
|
|
// Was able to inline SOME, but not all of the functions. Construct a
|
|
|
|
// new global node here.
|
|
|
|
//
|
|
|
|
assert(0 && "Unimpl!");
|
|
|
|
Inlined = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recompute the Incomplete markers. If there are any function calls left
|
|
|
|
// now that are complete, we must loop!
|
|
|
|
if (Inlined) {
|
|
|
|
Graph->maskIncompleteMarkers();
|
|
|
|
Graph->markIncompleteNodes();
|
2002-10-03 20:38:41 +00:00
|
|
|
Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
|
2002-07-18 00:12:30 +00:00
|
|
|
}
|
|
|
|
} while (Inlined && !FCs.empty());
|
|
|
|
|
2002-07-30 22:05:22 +00:00
|
|
|
Graph->maskIncompleteMarkers();
|
|
|
|
Graph->markIncompleteNodes();
|
2002-10-03 21:55:28 +00:00
|
|
|
Graph->removeTriviallyDeadNodes(false);
|
2002-10-03 20:38:41 +00:00
|
|
|
Graph->removeDeadNodes(/*KeepAllGlobals*/ true, /*KeepCalls*/ true);
|
2002-07-30 22:05:22 +00:00
|
|
|
|
2002-08-07 21:41:11 +00:00
|
|
|
DEBUG(std::cerr << " [BU] Done inlining: " << F.getName() << " ["
|
|
|
|
<< Graph->getGraphSize() << "+" << Graph->getFunctionCalls().size()
|
|
|
|
<< "]\n");
|
2002-07-30 22:05:22 +00:00
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
return *Graph;
|
|
|
|
}
|