Make error messages more useful than jsut an abort
[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
13 namespace {
14   class DSAA : public Pass, public AliasAnalysis {
15     TDDataStructures *TD;
16   public:
17     DSAA() : TD(0) {}
18
19     //------------------------------------------------
20     // Implement the Pass API
21     //
22
23     // run - Build up the result graph, representing the pointer graph for the
24     // program.
25     //
26     bool run(Module &M) {
27       InitializeAliasAnalysis(this);
28       TD = &getAnalysis<TDDataStructures>();
29       return false;
30     }
31
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...
37     }
38
39     //------------------------------------------------
40     // Implement the AliasAnalysis API
41     //  
42
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);
46   };
47
48   // Register the pass...
49   RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
50
51   // Register as an implementation of AliasAnalysis
52   RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
53 }
54
55 // getValueFunction - return the function containing the specified value if
56 // available, or null otherwise.
57 //
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();
65   return 0;
66 }
67
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?");
75   
76
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...
80   if (F2) F1 = F2;
81   if (F1) {
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);
86     if (I != GSM.end()) {
87       assert(I->second.getNode() && "Scalar map points to null node?");
88       hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2);
89       if (J != GSM.end()) {
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)
94               & DSNode::Incomplete)
95             return NoAlias;
96           // both are incomplete, they may alias...
97         } else {
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())
101             return NoAlias;
102         }
103       }
104     }
105   }
106
107   // FIXME: we could improve on this by checking the globals graph for aliased
108   // global queries...
109   return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
110 }