clang  16.0.0git
CodeGenTBAA.h
Go to the documentation of this file.
1 //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is the code that manages TBAA information and defines the TBAA policy
10 // for the optimizer to use.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
15 #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
16 
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"
22 
23 namespace clang {
24  class ASTContext;
25  class CodeGenOptions;
26  class LangOptions;
27  class MangleContext;
28  class QualType;
29  class Type;
30 
31 namespace CodeGen {
32 
33 // TBAAAccessKind - A kind of TBAA memory access descriptor.
34 enum class TBAAAccessKind : unsigned {
35  Ordinary,
36  MayAlias,
37  Incomplete,
38 };
39 
40 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
43  llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
46  {}
47 
48  TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
51  Offset, Size)
52  {}
53 
54  explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
55  : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
56  {}
57 
59  : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
60  {}
61 
64  /* BaseType= */ nullptr, /* AccessType= */ nullptr,
65  /* Offset= */ 0, /* Size= */ 0);
66  }
67 
68  bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
69 
72  /* BaseType= */ nullptr, /* AccessType= */ nullptr,
73  /* Offset= */ 0, /* Size= */ 0);
74  }
75 
76  bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
77 
78  bool operator==(const TBAAAccessInfo &Other) const {
79  return Kind == Other.Kind &&
80  BaseType == Other.BaseType &&
81  AccessType == Other.AccessType &&
82  Offset == Other.Offset &&
83  Size == Other.Size;
84  }
85 
86  bool operator!=(const TBAAAccessInfo &Other) const {
87  return !(*this == Other);
88  }
89 
90  explicit operator bool() const {
91  return *this != TBAAAccessInfo();
92  }
93 
94  /// Kind - The kind of the access descriptor.
96 
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;
101 
102  /// AccessType - The final access type. May be null if there is no TBAA
103  /// information available about this access.
104  llvm::MDNode *AccessType;
105 
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.
109 
110  /// Size - The size of access, in bytes.
112 };
113 
114 /// CodeGenTBAA - This class organizes the cross-module state that is used
115 /// while lowering AST types to LLVM types.
116 class CodeGenTBAA {
117  ASTContext &Context;
118  llvm::Module &Module;
119  const CodeGenOptions &CodeGenOpts;
120  const LangOptions &Features;
121  MangleContext &MContext;
122 
123  // MDHelper - Helper for creating metadata.
124  llvm::MDBuilder MDHelper;
125 
126  /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
127  /// them.
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;
133 
134  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
135  /// them for struct assignments.
136  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
137 
138  llvm::MDNode *Root;
139  llvm::MDNode *Char;
140 
141  /// getRoot - This is the mdnode for the root of the metadata type graph
142  /// for this translation unit.
143  llvm::MDNode *getRoot();
144 
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();
148 
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,
152  QualType Ty,
154  bool MayAlias);
155 
156  /// createScalarTypeNode - A wrapper function to create a metadata node
157  /// describing a scalar type.
158  llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
159  uint64_t Size);
160 
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);
164 
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);
168 
169 public:
170  CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
171  const LangOptions &Features, MangleContext &MContext);
172  ~CodeGenTBAA();
173 
174  /// getTypeInfo - Get metadata used to describe accesses to objects of the
175  /// given type.
176  llvm::MDNode *getTypeInfo(QualType QTy);
177 
178  /// getAccessInfo - Get TBAA information that describes an access to
179  /// an object of the given type.
181 
182  /// getVTablePtrAccessInfo - Get the TBAA information that describes an
183  /// access to a virtual table pointer.
184  TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
185 
186  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
187  /// the given type.
188  llvm::MDNode *getTBAAStructInfo(QualType QTy);
189 
190  /// getBaseTypeInfo - Get metadata that describes the given base access type.
191  /// Return null if the type is not suitable for use in TBAA access tags.
192  llvm::MDNode *getBaseTypeInfo(QualType QTy);
193 
194  /// getAccessTagInfo - Get TBAA tag for a given memory access.
195  llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
196 
197  /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
198  /// type casts.
201 
202  /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
203  /// purpose of conditional operator.
205  TBAAAccessInfo InfoB);
206 
207  /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
208  /// purpose of memory transfer calls.
210  TBAAAccessInfo SrcInfo);
211 };
212 
213 } // end namespace CodeGen
214 } // end namespace clang
215 
216 namespace llvm {
217 
218 template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
220  unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
222  static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
223  DenseMapInfo<MDNode *>::getEmptyKey(),
224  DenseMapInfo<MDNode *>::getEmptyKey(),
225  DenseMapInfo<uint64_t>::getEmptyKey(),
226  DenseMapInfo<uint64_t>::getEmptyKey());
227  }
228 
230  unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
232  static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
233  DenseMapInfo<MDNode *>::getTombstoneKey(),
234  DenseMapInfo<MDNode *>::getTombstoneKey(),
235  DenseMapInfo<uint64_t>::getTombstoneKey(),
236  DenseMapInfo<uint64_t>::getTombstoneKey());
237  }
238 
239  static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
240  auto KindValue = static_cast<unsigned>(Val.Kind);
241  return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
242  DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
243  DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
244  DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
245  DenseMapInfo<uint64_t>::getHashValue(Val.Size);
246  }
247 
248  static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
249  const clang::CodeGen::TBAAAccessInfo &RHS) {
250  return LHS == RHS;
251  }
252 };
253 
254 } // end namespace llvm
255 
256 #endif
clang::CodeGen::CodeGenTBAA::mergeTBAAInfoForConditionalOperator
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purpose of conditional oper...
Definition: CodeGenTBAA.cpp:467
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::CodeGen::TBAAAccessInfo::Kind
TBAAAccessKind Kind
Kind - The kind of the access descriptor.
Definition: CodeGenTBAA.h:95
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:48
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:42
clang::CodeGen::TBAAAccessKind::Incomplete
@ Incomplete
clang::CodeGen::CodeGenTBAA::getTypeInfo
llvm::MDNode * getTypeInfo(QualType QTy)
getTypeInfo - Get metadata used to describe accesses to objects of the given type.
Definition: CodeGenTBAA.cpp:225
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::TargetInfo
Exposes information about the current target.
Definition: TargetInfo.h:205
clang::CodeGen::CodeGenTBAA::getVTablePtrAccessInfo
TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table pointer...
Definition: CodeGenTBAA.cpp:268
clang::CodeGen::TBAAAccessKind
TBAAAccessKind
Definition: CodeGenTBAA.h:34
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
Definition: CodeGenTBAA.h:54
clang::ObjCSubstitutionContext::Ordinary
@ Ordinary
An ordinary type.
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getTombstoneKey
static clang::CodeGen::TBAAAccessInfo getTombstoneKey()
Definition: CodeGenTBAA.h:229
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1565
clang::CodeGen::TBAAAccessKind::Ordinary
@ Ordinary
clang::CodeGen::TBAAAccessKind::MayAlias
@ MayAlias
clang::CodeGen::TBAAAccessInfo::isMayAlias
bool isMayAlias() const
Definition: CodeGenTBAA.h:68
clang::CodeGen::TBAAAccessInfo
Definition: CodeGenTBAA.h:41
clang::MangleContext
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:44
clang::Module
Describes a module or submodule.
Definition: Module.h:98
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::isEqual
static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS, const clang::CodeGen::TBAAAccessInfo &RHS)
Definition: CodeGenTBAA.h:248
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::CodeGen::TBAAAccessInfo::isIncomplete
bool isIncomplete() const
Definition: CodeGenTBAA.h:76
clang::CodeGen::TBAAAccessInfo::BaseType
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:100
Type.h
bool
#define bool
Definition: stdbool.h:20
clang::CodeGen::CodeGenTBAA::CodeGenTBAA
CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO, const LangOptions &Features, MangleContext &MContext)
Definition: CodeGenTBAA.cpp:32
clang::CodeGen::CodeGenTBAA::getBaseTypeInfo
llvm::MDNode * getBaseTypeInfo(QualType QTy)
getBaseTypeInfo - Get metadata that describes the given base access type.
Definition: CodeGenTBAA.cpp:416
clang::CodeGen::CodeGenTBAA::getAccessTagInfo
llvm::MDNode * getAccessTagInfo(TBAAAccessInfo Info)
getAccessTagInfo - Get TBAA tag for a given memory access.
Definition: CodeGenTBAA.cpp:431
clang::CodeGen::TBAAAccessInfo::getIncompleteInfo
static TBAAAccessInfo getIncompleteInfo()
Definition: CodeGenTBAA.h:70
clang::CodeGenOptions
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
Definition: CodeGenOptions.h:49
clang::CodeGen::CodeGenTBAA::getTBAAStructInfo
llvm::MDNode * getTBAAStructInfo(QualType QTy)
getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of the given type.
Definition: CodeGenTBAA.cpp:320
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getEmptyKey
static clang::CodeGen::TBAAAccessInfo getEmptyKey()
Definition: CodeGenTBAA.h:219
clang::CodeGen::TBAAAccessInfo::Size
uint64_t Size
Size - The size of access, in bytes.
Definition: CodeGenTBAA.h:111
LLVM.h
clang::CodeGen::TBAAAccessInfo::operator!=
bool operator!=(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:86
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:81
llvm::DenseMapInfo< clang::CodeGen::TBAAAccessInfo >::getHashValue
static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val)
Definition: CodeGenTBAA.h:239
clang::CodeGen::TBAAAccessInfo::operator==
bool operator==(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:78
clang::CodeGen::CodeGenTBAA::mergeTBAAInfoForCast
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purpose of type casts.
Definition: CodeGenTBAA.cpp:459
clang::CodeGen::CodeGenTBAA::~CodeGenTBAA
~CodeGenTBAA()
Definition: CodeGenTBAA.cpp:40
clang::CodeGen::TBAAAccessInfo::Offset
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:108
clang
Definition: CalledOnceCheck.h:17
unsigned
clang::CodeGen::TBAAAccessInfo::getMayAliasInfo
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:62
clang::CodeGen::TBAAAccessInfo::TBAAAccessInfo
TBAAAccessInfo()
Definition: CodeGenTBAA.h:58
Parent
NodeId Parent
Definition: ASTDiff.cpp:190
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::CodeGen::CodeGenTBAA::getAccessInfo
TBAAAccessInfo getAccessInfo(QualType AccessType)
getAccessInfo - Get TBAA information that describes an access to an object of the given type.
Definition: CodeGenTBAA.cpp:255
clang::CodeGen::TBAAAccessInfo::AccessType
llvm::MDNode * AccessType
AccessType - The final access type.
Definition: CodeGenTBAA.h:104
clang::CodeGen::CodeGenTBAA
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:116
clang::CodeGen::CodeGenTBAA::mergeTBAAInfoForMemoryTransfer
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, TBAAAccessInfo SrcInfo)
mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the purpose of memory transfer calls...
Definition: CodeGenTBAA.cpp:485
Type
MatchType Type
Definition: ASTMatchFinder.cpp:71