CodeGenTBAA.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //===--- CodeGenTBAA.h - TBAA information for LLVM CodeGen ------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the code that manages TBAA information and defines the TBAA policy
  11. // for the optimizer to use.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
  15. #define LLVM_CLANG_LIB_CODEGEN_CODEGENTBAA_H
  16. #include "clang/Basic/LLVM.h"
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/IR/MDBuilder.h"
  19. namespace llvm {
  20. class LLVMContext;
  21. class MDNode;
  22. }
  23. namespace clang {
  24. class ASTContext;
  25. class CodeGenOptions;
  26. class LangOptions;
  27. class MangleContext;
  28. class QualType;
  29. class Type;
  30. namespace CodeGen {
  31. class CGRecordLayout;
  32. struct TBAAPathTag {
  33. TBAAPathTag(const Type *B, const llvm::MDNode *A, uint64_t O)
  34. : BaseT(B), AccessN(A), Offset(O) {}
  35. const Type *BaseT;
  36. const llvm::MDNode *AccessN;
  37. uint64_t Offset;
  38. };
  39. /// CodeGenTBAA - This class organizes the cross-module state that is used
  40. /// while lowering AST types to LLVM types.
  41. class CodeGenTBAA {
  42. ASTContext &Context;
  43. const CodeGenOptions &CodeGenOpts;
  44. const LangOptions &Features;
  45. MangleContext &MContext;
  46. // MDHelper - Helper for creating metadata.
  47. llvm::MDBuilder MDHelper;
  48. /// MetadataCache - This maps clang::Types to scalar llvm::MDNodes describing
  49. /// them.
  50. llvm::DenseMap<const Type *, llvm::MDNode *> MetadataCache;
  51. /// This maps clang::Types to a struct node in the type DAG.
  52. llvm::DenseMap<const Type *, llvm::MDNode *> StructTypeMetadataCache;
  53. /// This maps TBAAPathTags to a tag node.
  54. llvm::DenseMap<TBAAPathTag, llvm::MDNode *> StructTagMetadataCache;
  55. /// This maps a scalar type to a scalar tag node.
  56. llvm::DenseMap<const llvm::MDNode *, llvm::MDNode *> ScalarTagMetadataCache;
  57. /// StructMetadataCache - This maps clang::Types to llvm::MDNodes describing
  58. /// them for struct assignments.
  59. llvm::DenseMap<const Type *, llvm::MDNode *> StructMetadataCache;
  60. llvm::MDNode *Root;
  61. llvm::MDNode *Char;
  62. /// getRoot - This is the mdnode for the root of the metadata type graph
  63. /// for this translation unit.
  64. llvm::MDNode *getRoot();
  65. /// getChar - This is the mdnode for "char", which is special, and any types
  66. /// considered to be equivalent to it.
  67. llvm::MDNode *getChar();
  68. /// CollectFields - Collect information about the fields of a type for
  69. /// !tbaa.struct metadata formation. Return false for an unsupported type.
  70. bool CollectFields(uint64_t BaseOffset,
  71. QualType Ty,
  72. SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &Fields,
  73. bool MayAlias);
  74. /// A wrapper function to create a scalar type. For struct-path aware TBAA,
  75. /// the scalar type has the same format as the struct type: name, offset,
  76. /// pointer to another node in the type DAG.
  77. llvm::MDNode *createTBAAScalarType(StringRef Name, llvm::MDNode *Parent);
  78. public:
  79. CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext &VMContext,
  80. const CodeGenOptions &CGO,
  81. const LangOptions &Features,
  82. MangleContext &MContext);
  83. ~CodeGenTBAA();
  84. /// getTBAAInfo - Get the TBAA MDNode to be used for a dereference
  85. /// of the given type.
  86. llvm::MDNode *getTBAAInfo(QualType QTy);
  87. /// getTBAAInfoForVTablePtr - Get the TBAA MDNode to be used for a
  88. /// dereference of a vtable pointer.
  89. llvm::MDNode *getTBAAInfoForVTablePtr();
  90. /// getTBAAStructInfo - Get the TBAAStruct MDNode to be used for a memcpy of
  91. /// the given type.
  92. llvm::MDNode *getTBAAStructInfo(QualType QTy);
  93. /// Get the MDNode in the type DAG for given struct type QType.
  94. llvm::MDNode *getTBAAStructTypeInfo(QualType QType);
  95. /// Get the tag MDNode for a given base type, the actual scalar access MDNode
  96. /// and offset into the base type.
  97. llvm::MDNode *getTBAAStructTagInfo(QualType BaseQType,
  98. llvm::MDNode *AccessNode, uint64_t Offset);
  99. /// Get the scalar tag MDNode for a given scalar type.
  100. llvm::MDNode *getTBAAScalarTagInfo(llvm::MDNode *AccessNode);
  101. };
  102. } // end namespace CodeGen
  103. } // end namespace clang
  104. namespace llvm {
  105. template<> struct DenseMapInfo<clang::CodeGen::TBAAPathTag> {
  106. static clang::CodeGen::TBAAPathTag getEmptyKey() {
  107. return clang::CodeGen::TBAAPathTag(
  108. DenseMapInfo<const clang::Type *>::getEmptyKey(),
  109. DenseMapInfo<const MDNode *>::getEmptyKey(),
  110. DenseMapInfo<uint64_t>::getEmptyKey());
  111. }
  112. static clang::CodeGen::TBAAPathTag getTombstoneKey() {
  113. return clang::CodeGen::TBAAPathTag(
  114. DenseMapInfo<const clang::Type *>::getTombstoneKey(),
  115. DenseMapInfo<const MDNode *>::getTombstoneKey(),
  116. DenseMapInfo<uint64_t>::getTombstoneKey());
  117. }
  118. static unsigned getHashValue(const clang::CodeGen::TBAAPathTag &Val) {
  119. return DenseMapInfo<const clang::Type *>::getHashValue(Val.BaseT) ^
  120. DenseMapInfo<const MDNode *>::getHashValue(Val.AccessN) ^
  121. DenseMapInfo<uint64_t>::getHashValue(Val.Offset);
  122. }
  123. static bool isEqual(const clang::CodeGen::TBAAPathTag &LHS,
  124. const clang::CodeGen::TBAAPathTag &RHS) {
  125. return LHS.BaseT == RHS.BaseT &&
  126. LHS.AccessN == RHS.AccessN &&
  127. LHS.Offset == RHS.Offset;
  128. }
  129. };
  130. } // end namespace llvm
  131. #endif