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 /// 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. 53 llvm::MDNode *BaseType; 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 }; 63 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; 68 const CodeGenOptions &CodeGenOpts; 69 const LangOptions &Features; 70 MangleContext &MContext; 71 72 // MDHelper - Helper for creating metadata. 73 llvm::MDBuilder MDHelper; 74 75 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing 76 /// them. 77 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache; 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; 82 83 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing 84 /// them for struct assignments. 85 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache; 86 87 llvm::MDNode *Root; 88 llvm::MDNode *Char; 89 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 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 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 110 public: 111 CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext, 112 const CodeGenOptions &CGO, 113 const LangOptions &Features, 114 MangleContext &MContext); 115 ~CodeGenTBAA(); 116 117 /// getTypeInfo - Get metadata used to describe accesses to objects of the 118 /// given type. 119 llvm::MDNode *getTypeInfo(QualType QTy); 120 121 /// getVTablePtrAccessInfo - Get the TBAA information that describes an 122 /// access to a virtual table pointer. 123 TBAAAccessInfo getVTablePtrAccessInfo(); 124 125 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of 126 /// the given type. 127 llvm::MDNode *getTBAAStructInfo(QualType QTy); 128 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); 132 133 /// getAccessTagInfo - Get TBAA tag for a given memory access. 134 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info); 135 136 /// getMayAliasAccessInfo - Get TBAA information that represents may-alias 137 /// accesses. 138 TBAAAccessInfo getMayAliasAccessInfo(); 139 }; 140 141 } // end namespace CodeGen 142 } // end namespace clang 143 144 namespace llvm { 145 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(), 151 DenseMapInfo<uint64_t>::getEmptyKey()); 152 } 153 154 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() { 155 return clang::CodeGen::TBAAAccessInfo( 156 DenseMapInfo<MDNode *>::getTombstoneKey(), 157 DenseMapInfo<MDNode *>::getTombstoneKey(), 158 DenseMapInfo<uint64_t>::getTombstoneKey()); 159 } 160 161 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) { 162 return DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^ 163 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^ 164 DenseMapInfo<uint64_t>::getHashValue(Val.Offset); 165 } 166 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 && 171 LHS.Offset == RHS.Offset; 172 } 173 }; 174 175 } // end namespace llvm 176 177 #endif 178