From 8105cfbed0b4e51b6d7d62ad3e84885e27451cf3 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 3 Feb 2003 22:50:46 +0000 Subject: [PATCH] Initial implementation of ds-aa git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5484 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../DataStructure/DataStructureAA.cpp | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 lib/Analysis/DataStructure/DataStructureAA.cpp diff --git a/lib/Analysis/DataStructure/DataStructureAA.cpp b/lib/Analysis/DataStructure/DataStructureAA.cpp new file mode 100644 index 00000000000..786ed168f0c --- /dev/null +++ b/lib/Analysis/DataStructure/DataStructureAA.cpp @@ -0,0 +1,128 @@ +//===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===// +// +// This pass uses the top-down data structure graphs to implement a simple +// context sensitive alias analysis. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/DataStructure.h" +#include "llvm/Analysis/DSGraph.h" +#include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Module.h" +#include "Support/Statistic.h" + +namespace { + Statistic<> NumNoAlias ("ds-aa", "Number of 'no alias' replies"); + Statistic<> NumMayAlias ("ds-aa", "Number of 'may alias' replies"); +}; + +namespace { + class DSAA : public Pass, public AliasAnalysis { + TDDataStructures *TD; + public: + DSAA() : TD(0) {} + + //------------------------------------------------ + // Implement the Pass API + // + + // run - Build up the result graph, representing the pointer graph for the + // program. + // + bool run(Module &M) { + TD = &getAnalysis(); + return false; + } + + virtual void getAnalysisUsage(AnalysisUsage &AU) const { + AU.setPreservesAll(); // Does not transform code... + AU.addRequired(); // Uses TD Datastructures + AU.addRequired(); // Chains to another AA impl... + } + + //------------------------------------------------ + // Implement the AliasAnalysis API + // + + // alias - This is the only method here that does anything interesting... + Result alias(const Value *V1, const Value *V2); + + /// canCallModify - Not implemented yet: FIXME + /// + Result canCallModify(const CallInst &CI, const Value *Ptr) { + return MayAlias; + } + + /// canInvokeModify - Not implemented yet: FIXME + /// + Result canInvokeModify(const InvokeInst &I, const Value *Ptr) { + return MayAlias; + } + }; + + // Register the pass... + RegisterOpt X("ds-aa", "Data Structure Graph Based Alias Analysis"); + + // Register as an implementation of AliasAnalysis + RegisterAnalysisGroup Y; +} + +// getValueFunction - return the function containing the specified value if +// available, or null otherwise. +// +static const Function *getValueFunction(const Value *V) { + if (const Instruction *I = dyn_cast(V)) + return I->getParent()->getParent(); + else if (const Argument *A = dyn_cast(V)) + return A->getParent(); + else if (const BasicBlock *BB = dyn_cast(V)) + return BB->getParent(); + return 0; +} + +// alias - This is the only method here that does anything interesting... +AliasAnalysis::Result DSAA::alias(const Value *V1, const Value *V2) { + const Function *F1 = getValueFunction(V1); + const Function *F2 = getValueFunction(V2); + assert((!F1 || !F2 || F1 == F2) && "Alias query for 2 different functions?"); + + + // FIXME: This can return must alias if querying a DSNode for a global value + // where the node has only the G composition bit set, and only one entry in + // the globals list... + if (F2) F1 = F2; + if (F1) { + // Get the graph for a function... + DSGraph &G = TD->getDSGraph(*F1); + hash_map &GSM = G.getScalarMap(); + hash_map::iterator I = GSM.find((Value*)V1); + if (I != GSM.end()) { + assert(I->second.getNode() && "Scalar map points to null node?"); + hash_map::iterator J = GSM.find((Value*)V2); + if (J != GSM.end()) { + assert(J->second.getNode() && "Scalar map points to null node?"); + if (I->second.getNode() != J->second.getNode()) { + // Return noalias if one of the nodes is complete... + if ((~I->second.getNode()->NodeType | ~J->second.getNode()->NodeType) + & DSNode::Incomplete) { + ++NumNoAlias; + return NoAlias; + } + // both are incomplete, they may alias... + } else { + // Both point to the same node, see if they point to different + // offsets... FIXME: This needs to know the size of the alias query + if (I->second.getOffset() != J->second.getOffset()) { + ++NumNoAlias; + return NoAlias; + } + } + } + } + } + + // FIXME: we could improve on this by checking the globals graph for aliased + // global queries... + ++NumMayAlias; + return getAnalysis().alias(V1, V2); +} -- 2.34.1