1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
3 // This pass uses the top-down data structure graphs to implement a simple
4 // context sensitive alias analysis.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/DataStructure.h"
9 #include "llvm/Analysis/DSGraph.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Module.h"
14 class DSAA : public Pass, public AliasAnalysis {
19 //------------------------------------------------
20 // Implement the Pass API
23 // run - Build up the result graph, representing the pointer graph for the
27 InitializeAliasAnalysis(this);
28 TD = &getAnalysis<TDDataStructures>();
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
33 AliasAnalysis::getAnalysisUsage(AU);
34 AU.setPreservesAll(); // Does not transform code...
35 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
36 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
39 //------------------------------------------------
40 // Implement the AliasAnalysis API
43 // alias - This is the only method here that does anything interesting...
44 AliasResult alias(const Value *V1, unsigned V1Size,
45 const Value *V2, unsigned V2Size);
48 // Register the pass...
49 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
51 // Register as an implementation of AliasAnalysis
52 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
55 // getValueFunction - return the function containing the specified value if
56 // available, or null otherwise.
58 static const Function *getValueFunction(const Value *V) {
59 if (const Instruction *I = dyn_cast<Instruction>(V))
60 return I->getParent()->getParent();
61 else if (const Argument *A = dyn_cast<Argument>(V))
62 return A->getParent();
63 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
64 return BB->getParent();
68 // alias - This is the only method here that does anything interesting...
69 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
70 const Value *V2, unsigned V2Size) {
71 // FIXME: This should handle the Size argument as well!
72 const Function *F1 = getValueFunction(V1);
73 const Function *F2 = getValueFunction(V2);
74 assert((!F1 || !F2 || F1 == F2) && "Alias query for 2 different functions?");
77 // FIXME: This can return must alias if querying a DSNode for a global value
78 // where the node has only the G composition bit set, and only one entry in
79 // the globals list...
82 // Get the graph for a function...
83 DSGraph &G = TD->getDSGraph(*F1);
84 hash_map<Value*, DSNodeHandle> &GSM = G.getScalarMap();
85 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find((Value*)V1);
87 assert(I->second.getNode() && "Scalar map points to null node?");
88 hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2);
90 assert(J->second.getNode() && "Scalar map points to null node?");
91 if (I->second.getNode() != J->second.getNode()) {
92 // Return noalias if one of the nodes is complete...
93 if ((~I->second.getNode()->NodeType | ~J->second.getNode()->NodeType)
96 // both are incomplete, they may alias...
98 // Both point to the same node, see if they point to different
99 // offsets... FIXME: This needs to know the size of the alias query
100 if (I->second.getOffset() != J->second.getOffset())
107 // FIXME: we could improve on this by checking the globals graph for aliased
109 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);