2003-02-03 23:50:46 +01:00
|
|
|
//===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
|
2003-10-20 21:43:21 +02: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-02-03 23:50:46 +01:00
|
|
|
//
|
|
|
|
// This pass uses the top-down data structure graphs to implement a simple
|
|
|
|
// context sensitive alias analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-03-17 21:33:27 +01:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-03-12 07:14:22 +01:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2005-01-09 21:42:52 +01:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2004-07-07 08:32:21 +02:00
|
|
|
#include "llvm/Analysis/DataStructure/DataStructure.h"
|
|
|
|
#include "llvm/Analysis/DataStructure/DSGraph.h"
|
2003-11-13 00:11:14 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2003-02-03 23:50:46 +01:00
|
|
|
namespace {
|
2004-09-20 06:48:05 +02:00
|
|
|
class DSAA : public ModulePass, public AliasAnalysis {
|
2003-02-03 23:50:46 +01:00
|
|
|
TDDataStructures *TD;
|
2004-03-12 07:14:22 +01:00
|
|
|
BUDataStructures *BU;
|
2003-02-03 23:50:46 +01:00
|
|
|
public:
|
|
|
|
DSAA() : TD(0) {}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the Pass API
|
|
|
|
//
|
|
|
|
|
|
|
|
// run - Build up the result graph, representing the pointer graph for the
|
|
|
|
// program.
|
|
|
|
//
|
2004-09-20 06:48:05 +02:00
|
|
|
bool runOnModule(Module &M) {
|
2003-02-26 20:29:36 +01:00
|
|
|
InitializeAliasAnalysis(this);
|
2003-02-03 23:50:46 +01:00
|
|
|
TD = &getAnalysis<TDDataStructures>();
|
2004-03-12 07:14:22 +01:00
|
|
|
BU = &getAnalysis<BUDataStructures>();
|
2003-02-03 23:50:46 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2003-02-26 20:29:36 +01:00
|
|
|
AliasAnalysis::getAnalysisUsage(AU);
|
2004-03-12 07:14:22 +01:00
|
|
|
AU.setPreservesAll(); // Does not transform code
|
|
|
|
AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
|
|
|
|
AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
|
2003-02-03 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
// Implement the AliasAnalysis API
|
|
|
|
//
|
|
|
|
|
2003-02-26 20:29:36 +01:00
|
|
|
AliasResult alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size);
|
2003-06-29 02:54:08 +02:00
|
|
|
|
|
|
|
void getMustAliases(Value *P, std::vector<Value*> &RetVals);
|
|
|
|
|
2004-05-23 23:14:27 +02:00
|
|
|
ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
|
2004-12-07 09:11:24 +01:00
|
|
|
ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
|
|
|
|
return AliasAnalysis::getModRefInfo(CS1,CS2);
|
|
|
|
}
|
2004-01-30 23:20:55 +01:00
|
|
|
|
2005-01-24 21:00:14 +01:00
|
|
|
virtual void deleteValue(Value *V) {
|
|
|
|
BU->deleteValue(V);
|
|
|
|
TD->deleteValue(V);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void copyValue(Value *From, Value *To) {
|
|
|
|
if (From == To) return;
|
|
|
|
BU->copyValue(From, To);
|
|
|
|
TD->copyValue(From, To);
|
|
|
|
}
|
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
private:
|
|
|
|
DSGraph *getGraphForValue(const Value *V);
|
2003-02-03 23:50:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Register the pass...
|
|
|
|
RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
|
|
|
|
|
|
|
|
// Register as an implementation of AliasAnalysis
|
|
|
|
RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
|
|
|
|
}
|
|
|
|
|
2005-01-09 21:42:52 +01:00
|
|
|
ModulePass *llvm::createDSAAPass() { return new DSAA(); }
|
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
// getGraphForValue - Return the DSGraph to use for queries about the specified
|
|
|
|
// value...
|
2003-02-03 23:50:46 +01:00
|
|
|
//
|
2003-06-29 02:54:08 +02:00
|
|
|
DSGraph *DSAA::getGraphForValue(const Value *V) {
|
2003-02-03 23:50:46 +01:00
|
|
|
if (const Instruction *I = dyn_cast<Instruction>(V))
|
2003-06-29 02:54:08 +02:00
|
|
|
return &TD->getDSGraph(*I->getParent()->getParent());
|
2003-02-03 23:50:46 +01:00
|
|
|
else if (const Argument *A = dyn_cast<Argument>(V))
|
2003-06-29 02:54:08 +02:00
|
|
|
return &TD->getDSGraph(*A->getParent());
|
2003-02-03 23:50:46 +01:00
|
|
|
else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
|
2003-06-29 02:54:08 +02:00
|
|
|
return &TD->getDSGraph(*BB->getParent());
|
2003-02-03 23:50:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
// isSinglePhysicalObject - For now, the only case that we know that there is
|
|
|
|
// only one memory object in the node is when there is a single global in the
|
|
|
|
// node, and the only composition bit set is Global.
|
|
|
|
//
|
|
|
|
static bool isSinglePhysicalObject(DSNode *N) {
|
|
|
|
assert(N->isComplete() && "Can only tell if this is a complete object!");
|
|
|
|
return N->isGlobalNode() && N->getGlobals().size() == 1 &&
|
|
|
|
!N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
|
|
|
|
}
|
|
|
|
|
2003-02-03 23:50:46 +01:00
|
|
|
// alias - This is the only method here that does anything interesting...
|
2003-02-26 20:29:36 +01:00
|
|
|
AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
|
|
|
|
const Value *V2, unsigned V2Size) {
|
2003-06-19 23:15:11 +02:00
|
|
|
if (V1 == V2) return MustAlias;
|
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
DSGraph *G1 = getGraphForValue(V1);
|
|
|
|
DSGraph *G2 = getGraphForValue(V2);
|
|
|
|
assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
|
2003-02-03 23:50:46 +01:00
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
// Get the graph to use...
|
|
|
|
DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
|
|
|
|
|
|
|
|
const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
|
|
|
|
DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
|
2004-04-26 16:44:08 +02:00
|
|
|
if (I == GSM.end()) return NoAlias;
|
2005-03-18 01:21:03 +01:00
|
|
|
|
2004-04-26 16:44:08 +02:00
|
|
|
DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
|
|
|
|
if (J == GSM.end()) return NoAlias;
|
|
|
|
|
|
|
|
DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
|
|
|
|
unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
|
2005-03-18 01:21:03 +01:00
|
|
|
if (N1 == 0 || N2 == 0)
|
|
|
|
return MayAlias; // Can't tell whether anything aliases null.
|
2003-06-19 23:15:11 +02:00
|
|
|
|
2004-04-26 16:44:08 +02:00
|
|
|
// We can only make a judgment of one of the nodes is complete...
|
|
|
|
if (N1->isComplete() || N2->isComplete()) {
|
|
|
|
if (N1 != N2)
|
|
|
|
return NoAlias; // Completely different nodes.
|
2003-06-29 02:54:08 +02:00
|
|
|
|
2003-07-03 01:56:51 +02:00
|
|
|
#if 0 // This does not correctly handle arrays!
|
2004-04-26 16:44:08 +02:00
|
|
|
// Both point to the same node and same offset, and there is only one
|
|
|
|
// physical memory object represented in the node, return must alias.
|
|
|
|
//
|
|
|
|
// FIXME: This isn't correct because we do not handle array indexing
|
|
|
|
// correctly.
|
|
|
|
|
|
|
|
if (O1 == O2 && isSinglePhysicalObject(N1))
|
|
|
|
return MustAlias; // Exactly the same object & offset
|
2003-07-03 01:56:51 +02:00
|
|
|
#endif
|
2003-06-29 02:54:08 +02:00
|
|
|
|
2004-04-26 16:44:08 +02:00
|
|
|
// See if they point to different offsets... if so, we may be able to
|
|
|
|
// determine that they do not alias...
|
|
|
|
if (O1 != O2) {
|
|
|
|
if (O2 < O1) { // Ensure that O1 <= O2
|
|
|
|
std::swap(V1, V2);
|
|
|
|
std::swap(O1, O2);
|
|
|
|
std::swap(V1Size, V2Size);
|
2003-02-03 23:50:46 +01:00
|
|
|
}
|
2004-04-26 16:44:08 +02:00
|
|
|
|
|
|
|
// FIXME: This is not correct because we do not handle array
|
|
|
|
// indexing correctly with this check!
|
|
|
|
//if (O1+V1Size <= O2) return NoAlias;
|
2003-02-03 23:50:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: we could improve on this by checking the globals graph for aliased
|
|
|
|
// global queries...
|
2004-05-23 23:14:27 +02:00
|
|
|
return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
|
2003-02-03 23:50:46 +01:00
|
|
|
}
|
2003-06-29 02:54:08 +02:00
|
|
|
|
2004-03-12 07:14:22 +01:00
|
|
|
/// getModRefInfo - does a callsite modify or reference a value?
|
|
|
|
///
|
|
|
|
AliasAnalysis::ModRefResult
|
|
|
|
DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
|
2005-03-17 21:16:58 +01:00
|
|
|
AliasAnalysis::ModRefResult Result =AliasAnalysis::getModRefInfo(CS, P, Size);
|
2004-03-12 07:14:22 +01:00
|
|
|
Function *F = CS.getCalledFunction();
|
2005-03-17 21:16:58 +01:00
|
|
|
|
|
|
|
if (!F || F->isExternal() || Result == NoModRef)
|
|
|
|
return Result;
|
2004-03-12 07:14:22 +01:00
|
|
|
|
2005-03-18 01:21:03 +01:00
|
|
|
// Get the graphs for the callee and caller. Note that we want the BU graph
|
|
|
|
// for the callee because we don't want all caller's effects incorporated!
|
|
|
|
const Function *Caller = CS.getInstruction()->getParent()->getParent();
|
|
|
|
DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
|
|
|
|
DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
|
|
|
|
|
|
|
|
// Figure out which node in the TD graph this pointer corresponds to.
|
|
|
|
DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
|
|
|
|
DSScalarMap::iterator NI = CallerSM.find(P);
|
|
|
|
if (NI == CallerSM.end()) {
|
2005-03-17 21:33:27 +01:00
|
|
|
if (isa<ConstantPointerNull>(P))
|
2005-03-18 01:21:03 +01:00
|
|
|
Result = NoModRef; // null is never modified :)
|
|
|
|
else {
|
2005-03-17 21:33:27 +01:00
|
|
|
assert(isa<GlobalVariable>(P) &&
|
|
|
|
cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
|
|
|
|
"This isn't a global that DSA inconsiderately dropped "
|
|
|
|
"from the graph?");
|
2005-03-18 01:21:03 +01:00
|
|
|
}
|
|
|
|
return Result;
|
2005-03-17 21:16:58 +01:00
|
|
|
}
|
2005-03-18 01:21:03 +01:00
|
|
|
|
|
|
|
const DSNode *N = NI->second.getNode();
|
|
|
|
assert(N && "Null pointer in scalar map??");
|
|
|
|
|
|
|
|
// Compute the mapping from nodes in the callee graph to the nodes in the
|
|
|
|
// caller graph for this call site.
|
|
|
|
DSGraph::NodeMapTy CalleeCallerMap;
|
|
|
|
DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
|
|
|
|
CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
|
|
|
|
CalleeCallerMap);
|
|
|
|
|
|
|
|
// Loop over all of the nodes in the callee that correspond to "N", keeping
|
|
|
|
// track of aggregate mod/ref info.
|
|
|
|
bool NeverReads = true, NeverWrites = true;
|
|
|
|
for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
|
|
|
|
E = CalleeCallerMap.end(); I != E; ++I)
|
|
|
|
if (I->second.getNode() == N) {
|
|
|
|
if (I->first->isModified())
|
|
|
|
NeverWrites = false;
|
|
|
|
if (I->first->isRead())
|
|
|
|
NeverReads = false;
|
|
|
|
if (NeverReads == false && NeverWrites == false)
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NeverWrites) // We proved it was not modified.
|
|
|
|
Result = ModRefResult(Result & ~Mod);
|
|
|
|
if (NeverReads) // We proved it was not read.
|
|
|
|
Result = ModRefResult(Result & ~Ref);
|
|
|
|
|
2005-03-17 21:16:58 +01:00
|
|
|
return Result;
|
2004-03-12 07:14:22 +01:00
|
|
|
}
|
|
|
|
|
2003-06-29 02:54:08 +02:00
|
|
|
|
|
|
|
/// getMustAliases - If there are any pointers known that must alias this
|
|
|
|
/// pointer, return them now. This allows alias-set based alias analyses to
|
|
|
|
/// perform a form a value numbering (which is exposed by load-vn). If an alias
|
|
|
|
/// analysis supports this, it should ADD any must aliased pointers to the
|
|
|
|
/// specified vector.
|
|
|
|
///
|
|
|
|
void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
|
2003-07-03 01:56:51 +02:00
|
|
|
#if 0 // This does not correctly handle arrays!
|
2003-07-02 06:39:13 +02:00
|
|
|
// Currently the only must alias information we can provide is to say that
|
|
|
|
// something is equal to a global value. If we already have a global value,
|
|
|
|
// don't get worked up about it.
|
|
|
|
if (!isa<GlobalValue>(P)) {
|
|
|
|
DSGraph *G = getGraphForValue(P);
|
|
|
|
if (!G) G = &TD->getGlobalsGraph();
|
|
|
|
|
|
|
|
// The only must alias information we can currently determine occurs when
|
|
|
|
// the node for P is a global node with only one entry.
|
2004-01-27 22:51:19 +01:00
|
|
|
DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
|
|
|
|
if (I != G->getScalarMap().end()) {
|
2003-07-02 06:39:13 +02:00
|
|
|
DSNode *N = I->second.getNode();
|
|
|
|
if (N->isComplete() && isSinglePhysicalObject(N))
|
|
|
|
RetVals.push_back(N->getGlobals()[0]);
|
|
|
|
}
|
2003-06-29 02:54:08 +02:00
|
|
|
}
|
2003-07-03 01:56:51 +02:00
|
|
|
#endif
|
2004-05-23 23:14:27 +02:00
|
|
|
return AliasAnalysis::getMustAliases(P, RetVals);
|
2003-06-29 02:54:08 +02:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|