CodeGenTBAA.cpp 12 KB

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