CodeGenTBAA.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 and defines the TBAA policy
  11. // for the optimizer to use. Relevant standards text includes:
  12. //
  13. // C99 6.5p7
  14. // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #include "CodeGenTBAA.h"
  18. #include "clang/AST/ASTContext.h"
  19. #include "clang/AST/Mangle.h"
  20. #include "clang/Frontend/CodeGenOptions.h"
  21. #include "llvm/LLVMContext.h"
  22. #include "llvm/Metadata.h"
  23. #include "llvm/Constants.h"
  24. #include "llvm/Type.h"
  25. using namespace clang;
  26. using namespace CodeGen;
  27. CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
  28. const CodeGenOptions &CGO,
  29. const LangOptions &Features, MangleContext &MContext)
  30. : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
  31. MDHelper(VMContext), Root(0), Char(0) {
  32. }
  33. CodeGenTBAA::~CodeGenTBAA() {
  34. }
  35. llvm::MDNode *CodeGenTBAA::getRoot() {
  36. // Define the root of the tree. This identifies the tree, so that
  37. // if our LLVM IR is linked with LLVM IR from a different front-end
  38. // (or a different version of this front-end), their TBAA trees will
  39. // remain distinct, and the optimizer will treat them conservatively.
  40. if (!Root)
  41. Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
  42. return Root;
  43. }
  44. llvm::MDNode *CodeGenTBAA::getChar() {
  45. // Define the root of the tree for user-accessible memory. C and C++
  46. // give special powers to char and certain similar types. However,
  47. // these special powers only cover user-accessible memory, and doesn't
  48. // include things like vtables.
  49. if (!Char)
  50. Char = MDHelper.createTBAANode("omnipotent char", getRoot());
  51. return Char;
  52. }
  53. static bool TypeHasMayAlias(QualType QTy) {
  54. // Tagged types have declarations, and therefore may have attributes.
  55. if (const TagType *TTy = dyn_cast<TagType>(QTy))
  56. return TTy->getDecl()->hasAttr<MayAliasAttr>();
  57. // Typedef types have declarations, and therefore may have attributes.
  58. if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
  59. if (TTy->getDecl()->hasAttr<MayAliasAttr>())
  60. return true;
  61. // Also, their underlying types may have relevant attributes.
  62. return TypeHasMayAlias(TTy->desugar());
  63. }
  64. return false;
  65. }
  66. llvm::MDNode *
  67. CodeGenTBAA::getTBAAInfo(QualType QTy) {
  68. // At -O0 TBAA is not emitted for regular types.
  69. if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
  70. return NULL;
  71. // If the type has the may_alias attribute (even on a typedef), it is
  72. // effectively in the general char alias class.
  73. if (TypeHasMayAlias(QTy))
  74. return getChar();
  75. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  76. if (llvm::MDNode *N = MetadataCache[Ty])
  77. return N;
  78. // Handle builtin types.
  79. if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
  80. switch (BTy->getKind()) {
  81. // Character types are special and can alias anything.
  82. // In C++, this technically only includes "char" and "unsigned char",
  83. // and not "signed char". In C, it includes all three. For now,
  84. // the risk of exploiting this detail in C++ seems likely to outweigh
  85. // the benefit.
  86. case BuiltinType::Char_U:
  87. case BuiltinType::Char_S:
  88. case BuiltinType::UChar:
  89. case BuiltinType::SChar:
  90. return getChar();
  91. // Unsigned types can alias their corresponding signed types.
  92. case BuiltinType::UShort:
  93. return getTBAAInfo(Context.ShortTy);
  94. case BuiltinType::UInt:
  95. return getTBAAInfo(Context.IntTy);
  96. case BuiltinType::ULong:
  97. return getTBAAInfo(Context.LongTy);
  98. case BuiltinType::ULongLong:
  99. return getTBAAInfo(Context.LongLongTy);
  100. case BuiltinType::UInt128:
  101. return getTBAAInfo(Context.Int128Ty);
  102. // Treat all other builtin types as distinct types. This includes
  103. // treating wchar_t, char16_t, and char32_t as distinct from their
  104. // "underlying types".
  105. default:
  106. return MetadataCache[Ty] =
  107. MDHelper.createTBAANode(BTy->getName(Features), getChar());
  108. }
  109. }
  110. // Handle pointers.
  111. // TODO: Implement C++'s type "similarity" and consider dis-"similar"
  112. // pointers distinct.
  113. if (Ty->isPointerType())
  114. return MetadataCache[Ty] = MDHelper.createTBAANode("any pointer",
  115. getChar());
  116. // Enum types are distinct types. In C++ they have "underlying types",
  117. // however they aren't related for TBAA.
  118. if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
  119. // In C mode, two anonymous enums are compatible iff their members
  120. // are the same -- see C99 6.2.7p1. For now, be conservative. We could
  121. // theoretically implement this by combining information about all the
  122. // members into a single identifying MDNode.
  123. if (!Features.CPlusPlus &&
  124. ETy->getDecl()->getTypedefNameForAnonDecl())
  125. return MetadataCache[Ty] = getChar();
  126. // In C++ mode, types have linkage, so we can rely on the ODR and
  127. // on their mangled names, if they're external.
  128. // TODO: Is there a way to get a program-wide unique name for a
  129. // decl with local linkage or no linkage?
  130. if (Features.CPlusPlus &&
  131. ETy->getDecl()->getLinkage() != ExternalLinkage)
  132. return MetadataCache[Ty] = getChar();
  133. // TODO: This is using the RTTI name. Is there a better way to get
  134. // a unique string for a type?
  135. SmallString<256> OutName;
  136. llvm::raw_svector_ostream Out(OutName);
  137. MContext.mangleCXXRTTIName(QualType(ETy, 0), Out);
  138. Out.flush();
  139. return MetadataCache[Ty] = MDHelper.createTBAANode(OutName, getChar());
  140. }
  141. // For now, handle any other kind of type conservatively.
  142. return MetadataCache[Ty] = getChar();
  143. }
  144. llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
  145. return MDHelper.createTBAANode("vtable pointer", getRoot());
  146. }