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 "CGRecordLayout.h" |
20 | #include "CodeGenTypes.h" |
21 | #include "clang/AST/ASTContext.h" |
22 | #include "clang/AST/Attr.h" |
23 | #include "clang/AST/Mangle.h" |
24 | #include "clang/AST/RecordLayout.h" |
25 | #include "clang/Basic/CodeGenOptions.h" |
26 | #include "clang/Basic/TargetInfo.h" |
27 | #include "llvm/ADT/SmallSet.h" |
28 | #include "llvm/IR/Constants.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" |
34 | using namespace clang; |
35 | using namespace CodeGen; |
36 | |
37 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, |
38 | llvm::Module &M, const CodeGenOptions &CGO, |
39 | const LangOptions &Features, MangleContext &MContext) |
40 | : Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO), |
41 | Features(Features), MContext(MContext), MDHelper(M.getContext()), |
42 | Root(nullptr), Char(nullptr) {} |
43 | |
44 | CodeGenTBAA::~CodeGenTBAA() { |
45 | } |
46 | |
47 | llvm::MDNode *CodeGenTBAA::getRoot() { |
48 | // Define the root of the tree. This identifies the tree, so that |
49 | // if our LLVM IR is linked with LLVM IR from a different front-end |
50 | // (or a different version of this front-end), their TBAA trees will |
51 | // remain distinct, and the optimizer will treat them conservatively. |
52 | if (!Root) { |
53 | if (Features.CPlusPlus) |
54 | Root = MDHelper.createTBAARoot(Name: "Simple C++ TBAA" ); |
55 | else |
56 | Root = MDHelper.createTBAARoot(Name: "Simple C/C++ TBAA" ); |
57 | } |
58 | |
59 | return Root; |
60 | } |
61 | |
62 | llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name, |
63 | llvm::MDNode *Parent, |
64 | uint64_t Size) { |
65 | if (CodeGenOpts.NewStructPathTBAA) { |
66 | llvm::Metadata *Id = MDHelper.createString(Str: Name); |
67 | return MDHelper.createTBAATypeNode(Parent, Size, Id); |
68 | } |
69 | return MDHelper.createTBAAScalarTypeNode(Name, Parent); |
70 | } |
71 | |
72 | llvm::MDNode *CodeGenTBAA::getChar() { |
73 | // Define the root of the tree for user-accessible memory. C and C++ |
74 | // give special powers to char and certain similar types. However, |
75 | // these special powers only cover user-accessible memory, and doesn't |
76 | // include things like vtables. |
77 | if (!Char) |
78 | Char = createScalarTypeNode(Name: "omnipotent char" , Parent: getRoot(), /* Size= */ 1); |
79 | |
80 | return Char; |
81 | } |
82 | |
83 | static bool TypeHasMayAlias(QualType QTy) { |
84 | // Tagged types have declarations, and therefore may have attributes. |
85 | if (auto *TD = QTy->getAsTagDecl()) |
86 | if (TD->hasAttr<MayAliasAttr>()) |
87 | return true; |
88 | |
89 | // Also look for may_alias as a declaration attribute on a typedef. |
90 | // FIXME: We should follow GCC and model may_alias as a type attribute |
91 | // rather than as a declaration attribute. |
92 | while (auto *TT = QTy->getAs<TypedefType>()) { |
93 | if (TT->getDecl()->hasAttr<MayAliasAttr>()) |
94 | return true; |
95 | QTy = TT->desugar(); |
96 | } |
97 | return false; |
98 | } |
99 | |
100 | /// Check if the given type is a valid base type to be used in access tags. |
101 | static bool isValidBaseType(QualType QTy) { |
102 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
103 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
104 | // Incomplete types are not valid base access types. |
105 | if (!RD) |
106 | return false; |
107 | if (RD->hasFlexibleArrayMember()) |
108 | return false; |
109 | // RD can be struct, union, class, interface or enum. |
110 | // For now, we only handle struct and class. |
111 | if (RD->isStruct() || RD->isClass()) |
112 | return true; |
113 | } |
114 | return false; |
115 | } |
116 | |
117 | llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) { |
118 | uint64_t Size = Context.getTypeSizeInChars(T: Ty).getQuantity(); |
119 | |
120 | // Handle builtin types. |
121 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Val: 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(QTy: Context.ShortTy); |
137 | case BuiltinType::UInt: |
138 | return getTypeInfo(QTy: Context.IntTy); |
139 | case BuiltinType::ULong: |
140 | return getTypeInfo(QTy: Context.LongTy); |
141 | case BuiltinType::ULongLong: |
142 | return getTypeInfo(QTy: Context.LongLongTy); |
143 | case BuiltinType::UInt128: |
144 | return getTypeInfo(QTy: Context.Int128Ty); |
145 | |
146 | case BuiltinType::UShortFract: |
147 | return getTypeInfo(QTy: Context.ShortFractTy); |
148 | case BuiltinType::UFract: |
149 | return getTypeInfo(QTy: Context.FractTy); |
150 | case BuiltinType::ULongFract: |
151 | return getTypeInfo(QTy: Context.LongFractTy); |
152 | |
153 | case BuiltinType::SatUShortFract: |
154 | return getTypeInfo(QTy: Context.SatShortFractTy); |
155 | case BuiltinType::SatUFract: |
156 | return getTypeInfo(QTy: Context.SatFractTy); |
157 | case BuiltinType::SatULongFract: |
158 | return getTypeInfo(QTy: Context.SatLongFractTy); |
159 | |
160 | case BuiltinType::UShortAccum: |
161 | return getTypeInfo(QTy: Context.ShortAccumTy); |
162 | case BuiltinType::UAccum: |
163 | return getTypeInfo(QTy: Context.AccumTy); |
164 | case BuiltinType::ULongAccum: |
165 | return getTypeInfo(QTy: Context.LongAccumTy); |
166 | |
167 | case BuiltinType::SatUShortAccum: |
168 | return getTypeInfo(QTy: Context.SatShortAccumTy); |
169 | case BuiltinType::SatUAccum: |
170 | return getTypeInfo(QTy: Context.SatAccumTy); |
171 | case BuiltinType::SatULongAccum: |
172 | return getTypeInfo(QTy: 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(Name: BTy->getName(Policy: Features), Parent: 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 | // |
205 | // Computing the pointee type string recursively is implicitly more |
206 | // forgiving than the standards require. Effectively, we are turning |
207 | // the question "are these types compatible/similar" into "are |
208 | // accesses to these types allowed to alias". In both C and C++, |
209 | // the latter question has special carve-outs for signedness |
210 | // mismatches that only apply at the top level. As a result, we are |
211 | // allowing e.g. `int *` l-values to access `unsigned *` objects. |
212 | if (Ty->isPointerType() || Ty->isReferenceType()) { |
213 | llvm::MDNode *AnyPtr = createScalarTypeNode(Name: "any pointer" , Parent: getChar(), Size); |
214 | if (!CodeGenOpts.PointerTBAA) |
215 | return AnyPtr; |
216 | // Compute the depth of the pointer and generate a tag of the form "p<depth> |
217 | // <base type tag>". |
218 | unsigned PtrDepth = 0; |
219 | do { |
220 | PtrDepth++; |
221 | Ty = Ty->getPointeeType().getTypePtr(); |
222 | } while (Ty->isPointerType()); |
223 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
224 | // pointers distinct for non-builtin types. |
225 | if (isa<BuiltinType>(Val: Ty)) { |
226 | llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty); |
227 | StringRef Name = |
228 | cast<llvm::MDString>( |
229 | Val: ScalarMD->getOperand(I: CodeGenOpts.NewStructPathTBAA ? 2 : 0)) |
230 | ->getString(); |
231 | SmallString<256> OutName("p" ); |
232 | OutName += std::to_string(val: PtrDepth); |
233 | OutName += " " ; |
234 | OutName += Name; |
235 | return createScalarTypeNode(Name: OutName, Parent: AnyPtr, Size); |
236 | } |
237 | return AnyPtr; |
238 | } |
239 | |
240 | // Accesses to arrays are accesses to objects of their element types. |
241 | if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType()) |
242 | return getTypeInfo(QTy: cast<ArrayType>(Val: Ty)->getElementType()); |
243 | |
244 | // Enum types are distinct types. In C++ they have "underlying types", |
245 | // however they aren't related for TBAA. |
246 | if (const EnumType *ETy = dyn_cast<EnumType>(Val: Ty)) { |
247 | if (!Features.CPlusPlus) |
248 | return getTypeInfo(QTy: ETy->getDecl()->getIntegerType()); |
249 | |
250 | // In C++ mode, types have linkage, so we can rely on the ODR and |
251 | // on their mangled names, if they're external. |
252 | // TODO: Is there a way to get a program-wide unique name for a |
253 | // decl with local linkage or no linkage? |
254 | if (!ETy->getDecl()->isExternallyVisible()) |
255 | return getChar(); |
256 | |
257 | SmallString<256> OutName; |
258 | llvm::raw_svector_ostream Out(OutName); |
259 | MContext.mangleCanonicalTypeName(T: QualType(ETy, 0), Out); |
260 | return createScalarTypeNode(Name: OutName, Parent: getChar(), Size); |
261 | } |
262 | |
263 | if (const auto *EIT = dyn_cast<BitIntType>(Val: Ty)) { |
264 | SmallString<256> OutName; |
265 | llvm::raw_svector_ostream Out(OutName); |
266 | // Don't specify signed/unsigned since integer types can alias despite sign |
267 | // differences. |
268 | Out << "_BitInt(" << EIT->getNumBits() << ')'; |
269 | return createScalarTypeNode(Name: OutName, Parent: getChar(), Size); |
270 | } |
271 | |
272 | // For now, handle any other kind of type conservatively. |
273 | return getChar(); |
274 | } |
275 | |
276 | llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) { |
277 | // At -O0 or relaxed aliasing, TBAA is not emitted for regular types. |
278 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
279 | return nullptr; |
280 | |
281 | // If the type has the may_alias attribute (even on a typedef), it is |
282 | // effectively in the general char alias class. |
283 | if (TypeHasMayAlias(QTy)) |
284 | return getChar(); |
285 | |
286 | // We need this function to not fall back to returning the "omnipotent char" |
287 | // type node for aggregate and union types. Otherwise, any dereference of an |
288 | // aggregate will result into the may-alias access descriptor, meaning all |
289 | // subsequent accesses to direct and indirect members of that aggregate will |
290 | // be considered may-alias too. |
291 | // TODO: Combine getTypeInfo() and getValidBaseTypeInfo() into a single |
292 | // function. |
293 | if (isValidBaseType(QTy)) |
294 | return getValidBaseTypeInfo(QTy); |
295 | |
296 | const Type *Ty = Context.getCanonicalType(T: QTy).getTypePtr(); |
297 | if (llvm::MDNode *N = MetadataCache[Ty]) |
298 | return N; |
299 | |
300 | // Note that the following helper call is allowed to add new nodes to the |
301 | // cache, which invalidates all its previously obtained iterators. So we |
302 | // first generate the node for the type and then add that node to the cache. |
303 | llvm::MDNode *TypeNode = getTypeInfoHelper(Ty); |
304 | return MetadataCache[Ty] = TypeNode; |
305 | } |
306 | |
307 | TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) { |
308 | // Pointee values may have incomplete types, but they shall never be |
309 | // dereferenced. |
310 | if (AccessType->isIncompleteType()) |
311 | return TBAAAccessInfo::getIncompleteInfo(); |
312 | |
313 | if (TypeHasMayAlias(QTy: AccessType)) |
314 | return TBAAAccessInfo::getMayAliasInfo(); |
315 | |
316 | uint64_t Size = Context.getTypeSizeInChars(T: AccessType).getQuantity(); |
317 | return TBAAAccessInfo(getTypeInfo(QTy: AccessType), Size); |
318 | } |
319 | |
320 | TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
321 | llvm::DataLayout DL(&Module); |
322 | unsigned Size = DL.getPointerTypeSize(Ty: VTablePtrType); |
323 | return TBAAAccessInfo(createScalarTypeNode(Name: "vtable pointer" , Parent: getRoot(), Size), |
324 | Size); |
325 | } |
326 | |
327 | bool |
328 | CodeGenTBAA::CollectFields(uint64_t BaseOffset, |
329 | QualType QTy, |
330 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & |
331 | Fields, |
332 | bool MayAlias) { |
333 | /* Things not handled yet include: C++ base classes, bitfields, */ |
334 | |
335 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
336 | if (TTy->isUnionType()) { |
337 | uint64_t Size = Context.getTypeSizeInChars(T: QTy).getQuantity(); |
338 | llvm::MDNode *TBAAType = getChar(); |
339 | llvm::MDNode *TBAATag = getAccessTagInfo(Info: TBAAAccessInfo(TBAAType, Size)); |
340 | Fields.push_back( |
341 | Elt: llvm::MDBuilder::TBAAStructField(BaseOffset, Size, TBAATag)); |
342 | return true; |
343 | } |
344 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
345 | if (RD->hasFlexibleArrayMember()) |
346 | return false; |
347 | |
348 | // TODO: Handle C++ base classes. |
349 | if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(Val: RD)) |
350 | if (Decl->bases_begin() != Decl->bases_end()) |
351 | return false; |
352 | |
353 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(D: RD); |
354 | const CGRecordLayout &CGRL = CGTypes.getCGRecordLayout(RD); |
355 | |
356 | unsigned idx = 0; |
357 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
358 | i != e; ++i, ++idx) { |
359 | if (isEmptyFieldForLayout(Context, FD: *i)) |
360 | continue; |
361 | |
362 | uint64_t Offset = |
363 | BaseOffset + Layout.getFieldOffset(FieldNo: idx) / Context.getCharWidth(); |
364 | |
365 | // Create a single field for consecutive named bitfields using char as |
366 | // base type. |
367 | if ((*i)->isBitField()) { |
368 | const CGBitFieldInfo &Info = CGRL.getBitFieldInfo(FD: *i); |
369 | // For big endian targets the first bitfield in the consecutive run is |
370 | // at the most-significant end; see CGRecordLowering::setBitFieldInfo |
371 | // for more information. |
372 | bool IsBE = Context.getTargetInfo().isBigEndian(); |
373 | bool IsFirst = IsBE ? Info.StorageSize - (Info.Offset + Info.Size) == 0 |
374 | : Info.Offset == 0; |
375 | if (!IsFirst) |
376 | continue; |
377 | unsigned CurrentBitFieldSize = Info.StorageSize; |
378 | uint64_t Size = |
379 | llvm::divideCeil(Numerator: CurrentBitFieldSize, Denominator: Context.getCharWidth()); |
380 | llvm::MDNode *TBAAType = getChar(); |
381 | llvm::MDNode *TBAATag = |
382 | getAccessTagInfo(Info: TBAAAccessInfo(TBAAType, Size)); |
383 | Fields.push_back( |
384 | Elt: llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); |
385 | continue; |
386 | } |
387 | |
388 | QualType FieldQTy = i->getType(); |
389 | if (!CollectFields(BaseOffset: Offset, QTy: FieldQTy, Fields, |
390 | MayAlias: MayAlias || TypeHasMayAlias(QTy: FieldQTy))) |
391 | return false; |
392 | } |
393 | return true; |
394 | } |
395 | |
396 | /* Otherwise, treat whatever it is as a field. */ |
397 | uint64_t Offset = BaseOffset; |
398 | uint64_t Size = Context.getTypeSizeInChars(T: QTy).getQuantity(); |
399 | llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy); |
400 | llvm::MDNode *TBAATag = getAccessTagInfo(Info: TBAAAccessInfo(TBAAType, Size)); |
401 | Fields.push_back(Elt: llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); |
402 | return true; |
403 | } |
404 | |
405 | llvm::MDNode * |
406 | CodeGenTBAA::getTBAAStructInfo(QualType QTy) { |
407 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
408 | return nullptr; |
409 | |
410 | const Type *Ty = Context.getCanonicalType(T: QTy).getTypePtr(); |
411 | |
412 | if (llvm::MDNode *N = StructMetadataCache[Ty]) |
413 | return N; |
414 | |
415 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
416 | if (CollectFields(BaseOffset: 0, QTy, Fields, MayAlias: TypeHasMayAlias(QTy))) |
417 | return MDHelper.createTBAAStructNode(Fields); |
418 | |
419 | // For now, handle any other kind of type conservatively. |
420 | return StructMetadataCache[Ty] = nullptr; |
421 | } |
422 | |
423 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) { |
424 | if (auto *TTy = dyn_cast<RecordType>(Val: Ty)) { |
425 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
426 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(D: RD); |
427 | using TBAAStructField = llvm::MDBuilder::TBAAStructField; |
428 | SmallVector<TBAAStructField, 4> Fields; |
429 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
430 | // Handle C++ base classes. Non-virtual bases can treated a kind of |
431 | // field. Virtual bases are more complex and omitted, but avoid an |
432 | // incomplete view for NewStructPathTBAA. |
433 | if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0) |
434 | return nullptr; |
435 | for (const CXXBaseSpecifier &B : CXXRD->bases()) { |
436 | if (B.isVirtual()) |
437 | continue; |
438 | QualType BaseQTy = B.getType(); |
439 | const CXXRecordDecl *BaseRD = BaseQTy->getAsCXXRecordDecl(); |
440 | if (BaseRD->isEmpty()) |
441 | continue; |
442 | llvm::MDNode *TypeNode = isValidBaseType(QTy: BaseQTy) |
443 | ? getValidBaseTypeInfo(QTy: BaseQTy) |
444 | : getTypeInfo(QTy: BaseQTy); |
445 | if (!TypeNode) |
446 | return nullptr; |
447 | uint64_t Offset = Layout.getBaseClassOffset(Base: BaseRD).getQuantity(); |
448 | uint64_t Size = |
449 | Context.getASTRecordLayout(D: BaseRD).getDataSize().getQuantity(); |
450 | Fields.push_back( |
451 | Elt: llvm::MDBuilder::TBAAStructField(Offset, Size, TypeNode)); |
452 | } |
453 | // The order in which base class subobjects are allocated is unspecified, |
454 | // so may differ from declaration order. In particular, Itanium ABI will |
455 | // allocate a primary base first. |
456 | // Since we exclude empty subobjects, the objects are not overlapping and |
457 | // their offsets are unique. |
458 | llvm::sort(C&: Fields, |
459 | Comp: [](const TBAAStructField &A, const TBAAStructField &B) { |
460 | return A.Offset < B.Offset; |
461 | }); |
462 | } |
463 | for (FieldDecl *Field : RD->fields()) { |
464 | if (Field->isZeroSize(Ctx: Context) || Field->isUnnamedBitField()) |
465 | continue; |
466 | QualType FieldQTy = Field->getType(); |
467 | llvm::MDNode *TypeNode = isValidBaseType(QTy: FieldQTy) |
468 | ? getValidBaseTypeInfo(QTy: FieldQTy) |
469 | : getTypeInfo(QTy: FieldQTy); |
470 | if (!TypeNode) |
471 | return nullptr; |
472 | |
473 | uint64_t BitOffset = Layout.getFieldOffset(FieldNo: Field->getFieldIndex()); |
474 | uint64_t Offset = Context.toCharUnitsFromBits(BitSize: BitOffset).getQuantity(); |
475 | uint64_t Size = Context.getTypeSizeInChars(T: FieldQTy).getQuantity(); |
476 | Fields.push_back(Elt: llvm::MDBuilder::TBAAStructField(Offset, Size, |
477 | TypeNode)); |
478 | } |
479 | |
480 | SmallString<256> OutName; |
481 | if (Features.CPlusPlus) { |
482 | // Don't use the mangler for C code. |
483 | llvm::raw_svector_ostream Out(OutName); |
484 | MContext.mangleCanonicalTypeName(T: QualType(Ty, 0), Out); |
485 | } else { |
486 | OutName = RD->getName(); |
487 | } |
488 | |
489 | if (CodeGenOpts.NewStructPathTBAA) { |
490 | llvm::MDNode *Parent = getChar(); |
491 | uint64_t Size = Context.getTypeSizeInChars(T: Ty).getQuantity(); |
492 | llvm::Metadata *Id = MDHelper.createString(Str: OutName); |
493 | return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields); |
494 | } |
495 | |
496 | // Create the struct type node with a vector of pairs (offset, type). |
497 | SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes; |
498 | for (const auto &Field : Fields) |
499 | OffsetsAndTypes.push_back(Elt: std::make_pair(x: Field.Type, y: Field.Offset)); |
500 | return MDHelper.createTBAAStructTypeNode(Name: OutName, Fields: OffsetsAndTypes); |
501 | } |
502 | |
503 | return nullptr; |
504 | } |
505 | |
506 | llvm::MDNode *CodeGenTBAA::getValidBaseTypeInfo(QualType QTy) { |
507 | assert(isValidBaseType(QTy) && "Must be a valid base type" ); |
508 | |
509 | const Type *Ty = Context.getCanonicalType(T: QTy).getTypePtr(); |
510 | |
511 | // nullptr is a valid value in the cache, so use find rather than [] |
512 | auto I = BaseTypeMetadataCache.find(Val: Ty); |
513 | if (I != BaseTypeMetadataCache.end()) |
514 | return I->second; |
515 | |
516 | // First calculate the metadata, before recomputing the insertion point, as |
517 | // the helper can recursively call us. |
518 | llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty); |
519 | LLVM_ATTRIBUTE_UNUSED auto inserted = |
520 | BaseTypeMetadataCache.insert(KV: {Ty, TypeNode}); |
521 | assert(inserted.second && "BaseType metadata was already inserted" ); |
522 | |
523 | return TypeNode; |
524 | } |
525 | |
526 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { |
527 | return isValidBaseType(QTy) ? getValidBaseTypeInfo(QTy) : nullptr; |
528 | } |
529 | |
530 | llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { |
531 | assert(!Info.isIncomplete() && "Access to an object of an incomplete type!" ); |
532 | |
533 | if (Info.isMayAlias()) |
534 | Info = TBAAAccessInfo(getChar(), Info.Size); |
535 | |
536 | if (!Info.AccessType) |
537 | return nullptr; |
538 | |
539 | if (!CodeGenOpts.StructPathTBAA) |
540 | Info = TBAAAccessInfo(Info.AccessType, Info.Size); |
541 | |
542 | llvm::MDNode *&N = AccessTagMetadataCache[Info]; |
543 | if (N) |
544 | return N; |
545 | |
546 | if (!Info.BaseType) { |
547 | Info.BaseType = Info.AccessType; |
548 | assert(!Info.Offset && "Nonzero offset for an access with no base type!" ); |
549 | } |
550 | if (CodeGenOpts.NewStructPathTBAA) { |
551 | return N = MDHelper.createTBAAAccessTag(BaseType: Info.BaseType, AccessType: Info.AccessType, |
552 | Offset: Info.Offset, Size: Info.Size); |
553 | } |
554 | return N = MDHelper.createTBAAStructTagNode(BaseType: Info.BaseType, AccessType: Info.AccessType, |
555 | Offset: Info.Offset); |
556 | } |
557 | |
558 | TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
559 | TBAAAccessInfo TargetInfo) { |
560 | if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias()) |
561 | return TBAAAccessInfo::getMayAliasInfo(); |
562 | return TargetInfo; |
563 | } |
564 | |
565 | TBAAAccessInfo |
566 | CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
567 | TBAAAccessInfo InfoB) { |
568 | if (InfoA == InfoB) |
569 | return InfoA; |
570 | |
571 | if (!InfoA || !InfoB) |
572 | return TBAAAccessInfo(); |
573 | |
574 | if (InfoA.isMayAlias() || InfoB.isMayAlias()) |
575 | return TBAAAccessInfo::getMayAliasInfo(); |
576 | |
577 | // TODO: Implement the rest of the logic here. For example, two accesses |
578 | // with same final access types result in an access to an object of that final |
579 | // access type regardless of their base types. |
580 | return TBAAAccessInfo::getMayAliasInfo(); |
581 | } |
582 | |
583 | TBAAAccessInfo |
584 | CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
585 | TBAAAccessInfo SrcInfo) { |
586 | if (DestInfo == SrcInfo) |
587 | return DestInfo; |
588 | |
589 | if (!DestInfo || !SrcInfo) |
590 | return TBAAAccessInfo(); |
591 | |
592 | if (DestInfo.isMayAlias() || SrcInfo.isMayAlias()) |
593 | return TBAAAccessInfo::getMayAliasInfo(); |
594 | |
595 | // TODO: Implement the rest of the logic here. For example, two accesses |
596 | // with same final access types result in an access to an object of that final |
597 | // access type regardless of their base types. |
598 | return TBAAAccessInfo::getMayAliasInfo(); |
599 | } |
600 | |