CodeGenTBAA.cpp 6.5 KB

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