CodeGenTBAA.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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/Attr.h"
  20. #include "clang/AST/Mangle.h"
  21. #include "clang/AST/RecordLayout.h"
  22. #include "clang/Frontend/CodeGenOptions.h"
  23. #include "llvm/ADT/SmallSet.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/LLVMContext.h"
  26. #include "llvm/IR/Metadata.h"
  27. #include "llvm/IR/Type.h"
  28. using namespace clang;
  29. using namespace CodeGen;
  30. CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::LLVMContext& VMContext,
  31. const CodeGenOptions &CGO,
  32. const LangOptions &Features, MangleContext &MContext)
  33. : Context(Ctx), CodeGenOpts(CGO), Features(Features), MContext(MContext),
  34. MDHelper(VMContext), Root(nullptr), Char(nullptr) {
  35. }
  36. CodeGenTBAA::~CodeGenTBAA() {
  37. }
  38. llvm::MDNode *CodeGenTBAA::getRoot() {
  39. // Define the root of the tree. This identifies the tree, so that
  40. // if our LLVM IR is linked with LLVM IR from a different front-end
  41. // (or a different version of this front-end), their TBAA trees will
  42. // remain distinct, and the optimizer will treat them conservatively.
  43. if (!Root)
  44. Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
  45. return Root;
  46. }
  47. // For both scalar TBAA and struct-path aware TBAA, the scalar type has the
  48. // same format: name, parent node, and offset.
  49. llvm::MDNode *CodeGenTBAA::createTBAAScalarType(StringRef Name,
  50. llvm::MDNode *Parent) {
  51. return MDHelper.createTBAAScalarTypeNode(Name, Parent);
  52. }
  53. llvm::MDNode *CodeGenTBAA::getChar() {
  54. // Define the root of the tree for user-accessible memory. C and C++
  55. // give special powers to char and certain similar types. However,
  56. // these special powers only cover user-accessible memory, and doesn't
  57. // include things like vtables.
  58. if (!Char)
  59. Char = createTBAAScalarType("omnipotent char", getRoot());
  60. return Char;
  61. }
  62. static bool TypeHasMayAlias(QualType QTy) {
  63. // Tagged types have declarations, and therefore may have attributes.
  64. if (const TagType *TTy = dyn_cast<TagType>(QTy))
  65. return TTy->getDecl()->hasAttr<MayAliasAttr>();
  66. // Typedef types have declarations, and therefore may have attributes.
  67. if (const TypedefType *TTy = dyn_cast<TypedefType>(QTy)) {
  68. if (TTy->getDecl()->hasAttr<MayAliasAttr>())
  69. return true;
  70. // Also, their underlying types may have relevant attributes.
  71. return TypeHasMayAlias(TTy->desugar());
  72. }
  73. return false;
  74. }
  75. llvm::MDNode *
  76. CodeGenTBAA::getTBAAInfo(QualType QTy) {
  77. // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
  78. if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
  79. return nullptr;
  80. // If the type has the may_alias attribute (even on a typedef), it is
  81. // effectively in the general char alias class.
  82. if (TypeHasMayAlias(QTy))
  83. return getChar();
  84. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  85. if (llvm::MDNode *N = MetadataCache[Ty])
  86. return N;
  87. // Handle builtin types.
  88. if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
  89. switch (BTy->getKind()) {
  90. // Character types are special and can alias anything.
  91. // In C++, this technically only includes "char" and "unsigned char",
  92. // and not "signed char". In C, it includes all three. For now,
  93. // the risk of exploiting this detail in C++ seems likely to outweigh
  94. // the benefit.
  95. case BuiltinType::Char_U:
  96. case BuiltinType::Char_S:
  97. case BuiltinType::UChar:
  98. case BuiltinType::SChar:
  99. return getChar();
  100. // Unsigned types can alias their corresponding signed types.
  101. case BuiltinType::UShort:
  102. return getTBAAInfo(Context.ShortTy);
  103. case BuiltinType::UInt:
  104. return getTBAAInfo(Context.IntTy);
  105. case BuiltinType::ULong:
  106. return getTBAAInfo(Context.LongTy);
  107. case BuiltinType::ULongLong:
  108. return getTBAAInfo(Context.LongLongTy);
  109. case BuiltinType::UInt128:
  110. return getTBAAInfo(Context.Int128Ty);
  111. // Treat all other builtin types as distinct types. This includes
  112. // treating wchar_t, char16_t, and char32_t as distinct from their
  113. // "underlying types".
  114. default:
  115. return MetadataCache[Ty] =
  116. createTBAAScalarType(BTy->getName(Features), getChar());
  117. }
  118. }
  119. // Handle pointers.
  120. // TODO: Implement C++'s type "similarity" and consider dis-"similar"
  121. // pointers distinct.
  122. if (Ty->isPointerType())
  123. return MetadataCache[Ty] = createTBAAScalarType("any pointer",
  124. getChar());
  125. // Enum types are distinct types. In C++ they have "underlying types",
  126. // however they aren't related for TBAA.
  127. if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
  128. // In C++ mode, types have linkage, so we can rely on the ODR and
  129. // on their mangled names, if they're external.
  130. // TODO: Is there a way to get a program-wide unique name for a
  131. // decl with local linkage or no linkage?
  132. if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
  133. return MetadataCache[Ty] = getChar();
  134. SmallString<256> OutName;
  135. llvm::raw_svector_ostream Out(OutName);
  136. MContext.mangleTypeName(QualType(ETy, 0), Out);
  137. return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
  138. }
  139. // For now, handle any other kind of type conservatively.
  140. return MetadataCache[Ty] = getChar();
  141. }
  142. llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
  143. return createTBAAScalarType("vtable pointer", getRoot());
  144. }
  145. bool
  146. CodeGenTBAA::CollectFields(uint64_t BaseOffset,
  147. QualType QTy,
  148. SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
  149. Fields,
  150. bool MayAlias) {
  151. /* Things not handled yet include: C++ base classes, bitfields, */
  152. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  153. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  154. if (RD->hasFlexibleArrayMember())
  155. return false;
  156. // TODO: Handle C++ base classes.
  157. if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
  158. if (Decl->bases_begin() != Decl->bases_end())
  159. return false;
  160. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  161. unsigned idx = 0;
  162. for (RecordDecl::field_iterator i = RD->field_begin(),
  163. e = RD->field_end(); i != e; ++i, ++idx) {
  164. uint64_t Offset = BaseOffset +
  165. Layout.getFieldOffset(idx) / Context.getCharWidth();
  166. QualType FieldQTy = i->getType();
  167. if (!CollectFields(Offset, FieldQTy, Fields,
  168. MayAlias || TypeHasMayAlias(FieldQTy)))
  169. return false;
  170. }
  171. return true;
  172. }
  173. /* Otherwise, treat whatever it is as a field. */
  174. uint64_t Offset = BaseOffset;
  175. uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
  176. llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
  177. llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
  178. Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
  179. return true;
  180. }
  181. llvm::MDNode *
  182. CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
  183. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  184. if (llvm::MDNode *N = StructMetadataCache[Ty])
  185. return N;
  186. SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
  187. if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
  188. return MDHelper.createTBAAStructNode(Fields);
  189. // For now, handle any other kind of type conservatively.
  190. return StructMetadataCache[Ty] = nullptr;
  191. }
  192. /// Check if the given type can be handled by path-aware TBAA.
  193. static bool isTBAAPathStruct(QualType QTy) {
  194. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  195. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  196. if (RD->hasFlexibleArrayMember())
  197. return false;
  198. // RD can be struct, union, class, interface or enum.
  199. // For now, we only handle struct and class.
  200. if (RD->isStruct() || RD->isClass())
  201. return true;
  202. }
  203. return false;
  204. }
  205. llvm::MDNode *
  206. CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
  207. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  208. assert(isTBAAPathStruct(QTy));
  209. if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
  210. return N;
  211. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  212. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  213. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  214. SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
  215. unsigned idx = 0;
  216. for (RecordDecl::field_iterator i = RD->field_begin(),
  217. e = RD->field_end(); i != e; ++i, ++idx) {
  218. QualType FieldQTy = i->getType();
  219. llvm::MDNode *FieldNode;
  220. if (isTBAAPathStruct(FieldQTy))
  221. FieldNode = getTBAAStructTypeInfo(FieldQTy);
  222. else
  223. FieldNode = getTBAAInfo(FieldQTy);
  224. if (!FieldNode)
  225. return StructTypeMetadataCache[Ty] = nullptr;
  226. Fields.push_back(std::make_pair(
  227. FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
  228. }
  229. SmallString<256> OutName;
  230. if (Features.CPlusPlus) {
  231. // Don't use the mangler for C code.
  232. llvm::raw_svector_ostream Out(OutName);
  233. MContext.mangleTypeName(QualType(Ty, 0), Out);
  234. } else {
  235. OutName = RD->getName();
  236. }
  237. // Create the struct type node with a vector of pairs (offset, type).
  238. return StructTypeMetadataCache[Ty] =
  239. MDHelper.createTBAAStructTypeNode(OutName, Fields);
  240. }
  241. return StructMetadataCache[Ty] = nullptr;
  242. }
  243. /// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
  244. llvm::MDNode *
  245. CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
  246. uint64_t Offset) {
  247. if (!AccessNode)
  248. return nullptr;
  249. if (!CodeGenOpts.StructPathTBAA)
  250. return getTBAAScalarTagInfo(AccessNode);
  251. const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
  252. TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
  253. if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
  254. return N;
  255. llvm::MDNode *BNode = nullptr;
  256. if (isTBAAPathStruct(BaseQTy))
  257. BNode = getTBAAStructTypeInfo(BaseQTy);
  258. if (!BNode)
  259. return StructTagMetadataCache[PathTag] =
  260. MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
  261. return StructTagMetadataCache[PathTag] =
  262. MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
  263. }
  264. llvm::MDNode *
  265. CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
  266. if (!AccessNode)
  267. return nullptr;
  268. if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
  269. return N;
  270. return ScalarTagMetadataCache[AccessNode] =
  271. MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
  272. }