CodeGenTBAA.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //===--- CodeGenTypes.cpp - TBAA information for LLVM CodeGen -------------===//
  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.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CodeGenTBAA.h"
  14. #include "Mangle.h"
  15. #include "clang/AST/ASTContext.h"
  16. #include "llvm/LLVMContext.h"
  17. #include "llvm/Metadata.h"
  18. using namespace clang;
  19. using namespace CodeGen;
  20. CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
  21. const LangOptions &Features, MangleContext &MContext)
  22. : Context(Ctx), VMContext(VMContext), Features(Features), MContext(MContext),
  23. Root(0), Char(0) {
  24. }
  25. CodeGenTBAA::~CodeGenTBAA() {
  26. }
  27. llvm::MDNode *CodeGenTBAA::getTBAAInfoForNamedType(llvm::StringRef NameStr,
  28. llvm::MDNode *Parent) {
  29. llvm::Value *Ops[] = {
  30. llvm::MDString::get(VMContext, NameStr),
  31. Parent
  32. };
  33. return llvm::MDNode::get(VMContext, Ops, llvm::array_lengthof(Ops));
  34. }
  35. llvm::MDNode *
  36. CodeGenTBAA::getTBAAInfo(QualType QTy) {
  37. Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  38. if (llvm::MDNode *N = MetadataCache[Ty])
  39. return N;
  40. if (!Root) {
  41. Root = getTBAAInfoForNamedType("Experimental TBAA", 0);
  42. Char = getTBAAInfoForNamedType("omnipotent char", Root);
  43. }
  44. // For now, just emit a very minimal tree.
  45. if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
  46. switch (BTy->getKind()) {
  47. // Character types are special and can alias anything.
  48. // In C++, this technically only includes "char" and "unsigned char",
  49. // and not "signed char". In C, it includes all three. For now,
  50. // the risk of exploiting this detail in C++ seems likely to outweigh
  51. // the benefit.
  52. case BuiltinType::Char_U:
  53. case BuiltinType::Char_S:
  54. case BuiltinType::UChar:
  55. case BuiltinType::SChar:
  56. return Char;
  57. // Unsigned types can alias their corresponding signed types.
  58. case BuiltinType::UShort:
  59. return getTBAAInfo(Context.ShortTy);
  60. case BuiltinType::UInt:
  61. return getTBAAInfo(Context.IntTy);
  62. case BuiltinType::ULong:
  63. return getTBAAInfo(Context.LongTy);
  64. case BuiltinType::ULongLong:
  65. return getTBAAInfo(Context.LongLongTy);
  66. case BuiltinType::UInt128:
  67. return getTBAAInfo(Context.Int128Ty);
  68. // Other builtin types.
  69. default:
  70. return MetadataCache[Ty] =
  71. getTBAAInfoForNamedType(BTy->getName(Features), Char);
  72. }
  73. }
  74. // For now, treat all pointers as equivalent to each other.
  75. if (Ty->isPointerType())
  76. return MetadataCache[Ty] = getTBAAInfoForNamedType("TBAA.pointer", Char);
  77. // Enum types are distinct types. In C++ they have "underlying types",
  78. // however they aren't related for TBAA.
  79. if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
  80. // In C mode, two anonymous enums are compatible iff their members
  81. // are the same -- see C99 6.2.7p1. For now, be conservative. We could
  82. // theoretically implement this by combining information about all the
  83. // members into a single identifying MDNode.
  84. if (!Features.CPlusPlus &&
  85. ETy->getDecl()->getTypedefForAnonDecl())
  86. return MetadataCache[Ty] = Char;
  87. // In C++ mode, types have linkage, so we can rely on the ODR and
  88. // on their mangled names, if they're external.
  89. // TODO: Is there a way to get a program-wide unique name for a
  90. // decl with local linkage or no linkage?
  91. if (Features.CPlusPlus &&
  92. ETy->getDecl()->getLinkage() != ExternalLinkage)
  93. return MetadataCache[Ty] = Char;
  94. // TODO: This is using the RTTI name. Is there a better way to get
  95. // a unique string for a type?
  96. llvm::SmallString<256> OutName;
  97. MContext.mangleCXXRTTIName(QualType(ETy, 0), OutName);
  98. return MetadataCache[Ty] = getTBAAInfoForNamedType(OutName, Char);
  99. }
  100. // For now, handle any other kind of type conservatively.
  101. return MetadataCache[Ty] = Char;
  102. }