1 //===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that manages TBAA information and defines the TBAA policy
11 // for the optimizer to use. Relevant standards text includes:
14 // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
16 //===----------------------------------------------------------------------===//
18 #include "CodeGenTBAA.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/Mangle.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Metadata.h"
23 #include "llvm/Constants.h"
24 #include "llvm/Type.h"
25 using namespace clang;
26 using namespace CodeGen;
28 CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
29 const LangOptions &Features, MangleContext &MContext)
30 : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
34 CodeGenTBAA::~CodeGenTBAA() {
37 llvm::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.
43 Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0);
48 llvm::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.
54 Char = getTBAAInfoForNamedType("omnipotent char", getRoot());
59 /// getTBAAInfoForNamedType - Create a TBAA tree node with the given string
60 /// as its identifier, and the given Parent node as its tree parent.
61 llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr,
64 // Currently there is only one flag defined - the readonly flag.
65 llvm::Value *Flags = 0;
67 Flags = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), true);
69 // Set up the mdnode operand list.
70 llvm::Value *Ops[] = {
71 llvm::MDString::get(VMContext, NameStr),
77 unsigned Len = llvm::array_lengthof(Ops) - !Flags;
78 return llvm::MDNode::get(VMContext, llvm::ArrayRef<llvm::Value*>(Ops, Len));
81 static bool TypeHasMayAlias(QualType QTy) {
82 // Tagged types have declarations, and therefore may have attributes.
83 if (const TagType *TTy = dyn_cast<TagType>(QTy))
84 return TTy->getDecl()->hasAttr<MayAliasAttr>();
86 // Typedef types have declarations, and therefore may have attributes.
87 if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
88 if (TTy->getDecl()->hasAttr<MayAliasAttr>())
90 // Also, their underlying types may have relevant attributes.
91 return TypeHasMayAlias(TTy->desugar());
98 CodeGenTBAA::getTBAAInfo(QualType QTy) {
99 // If the type has the may_alias attribute (even on a typedef), it is
100 // effectively in the general char alias class.
101 if (TypeHasMayAlias(QTy))
104 const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
106 if (llvm::MDNode *N = MetadataCache[Ty])
109 // Handle builtin types.
110 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
111 switch (BTy->getKind()) {
112 // Character types are special and can alias anything.
113 // In C++, this technically only includes "char" and "unsigned char",
114 // and not "signed char". In C, it includes all three. For now,
115 // the risk of exploiting this detail in C++ seems likely to outweigh
117 case BuiltinType::Char_U:
118 case BuiltinType::Char_S:
119 case BuiltinType::UChar:
120 case BuiltinType::SChar:
123 // Unsigned types can alias their corresponding signed types.
124 case BuiltinType::UShort:
125 return getTBAAInfo(Context.ShortTy);
126 case BuiltinType::UInt:
127 return getTBAAInfo(Context.IntTy);
128 case BuiltinType::ULong:
129 return getTBAAInfo(Context.LongTy);
130 case BuiltinType::ULongLong:
131 return getTBAAInfo(Context.LongLongTy);
132 case BuiltinType::UInt128:
133 return getTBAAInfo(Context.Int128Ty);
135 // Treat all other builtin types as distinct types. This includes
136 // treating wchar_t, char16_t, and char32_t as distinct from their
137 // "underlying types".
139 return MetadataCache[Ty] =
140 getTBAAInfoForNamedType(BTy->getName(Features), getChar());
145 // TODO: Implement C++'s type "similarity" and consider dis-"similar"
146 // pointers distinct.
147 if (Ty->isPointerType())
148 return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer",
151 // Enum types are distinct types. In C++ they have "underlying types",
152 // however they aren't related for TBAA.
153 if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
154 // In C mode, two anonymous enums are compatible iff their members
155 // are the same -- see C99 6.2.7p1. For now, be conservative. We could
156 // theoretically implement this by combining information about all the
157 // members into a single identifying MDNode.
158 if (!Features.CPlusPlus &&
159 ETy->getDecl()->getTypedefNameForAnonDecl())
160 return MetadataCache[Ty] = getChar();
162 // In C++ mode, types have linkage, so we can rely on the ODR and
163 // on their mangled names, if they're external.
164 // TODO: Is there a way to get a program-wide unique name for a
165 // decl with local linkage or no linkage?
166 if (Features.CPlusPlus &&
167 ETy->getDecl()->getLinkage() != ExternalLinkage)
168 return MetadataCache[Ty] = getChar();
170 // TODO: This is using the RTTI name. Is there a better way to get
171 // a unique string for a type?
172 llvm::SmallString<256> OutName;
173 llvm::raw_svector_ostream Out(OutName);
174 MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
176 return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar());
179 // For now, handle any other kind of type conservatively.
180 return MetadataCache[Ty] = getChar();