blob: d9004a02ae2551122b3f373ea1af06823ac4f738 [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"
Dan Gohmanb22c7dc2012-09-28 21:58:29 +000020#include "clang/AST/RecordLayout.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000021#include "clang/AST/Mangle.h"
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000022#include "clang/Frontend/CodeGenOptions.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000023#include "llvm/LLVMContext.h"
24#include "llvm/Metadata.h"
Dan Gohman455553b2010-10-25 23:51:23 +000025#include "llvm/Constants.h"
26#include "llvm/Type.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000027using namespace clang;
28using namespace CodeGen;
29
30CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000031 const CodeGenOptions &CGO,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000032 const LangOptions &Features, MangleContext &MContext)
Benjamin Kramerfacde172012-06-06 17:32:50 +000033 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
Duncan Sands2d7cb062012-04-15 18:04:54 +000034 MDHelper(VMContext), Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000035}
36
37CodeGenTBAA::~CodeGenTBAA() {
38}
39
Dan Gohman224d7592010-10-25 21:48:30 +000040llvm::MDNode *CodeGenTBAA::getRoot() {
41 // Define the root of the tree. This identifies the tree, so that
42 // if our LLVM IR is linked with LLVM IR from a different front-end
43 // (or a different version of this front-end), their TBAA trees will
44 // remain distinct, and the optimizer will treat them conservatively.
45 if (!Root)
Duncan Sands60c77072012-04-16 16:29:47 +000046 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman224d7592010-10-25 21:48:30 +000047
48 return Root;
49}
50
51llvm::MDNode *CodeGenTBAA::getChar() {
52 // Define the root of the tree for user-accessible memory. C and C++
53 // give special powers to char and certain similar types. However,
54 // these special powers only cover user-accessible memory, and doesn't
55 // include things like vtables.
56 if (!Char)
Duncan Sands60c77072012-04-16 16:29:47 +000057 Char = MDHelper.createTBAANode("omnipotent char", getRoot());
Dan Gohman224d7592010-10-25 21:48:30 +000058
59 return Char;
60}
61
Dan Gohman2ea7e732010-12-13 23:51:08 +000062static bool TypeHasMayAlias(QualType QTy) {
63 // Tagged types have declarations, and therefore may have attributes.
64 if (const TagType *TTy = dyn_cast<TagType>(QTy))
65 return TTy->getDecl()->hasAttr<MayAliasAttr>();
66
67 // Typedef types have declarations, and therefore may have attributes.
68 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
69 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
70 return true;
71 // Also, their underlying types may have relevant attributes.
72 return TypeHasMayAlias(TTy->desugar());
73 }
74
75 return false;
76}
77
Dan Gohman3d5aff52010-10-14 23:06:10 +000078llvm::MDNode *
79CodeGenTBAA::getTBAAInfo(QualType QTy) {
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000080 // At -O0 TBAA is not emitted for regular types.
81 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
82 return NULL;
83
Dan Gohman2ea7e732010-12-13 23:51:08 +000084 // If the type has the may_alias attribute (even on a typedef), it is
85 // effectively in the general char alias class.
86 if (TypeHasMayAlias(QTy))
87 return getChar();
88
John McCallf4c73712011-01-19 06:33:43 +000089 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +000090
91 if (llvm::MDNode *N = MetadataCache[Ty])
92 return N;
93
Dan Gohman565cc442010-10-21 18:49:12 +000094 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +000095 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000096 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +000097 // Character types are special and can alias anything.
98 // In C++, this technically only includes "char" and "unsigned char",
99 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +0000100 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000101 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000102 case BuiltinType::Char_U:
103 case BuiltinType::Char_S:
104 case BuiltinType::UChar:
105 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000106 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000107
108 // Unsigned types can alias their corresponding signed types.
109 case BuiltinType::UShort:
110 return getTBAAInfo(Context.ShortTy);
111 case BuiltinType::UInt:
112 return getTBAAInfo(Context.IntTy);
113 case BuiltinType::ULong:
114 return getTBAAInfo(Context.LongTy);
115 case BuiltinType::ULongLong:
116 return getTBAAInfo(Context.LongLongTy);
117 case BuiltinType::UInt128:
118 return getTBAAInfo(Context.Int128Ty);
119
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000120 // Treat all other builtin types as distinct types. This includes
121 // treating wchar_t, char16_t, and char32_t as distinct from their
122 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000123 default:
124 return MetadataCache[Ty] =
Duncan Sands60c77072012-04-16 16:29:47 +0000125 MDHelper.createTBAANode(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000126 }
127 }
128
Dan Gohman565cc442010-10-21 18:49:12 +0000129 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000130 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
131 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000132 if (Ty->isPointerType())
Duncan Sands60c77072012-04-16 16:29:47 +0000133 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer",
Dan Gohman224d7592010-10-25 21:48:30 +0000134 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000135
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000136 // Enum types are distinct types. In C++ they have "underlying types",
137 // however they aren't related for TBAA.
138 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000139 // In C mode, two anonymous enums are compatible iff their members
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000140 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
141 // theoretically implement this by combining information about all the
142 // members into a single identifying MDNode.
143 if (!Features.CPlusPlus &&
Richard Smith162e1c12011-04-15 14:24:37 +0000144 ETy->getDecl()->getTypedefNameForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000145 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000146
147 // 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?
151 if (Features.CPlusPlus &&
152 ETy->getDecl()->getLinkage() != ExternalLinkage)
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();
Duncan Sands60c77072012-04-16 16:29:47 +0000161 return MetadataCache[Ty] = MDHelper.createTBAANode(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() {
Duncan Sands60c77072012-04-16 16:29:47 +0000169 return MDHelper.createTBAANode("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);
209 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAAInfo));
210 return true;
211}
212
213llvm::MDNode *
214CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
215 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
216
217 if (llvm::MDNode *N = StructMetadataCache[Ty])
218 return N;
219
220 SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
221 if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
222 return MDHelper.createTBAAStructNode(Fields);
223
224 // For now, handle any other kind of type conservatively.
225 return StructMetadataCache[Ty] = NULL;
226}