clang 24.0.0git
CodeGenTBAA.cpp
Go to the documentation of this file.
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"
23#include "clang/AST/Attr.h"
24#include "clang/AST/Mangle.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
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(Ctx, Ctx.getDiagnostics())),
43 MDHelper(M.getContext()), Root(nullptr), Char(nullptr) {}
44
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("Simple C++ TBAA");
56 else
57 Root = MDHelper.createTBAARoot("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(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("omnipotent char", 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(PtrDepth);
106 auto Size = Module.getDataLayout().getPointerSize();
107 // Populate first element.
108 if (AnyPtrs.empty())
109 AnyPtrs.push_back(createScalarTypeNode("any pointer", 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(createScalarTypeNode(Name, 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(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(Ty).getQuantity();
162
163 // Handle builtin types.
164 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(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(Context.ShortTy);
180 case BuiltinType::UInt:
181 return getTypeInfo(Context.IntTy);
182 case BuiltinType::ULong:
183 return getTypeInfo(Context.LongTy);
184 case BuiltinType::ULongLong:
185 return getTypeInfo(Context.LongLongTy);
186 case BuiltinType::UInt128:
187 return getTypeInfo(Context.Int128Ty);
188
189 case BuiltinType::UShortFract:
190 return getTypeInfo(Context.ShortFractTy);
191 case BuiltinType::UFract:
192 return getTypeInfo(Context.FractTy);
193 case BuiltinType::ULongFract:
194 return getTypeInfo(Context.LongFractTy);
195
196 case BuiltinType::SatUShortFract:
197 return getTypeInfo(Context.SatShortFractTy);
198 case BuiltinType::SatUFract:
199 return getTypeInfo(Context.SatFractTy);
200 case BuiltinType::SatULongFract:
201 return getTypeInfo(Context.SatLongFractTy);
202
203 case BuiltinType::UShortAccum:
204 return getTypeInfo(Context.ShortAccumTy);
205 case BuiltinType::UAccum:
206 return getTypeInfo(Context.AccumTy);
207 case BuiltinType::ULongAccum:
208 return getTypeInfo(Context.LongAccumTy);
209
210 case BuiltinType::SatUShortAccum:
211 return getTypeInfo(Context.SatShortAccumTy);
212 case BuiltinType::SatUAccum:
213 return getTypeInfo(Context.SatAccumTy);
214 case BuiltinType::SatULongAccum:
215 return getTypeInfo(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(BTy->getName(Features), 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++;
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>(Ty)) {
289 llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty);
290 StringRef Name =
292 ScalarMD->getOperand(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(QualType(Ty, 0), TyOut);
320 }
321
322 SmallString<256> OutName("p");
323 OutName += std::to_string(PtrDepth);
324 OutName += " ";
325 OutName += TyName;
326 return createScalarTypeNode(OutName, 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(cast<ArrayType>(Ty)->getElementType());
332
333 // Accesses to matrix types are accesses to objects of their element types.
334 if (const auto *MTy = dyn_cast<MatrixType>(Ty)) {
335 assert(isa<ConstantMatrixType>(Ty) &&
336 "only ConstantMatrixType should reach CodeGen");
337 return getTypeInfo(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>(Ty))
342 return getTypeInfo(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>(Ty)) {
347 const EnumDecl *ED = ETy->getDecl()->getDefinitionOrSelf();
348 if (!Features.CPlusPlus)
349 return getTypeInfo(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 QualType(ETy, 0), Out);
362 return createScalarTypeNode(OutName, getChar(), Size);
363 }
364
365 if (const auto *EIT = dyn_cast<BitIntType>(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(OutName, getChar(), Size);
372 }
373
374 // For now, handle any other kind of type conservatively.
375 return getChar();
376}
377
379 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types (unless
380 // we're running TypeSanitizer).
381 if (!Features.Sanitize.has(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(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
412 // Pointee values may have incomplete types, but they shall never be
413 // dereferenced.
414 if (AccessType->isIncompleteType())
416
417 if (TypeHasMayAlias(AccessType))
419
420 uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
421 return TBAAAccessInfo(getTypeInfo(AccessType), Size);
422}
423
425 const llvm::DataLayout &DL = Module.getDataLayout();
426 unsigned Size = DL.getPointerTypeSize(VTablePtrType);
427 return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
428 Size);
429}
430
431bool
432CodeGenTBAA::CollectFields(uint64_t BaseOffset,
433 QualType QTy,
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(QTy).getQuantity();
442 llvm::MDNode *TBAAType = getChar();
443 llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
444 Fields.push_back(
445 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>(RD))
454 if (!Decl->bases().empty())
455 return false;
456
457 const ASTRecordLayout &Layout = Context.getASTRecordLayout(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) {
464 continue;
465
466 uint64_t Offset =
467 BaseOffset + Layout.getFieldOffset(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(*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(CurrentBitFieldSize, Context.getCharWidth());
484 llvm::MDNode *TBAAType = getChar();
485 llvm::MDNode *TBAATag =
486 getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
487 Fields.push_back(
488 llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
489 continue;
490 }
491
492 QualType FieldQTy = i->getType();
493 if (!CollectFields(Offset, FieldQTy, Fields,
494 MayAlias || TypeHasMayAlias(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(ElemQTy).getQuantity();
503 llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(ElemQTy);
504 llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
505 Fields.push_back(
506 llvm::MDBuilder::TBAAStructField(BaseOffset, Size, TBAATag));
507 Fields.push_back(
508 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(QTy).getQuantity();
515 llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
516 llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
517 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
518 return true;
519}
520
521llvm::MDNode *
523 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
524 return nullptr;
525
526 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
527
528 if (llvm::MDNode *N = StructMetadataCache[Ty])
529 return N;
530
532 if (CollectFields(0, QTy, Fields, 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>(Ty)) {
541 const RecordDecl *RD = TTy->getDecl()->getDefinition();
542 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
543 using TBAAStructField = llvm::MDBuilder::TBAAStructField;
545 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(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(BaseQTy)
559 ? getValidBaseTypeInfo(BaseQTy)
560 : getTypeInfo(BaseQTy);
561 if (!TypeNode)
562 return nullptr;
563 uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
564 uint64_t Size =
565 Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
566 Fields.push_back(
567 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(Fields,
575 [](const TBAAStructField &A, const TBAAStructField &B) {
576 return A.Offset < B.Offset;
577 });
578 }
579 for (FieldDecl *Field : RD->fields()) {
580 if (Field->isZeroSize(Context) || Field->isUnnamedBitField())
581 continue;
582 QualType FieldQTy = Field->getType();
583 llvm::MDNode *TypeNode = isValidBaseType(FieldQTy)
584 ? getValidBaseTypeInfo(FieldQTy)
585 : getTypeInfo(FieldQTy);
586 if (!TypeNode)
587 return nullptr;
588
589 uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
590 uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
591 uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
592 Fields.push_back(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 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(Ty).getQuantity();
609 llvm::Metadata *Id = MDHelper.createString(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(std::make_pair(Field.Type, Field.Offset));
617 return MDHelper.createTBAAStructTypeNode(OutName, 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(QTy).getTypePtr();
627
628 // nullptr is a valid value in the cache, so use find rather than []
629 auto I = BaseTypeMetadataCache.find(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({Ty, TypeNode});
637 assert(inserted.second && "BaseType metadata was already inserted");
638
639 return TypeNode;
640}
641
643 return isValidBaseType(QTy) ? getValidBaseTypeInfo(QTy) : nullptr;
644}
645
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(Info.BaseType, Info.AccessType,
668 Info.Offset, Info.Size);
669 }
670 return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
671 Info.Offset);
672}
673
680
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())
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.
697}
698
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())
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.
715}
Defines the clang::ASTContext interface.
static bool TypeHasMayAlias(QualType QTy)
static bool isValidBaseType(QualType QTy)
Check if the given type is a valid base type to be used in access tags.
unsigned IsFirst
Indicates that this is the first token of the file.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1196
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
llvm::MDNode * getBaseTypeInfo(QualType QTy)
getBaseTypeInfo - Get metadata that describes the given base access type.
llvm::MDNode * getTypeInfo(QualType QTy)
getTypeInfo - Get metadata used to describe accesses to objects of the given type.
TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table pointer...
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, TBAAAccessInfo SrcInfo)
mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the purpose of memory transfer calls...
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purpose of type casts.
llvm::MDNode * getChar()
getChar - This is the mdnode for "char", which is special, and any types considered to be equivalent ...
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purpose of conditional oper...
llvm::MDNode * getAccessTagInfo(TBAAAccessInfo Info)
getAccessTagInfo - Get TBAA tag for a given memory access.
llvm::MDNode * getTBAAStructInfo(QualType QTy)
getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of the given type.
CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M, const CodeGenOptions &CGO, const LangOptions &Features)
TBAAAccessInfo getAccessInfo(QualType AccessType)
getAccessInfo - Get TBAA information that describes an access to an object of the given type.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
QualType getIntegerType() const
Return the integer type this enum decl corresponds to.
Definition Decl.h:4367
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes a module or submodule.
Definition Module.h:340
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:303
bool isExternallyVisible() const
Definition Decl.h:435
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4508
bool hasFlexibleArrayMember() const
Definition Decl.h:4541
field_iterator field_end() const
Definition Decl.h:4714
field_range fields() const
Definition Decl.h:4711
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition Decl.h:4692
specific_decl_iterator< FieldDecl > field_iterator
Definition Decl.h:4708
field_iterator field_begin() const
Definition Decl.cpp:5412
Exposes information about the current target.
Definition TargetInfo.h:228
bool isVoidType() const
Definition TypeBase.h:9200
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8914
bool isPointerType() const
Definition TypeBase.h:8815
bool isReferenceType() const
Definition TypeBase.h:8839
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition Type.h:63
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition TypeBase.h:9384
bool isStdByteType() const
Definition Type.cpp:3449
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9484
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition Type.cpp:2655
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:3002
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9431
Defines the clang::TargetInfo interface.
bool isEmptyFieldForLayout(const ASTContext &Ctx, const FieldDecl *FD)
Return true iff the field is "empty", that is, either a zero-width bit-field or an isEmptyRecordForLa...
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
Definition CGValue.h:155
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327
Structure with information about how a bitfield should be accessed.
unsigned Offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the L...
unsigned Size
The total size of the bit-field, in bits, not including padding.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
llvm::MDNode * AccessType
AccessType - The final access type.
uint64_t Offset
Offset - The byte offset of the final access within the base one.
static TBAAAccessInfo getMayAliasInfo()
Definition CodeGenTBAA.h:63
uint64_t Size
Size - The size of access, in bytes.
static TBAAAccessInfo getIncompleteInfo()
Definition CodeGenTBAA.h:71
llvm::MDNode * BaseType
BaseType - The base/leading access type.