blob: 0a5a0197648690314cd567ebbf0b095cca1d1e6e [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"
Benjamin Kramer2fa67ef2012-12-01 15:09:41 +000024#include "llvm/Constants.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000025#include "llvm/LLVMContext.h"
26#include "llvm/Metadata.h"
Dan Gohman455553b2010-10-25 23:51:23 +000027#include "llvm/Type.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000028using namespace clang;
29using namespace CodeGen;
30
31CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000032 const CodeGenOptions &CGO,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000033 const LangOptions &Features, MangleContext &MContext)
Benjamin Kramerfacde172012-06-06 17:32:50 +000034 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
Duncan Sands2d7cb062012-04-15 18:04:54 +000035 MDHelper(VMContext), Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000036}
37
38CodeGenTBAA::~CodeGenTBAA() {
39}
40
Dan Gohman224d7592010-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 Sands60c77072012-04-16 16:29:47 +000047 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman224d7592010-10-25 21:48:30 +000048
49 return Root;
50}
51
52llvm::MDNode *CodeGenTBAA::getChar() {
53 // Define the root of the tree for user-accessible memory. C and C++
54 // give special powers to char and certain similar types. However,
55 // these special powers only cover user-accessible memory, and doesn't
56 // include things like vtables.
57 if (!Char)
Duncan Sands60c77072012-04-16 16:29:47 +000058 Char = MDHelper.createTBAANode("omnipotent char", getRoot());
Dan Gohman224d7592010-10-25 21:48:30 +000059
60 return Char;
61}
62
Dan Gohman2ea7e732010-12-13 23:51:08 +000063static bool TypeHasMayAlias(QualType QTy) {
64 // Tagged types have declarations, and therefore may have attributes.
65 if (const TagType *TTy = dyn_cast<TagType>(QTy))
66 return TTy->getDecl()->hasAttr<MayAliasAttr>();
67
68 // Typedef types have declarations, and therefore may have attributes.
69 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
70 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
71 return true;
72 // Also, their underlying types may have relevant attributes.
73 return TypeHasMayAlias(TTy->desugar());
74 }
75
76 return false;
77}
78
Dan Gohman3d5aff52010-10-14 23:06:10 +000079llvm::MDNode *
80CodeGenTBAA::getTBAAInfo(QualType QTy) {
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000081 // At -O0 TBAA is not emitted for regular types.
82 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
83 return NULL;
84
Dan Gohman2ea7e732010-12-13 23:51:08 +000085 // If the type has the may_alias attribute (even on a typedef), it is
86 // effectively in the general char alias class.
87 if (TypeHasMayAlias(QTy))
88 return getChar();
89
John McCallf4c73712011-01-19 06:33:43 +000090 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +000091
92 if (llvm::MDNode *N = MetadataCache[Ty])
93 return N;
94
Dan Gohman565cc442010-10-21 18:49:12 +000095 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +000096 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000097 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +000098 // Character types are special and can alias anything.
99 // In C++, this technically only includes "char" and "unsigned char",
100 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +0000101 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000102 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000103 case BuiltinType::Char_U:
104 case BuiltinType::Char_S:
105 case BuiltinType::UChar:
106 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000107 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000108
109 // Unsigned types can alias their corresponding signed types.
110 case BuiltinType::UShort:
111 return getTBAAInfo(Context.ShortTy);
112 case BuiltinType::UInt:
113 return getTBAAInfo(Context.IntTy);
114 case BuiltinType::ULong:
115 return getTBAAInfo(Context.LongTy);
116 case BuiltinType::ULongLong:
117 return getTBAAInfo(Context.LongLongTy);
118 case BuiltinType::UInt128:
119 return getTBAAInfo(Context.Int128Ty);
120
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000121 // Treat all other builtin types as distinct types. This includes
122 // treating wchar_t, char16_t, and char32_t as distinct from their
123 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000124 default:
125 return MetadataCache[Ty] =
Duncan Sands60c77072012-04-16 16:29:47 +0000126 MDHelper.createTBAANode(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000127 }
128 }
129
Dan Gohman565cc442010-10-21 18:49:12 +0000130 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000131 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
132 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000133 if (Ty->isPointerType())
Duncan Sands60c77072012-04-16 16:29:47 +0000134 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer",
Dan Gohman224d7592010-10-25 21:48:30 +0000135 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000136
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000137 // Enum types are distinct types. In C++ they have "underlying types",
138 // however they aren't related for TBAA.
139 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000140 // In C mode, two anonymous enums are compatible iff their members
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000141 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
142 // theoretically implement this by combining information about all the
143 // members into a single identifying MDNode.
144 if (!Features.CPlusPlus &&
Richard Smith162e1c12011-04-15 14:24:37 +0000145 ETy->getDecl()->getTypedefNameForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000146 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000147
148 // 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?
152 if (Features.CPlusPlus &&
153 ETy->getDecl()->getLinkage() != ExternalLinkage)
Dan Gohman224d7592010-10-25 21:48:30 +0000154 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000155
156 // TODO: This is using the RTTI name. Is there a better way to get
157 // a unique string for a type?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000158 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000159 llvm::raw_svector_ostream Out(OutName);
160 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
161 Out.flush();
Duncan Sands60c77072012-04-16 16:29:47 +0000162 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000163 }
164
Dan Gohman9af2f832010-10-14 23:39:00 +0000165 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000166 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000167}
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000168
169llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Duncan Sands60c77072012-04-16 16:29:47 +0000170 return MDHelper.createTBAANode("vtable pointer", getRoot());
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000171}
Dan Gohmanb22c7dc2012-09-28 21:58:29 +0000172
173bool
174CodeGenTBAA::CollectFields(uint64_t BaseOffset,
175 QualType QTy,
176 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
177 Fields,
178 bool MayAlias) {
179 /* Things not handled yet include: C++ base classes, bitfields, */
180
181 if (const RecordType *TTy = QTy->getAs<RecordType>()) {
182 const RecordDecl *RD = TTy->getDecl()->getDefinition();
183 if (RD->hasFlexibleArrayMember())
184 return false;
185
186 // TODO: Handle C++ base classes.
187 if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
188 if (Decl->bases_begin() != Decl->bases_end())
189 return false;
190
191 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
192
193 unsigned idx = 0;
194 for (RecordDecl::field_iterator i = RD->field_begin(),
195 e = RD->field_end(); i != e; ++i, ++idx) {
196 uint64_t Offset = BaseOffset +
197 Layout.getFieldOffset(idx) / Context.getCharWidth();
198 QualType FieldQTy = i->getType();
199 if (!CollectFields(Offset, FieldQTy, Fields,
200 MayAlias || TypeHasMayAlias(FieldQTy)))
201 return false;
202 }
203 return true;
204 }
205
206 /* Otherwise, treat whatever it is as a field. */
207 uint64_t Offset = BaseOffset;
208 uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
209 llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
210 Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAAInfo));
211 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}