1 //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is the code that manages TBAA information and defines the TBAA policy
10 // for the optimizer to use.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/IR/MDBuilder.h"
21 #include "llvm/IR/Metadata.h"
33 // TBAAAccessKind - A kind of TBAA memory access descriptor.
34 enum class TBAAAccessKind
: unsigned {
40 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
41 struct TBAAAccessInfo
{
42 TBAAAccessInfo(TBAAAccessKind Kind
, llvm::MDNode
*BaseType
,
43 llvm::MDNode
*AccessType
, uint64_t Offset
, uint64_t Size
)
44 : Kind(Kind
), BaseType(BaseType
), AccessType(AccessType
),
45 Offset(Offset
), Size(Size
)
48 TBAAAccessInfo(llvm::MDNode
*BaseType
, llvm::MDNode
*AccessType
,
49 uint64_t Offset
, uint64_t Size
)
50 : TBAAAccessInfo(TBAAAccessKind::Ordinary
, BaseType
, AccessType
,
54 explicit TBAAAccessInfo(llvm::MDNode
*AccessType
, uint64_t Size
)
55 : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType
, /* Offset= */ 0, Size
)
59 : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
62 static TBAAAccessInfo
getMayAliasInfo() {
63 return TBAAAccessInfo(TBAAAccessKind::MayAlias
,
64 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
65 /* Offset= */ 0, /* Size= */ 0);
68 bool isMayAlias() const { return Kind
== TBAAAccessKind::MayAlias
; }
70 static TBAAAccessInfo
getIncompleteInfo() {
71 return TBAAAccessInfo(TBAAAccessKind::Incomplete
,
72 /* BaseType= */ nullptr, /* AccessType= */ nullptr,
73 /* Offset= */ 0, /* Size= */ 0);
76 bool isIncomplete() const { return Kind
== TBAAAccessKind::Incomplete
; }
78 bool operator==(const TBAAAccessInfo
&Other
) const {
79 return Kind
== Other
.Kind
&&
80 BaseType
== Other
.BaseType
&&
81 AccessType
== Other
.AccessType
&&
82 Offset
== Other
.Offset
&&
86 bool operator!=(const TBAAAccessInfo
&Other
) const {
87 return !(*this == Other
);
90 explicit operator bool() const {
91 return *this != TBAAAccessInfo();
94 /// Kind - The kind of the access descriptor.
97 /// BaseType - The base/leading access type. May be null if this access
98 /// descriptor represents an access that is not considered to be an access
99 /// to an aggregate or union member.
100 llvm::MDNode
*BaseType
;
102 /// AccessType - The final access type. May be null if there is no TBAA
103 /// information available about this access.
104 llvm::MDNode
*AccessType
;
106 /// Offset - The byte offset of the final access within the base one. Must be
107 /// zero if the base access type is not specified.
110 /// Size - The size of access, in bytes.
114 /// CodeGenTBAA - This class organizes the cross-module state that is used
115 /// while lowering AST types to LLVM types.
118 CodeGenTypes
&CGTypes
;
119 llvm::Module
&Module
;
120 const CodeGenOptions
&CodeGenOpts
;
121 const LangOptions
&Features
;
123 // MDHelper - Helper for creating metadata.
124 llvm::MDBuilder MDHelper
;
126 /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
128 llvm::DenseMap
<const Type
*, llvm::MDNode
*> MetadataCache
;
129 /// This maps clang::Types to a base access type in the type DAG.
130 llvm::DenseMap
<const Type
*, llvm::MDNode
*> BaseTypeMetadataCache
;
131 /// This maps TBAA access descriptors to tag nodes.
132 llvm::DenseMap
<TBAAAccessInfo
, llvm::MDNode
*> AccessTagMetadataCache
;
134 /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
135 /// them for struct assignments.
136 llvm::DenseMap
<const Type
*, llvm::MDNode
*> StructMetadataCache
;
141 /// getRoot - This is the mdnode for the root of the metadata type graph
142 /// for this translation unit.
143 llvm::MDNode
*getRoot();
145 /// getChar - This is the mdnode for "char", which is special, and any types
146 /// considered to be equivalent to it.
147 llvm::MDNode
*getChar();
149 /// CollectFields - Collect information about the fields of a type for
150 /// !tbaa.struct metadata formation. Return false for an unsupported type.
151 bool CollectFields(uint64_t BaseOffset
,
153 SmallVectorImpl
<llvm::MDBuilder::TBAAStructField
> &Fields
,
156 /// createScalarTypeNode - A wrapper function to create a metadata node
157 /// describing a scalar type.
158 llvm::MDNode
*createScalarTypeNode(StringRef Name
, llvm::MDNode
*Parent
,
161 /// getTypeInfoHelper - An internal helper function to generate metadata used
162 /// to describe accesses to objects of the given type.
163 llvm::MDNode
*getTypeInfoHelper(const Type
*Ty
);
165 /// getBaseTypeInfoHelper - An internal helper function to generate metadata
166 /// used to describe accesses to objects of the given base type.
167 llvm::MDNode
*getBaseTypeInfoHelper(const Type
*Ty
);
169 /// getValidBaseTypeInfo - Return metadata that describes the given base
170 /// access type. The type must be suitable.
171 llvm::MDNode
*getValidBaseTypeInfo(QualType QTy
);
174 CodeGenTBAA(ASTContext
&Ctx
, CodeGenTypes
&CGTypes
, llvm::Module
&M
,
175 const CodeGenOptions
&CGO
, const LangOptions
&Features
);
178 /// getTypeInfo - Get metadata used to describe accesses to objects of the
180 llvm::MDNode
*getTypeInfo(QualType QTy
);
182 /// getAccessInfo - Get TBAA information that describes an access to
183 /// an object of the given type.
184 TBAAAccessInfo
getAccessInfo(QualType AccessType
);
186 /// getVTablePtrAccessInfo - Get the TBAA information that describes an
187 /// access to a virtual table pointer.
188 TBAAAccessInfo
getVTablePtrAccessInfo(llvm::Type
*VTablePtrType
);
190 /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
192 llvm::MDNode
*getTBAAStructInfo(QualType QTy
);
194 /// getBaseTypeInfo - Get metadata that describes the given base access
195 /// type. Return null if the type is not suitable for use in TBAA access
197 llvm::MDNode
*getBaseTypeInfo(QualType QTy
);
199 /// getAccessTagInfo - Get TBAA tag for a given memory access.
200 llvm::MDNode
*getAccessTagInfo(TBAAAccessInfo Info
);
202 /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
204 TBAAAccessInfo
mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo
,
205 TBAAAccessInfo TargetInfo
);
207 /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
208 /// purpose of conditional operator.
209 TBAAAccessInfo
mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA
,
210 TBAAAccessInfo InfoB
);
212 /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
213 /// purpose of memory transfer calls.
214 TBAAAccessInfo
mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo
,
215 TBAAAccessInfo SrcInfo
);
218 } // end namespace CodeGen
219 } // end namespace clang
223 template<> struct DenseMapInfo
<clang::CodeGen::TBAAAccessInfo
> {
224 static clang::CodeGen::TBAAAccessInfo
getEmptyKey() {
225 unsigned UnsignedKey
= DenseMapInfo
<unsigned>::getEmptyKey();
226 return clang::CodeGen::TBAAAccessInfo(
227 static_cast<clang::CodeGen::TBAAAccessKind
>(UnsignedKey
),
228 DenseMapInfo
<MDNode
*>::getEmptyKey(),
229 DenseMapInfo
<MDNode
*>::getEmptyKey(),
230 DenseMapInfo
<uint64_t>::getEmptyKey(),
231 DenseMapInfo
<uint64_t>::getEmptyKey());
234 static clang::CodeGen::TBAAAccessInfo
getTombstoneKey() {
235 unsigned UnsignedKey
= DenseMapInfo
<unsigned>::getTombstoneKey();
236 return clang::CodeGen::TBAAAccessInfo(
237 static_cast<clang::CodeGen::TBAAAccessKind
>(UnsignedKey
),
238 DenseMapInfo
<MDNode
*>::getTombstoneKey(),
239 DenseMapInfo
<MDNode
*>::getTombstoneKey(),
240 DenseMapInfo
<uint64_t>::getTombstoneKey(),
241 DenseMapInfo
<uint64_t>::getTombstoneKey());
244 static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo
&Val
) {
245 auto KindValue
= static_cast<unsigned>(Val
.Kind
);
246 return DenseMapInfo
<unsigned>::getHashValue(KindValue
) ^
247 DenseMapInfo
<MDNode
*>::getHashValue(Val
.BaseType
) ^
248 DenseMapInfo
<MDNode
*>::getHashValue(Val
.AccessType
) ^
249 DenseMapInfo
<uint64_t>::getHashValue(Val
.Offset
) ^
250 DenseMapInfo
<uint64_t>::getHashValue(Val
.Size
);
253 static bool isEqual(const clang::CodeGen::TBAAAccessInfo
&LHS
,
254 const clang::CodeGen::TBAAAccessInfo
&RHS
) {
259 } // end namespace llvm