blob: c3c925cde2fd1f825563d0dca632a15d393c40ef [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"
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +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.
47 if (!Root)
Duncan Sands65229ed2012-04-16 16:29:47 +000048 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman7dfd13c2010-10-25 21:48:30 +000049
50 return Root;
51}
52
Manman Ren4f755de2013-10-08 00:08:49 +000053// For both scalar TBAA and struct-path aware TBAA, the scalar type has the
54// same format: name, parent node, and offset.
Manman Rene1ad74e2013-04-11 23:02:56 +000055llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
56 llvm::MDNode *Parent) {
Manman Ren4f755de2013-10-08 00:08:49 +000057 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Rene1ad74e2013-04-11 23:02:56 +000058}
59
Dan Gohman7dfd13c2010-10-25 21:48:30 +000060llvm::MDNode *CodeGenTBAA::getChar() {
61 // Define the root of the tree for user-accessible memory. C and C++
62 // give special powers to char and certain similar types. However,
63 // these special powers only cover user-accessible memory, and doesn't
64 // include things like vtables.
65 if (!Char)
Manman Rene1ad74e2013-04-11 23:02:56 +000066 Char = createTBAAScalarType("omnipotent char", getRoot());
Dan Gohman7dfd13c2010-10-25 21:48:30 +000067
68 return Char;
69}
70
Dan Gohmanc2897692010-12-13 23:51:08 +000071static bool TypeHasMayAlias(QualType QTy) {
72 // Tagged types have declarations, and therefore may have attributes.
73 if (const TagType *TTy = dyn_cast<TagType>(QTy))
74 return TTy->getDecl()->hasAttr<MayAliasAttr>();
75
76 // Typedef types have declarations, and therefore may have attributes.
77 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
78 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
79 return true;
80 // Also, their underlying types may have relevant attributes.
81 return TypeHasMayAlias(TTy->desugar());
82 }
83
84 return false;
85}
86
Dan Gohman947c9af2010-10-14 23:06:10 +000087llvm::MDNode *
88CodeGenTBAA::getTBAAInfo(QualType QTy) {
Manman Ren4f755de2013-10-08 00:08:49 +000089 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000090 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
Craig Topper8a13c412014-05-21 05:09:00 +000091 return nullptr;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000092
Dan Gohmanc2897692010-12-13 23:51:08 +000093 // If the type has the may_alias attribute (even on a typedef), it is
94 // effectively in the general char alias class.
95 if (TypeHasMayAlias(QTy))
96 return getChar();
97
John McCall424cec92011-01-19 06:33:43 +000098 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman947c9af2010-10-14 23:06:10 +000099
100 if (llvm::MDNode *N = MetadataCache[Ty])
101 return N;
102
Dan Gohman5419ce62010-10-21 18:49:12 +0000103 // Handle builtin types.
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000104 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman947c9af2010-10-14 23:06:10 +0000105 switch (BTy->getKind()) {
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000106 // Character types are special and can alias anything.
107 // In C++, this technically only includes "char" and "unsigned char",
108 // and not "signed char". In C, it includes all three. For now,
Dan Gohman4a3b1b32010-10-15 20:24:10 +0000109 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000110 // the benefit.
Dan Gohman947c9af2010-10-14 23:06:10 +0000111 case BuiltinType::Char_U:
112 case BuiltinType::Char_S:
113 case BuiltinType::UChar:
114 case BuiltinType::SChar:
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000115 return getChar();
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000116
117 // Unsigned types can alias their corresponding signed types.
118 case BuiltinType::UShort:
119 return getTBAAInfo(Context.ShortTy);
120 case BuiltinType::UInt:
121 return getTBAAInfo(Context.IntTy);
122 case BuiltinType::ULong:
123 return getTBAAInfo(Context.LongTy);
124 case BuiltinType::ULongLong:
125 return getTBAAInfo(Context.LongLongTy);
126 case BuiltinType::UInt128:
127 return getTBAAInfo(Context.Int128Ty);
128
Dan Gohman2d0a3c72010-10-15 20:24:53 +0000129 // Treat all other builtin types as distinct types. This includes
130 // treating wchar_t, char16_t, and char32_t as distinct from their
131 // "underlying types".
Dan Gohman947c9af2010-10-14 23:06:10 +0000132 default:
133 return MetadataCache[Ty] =
Manman Rene1ad74e2013-04-11 23:02:56 +0000134 createTBAAScalarType(BTy->getName(Features), getChar());
Dan Gohman947c9af2010-10-14 23:06:10 +0000135 }
136 }
137
Dan Gohman5419ce62010-10-21 18:49:12 +0000138 // Handle pointers.
Dan Gohmanc44fd642010-10-15 20:26:20 +0000139 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
140 // pointers distinct.
Dan Gohmand65c1962010-10-15 00:01:39 +0000141 if (Ty->isPointerType())
Manman Rene1ad74e2013-04-11 23:02:56 +0000142 return MetadataCache[Ty] = createTBAAScalarType("any pointer",
143 getChar());
Dan Gohmand65c1962010-10-15 00:01:39 +0000144
Dan Gohman2e29eb52010-10-15 20:23:12 +0000145 // Enum types are distinct types. In C++ they have "underlying types",
146 // however they aren't related for TBAA.
147 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman2e29eb52010-10-15 20:23:12 +0000148 // In C++ mode, types have linkage, so we can rely on the ODR and
149 // on their mangled names, if they're external.
150 // TODO: Is there a way to get a program-wide unique name for a
151 // decl with local linkage or no linkage?
Eli Friedmaneecc09a2013-07-05 20:27:40 +0000152 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000153 return MetadataCache[Ty] = getChar();
Dan Gohman2e29eb52010-10-15 20:23:12 +0000154
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000155 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000156 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000157 MContext.mangleTypeName(QualType(ETy, 0), Out);
Manman Rene1ad74e2013-04-11 23:02:56 +0000158 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
Dan Gohman2e29eb52010-10-15 20:23:12 +0000159 }
160
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000161 // For now, handle any other kind of type conservatively.
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000162 return MetadataCache[Ty] = getChar();
Dan Gohman947c9af2010-10-14 23:06:10 +0000163}
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000164
165llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Manman Rene1ad74e2013-04-11 23:02:56 +0000166 return createTBAAScalarType("vtable pointer", getRoot());
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000167}
Dan Gohman22695fc2012-09-28 21:58:29 +0000168
169bool
170CodeGenTBAA::CollectFields(uint64_t BaseOffset,
171 QualType QTy,
172 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
173 Fields,
174 bool MayAlias) {
175 /* Things not handled yet include: C++ base classes, bitfields, */
176
177 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
178 const RecordDecl *RD = TTy->getDecl()->getDefinition();
179 if (RD->hasFlexibleArrayMember())
180 return false;
181
182 // TODO: Handle C++ base classes.
183 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
184 if (Decl->bases_begin() != Decl->bases_end())
185 return false;
186
187 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
188
189 unsigned idx = 0;
190 for (RecordDecl::field_iterator i = RD->field_begin(),
191 e = RD->field_end(); i != e; ++i, ++idx) {
192 uint64_t Offset = BaseOffset +
193 Layout.getFieldOffset(idx) / Context.getCharWidth();
194 QualType FieldQTy = i->getType();
195 if (!CollectFields(Offset, FieldQTy, Fields,
196 MayAlias || TypeHasMayAlias(FieldQTy)))
197 return false;
198 }
199 return true;
200 }
201
202 /* Otherwise, treat whatever it is as a field. */
203 uint64_t Offset = BaseOffset;
204 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
205 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
Manman Ren4f755de2013-10-08 00:08:49 +0000206 llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
Manman Ren09a39122013-04-22 19:50:07 +0000207 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohman22695fc2012-09-28 21:58:29 +0000208 return true;
209}
210
211llvm::MDNode *
212CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
213 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
214
215 if (llvm::MDNode *N = StructMetadataCache[Ty])
216 return N;
217
218 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
219 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
220 return MDHelper.createTBAAStructNode(Fields);
221
222 // For now, handle any other kind of type conservatively.
Craig Topper8a13c412014-05-21 05:09:00 +0000223 return StructMetadataCache[Ty] = nullptr;
Dan Gohman22695fc2012-09-28 21:58:29 +0000224}
Manman Renc451e572013-04-04 21:53:22 +0000225
226/// Check if the given type can be handled by path-aware TBAA.
227static bool isTBAAPathStruct(QualType QTy) {
228 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
229 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Rene36d3412013-04-30 17:38:09 +0000230 if (RD->hasFlexibleArrayMember())
231 return false;
Manman Renc451e572013-04-04 21:53:22 +0000232 // RD can be struct, union, class, interface or enum.
Manman Rene36d3412013-04-30 17:38:09 +0000233 // For now, we only handle struct and class.
234 if (RD->isStruct() || RD->isClass())
Manman Renc451e572013-04-04 21:53:22 +0000235 return true;
236 }
237 return false;
238}
239
240llvm::MDNode *
241CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
242 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
243 assert(isTBAAPathStruct(QTy));
244
245 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
246 return N;
247
248 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
249 const RecordDecl *RD = TTy->getDecl()->getDefinition();
250
251 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Manman Rena6b73032013-04-27 00:26:07 +0000252 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
Manman Renc451e572013-04-04 21:53:22 +0000253 unsigned idx = 0;
254 for (RecordDecl::field_iterator i = RD->field_begin(),
255 e = RD->field_end(); i != e; ++i, ++idx) {
256 QualType FieldQTy = i->getType();
257 llvm::MDNode *FieldNode;
258 if (isTBAAPathStruct(FieldQTy))
259 FieldNode = getTBAAStructTypeInfo(FieldQTy);
Manman Rene1ad74e2013-04-11 23:02:56 +0000260 else
Manman Renc451e572013-04-04 21:53:22 +0000261 FieldNode = getTBAAInfo(FieldQTy);
Manman Renc451e572013-04-04 21:53:22 +0000262 if (!FieldNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000263 return StructTypeMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000264 Fields.push_back(std::make_pair(
Manman Rena6b73032013-04-27 00:26:07 +0000265 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
Manman Renc451e572013-04-04 21:53:22 +0000266 }
267
Manman Renc451e572013-04-04 21:53:22 +0000268 SmallString<256> OutName;
Manman Ren879ce882013-08-21 20:58:45 +0000269 if (Features.CPlusPlus) {
Reid Klecknercc99e262013-11-19 23:23:00 +0000270 // Don't use the mangler for C code.
Manman Ren879ce882013-08-21 20:58:45 +0000271 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000272 MContext.mangleTypeName(QualType(Ty, 0), Out);
Manman Ren879ce882013-08-21 20:58:45 +0000273 } else {
274 OutName = RD->getName();
275 }
Manman Renc451e572013-04-04 21:53:22 +0000276 // Create the struct type node with a vector of pairs (offset, type).
277 return StructTypeMetadataCache[Ty] =
278 MDHelper.createTBAAStructTypeNode(OutName, Fields);
279 }
280
Craig Topper8a13c412014-05-21 05:09:00 +0000281 return StructMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000282}
283
Manman Ren4f755de2013-10-08 00:08:49 +0000284/// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
Manman Renc451e572013-04-04 21:53:22 +0000285llvm::MDNode *
286CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
287 uint64_t Offset) {
Manman Ren4f755de2013-10-08 00:08:49 +0000288 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000289 return nullptr;
Manman Ren4f755de2013-10-08 00:08:49 +0000290
Manman Renc451e572013-04-04 21:53:22 +0000291 if (!CodeGenOpts.StructPathTBAA)
Manman Ren4f755de2013-10-08 00:08:49 +0000292 return getTBAAScalarTagInfo(AccessNode);
Manman Renc451e572013-04-04 21:53:22 +0000293
294 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
295 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
296 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
297 return N;
298
Craig Topper8a13c412014-05-21 05:09:00 +0000299 llvm::MDNode *BNode = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000300 if (isTBAAPathStruct(BaseQTy))
301 BNode = getTBAAStructTypeInfo(BaseQTy);
302 if (!BNode)
Manman Rene1ad74e2013-04-11 23:02:56 +0000303 return StructTagMetadataCache[PathTag] =
304 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Manman Renc451e572013-04-04 21:53:22 +0000305
306 return StructTagMetadataCache[PathTag] =
307 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
308}
Manman Rene1ad74e2013-04-11 23:02:56 +0000309
310llvm::MDNode *
311CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
Manman Ren4f755de2013-10-08 00:08:49 +0000312 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000313 return nullptr;
Manman Rene1ad74e2013-04-11 23:02:56 +0000314 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
315 return N;
316
317 return ScalarTagMetadataCache[AccessNode] =
318 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
319}