CodeGenTBAA.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
  14. #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
  15. #include "clang/AST/Type.h"
  16. #include "clang/Basic/LLVM.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/IR/MDBuilder.h"
  19. #include "llvm/IR/Metadata.h"
  20. namespace clang {
  21. class ASTContext;
  22. class CodeGenOptions;
  23. class LangOptions;
  24. class MangleContext;
  25. class QualType;
  26. class Type;
  27. namespace CodeGen {
  28. class CGRecordLayout;
  29. // TBAAAccessKind - A kind of TBAA memory access descriptor.
  30. enum class TBAAAccessKind : unsigned {
  31. Ordinary,
  32. MayAlias,
  33. Incomplete,
  34. };
  35. // TBAAAccessInfo - Describes a memory access in terms of TBAA.
  36. struct TBAAAccessInfo {
  37. TBAAAccessInfo(TBAAAccessKind Kind, llvm::MDNode *BaseType,
  38. llvm::MDNode *AccessType, uint64_t Offset, uint64_t Size)
  39. : Kind(Kind), BaseType(BaseType), AccessType(AccessType),
  40. Offset(Offset), Size(Size)
  41. {}
  42. TBAAAccessInfo(llvm::MDNode *BaseType, llvm::MDNode *AccessType,
  43. uint64_t Offset, uint64_t Size)
  44. : TBAAAccessInfo(TBAAAccessKind::Ordinary, BaseType, AccessType,
  45. Offset, Size)
  46. {}
  47. explicit TBAAAccessInfo(llvm::MDNode *AccessType, uint64_t Size)
  48. : TBAAAccessInfo(/* BaseType= */ nullptr, AccessType, /* Offset= */ 0, Size)
  49. {}
  50. TBAAAccessInfo()
  51. : TBAAAccessInfo(/* AccessType= */ nullptr, /* Size= */ 0)
  52. {}
  53. static TBAAAccessInfo getMayAliasInfo() {
  54. return TBAAAccessInfo(TBAAAccessKind::MayAlias,
  55. /* BaseType= */ nullptr, /* AccessType= */ nullptr,
  56. /* Offset= */ 0, /* Size= */ 0);
  57. }
  58. bool isMayAlias() const { return Kind == TBAAAccessKind::MayAlias; }
  59. static TBAAAccessInfo getIncompleteInfo() {
  60. return TBAAAccessInfo(TBAAAccessKind::Incomplete,
  61. /* BaseType= */ nullptr, /* AccessType= */ nullptr,
  62. /* Offset= */ 0, /* Size= */ 0);
  63. }
  64. bool isIncomplete() const { return Kind == TBAAAccessKind::Incomplete; }
  65. bool operator==(const TBAAAccessInfo &Other) const {
  66. return Kind == Other.Kind &&
  67. BaseType == Other.BaseType &&
  68. AccessType == Other.AccessType &&
  69. Offset == Other.Offset &&
  70. Size == Other.Size;
  71. }
  72. bool operator!=(const TBAAAccessInfo &Other) const {
  73. return !(*this == Other);
  74. }
  75. explicit operator bool() const {
  76. return *this != TBAAAccessInfo();
  77. }
  78. /// Kind - The kind of the access descriptor.
  79. TBAAAccessKind Kind;
  80. /// BaseType - The base/leading access type. May be null if this access
  81. /// descriptor represents an access that is not considered to be an access
  82. /// to an aggregate or union member.
  83. llvm::MDNode *BaseType;
  84. /// AccessType - The final access type. May be null if there is no TBAA
  85. /// information available about this access.
  86. llvm::MDNode *AccessType;
  87. /// Offset - The byte offset of the final access within the base one. Must be
  88. /// zero if the base access type is not specified.
  89. uint64_t Offset;
  90. /// Size - The size of access, in bytes.
  91. uint64_t Size;
  92. };
  93. /// CodeGenTBAA - This class organizes the cross-module state that is used
  94. /// while lowering AST types to LLVM types.
  95. class CodeGenTBAA {
  96. ASTContext &Context;
  97. llvm::Module &Module;
  98. const CodeGenOptions &CodeGenOpts;
  99. const LangOptions &Features;
  100. MangleContext &MContext;
  101. // MDHelper - Helper for creating metadata.
  102. llvm::MDBuilder MDHelper;
  103. /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
  104. /// them.
  105. llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
  106. /// This maps clang::Types to a base access type in the type DAG.
  107. llvm::DenseMap<const Type *, llvm::MDNode *> BaseTypeMetadataCache;
  108. /// This maps TBAA access descriptors to tag nodes.
  109. llvm::DenseMap<TBAAAccessInfo, llvm::MDNode *> AccessTagMetadataCache;
  110. /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
  111. /// them for struct assignments.
  112. llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
  113. llvm::MDNode *Root;
  114. llvm::MDNode *Char;
  115. /// getRoot - This is the mdnode for the root of the metadata type graph
  116. /// for this translation unit.
  117. llvm::MDNode *getRoot();
  118. /// getChar - This is the mdnode for "char", which is special, and any types
  119. /// considered to be equivalent to it.
  120. llvm::MDNode *getChar();
  121. /// CollectFields - Collect information about the fields of a type for
  122. /// !tbaa.struct metadata formation. Return false for an unsupported type.
  123. bool CollectFields(uint64_t BaseOffset,
  124. QualType Ty,
  125. SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
  126. bool MayAlias);
  127. /// createScalarTypeNode - A wrapper function to create a metadata node
  128. /// describing a scalar type.
  129. llvm::MDNode *createScalarTypeNode(StringRef Name, llvm::MDNode *Parent,
  130. uint64_t Size);
  131. /// getTypeInfoHelper - An internal helper function to generate metadata used
  132. /// to describe accesses to objects of the given type.
  133. llvm::MDNode *getTypeInfoHelper(const Type *Ty);
  134. /// getBaseTypeInfoHelper - An internal helper function to generate metadata
  135. /// used to describe accesses to objects of the given base type.
  136. llvm::MDNode *getBaseTypeInfoHelper(const Type *Ty);
  137. public:
  138. CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, const CodeGenOptions &CGO,
  139. const LangOptions &Features, MangleContext &MContext);
  140. ~CodeGenTBAA();
  141. /// getTypeInfo - Get metadata used to describe accesses to objects of the
  142. /// given type.
  143. llvm::MDNode *getTypeInfo(QualType QTy);
  144. /// getAccessInfo - Get TBAA information that describes an access to
  145. /// an object of the given type.
  146. TBAAAccessInfo getAccessInfo(QualType AccessType);
  147. /// getVTablePtrAccessInfo - Get the TBAA information that describes an
  148. /// access to a virtual table pointer.
  149. TBAAAccessInfo getVTablePtrAccessInfo(llvm::Type *VTablePtrType);
  150. /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
  151. /// the given type.
  152. llvm::MDNode *getTBAAStructInfo(QualType QTy);
  153. /// getBaseTypeInfo - Get metadata that describes the given base access type.
  154. /// Return null if the type is not suitable for use in TBAA access tags.
  155. llvm::MDNode *getBaseTypeInfo(QualType QTy);
  156. /// getAccessTagInfo - Get TBAA tag for a given memory access.
  157. llvm::MDNode *getAccessTagInfo(TBAAAccessInfo Info);
  158. /// mergeTBAAInfoForCast - Get merged TBAA information for the purpose of
  159. /// type casts.
  160. TBAAAccessInfo mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
  161. TBAAAccessInfo TargetInfo);
  162. /// mergeTBAAInfoForConditionalOperator - Get merged TBAA information for the
  163. /// purpose of conditional operator.
  164. TBAAAccessInfo mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
  165. TBAAAccessInfo InfoB);
  166. /// mergeTBAAInfoForMemoryTransfer - Get merged TBAA information for the
  167. /// purpose of memory transfer calls.
  168. TBAAAccessInfo mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
  169. TBAAAccessInfo SrcInfo);
  170. };
  171. } // end namespace CodeGen
  172. } // end namespace clang
  173. namespace llvm {
  174. template<> struct DenseMapInfo<clang::CodeGen::TBAAAccessInfo> {
  175. static clang::CodeGen::TBAAAccessInfo getEmptyKey() {
  176. unsigned UnsignedKey = DenseMapInfo<unsigned>::getEmptyKey();
  177. return clang::CodeGen::TBAAAccessInfo(
  178. static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
  179. DenseMapInfo<MDNode *>::getEmptyKey(),
  180. DenseMapInfo<MDNode *>::getEmptyKey(),
  181. DenseMapInfo<uint64_t>::getEmptyKey(),
  182. DenseMapInfo<uint64_t>::getEmptyKey());
  183. }
  184. static clang::CodeGen::TBAAAccessInfo getTombstoneKey() {
  185. unsigned UnsignedKey = DenseMapInfo<unsigned>::getTombstoneKey();
  186. return clang::CodeGen::TBAAAccessInfo(
  187. static_cast<clang::CodeGen::TBAAAccessKind>(UnsignedKey),
  188. DenseMapInfo<MDNode *>::getTombstoneKey(),
  189. DenseMapInfo<MDNode *>::getTombstoneKey(),
  190. DenseMapInfo<uint64_t>::getTombstoneKey(),
  191. DenseMapInfo<uint64_t>::getTombstoneKey());
  192. }
  193. static unsigned getHashValue(const clang::CodeGen::TBAAAccessInfo &Val) {
  194. auto KindValue = static_cast<unsigned>(Val.Kind);
  195. return DenseMapInfo<unsigned>::getHashValue(KindValue) ^
  196. DenseMapInfo<MDNode *>::getHashValue(Val.BaseType) ^
  197. DenseMapInfo<MDNode *>::getHashValue(Val.AccessType) ^
  198. DenseMapInfo<uint64_t>::getHashValue(Val.Offset) ^
  199. DenseMapInfo<uint64_t>::getHashValue(Val.Size);
  200. }
  201. static bool isEqual(const clang::CodeGen::TBAAAccessInfo &LHS,
  202. const clang::CodeGen::TBAAAccessInfo &RHS) {
  203. return LHS == RHS;
  204. }
  205. };
  206. } // end namespace llvm
  207. #endif