1e5dd7070Spatrick //===-- CodeGenTBAA.cpp - TBAA information for LLVM CodeGen ---------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This is the code that manages TBAA information and defines the TBAA policy
10e5dd7070Spatrick // for the optimizer to use. Relevant standards text includes:
11e5dd7070Spatrick //
12e5dd7070Spatrick // C99 6.5p7
13e5dd7070Spatrick // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
14e5dd7070Spatrick //
15e5dd7070Spatrick //===----------------------------------------------------------------------===//
16e5dd7070Spatrick
17e5dd7070Spatrick #include "CodeGenTBAA.h"
18e5dd7070Spatrick #include "clang/AST/ASTContext.h"
19e5dd7070Spatrick #include "clang/AST/Attr.h"
20e5dd7070Spatrick #include "clang/AST/Mangle.h"
21e5dd7070Spatrick #include "clang/AST/RecordLayout.h"
22e5dd7070Spatrick #include "clang/Basic/CodeGenOptions.h"
23e5dd7070Spatrick #include "llvm/ADT/SmallSet.h"
24e5dd7070Spatrick #include "llvm/IR/Constants.h"
25e5dd7070Spatrick #include "llvm/IR/LLVMContext.h"
26e5dd7070Spatrick #include "llvm/IR/Metadata.h"
27e5dd7070Spatrick #include "llvm/IR/Module.h"
28e5dd7070Spatrick #include "llvm/IR/Type.h"
29e5dd7070Spatrick using namespace clang;
30e5dd7070Spatrick using namespace CodeGen;
31e5dd7070Spatrick
CodeGenTBAA(ASTContext & Ctx,llvm::Module & M,const CodeGenOptions & CGO,const LangOptions & Features,MangleContext & MContext)32e5dd7070Spatrick CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
33e5dd7070Spatrick const CodeGenOptions &CGO,
34e5dd7070Spatrick const LangOptions &Features, MangleContext &MContext)
35e5dd7070Spatrick : Context(Ctx), Module(M), CodeGenOpts(CGO),
36e5dd7070Spatrick Features(Features), MContext(MContext), MDHelper(M.getContext()),
37e5dd7070Spatrick Root(nullptr), Char(nullptr)
38e5dd7070Spatrick {}
39e5dd7070Spatrick
~CodeGenTBAA()40e5dd7070Spatrick CodeGenTBAA::~CodeGenTBAA() {
41e5dd7070Spatrick }
42e5dd7070Spatrick
getRoot()43e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getRoot() {
44e5dd7070Spatrick // Define the root of the tree. This identifies the tree, so that
45e5dd7070Spatrick // if our LLVM IR is linked with LLVM IR from a different front-end
46e5dd7070Spatrick // (or a different version of this front-end), their TBAA trees will
47e5dd7070Spatrick // remain distinct, and the optimizer will treat them conservatively.
48e5dd7070Spatrick if (!Root) {
49e5dd7070Spatrick if (Features.CPlusPlus)
50e5dd7070Spatrick Root = MDHelper.createTBAARoot("Simple C++ TBAA");
51e5dd7070Spatrick else
52e5dd7070Spatrick Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
53e5dd7070Spatrick }
54e5dd7070Spatrick
55e5dd7070Spatrick return Root;
56e5dd7070Spatrick }
57e5dd7070Spatrick
createScalarTypeNode(StringRef Name,llvm::MDNode * Parent,uint64_t Size)58e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
59e5dd7070Spatrick llvm::MDNode *Parent,
60e5dd7070Spatrick uint64_t Size) {
61e5dd7070Spatrick if (CodeGenOpts.NewStructPathTBAA) {
62e5dd7070Spatrick llvm::Metadata *Id = MDHelper.createString(Name);
63e5dd7070Spatrick return MDHelper.createTBAATypeNode(Parent, Size, Id);
64e5dd7070Spatrick }
65e5dd7070Spatrick return MDHelper.createTBAAScalarTypeNode(Name, Parent);
66e5dd7070Spatrick }
67e5dd7070Spatrick
getChar()68e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getChar() {
69e5dd7070Spatrick // Define the root of the tree for user-accessible memory. C and C++
70e5dd7070Spatrick // give special powers to char and certain similar types. However,
71e5dd7070Spatrick // these special powers only cover user-accessible memory, and doesn't
72e5dd7070Spatrick // include things like vtables.
73e5dd7070Spatrick if (!Char)
74e5dd7070Spatrick Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1);
75e5dd7070Spatrick
76e5dd7070Spatrick return Char;
77e5dd7070Spatrick }
78e5dd7070Spatrick
TypeHasMayAlias(QualType QTy)79e5dd7070Spatrick static bool TypeHasMayAlias(QualType QTy) {
80e5dd7070Spatrick // Tagged types have declarations, and therefore may have attributes.
81e5dd7070Spatrick if (auto *TD = QTy->getAsTagDecl())
82e5dd7070Spatrick if (TD->hasAttr<MayAliasAttr>())
83e5dd7070Spatrick return true;
84e5dd7070Spatrick
85e5dd7070Spatrick // Also look for may_alias as a declaration attribute on a typedef.
86e5dd7070Spatrick // FIXME: We should follow GCC and model may_alias as a type attribute
87e5dd7070Spatrick // rather than as a declaration attribute.
88e5dd7070Spatrick while (auto *TT = QTy->getAs<TypedefType>()) {
89e5dd7070Spatrick if (TT->getDecl()->hasAttr<MayAliasAttr>())
90e5dd7070Spatrick return true;
91e5dd7070Spatrick QTy = TT->desugar();
92e5dd7070Spatrick }
93e5dd7070Spatrick return false;
94e5dd7070Spatrick }
95e5dd7070Spatrick
96e5dd7070Spatrick /// Check if the given type is a valid base type to be used in access tags.
isValidBaseType(QualType QTy)97e5dd7070Spatrick static bool isValidBaseType(QualType QTy) {
98e5dd7070Spatrick if (QTy->isReferenceType())
99e5dd7070Spatrick return false;
100e5dd7070Spatrick if (const RecordType *TTy = QTy->getAs<RecordType>()) {
101e5dd7070Spatrick const RecordDecl *RD = TTy->getDecl()->getDefinition();
102e5dd7070Spatrick // Incomplete types are not valid base access types.
103e5dd7070Spatrick if (!RD)
104e5dd7070Spatrick return false;
105e5dd7070Spatrick if (RD->hasFlexibleArrayMember())
106e5dd7070Spatrick return false;
107e5dd7070Spatrick // RD can be struct, union, class, interface or enum.
108e5dd7070Spatrick // For now, we only handle struct and class.
109e5dd7070Spatrick if (RD->isStruct() || RD->isClass())
110e5dd7070Spatrick return true;
111e5dd7070Spatrick }
112e5dd7070Spatrick return false;
113e5dd7070Spatrick }
114e5dd7070Spatrick
getTypeInfoHelper(const Type * Ty)115e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
116e5dd7070Spatrick uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
117e5dd7070Spatrick
118e5dd7070Spatrick // Handle builtin types.
119e5dd7070Spatrick if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
120e5dd7070Spatrick switch (BTy->getKind()) {
121e5dd7070Spatrick // Character types are special and can alias anything.
122e5dd7070Spatrick // In C++, this technically only includes "char" and "unsigned char",
123e5dd7070Spatrick // and not "signed char". In C, it includes all three. For now,
124e5dd7070Spatrick // the risk of exploiting this detail in C++ seems likely to outweigh
125e5dd7070Spatrick // the benefit.
126e5dd7070Spatrick case BuiltinType::Char_U:
127e5dd7070Spatrick case BuiltinType::Char_S:
128e5dd7070Spatrick case BuiltinType::UChar:
129e5dd7070Spatrick case BuiltinType::SChar:
130e5dd7070Spatrick return getChar();
131e5dd7070Spatrick
132e5dd7070Spatrick // Unsigned types can alias their corresponding signed types.
133e5dd7070Spatrick case BuiltinType::UShort:
134e5dd7070Spatrick return getTypeInfo(Context.ShortTy);
135e5dd7070Spatrick case BuiltinType::UInt:
136e5dd7070Spatrick return getTypeInfo(Context.IntTy);
137e5dd7070Spatrick case BuiltinType::ULong:
138e5dd7070Spatrick return getTypeInfo(Context.LongTy);
139e5dd7070Spatrick case BuiltinType::ULongLong:
140e5dd7070Spatrick return getTypeInfo(Context.LongLongTy);
141e5dd7070Spatrick case BuiltinType::UInt128:
142e5dd7070Spatrick return getTypeInfo(Context.Int128Ty);
143e5dd7070Spatrick
144ec727ea7Spatrick case BuiltinType::UShortFract:
145ec727ea7Spatrick return getTypeInfo(Context.ShortFractTy);
146ec727ea7Spatrick case BuiltinType::UFract:
147ec727ea7Spatrick return getTypeInfo(Context.FractTy);
148ec727ea7Spatrick case BuiltinType::ULongFract:
149ec727ea7Spatrick return getTypeInfo(Context.LongFractTy);
150ec727ea7Spatrick
151ec727ea7Spatrick case BuiltinType::SatUShortFract:
152ec727ea7Spatrick return getTypeInfo(Context.SatShortFractTy);
153ec727ea7Spatrick case BuiltinType::SatUFract:
154ec727ea7Spatrick return getTypeInfo(Context.SatFractTy);
155ec727ea7Spatrick case BuiltinType::SatULongFract:
156ec727ea7Spatrick return getTypeInfo(Context.SatLongFractTy);
157ec727ea7Spatrick
158ec727ea7Spatrick case BuiltinType::UShortAccum:
159ec727ea7Spatrick return getTypeInfo(Context.ShortAccumTy);
160ec727ea7Spatrick case BuiltinType::UAccum:
161ec727ea7Spatrick return getTypeInfo(Context.AccumTy);
162ec727ea7Spatrick case BuiltinType::ULongAccum:
163ec727ea7Spatrick return getTypeInfo(Context.LongAccumTy);
164ec727ea7Spatrick
165ec727ea7Spatrick case BuiltinType::SatUShortAccum:
166ec727ea7Spatrick return getTypeInfo(Context.SatShortAccumTy);
167ec727ea7Spatrick case BuiltinType::SatUAccum:
168ec727ea7Spatrick return getTypeInfo(Context.SatAccumTy);
169ec727ea7Spatrick case BuiltinType::SatULongAccum:
170ec727ea7Spatrick return getTypeInfo(Context.SatLongAccumTy);
171ec727ea7Spatrick
172e5dd7070Spatrick // Treat all other builtin types as distinct types. This includes
173e5dd7070Spatrick // treating wchar_t, char16_t, and char32_t as distinct from their
174e5dd7070Spatrick // "underlying types".
175e5dd7070Spatrick default:
176e5dd7070Spatrick return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
177e5dd7070Spatrick }
178e5dd7070Spatrick }
179e5dd7070Spatrick
180e5dd7070Spatrick // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
181e5dd7070Spatrick // an object through a glvalue of other than one of the following types the
182e5dd7070Spatrick // behavior is undefined: [...] a char, unsigned char, or std::byte type."
183e5dd7070Spatrick if (Ty->isStdByteType())
184e5dd7070Spatrick return getChar();
185e5dd7070Spatrick
186e5dd7070Spatrick // Handle pointers and references.
187e5dd7070Spatrick // TODO: Implement C++'s type "similarity" and consider dis-"similar"
188e5dd7070Spatrick // pointers distinct.
189e5dd7070Spatrick if (Ty->isPointerType() || Ty->isReferenceType())
190e5dd7070Spatrick return createScalarTypeNode("any pointer", getChar(), Size);
191e5dd7070Spatrick
192e5dd7070Spatrick // Accesses to arrays are accesses to objects of their element types.
193e5dd7070Spatrick if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
194e5dd7070Spatrick return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
195e5dd7070Spatrick
196e5dd7070Spatrick // Enum types are distinct types. In C++ they have "underlying types",
197e5dd7070Spatrick // however they aren't related for TBAA.
198e5dd7070Spatrick if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
199e5dd7070Spatrick // In C++ mode, types have linkage, so we can rely on the ODR and
200e5dd7070Spatrick // on their mangled names, if they're external.
201e5dd7070Spatrick // TODO: Is there a way to get a program-wide unique name for a
202e5dd7070Spatrick // decl with local linkage or no linkage?
203e5dd7070Spatrick if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
204e5dd7070Spatrick return getChar();
205e5dd7070Spatrick
206e5dd7070Spatrick SmallString<256> OutName;
207e5dd7070Spatrick llvm::raw_svector_ostream Out(OutName);
208e5dd7070Spatrick MContext.mangleTypeName(QualType(ETy, 0), Out);
209e5dd7070Spatrick return createScalarTypeNode(OutName, getChar(), Size);
210e5dd7070Spatrick }
211e5dd7070Spatrick
212*12c85518Srobert if (const auto *EIT = dyn_cast<BitIntType>(Ty)) {
213ec727ea7Spatrick SmallString<256> OutName;
214ec727ea7Spatrick llvm::raw_svector_ostream Out(OutName);
215ec727ea7Spatrick // Don't specify signed/unsigned since integer types can alias despite sign
216ec727ea7Spatrick // differences.
217*12c85518Srobert Out << "_BitInt(" << EIT->getNumBits() << ')';
218ec727ea7Spatrick return createScalarTypeNode(OutName, getChar(), Size);
219ec727ea7Spatrick }
220ec727ea7Spatrick
221e5dd7070Spatrick // For now, handle any other kind of type conservatively.
222e5dd7070Spatrick return getChar();
223e5dd7070Spatrick }
224e5dd7070Spatrick
getTypeInfo(QualType QTy)225e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
226e5dd7070Spatrick // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
227e5dd7070Spatrick if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
228e5dd7070Spatrick return nullptr;
229e5dd7070Spatrick
230e5dd7070Spatrick // If the type has the may_alias attribute (even on a typedef), it is
231e5dd7070Spatrick // effectively in the general char alias class.
232e5dd7070Spatrick if (TypeHasMayAlias(QTy))
233e5dd7070Spatrick return getChar();
234e5dd7070Spatrick
235e5dd7070Spatrick // We need this function to not fall back to returning the "omnipotent char"
236e5dd7070Spatrick // type node for aggregate and union types. Otherwise, any dereference of an
237e5dd7070Spatrick // aggregate will result into the may-alias access descriptor, meaning all
238e5dd7070Spatrick // subsequent accesses to direct and indirect members of that aggregate will
239e5dd7070Spatrick // be considered may-alias too.
240e5dd7070Spatrick // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
241e5dd7070Spatrick if (isValidBaseType(QTy))
242e5dd7070Spatrick return getBaseTypeInfo(QTy);
243e5dd7070Spatrick
244e5dd7070Spatrick const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
245e5dd7070Spatrick if (llvm::MDNode *N = MetadataCache[Ty])
246e5dd7070Spatrick return N;
247e5dd7070Spatrick
248e5dd7070Spatrick // Note that the following helper call is allowed to add new nodes to the
249e5dd7070Spatrick // cache, which invalidates all its previously obtained iterators. So we
250e5dd7070Spatrick // first generate the node for the type and then add that node to the cache.
251e5dd7070Spatrick llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
252e5dd7070Spatrick return MetadataCache[Ty] = TypeNode;
253e5dd7070Spatrick }
254e5dd7070Spatrick
getAccessInfo(QualType AccessType)255e5dd7070Spatrick TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
256e5dd7070Spatrick // Pointee values may have incomplete types, but they shall never be
257e5dd7070Spatrick // dereferenced.
258e5dd7070Spatrick if (AccessType->isIncompleteType())
259e5dd7070Spatrick return TBAAAccessInfo::getIncompleteInfo();
260e5dd7070Spatrick
261e5dd7070Spatrick if (TypeHasMayAlias(AccessType))
262e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
263e5dd7070Spatrick
264e5dd7070Spatrick uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
265e5dd7070Spatrick return TBAAAccessInfo(getTypeInfo(AccessType), Size);
266e5dd7070Spatrick }
267e5dd7070Spatrick
getVTablePtrAccessInfo(llvm::Type * VTablePtrType)268e5dd7070Spatrick TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
269e5dd7070Spatrick llvm::DataLayout DL(&Module);
270e5dd7070Spatrick unsigned Size = DL.getPointerTypeSize(VTablePtrType);
271e5dd7070Spatrick return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
272e5dd7070Spatrick Size);
273e5dd7070Spatrick }
274e5dd7070Spatrick
275e5dd7070Spatrick bool
CollectFields(uint64_t BaseOffset,QualType QTy,SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & Fields,bool MayAlias)276e5dd7070Spatrick CodeGenTBAA::CollectFields(uint64_t BaseOffset,
277e5dd7070Spatrick QualType QTy,
278e5dd7070Spatrick SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
279e5dd7070Spatrick Fields,
280e5dd7070Spatrick bool MayAlias) {
281e5dd7070Spatrick /* Things not handled yet include: C++ base classes, bitfields, */
282e5dd7070Spatrick
283e5dd7070Spatrick if (const RecordType *TTy = QTy->getAs<RecordType>()) {
284e5dd7070Spatrick const RecordDecl *RD = TTy->getDecl()->getDefinition();
285e5dd7070Spatrick if (RD->hasFlexibleArrayMember())
286e5dd7070Spatrick return false;
287e5dd7070Spatrick
288e5dd7070Spatrick // TODO: Handle C++ base classes.
289e5dd7070Spatrick if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
290e5dd7070Spatrick if (Decl->bases_begin() != Decl->bases_end())
291e5dd7070Spatrick return false;
292e5dd7070Spatrick
293e5dd7070Spatrick const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
294e5dd7070Spatrick
295e5dd7070Spatrick unsigned idx = 0;
296e5dd7070Spatrick for (RecordDecl::field_iterator i = RD->field_begin(),
297e5dd7070Spatrick e = RD->field_end(); i != e; ++i, ++idx) {
298e5dd7070Spatrick if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
299e5dd7070Spatrick continue;
300e5dd7070Spatrick uint64_t Offset = BaseOffset +
301e5dd7070Spatrick Layout.getFieldOffset(idx) / Context.getCharWidth();
302e5dd7070Spatrick QualType FieldQTy = i->getType();
303e5dd7070Spatrick if (!CollectFields(Offset, FieldQTy, Fields,
304e5dd7070Spatrick MayAlias || TypeHasMayAlias(FieldQTy)))
305e5dd7070Spatrick return false;
306e5dd7070Spatrick }
307e5dd7070Spatrick return true;
308e5dd7070Spatrick }
309e5dd7070Spatrick
310e5dd7070Spatrick /* Otherwise, treat whatever it is as a field. */
311e5dd7070Spatrick uint64_t Offset = BaseOffset;
312e5dd7070Spatrick uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
313e5dd7070Spatrick llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
314e5dd7070Spatrick llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
315e5dd7070Spatrick Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
316e5dd7070Spatrick return true;
317e5dd7070Spatrick }
318e5dd7070Spatrick
319e5dd7070Spatrick llvm::MDNode *
getTBAAStructInfo(QualType QTy)320e5dd7070Spatrick CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
321e5dd7070Spatrick const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
322e5dd7070Spatrick
323e5dd7070Spatrick if (llvm::MDNode *N = StructMetadataCache[Ty])
324e5dd7070Spatrick return N;
325e5dd7070Spatrick
326e5dd7070Spatrick SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
327e5dd7070Spatrick if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
328e5dd7070Spatrick return MDHelper.createTBAAStructNode(Fields);
329e5dd7070Spatrick
330e5dd7070Spatrick // For now, handle any other kind of type conservatively.
331e5dd7070Spatrick return StructMetadataCache[Ty] = nullptr;
332e5dd7070Spatrick }
333e5dd7070Spatrick
getBaseTypeInfoHelper(const Type * Ty)334e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
335e5dd7070Spatrick if (auto *TTy = dyn_cast<RecordType>(Ty)) {
336e5dd7070Spatrick const RecordDecl *RD = TTy->getDecl()->getDefinition();
337e5dd7070Spatrick const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
338*12c85518Srobert using TBAAStructField = llvm::MDBuilder::TBAAStructField;
339*12c85518Srobert SmallVector<TBAAStructField, 4> Fields;
340*12c85518Srobert if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
341*12c85518Srobert // Handle C++ base classes. Non-virtual bases can treated a kind of
342*12c85518Srobert // field. Virtual bases are more complex and omitted, but avoid an
343*12c85518Srobert // incomplete view for NewStructPathTBAA.
344*12c85518Srobert if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0)
345*12c85518Srobert return BaseTypeMetadataCache[Ty] = nullptr;
346*12c85518Srobert for (const CXXBaseSpecifier &B : CXXRD->bases()) {
347*12c85518Srobert if (B.isVirtual())
348*12c85518Srobert continue;
349*12c85518Srobert QualType BaseQTy = B.getType();
350*12c85518Srobert const CXXRecordDecl *BaseRD = BaseQTy->getAsCXXRecordDecl();
351*12c85518Srobert if (BaseRD->isEmpty())
352*12c85518Srobert continue;
353*12c85518Srobert llvm::MDNode *TypeNode = isValidBaseType(BaseQTy)
354*12c85518Srobert ? getBaseTypeInfo(BaseQTy)
355*12c85518Srobert : getTypeInfo(BaseQTy);
356*12c85518Srobert if (!TypeNode)
357*12c85518Srobert return BaseTypeMetadataCache[Ty] = nullptr;
358*12c85518Srobert uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
359*12c85518Srobert uint64_t Size =
360*12c85518Srobert Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
361*12c85518Srobert Fields.push_back(
362*12c85518Srobert llvm::MDBuilder::TBAAStructField(Offset, Size, TypeNode));
363*12c85518Srobert }
364*12c85518Srobert // The order in which base class subobjects are allocated is unspecified,
365*12c85518Srobert // so may differ from declaration order. In particular, Itanium ABI will
366*12c85518Srobert // allocate a primary base first.
367*12c85518Srobert // Since we exclude empty subobjects, the objects are not overlapping and
368*12c85518Srobert // their offsets are unique.
369*12c85518Srobert llvm::sort(Fields,
370*12c85518Srobert [](const TBAAStructField &A, const TBAAStructField &B) {
371*12c85518Srobert return A.Offset < B.Offset;
372*12c85518Srobert });
373*12c85518Srobert }
374e5dd7070Spatrick for (FieldDecl *Field : RD->fields()) {
375e5dd7070Spatrick if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
376e5dd7070Spatrick continue;
377e5dd7070Spatrick QualType FieldQTy = Field->getType();
378e5dd7070Spatrick llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
379e5dd7070Spatrick getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
380e5dd7070Spatrick if (!TypeNode)
381e5dd7070Spatrick return BaseTypeMetadataCache[Ty] = nullptr;
382e5dd7070Spatrick
383e5dd7070Spatrick uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
384e5dd7070Spatrick uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
385e5dd7070Spatrick uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
386e5dd7070Spatrick Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
387e5dd7070Spatrick TypeNode));
388e5dd7070Spatrick }
389e5dd7070Spatrick
390e5dd7070Spatrick SmallString<256> OutName;
391e5dd7070Spatrick if (Features.CPlusPlus) {
392e5dd7070Spatrick // Don't use the mangler for C code.
393e5dd7070Spatrick llvm::raw_svector_ostream Out(OutName);
394e5dd7070Spatrick MContext.mangleTypeName(QualType(Ty, 0), Out);
395e5dd7070Spatrick } else {
396e5dd7070Spatrick OutName = RD->getName();
397e5dd7070Spatrick }
398e5dd7070Spatrick
399e5dd7070Spatrick if (CodeGenOpts.NewStructPathTBAA) {
400e5dd7070Spatrick llvm::MDNode *Parent = getChar();
401e5dd7070Spatrick uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
402e5dd7070Spatrick llvm::Metadata *Id = MDHelper.createString(OutName);
403e5dd7070Spatrick return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
404e5dd7070Spatrick }
405e5dd7070Spatrick
406e5dd7070Spatrick // Create the struct type node with a vector of pairs (offset, type).
407e5dd7070Spatrick SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
408e5dd7070Spatrick for (const auto &Field : Fields)
409e5dd7070Spatrick OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
410e5dd7070Spatrick return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
411e5dd7070Spatrick }
412e5dd7070Spatrick
413e5dd7070Spatrick return nullptr;
414e5dd7070Spatrick }
415e5dd7070Spatrick
getBaseTypeInfo(QualType QTy)416e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
417e5dd7070Spatrick if (!isValidBaseType(QTy))
418e5dd7070Spatrick return nullptr;
419e5dd7070Spatrick
420e5dd7070Spatrick const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
421e5dd7070Spatrick if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
422e5dd7070Spatrick return N;
423e5dd7070Spatrick
424e5dd7070Spatrick // Note that the following helper call is allowed to add new nodes to the
425e5dd7070Spatrick // cache, which invalidates all its previously obtained iterators. So we
426e5dd7070Spatrick // first generate the node for the type and then add that node to the cache.
427e5dd7070Spatrick llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
428e5dd7070Spatrick return BaseTypeMetadataCache[Ty] = TypeNode;
429e5dd7070Spatrick }
430e5dd7070Spatrick
getAccessTagInfo(TBAAAccessInfo Info)431e5dd7070Spatrick llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
432e5dd7070Spatrick assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
433e5dd7070Spatrick
434e5dd7070Spatrick if (Info.isMayAlias())
435e5dd7070Spatrick Info = TBAAAccessInfo(getChar(), Info.Size);
436e5dd7070Spatrick
437e5dd7070Spatrick if (!Info.AccessType)
438e5dd7070Spatrick return nullptr;
439e5dd7070Spatrick
440e5dd7070Spatrick if (!CodeGenOpts.StructPathTBAA)
441e5dd7070Spatrick Info = TBAAAccessInfo(Info.AccessType, Info.Size);
442e5dd7070Spatrick
443e5dd7070Spatrick llvm::MDNode *&N = AccessTagMetadataCache[Info];
444e5dd7070Spatrick if (N)
445e5dd7070Spatrick return N;
446e5dd7070Spatrick
447e5dd7070Spatrick if (!Info.BaseType) {
448e5dd7070Spatrick Info.BaseType = Info.AccessType;
449e5dd7070Spatrick assert(!Info.Offset && "Nonzero offset for an access with no base type!");
450e5dd7070Spatrick }
451e5dd7070Spatrick if (CodeGenOpts.NewStructPathTBAA) {
452e5dd7070Spatrick return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
453e5dd7070Spatrick Info.Offset, Info.Size);
454e5dd7070Spatrick }
455e5dd7070Spatrick return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
456e5dd7070Spatrick Info.Offset);
457e5dd7070Spatrick }
458e5dd7070Spatrick
mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,TBAAAccessInfo TargetInfo)459e5dd7070Spatrick TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
460e5dd7070Spatrick TBAAAccessInfo TargetInfo) {
461e5dd7070Spatrick if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
462e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
463e5dd7070Spatrick return TargetInfo;
464e5dd7070Spatrick }
465e5dd7070Spatrick
466e5dd7070Spatrick TBAAAccessInfo
mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,TBAAAccessInfo InfoB)467e5dd7070Spatrick CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
468e5dd7070Spatrick TBAAAccessInfo InfoB) {
469e5dd7070Spatrick if (InfoA == InfoB)
470e5dd7070Spatrick return InfoA;
471e5dd7070Spatrick
472e5dd7070Spatrick if (!InfoA || !InfoB)
473e5dd7070Spatrick return TBAAAccessInfo();
474e5dd7070Spatrick
475e5dd7070Spatrick if (InfoA.isMayAlias() || InfoB.isMayAlias())
476e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
477e5dd7070Spatrick
478e5dd7070Spatrick // TODO: Implement the rest of the logic here. For example, two accesses
479e5dd7070Spatrick // with same final access types result in an access to an object of that final
480e5dd7070Spatrick // access type regardless of their base types.
481e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
482e5dd7070Spatrick }
483e5dd7070Spatrick
484e5dd7070Spatrick TBAAAccessInfo
mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,TBAAAccessInfo SrcInfo)485e5dd7070Spatrick CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
486e5dd7070Spatrick TBAAAccessInfo SrcInfo) {
487e5dd7070Spatrick if (DestInfo == SrcInfo)
488e5dd7070Spatrick return DestInfo;
489e5dd7070Spatrick
490e5dd7070Spatrick if (!DestInfo || !SrcInfo)
491e5dd7070Spatrick return TBAAAccessInfo();
492e5dd7070Spatrick
493e5dd7070Spatrick if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
494e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
495e5dd7070Spatrick
496e5dd7070Spatrick // TODO: Implement the rest of the logic here. For example, two accesses
497e5dd7070Spatrick // with same final access types result in an access to an object of that final
498e5dd7070Spatrick // access type regardless of their base types.
499e5dd7070Spatrick return TBAAAccessInfo::getMayAliasInfo();
500e5dd7070Spatrick }
501