blob: 0a8c293cb70cd103458083df7f73a939644582d4 [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 Renca835182013-04-11 23:02:56 +000053// For struct-path aware TBAA, the scalar type has the same format as
54// the struct type: name, offset, pointer to another node in the type DAG.
55// For scalar TBAA, the scalar type is the same as the scalar tag:
56// name and a parent pointer.
57llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
58 llvm::MDNode *Parent) {
59 if (CodeGenOpts.StructPathTBAA)
Manman Ren50be9042013-04-27 00:26:07 +000060 return MDHelper.createTBAAScalarTypeNode(Name, Parent);
Manman Renca835182013-04-11 23:02:56 +000061 else
62 return MDHelper.createTBAANode(Name, Parent);
63}
64
Dan Gohman224d7592010-10-25 21:48:30 +000065llvm::MDNode *CodeGenTBAA::getChar() {
66 // Define the root of the tree for user-accessible memory. C and C++
67 // give special powers to char and certain similar types. However,
68 // these special powers only cover user-accessible memory, and doesn't
69 // include things like vtables.
70 if (!Char)
Manman Renca835182013-04-11 23:02:56 +000071 Char = createTBAAScalarType("omnipotent char", getRoot());
Dan Gohman224d7592010-10-25 21:48:30 +000072
73 return Char;
74}
75
Dan Gohman2ea7e732010-12-13 23:51:08 +000076static bool TypeHasMayAlias(QualType QTy) {
77 // Tagged types have declarations, and therefore may have attributes.
78 if (const TagType *TTy = dyn_cast<TagType>(QTy))
79 return TTy->getDecl()->hasAttr<MayAliasAttr>();
80
81 // Typedef types have declarations, and therefore may have attributes.
82 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
83 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
84 return true;
85 // Also, their underlying types may have relevant attributes.
86 return TypeHasMayAlias(TTy->desugar());
87 }
88
89 return false;
90}
91
Dan Gohman3d5aff52010-10-14 23:06:10 +000092llvm::MDNode *
93CodeGenTBAA::getTBAAInfo(QualType QTy) {
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000094 // At -O0 TBAA is not emitted for regular types.
95 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
96 return NULL;
97
Dan Gohman2ea7e732010-12-13 23:51:08 +000098 // If the type has the may_alias attribute (even on a typedef), it is
99 // effectively in the general char alias class.
100 if (TypeHasMayAlias(QTy))
101 return getChar();
102
John McCallf4c73712011-01-19 06:33:43 +0000103 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000104
105 if (llvm::MDNode *N = MetadataCache[Ty])
106 return N;
107
Dan Gohman565cc442010-10-21 18:49:12 +0000108 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +0000109 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +0000110 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +0000111 // Character types are special and can alias anything.
112 // In C++, this technically only includes "char" and "unsigned char",
113 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +0000114 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000115 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000116 case BuiltinType::Char_U:
117 case BuiltinType::Char_S:
118 case BuiltinType::UChar:
119 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000120 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000121
122 // Unsigned types can alias their corresponding signed types.
123 case BuiltinType::UShort:
124 return getTBAAInfo(Context.ShortTy);
125 case BuiltinType::UInt:
126 return getTBAAInfo(Context.IntTy);
127 case BuiltinType::ULong:
128 return getTBAAInfo(Context.LongTy);
129 case BuiltinType::ULongLong:
130 return getTBAAInfo(Context.LongLongTy);
131 case BuiltinType::UInt128:
132 return getTBAAInfo(Context.Int128Ty);
133
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000134 // Treat all other builtin types as distinct types. This includes
135 // treating wchar_t, char16_t, and char32_t as distinct from their
136 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000137 default:
138 return MetadataCache[Ty] =
Manman Renca835182013-04-11 23:02:56 +0000139 createTBAAScalarType(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000140 }
141 }
142
Dan Gohman565cc442010-10-21 18:49:12 +0000143 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000144 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
145 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000146 if (Ty->isPointerType())
Manman Renca835182013-04-11 23:02:56 +0000147 return MetadataCache[Ty] = createTBAAScalarType("any pointer",
148 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000149
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000150 // Enum types are distinct types. In C++ they have "underlying types",
151 // however they aren't related for TBAA.
152 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000153 // In C++ mode, types have linkage, so we can rely on the ODR and
154 // on their mangled names, if they're external.
155 // TODO: Is there a way to get a program-wide unique name for a
156 // decl with local linkage or no linkage?
Eli Friedman1847c712013-07-05 20:27:40 +0000157 if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
Dan Gohman224d7592010-10-25 21:48:30 +0000158 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000159
160 // TODO: This is using the RTTI name. Is there a better way to get
161 // a unique string for a type?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000162 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000163 llvm::raw_svector_ostream Out(OutName);
164 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
165 Out.flush();
Manman Renca835182013-04-11 23:02:56 +0000166 return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000167 }
168
Dan Gohman9af2f832010-10-14 23:39:00 +0000169 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000170 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000171}
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000172
173llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Manman Renca835182013-04-11 23:02:56 +0000174 return createTBAAScalarType("vtable pointer", getRoot());
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000175}
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000176
177bool
178CodeGenTBAA::CollectFields(uint64_t BaseOffset,
179 QualType QTy,
180 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
181 Fields,
182 bool MayAlias) {
183 /* Things not handled yet include: C++ base classes, bitfields, */
184
185 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
186 const RecordDecl *RD = TTy->getDecl()->getDefinition();
187 if (RD->hasFlexibleArrayMember())
188 return false;
189
190 // TODO: Handle C++ base classes.
191 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
192 if (Decl->bases_begin() != Decl->bases_end())
193 return false;
194
195 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
196
197 unsigned idx = 0;
198 for (RecordDecl::field_iterator i = RD->field_begin(),
199 e = RD->field_end(); i != e; ++i, ++idx) {
200 uint64_t Offset = BaseOffset +
201 Layout.getFieldOffset(idx) / Context.getCharWidth();
202 QualType FieldQTy = i->getType();
203 if (!CollectFields(Offset, FieldQTy, Fields,
204 MayAlias || TypeHasMayAlias(FieldQTy)))
205 return false;
206 }
207 return true;
208 }
209
210 /* Otherwise, treat whatever it is as a field. */
211 uint64_t Offset = BaseOffset;
212 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
213 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
Manman Renc7f2bfb2013-04-22 19:50:07 +0000214 llvm::MDNode *TBAATag = CodeGenOpts.StructPathTBAA ?
215 getTBAAScalarTagInfo(TBAAInfo) : TBAAInfo;
216 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
Dan Gohmanb22c7dc2012-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.
232 return StructMetadataCache[Ty] = NULL;
233}
Manman Renb37a73d2013-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 Renc7d77b22013-04-30 17:38:09 +0000239 if (RD->hasFlexibleArrayMember())
240 return false;
Manman Renb37a73d2013-04-04 21:53:22 +0000241 // RD can be struct, union, class, interface or enum.
Manman Renc7d77b22013-04-30 17:38:09 +0000242 // For now, we only handle struct and class.
243 if (RD->isStruct() || RD->isClass())
Manman Renb37a73d2013-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 Ren50be9042013-04-27 00:26:07 +0000261 SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
Manman Renb37a73d2013-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 Renca835182013-04-11 23:02:56 +0000269 else
Manman Renb37a73d2013-04-04 21:53:22 +0000270 FieldNode = getTBAAInfo(FieldQTy);
Manman Renb37a73d2013-04-04 21:53:22 +0000271 if (!FieldNode)
272 return StructTypeMetadataCache[Ty] = NULL;
273 Fields.push_back(std::make_pair(
Manman Ren50be9042013-04-27 00:26:07 +0000274 FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
Manman Renb37a73d2013-04-04 21:53:22 +0000275 }
276
277 // TODO: This is using the RTTI name. Is there a better way to get
278 // a unique string for a type?
279 SmallString<256> OutName;
280 llvm::raw_svector_ostream Out(OutName);
281 MContext.mangleCXXRTTIName(QualType(Ty, 0), Out);
282 Out.flush();
283 // Create the struct type node with a vector of pairs (offset, type).
284 return StructTypeMetadataCache[Ty] =
285 MDHelper.createTBAAStructTypeNode(OutName, Fields);
286 }
287
288 return StructMetadataCache[Ty] = NULL;
289}
290
291llvm::MDNode *
292CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
293 uint64_t Offset) {
294 if (!CodeGenOpts.StructPathTBAA)
295 return AccessNode;
296
297 const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
298 TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
299 if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
300 return N;
301
302 llvm::MDNode *BNode = 0;
303 if (isTBAAPathStruct(BaseQTy))
304 BNode = getTBAAStructTypeInfo(BaseQTy);
305 if (!BNode)
Manman Renca835182013-04-11 23:02:56 +0000306 return StructTagMetadataCache[PathTag] =
307 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
Manman Renb37a73d2013-04-04 21:53:22 +0000308
309 return StructTagMetadataCache[PathTag] =
310 MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
311}
Manman Renca835182013-04-11 23:02:56 +0000312
313llvm::MDNode *
314CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
315 if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
316 return N;
317
318 return ScalarTagMetadataCache[AccessNode] =
319 MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
320}