1 //===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the TypeBasedAliasAnalysis pass, which implements
11 // metadata-based TBAA.
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.
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 }
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.
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 descendants 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.
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).
43 // TODO: The current metadata format doesn't support struct
44 // fields. For example:
49 // void foo(struct X *x, struct X *y, double *p) {
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.
58 //===----------------------------------------------------------------------===//
60 #include "llvm/Analysis/Passes.h"
61 #include "llvm/Analysis/AliasAnalysis.h"
62 #include "llvm/IR/Constants.h"
63 #include "llvm/IR/LLVMContext.h"
64 #include "llvm/IR/Metadata.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/Pass.h"
67 #include "llvm/Support/CommandLine.h"
70 // A handy option for disabling TBAA functionality. The same effect can also be
71 // achieved by stripping the !tbaa tags from IR, but this option is sometimes
73 static cl::opt<bool> EnableTBAA("enable-tbaa", cl::init(true));
74 static cl::opt<bool> EnableStructPathTBAA("struct-path-tbaa", cl::init(false));
77 /// TBAANode - This is a simple wrapper around an MDNode which provides a
78 /// higher-level interface by hiding the details of how alias analysis
79 /// information is encoded in its operands.
84 TBAANode() : Node(0) {}
85 explicit TBAANode(const MDNode *N) : Node(N) {}
87 /// getNode - Get the MDNode for this TBAANode.
88 const MDNode *getNode() const { return Node; }
90 /// getParent - Get this TBAANode's Alias tree parent.
91 TBAANode getParent() const {
92 if (Node->getNumOperands() < 2)
94 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
97 // Ok, this node has a valid parent. Return it.
101 /// TypeIsImmutable - Test if this TBAANode represents a type for objects
102 /// which are not modified (by any means) in the context where this
103 /// AliasAnalysis is relevant.
104 bool TypeIsImmutable() const {
105 if (Node->getNumOperands() < 3)
107 ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2));
110 return CI->getValue()[0];
114 /// This is a simple wrapper around an MDNode which provides a
115 /// higher-level interface by hiding the details of how alias analysis
116 /// information is encoded in its operands.
117 class TBAAStructTagNode {
118 /// This node should be created with createTBAAStructTagNode.
122 TBAAStructTagNode() : Node(0) {}
123 explicit TBAAStructTagNode(const MDNode *N) : Node(N) {}
125 /// Get the MDNode for this TBAAStructTagNode.
126 const MDNode *getNode() const { return Node; }
128 const MDNode *getBaseType() const {
129 return dyn_cast_or_null<MDNode>(Node->getOperand(0));
131 const MDNode *getAccessType() const {
132 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
134 uint64_t getOffset() const {
135 return cast<ConstantInt>(Node->getOperand(2))->getZExtValue();
137 /// TypeIsImmutable - Test if this TBAAStructTagNode represents a type for
138 /// objects which are not modified (by any means) in the context where this
139 /// AliasAnalysis is relevant.
140 bool TypeIsImmutable() const {
141 if (Node->getNumOperands() < 4)
143 ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(3));
146 return CI->getValue()[0];
150 /// This is a simple wrapper around an MDNode which provides a
151 /// higher-level interface by hiding the details of how alias analysis
152 /// information is encoded in its operands.
153 class TBAAStructTypeNode {
154 /// This node should be created with createTBAAStructTypeNode.
158 TBAAStructTypeNode() : Node(0) {}
159 explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
161 /// Get the MDNode for this TBAAStructTypeNode.
162 const MDNode *getNode() const { return Node; }
164 /// Get this TBAAStructTypeNode's field in the type DAG with
165 /// given offset. Update the offset to be relative to the field type.
166 TBAAStructTypeNode getParent(uint64_t &Offset) const {
167 // Parent can be omitted for the root node.
168 if (Node->getNumOperands() < 2)
169 return TBAAStructTypeNode();
171 // Special handling for a scalar type node.
172 if (Node->getNumOperands() <= 3) {
173 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
175 return TBAAStructTypeNode();
176 return TBAAStructTypeNode(P);
179 // Assume the offsets are in order. We return the previous field if
180 // the current offset is bigger than the given offset.
182 for (unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) {
183 uint64_t Cur = cast<ConstantInt>(Node->getOperand(Idx + 1))->
187 "TBAAStructTypeNode::getParent should have an offset match!");
192 // Move along the last field.
194 TheIdx = Node->getNumOperands() - 2;
195 uint64_t Cur = cast<ConstantInt>(Node->getOperand(TheIdx + 1))->
198 MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx));
200 return TBAAStructTypeNode();
201 return TBAAStructTypeNode(P);
207 /// TypeBasedAliasAnalysis - This is a simple alias analysis
208 /// implementation that uses TypeBased to answer queries.
209 class TypeBasedAliasAnalysis : public ImmutablePass,
210 public AliasAnalysis {
212 static char ID; // Class identification, replacement for typeinfo
213 TypeBasedAliasAnalysis() : ImmutablePass(ID) {
214 initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
217 virtual void initializePass() {
218 InitializeAliasAnalysis(this);
221 /// getAdjustedAnalysisPointer - This method is used when a pass implements
222 /// an analysis interface through multiple inheritance. If needed, it
223 /// should override this to adjust the this pointer as needed for the
224 /// specified pass info.
225 virtual void *getAdjustedAnalysisPointer(const void *PI) {
226 if (PI == &AliasAnalysis::ID)
227 return (AliasAnalysis*)this;
231 bool Aliases(const MDNode *A, const MDNode *B) const;
232 bool PathAliases(const MDNode *A, const MDNode *B) const;
235 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
236 virtual AliasResult alias(const Location &LocA, const Location &LocB);
237 virtual bool pointsToConstantMemory(const Location &Loc, bool OrLocal);
238 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
239 virtual ModRefBehavior getModRefBehavior(const Function *F);
240 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
241 const Location &Loc);
242 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
243 ImmutableCallSite CS2);
245 } // End of anonymous namespace
247 // Register this pass...
248 char TypeBasedAliasAnalysis::ID = 0;
249 INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
250 "Type-Based Alias Analysis", false, true, false)
252 ImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
253 return new TypeBasedAliasAnalysis();
257 TypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
258 AU.setPreservesAll();
259 AliasAnalysis::getAnalysisUsage(AU);
262 /// Aliases - Test whether the type represented by A may alias the
263 /// type represented by B.
265 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
266 const MDNode *B) const {
267 if (EnableStructPathTBAA)
268 return PathAliases(A, B);
270 // Keep track of the root node for A and B.
271 TBAANode RootA, RootB;
273 // Climb the tree from A to see if we reach B.
274 for (TBAANode T(A); ; ) {
275 if (T.getNode() == B)
276 // B is an ancestor of A.
285 // Climb the tree from B to see if we reach A.
286 for (TBAANode T(B); ; ) {
287 if (T.getNode() == A)
288 // A is an ancestor of B.
297 // Neither node is an ancestor of the other.
299 // If they have different roots, they're part of different potentially
300 // unrelated type systems, so we must be conservative.
301 if (RootA.getNode() != RootB.getNode())
304 // If they have the same root, then we've proved there's no alias.
308 /// Test whether the struct-path tag represented by A may alias the
309 /// struct-path tag represented by B.
311 TypeBasedAliasAnalysis::PathAliases(const MDNode *A,
312 const MDNode *B) const {
313 // Keep track of the root node for A and B.
314 TBAAStructTypeNode RootA, RootB;
315 TBAAStructTagNode TagA(A), TagB(B);
317 // TODO: We need to check if AccessType of TagA encloses AccessType of
318 // TagB to support aggregate AccessType. If yes, return true.
320 // Start from the base type of A, follow the edge with the correct offset in
321 // the type DAG and adjust the offset until we reach the base type of B or
322 // until we reach the Root node.
323 // Compare the adjusted offset once we have the same base.
325 // Climb the type DAG from base type of A to see if we reach base type of B.
326 const MDNode *BaseA = TagA.getBaseType();
327 const MDNode *BaseB = TagB.getBaseType();
328 uint64_t OffsetA = TagA.getOffset(), OffsetB = TagB.getOffset();
329 for (TBAAStructTypeNode T(BaseA); ; ) {
330 if (T.getNode() == BaseB)
331 // Base type of A encloses base type of B, check if the offsets match.
332 return OffsetA == OffsetB;
335 // Follow the edge with the correct offset, OffsetA will be adjusted to
336 // be relative to the field type.
337 T = T.getParent(OffsetA);
342 // Reset OffsetA and climb the type DAG from base type of B to see if we reach
344 OffsetA = TagA.getOffset();
345 for (TBAAStructTypeNode T(BaseB); ; ) {
346 if (T.getNode() == BaseA)
347 // Base type of B encloses base type of A, check if the offsets match.
348 return OffsetA == OffsetB;
351 // Follow the edge with the correct offset, OffsetB will be adjusted to
352 // be relative to the field type.
353 T = T.getParent(OffsetB);
358 // Neither node is an ancestor of the other.
360 // If they have different roots, they're part of different potentially
361 // unrelated type systems, so we must be conservative.
362 if (RootA.getNode() != RootB.getNode())
365 // If they have the same root, then we've proved there's no alias.
369 AliasAnalysis::AliasResult
370 TypeBasedAliasAnalysis::alias(const Location &LocA,
371 const Location &LocB) {
373 return AliasAnalysis::alias(LocA, LocB);
375 // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
377 const MDNode *AM = LocA.TBAATag;
378 if (!AM) return AliasAnalysis::alias(LocA, LocB);
379 const MDNode *BM = LocB.TBAATag;
380 if (!BM) return AliasAnalysis::alias(LocA, LocB);
382 // If they may alias, chain to the next AliasAnalysis.
384 return AliasAnalysis::alias(LocA, LocB);
386 // Otherwise return a definitive result.
390 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc,
393 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
395 const MDNode *M = Loc.TBAATag;
396 if (!M) return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
398 // If this is an "immutable" type, we can assume the pointer is pointing
399 // to constant memory.
400 if ((!EnableStructPathTBAA && TBAANode(M).TypeIsImmutable()) ||
401 (EnableStructPathTBAA && TBAAStructTagNode(M).TypeIsImmutable()))
404 return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
407 AliasAnalysis::ModRefBehavior
408 TypeBasedAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
410 return AliasAnalysis::getModRefBehavior(CS);
412 ModRefBehavior Min = UnknownModRefBehavior;
414 // If this is an "immutable" type, we can assume the call doesn't write
416 if (const MDNode *M = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
417 if ((!EnableStructPathTBAA && TBAANode(M).TypeIsImmutable()) ||
418 (EnableStructPathTBAA && TBAAStructTagNode(M).TypeIsImmutable()))
419 Min = OnlyReadsMemory;
421 return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
424 AliasAnalysis::ModRefBehavior
425 TypeBasedAliasAnalysis::getModRefBehavior(const Function *F) {
426 // Functions don't have metadata. Just chain to the next implementation.
427 return AliasAnalysis::getModRefBehavior(F);
430 AliasAnalysis::ModRefResult
431 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
432 const Location &Loc) {
434 return AliasAnalysis::getModRefInfo(CS, Loc);
436 if (const MDNode *L = Loc.TBAATag)
437 if (const MDNode *M =
438 CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
442 return AliasAnalysis::getModRefInfo(CS, Loc);
445 AliasAnalysis::ModRefResult
446 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
447 ImmutableCallSite CS2) {
449 return AliasAnalysis::getModRefInfo(CS1, CS2);
451 if (const MDNode *M1 =
452 CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
453 if (const MDNode *M2 =
454 CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
455 if (!Aliases(M1, M2))
458 return AliasAnalysis::getModRefInfo(CS1, CS2);
461 bool MDNode::isTBAAVtableAccess() const {
462 if (!EnableStructPathTBAA) {
463 if (getNumOperands() < 1) return false;
464 if (MDString *Tag1 = dyn_cast<MDString>(getOperand(0))) {
465 if (Tag1->getString() == "vtable pointer") return true;
470 // For struct-path aware TBAA, we use the access type of the tag.
471 if (getNumOperands() < 2) return false;
472 MDNode *Tag = cast_or_null<MDNode>(getOperand(1));
473 if (!Tag) return false;
474 if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
475 if (Tag1->getString() == "vtable pointer") return true;
480 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
487 // For struct-path aware TBAA, we use the access type of the tag.
488 if (EnableStructPathTBAA) {
489 A = cast_or_null<MDNode>(A->getOperand(1));
491 B = cast_or_null<MDNode>(B->getOperand(1));
495 SmallVector<MDNode *, 4> PathA;
499 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
502 SmallVector<MDNode *, 4> PathB;
506 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
509 int IA = PathA.size() - 1;
510 int IB = PathB.size() - 1;
513 while (IA >= 0 && IB >=0) {
514 if (PathA[IA] == PathB[IB])
521 if (!EnableStructPathTBAA)
526 // We need to convert from a type node to a tag node.
527 Type *Int64 = IntegerType::get(A->getContext(), 64);
528 Value *Ops[3] = { Ret, Ret, ConstantInt::get(Int64, 0) };
529 return MDNode::get(A->getContext(), Ops);