LLVM API Documentation

TypeBasedAliasAnalysis.cpp
Go to the documentation of this file.
00001 //===- TypeBasedAliasAnalysis.cpp - Type-Based Alias Analysis -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the TypeBasedAliasAnalysis pass, which implements
00011 // metadata-based TBAA.
00012 //
00013 // In LLVM IR, memory does not have types, so LLVM's own type system is not
00014 // suitable for doing TBAA. Instead, metadata is added to the IR to describe
00015 // a type system of a higher level language. This can be used to implement
00016 // typical C/C++ TBAA, but it can also be used to implement custom alias
00017 // analysis behavior for other languages.
00018 //
00019 // We now support two types of metadata format: scalar TBAA and struct-path
00020 // aware TBAA. After all testing cases are upgraded to use struct-path aware
00021 // TBAA and we can auto-upgrade existing bc files, the support for scalar TBAA
00022 // can be dropped.
00023 //
00024 // The scalar TBAA metadata format is very simple. TBAA MDNodes have up to
00025 // three fields, e.g.:
00026 //   !0 = metadata !{ metadata !"an example type tree" }
00027 //   !1 = metadata !{ metadata !"int", metadata !0 }
00028 //   !2 = metadata !{ metadata !"float", metadata !0 }
00029 //   !3 = metadata !{ metadata !"const float", metadata !2, i64 1 }
00030 //
00031 // The first field is an identity field. It can be any value, usually
00032 // an MDString, which uniquely identifies the type. The most important
00033 // name in the tree is the name of the root node. Two trees with
00034 // different root node names are entirely disjoint, even if they
00035 // have leaves with common names.
00036 //
00037 // The second field identifies the type's parent node in the tree, or
00038 // is null or omitted for a root node. A type is considered to alias
00039 // all of its descendants and all of its ancestors in the tree. Also,
00040 // a type is considered to alias all types in other trees, so that
00041 // bitcode produced from multiple front-ends is handled conservatively.
00042 //
00043 // If the third field is present, it's an integer which if equal to 1
00044 // indicates that the type is "constant" (meaning pointsToConstantMemory
00045 // should return true; see
00046 // http://llvm.org/docs/AliasAnalysis.html#OtherItfs).
00047 //
00048 // With struct-path aware TBAA, the MDNodes attached to an instruction using
00049 // "!tbaa" are called path tag nodes.
00050 //
00051 // The path tag node has 4 fields with the last field being optional.
00052 //
00053 // The first field is the base type node, it can be a struct type node
00054 // or a scalar type node. The second field is the access type node, it
00055 // must be a scalar type node. The third field is the offset into the base type.
00056 // The last field has the same meaning as the last field of our scalar TBAA:
00057 // it's an integer which if equal to 1 indicates that the access is "constant".
00058 //
00059 // The struct type node has a name and a list of pairs, one pair for each member
00060 // of the struct. The first element of each pair is a type node (a struct type
00061 // node or a sclar type node), specifying the type of the member, the second
00062 // element of each pair is the offset of the member.
00063 //
00064 // Given an example
00065 // typedef struct {
00066 //   short s;
00067 // } A;
00068 // typedef struct {
00069 //   uint16_t s;
00070 //   A a;
00071 // } B;
00072 //
00073 // For an acess to B.a.s, we attach !5 (a path tag node) to the load/store
00074 // instruction. The base type is !4 (struct B), the access type is !2 (scalar
00075 // type short) and the offset is 4.
00076 //
00077 // !0 = metadata !{metadata !"Simple C/C++ TBAA"}
00078 // !1 = metadata !{metadata !"omnipotent char", metadata !0} // Scalar type node
00079 // !2 = metadata !{metadata !"short", metadata !1}           // Scalar type node
00080 // !3 = metadata !{metadata !"A", metadata !2, i64 0}        // Struct type node
00081 // !4 = metadata !{metadata !"B", metadata !2, i64 0, metadata !3, i64 4}
00082 //                                                           // Struct type node
00083 // !5 = metadata !{metadata !4, metadata !2, i64 4}          // Path tag node
00084 //
00085 // The struct type nodes and the scalar type nodes form a type DAG.
00086 //         Root (!0)
00087 //         char (!1)  -- edge to Root
00088 //         short (!2) -- edge to char
00089 //         A (!3) -- edge with offset 0 to short
00090 //         B (!4) -- edge with offset 0 to short and edge with offset 4 to A
00091 //
00092 // To check if two tags (tagX and tagY) can alias, we start from the base type
00093 // of tagX, follow the edge with the correct offset in the type DAG and adjust
00094 // the offset until we reach the base type of tagY or until we reach the Root
00095 // node.
00096 // If we reach the base type of tagY, compare the adjusted offset with
00097 // offset of tagY, return Alias if the offsets are the same, return NoAlias
00098 // otherwise.
00099 // If we reach the Root node, perform the above starting from base type of tagY
00100 // to see if we reach base type of tagX.
00101 //
00102 // If they have different roots, they're part of different potentially
00103 // unrelated type systems, so we return Alias to be conservative.
00104 // If neither node is an ancestor of the other and they have the same root,
00105 // then we say NoAlias.
00106 //
00107 // TODO: The current metadata format doesn't support struct
00108 // fields. For example:
00109 //   struct X {
00110 //     double d;
00111 //     int i;
00112 //   };
00113 //   void foo(struct X *x, struct X *y, double *p) {
00114 //     *x = *y;
00115 //     *p = 0.0;
00116 //   }
00117 // Struct X has a double member, so the store to *x can alias the store to *p.
00118 // Currently it's not possible to precisely describe all the things struct X
00119 // aliases, so struct assignments must use conservative TBAA nodes. There's
00120 // no scheme for attaching metadata to @llvm.memcpy yet either.
00121 //
00122 //===----------------------------------------------------------------------===//
00123 
00124 #include "llvm/Analysis/Passes.h"
00125 #include "llvm/Analysis/AliasAnalysis.h"
00126 #include "llvm/IR/Constants.h"
00127 #include "llvm/IR/LLVMContext.h"
00128 #include "llvm/IR/Metadata.h"
00129 #include "llvm/IR/Module.h"
00130 #include "llvm/Pass.h"
00131 #include "llvm/Support/CommandLine.h"
00132 using namespace llvm;
00133 
00134 // A handy option for disabling TBAA functionality. The same effect can also be
00135 // achieved by stripping the !tbaa tags from IR, but this option is sometimes
00136 // more convenient.
00137 static cl::opt<bool> EnableTBAA("enable-tbaa", cl::init(true));
00138 
00139 namespace {
00140   /// TBAANode - This is a simple wrapper around an MDNode which provides a
00141   /// higher-level interface by hiding the details of how alias analysis
00142   /// information is encoded in its operands.
00143   class TBAANode {
00144     const MDNode *Node;
00145 
00146   public:
00147     TBAANode() : Node(nullptr) {}
00148     explicit TBAANode(const MDNode *N) : Node(N) {}
00149 
00150     /// getNode - Get the MDNode for this TBAANode.
00151     const MDNode *getNode() const { return Node; }
00152 
00153     /// getParent - Get this TBAANode's Alias tree parent.
00154     TBAANode getParent() const {
00155       if (Node->getNumOperands() < 2)
00156         return TBAANode();
00157       MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
00158       if (!P)
00159         return TBAANode();
00160       // Ok, this node has a valid parent. Return it.
00161       return TBAANode(P);
00162     }
00163 
00164     /// TypeIsImmutable - Test if this TBAANode represents a type for objects
00165     /// which are not modified (by any means) in the context where this
00166     /// AliasAnalysis is relevant.
00167     bool TypeIsImmutable() const {
00168       if (Node->getNumOperands() < 3)
00169         return false;
00170       ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(2));
00171       if (!CI)
00172         return false;
00173       return CI->getValue()[0];
00174     }
00175   };
00176 
00177   /// This is a simple wrapper around an MDNode which provides a
00178   /// higher-level interface by hiding the details of how alias analysis
00179   /// information is encoded in its operands.
00180   class TBAAStructTagNode {
00181     /// This node should be created with createTBAAStructTagNode.
00182     const MDNode *Node;
00183 
00184   public:
00185     explicit TBAAStructTagNode(const MDNode *N) : Node(N) {}
00186 
00187     /// Get the MDNode for this TBAAStructTagNode.
00188     const MDNode *getNode() const { return Node; }
00189 
00190     const MDNode *getBaseType() const {
00191       return dyn_cast_or_null<MDNode>(Node->getOperand(0));
00192     }
00193     const MDNode *getAccessType() const {
00194       return dyn_cast_or_null<MDNode>(Node->getOperand(1));
00195     }
00196     uint64_t getOffset() const {
00197       return cast<ConstantInt>(Node->getOperand(2))->getZExtValue();
00198     }
00199     /// TypeIsImmutable - Test if this TBAAStructTagNode represents a type for
00200     /// objects which are not modified (by any means) in the context where this
00201     /// AliasAnalysis is relevant.
00202     bool TypeIsImmutable() const {
00203       if (Node->getNumOperands() < 4)
00204         return false;
00205       ConstantInt *CI = dyn_cast<ConstantInt>(Node->getOperand(3));
00206       if (!CI)
00207         return false;
00208       return CI->getValue()[0];
00209     }
00210   };
00211 
00212   /// This is a simple wrapper around an MDNode which provides a
00213   /// higher-level interface by hiding the details of how alias analysis
00214   /// information is encoded in its operands.
00215   class TBAAStructTypeNode {
00216     /// This node should be created with createTBAAStructTypeNode.
00217     const MDNode *Node;
00218 
00219   public:
00220     TBAAStructTypeNode() : Node(nullptr) {}
00221     explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
00222 
00223     /// Get the MDNode for this TBAAStructTypeNode.
00224     const MDNode *getNode() const { return Node; }
00225 
00226     /// Get this TBAAStructTypeNode's field in the type DAG with
00227     /// given offset. Update the offset to be relative to the field type.
00228     TBAAStructTypeNode getParent(uint64_t &Offset) const {
00229       // Parent can be omitted for the root node.
00230       if (Node->getNumOperands() < 2)
00231         return TBAAStructTypeNode();
00232 
00233       // Fast path for a scalar type node and a struct type node with a single
00234       // field.
00235       if (Node->getNumOperands() <= 3) {
00236         uint64_t Cur = Node->getNumOperands() == 2 ? 0 :
00237                        cast<ConstantInt>(Node->getOperand(2))->getZExtValue();
00238         Offset -= Cur;
00239         MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
00240         if (!P)
00241           return TBAAStructTypeNode();
00242         return TBAAStructTypeNode(P);
00243       }
00244 
00245       // Assume the offsets are in order. We return the previous field if
00246       // the current offset is bigger than the given offset.
00247       unsigned TheIdx = 0;
00248       for (unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) {
00249         uint64_t Cur = cast<ConstantInt>(Node->getOperand(Idx + 1))->
00250                          getZExtValue();
00251         if (Cur > Offset) {
00252           assert(Idx >= 3 &&
00253                  "TBAAStructTypeNode::getParent should have an offset match!");
00254           TheIdx = Idx - 2;
00255           break;
00256         }
00257       }
00258       // Move along the last field.
00259       if (TheIdx == 0)
00260         TheIdx = Node->getNumOperands() - 2;
00261       uint64_t Cur = cast<ConstantInt>(Node->getOperand(TheIdx + 1))->
00262                        getZExtValue();
00263       Offset -= Cur;
00264       MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx));
00265       if (!P)
00266         return TBAAStructTypeNode();
00267       return TBAAStructTypeNode(P);
00268     }
00269   };
00270 }
00271 
00272 namespace {
00273   /// TypeBasedAliasAnalysis - This is a simple alias analysis
00274   /// implementation that uses TypeBased to answer queries.
00275   class TypeBasedAliasAnalysis : public ImmutablePass,
00276                                  public AliasAnalysis {
00277   public:
00278     static char ID; // Class identification, replacement for typeinfo
00279     TypeBasedAliasAnalysis() : ImmutablePass(ID) {
00280       initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
00281     }
00282 
00283     void initializePass() override {
00284       InitializeAliasAnalysis(this);
00285     }
00286 
00287     /// getAdjustedAnalysisPointer - This method is used when a pass implements
00288     /// an analysis interface through multiple inheritance.  If needed, it
00289     /// should override this to adjust the this pointer as needed for the
00290     /// specified pass info.
00291     void *getAdjustedAnalysisPointer(const void *PI) override {
00292       if (PI == &AliasAnalysis::ID)
00293         return (AliasAnalysis*)this;
00294       return this;
00295     }
00296 
00297     bool Aliases(const MDNode *A, const MDNode *B) const;
00298     bool PathAliases(const MDNode *A, const MDNode *B) const;
00299 
00300   private:
00301     void getAnalysisUsage(AnalysisUsage &AU) const override;
00302     AliasResult alias(const Location &LocA, const Location &LocB) override;
00303     bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override;
00304     ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
00305     ModRefBehavior getModRefBehavior(const Function *F) override;
00306     ModRefResult getModRefInfo(ImmutableCallSite CS,
00307                                const Location &Loc) override;
00308     ModRefResult getModRefInfo(ImmutableCallSite CS1,
00309                                ImmutableCallSite CS2) override;
00310   };
00311 }  // End of anonymous namespace
00312 
00313 // Register this pass...
00314 char TypeBasedAliasAnalysis::ID = 0;
00315 INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
00316                    "Type-Based Alias Analysis", false, true, false)
00317 
00318 ImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
00319   return new TypeBasedAliasAnalysis();
00320 }
00321 
00322 void
00323 TypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
00324   AU.setPreservesAll();
00325   AliasAnalysis::getAnalysisUsage(AU);
00326 }
00327 
00328 /// Check the first operand of the tbaa tag node, if it is a MDNode, we treat
00329 /// it as struct-path aware TBAA format, otherwise, we treat it as scalar TBAA
00330 /// format.
00331 static bool isStructPathTBAA(const MDNode *MD) {
00332   // Anonymous TBAA root starts with a MDNode and dragonegg uses it as
00333   // a TBAA tag.
00334   return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
00335 }
00336 
00337 /// Aliases - Test whether the type represented by A may alias the
00338 /// type represented by B.
00339 bool
00340 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
00341                                 const MDNode *B) const {
00342   // Make sure that both MDNodes are struct-path aware.
00343   if (isStructPathTBAA(A) && isStructPathTBAA(B))
00344     return PathAliases(A, B);
00345 
00346   // Keep track of the root node for A and B.
00347   TBAANode RootA, RootB;
00348 
00349   // Climb the tree from A to see if we reach B.
00350   for (TBAANode T(A); ; ) {
00351     if (T.getNode() == B)
00352       // B is an ancestor of A.
00353       return true;
00354 
00355     RootA = T;
00356     T = T.getParent();
00357     if (!T.getNode())
00358       break;
00359   }
00360 
00361   // Climb the tree from B to see if we reach A.
00362   for (TBAANode T(B); ; ) {
00363     if (T.getNode() == A)
00364       // A is an ancestor of B.
00365       return true;
00366 
00367     RootB = T;
00368     T = T.getParent();
00369     if (!T.getNode())
00370       break;
00371   }
00372 
00373   // Neither node is an ancestor of the other.
00374   
00375   // If they have different roots, they're part of different potentially
00376   // unrelated type systems, so we must be conservative.
00377   if (RootA.getNode() != RootB.getNode())
00378     return true;
00379 
00380   // If they have the same root, then we've proved there's no alias.
00381   return false;
00382 }
00383 
00384 /// Test whether the struct-path tag represented by A may alias the
00385 /// struct-path tag represented by B.
00386 bool
00387 TypeBasedAliasAnalysis::PathAliases(const MDNode *A,
00388                                     const MDNode *B) const {
00389   // Verify that both input nodes are struct-path aware.
00390   assert(isStructPathTBAA(A) && "MDNode A is not struct-path aware.");
00391   assert(isStructPathTBAA(B) && "MDNode B is not struct-path aware.");
00392 
00393   // Keep track of the root node for A and B.
00394   TBAAStructTypeNode RootA, RootB;
00395   TBAAStructTagNode TagA(A), TagB(B);
00396 
00397   // TODO: We need to check if AccessType of TagA encloses AccessType of
00398   // TagB to support aggregate AccessType. If yes, return true.
00399 
00400   // Start from the base type of A, follow the edge with the correct offset in
00401   // the type DAG and adjust the offset until we reach the base type of B or
00402   // until we reach the Root node.
00403   // Compare the adjusted offset once we have the same base.
00404 
00405   // Climb the type DAG from base type of A to see if we reach base type of B.
00406   const MDNode *BaseA = TagA.getBaseType();
00407   const MDNode *BaseB = TagB.getBaseType();
00408   uint64_t OffsetA = TagA.getOffset(), OffsetB = TagB.getOffset();
00409   for (TBAAStructTypeNode T(BaseA); ; ) {
00410     if (T.getNode() == BaseB)
00411       // Base type of A encloses base type of B, check if the offsets match.
00412       return OffsetA == OffsetB;
00413 
00414     RootA = T;
00415     // Follow the edge with the correct offset, OffsetA will be adjusted to
00416     // be relative to the field type.
00417     T = T.getParent(OffsetA);
00418     if (!T.getNode())
00419       break;
00420   }
00421 
00422   // Reset OffsetA and climb the type DAG from base type of B to see if we reach
00423   // base type of A.
00424   OffsetA = TagA.getOffset();
00425   for (TBAAStructTypeNode T(BaseB); ; ) {
00426     if (T.getNode() == BaseA)
00427       // Base type of B encloses base type of A, check if the offsets match.
00428       return OffsetA == OffsetB;
00429 
00430     RootB = T;
00431     // Follow the edge with the correct offset, OffsetB will be adjusted to
00432     // be relative to the field type.
00433     T = T.getParent(OffsetB);
00434     if (!T.getNode())
00435       break;
00436   }
00437 
00438   // Neither node is an ancestor of the other.
00439 
00440   // If they have different roots, they're part of different potentially
00441   // unrelated type systems, so we must be conservative.
00442   if (RootA.getNode() != RootB.getNode())
00443     return true;
00444 
00445   // If they have the same root, then we've proved there's no alias.
00446   return false;
00447 }
00448 
00449 AliasAnalysis::AliasResult
00450 TypeBasedAliasAnalysis::alias(const Location &LocA,
00451                               const Location &LocB) {
00452   if (!EnableTBAA)
00453     return AliasAnalysis::alias(LocA, LocB);
00454 
00455   // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
00456   // be conservative.
00457   const MDNode *AM = LocA.AATags.TBAA;
00458   if (!AM) return AliasAnalysis::alias(LocA, LocB);
00459   const MDNode *BM = LocB.AATags.TBAA;
00460   if (!BM) return AliasAnalysis::alias(LocA, LocB);
00461 
00462   // If they may alias, chain to the next AliasAnalysis.
00463   if (Aliases(AM, BM))
00464     return AliasAnalysis::alias(LocA, LocB);
00465 
00466   // Otherwise return a definitive result.
00467   return NoAlias;
00468 }
00469 
00470 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc,
00471                                                     bool OrLocal) {
00472   if (!EnableTBAA)
00473     return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00474 
00475   const MDNode *M = Loc.AATags.TBAA;
00476   if (!M) return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00477 
00478   // If this is an "immutable" type, we can assume the pointer is pointing
00479   // to constant memory.
00480   if ((!isStructPathTBAA(M) && TBAANode(M).TypeIsImmutable()) ||
00481       (isStructPathTBAA(M) && TBAAStructTagNode(M).TypeIsImmutable()))
00482     return true;
00483 
00484   return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
00485 }
00486 
00487 AliasAnalysis::ModRefBehavior
00488 TypeBasedAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
00489   if (!EnableTBAA)
00490     return AliasAnalysis::getModRefBehavior(CS);
00491 
00492   ModRefBehavior Min = UnknownModRefBehavior;
00493 
00494   // If this is an "immutable" type, we can assume the call doesn't write
00495   // to memory.
00496   if (const MDNode *M = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
00497     if ((!isStructPathTBAA(M) && TBAANode(M).TypeIsImmutable()) ||
00498         (isStructPathTBAA(M) && TBAAStructTagNode(M).TypeIsImmutable()))
00499       Min = OnlyReadsMemory;
00500 
00501   return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
00502 }
00503 
00504 AliasAnalysis::ModRefBehavior
00505 TypeBasedAliasAnalysis::getModRefBehavior(const Function *F) {
00506   // Functions don't have metadata. Just chain to the next implementation.
00507   return AliasAnalysis::getModRefBehavior(F);
00508 }
00509 
00510 AliasAnalysis::ModRefResult
00511 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
00512                                       const Location &Loc) {
00513   if (!EnableTBAA)
00514     return AliasAnalysis::getModRefInfo(CS, Loc);
00515 
00516   if (const MDNode *L = Loc.AATags.TBAA)
00517     if (const MDNode *M =
00518           CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
00519       if (!Aliases(L, M))
00520         return NoModRef;
00521 
00522   return AliasAnalysis::getModRefInfo(CS, Loc);
00523 }
00524 
00525 AliasAnalysis::ModRefResult
00526 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
00527                                       ImmutableCallSite CS2) {
00528   if (!EnableTBAA)
00529     return AliasAnalysis::getModRefInfo(CS1, CS2);
00530 
00531   if (const MDNode *M1 =
00532         CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
00533     if (const MDNode *M2 =
00534           CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
00535       if (!Aliases(M1, M2))
00536         return NoModRef;
00537 
00538   return AliasAnalysis::getModRefInfo(CS1, CS2);
00539 }
00540 
00541 bool MDNode::isTBAAVtableAccess() const {
00542   if (!isStructPathTBAA(this)) {
00543     if (getNumOperands() < 1) return false;
00544     if (MDString *Tag1 = dyn_cast<MDString>(getOperand(0))) {
00545       if (Tag1->getString() == "vtable pointer") return true;
00546     }
00547     return false;
00548   }
00549 
00550   // For struct-path aware TBAA, we use the access type of the tag.
00551   if (getNumOperands() < 2) return false;
00552   MDNode *Tag = cast_or_null<MDNode>(getOperand(1));
00553   if (!Tag) return false;
00554   if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
00555     if (Tag1->getString() == "vtable pointer") return true;
00556   }
00557   return false;  
00558 }
00559 
00560 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
00561   if (!A || !B)
00562     return nullptr;
00563 
00564   if (A == B)
00565     return A;
00566 
00567   // For struct-path aware TBAA, we use the access type of the tag.
00568   bool StructPath = isStructPathTBAA(A) && isStructPathTBAA(B);
00569   if (StructPath) {
00570     A = cast_or_null<MDNode>(A->getOperand(1));
00571     if (!A) return nullptr;
00572     B = cast_or_null<MDNode>(B->getOperand(1));
00573     if (!B) return nullptr;
00574   }
00575 
00576   SmallVector<MDNode *, 4> PathA;
00577   MDNode *T = A;
00578   while (T) {
00579     PathA.push_back(T);
00580     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1))
00581                                  : nullptr;
00582   }
00583 
00584   SmallVector<MDNode *, 4> PathB;
00585   T = B;
00586   while (T) {
00587     PathB.push_back(T);
00588     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1))
00589                                  : nullptr;
00590   }
00591 
00592   int IA = PathA.size() - 1;
00593   int IB = PathB.size() - 1;
00594 
00595   MDNode *Ret = nullptr;
00596   while (IA >= 0 && IB >=0) {
00597     if (PathA[IA] == PathB[IB])
00598       Ret = PathA[IA];
00599     else
00600       break;
00601     --IA;
00602     --IB;
00603   }
00604   if (!StructPath)
00605     return Ret;
00606 
00607   if (!Ret)
00608     return nullptr;
00609   // We need to convert from a type node to a tag node.
00610   Type *Int64 = IntegerType::get(A->getContext(), 64);
00611   Value *Ops[3] = { Ret, Ret, ConstantInt::get(Int64, 0) };
00612   return MDNode::get(A->getContext(), Ops);
00613 }
00614 
00615 void Instruction::getAAMetadata(AAMDNodes &N, bool Merge) const {
00616   if (Merge)
00617     N.TBAA = MDNode::getMostGenericTBAA(N.TBAA,
00618                                         getMetadata(LLVMContext::MD_tbaa));
00619   else
00620     N.TBAA = getMetadata(LLVMContext::MD_tbaa);
00621 
00622   if (Merge)
00623     N.Scope = MDNode::intersect(N.Scope,
00624                                 getMetadata(LLVMContext::MD_alias_scope));
00625   else
00626     N.Scope = getMetadata(LLVMContext::MD_alias_scope);
00627 
00628   if (Merge)
00629     N.NoAlias = MDNode::intersect(N.NoAlias,
00630                                   getMetadata(LLVMContext::MD_noalias));
00631   else
00632     N.NoAlias = getMetadata(LLVMContext::MD_noalias);
00633 }
00634