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" |
| 19 | #include "clang/AST/ASTContext.h" |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 20 | #include "clang/AST/Mangle.h" |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CodeGenOptions.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 22 | #include "llvm/LLVMContext.h" |
| 23 | #include "llvm/Metadata.h" |
Dan Gohman | 455553b | 2010-10-25 23:51:23 +0000 | [diff] [blame] | 24 | #include "llvm/Constants.h" |
| 25 | #include "llvm/Type.h" |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
| 29 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 30 | const CodeGenOptions &CGO, |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 31 | const LangOptions &Features, MangleContext &MContext) |
Benjamin Kramer | facde17 | 2012-06-06 17:32:50 +0000 | [diff] [blame^] | 32 | : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), |
Duncan Sands | 2d7cb06 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 33 | MDHelper(VMContext), Root(0), Char(0) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | CodeGenTBAA::~CodeGenTBAA() { |
| 37 | } |
| 38 | |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 39 | llvm::MDNode *CodeGenTBAA::getRoot() { |
| 40 | // Define the root of the tree. This identifies the tree, so that |
| 41 | // if our LLVM IR is linked with LLVM IR from a different front-end |
| 42 | // (or a different version of this front-end), their TBAA trees will |
| 43 | // remain distinct, and the optimizer will treat them conservatively. |
| 44 | if (!Root) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 45 | Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 46 | |
| 47 | return Root; |
| 48 | } |
| 49 | |
| 50 | llvm::MDNode *CodeGenTBAA::getChar() { |
| 51 | // Define the root of the tree for user-accessible memory. C and C++ |
| 52 | // give special powers to char and certain similar types. However, |
| 53 | // these special powers only cover user-accessible memory, and doesn't |
| 54 | // include things like vtables. |
| 55 | if (!Char) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 56 | Char = MDHelper.createTBAANode("omnipotent char", getRoot()); |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 57 | |
| 58 | return Char; |
| 59 | } |
| 60 | |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 61 | static bool TypeHasMayAlias(QualType QTy) { |
| 62 | // Tagged types have declarations, and therefore may have attributes. |
| 63 | if (const TagType *TTy = dyn_cast<TagType>(QTy)) |
| 64 | return TTy->getDecl()->hasAttr<MayAliasAttr>(); |
| 65 | |
| 66 | // Typedef types have declarations, and therefore may have attributes. |
| 67 | if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { |
| 68 | if (TTy->getDecl()->hasAttr<MayAliasAttr>()) |
| 69 | return true; |
| 70 | // Also, their underlying types may have relevant attributes. |
| 71 | return TypeHasMayAlias(TTy->desugar()); |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 77 | llvm::MDNode * |
| 78 | CodeGenTBAA::getTBAAInfo(QualType QTy) { |
Kostya Serebryany | c9fe605 | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 79 | // At -O0 TBAA is not emitted for regular types. |
| 80 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
| 81 | return NULL; |
| 82 | |
Dan Gohman | 2ea7e73 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 83 | // If the type has the may_alias attribute (even on a typedef), it is |
| 84 | // effectively in the general char alias class. |
| 85 | if (TypeHasMayAlias(QTy)) |
| 86 | return getChar(); |
| 87 | |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 88 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 89 | |
| 90 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 91 | return N; |
| 92 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 93 | // Handle builtin types. |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 94 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 95 | switch (BTy->getKind()) { |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 96 | // Character types are special and can alias anything. |
| 97 | // In C++, this technically only includes "char" and "unsigned char", |
| 98 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 0a53198 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 99 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | 8562348 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 100 | // the benefit. |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 101 | case BuiltinType::Char_U: |
| 102 | case BuiltinType::Char_S: |
| 103 | case BuiltinType::UChar: |
| 104 | case BuiltinType::SChar: |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 105 | return getChar(); |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 106 | |
| 107 | // Unsigned types can alias their corresponding signed types. |
| 108 | case BuiltinType::UShort: |
| 109 | return getTBAAInfo(Context.ShortTy); |
| 110 | case BuiltinType::UInt: |
| 111 | return getTBAAInfo(Context.IntTy); |
| 112 | case BuiltinType::ULong: |
| 113 | return getTBAAInfo(Context.LongTy); |
| 114 | case BuiltinType::ULongLong: |
| 115 | return getTBAAInfo(Context.LongLongTy); |
| 116 | case BuiltinType::UInt128: |
| 117 | return getTBAAInfo(Context.Int128Ty); |
| 118 | |
Dan Gohman | 2f8c21d | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 119 | // Treat all other builtin types as distinct types. This includes |
| 120 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 121 | // "underlying types". |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 122 | default: |
| 123 | return MetadataCache[Ty] = |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 124 | MDHelper.createTBAANode(BTy->getName(Features), getChar()); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Dan Gohman | 565cc44 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 128 | // Handle pointers. |
Dan Gohman | dc49111 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 129 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 130 | // pointers distinct. |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 131 | if (Ty->isPointerType()) |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 132 | return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer", |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 133 | getChar()); |
Dan Gohman | c1028f4 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 134 | |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 135 | // Enum types are distinct types. In C++ they have "underlying types", |
| 136 | // however they aren't related for TBAA. |
| 137 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
| 138 | // In C mode, two anonymous enums are compatible iff their members |
| 139 | // are the same -- see C99 6.2.7p1. For now, be conservative. We could |
| 140 | // theoretically implement this by combining information about all the |
| 141 | // members into a single identifying MDNode. |
| 142 | if (!Features.CPlusPlus && |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 143 | ETy->getDecl()->getTypedefNameForAnonDecl()) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 144 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 145 | |
| 146 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 147 | // on their mangled names, if they're external. |
| 148 | // TODO: Is there a way to get a program-wide unique name for a |
| 149 | // decl with local linkage or no linkage? |
| 150 | if (Features.CPlusPlus && |
| 151 | ETy->getDecl()->getLinkage() != ExternalLinkage) |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 152 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 153 | |
| 154 | // TODO: This is using the RTTI name. Is there a better way to get |
| 155 | // a unique string for a type? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 156 | SmallString<256> OutName; |
Rafael Espindola | f0be979 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 157 | llvm::raw_svector_ostream Out(OutName); |
| 158 | MContext.mangleCXXRTTIName(QualType(ETy, 0), Out); |
| 159 | Out.flush(); |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 160 | return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar()); |
Dan Gohman | 0b5c4fc | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Dan Gohman | 9af2f83 | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 163 | // For now, handle any other kind of type conservatively. |
Dan Gohman | 224d759 | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 164 | return MetadataCache[Ty] = getChar(); |
Dan Gohman | 3d5aff5 | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 165 | } |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 166 | |
| 167 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { |
Duncan Sands | 60c7707 | 2012-04-16 16:29:47 +0000 | [diff] [blame] | 168 | return MDHelper.createTBAANode("vtable pointer", getRoot()); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 169 | } |