blob: d8618f0d3ad0082ba538bbc0e30f3ba26faa5311 [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"
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000019#include "Mangle.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000020#include "clang/AST/ASTContext.h"
21#include "llvm/LLVMContext.h"
22#include "llvm/Metadata.h"
23using namespace clang;
24using namespace CodeGen;
25
26CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000027 const LangOptions &Features, MangleContext &MContext)
28 : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
29 Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000030}
31
32CodeGenTBAA::~CodeGenTBAA() {
33}
34
Dan Gohman224d7592010-10-25 21:48:30 +000035llvm::MDNode *CodeGenTBAA::getRoot() {
36 // Define the root of the tree. This identifies the tree, so that
37 // if our LLVM IR is linked with LLVM IR from a different front-end
38 // (or a different version of this front-end), their TBAA trees will
39 // remain distinct, and the optimizer will treat them conservatively.
40 if (!Root)
41 Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0);
42
43 return Root;
44}
45
46llvm::MDNode *CodeGenTBAA::getChar() {
47 // Define the root of the tree for user-accessible memory. C and C++
48 // give special powers to char and certain similar types. However,
49 // these special powers only cover user-accessible memory, and doesn't
50 // include things like vtables.
51 if (!Char)
52 Char = getTBAAInfoForNamedType("omnipotent char", getRoot());
53
54 return Char;
55}
56
Dan Gohman565cc442010-10-21 18:49:12 +000057/// getTBAAInfoForNamedType - Create a TBAA tree node with the given string
58/// as its identifier, and the given Parent node as its tree parent.
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000059llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr,
Dan Gohman3d5aff52010-10-14 23:06:10 +000060 llvm::MDNode *Parent) {
61 llvm::Value *Ops[] = {
62 llvm::MDString::get(VMContext, NameStr),
63 Parent
64 };
65
66 return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops));
67}
68
69llvm::MDNode *
70CodeGenTBAA::getTBAAInfo(QualType QTy) {
71 Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
72
73 if (llvm::MDNode *N = MetadataCache[Ty])
74 return N;
75
Dan Gohman565cc442010-10-21 18:49:12 +000076 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +000077 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000078 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +000079 // Character types are special and can alias anything.
80 // In C++, this technically only includes "char" and "unsigned char",
81 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +000082 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +000083 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +000084 case BuiltinType::Char_U:
85 case BuiltinType::Char_S:
86 case BuiltinType::UChar:
87 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +000088 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +000089
90 // Unsigned types can alias their corresponding signed types.
91 case BuiltinType::UShort:
92 return getTBAAInfo(Context.ShortTy);
93 case BuiltinType::UInt:
94 return getTBAAInfo(Context.IntTy);
95 case BuiltinType::ULong:
96 return getTBAAInfo(Context.LongTy);
97 case BuiltinType::ULongLong:
98 return getTBAAInfo(Context.LongLongTy);
99 case BuiltinType::UInt128:
100 return getTBAAInfo(Context.Int128Ty);
101
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000102 // Treat all other builtin types as distinct types. This includes
103 // treating wchar_t, char16_t, and char32_t as distinct from their
104 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000105 default:
106 return MetadataCache[Ty] =
Dan Gohman224d7592010-10-25 21:48:30 +0000107 getTBAAInfoForNamedType(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000108 }
109 }
110
Dan Gohman565cc442010-10-21 18:49:12 +0000111 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000112 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
113 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000114 if (Ty->isPointerType())
Dan Gohman224d7592010-10-25 21:48:30 +0000115 return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer",
116 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000117
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000118 // Enum types are distinct types. In C++ they have "underlying types",
119 // however they aren't related for TBAA.
120 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
121 // In C mode, two anonymous enums are compatible iff their members
122 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
123 // theoretically implement this by combining information about all the
124 // members into a single identifying MDNode.
125 if (!Features.CPlusPlus &&
126 ETy->getDecl()->getTypedefForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000127 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000128
129 // In C++ mode, types have linkage, so we can rely on the ODR and
130 // on their mangled names, if they're external.
131 // TODO: Is there a way to get a program-wide unique name for a
132 // decl with local linkage or no linkage?
133 if (Features.CPlusPlus &&
134 ETy->getDecl()->getLinkage() != ExternalLinkage)
Dan Gohman224d7592010-10-25 21:48:30 +0000135 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000136
137 // TODO: This is using the RTTI name. Is there a better way to get
138 // a unique string for a type?
139 llvm::SmallString<256> OutName;
140 MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName);
Dan Gohman224d7592010-10-25 21:48:30 +0000141 return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000142 }
143
Dan Gohman9af2f832010-10-14 23:39:00 +0000144 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000145 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000146}