blob: d4d71a755745cdb718199515e253ca331efb0ec3 [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"
Dan Gohman455553b2010-10-25 23:51:23 +000023#include "llvm/Constants.h"
24#include "llvm/Type.h"
Dan Gohman3d5aff52010-10-14 23:06:10 +000025using namespace clang;
26using namespace CodeGen;
27
28CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000029 const LangOptions &Features, MangleContext &MContext)
30 : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
31 Root(0), Char(0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000032}
33
34CodeGenTBAA::~CodeGenTBAA() {
35}
36
Dan Gohman224d7592010-10-25 21:48:30 +000037llvm::MDNode *CodeGenTBAA::getRoot() {
38 // Define the root of the tree. This identifies the tree, so that
39 // if our LLVM IR is linked with LLVM IR from a different front-end
40 // (or a different version of this front-end), their TBAA trees will
41 // remain distinct, and the optimizer will treat them conservatively.
42 if (!Root)
43 Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0);
44
45 return Root;
46}
47
48llvm::MDNode *CodeGenTBAA::getChar() {
49 // Define the root of the tree for user-accessible memory. C and C++
50 // give special powers to char and certain similar types. However,
51 // these special powers only cover user-accessible memory, and doesn't
52 // include things like vtables.
53 if (!Char)
54 Char = getTBAAInfoForNamedType("omnipotent char", getRoot());
55
56 return Char;
57}
58
Dan Gohman565cc442010-10-21 18:49:12 +000059/// getTBAAInfoForNamedType - Create a TBAA tree node with the given string
60/// as its identifier, and the given Parent node as its tree parent.
Dan Gohman0b5c4fc2010-10-15 20:23:12 +000061llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr,
Dan Gohman455553b2010-10-25 23:51:23 +000062 llvm::MDNode *Parent,
63 bool Readonly) {
64 // Currently there is only one flag defined - the readonly flag.
65 llvm::Value *Flags = 0;
66 if (Readonly)
67 Flags = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), true);
68
69 // Set up the mdnode operand list.
Dan Gohman3d5aff52010-10-14 23:06:10 +000070 llvm::Value *Ops[] = {
71 llvm::MDString::get(VMContext, NameStr),
Dan Gohman455553b2010-10-25 23:51:23 +000072 Parent,
73 Flags
Dan Gohman3d5aff52010-10-14 23:06:10 +000074 };
75
Dan Gohman455553b2010-10-25 23:51:23 +000076 // Create the mdnode.
77 return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops) - !Flags);
Dan Gohman3d5aff52010-10-14 23:06:10 +000078}
79
80llvm::MDNode *
81CodeGenTBAA::getTBAAInfo(QualType QTy) {
82 Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
83
84 if (llvm::MDNode *N = MetadataCache[Ty])
85 return N;
86
Dan Gohman565cc442010-10-21 18:49:12 +000087 // Handle builtin types.
Dan Gohman9af2f832010-10-14 23:39:00 +000088 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
Dan Gohman3d5aff52010-10-14 23:06:10 +000089 switch (BTy->getKind()) {
Dan Gohman85623482010-10-15 17:52:03 +000090 // Character types are special and can alias anything.
91 // In C++, this technically only includes "char" and "unsigned char",
92 // and not "signed char". In C, it includes all three. For now,
Dan Gohman0a531982010-10-15 20:24:10 +000093 // the risk of exploiting this detail in C++ seems likely to outweigh
Dan Gohman85623482010-10-15 17:52:03 +000094 // the benefit.
Dan Gohman3d5aff52010-10-14 23:06:10 +000095 case BuiltinType::Char_U:
96 case BuiltinType::Char_S:
97 case BuiltinType::UChar:
98 case BuiltinType::SChar:
Dan Gohman224d7592010-10-25 21:48:30 +000099 return getChar();
Dan Gohman9af2f832010-10-14 23:39:00 +0000100
101 // Unsigned types can alias their corresponding signed types.
102 case BuiltinType::UShort:
103 return getTBAAInfo(Context.ShortTy);
104 case BuiltinType::UInt:
105 return getTBAAInfo(Context.IntTy);
106 case BuiltinType::ULong:
107 return getTBAAInfo(Context.LongTy);
108 case BuiltinType::ULongLong:
109 return getTBAAInfo(Context.LongLongTy);
110 case BuiltinType::UInt128:
111 return getTBAAInfo(Context.Int128Ty);
112
Dan Gohman2f8c21d2010-10-15 20:24:53 +0000113 // Treat all other builtin types as distinct types. This includes
114 // treating wchar_t, char16_t, and char32_t as distinct from their
115 // "underlying types".
Dan Gohman3d5aff52010-10-14 23:06:10 +0000116 default:
117 return MetadataCache[Ty] =
Dan Gohman224d7592010-10-25 21:48:30 +0000118 getTBAAInfoForNamedType(BTy->getName(Features), getChar());
Dan Gohman3d5aff52010-10-14 23:06:10 +0000119 }
120 }
121
Dan Gohman565cc442010-10-21 18:49:12 +0000122 // Handle pointers.
Dan Gohmandc491112010-10-15 20:26:20 +0000123 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
124 // pointers distinct.
Dan Gohmanc1028f42010-10-15 00:01:39 +0000125 if (Ty->isPointerType())
Dan Gohman224d7592010-10-25 21:48:30 +0000126 return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer",
127 getChar());
Dan Gohmanc1028f42010-10-15 00:01:39 +0000128
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000129 // Enum types are distinct types. In C++ they have "underlying types",
130 // however they aren't related for TBAA.
131 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
132 // In C mode, two anonymous enums are compatible iff their members
133 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
134 // theoretically implement this by combining information about all the
135 // members into a single identifying MDNode.
136 if (!Features.CPlusPlus &&
137 ETy->getDecl()->getTypedefForAnonDecl())
Dan Gohman224d7592010-10-25 21:48:30 +0000138 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000139
140 // In C++ mode, types have linkage, so we can rely on the ODR and
141 // on their mangled names, if they're external.
142 // TODO: Is there a way to get a program-wide unique name for a
143 // decl with local linkage or no linkage?
144 if (Features.CPlusPlus &&
145 ETy->getDecl()->getLinkage() != ExternalLinkage)
Dan Gohman224d7592010-10-25 21:48:30 +0000146 return MetadataCache[Ty] = getChar();
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000147
148 // TODO: This is using the RTTI name. Is there a better way to get
149 // a unique string for a type?
150 llvm::SmallString<256> OutName;
151 MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName);
Dan Gohman224d7592010-10-25 21:48:30 +0000152 return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar());
Dan Gohman0b5c4fc2010-10-15 20:23:12 +0000153 }
154
Dan Gohman9af2f832010-10-14 23:39:00 +0000155 // For now, handle any other kind of type conservatively.
Dan Gohman224d7592010-10-25 21:48:30 +0000156 return MetadataCache[Ty] = getChar();
Dan Gohman3d5aff52010-10-14 23:06:10 +0000157}