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