CodeGenTBAA.cpp 6.5 KB

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