clang  19.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 class CodeGenTypes;
33 
34 // TBAAAccessKind - A kind of TBAA memory access descriptor.
35 enum class TBAAAccessKind : unsigned {
36  Ordinary,
37  MayAlias,
38  Incomplete,
39 };
40 
41 // TBAAAccessInfo - Describes a memory access in terms of TBAA.
44  llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
47  {}
48 
49  TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
52  Offset, Size)
53  {}
54 
55  explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
56  : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
57  {}
58 
60  : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
61  {}
62 
65  /* BaseType= */ nullptr, /* AccessType= */ nullptr,
66  /* Offset= */ 0, /* Size= */ 0);
67  }
68 
69  bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
70 
73  /* BaseType= */ nullptr, /* AccessType= */ nullptr,
74  /* Offset= */ 0, /* Size= */ 0);
75  }
76 
77  bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
78 
79  bool operator==(const TBAAAccessInfo &Other) const {
80  return Kind == Other.Kind &&
81  BaseType == Other.BaseType &&
82  AccessType == Other.AccessType &&
83  Offset == Other.Offset &&
84  Size == Other.Size;
85  }
86 
87  bool operator!=(const TBAAAccessInfo &Other) const {
88  return !(*this == Other);
89  }
90 
91  explicit operator bool() const {
92  return *this != TBAAAccessInfo();
93  }
94 
95  /// Kind - The kind of the access descriptor.
97 
98  /// BaseType - The base/leading access type. May be null if this access
99  /// descriptor represents an access that is not considered to be an access
100  /// to an aggregate or union member.
101  llvm::MDNode *BaseType;
102 
103  /// AccessType - The final access type. May be null if there is no TBAA
104  /// information available about this access.
105  llvm::MDNode *AccessType;
106 
107  /// Offset - The byte offset of the final access within the base one. Must be
108  /// zero if the base access type is not specified.
110 
111  /// Size - The size of access, in bytes.
113 };
114 
115 /// CodeGenTBAA - This class organizes the cross-module state that is used
116 /// while lowering AST types to LLVM types.
117 class CodeGenTBAA {
118  ASTContext &Context;
119  CodeGenTypes &CGTypes;
120  llvm::Module &Module;
121  const CodeGenOptions &CodeGenOpts;
122  const LangOptions &Features;
123  MangleContext &MContext;
124 
125  // MDHelper - Helper for creating metadata.
126  llvm::MDBuilder MDHelper;
127 
128  /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
129  /// them.
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;
135 
136  /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
137  /// them for struct assignments.
138  llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
139 
140  llvm::MDNode *Root;
141  llvm::MDNode *Char;
142 
143  /// getRoot - This is the mdnode for the root of the metadata type graph
144  /// for this translation unit.
145  llvm::MDNode *getRoot();
146 
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();
150 
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,
154  QualType Ty,
156  bool MayAlias);
157 
158  /// createScalarTypeNode - A wrapper function to create a metadata node
159  /// describing a scalar type.
160  llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
161  uint64_t Size);
162 
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);
166 
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);
170 
171  /// getValidBaseTypeInfo - Return metadata that describes the given base
172  /// access type. The type must be suitable.
173  llvm::MDNode *getValidBaseTypeInfo(QualType QTy);
174 
175 public:
176  CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M,
177  const CodeGenOptions &CGO, const LangOptions &Features,
178  MangleContext &MContext);
179  ~CodeGenTBAA();
180 
181  /// getTypeInfo - Get metadata used to describe accesses to objects of the
182  /// given type.
183  llvm::MDNode *getTypeInfo(QualType QTy);
184 
185  /// getAccessInfo - Get TBAA information that describes an access to
186  /// an object of the given type.
188 
189  /// getVTablePtrAccessInfo - Get the TBAA information that describes an
190  /// access to a virtual table pointer.
191  TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
192 
193  /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
194  /// the given type.
195  llvm::MDNode *getTBAAStructInfo(QualType QTy);
196 
197  /// getBaseTypeInfo - Get metadata that describes the given base access
198  /// type. Return null if the type is not suitable for use in TBAA access
199  /// tags.
200  llvm::MDNode *getBaseTypeInfo(QualType QTy);
201 
202  /// getAccessTagInfo - Get TBAA tag for a given memory access.
203  llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
204 
205  /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
206  /// type casts.
209 
210  /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
211  /// purpose of conditional operator.
213  TBAAAccessInfo InfoB);
214 
215  /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
216  /// purpose of memory transfer calls.
218  TBAAAccessInfo SrcInfo);
219 };
220 
221 } // end namespace CodeGen
222 } // end namespace clang
223 
224 namespace llvm {
225 
226 template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
228  unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
230  static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
231  DenseMapInfo<MDNode *>::getEmptyKey(),
232  DenseMapInfo<MDNode *>::getEmptyKey(),
233  DenseMapInfo<uint64_t>::getEmptyKey(),
234  DenseMapInfo<uint64_t>::getEmptyKey());
235  }
236 
238  unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
240  static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
241  DenseMapInfo<MDNode *>::getTombstoneKey(),
242  DenseMapInfo<MDNode *>::getTombstoneKey(),
243  DenseMapInfo<uint64_t>::getTombstoneKey(),
244  DenseMapInfo<uint64_t>::getTombstoneKey());
245  }
246 
247  static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
248  auto KindValue = static_cast<unsigned>(Val.Kind);
249  return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
250  DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
251  DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
252  DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
253  DenseMapInfo<uint64_t>::getHashValue(Val.Size);
254  }
255 
256  static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
257  const clang::CodeGen::TBAAAccessInfo &RHS) {
258  return LHS == RHS;
259  }
260 };
261 
262 } // end namespace llvm
263 
264 #endif
NodeId Parent
Definition: ASTDiff.cpp:191
MatchType Type
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CodeGenTBAA - This class organizes the cross-module state that is used while lowering AST types to LL...
Definition: CodeGenTBAA.h:117
llvm::MDNode * getBaseTypeInfo(QualType QTy)
getBaseTypeInfo - Get metadata that describes the given base access type.
llvm::MDNode * getTypeInfo(QualType QTy)
getTypeInfo - Get metadata used to describe accesses to objects of the given type.
TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType)
getVTablePtrAccessInfo - Get the TBAA information that describes an access to a virtual table pointer...
TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo, TBAAAccessInfo SrcInfo)
mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the purpose of memory transfer calls...
TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo, TBAAAccessInfo TargetInfo)
mergeTBAAInfoForCast - Get merged TBAA information for the purpose of type casts.
TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA, TBAAAccessInfo InfoB)
mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the purpose of conditional oper...
llvm::MDNode * getAccessTagInfo(TBAAAccessInfo Info)
getAccessTagInfo - Get TBAA tag for a given memory access.
llvm::MDNode * getTBAAStructInfo(QualType QTy)
getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of the given type.
CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M, const CodeGenOptions &CGO, const LangOptions &Features, MangleContext &MContext)
Definition: CodeGenTBAA.cpp:36
TBAAAccessInfo getAccessInfo(QualType AccessType)
getAccessInfo - Get TBAA information that describes an access to an object of the given type.
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
MangleContext - Context for tracking state which persists across multiple calls to the C++ name mangl...
Definition: Mangle.h:45
Describes a module or submodule.
Definition: Module.h:105
A (possibly-)qualified type.
Definition: Type.h:940
Exposes information about the current target.
Definition: TargetInfo.h:218
The base class of the type hierarchy.
Definition: Type.h:1813
The JSON file list parser is used to communicate input to InstallAPI.
@ Other
Other implicit parameter.
unsigned long uint64_t
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
#define bool
Definition: stdbool.h:24
TBAAAccessKind Kind
Kind - The kind of the access descriptor.
Definition: CodeGenTBAA.h:96
llvm::MDNode * AccessType
AccessType - The final access type.
Definition: CodeGenTBAA.h:105
uint64_t Offset
Offset - The byte offset of the final access within the base one.
Definition: CodeGenTBAA.h:109
TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:43
static TBAAAccessInfo getMayAliasInfo()
Definition: CodeGenTBAA.h:63
TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
Definition: CodeGenTBAA.h:55
uint64_t Size
Size - The size of access, in bytes.
Definition: CodeGenTBAA.h:112
bool operator==(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:79
static TBAAAccessInfo getIncompleteInfo()
Definition: CodeGenTBAA.h:71
bool operator!=(const TBAAAccessInfo &Other) const
Definition: CodeGenTBAA.h:87
llvm::MDNode * BaseType
BaseType - The base/leading access type.
Definition: CodeGenTBAA.h:101
TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
Definition: CodeGenTBAA.h:49
static clang::CodeGen::TBAAAccessInfo getTombstoneKey()
Definition: CodeGenTBAA.h:237
static clang::CodeGen::TBAAAccessInfo getEmptyKey()
Definition: CodeGenTBAA.h:227
static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val)
Definition: CodeGenTBAA.h:247
static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS, const clang::CodeGen::TBAAAccessInfo &RHS)
Definition: CodeGenTBAA.h:256