xref: /llvm-project/clang/lib/CodeGen/CodeGenTBAA.cpp (revision c135f6ffe2542bdde5a2a3e1d6515a6fc7031967)
1 //===-- CodeGenTBAA.cpp - TBAA information for LLVM CodeGen ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is the code that manages TBAA information and defines the TBAA policy
10 // for the optimizer to use. Relevant standards text includes:
11 //
12 //   C99 6.5p7
13 //   C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "CodeGenTBAA.h"
18 #include "ABIInfoImpl.h"
19 #include "CGCXXABI.h"
20 #include "CGRecordLayout.h"
21 #include "CodeGenTypes.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Attr.h"
24 #include "clang/AST/Mangle.h"
25 #include "clang/AST/RecordLayout.h"
26 #include "clang/Basic/CodeGenOptions.h"
27 #include "clang/Basic/TargetInfo.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
32 #include "llvm/Support/Debug.h"
33 using namespace clang;
34 using namespace CodeGen;
35 
36 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes,
37                          llvm::Module &M, const CodeGenOptions &CGO,
38                          const LangOptions &Features)
39     : Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO),
40       Features(Features),
41       MangleCtx(ItaniumMangleContext::create(Ctx, Ctx.getDiagnostics())),
42       MDHelper(M.getContext()), Root(nullptr), Char(nullptr) {}
43 
44 CodeGenTBAA::~CodeGenTBAA() {
45 }
46 
47 llvm::MDNode *CodeGenTBAA::getRoot() {
48   // Define the root of the tree. This identifies the tree, so that
49   // if our LLVM IR is linked with LLVM IR from a different front-end
50   // (or a different version of this front-end), their TBAA trees will
51   // remain distinct, and the optimizer will treat them conservatively.
52   if (!Root) {
53     if (Features.CPlusPlus)
54       Root = MDHelper.createTBAARoot("Simple C++ TBAA");
55     else
56       Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
57   }
58 
59   return Root;
60 }
61 
62 llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
63                                                 llvm::MDNode *Parent,
64                                                 uint64_t Size) {
65   if (CodeGenOpts.NewStructPathTBAA) {
66     llvm::Metadata *Id = MDHelper.createString(Name);
67     return MDHelper.createTBAATypeNode(Parent, Size, Id);
68   }
69   return MDHelper.createTBAAScalarTypeNode(Name, Parent);
70 }
71 
72 llvm::MDNode *CodeGenTBAA::getChar() {
73   // Define the root of the tree for user-accessible memory. C and C++
74   // give special powers to char and certain similar types. However,
75   // these special powers only cover user-accessible memory, and doesn't
76   // include things like vtables.
77   if (!Char)
78     Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1);
79 
80   return Char;
81 }
82 
83 static bool TypeHasMayAlias(QualType QTy) {
84   // Tagged types have declarations, and therefore may have attributes.
85   if (auto *TD = QTy->getAsTagDecl())
86     if (TD->hasAttr<MayAliasAttr>())
87       return true;
88 
89   // Also look for may_alias as a declaration attribute on a typedef.
90   // FIXME: We should follow GCC and model may_alias as a type attribute
91   // rather than as a declaration attribute.
92   while (auto *TT = QTy->getAs<TypedefType>()) {
93     if (TT->getDecl()->hasAttr<MayAliasAttr>())
94       return true;
95     QTy = TT->desugar();
96   }
97   return false;
98 }
99 
100 /// Check if the given type is a valid base type to be used in access tags.
101 static bool isValidBaseType(QualType QTy) {
102   if (const RecordType *TTy = QTy->getAs<RecordType>()) {
103     const RecordDecl *RD = TTy->getDecl()->getDefinition();
104     // Incomplete types are not valid base access types.
105     if (!RD)
106       return false;
107     if (RD->hasFlexibleArrayMember())
108       return false;
109     // RD can be struct, union, class, interface or enum.
110     // For now, we only handle struct and class.
111     if (RD->isStruct() || RD->isClass())
112       return true;
113   }
114   return false;
115 }
116 
117 llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
118   uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
119 
120   // Handle builtin types.
121   if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
122     switch (BTy->getKind()) {
123     // Character types are special and can alias anything.
124     // In C++, this technically only includes "char" and "unsigned char",
125     // and not "signed char". In C, it includes all three. For now,
126     // the risk of exploiting this detail in C++ seems likely to outweigh
127     // the benefit.
128     case BuiltinType::Char_U:
129     case BuiltinType::Char_S:
130     case BuiltinType::UChar:
131     case BuiltinType::SChar:
132       return getChar();
133 
134     // Unsigned types can alias their corresponding signed types.
135     case BuiltinType::UShort:
136       return getTypeInfo(Context.ShortTy);
137     case BuiltinType::UInt:
138       return getTypeInfo(Context.IntTy);
139     case BuiltinType::ULong:
140       return getTypeInfo(Context.LongTy);
141     case BuiltinType::ULongLong:
142       return getTypeInfo(Context.LongLongTy);
143     case BuiltinType::UInt128:
144       return getTypeInfo(Context.Int128Ty);
145 
146     case BuiltinType::UShortFract:
147       return getTypeInfo(Context.ShortFractTy);
148     case BuiltinType::UFract:
149       return getTypeInfo(Context.FractTy);
150     case BuiltinType::ULongFract:
151       return getTypeInfo(Context.LongFractTy);
152 
153     case BuiltinType::SatUShortFract:
154       return getTypeInfo(Context.SatShortFractTy);
155     case BuiltinType::SatUFract:
156       return getTypeInfo(Context.SatFractTy);
157     case BuiltinType::SatULongFract:
158       return getTypeInfo(Context.SatLongFractTy);
159 
160     case BuiltinType::UShortAccum:
161       return getTypeInfo(Context.ShortAccumTy);
162     case BuiltinType::UAccum:
163       return getTypeInfo(Context.AccumTy);
164     case BuiltinType::ULongAccum:
165       return getTypeInfo(Context.LongAccumTy);
166 
167     case BuiltinType::SatUShortAccum:
168       return getTypeInfo(Context.SatShortAccumTy);
169     case BuiltinType::SatUAccum:
170       return getTypeInfo(Context.SatAccumTy);
171     case BuiltinType::SatULongAccum:
172       return getTypeInfo(Context.SatLongAccumTy);
173 
174     // Treat all other builtin types as distinct types. This includes
175     // treating wchar_t, char16_t, and char32_t as distinct from their
176     // "underlying types".
177     default:
178       return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
179     }
180   }
181 
182   // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
183   // an object through a glvalue of other than one of the following types the
184   // behavior is undefined: [...] a char, unsigned char, or std::byte type."
185   if (Ty->isStdByteType())
186     return getChar();
187 
188   // Handle pointers and references.
189   //
190   // C has a very strict rule for pointer aliasing. C23 6.7.6.1p2:
191   //     For two pointer types to be compatible, both shall be identically
192   //     qualified and both shall be pointers to compatible types.
193   //
194   // This rule is impractically strict; we want to at least ignore CVR
195   // qualifiers. Distinguishing by CVR qualifiers would make it UB to
196   // e.g. cast a `char **` to `const char * const *` and dereference it,
197   // which is too common and useful to invalidate. C++'s similar types
198   // rule permits qualifier differences in these nested positions; in fact,
199   // C++ even allows that cast as an implicit conversion.
200   //
201   // Other qualifiers could theoretically be distinguished, especially if
202   // they involve a significant representation difference.  We don't
203   // currently do so, however.
204   if (Ty->isPointerType() || Ty->isReferenceType()) {
205     llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), Size);
206     if (!CodeGenOpts.PointerTBAA)
207       return AnyPtr;
208     // C++ [basic.lval]p11 permits objects to accessed through an l-value of
209     // similar type. Two types are similar under C++ [conv.qual]p2 if the
210     // decomposition of the types into pointers, member pointers, and arrays has
211     // the same structure when ignoring cv-qualifiers at each level of the
212     // decomposition. Meanwhile, C makes T(*)[] and T(*)[N] compatible, which
213     // would really complicate any attempt to distinguish pointers to arrays by
214     // their bounds. It's simpler, and much easier to explain to users, to
215     // simply treat all pointers to arrays as pointers to their element type for
216     // aliasing purposes. So when creating a TBAA tag for a pointer type, we
217     // recursively ignore both qualifiers and array types when decomposing the
218     // pointee type. The only meaningful remaining structure is the number of
219     // pointer types we encountered along the way, so we just produce the tag
220     // "p<depth> <base type tag>". If we do find a member pointer type, for now
221     // we just conservatively bail out with AnyPtr (below) rather than trying to
222     // create a tag that honors the similar-type rules while still
223     // distinguishing different kinds of member pointer.
224     unsigned PtrDepth = 0;
225     do {
226       PtrDepth++;
227       Ty = Ty->getPointeeType()->getBaseElementTypeUnsafe();
228     } while (Ty->isPointerType());
229     assert(!isa<VariableArrayType>(Ty));
230     // When the underlying type is a builtin type, we compute the pointee type
231     // string recursively, which is implicitly more forgiving than the standards
232     // require.  Effectively, we are turning the question "are these types
233     // compatible/similar" into "are accesses to these types allowed to alias".
234     // In both C and C++, the latter question has special carve-outs for
235     // signedness mismatches that only apply at the top level.  As a result, we
236     // are allowing e.g. `int *` l-values to access `unsigned *` objects.
237     SmallString<256> TyName;
238     if (isa<BuiltinType>(Ty)) {
239       llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty);
240       StringRef Name =
241           cast<llvm::MDString>(
242               ScalarMD->getOperand(CodeGenOpts.NewStructPathTBAA ? 2 : 0))
243               ->getString();
244       TyName = Name;
245     } else {
246       // Be conservative if the type isn't a RecordType. We are specifically
247       // required to do this for member pointers until we implement the
248       // similar-types rule.
249       const auto *RT = Ty->getAs<RecordType>();
250       if (!RT)
251         return AnyPtr;
252 
253       // For unnamed structs or unions C's compatible types rule applies. Two
254       // compatible types in different compilation units can have different
255       // mangled names, meaning the metadata emitted below would incorrectly
256       // mark them as no-alias. Use AnyPtr for such types in both C and C++, as
257       // C and C++ types may be visible when doing LTO.
258       //
259       // Note that using AnyPtr is overly conservative. We could summarize the
260       // members of the type, as per the C compatibility rule in the future.
261       // This also covers anonymous structs and unions, which have a different
262       // compatibility rule, but it doesn't matter because you can never have a
263       // pointer to an anonymous struct or union.
264       if (!RT->getDecl()->getDeclName())
265         return AnyPtr;
266 
267       // For non-builtin types use the mangled name of the canonical type.
268       llvm::raw_svector_ostream TyOut(TyName);
269       MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);
270     }
271 
272     SmallString<256> OutName("p");
273     OutName += std::to_string(PtrDepth);
274     OutName += " ";
275     OutName += TyName;
276     return createScalarTypeNode(OutName, AnyPtr, Size);
277   }
278 
279   // Accesses to arrays are accesses to objects of their element types.
280   if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
281     return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
282 
283   // Enum types are distinct types. In C++ they have "underlying types",
284   // however they aren't related for TBAA.
285   if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
286     if (!Features.CPlusPlus)
287       return getTypeInfo(ETy->getDecl()->getIntegerType());
288 
289     // In C++ mode, types have linkage, so we can rely on the ODR and
290     // on their mangled names, if they're external.
291     // TODO: Is there a way to get a program-wide unique name for a
292     // decl with local linkage or no linkage?
293     if (!ETy->getDecl()->isExternallyVisible())
294       return getChar();
295 
296     SmallString<256> OutName;
297     llvm::raw_svector_ostream Out(OutName);
298     CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
299         QualType(ETy, 0), Out);
300     return createScalarTypeNode(OutName, getChar(), Size);
301   }
302 
303   if (const auto *EIT = dyn_cast<BitIntType>(Ty)) {
304     SmallString<256> OutName;
305     llvm::raw_svector_ostream Out(OutName);
306     // Don't specify signed/unsigned since integer types can alias despite sign
307     // differences.
308     Out << "_BitInt(" << EIT->getNumBits() << ')';
309     return createScalarTypeNode(OutName, getChar(), Size);
310   }
311 
312   // For now, handle any other kind of type conservatively.
313   return getChar();
314 }
315 
316 llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
317   // At -O0 or relaxed aliasing, TBAA is not emitted for regular types (unless
318   // we're running TypeSanitizer).
319   if (!Features.Sanitize.has(SanitizerKind::Type) &&
320       (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing))
321     return nullptr;
322 
323   // If the type has the may_alias attribute (even on a typedef), it is
324   // effectively in the general char alias class.
325   if (TypeHasMayAlias(QTy))
326     return getChar();
327 
328   // We need this function to not fall back to returning the "omnipotent char"
329   // type node for aggregate and union types. Otherwise, any dereference of an
330   // aggregate will result into the may-alias access descriptor, meaning all
331   // subsequent accesses to direct and indirect members of that aggregate will
332   // be considered may-alias too.
333   // TODO: Combine getTypeInfo() and getValidBaseTypeInfo() into a single
334   // function.
335   if (isValidBaseType(QTy))
336     return getValidBaseTypeInfo(QTy);
337 
338   const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
339   if (llvm::MDNode *N = MetadataCache[Ty])
340     return N;
341 
342   // Note that the following helper call is allowed to add new nodes to the
343   // cache, which invalidates all its previously obtained iterators. So we
344   // first generate the node for the type and then add that node to the cache.
345   llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
346   return MetadataCache[Ty] = TypeNode;
347 }
348 
349 TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
350   // Pointee values may have incomplete types, but they shall never be
351   // dereferenced.
352   if (AccessType->isIncompleteType())
353     return TBAAAccessInfo::getIncompleteInfo();
354 
355   if (TypeHasMayAlias(AccessType))
356     return TBAAAccessInfo::getMayAliasInfo();
357 
358   uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
359   return TBAAAccessInfo(getTypeInfo(AccessType), Size);
360 }
361 
362 TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
363   const llvm::DataLayout &DL = Module.getDataLayout();
364   unsigned Size = DL.getPointerTypeSize(VTablePtrType);
365   return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
366                         Size);
367 }
368 
369 bool
370 CodeGenTBAA::CollectFields(uint64_t BaseOffset,
371                            QualType QTy,
372                            SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
373                              Fields,
374                            bool MayAlias) {
375   /* Things not handled yet include: C++ base classes, bitfields, */
376 
377   if (const RecordType *TTy = QTy->getAs<RecordType>()) {
378     if (TTy->isUnionType()) {
379       uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
380       llvm::MDNode *TBAAType = getChar();
381       llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
382       Fields.push_back(
383           llvm::MDBuilder::TBAAStructField(BaseOffset, Size, TBAATag));
384       return true;
385     }
386     const RecordDecl *RD = TTy->getDecl()->getDefinition();
387     if (RD->hasFlexibleArrayMember())
388       return false;
389 
390     // TODO: Handle C++ base classes.
391     if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
392       if (Decl->bases_begin() != Decl->bases_end())
393         return false;
394 
395     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
396     const CGRecordLayout &CGRL = CGTypes.getCGRecordLayout(RD);
397 
398     unsigned idx = 0;
399     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
400          i != e; ++i, ++idx) {
401       if (isEmptyFieldForLayout(Context, *i))
402         continue;
403 
404       uint64_t Offset =
405           BaseOffset + Layout.getFieldOffset(idx) / Context.getCharWidth();
406 
407       // Create a single field for consecutive named bitfields using char as
408       // base type.
409       if ((*i)->isBitField()) {
410         const CGBitFieldInfo &Info = CGRL.getBitFieldInfo(*i);
411         // For big endian targets the first bitfield in the consecutive run is
412         // at the most-significant end; see CGRecordLowering::setBitFieldInfo
413         // for more information.
414         bool IsBE = Context.getTargetInfo().isBigEndian();
415         bool IsFirst = IsBE ? Info.StorageSize - (Info.Offset + Info.Size) == 0
416                             : Info.Offset == 0;
417         if (!IsFirst)
418           continue;
419         unsigned CurrentBitFieldSize = Info.StorageSize;
420         uint64_t Size =
421             llvm::divideCeil(CurrentBitFieldSize, Context.getCharWidth());
422         llvm::MDNode *TBAAType = getChar();
423         llvm::MDNode *TBAATag =
424             getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
425         Fields.push_back(
426             llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
427         continue;
428       }
429 
430       QualType FieldQTy = i->getType();
431       if (!CollectFields(Offset, FieldQTy, Fields,
432                          MayAlias || TypeHasMayAlias(FieldQTy)))
433         return false;
434     }
435     return true;
436   }
437 
438   /* Otherwise, treat whatever it is as a field. */
439   uint64_t Offset = BaseOffset;
440   uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
441   llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
442   llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
443   Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
444   return true;
445 }
446 
447 llvm::MDNode *
448 CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
449   if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
450     return nullptr;
451 
452   const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
453 
454   if (llvm::MDNode *N = StructMetadataCache[Ty])
455     return N;
456 
457   SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
458   if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
459     return MDHelper.createTBAAStructNode(Fields);
460 
461   // For now, handle any other kind of type conservatively.
462   return StructMetadataCache[Ty] = nullptr;
463 }
464 
465 llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
466   if (auto *TTy = dyn_cast<RecordType>(Ty)) {
467     const RecordDecl *RD = TTy->getDecl()->getDefinition();
468     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
469     using TBAAStructField = llvm::MDBuilder::TBAAStructField;
470     SmallVector<TBAAStructField, 4> Fields;
471     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
472       // Handle C++ base classes. Non-virtual bases can treated a kind of
473       // field. Virtual bases are more complex and omitted, but avoid an
474       // incomplete view for NewStructPathTBAA.
475       if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0)
476         return nullptr;
477       for (const CXXBaseSpecifier &B : CXXRD->bases()) {
478         if (B.isVirtual())
479           continue;
480         QualType BaseQTy = B.getType();
481         const CXXRecordDecl *BaseRD = BaseQTy->getAsCXXRecordDecl();
482         if (BaseRD->isEmpty())
483           continue;
484         llvm::MDNode *TypeNode = isValidBaseType(BaseQTy)
485                                      ? getValidBaseTypeInfo(BaseQTy)
486                                      : getTypeInfo(BaseQTy);
487         if (!TypeNode)
488           return nullptr;
489         uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
490         uint64_t Size =
491             Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
492         Fields.push_back(
493             llvm::MDBuilder::TBAAStructField(Offset, Size, TypeNode));
494       }
495       // The order in which base class subobjects are allocated is unspecified,
496       // so may differ from declaration order. In particular, Itanium ABI will
497       // allocate a primary base first.
498       // Since we exclude empty subobjects, the objects are not overlapping and
499       // their offsets are unique.
500       llvm::sort(Fields,
501                  [](const TBAAStructField &A, const TBAAStructField &B) {
502                    return A.Offset < B.Offset;
503                  });
504     }
505     for (FieldDecl *Field : RD->fields()) {
506       if (Field->isZeroSize(Context) || Field->isUnnamedBitField())
507         continue;
508       QualType FieldQTy = Field->getType();
509       llvm::MDNode *TypeNode = isValidBaseType(FieldQTy)
510                                    ? getValidBaseTypeInfo(FieldQTy)
511                                    : getTypeInfo(FieldQTy);
512       if (!TypeNode)
513         return nullptr;
514 
515       uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
516       uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
517       uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
518       Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
519                                                         TypeNode));
520     }
521 
522     SmallString<256> OutName;
523     if (Features.CPlusPlus) {
524       // Don't use the mangler for C code.
525       llvm::raw_svector_ostream Out(OutName);
526       CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
527           QualType(Ty, 0), Out);
528     } else {
529       OutName = RD->getName();
530     }
531 
532     if (CodeGenOpts.NewStructPathTBAA) {
533       llvm::MDNode *Parent = getChar();
534       uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
535       llvm::Metadata *Id = MDHelper.createString(OutName);
536       return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
537     }
538 
539     // Create the struct type node with a vector of pairs (offset, type).
540     SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
541     for (const auto &Field : Fields)
542         OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
543     return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
544   }
545 
546   return nullptr;
547 }
548 
549 llvm::MDNode *CodeGenTBAA::getValidBaseTypeInfo(QualType QTy) {
550   assert(isValidBaseType(QTy) && "Must be a valid base type");
551 
552   const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
553 
554   // nullptr is a valid value in the cache, so use find rather than []
555   auto I = BaseTypeMetadataCache.find(Ty);
556   if (I != BaseTypeMetadataCache.end())
557     return I->second;
558 
559   // First calculate the metadata, before recomputing the insertion point, as
560   // the helper can recursively call us.
561   llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
562   LLVM_ATTRIBUTE_UNUSED auto inserted =
563       BaseTypeMetadataCache.insert({Ty, TypeNode});
564   assert(inserted.second && "BaseType metadata was already inserted");
565 
566   return TypeNode;
567 }
568 
569 llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
570   return isValidBaseType(QTy) ? getValidBaseTypeInfo(QTy) : nullptr;
571 }
572 
573 llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
574   assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
575 
576   if (Info.isMayAlias())
577     Info = TBAAAccessInfo(getChar(), Info.Size);
578 
579   if (!Info.AccessType)
580     return nullptr;
581 
582   if (!CodeGenOpts.StructPathTBAA)
583     Info = TBAAAccessInfo(Info.AccessType, Info.Size);
584 
585   llvm::MDNode *&N = AccessTagMetadataCache[Info];
586   if (N)
587     return N;
588 
589   if (!Info.BaseType) {
590     Info.BaseType = Info.AccessType;
591     assert(!Info.Offset && "Nonzero offset for an access with no base type!");
592   }
593   if (CodeGenOpts.NewStructPathTBAA) {
594     return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
595                                             Info.Offset, Info.Size);
596   }
597   return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
598                                               Info.Offset);
599 }
600 
601 TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
602                                                  TBAAAccessInfo TargetInfo) {
603   if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
604     return TBAAAccessInfo::getMayAliasInfo();
605   return TargetInfo;
606 }
607 
608 TBAAAccessInfo
609 CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
610                                                  TBAAAccessInfo InfoB) {
611   if (InfoA == InfoB)
612     return InfoA;
613 
614   if (!InfoA || !InfoB)
615     return TBAAAccessInfo();
616 
617   if (InfoA.isMayAlias() || InfoB.isMayAlias())
618     return TBAAAccessInfo::getMayAliasInfo();
619 
620   // TODO: Implement the rest of the logic here. For example, two accesses
621   // with same final access types result in an access to an object of that final
622   // access type regardless of their base types.
623   return TBAAAccessInfo::getMayAliasInfo();
624 }
625 
626 TBAAAccessInfo
627 CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
628                                             TBAAAccessInfo SrcInfo) {
629   if (DestInfo == SrcInfo)
630     return DestInfo;
631 
632   if (!DestInfo || !SrcInfo)
633     return TBAAAccessInfo();
634 
635   if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
636     return TBAAAccessInfo::getMayAliasInfo();
637 
638   // TODO: Implement the rest of the logic here. For example, two accesses
639   // with same final access types result in an access to an object of that final
640   // access type regardless of their base types.
641   return TBAAAccessInfo::getMayAliasInfo();
642 }
643