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" |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Module.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Type.h" |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 33 | CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 34 | const CodeGenOptions &CGO, |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 35 | const LangOptions &Features, MangleContext &MContext) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 36 | : Context(Ctx), Module(M), CodeGenOpts(CGO), |
| 37 | Features(Features), MContext(MContext), MDHelper(M.getContext()), |
| 38 | Root(nullptr), Char(nullptr) |
| 39 | {} |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 40 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 41 | CodeGenTBAA::~CodeGenTBAA() { |
| 42 | } |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 43 | |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 44 | llvm::MDNode *CodeGenTBAA::getRoot() { |
| 45 | // Define the root of the tree. This identifies the tree, so that |
| 46 | // if our LLVM IR is linked with LLVM IR from a different front-end |
| 47 | // (or a different version of this front-end), their TBAA trees will |
| 48 | // remain distinct, and the optimizer will treat them conservatively. |
Manman Ren | 37dec10 | 2016-02-11 19:19:18 +0000 | [diff] [blame] | 49 | if (!Root) { |
| 50 | if (Features.CPlusPlus) |
| 51 | Root = MDHelper.createTBAARoot("Simple C++ TBAA"); |
| 52 | else |
| 53 | Root = MDHelper.createTBAARoot("Simple C/C++ TBAA"); |
| 54 | } |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 55 | |
| 56 | return Root; |
| 57 | } |
| 58 | |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 59 | llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name, |
| 60 | llvm::MDNode *Parent, |
| 61 | uint64_t Size) { |
Ivan A. Kosarev | d50b847 | 2017-12-22 09:54:23 +0000 | [diff] [blame] | 62 | if (CodeGenOpts.NewStructPathTBAA) { |
| 63 | llvm::Metadata *Id = MDHelper.createString(Name); |
| 64 | return MDHelper.createTBAATypeNode(Parent, Size, Id); |
| 65 | } |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 66 | return MDHelper.createTBAAScalarTypeNode(Name, Parent); |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 69 | llvm::MDNode *CodeGenTBAA::getChar() { |
| 70 | // Define the root of the tree for user-accessible memory. C and C++ |
| 71 | // give special powers to char and certain similar types. However, |
| 72 | // these special powers only cover user-accessible memory, and doesn't |
| 73 | // include things like vtables. |
| 74 | if (!Char) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 75 | Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1); |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 76 | |
| 77 | return Char; |
| 78 | } |
| 79 | |
Dan Gohman | c289769 | 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 | |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 96 | /// Check if the given type is a valid base type to be used in access tags. |
| 97 | static bool isValidBaseType(QualType QTy) { |
| 98 | if (QTy->isReferenceType()) |
| 99 | return false; |
| 100 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
| 101 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
| 102 | // Incomplete types are not valid base access types. |
| 103 | if (!RD) |
| 104 | return false; |
| 105 | if (RD->hasFlexibleArrayMember()) |
| 106 | return false; |
Hal Finkel | a5986b9 | 2017-12-03 03:10:13 +0000 | [diff] [blame] | 107 | // RD can be struct, union, class, interface or enum. |
| 108 | // For now, we only handle struct and class. |
| 109 | if (RD->isStruct() || RD->isClass()) |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 115 | llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) { |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 116 | uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity(); |
| 117 | |
Dan Gohman | 5419ce6 | 2010-10-21 18:49:12 +0000 | [diff] [blame] | 118 | // Handle builtin types. |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 119 | if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) { |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 120 | switch (BTy->getKind()) { |
Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 121 | // Character types are special and can alias anything. |
| 122 | // In C++, this technically only includes "char" and "unsigned char", |
| 123 | // and not "signed char". In C, it includes all three. For now, |
Dan Gohman | 4a3b1b3 | 2010-10-15 20:24:10 +0000 | [diff] [blame] | 124 | // the risk of exploiting this detail in C++ seems likely to outweigh |
Dan Gohman | f5c5e07 | 2010-10-15 17:52:03 +0000 | [diff] [blame] | 125 | // the benefit. |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 126 | case BuiltinType::Char_U: |
| 127 | case BuiltinType::Char_S: |
| 128 | case BuiltinType::UChar: |
| 129 | case BuiltinType::SChar: |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 130 | return getChar(); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 131 | |
| 132 | // Unsigned types can alias their corresponding signed types. |
| 133 | case BuiltinType::UShort: |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 134 | return getTypeInfo(Context.ShortTy); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 135 | case BuiltinType::UInt: |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 136 | return getTypeInfo(Context.IntTy); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 137 | case BuiltinType::ULong: |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 138 | return getTypeInfo(Context.LongTy); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 139 | case BuiltinType::ULongLong: |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 140 | return getTypeInfo(Context.LongLongTy); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 141 | case BuiltinType::UInt128: |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 142 | return getTypeInfo(Context.Int128Ty); |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 143 | |
Dan Gohman | 2d0a3c7 | 2010-10-15 20:24:53 +0000 | [diff] [blame] | 144 | // Treat all other builtin types as distinct types. This includes |
| 145 | // treating wchar_t, char16_t, and char32_t as distinct from their |
| 146 | // "underlying types". |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 147 | default: |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 148 | return createScalarTypeNode(BTy->getName(Features), getChar(), Size); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
David Majnemer | 8f94a23 | 2017-07-25 23:33:58 +0000 | [diff] [blame] | 152 | // C++1z [basic.lval]p10: "If a program attempts to access the stored value of |
| 153 | // an object through a glvalue of other than one of the following types the |
| 154 | // behavior is undefined: [...] a char, unsigned char, or std::byte type." |
| 155 | if (Ty->isStdByteType()) |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 156 | return getChar(); |
David Majnemer | 8f94a23 | 2017-07-25 23:33:58 +0000 | [diff] [blame] | 157 | |
Ivan A. Kosarev | b75a50b | 2017-09-26 14:22:48 +0000 | [diff] [blame] | 158 | // Handle pointers and references. |
Dan Gohman | c44fd64 | 2010-10-15 20:26:20 +0000 | [diff] [blame] | 159 | // TODO: Implement C++'s type "similarity" and consider dis-"similar" |
| 160 | // pointers distinct. |
Ivan A. Kosarev | b75a50b | 2017-09-26 14:22:48 +0000 | [diff] [blame] | 161 | if (Ty->isPointerType() || Ty->isReferenceType()) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 162 | return createScalarTypeNode("any pointer", getChar(), Size); |
Dan Gohman | d65c196 | 2010-10-15 00:01:39 +0000 | [diff] [blame] | 163 | |
Ivan A. Kosarev | 57493e2 | 2017-12-22 09:57:24 +0000 | [diff] [blame] | 164 | // Accesses to arrays are accesses to objects of their element types. |
| 165 | if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType()) |
| 166 | return getTypeInfo(cast<ArrayType>(Ty)->getElementType()); |
| 167 | |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 168 | // Enum types are distinct types. In C++ they have "underlying types", |
| 169 | // however they aren't related for TBAA. |
| 170 | if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) { |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 171 | // In C++ mode, types have linkage, so we can rely on the ODR and |
| 172 | // on their mangled names, if they're external. |
| 173 | // TODO: Is there a way to get a program-wide unique name for a |
| 174 | // decl with local linkage or no linkage? |
Eli Friedman | eecc09a | 2013-07-05 20:27:40 +0000 | [diff] [blame] | 175 | if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible()) |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 176 | return getChar(); |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 177 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 178 | SmallString<256> OutName; |
Rafael Espindola | 3968cd0 | 2011-02-11 02:52:17 +0000 | [diff] [blame] | 179 | llvm::raw_svector_ostream Out(OutName); |
Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 180 | MContext.mangleTypeName(QualType(ETy, 0), Out); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 181 | return createScalarTypeNode(OutName, getChar(), Size); |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Dan Gohman | 3f1cf0f | 2010-10-14 23:39:00 +0000 | [diff] [blame] | 184 | // For now, handle any other kind of type conservatively. |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 185 | return getChar(); |
| 186 | } |
| 187 | |
| 188 | llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) { |
| 189 | // At -O0 or relaxed aliasing, TBAA is not emitted for regular types. |
| 190 | if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing) |
| 191 | return nullptr; |
| 192 | |
| 193 | // If the type has the may_alias attribute (even on a typedef), it is |
| 194 | // effectively in the general char alias class. |
| 195 | if (TypeHasMayAlias(QTy)) |
| 196 | return getChar(); |
| 197 | |
| 198 | // We need this function to not fall back to returning the "omnipotent char" |
| 199 | // type node for aggregate and union types. Otherwise, any dereference of an |
| 200 | // aggregate will result into the may-alias access descriptor, meaning all |
| 201 | // subsequent accesses to direct and indirect members of that aggregate will |
| 202 | // be considered may-alias too. |
| 203 | // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function. |
| 204 | if (isValidBaseType(QTy)) |
| 205 | return getBaseTypeInfo(QTy); |
| 206 | |
| 207 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 208 | if (llvm::MDNode *N = MetadataCache[Ty]) |
| 209 | return N; |
| 210 | |
| 211 | // Note that the following helper call is allowed to add new nodes to the |
| 212 | // cache, which invalidates all its previously obtained iterators. So we |
| 213 | // first generate the node for the type and then add that node to the cache. |
| 214 | llvm::MDNode *TypeNode = getTypeInfoHelper(Ty); |
| 215 | return MetadataCache[Ty] = TypeNode; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 216 | } |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 217 | |
Ivan A. Kosarev | 124a218 | 2018-02-20 12:33:04 +0000 | [diff] [blame] | 218 | TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) { |
| 219 | // Pointee values may have incomplete types, but they shall never be |
| 220 | // dereferenced. |
| 221 | if (AccessType->isIncompleteType()) |
| 222 | return TBAAAccessInfo::getIncompleteInfo(); |
| 223 | |
| 224 | if (TypeHasMayAlias(AccessType)) |
| 225 | return TBAAAccessInfo::getMayAliasInfo(); |
| 226 | |
| 227 | uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity(); |
| 228 | return TBAAAccessInfo(getTypeInfo(AccessType), Size); |
| 229 | } |
| 230 | |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 231 | TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) { |
| 232 | llvm::DataLayout DL(&Module); |
| 233 | unsigned Size = DL.getPointerTypeSize(VTablePtrType); |
| 234 | return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size), |
| 235 | Size); |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 236 | } |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 237 | |
| 238 | bool |
| 239 | CodeGenTBAA::CollectFields(uint64_t BaseOffset, |
| 240 | QualType QTy, |
| 241 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> & |
| 242 | Fields, |
| 243 | bool MayAlias) { |
| 244 | /* Things not handled yet include: C++ base classes, bitfields, */ |
| 245 | |
| 246 | if (const RecordType *TTy = QTy->getAs<RecordType>()) { |
| 247 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
| 248 | if (RD->hasFlexibleArrayMember()) |
| 249 | return false; |
| 250 | |
| 251 | // TODO: Handle C++ base classes. |
| 252 | if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD)) |
| 253 | if (Decl->bases_begin() != Decl->bases_end()) |
| 254 | return false; |
| 255 | |
| 256 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 257 | |
| 258 | unsigned idx = 0; |
| 259 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 260 | e = RD->field_end(); i != e; ++i, ++idx) { |
| 261 | uint64_t Offset = BaseOffset + |
| 262 | Layout.getFieldOffset(idx) / Context.getCharWidth(); |
| 263 | QualType FieldQTy = i->getType(); |
| 264 | if (!CollectFields(Offset, FieldQTy, Fields, |
| 265 | MayAlias || TypeHasMayAlias(FieldQTy))) |
| 266 | return false; |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | /* Otherwise, treat whatever it is as a field. */ |
| 272 | uint64_t Offset = BaseOffset; |
| 273 | uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity(); |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 274 | llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 275 | llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size)); |
Manman Ren | 09a3912 | 2013-04-22 19:50:07 +0000 | [diff] [blame] | 276 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | |
| 280 | llvm::MDNode * |
| 281 | CodeGenTBAA::getTBAAStructInfo(QualType QTy) { |
| 282 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 283 | |
| 284 | if (llvm::MDNode *N = StructMetadataCache[Ty]) |
| 285 | return N; |
| 286 | |
| 287 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
| 288 | if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy))) |
| 289 | return MDHelper.createTBAAStructNode(Fields); |
| 290 | |
| 291 | // For now, handle any other kind of type conservatively. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 292 | return StructMetadataCache[Ty] = nullptr; |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 293 | } |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 294 | |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 295 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) { |
| 296 | if (auto *TTy = dyn_cast<RecordType>(Ty)) { |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 297 | const RecordDecl *RD = TTy->getDecl()->getDefinition(); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 298 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 299 | SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields; |
Hal Finkel | a5986b9 | 2017-12-03 03:10:13 +0000 | [diff] [blame] | 300 | for (FieldDecl *Field : RD->fields()) { |
| 301 | QualType FieldQTy = Field->getType(); |
| 302 | llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ? |
| 303 | getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy); |
| 304 | if (!TypeNode) |
| 305 | return BaseTypeMetadataCache[Ty] = nullptr; |
Ivan A. Kosarev | da34247 | 2017-11-30 09:26:39 +0000 | [diff] [blame] | 306 | |
Hal Finkel | a5986b9 | 2017-12-03 03:10:13 +0000 | [diff] [blame] | 307 | uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 308 | uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity(); |
| 309 | uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity(); |
| 310 | Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, |
| 311 | TypeNode)); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 314 | SmallString<256> OutName; |
Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 315 | if (Features.CPlusPlus) { |
Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 316 | // Don't use the mangler for C code. |
Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 317 | llvm::raw_svector_ostream Out(OutName); |
Reid Kleckner | cc99e26 | 2013-11-19 23:23:00 +0000 | [diff] [blame] | 318 | MContext.mangleTypeName(QualType(Ty, 0), Out); |
Manman Ren | 879ce88 | 2013-08-21 20:58:45 +0000 | [diff] [blame] | 319 | } else { |
| 320 | OutName = RD->getName(); |
| 321 | } |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 322 | |
Ivan A. Kosarev | d50b847 | 2017-12-22 09:54:23 +0000 | [diff] [blame] | 323 | if (CodeGenOpts.NewStructPathTBAA) { |
| 324 | llvm::MDNode *Parent = getChar(); |
| 325 | uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity(); |
| 326 | llvm::Metadata *Id = MDHelper.createString(OutName); |
| 327 | return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields); |
| 328 | } |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 329 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 330 | // Create the struct type node with a vector of pairs (offset, type). |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 331 | SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes; |
| 332 | for (const auto &Field : Fields) |
Ivan A. Kosarev | e814f32 | 2017-12-18 16:50:11 +0000 | [diff] [blame] | 333 | OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset)); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 334 | return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Ivan A. Kosarev | 5d9d32e | 2017-11-21 11:18:06 +0000 | [diff] [blame] | 337 | return nullptr; |
| 338 | } |
| 339 | |
| 340 | llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) { |
| 341 | if (!isValidBaseType(QTy)) |
| 342 | return nullptr; |
| 343 | |
| 344 | const Type *Ty = Context.getCanonicalType(QTy).getTypePtr(); |
| 345 | if (llvm::MDNode *N = BaseTypeMetadataCache[Ty]) |
| 346 | return N; |
| 347 | |
| 348 | // Note that the following helper call is allowed to add new nodes to the |
| 349 | // cache, which invalidates all its previously obtained iterators. So we |
| 350 | // first generate the node for the type and then add that node to the cache. |
| 351 | llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty); |
| 352 | return BaseTypeMetadataCache[Ty] = TypeNode; |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 355 | llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) { |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 356 | assert(!Info.isIncomplete() && "Access to an object of an incomplete type!"); |
| 357 | |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 358 | if (Info.isMayAlias()) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 359 | Info = TBAAAccessInfo(getChar(), Info.Size); |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 360 | |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 361 | if (!Info.AccessType) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 362 | return nullptr; |
Manman Ren | 4f755de | 2013-10-08 00:08:49 +0000 | [diff] [blame] | 363 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 364 | if (!CodeGenOpts.StructPathTBAA) |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 365 | Info = TBAAAccessInfo(Info.AccessType, Info.Size); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 366 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 367 | llvm::MDNode *&N = AccessTagMetadataCache[Info]; |
| 368 | if (N) |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 369 | return N; |
| 370 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 371 | if (!Info.BaseType) { |
| 372 | Info.BaseType = Info.AccessType; |
| 373 | assert(!Info.Offset && "Nonzero offset for an access with no base type!"); |
| 374 | } |
Ivan A. Kosarev | d50b847 | 2017-12-22 09:54:23 +0000 | [diff] [blame] | 375 | if (CodeGenOpts.NewStructPathTBAA) { |
| 376 | return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType, |
| 377 | Info.Offset, Info.Size); |
| 378 | } |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame] | 379 | return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType, |
| 380 | Info.Offset); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 381 | } |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 382 | |
Ivan A. Kosarev | ed141ba | 2017-10-17 09:12:13 +0000 | [diff] [blame] | 383 | TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, |
| 384 | TBAAAccessInfo TargetInfo) { |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 385 | if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias()) |
| 386 | return TBAAAccessInfo::getMayAliasInfo(); |
Ivan A. Kosarev | ed141ba | 2017-10-17 09:12:13 +0000 | [diff] [blame] | 387 | return TargetInfo; |
| 388 | } |
Ivan A. Kosarev | b9c59f3 | 2017-10-31 11:05:34 +0000 | [diff] [blame] | 389 | |
| 390 | TBAAAccessInfo |
| 391 | CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, |
| 392 | TBAAAccessInfo InfoB) { |
| 393 | if (InfoA == InfoB) |
| 394 | return InfoA; |
| 395 | |
| 396 | if (!InfoA || !InfoB) |
| 397 | return TBAAAccessInfo(); |
| 398 | |
| 399 | if (InfoA.isMayAlias() || InfoB.isMayAlias()) |
| 400 | return TBAAAccessInfo::getMayAliasInfo(); |
| 401 | |
| 402 | // TODO: Implement the rest of the logic here. For example, two accesses |
| 403 | // with same final access types result in an access to an object of that final |
| 404 | // access type regardless of their base types. |
| 405 | return TBAAAccessInfo::getMayAliasInfo(); |
| 406 | } |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 407 | |
| 408 | TBAAAccessInfo |
| 409 | CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, |
| 410 | TBAAAccessInfo SrcInfo) { |
| 411 | if (DestInfo == SrcInfo) |
| 412 | return DestInfo; |
| 413 | |
| 414 | if (!DestInfo || !SrcInfo) |
| 415 | return TBAAAccessInfo(); |
| 416 | |
| 417 | if (DestInfo.isMayAlias() || SrcInfo.isMayAlias()) |
| 418 | return TBAAAccessInfo::getMayAliasInfo(); |
| 419 | |
| 420 | // TODO: Implement the rest of the logic here. For example, two accesses |
| 421 | // with same final access types result in an access to an object of that final |
| 422 | // access type regardless of their base types. |
| 423 | return TBAAAccessInfo::getMayAliasInfo(); |
| 424 | } |