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"
12 #include "Support/Statistic.h"
15 Statistic<> NumNoAlias ("ds-aa", "Number of 'no alias' replies");
16 Statistic<> NumMayAlias ("ds-aa", "Number of 'may alias' replies");
20 class DSAA : public Pass, public AliasAnalysis {
25 //------------------------------------------------
26 // Implement the Pass API
29 // run - Build up the result graph, representing the pointer graph for the
33 TD = &getAnalysis<TDDataStructures>();
37 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38 AU.setPreservesAll(); // Does not transform code...
39 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
40 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
43 //------------------------------------------------
44 // Implement the AliasAnalysis API
47 // alias - This is the only method here that does anything interesting...
48 Result alias(const Value *V1, const Value *V2);
50 /// canCallModify - Not implemented yet: FIXME
52 Result canCallModify(const CallInst &CI, const Value *Ptr) {
56 /// canInvokeModify - Not implemented yet: FIXME
58 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
63 // Register the pass...
64 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
66 // Register as an implementation of AliasAnalysis
67 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
70 // getValueFunction - return the function containing the specified value if
71 // available, or null otherwise.
73 static const Function *getValueFunction(const Value *V) {
74 if (const Instruction *I = dyn_cast<Instruction>(V))
75 return I->getParent()->getParent();
76 else if (const Argument *A = dyn_cast<Argument>(V))
77 return A->getParent();
78 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
79 return BB->getParent();
83 // alias - This is the only method here that does anything interesting...
84 AliasAnalysis::Result DSAA::alias(const Value *V1, const Value *V2) {
85 const Function *F1 = getValueFunction(V1);
86 const Function *F2 = getValueFunction(V2);
87 assert((!F1 || !F2 || F1 == F2) && "Alias query for 2 different functions?");
90 // FIXME: This can return must alias if querying a DSNode for a global value
91 // where the node has only the G composition bit set, and only one entry in
92 // the globals list...
95 // Get the graph for a function...
96 DSGraph &G = TD->getDSGraph(*F1);
97 hash_map<Value*, DSNodeHandle> &GSM = G.getScalarMap();
98 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find((Value*)V1);
100 assert(I->second.getNode() && "Scalar map points to null node?");
101 hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2);
102 if (J != GSM.end()) {
103 assert(J->second.getNode() && "Scalar map points to null node?");
104 if (I->second.getNode() != J->second.getNode()) {
105 // Return noalias if one of the nodes is complete...
106 if ((~I->second.getNode()->NodeType | ~J->second.getNode()->NodeType)
107 & DSNode::Incomplete) {
111 // both are incomplete, they may alias...
113 // Both point to the same node, see if they point to different
114 // offsets... FIXME: This needs to know the size of the alias query
115 if (I->second.getOffset() != J->second.getOffset()) {
124 // FIXME: we could improve on this by checking the globals graph for aliased
127 return getAnalysis<AliasAnalysis>().alias(V1, V2);