CodeGenTBAA.h 9.2 KB

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