blob: 11e376ff4034cd1781d3d9417a8fc4300526e358 [file] [log] [blame]
Dan Gohman3d5aff52010-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 Gohman565cc442010-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 Gohman780658a2010-10-15 20:54:41 +000012//
13// C99 6.5p7
14// C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
Dan Gohman3d5aff52010-10-14 23:06:10 +000015//
16//===----------------------------------------------------------------------===//
17
18#include "CodeGenTBAA.h"
19#include "clang/AST/ASTContext.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000020#include "clang/AST/Attr.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000021#include "clang/AST/Mangle.h"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000022#include "clang/AST/RecordLayout.h"
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000023#include "clang/Frontend/CodeGenOptions.h"
Manman Renb37a73d2013-04-04 21:53:22 +000024#include "llvm/ADT/SmallSet.h"
Chandler Carruth3b844ba2013-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 Gohman3d5aff52010-10-14 23:06:10 +000029using namespace clang;
30using namespace CodeGen;
31
32CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000033 const CodeGenOptions &CGO,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000034 const LangOptions &Features, MangleContext &MContext)
Benjamin Kramerfacde172012-06-06 17:32:50 +000035 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
Duncan Sands2d7cb062012-04-15 18:04:54 +000036 MDHelper(VMContext), Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000037}
38
39CodeGenTBAA::~CodeGenTBAA() {
40}
41
Dan Gohman224d7592010-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 Sands60c77072012-04-16 16:29:47 +000048 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman224d7592010-10-25 21:48:30 +000049
50 return Root;
51}
52
Manman Renfeba9f22013-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 Renca835182013-04-11 23:02:56 +000055llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
56 llvm::MDNode *Parent) {
Manman Renfeba9f22013-10-08 00:08:49 +000057 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Renca835182013-04-11 23:02:56 +000058}
59
Dan Gohman224d7592010-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 Renca835182013-04-11 23:02:56 +000066 Char = createTBAAScalarType("omnipotent char", getRoot());
Dan Gohman224d7592010-10-25 21:48:30 +000067
68 return Char;
69}
70
Dan Gohman2ea7e732010-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 Gohman3d5aff52010-10-14 23:06:10 +000087llvm::MDNode *
88CodeGenTBAA::getTBAAInfo(QualType QTy) {
Manman Renfeba9f22013-10-08 00:08:49 +000089 // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000090 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
91 return NULL;
92
Dan Gohman2ea7e732010-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 McCallf4c73712011-01-19 06:33:43 +000098 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +000099
100 if (llvm::MDNode *N = MetadataCache[Ty])
101 return N;
102
Dan Gohman565cc442010-10-21 18:49:12 +0000103 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +0000104 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +0000105 switch (BTy->getKind()) {
Dan Gohman85623482010-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 Gohman0a531982010-10-15 20:24:10 +0000109 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000110 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000111 case BuiltinType::Char_U:
112 case BuiltinType::Char_S:
113 case BuiltinType::UChar:
114 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000115 return getChar();
Dan Gohman9af2f832010-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 Gohman2f8c21d2010-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 Gohman3d5aff52010-10-14 23:06:10 +0000132 default:
133 return MetadataCache[Ty] =
Manman Renca835182013-04-11 23:02:56 +0000134 createTBAAScalarType(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000135 }
136 }
137
Dan Gohman565cc442010-10-21 18:49:12 +0000138 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000139 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
140 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000141 if (Ty->isPointerType())
Manman Renca835182013-04-11 23:02:56 +0000142 return MetadataCache[Ty] = createTBAAScalarType("any pointer",
143 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000144
Dan Gohman0b5c4fc2010-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 Gohman0b5c4fc2010-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 Friedman1847c712013-07-05 20:27:40 +0000152 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Dan Gohman224d7592010-10-25 21:48:30 +0000153 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000154
155 // TODO: This is using the RTTI name. Is there a better way to get
156 // a unique string for a type?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000157 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000158 llvm::raw_svector_ostream Out(OutName);
159 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
160 Out.flush();
Manman Renca835182013-04-11 23:02:56 +0000161 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000162 }
163
Dan Gohman9af2f832010-10-14 23:39:00 +0000164 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000165 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000166}
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000167
168llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Manman Renca835182013-04-11 23:02:56 +0000169 return createTBAAScalarType("vtable pointer", getRoot());
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000170}
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000171
172bool
173CodeGenTBAA::CollectFields(uint64_t BaseOffset,
174 QualType QTy,
175 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
176 Fields,
177 bool MayAlias) {
178 /* Things not handled yet include: C++ base classes, bitfields, */
179
180 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
181 const RecordDecl *RD = TTy->getDecl()->getDefinition();
182 if (RD->hasFlexibleArrayMember())
183 return false;
184
185 // TODO: Handle C++ base classes.
186 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
187 if (Decl->bases_begin() != Decl->bases_end())
188 return false;
189
190 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
191
192 unsigned idx = 0;
193 for (RecordDecl::field_iterator i = RD->field_begin(),
194 e = RD->field_end(); i != e; ++i, ++idx) {
195 uint64_t Offset = BaseOffset +
196 Layout.getFieldOffset(idx) / Context.getCharWidth();
197 QualType FieldQTy = i->getType();
198 if (!CollectFields(Offset, FieldQTy, Fields,
199 MayAlias || TypeHasMayAlias(FieldQTy)))
200 return false;
201 }
202 return true;
203 }
204
205 /* Otherwise, treat whatever it is as a field. */
206 uint64_t Offset = BaseOffset;
207 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
208 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
Manman Renfeba9f22013-10-08 00:08:49 +0000209 llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
Manman Renc7f2bfb2013-04-22 19:50:07 +0000210 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000211 return true;
212}
213
214llvm::MDNode *
215CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
216 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
217
218 if (llvm::MDNode *N = StructMetadataCache[Ty])
219 return N;
220
221 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
222 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
223 return MDHelper.createTBAAStructNode(Fields);
224
225 // For now, handle any other kind of type conservatively.
226 return StructMetadataCache[Ty] = NULL;
227}
Manman Renb37a73d2013-04-04 21:53:22 +0000228
229/// Check if the given type can be handled by path-aware TBAA.
230static bool isTBAAPathStruct(QualType QTy) {
231 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
232 const RecordDecl *RD = TTy->getDecl()->getDefinition();
Manman Renc7d77b22013-04-30 17:38:09 +0000233 if (RD->hasFlexibleArrayMember())
234 return false;
Manman Renb37a73d2013-04-04 21:53:22 +0000235 // RD can be struct, union, class, interface or enum.
Manman Renc7d77b22013-04-30 17:38:09 +0000236 // For now, we only handle struct and class.
237 if (RD->isStruct() || RD->isClass())
Manman Renb37a73d2013-04-04 21:53:22 +0000238 return true;
239 }
240 return false;
241}
242
243llvm::MDNode *
244CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
245 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
246 assert(isTBAAPathStruct(QTy));
247
248 if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
249 return N;
250
251 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
252 const RecordDecl *RD = TTy->getDecl()->getDefinition();
253
254 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Manman Ren50be9042013-04-27 00:26:07 +0000255 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
Manman Renb37a73d2013-04-04 21:53:22 +0000256 unsigned idx = 0;
257 for (RecordDecl::field_iterator i = RD->field_begin(),
258 e = RD->field_end(); i != e; ++i, ++idx) {
259 QualType FieldQTy = i->getType();
260 llvm::MDNode *FieldNode;
261 if (isTBAAPathStruct(FieldQTy))
262 FieldNode = getTBAAStructTypeInfo(FieldQTy);
Manman Renca835182013-04-11 23:02:56 +0000263 else
Manman Renb37a73d2013-04-04 21:53:22 +0000264 FieldNode = getTBAAInfo(FieldQTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000265 if (!FieldNode)
266 return StructTypeMetadataCache[Ty] = NULL;
267 Fields.push_back(std::make_pair(
Manman Ren50be9042013-04-27 00:26:07 +0000268 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
Manman Renb37a73d2013-04-04 21:53:22 +0000269 }
270
271 // TODO: This is using the RTTI name. Is there a better way to get
272 // a unique string for a type?
273 SmallString<256> OutName;
Manman Ren312c0b62013-08-21 20:58:45 +0000274 if (Features.CPlusPlus) {
275 // Don't use mangleCXXRTTIName for C code.
276 llvm::raw_svector_ostream Out(OutName);
277 MContext.mangleCXXRTTIName(QualType(Ty, 0), Out);
278 Out.flush();
279 } else {
280 OutName = RD->getName();
281 }
Manman Renb37a73d2013-04-04 21:53:22 +0000282 // Create the struct type node with a vector of pairs (offset, type).
283 return StructTypeMetadataCache[Ty] =
284 MDHelper.createTBAAStructTypeNode(OutName, Fields);
285 }
286
287 return StructMetadataCache[Ty] = NULL;
288}
289
Manman Renfeba9f22013-10-08 00:08:49 +0000290/// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
Manman Renb37a73d2013-04-04 21:53:22 +0000291llvm::MDNode *
292CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
293 uint64_t Offset) {
Manman Renfeba9f22013-10-08 00:08:49 +0000294 if (!AccessNode)
295 return NULL;
296
Manman Renb37a73d2013-04-04 21:53:22 +0000297 if (!CodeGenOpts.StructPathTBAA)
Manman Renfeba9f22013-10-08 00:08:49 +0000298 return getTBAAScalarTagInfo(AccessNode);
Manman Renb37a73d2013-04-04 21:53:22 +0000299
300 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
301 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
302 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
303 return N;
304
305 llvm::MDNode *BNode = 0;
306 if (isTBAAPathStruct(BaseQTy))
307 BNode = getTBAAStructTypeInfo(BaseQTy);
308 if (!BNode)
Manman Renca835182013-04-11 23:02:56 +0000309 return StructTagMetadataCache[PathTag] =
310 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Manman Renb37a73d2013-04-04 21:53:22 +0000311
312 return StructTagMetadataCache[PathTag] =
313 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
314}
Manman Renca835182013-04-11 23:02:56 +0000315
316llvm::MDNode *
317CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
Manman Renfeba9f22013-10-08 00:08:49 +0000318 if (!AccessNode)
319 return NULL;
Manman Renca835182013-04-11 23:02:56 +0000320 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
321 return N;
322
323 return ScalarTagMetadataCache[AccessNode] =
324 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
325}