1 //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the code that manages TBAA information and defines the TBAA policy
11 // for the optimizer to use.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
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"
35 // TBAAAccessKind - A kind of TBAA memory access descriptor.
36 enum class TBAAAccessKind : unsigned {
42 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
43 struct TBAAAccessInfo {
44 TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType,
45 llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
46 : Kind(Kind), BaseType(BaseType), AccessType(AccessType),
47 Offset(Offset), Size(Size)
50 TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
51 uint64_t Offset, uint64_t Size)
52 : TBAAAccessInfo(TBAAAccessKind::Ordinary, BaseType, AccessType,
56 explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
57 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
61 : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
64 static TBAAAccessInfo getMayAliasInfo() {
65 return TBAAAccessInfo(TBAAAccessKind::MayAlias,
66 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
67 /* Offset= */ 0, /* Size= */ 0);
70 bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
72 static TBAAAccessInfo getIncompleteInfo() {
73 return TBAAAccessInfo(TBAAAccessKind::Incomplete,
74 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
75 /* Offset= */ 0, /* Size= */ 0);
78 bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
80 bool operator==(const TBAAAccessInfo &Other) const {
81 return Kind == Other.Kind &&
82 BaseType == Other.BaseType &&
83 AccessType == Other.AccessType &&
84 Offset == Other.Offset &&
88 bool operator!=(const TBAAAccessInfo &Other) const {
89 return !(*this == Other);
92 explicit operator bool() const {
93 return *this != TBAAAccessInfo();
96 /// Kind - The kind of the access descriptor.
99 /// BaseType - The base/leading access type. May be null if this access
100 /// descriptor represents an access that is not considered to be an access
101 /// to an aggregate or union member.
102 llvm::MDNode *BaseType;
104 /// AccessType - The final access type. May be null if there is no TBAA
105 /// information available about this access.
106 llvm::MDNode *AccessType;
108 /// Offset - The byte offset of the final access within the base one. Must be
109 /// zero if the base access type is not specified.
112 /// Size - The size of access, in bytes.
116 /// CodeGenTBAA - This class organizes the cross-module state that is used
117 /// while lowering AST types to LLVM types.
120 llvm::Module &Module;
121 const CodeGenOptions &CodeGenOpts;
122 const LangOptions &Features;
123 MangleContext &MContext;
125 // MDHelper - Helper for creating metadata.
126 llvm::MDBuilder MDHelper;
128 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
130 llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
131 /// This maps clang::Types to a base access type in the type DAG.
132 llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
133 /// This maps TBAA access descriptors to tag nodes.
134 llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
136 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
137 /// them for struct assignments.
138 llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
143 /// getRoot - This is the mdnode for the root of the metadata type graph
144 /// for this translation unit.
145 llvm::MDNode *getRoot();
147 /// getChar - This is the mdnode for "char", which is special, and any types
148 /// considered to be equivalent to it.
149 llvm::MDNode *getChar();
151 /// CollectFields - Collect information about the fields of a type for
152 /// !tbaa.struct metadata formation. Return false for an unsupported type.
153 bool CollectFields(uint64_t BaseOffset,
155 SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
158 /// createScalarTypeNode - A wrapper function to create a metadata node
159 /// describing a scalar type.
160 llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
163 /// getTypeInfoHelper - An internal helper function to generate metadata used
164 /// to describe accesses to objects of the given type.
165 llvm::MDNode *getTypeInfoHelper(const Type *Ty);
167 /// getBaseTypeInfoHelper - An internal helper function to generate metadata
168 /// used to describe accesses to objects of the given base type.
169 llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
172 CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
173 const LangOptions &Features, MangleContext &MContext);
176 /// getTypeInfo - Get metadata used to describe accesses to objects of the
178 llvm::MDNode *getTypeInfo(QualType QTy);
180 /// getAccessInfo - Get TBAA information that describes an access to
181 /// an object of the given type.
182 TBAAAccessInfo getAccessInfo(QualType AccessType);
184 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
185 /// access to a virtual table pointer.
186 TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
188 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
190 llvm::MDNode *getTBAAStructInfo(QualType QTy);
192 /// getBaseTypeInfo - Get metadata that describes the given base access type.
193 /// Return null if the type is not suitable for use in TBAA access tags.
194 llvm::MDNode *getBaseTypeInfo(QualType QTy);
196 /// getAccessTagInfo - Get TBAA tag for a given memory access.
197 llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
199 /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
201 TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
202 TBAAAccessInfo TargetInfo);
204 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
205 /// purpose of conditional operator.
206 TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
207 TBAAAccessInfo InfoB);
209 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
210 /// purpose of memory transfer calls.
211 TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
212 TBAAAccessInfo SrcInfo);
215 } // end namespace CodeGen
216 } // end namespace clang
220 template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
221 static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
222 unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
223 return clang::CodeGen::TBAAAccessInfo(
224 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
225 DenseMapInfo<MDNode *>::getEmptyKey(),
226 DenseMapInfo<MDNode *>::getEmptyKey(),
227 DenseMapInfo<uint64_t>::getEmptyKey(),
228 DenseMapInfo<uint64_t>::getEmptyKey());
231 static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
232 unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
233 return clang::CodeGen::TBAAAccessInfo(
234 static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
235 DenseMapInfo<MDNode *>::getTombstoneKey(),
236 DenseMapInfo<MDNode *>::getTombstoneKey(),
237 DenseMapInfo<uint64_t>::getTombstoneKey(),
238 DenseMapInfo<uint64_t>::getTombstoneKey());
241 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
242 auto KindValue = static_cast<unsigned>(Val.Kind);
243 return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
244 DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
245 DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
246 DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
247 DenseMapInfo<uint64_t>::getHashValue(Val.Size);
250 static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
251 const clang::CodeGen::TBAAAccessInfo &RHS) {
256 } // end namespace llvm