blob: ee232ce7736f06bb7a4bbb202040c9cded5cba3c [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 Gomezb5250d32015-10-20 12:52:55 +000039CodeGenTBAA::~CodeGenTBAA() = default;
Dan Gohman947c9af2010-10-14 23:06:10 +000040
Dan Gohman7dfd13c2010-10-25 21:48:30 +000041llvm::MDNode *CodeGenTBAA::getRoot() {
42 // Define the root of the tree. This identifies the tree, so that
43 // if our LLVM IR is linked with LLVM IR from a different front-end
44 // (or a different version of this front-end), their TBAA trees will
45 // remain distinct, and the optimizer will treat them conservatively.
46 if (!Root)
Duncan Sands65229ed2012-04-16 16:29:47 +000047 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman7dfd13c2010-10-25 21:48:30 +000048
49 return Root;
50}
51
Manman Ren4f755de2013-10-08 00:08:49 +000052// For both scalar TBAA and struct-path aware TBAA, the scalar type has the
53// same format: name, parent node, and offset.
Manman Rene1ad74e2013-04-11 23:02:56 +000054llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
55 llvm::MDNode *Parent) {
Manman Ren4f755de2013-10-08 00:08:49 +000056 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Rene1ad74e2013-04-11 23:02:56 +000057}
58
Dan Gohman7dfd13c2010-10-25 21:48:30 +000059llvm::MDNode *CodeGenTBAA::getChar() {
60 // Define the root of the tree for user-accessible memory. C and C++
61 // give special powers to char and certain similar types. However,
62 // these special powers only cover user-accessible memory, and doesn't
63 // include things like vtables.
64 if (!Char)
Manman Rene1ad74e2013-04-11 23:02:56 +000065 Char = createTBAAScalarType("omnipotent char", getRoot());
Dan Gohman7dfd13c2010-10-25 21:48:30 +000066
67 return Char;
68}
69
Dan Gohmanc2897692010-12-13 23:51:08 +000070static bool TypeHasMayAlias(QualType QTy) {
71 // Tagged types have declarations, and therefore may have attributes.
72 if (const TagType *TTy = dyn_cast<TagType>(QTy))
73 return TTy->getDecl()->hasAttr<MayAliasAttr>();
74
75 // Typedef types have declarations, and therefore may have attributes.
76 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
77 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
78 return true;
79 // Also, their underlying types may have relevant attributes.
80 return TypeHasMayAlias(TTy->desugar());
81 }
82
83 return false;
84}
85
Dan Gohman947c9af2010-10-14 23:06:10 +000086llvm::MDNode *
87CodeGenTBAA::getTBAAInfo(QualType QTy) {
Manman Ren4f755de2013-10-08 00:08:49 +000088 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000089 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
Craig Topper8a13c412014-05-21 05:09:00 +000090 return nullptr;
Kostya Serebryany5dd2cfc2012-04-24 06:57:01 +000091
Dan Gohmanc2897692010-12-13 23:51:08 +000092 // If the type has the may_alias attribute (even on a typedef), it is
93 // effectively in the general char alias class.
94 if (TypeHasMayAlias(QTy))
95 return getChar();
96
John McCall424cec92011-01-19 06:33:43 +000097 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman947c9af2010-10-14 23:06:10 +000098
99 if (llvm::MDNode *N = MetadataCache[Ty])
100 return N;
101
Dan Gohman5419ce62010-10-21 18:49:12 +0000102 // Handle builtin types.
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000103 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman947c9af2010-10-14 23:06:10 +0000104 switch (BTy->getKind()) {
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000105 // Character types are special and can alias anything.
106 // In C++, this technically only includes "char" and "unsigned char",
107 // and not "signed char". In C, it includes all three. For now,
Dan Gohman4a3b1b32010-10-15 20:24:10 +0000108 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohmanf5c5e072010-10-15 17:52:03 +0000109 // the benefit.
Dan Gohman947c9af2010-10-14 23:06:10 +0000110 case BuiltinType::Char_U:
111 case BuiltinType::Char_S:
112 case BuiltinType::UChar:
113 case BuiltinType::SChar:
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000114 return getChar();
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000115
116 // Unsigned types can alias their corresponding signed types.
117 case BuiltinType::UShort:
118 return getTBAAInfo(Context.ShortTy);
119 case BuiltinType::UInt:
120 return getTBAAInfo(Context.IntTy);
121 case BuiltinType::ULong:
122 return getTBAAInfo(Context.LongTy);
123 case BuiltinType::ULongLong:
124 return getTBAAInfo(Context.LongLongTy);
125 case BuiltinType::UInt128:
126 return getTBAAInfo(Context.Int128Ty);
127
Dan Gohman2d0a3c72010-10-15 20:24:53 +0000128 // Treat all other builtin types as distinct types. This includes
129 // treating wchar_t, char16_t, and char32_t as distinct from their
130 // "underlying types".
Dan Gohman947c9af2010-10-14 23:06:10 +0000131 default:
132 return MetadataCache[Ty] =
Manman Rene1ad74e2013-04-11 23:02:56 +0000133 createTBAAScalarType(BTy->getName(Features), getChar());
Dan Gohman947c9af2010-10-14 23:06:10 +0000134 }
135 }
136
Dan Gohman5419ce62010-10-21 18:49:12 +0000137 // Handle pointers.
Dan Gohmanc44fd642010-10-15 20:26:20 +0000138 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
139 // pointers distinct.
Dan Gohmand65c1962010-10-15 00:01:39 +0000140 if (Ty->isPointerType())
Manman Rene1ad74e2013-04-11 23:02:56 +0000141 return MetadataCache[Ty] = createTBAAScalarType("any pointer",
142 getChar());
Dan Gohmand65c1962010-10-15 00:01:39 +0000143
Dan Gohman2e29eb52010-10-15 20:23:12 +0000144 // Enum types are distinct types. In C++ they have "underlying types",
145 // however they aren't related for TBAA.
146 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman2e29eb52010-10-15 20:23:12 +0000147 // In C++ mode, types have linkage, so we can rely on the ODR and
148 // on their mangled names, if they're external.
149 // TODO: Is there a way to get a program-wide unique name for a
150 // decl with local linkage or no linkage?
Eli Friedmaneecc09a2013-07-05 20:27:40 +0000151 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000152 return MetadataCache[Ty] = getChar();
Dan Gohman2e29eb52010-10-15 20:23:12 +0000153
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000154 SmallString<256> OutName;
Rafael Espindola3968cd02011-02-11 02:52:17 +0000155 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000156 MContext.mangleTypeName(QualType(ETy, 0), Out);
Manman Rene1ad74e2013-04-11 23:02:56 +0000157 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
Dan Gohman2e29eb52010-10-15 20:23:12 +0000158 }
159
Dan Gohman3f1cf0f2010-10-14 23:39:00 +0000160 // For now, handle any other kind of type conservatively.
Dan Gohman7dfd13c2010-10-25 21:48:30 +0000161 return MetadataCache[Ty] = getChar();
Dan Gohman947c9af2010-10-14 23:06:10 +0000162}
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000163
164llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Manman Rene1ad74e2013-04-11 23:02:56 +0000165 return createTBAAScalarType("vtable pointer", getRoot());
Kostya Serebryany141e46f2012-03-26 17:03:51 +0000166}
Dan Gohman22695fc2012-09-28 21:58:29 +0000167
168bool
169CodeGenTBAA::CollectFields(uint64_t BaseOffset,
170 QualType QTy,
171 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
172 Fields,
173 bool MayAlias) {
174 /* Things not handled yet include: C++ base classes, bitfields, */
175
176 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
177 const RecordDecl *RD = TTy->getDecl()->getDefinition();
178 if (RD->hasFlexibleArrayMember())
179 return false;
180
181 // TODO: Handle C++ base classes.
182 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
183 if (Decl->bases_begin() != Decl->bases_end())
184 return false;
185
186 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
187
188 unsigned idx = 0;
189 for (RecordDecl::field_iterator i = RD->field_begin(),
190 e = RD->field_end(); i != e; ++i, ++idx) {
191 uint64_t Offset = BaseOffset +
192 Layout.getFieldOffset(idx) / Context.getCharWidth();
193 QualType FieldQTy = i->getType();
194 if (!CollectFields(Offset, FieldQTy, Fields,
195 MayAlias || TypeHasMayAlias(FieldQTy)))
196 return false;
197 }
198 return true;
199 }
200
201 /* Otherwise, treat whatever it is as a field. */
202 uint64_t Offset = BaseOffset;
203 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
204 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
Manman Ren4f755de2013-10-08 00:08:49 +0000205 llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
Manman Ren09a39122013-04-22 19:50:07 +0000206 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohman22695fc2012-09-28 21:58:29 +0000207 return true;
208}
209
210llvm::MDNode *
211CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
212 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
213
214 if (llvm::MDNode *N = StructMetadataCache[Ty])
215 return N;
216
217 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
218 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
219 return MDHelper.createTBAAStructNode(Fields);
220
221 // For now, handle any other kind of type conservatively.
Craig Topper8a13c412014-05-21 05:09:00 +0000222 return StructMetadataCache[Ty] = nullptr;
Dan Gohman22695fc2012-09-28 21:58:29 +0000223}
Manman Renc451e572013-04-04 21:53:22 +0000224
225/// Check if the given type can be handled by path-aware TBAA.
226static bool isTBAAPathStruct(QualType QTy) {
227 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
228 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Rene36d3412013-04-30 17:38:09 +0000229 if (RD->hasFlexibleArrayMember())
230 return false;
Manman Renc451e572013-04-04 21:53:22 +0000231 // RD can be struct, union, class, interface or enum.
Manman Rene36d3412013-04-30 17:38:09 +0000232 // For now, we only handle struct and class.
233 if (RD->isStruct() || RD->isClass())
Manman Renc451e572013-04-04 21:53:22 +0000234 return true;
235 }
236 return false;
237}
238
239llvm::MDNode *
240CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
241 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
242 assert(isTBAAPathStruct(QTy));
243
244 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
245 return N;
246
247 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
248 const RecordDecl *RD = TTy->getDecl()->getDefinition();
249
250 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Manman Rena6b73032013-04-27 00:26:07 +0000251 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
Manman Renc451e572013-04-04 21:53:22 +0000252 unsigned idx = 0;
253 for (RecordDecl::field_iterator i = RD->field_begin(),
254 e = RD->field_end(); i != e; ++i, ++idx) {
255 QualType FieldQTy = i->getType();
256 llvm::MDNode *FieldNode;
257 if (isTBAAPathStruct(FieldQTy))
258 FieldNode = getTBAAStructTypeInfo(FieldQTy);
Manman Rene1ad74e2013-04-11 23:02:56 +0000259 else
Manman Renc451e572013-04-04 21:53:22 +0000260 FieldNode = getTBAAInfo(FieldQTy);
Manman Renc451e572013-04-04 21:53:22 +0000261 if (!FieldNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000262 return StructTypeMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000263 Fields.push_back(std::make_pair(
Manman Rena6b73032013-04-27 00:26:07 +0000264 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
Manman Renc451e572013-04-04 21:53:22 +0000265 }
266
Manman Renc451e572013-04-04 21:53:22 +0000267 SmallString<256> OutName;
Manman Ren879ce882013-08-21 20:58:45 +0000268 if (Features.CPlusPlus) {
Reid Klecknercc99e262013-11-19 23:23:00 +0000269 // Don't use the mangler for C code.
Manman Ren879ce882013-08-21 20:58:45 +0000270 llvm::raw_svector_ostream Out(OutName);
Reid Klecknercc99e262013-11-19 23:23:00 +0000271 MContext.mangleTypeName(QualType(Ty, 0), Out);
Manman Ren879ce882013-08-21 20:58:45 +0000272 } else {
273 OutName = RD->getName();
274 }
Manman Renc451e572013-04-04 21:53:22 +0000275 // Create the struct type node with a vector of pairs (offset, type).
276 return StructTypeMetadataCache[Ty] =
277 MDHelper.createTBAAStructTypeNode(OutName, Fields);
278 }
279
Craig Topper8a13c412014-05-21 05:09:00 +0000280 return StructMetadataCache[Ty] = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000281}
282
Manman Ren4f755de2013-10-08 00:08:49 +0000283/// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
Manman Renc451e572013-04-04 21:53:22 +0000284llvm::MDNode *
285CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
286 uint64_t Offset) {
Manman Ren4f755de2013-10-08 00:08:49 +0000287 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000288 return nullptr;
Manman Ren4f755de2013-10-08 00:08:49 +0000289
Manman Renc451e572013-04-04 21:53:22 +0000290 if (!CodeGenOpts.StructPathTBAA)
Manman Ren4f755de2013-10-08 00:08:49 +0000291 return getTBAAScalarTagInfo(AccessNode);
Manman Renc451e572013-04-04 21:53:22 +0000292
293 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
294 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
295 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
296 return N;
297
Craig Topper8a13c412014-05-21 05:09:00 +0000298 llvm::MDNode *BNode = nullptr;
Manman Renc451e572013-04-04 21:53:22 +0000299 if (isTBAAPathStruct(BaseQTy))
300 BNode = getTBAAStructTypeInfo(BaseQTy);
301 if (!BNode)
Manman Rene1ad74e2013-04-11 23:02:56 +0000302 return StructTagMetadataCache[PathTag] =
303 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Manman Renc451e572013-04-04 21:53:22 +0000304
305 return StructTagMetadataCache[PathTag] =
306 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
307}
Manman Rene1ad74e2013-04-11 23:02:56 +0000308
309llvm::MDNode *
310CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
Manman Ren4f755de2013-10-08 00:08:49 +0000311 if (!AccessNode)
Craig Topper8a13c412014-05-21 05:09:00 +0000312 return nullptr;
Manman Rene1ad74e2013-04-11 23:02:56 +0000313 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
314 return N;
315
316 return ScalarTagMetadataCache[AccessNode] =
317 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
318}