38f3a11372d2ed5a9b9b699523bce8edd318210c
[oota-llvm.git] / lib / Analysis / TypeBasedAliasAnalysis.cpp
1 //===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the TypeBasedAliasAnalysis pass, which implements
11 // metadata-based TBAA.
12 //
13 // In LLVM IR, memory does not have types, so LLVM's own type system is not
14 // suitable for doing TBAA. Instead, metadata is added to the IR to describe
15 // a type system of a higher level language.
16 //
17 // This pass is language-independent. The type system is encoded in
18 // metadata. This allows this pass to support typical C and C++ TBAA, but
19 // it can also support custom aliasing behavior for other languages.
20 //
21 // This is a work-in-progress. It doesn't work yet, and the metadata
22 // format isn't stable.
23 //
24 // The current metadata format is very simple. MDNodes have up to three
25 // fields, e.g.:
26 //   !0 = metadata !{ !"name", !1, 0 }
27 // The first field is an identity field. It can be any MDString which
28 // uniquely identifies the type. The second field identifies the type's
29 // parent node in the tree, or is null or omitted for a root node.
30 // If the third field is present, it's an integer which if equal to 1
31 // indicates that the type is "constant" (meaning 
32 // pointsToConstantMemory should return true; see
33 // http://llvm.org/docs/AliasAnalysis.html#OtherItfs).
34 //
35 // TODO: The current metadata encoding scheme doesn't support struct
36 // fields. For example:
37 //   struct X {
38 //     double d;
39 //     int i;
40 //   };
41 //   void foo(struct X *x, struct X *y, double *p) {
42 //     *x = *y;
43 //     *p = 0.0;
44 //   }
45 // Struct X has a double member, so the store to *x can alias the store to *p.
46 // Currently it's not possible to precisely describe all the things struct X
47 // aliases, so struct assignments must use conservative TBAA nodes. There's
48 // no scheme for attaching metadata to @llvm.memcpy yet either.
49 //
50 //===----------------------------------------------------------------------===//
51
52 #include "llvm/Analysis/AliasAnalysis.h"
53 #include "llvm/Analysis/Passes.h"
54 #include "llvm/Module.h"
55 #include "llvm/Metadata.h"
56 #include "llvm/Pass.h"
57 #include "llvm/Support/CommandLine.h"
58 using namespace llvm;
59
60 // For testing purposes, enable TBAA only via a special option.
61 static cl::opt<bool> EnableTBAA("enable-tbaa");
62
63 namespace {
64   /// TBAANode - This is a simple wrapper around an MDNode which provides a
65   /// higher-level interface by hiding the details of how alias analysis
66   /// information is encoded in its operands.
67   class TBAANode {
68     const MDNode *Node;
69
70   public:
71     TBAANode() : Node(0) {}
72     explicit TBAANode(const MDNode *N) : Node(N) {}
73
74     /// getNode - Get the MDNode for this TBAANode.
75     const MDNode *getNode() const { return Node; }
76
77     /// getParent - Get this TBAANode's Alias tree parent.
78     TBAANode getParent() const {
79       if (Node->getNumOperands() < 2)
80         return TBAANode();
81       MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
82       if (!P)
83         return TBAANode();
84       // Ok, this node has a valid parent. Return it.
85       return TBAANode(P);
86     }
87
88     /// TypeIsImmutable - Test if this TBAANode represents a type for objects
89     /// which are not modified (by any means) in the context where this
90     /// AliasAnalysis is relevant.
91     bool TypeIsImmutable() const {
92       if (Node->getNumOperands() < 3)
93         return false;
94       ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2));
95       if (!CI)
96         return false;
97       // TODO: Think about the encoding.
98       return CI->isOne();
99     }
100   };
101 }
102
103 namespace {
104   /// TypeBasedAliasAnalysis - This is a simple alias analysis
105   /// implementation that uses TypeBased to answer queries.
106   class TypeBasedAliasAnalysis : public ImmutablePass,
107                                  public AliasAnalysis {
108   public:
109     static char ID; // Class identification, replacement for typeinfo
110     TypeBasedAliasAnalysis() : ImmutablePass(ID) {
111       initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
112     }
113
114     virtual void initializePass() {
115       InitializeAliasAnalysis(this);
116     }
117
118     /// getAdjustedAnalysisPointer - This method is used when a pass implements
119     /// an analysis interface through multiple inheritance.  If needed, it
120     /// should override this to adjust the this pointer as needed for the
121     /// specified pass info.
122     virtual void *getAdjustedAnalysisPointer(const void *PI) {
123       if (PI == &AliasAnalysis::ID)
124         return (AliasAnalysis*)this;
125       return this;
126     }
127
128     bool Aliases(const MDNode *A, const MDNode *B) const;
129
130   private:
131     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
132     virtual AliasResult alias(const Location &LocA, const Location &LocB);
133     virtual bool pointsToConstantMemory(const Location &Loc);
134   };
135 }  // End of anonymous namespace
136
137 // Register this pass...
138 char TypeBasedAliasAnalysis::ID = 0;
139 INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
140                    "Type-Based Alias Analysis", false, true, false)
141
142 ImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
143   return new TypeBasedAliasAnalysis();
144 }
145
146 void
147 TypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
148   AU.setPreservesAll();
149   AliasAnalysis::getAnalysisUsage(AU);
150 }
151
152 /// Aliases - Test whether the type represented by A may alias the
153 /// type represented by B.
154 bool
155 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
156                                 const MDNode *B) const {
157   // Keep track of the root node for A and B.
158   TBAANode RootA, RootB;
159
160   // Climb the tree from A to see if we reach B.
161   for (TBAANode T(A); ; ) {
162     if (T.getNode() == B)
163       // B is an ancestor of A.
164       return true;
165
166     RootA = T;
167     T = T.getParent();
168     if (!T.getNode())
169       break;
170   }
171
172   // Climb the tree from B to see if we reach A.
173   for (TBAANode T(B); ; ) {
174     if (T.getNode() == A)
175       // A is an ancestor of B.
176       return true;
177
178     RootB = T;
179     T = T.getParent();
180     if (!T.getNode())
181       break;
182   }
183
184   // Neither node is an ancestor of the other.
185   
186   // If they have different roots, they're part of different potentially
187   // unrelated type systems, so we must be conservative.
188   if (RootA.getNode() != RootB.getNode())
189     return true;
190
191   // If they have the same root, then we've proved there's no alias.
192   return false;
193 }
194
195 AliasAnalysis::AliasResult
196 TypeBasedAliasAnalysis::alias(const Location &LocA,
197                               const Location &LocB) {
198   if (!EnableTBAA)
199     return AliasAnalysis::alias(LocA, LocB);
200
201   // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
202   // be conservative.
203   const MDNode *AM = LocA.TBAATag;
204   if (!AM) return AliasAnalysis::alias(LocA, LocB);
205   const MDNode *BM = LocB.TBAATag;
206   if (!BM) return AliasAnalysis::alias(LocA, LocB);
207
208   // If they may alias, chain to the next AliasAnalysis.
209   if (Aliases(AM, BM))
210     return AliasAnalysis::alias(LocA, LocB);
211
212   // Otherwise return a definitive result.
213   return NoAlias;
214 }
215
216 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc) {
217   if (!EnableTBAA)
218     return AliasAnalysis::pointsToConstantMemory(Loc);
219
220   const MDNode *M = Loc.TBAATag;
221   if (!M) return false;
222
223   // If this is an "immutable" type, we can assume the pointer is pointing
224   // to constant memory.
225   if (TBAANode(M).TypeIsImmutable())
226     return true;
227
228   return AliasAnalysis::pointsToConstantMemory(Loc);
229 }