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