CodeGenTBAA.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. SmallVector<llvm::MDBuilder::TBAAStructField, 4> Fields;
  293. for (FieldDecl *Field : RD->fields()) {
  294. if (Field->isZeroSize(Context) || Field->isUnnamedBitfield())
  295. continue;
  296. QualType FieldQTy = Field->getType();
  297. llvm::MDNode *TypeNode = isValidBaseType(FieldQTy) ?
  298. getBaseTypeInfo(FieldQTy) : getTypeInfo(FieldQTy);
  299. if (!TypeNode)
  300. return BaseTypeMetadataCache[Ty] = nullptr;
  301. uint64_t BitOffset = Layout.getFieldOffset(Field->getFieldIndex());
  302. uint64_t Offset = Context.toCharUnitsFromBits(BitOffset).getQuantity();
  303. uint64_t Size = Context.getTypeSizeInChars(FieldQTy).getQuantity();
  304. Fields.push_back(llvm::MDBuilder::TBAAStructField(Offset, Size,
  305. TypeNode));
  306. }
  307. SmallString<256> OutName;
  308. if (Features.CPlusPlus) {
  309. // Don't use the mangler for C code.
  310. llvm::raw_svector_ostream Out(OutName);
  311. MContext.mangleTypeName(QualType(Ty, 0), Out);
  312. } else {
  313. OutName = RD->getName();
  314. }
  315. if (CodeGenOpts.NewStructPathTBAA) {
  316. llvm::MDNode *Parent = getChar();
  317. uint64_t Size = Context.getTypeSizeInChars(Ty).getQuantity();
  318. llvm::Metadata *Id = MDHelper.createString(OutName);
  319. return MDHelper.createTBAATypeNode(Parent, Size, Id, Fields);
  320. }
  321. // Create the struct type node with a vector of pairs (offset, type).
  322. SmallVector<std::pair<llvm::MDNode*, uint64_t>, 4> OffsetsAndTypes;
  323. for (const auto &Field : Fields)
  324. OffsetsAndTypes.push_back(std::make_pair(Field.Type, Field.Offset));
  325. return MDHelper.createTBAAStructTypeNode(OutName, OffsetsAndTypes);
  326. }
  327. return nullptr;
  328. }
  329. llvm::MDNode *CodeGenTBAA::getBaseTypeInfo(QualType QTy) {
  330. if (!isValidBaseType(QTy))
  331. return nullptr;
  332. const Type *Ty = Context.getCanonicalType(QTy).getTypePtr();
  333. if (llvm::MDNode *N = BaseTypeMetadataCache[Ty])
  334. return N;
  335. // Note that the following helper call is allowed to add new nodes to the
  336. // cache, which invalidates all its previously obtained iterators. So we
  337. // first generate the node for the type and then add that node to the cache.
  338. llvm::MDNode *TypeNode = getBaseTypeInfoHelper(Ty);
  339. return BaseTypeMetadataCache[Ty] = TypeNode;
  340. }
  341. llvm::MDNode *CodeGenTBAA::getAccessTagInfo(TBAAAccessInfo Info) {
  342. assert(!Info.isIncomplete() && "Access to an object of an incomplete type!");
  343. if (Info.isMayAlias())
  344. Info = TBAAAccessInfo(getChar(), Info.Size);
  345. if (!Info.AccessType)
  346. return nullptr;
  347. if (!CodeGenOpts.StructPathTBAA)
  348. Info = TBAAAccessInfo(Info.AccessType, Info.Size);
  349. llvm::MDNode *&N = AccessTagMetadataCache[Info];
  350. if (N)
  351. return N;
  352. if (!Info.BaseType) {
  353. Info.BaseType = Info.AccessType;
  354. assert(!Info.Offset && "Nonzero offset for an access with no base type!");
  355. }
  356. if (CodeGenOpts.NewStructPathTBAA) {
  357. return N = MDHelper.createTBAAAccessTag(Info.BaseType, Info.AccessType,
  358. Info.Offset, Info.Size);
  359. }
  360. return N = MDHelper.createTBAAStructTagNode(Info.BaseType, Info.AccessType,
  361. Info.Offset);
  362. }
  363. TBAAAccessInfo CodeGenTBAA::mergeTBAAInfoForCast(TBAAAccessInfo SourceInfo,
  364. TBAAAccessInfo TargetInfo) {
  365. if (SourceInfo.isMayAlias() || TargetInfo.isMayAlias())
  366. return TBAAAccessInfo::getMayAliasInfo();
  367. return TargetInfo;
  368. }
  369. TBAAAccessInfo
  370. CodeGenTBAA::mergeTBAAInfoForConditionalOperator(TBAAAccessInfo InfoA,
  371. TBAAAccessInfo InfoB) {
  372. if (InfoA == InfoB)
  373. return InfoA;
  374. if (!InfoA || !InfoB)
  375. return TBAAAccessInfo();
  376. if (InfoA.isMayAlias() || InfoB.isMayAlias())
  377. return TBAAAccessInfo::getMayAliasInfo();
  378. // TODO: Implement the rest of the logic here. For example, two accesses
  379. // with same final access types result in an access to an object of that final
  380. // access type regardless of their base types.
  381. return TBAAAccessInfo::getMayAliasInfo();
  382. }
  383. TBAAAccessInfo
  384. CodeGenTBAA::mergeTBAAInfoForMemoryTransfer(TBAAAccessInfo DestInfo,
  385. TBAAAccessInfo SrcInfo) {
  386. if (DestInfo == SrcInfo)
  387. return DestInfo;
  388. if (!DestInfo || !SrcInfo)
  389. return TBAAAccessInfo();
  390. if (DestInfo.isMayAlias() || SrcInfo.isMayAlias())
  391. return TBAAAccessInfo::getMayAliasInfo();
  392. // TODO: Implement the rest of the logic here. For example, two accesses
  393. // with same final access types result in an access to an object of that final
  394. // access type regardless of their base types.
  395. return TBAAAccessInfo::getMayAliasInfo();
  396. }