blob: 7d730cb1ed154329194e4c9368f626190e03e455 [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===-- CodeGenTBAA.cpp - TBAA information for LLVM CodeGen ---------------===//
Dan Gohman947c9af2010-10-14 23:06:10 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohman947c9af2010-10-14 23:06:10 +00006//
7//===----------------------------------------------------------------------===//
8//
Dan Gohman5419ce62010-10-21 18:49:12 +00009// This is the code that manages TBAA information and defines the TBAA policy
10// for the optimizer to use. Relevant standards text includes:
Dan Gohmanf47df3e2010-10-15 20:54:41 +000011//
12// C99 6.5p7
13// C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
Dan Gohman947c9af2010-10-14 23:06:10 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CodeGenTBAA.h"
18#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/Attr.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000020#include "clang/AST/Mangle.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000021#include "clang/AST/RecordLayout.h"
Richard Trieu63688182018-12-11 03:18:39 +000022#include "clang/Basic/CodeGenOptions.h"
Manman Renc451e572013-04-04 21:53:22 +000023#include "llvm/ADT/SmallSet.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000024#include "llvm/IR/Constants.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Metadata.h"
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000027#include "llvm/IR/Module.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000028#include "llvm/IR/Type.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000029using namespace clang;
30using namespace CodeGen;
31
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000032CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000033 const CodeGenOptions &CGO,
Dan Gohman2e29eb52010-10-15 20:23:12 +000034 const LangOptions &Features, MangleContext &MContext)
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000035 : Context(Ctx), Module(M), CodeGenOpts(CGO),
36 Features(Features), MContext(MContext), MDHelper(M.getContext()),
37 Root(nullptr), Char(nullptr)
38{}
Dan Gohman947c9af2010-10-14 23:06:10 +000039
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000040CodeGenTBAA::~CodeGenTBAA() {
41}
Dan Gohman947c9af2010-10-14 23:06:10 +000042
Dan Gohman7dfd13c2010-10-25 21:48:30 +000043llvm::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.
Manman Ren37dec102016-02-11 19:19:18 +000048 if (!Root) {
49 if (Features.CPlusPlus)
50 Root = MDHelper.createTBAARoot("Simple C++ TBAA");
51 else
52 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
53 }
Dan Gohman7dfd13c2010-10-25 21:48:30 +000054
55 return Root;
56}
57
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000058llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
59 llvm::MDNode *Parent,
60 uint64_t Size) {
Ivan A. Kosarevd50b8472017-12-22 09:54:23 +000061 if (CodeGenOpts.NewStructPathTBAA) {
62 llvm::Metadata *Id = MDHelper.createString(Name);
63 return MDHelper.createTBAATypeNode(Parent, Size, Id);
64 }
Manman Ren4f755de2013-10-08 00:08:49 +000065 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Rene1ad74e2013-04-11 23:02:56 +000066}
67
Dan Gohman7dfd13c2010-10-25 21:48:30 +000068llvm::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 if (!Char)
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +000074 Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1);
Dan Gohman7dfd13c2010-10-25 21:48:30 +000075
76 return Char;
77}
78
Dan Gohmanc2897692010-12-13 23:51:08 +000079static bool TypeHasMayAlias(QualType QTy) {
80 // Tagged types have declarations, and therefore may have attributes.
Richard Smithe0e07a72019-12-11 14:03:35 -080081 if (auto *TD = QTy->getAsTagDecl())
82 if (TD->hasAttr<MayAliasAttr>())
Dan Gohmanc2897692010-12-13 23:51:08 +000083 return true;
Dan Gohmanc2897692010-12-13 23:51:08 +000084
Richard Smithe0e07a72019-12-11 14:03:35 -080085 // 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 while (auto *TT = QTy->getAs<TypedefType>()) {
89 if (TT->getDecl()->hasAttr<MayAliasAttr>())
90 return true;
91 QTy = TT->desugar();
92 }
Dan Gohmanc2897692010-12-13 23:51:08 +000093 return false;
94}
95
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000096/// Check if the given type is a valid base type to be used in access tags.
97static bool isValidBaseType(QualType QTy) {
98 if (QTy->isReferenceType())
99 return false;
100 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
101 const RecordDecl *RD = TTy->getDecl()->getDefinition();
102 // Incomplete types are not valid base access types.
103 if (!RD)
104 return false;
105 if (RD->hasFlexibleArrayMember())
106 return false;
Hal Finkela5986b92017-12-03 03:10:13 +0000107 // RD can be struct, union, class, interface or enum.
108 // For now, we only handle struct and class.
109 if (RD->isStruct() || RD->isClass())
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000110 return true;
111 }
112 return false;
113}
114
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000115llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000116 uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
117
Dan Gohman5419ce62010-10-21 18:49:12 +0000118 // Handle builtin types.
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000119 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman947c9af2010-10-14 23:06:10 +0000120 switch (BTy->getKind()) {
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000121 // 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,
Dan Gohman4a3b1b32010-10-15 20:24:10 +0000124 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000125 // the benefit.
Dan Gohman947c9af2010-10-14 23:06:10 +0000126 case BuiltinType::Char_U:
127 case BuiltinType::Char_S:
128 case BuiltinType::UChar:
129 case BuiltinType::SChar:
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000130 return getChar();
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000131
132 // Unsigned types can alias their corresponding signed types.
133 case BuiltinType::UShort:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000134 return getTypeInfo(Context.ShortTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000135 case BuiltinType::UInt:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000136 return getTypeInfo(Context.IntTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000137 case BuiltinType::ULong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000138 return getTypeInfo(Context.LongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000139 case BuiltinType::ULongLong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000140 return getTypeInfo(Context.LongLongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000141 case BuiltinType::UInt128:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000142 return getTypeInfo(Context.Int128Ty);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000143
Dan Gohman2d0a3c72010-10-15 20:24:53 +0000144 // Treat all other builtin types as distinct types. This includes
145 // treating wchar_t, char16_t, and char32_t as distinct from their
146 // "underlying types".
Dan Gohman947c9af2010-10-14 23:06:10 +0000147 default:
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000148 return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
Dan Gohman947c9af2010-10-14 23:06:10 +0000149 }
150 }
151
David Majnemer8f94a232017-07-25 23:33:58 +0000152 // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
153 // an object through a glvalue of other than one of the following types the
154 // behavior is undefined: [...] a char, unsigned char, or std::byte type."
155 if (Ty->isStdByteType())
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000156 return getChar();
David Majnemer8f94a232017-07-25 23:33:58 +0000157
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000158 // Handle pointers and references.
Dan Gohmanc44fd642010-10-15 20:26:20 +0000159 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
160 // pointers distinct.
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000161 if (Ty->isPointerType() || Ty->isReferenceType())
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000162 return createScalarTypeNode("any pointer", getChar(), Size);
Dan Gohmand65c1962010-10-15 00:01:39 +0000163
Ivan A. Kosarev57493e22017-12-22 09:57:24 +0000164 // Accesses to arrays are accesses to objects of their element types.
165 if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
166 return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
167
Dan Gohman2e29eb52010-10-15 20:23:12 +0000168 // Enum types are distinct types. In C++ they have "underlying types",
169 // however they aren't related for TBAA.
170 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman2e29eb52010-10-15 20:23:12 +0000171 // In C++ mode, types have linkage, so we can rely on the ODR and
172 // on their mangled names, if they're external.
173 // TODO: Is there a way to get a program-wide unique name for a
174 // decl with local linkage or no linkage?
Eli Friedmaneecc09a2013-07-05 20:27:40 +0000175 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000176 return getChar();
Dan Gohman2e29eb52010-10-15 20:23:12 +0000177
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000178 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000179 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000180 MContext.mangleTypeName(QualType(ETy, 0), Out);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000181 return createScalarTypeNode(OutName, getChar(), Size);
Dan Gohman2e29eb52010-10-15 20:23:12 +0000182 }
183
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000184 // For now, handle any other kind of type conservatively.
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000185 return getChar();
186}
187
188llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
189 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
190 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
191 return nullptr;
192
193 // If the type has the may_alias attribute (even on a typedef), it is
194 // effectively in the general char alias class.
195 if (TypeHasMayAlias(QTy))
196 return getChar();
197
198 // We need this function to not fall back to returning the "omnipotent char"
199 // type node for aggregate and union types. Otherwise, any dereference of an
200 // aggregate will result into the may-alias access descriptor, meaning all
201 // subsequent accesses to direct and indirect members of that aggregate will
202 // be considered may-alias too.
203 // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
204 if (isValidBaseType(QTy))
205 return getBaseTypeInfo(QTy);
206
207 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
208 if (llvm::MDNode *N = MetadataCache[Ty])
209 return N;
210
211 // Note that the following helper call is allowed to add new nodes to the
212 // cache, which invalidates all its previously obtained iterators. So we
213 // first generate the node for the type and then add that node to the cache.
214 llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
215 return MetadataCache[Ty] = TypeNode;
Dan Gohman947c9af2010-10-14 23:06:10 +0000216}
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000217
Ivan A. Kosarev124a2182018-02-20 12:33:04 +0000218TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
219 // Pointee values may have incomplete types, but they shall never be
220 // dereferenced.
221 if (AccessType->isIncompleteType())
222 return TBAAAccessInfo::getIncompleteInfo();
223
224 if (TypeHasMayAlias(AccessType))
225 return TBAAAccessInfo::getMayAliasInfo();
226
227 uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
228 return TBAAAccessInfo(getTypeInfo(AccessType), Size);
229}
230
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000231TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
232 llvm::DataLayout DL(&Module);
233 unsigned Size = DL.getPointerTypeSize(VTablePtrType);
234 return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
235 Size);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000236}
Dan Gohman22695fc2012-09-28 21:58:29 +0000237
238bool
239CodeGenTBAA::CollectFields(uint64_t BaseOffset,
240 QualType QTy,
241 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
242 Fields,
243 bool MayAlias) {
244 /* Things not handled yet include: C++ base classes, bitfields, */
245
246 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
247 const RecordDecl *RD = TTy->getDecl()->getDefinition();
248 if (RD->hasFlexibleArrayMember())
249 return false;
250
251 // TODO: Handle C++ base classes.
252 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
253 if (Decl->bases_begin() != Decl->bases_end())
254 return false;
255
256 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
257
258 unsigned idx = 0;
259 for (RecordDecl::field_iterator i = RD->field_begin(),
260 e = RD->field_end(); i != e; ++i, ++idx) {
Richard Smith1fa07eb2019-06-22 21:30:43 +0000261 if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
262 continue;
Dan Gohman22695fc2012-09-28 21:58:29 +0000263 uint64_t Offset = BaseOffset +
264 Layout.getFieldOffset(idx) / Context.getCharWidth();
265 QualType FieldQTy = i->getType();
266 if (!CollectFields(Offset, FieldQTy, Fields,
267 MayAlias || TypeHasMayAlias(FieldQTy)))
268 return false;
269 }
270 return true;
271 }
272
273 /* Otherwise, treat whatever it is as a field. */
274 uint64_t Offset = BaseOffset;
275 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000276 llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000277 llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
Manman Ren09a39122013-04-22 19:50:07 +0000278 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohman22695fc2012-09-28 21:58:29 +0000279 return true;
280}
281
282llvm::MDNode *
283CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
284 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
285
286 if (llvm::MDNode *N = StructMetadataCache[Ty])
287 return N;
288
289 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
290 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
291 return MDHelper.createTBAAStructNode(Fields);
292
293 // For now, handle any other kind of type conservatively.
Craig Topper8a13c412014-05-21 05:09:00 +0000294 return StructMetadataCache[Ty] = nullptr;
Dan Gohman22695fc2012-09-28 21:58:29 +0000295}
Manman Renc451e572013-04-04 21:53:22 +0000296
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000297llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
298 if (auto *TTy = dyn_cast<RecordType>(Ty)) {
Manman Renc451e572013-04-04 21:53:22 +0000299 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Renc451e572013-04-04 21:53:22 +0000300 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000301 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
Hal Finkela5986b92017-12-03 03:10:13 +0000302 for (FieldDecl *Field : RD->fields()) {
Richard Smith1fa07eb2019-06-22 21:30:43 +0000303 if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
304 continue;
Hal Finkela5986b92017-12-03 03:10:13 +0000305 QualType FieldQTy = Field->getType();
306 llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
307 getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
308 if (!TypeNode)
309 return BaseTypeMetadataCache[Ty] = nullptr;
Ivan A. Kosarevda342472017-11-30 09:26:39 +0000310
Hal Finkela5986b92017-12-03 03:10:13 +0000311 uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
312 uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
313 uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
314 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
315 TypeNode));
Manman Renc451e572013-04-04 21:53:22 +0000316 }
317
Manman Renc451e572013-04-04 21:53:22 +0000318 SmallString<256> OutName;
Manman Ren879ce882013-08-21 20:58:45 +0000319 if (Features.CPlusPlus) {
Reid Klecknercc99e262013-11-19 23:23:00 +0000320 // Don't use the mangler for C code.
Manman Ren879ce882013-08-21 20:58:45 +0000321 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000322 MContext.mangleTypeName(QualType(Ty, 0), Out);
Manman Ren879ce882013-08-21 20:58:45 +0000323 } else {
324 OutName = RD->getName();
325 }
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000326
Ivan A. Kosarevd50b8472017-12-22 09:54:23 +0000327 if (CodeGenOpts.NewStructPathTBAA) {
328 llvm::MDNode *Parent = getChar();
329 uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
330 llvm::Metadata *Id = MDHelper.createString(OutName);
331 return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
332 }
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000333
Manman Renc451e572013-04-04 21:53:22 +0000334 // Create the struct type node with a vector of pairs (offset, type).
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000335 SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
336 for (const auto &Field : Fields)
Ivan A. Kosareve814f322017-12-18 16:50:11 +0000337 OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000338 return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
Manman Renc451e572013-04-04 21:53:22 +0000339 }
340
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000341 return nullptr;
342}
343
344llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
345 if (!isValidBaseType(QTy))
346 return nullptr;
347
348 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
349 if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
350 return N;
351
352 // Note that the following helper call is allowed to add new nodes to the
353 // cache, which invalidates all its previously obtained iterators. So we
354 // first generate the node for the type and then add that node to the cache.
355 llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
356 return BaseTypeMetadataCache[Ty] = TypeNode;
Manman Renc451e572013-04-04 21:53:22 +0000357}
358
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000359llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000360 assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
361
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000362 if (Info.isMayAlias())
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000363 Info = TBAAAccessInfo(getChar(), Info.Size);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000364
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000365 if (!Info.AccessType)
Craig Topper8a13c412014-05-21 05:09:00 +0000366 return nullptr;
Manman Ren4f755de2013-10-08 00:08:49 +0000367
Manman Renc451e572013-04-04 21:53:22 +0000368 if (!CodeGenOpts.StructPathTBAA)
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000369 Info = TBAAAccessInfo(Info.AccessType, Info.Size);
Manman Renc451e572013-04-04 21:53:22 +0000370
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000371 llvm::MDNode *&N = AccessTagMetadataCache[Info];
372 if (N)
Manman Renc451e572013-04-04 21:53:22 +0000373 return N;
374
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000375 if (!Info.BaseType) {
376 Info.BaseType = Info.AccessType;
377 assert(!Info.Offset && "Nonzero offset for an access with no base type!");
378 }
Ivan A. Kosarevd50b8472017-12-22 09:54:23 +0000379 if (CodeGenOpts.NewStructPathTBAA) {
380 return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
381 Info.Offset, Info.Size);
382 }
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000383 return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
384 Info.Offset);
Manman Renc451e572013-04-04 21:53:22 +0000385}
Manman Rene1ad74e2013-04-11 23:02:56 +0000386
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000387TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
388 TBAAAccessInfo TargetInfo) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000389 if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
390 return TBAAAccessInfo::getMayAliasInfo();
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000391 return TargetInfo;
392}
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000393
394TBAAAccessInfo
395CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
396 TBAAAccessInfo InfoB) {
397 if (InfoA == InfoB)
398 return InfoA;
399
400 if (!InfoA || !InfoB)
401 return TBAAAccessInfo();
402
403 if (InfoA.isMayAlias() || InfoB.isMayAlias())
404 return TBAAAccessInfo::getMayAliasInfo();
405
406 // TODO: Implement the rest of the logic here. For example, two accesses
407 // with same final access types result in an access to an object of that final
408 // access type regardless of their base types.
409 return TBAAAccessInfo::getMayAliasInfo();
410}
Ivan A. Kosarev1860b522018-01-25 14:21:55 +0000411
412TBAAAccessInfo
413CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
414 TBAAAccessInfo SrcInfo) {
415 if (DestInfo == SrcInfo)
416 return DestInfo;
417
418 if (!DestInfo || !SrcInfo)
419 return TBAAAccessInfo();
420
421 if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
422 return TBAAAccessInfo::getMayAliasInfo();
423
424 // TODO: Implement the rest of the logic here. For example, two accesses
425 // with same final access types result in an access to an object of that final
426 // access type regardless of their base types.
427 return TBAAAccessInfo::getMayAliasInfo();
428}