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