Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 1 | //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===// |
| 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. |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 17 | |
Benjamin Kramer | 8fdba91 | 2016-02-02 14:24:21 +0000 | [diff] [blame] | 18 | #include "clang/AST/Type.h" |
Chris Lattner | 01cf8db | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LLVM.h" |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/MDBuilder.h" |
Benjamin Kramer | 8fdba91 | 2016-02-02 14:24:21 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Metadata.h" |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 23 | |
| 24 | namespace clang { |
| 25 | class ASTContext; |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 26 | class CodeGenOptions; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 27 | class LangOptions; |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 28 | class MangleContext; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 29 | class QualType; |
| 30 | class Type; |
| 31 | |
| 32 | namespace CodeGen { |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 33 | class CGRecordLayout; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 34 | |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 35 | // TBAAAccessInfo - Describes a memory access in terms of TBAA. |
| 36 | struct TBAAAccessInfo { |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 37 | TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType, |
| 38 | uint64_t Offset) |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 39 | : BaseType(BaseType), AccessType(AccessType), Offset(Offset) |
| 40 | {} |
| 41 | |
| 42 | explicit TBAAAccessInfo(llvm::MDNode *AccessType) |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 43 | : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0) |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 44 | {} |
| 45 | |
| 46 | TBAAAccessInfo() |
| 47 | : TBAAAccessInfo(/* AccessType= */ nullptr) |
| 48 | {} |
| 49 | |
| 50 | /// BaseType - The base/leading access type. May be null if this access |
| 51 | /// descriptor represents an access that is not considered to be an access |
| 52 | /// to an aggregate or union member. |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 53 | llvm::MDNode *BaseType; |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 54 | |
| 55 | /// AccessType - The final access type. May be null if there is no TBAA |
| 56 | /// information available about this access. |
| 57 | llvm::MDNode *AccessType; |
| 58 | |
| 59 | /// Offset - The byte offset of the final access within the base one. Must be |
| 60 | /// zero if the base access type is not specified. |
| 61 | uint64_t Offset; |
| 62 | }; |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 63 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 64 | /// CodeGenTBAA - This class organizes the cross-module state that is used |
| 65 | /// while lowering AST types to LLVM types. |
| 66 | class CodeGenTBAA { |
| 67 | ASTContext &Context; |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 68 | const CodeGenOptions &CodeGenOpts; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 69 | const LangOptions &Features; |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 70 | MangleContext &MContext; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 71 | |
Duncan Sands | c720e78 | 2012-04-15 18:04:54 +0000 | [diff] [blame] | 72 | // MDHelper - Helper for creating metadata. |
| 73 | llvm::MDBuilder MDHelper; |
| 74 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 75 | /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing |
| 76 | /// them. |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 77 | llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache; |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 78 | /// This maps clang::Types to a base access type in the type DAG. |
| 79 | llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache; |
| 80 | /// This maps TBAA access descriptors to tag nodes. |
| 81 | llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 82 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 83 | /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing |
| 84 | /// them for struct assignments. |
| 85 | llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache; |
| 86 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 87 | llvm::MDNode *Root; |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 88 | llvm::MDNode *Char; |
| 89 | |
Dan Gohman | 7dfd13c | 2010-10-25 21:48:30 +0000 | [diff] [blame] | 90 | /// getRoot - This is the mdnode for the root of the metadata type graph |
| 91 | /// for this translation unit. |
| 92 | llvm::MDNode *getRoot(); |
| 93 | |
| 94 | /// getChar - This is the mdnode for "char", which is special, and any types |
| 95 | /// considered to be equivalent to it. |
| 96 | llvm::MDNode *getChar(); |
| 97 | |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 98 | /// CollectFields - Collect information about the fields of a type for |
| 99 | /// !tbaa.struct metadata formation. Return false for an unsupported type. |
| 100 | bool CollectFields(uint64_t BaseOffset, |
| 101 | QualType Ty, |
| 102 | SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields, |
| 103 | bool MayAlias); |
| 104 | |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 105 | /// A wrapper function to create a scalar type. For struct-path aware TBAA, |
| 106 | /// the scalar type has the same format as the struct type: name, offset, |
| 107 | /// pointer to another node in the type DAG. |
| 108 | llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent); |
| 109 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 110 | public: |
| 111 | CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, |
Kostya Serebryany | 5dd2cfc | 2012-04-24 06:57:01 +0000 | [diff] [blame] | 112 | const CodeGenOptions &CGO, |
Dan Gohman | 2e29eb5 | 2010-10-15 20:23:12 +0000 | [diff] [blame] | 113 | const LangOptions &Features, |
| 114 | MangleContext &MContext); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 115 | ~CodeGenTBAA(); |
| 116 | |
Ivan A. Kosarev | 289574e | 2017-10-02 09:54:47 +0000 | [diff] [blame] | 117 | /// getTypeInfo - Get metadata used to describe accesses to objects of the |
| 118 | /// given type. |
| 119 | llvm::MDNode *getTypeInfo(QualType QTy); |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 120 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 121 | /// getVTablePtrAccessInfo - Get the TBAA information that describes an |
| 122 | /// access to a virtual table pointer. |
| 123 | TBAAAccessInfo getVTablePtrAccessInfo(); |
Dan Gohman | 22695fc | 2012-09-28 21:58:29 +0000 | [diff] [blame] | 124 | |
| 125 | /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of |
| 126 | /// the given type. |
| 127 | llvm::MDNode *getTBAAStructInfo(QualType QTy); |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 128 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 129 | /// getBaseTypeInfo - Get metadata that describes the given base access type. |
| 130 | /// Return null if the type is not suitable for use in TBAA access tags. |
| 131 | llvm::MDNode *getBaseTypeInfo(QualType QTy); |
Ivan A. Kosarev | a511ed7 | 2017-10-03 10:52:39 +0000 | [diff] [blame] | 132 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 133 | /// getAccessTagInfo - Get TBAA tag for a given memory access. |
| 134 | llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info); |
Manman Ren | e1ad74e | 2013-04-11 23:02:56 +0000 | [diff] [blame] | 135 | |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 136 | /// getMayAliasAccessInfo - Get TBAA information that represents may-alias |
Ivan A. Kosarev | 5c8e759 | 2017-10-02 11:10:04 +0000 | [diff] [blame] | 137 | /// accesses. |
Ivan A. Kosarev | 3d68ce9 | 2017-10-05 11:08:17 +0000 | [diff] [blame] | 138 | TBAAAccessInfo getMayAliasAccessInfo(); |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // end namespace CodeGen |
| 142 | } // end namespace clang |
| 143 | |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 144 | namespace llvm { |
| 145 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 146 | template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> { |
| 147 | static clang::CodeGen::TBAAAccessInfo getEmptyKey() { |
| 148 | return clang::CodeGen::TBAAAccessInfo( |
| 149 | DenseMapInfo<MDNode *>::getEmptyKey(), |
| 150 | DenseMapInfo<MDNode *>::getEmptyKey(), |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 151 | DenseMapInfo<uint64_t>::getEmptyKey()); |
| 152 | } |
| 153 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 154 | static clang::CodeGen::TBAAAccessInfo getTombstoneKey() { |
| 155 | return clang::CodeGen::TBAAAccessInfo( |
| 156 | DenseMapInfo<MDNode *>::getTombstoneKey(), |
| 157 | DenseMapInfo<MDNode *>::getTombstoneKey(), |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 158 | DenseMapInfo<uint64_t>::getTombstoneKey()); |
| 159 | } |
| 160 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 161 | static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) { |
| 162 | return DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^ |
| 163 | DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^ |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 164 | DenseMapInfo<uint64_t>::getHashValue(Val.Offset); |
| 165 | } |
| 166 | |
Ivan A. Kosarev | 383890b | 2017-10-06 08:17:48 +0000 | [diff] [blame^] | 167 | static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS, |
| 168 | const clang::CodeGen::TBAAAccessInfo &RHS) { |
| 169 | return LHS.BaseType == RHS.BaseType && |
| 170 | LHS.AccessType == RHS.AccessType && |
Manman Ren | c451e57 | 2013-04-04 21:53:22 +0000 | [diff] [blame] | 171 | LHS.Offset == RHS.Offset; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | } // end namespace llvm |
| 176 | |
Dan Gohman | 947c9af | 2010-10-14 23:06:10 +0000 | [diff] [blame] | 177 | #endif |