blob: 09de9591de7eac56bc6863bfc1e5f794a5419555 [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.
81 if (const TagType *TTy = dyn_cast<TagType>(QTy))
82 return TTy->getDecl()->hasAttr<MayAliasAttr>();
83
84 // Typedef types have declarations, and therefore may have attributes.
85 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
86 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
87 return true;
88 // Also, their underlying types may have relevant attributes.
89 return TypeHasMayAlias(TTy->desugar());
90 }
91
92 return false;
93}
94
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +000095/// Check if the given type is a valid base type to be used in access tags.
96static bool isValidBaseType(QualType QTy) {
97 if (QTy->isReferenceType())
98 return false;
99 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
100 const RecordDecl *RD = TTy->getDecl()->getDefinition();
101 // Incomplete types are not valid base access types.
102 if (!RD)
103 return false;
104 if (RD->hasFlexibleArrayMember())
105 return false;
Hal Finkela5986b92017-12-03 03:10:13 +0000106 // RD can be struct, union, class, interface or enum.
107 // For now, we only handle struct and class.
108 if (RD->isStruct() || RD->isClass())
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000109 return true;
110 }
111 return false;
112}
113
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000114llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000115 uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
116
Dan Gohman5419ce62010-10-21 18:49:12 +0000117 // Handle builtin types.
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000118 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman947c9af2010-10-14 23:06:10 +0000119 switch (BTy->getKind()) {
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000120 // Character types are special and can alias anything.
121 // In C++, this technically only includes "char" and "unsigned char",
122 // and not "signed char". In C, it includes all three. For now,
Dan Gohman4a3b1b32010-10-15 20:24:10 +0000123 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000124 // the benefit.
Dan Gohman947c9af2010-10-14 23:06:10 +0000125 case BuiltinType::Char_U:
126 case BuiltinType::Char_S:
127 case BuiltinType::UChar:
128 case BuiltinType::SChar:
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000129 return getChar();
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000130
131 // Unsigned types can alias their corresponding signed types.
132 case BuiltinType::UShort:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000133 return getTypeInfo(Context.ShortTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000134 case BuiltinType::UInt:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000135 return getTypeInfo(Context.IntTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000136 case BuiltinType::ULong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000137 return getTypeInfo(Context.LongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000138 case BuiltinType::ULongLong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000139 return getTypeInfo(Context.LongLongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000140 case BuiltinType::UInt128:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000141 return getTypeInfo(Context.Int128Ty);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000142
Dan Gohman2d0a3c72010-10-15 20:24:53 +0000143 // Treat all other builtin types as distinct types. This includes
144 // treating wchar_t, char16_t, and char32_t as distinct from their
145 // "underlying types".
Dan Gohman947c9af2010-10-14 23:06:10 +0000146 default:
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000147 return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
Dan Gohman947c9af2010-10-14 23:06:10 +0000148 }
149 }
150
David Majnemer8f94a232017-07-25 23:33:58 +0000151 // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
152 // an object through a glvalue of other than one of the following types the
153 // behavior is undefined: [...] a char, unsigned char, or std::byte type."
154 if (Ty->isStdByteType())
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000155 return getChar();
David Majnemer8f94a232017-07-25 23:33:58 +0000156
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000157 // Handle pointers and references.
Dan Gohmanc44fd642010-10-15 20:26:20 +0000158 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
159 // pointers distinct.
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000160 if (Ty->isPointerType() || Ty->isReferenceType())
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000161 return createScalarTypeNode("any pointer", getChar(), Size);
Dan Gohmand65c1962010-10-15 00:01:39 +0000162
Ivan A. Kosarev57493e22017-12-22 09:57:24 +0000163 // Accesses to arrays are accesses to objects of their element types.
164 if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
165 return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
166
Dan Gohman2e29eb52010-10-15 20:23:12 +0000167 // Enum types are distinct types. In C++ they have "underlying types",
168 // however they aren't related for TBAA.
169 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman2e29eb52010-10-15 20:23:12 +0000170 // In C++ mode, types have linkage, so we can rely on the ODR and
171 // on their mangled names, if they're external.
172 // TODO: Is there a way to get a program-wide unique name for a
173 // decl with local linkage or no linkage?
Eli Friedmaneecc09a2013-07-05 20:27:40 +0000174 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000175 return getChar();
Dan Gohman2e29eb52010-10-15 20:23:12 +0000176
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000177 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000178 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000179 MContext.mangleTypeName(QualType(ETy, 0), Out);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000180 return createScalarTypeNode(OutName, getChar(), Size);
Dan Gohman2e29eb52010-10-15 20:23:12 +0000181 }
182
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000183 // For now, handle any other kind of type conservatively.
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000184 return getChar();
185}
186
187llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
188 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
189 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
190 return nullptr;
191
192 // If the type has the may_alias attribute (even on a typedef), it is
193 // effectively in the general char alias class.
194 if (TypeHasMayAlias(QTy))
195 return getChar();
196
197 // We need this function to not fall back to returning the "omnipotent char"
198 // type node for aggregate and union types. Otherwise, any dereference of an
199 // aggregate will result into the may-alias access descriptor, meaning all
200 // subsequent accesses to direct and indirect members of that aggregate will
201 // be considered may-alias too.
202 // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
203 if (isValidBaseType(QTy))
204 return getBaseTypeInfo(QTy);
205
206 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
207 if (llvm::MDNode *N = MetadataCache[Ty])
208 return N;
209
210 // Note that the following helper call is allowed to add new nodes to the
211 // cache, which invalidates all its previously obtained iterators. So we
212 // first generate the node for the type and then add that node to the cache.
213 llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
214 return MetadataCache[Ty] = TypeNode;
Dan Gohman947c9af2010-10-14 23:06:10 +0000215}
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000216
Ivan A. Kosarev124a2182018-02-20 12:33:04 +0000217TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
218 // Pointee values may have incomplete types, but they shall never be
219 // dereferenced.
220 if (AccessType->isIncompleteType())
221 return TBAAAccessInfo::getIncompleteInfo();
222
223 if (TypeHasMayAlias(AccessType))
224 return TBAAAccessInfo::getMayAliasInfo();
225
226 uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
227 return TBAAAccessInfo(getTypeInfo(AccessType), Size);
228}
229
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000230TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
231 llvm::DataLayout DL(&Module);
232 unsigned Size = DL.getPointerTypeSize(VTablePtrType);
233 return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
234 Size);
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000235}
Dan Gohman22695fc2012-09-28 21:58:29 +0000236
237bool
238CodeGenTBAA::CollectFields(uint64_t BaseOffset,
239 QualType QTy,
240 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
241 Fields,
242 bool MayAlias) {
243 /* Things not handled yet include: C++ base classes, bitfields, */
244
245 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
246 const RecordDecl *RD = TTy->getDecl()->getDefinition();
247 if (RD->hasFlexibleArrayMember())
248 return false;
249
250 // TODO: Handle C++ base classes.
251 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
252 if (Decl->bases_begin() != Decl->bases_end())
253 return false;
254
255 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
256
257 unsigned idx = 0;
258 for (RecordDecl::field_iterator i = RD->field_begin(),
259 e = RD->field_end(); i != e; ++i, ++idx) {
Richard Smith1fa07eb2019-06-22 21:30:43 +0000260 if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
261 continue;
Dan Gohman22695fc2012-09-28 21:58:29 +0000262 uint64_t Offset = BaseOffset +
263 Layout.getFieldOffset(idx) / Context.getCharWidth();
264 QualType FieldQTy = i->getType();
265 if (!CollectFields(Offset, FieldQTy, Fields,
266 MayAlias || TypeHasMayAlias(FieldQTy)))
267 return false;
268 }
269 return true;
270 }
271
272 /* Otherwise, treat whatever it is as a field. */
273 uint64_t Offset = BaseOffset;
274 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000275 llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000276 llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
Manman Ren09a39122013-04-22 19:50:07 +0000277 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohman22695fc2012-09-28 21:58:29 +0000278 return true;
279}
280
281llvm::MDNode *
282CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
283 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
284
285 if (llvm::MDNode *N = StructMetadataCache[Ty])
286 return N;
287
288 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
289 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
290 return MDHelper.createTBAAStructNode(Fields);
291
292 // For now, handle any other kind of type conservatively.
Craig Topper8a13c412014-05-21 05:09:00 +0000293 return StructMetadataCache[Ty] = nullptr;
Dan Gohman22695fc2012-09-28 21:58:29 +0000294}
Manman Renc451e572013-04-04 21:53:22 +0000295
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000296llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
297 if (auto *TTy = dyn_cast<RecordType>(Ty)) {
Manman Renc451e572013-04-04 21:53:22 +0000298 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Renc451e572013-04-04 21:53:22 +0000299 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000300 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
Hal Finkela5986b92017-12-03 03:10:13 +0000301 for (FieldDecl *Field : RD->fields()) {
Richard Smith1fa07eb2019-06-22 21:30:43 +0000302 if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
303 continue;
Hal Finkela5986b92017-12-03 03:10:13 +0000304 QualType FieldQTy = Field->getType();
305 llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
306 getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
307 if (!TypeNode)
308 return BaseTypeMetadataCache[Ty] = nullptr;
Ivan A. Kosarevda342472017-11-30 09:26:39 +0000309
Hal Finkela5986b92017-12-03 03:10:13 +0000310 uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
311 uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
312 uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
313 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
314 TypeNode));
Manman Renc451e572013-04-04 21:53:22 +0000315 }
316
Manman Renc451e572013-04-04 21:53:22 +0000317 SmallString<256> OutName;
Manman Ren879ce882013-08-21 20:58:45 +0000318 if (Features.CPlusPlus) {
Reid Klecknercc99e262013-11-19 23:23:00 +0000319 // Don't use the mangler for C code.
Manman Ren879ce882013-08-21 20:58:45 +0000320 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000321 MContext.mangleTypeName(QualType(Ty, 0), Out);
Manman Ren879ce882013-08-21 20:58:45 +0000322 } else {
323 OutName = RD->getName();
324 }
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000325
Ivan A. Kosarevd50b8472017-12-22 09:54:23 +0000326 if (CodeGenOpts.NewStructPathTBAA) {
327 llvm::MDNode *Parent = getChar();
328 uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
329 llvm::Metadata *Id = MDHelper.createString(OutName);
330 return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
331 }
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000332
Manman Renc451e572013-04-04 21:53:22 +0000333 // Create the struct type node with a vector of pairs (offset, type).
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000334 SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
335 for (const auto &Field : Fields)
Ivan A. Kosareve814f322017-12-18 16:50:11 +0000336 OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000337 return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
Manman Renc451e572013-04-04 21:53:22 +0000338 }
339
Ivan A. Kosarev5d9d32e2017-11-21 11:18:06 +0000340 return nullptr;
341}
342
343llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
344 if (!isValidBaseType(QTy))
345 return nullptr;
346
347 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
348 if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
349 return N;
350
351 // Note that the following helper call is allowed to add new nodes to the
352 // cache, which invalidates all its previously obtained iterators. So we
353 // first generate the node for the type and then add that node to the cache.
354 llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
355 return BaseTypeMetadataCache[Ty] = TypeNode;
Manman Renc451e572013-04-04 21:53:22 +0000356}
357
Ivan A. Kosarev3d68ce92017-10-05 11:08:17 +0000358llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000359 assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
360
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000361 if (Info.isMayAlias())
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000362 Info = TBAAAccessInfo(getChar(), Info.Size);
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000363
Ivan A. Kosareva511ed72017-10-03 10:52:39 +0000364 if (!Info.AccessType)
Craig Topper8a13c412014-05-21 05:09:00 +0000365 return nullptr;
Manman Ren4f755de2013-10-08 00:08:49 +0000366
Manman Renc451e572013-04-04 21:53:22 +0000367 if (!CodeGenOpts.StructPathTBAA)
Ivan A. Kosarev4e50e702017-11-27 09:39:29 +0000368 Info = TBAAAccessInfo(Info.AccessType, Info.Size);
Manman Renc451e572013-04-04 21:53:22 +0000369
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000370 llvm::MDNode *&N = AccessTagMetadataCache[Info];
371 if (N)
Manman Renc451e572013-04-04 21:53:22 +0000372 return N;
373
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000374 if (!Info.BaseType) {
375 Info.BaseType = Info.AccessType;
376 assert(!Info.Offset && "Nonzero offset for an access with no base type!");
377 }
Ivan A. Kosarevd50b8472017-12-22 09:54:23 +0000378 if (CodeGenOpts.NewStructPathTBAA) {
379 return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
380 Info.Offset, Info.Size);
381 }
Ivan A. Kosarev383890b2017-10-06 08:17:48 +0000382 return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
383 Info.Offset);
Manman Renc451e572013-04-04 21:53:22 +0000384}
Manman Rene1ad74e2013-04-11 23:02:56 +0000385
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000386TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
387 TBAAAccessInfo TargetInfo) {
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000388 if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
389 return TBAAAccessInfo::getMayAliasInfo();
Ivan A. Kosareved141ba2017-10-17 09:12:13 +0000390 return TargetInfo;
391}
Ivan A. Kosarevb9c59f32017-10-31 11:05:34 +0000392
393TBAAAccessInfo
394CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
395 TBAAAccessInfo InfoB) {
396 if (InfoA == InfoB)
397 return InfoA;
398
399 if (!InfoA || !InfoB)
400 return TBAAAccessInfo();
401
402 if (InfoA.isMayAlias() || InfoB.isMayAlias())
403 return TBAAAccessInfo::getMayAliasInfo();
404
405 // TODO: Implement the rest of the logic here. For example, two accesses
406 // with same final access types result in an access to an object of that final
407 // access type regardless of their base types.
408 return TBAAAccessInfo::getMayAliasInfo();
409}
Ivan A. Kosarev1860b522018-01-25 14:21:55 +0000410
411TBAAAccessInfo
412CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
413 TBAAAccessInfo SrcInfo) {
414 if (DestInfo == SrcInfo)
415 return DestInfo;
416
417 if (!DestInfo || !SrcInfo)
418 return TBAAAccessInfo();
419
420 if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
421 return TBAAAccessInfo::getMayAliasInfo();
422
423 // TODO: Implement the rest of the logic here. For example, two accesses
424 // with same final access types result in an access to an object of that final
425 // access type regardless of their base types.
426 return TBAAAccessInfo::getMayAliasInfo();
427}