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" |
Dan Gohman | 455553b | 2010-10-25 23:51:23 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/Type.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | using namespace CodeGen; |
| 27 | |
| 28 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 29 | const LangOptions &Features, MangleContext &MContext) |
| 30 | : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext), |
| 31 | Root(0), Char(0) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | CodeGenTBAA::~CodeGenTBAA() { |
| 35 | } |
| 36 | |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 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. |
| 42 | if (!Root) |
| 43 | Root = getTBAAInfoForNamedType("Simple C/C++ TBAA", 0); |
| 44 | |
| 45 | return Root; |
| 46 | } |
| 47 | |
| 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. |
| 53 | if (!Char) |
| 54 | Char = getTBAAInfoForNamedType("omnipotent char", getRoot()); |
| 55 | |
| 56 | return Char; |
| 57 | } |
| 58 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 59 | /// getTBAAInfoForNamedType - Create a TBAA tree node with the given string |
| 60 | /// as its identifier, and the given Parent node as its tree parent. |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 61 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr, |
Dan Gohman | 455553b | 2010-10-25 23:51:23 +0000 | [diff] [blame] | 62 | 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 Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 70 | llvm::Value *Ops[] = { |
| 71 | llvm::MDString::get(VMContext, NameStr), |
Dan Gohman | 455553b | 2010-10-25 23:51:23 +0000 | [diff] [blame] | 72 | Parent, |
| 73 | Flags |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Dan Gohman | 455553b | 2010-10-25 23:51:23 +0000 | [diff] [blame] | 76 | // Create the mdnode. |
| 77 | return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops) - !Flags); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 80 | static bool TypeHasMayAlias(QualType QTy) { |
| 81 | // Tagged types have declarations, and therefore may have attributes. |
| 82 | if (const TagType *TTy = dyn_cast<TagType>(QTy)) |
| 83 | return TTy->getDecl()->hasAttr<MayAliasAttr>(); |
| 84 | |
| 85 | // Typedef types have declarations, and therefore may have attributes. |
| 86 | if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { |
| 87 | if (TTy->getDecl()->hasAttr<MayAliasAttr>()) |
| 88 | return true; |
| 89 | // Also, their underlying types may have relevant attributes. |
| 90 | return TypeHasMayAlias(TTy->desugar()); |
| 91 | } |
| 92 | |
| 93 | return false; |
| 94 | } |
| 95 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 96 | llvm::MDNode * |
| 97 | CodeGenTBAA::getTBAAInfo(QualType QTy) { |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 98 | // If the type has the may_alias attribute (even on a typedef), it is |
| 99 | // effectively in the general char alias class. |
| 100 | if (TypeHasMayAlias(QTy)) |
| 101 | return getChar(); |
| 102 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 103 | Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 104 | |
| 105 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 106 | return N; |
| 107 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 108 | // Handle builtin types. |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 109 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 110 | switch (BTy->getKind()) { |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 111 | // Character types are special and can alias anything. |
| 112 | // In C++, this technically only includes "char" and "unsigned char", |
| 113 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 0a53198 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 114 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 115 | // the benefit. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 116 | case BuiltinType::Char_U: |
| 117 | case BuiltinType::Char_S: |
| 118 | case BuiltinType::UChar: |
| 119 | case BuiltinType::SChar: |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 120 | return getChar(); |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 121 | |
| 122 | // Unsigned types can alias their corresponding signed types. |
| 123 | case BuiltinType::UShort: |
| 124 | return getTBAAInfo(Context.ShortTy); |
| 125 | case BuiltinType::UInt: |
| 126 | return getTBAAInfo(Context.IntTy); |
| 127 | case BuiltinType::ULong: |
| 128 | return getTBAAInfo(Context.LongTy); |
| 129 | case BuiltinType::ULongLong: |
| 130 | return getTBAAInfo(Context.LongLongTy); |
| 131 | case BuiltinType::UInt128: |
| 132 | return getTBAAInfo(Context.Int128Ty); |
| 133 | |
Dan Gohman | 2f8c21d | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 134 | // Treat all other builtin types as distinct types. This includes |
| 135 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 136 | // "underlying types". |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 137 | default: |
| 138 | return MetadataCache[Ty] = |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 139 | getTBAAInfoForNamedType(BTy->getName(Features), getChar()); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 143 | // Handle pointers. |
Dan Gohman | dc49111 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 144 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 145 | // pointers distinct. |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 146 | if (Ty->isPointerType()) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 147 | return MetadataCache[Ty] = getTBAAInfoForNamedType("any pointer", |
| 148 | getChar()); |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 149 | |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 150 | // Enum types are distinct types. In C++ they have "underlying types", |
| 151 | // however they aren't related for TBAA. |
| 152 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
| 153 | // In C mode, two anonymous enums are compatible iff their members |
| 154 | // are the same -- see C99 6.2.7p1. For now, be conservative. We could |
| 155 | // theoretically implement this by combining information about all the |
| 156 | // members into a single identifying MDNode. |
| 157 | if (!Features.CPlusPlus && |
| 158 | ETy->getDecl()->getTypedefForAnonDecl()) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 159 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 160 | |
| 161 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 162 | // on their mangled names, if they're external. |
| 163 | // TODO: Is there a way to get a program-wide unique name for a |
| 164 | // decl with local linkage or no linkage? |
| 165 | if (Features.CPlusPlus && |
| 166 | ETy->getDecl()->getLinkage() != ExternalLinkage) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 167 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 168 | |
| 169 | // TODO: This is using the RTTI name. Is there a better way to get |
| 170 | // a unique string for a type? |
| 171 | llvm::SmallString<256> OutName; |
| 172 | MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName); |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 173 | return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, getChar()); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 176 | // For now, handle any other kind of type conservatively. |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 177 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 178 | } |