CodeGenTBAA.cpp 6.1 KB

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