clang 20.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"
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"
33using namespace clang;
34using namespace CodeGen;
35
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
45}
46
47llvm::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
62llvm::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
72llvm::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
83static 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.
101static 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
117llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
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++;
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);
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
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
350 // Pointee values may have incomplete types, but they shall never be
351 // dereferenced.
352 if (AccessType->isIncompleteType())
354
355 if (TypeHasMayAlias(AccessType))
357
358 uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
359 return TBAAAccessInfo(getTypeInfo(AccessType), Size);
360}
361
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
369bool
370CodeGenTBAA::CollectFields(uint64_t BaseOffset,
371 QualType QTy,
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;
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
447llvm::MDNode *
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
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
465llvm::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;
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);
527 QualType(Ty, 0), Out);
528 } else {
529 OutName = RD->getName();
530 }
531
532 if (CodeGenOpts.NewStructPathTBAA) {
533 llvm::MDNode *Parent = getChar();
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).
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
549llvm::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
570 return isValidBaseType(QTy) ? getValidBaseTypeInfo(QTy) : nullptr;
571}
572
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
603 if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
605 return TargetInfo;
606}
607
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())
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.
624}
625
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())
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.
642}
Defines the clang::ASTContext interface.
NodeId Parent
Definition: ASTDiff.cpp:191
static bool TypeHasMayAlias(QualType QTy)
Definition: CodeGenTBAA.cpp:83
static bool isValidBaseType(QualType QTy)
Check if the given type is a valid base type to be used in access tags.
uint32_t Id
Definition: SemaARM.cpp:1134
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
CanQualType AccumTy
Definition: ASTContext.h:1173
CanQualType LongTy
Definition: ASTContext.h:1169
CanQualType Int128Ty
Definition: ASTContext.h:1169
CanQualType SatAccumTy
Definition: ASTContext.h:1178
CanQualType ShortAccumTy
Definition: ASTContext.h:1173
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2716
CanQualType SatLongAccumTy
Definition: ASTContext.h:1178
CanQualType SatShortFractTy
Definition: ASTContext.h:1181
CanQualType ShortFractTy
Definition: ASTContext.h:1176
CanQualType IntTy
Definition: ASTContext.h:1169
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CanQualType ShortTy
Definition: ASTContext.h:1169
CanQualType FractTy
Definition: ASTContext.h:1176
CanQualType LongAccumTy
Definition: ASTContext.h:1174
CanQualType SatFractTy
Definition: ASTContext.h:1181
CanQualType SatLongFractTy
Definition: ASTContext.h:1181
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:799
CanQualType LongFractTy
Definition: ASTContext.h:1176
CanQualType SatShortAccumTy
Definition: ASTContext.h:1178
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
CanQualType LongLongTy
Definition: ASTContext.h:1169
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2486
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:200
CharUnits getDataSize() const
getDataSize() - Get the record data size, which is the record size without tail padding,...
Definition: RecordLayout.h:206
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:249
This class is used for builtin types like 'int'.
Definition: Type.h:3034
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:1198
const T * getTypePtr() const
Retrieve the underlying type pointer, which refers to a canonical type.
Definition: CanonicalType.h:84
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:185
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:113
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.
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)
Definition: CodeGenTBAA.cpp:36
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.
Definition: CodeGenTypes.h:54
CGCXXABI & getCXXABI() const
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext,...
Definition: DeclBase.h:2369
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:6098
Represents a member of a struct/union/class.
Definition: Decl.h:3033
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:505
virtual void mangleCanonicalTypeName(QualType T, raw_ostream &, bool NormalizeIntegers=false)=0
Generates a unique string for an externally visible type for use with TBAA or type uniquing.
Describes a module or submodule.
Definition: Module.h:115
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:280
A (possibly-)qualified type.
Definition: Type.h:929
Represents a struct/union/class.
Definition: Decl.h:4148
bool hasFlexibleArrayMember() const
Definition: Decl.h:4181
field_iterator field_end() const
Definition: Decl.h:4357
field_range fields() const
Definition: Decl.h:4354
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
Definition: Decl.h:4339
field_iterator field_begin() const
Definition: Decl.cpp:5092
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
bool isStruct() const
Definition: Decl.h:3767
bool isClass() const
Definition: Decl.h:3769
Exposes information about the current target.
Definition: TargetInfo.h:220
bool isBigEndian() const
Definition: TargetInfo.h:1665
The base class of the type hierarchy.
Definition: Type.h:1828
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
bool isArrayType() const
Definition: Type.h:8258
bool isPointerType() const
Definition: Type.h:8186
bool isReferenceType() const
Definition: Type.h:8204
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:738
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:8681
bool isStdByteType() const
Definition: Type.cpp:3124
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2396
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
Definition: Type.cpp:1924
Defines the clang::TargetInfo interface.
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD)
isEmptyFieldForLayout - Return true iff the field is "empty", that is, either a zero-width bit-field ...
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
unsigned long uint64_t
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.
unsigned StorageSize
The storage size in bits which should be used when accessing this bitfield.
llvm::MDNode * AccessType
AccessType - The final access type.
Definition: CodeGenTBAA.h:105
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:109
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:63
uint64_t Size
Size - The size of access, in bytes.
Definition: CodeGenTBAA.h:112
static TBAAAccessInfo getIncompleteInfo()
Definition: CodeGenTBAA.h:71
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:101
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:159