LLVMContextImpl.h 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  1. //===- LLVMContextImpl.h - The LLVMContextImpl opaque class -----*- C++ -*-===//
  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 file declares LLVMContextImpl, the opaque implementation
  10. // of LLVMContext.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_LIB_IR_LLVMCONTEXTIMPL_H
  14. #define LLVM_LIB_IR_LLVMCONTEXTIMPL_H
  15. #include "ConstantsContext.h"
  16. #include "llvm/ADT/APFloat.h"
  17. #include "llvm/ADT/APInt.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/DenseMapInfo.h"
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/ADT/FoldingSet.h"
  23. #include "llvm/ADT/Hashing.h"
  24. #include "llvm/ADT/STLExtras.h"
  25. #include "llvm/ADT/SmallPtrSet.h"
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/StringMap.h"
  28. #include "llvm/BinaryFormat/Dwarf.h"
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/DebugInfoMetadata.h"
  31. #include "llvm/IR/DerivedTypes.h"
  32. #include "llvm/IR/LLVMContext.h"
  33. #include "llvm/IR/Metadata.h"
  34. #include "llvm/IR/Module.h"
  35. #include "llvm/IR/TrackingMDRef.h"
  36. #include "llvm/IR/Type.h"
  37. #include "llvm/IR/Value.h"
  38. #include "llvm/Support/Allocator.h"
  39. #include "llvm/Support/Casting.h"
  40. #include "llvm/Support/StringSaver.h"
  41. #include <algorithm>
  42. #include <cassert>
  43. #include <cstddef>
  44. #include <cstdint>
  45. #include <memory>
  46. #include <optional>
  47. #include <string>
  48. #include <utility>
  49. #include <vector>
  50. namespace llvm {
  51. class AttributeImpl;
  52. class AttributeListImpl;
  53. class AttributeSetNode;
  54. class BasicBlock;
  55. struct DiagnosticHandler;
  56. class ElementCount;
  57. class Function;
  58. class GlobalObject;
  59. class GlobalValue;
  60. class InlineAsm;
  61. class LLVMRemarkStreamer;
  62. class OptPassGate;
  63. namespace remarks {
  64. class RemarkStreamer;
  65. }
  66. template <typename T> class StringMapEntry;
  67. class StringRef;
  68. class TypedPointerType;
  69. class ValueHandleBase;
  70. using DenseMapAPIntKeyInfo = DenseMapInfo<APInt>;
  71. struct DenseMapAPFloatKeyInfo {
  72. static inline APFloat getEmptyKey() { return APFloat(APFloat::Bogus(), 1); }
  73. static inline APFloat getTombstoneKey() {
  74. return APFloat(APFloat::Bogus(), 2);
  75. }
  76. static unsigned getHashValue(const APFloat &Key) {
  77. return static_cast<unsigned>(hash_value(Key));
  78. }
  79. static bool isEqual(const APFloat &LHS, const APFloat &RHS) {
  80. return LHS.bitwiseIsEqual(RHS);
  81. }
  82. };
  83. struct AnonStructTypeKeyInfo {
  84. struct KeyTy {
  85. ArrayRef<Type *> ETypes;
  86. bool isPacked;
  87. KeyTy(const ArrayRef<Type *> &E, bool P) : ETypes(E), isPacked(P) {}
  88. KeyTy(const StructType *ST)
  89. : ETypes(ST->elements()), isPacked(ST->isPacked()) {}
  90. bool operator==(const KeyTy &that) const {
  91. if (isPacked != that.isPacked)
  92. return false;
  93. if (ETypes != that.ETypes)
  94. return false;
  95. return true;
  96. }
  97. bool operator!=(const KeyTy &that) const { return !this->operator==(that); }
  98. };
  99. static inline StructType *getEmptyKey() {
  100. return DenseMapInfo<StructType *>::getEmptyKey();
  101. }
  102. static inline StructType *getTombstoneKey() {
  103. return DenseMapInfo<StructType *>::getTombstoneKey();
  104. }
  105. static unsigned getHashValue(const KeyTy &Key) {
  106. return hash_combine(
  107. hash_combine_range(Key.ETypes.begin(), Key.ETypes.end()), Key.isPacked);
  108. }
  109. static unsigned getHashValue(const StructType *ST) {
  110. return getHashValue(KeyTy(ST));
  111. }
  112. static bool isEqual(const KeyTy &LHS, const StructType *RHS) {
  113. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  114. return false;
  115. return LHS == KeyTy(RHS);
  116. }
  117. static bool isEqual(const StructType *LHS, const StructType *RHS) {
  118. return LHS == RHS;
  119. }
  120. };
  121. struct FunctionTypeKeyInfo {
  122. struct KeyTy {
  123. const Type *ReturnType;
  124. ArrayRef<Type *> Params;
  125. bool isVarArg;
  126. KeyTy(const Type *R, const ArrayRef<Type *> &P, bool V)
  127. : ReturnType(R), Params(P), isVarArg(V) {}
  128. KeyTy(const FunctionType *FT)
  129. : ReturnType(FT->getReturnType()), Params(FT->params()),
  130. isVarArg(FT->isVarArg()) {}
  131. bool operator==(const KeyTy &that) const {
  132. if (ReturnType != that.ReturnType)
  133. return false;
  134. if (isVarArg != that.isVarArg)
  135. return false;
  136. if (Params != that.Params)
  137. return false;
  138. return true;
  139. }
  140. bool operator!=(const KeyTy &that) const { return !this->operator==(that); }
  141. };
  142. static inline FunctionType *getEmptyKey() {
  143. return DenseMapInfo<FunctionType *>::getEmptyKey();
  144. }
  145. static inline FunctionType *getTombstoneKey() {
  146. return DenseMapInfo<FunctionType *>::getTombstoneKey();
  147. }
  148. static unsigned getHashValue(const KeyTy &Key) {
  149. return hash_combine(
  150. Key.ReturnType,
  151. hash_combine_range(Key.Params.begin(), Key.Params.end()), Key.isVarArg);
  152. }
  153. static unsigned getHashValue(const FunctionType *FT) {
  154. return getHashValue(KeyTy(FT));
  155. }
  156. static bool isEqual(const KeyTy &LHS, const FunctionType *RHS) {
  157. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  158. return false;
  159. return LHS == KeyTy(RHS);
  160. }
  161. static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) {
  162. return LHS == RHS;
  163. }
  164. };
  165. struct TargetExtTypeKeyInfo {
  166. struct KeyTy {
  167. StringRef Name;
  168. ArrayRef<Type *> TypeParams;
  169. ArrayRef<unsigned> IntParams;
  170. KeyTy(StringRef N, const ArrayRef<Type *> &TP, const ArrayRef<unsigned> &IP)
  171. : Name(N), TypeParams(TP), IntParams(IP) {}
  172. KeyTy(const TargetExtType *TT)
  173. : Name(TT->getName()), TypeParams(TT->type_params()),
  174. IntParams(TT->int_params()) {}
  175. bool operator==(const KeyTy &that) const {
  176. return Name == that.Name && TypeParams == that.TypeParams &&
  177. IntParams == that.IntParams;
  178. }
  179. bool operator!=(const KeyTy &that) const { return !this->operator==(that); }
  180. };
  181. static inline TargetExtType *getEmptyKey() {
  182. return DenseMapInfo<TargetExtType *>::getEmptyKey();
  183. }
  184. static inline TargetExtType *getTombstoneKey() {
  185. return DenseMapInfo<TargetExtType *>::getTombstoneKey();
  186. }
  187. static unsigned getHashValue(const KeyTy &Key) {
  188. return hash_combine(
  189. Key.Name,
  190. hash_combine_range(Key.TypeParams.begin(), Key.TypeParams.end()),
  191. hash_combine_range(Key.IntParams.begin(), Key.IntParams.end()));
  192. }
  193. static unsigned getHashValue(const TargetExtType *FT) {
  194. return getHashValue(KeyTy(FT));
  195. }
  196. static bool isEqual(const KeyTy &LHS, const TargetExtType *RHS) {
  197. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  198. return false;
  199. return LHS == KeyTy(RHS);
  200. }
  201. static bool isEqual(const TargetExtType *LHS, const TargetExtType *RHS) {
  202. return LHS == RHS;
  203. }
  204. };
  205. /// Structure for hashing arbitrary MDNode operands.
  206. class MDNodeOpsKey {
  207. ArrayRef<Metadata *> RawOps;
  208. ArrayRef<MDOperand> Ops;
  209. unsigned Hash;
  210. protected:
  211. MDNodeOpsKey(ArrayRef<Metadata *> Ops)
  212. : RawOps(Ops), Hash(calculateHash(Ops)) {}
  213. template <class NodeTy>
  214. MDNodeOpsKey(const NodeTy *N, unsigned Offset = 0)
  215. : Ops(N->op_begin() + Offset, N->op_end()), Hash(N->getHash()) {}
  216. template <class NodeTy>
  217. bool compareOps(const NodeTy *RHS, unsigned Offset = 0) const {
  218. if (getHash() != RHS->getHash())
  219. return false;
  220. assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
  221. return RawOps.empty() ? compareOps(Ops, RHS, Offset)
  222. : compareOps(RawOps, RHS, Offset);
  223. }
  224. static unsigned calculateHash(MDNode *N, unsigned Offset = 0);
  225. private:
  226. template <class T>
  227. static bool compareOps(ArrayRef<T> Ops, const MDNode *RHS, unsigned Offset) {
  228. if (Ops.size() != RHS->getNumOperands() - Offset)
  229. return false;
  230. return std::equal(Ops.begin(), Ops.end(), RHS->op_begin() + Offset);
  231. }
  232. static unsigned calculateHash(ArrayRef<Metadata *> Ops);
  233. public:
  234. unsigned getHash() const { return Hash; }
  235. };
  236. template <class NodeTy> struct MDNodeKeyImpl;
  237. /// Configuration point for MDNodeInfo::isEqual().
  238. template <class NodeTy> struct MDNodeSubsetEqualImpl {
  239. using KeyTy = MDNodeKeyImpl<NodeTy>;
  240. static bool isSubsetEqual(const KeyTy &LHS, const NodeTy *RHS) {
  241. return false;
  242. }
  243. static bool isSubsetEqual(const NodeTy *LHS, const NodeTy *RHS) {
  244. return false;
  245. }
  246. };
  247. /// DenseMapInfo for MDTuple.
  248. ///
  249. /// Note that we don't need the is-function-local bit, since that's implicit in
  250. /// the operands.
  251. template <> struct MDNodeKeyImpl<MDTuple> : MDNodeOpsKey {
  252. MDNodeKeyImpl(ArrayRef<Metadata *> Ops) : MDNodeOpsKey(Ops) {}
  253. MDNodeKeyImpl(const MDTuple *N) : MDNodeOpsKey(N) {}
  254. bool isKeyOf(const MDTuple *RHS) const { return compareOps(RHS); }
  255. unsigned getHashValue() const { return getHash(); }
  256. static unsigned calculateHash(MDTuple *N) {
  257. return MDNodeOpsKey::calculateHash(N);
  258. }
  259. };
  260. /// DenseMapInfo for DILocation.
  261. template <> struct MDNodeKeyImpl<DILocation> {
  262. unsigned Line;
  263. unsigned Column;
  264. Metadata *Scope;
  265. Metadata *InlinedAt;
  266. bool ImplicitCode;
  267. MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
  268. Metadata *InlinedAt, bool ImplicitCode)
  269. : Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt),
  270. ImplicitCode(ImplicitCode) {}
  271. MDNodeKeyImpl(const DILocation *L)
  272. : Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
  273. InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {}
  274. bool isKeyOf(const DILocation *RHS) const {
  275. return Line == RHS->getLine() && Column == RHS->getColumn() &&
  276. Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() &&
  277. ImplicitCode == RHS->isImplicitCode();
  278. }
  279. unsigned getHashValue() const {
  280. return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode);
  281. }
  282. };
  283. /// DenseMapInfo for GenericDINode.
  284. template <> struct MDNodeKeyImpl<GenericDINode> : MDNodeOpsKey {
  285. unsigned Tag;
  286. MDString *Header;
  287. MDNodeKeyImpl(unsigned Tag, MDString *Header, ArrayRef<Metadata *> DwarfOps)
  288. : MDNodeOpsKey(DwarfOps), Tag(Tag), Header(Header) {}
  289. MDNodeKeyImpl(const GenericDINode *N)
  290. : MDNodeOpsKey(N, 1), Tag(N->getTag()), Header(N->getRawHeader()) {}
  291. bool isKeyOf(const GenericDINode *RHS) const {
  292. return Tag == RHS->getTag() && Header == RHS->getRawHeader() &&
  293. compareOps(RHS, 1);
  294. }
  295. unsigned getHashValue() const { return hash_combine(getHash(), Tag, Header); }
  296. static unsigned calculateHash(GenericDINode *N) {
  297. return MDNodeOpsKey::calculateHash(N, 1);
  298. }
  299. };
  300. template <> struct MDNodeKeyImpl<DISubrange> {
  301. Metadata *CountNode;
  302. Metadata *LowerBound;
  303. Metadata *UpperBound;
  304. Metadata *Stride;
  305. MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound,
  306. Metadata *Stride)
  307. : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound),
  308. Stride(Stride) {}
  309. MDNodeKeyImpl(const DISubrange *N)
  310. : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()),
  311. UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {}
  312. bool isKeyOf(const DISubrange *RHS) const {
  313. auto BoundsEqual = [=](Metadata *Node1, Metadata *Node2) -> bool {
  314. if (Node1 == Node2)
  315. return true;
  316. ConstantAsMetadata *MD1 = dyn_cast_or_null<ConstantAsMetadata>(Node1);
  317. ConstantAsMetadata *MD2 = dyn_cast_or_null<ConstantAsMetadata>(Node2);
  318. if (MD1 && MD2) {
  319. ConstantInt *CV1 = cast<ConstantInt>(MD1->getValue());
  320. ConstantInt *CV2 = cast<ConstantInt>(MD2->getValue());
  321. if (CV1->getSExtValue() == CV2->getSExtValue())
  322. return true;
  323. }
  324. return false;
  325. };
  326. return BoundsEqual(CountNode, RHS->getRawCountNode()) &&
  327. BoundsEqual(LowerBound, RHS->getRawLowerBound()) &&
  328. BoundsEqual(UpperBound, RHS->getRawUpperBound()) &&
  329. BoundsEqual(Stride, RHS->getRawStride());
  330. }
  331. unsigned getHashValue() const {
  332. if (CountNode)
  333. if (auto *MD = dyn_cast<ConstantAsMetadata>(CountNode))
  334. return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(),
  335. LowerBound, UpperBound, Stride);
  336. return hash_combine(CountNode, LowerBound, UpperBound, Stride);
  337. }
  338. };
  339. template <> struct MDNodeKeyImpl<DIGenericSubrange> {
  340. Metadata *CountNode;
  341. Metadata *LowerBound;
  342. Metadata *UpperBound;
  343. Metadata *Stride;
  344. MDNodeKeyImpl(Metadata *CountNode, Metadata *LowerBound, Metadata *UpperBound,
  345. Metadata *Stride)
  346. : CountNode(CountNode), LowerBound(LowerBound), UpperBound(UpperBound),
  347. Stride(Stride) {}
  348. MDNodeKeyImpl(const DIGenericSubrange *N)
  349. : CountNode(N->getRawCountNode()), LowerBound(N->getRawLowerBound()),
  350. UpperBound(N->getRawUpperBound()), Stride(N->getRawStride()) {}
  351. bool isKeyOf(const DIGenericSubrange *RHS) const {
  352. return (CountNode == RHS->getRawCountNode()) &&
  353. (LowerBound == RHS->getRawLowerBound()) &&
  354. (UpperBound == RHS->getRawUpperBound()) &&
  355. (Stride == RHS->getRawStride());
  356. }
  357. unsigned getHashValue() const {
  358. auto *MD = dyn_cast_or_null<ConstantAsMetadata>(CountNode);
  359. if (CountNode && MD)
  360. return hash_combine(cast<ConstantInt>(MD->getValue())->getSExtValue(),
  361. LowerBound, UpperBound, Stride);
  362. return hash_combine(CountNode, LowerBound, UpperBound, Stride);
  363. }
  364. };
  365. template <> struct MDNodeKeyImpl<DIEnumerator> {
  366. APInt Value;
  367. MDString *Name;
  368. bool IsUnsigned;
  369. MDNodeKeyImpl(APInt Value, bool IsUnsigned, MDString *Name)
  370. : Value(Value), Name(Name), IsUnsigned(IsUnsigned) {}
  371. MDNodeKeyImpl(int64_t Value, bool IsUnsigned, MDString *Name)
  372. : Value(APInt(64, Value, !IsUnsigned)), Name(Name),
  373. IsUnsigned(IsUnsigned) {}
  374. MDNodeKeyImpl(const DIEnumerator *N)
  375. : Value(N->getValue()), Name(N->getRawName()),
  376. IsUnsigned(N->isUnsigned()) {}
  377. bool isKeyOf(const DIEnumerator *RHS) const {
  378. return Value.getBitWidth() == RHS->getValue().getBitWidth() &&
  379. Value == RHS->getValue() && IsUnsigned == RHS->isUnsigned() &&
  380. Name == RHS->getRawName();
  381. }
  382. unsigned getHashValue() const { return hash_combine(Value, Name); }
  383. };
  384. template <> struct MDNodeKeyImpl<DIBasicType> {
  385. unsigned Tag;
  386. MDString *Name;
  387. uint64_t SizeInBits;
  388. uint32_t AlignInBits;
  389. unsigned Encoding;
  390. unsigned Flags;
  391. MDNodeKeyImpl(unsigned Tag, MDString *Name, uint64_t SizeInBits,
  392. uint32_t AlignInBits, unsigned Encoding, unsigned Flags)
  393. : Tag(Tag), Name(Name), SizeInBits(SizeInBits), AlignInBits(AlignInBits),
  394. Encoding(Encoding), Flags(Flags) {}
  395. MDNodeKeyImpl(const DIBasicType *N)
  396. : Tag(N->getTag()), Name(N->getRawName()), SizeInBits(N->getSizeInBits()),
  397. AlignInBits(N->getAlignInBits()), Encoding(N->getEncoding()),
  398. Flags(N->getFlags()) {}
  399. bool isKeyOf(const DIBasicType *RHS) const {
  400. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  401. SizeInBits == RHS->getSizeInBits() &&
  402. AlignInBits == RHS->getAlignInBits() &&
  403. Encoding == RHS->getEncoding() && Flags == RHS->getFlags();
  404. }
  405. unsigned getHashValue() const {
  406. return hash_combine(Tag, Name, SizeInBits, AlignInBits, Encoding);
  407. }
  408. };
  409. template <> struct MDNodeKeyImpl<DIStringType> {
  410. unsigned Tag;
  411. MDString *Name;
  412. Metadata *StringLength;
  413. Metadata *StringLengthExp;
  414. Metadata *StringLocationExp;
  415. uint64_t SizeInBits;
  416. uint32_t AlignInBits;
  417. unsigned Encoding;
  418. MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *StringLength,
  419. Metadata *StringLengthExp, Metadata *StringLocationExp,
  420. uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding)
  421. : Tag(Tag), Name(Name), StringLength(StringLength),
  422. StringLengthExp(StringLengthExp), StringLocationExp(StringLocationExp),
  423. SizeInBits(SizeInBits), AlignInBits(AlignInBits), Encoding(Encoding) {}
  424. MDNodeKeyImpl(const DIStringType *N)
  425. : Tag(N->getTag()), Name(N->getRawName()),
  426. StringLength(N->getRawStringLength()),
  427. StringLengthExp(N->getRawStringLengthExp()),
  428. StringLocationExp(N->getRawStringLocationExp()),
  429. SizeInBits(N->getSizeInBits()), AlignInBits(N->getAlignInBits()),
  430. Encoding(N->getEncoding()) {}
  431. bool isKeyOf(const DIStringType *RHS) const {
  432. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  433. SizeInBits == RHS->getSizeInBits() &&
  434. AlignInBits == RHS->getAlignInBits() &&
  435. Encoding == RHS->getEncoding();
  436. }
  437. unsigned getHashValue() const { return hash_combine(Tag, Name, Encoding); }
  438. };
  439. template <> struct MDNodeKeyImpl<DIDerivedType> {
  440. unsigned Tag;
  441. MDString *Name;
  442. Metadata *File;
  443. unsigned Line;
  444. Metadata *Scope;
  445. Metadata *BaseType;
  446. uint64_t SizeInBits;
  447. uint64_t OffsetInBits;
  448. uint32_t AlignInBits;
  449. std::optional<unsigned> DWARFAddressSpace;
  450. unsigned Flags;
  451. Metadata *ExtraData;
  452. Metadata *Annotations;
  453. MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
  454. Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  455. uint32_t AlignInBits, uint64_t OffsetInBits,
  456. std::optional<unsigned> DWARFAddressSpace, unsigned Flags,
  457. Metadata *ExtraData, Metadata *Annotations)
  458. : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
  459. BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
  460. AlignInBits(AlignInBits), DWARFAddressSpace(DWARFAddressSpace),
  461. Flags(Flags), ExtraData(ExtraData), Annotations(Annotations) {}
  462. MDNodeKeyImpl(const DIDerivedType *N)
  463. : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
  464. Line(N->getLine()), Scope(N->getRawScope()),
  465. BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
  466. OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
  467. DWARFAddressSpace(N->getDWARFAddressSpace()), Flags(N->getFlags()),
  468. ExtraData(N->getRawExtraData()), Annotations(N->getRawAnnotations()) {}
  469. bool isKeyOf(const DIDerivedType *RHS) const {
  470. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  471. File == RHS->getRawFile() && Line == RHS->getLine() &&
  472. Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
  473. SizeInBits == RHS->getSizeInBits() &&
  474. AlignInBits == RHS->getAlignInBits() &&
  475. OffsetInBits == RHS->getOffsetInBits() &&
  476. DWARFAddressSpace == RHS->getDWARFAddressSpace() &&
  477. Flags == RHS->getFlags() && ExtraData == RHS->getRawExtraData() &&
  478. Annotations == RHS->getRawAnnotations();
  479. }
  480. unsigned getHashValue() const {
  481. // If this is a member inside an ODR type, only hash the type and the name.
  482. // Otherwise the hash will be stronger than
  483. // MDNodeSubsetEqualImpl::isODRMember().
  484. if (Tag == dwarf::DW_TAG_member && Name)
  485. if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
  486. if (CT->getRawIdentifier())
  487. return hash_combine(Name, Scope);
  488. // Intentionally computes the hash on a subset of the operands for
  489. // performance reason. The subset has to be significant enough to avoid
  490. // collision "most of the time". There is no correctness issue in case of
  491. // collision because of the full check above.
  492. return hash_combine(Tag, Name, File, Line, Scope, BaseType, Flags);
  493. }
  494. };
  495. template <> struct MDNodeSubsetEqualImpl<DIDerivedType> {
  496. using KeyTy = MDNodeKeyImpl<DIDerivedType>;
  497. static bool isSubsetEqual(const KeyTy &LHS, const DIDerivedType *RHS) {
  498. return isODRMember(LHS.Tag, LHS.Scope, LHS.Name, RHS);
  499. }
  500. static bool isSubsetEqual(const DIDerivedType *LHS,
  501. const DIDerivedType *RHS) {
  502. return isODRMember(LHS->getTag(), LHS->getRawScope(), LHS->getRawName(),
  503. RHS);
  504. }
  505. /// Subprograms compare equal if they declare the same function in an ODR
  506. /// type.
  507. static bool isODRMember(unsigned Tag, const Metadata *Scope,
  508. const MDString *Name, const DIDerivedType *RHS) {
  509. // Check whether the LHS is eligible.
  510. if (Tag != dwarf::DW_TAG_member || !Name)
  511. return false;
  512. auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
  513. if (!CT || !CT->getRawIdentifier())
  514. return false;
  515. // Compare to the RHS.
  516. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  517. Scope == RHS->getRawScope();
  518. }
  519. };
  520. template <> struct MDNodeKeyImpl<DICompositeType> {
  521. unsigned Tag;
  522. MDString *Name;
  523. Metadata *File;
  524. unsigned Line;
  525. Metadata *Scope;
  526. Metadata *BaseType;
  527. uint64_t SizeInBits;
  528. uint64_t OffsetInBits;
  529. uint32_t AlignInBits;
  530. unsigned Flags;
  531. Metadata *Elements;
  532. unsigned RuntimeLang;
  533. Metadata *VTableHolder;
  534. Metadata *TemplateParams;
  535. MDString *Identifier;
  536. Metadata *Discriminator;
  537. Metadata *DataLocation;
  538. Metadata *Associated;
  539. Metadata *Allocated;
  540. Metadata *Rank;
  541. Metadata *Annotations;
  542. MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
  543. Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  544. uint32_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
  545. Metadata *Elements, unsigned RuntimeLang,
  546. Metadata *VTableHolder, Metadata *TemplateParams,
  547. MDString *Identifier, Metadata *Discriminator,
  548. Metadata *DataLocation, Metadata *Associated,
  549. Metadata *Allocated, Metadata *Rank, Metadata *Annotations)
  550. : Tag(Tag), Name(Name), File(File), Line(Line), Scope(Scope),
  551. BaseType(BaseType), SizeInBits(SizeInBits), OffsetInBits(OffsetInBits),
  552. AlignInBits(AlignInBits), Flags(Flags), Elements(Elements),
  553. RuntimeLang(RuntimeLang), VTableHolder(VTableHolder),
  554. TemplateParams(TemplateParams), Identifier(Identifier),
  555. Discriminator(Discriminator), DataLocation(DataLocation),
  556. Associated(Associated), Allocated(Allocated), Rank(Rank),
  557. Annotations(Annotations) {}
  558. MDNodeKeyImpl(const DICompositeType *N)
  559. : Tag(N->getTag()), Name(N->getRawName()), File(N->getRawFile()),
  560. Line(N->getLine()), Scope(N->getRawScope()),
  561. BaseType(N->getRawBaseType()), SizeInBits(N->getSizeInBits()),
  562. OffsetInBits(N->getOffsetInBits()), AlignInBits(N->getAlignInBits()),
  563. Flags(N->getFlags()), Elements(N->getRawElements()),
  564. RuntimeLang(N->getRuntimeLang()), VTableHolder(N->getRawVTableHolder()),
  565. TemplateParams(N->getRawTemplateParams()),
  566. Identifier(N->getRawIdentifier()),
  567. Discriminator(N->getRawDiscriminator()),
  568. DataLocation(N->getRawDataLocation()),
  569. Associated(N->getRawAssociated()), Allocated(N->getRawAllocated()),
  570. Rank(N->getRawRank()), Annotations(N->getRawAnnotations()) {}
  571. bool isKeyOf(const DICompositeType *RHS) const {
  572. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  573. File == RHS->getRawFile() && Line == RHS->getLine() &&
  574. Scope == RHS->getRawScope() && BaseType == RHS->getRawBaseType() &&
  575. SizeInBits == RHS->getSizeInBits() &&
  576. AlignInBits == RHS->getAlignInBits() &&
  577. OffsetInBits == RHS->getOffsetInBits() && Flags == RHS->getFlags() &&
  578. Elements == RHS->getRawElements() &&
  579. RuntimeLang == RHS->getRuntimeLang() &&
  580. VTableHolder == RHS->getRawVTableHolder() &&
  581. TemplateParams == RHS->getRawTemplateParams() &&
  582. Identifier == RHS->getRawIdentifier() &&
  583. Discriminator == RHS->getRawDiscriminator() &&
  584. DataLocation == RHS->getRawDataLocation() &&
  585. Associated == RHS->getRawAssociated() &&
  586. Allocated == RHS->getRawAllocated() && Rank == RHS->getRawRank() &&
  587. Annotations == RHS->getRawAnnotations();
  588. }
  589. unsigned getHashValue() const {
  590. // Intentionally computes the hash on a subset of the operands for
  591. // performance reason. The subset has to be significant enough to avoid
  592. // collision "most of the time". There is no correctness issue in case of
  593. // collision because of the full check above.
  594. return hash_combine(Name, File, Line, BaseType, Scope, Elements,
  595. TemplateParams, Annotations);
  596. }
  597. };
  598. template <> struct MDNodeKeyImpl<DISubroutineType> {
  599. unsigned Flags;
  600. uint8_t CC;
  601. Metadata *TypeArray;
  602. MDNodeKeyImpl(unsigned Flags, uint8_t CC, Metadata *TypeArray)
  603. : Flags(Flags), CC(CC), TypeArray(TypeArray) {}
  604. MDNodeKeyImpl(const DISubroutineType *N)
  605. : Flags(N->getFlags()), CC(N->getCC()), TypeArray(N->getRawTypeArray()) {}
  606. bool isKeyOf(const DISubroutineType *RHS) const {
  607. return Flags == RHS->getFlags() && CC == RHS->getCC() &&
  608. TypeArray == RHS->getRawTypeArray();
  609. }
  610. unsigned getHashValue() const { return hash_combine(Flags, CC, TypeArray); }
  611. };
  612. template <> struct MDNodeKeyImpl<DIFile> {
  613. MDString *Filename;
  614. MDString *Directory;
  615. std::optional<DIFile::ChecksumInfo<MDString *>> Checksum;
  616. MDString *Source;
  617. MDNodeKeyImpl(MDString *Filename, MDString *Directory,
  618. std::optional<DIFile::ChecksumInfo<MDString *>> Checksum,
  619. MDString *Source)
  620. : Filename(Filename), Directory(Directory), Checksum(Checksum),
  621. Source(Source) {}
  622. MDNodeKeyImpl(const DIFile *N)
  623. : Filename(N->getRawFilename()), Directory(N->getRawDirectory()),
  624. Checksum(N->getRawChecksum()), Source(N->getRawSource()) {}
  625. bool isKeyOf(const DIFile *RHS) const {
  626. return Filename == RHS->getRawFilename() &&
  627. Directory == RHS->getRawDirectory() &&
  628. Checksum == RHS->getRawChecksum() && Source == RHS->getRawSource();
  629. }
  630. unsigned getHashValue() const {
  631. return hash_combine(Filename, Directory, Checksum ? Checksum->Kind : 0,
  632. Checksum ? Checksum->Value : nullptr, Source);
  633. }
  634. };
  635. template <> struct MDNodeKeyImpl<DISubprogram> {
  636. Metadata *Scope;
  637. MDString *Name;
  638. MDString *LinkageName;
  639. Metadata *File;
  640. unsigned Line;
  641. Metadata *Type;
  642. unsigned ScopeLine;
  643. Metadata *ContainingType;
  644. unsigned VirtualIndex;
  645. int ThisAdjustment;
  646. unsigned Flags;
  647. unsigned SPFlags;
  648. Metadata *Unit;
  649. Metadata *TemplateParams;
  650. Metadata *Declaration;
  651. Metadata *RetainedNodes;
  652. Metadata *ThrownTypes;
  653. Metadata *Annotations;
  654. MDString *TargetFuncName;
  655. MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
  656. Metadata *File, unsigned Line, Metadata *Type,
  657. unsigned ScopeLine, Metadata *ContainingType,
  658. unsigned VirtualIndex, int ThisAdjustment, unsigned Flags,
  659. unsigned SPFlags, Metadata *Unit, Metadata *TemplateParams,
  660. Metadata *Declaration, Metadata *RetainedNodes,
  661. Metadata *ThrownTypes, Metadata *Annotations,
  662. MDString *TargetFuncName)
  663. : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
  664. Line(Line), Type(Type), ScopeLine(ScopeLine),
  665. ContainingType(ContainingType), VirtualIndex(VirtualIndex),
  666. ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags),
  667. Unit(Unit), TemplateParams(TemplateParams), Declaration(Declaration),
  668. RetainedNodes(RetainedNodes), ThrownTypes(ThrownTypes),
  669. Annotations(Annotations), TargetFuncName(TargetFuncName) {}
  670. MDNodeKeyImpl(const DISubprogram *N)
  671. : Scope(N->getRawScope()), Name(N->getRawName()),
  672. LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
  673. Line(N->getLine()), Type(N->getRawType()), ScopeLine(N->getScopeLine()),
  674. ContainingType(N->getRawContainingType()),
  675. VirtualIndex(N->getVirtualIndex()),
  676. ThisAdjustment(N->getThisAdjustment()), Flags(N->getFlags()),
  677. SPFlags(N->getSPFlags()), Unit(N->getRawUnit()),
  678. TemplateParams(N->getRawTemplateParams()),
  679. Declaration(N->getRawDeclaration()),
  680. RetainedNodes(N->getRawRetainedNodes()),
  681. ThrownTypes(N->getRawThrownTypes()),
  682. Annotations(N->getRawAnnotations()),
  683. TargetFuncName(N->getRawTargetFuncName()) {}
  684. bool isKeyOf(const DISubprogram *RHS) const {
  685. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  686. LinkageName == RHS->getRawLinkageName() &&
  687. File == RHS->getRawFile() && Line == RHS->getLine() &&
  688. Type == RHS->getRawType() && ScopeLine == RHS->getScopeLine() &&
  689. ContainingType == RHS->getRawContainingType() &&
  690. VirtualIndex == RHS->getVirtualIndex() &&
  691. ThisAdjustment == RHS->getThisAdjustment() &&
  692. Flags == RHS->getFlags() && SPFlags == RHS->getSPFlags() &&
  693. Unit == RHS->getUnit() &&
  694. TemplateParams == RHS->getRawTemplateParams() &&
  695. Declaration == RHS->getRawDeclaration() &&
  696. RetainedNodes == RHS->getRawRetainedNodes() &&
  697. ThrownTypes == RHS->getRawThrownTypes() &&
  698. Annotations == RHS->getRawAnnotations() &&
  699. TargetFuncName == RHS->getRawTargetFuncName();
  700. }
  701. bool isDefinition() const { return SPFlags & DISubprogram::SPFlagDefinition; }
  702. unsigned getHashValue() const {
  703. // If this is a declaration inside an ODR type, only hash the type and the
  704. // name. Otherwise the hash will be stronger than
  705. // MDNodeSubsetEqualImpl::isDeclarationOfODRMember().
  706. if (!isDefinition() && LinkageName)
  707. if (auto *CT = dyn_cast_or_null<DICompositeType>(Scope))
  708. if (CT->getRawIdentifier())
  709. return hash_combine(LinkageName, Scope);
  710. // Intentionally computes the hash on a subset of the operands for
  711. // performance reason. The subset has to be significant enough to avoid
  712. // collision "most of the time". There is no correctness issue in case of
  713. // collision because of the full check above.
  714. return hash_combine(Name, Scope, File, Type, Line);
  715. }
  716. };
  717. template <> struct MDNodeSubsetEqualImpl<DISubprogram> {
  718. using KeyTy = MDNodeKeyImpl<DISubprogram>;
  719. static bool isSubsetEqual(const KeyTy &LHS, const DISubprogram *RHS) {
  720. return isDeclarationOfODRMember(LHS.isDefinition(), LHS.Scope,
  721. LHS.LinkageName, LHS.TemplateParams, RHS);
  722. }
  723. static bool isSubsetEqual(const DISubprogram *LHS, const DISubprogram *RHS) {
  724. return isDeclarationOfODRMember(LHS->isDefinition(), LHS->getRawScope(),
  725. LHS->getRawLinkageName(),
  726. LHS->getRawTemplateParams(), RHS);
  727. }
  728. /// Subprograms compare equal if they declare the same function in an ODR
  729. /// type.
  730. static bool isDeclarationOfODRMember(bool IsDefinition, const Metadata *Scope,
  731. const MDString *LinkageName,
  732. const Metadata *TemplateParams,
  733. const DISubprogram *RHS) {
  734. // Check whether the LHS is eligible.
  735. if (IsDefinition || !Scope || !LinkageName)
  736. return false;
  737. auto *CT = dyn_cast_or_null<DICompositeType>(Scope);
  738. if (!CT || !CT->getRawIdentifier())
  739. return false;
  740. // Compare to the RHS.
  741. // FIXME: We need to compare template parameters here to avoid incorrect
  742. // collisions in mapMetadata when RF_ReuseAndMutateDistinctMDs and a
  743. // ODR-DISubprogram has a non-ODR template parameter (i.e., a
  744. // DICompositeType that does not have an identifier). Eventually we should
  745. // decouple ODR logic from uniquing logic.
  746. return IsDefinition == RHS->isDefinition() && Scope == RHS->getRawScope() &&
  747. LinkageName == RHS->getRawLinkageName() &&
  748. TemplateParams == RHS->getRawTemplateParams();
  749. }
  750. };
  751. template <> struct MDNodeKeyImpl<DILexicalBlock> {
  752. Metadata *Scope;
  753. Metadata *File;
  754. unsigned Line;
  755. unsigned Column;
  756. MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Line, unsigned Column)
  757. : Scope(Scope), File(File), Line(Line), Column(Column) {}
  758. MDNodeKeyImpl(const DILexicalBlock *N)
  759. : Scope(N->getRawScope()), File(N->getRawFile()), Line(N->getLine()),
  760. Column(N->getColumn()) {}
  761. bool isKeyOf(const DILexicalBlock *RHS) const {
  762. return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
  763. Line == RHS->getLine() && Column == RHS->getColumn();
  764. }
  765. unsigned getHashValue() const {
  766. return hash_combine(Scope, File, Line, Column);
  767. }
  768. };
  769. template <> struct MDNodeKeyImpl<DILexicalBlockFile> {
  770. Metadata *Scope;
  771. Metadata *File;
  772. unsigned Discriminator;
  773. MDNodeKeyImpl(Metadata *Scope, Metadata *File, unsigned Discriminator)
  774. : Scope(Scope), File(File), Discriminator(Discriminator) {}
  775. MDNodeKeyImpl(const DILexicalBlockFile *N)
  776. : Scope(N->getRawScope()), File(N->getRawFile()),
  777. Discriminator(N->getDiscriminator()) {}
  778. bool isKeyOf(const DILexicalBlockFile *RHS) const {
  779. return Scope == RHS->getRawScope() && File == RHS->getRawFile() &&
  780. Discriminator == RHS->getDiscriminator();
  781. }
  782. unsigned getHashValue() const {
  783. return hash_combine(Scope, File, Discriminator);
  784. }
  785. };
  786. template <> struct MDNodeKeyImpl<DINamespace> {
  787. Metadata *Scope;
  788. MDString *Name;
  789. bool ExportSymbols;
  790. MDNodeKeyImpl(Metadata *Scope, MDString *Name, bool ExportSymbols)
  791. : Scope(Scope), Name(Name), ExportSymbols(ExportSymbols) {}
  792. MDNodeKeyImpl(const DINamespace *N)
  793. : Scope(N->getRawScope()), Name(N->getRawName()),
  794. ExportSymbols(N->getExportSymbols()) {}
  795. bool isKeyOf(const DINamespace *RHS) const {
  796. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  797. ExportSymbols == RHS->getExportSymbols();
  798. }
  799. unsigned getHashValue() const { return hash_combine(Scope, Name); }
  800. };
  801. template <> struct MDNodeKeyImpl<DICommonBlock> {
  802. Metadata *Scope;
  803. Metadata *Decl;
  804. MDString *Name;
  805. Metadata *File;
  806. unsigned LineNo;
  807. MDNodeKeyImpl(Metadata *Scope, Metadata *Decl, MDString *Name, Metadata *File,
  808. unsigned LineNo)
  809. : Scope(Scope), Decl(Decl), Name(Name), File(File), LineNo(LineNo) {}
  810. MDNodeKeyImpl(const DICommonBlock *N)
  811. : Scope(N->getRawScope()), Decl(N->getRawDecl()), Name(N->getRawName()),
  812. File(N->getRawFile()), LineNo(N->getLineNo()) {}
  813. bool isKeyOf(const DICommonBlock *RHS) const {
  814. return Scope == RHS->getRawScope() && Decl == RHS->getRawDecl() &&
  815. Name == RHS->getRawName() && File == RHS->getRawFile() &&
  816. LineNo == RHS->getLineNo();
  817. }
  818. unsigned getHashValue() const {
  819. return hash_combine(Scope, Decl, Name, File, LineNo);
  820. }
  821. };
  822. template <> struct MDNodeKeyImpl<DIModule> {
  823. Metadata *File;
  824. Metadata *Scope;
  825. MDString *Name;
  826. MDString *ConfigurationMacros;
  827. MDString *IncludePath;
  828. MDString *APINotesFile;
  829. unsigned LineNo;
  830. bool IsDecl;
  831. MDNodeKeyImpl(Metadata *File, Metadata *Scope, MDString *Name,
  832. MDString *ConfigurationMacros, MDString *IncludePath,
  833. MDString *APINotesFile, unsigned LineNo, bool IsDecl)
  834. : File(File), Scope(Scope), Name(Name),
  835. ConfigurationMacros(ConfigurationMacros), IncludePath(IncludePath),
  836. APINotesFile(APINotesFile), LineNo(LineNo), IsDecl(IsDecl) {}
  837. MDNodeKeyImpl(const DIModule *N)
  838. : File(N->getRawFile()), Scope(N->getRawScope()), Name(N->getRawName()),
  839. ConfigurationMacros(N->getRawConfigurationMacros()),
  840. IncludePath(N->getRawIncludePath()),
  841. APINotesFile(N->getRawAPINotesFile()), LineNo(N->getLineNo()),
  842. IsDecl(N->getIsDecl()) {}
  843. bool isKeyOf(const DIModule *RHS) const {
  844. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  845. ConfigurationMacros == RHS->getRawConfigurationMacros() &&
  846. IncludePath == RHS->getRawIncludePath() &&
  847. APINotesFile == RHS->getRawAPINotesFile() &&
  848. File == RHS->getRawFile() && LineNo == RHS->getLineNo() &&
  849. IsDecl == RHS->getIsDecl();
  850. }
  851. unsigned getHashValue() const {
  852. return hash_combine(Scope, Name, ConfigurationMacros, IncludePath);
  853. }
  854. };
  855. template <> struct MDNodeKeyImpl<DITemplateTypeParameter> {
  856. MDString *Name;
  857. Metadata *Type;
  858. bool IsDefault;
  859. MDNodeKeyImpl(MDString *Name, Metadata *Type, bool IsDefault)
  860. : Name(Name), Type(Type), IsDefault(IsDefault) {}
  861. MDNodeKeyImpl(const DITemplateTypeParameter *N)
  862. : Name(N->getRawName()), Type(N->getRawType()),
  863. IsDefault(N->isDefault()) {}
  864. bool isKeyOf(const DITemplateTypeParameter *RHS) const {
  865. return Name == RHS->getRawName() && Type == RHS->getRawType() &&
  866. IsDefault == RHS->isDefault();
  867. }
  868. unsigned getHashValue() const { return hash_combine(Name, Type, IsDefault); }
  869. };
  870. template <> struct MDNodeKeyImpl<DITemplateValueParameter> {
  871. unsigned Tag;
  872. MDString *Name;
  873. Metadata *Type;
  874. bool IsDefault;
  875. Metadata *Value;
  876. MDNodeKeyImpl(unsigned Tag, MDString *Name, Metadata *Type, bool IsDefault,
  877. Metadata *Value)
  878. : Tag(Tag), Name(Name), Type(Type), IsDefault(IsDefault), Value(Value) {}
  879. MDNodeKeyImpl(const DITemplateValueParameter *N)
  880. : Tag(N->getTag()), Name(N->getRawName()), Type(N->getRawType()),
  881. IsDefault(N->isDefault()), Value(N->getValue()) {}
  882. bool isKeyOf(const DITemplateValueParameter *RHS) const {
  883. return Tag == RHS->getTag() && Name == RHS->getRawName() &&
  884. Type == RHS->getRawType() && IsDefault == RHS->isDefault() &&
  885. Value == RHS->getValue();
  886. }
  887. unsigned getHashValue() const {
  888. return hash_combine(Tag, Name, Type, IsDefault, Value);
  889. }
  890. };
  891. template <> struct MDNodeKeyImpl<DIGlobalVariable> {
  892. Metadata *Scope;
  893. MDString *Name;
  894. MDString *LinkageName;
  895. Metadata *File;
  896. unsigned Line;
  897. Metadata *Type;
  898. bool IsLocalToUnit;
  899. bool IsDefinition;
  900. Metadata *StaticDataMemberDeclaration;
  901. Metadata *TemplateParams;
  902. uint32_t AlignInBits;
  903. Metadata *Annotations;
  904. MDNodeKeyImpl(Metadata *Scope, MDString *Name, MDString *LinkageName,
  905. Metadata *File, unsigned Line, Metadata *Type,
  906. bool IsLocalToUnit, bool IsDefinition,
  907. Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
  908. uint32_t AlignInBits, Metadata *Annotations)
  909. : Scope(Scope), Name(Name), LinkageName(LinkageName), File(File),
  910. Line(Line), Type(Type), IsLocalToUnit(IsLocalToUnit),
  911. IsDefinition(IsDefinition),
  912. StaticDataMemberDeclaration(StaticDataMemberDeclaration),
  913. TemplateParams(TemplateParams), AlignInBits(AlignInBits),
  914. Annotations(Annotations) {}
  915. MDNodeKeyImpl(const DIGlobalVariable *N)
  916. : Scope(N->getRawScope()), Name(N->getRawName()),
  917. LinkageName(N->getRawLinkageName()), File(N->getRawFile()),
  918. Line(N->getLine()), Type(N->getRawType()),
  919. IsLocalToUnit(N->isLocalToUnit()), IsDefinition(N->isDefinition()),
  920. StaticDataMemberDeclaration(N->getRawStaticDataMemberDeclaration()),
  921. TemplateParams(N->getRawTemplateParams()),
  922. AlignInBits(N->getAlignInBits()), Annotations(N->getRawAnnotations()) {}
  923. bool isKeyOf(const DIGlobalVariable *RHS) const {
  924. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  925. LinkageName == RHS->getRawLinkageName() &&
  926. File == RHS->getRawFile() && Line == RHS->getLine() &&
  927. Type == RHS->getRawType() && IsLocalToUnit == RHS->isLocalToUnit() &&
  928. IsDefinition == RHS->isDefinition() &&
  929. StaticDataMemberDeclaration ==
  930. RHS->getRawStaticDataMemberDeclaration() &&
  931. TemplateParams == RHS->getRawTemplateParams() &&
  932. AlignInBits == RHS->getAlignInBits() &&
  933. Annotations == RHS->getRawAnnotations();
  934. }
  935. unsigned getHashValue() const {
  936. // We do not use AlignInBits in hashing function here on purpose:
  937. // in most cases this param for local variable is zero (for function param
  938. // it is always zero). This leads to lots of hash collisions and errors on
  939. // cases with lots of similar variables.
  940. // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
  941. // generated IR is random for each run and test fails with Align included.
  942. // TODO: make hashing work fine with such situations
  943. return hash_combine(Scope, Name, LinkageName, File, Line, Type,
  944. IsLocalToUnit, IsDefinition, /* AlignInBits, */
  945. StaticDataMemberDeclaration, Annotations);
  946. }
  947. };
  948. template <> struct MDNodeKeyImpl<DILocalVariable> {
  949. Metadata *Scope;
  950. MDString *Name;
  951. Metadata *File;
  952. unsigned Line;
  953. Metadata *Type;
  954. unsigned Arg;
  955. unsigned Flags;
  956. uint32_t AlignInBits;
  957. Metadata *Annotations;
  958. MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line,
  959. Metadata *Type, unsigned Arg, unsigned Flags,
  960. uint32_t AlignInBits, Metadata *Annotations)
  961. : Scope(Scope), Name(Name), File(File), Line(Line), Type(Type), Arg(Arg),
  962. Flags(Flags), AlignInBits(AlignInBits), Annotations(Annotations) {}
  963. MDNodeKeyImpl(const DILocalVariable *N)
  964. : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
  965. Line(N->getLine()), Type(N->getRawType()), Arg(N->getArg()),
  966. Flags(N->getFlags()), AlignInBits(N->getAlignInBits()),
  967. Annotations(N->getRawAnnotations()) {}
  968. bool isKeyOf(const DILocalVariable *RHS) const {
  969. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  970. File == RHS->getRawFile() && Line == RHS->getLine() &&
  971. Type == RHS->getRawType() && Arg == RHS->getArg() &&
  972. Flags == RHS->getFlags() && AlignInBits == RHS->getAlignInBits() &&
  973. Annotations == RHS->getRawAnnotations();
  974. }
  975. unsigned getHashValue() const {
  976. // We do not use AlignInBits in hashing function here on purpose:
  977. // in most cases this param for local variable is zero (for function param
  978. // it is always zero). This leads to lots of hash collisions and errors on
  979. // cases with lots of similar variables.
  980. // clang/test/CodeGen/debug-info-257-args.c is an example of this problem,
  981. // generated IR is random for each run and test fails with Align included.
  982. // TODO: make hashing work fine with such situations
  983. return hash_combine(Scope, Name, File, Line, Type, Arg, Flags, Annotations);
  984. }
  985. };
  986. template <> struct MDNodeKeyImpl<DILabel> {
  987. Metadata *Scope;
  988. MDString *Name;
  989. Metadata *File;
  990. unsigned Line;
  991. MDNodeKeyImpl(Metadata *Scope, MDString *Name, Metadata *File, unsigned Line)
  992. : Scope(Scope), Name(Name), File(File), Line(Line) {}
  993. MDNodeKeyImpl(const DILabel *N)
  994. : Scope(N->getRawScope()), Name(N->getRawName()), File(N->getRawFile()),
  995. Line(N->getLine()) {}
  996. bool isKeyOf(const DILabel *RHS) const {
  997. return Scope == RHS->getRawScope() && Name == RHS->getRawName() &&
  998. File == RHS->getRawFile() && Line == RHS->getLine();
  999. }
  1000. /// Using name and line to get hash value. It should already be mostly unique.
  1001. unsigned getHashValue() const { return hash_combine(Scope, Name, Line); }
  1002. };
  1003. template <> struct MDNodeKeyImpl<DIExpression> {
  1004. ArrayRef<uint64_t> Elements;
  1005. MDNodeKeyImpl(ArrayRef<uint64_t> Elements) : Elements(Elements) {}
  1006. MDNodeKeyImpl(const DIExpression *N) : Elements(N->getElements()) {}
  1007. bool isKeyOf(const DIExpression *RHS) const {
  1008. return Elements == RHS->getElements();
  1009. }
  1010. unsigned getHashValue() const {
  1011. return hash_combine_range(Elements.begin(), Elements.end());
  1012. }
  1013. };
  1014. template <> struct MDNodeKeyImpl<DIGlobalVariableExpression> {
  1015. Metadata *Variable;
  1016. Metadata *Expression;
  1017. MDNodeKeyImpl(Metadata *Variable, Metadata *Expression)
  1018. : Variable(Variable), Expression(Expression) {}
  1019. MDNodeKeyImpl(const DIGlobalVariableExpression *N)
  1020. : Variable(N->getRawVariable()), Expression(N->getRawExpression()) {}
  1021. bool isKeyOf(const DIGlobalVariableExpression *RHS) const {
  1022. return Variable == RHS->getRawVariable() &&
  1023. Expression == RHS->getRawExpression();
  1024. }
  1025. unsigned getHashValue() const { return hash_combine(Variable, Expression); }
  1026. };
  1027. template <> struct MDNodeKeyImpl<DIObjCProperty> {
  1028. MDString *Name;
  1029. Metadata *File;
  1030. unsigned Line;
  1031. MDString *GetterName;
  1032. MDString *SetterName;
  1033. unsigned Attributes;
  1034. Metadata *Type;
  1035. MDNodeKeyImpl(MDString *Name, Metadata *File, unsigned Line,
  1036. MDString *GetterName, MDString *SetterName, unsigned Attributes,
  1037. Metadata *Type)
  1038. : Name(Name), File(File), Line(Line), GetterName(GetterName),
  1039. SetterName(SetterName), Attributes(Attributes), Type(Type) {}
  1040. MDNodeKeyImpl(const DIObjCProperty *N)
  1041. : Name(N->getRawName()), File(N->getRawFile()), Line(N->getLine()),
  1042. GetterName(N->getRawGetterName()), SetterName(N->getRawSetterName()),
  1043. Attributes(N->getAttributes()), Type(N->getRawType()) {}
  1044. bool isKeyOf(const DIObjCProperty *RHS) const {
  1045. return Name == RHS->getRawName() && File == RHS->getRawFile() &&
  1046. Line == RHS->getLine() && GetterName == RHS->getRawGetterName() &&
  1047. SetterName == RHS->getRawSetterName() &&
  1048. Attributes == RHS->getAttributes() && Type == RHS->getRawType();
  1049. }
  1050. unsigned getHashValue() const {
  1051. return hash_combine(Name, File, Line, GetterName, SetterName, Attributes,
  1052. Type);
  1053. }
  1054. };
  1055. template <> struct MDNodeKeyImpl<DIImportedEntity> {
  1056. unsigned Tag;
  1057. Metadata *Scope;
  1058. Metadata *Entity;
  1059. Metadata *File;
  1060. unsigned Line;
  1061. MDString *Name;
  1062. Metadata *Elements;
  1063. MDNodeKeyImpl(unsigned Tag, Metadata *Scope, Metadata *Entity, Metadata *File,
  1064. unsigned Line, MDString *Name, Metadata *Elements)
  1065. : Tag(Tag), Scope(Scope), Entity(Entity), File(File), Line(Line),
  1066. Name(Name), Elements(Elements) {}
  1067. MDNodeKeyImpl(const DIImportedEntity *N)
  1068. : Tag(N->getTag()), Scope(N->getRawScope()), Entity(N->getRawEntity()),
  1069. File(N->getRawFile()), Line(N->getLine()), Name(N->getRawName()),
  1070. Elements(N->getRawElements()) {}
  1071. bool isKeyOf(const DIImportedEntity *RHS) const {
  1072. return Tag == RHS->getTag() && Scope == RHS->getRawScope() &&
  1073. Entity == RHS->getRawEntity() && File == RHS->getFile() &&
  1074. Line == RHS->getLine() && Name == RHS->getRawName() &&
  1075. Elements == RHS->getRawElements();
  1076. }
  1077. unsigned getHashValue() const {
  1078. return hash_combine(Tag, Scope, Entity, File, Line, Name, Elements);
  1079. }
  1080. };
  1081. template <> struct MDNodeKeyImpl<DIMacro> {
  1082. unsigned MIType;
  1083. unsigned Line;
  1084. MDString *Name;
  1085. MDString *Value;
  1086. MDNodeKeyImpl(unsigned MIType, unsigned Line, MDString *Name, MDString *Value)
  1087. : MIType(MIType), Line(Line), Name(Name), Value(Value) {}
  1088. MDNodeKeyImpl(const DIMacro *N)
  1089. : MIType(N->getMacinfoType()), Line(N->getLine()), Name(N->getRawName()),
  1090. Value(N->getRawValue()) {}
  1091. bool isKeyOf(const DIMacro *RHS) const {
  1092. return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
  1093. Name == RHS->getRawName() && Value == RHS->getRawValue();
  1094. }
  1095. unsigned getHashValue() const {
  1096. return hash_combine(MIType, Line, Name, Value);
  1097. }
  1098. };
  1099. template <> struct MDNodeKeyImpl<DIMacroFile> {
  1100. unsigned MIType;
  1101. unsigned Line;
  1102. Metadata *File;
  1103. Metadata *Elements;
  1104. MDNodeKeyImpl(unsigned MIType, unsigned Line, Metadata *File,
  1105. Metadata *Elements)
  1106. : MIType(MIType), Line(Line), File(File), Elements(Elements) {}
  1107. MDNodeKeyImpl(const DIMacroFile *N)
  1108. : MIType(N->getMacinfoType()), Line(N->getLine()), File(N->getRawFile()),
  1109. Elements(N->getRawElements()) {}
  1110. bool isKeyOf(const DIMacroFile *RHS) const {
  1111. return MIType == RHS->getMacinfoType() && Line == RHS->getLine() &&
  1112. File == RHS->getRawFile() && Elements == RHS->getRawElements();
  1113. }
  1114. unsigned getHashValue() const {
  1115. return hash_combine(MIType, Line, File, Elements);
  1116. }
  1117. };
  1118. template <> struct MDNodeKeyImpl<DIArgList> {
  1119. ArrayRef<ValueAsMetadata *> Args;
  1120. MDNodeKeyImpl(ArrayRef<ValueAsMetadata *> Args) : Args(Args) {}
  1121. MDNodeKeyImpl(const DIArgList *N) : Args(N->getArgs()) {}
  1122. bool isKeyOf(const DIArgList *RHS) const { return Args == RHS->getArgs(); }
  1123. unsigned getHashValue() const {
  1124. return hash_combine_range(Args.begin(), Args.end());
  1125. }
  1126. };
  1127. /// DenseMapInfo for MDNode subclasses.
  1128. template <class NodeTy> struct MDNodeInfo {
  1129. using KeyTy = MDNodeKeyImpl<NodeTy>;
  1130. using SubsetEqualTy = MDNodeSubsetEqualImpl<NodeTy>;
  1131. static inline NodeTy *getEmptyKey() {
  1132. return DenseMapInfo<NodeTy *>::getEmptyKey();
  1133. }
  1134. static inline NodeTy *getTombstoneKey() {
  1135. return DenseMapInfo<NodeTy *>::getTombstoneKey();
  1136. }
  1137. static unsigned getHashValue(const KeyTy &Key) { return Key.getHashValue(); }
  1138. static unsigned getHashValue(const NodeTy *N) {
  1139. return KeyTy(N).getHashValue();
  1140. }
  1141. static bool isEqual(const KeyTy &LHS, const NodeTy *RHS) {
  1142. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  1143. return false;
  1144. return SubsetEqualTy::isSubsetEqual(LHS, RHS) || LHS.isKeyOf(RHS);
  1145. }
  1146. static bool isEqual(const NodeTy *LHS, const NodeTy *RHS) {
  1147. if (LHS == RHS)
  1148. return true;
  1149. if (RHS == getEmptyKey() || RHS == getTombstoneKey())
  1150. return false;
  1151. return SubsetEqualTy::isSubsetEqual(LHS, RHS);
  1152. }
  1153. };
  1154. #define HANDLE_MDNODE_LEAF(CLASS) using CLASS##Info = MDNodeInfo<CLASS>;
  1155. #include "llvm/IR/Metadata.def"
  1156. /// Multimap-like storage for metadata attachments.
  1157. class MDAttachments {
  1158. public:
  1159. struct Attachment {
  1160. unsigned MDKind;
  1161. TrackingMDNodeRef Node;
  1162. };
  1163. private:
  1164. SmallVector<Attachment, 1> Attachments;
  1165. public:
  1166. bool empty() const { return Attachments.empty(); }
  1167. size_t size() const { return Attachments.size(); }
  1168. /// Returns the first attachment with the given ID or nullptr if no such
  1169. /// attachment exists.
  1170. MDNode *lookup(unsigned ID) const;
  1171. /// Appends all attachments with the given ID to \c Result in insertion order.
  1172. /// If the global has no attachments with the given ID, or if ID is invalid,
  1173. /// leaves Result unchanged.
  1174. void get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const;
  1175. /// Appends all attachments for the global to \c Result, sorting by attachment
  1176. /// ID. Attachments with the same ID appear in insertion order. This function
  1177. /// does \em not clear \c Result.
  1178. void getAll(SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const;
  1179. /// Set an attachment to a particular node.
  1180. ///
  1181. /// Set the \c ID attachment to \c MD, replacing the current attachments at \c
  1182. /// ID (if anyway).
  1183. void set(unsigned ID, MDNode *MD);
  1184. /// Adds an attachment to a particular node.
  1185. void insert(unsigned ID, MDNode &MD);
  1186. /// Remove attachments with the given ID.
  1187. ///
  1188. /// Remove the attachments at \c ID, if any.
  1189. bool erase(unsigned ID);
  1190. /// Erase matching attachments.
  1191. ///
  1192. /// Erases all attachments matching the \c shouldRemove predicate.
  1193. template <class PredTy> void remove_if(PredTy shouldRemove) {
  1194. llvm::erase_if(Attachments, shouldRemove);
  1195. }
  1196. };
  1197. class LLVMContextImpl {
  1198. public:
  1199. /// OwnedModules - The set of modules instantiated in this context, and which
  1200. /// will be automatically deleted if this context is deleted.
  1201. SmallPtrSet<Module *, 4> OwnedModules;
  1202. /// The main remark streamer used by all the other streamers (e.g. IR, MIR,
  1203. /// frontends, etc.). This should only be used by the specific streamers, and
  1204. /// never directly.
  1205. std::unique_ptr<remarks::RemarkStreamer> MainRemarkStreamer;
  1206. std::unique_ptr<DiagnosticHandler> DiagHandler;
  1207. bool RespectDiagnosticFilters = false;
  1208. bool DiagnosticsHotnessRequested = false;
  1209. /// The minimum hotness value a diagnostic needs in order to be included in
  1210. /// optimization diagnostics.
  1211. ///
  1212. /// The threshold is an Optional value, which maps to one of the 3 states:
  1213. /// 1). 0 => threshold disabled. All emarks will be printed.
  1214. /// 2). positive int => manual threshold by user. Remarks with hotness exceed
  1215. /// threshold will be printed.
  1216. /// 3). None => 'auto' threshold by user. The actual value is not
  1217. /// available at command line, but will be synced with
  1218. /// hotness threhold from profile summary during
  1219. /// compilation.
  1220. ///
  1221. /// State 1 and 2 are considered as terminal states. State transition is
  1222. /// only allowed from 3 to 2, when the threshold is first synced with profile
  1223. /// summary. This ensures that the threshold is set only once and stays
  1224. /// constant.
  1225. ///
  1226. /// If threshold option is not specified, it is disabled (0) by default.
  1227. std::optional<uint64_t> DiagnosticsHotnessThreshold = 0;
  1228. /// The percentage of difference between profiling branch weights and
  1229. /// llvm.expect branch weights to tolerate when emiting MisExpect diagnostics
  1230. std::optional<uint32_t> DiagnosticsMisExpectTolerance = 0;
  1231. bool MisExpectWarningRequested = false;
  1232. /// The specialized remark streamer used by LLVM's OptimizationRemarkEmitter.
  1233. std::unique_ptr<LLVMRemarkStreamer> LLVMRS;
  1234. LLVMContext::YieldCallbackTy YieldCallback = nullptr;
  1235. void *YieldOpaqueHandle = nullptr;
  1236. DenseMap<const Value *, ValueName *> ValueNames;
  1237. using IntMapTy =
  1238. DenseMap<APInt, std::unique_ptr<ConstantInt>, DenseMapAPIntKeyInfo>;
  1239. IntMapTy IntConstants;
  1240. using FPMapTy =
  1241. DenseMap<APFloat, std::unique_ptr<ConstantFP>, DenseMapAPFloatKeyInfo>;
  1242. FPMapTy FPConstants;
  1243. FoldingSet<AttributeImpl> AttrsSet;
  1244. FoldingSet<AttributeListImpl> AttrsLists;
  1245. FoldingSet<AttributeSetNode> AttrsSetNodes;
  1246. StringMap<MDString, BumpPtrAllocator> MDStringCache;
  1247. DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
  1248. DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;
  1249. #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
  1250. DenseSet<CLASS *, CLASS##Info> CLASS##s;
  1251. #include "llvm/IR/Metadata.def"
  1252. // Optional map for looking up composite types by identifier.
  1253. std::optional<DenseMap<const MDString *, DICompositeType *>> DITypeMap;
  1254. // MDNodes may be uniqued or not uniqued. When they're not uniqued, they
  1255. // aren't in the MDNodeSet, but they're still shared between objects, so no
  1256. // one object can destroy them. Keep track of them here so we can delete
  1257. // them on context teardown.
  1258. std::vector<MDNode *> DistinctMDNodes;
  1259. DenseMap<Type *, std::unique_ptr<ConstantAggregateZero>> CAZConstants;
  1260. using ArrayConstantsTy = ConstantUniqueMap<ConstantArray>;
  1261. ArrayConstantsTy ArrayConstants;
  1262. using StructConstantsTy = ConstantUniqueMap<ConstantStruct>;
  1263. StructConstantsTy StructConstants;
  1264. using VectorConstantsTy = ConstantUniqueMap<ConstantVector>;
  1265. VectorConstantsTy VectorConstants;
  1266. DenseMap<PointerType *, std::unique_ptr<ConstantPointerNull>> CPNConstants;
  1267. DenseMap<TargetExtType *, std::unique_ptr<ConstantTargetNone>> CTNConstants;
  1268. DenseMap<Type *, std::unique_ptr<UndefValue>> UVConstants;
  1269. DenseMap<Type *, std::unique_ptr<PoisonValue>> PVConstants;
  1270. StringMap<std::unique_ptr<ConstantDataSequential>> CDSConstants;
  1271. DenseMap<std::pair<const Function *, const BasicBlock *>, BlockAddress *>
  1272. BlockAddresses;
  1273. DenseMap<const GlobalValue *, DSOLocalEquivalent *> DSOLocalEquivalents;
  1274. DenseMap<const GlobalValue *, NoCFIValue *> NoCFIValues;
  1275. ConstantUniqueMap<ConstantExpr> ExprConstants;
  1276. ConstantUniqueMap<InlineAsm> InlineAsms;
  1277. ConstantInt *TheTrueVal = nullptr;
  1278. ConstantInt *TheFalseVal = nullptr;
  1279. // Basic type instances.
  1280. Type VoidTy, LabelTy, HalfTy, BFloatTy, FloatTy, DoubleTy, MetadataTy,
  1281. TokenTy;
  1282. Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy, X86_AMXTy;
  1283. IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty, Int128Ty;
  1284. std::unique_ptr<ConstantTokenNone> TheNoneToken;
  1285. BumpPtrAllocator Alloc;
  1286. UniqueStringSaver Saver{Alloc};
  1287. DenseMap<unsigned, IntegerType *> IntegerTypes;
  1288. using FunctionTypeSet = DenseSet<FunctionType *, FunctionTypeKeyInfo>;
  1289. FunctionTypeSet FunctionTypes;
  1290. using StructTypeSet = DenseSet<StructType *, AnonStructTypeKeyInfo>;
  1291. StructTypeSet AnonStructTypes;
  1292. StringMap<StructType *> NamedStructTypes;
  1293. unsigned NamedStructTypesUniqueID = 0;
  1294. using TargetExtTypeSet = DenseSet<TargetExtType *, TargetExtTypeKeyInfo>;
  1295. TargetExtTypeSet TargetExtTypes;
  1296. DenseMap<std::pair<Type *, uint64_t>, ArrayType *> ArrayTypes;
  1297. DenseMap<std::pair<Type *, ElementCount>, VectorType *> VectorTypes;
  1298. DenseMap<Type *, PointerType *> PointerTypes; // Pointers in AddrSpace = 0
  1299. DenseMap<std::pair<Type *, unsigned>, PointerType *> ASPointerTypes;
  1300. DenseMap<std::pair<Type *, unsigned>, TypedPointerType *> ASTypedPointerTypes;
  1301. /// ValueHandles - This map keeps track of all of the value handles that are
  1302. /// watching a Value*. The Value::HasValueHandle bit is used to know
  1303. /// whether or not a value has an entry in this map.
  1304. using ValueHandlesTy = DenseMap<Value *, ValueHandleBase *>;
  1305. ValueHandlesTy ValueHandles;
  1306. /// CustomMDKindNames - Map to hold the metadata string to ID mapping.
  1307. StringMap<unsigned> CustomMDKindNames;
  1308. /// Collection of metadata used in this context.
  1309. DenseMap<const Value *, MDAttachments> ValueMetadata;
  1310. /// Map DIAssignID -> Instructions with that attachment.
  1311. /// Managed by Instruction via Instruction::updateDIAssignIDMapping.
  1312. /// Query using the at:: functions defined in DebugInfo.h.
  1313. DenseMap<DIAssignID *, SmallVector<Instruction *, 1>> AssignmentIDToInstrs;
  1314. /// Collection of per-GlobalObject sections used in this context.
  1315. DenseMap<const GlobalObject *, StringRef> GlobalObjectSections;
  1316. /// Collection of per-GlobalValue partitions used in this context.
  1317. DenseMap<const GlobalValue *, StringRef> GlobalValuePartitions;
  1318. DenseMap<const GlobalValue *, GlobalValue::SanitizerMetadata>
  1319. GlobalValueSanitizerMetadata;
  1320. /// DiscriminatorTable - This table maps file:line locations to an
  1321. /// integer representing the next DWARF path discriminator to assign to
  1322. /// instructions in different blocks at the same location.
  1323. DenseMap<std::pair<const char *, unsigned>, unsigned> DiscriminatorTable;
  1324. /// A set of interned tags for operand bundles. The StringMap maps
  1325. /// bundle tags to their IDs.
  1326. ///
  1327. /// \see LLVMContext::getOperandBundleTagID
  1328. StringMap<uint32_t> BundleTagCache;
  1329. StringMapEntry<uint32_t> *getOrInsertBundleTag(StringRef Tag);
  1330. void getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const;
  1331. uint32_t getOperandBundleTagID(StringRef Tag) const;
  1332. /// A set of interned synchronization scopes. The StringMap maps
  1333. /// synchronization scope names to their respective synchronization scope IDs.
  1334. StringMap<SyncScope::ID> SSC;
  1335. /// getOrInsertSyncScopeID - Maps synchronization scope name to
  1336. /// synchronization scope ID. Every synchronization scope registered with
  1337. /// LLVMContext has unique ID except pre-defined ones.
  1338. SyncScope::ID getOrInsertSyncScopeID(StringRef SSN);
  1339. /// getSyncScopeNames - Populates client supplied SmallVector with
  1340. /// synchronization scope names registered with LLVMContext. Synchronization
  1341. /// scope names are ordered by increasing synchronization scope IDs.
  1342. void getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const;
  1343. /// Maintain the GC name for each function.
  1344. ///
  1345. /// This saves allocating an additional word in Function for programs which
  1346. /// do not use GC (i.e., most programs) at the cost of increased overhead for
  1347. /// clients which do use GC.
  1348. DenseMap<const Function *, std::string> GCNames;
  1349. /// Flag to indicate if Value (other than GlobalValue) retains their name or
  1350. /// not.
  1351. bool DiscardValueNames = false;
  1352. LLVMContextImpl(LLVMContext &C);
  1353. ~LLVMContextImpl();
  1354. /// Destroy the ConstantArrays if they are not used.
  1355. void dropTriviallyDeadConstantArrays();
  1356. mutable OptPassGate *OPG = nullptr;
  1357. /// Access the object which can disable optional passes and individual
  1358. /// optimizations at compile time.
  1359. OptPassGate &getOptPassGate() const;
  1360. /// Set the object which can disable optional passes and individual
  1361. /// optimizations at compile time.
  1362. ///
  1363. /// The lifetime of the object must be guaranteed to extend as long as the
  1364. /// LLVMContext is used by compilation.
  1365. void setOptPassGate(OptPassGate &);
  1366. // TODO: clean up the following after we no longer support non-opaque pointer
  1367. // types.
  1368. bool getOpaquePointers();
  1369. void setOpaquePointers(bool OP);
  1370. private:
  1371. std::optional<bool> OpaquePointers;
  1372. };
  1373. } // end namespace llvm
  1374. #endif // LLVM_LIB_IR_LLVMCONTEXTIMPL_H