Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1 | //===--- 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 | // |
| 10 | // This is the code that manages TBAA information. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenTBAA.h" |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 15 | #include "Mangle.h" |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "llvm/LLVMContext.h" |
| 18 | #include "llvm/Metadata.h" |
| 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
| 22 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 23 | const LangOptions &Features, MangleContext &MContext) |
| 24 | : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext), |
| 25 | Root(0), Char(0) { |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | CodeGenTBAA::~CodeGenTBAA() { |
| 29 | } |
| 30 | |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 31 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr, |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 32 | llvm::MDNode *Parent) { |
| 33 | llvm::Value *Ops[] = { |
| 34 | llvm::MDString::get(VMContext, NameStr), |
| 35 | Parent |
| 36 | }; |
| 37 | |
| 38 | return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops)); |
| 39 | } |
| 40 | |
| 41 | llvm::MDNode * |
| 42 | CodeGenTBAA::getTBAAInfo(QualType QTy) { |
| 43 | Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 44 | |
| 45 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 46 | return N; |
| 47 | |
| 48 | if (!Root) { |
| 49 | Root = getTBAAInfoForNamedType("Experimental TBAA", 0); |
| 50 | Char = getTBAAInfoForNamedType("omnipotent char", Root); |
| 51 | } |
| 52 | |
| 53 | // For now, just emit a very minimal tree. |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 54 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 55 | switch (BTy->getKind()) { |
Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 56 | // Character types are special and can alias anything. |
| 57 | // In C++, this technically only includes "char" and "unsigned char", |
| 58 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 4a3b1b3 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 59 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 60 | // the benefit. |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 61 | case BuiltinType::Char_U: |
| 62 | case BuiltinType::Char_S: |
| 63 | case BuiltinType::UChar: |
| 64 | case BuiltinType::SChar: |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 65 | return Char; |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 66 | |
| 67 | // Unsigned types can alias their corresponding signed types. |
| 68 | case BuiltinType::UShort: |
| 69 | return getTBAAInfo(Context.ShortTy); |
| 70 | case BuiltinType::UInt: |
| 71 | return getTBAAInfo(Context.IntTy); |
| 72 | case BuiltinType::ULong: |
| 73 | return getTBAAInfo(Context.LongTy); |
| 74 | case BuiltinType::ULongLong: |
| 75 | return getTBAAInfo(Context.LongLongTy); |
| 76 | case BuiltinType::UInt128: |
| 77 | return getTBAAInfo(Context.Int128Ty); |
| 78 | |
Dan Gohman | 2d0a3c7 | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 79 | // Treat all other builtin types as distinct types. This includes |
| 80 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 81 | // "underlying types". |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 82 | default: |
| 83 | return MetadataCache[Ty] = |
| 84 | getTBAAInfoForNamedType(BTy->getName(Features), Char); |
| 85 | } |
| 86 | } |
| 87 | |
Dan Gohman | c44fd64 | 2010-10-15 20:26:20 +0000 | [diff] [blame^] | 88 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 89 | // pointers distinct. |
Dan Gohman | d65c196 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 90 | if (Ty->isPointerType()) |
Dan Gohman | c44fd64 | 2010-10-15 20:26:20 +0000 | [diff] [blame^] | 91 | return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", Char); |
Dan Gohman | d65c196 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 92 | |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 93 | // Enum types are distinct types. In C++ they have "underlying types", |
| 94 | // however they aren't related for TBAA. |
| 95 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
| 96 | // In C mode, two anonymous enums are compatible iff their members |
| 97 | // are the same -- see C99 6.2.7p1. For now, be conservative. We could |
| 98 | // theoretically implement this by combining information about all the |
| 99 | // members into a single identifying MDNode. |
| 100 | if (!Features.CPlusPlus && |
| 101 | ETy->getDecl()->getTypedefForAnonDecl()) |
| 102 | return MetadataCache[Ty] = Char; |
| 103 | |
| 104 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 105 | // on their mangled names, if they're external. |
| 106 | // TODO: Is there a way to get a program-wide unique name for a |
| 107 | // decl with local linkage or no linkage? |
| 108 | if (Features.CPlusPlus && |
| 109 | ETy->getDecl()->getLinkage() != ExternalLinkage) |
| 110 | return MetadataCache[Ty] = Char; |
| 111 | |
| 112 | // TODO: This is using the RTTI name. Is there a better way to get |
| 113 | // a unique string for a type? |
| 114 | llvm::SmallString<256> OutName; |
| 115 | MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName); |
| 116 | return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, Char); |
| 117 | } |
| 118 | |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 119 | // For now, handle any other kind of type conservatively. |
| 120 | return MetadataCache[Ty] = Char; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 121 | } |