2002-10-02 00:34:12 +02:00
|
|
|
//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
|
|
|
|
//
|
|
|
|
// This pass uses the data structure graphs to implement a simple context
|
|
|
|
// insensitive alias analysis. It does this by computing the local analysis
|
|
|
|
// graphs for all of the functions, then merging them together into a single big
|
|
|
|
// graph without cloning.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
2002-10-03 00:14:38 +02:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-10-02 00:34:12 +02:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Module.h"
|
2003-02-09 19:42:16 +01:00
|
|
|
#include "Support/Statistic.h"
|
2002-12-12 06:34:10 +01:00
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
namespace {
|
|
|
|
class Steens : public Pass, public AliasAnalysis {
|
|
|
|
DSGraph *ResultGraph;
|
2003-02-04 01:03:37 +01:00
|
|
|
DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
|
2002-10-02 00:34:12 +02:00
|
|
|
public:
|
|
|
|
Steens() : ResultGraph(0) {}
|
2003-02-13 22:44:18 +01:00
|
|
|
~Steens() {
|
|
|
|
releaseMyMemory();
|
|
|
|
assert(ResultGraph == 0 && "releaseMemory not called?");
|
|
|
|
}
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the Pass API
|
|
|
|
//
|
|
|
|
|
|
|
|
// run - Build up the result graph, representing the pointer graph for the
|
|
|
|
// program.
|
|
|
|
//
|
|
|
|
bool run(Module &M);
|
|
|
|
|
2003-02-13 22:44:18 +01:00
|
|
|
virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll(); // Does not transform code...
|
|
|
|
AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
|
|
|
|
AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
|
|
|
|
}
|
|
|
|
|
|
|
|
// print - Implement the Pass::print method...
|
|
|
|
void print(std::ostream &O, const Module *M) const {
|
|
|
|
assert(ResultGraph && "Result graph has not yet been computed!");
|
|
|
|
ResultGraph->writeGraphToFile(O, "steensgaards");
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
|
|
|
//
|
|
|
|
|
|
|
|
// alias - This is the only method here that does anything interesting...
|
2002-11-06 19:08:32 +01:00
|
|
|
Result alias(const Value *V1, const Value *V2);
|
2002-10-02 00:34:12 +02:00
|
|
|
|
2002-11-01 18:34:23 +01:00
|
|
|
/// canCallModify - Not implemented yet: FIXME
|
2002-10-02 00:34:12 +02:00
|
|
|
///
|
2002-11-06 19:08:32 +01:00
|
|
|
Result canCallModify(const CallInst &CI, const Value *Ptr) {
|
2002-10-02 00:34:12 +02:00
|
|
|
return MayAlias;
|
|
|
|
}
|
|
|
|
|
2002-11-01 18:34:23 +01:00
|
|
|
/// canInvokeModify - Not implemented yet: FIXME
|
2002-10-02 00:34:12 +02:00
|
|
|
///
|
2002-11-06 19:08:32 +01:00
|
|
|
Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
|
2002-11-01 18:34:23 +01:00
|
|
|
return MayAlias;
|
2002-10-02 00:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2002-10-20 20:07:37 +02:00
|
|
|
void ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
2002-10-02 00:34:12 +02:00
|
|
|
DSNodeHandle &RetVal);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the pass...
|
|
|
|
RegisterOpt<Steens> X("steens-aa",
|
2003-02-09 00:03:17 +01:00
|
|
|
"Steensgaard's alias analysis (DSGraph based)");
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
// Register as an implementation of AliasAnalysis
|
|
|
|
RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
|
|
|
|
/// with the specified call site descriptor. This function links the arguments
|
|
|
|
/// and the return value for the call site context-insensitively.
|
|
|
|
///
|
2003-02-04 01:03:37 +01:00
|
|
|
void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
|
2002-10-02 00:34:12 +02:00
|
|
|
DSNodeHandle &RetVal) {
|
|
|
|
assert(ResultGraph != 0 && "Result graph not allocated!");
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
|
2002-10-02 00:34:12 +02:00
|
|
|
|
2002-10-20 20:07:37 +02:00
|
|
|
// Handle the return value of the function...
|
2002-10-21 04:08:03 +02:00
|
|
|
if (Call.getRetVal().getNode() && RetVal.getNode())
|
|
|
|
RetVal.mergeWith(Call.getRetVal());
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
// Loop over all pointer arguments, resolving them to their provided pointers
|
2002-10-20 23:41:02 +02:00
|
|
|
unsigned PtrArgIdx = 0;
|
2003-02-10 19:16:19 +01:00
|
|
|
for (Function::aiterator AI = F->abegin(), AE = F->aend();
|
|
|
|
AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
|
2002-10-02 00:34:12 +02:00
|
|
|
if (I != ValMap.end()) // If its a pointer argument...
|
2003-02-04 01:03:37 +01:00
|
|
|
I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
|
2002-10-02 00:34:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// run - Build up the result graph, representing the pointer graph for the
|
|
|
|
/// program.
|
|
|
|
///
|
|
|
|
bool Steens::run(Module &M) {
|
|
|
|
assert(ResultGraph == 0 && "Result graph already allocated!");
|
|
|
|
LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
|
|
|
|
|
|
|
|
// Create a new, empty, graph...
|
|
|
|
ResultGraph = new DSGraph();
|
2003-02-04 01:03:37 +01:00
|
|
|
GlobalsGraph = new DSGraph();
|
|
|
|
ResultGraph->setGlobalsGraph(GlobalsGraph);
|
2003-02-09 20:26:47 +01:00
|
|
|
ResultGraph->setPrintAuxCalls();
|
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
// RetValMap - Keep track of the return values for all functions that return
|
|
|
|
// valid pointers.
|
|
|
|
//
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Function*, DSNodeHandle> RetValMap;
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
// Loop over the rest of the module, merging graphs for non-external functions
|
|
|
|
// into this graph.
|
|
|
|
//
|
2003-02-10 01:14:57 +01:00
|
|
|
unsigned Count = 0;
|
2002-10-02 00:34:12 +02:00
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Value*, DSNodeHandle> ValMap;
|
2002-10-02 00:34:12 +02:00
|
|
|
{ // Scope to free NodeMap memory ASAP
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<const DSNode*, DSNodeHandle> NodeMap;
|
2002-10-02 00:34:12 +02:00
|
|
|
const DSGraph &FDSG = LDS.getDSGraph(*I);
|
|
|
|
DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
|
|
|
|
|
|
|
|
// Keep track of the return node of the function's graph if it returns a
|
|
|
|
// value...
|
|
|
|
//
|
|
|
|
if (RetNode.getNode())
|
|
|
|
RetValMap[I] = RetNode;
|
|
|
|
}
|
|
|
|
|
2002-11-03 22:27:48 +01:00
|
|
|
// Incorporate the inlined Function's ScalarMap into the global
|
|
|
|
// ScalarMap...
|
2003-02-01 05:52:08 +01:00
|
|
|
hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
|
2003-02-04 01:03:37 +01:00
|
|
|
for (hash_map<Value*, DSNodeHandle>::iterator I = ValMap.begin(),
|
|
|
|
E = ValMap.end(); I != E; ++I)
|
|
|
|
GVM[I->first].mergeWith(I->second);
|
2003-02-10 01:14:57 +01:00
|
|
|
|
|
|
|
if ((++Count & 1) == 0) // Prune nodes out every other time...
|
|
|
|
ResultGraph->removeTriviallyDeadNodes();
|
2002-10-02 00:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Must recalculate and use the Incomplete markers!!
|
|
|
|
|
|
|
|
// Now that we have all of the graphs inlined, we can go about eliminating
|
|
|
|
// call nodes...
|
|
|
|
//
|
2002-10-20 20:07:37 +02:00
|
|
|
std::vector<DSCallSite> &Calls =
|
2002-11-08 22:27:37 +01:00
|
|
|
ResultGraph->getAuxFunctionCalls();
|
|
|
|
assert(Calls.empty() && "Aux call list is already in use??");
|
|
|
|
|
|
|
|
// Start with a copy of the original call sites...
|
|
|
|
Calls = ResultGraph->getFunctionCalls();
|
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
for (unsigned i = 0; i != Calls.size(); ) {
|
2002-10-20 20:07:37 +02:00
|
|
|
DSCallSite &CurCall = Calls[i];
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
// Loop over the called functions, eliminating as many as possible...
|
2003-02-05 22:59:58 +01:00
|
|
|
std::vector<GlobalValue*> CallTargets;
|
|
|
|
if (CurCall.isDirectCall())
|
|
|
|
CallTargets.push_back(CurCall.getCalleeFunc());
|
|
|
|
else
|
|
|
|
CallTargets = CurCall.getCalleeNode()->getGlobals();
|
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
for (unsigned c = 0; c != CallTargets.size(); ) {
|
|
|
|
// If we can eliminate this function call, do so!
|
|
|
|
bool Eliminated = false;
|
|
|
|
if (Function *F = dyn_cast<Function>(CallTargets[c]))
|
|
|
|
if (!F->isExternal()) {
|
|
|
|
ResolveFunctionCall(F, CurCall, RetValMap[F]);
|
|
|
|
Eliminated = true;
|
|
|
|
}
|
2003-02-10 01:14:57 +01:00
|
|
|
if (Eliminated) {
|
|
|
|
CallTargets[c] = CallTargets.back();
|
|
|
|
CallTargets.pop_back();
|
|
|
|
} else
|
2002-10-02 00:34:12 +02:00
|
|
|
++c; // Cannot eliminate this call, skip over it...
|
|
|
|
}
|
|
|
|
|
2003-02-10 01:14:57 +01:00
|
|
|
if (CallTargets.empty()) { // Eliminated all calls?
|
|
|
|
CurCall = Calls.back(); // Remove entry
|
|
|
|
Calls.pop_back();
|
|
|
|
} else
|
2002-10-02 00:34:12 +02:00
|
|
|
++i; // Skip this call site...
|
|
|
|
}
|
|
|
|
|
2003-02-12 00:11:51 +01:00
|
|
|
RetValMap.clear();
|
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
// Update the "incomplete" markers on the nodes, ignoring unknownness due to
|
|
|
|
// incoming arguments...
|
|
|
|
ResultGraph->maskIncompleteMarkers();
|
2003-01-23 23:05:33 +01:00
|
|
|
ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
|
2002-10-02 00:34:12 +02:00
|
|
|
|
|
|
|
// Remove any nodes that are dead after all of the merging we have done...
|
2003-02-04 01:03:37 +01:00
|
|
|
// FIXME: We should be able to disable the globals graph for steens!
|
2003-01-23 23:05:33 +01:00
|
|
|
ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
|
2002-10-02 00:34:12 +02:00
|
|
|
|
2003-02-09 19:42:16 +01:00
|
|
|
DEBUG(print(std::cerr, &M));
|
2002-10-02 00:34:12 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// alias - This is the only method here that does anything interesting...
|
2002-11-06 19:08:32 +01:00
|
|
|
AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
|
2002-12-12 06:34:10 +01:00
|
|
|
assert(ResultGraph && "Result graph has not been computed yet!");
|
2002-10-02 00:34:12 +02:00
|
|
|
|
2003-02-03 23:51:53 +01:00
|
|
|
hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
|
2002-10-02 00:34:12 +02:00
|
|
|
|
2003-02-03 23:51:53 +01:00
|
|
|
hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
|
|
|
|
if (I != GSM.end() && I->second.getNode()) {
|
2002-10-02 00:34:12 +02:00
|
|
|
DSNodeHandle &V1H = I->second;
|
2003-02-03 23:51:53 +01:00
|
|
|
hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
|
|
|
|
if (J != GSM.end() && J->second.getNode()) {
|
2002-10-02 00:34:12 +02:00
|
|
|
DSNodeHandle &V2H = J->second;
|
|
|
|
// If the two pointers point to different data structure graph nodes, they
|
|
|
|
// cannot alias!
|
2003-02-09 00:03:17 +01:00
|
|
|
if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
|
2002-10-02 00:34:12 +02:00
|
|
|
return NoAlias;
|
2003-02-09 00:03:17 +01:00
|
|
|
|
2002-10-02 00:34:12 +02:00
|
|
|
// FIXME: If the two pointers point to the same node, and the offsets are
|
|
|
|
// different, and the LinkIndex vector doesn't alias the section, then the
|
|
|
|
// two pointers do not alias. We need access size information for the two
|
|
|
|
// accesses though!
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we cannot determine alias properties based on our graph, fall back on
|
|
|
|
// some other AA implementation.
|
|
|
|
//
|
|
|
|
return getAnalysis<AliasAnalysis>().alias(V1, V2);
|
|
|
|
}
|