CodeGenTBAA.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. Out.flush();
  138. return MetadataCache[Ty] = createTBAAScalarType(OutName, getChar());
  139. }
  140. // For now, handle any other kind of type conservatively.
  141. return MetadataCache[Ty] = getChar();
  142. }
  143. llvm::MDNode *CodeGenTBAA::getTBAAInfoForVTablePtr() {
  144. return createTBAAScalarType("vtable pointer", getRoot());
  145. }
  146. bool
  147. CodeGenTBAA::CollectFields(uint64_t BaseOffset,
  148. QualType QTy,
  149. SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
  150. Fields,
  151. bool MayAlias) {
  152. /* Things not handled yet include: C++ base classes, bitfields, */
  153. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  154. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  155. if (RD->hasFlexibleArrayMember())
  156. return false;
  157. // TODO: Handle C++ base classes.
  158. if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
  159. if (Decl->bases_begin() != Decl->bases_end())
  160. return false;
  161. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  162. unsigned idx = 0;
  163. for (RecordDecl::field_iterator i = RD->field_begin(),
  164. e = RD->field_end(); i != e; ++i, ++idx) {
  165. uint64_t Offset = BaseOffset +
  166. Layout.getFieldOffset(idx) / Context.getCharWidth();
  167. QualType FieldQTy = i->getType();
  168. if (!CollectFields(Offset, FieldQTy, Fields,
  169. MayAlias || TypeHasMayAlias(FieldQTy)))
  170. return false;
  171. }
  172. return true;
  173. }
  174. /* Otherwise, treat whatever it is as a field. */
  175. uint64_t Offset = BaseOffset;
  176. uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
  177. llvm::MDNode *TBAAInfo = MayAlias ? getChar() : getTBAAInfo(QTy);
  178. llvm::MDNode *TBAATag = getTBAAScalarTagInfo(TBAAInfo);
  179. Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
  180. return true;
  181. }
  182. llvm::MDNode *
  183. CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
  184. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  185. if (llvm::MDNode *N = StructMetadataCache[Ty])
  186. return N;
  187. SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
  188. if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
  189. return MDHelper.createTBAAStructNode(Fields);
  190. // For now, handle any other kind of type conservatively.
  191. return StructMetadataCache[Ty] = nullptr;
  192. }
  193. /// Check if the given type can be handled by path-aware TBAA.
  194. static bool isTBAAPathStruct(QualType QTy) {
  195. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  196. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  197. if (RD->hasFlexibleArrayMember())
  198. return false;
  199. // RD can be struct, union, class, interface or enum.
  200. // For now, we only handle struct and class.
  201. if (RD->isStruct() || RD->isClass())
  202. return true;
  203. }
  204. return false;
  205. }
  206. llvm::MDNode *
  207. CodeGenTBAA::getTBAAStructTypeInfo(QualType QTy) {
  208. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  209. assert(isTBAAPathStruct(QTy));
  210. if (llvm::MDNode *N = StructTypeMetadataCache[Ty])
  211. return N;
  212. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  213. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  214. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  215. SmallVector <std::pair<llvm::MDNode*, uint64_t>, 4> Fields;
  216. unsigned idx = 0;
  217. for (RecordDecl::field_iterator i = RD->field_begin(),
  218. e = RD->field_end(); i != e; ++i, ++idx) {
  219. QualType FieldQTy = i->getType();
  220. llvm::MDNode *FieldNode;
  221. if (isTBAAPathStruct(FieldQTy))
  222. FieldNode = getTBAAStructTypeInfo(FieldQTy);
  223. else
  224. FieldNode = getTBAAInfo(FieldQTy);
  225. if (!FieldNode)
  226. return StructTypeMetadataCache[Ty] = nullptr;
  227. Fields.push_back(std::make_pair(
  228. FieldNode, Layout.getFieldOffset(idx) / Context.getCharWidth()));
  229. }
  230. SmallString<256> OutName;
  231. if (Features.CPlusPlus) {
  232. // Don't use the mangler for C code.
  233. llvm::raw_svector_ostream Out(OutName);
  234. MContext.mangleTypeName(QualType(Ty, 0), Out);
  235. Out.flush();
  236. } else {
  237. OutName = RD->getName();
  238. }
  239. // Create the struct type node with a vector of pairs (offset, type).
  240. return StructTypeMetadataCache[Ty] =
  241. MDHelper.createTBAAStructTypeNode(OutName, Fields);
  242. }
  243. return StructMetadataCache[Ty] = nullptr;
  244. }
  245. /// Return a TBAA tag node for both scalar TBAA and struct-path aware TBAA.
  246. llvm::MDNode *
  247. CodeGenTBAA::getTBAAStructTagInfo(QualType BaseQTy, llvm::MDNode *AccessNode,
  248. uint64_t Offset) {
  249. if (!AccessNode)
  250. return nullptr;
  251. if (!CodeGenOpts.StructPathTBAA)
  252. return getTBAAScalarTagInfo(AccessNode);
  253. const Type *BTy = Context.getCanonicalType(BaseQTy).getTypePtr();
  254. TBAAPathTag PathTag = TBAAPathTag(BTy, AccessNode, Offset);
  255. if (llvm::MDNode *N = StructTagMetadataCache[PathTag])
  256. return N;
  257. llvm::MDNode *BNode = nullptr;
  258. if (isTBAAPathStruct(BaseQTy))
  259. BNode = getTBAAStructTypeInfo(BaseQTy);
  260. if (!BNode)
  261. return StructTagMetadataCache[PathTag] =
  262. MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
  263. return StructTagMetadataCache[PathTag] =
  264. MDHelper.createTBAAStructTagNode(BNode, AccessNode, Offset);
  265. }
  266. llvm::MDNode *
  267. CodeGenTBAA::getTBAAScalarTagInfo(llvm::MDNode *AccessNode) {
  268. if (!AccessNode)
  269. return nullptr;
  270. if (llvm::MDNode *N = ScalarTagMetadataCache[AccessNode])
  271. return N;
  272. return ScalarTagMetadataCache[AccessNode] =
  273. MDHelper.createTBAAStructTagNode(AccessNode, AccessNode, 0);
  274. }