LLVMContextImpl.h 50 KB

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