Initial implementation of ds-aa
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureAA.cpp
1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
2 //
3 // This pass uses the top-down data structure graphs to implement a simple
4 // context sensitive alias analysis.
5 //
6 //===----------------------------------------------------------------------===//
7
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"
13
14 namespace {
15   Statistic<> NumNoAlias  ("ds-aa", "Number of 'no alias' replies");
16   Statistic<> NumMayAlias ("ds-aa", "Number of 'may alias' replies");
17 };
18
19 namespace {
20   class DSAA : public Pass, public AliasAnalysis {
21     TDDataStructures *TD;
22   public:
23     DSAA() : TD(0) {}
24
25     //------------------------------------------------
26     // Implement the Pass API
27     //
28
29     // run - Build up the result graph, representing the pointer graph for the
30     // program.
31     //
32     bool run(Module &M) {
33       TD = &getAnalysis<TDDataStructures>();
34       return false;
35     }
36
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...
41     }
42
43     //------------------------------------------------
44     // Implement the AliasAnalysis API
45     //  
46
47     // alias - This is the only method here that does anything interesting...
48     Result alias(const Value *V1, const Value *V2);
49     
50     /// canCallModify - Not implemented yet: FIXME
51     ///
52     Result canCallModify(const CallInst &CI, const Value *Ptr) {
53       return MayAlias;
54     }
55     
56     /// canInvokeModify - Not implemented yet: FIXME
57     ///
58     Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
59       return MayAlias;
60     }
61   };
62
63   // Register the pass...
64   RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
65
66   // Register as an implementation of AliasAnalysis
67   RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
68 }
69
70 // getValueFunction - return the function containing the specified value if
71 // available, or null otherwise.
72 //
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();
80   return 0;
81 }
82
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?");
88   
89
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...
93   if (F2) F1 = F2;
94   if (F1) {
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);
99     if (I != GSM.end()) {
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) {
108             ++NumNoAlias;
109             return NoAlias;
110           }
111           // both are incomplete, they may alias...
112         } else {
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()) {
116             ++NumNoAlias;
117             return NoAlias;
118           }
119         }
120       }
121     }
122   }
123
124   // FIXME: we could improve on this by checking the globals graph for aliased
125   // global queries...
126   ++NumMayAlias;
127   return getAnalysis<AliasAnalysis>().alias(V1, V2);
128 }