CodeGenTBAA.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //===-- CodeGenTBAA.cpp - TBAA information for LLVM CodeGen ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is the code that manages TBAA information and defines the TBAA policy
  10. // for the optimizer to use. Relevant standards text includes:
  11. //
  12. // C99 6.5p7
  13. // C++ [basic.lval] (p10 in n3126, p15 in some earlier versions)
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "CodeGenTBAA.h"
  17. #include "clang/AST/ASTContext.h"
  18. #include "clang/AST/Attr.h"
  19. #include "clang/AST/Mangle.h"
  20. #include "clang/AST/RecordLayout.h"
  21. #include "clang/Basic/CodeGenOptions.h"
  22. #include "llvm/ADT/SmallSet.h"
  23. #include "llvm/IR/Constants.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/IR/Metadata.h"
  26. #include "llvm/IR/Module.h"
  27. #include "llvm/IR/Type.h"
  28. using namespace clang;
  29. using namespace CodeGen;
  30. CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M,
  31. const CodeGenOptions &CGO,
  32. const LangOptions &Features, MangleContext &MContext)
  33. : Context(Ctx), Module(M), CodeGenOpts(CGO),
  34. Features(Features), MContext(MContext), MDHelper(M.getContext()),
  35. Root(nullptr), Char(nullptr)
  36. {}
  37. CodeGenTBAA::~CodeGenTBAA() {
  38. }
  39. llvm::MDNode *CodeGenTBAA::getRoot() {
  40. // Define the root of the tree. This identifies the tree, so that
  41. // if our LLVM IR is linked with LLVM IR from a different front-end
  42. // (or a different version of this front-end), their TBAA trees will
  43. // remain distinct, and the optimizer will treat them conservatively.
  44. if (!Root) {
  45. if (Features.CPlusPlus)
  46. Root = MDHelper.createTBAARoot("Simple C++ TBAA");
  47. else
  48. Root = MDHelper.createTBAARoot("Simple C/C++ TBAA");
  49. }
  50. return Root;
  51. }
  52. llvm::MDNode *CodeGenTBAA::createScalarTypeNode(StringRef Name,
  53. llvm::MDNode *Parent,
  54. uint64_t Size) {
  55. if (CodeGenOpts.NewStructPathTBAA) {
  56. llvm::Metadata *Id = MDHelper.createString(Name);
  57. return MDHelper.createTBAATypeNode(Parent, Size, Id);
  58. }
  59. return MDHelper.createTBAAScalarTypeNode(Name, Parent);
  60. }
  61. llvm::MDNode *CodeGenTBAA::getChar() {
  62. // Define the root of the tree for user-accessible memory. C and C++
  63. // give special powers to char and certain similar types. However,
  64. // these special powers only cover user-accessible memory, and doesn't
  65. // include things like vtables.
  66. if (!Char)
  67. Char = createScalarTypeNode("omnipotent char", getRoot(), /* Size= */ 1);
  68. return Char;
  69. }
  70. static bool TypeHasMayAlias(QualType QTy) {
  71. // Tagged types have declarations, and therefore may have attributes.
  72. if (auto *TD = QTy->getAsTagDecl())
  73. if (TD->hasAttr<MayAliasAttr>())
  74. return true;
  75. // Also look for may_alias as a declaration attribute on a typedef.
  76. // FIXME: We should follow GCC and model may_alias as a type attribute
  77. // rather than as a declaration attribute.
  78. while (auto *TT = QTy->getAs<TypedefType>()) {
  79. if (TT->getDecl()->hasAttr<MayAliasAttr>())
  80. return true;
  81. QTy = TT->desugar();
  82. }
  83. return false;
  84. }
  85. /// Check if the given type is a valid base type to be used in access tags.
  86. static bool isValidBaseType(QualType QTy) {
  87. if (QTy->isReferenceType())
  88. return false;
  89. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  90. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  91. // Incomplete types are not valid base access types.
  92. if (!RD)
  93. return false;
  94. if (RD->hasFlexibleArrayMember())
  95. return false;
  96. // RD can be struct, union, class, interface or enum.
  97. // For now, we only handle struct and class.
  98. if (RD->isStruct() || RD->isClass())
  99. return true;
  100. }
  101. return false;
  102. }
  103. llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
  104. uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
  105. // Handle builtin types.
  106. if (const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty)) {
  107. switch (BTy->getKind()) {
  108. // Character types are special and can alias anything.
  109. // In C++, this technically only includes "char" and "unsigned char",
  110. // and not "signed char". In C, it includes all three. For now,
  111. // the risk of exploiting this detail in C++ seems likely to outweigh
  112. // the benefit.
  113. case BuiltinType::Char_U:
  114. case BuiltinType::Char_S:
  115. case BuiltinType::UChar:
  116. case BuiltinType::SChar:
  117. return getChar();
  118. // Unsigned types can alias their corresponding signed types.
  119. case BuiltinType::UShort:
  120. return getTypeInfo(Context.ShortTy);
  121. case BuiltinType::UInt:
  122. return getTypeInfo(Context.IntTy);
  123. case BuiltinType::ULong:
  124. return getTypeInfo(Context.LongTy);
  125. case BuiltinType::ULongLong:
  126. return getTypeInfo(Context.LongLongTy);
  127. case BuiltinType::UInt128:
  128. return getTypeInfo(Context.Int128Ty);
  129. case BuiltinType::UShortFract:
  130. return getTypeInfo(Context.ShortFractTy);
  131. case BuiltinType::UFract:
  132. return getTypeInfo(Context.FractTy);
  133. case BuiltinType::ULongFract:
  134. return getTypeInfo(Context.LongFractTy);
  135. case BuiltinType::SatUShortFract:
  136. return getTypeInfo(Context.SatShortFractTy);
  137. case BuiltinType::SatUFract:
  138. return getTypeInfo(Context.SatFractTy);
  139. case BuiltinType::SatULongFract:
  140. return getTypeInfo(Context.SatLongFractTy);
  141. case BuiltinType::UShortAccum:
  142. return getTypeInfo(Context.ShortAccumTy);
  143. case BuiltinType::UAccum:
  144. return getTypeInfo(Context.AccumTy);
  145. case BuiltinType::ULongAccum:
  146. return getTypeInfo(Context.LongAccumTy);
  147. case BuiltinType::SatUShortAccum:
  148. return getTypeInfo(Context.SatShortAccumTy);
  149. case BuiltinType::SatUAccum:
  150. return getTypeInfo(Context.SatAccumTy);
  151. case BuiltinType::SatULongAccum:
  152. return getTypeInfo(Context.SatLongAccumTy);
  153. // Treat all other builtin types as distinct types. This includes
  154. // treating wchar_t, char16_t, and char32_t as distinct from their
  155. // "underlying types".
  156. default:
  157. return createScalarTypeNode(BTy->getName(Features), getChar(), Size);
  158. }
  159. }
  160. // C++1z [basic.lval]p10: "If a program attempts to access the stored value of
  161. // an object through a glvalue of other than one of the following types the
  162. // behavior is undefined: [...] a char, unsigned char, or std::byte type."
  163. if (Ty->isStdByteType())
  164. return getChar();
  165. // Handle pointers and references.
  166. // TODO: Implement C++'s type "similarity" and consider dis-"similar"
  167. // pointers distinct.
  168. if (Ty->isPointerType() || Ty->isReferenceType())
  169. return createScalarTypeNode("any pointer", getChar(), Size);
  170. // Accesses to arrays are accesses to objects of their element types.
  171. if (CodeGenOpts.NewStructPathTBAA && Ty->isArrayType())
  172. return getTypeInfo(cast<ArrayType>(Ty)->getElementType());
  173. // Enum types are distinct types. In C++ they have "underlying types",
  174. // however they aren't related for TBAA.
  175. if (const EnumType *ETy = dyn_cast<EnumType>(Ty)) {
  176. // In C++ mode, types have linkage, so we can rely on the ODR and
  177. // on their mangled names, if they're external.
  178. // TODO: Is there a way to get a program-wide unique name for a
  179. // decl with local linkage or no linkage?
  180. if (!Features.CPlusPlus || !ETy->getDecl()->isExternallyVisible())
  181. return getChar();
  182. SmallString<256> OutName;
  183. llvm::raw_svector_ostream Out(OutName);
  184. MContext.mangleTypeName(QualType(ETy, 0), Out);
  185. return createScalarTypeNode(OutName, getChar(), Size);
  186. }
  187. if (const auto *EIT = dyn_cast<BitIntType>(Ty)) {
  188. SmallString<256> OutName;
  189. llvm::raw_svector_ostream Out(OutName);
  190. // Don't specify signed/unsigned since integer types can alias despite sign
  191. // differences.
  192. Out << "_BitInt(" << EIT->getNumBits() << ')';
  193. return createScalarTypeNode(OutName, getChar(), Size);
  194. }
  195. // For now, handle any other kind of type conservatively.
  196. return getChar();
  197. }
  198. llvm::MDNode *CodeGenTBAA::getTypeInfo(QualType QTy) {
  199. // At -O0 or relaxed aliasing, TBAA is not emitted for regular types.
  200. if (CodeGenOpts.OptimizationLevel == 0 || CodeGenOpts.RelaxedAliasing)
  201. return nullptr;
  202. // If the type has the may_alias attribute (even on a typedef), it is
  203. // effectively in the general char alias class.
  204. if (TypeHasMayAlias(QTy))
  205. return getChar();
  206. // We need this function to not fall back to returning the "omnipotent char"
  207. // type node for aggregate and union types. Otherwise, any dereference of an
  208. // aggregate will result into the may-alias access descriptor, meaning all
  209. // subsequent accesses to direct and indirect members of that aggregate will
  210. // be considered may-alias too.
  211. // TODO: Combine getTypeInfo() and getBaseTypeInfo() into a single function.
  212. if (isValidBaseType(QTy))
  213. return getBaseTypeInfo(QTy);
  214. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  215. if (llvm::MDNode *N = MetadataCache[Ty])
  216. return N;
  217. // Note that the following helper call is allowed to add new nodes to the
  218. // cache, which invalidates all its previously obtained iterators. So we
  219. // first generate the node for the type and then add that node to the cache.
  220. llvm::MDNode *TypeNode = getTypeInfoHelper(Ty);
  221. return MetadataCache[Ty] = TypeNode;
  222. }
  223. TBAAAccessInfo CodeGenTBAA::getAccessInfo(QualType AccessType) {
  224. // Pointee values may have incomplete types, but they shall never be
  225. // dereferenced.
  226. if (AccessType->isIncompleteType())
  227. return TBAAAccessInfo::getIncompleteInfo();
  228. if (TypeHasMayAlias(AccessType))
  229. return TBAAAccessInfo::getMayAliasInfo();
  230. uint64_t Size = Context.getTypeSizeInChars(AccessType).getQuantity();
  231. return TBAAAccessInfo(getTypeInfo(AccessType), Size);
  232. }
  233. TBAAAccessInfo CodeGenTBAA::getVTablePtrAccessInfo(llvm::Type *VTablePtrType) {
  234. llvm::DataLayout DL(&Module);
  235. unsigned Size = DL.getPointerTypeSize(VTablePtrType);
  236. return TBAAAccessInfo(createScalarTypeNode("vtable pointer", getRoot(), Size),
  237. Size);
  238. }
  239. bool
  240. CodeGenTBAA::CollectFields(uint64_t BaseOffset,
  241. QualType QTy,
  242. SmallVectorImpl<llvm::MDBuilder::TBAAStructField> &
  243. Fields,
  244. bool MayAlias) {
  245. /* Things not handled yet include: C++ base classes, bitfields, */
  246. if (const RecordType *TTy = QTy->getAs<RecordType>()) {
  247. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  248. if (RD->hasFlexibleArrayMember())
  249. return false;
  250. // TODO: Handle C++ base classes.
  251. if (const CXXRecordDecl *Decl = dyn_cast<CXXRecordDecl>(RD))
  252. if (Decl->bases_begin() != Decl->bases_end())
  253. return false;
  254. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  255. unsigned idx = 0;
  256. for (RecordDecl::field_iterator i = RD->field_begin(),
  257. e = RD->field_end(); i != e; ++i, ++idx) {
  258. if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield())
  259. continue;
  260. uint64_t Offset = BaseOffset +
  261. Layout.getFieldOffset(idx) / Context.getCharWidth();
  262. QualType FieldQTy = i->getType();
  263. if (!CollectFields(Offset, FieldQTy, Fields,
  264. MayAlias || TypeHasMayAlias(FieldQTy)))
  265. return false;
  266. }
  267. return true;
  268. }
  269. /* Otherwise, treat whatever it is as a field. */
  270. uint64_t Offset = BaseOffset;
  271. uint64_t Size = Context.getTypeSizeInChars(QTy).getQuantity();
  272. llvm::MDNode *TBAAType = MayAlias ? getChar() : getTypeInfo(QTy);
  273. llvm::MDNode *TBAATag = getAccessTagInfo(TBAAAccessInfo(TBAAType, Size));
  274. Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag));
  275. return true;
  276. }
  277. llvm::MDNode *
  278. CodeGenTBAA::getTBAAStructInfo(QualType QTy) {
  279. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  280. if (llvm::MDNode *N = StructMetadataCache[Ty])
  281. return N;
  282. SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
  283. if (CollectFields(0, QTy, Fields, TypeHasMayAlias(QTy)))
  284. return MDHelper.createTBAAStructNode(Fields);
  285. // For now, handle any other kind of type conservatively.
  286. return StructMetadataCache[Ty] = nullptr;
  287. }
  288. llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
  289. if (auto *TTy = dyn_cast<RecordType>(Ty)) {
  290. const RecordDecl *RD = TTy->getDecl()->getDefinition();
  291. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
  292. using TBAAStructField = llvm::MDBuilder::TBAAStructField;
  293. SmallVector<TBAAStructField, 4> Fields;
  294. if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
  295. // Handle C++ base classes. Non-virtual bases can treated a kind of
  296. // field. Virtual bases are more complex and omitted, but avoid an
  297. // incomplete view for NewStructPathTBAA.
  298. if (CodeGenOpts.NewStructPathTBAA && CXXRD->getNumVBases() != 0)
  299. return BaseTypeMetadataCache[Ty] = nullptr;
  300. for (const CXXBaseSpecifier &B : CXXRD->bases()) {
  301. if (B.isVirtual())
  302. continue;
  303. QualType BaseQTy = B.getType();
  304. const CXXRecordDecl *BaseRD = BaseQTy->getAsCXXRecordDecl();
  305. if (BaseRD->isEmpty())
  306. continue;
  307. llvm::MDNode *TypeNode = isValidBaseType(BaseQTy)
  308. ? getBaseTypeInfo(BaseQTy)
  309. : getTypeInfo(BaseQTy);
  310. if (!TypeNode)
  311. return BaseTypeMetadataCache[Ty] = nullptr;
  312. uint64_t Offset = Layout.getBaseClassOffset(BaseRD).getQuantity();
  313. uint64_t Size =
  314. Context.getASTRecordLayout(BaseRD).getDataSize().getQuantity();
  315. Fields.push_back(
  316. llvm::MDBuilder::TBAAStructField(Offset, Size, TypeNode));
  317. }
  318. // The order in which base class subobjects are allocated is unspecified,
  319. // so may differ from declaration order. In particular, Itanium ABI will
  320. // allocate a primary base first.
  321. // Since we exclude empty subobjects, the objects are not overlapping and
  322. // their offsets are unique.
  323. llvm::sort(Fields,
  324. [](const TBAAStructField &A, const TBAAStructField &B) {
  325. return A.Offset < B.Offset;
  326. });
  327. }
  328. for (FieldDecl *Field : RD->fields()) {
  329. if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
  330. continue;
  331. QualType FieldQTy = Field->getType();
  332. llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
  333. getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
  334. if (!TypeNode)
  335. return BaseTypeMetadataCache[Ty] = nullptr;
  336. uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
  337. uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
  338. uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
  339. Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
  340. TypeNode));
  341. }
  342. SmallString<256> OutName;
  343. if (Features.CPlusPlus) {
  344. // Don't use the mangler for C code.
  345. llvm::raw_svector_ostream Out(OutName);
  346. MContext.mangleTypeName(QualType(Ty, 0), Out);
  347. } else {
  348. OutName = RD->getName();
  349. }
  350. if (CodeGenOpts.NewStructPathTBAA) {
  351. llvm::MDNode *Parent = getChar();
  352. uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
  353. llvm::Metadata *Id = MDHelper.createString(OutName);
  354. return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
  355. }
  356. // Create the struct type node with a vector of pairs (offset, type).
  357. SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
  358. for (const auto &Field : Fields)
  359. OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
  360. return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
  361. }
  362. return nullptr;
  363. }
  364. llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
  365. if (!isValidBaseType(QTy))
  366. return nullptr;
  367. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  368. if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
  369. return N;
  370. // Note that the following helper call is allowed to add new nodes to the
  371. // cache, which invalidates all its previously obtained iterators. So we
  372. // first generate the node for the type and then add that node to the cache.
  373. llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
  374. return BaseTypeMetadataCache[Ty] = TypeNode;
  375. }
  376. llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
  377. assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
  378. if (Info.isMayAlias())
  379. Info = TBAAAccessInfo(getChar(), Info.Size);
  380. if (!Info.AccessType)
  381. return nullptr;
  382. if (!CodeGenOpts.StructPathTBAA)
  383. Info = TBAAAccessInfo(Info.AccessType, Info.Size);
  384. llvm::MDNode *&N = AccessTagMetadataCache[Info];
  385. if (N)
  386. return N;
  387. if (!Info.BaseType) {
  388. Info.BaseType = Info.AccessType;
  389. assert(!Info.Offset && "Nonzero offset for an access with no base type!");
  390. }
  391. if (CodeGenOpts.NewStructPathTBAA) {
  392. return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
  393. Info.Offset, Info.Size);
  394. }
  395. return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
  396. Info.Offset);
  397. }
  398. TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
  399. TBAAAccessInfo TargetInfo) {
  400. if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
  401. return TBAAAccessInfo::getMayAliasInfo();
  402. return TargetInfo;
  403. }
  404. TBAAAccessInfo
  405. CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
  406. TBAAAccessInfo InfoB) {
  407. if (InfoA == InfoB)
  408. return InfoA;
  409. if (!InfoA || !InfoB)
  410. return TBAAAccessInfo();
  411. if (InfoA.isMayAlias() || InfoB.isMayAlias())
  412. return TBAAAccessInfo::getMayAliasInfo();
  413. // TODO: Implement the rest of the logic here. For example, two accesses
  414. // with same final access types result in an access to an object of that final
  415. // access type regardless of their base types.
  416. return TBAAAccessInfo::getMayAliasInfo();
  417. }
  418. TBAAAccessInfo
  419. CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
  420. TBAAAccessInfo SrcInfo) {
  421. if (DestInfo == SrcInfo)
  422. return DestInfo;
  423. if (!DestInfo || !SrcInfo)
  424. return TBAAAccessInfo();
  425. if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
  426. return TBAAAccessInfo::getMayAliasInfo();
  427. // TODO: Implement the rest of the logic here. For example, two accesses
  428. // with same final access types result in an access to an object of that final
  429. // access type regardless of their base types.
  430. return TBAAAccessInfo::getMayAliasInfo();
  431. }