blob: 401ad10638fd5e4adc71f9404e31cdeeb4c9d27b [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"
Dan Gohman3d5aff52010-10-14 23:06:10 +000021#include "llvm/LLVMContext.h"
22#include "llvm/Metadata.h"
Dan Gohman455553b2010-10-25 23:51:23 +000023#include "llvm/Constants.h"
24#include "llvm/Type.h"
Benjamin Kramer00bd44d2012-02-04 12:31:12 +000025#include "llvm/ADT/STLExtras.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000026using namespace clang;
27using namespace CodeGen;
28
29CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000030 const LangOptions &Features, MangleContext &MContext)
31 : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
32 Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000033}
34
35CodeGenTBAA::~CodeGenTBAA() {
36}
37
Dan Gohman224d7592010-10-25 21:48:30 +000038llvm::MDNode *CodeGenTBAA::getRoot() {
39 // Define the root of the tree. This identifies the tree, so that
40 // if our LLVM IR is linked with LLVM IR from a different front-end
41 // (or a different version of this front-end), their TBAA trees will
42 // remain distinct, and the optimizer will treat them conservatively.
43 if (!Root)
44 Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0);
45
46 return Root;
47}
48
49llvm::MDNode *CodeGenTBAA::getChar() {
50 // Define the root of the tree for user-accessible memory. C and C++
51 // give special powers to char and certain similar types. However,
52 // these special powers only cover user-accessible memory, and doesn't
53 // include things like vtables.
54 if (!Char)
55 Char = getTBAAInfoForNamedType("omnipotent char", getRoot());
56
57 return Char;
58}
59
Dan Gohman565cc442010-10-21 18:49:12 +000060/// getTBAAInfoForNamedType - Create a TBAA tree node with the given string
61/// as its identifier, and the given Parent node as its tree parent.
Chris Lattner5f9e2722011-07-23 10:55:15 +000062llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(StringRef NameStr,
Dan Gohman455553b2010-10-25 23:51:23 +000063 llvm::MDNode *Parent,
64 bool Readonly) {
65 // Currently there is only one flag defined - the readonly flag.
66 llvm::Value *Flags = 0;
67 if (Readonly)
68 Flags = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), true);
69
70 // Set up the mdnode operand list.
Dan Gohman3d5aff52010-10-14 23:06:10 +000071 llvm::Value *Ops[] = {
72 llvm::MDString::get(VMContext, NameStr),
Dan Gohman455553b2010-10-25 23:51:23 +000073 Parent,
74 Flags
Dan Gohman3d5aff52010-10-14 23:06:10 +000075 };
76
Dan Gohman455553b2010-10-25 23:51:23 +000077 // Create the mdnode.
Jay Foad6f141652011-04-21 19:59:12 +000078 unsigned Len = llvm::array_lengthof(Ops) - !Flags;
Frits van Bommele9c02652011-07-18 12:00:32 +000079 return llvm::MDNode::get(VMContext, llvm::makeArrayRef(Ops, Len));
Dan Gohman3d5aff52010-10-14 23:06:10 +000080}
81
Dan Gohman2ea7e732010-12-13 23:51:08 +000082static bool TypeHasMayAlias(QualType QTy) {
83 // Tagged types have declarations, and therefore may have attributes.
84 if (const TagType *TTy = dyn_cast<TagType>(QTy))
85 return TTy->getDecl()->hasAttr<MayAliasAttr>();
86
87 // Typedef types have declarations, and therefore may have attributes.
88 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
89 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
90 return true;
91 // Also, their underlying types may have relevant attributes.
92 return TypeHasMayAlias(TTy->desugar());
93 }
94
95 return false;
96}
97
Dan Gohman3d5aff52010-10-14 23:06:10 +000098llvm::MDNode *
99CodeGenTBAA::getTBAAInfo(QualType QTy) {
Dan Gohman2ea7e732010-12-13 23:51:08 +0000100 // If the type has the may_alias attribute (even on a typedef), it is
101 // effectively in the general char alias class.
102 if (TypeHasMayAlias(QTy))
103 return getChar();
104
John McCallf4c73712011-01-19 06:33:43 +0000105 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000106
107 if (llvm::MDNode *N = MetadataCache[Ty])
108 return N;
109
Dan Gohman565cc442010-10-21 18:49:12 +0000110 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +0000111 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +0000112 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +0000113 // Character types are special and can alias anything.
114 // In C++, this technically only includes "char" and "unsigned char",
115 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +0000116 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +0000117 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +0000118 case BuiltinType::Char_U:
119 case BuiltinType::Char_S:
120 case BuiltinType::UChar:
121 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +0000122 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000123
124 // Unsigned types can alias their corresponding signed types.
125 case BuiltinType::UShort:
126 return getTBAAInfo(Context.ShortTy);
127 case BuiltinType::UInt:
128 return getTBAAInfo(Context.IntTy);
129 case BuiltinType::ULong:
130 return getTBAAInfo(Context.LongTy);
131 case BuiltinType::ULongLong:
132 return getTBAAInfo(Context.LongLongTy);
133 case BuiltinType::UInt128:
134 return getTBAAInfo(Context.Int128Ty);
135
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000136 // Treat all other builtin types as distinct types. This includes
137 // treating wchar_t, char16_t, and char32_t as distinct from their
138 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000139 default:
140 return MetadataCache[Ty] =
Dan Gohman224d7592010-10-25 21:48:30 +0000141 getTBAAInfoForNamedType(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000142 }
143 }
144
Dan Gohman565cc442010-10-21 18:49:12 +0000145 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000146 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
147 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000148 if (Ty->isPointerType())
Dan Gohman224d7592010-10-25 21:48:30 +0000149 return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer",
150 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000151
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000152 // Enum types are distinct types. In C++ they have "underlying types",
153 // however they aren't related for TBAA.
154 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
155 // In C mode, two anonymous enums are compatible iff their members
156 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
157 // theoretically implement this by combining information about all the
158 // members into a single identifying MDNode.
159 if (!Features.CPlusPlus &&
Richard Smith162e1c12011-04-15 14:24:37 +0000160 ETy->getDecl()->getTypedefNameForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000161 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000162
163 // In C++ mode, types have linkage, so we can rely on the ODR and
164 // on their mangled names, if they're external.
165 // TODO: Is there a way to get a program-wide unique name for a
166 // decl with local linkage or no linkage?
167 if (Features.CPlusPlus &&
168 ETy->getDecl()->getLinkage() != ExternalLinkage)
Dan Gohman224d7592010-10-25 21:48:30 +0000169 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000170
171 // TODO: This is using the RTTI name. Is there a better way to get
172 // a unique string for a type?
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000173 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000174 llvm::raw_svector_ostream Out(OutName);
175 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
176 Out.flush();
Dan Gohman224d7592010-10-25 21:48:30 +0000177 return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000178 }
179
Dan Gohman9af2f832010-10-14 23:39:00 +0000180 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000181 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000182}