blob: b341754ba09afa9fe40c76c46dd9568e79988e71 [file] [log] [blame]
Dan Gohman947c9af2010-10-14 23:06:10 +00001//===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Dan Gohman5419ce62010-10-21 18:49:12 +000010// This is the code that manages TBAA information and defines the TBAA policy
11// for the optimizer to use. Relevant standards text includes:
Dan Gohmanf47df3e2010-10-15 20:54:41 +000012//
13// C99 6.5p7
14// C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
Dan Gohman947c9af2010-10-14 23:06:10 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "CodeGenTBAA.h"
19#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/AST/Attr.h"
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000021#include "clang/AST/Mangle.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000022#include "clang/AST/RecordLayout.h"
Saleem Abdulrasool10a49722016-04-08 16:52:00 +000023#include "clang/Frontend/CodeGenOptions.h"
Manman Renc451e572013-04-04 21:53:22 +000024#include "llvm/ADT/SmallSet.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000025#include "llvm/IR/Constants.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Metadata.h"
28#include "llvm/IR/Type.h"
Dan Gohman947c9af2010-10-14 23:06:10 +000029using namespace clang;
30using namespace CodeGen;
31
32CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000033 const CodeGenOptions &CGO,
Dan Gohman2e29eb52010-10-15 20:23:12 +000034 const LangOptions &Features, MangleContext &MContext)
Benjamin Kramerd1d76b22012-06-06 17:32:50 +000035 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
Craig Topper8a13c412014-05-21 05:09:00 +000036 MDHelper(VMContext), Root(nullptr), Char(nullptr) {
Dan Gohman947c9af2010-10-14 23:06:10 +000037}
38
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000039CodeGenTBAA::~CodeGenTBAA() {
40}
Dan Gohman947c9af2010-10-14 23:06:10 +000041
Dan Gohman7dfd13c2010-10-25 21:48:30 +000042llvm::MDNode *CodeGenTBAA::getRoot() {
43 // Define the root of the tree. This identifies the tree, so that
44 // if our LLVM IR is linked with LLVM IR from a different front-end
45 // (or a different version of this front-end), their TBAA trees will
46 // remain distinct, and the optimizer will treat them conservatively.
Manman Ren37dec102016-02-11 19:19:18 +000047 if (!Root) {
48 if (Features.CPlusPlus)
49 Root = MDHelper.createTBAARoot("Simple C++ TBAA");
50 else
51 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
52 }
Dan Gohman7dfd13c2010-10-25 21:48:30 +000053
54 return Root;
55}
56
Manman Ren4f755de2013-10-08 00:08:49 +000057// For both scalar TBAA and struct-path aware TBAA, the scalar type has the
58// same format: name, parent node, and offset.
Manman Rene1ad74e2013-04-11 23:02:56 +000059llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
60 llvm::MDNode *Parent) {
Manman Ren4f755de2013-10-08 00:08:49 +000061 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Rene1ad74e2013-04-11 23:02:56 +000062}
63
Dan Gohman7dfd13c2010-10-25 21:48:30 +000064llvm::MDNode *CodeGenTBAA::getChar() {
65 // Define the root of the tree for user-accessible memory. C and C++
66 // give special powers to char and certain similar types. However,
67 // these special powers only cover user-accessible memory, and doesn't
68 // include things like vtables.
69 if (!Char)
Manman Rene1ad74e2013-04-11 23:02:56 +000070 Char = createTBAAScalarType("omnipotent char", getRoot());
Dan Gohman7dfd13c2010-10-25 21:48:30 +000071
72 return Char;
73}
74
Dan Gohmanc2897692010-12-13 23:51:08 +000075static bool TypeHasMayAlias(QualType QTy) {
76 // Tagged types have declarations, and therefore may have attributes.
77 if (const TagType *TTy = dyn_cast<TagType>(QTy))
78 return TTy->getDecl()->hasAttr<MayAliasAttr>();
79
80 // Typedef types have declarations, and therefore may have attributes.
81 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
82 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
83 return true;
84 // Also, their underlying types may have relevant attributes.
85 return TypeHasMayAlias(TTy->desugar());
86 }
87
88 return false;
89}
90
Ivan A. Kosarev289574e2017-10-02 09:54:47 +000091llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
Manman Ren4f755de2013-10-08 00:08:49 +000092 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000093 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
Craig Topper8a13c412014-05-21 05:09:00 +000094 return nullptr;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000095
Dan Gohmanc2897692010-12-13 23:51:08 +000096 // If the type has the may_alias attribute (even on a typedef), it is
97 // effectively in the general char alias class.
98 if (TypeHasMayAlias(QTy))
99 return getChar();
100
John McCall424cec92011-01-19 06:33:43 +0000101 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman947c9af2010-10-14 23:06:10 +0000102
103 if (llvm::MDNode *N = MetadataCache[Ty])
104 return N;
105
Dan Gohman5419ce62010-10-21 18:49:12 +0000106 // Handle builtin types.
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000107 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman947c9af2010-10-14 23:06:10 +0000108 switch (BTy->getKind()) {
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000109 // Character types are special and can alias anything.
110 // In C++, this technically only includes "char" and "unsigned char",
111 // and not "signed char". In C, it includes all three. For now,
Dan Gohman4a3b1b32010-10-15 20:24:10 +0000112 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000113 // the benefit.
Dan Gohman947c9af2010-10-14 23:06:10 +0000114 case BuiltinType::Char_U:
115 case BuiltinType::Char_S:
116 case BuiltinType::UChar:
117 case BuiltinType::SChar:
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000118 return getChar();
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000119
120 // Unsigned types can alias their corresponding signed types.
121 case BuiltinType::UShort:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000122 return getTypeInfo(Context.ShortTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000123 case BuiltinType::UInt:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000124 return getTypeInfo(Context.IntTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000125 case BuiltinType::ULong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000126 return getTypeInfo(Context.LongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000127 case BuiltinType::ULongLong:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000128 return getTypeInfo(Context.LongLongTy);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000129 case BuiltinType::UInt128:
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000130 return getTypeInfo(Context.Int128Ty);
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000131
Dan Gohman2d0a3c72010-10-15 20:24:53 +0000132 // Treat all other builtin types as distinct types. This includes
133 // treating wchar_t, char16_t, and char32_t as distinct from their
134 // "underlying types".
Dan Gohman947c9af2010-10-14 23:06:10 +0000135 default:
136 return MetadataCache[Ty] =
Manman Rene1ad74e2013-04-11 23:02:56 +0000137 createTBAAScalarType(BTy->getName(Features), getChar());
Dan Gohman947c9af2010-10-14 23:06:10 +0000138 }
139 }
140
David Majnemer8f94a232017-07-25 23:33:58 +0000141 // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
142 // an object through a glvalue of other than one of the following types the
143 // behavior is undefined: [...] a char, unsigned char, or std::byte type."
144 if (Ty->isStdByteType())
145 return MetadataCache[Ty] = getChar();
146
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000147 // Handle pointers and references.
Dan Gohmanc44fd642010-10-15 20:26:20 +0000148 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
149 // pointers distinct.
Ivan A. Kosarevb75a50b2017-09-26 14:22:48 +0000150 if (Ty->isPointerType() || Ty->isReferenceType())
Manman Rene1ad74e2013-04-11 23:02:56 +0000151 return MetadataCache[Ty] = createTBAAScalarType("any pointer",
152 getChar());
Dan Gohmand65c1962010-10-15 00:01:39 +0000153
Dan Gohman2e29eb52010-10-15 20:23:12 +0000154 // Enum types are distinct types. In C++ they have "underlying types",
155 // however they aren't related for TBAA.
156 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman2e29eb52010-10-15 20:23:12 +0000157 // In C++ mode, types have linkage, so we can rely on the ODR and
158 // on their mangled names, if they're external.
159 // TODO: Is there a way to get a program-wide unique name for a
160 // decl with local linkage or no linkage?
Eli Friedmaneecc09a2013-07-05 20:27:40 +0000161 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000162 return MetadataCache[Ty] = getChar();
Dan Gohman2e29eb52010-10-15 20:23:12 +0000163
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000164 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000165 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000166 MContext.mangleTypeName(QualType(ETy, 0), Out);
Manman Rene1ad74e2013-04-11 23:02:56 +0000167 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
Dan Gohman2e29eb52010-10-15 20:23:12 +0000168 }
169
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000170 // For now, handle any other kind of type conservatively.
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000171 return MetadataCache[Ty] = getChar();
Dan Gohman947c9af2010-10-14 23:06:10 +0000172}
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000173
174llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Manman Rene1ad74e2013-04-11 23:02:56 +0000175 return createTBAAScalarType("vtable pointer", getRoot());
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000176}
Dan Gohman22695fc2012-09-28 21:58:29 +0000177
178bool
179CodeGenTBAA::CollectFields(uint64_t BaseOffset,
180 QualType QTy,
181 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
182 Fields,
183 bool MayAlias) {
184 /* Things not handled yet include: C++ base classes, bitfields, */
185
186 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
187 const RecordDecl *RD = TTy->getDecl()->getDefinition();
188 if (RD->hasFlexibleArrayMember())
189 return false;
190
191 // TODO: Handle C++ base classes.
192 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
193 if (Decl->bases_begin() != Decl->bases_end())
194 return false;
195
196 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
197
198 unsigned idx = 0;
199 for (RecordDecl::field_iterator i = RD->field_begin(),
200 e = RD->field_end(); i != e; ++i, ++idx) {
201 uint64_t Offset = BaseOffset +
202 Layout.getFieldOffset(idx) / Context.getCharWidth();
203 QualType FieldQTy = i->getType();
204 if (!CollectFields(Offset, FieldQTy, Fields,
205 MayAlias || TypeHasMayAlias(FieldQTy)))
206 return false;
207 }
208 return true;
209 }
210
211 /* Otherwise, treat whatever it is as a field. */
212 uint64_t Offset = BaseOffset;
213 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000214 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTypeInfo(QTy);
Manman Ren4f755de2013-10-08 00:08:49 +0000215 llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
Manman Ren09a39122013-04-22 19:50:07 +0000216 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohman22695fc2012-09-28 21:58:29 +0000217 return true;
218}
219
220llvm::MDNode *
221CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
222 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
223
224 if (llvm::MDNode *N = StructMetadataCache[Ty])
225 return N;
226
227 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
228 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
229 return MDHelper.createTBAAStructNode(Fields);
230
231 // For now, handle any other kind of type conservatively.
Craig Topper8a13c412014-05-21 05:09:00 +0000232 return StructMetadataCache[Ty] = nullptr;
Dan Gohman22695fc2012-09-28 21:58:29 +0000233}
Manman Renc451e572013-04-04 21:53:22 +0000234
235/// Check if the given type can be handled by path-aware TBAA.
236static bool isTBAAPathStruct(QualType QTy) {
237 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
238 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Rene36d3412013-04-30 17:38:09 +0000239 if (RD->hasFlexibleArrayMember())
240 return false;
Manman Renc451e572013-04-04 21:53:22 +0000241 // RD can be struct, union, class, interface or enum.
Manman Rene36d3412013-04-30 17:38:09 +0000242 // For now, we only handle struct and class.
243 if (RD->isStruct() || RD->isClass())
Manman Renc451e572013-04-04 21:53:22 +0000244 return true;
245 }
246 return false;
247}
248
249llvm::MDNode *
250CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
251 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
252 assert(isTBAAPathStruct(QTy));
253
254 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
255 return N;
256
257 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
258 const RecordDecl *RD = TTy->getDecl()->getDefinition();
259
260 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Manman Rena6b73032013-04-27 00:26:07 +0000261 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
Manman Renc451e572013-04-04 21:53:22 +0000262 unsigned idx = 0;
263 for (RecordDecl::field_iterator i = RD->field_begin(),
264 e = RD->field_end(); i != e; ++i, ++idx) {
265 QualType FieldQTy = i->getType();
266 llvm::MDNode *FieldNode;
267 if (isTBAAPathStruct(FieldQTy))
268 FieldNode = getTBAAStructTypeInfo(FieldQTy);
Manman Rene1ad74e2013-04-11 23:02:56 +0000269 else
Ivan A. Kosarev289574e2017-10-02 09:54:47 +0000270 FieldNode = getTypeInfo(FieldQTy);
Manman Renc451e572013-04-04 21:53:22 +0000271 if (!FieldNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000272 return StructTypeMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000273 Fields.push_back(std::make_pair(
Manman Rena6b73032013-04-27 00:26:07 +0000274 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
Manman Renc451e572013-04-04 21:53:22 +0000275 }
276
Manman Renc451e572013-04-04 21:53:22 +0000277 SmallString<256> OutName;
Manman Ren879ce882013-08-21 20:58:45 +0000278 if (Features.CPlusPlus) {
Reid Klecknercc99e262013-11-19 23:23:00 +0000279 // Don't use the mangler for C code.
Manman Ren879ce882013-08-21 20:58:45 +0000280 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000281 MContext.mangleTypeName(QualType(Ty, 0), Out);
Manman Ren879ce882013-08-21 20:58:45 +0000282 } else {
283 OutName = RD->getName();
284 }
Manman Renc451e572013-04-04 21:53:22 +0000285 // Create the struct type node with a vector of pairs (offset, type).
286 return StructTypeMetadataCache[Ty] =
287 MDHelper.createTBAAStructTypeNode(OutName, Fields);
288 }
289
Craig Topper8a13c412014-05-21 05:09:00 +0000290 return StructMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000291}
292
Manman Ren4f755de2013-10-08 00:08:49 +0000293/// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
Manman Renc451e572013-04-04 21:53:22 +0000294llvm::MDNode *
295CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
296 uint64_t Offset) {
Manman Ren4f755de2013-10-08 00:08:49 +0000297 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000298 return nullptr;
Manman Ren4f755de2013-10-08 00:08:49 +0000299
Manman Renc451e572013-04-04 21:53:22 +0000300 if (!CodeGenOpts.StructPathTBAA)
Manman Ren4f755de2013-10-08 00:08:49 +0000301 return getTBAAScalarTagInfo(AccessNode);
Manman Renc451e572013-04-04 21:53:22 +0000302
303 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
304 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
305 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
306 return N;
307
Craig Topper8a13c412014-05-21 05:09:00 +0000308 llvm::MDNode *BNode = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000309 if (isTBAAPathStruct(BaseQTy))
310 BNode = getTBAAStructTypeInfo(BaseQTy);
311 if (!BNode)
Manman Rene1ad74e2013-04-11 23:02:56 +0000312 return StructTagMetadataCache[PathTag] =
313 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Manman Renc451e572013-04-04 21:53:22 +0000314
315 return StructTagMetadataCache[PathTag] =
316 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
317}
Manman Rene1ad74e2013-04-11 23:02:56 +0000318
319llvm::MDNode *
320CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
Manman Ren4f755de2013-10-08 00:08:49 +0000321 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000322 return nullptr;
Manman Rene1ad74e2013-04-11 23:02:56 +0000323 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
324 return N;
325
326 return ScalarTagMetadataCache[AccessNode] =
327 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
328}