Make DataLayout Non-Optional in the Module
[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 // We now support two types of metadata format: scalar TBAA and struct-path
20 // aware TBAA. After all testing cases are upgraded to use struct-path aware
21 // TBAA and we can auto-upgrade existing bc files, the support for scalar TBAA
22 // can be dropped.
23 //
24 // The scalar TBAA metadata format is very simple. TBAA MDNodes have up to
25 // three fields, e.g.:
26 //   !0 = metadata !{ metadata !"an example type tree" }
27 //   !1 = metadata !{ metadata !"int", metadata !0 }
28 //   !2 = metadata !{ metadata !"float", metadata !0 }
29 //   !3 = metadata !{ metadata !"const float", metadata !2, i64 1 }
30 //
31 // The first field is an identity field. It can be any value, usually
32 // an MDString, which uniquely identifies the type. The most important
33 // name in the tree is the name of the root node. Two trees with
34 // different root node names are entirely disjoint, even if they
35 // have leaves with common names.
36 //
37 // The second field identifies the type's parent node in the tree, or
38 // is null or omitted for a root node. A type is considered to alias
39 // all of its descendants and all of its ancestors in the tree. Also,
40 // a type is considered to alias all types in other trees, so that
41 // bitcode produced from multiple front-ends is handled conservatively.
42 //
43 // If the third field is present, it's an integer which if equal to 1
44 // indicates that the type is "constant" (meaning pointsToConstantMemory
45 // should return true; see
46 // http://llvm.org/docs/AliasAnalysis.html#OtherItfs).
47 //
48 // With struct-path aware TBAA, the MDNodes attached to an instruction using
49 // "!tbaa" are called path tag nodes.
50 //
51 // The path tag node has 4 fields with the last field being optional.
52 //
53 // The first field is the base type node, it can be a struct type node
54 // or a scalar type node. The second field is the access type node, it
55 // must be a scalar type node. The third field is the offset into the base type.
56 // The last field has the same meaning as the last field of our scalar TBAA:
57 // it's an integer which if equal to 1 indicates that the access is "constant".
58 //
59 // The struct type node has a name and a list of pairs, one pair for each member
60 // of the struct. The first element of each pair is a type node (a struct type
61 // node or a sclar type node), specifying the type of the member, the second
62 // element of each pair is the offset of the member.
63 //
64 // Given an example
65 // typedef struct {
66 //   short s;
67 // } A;
68 // typedef struct {
69 //   uint16_t s;
70 //   A a;
71 // } B;
72 //
73 // For an acess to B.a.s, we attach !5 (a path tag node) to the load/store
74 // instruction. The base type is !4 (struct B), the access type is !2 (scalar
75 // type short) and the offset is 4.
76 //
77 // !0 = metadata !{metadata !"Simple C/C++ TBAA"}
78 // !1 = metadata !{metadata !"omnipotent char", metadata !0} // Scalar type node
79 // !2 = metadata !{metadata !"short", metadata !1}           // Scalar type node
80 // !3 = metadata !{metadata !"A", metadata !2, i64 0}        // Struct type node
81 // !4 = metadata !{metadata !"B", metadata !2, i64 0, metadata !3, i64 4}
82 //                                                           // Struct type node
83 // !5 = metadata !{metadata !4, metadata !2, i64 4}          // Path tag node
84 //
85 // The struct type nodes and the scalar type nodes form a type DAG.
86 //         Root (!0)
87 //         char (!1)  -- edge to Root
88 //         short (!2) -- edge to char
89 //         A (!3) -- edge with offset 0 to short
90 //         B (!4) -- edge with offset 0 to short and edge with offset 4 to A
91 //
92 // To check if two tags (tagX and tagY) can alias, we start from the base type
93 // of tagX, follow the edge with the correct offset in the type DAG and adjust
94 // the offset until we reach the base type of tagY or until we reach the Root
95 // node.
96 // If we reach the base type of tagY, compare the adjusted offset with
97 // offset of tagY, return Alias if the offsets are the same, return NoAlias
98 // otherwise.
99 // If we reach the Root node, perform the above starting from base type of tagY
100 // to see if we reach base type of tagX.
101 //
102 // If they have different roots, they're part of different potentially
103 // unrelated type systems, so we return Alias to be conservative.
104 // If neither node is an ancestor of the other and they have the same root,
105 // then we say NoAlias.
106 //
107 // TODO: The current metadata format doesn't support struct
108 // fields. For example:
109 //   struct X {
110 //     double d;
111 //     int i;
112 //   };
113 //   void foo(struct X *x, struct X *y, double *p) {
114 //     *x = *y;
115 //     *p = 0.0;
116 //   }
117 // Struct X has a double member, so the store to *x can alias the store to *p.
118 // Currently it's not possible to precisely describe all the things struct X
119 // aliases, so struct assignments must use conservative TBAA nodes. There's
120 // no scheme for attaching metadata to @llvm.memcpy yet either.
121 //
122 //===----------------------------------------------------------------------===//
123
124 #include "llvm/Analysis/Passes.h"
125 #include "llvm/Analysis/AliasAnalysis.h"
126 #include "llvm/IR/Constants.h"
127 #include "llvm/IR/LLVMContext.h"
128 #include "llvm/IR/Metadata.h"
129 #include "llvm/IR/Module.h"
130 #include "llvm/Pass.h"
131 #include "llvm/Support/CommandLine.h"
132 using namespace llvm;
133
134 // A handy option for disabling TBAA functionality. The same effect can also be
135 // achieved by stripping the !tbaa tags from IR, but this option is sometimes
136 // more convenient.
137 static cl::opt<bool> EnableTBAA("enable-tbaa", cl::init(true));
138
139 namespace {
140   /// TBAANode - This is a simple wrapper around an MDNode which provides a
141   /// higher-level interface by hiding the details of how alias analysis
142   /// information is encoded in its operands.
143   class TBAANode {
144     const MDNode *Node;
145
146   public:
147     TBAANode() : Node(nullptr) {}
148     explicit TBAANode(const MDNode *N) : Node(N) {}
149
150     /// getNode - Get the MDNode for this TBAANode.
151     const MDNode *getNode() const { return Node; }
152
153     /// getParent - Get this TBAANode's Alias tree parent.
154     TBAANode getParent() const {
155       if (Node->getNumOperands() < 2)
156         return TBAANode();
157       MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
158       if (!P)
159         return TBAANode();
160       // Ok, this node has a valid parent. Return it.
161       return TBAANode(P);
162     }
163
164     /// TypeIsImmutable - Test if this TBAANode represents a type for objects
165     /// which are not modified (by any means) in the context where this
166     /// AliasAnalysis is relevant.
167     bool TypeIsImmutable() const {
168       if (Node->getNumOperands() < 3)
169         return false;
170       ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(2));
171       if (!CI)
172         return false;
173       return CI->getValue()[0];
174     }
175   };
176
177   /// This is a simple wrapper around an MDNode which provides a
178   /// higher-level interface by hiding the details of how alias analysis
179   /// information is encoded in its operands.
180   class TBAAStructTagNode {
181     /// This node should be created with createTBAAStructTagNode.
182     const MDNode *Node;
183
184   public:
185     explicit TBAAStructTagNode(const MDNode *N) : Node(N) {}
186
187     /// Get the MDNode for this TBAAStructTagNode.
188     const MDNode *getNode() const { return Node; }
189
190     const MDNode *getBaseType() const {
191       return dyn_cast_or_null<MDNode>(Node->getOperand(0));
192     }
193     const MDNode *getAccessType() const {
194       return dyn_cast_or_null<MDNode>(Node->getOperand(1));
195     }
196     uint64_t getOffset() const {
197       return mdconst::extract<ConstantInt>(Node->getOperand(2))->getZExtValue();
198     }
199     /// TypeIsImmutable - Test if this TBAAStructTagNode represents a type for
200     /// objects which are not modified (by any means) in the context where this
201     /// AliasAnalysis is relevant.
202     bool TypeIsImmutable() const {
203       if (Node->getNumOperands() < 4)
204         return false;
205       ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Node->getOperand(3));
206       if (!CI)
207         return false;
208       return CI->getValue()[0];
209     }
210   };
211
212   /// This is a simple wrapper around an MDNode which provides a
213   /// higher-level interface by hiding the details of how alias analysis
214   /// information is encoded in its operands.
215   class TBAAStructTypeNode {
216     /// This node should be created with createTBAAStructTypeNode.
217     const MDNode *Node;
218
219   public:
220     TBAAStructTypeNode() : Node(nullptr) {}
221     explicit TBAAStructTypeNode(const MDNode *N) : Node(N) {}
222
223     /// Get the MDNode for this TBAAStructTypeNode.
224     const MDNode *getNode() const { return Node; }
225
226     /// Get this TBAAStructTypeNode's field in the type DAG with
227     /// given offset. Update the offset to be relative to the field type.
228     TBAAStructTypeNode getParent(uint64_t &Offset) const {
229       // Parent can be omitted for the root node.
230       if (Node->getNumOperands() < 2)
231         return TBAAStructTypeNode();
232
233       // Fast path for a scalar type node and a struct type node with a single
234       // field.
235       if (Node->getNumOperands() <= 3) {
236         uint64_t Cur = Node->getNumOperands() == 2
237                            ? 0
238                            : mdconst::extract<ConstantInt>(Node->getOperand(2))
239                                  ->getZExtValue();
240         Offset -= Cur;
241         MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(1));
242         if (!P)
243           return TBAAStructTypeNode();
244         return TBAAStructTypeNode(P);
245       }
246
247       // Assume the offsets are in order. We return the previous field if
248       // the current offset is bigger than the given offset.
249       unsigned TheIdx = 0;
250       for (unsigned Idx = 1; Idx < Node->getNumOperands(); Idx += 2) {
251         uint64_t Cur = mdconst::extract<ConstantInt>(Node->getOperand(Idx + 1))
252                            ->getZExtValue();
253         if (Cur > Offset) {
254           assert(Idx >= 3 &&
255                  "TBAAStructTypeNode::getParent should have an offset match!");
256           TheIdx = Idx - 2;
257           break;
258         }
259       }
260       // Move along the last field.
261       if (TheIdx == 0)
262         TheIdx = Node->getNumOperands() - 2;
263       uint64_t Cur = mdconst::extract<ConstantInt>(Node->getOperand(TheIdx + 1))
264                          ->getZExtValue();
265       Offset -= Cur;
266       MDNode *P = dyn_cast_or_null<MDNode>(Node->getOperand(TheIdx));
267       if (!P)
268         return TBAAStructTypeNode();
269       return TBAAStructTypeNode(P);
270     }
271   };
272 }
273
274 namespace {
275   /// TypeBasedAliasAnalysis - This is a simple alias analysis
276   /// implementation that uses TypeBased to answer queries.
277   class TypeBasedAliasAnalysis : public ImmutablePass,
278                                  public AliasAnalysis {
279   public:
280     static char ID; // Class identification, replacement for typeinfo
281     TypeBasedAliasAnalysis() : ImmutablePass(ID) {
282       initializeTypeBasedAliasAnalysisPass(*PassRegistry::getPassRegistry());
283     }
284
285     bool doInitialization(Module &M) override;
286
287     /// getAdjustedAnalysisPointer - This method is used when a pass implements
288     /// an analysis interface through multiple inheritance.  If needed, it
289     /// should override this to adjust the this pointer as needed for the
290     /// specified pass info.
291     void *getAdjustedAnalysisPointer(const void *PI) override {
292       if (PI == &AliasAnalysis::ID)
293         return (AliasAnalysis*)this;
294       return this;
295     }
296
297     bool Aliases(const MDNode *A, const MDNode *B) const;
298     bool PathAliases(const MDNode *A, const MDNode *B) const;
299
300   private:
301     void getAnalysisUsage(AnalysisUsage &AU) const override;
302     AliasResult alias(const Location &LocA, const Location &LocB) override;
303     bool pointsToConstantMemory(const Location &Loc, bool OrLocal) override;
304     ModRefBehavior getModRefBehavior(ImmutableCallSite CS) override;
305     ModRefBehavior getModRefBehavior(const Function *F) override;
306     ModRefResult getModRefInfo(ImmutableCallSite CS,
307                                const Location &Loc) override;
308     ModRefResult getModRefInfo(ImmutableCallSite CS1,
309                                ImmutableCallSite CS2) override;
310   };
311 }  // End of anonymous namespace
312
313 // Register this pass...
314 char TypeBasedAliasAnalysis::ID = 0;
315 INITIALIZE_AG_PASS(TypeBasedAliasAnalysis, AliasAnalysis, "tbaa",
316                    "Type-Based Alias Analysis", false, true, false)
317
318 ImmutablePass *llvm::createTypeBasedAliasAnalysisPass() {
319   return new TypeBasedAliasAnalysis();
320 }
321
322 bool TypeBasedAliasAnalysis::doInitialization(Module &M) {
323   InitializeAliasAnalysis(this, &M.getDataLayout());
324   return true;
325 }
326
327 void
328 TypeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
329   AU.setPreservesAll();
330   AliasAnalysis::getAnalysisUsage(AU);
331 }
332
333 /// Check the first operand of the tbaa tag node, if it is a MDNode, we treat
334 /// it as struct-path aware TBAA format, otherwise, we treat it as scalar TBAA
335 /// format.
336 static bool isStructPathTBAA(const MDNode *MD) {
337   // Anonymous TBAA root starts with a MDNode and dragonegg uses it as
338   // a TBAA tag.
339   return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
340 }
341
342 /// Aliases - Test whether the type represented by A may alias the
343 /// type represented by B.
344 bool
345 TypeBasedAliasAnalysis::Aliases(const MDNode *A,
346                                 const MDNode *B) const {
347   // Make sure that both MDNodes are struct-path aware.
348   if (isStructPathTBAA(A) && isStructPathTBAA(B))
349     return PathAliases(A, B);
350
351   // Keep track of the root node for A and B.
352   TBAANode RootA, RootB;
353
354   // Climb the tree from A to see if we reach B.
355   for (TBAANode T(A); ; ) {
356     if (T.getNode() == B)
357       // B is an ancestor of A.
358       return true;
359
360     RootA = T;
361     T = T.getParent();
362     if (!T.getNode())
363       break;
364   }
365
366   // Climb the tree from B to see if we reach A.
367   for (TBAANode T(B); ; ) {
368     if (T.getNode() == A)
369       // A is an ancestor of B.
370       return true;
371
372     RootB = T;
373     T = T.getParent();
374     if (!T.getNode())
375       break;
376   }
377
378   // Neither node is an ancestor of the other.
379   
380   // If they have different roots, they're part of different potentially
381   // unrelated type systems, so we must be conservative.
382   if (RootA.getNode() != RootB.getNode())
383     return true;
384
385   // If they have the same root, then we've proved there's no alias.
386   return false;
387 }
388
389 /// Test whether the struct-path tag represented by A may alias the
390 /// struct-path tag represented by B.
391 bool
392 TypeBasedAliasAnalysis::PathAliases(const MDNode *A,
393                                     const MDNode *B) const {
394   // Verify that both input nodes are struct-path aware.
395   assert(isStructPathTBAA(A) && "MDNode A is not struct-path aware.");
396   assert(isStructPathTBAA(B) && "MDNode B is not struct-path aware.");
397
398   // Keep track of the root node for A and B.
399   TBAAStructTypeNode RootA, RootB;
400   TBAAStructTagNode TagA(A), TagB(B);
401
402   // TODO: We need to check if AccessType of TagA encloses AccessType of
403   // TagB to support aggregate AccessType. If yes, return true.
404
405   // Start from the base type of A, follow the edge with the correct offset in
406   // the type DAG and adjust the offset until we reach the base type of B or
407   // until we reach the Root node.
408   // Compare the adjusted offset once we have the same base.
409
410   // Climb the type DAG from base type of A to see if we reach base type of B.
411   const MDNode *BaseA = TagA.getBaseType();
412   const MDNode *BaseB = TagB.getBaseType();
413   uint64_t OffsetA = TagA.getOffset(), OffsetB = TagB.getOffset();
414   for (TBAAStructTypeNode T(BaseA); ; ) {
415     if (T.getNode() == BaseB)
416       // Base type of A encloses base type of B, check if the offsets match.
417       return OffsetA == OffsetB;
418
419     RootA = T;
420     // Follow the edge with the correct offset, OffsetA will be adjusted to
421     // be relative to the field type.
422     T = T.getParent(OffsetA);
423     if (!T.getNode())
424       break;
425   }
426
427   // Reset OffsetA and climb the type DAG from base type of B to see if we reach
428   // base type of A.
429   OffsetA = TagA.getOffset();
430   for (TBAAStructTypeNode T(BaseB); ; ) {
431     if (T.getNode() == BaseA)
432       // Base type of B encloses base type of A, check if the offsets match.
433       return OffsetA == OffsetB;
434
435     RootB = T;
436     // Follow the edge with the correct offset, OffsetB will be adjusted to
437     // be relative to the field type.
438     T = T.getParent(OffsetB);
439     if (!T.getNode())
440       break;
441   }
442
443   // Neither node is an ancestor of the other.
444
445   // If they have different roots, they're part of different potentially
446   // unrelated type systems, so we must be conservative.
447   if (RootA.getNode() != RootB.getNode())
448     return true;
449
450   // If they have the same root, then we've proved there's no alias.
451   return false;
452 }
453
454 AliasAnalysis::AliasResult
455 TypeBasedAliasAnalysis::alias(const Location &LocA,
456                               const Location &LocB) {
457   if (!EnableTBAA)
458     return AliasAnalysis::alias(LocA, LocB);
459
460   // Get the attached MDNodes. If either value lacks a tbaa MDNode, we must
461   // be conservative.
462   const MDNode *AM = LocA.AATags.TBAA;
463   if (!AM) return AliasAnalysis::alias(LocA, LocB);
464   const MDNode *BM = LocB.AATags.TBAA;
465   if (!BM) return AliasAnalysis::alias(LocA, LocB);
466
467   // If they may alias, chain to the next AliasAnalysis.
468   if (Aliases(AM, BM))
469     return AliasAnalysis::alias(LocA, LocB);
470
471   // Otherwise return a definitive result.
472   return NoAlias;
473 }
474
475 bool TypeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc,
476                                                     bool OrLocal) {
477   if (!EnableTBAA)
478     return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
479
480   const MDNode *M = Loc.AATags.TBAA;
481   if (!M) return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
482
483   // If this is an "immutable" type, we can assume the pointer is pointing
484   // to constant memory.
485   if ((!isStructPathTBAA(M) && TBAANode(M).TypeIsImmutable()) ||
486       (isStructPathTBAA(M) && TBAAStructTagNode(M).TypeIsImmutable()))
487     return true;
488
489   return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
490 }
491
492 AliasAnalysis::ModRefBehavior
493 TypeBasedAliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
494   if (!EnableTBAA)
495     return AliasAnalysis::getModRefBehavior(CS);
496
497   ModRefBehavior Min = UnknownModRefBehavior;
498
499   // If this is an "immutable" type, we can assume the call doesn't write
500   // to memory.
501   if (const MDNode *M = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
502     if ((!isStructPathTBAA(M) && TBAANode(M).TypeIsImmutable()) ||
503         (isStructPathTBAA(M) && TBAAStructTagNode(M).TypeIsImmutable()))
504       Min = OnlyReadsMemory;
505
506   return ModRefBehavior(AliasAnalysis::getModRefBehavior(CS) & Min);
507 }
508
509 AliasAnalysis::ModRefBehavior
510 TypeBasedAliasAnalysis::getModRefBehavior(const Function *F) {
511   // Functions don't have metadata. Just chain to the next implementation.
512   return AliasAnalysis::getModRefBehavior(F);
513 }
514
515 AliasAnalysis::ModRefResult
516 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
517                                       const Location &Loc) {
518   if (!EnableTBAA)
519     return AliasAnalysis::getModRefInfo(CS, Loc);
520
521   if (const MDNode *L = Loc.AATags.TBAA)
522     if (const MDNode *M =
523             CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
524       if (!Aliases(L, M))
525         return NoModRef;
526
527   return AliasAnalysis::getModRefInfo(CS, Loc);
528 }
529
530 AliasAnalysis::ModRefResult
531 TypeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS1,
532                                       ImmutableCallSite CS2) {
533   if (!EnableTBAA)
534     return AliasAnalysis::getModRefInfo(CS1, CS2);
535
536   if (const MDNode *M1 =
537           CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
538     if (const MDNode *M2 =
539             CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa))
540       if (!Aliases(M1, M2))
541         return NoModRef;
542
543   return AliasAnalysis::getModRefInfo(CS1, CS2);
544 }
545
546 bool MDNode::isTBAAVtableAccess() const {
547   if (!isStructPathTBAA(this)) {
548     if (getNumOperands() < 1) return false;
549     if (MDString *Tag1 = dyn_cast<MDString>(getOperand(0))) {
550       if (Tag1->getString() == "vtable pointer") return true;
551     }
552     return false;
553   }
554
555   // For struct-path aware TBAA, we use the access type of the tag.
556   if (getNumOperands() < 2) return false;
557   MDNode *Tag = cast_or_null<MDNode>(getOperand(1));
558   if (!Tag) return false;
559   if (MDString *Tag1 = dyn_cast<MDString>(Tag->getOperand(0))) {
560     if (Tag1->getString() == "vtable pointer") return true;
561   }
562   return false;  
563 }
564
565 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
566   if (!A || !B)
567     return nullptr;
568
569   if (A == B)
570     return A;
571
572   // For struct-path aware TBAA, we use the access type of the tag.
573   bool StructPath = isStructPathTBAA(A) && isStructPathTBAA(B);
574   if (StructPath) {
575     A = cast_or_null<MDNode>(A->getOperand(1));
576     if (!A) return nullptr;
577     B = cast_or_null<MDNode>(B->getOperand(1));
578     if (!B) return nullptr;
579   }
580
581   SmallVector<MDNode *, 4> PathA;
582   MDNode *T = A;
583   while (T) {
584     PathA.push_back(T);
585     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1))
586                                  : nullptr;
587   }
588
589   SmallVector<MDNode *, 4> PathB;
590   T = B;
591   while (T) {
592     PathB.push_back(T);
593     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1))
594                                  : nullptr;
595   }
596
597   int IA = PathA.size() - 1;
598   int IB = PathB.size() - 1;
599
600   MDNode *Ret = nullptr;
601   while (IA >= 0 && IB >=0) {
602     if (PathA[IA] == PathB[IB])
603       Ret = PathA[IA];
604     else
605       break;
606     --IA;
607     --IB;
608   }
609   if (!StructPath)
610     return Ret;
611
612   if (!Ret)
613     return nullptr;
614   // We need to convert from a type node to a tag node.
615   Type *Int64 = IntegerType::get(A->getContext(), 64);
616   Metadata *Ops[3] = {Ret, Ret,
617                       ConstantAsMetadata::get(ConstantInt::get(Int64, 0))};
618   return MDNode::get(A->getContext(), Ops);
619 }
620
621 void Instruction::getAAMetadata(AAMDNodes &N, bool Merge) const {
622   if (Merge)
623     N.TBAA =
624         MDNode::getMostGenericTBAA(N.TBAA, getMetadata(LLVMContext::MD_tbaa));
625   else
626     N.TBAA = getMetadata(LLVMContext::MD_tbaa);
627
628   if (Merge)
629     N.Scope = MDNode::getMostGenericAliasScope(
630         N.Scope, getMetadata(LLVMContext::MD_alias_scope));
631   else
632     N.Scope = getMetadata(LLVMContext::MD_alias_scope);
633
634   if (Merge)
635     N.NoAlias =
636         MDNode::intersect(N.NoAlias, getMetadata(LLVMContext::MD_noalias));
637   else
638     N.NoAlias = getMetadata(LLVMContext::MD_noalias);
639 }
640