Dan Gohman | 3d5aff5 | 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 | // |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 10 | // This is the code that manages TBAA information and defines the TBAA policy |
| 11 | // for the optimizer to use. Relevant standards text includes: |
Dan Gohman | 780658a | 2010-10-15 20:54:41 +0000 | [diff] [blame] | 12 | // |
| 13 | // C99 6.5p7 |
| 14 | // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions) |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "CodeGenTBAA.h" |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 19 | #include "Mangle.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "llvm/LLVMContext.h" |
| 22 | #include "llvm/Metadata.h" |
| 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
| 26 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 27 | const LangOptions &Features, MangleContext &MContext) |
| 28 | : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext), |
| 29 | Root(0), Char(0) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | CodeGenTBAA::~CodeGenTBAA() { |
| 33 | } |
| 34 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 35 | /// getTBAAInfoForNamedType - Create a TBAA tree node with the given string |
| 36 | /// as its identifier, and the given Parent node as its tree parent. |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 37 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr, |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 38 | llvm::MDNode *Parent) { |
| 39 | llvm::Value *Ops[] = { |
| 40 | llvm::MDString::get(VMContext, NameStr), |
| 41 | Parent |
| 42 | }; |
| 43 | |
| 44 | return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops)); |
| 45 | } |
| 46 | |
| 47 | llvm::MDNode * |
| 48 | CodeGenTBAA::getTBAAInfo(QualType QTy) { |
| 49 | Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 50 | |
| 51 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 52 | return N; |
| 53 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 54 | // If this is our first node, create the initial tree. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 55 | if (!Root) { |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 56 | // Define the root of the tree. This identifies the tree, so that |
| 57 | // if our LLVM IR is linked with LLVM IR from a different front-end |
| 58 | // (or a different version of this front-end), their TBAA trees will |
| 59 | // remain distinct, and the optimizer will treat them conservatively. |
Dan Gohman | 31da545 | 2010-10-21 18:50:04 +0000 | [diff] [blame] | 60 | Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0); |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 61 | |
| 62 | // Define the root of the tree for user-accessible memory. C and C++ |
| 63 | // give special powers to char and certain similar types. However, |
| 64 | // these special powers only cover user-accessible memory, and doesn't |
| 65 | // include things like vtables. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 66 | Char = getTBAAInfoForNamedType("omnipotent char", Root); |
| 67 | } |
| 68 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 69 | // Handle builtin types. |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 70 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 71 | switch (BTy->getKind()) { |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 72 | // Character types are special and can alias anything. |
| 73 | // In C++, this technically only includes "char" and "unsigned char", |
| 74 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 0a53198 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 75 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 76 | // the benefit. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 77 | case BuiltinType::Char_U: |
| 78 | case BuiltinType::Char_S: |
| 79 | case BuiltinType::UChar: |
| 80 | case BuiltinType::SChar: |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 81 | return Char; |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 82 | |
| 83 | // Unsigned types can alias their corresponding signed types. |
| 84 | case BuiltinType::UShort: |
| 85 | return getTBAAInfo(Context.ShortTy); |
| 86 | case BuiltinType::UInt: |
| 87 | return getTBAAInfo(Context.IntTy); |
| 88 | case BuiltinType::ULong: |
| 89 | return getTBAAInfo(Context.LongTy); |
| 90 | case BuiltinType::ULongLong: |
| 91 | return getTBAAInfo(Context.LongLongTy); |
| 92 | case BuiltinType::UInt128: |
| 93 | return getTBAAInfo(Context.Int128Ty); |
| 94 | |
Dan Gohman | 2f8c21d | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 95 | // Treat all other builtin types as distinct types. This includes |
| 96 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 97 | // "underlying types". |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 98 | default: |
| 99 | return MetadataCache[Ty] = |
| 100 | getTBAAInfoForNamedType(BTy->getName(Features), Char); |
| 101 | } |
| 102 | } |
| 103 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 104 | // Handle pointers. |
Dan Gohman | dc49111 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 105 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 106 | // pointers distinct. |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 107 | if (Ty->isPointerType()) |
Dan Gohman | dc49111 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 108 | return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", Char); |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 109 | |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 110 | // Enum types are distinct types. In C++ they have "underlying types", |
| 111 | // however they aren't related for TBAA. |
| 112 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
| 113 | // In C mode, two anonymous enums are compatible iff their members |
| 114 | // are the same -- see C99 6.2.7p1. For now, be conservative. We could |
| 115 | // theoretically implement this by combining information about all the |
| 116 | // members into a single identifying MDNode. |
| 117 | if (!Features.CPlusPlus && |
| 118 | ETy->getDecl()->getTypedefForAnonDecl()) |
| 119 | return MetadataCache[Ty] = Char; |
| 120 | |
| 121 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 122 | // on their mangled names, if they're external. |
| 123 | // TODO: Is there a way to get a program-wide unique name for a |
| 124 | // decl with local linkage or no linkage? |
| 125 | if (Features.CPlusPlus && |
| 126 | ETy->getDecl()->getLinkage() != ExternalLinkage) |
| 127 | return MetadataCache[Ty] = Char; |
| 128 | |
| 129 | // TODO: This is using the RTTI name. Is there a better way to get |
| 130 | // a unique string for a type? |
| 131 | llvm::SmallString<256> OutName; |
| 132 | MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName); |
| 133 | return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, Char); |
| 134 | } |
| 135 | |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 136 | // For now, handle any other kind of type conservatively. |
| 137 | return MetadataCache[Ty] = Char; |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 138 | } |