| 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 | // | 
| Dan Gohman | 5419ce6 | 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 | f47df3e | 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 | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 15 | // | 
|  | 16 | //===----------------------------------------------------------------------===// | 
|  | 17 |  | 
|  | 18 | #include "CodeGenTBAA.h" | 
|  | 19 | #include "clang/AST/ASTContext.h" | 
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/Attr.h" | 
| Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" | 
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" | 
| Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/CodeGenOptions.h" | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallSet.h" | 
| Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" | 
|  | 26 | #include "llvm/IR/LLVMContext.h" | 
|  | 27 | #include "llvm/IR/Metadata.h" | 
|  | 28 | #include "llvm/IR/Type.h" | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 29 | using namespace clang; | 
|  | 30 | using namespace CodeGen; | 
|  | 31 |  | 
|  | 32 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext, | 
| Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 33 | const CodeGenOptions &CGO, | 
| Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 34 | const LangOptions &Features, MangleContext &MContext) | 
| Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 35 | : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext), | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 36 | MDHelper(VMContext), Root(nullptr), Char(nullptr) { | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 39 | CodeGenTBAA::~CodeGenTBAA() { | 
|  | 40 | } | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 41 |  | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 42 | llvm::MDNode *CodeGenTBAA::getRoot() { | 
|  | 43 | // Define the root of the tree. This identifies the tree, so that | 
|  | 44 | // if our LLVM IR is linked with LLVM IR from a different front-end | 
|  | 45 | // (or a different version of this front-end), their TBAA trees will | 
|  | 46 | // remain distinct, and the optimizer will treat them conservatively. | 
| Manman Ren | 37dec10 | 2016-02-11 19:19:18 +0000 | [diff] [blame] | 47 | if (!Root) { | 
|  | 48 | if (Features.CPlusPlus) | 
|  | 49 | Root = MDHelper.createTBAARoot("Simple C++ TBAA"); | 
|  | 50 | else | 
|  | 51 | Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); | 
|  | 52 | } | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 53 |  | 
|  | 54 | return Root; | 
|  | 55 | } | 
|  | 56 |  | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 57 | // For both scalar TBAA and struct-path aware TBAA, the scalar type has the | 
|  | 58 | // same format: name, parent node, and offset. | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 59 | llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name, | 
|  | 60 | llvm::MDNode *Parent) { | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 61 | return MDHelper.createTBAAScalarTypeNode(Name, Parent); | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 64 | llvm::MDNode *CodeGenTBAA::getChar() { | 
|  | 65 | // Define the root of the tree for user-accessible memory. C and C++ | 
|  | 66 | // give special powers to char and certain similar types. However, | 
|  | 67 | // these special powers only cover user-accessible memory, and doesn't | 
|  | 68 | // include things like vtables. | 
|  | 69 | if (!Char) | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 70 | Char = createTBAAScalarType("omnipotent char", getRoot()); | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | return Char; | 
|  | 73 | } | 
|  | 74 |  | 
| Dan Gohman | c289769 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 75 | static bool TypeHasMayAlias(QualType QTy) { | 
|  | 76 | // Tagged types have declarations, and therefore may have attributes. | 
|  | 77 | if (const TagType *TTy = dyn_cast<TagType>(QTy)) | 
|  | 78 | return TTy->getDecl()->hasAttr<MayAliasAttr>(); | 
|  | 79 |  | 
|  | 80 | // Typedef types have declarations, and therefore may have attributes. | 
|  | 81 | if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) { | 
|  | 82 | if (TTy->getDecl()->hasAttr<MayAliasAttr>()) | 
|  | 83 | return true; | 
|  | 84 | // Also, their underlying types may have relevant attributes. | 
|  | 85 | return TypeHasMayAlias(TTy->desugar()); | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | return false; | 
|  | 89 | } | 
|  | 90 |  | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 91 | llvm::MDNode * | 
|  | 92 | CodeGenTBAA::getTBAAInfo(QualType QTy) { | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 93 | // At -O0 or relaxed aliasing, TBAA is not emitted for regular types. | 
| Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 94 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 95 | return nullptr; | 
| Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 96 |  | 
| Dan Gohman | c289769 | 2010-12-13 23:51:08 +0000 | [diff] [blame] | 97 | // If the type has the may_alias attribute (even on a typedef), it is | 
|  | 98 | // effectively in the general char alias class. | 
|  | 99 | if (TypeHasMayAlias(QTy)) | 
|  | 100 | return getChar(); | 
|  | 101 |  | 
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 102 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 103 |  | 
|  | 104 | if (llvm::MDNode *N = MetadataCache[Ty]) | 
|  | 105 | return N; | 
|  | 106 |  | 
| Dan Gohman | 5419ce6 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 107 | // Handle builtin types. | 
| Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 108 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 109 | switch (BTy->getKind()) { | 
| Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 110 | // Character types are special and can alias anything. | 
|  | 111 | // In C++, this technically only includes "char" and "unsigned char", | 
|  | 112 | // and not "signed char". In C, it includes all three. For now, | 
| Dan Gohman | 4a3b1b3 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 113 | // the risk of exploiting this detail in C++ seems likely to outweigh | 
| Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 114 | // the benefit. | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 115 | case BuiltinType::Char_U: | 
|  | 116 | case BuiltinType::Char_S: | 
|  | 117 | case BuiltinType::UChar: | 
|  | 118 | case BuiltinType::SChar: | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 119 | return getChar(); | 
| Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 120 |  | 
|  | 121 | // Unsigned types can alias their corresponding signed types. | 
|  | 122 | case BuiltinType::UShort: | 
|  | 123 | return getTBAAInfo(Context.ShortTy); | 
|  | 124 | case BuiltinType::UInt: | 
|  | 125 | return getTBAAInfo(Context.IntTy); | 
|  | 126 | case BuiltinType::ULong: | 
|  | 127 | return getTBAAInfo(Context.LongTy); | 
|  | 128 | case BuiltinType::ULongLong: | 
|  | 129 | return getTBAAInfo(Context.LongLongTy); | 
|  | 130 | case BuiltinType::UInt128: | 
|  | 131 | return getTBAAInfo(Context.Int128Ty); | 
|  | 132 |  | 
| Dan Gohman | 2d0a3c7 | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 133 | // Treat all other builtin types as distinct types. This includes | 
|  | 134 | // treating wchar_t, char16_t, and char32_t as distinct from their | 
|  | 135 | // "underlying types". | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 136 | default: | 
|  | 137 | return MetadataCache[Ty] = | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 138 | createTBAAScalarType(BTy->getName(Features), getChar()); | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
| Dan Gohman | 5419ce6 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 142 | // Handle pointers. | 
| Dan Gohman | c44fd64 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 143 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" | 
|  | 144 | // pointers distinct. | 
| Dan Gohman | d65c196 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 145 | if (Ty->isPointerType()) | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 146 | return MetadataCache[Ty] = createTBAAScalarType("any pointer", | 
|  | 147 | getChar()); | 
| Dan Gohman | d65c196 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 148 |  | 
| Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 149 | // Enum types are distinct types. In C++ they have "underlying types", | 
|  | 150 | // however they aren't related for TBAA. | 
|  | 151 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { | 
| Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 152 | // In C++ mode, types have linkage, so we can rely on the ODR and | 
|  | 153 | // on their mangled names, if they're external. | 
|  | 154 | // TODO: Is there a way to get a program-wide unique name for a | 
|  | 155 | // decl with local linkage or no linkage? | 
| Eli Friedman | eecc09a | 2013-07-05 20:27:40 +0000 | [diff] [blame] | 156 | if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible()) | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 157 | return MetadataCache[Ty] = getChar(); | 
| Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 158 |  | 
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 159 | SmallString<256> OutName; | 
| Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 160 | llvm::raw_svector_ostream Out(OutName); | 
| Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 161 | MContext.mangleTypeName(QualType(ETy, 0), Out); | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 162 | return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar()); | 
| Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 165 | // For now, handle any other kind of type conservatively. | 
| Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 166 | return MetadataCache[Ty] = getChar(); | 
| Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 167 | } | 
| Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 168 |  | 
|  | 169 | llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() { | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 170 | return createTBAAScalarType("vtable pointer", getRoot()); | 
| Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 171 | } | 
| Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 172 |  | 
|  | 173 | bool | 
|  | 174 | CodeGenTBAA::CollectFields(uint64_t BaseOffset, | 
|  | 175 | QualType QTy, | 
|  | 176 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & | 
|  | 177 | Fields, | 
|  | 178 | bool MayAlias) { | 
|  | 179 | /* Things not handled yet include: C++ base classes, bitfields, */ | 
|  | 180 |  | 
|  | 181 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { | 
|  | 182 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); | 
|  | 183 | if (RD->hasFlexibleArrayMember()) | 
|  | 184 | return false; | 
|  | 185 |  | 
|  | 186 | // TODO: Handle C++ base classes. | 
|  | 187 | if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) | 
|  | 188 | if (Decl->bases_begin() != Decl->bases_end()) | 
|  | 189 | return false; | 
|  | 190 |  | 
|  | 191 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); | 
|  | 192 |  | 
|  | 193 | unsigned idx = 0; | 
|  | 194 | for (RecordDecl::field_iterator i = RD->field_begin(), | 
|  | 195 | e = RD->field_end(); i != e; ++i, ++idx) { | 
|  | 196 | uint64_t Offset = BaseOffset + | 
|  | 197 | Layout.getFieldOffset(idx) / Context.getCharWidth(); | 
|  | 198 | QualType FieldQTy = i->getType(); | 
|  | 199 | if (!CollectFields(Offset, FieldQTy, Fields, | 
|  | 200 | MayAlias || TypeHasMayAlias(FieldQTy))) | 
|  | 201 | return false; | 
|  | 202 | } | 
|  | 203 | return true; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | /* Otherwise, treat whatever it is as a field. */ | 
|  | 207 | uint64_t Offset = BaseOffset; | 
|  | 208 | uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); | 
|  | 209 | llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy); | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 210 | llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo); | 
| Manman Ren | 09a3912 | 2013-04-22 19:50:07 +0000 | [diff] [blame] | 211 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); | 
| Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 212 | return true; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | llvm::MDNode * | 
|  | 216 | CodeGenTBAA::getTBAAStructInfo(QualType QTy) { | 
|  | 217 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); | 
|  | 218 |  | 
|  | 219 | if (llvm::MDNode *N = StructMetadataCache[Ty]) | 
|  | 220 | return N; | 
|  | 221 |  | 
|  | 222 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; | 
|  | 223 | if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) | 
|  | 224 | return MDHelper.createTBAAStructNode(Fields); | 
|  | 225 |  | 
|  | 226 | // For now, handle any other kind of type conservatively. | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 227 | return StructMetadataCache[Ty] = nullptr; | 
| Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 228 | } | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 229 |  | 
|  | 230 | /// Check if the given type can be handled by path-aware TBAA. | 
|  | 231 | static bool isTBAAPathStruct(QualType QTy) { | 
|  | 232 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { | 
|  | 233 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); | 
| Manman Ren | e36d341 | 2013-04-30 17:38:09 +0000 | [diff] [blame] | 234 | if (RD->hasFlexibleArrayMember()) | 
|  | 235 | return false; | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 236 | // RD can be struct, union, class, interface or enum. | 
| Manman Ren | e36d341 | 2013-04-30 17:38:09 +0000 | [diff] [blame] | 237 | // For now, we only handle struct and class. | 
|  | 238 | if (RD->isStruct() || RD->isClass()) | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 239 | return true; | 
|  | 240 | } | 
|  | 241 | return false; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | llvm::MDNode * | 
|  | 245 | CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) { | 
|  | 246 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); | 
|  | 247 | assert(isTBAAPathStruct(QTy)); | 
|  | 248 |  | 
|  | 249 | if (llvm::MDNode *N = StructTypeMetadataCache[Ty]) | 
|  | 250 | return N; | 
|  | 251 |  | 
|  | 252 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { | 
|  | 253 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); | 
|  | 254 |  | 
|  | 255 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); | 
| Manman Ren | a6b7303 | 2013-04-27 00:26:07 +0000 | [diff] [blame] | 256 | SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields; | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 257 | unsigned idx = 0; | 
|  | 258 | for (RecordDecl::field_iterator i = RD->field_begin(), | 
|  | 259 | e = RD->field_end(); i != e; ++i, ++idx) { | 
|  | 260 | QualType FieldQTy = i->getType(); | 
|  | 261 | llvm::MDNode *FieldNode; | 
|  | 262 | if (isTBAAPathStruct(FieldQTy)) | 
|  | 263 | FieldNode = getTBAAStructTypeInfo(FieldQTy); | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 264 | else | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 265 | FieldNode = getTBAAInfo(FieldQTy); | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 266 | if (!FieldNode) | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 267 | return StructTypeMetadataCache[Ty] = nullptr; | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 268 | Fields.push_back(std::make_pair( | 
| Manman Ren | a6b7303 | 2013-04-27 00:26:07 +0000 | [diff] [blame] | 269 | FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth())); | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 272 | SmallString<256> OutName; | 
| Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 273 | if (Features.CPlusPlus) { | 
| Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 274 | // Don't use the mangler for C code. | 
| Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 275 | llvm::raw_svector_ostream Out(OutName); | 
| Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 276 | MContext.mangleTypeName(QualType(Ty, 0), Out); | 
| Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 277 | } else { | 
|  | 278 | OutName = RD->getName(); | 
|  | 279 | } | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 280 | // Create the struct type node with a vector of pairs (offset, type). | 
|  | 281 | return StructTypeMetadataCache[Ty] = | 
|  | 282 | MDHelper.createTBAAStructTypeNode(OutName, Fields); | 
|  | 283 | } | 
|  | 284 |  | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 285 | return StructMetadataCache[Ty] = nullptr; | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 288 | /// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA. | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 289 | llvm::MDNode * | 
|  | 290 | CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode, | 
|  | 291 | uint64_t Offset) { | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 292 | if (!AccessNode) | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 293 | return nullptr; | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 294 |  | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 295 | if (!CodeGenOpts.StructPathTBAA) | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 296 | return getTBAAScalarTagInfo(AccessNode); | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 297 |  | 
|  | 298 | const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr(); | 
|  | 299 | TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset); | 
|  | 300 | if (llvm::MDNode *N = StructTagMetadataCache[PathTag]) | 
|  | 301 | return N; | 
|  | 302 |  | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 303 | llvm::MDNode *BNode = nullptr; | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 304 | if (isTBAAPathStruct(BaseQTy)) | 
|  | 305 | BNode  = getTBAAStructTypeInfo(BaseQTy); | 
|  | 306 | if (!BNode) | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 307 | return StructTagMetadataCache[PathTag] = | 
|  | 308 | MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); | 
| Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 309 |  | 
|  | 310 | return StructTagMetadataCache[PathTag] = | 
|  | 311 | MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset); | 
|  | 312 | } | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 313 |  | 
|  | 314 | llvm::MDNode * | 
|  | 315 | CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) { | 
| Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 316 | if (!AccessNode) | 
| Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 317 | return nullptr; | 
| Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 318 | if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode]) | 
|  | 319 | return N; | 
|  | 320 |  | 
|  | 321 | return ScalarTagMetadataCache[AccessNode] = | 
|  | 322 | MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0); | 
|  | 323 | } |