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