blob: bab60afbb7f2f0a34ae9d5060fad4024f691649d [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"
Peter Collingbourne14110472011-01-13 18:57:25 +000020#include "clang/AST/Mangle.h"
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000021#include "clang/Frontend/CodeGenOptions.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000022#include "llvm/LLVMContext.h"
23#include "llvm/Metadata.h"
Dan Gohman455553b2010-10-25 23:51:23 +000024#include "llvm/Constants.h"
25#include "llvm/Type.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000026using namespace clang;
27using namespace CodeGen;
28
29CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000030 const CodeGenOptions &CGO,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000031 const LangOptions &Features, MangleContext &MContext)
Benjamin Kramerfacde172012-06-06 17:32:50 +000032 : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
Duncan Sands2d7cb062012-04-15 18:04:54 +000033 MDHelper(VMContext), Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000034}
35
36CodeGenTBAA::~CodeGenTBAA() {
37}
38
Dan Gohman224d7592010-10-25 21:48:30 +000039llvm::MDNode *CodeGenTBAA::getRoot() {
40 // Define the root of the tree. This identifies the tree, so that
41 // if our LLVM IR is linked with LLVM IR from a different front-end
42 // (or a different version of this front-end), their TBAA trees will
43 // remain distinct, and the optimizer will treat them conservatively.
44 if (!Root)
Duncan Sands60c77072012-04-16 16:29:47 +000045 Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
Dan Gohman224d7592010-10-25 21:48:30 +000046
47 return Root;
48}
49
50llvm::MDNode *CodeGenTBAA::getChar() {
51 // Define the root of the tree for user-accessible memory. C and C++
52 // give special powers to char and certain similar types. However,
53 // these special powers only cover user-accessible memory, and doesn't
54 // include things like vtables.
55 if (!Char)
Duncan Sands60c77072012-04-16 16:29:47 +000056 Char = MDHelper.createTBAANode("omnipotent char", getRoot());
Dan Gohman224d7592010-10-25 21:48:30 +000057
58 return Char;
59}
60
Dan Gohman2ea7e732010-12-13 23:51:08 +000061static bool TypeHasMayAlias(QualType QTy) {
62 // Tagged types have declarations, and therefore may have attributes.
63 if (const TagType *TTy = dyn_cast<TagType>(QTy))
64 return TTy->getDecl()->hasAttr<MayAliasAttr>();
65
66 // Typedef types have declarations, and therefore may have attributes.
67 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
68 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
69 return true;
70 // Also, their underlying types may have relevant attributes.
71 return TypeHasMayAlias(TTy->desugar());
72 }
73
74 return false;
75}
76
Dan Gohman3d5aff52010-10-14 23:06:10 +000077llvm::MDNode *
78CodeGenTBAA::getTBAAInfo(QualType QTy) {
Kostya Serebryanyc9fe6052012-04-24 06:57:01 +000079 // At -O0 TBAA is not emitted for regular types.
80 if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
81 return NULL;
82
Dan Gohman2ea7e732010-12-13 23:51:08 +000083 // If the type has the may_alias attribute (even on a typedef), it is
84 // effectively in the general char alias class.
85 if (TypeHasMayAlias(QTy))
86 return getChar();
87
John McCallf4c73712011-01-19 06:33:43 +000088 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +000089
90 if (llvm::MDNode *N = MetadataCache[Ty])
91 return N;
92
Dan Gohman565cc442010-10-21 18:49:12 +000093 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +000094 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000095 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +000096 // Character types are special and can alias anything.
97 // In C++, this technically only includes "char" and "unsigned char",
98 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +000099 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000100 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000101 case BuiltinType::Char_U:
102 case BuiltinType::Char_S:
103 case BuiltinType::UChar:
104 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000105 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000106
107 // Unsigned types can alias their corresponding signed types.
108 case BuiltinType::UShort:
109 return getTBAAInfo(Context.ShortTy);
110 case BuiltinType::UInt:
111 return getTBAAInfo(Context.IntTy);
112 case BuiltinType::ULong:
113 return getTBAAInfo(Context.LongTy);
114 case BuiltinType::ULongLong:
115 return getTBAAInfo(Context.LongLongTy);
116 case BuiltinType::UInt128:
117 return getTBAAInfo(Context.Int128Ty);
118
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000119 // Treat all other builtin types as distinct types. This includes
120 // treating wchar_t, char16_t, and char32_t as distinct from their
121 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000122 default:
123 return MetadataCache[Ty] =
Duncan Sands60c77072012-04-16 16:29:47 +0000124 MDHelper.createTBAANode(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000125 }
126 }
127
Dan Gohman565cc442010-10-21 18:49:12 +0000128 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000129 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
130 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000131 if (Ty->isPointerType())
Duncan Sands60c77072012-04-16 16:29:47 +0000132 return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer",
Dan Gohman224d7592010-10-25 21:48:30 +0000133 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000134
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000135 // Enum types are distinct types. In C++ they have "underlying types",
136 // however they aren't related for TBAA.
137 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
138 // In C mode, two anonymous enums are compatible iff their members
139 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
140 // theoretically implement this by combining information about all the
141 // members into a single identifying MDNode.
142 if (!Features.CPlusPlus &&
Richard Smith162e1c12011-04-15 14:24:37 +0000143 ETy->getDecl()->getTypedefNameForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000144 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000145
146 // In C++ mode, types have linkage, so we can rely on the ODR and
147 // on their mangled names, if they're external.
148 // TODO: Is there a way to get a program-wide unique name for a
149 // decl with local linkage or no linkage?
150 if (Features.CPlusPlus &&
151 ETy->getDecl()->getLinkage() != ExternalLinkage)
Dan Gohman224d7592010-10-25 21:48:30 +0000152 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000153
154 // TODO: This is using the RTTI name. Is there a better way to get
155 // a unique string for a type?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000156 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000157 llvm::raw_svector_ostream Out(OutName);
158 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
159 Out.flush();
Duncan Sands60c77072012-04-16 16:29:47 +0000160 return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000161 }
162
Dan Gohman9af2f832010-10-14 23:39:00 +0000163 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000164 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000165}
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000166
167llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
Duncan Sands60c77072012-04-16 16:29:47 +0000168 return MDHelper.createTBAANode("vtable pointer", getRoot());
Kostya Serebryany8cb4a072012-03-26 17:03:51 +0000169}