DebugInfoMetadata.h 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/IR/DebugInfoMetadata.h - Debug info metadata --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Declarations for metadata specific to debug info.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_DEBUGINFOMETADATA_H
  18. #define LLVM_IR_DEBUGINFOMETADATA_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/BitmaskEnum.h"
  21. #include "llvm/ADT/PointerUnion.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include "llvm/ADT/iterator_range.h"
  26. #include "llvm/IR/Constants.h"
  27. #include "llvm/IR/Metadata.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Discriminator.h"
  31. #include <cassert>
  32. #include <climits>
  33. #include <cstddef>
  34. #include <cstdint>
  35. #include <iterator>
  36. #include <optional>
  37. #include <vector>
  38. // Helper macros for defining get() overrides.
  39. #define DEFINE_MDNODE_GET_UNPACK_IMPL(...) __VA_ARGS__
  40. #define DEFINE_MDNODE_GET_UNPACK(ARGS) DEFINE_MDNODE_GET_UNPACK_IMPL ARGS
  41. #define DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS) \
  42. static CLASS *getDistinct(LLVMContext &Context, \
  43. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  44. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Distinct); \
  45. } \
  46. static Temp##CLASS getTemporary(LLVMContext &Context, \
  47. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  48. return Temp##CLASS( \
  49. getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Temporary)); \
  50. }
  51. #define DEFINE_MDNODE_GET(CLASS, FORMAL, ARGS) \
  52. static CLASS *get(LLVMContext &Context, DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  53. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued); \
  54. } \
  55. static CLASS *getIfExists(LLVMContext &Context, \
  56. DEFINE_MDNODE_GET_UNPACK(FORMAL)) { \
  57. return getImpl(Context, DEFINE_MDNODE_GET_UNPACK(ARGS), Uniqued, \
  58. /* ShouldCreate */ false); \
  59. } \
  60. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(CLASS, FORMAL, ARGS)
  61. namespace llvm {
  62. namespace dwarf {
  63. enum Tag : uint16_t;
  64. }
  65. class DbgVariableIntrinsic;
  66. extern cl::opt<bool> EnableFSDiscriminator;
  67. class DITypeRefArray {
  68. const MDTuple *N = nullptr;
  69. public:
  70. DITypeRefArray() = default;
  71. DITypeRefArray(const MDTuple *N) : N(N) {}
  72. explicit operator bool() const { return get(); }
  73. explicit operator MDTuple *() const { return get(); }
  74. MDTuple *get() const { return const_cast<MDTuple *>(N); }
  75. MDTuple *operator->() const { return get(); }
  76. MDTuple &operator*() const { return *get(); }
  77. // FIXME: Fix callers and remove condition on N.
  78. unsigned size() const { return N ? N->getNumOperands() : 0u; }
  79. DIType *operator[](unsigned I) const {
  80. return cast_or_null<DIType>(N->getOperand(I));
  81. }
  82. class iterator {
  83. MDNode::op_iterator I = nullptr;
  84. public:
  85. using iterator_category = std::input_iterator_tag;
  86. using value_type = DIType *;
  87. using difference_type = std::ptrdiff_t;
  88. using pointer = void;
  89. using reference = DIType *;
  90. iterator() = default;
  91. explicit iterator(MDNode::op_iterator I) : I(I) {}
  92. DIType *operator*() const { return cast_or_null<DIType>(*I); }
  93. iterator &operator++() {
  94. ++I;
  95. return *this;
  96. }
  97. iterator operator++(int) {
  98. iterator Temp(*this);
  99. ++I;
  100. return Temp;
  101. }
  102. bool operator==(const iterator &X) const { return I == X.I; }
  103. bool operator!=(const iterator &X) const { return I != X.I; }
  104. };
  105. // FIXME: Fix callers and remove condition on N.
  106. iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
  107. iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
  108. };
  109. /// Tagged DWARF-like metadata node.
  110. ///
  111. /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
  112. /// defined in llvm/BinaryFormat/Dwarf.h). Called \a DINode because it's
  113. /// potentially used for non-DWARF output.
  114. class DINode : public MDNode {
  115. friend class LLVMContextImpl;
  116. friend class MDNode;
  117. protected:
  118. DINode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  119. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = std::nullopt)
  120. : MDNode(C, ID, Storage, Ops1, Ops2) {
  121. assert(Tag < 1u << 16);
  122. SubclassData16 = Tag;
  123. }
  124. ~DINode() = default;
  125. template <class Ty> Ty *getOperandAs(unsigned I) const {
  126. return cast_or_null<Ty>(getOperand(I));
  127. }
  128. StringRef getStringOperand(unsigned I) const {
  129. if (auto *S = getOperandAs<MDString>(I))
  130. return S->getString();
  131. return StringRef();
  132. }
  133. static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  134. if (S.empty())
  135. return nullptr;
  136. return MDString::get(Context, S);
  137. }
  138. /// Allow subclasses to mutate the tag.
  139. void setTag(unsigned Tag) { SubclassData16 = Tag; }
  140. public:
  141. dwarf::Tag getTag() const;
  142. /// Debug info flags.
  143. ///
  144. /// The three accessibility flags are mutually exclusive and rolled together
  145. /// in the first two bits.
  146. enum DIFlags : uint32_t {
  147. #define HANDLE_DI_FLAG(ID, NAME) Flag##NAME = ID,
  148. #define DI_FLAG_LARGEST_NEEDED
  149. #include "llvm/IR/DebugInfoFlags.def"
  150. FlagAccessibility = FlagPrivate | FlagProtected | FlagPublic,
  151. FlagPtrToMemberRep = FlagSingleInheritance | FlagMultipleInheritance |
  152. FlagVirtualInheritance,
  153. LLVM_MARK_AS_BITMASK_ENUM(FlagLargest)
  154. };
  155. static DIFlags getFlag(StringRef Flag);
  156. static StringRef getFlagString(DIFlags Flag);
  157. /// Split up a flags bitfield.
  158. ///
  159. /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
  160. /// any remaining (unrecognized) bits.
  161. static DIFlags splitFlags(DIFlags Flags,
  162. SmallVectorImpl<DIFlags> &SplitFlags);
  163. static bool classof(const Metadata *MD) {
  164. switch (MD->getMetadataID()) {
  165. default:
  166. return false;
  167. case GenericDINodeKind:
  168. case DISubrangeKind:
  169. case DIEnumeratorKind:
  170. case DIBasicTypeKind:
  171. case DIStringTypeKind:
  172. case DIDerivedTypeKind:
  173. case DICompositeTypeKind:
  174. case DISubroutineTypeKind:
  175. case DIFileKind:
  176. case DICompileUnitKind:
  177. case DISubprogramKind:
  178. case DILexicalBlockKind:
  179. case DILexicalBlockFileKind:
  180. case DINamespaceKind:
  181. case DICommonBlockKind:
  182. case DITemplateTypeParameterKind:
  183. case DITemplateValueParameterKind:
  184. case DIGlobalVariableKind:
  185. case DILocalVariableKind:
  186. case DILabelKind:
  187. case DIObjCPropertyKind:
  188. case DIImportedEntityKind:
  189. case DIModuleKind:
  190. case DIGenericSubrangeKind:
  191. case DIAssignIDKind:
  192. return true;
  193. }
  194. }
  195. };
  196. /// Generic tagged DWARF-like metadata node.
  197. ///
  198. /// An un-specialized DWARF-like metadata node. The first operand is a
  199. /// (possibly empty) null-separated \a MDString header that contains arbitrary
  200. /// fields. The remaining operands are \a dwarf_operands(), and are pointers
  201. /// to other metadata.
  202. class GenericDINode : public DINode {
  203. friend class LLVMContextImpl;
  204. friend class MDNode;
  205. GenericDINode(LLVMContext &C, StorageType Storage, unsigned Hash,
  206. unsigned Tag, ArrayRef<Metadata *> Ops1,
  207. ArrayRef<Metadata *> Ops2)
  208. : DINode(C, GenericDINodeKind, Storage, Tag, Ops1, Ops2) {
  209. setHash(Hash);
  210. }
  211. ~GenericDINode() { dropAllReferences(); }
  212. void setHash(unsigned Hash) { SubclassData32 = Hash; }
  213. void recalculateHash();
  214. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  215. StringRef Header, ArrayRef<Metadata *> DwarfOps,
  216. StorageType Storage, bool ShouldCreate = true) {
  217. return getImpl(Context, Tag, getCanonicalMDString(Context, Header),
  218. DwarfOps, Storage, ShouldCreate);
  219. }
  220. static GenericDINode *getImpl(LLVMContext &Context, unsigned Tag,
  221. MDString *Header, ArrayRef<Metadata *> DwarfOps,
  222. StorageType Storage, bool ShouldCreate = true);
  223. TempGenericDINode cloneImpl() const {
  224. return getTemporary(getContext(), getTag(), getHeader(),
  225. SmallVector<Metadata *, 4>(dwarf_operands()));
  226. }
  227. public:
  228. unsigned getHash() const { return SubclassData32; }
  229. DEFINE_MDNODE_GET(GenericDINode,
  230. (unsigned Tag, StringRef Header,
  231. ArrayRef<Metadata *> DwarfOps),
  232. (Tag, Header, DwarfOps))
  233. DEFINE_MDNODE_GET(GenericDINode,
  234. (unsigned Tag, MDString *Header,
  235. ArrayRef<Metadata *> DwarfOps),
  236. (Tag, Header, DwarfOps))
  237. /// Return a (temporary) clone of this.
  238. TempGenericDINode clone() const { return cloneImpl(); }
  239. dwarf::Tag getTag() const;
  240. StringRef getHeader() const { return getStringOperand(0); }
  241. MDString *getRawHeader() const { return getOperandAs<MDString>(0); }
  242. op_iterator dwarf_op_begin() const { return op_begin() + 1; }
  243. op_iterator dwarf_op_end() const { return op_end(); }
  244. op_range dwarf_operands() const {
  245. return op_range(dwarf_op_begin(), dwarf_op_end());
  246. }
  247. unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
  248. const MDOperand &getDwarfOperand(unsigned I) const {
  249. return getOperand(I + 1);
  250. }
  251. void replaceDwarfOperandWith(unsigned I, Metadata *New) {
  252. replaceOperandWith(I + 1, New);
  253. }
  254. static bool classof(const Metadata *MD) {
  255. return MD->getMetadataID() == GenericDINodeKind;
  256. }
  257. };
  258. /// Assignment ID.
  259. /// Used to link stores (as an attachment) and dbg.assigns (as an operand).
  260. /// DIAssignID metadata is never uniqued as we compare instances using
  261. /// referential equality (the instance/address is the ID).
  262. class DIAssignID : public MDNode {
  263. friend class LLVMContextImpl;
  264. friend class MDNode;
  265. DIAssignID(LLVMContext &C, StorageType Storage)
  266. : MDNode(C, DIAssignIDKind, Storage, std::nullopt) {}
  267. ~DIAssignID() { dropAllReferences(); }
  268. static DIAssignID *getImpl(LLVMContext &Context, StorageType Storage,
  269. bool ShouldCreate = true);
  270. TempDIAssignID cloneImpl() const { return getTemporary(getContext()); }
  271. public:
  272. // This node has no operands to replace.
  273. void replaceOperandWith(unsigned I, Metadata *New) = delete;
  274. static DIAssignID *getDistinct(LLVMContext &Context) {
  275. return getImpl(Context, Distinct);
  276. }
  277. static TempDIAssignID getTemporary(LLVMContext &Context) {
  278. return TempDIAssignID(getImpl(Context, Temporary));
  279. }
  280. // NOTE: Do not define get(LLVMContext&) - see class comment.
  281. static bool classof(const Metadata *MD) {
  282. return MD->getMetadataID() == DIAssignIDKind;
  283. }
  284. };
  285. /// Array subrange.
  286. ///
  287. /// TODO: Merge into node for DW_TAG_array_type, which should have a custom
  288. /// type.
  289. class DISubrange : public DINode {
  290. friend class LLVMContextImpl;
  291. friend class MDNode;
  292. DISubrange(LLVMContext &C, StorageType Storage, ArrayRef<Metadata *> Ops);
  293. ~DISubrange() = default;
  294. static DISubrange *getImpl(LLVMContext &Context, int64_t Count,
  295. int64_t LowerBound, StorageType Storage,
  296. bool ShouldCreate = true);
  297. static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  298. int64_t LowerBound, StorageType Storage,
  299. bool ShouldCreate = true);
  300. static DISubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  301. Metadata *LowerBound, Metadata *UpperBound,
  302. Metadata *Stride, StorageType Storage,
  303. bool ShouldCreate = true);
  304. TempDISubrange cloneImpl() const {
  305. return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
  306. getRawUpperBound(), getRawStride());
  307. }
  308. public:
  309. DEFINE_MDNODE_GET(DISubrange, (int64_t Count, int64_t LowerBound = 0),
  310. (Count, LowerBound))
  311. DEFINE_MDNODE_GET(DISubrange, (Metadata * CountNode, int64_t LowerBound = 0),
  312. (CountNode, LowerBound))
  313. DEFINE_MDNODE_GET(DISubrange,
  314. (Metadata * CountNode, Metadata *LowerBound,
  315. Metadata *UpperBound, Metadata *Stride),
  316. (CountNode, LowerBound, UpperBound, Stride))
  317. TempDISubrange clone() const { return cloneImpl(); }
  318. Metadata *getRawCountNode() const { return getOperand(0).get(); }
  319. Metadata *getRawLowerBound() const { return getOperand(1).get(); }
  320. Metadata *getRawUpperBound() const { return getOperand(2).get(); }
  321. Metadata *getRawStride() const { return getOperand(3).get(); }
  322. typedef PointerUnion<ConstantInt *, DIVariable *, DIExpression *> BoundType;
  323. BoundType getCount() const;
  324. BoundType getLowerBound() const;
  325. BoundType getUpperBound() const;
  326. BoundType getStride() const;
  327. static bool classof(const Metadata *MD) {
  328. return MD->getMetadataID() == DISubrangeKind;
  329. }
  330. };
  331. class DIGenericSubrange : public DINode {
  332. friend class LLVMContextImpl;
  333. friend class MDNode;
  334. DIGenericSubrange(LLVMContext &C, StorageType Storage,
  335. ArrayRef<Metadata *> Ops);
  336. ~DIGenericSubrange() = default;
  337. static DIGenericSubrange *getImpl(LLVMContext &Context, Metadata *CountNode,
  338. Metadata *LowerBound, Metadata *UpperBound,
  339. Metadata *Stride, StorageType Storage,
  340. bool ShouldCreate = true);
  341. TempDIGenericSubrange cloneImpl() const {
  342. return getTemporary(getContext(), getRawCountNode(), getRawLowerBound(),
  343. getRawUpperBound(), getRawStride());
  344. }
  345. public:
  346. DEFINE_MDNODE_GET(DIGenericSubrange,
  347. (Metadata * CountNode, Metadata *LowerBound,
  348. Metadata *UpperBound, Metadata *Stride),
  349. (CountNode, LowerBound, UpperBound, Stride))
  350. TempDIGenericSubrange clone() const { return cloneImpl(); }
  351. Metadata *getRawCountNode() const { return getOperand(0).get(); }
  352. Metadata *getRawLowerBound() const { return getOperand(1).get(); }
  353. Metadata *getRawUpperBound() const { return getOperand(2).get(); }
  354. Metadata *getRawStride() const { return getOperand(3).get(); }
  355. using BoundType = PointerUnion<DIVariable *, DIExpression *>;
  356. BoundType getCount() const;
  357. BoundType getLowerBound() const;
  358. BoundType getUpperBound() const;
  359. BoundType getStride() const;
  360. static bool classof(const Metadata *MD) {
  361. return MD->getMetadataID() == DIGenericSubrangeKind;
  362. }
  363. };
  364. /// Enumeration value.
  365. ///
  366. /// TODO: Add a pointer to the context (DW_TAG_enumeration_type) once that no
  367. /// longer creates a type cycle.
  368. class DIEnumerator : public DINode {
  369. friend class LLVMContextImpl;
  370. friend class MDNode;
  371. APInt Value;
  372. DIEnumerator(LLVMContext &C, StorageType Storage, const APInt &Value,
  373. bool IsUnsigned, ArrayRef<Metadata *> Ops);
  374. DIEnumerator(LLVMContext &C, StorageType Storage, int64_t Value,
  375. bool IsUnsigned, ArrayRef<Metadata *> Ops)
  376. : DIEnumerator(C, Storage, APInt(64, Value, !IsUnsigned), IsUnsigned,
  377. Ops) {}
  378. ~DIEnumerator() = default;
  379. static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
  380. bool IsUnsigned, StringRef Name,
  381. StorageType Storage, bool ShouldCreate = true) {
  382. return getImpl(Context, Value, IsUnsigned,
  383. getCanonicalMDString(Context, Name), Storage, ShouldCreate);
  384. }
  385. static DIEnumerator *getImpl(LLVMContext &Context, const APInt &Value,
  386. bool IsUnsigned, MDString *Name,
  387. StorageType Storage, bool ShouldCreate = true);
  388. TempDIEnumerator cloneImpl() const {
  389. return getTemporary(getContext(), getValue(), isUnsigned(), getName());
  390. }
  391. public:
  392. DEFINE_MDNODE_GET(DIEnumerator,
  393. (int64_t Value, bool IsUnsigned, StringRef Name),
  394. (APInt(64, Value, !IsUnsigned), IsUnsigned, Name))
  395. DEFINE_MDNODE_GET(DIEnumerator,
  396. (int64_t Value, bool IsUnsigned, MDString *Name),
  397. (APInt(64, Value, !IsUnsigned), IsUnsigned, Name))
  398. DEFINE_MDNODE_GET(DIEnumerator,
  399. (APInt Value, bool IsUnsigned, StringRef Name),
  400. (Value, IsUnsigned, Name))
  401. DEFINE_MDNODE_GET(DIEnumerator,
  402. (APInt Value, bool IsUnsigned, MDString *Name),
  403. (Value, IsUnsigned, Name))
  404. TempDIEnumerator clone() const { return cloneImpl(); }
  405. const APInt &getValue() const { return Value; }
  406. bool isUnsigned() const { return SubclassData32; }
  407. StringRef getName() const { return getStringOperand(0); }
  408. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  409. static bool classof(const Metadata *MD) {
  410. return MD->getMetadataID() == DIEnumeratorKind;
  411. }
  412. };
  413. /// Base class for scope-like contexts.
  414. ///
  415. /// Base class for lexical scopes and types (which are also declaration
  416. /// contexts).
  417. ///
  418. /// TODO: Separate the concepts of declaration contexts and lexical scopes.
  419. class DIScope : public DINode {
  420. protected:
  421. DIScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  422. ArrayRef<Metadata *> Ops)
  423. : DINode(C, ID, Storage, Tag, Ops) {}
  424. ~DIScope() = default;
  425. public:
  426. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  427. inline StringRef getFilename() const;
  428. inline StringRef getDirectory() const;
  429. inline std::optional<StringRef> getSource() const;
  430. StringRef getName() const;
  431. DIScope *getScope() const;
  432. /// Return the raw underlying file.
  433. ///
  434. /// A \a DIFile is a \a DIScope, but it doesn't point at a separate file (it
  435. /// \em is the file). If \c this is an \a DIFile, we need to return \c this.
  436. /// Otherwise, return the first operand, which is where all other subclasses
  437. /// store their file pointer.
  438. Metadata *getRawFile() const {
  439. return isa<DIFile>(this) ? const_cast<DIScope *>(this)
  440. : static_cast<Metadata *>(getOperand(0));
  441. }
  442. static bool classof(const Metadata *MD) {
  443. switch (MD->getMetadataID()) {
  444. default:
  445. return false;
  446. case DIBasicTypeKind:
  447. case DIStringTypeKind:
  448. case DIDerivedTypeKind:
  449. case DICompositeTypeKind:
  450. case DISubroutineTypeKind:
  451. case DIFileKind:
  452. case DICompileUnitKind:
  453. case DISubprogramKind:
  454. case DILexicalBlockKind:
  455. case DILexicalBlockFileKind:
  456. case DINamespaceKind:
  457. case DICommonBlockKind:
  458. case DIModuleKind:
  459. return true;
  460. }
  461. }
  462. };
  463. /// File.
  464. ///
  465. /// TODO: Merge with directory/file node (including users).
  466. /// TODO: Canonicalize paths on creation.
  467. class DIFile : public DIScope {
  468. friend class LLVMContextImpl;
  469. friend class MDNode;
  470. public:
  471. /// Which algorithm (e.g. MD5) a checksum was generated with.
  472. ///
  473. /// The encoding is explicit because it is used directly in Bitcode. The
  474. /// value 0 is reserved to indicate the absence of a checksum in Bitcode.
  475. enum ChecksumKind {
  476. // The first variant was originally CSK_None, encoded as 0. The new
  477. // internal representation removes the need for this by wrapping the
  478. // ChecksumInfo in an Optional, but to preserve Bitcode compatibility the 0
  479. // encoding is reserved.
  480. CSK_MD5 = 1,
  481. CSK_SHA1 = 2,
  482. CSK_SHA256 = 3,
  483. CSK_Last = CSK_SHA256 // Should be last enumeration.
  484. };
  485. /// A single checksum, represented by a \a Kind and a \a Value (a string).
  486. template <typename T> struct ChecksumInfo {
  487. /// The kind of checksum which \a Value encodes.
  488. ChecksumKind Kind;
  489. /// The string value of the checksum.
  490. T Value;
  491. ChecksumInfo(ChecksumKind Kind, T Value) : Kind(Kind), Value(Value) {}
  492. ~ChecksumInfo() = default;
  493. bool operator==(const ChecksumInfo<T> &X) const {
  494. return Kind == X.Kind && Value == X.Value;
  495. }
  496. bool operator!=(const ChecksumInfo<T> &X) const { return !(*this == X); }
  497. StringRef getKindAsString() const { return getChecksumKindAsString(Kind); }
  498. };
  499. private:
  500. std::optional<ChecksumInfo<MDString *>> Checksum;
  501. /// An optional source. A nullptr means none.
  502. MDString *Source;
  503. DIFile(LLVMContext &C, StorageType Storage,
  504. std::optional<ChecksumInfo<MDString *>> CS, MDString *Src,
  505. ArrayRef<Metadata *> Ops);
  506. ~DIFile() = default;
  507. static DIFile *getImpl(LLVMContext &Context, StringRef Filename,
  508. StringRef Directory,
  509. std::optional<ChecksumInfo<StringRef>> CS,
  510. std::optional<StringRef> Source, StorageType Storage,
  511. bool ShouldCreate = true) {
  512. std::optional<ChecksumInfo<MDString *>> MDChecksum;
  513. if (CS)
  514. MDChecksum.emplace(CS->Kind, getCanonicalMDString(Context, CS->Value));
  515. return getImpl(Context, getCanonicalMDString(Context, Filename),
  516. getCanonicalMDString(Context, Directory), MDChecksum,
  517. Source ? MDString::get(Context, *Source) : nullptr, Storage,
  518. ShouldCreate);
  519. }
  520. static DIFile *getImpl(LLVMContext &Context, MDString *Filename,
  521. MDString *Directory,
  522. std::optional<ChecksumInfo<MDString *>> CS,
  523. MDString *Source, StorageType Storage,
  524. bool ShouldCreate = true);
  525. TempDIFile cloneImpl() const {
  526. return getTemporary(getContext(), getFilename(), getDirectory(),
  527. getChecksum(), getSource());
  528. }
  529. public:
  530. DEFINE_MDNODE_GET(DIFile,
  531. (StringRef Filename, StringRef Directory,
  532. std::optional<ChecksumInfo<StringRef>> CS = std::nullopt,
  533. std::optional<StringRef> Source = std::nullopt),
  534. (Filename, Directory, CS, Source))
  535. DEFINE_MDNODE_GET(DIFile,
  536. (MDString * Filename, MDString *Directory,
  537. std::optional<ChecksumInfo<MDString *>> CS = std::nullopt,
  538. MDString *Source = nullptr),
  539. (Filename, Directory, CS, Source))
  540. TempDIFile clone() const { return cloneImpl(); }
  541. StringRef getFilename() const { return getStringOperand(0); }
  542. StringRef getDirectory() const { return getStringOperand(1); }
  543. std::optional<ChecksumInfo<StringRef>> getChecksum() const {
  544. std::optional<ChecksumInfo<StringRef>> StringRefChecksum;
  545. if (Checksum)
  546. StringRefChecksum.emplace(Checksum->Kind, Checksum->Value->getString());
  547. return StringRefChecksum;
  548. }
  549. std::optional<StringRef> getSource() const {
  550. return Source ? std::optional<StringRef>(Source->getString())
  551. : std::nullopt;
  552. }
  553. MDString *getRawFilename() const { return getOperandAs<MDString>(0); }
  554. MDString *getRawDirectory() const { return getOperandAs<MDString>(1); }
  555. std::optional<ChecksumInfo<MDString *>> getRawChecksum() const {
  556. return Checksum;
  557. }
  558. MDString *getRawSource() const { return Source; }
  559. static StringRef getChecksumKindAsString(ChecksumKind CSKind);
  560. static std::optional<ChecksumKind> getChecksumKind(StringRef CSKindStr);
  561. static bool classof(const Metadata *MD) {
  562. return MD->getMetadataID() == DIFileKind;
  563. }
  564. };
  565. StringRef DIScope::getFilename() const {
  566. if (auto *F = getFile())
  567. return F->getFilename();
  568. return "";
  569. }
  570. StringRef DIScope::getDirectory() const {
  571. if (auto *F = getFile())
  572. return F->getDirectory();
  573. return "";
  574. }
  575. std::optional<StringRef> DIScope::getSource() const {
  576. if (auto *F = getFile())
  577. return F->getSource();
  578. return std::nullopt;
  579. }
  580. /// Base class for types.
  581. ///
  582. /// TODO: Remove the hardcoded name and context, since many types don't use
  583. /// them.
  584. /// TODO: Split up flags.
  585. class DIType : public DIScope {
  586. unsigned Line;
  587. DIFlags Flags;
  588. uint64_t SizeInBits;
  589. uint64_t OffsetInBits;
  590. uint32_t AlignInBits;
  591. protected:
  592. DIType(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  593. unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  594. uint64_t OffsetInBits, DIFlags Flags, ArrayRef<Metadata *> Ops)
  595. : DIScope(C, ID, Storage, Tag, Ops) {
  596. init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  597. }
  598. ~DIType() = default;
  599. void init(unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  600. uint64_t OffsetInBits, DIFlags Flags) {
  601. this->Line = Line;
  602. this->Flags = Flags;
  603. this->SizeInBits = SizeInBits;
  604. this->AlignInBits = AlignInBits;
  605. this->OffsetInBits = OffsetInBits;
  606. }
  607. /// Change fields in place.
  608. void mutate(unsigned Tag, unsigned Line, uint64_t SizeInBits,
  609. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags) {
  610. assert(isDistinct() && "Only distinct nodes can mutate");
  611. setTag(Tag);
  612. init(Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  613. }
  614. public:
  615. TempDIType clone() const {
  616. return TempDIType(cast<DIType>(MDNode::clone().release()));
  617. }
  618. unsigned getLine() const { return Line; }
  619. uint64_t getSizeInBits() const { return SizeInBits; }
  620. uint32_t getAlignInBits() const { return AlignInBits; }
  621. uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
  622. uint64_t getOffsetInBits() const { return OffsetInBits; }
  623. DIFlags getFlags() const { return Flags; }
  624. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  625. StringRef getName() const { return getStringOperand(2); }
  626. Metadata *getRawScope() const { return getOperand(1); }
  627. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  628. /// Returns a new temporary DIType with updated Flags
  629. TempDIType cloneWithFlags(DIFlags NewFlags) const {
  630. auto NewTy = clone();
  631. NewTy->Flags = NewFlags;
  632. return NewTy;
  633. }
  634. bool isPrivate() const {
  635. return (getFlags() & FlagAccessibility) == FlagPrivate;
  636. }
  637. bool isProtected() const {
  638. return (getFlags() & FlagAccessibility) == FlagProtected;
  639. }
  640. bool isPublic() const {
  641. return (getFlags() & FlagAccessibility) == FlagPublic;
  642. }
  643. bool isForwardDecl() const { return getFlags() & FlagFwdDecl; }
  644. bool isAppleBlockExtension() const { return getFlags() & FlagAppleBlock; }
  645. bool isVirtual() const { return getFlags() & FlagVirtual; }
  646. bool isArtificial() const { return getFlags() & FlagArtificial; }
  647. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  648. bool isObjcClassComplete() const {
  649. return getFlags() & FlagObjcClassComplete;
  650. }
  651. bool isVector() const { return getFlags() & FlagVector; }
  652. bool isBitField() const { return getFlags() & FlagBitField; }
  653. bool isStaticMember() const { return getFlags() & FlagStaticMember; }
  654. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  655. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  656. bool isTypePassByValue() const { return getFlags() & FlagTypePassByValue; }
  657. bool isTypePassByReference() const {
  658. return getFlags() & FlagTypePassByReference;
  659. }
  660. bool isBigEndian() const { return getFlags() & FlagBigEndian; }
  661. bool isLittleEndian() const { return getFlags() & FlagLittleEndian; }
  662. bool getExportSymbols() const { return getFlags() & FlagExportSymbols; }
  663. static bool classof(const Metadata *MD) {
  664. switch (MD->getMetadataID()) {
  665. default:
  666. return false;
  667. case DIBasicTypeKind:
  668. case DIStringTypeKind:
  669. case DIDerivedTypeKind:
  670. case DICompositeTypeKind:
  671. case DISubroutineTypeKind:
  672. return true;
  673. }
  674. }
  675. };
  676. /// Basic type, like 'int' or 'float'.
  677. ///
  678. /// TODO: Split out DW_TAG_unspecified_type.
  679. /// TODO: Drop unused accessors.
  680. class DIBasicType : public DIType {
  681. friend class LLVMContextImpl;
  682. friend class MDNode;
  683. unsigned Encoding;
  684. DIBasicType(LLVMContext &C, StorageType Storage, unsigned Tag,
  685. uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
  686. DIFlags Flags, ArrayRef<Metadata *> Ops)
  687. : DIType(C, DIBasicTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  688. Flags, Ops),
  689. Encoding(Encoding) {}
  690. ~DIBasicType() = default;
  691. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  692. StringRef Name, uint64_t SizeInBits,
  693. uint32_t AlignInBits, unsigned Encoding,
  694. DIFlags Flags, StorageType Storage,
  695. bool ShouldCreate = true) {
  696. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  697. SizeInBits, AlignInBits, Encoding, Flags, Storage,
  698. ShouldCreate);
  699. }
  700. static DIBasicType *getImpl(LLVMContext &Context, unsigned Tag,
  701. MDString *Name, uint64_t SizeInBits,
  702. uint32_t AlignInBits, unsigned Encoding,
  703. DIFlags Flags, StorageType Storage,
  704. bool ShouldCreate = true);
  705. TempDIBasicType cloneImpl() const {
  706. return getTemporary(getContext(), getTag(), getName(), getSizeInBits(),
  707. getAlignInBits(), getEncoding(), getFlags());
  708. }
  709. public:
  710. DEFINE_MDNODE_GET(DIBasicType, (unsigned Tag, StringRef Name),
  711. (Tag, Name, 0, 0, 0, FlagZero))
  712. DEFINE_MDNODE_GET(DIBasicType,
  713. (unsigned Tag, StringRef Name, uint64_t SizeInBits),
  714. (Tag, Name, SizeInBits, 0, 0, FlagZero))
  715. DEFINE_MDNODE_GET(DIBasicType,
  716. (unsigned Tag, MDString *Name, uint64_t SizeInBits),
  717. (Tag, Name, SizeInBits, 0, 0, FlagZero))
  718. DEFINE_MDNODE_GET(DIBasicType,
  719. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  720. uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
  721. (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
  722. DEFINE_MDNODE_GET(DIBasicType,
  723. (unsigned Tag, MDString *Name, uint64_t SizeInBits,
  724. uint32_t AlignInBits, unsigned Encoding, DIFlags Flags),
  725. (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags))
  726. TempDIBasicType clone() const { return cloneImpl(); }
  727. unsigned getEncoding() const { return Encoding; }
  728. enum class Signedness { Signed, Unsigned };
  729. /// Return the signedness of this type, or std::nullopt if this type is
  730. /// neither signed nor unsigned.
  731. std::optional<Signedness> getSignedness() const;
  732. static bool classof(const Metadata *MD) {
  733. return MD->getMetadataID() == DIBasicTypeKind;
  734. }
  735. };
  736. /// String type, Fortran CHARACTER(n)
  737. class DIStringType : public DIType {
  738. friend class LLVMContextImpl;
  739. friend class MDNode;
  740. unsigned Encoding;
  741. DIStringType(LLVMContext &C, StorageType Storage, unsigned Tag,
  742. uint64_t SizeInBits, uint32_t AlignInBits, unsigned Encoding,
  743. ArrayRef<Metadata *> Ops)
  744. : DIType(C, DIStringTypeKind, Storage, Tag, 0, SizeInBits, AlignInBits, 0,
  745. FlagZero, Ops),
  746. Encoding(Encoding) {}
  747. ~DIStringType() = default;
  748. static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
  749. StringRef Name, Metadata *StringLength,
  750. Metadata *StrLenExp, Metadata *StrLocationExp,
  751. uint64_t SizeInBits, uint32_t AlignInBits,
  752. unsigned Encoding, StorageType Storage,
  753. bool ShouldCreate = true) {
  754. return getImpl(Context, Tag, getCanonicalMDString(Context, Name),
  755. StringLength, StrLenExp, StrLocationExp, SizeInBits,
  756. AlignInBits, Encoding, Storage, ShouldCreate);
  757. }
  758. static DIStringType *getImpl(LLVMContext &Context, unsigned Tag,
  759. MDString *Name, Metadata *StringLength,
  760. Metadata *StrLenExp, Metadata *StrLocationExp,
  761. uint64_t SizeInBits, uint32_t AlignInBits,
  762. unsigned Encoding, StorageType Storage,
  763. bool ShouldCreate = true);
  764. TempDIStringType cloneImpl() const {
  765. return getTemporary(getContext(), getTag(), getRawName(),
  766. getRawStringLength(), getRawStringLengthExp(),
  767. getRawStringLocationExp(), getSizeInBits(),
  768. getAlignInBits(), getEncoding());
  769. }
  770. public:
  771. DEFINE_MDNODE_GET(DIStringType,
  772. (unsigned Tag, StringRef Name, uint64_t SizeInBits,
  773. uint32_t AlignInBits),
  774. (Tag, Name, nullptr, nullptr, nullptr, SizeInBits,
  775. AlignInBits, 0))
  776. DEFINE_MDNODE_GET(DIStringType,
  777. (unsigned Tag, MDString *Name, Metadata *StringLength,
  778. Metadata *StringLengthExp, Metadata *StringLocationExp,
  779. uint64_t SizeInBits, uint32_t AlignInBits,
  780. unsigned Encoding),
  781. (Tag, Name, StringLength, StringLengthExp,
  782. StringLocationExp, SizeInBits, AlignInBits, Encoding))
  783. DEFINE_MDNODE_GET(DIStringType,
  784. (unsigned Tag, StringRef Name, Metadata *StringLength,
  785. Metadata *StringLengthExp, Metadata *StringLocationExp,
  786. uint64_t SizeInBits, uint32_t AlignInBits,
  787. unsigned Encoding),
  788. (Tag, Name, StringLength, StringLengthExp,
  789. StringLocationExp, SizeInBits, AlignInBits, Encoding))
  790. TempDIStringType clone() const { return cloneImpl(); }
  791. static bool classof(const Metadata *MD) {
  792. return MD->getMetadataID() == DIStringTypeKind;
  793. }
  794. DIVariable *getStringLength() const {
  795. return cast_or_null<DIVariable>(getRawStringLength());
  796. }
  797. DIExpression *getStringLengthExp() const {
  798. return cast_or_null<DIExpression>(getRawStringLengthExp());
  799. }
  800. DIExpression *getStringLocationExp() const {
  801. return cast_or_null<DIExpression>(getRawStringLocationExp());
  802. }
  803. unsigned getEncoding() const { return Encoding; }
  804. Metadata *getRawStringLength() const { return getOperand(3); }
  805. Metadata *getRawStringLengthExp() const { return getOperand(4); }
  806. Metadata *getRawStringLocationExp() const { return getOperand(5); }
  807. };
  808. /// Derived types.
  809. ///
  810. /// This includes qualified types, pointers, references, friends, typedefs, and
  811. /// class members.
  812. ///
  813. /// TODO: Split out members (inheritance, fields, methods, etc.).
  814. class DIDerivedType : public DIType {
  815. friend class LLVMContextImpl;
  816. friend class MDNode;
  817. /// The DWARF address space of the memory pointed to or referenced by a
  818. /// pointer or reference type respectively.
  819. std::optional<unsigned> DWARFAddressSpace;
  820. DIDerivedType(LLVMContext &C, StorageType Storage, unsigned Tag,
  821. unsigned Line, uint64_t SizeInBits, uint32_t AlignInBits,
  822. uint64_t OffsetInBits,
  823. std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  824. ArrayRef<Metadata *> Ops)
  825. : DIType(C, DIDerivedTypeKind, Storage, Tag, Line, SizeInBits,
  826. AlignInBits, OffsetInBits, Flags, Ops),
  827. DWARFAddressSpace(DWARFAddressSpace) {}
  828. ~DIDerivedType() = default;
  829. static DIDerivedType *
  830. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, DIFile *File,
  831. unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  832. uint32_t AlignInBits, uint64_t OffsetInBits,
  833. std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  834. Metadata *ExtraData, DINodeArray Annotations, StorageType Storage,
  835. bool ShouldCreate = true) {
  836. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), File,
  837. Line, Scope, BaseType, SizeInBits, AlignInBits, OffsetInBits,
  838. DWARFAddressSpace, Flags, ExtraData, Annotations.get(),
  839. Storage, ShouldCreate);
  840. }
  841. static DIDerivedType *
  842. getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  843. unsigned Line, Metadata *Scope, Metadata *BaseType,
  844. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  845. std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  846. Metadata *ExtraData, Metadata *Annotations, StorageType Storage,
  847. bool ShouldCreate = true);
  848. TempDIDerivedType cloneImpl() const {
  849. return getTemporary(
  850. getContext(), getTag(), getName(), getFile(), getLine(), getScope(),
  851. getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(),
  852. getDWARFAddressSpace(), getFlags(), getExtraData(), getAnnotations());
  853. }
  854. public:
  855. DEFINE_MDNODE_GET(
  856. DIDerivedType,
  857. (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
  858. Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  859. uint32_t AlignInBits, uint64_t OffsetInBits,
  860. std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  861. Metadata *ExtraData = nullptr, Metadata *Annotations = nullptr),
  862. (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  863. OffsetInBits, DWARFAddressSpace, Flags, ExtraData, Annotations))
  864. DEFINE_MDNODE_GET(DIDerivedType,
  865. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  866. DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  867. uint32_t AlignInBits, uint64_t OffsetInBits,
  868. std::optional<unsigned> DWARFAddressSpace, DIFlags Flags,
  869. Metadata *ExtraData = nullptr,
  870. DINodeArray Annotations = nullptr),
  871. (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  872. AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
  873. ExtraData, Annotations))
  874. TempDIDerivedType clone() const { return cloneImpl(); }
  875. /// Get the base type this is derived from.
  876. DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
  877. Metadata *getRawBaseType() const { return getOperand(3); }
  878. /// \returns The DWARF address space of the memory pointed to or referenced by
  879. /// a pointer or reference type respectively.
  880. std::optional<unsigned> getDWARFAddressSpace() const {
  881. return DWARFAddressSpace;
  882. }
  883. /// Get extra data associated with this derived type.
  884. ///
  885. /// Class type for pointer-to-members, objective-c property node for ivars,
  886. /// global constant wrapper for static members, or virtual base pointer offset
  887. /// for inheritance.
  888. ///
  889. /// TODO: Separate out types that need this extra operand: pointer-to-member
  890. /// types and member fields (static members and ivars).
  891. Metadata *getExtraData() const { return getRawExtraData(); }
  892. Metadata *getRawExtraData() const { return getOperand(4); }
  893. /// Get annotations associated with this derived type.
  894. DINodeArray getAnnotations() const {
  895. return cast_or_null<MDTuple>(getRawAnnotations());
  896. }
  897. Metadata *getRawAnnotations() const { return getOperand(5); }
  898. /// Get casted version of extra data.
  899. /// @{
  900. DIType *getClassType() const;
  901. DIObjCProperty *getObjCProperty() const {
  902. return dyn_cast_or_null<DIObjCProperty>(getExtraData());
  903. }
  904. uint32_t getVBPtrOffset() const;
  905. Constant *getStorageOffsetInBits() const;
  906. Constant *getConstant() const;
  907. Constant *getDiscriminantValue() const;
  908. /// @}
  909. static bool classof(const Metadata *MD) {
  910. return MD->getMetadataID() == DIDerivedTypeKind;
  911. }
  912. };
  913. /// Composite types.
  914. ///
  915. /// TODO: Detach from DerivedTypeBase (split out MDEnumType?).
  916. /// TODO: Create a custom, unrelated node for DW_TAG_array_type.
  917. class DICompositeType : public DIType {
  918. friend class LLVMContextImpl;
  919. friend class MDNode;
  920. unsigned RuntimeLang;
  921. DICompositeType(LLVMContext &C, StorageType Storage, unsigned Tag,
  922. unsigned Line, unsigned RuntimeLang, uint64_t SizeInBits,
  923. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  924. ArrayRef<Metadata *> Ops)
  925. : DIType(C, DICompositeTypeKind, Storage, Tag, Line, SizeInBits,
  926. AlignInBits, OffsetInBits, Flags, Ops),
  927. RuntimeLang(RuntimeLang) {}
  928. ~DICompositeType() = default;
  929. /// Change fields in place.
  930. void mutate(unsigned Tag, unsigned Line, unsigned RuntimeLang,
  931. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  932. DIFlags Flags) {
  933. assert(isDistinct() && "Only distinct nodes can mutate");
  934. assert(getRawIdentifier() && "Only ODR-uniqued nodes should mutate");
  935. this->RuntimeLang = RuntimeLang;
  936. DIType::mutate(Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags);
  937. }
  938. static DICompositeType *
  939. getImpl(LLVMContext &Context, unsigned Tag, StringRef Name, Metadata *File,
  940. unsigned Line, DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  941. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  942. DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
  943. DITemplateParameterArray TemplateParams, StringRef Identifier,
  944. DIDerivedType *Discriminator, Metadata *DataLocation,
  945. Metadata *Associated, Metadata *Allocated, Metadata *Rank,
  946. DINodeArray Annotations, StorageType Storage,
  947. bool ShouldCreate = true) {
  948. return getImpl(
  949. Context, Tag, getCanonicalMDString(Context, Name), File, Line, Scope,
  950. BaseType, SizeInBits, AlignInBits, OffsetInBits, Flags, Elements.get(),
  951. RuntimeLang, VTableHolder, TemplateParams.get(),
  952. getCanonicalMDString(Context, Identifier), Discriminator, DataLocation,
  953. Associated, Allocated, Rank, Annotations.get(), Storage, ShouldCreate);
  954. }
  955. static DICompositeType *
  956. getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
  957. unsigned Line, Metadata *Scope, Metadata *BaseType,
  958. uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
  959. DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
  960. Metadata *VTableHolder, Metadata *TemplateParams,
  961. MDString *Identifier, Metadata *Discriminator, Metadata *DataLocation,
  962. Metadata *Associated, Metadata *Allocated, Metadata *Rank,
  963. Metadata *Annotations, StorageType Storage, bool ShouldCreate = true);
  964. TempDICompositeType cloneImpl() const {
  965. return getTemporary(
  966. getContext(), getTag(), getName(), getFile(), getLine(), getScope(),
  967. getBaseType(), getSizeInBits(), getAlignInBits(), getOffsetInBits(),
  968. getFlags(), getElements(), getRuntimeLang(), getVTableHolder(),
  969. getTemplateParams(), getIdentifier(), getDiscriminator(),
  970. getRawDataLocation(), getRawAssociated(), getRawAllocated(),
  971. getRawRank(), getAnnotations());
  972. }
  973. public:
  974. DEFINE_MDNODE_GET(
  975. DICompositeType,
  976. (unsigned Tag, StringRef Name, DIFile *File, unsigned Line,
  977. DIScope *Scope, DIType *BaseType, uint64_t SizeInBits,
  978. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  979. DINodeArray Elements, unsigned RuntimeLang, DIType *VTableHolder,
  980. DITemplateParameterArray TemplateParams = nullptr,
  981. StringRef Identifier = "", DIDerivedType *Discriminator = nullptr,
  982. Metadata *DataLocation = nullptr, Metadata *Associated = nullptr,
  983. Metadata *Allocated = nullptr, Metadata *Rank = nullptr,
  984. DINodeArray Annotations = nullptr),
  985. (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  986. OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
  987. Identifier, Discriminator, DataLocation, Associated, Allocated, Rank,
  988. Annotations))
  989. DEFINE_MDNODE_GET(
  990. DICompositeType,
  991. (unsigned Tag, MDString *Name, Metadata *File, unsigned Line,
  992. Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
  993. uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
  994. Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
  995. Metadata *TemplateParams = nullptr, MDString *Identifier = nullptr,
  996. Metadata *Discriminator = nullptr, Metadata *DataLocation = nullptr,
  997. Metadata *Associated = nullptr, Metadata *Allocated = nullptr,
  998. Metadata *Rank = nullptr, Metadata *Annotations = nullptr),
  999. (Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1000. OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, TemplateParams,
  1001. Identifier, Discriminator, DataLocation, Associated, Allocated, Rank,
  1002. Annotations))
  1003. TempDICompositeType clone() const { return cloneImpl(); }
  1004. /// Get a DICompositeType with the given ODR identifier.
  1005. ///
  1006. /// If \a LLVMContext::isODRUniquingDebugTypes(), gets the mapped
  1007. /// DICompositeType for the given ODR \c Identifier. If none exists, creates
  1008. /// a new node.
  1009. ///
  1010. /// Else, returns \c nullptr.
  1011. static DICompositeType *
  1012. getODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
  1013. MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
  1014. Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
  1015. uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
  1016. unsigned RuntimeLang, Metadata *VTableHolder,
  1017. Metadata *TemplateParams, Metadata *Discriminator,
  1018. Metadata *DataLocation, Metadata *Associated, Metadata *Allocated,
  1019. Metadata *Rank, Metadata *Annotations);
  1020. static DICompositeType *getODRTypeIfExists(LLVMContext &Context,
  1021. MDString &Identifier);
  1022. /// Build a DICompositeType with the given ODR identifier.
  1023. ///
  1024. /// Looks up the mapped DICompositeType for the given ODR \c Identifier. If
  1025. /// it doesn't exist, creates a new one. If it does exist and \a
  1026. /// isForwardDecl(), and the new arguments would be a definition, mutates the
  1027. /// the type in place. In either case, returns the type.
  1028. ///
  1029. /// If not \a LLVMContext::isODRUniquingDebugTypes(), this function returns
  1030. /// nullptr.
  1031. static DICompositeType *
  1032. buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag,
  1033. MDString *Name, Metadata *File, unsigned Line, Metadata *Scope,
  1034. Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits,
  1035. uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements,
  1036. unsigned RuntimeLang, Metadata *VTableHolder,
  1037. Metadata *TemplateParams, Metadata *Discriminator,
  1038. Metadata *DataLocation, Metadata *Associated,
  1039. Metadata *Allocated, Metadata *Rank, Metadata *Annotations);
  1040. DIType *getBaseType() const { return cast_or_null<DIType>(getRawBaseType()); }
  1041. DINodeArray getElements() const {
  1042. return cast_or_null<MDTuple>(getRawElements());
  1043. }
  1044. DIType *getVTableHolder() const {
  1045. return cast_or_null<DIType>(getRawVTableHolder());
  1046. }
  1047. DITemplateParameterArray getTemplateParams() const {
  1048. return cast_or_null<MDTuple>(getRawTemplateParams());
  1049. }
  1050. StringRef getIdentifier() const { return getStringOperand(7); }
  1051. unsigned getRuntimeLang() const { return RuntimeLang; }
  1052. Metadata *getRawBaseType() const { return getOperand(3); }
  1053. Metadata *getRawElements() const { return getOperand(4); }
  1054. Metadata *getRawVTableHolder() const { return getOperand(5); }
  1055. Metadata *getRawTemplateParams() const { return getOperand(6); }
  1056. MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
  1057. Metadata *getRawDiscriminator() const { return getOperand(8); }
  1058. DIDerivedType *getDiscriminator() const {
  1059. return getOperandAs<DIDerivedType>(8);
  1060. }
  1061. Metadata *getRawDataLocation() const { return getOperand(9); }
  1062. DIVariable *getDataLocation() const {
  1063. return dyn_cast_or_null<DIVariable>(getRawDataLocation());
  1064. }
  1065. DIExpression *getDataLocationExp() const {
  1066. return dyn_cast_or_null<DIExpression>(getRawDataLocation());
  1067. }
  1068. Metadata *getRawAssociated() const { return getOperand(10); }
  1069. DIVariable *getAssociated() const {
  1070. return dyn_cast_or_null<DIVariable>(getRawAssociated());
  1071. }
  1072. DIExpression *getAssociatedExp() const {
  1073. return dyn_cast_or_null<DIExpression>(getRawAssociated());
  1074. }
  1075. Metadata *getRawAllocated() const { return getOperand(11); }
  1076. DIVariable *getAllocated() const {
  1077. return dyn_cast_or_null<DIVariable>(getRawAllocated());
  1078. }
  1079. DIExpression *getAllocatedExp() const {
  1080. return dyn_cast_or_null<DIExpression>(getRawAllocated());
  1081. }
  1082. Metadata *getRawRank() const { return getOperand(12); }
  1083. ConstantInt *getRankConst() const {
  1084. if (auto *MD = dyn_cast_or_null<ConstantAsMetadata>(getRawRank()))
  1085. return dyn_cast_or_null<ConstantInt>(MD->getValue());
  1086. return nullptr;
  1087. }
  1088. DIExpression *getRankExp() const {
  1089. return dyn_cast_or_null<DIExpression>(getRawRank());
  1090. }
  1091. Metadata *getRawAnnotations() const { return getOperand(13); }
  1092. DINodeArray getAnnotations() const {
  1093. return cast_or_null<MDTuple>(getRawAnnotations());
  1094. }
  1095. /// Replace operands.
  1096. ///
  1097. /// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
  1098. /// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
  1099. /// of its movement if necessary.
  1100. /// @{
  1101. void replaceElements(DINodeArray Elements) {
  1102. #ifndef NDEBUG
  1103. for (DINode *Op : getElements())
  1104. assert(is_contained(Elements->operands(), Op) &&
  1105. "Lost a member during member list replacement");
  1106. #endif
  1107. replaceOperandWith(4, Elements.get());
  1108. }
  1109. void replaceVTableHolder(DIType *VTableHolder) {
  1110. replaceOperandWith(5, VTableHolder);
  1111. }
  1112. void replaceTemplateParams(DITemplateParameterArray TemplateParams) {
  1113. replaceOperandWith(6, TemplateParams.get());
  1114. }
  1115. /// @}
  1116. static bool classof(const Metadata *MD) {
  1117. return MD->getMetadataID() == DICompositeTypeKind;
  1118. }
  1119. };
  1120. /// Type array for a subprogram.
  1121. ///
  1122. /// TODO: Fold the array of types in directly as operands.
  1123. class DISubroutineType : public DIType {
  1124. friend class LLVMContextImpl;
  1125. friend class MDNode;
  1126. /// The calling convention used with DW_AT_calling_convention. Actually of
  1127. /// type dwarf::CallingConvention.
  1128. uint8_t CC;
  1129. DISubroutineType(LLVMContext &C, StorageType Storage, DIFlags Flags,
  1130. uint8_t CC, ArrayRef<Metadata *> Ops);
  1131. ~DISubroutineType() = default;
  1132. static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
  1133. uint8_t CC, DITypeRefArray TypeArray,
  1134. StorageType Storage,
  1135. bool ShouldCreate = true) {
  1136. return getImpl(Context, Flags, CC, TypeArray.get(), Storage, ShouldCreate);
  1137. }
  1138. static DISubroutineType *getImpl(LLVMContext &Context, DIFlags Flags,
  1139. uint8_t CC, Metadata *TypeArray,
  1140. StorageType Storage,
  1141. bool ShouldCreate = true);
  1142. TempDISubroutineType cloneImpl() const {
  1143. return getTemporary(getContext(), getFlags(), getCC(), getTypeArray());
  1144. }
  1145. public:
  1146. DEFINE_MDNODE_GET(DISubroutineType,
  1147. (DIFlags Flags, uint8_t CC, DITypeRefArray TypeArray),
  1148. (Flags, CC, TypeArray))
  1149. DEFINE_MDNODE_GET(DISubroutineType,
  1150. (DIFlags Flags, uint8_t CC, Metadata *TypeArray),
  1151. (Flags, CC, TypeArray))
  1152. TempDISubroutineType clone() const { return cloneImpl(); }
  1153. // Returns a new temporary DISubroutineType with updated CC
  1154. TempDISubroutineType cloneWithCC(uint8_t CC) const {
  1155. auto NewTy = clone();
  1156. NewTy->CC = CC;
  1157. return NewTy;
  1158. }
  1159. uint8_t getCC() const { return CC; }
  1160. DITypeRefArray getTypeArray() const {
  1161. return cast_or_null<MDTuple>(getRawTypeArray());
  1162. }
  1163. Metadata *getRawTypeArray() const { return getOperand(3); }
  1164. static bool classof(const Metadata *MD) {
  1165. return MD->getMetadataID() == DISubroutineTypeKind;
  1166. }
  1167. };
  1168. /// Compile unit.
  1169. class DICompileUnit : public DIScope {
  1170. friend class LLVMContextImpl;
  1171. friend class MDNode;
  1172. public:
  1173. enum DebugEmissionKind : unsigned {
  1174. NoDebug = 0,
  1175. FullDebug,
  1176. LineTablesOnly,
  1177. DebugDirectivesOnly,
  1178. LastEmissionKind = DebugDirectivesOnly
  1179. };
  1180. enum class DebugNameTableKind : unsigned {
  1181. Default = 0,
  1182. GNU = 1,
  1183. None = 2,
  1184. LastDebugNameTableKind = None
  1185. };
  1186. static std::optional<DebugEmissionKind> getEmissionKind(StringRef Str);
  1187. static const char *emissionKindString(DebugEmissionKind EK);
  1188. static std::optional<DebugNameTableKind> getNameTableKind(StringRef Str);
  1189. static const char *nameTableKindString(DebugNameTableKind PK);
  1190. private:
  1191. unsigned SourceLanguage;
  1192. bool IsOptimized;
  1193. unsigned RuntimeVersion;
  1194. unsigned EmissionKind;
  1195. uint64_t DWOId;
  1196. bool SplitDebugInlining;
  1197. bool DebugInfoForProfiling;
  1198. unsigned NameTableKind;
  1199. bool RangesBaseAddress;
  1200. DICompileUnit(LLVMContext &C, StorageType Storage, unsigned SourceLanguage,
  1201. bool IsOptimized, unsigned RuntimeVersion,
  1202. unsigned EmissionKind, uint64_t DWOId, bool SplitDebugInlining,
  1203. bool DebugInfoForProfiling, unsigned NameTableKind,
  1204. bool RangesBaseAddress, ArrayRef<Metadata *> Ops);
  1205. ~DICompileUnit() = default;
  1206. static DICompileUnit *
  1207. getImpl(LLVMContext &Context, unsigned SourceLanguage, DIFile *File,
  1208. StringRef Producer, bool IsOptimized, StringRef Flags,
  1209. unsigned RuntimeVersion, StringRef SplitDebugFilename,
  1210. unsigned EmissionKind, DICompositeTypeArray EnumTypes,
  1211. DIScopeArray RetainedTypes,
  1212. DIGlobalVariableExpressionArray GlobalVariables,
  1213. DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
  1214. uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
  1215. unsigned NameTableKind, bool RangesBaseAddress, StringRef SysRoot,
  1216. StringRef SDK, StorageType Storage, bool ShouldCreate = true) {
  1217. return getImpl(
  1218. Context, SourceLanguage, File, getCanonicalMDString(Context, Producer),
  1219. IsOptimized, getCanonicalMDString(Context, Flags), RuntimeVersion,
  1220. getCanonicalMDString(Context, SplitDebugFilename), EmissionKind,
  1221. EnumTypes.get(), RetainedTypes.get(), GlobalVariables.get(),
  1222. ImportedEntities.get(), Macros.get(), DWOId, SplitDebugInlining,
  1223. DebugInfoForProfiling, NameTableKind, RangesBaseAddress,
  1224. getCanonicalMDString(Context, SysRoot),
  1225. getCanonicalMDString(Context, SDK), Storage, ShouldCreate);
  1226. }
  1227. static DICompileUnit *
  1228. getImpl(LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
  1229. MDString *Producer, bool IsOptimized, MDString *Flags,
  1230. unsigned RuntimeVersion, MDString *SplitDebugFilename,
  1231. unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
  1232. Metadata *GlobalVariables, Metadata *ImportedEntities,
  1233. Metadata *Macros, uint64_t DWOId, bool SplitDebugInlining,
  1234. bool DebugInfoForProfiling, unsigned NameTableKind,
  1235. bool RangesBaseAddress, MDString *SysRoot, MDString *SDK,
  1236. StorageType Storage, bool ShouldCreate = true);
  1237. TempDICompileUnit cloneImpl() const {
  1238. return getTemporary(
  1239. getContext(), getSourceLanguage(), getFile(), getProducer(),
  1240. isOptimized(), getFlags(), getRuntimeVersion(), getSplitDebugFilename(),
  1241. getEmissionKind(), getEnumTypes(), getRetainedTypes(),
  1242. getGlobalVariables(), getImportedEntities(), getMacros(), DWOId,
  1243. getSplitDebugInlining(), getDebugInfoForProfiling(), getNameTableKind(),
  1244. getRangesBaseAddress(), getSysRoot(), getSDK());
  1245. }
  1246. public:
  1247. static void get() = delete;
  1248. static void getIfExists() = delete;
  1249. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
  1250. DICompileUnit,
  1251. (unsigned SourceLanguage, DIFile *File, StringRef Producer,
  1252. bool IsOptimized, StringRef Flags, unsigned RuntimeVersion,
  1253. StringRef SplitDebugFilename, DebugEmissionKind EmissionKind,
  1254. DICompositeTypeArray EnumTypes, DIScopeArray RetainedTypes,
  1255. DIGlobalVariableExpressionArray GlobalVariables,
  1256. DIImportedEntityArray ImportedEntities, DIMacroNodeArray Macros,
  1257. uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
  1258. DebugNameTableKind NameTableKind, bool RangesBaseAddress,
  1259. StringRef SysRoot, StringRef SDK),
  1260. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  1261. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
  1262. GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
  1263. DebugInfoForProfiling, (unsigned)NameTableKind, RangesBaseAddress,
  1264. SysRoot, SDK))
  1265. DEFINE_MDNODE_GET_DISTINCT_TEMPORARY(
  1266. DICompileUnit,
  1267. (unsigned SourceLanguage, Metadata *File, MDString *Producer,
  1268. bool IsOptimized, MDString *Flags, unsigned RuntimeVersion,
  1269. MDString *SplitDebugFilename, unsigned EmissionKind, Metadata *EnumTypes,
  1270. Metadata *RetainedTypes, Metadata *GlobalVariables,
  1271. Metadata *ImportedEntities, Metadata *Macros, uint64_t DWOId,
  1272. bool SplitDebugInlining, bool DebugInfoForProfiling,
  1273. unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot,
  1274. MDString *SDK),
  1275. (SourceLanguage, File, Producer, IsOptimized, Flags, RuntimeVersion,
  1276. SplitDebugFilename, EmissionKind, EnumTypes, RetainedTypes,
  1277. GlobalVariables, ImportedEntities, Macros, DWOId, SplitDebugInlining,
  1278. DebugInfoForProfiling, NameTableKind, RangesBaseAddress, SysRoot, SDK))
  1279. TempDICompileUnit clone() const { return cloneImpl(); }
  1280. unsigned getSourceLanguage() const { return SourceLanguage; }
  1281. bool isOptimized() const { return IsOptimized; }
  1282. unsigned getRuntimeVersion() const { return RuntimeVersion; }
  1283. DebugEmissionKind getEmissionKind() const {
  1284. return (DebugEmissionKind)EmissionKind;
  1285. }
  1286. bool isDebugDirectivesOnly() const {
  1287. return EmissionKind == DebugDirectivesOnly;
  1288. }
  1289. bool getDebugInfoForProfiling() const { return DebugInfoForProfiling; }
  1290. DebugNameTableKind getNameTableKind() const {
  1291. return (DebugNameTableKind)NameTableKind;
  1292. }
  1293. bool getRangesBaseAddress() const { return RangesBaseAddress; }
  1294. StringRef getProducer() const { return getStringOperand(1); }
  1295. StringRef getFlags() const { return getStringOperand(2); }
  1296. StringRef getSplitDebugFilename() const { return getStringOperand(3); }
  1297. DICompositeTypeArray getEnumTypes() const {
  1298. return cast_or_null<MDTuple>(getRawEnumTypes());
  1299. }
  1300. DIScopeArray getRetainedTypes() const {
  1301. return cast_or_null<MDTuple>(getRawRetainedTypes());
  1302. }
  1303. DIGlobalVariableExpressionArray getGlobalVariables() const {
  1304. return cast_or_null<MDTuple>(getRawGlobalVariables());
  1305. }
  1306. DIImportedEntityArray getImportedEntities() const {
  1307. return cast_or_null<MDTuple>(getRawImportedEntities());
  1308. }
  1309. DIMacroNodeArray getMacros() const {
  1310. return cast_or_null<MDTuple>(getRawMacros());
  1311. }
  1312. uint64_t getDWOId() const { return DWOId; }
  1313. void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
  1314. bool getSplitDebugInlining() const { return SplitDebugInlining; }
  1315. void setSplitDebugInlining(bool SplitDebugInlining) {
  1316. this->SplitDebugInlining = SplitDebugInlining;
  1317. }
  1318. StringRef getSysRoot() const { return getStringOperand(9); }
  1319. StringRef getSDK() const { return getStringOperand(10); }
  1320. MDString *getRawProducer() const { return getOperandAs<MDString>(1); }
  1321. MDString *getRawFlags() const { return getOperandAs<MDString>(2); }
  1322. MDString *getRawSplitDebugFilename() const {
  1323. return getOperandAs<MDString>(3);
  1324. }
  1325. Metadata *getRawEnumTypes() const { return getOperand(4); }
  1326. Metadata *getRawRetainedTypes() const { return getOperand(5); }
  1327. Metadata *getRawGlobalVariables() const { return getOperand(6); }
  1328. Metadata *getRawImportedEntities() const { return getOperand(7); }
  1329. Metadata *getRawMacros() const { return getOperand(8); }
  1330. MDString *getRawSysRoot() const { return getOperandAs<MDString>(9); }
  1331. MDString *getRawSDK() const { return getOperandAs<MDString>(10); }
  1332. /// Replace arrays.
  1333. ///
  1334. /// If this \a isUniqued() and not \a isResolved(), it will be RAUW'ed and
  1335. /// deleted on a uniquing collision. In practice, uniquing collisions on \a
  1336. /// DICompileUnit should be fairly rare.
  1337. /// @{
  1338. void replaceEnumTypes(DICompositeTypeArray N) {
  1339. replaceOperandWith(4, N.get());
  1340. }
  1341. void replaceRetainedTypes(DITypeArray N) { replaceOperandWith(5, N.get()); }
  1342. void replaceGlobalVariables(DIGlobalVariableExpressionArray N) {
  1343. replaceOperandWith(6, N.get());
  1344. }
  1345. void replaceImportedEntities(DIImportedEntityArray N) {
  1346. replaceOperandWith(7, N.get());
  1347. }
  1348. void replaceMacros(DIMacroNodeArray N) { replaceOperandWith(8, N.get()); }
  1349. /// @}
  1350. static bool classof(const Metadata *MD) {
  1351. return MD->getMetadataID() == DICompileUnitKind;
  1352. }
  1353. };
  1354. /// A scope for locals.
  1355. ///
  1356. /// A legal scope for lexical blocks, local variables, and debug info
  1357. /// locations. Subclasses are \a DISubprogram, \a DILexicalBlock, and \a
  1358. /// DILexicalBlockFile.
  1359. class DILocalScope : public DIScope {
  1360. protected:
  1361. DILocalScope(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
  1362. ArrayRef<Metadata *> Ops)
  1363. : DIScope(C, ID, Storage, Tag, Ops) {}
  1364. ~DILocalScope() = default;
  1365. public:
  1366. /// Get the subprogram for this scope.
  1367. ///
  1368. /// Return this if it's an \a DISubprogram; otherwise, look up the scope
  1369. /// chain.
  1370. DISubprogram *getSubprogram() const;
  1371. /// Traverses the scope chain rooted at RootScope until it hits a Subprogram,
  1372. /// recreating the chain with "NewSP" instead.
  1373. static DILocalScope *
  1374. cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP,
  1375. LLVMContext &Ctx,
  1376. DenseMap<const MDNode *, MDNode *> &Cache);
  1377. /// Get the first non DILexicalBlockFile scope of this scope.
  1378. ///
  1379. /// Return this if it's not a \a DILexicalBlockFIle; otherwise, look up the
  1380. /// scope chain.
  1381. DILocalScope *getNonLexicalBlockFileScope() const;
  1382. static bool classof(const Metadata *MD) {
  1383. return MD->getMetadataID() == DISubprogramKind ||
  1384. MD->getMetadataID() == DILexicalBlockKind ||
  1385. MD->getMetadataID() == DILexicalBlockFileKind;
  1386. }
  1387. };
  1388. /// Debug location.
  1389. ///
  1390. /// A debug location in source code, used for debug info and otherwise.
  1391. class DILocation : public MDNode {
  1392. friend class LLVMContextImpl;
  1393. friend class MDNode;
  1394. DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
  1395. unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
  1396. ~DILocation() { dropAllReferences(); }
  1397. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  1398. unsigned Column, Metadata *Scope,
  1399. Metadata *InlinedAt, bool ImplicitCode,
  1400. StorageType Storage, bool ShouldCreate = true);
  1401. static DILocation *getImpl(LLVMContext &Context, unsigned Line,
  1402. unsigned Column, DILocalScope *Scope,
  1403. DILocation *InlinedAt, bool ImplicitCode,
  1404. StorageType Storage, bool ShouldCreate = true) {
  1405. return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
  1406. static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
  1407. ShouldCreate);
  1408. }
  1409. TempDILocation cloneImpl() const {
  1410. // Get the raw scope/inlinedAt since it is possible to invoke this on
  1411. // a DILocation containing temporary metadata.
  1412. return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
  1413. getRawInlinedAt(), isImplicitCode());
  1414. }
  1415. public:
  1416. // Disallow replacing operands.
  1417. void replaceOperandWith(unsigned I, Metadata *New) = delete;
  1418. DEFINE_MDNODE_GET(DILocation,
  1419. (unsigned Line, unsigned Column, Metadata *Scope,
  1420. Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
  1421. (Line, Column, Scope, InlinedAt, ImplicitCode))
  1422. DEFINE_MDNODE_GET(DILocation,
  1423. (unsigned Line, unsigned Column, DILocalScope *Scope,
  1424. DILocation *InlinedAt = nullptr,
  1425. bool ImplicitCode = false),
  1426. (Line, Column, Scope, InlinedAt, ImplicitCode))
  1427. /// Return a (temporary) clone of this.
  1428. TempDILocation clone() const { return cloneImpl(); }
  1429. unsigned getLine() const { return SubclassData32; }
  1430. unsigned getColumn() const { return SubclassData16; }
  1431. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1432. DILocation *getInlinedAt() const {
  1433. return cast_or_null<DILocation>(getRawInlinedAt());
  1434. }
  1435. /// Check if the location corresponds to an implicit code.
  1436. /// When the ImplicitCode flag is true, it means that the Instruction
  1437. /// with this DILocation has been added by the front-end but it hasn't been
  1438. /// written explicitly by the user (e.g. cleanup stuff in C++ put on a closing
  1439. /// bracket). It's useful for code coverage to not show a counter on "empty"
  1440. /// lines.
  1441. bool isImplicitCode() const { return SubclassData1; }
  1442. void setImplicitCode(bool ImplicitCode) { SubclassData1 = ImplicitCode; }
  1443. DIFile *getFile() const { return getScope()->getFile(); }
  1444. StringRef getFilename() const { return getScope()->getFilename(); }
  1445. StringRef getDirectory() const { return getScope()->getDirectory(); }
  1446. std::optional<StringRef> getSource() const { return getScope()->getSource(); }
  1447. /// Get the scope where this is inlined.
  1448. ///
  1449. /// Walk through \a getInlinedAt() and return \a getScope() from the deepest
  1450. /// location.
  1451. DILocalScope *getInlinedAtScope() const {
  1452. if (auto *IA = getInlinedAt())
  1453. return IA->getInlinedAtScope();
  1454. return getScope();
  1455. }
  1456. /// Get the DWARF discriminator.
  1457. ///
  1458. /// DWARF discriminators distinguish identical file locations between
  1459. /// instructions that are on different basic blocks.
  1460. ///
  1461. /// There are 3 components stored in discriminator, from lower bits:
  1462. ///
  1463. /// Base discriminator: assigned by AddDiscriminators pass to identify IRs
  1464. /// that are defined by the same source line, but
  1465. /// different basic blocks.
  1466. /// Duplication factor: assigned by optimizations that will scale down
  1467. /// the execution frequency of the original IR.
  1468. /// Copy Identifier: assigned by optimizations that clones the IR.
  1469. /// Each copy of the IR will be assigned an identifier.
  1470. ///
  1471. /// Encoding:
  1472. ///
  1473. /// The above 3 components are encoded into a 32bit unsigned integer in
  1474. /// order. If the lowest bit is 1, the current component is empty, and the
  1475. /// next component will start in the next bit. Otherwise, the current
  1476. /// component is non-empty, and its content starts in the next bit. The
  1477. /// value of each components is either 5 bit or 12 bit: if the 7th bit
  1478. /// is 0, the bit 2~6 (5 bits) are used to represent the component; if the
  1479. /// 7th bit is 1, the bit 2~6 (5 bits) and 8~14 (7 bits) are combined to
  1480. /// represent the component. Thus, the number of bits used for a component
  1481. /// is either 0 (if it and all the next components are empty); 1 - if it is
  1482. /// empty; 7 - if its value is up to and including 0x1f (lsb and msb are both
  1483. /// 0); or 14, if its value is up to and including 0x1ff. Note that the last
  1484. /// component is also capped at 0x1ff, even in the case when both first
  1485. /// components are 0, and we'd technically have 29 bits available.
  1486. ///
  1487. /// For precise control over the data being encoded in the discriminator,
  1488. /// use encodeDiscriminator/decodeDiscriminator.
  1489. inline unsigned getDiscriminator() const;
  1490. // For the regular discriminator, it stands for all empty components if all
  1491. // the lowest 3 bits are non-zero and all higher 29 bits are unused(zero by
  1492. // default). Here we fully leverage the higher 29 bits for pseudo probe use.
  1493. // This is the format:
  1494. // [2:0] - 0x7
  1495. // [31:3] - pseudo probe fields guaranteed to be non-zero as a whole
  1496. // So if the lower 3 bits is non-zero and the others has at least one
  1497. // non-zero bit, it guarantees to be a pseudo probe discriminator
  1498. inline static bool isPseudoProbeDiscriminator(unsigned Discriminator) {
  1499. return ((Discriminator & 0x7) == 0x7) && (Discriminator & 0xFFFFFFF8);
  1500. }
  1501. /// Returns a new DILocation with updated \p Discriminator.
  1502. inline const DILocation *cloneWithDiscriminator(unsigned Discriminator) const;
  1503. /// Returns a new DILocation with updated base discriminator \p BD. Only the
  1504. /// base discriminator is set in the new DILocation, the other encoded values
  1505. /// are elided.
  1506. /// If the discriminator cannot be encoded, the function returns std::nullopt.
  1507. inline std::optional<const DILocation *>
  1508. cloneWithBaseDiscriminator(unsigned BD) const;
  1509. /// Returns the duplication factor stored in the discriminator, or 1 if no
  1510. /// duplication factor (or 0) is encoded.
  1511. inline unsigned getDuplicationFactor() const;
  1512. /// Returns the copy identifier stored in the discriminator.
  1513. inline unsigned getCopyIdentifier() const;
  1514. /// Returns the base discriminator stored in the discriminator.
  1515. inline unsigned getBaseDiscriminator() const;
  1516. /// Returns a new DILocation with duplication factor \p DF * current
  1517. /// duplication factor encoded in the discriminator. The current duplication
  1518. /// factor is as defined by getDuplicationFactor().
  1519. /// Returns std::nullopt if encoding failed.
  1520. inline std::optional<const DILocation *>
  1521. cloneByMultiplyingDuplicationFactor(unsigned DF) const;
  1522. /// When two instructions are combined into a single instruction we also
  1523. /// need to combine the original locations into a single location.
  1524. /// When the locations are the same we can use either location.
  1525. /// When they differ, we need a third location which is distinct from either.
  1526. /// If they share a common scope, use this scope and compare the line/column
  1527. /// pair of the locations with the common scope:
  1528. /// * if both match, keep the line and column;
  1529. /// * if only the line number matches, keep the line and set the column as 0;
  1530. /// * otherwise set line and column as 0.
  1531. /// If they do not share a common scope the location is ambiguous and can't be
  1532. /// represented in a line entry. In this case, set line and column as 0 and
  1533. /// use the scope of any location.
  1534. ///
  1535. /// \p LocA \p LocB: The locations to be merged.
  1536. static const DILocation *getMergedLocation(const DILocation *LocA,
  1537. const DILocation *LocB);
  1538. /// Try to combine the vector of locations passed as input in a single one.
  1539. /// This function applies getMergedLocation() repeatedly left-to-right.
  1540. ///
  1541. /// \p Locs: The locations to be merged.
  1542. static const DILocation *
  1543. getMergedLocations(ArrayRef<const DILocation *> Locs);
  1544. /// Return the masked discriminator value for an input discrimnator value D
  1545. /// (i.e. zero out the (B+1)-th and above bits for D (B is 0-base).
  1546. // Example: an input of (0x1FF, 7) returns 0xFF.
  1547. static unsigned getMaskedDiscriminator(unsigned D, unsigned B) {
  1548. return (D & getN1Bits(B));
  1549. }
  1550. /// Return the bits used for base discriminators.
  1551. static unsigned getBaseDiscriminatorBits() { return getBaseFSBitEnd(); }
  1552. /// Returns the base discriminator for a given encoded discriminator \p D.
  1553. static unsigned
  1554. getBaseDiscriminatorFromDiscriminator(unsigned D,
  1555. bool IsFSDiscriminator = false) {
  1556. if (IsFSDiscriminator)
  1557. return getMaskedDiscriminator(D, getBaseDiscriminatorBits());
  1558. return getUnsignedFromPrefixEncoding(D);
  1559. }
  1560. /// Raw encoding of the discriminator. APIs such as cloneWithDuplicationFactor
  1561. /// have certain special case behavior (e.g. treating empty duplication factor
  1562. /// as the value '1').
  1563. /// This API, in conjunction with cloneWithDiscriminator, may be used to
  1564. /// encode the raw values provided.
  1565. ///
  1566. /// \p BD: base discriminator
  1567. /// \p DF: duplication factor
  1568. /// \p CI: copy index
  1569. ///
  1570. /// The return is std::nullopt if the values cannot be encoded in 32 bits -
  1571. /// for example, values for BD or DF larger than 12 bits. Otherwise, the
  1572. /// return is the encoded value.
  1573. static std::optional<unsigned> encodeDiscriminator(unsigned BD, unsigned DF,
  1574. unsigned CI);
  1575. /// Raw decoder for values in an encoded discriminator D.
  1576. static void decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF,
  1577. unsigned &CI);
  1578. /// Returns the duplication factor for a given encoded discriminator \p D, or
  1579. /// 1 if no value or 0 is encoded.
  1580. static unsigned getDuplicationFactorFromDiscriminator(unsigned D) {
  1581. if (EnableFSDiscriminator)
  1582. return 1;
  1583. D = getNextComponentInDiscriminator(D);
  1584. unsigned Ret = getUnsignedFromPrefixEncoding(D);
  1585. if (Ret == 0)
  1586. return 1;
  1587. return Ret;
  1588. }
  1589. /// Returns the copy identifier for a given encoded discriminator \p D.
  1590. static unsigned getCopyIdentifierFromDiscriminator(unsigned D) {
  1591. return getUnsignedFromPrefixEncoding(
  1592. getNextComponentInDiscriminator(getNextComponentInDiscriminator(D)));
  1593. }
  1594. Metadata *getRawScope() const { return getOperand(0); }
  1595. Metadata *getRawInlinedAt() const {
  1596. if (getNumOperands() == 2)
  1597. return getOperand(1);
  1598. return nullptr;
  1599. }
  1600. static bool classof(const Metadata *MD) {
  1601. return MD->getMetadataID() == DILocationKind;
  1602. }
  1603. };
  1604. /// Subprogram description.
  1605. class DISubprogram : public DILocalScope {
  1606. friend class LLVMContextImpl;
  1607. friend class MDNode;
  1608. unsigned Line;
  1609. unsigned ScopeLine;
  1610. unsigned VirtualIndex;
  1611. /// In the MS ABI, the implicit 'this' parameter is adjusted in the prologue
  1612. /// of method overrides from secondary bases by this amount. It may be
  1613. /// negative.
  1614. int ThisAdjustment;
  1615. public:
  1616. /// Debug info subprogram flags.
  1617. enum DISPFlags : uint32_t {
  1618. #define HANDLE_DISP_FLAG(ID, NAME) SPFlag##NAME = ID,
  1619. #define DISP_FLAG_LARGEST_NEEDED
  1620. #include "llvm/IR/DebugInfoFlags.def"
  1621. SPFlagNonvirtual = SPFlagZero,
  1622. SPFlagVirtuality = SPFlagVirtual | SPFlagPureVirtual,
  1623. LLVM_MARK_AS_BITMASK_ENUM(SPFlagLargest)
  1624. };
  1625. static DISPFlags getFlag(StringRef Flag);
  1626. static StringRef getFlagString(DISPFlags Flag);
  1627. /// Split up a flags bitfield for easier printing.
  1628. ///
  1629. /// Split \c Flags into \c SplitFlags, a vector of its components. Returns
  1630. /// any remaining (unrecognized) bits.
  1631. static DISPFlags splitFlags(DISPFlags Flags,
  1632. SmallVectorImpl<DISPFlags> &SplitFlags);
  1633. // Helper for converting old bitfields to new flags word.
  1634. static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition,
  1635. bool IsOptimized,
  1636. unsigned Virtuality = SPFlagNonvirtual,
  1637. bool IsMainSubprogram = false);
  1638. private:
  1639. DIFlags Flags;
  1640. DISPFlags SPFlags;
  1641. DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line,
  1642. unsigned ScopeLine, unsigned VirtualIndex, int ThisAdjustment,
  1643. DIFlags Flags, DISPFlags SPFlags, ArrayRef<Metadata *> Ops);
  1644. ~DISubprogram() = default;
  1645. static DISubprogram *
  1646. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  1647. StringRef LinkageName, DIFile *File, unsigned Line,
  1648. DISubroutineType *Type, unsigned ScopeLine, DIType *ContainingType,
  1649. unsigned VirtualIndex, int ThisAdjustment, DIFlags Flags,
  1650. DISPFlags SPFlags, DICompileUnit *Unit,
  1651. DITemplateParameterArray TemplateParams, DISubprogram *Declaration,
  1652. DINodeArray RetainedNodes, DITypeArray ThrownTypes,
  1653. DINodeArray Annotations, StringRef TargetFuncName,
  1654. StorageType Storage, bool ShouldCreate = true) {
  1655. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  1656. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  1657. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1658. Flags, SPFlags, Unit, TemplateParams.get(), Declaration,
  1659. RetainedNodes.get(), ThrownTypes.get(), Annotations.get(),
  1660. getCanonicalMDString(Context, TargetFuncName),
  1661. Storage, ShouldCreate);
  1662. }
  1663. static DISubprogram *
  1664. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  1665. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  1666. unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex,
  1667. int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
  1668. Metadata *TemplateParams, Metadata *Declaration,
  1669. Metadata *RetainedNodes, Metadata *ThrownTypes, Metadata *Annotations,
  1670. MDString *TargetFuncName, StorageType Storage,
  1671. bool ShouldCreate = true);
  1672. TempDISubprogram cloneImpl() const {
  1673. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  1674. getFile(), getLine(), getType(), getScopeLine(),
  1675. getContainingType(), getVirtualIndex(),
  1676. getThisAdjustment(), getFlags(), getSPFlags(),
  1677. getUnit(), getTemplateParams(), getDeclaration(),
  1678. getRetainedNodes(), getThrownTypes(), getAnnotations(),
  1679. getTargetFuncName());
  1680. }
  1681. public:
  1682. DEFINE_MDNODE_GET(
  1683. DISubprogram,
  1684. (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File,
  1685. unsigned Line, DISubroutineType *Type, unsigned ScopeLine,
  1686. DIType *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
  1687. DIFlags Flags, DISPFlags SPFlags, DICompileUnit *Unit,
  1688. DITemplateParameterArray TemplateParams = nullptr,
  1689. DISubprogram *Declaration = nullptr, DINodeArray RetainedNodes = nullptr,
  1690. DITypeArray ThrownTypes = nullptr, DINodeArray Annotations = nullptr,
  1691. StringRef TargetFuncName = ""),
  1692. (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
  1693. VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
  1694. Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName))
  1695. DEFINE_MDNODE_GET(
  1696. DISubprogram,
  1697. (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
  1698. unsigned Line, Metadata *Type, unsigned ScopeLine,
  1699. Metadata *ContainingType, unsigned VirtualIndex, int ThisAdjustment,
  1700. DIFlags Flags, DISPFlags SPFlags, Metadata *Unit,
  1701. Metadata *TemplateParams = nullptr, Metadata *Declaration = nullptr,
  1702. Metadata *RetainedNodes = nullptr, Metadata *ThrownTypes = nullptr,
  1703. Metadata *Annotations = nullptr, MDString *TargetFuncName = nullptr),
  1704. (Scope, Name, LinkageName, File, Line, Type, ScopeLine, ContainingType,
  1705. VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit, TemplateParams,
  1706. Declaration, RetainedNodes, ThrownTypes, Annotations, TargetFuncName))
  1707. TempDISubprogram clone() const { return cloneImpl(); }
  1708. /// Returns a new temporary DISubprogram with updated Flags
  1709. TempDISubprogram cloneWithFlags(DIFlags NewFlags) const {
  1710. auto NewSP = clone();
  1711. NewSP->Flags = NewFlags;
  1712. return NewSP;
  1713. }
  1714. public:
  1715. unsigned getLine() const { return Line; }
  1716. unsigned getVirtuality() const { return getSPFlags() & SPFlagVirtuality; }
  1717. unsigned getVirtualIndex() const { return VirtualIndex; }
  1718. int getThisAdjustment() const { return ThisAdjustment; }
  1719. unsigned getScopeLine() const { return ScopeLine; }
  1720. void setScopeLine(unsigned L) {
  1721. assert(isDistinct());
  1722. ScopeLine = L;
  1723. }
  1724. DIFlags getFlags() const { return Flags; }
  1725. DISPFlags getSPFlags() const { return SPFlags; }
  1726. bool isLocalToUnit() const { return getSPFlags() & SPFlagLocalToUnit; }
  1727. bool isDefinition() const { return getSPFlags() & SPFlagDefinition; }
  1728. bool isOptimized() const { return getSPFlags() & SPFlagOptimized; }
  1729. bool isMainSubprogram() const { return getSPFlags() & SPFlagMainSubprogram; }
  1730. bool isArtificial() const { return getFlags() & FlagArtificial; }
  1731. bool isPrivate() const {
  1732. return (getFlags() & FlagAccessibility) == FlagPrivate;
  1733. }
  1734. bool isProtected() const {
  1735. return (getFlags() & FlagAccessibility) == FlagProtected;
  1736. }
  1737. bool isPublic() const {
  1738. return (getFlags() & FlagAccessibility) == FlagPublic;
  1739. }
  1740. bool isExplicit() const { return getFlags() & FlagExplicit; }
  1741. bool isPrototyped() const { return getFlags() & FlagPrototyped; }
  1742. bool areAllCallsDescribed() const {
  1743. return getFlags() & FlagAllCallsDescribed;
  1744. }
  1745. bool isPure() const { return getSPFlags() & SPFlagPure; }
  1746. bool isElemental() const { return getSPFlags() & SPFlagElemental; }
  1747. bool isRecursive() const { return getSPFlags() & SPFlagRecursive; }
  1748. bool isObjCDirect() const { return getSPFlags() & SPFlagObjCDirect; }
  1749. /// Check if this is deleted member function.
  1750. ///
  1751. /// Return true if this subprogram is a C++11 special
  1752. /// member function declared deleted.
  1753. bool isDeleted() const { return getSPFlags() & SPFlagDeleted; }
  1754. /// Check if this is reference-qualified.
  1755. ///
  1756. /// Return true if this subprogram is a C++11 reference-qualified non-static
  1757. /// member function (void foo() &).
  1758. bool isLValueReference() const { return getFlags() & FlagLValueReference; }
  1759. /// Check if this is rvalue-reference-qualified.
  1760. ///
  1761. /// Return true if this subprogram is a C++11 rvalue-reference-qualified
  1762. /// non-static member function (void foo() &&).
  1763. bool isRValueReference() const { return getFlags() & FlagRValueReference; }
  1764. /// Check if this is marked as noreturn.
  1765. ///
  1766. /// Return true if this subprogram is C++11 noreturn or C11 _Noreturn
  1767. bool isNoReturn() const { return getFlags() & FlagNoReturn; }
  1768. // Check if this routine is a compiler-generated thunk.
  1769. //
  1770. // Returns true if this subprogram is a thunk generated by the compiler.
  1771. bool isThunk() const { return getFlags() & FlagThunk; }
  1772. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  1773. StringRef getName() const { return getStringOperand(2); }
  1774. StringRef getLinkageName() const { return getStringOperand(3); }
  1775. /// Only used by clients of CloneFunction, and only right after the cloning.
  1776. void replaceLinkageName(MDString *LN) { replaceOperandWith(3, LN); }
  1777. DISubroutineType *getType() const {
  1778. return cast_or_null<DISubroutineType>(getRawType());
  1779. }
  1780. DIType *getContainingType() const {
  1781. return cast_or_null<DIType>(getRawContainingType());
  1782. }
  1783. void replaceType(DISubroutineType *Ty) {
  1784. assert(isDistinct() && "Only distinct nodes can mutate");
  1785. replaceOperandWith(4, Ty);
  1786. }
  1787. DICompileUnit *getUnit() const {
  1788. return cast_or_null<DICompileUnit>(getRawUnit());
  1789. }
  1790. void replaceUnit(DICompileUnit *CU) { replaceOperandWith(5, CU); }
  1791. DITemplateParameterArray getTemplateParams() const {
  1792. return cast_or_null<MDTuple>(getRawTemplateParams());
  1793. }
  1794. DISubprogram *getDeclaration() const {
  1795. return cast_or_null<DISubprogram>(getRawDeclaration());
  1796. }
  1797. DINodeArray getRetainedNodes() const {
  1798. return cast_or_null<MDTuple>(getRawRetainedNodes());
  1799. }
  1800. DITypeArray getThrownTypes() const {
  1801. return cast_or_null<MDTuple>(getRawThrownTypes());
  1802. }
  1803. DINodeArray getAnnotations() const {
  1804. return cast_or_null<MDTuple>(getRawAnnotations());
  1805. }
  1806. StringRef getTargetFuncName() const {
  1807. return (getRawTargetFuncName()) ? getStringOperand(12) : StringRef();
  1808. }
  1809. Metadata *getRawScope() const { return getOperand(1); }
  1810. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  1811. MDString *getRawLinkageName() const { return getOperandAs<MDString>(3); }
  1812. Metadata *getRawType() const { return getOperand(4); }
  1813. Metadata *getRawUnit() const { return getOperand(5); }
  1814. Metadata *getRawDeclaration() const { return getOperand(6); }
  1815. Metadata *getRawRetainedNodes() const { return getOperand(7); }
  1816. Metadata *getRawContainingType() const {
  1817. return getNumOperands() > 8 ? getOperandAs<Metadata>(8) : nullptr;
  1818. }
  1819. Metadata *getRawTemplateParams() const {
  1820. return getNumOperands() > 9 ? getOperandAs<Metadata>(9) : nullptr;
  1821. }
  1822. Metadata *getRawThrownTypes() const {
  1823. return getNumOperands() > 10 ? getOperandAs<Metadata>(10) : nullptr;
  1824. }
  1825. Metadata *getRawAnnotations() const {
  1826. return getNumOperands() > 11 ? getOperandAs<Metadata>(11) : nullptr;
  1827. }
  1828. MDString *getRawTargetFuncName() const {
  1829. return getNumOperands() > 12 ? getOperandAs<MDString>(12) : nullptr;
  1830. }
  1831. void replaceRawLinkageName(MDString *LinkageName) {
  1832. replaceOperandWith(3, LinkageName);
  1833. }
  1834. /// Check if this subprogram describes the given function.
  1835. ///
  1836. /// FIXME: Should this be looking through bitcasts?
  1837. bool describes(const Function *F) const;
  1838. static bool classof(const Metadata *MD) {
  1839. return MD->getMetadataID() == DISubprogramKind;
  1840. }
  1841. };
  1842. class DILexicalBlockBase : public DILocalScope {
  1843. protected:
  1844. DILexicalBlockBase(LLVMContext &C, unsigned ID, StorageType Storage,
  1845. ArrayRef<Metadata *> Ops);
  1846. ~DILexicalBlockBase() = default;
  1847. public:
  1848. DILocalScope *getScope() const { return cast<DILocalScope>(getRawScope()); }
  1849. Metadata *getRawScope() const { return getOperand(1); }
  1850. void replaceScope(DIScope *Scope) {
  1851. assert(!isUniqued());
  1852. setOperand(1, Scope);
  1853. }
  1854. static bool classof(const Metadata *MD) {
  1855. return MD->getMetadataID() == DILexicalBlockKind ||
  1856. MD->getMetadataID() == DILexicalBlockFileKind;
  1857. }
  1858. };
  1859. class DILexicalBlock : public DILexicalBlockBase {
  1860. friend class LLVMContextImpl;
  1861. friend class MDNode;
  1862. unsigned Line;
  1863. uint16_t Column;
  1864. DILexicalBlock(LLVMContext &C, StorageType Storage, unsigned Line,
  1865. unsigned Column, ArrayRef<Metadata *> Ops)
  1866. : DILexicalBlockBase(C, DILexicalBlockKind, Storage, Ops), Line(Line),
  1867. Column(Column) {
  1868. assert(Column < (1u << 16) && "Expected 16-bit column");
  1869. }
  1870. ~DILexicalBlock() = default;
  1871. static DILexicalBlock *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1872. DIFile *File, unsigned Line, unsigned Column,
  1873. StorageType Storage,
  1874. bool ShouldCreate = true) {
  1875. return getImpl(Context, static_cast<Metadata *>(Scope),
  1876. static_cast<Metadata *>(File), Line, Column, Storage,
  1877. ShouldCreate);
  1878. }
  1879. static DILexicalBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  1880. Metadata *File, unsigned Line, unsigned Column,
  1881. StorageType Storage, bool ShouldCreate = true);
  1882. TempDILexicalBlock cloneImpl() const {
  1883. return getTemporary(getContext(), getScope(), getFile(), getLine(),
  1884. getColumn());
  1885. }
  1886. public:
  1887. DEFINE_MDNODE_GET(DILexicalBlock,
  1888. (DILocalScope * Scope, DIFile *File, unsigned Line,
  1889. unsigned Column),
  1890. (Scope, File, Line, Column))
  1891. DEFINE_MDNODE_GET(DILexicalBlock,
  1892. (Metadata * Scope, Metadata *File, unsigned Line,
  1893. unsigned Column),
  1894. (Scope, File, Line, Column))
  1895. TempDILexicalBlock clone() const { return cloneImpl(); }
  1896. unsigned getLine() const { return Line; }
  1897. unsigned getColumn() const { return Column; }
  1898. static bool classof(const Metadata *MD) {
  1899. return MD->getMetadataID() == DILexicalBlockKind;
  1900. }
  1901. };
  1902. class DILexicalBlockFile : public DILexicalBlockBase {
  1903. friend class LLVMContextImpl;
  1904. friend class MDNode;
  1905. unsigned Discriminator;
  1906. DILexicalBlockFile(LLVMContext &C, StorageType Storage,
  1907. unsigned Discriminator, ArrayRef<Metadata *> Ops)
  1908. : DILexicalBlockBase(C, DILexicalBlockFileKind, Storage, Ops),
  1909. Discriminator(Discriminator) {}
  1910. ~DILexicalBlockFile() = default;
  1911. static DILexicalBlockFile *getImpl(LLVMContext &Context, DILocalScope *Scope,
  1912. DIFile *File, unsigned Discriminator,
  1913. StorageType Storage,
  1914. bool ShouldCreate = true) {
  1915. return getImpl(Context, static_cast<Metadata *>(Scope),
  1916. static_cast<Metadata *>(File), Discriminator, Storage,
  1917. ShouldCreate);
  1918. }
  1919. static DILexicalBlockFile *getImpl(LLVMContext &Context, Metadata *Scope,
  1920. Metadata *File, unsigned Discriminator,
  1921. StorageType Storage,
  1922. bool ShouldCreate = true);
  1923. TempDILexicalBlockFile cloneImpl() const {
  1924. return getTemporary(getContext(), getScope(), getFile(),
  1925. getDiscriminator());
  1926. }
  1927. public:
  1928. DEFINE_MDNODE_GET(DILexicalBlockFile,
  1929. (DILocalScope * Scope, DIFile *File,
  1930. unsigned Discriminator),
  1931. (Scope, File, Discriminator))
  1932. DEFINE_MDNODE_GET(DILexicalBlockFile,
  1933. (Metadata * Scope, Metadata *File, unsigned Discriminator),
  1934. (Scope, File, Discriminator))
  1935. TempDILexicalBlockFile clone() const { return cloneImpl(); }
  1936. unsigned getDiscriminator() const { return Discriminator; }
  1937. static bool classof(const Metadata *MD) {
  1938. return MD->getMetadataID() == DILexicalBlockFileKind;
  1939. }
  1940. };
  1941. unsigned DILocation::getDiscriminator() const {
  1942. if (auto *F = dyn_cast<DILexicalBlockFile>(getScope()))
  1943. return F->getDiscriminator();
  1944. return 0;
  1945. }
  1946. const DILocation *
  1947. DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
  1948. DIScope *Scope = getScope();
  1949. // Skip all parent DILexicalBlockFile that already have a discriminator
  1950. // assigned. We do not want to have nested DILexicalBlockFiles that have
  1951. // mutliple discriminators because only the leaf DILexicalBlockFile's
  1952. // dominator will be used.
  1953. for (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope);
  1954. LBF && LBF->getDiscriminator() != 0;
  1955. LBF = dyn_cast<DILexicalBlockFile>(Scope))
  1956. Scope = LBF->getScope();
  1957. DILexicalBlockFile *NewScope =
  1958. DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
  1959. return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
  1960. getInlinedAt());
  1961. }
  1962. unsigned DILocation::getBaseDiscriminator() const {
  1963. return getBaseDiscriminatorFromDiscriminator(getDiscriminator(),
  1964. EnableFSDiscriminator);
  1965. }
  1966. unsigned DILocation::getDuplicationFactor() const {
  1967. return getDuplicationFactorFromDiscriminator(getDiscriminator());
  1968. }
  1969. unsigned DILocation::getCopyIdentifier() const {
  1970. return getCopyIdentifierFromDiscriminator(getDiscriminator());
  1971. }
  1972. std::optional<const DILocation *>
  1973. DILocation::cloneWithBaseDiscriminator(unsigned D) const {
  1974. unsigned BD, DF, CI;
  1975. if (EnableFSDiscriminator) {
  1976. BD = getBaseDiscriminator();
  1977. if (D == BD)
  1978. return this;
  1979. return cloneWithDiscriminator(D);
  1980. }
  1981. decodeDiscriminator(getDiscriminator(), BD, DF, CI);
  1982. if (D == BD)
  1983. return this;
  1984. if (std::optional<unsigned> Encoded = encodeDiscriminator(D, DF, CI))
  1985. return cloneWithDiscriminator(*Encoded);
  1986. return std::nullopt;
  1987. }
  1988. std::optional<const DILocation *>
  1989. DILocation::cloneByMultiplyingDuplicationFactor(unsigned DF) const {
  1990. assert(!EnableFSDiscriminator && "FSDiscriminator should not call this.");
  1991. DF *= getDuplicationFactor();
  1992. if (DF <= 1)
  1993. return this;
  1994. unsigned BD = getBaseDiscriminator();
  1995. unsigned CI = getCopyIdentifier();
  1996. if (std::optional<unsigned> D = encodeDiscriminator(BD, DF, CI))
  1997. return cloneWithDiscriminator(*D);
  1998. return std::nullopt;
  1999. }
  2000. class DINamespace : public DIScope {
  2001. friend class LLVMContextImpl;
  2002. friend class MDNode;
  2003. unsigned ExportSymbols : 1;
  2004. DINamespace(LLVMContext &Context, StorageType Storage, bool ExportSymbols,
  2005. ArrayRef<Metadata *> Ops);
  2006. ~DINamespace() = default;
  2007. static DINamespace *getImpl(LLVMContext &Context, DIScope *Scope,
  2008. StringRef Name, bool ExportSymbols,
  2009. StorageType Storage, bool ShouldCreate = true) {
  2010. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  2011. ExportSymbols, Storage, ShouldCreate);
  2012. }
  2013. static DINamespace *getImpl(LLVMContext &Context, Metadata *Scope,
  2014. MDString *Name, bool ExportSymbols,
  2015. StorageType Storage, bool ShouldCreate = true);
  2016. TempDINamespace cloneImpl() const {
  2017. return getTemporary(getContext(), getScope(), getName(),
  2018. getExportSymbols());
  2019. }
  2020. public:
  2021. DEFINE_MDNODE_GET(DINamespace,
  2022. (DIScope * Scope, StringRef Name, bool ExportSymbols),
  2023. (Scope, Name, ExportSymbols))
  2024. DEFINE_MDNODE_GET(DINamespace,
  2025. (Metadata * Scope, MDString *Name, bool ExportSymbols),
  2026. (Scope, Name, ExportSymbols))
  2027. TempDINamespace clone() const { return cloneImpl(); }
  2028. bool getExportSymbols() const { return ExportSymbols; }
  2029. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2030. StringRef getName() const { return getStringOperand(2); }
  2031. Metadata *getRawScope() const { return getOperand(1); }
  2032. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2033. static bool classof(const Metadata *MD) {
  2034. return MD->getMetadataID() == DINamespaceKind;
  2035. }
  2036. };
  2037. /// Represents a module in the programming language, for example, a Clang
  2038. /// module, or a Fortran module.
  2039. class DIModule : public DIScope {
  2040. friend class LLVMContextImpl;
  2041. friend class MDNode;
  2042. unsigned LineNo;
  2043. bool IsDecl;
  2044. DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo,
  2045. bool IsDecl, ArrayRef<Metadata *> Ops);
  2046. ~DIModule() = default;
  2047. static DIModule *getImpl(LLVMContext &Context, DIFile *File, DIScope *Scope,
  2048. StringRef Name, StringRef ConfigurationMacros,
  2049. StringRef IncludePath, StringRef APINotesFile,
  2050. unsigned LineNo, bool IsDecl, StorageType Storage,
  2051. bool ShouldCreate = true) {
  2052. return getImpl(Context, File, Scope, getCanonicalMDString(Context, Name),
  2053. getCanonicalMDString(Context, ConfigurationMacros),
  2054. getCanonicalMDString(Context, IncludePath),
  2055. getCanonicalMDString(Context, APINotesFile), LineNo, IsDecl,
  2056. Storage, ShouldCreate);
  2057. }
  2058. static DIModule *getImpl(LLVMContext &Context, Metadata *File,
  2059. Metadata *Scope, MDString *Name,
  2060. MDString *ConfigurationMacros, MDString *IncludePath,
  2061. MDString *APINotesFile, unsigned LineNo, bool IsDecl,
  2062. StorageType Storage, bool ShouldCreate = true);
  2063. TempDIModule cloneImpl() const {
  2064. return getTemporary(getContext(), getFile(), getScope(), getName(),
  2065. getConfigurationMacros(), getIncludePath(),
  2066. getAPINotesFile(), getLineNo(), getIsDecl());
  2067. }
  2068. public:
  2069. DEFINE_MDNODE_GET(DIModule,
  2070. (DIFile * File, DIScope *Scope, StringRef Name,
  2071. StringRef ConfigurationMacros, StringRef IncludePath,
  2072. StringRef APINotesFile, unsigned LineNo,
  2073. bool IsDecl = false),
  2074. (File, Scope, Name, ConfigurationMacros, IncludePath,
  2075. APINotesFile, LineNo, IsDecl))
  2076. DEFINE_MDNODE_GET(DIModule,
  2077. (Metadata * File, Metadata *Scope, MDString *Name,
  2078. MDString *ConfigurationMacros, MDString *IncludePath,
  2079. MDString *APINotesFile, unsigned LineNo,
  2080. bool IsDecl = false),
  2081. (File, Scope, Name, ConfigurationMacros, IncludePath,
  2082. APINotesFile, LineNo, IsDecl))
  2083. TempDIModule clone() const { return cloneImpl(); }
  2084. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2085. StringRef getName() const { return getStringOperand(2); }
  2086. StringRef getConfigurationMacros() const { return getStringOperand(3); }
  2087. StringRef getIncludePath() const { return getStringOperand(4); }
  2088. StringRef getAPINotesFile() const { return getStringOperand(5); }
  2089. unsigned getLineNo() const { return LineNo; }
  2090. bool getIsDecl() const { return IsDecl; }
  2091. Metadata *getRawScope() const { return getOperand(1); }
  2092. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2093. MDString *getRawConfigurationMacros() const {
  2094. return getOperandAs<MDString>(3);
  2095. }
  2096. MDString *getRawIncludePath() const { return getOperandAs<MDString>(4); }
  2097. MDString *getRawAPINotesFile() const { return getOperandAs<MDString>(5); }
  2098. static bool classof(const Metadata *MD) {
  2099. return MD->getMetadataID() == DIModuleKind;
  2100. }
  2101. };
  2102. /// Base class for template parameters.
  2103. class DITemplateParameter : public DINode {
  2104. protected:
  2105. bool IsDefault;
  2106. DITemplateParameter(LLVMContext &Context, unsigned ID, StorageType Storage,
  2107. unsigned Tag, bool IsDefault, ArrayRef<Metadata *> Ops)
  2108. : DINode(Context, ID, Storage, Tag, Ops), IsDefault(IsDefault) {}
  2109. ~DITemplateParameter() = default;
  2110. public:
  2111. StringRef getName() const { return getStringOperand(0); }
  2112. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2113. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  2114. Metadata *getRawType() const { return getOperand(1); }
  2115. bool isDefault() const { return IsDefault; }
  2116. static bool classof(const Metadata *MD) {
  2117. return MD->getMetadataID() == DITemplateTypeParameterKind ||
  2118. MD->getMetadataID() == DITemplateValueParameterKind;
  2119. }
  2120. };
  2121. class DITemplateTypeParameter : public DITemplateParameter {
  2122. friend class LLVMContextImpl;
  2123. friend class MDNode;
  2124. DITemplateTypeParameter(LLVMContext &Context, StorageType Storage,
  2125. bool IsDefault, ArrayRef<Metadata *> Ops);
  2126. ~DITemplateTypeParameter() = default;
  2127. static DITemplateTypeParameter *getImpl(LLVMContext &Context, StringRef Name,
  2128. DIType *Type, bool IsDefault,
  2129. StorageType Storage,
  2130. bool ShouldCreate = true) {
  2131. return getImpl(Context, getCanonicalMDString(Context, Name), Type,
  2132. IsDefault, Storage, ShouldCreate);
  2133. }
  2134. static DITemplateTypeParameter *getImpl(LLVMContext &Context, MDString *Name,
  2135. Metadata *Type, bool IsDefault,
  2136. StorageType Storage,
  2137. bool ShouldCreate = true);
  2138. TempDITemplateTypeParameter cloneImpl() const {
  2139. return getTemporary(getContext(), getName(), getType(), isDefault());
  2140. }
  2141. public:
  2142. DEFINE_MDNODE_GET(DITemplateTypeParameter,
  2143. (StringRef Name, DIType *Type, bool IsDefault),
  2144. (Name, Type, IsDefault))
  2145. DEFINE_MDNODE_GET(DITemplateTypeParameter,
  2146. (MDString * Name, Metadata *Type, bool IsDefault),
  2147. (Name, Type, IsDefault))
  2148. TempDITemplateTypeParameter clone() const { return cloneImpl(); }
  2149. static bool classof(const Metadata *MD) {
  2150. return MD->getMetadataID() == DITemplateTypeParameterKind;
  2151. }
  2152. };
  2153. class DITemplateValueParameter : public DITemplateParameter {
  2154. friend class LLVMContextImpl;
  2155. friend class MDNode;
  2156. DITemplateValueParameter(LLVMContext &Context, StorageType Storage,
  2157. unsigned Tag, bool IsDefault,
  2158. ArrayRef<Metadata *> Ops)
  2159. : DITemplateParameter(Context, DITemplateValueParameterKind, Storage, Tag,
  2160. IsDefault, Ops) {}
  2161. ~DITemplateValueParameter() = default;
  2162. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  2163. StringRef Name, DIType *Type,
  2164. bool IsDefault, Metadata *Value,
  2165. StorageType Storage,
  2166. bool ShouldCreate = true) {
  2167. return getImpl(Context, Tag, getCanonicalMDString(Context, Name), Type,
  2168. IsDefault, Value, Storage, ShouldCreate);
  2169. }
  2170. static DITemplateValueParameter *getImpl(LLVMContext &Context, unsigned Tag,
  2171. MDString *Name, Metadata *Type,
  2172. bool IsDefault, Metadata *Value,
  2173. StorageType Storage,
  2174. bool ShouldCreate = true);
  2175. TempDITemplateValueParameter cloneImpl() const {
  2176. return getTemporary(getContext(), getTag(), getName(), getType(),
  2177. isDefault(), getValue());
  2178. }
  2179. public:
  2180. DEFINE_MDNODE_GET(DITemplateValueParameter,
  2181. (unsigned Tag, StringRef Name, DIType *Type, bool IsDefault,
  2182. Metadata *Value),
  2183. (Tag, Name, Type, IsDefault, Value))
  2184. DEFINE_MDNODE_GET(DITemplateValueParameter,
  2185. (unsigned Tag, MDString *Name, Metadata *Type,
  2186. bool IsDefault, Metadata *Value),
  2187. (Tag, Name, Type, IsDefault, Value))
  2188. TempDITemplateValueParameter clone() const { return cloneImpl(); }
  2189. Metadata *getValue() const { return getOperand(2); }
  2190. static bool classof(const Metadata *MD) {
  2191. return MD->getMetadataID() == DITemplateValueParameterKind;
  2192. }
  2193. };
  2194. /// Base class for variables.
  2195. class DIVariable : public DINode {
  2196. unsigned Line;
  2197. uint32_t AlignInBits;
  2198. protected:
  2199. DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, signed Line,
  2200. ArrayRef<Metadata *> Ops, uint32_t AlignInBits = 0);
  2201. ~DIVariable() = default;
  2202. public:
  2203. unsigned getLine() const { return Line; }
  2204. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2205. StringRef getName() const { return getStringOperand(1); }
  2206. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2207. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2208. uint32_t getAlignInBits() const { return AlignInBits; }
  2209. uint32_t getAlignInBytes() const { return getAlignInBits() / CHAR_BIT; }
  2210. /// Determines the size of the variable's type.
  2211. std::optional<uint64_t> getSizeInBits() const;
  2212. /// Return the signedness of this variable's type, or std::nullopt if this
  2213. /// type is neither signed nor unsigned.
  2214. std::optional<DIBasicType::Signedness> getSignedness() const {
  2215. if (auto *BT = dyn_cast<DIBasicType>(getType()))
  2216. return BT->getSignedness();
  2217. return std::nullopt;
  2218. }
  2219. StringRef getFilename() const {
  2220. if (auto *F = getFile())
  2221. return F->getFilename();
  2222. return "";
  2223. }
  2224. StringRef getDirectory() const {
  2225. if (auto *F = getFile())
  2226. return F->getDirectory();
  2227. return "";
  2228. }
  2229. std::optional<StringRef> getSource() const {
  2230. if (auto *F = getFile())
  2231. return F->getSource();
  2232. return std::nullopt;
  2233. }
  2234. Metadata *getRawScope() const { return getOperand(0); }
  2235. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  2236. Metadata *getRawFile() const { return getOperand(2); }
  2237. Metadata *getRawType() const { return getOperand(3); }
  2238. static bool classof(const Metadata *MD) {
  2239. return MD->getMetadataID() == DILocalVariableKind ||
  2240. MD->getMetadataID() == DIGlobalVariableKind;
  2241. }
  2242. };
  2243. /// DWARF expression.
  2244. ///
  2245. /// This is (almost) a DWARF expression that modifies the location of a
  2246. /// variable, or the location of a single piece of a variable, or (when using
  2247. /// DW_OP_stack_value) is the constant variable value.
  2248. ///
  2249. /// TODO: Co-allocate the expression elements.
  2250. /// TODO: Separate from MDNode, or otherwise drop Distinct and Temporary
  2251. /// storage types.
  2252. class DIExpression : public MDNode {
  2253. friend class LLVMContextImpl;
  2254. friend class MDNode;
  2255. std::vector<uint64_t> Elements;
  2256. DIExpression(LLVMContext &C, StorageType Storage, ArrayRef<uint64_t> Elements)
  2257. : MDNode(C, DIExpressionKind, Storage, std::nullopt),
  2258. Elements(Elements.begin(), Elements.end()) {}
  2259. ~DIExpression() = default;
  2260. static DIExpression *getImpl(LLVMContext &Context,
  2261. ArrayRef<uint64_t> Elements, StorageType Storage,
  2262. bool ShouldCreate = true);
  2263. TempDIExpression cloneImpl() const {
  2264. return getTemporary(getContext(), getElements());
  2265. }
  2266. public:
  2267. DEFINE_MDNODE_GET(DIExpression, (ArrayRef<uint64_t> Elements), (Elements))
  2268. TempDIExpression clone() const { return cloneImpl(); }
  2269. ArrayRef<uint64_t> getElements() const { return Elements; }
  2270. unsigned getNumElements() const { return Elements.size(); }
  2271. uint64_t getElement(unsigned I) const {
  2272. assert(I < Elements.size() && "Index out of range");
  2273. return Elements[I];
  2274. }
  2275. enum SignedOrUnsignedConstant { SignedConstant, UnsignedConstant };
  2276. /// Determine whether this represents a constant value, if so
  2277. // return it's sign information.
  2278. std::optional<SignedOrUnsignedConstant> isConstant() const;
  2279. /// Return the number of unique location operands referred to (via
  2280. /// DW_OP_LLVM_arg) in this expression; this is not necessarily the number of
  2281. /// instances of DW_OP_LLVM_arg within the expression.
  2282. /// For example, for the expression:
  2283. /// (DW_OP_LLVM_arg 0, DW_OP_LLVM_arg 1, DW_OP_plus,
  2284. /// DW_OP_LLVM_arg 0, DW_OP_mul)
  2285. /// This function would return 2, as there are two unique location operands
  2286. /// (0 and 1).
  2287. uint64_t getNumLocationOperands() const;
  2288. using element_iterator = ArrayRef<uint64_t>::iterator;
  2289. element_iterator elements_begin() const { return getElements().begin(); }
  2290. element_iterator elements_end() const { return getElements().end(); }
  2291. /// A lightweight wrapper around an expression operand.
  2292. ///
  2293. /// TODO: Store arguments directly and change \a DIExpression to store a
  2294. /// range of these.
  2295. class ExprOperand {
  2296. const uint64_t *Op = nullptr;
  2297. public:
  2298. ExprOperand() = default;
  2299. explicit ExprOperand(const uint64_t *Op) : Op(Op) {}
  2300. const uint64_t *get() const { return Op; }
  2301. /// Get the operand code.
  2302. uint64_t getOp() const { return *Op; }
  2303. /// Get an argument to the operand.
  2304. ///
  2305. /// Never returns the operand itself.
  2306. uint64_t getArg(unsigned I) const { return Op[I + 1]; }
  2307. unsigned getNumArgs() const { return getSize() - 1; }
  2308. /// Return the size of the operand.
  2309. ///
  2310. /// Return the number of elements in the operand (1 + args).
  2311. unsigned getSize() const;
  2312. /// Append the elements of this operand to \p V.
  2313. void appendToVector(SmallVectorImpl<uint64_t> &V) const {
  2314. V.append(get(), get() + getSize());
  2315. }
  2316. };
  2317. /// An iterator for expression operands.
  2318. class expr_op_iterator {
  2319. ExprOperand Op;
  2320. public:
  2321. using iterator_category = std::input_iterator_tag;
  2322. using value_type = ExprOperand;
  2323. using difference_type = std::ptrdiff_t;
  2324. using pointer = value_type *;
  2325. using reference = value_type &;
  2326. expr_op_iterator() = default;
  2327. explicit expr_op_iterator(element_iterator I) : Op(I) {}
  2328. element_iterator getBase() const { return Op.get(); }
  2329. const ExprOperand &operator*() const { return Op; }
  2330. const ExprOperand *operator->() const { return &Op; }
  2331. expr_op_iterator &operator++() {
  2332. increment();
  2333. return *this;
  2334. }
  2335. expr_op_iterator operator++(int) {
  2336. expr_op_iterator T(*this);
  2337. increment();
  2338. return T;
  2339. }
  2340. /// Get the next iterator.
  2341. ///
  2342. /// \a std::next() doesn't work because this is technically an
  2343. /// input_iterator, but it's a perfectly valid operation. This is an
  2344. /// accessor to provide the same functionality.
  2345. expr_op_iterator getNext() const { return ++expr_op_iterator(*this); }
  2346. bool operator==(const expr_op_iterator &X) const {
  2347. return getBase() == X.getBase();
  2348. }
  2349. bool operator!=(const expr_op_iterator &X) const {
  2350. return getBase() != X.getBase();
  2351. }
  2352. private:
  2353. void increment() { Op = ExprOperand(getBase() + Op.getSize()); }
  2354. };
  2355. /// Visit the elements via ExprOperand wrappers.
  2356. ///
  2357. /// These range iterators visit elements through \a ExprOperand wrappers.
  2358. /// This is not guaranteed to be a valid range unless \a isValid() gives \c
  2359. /// true.
  2360. ///
  2361. /// \pre \a isValid() gives \c true.
  2362. /// @{
  2363. expr_op_iterator expr_op_begin() const {
  2364. return expr_op_iterator(elements_begin());
  2365. }
  2366. expr_op_iterator expr_op_end() const {
  2367. return expr_op_iterator(elements_end());
  2368. }
  2369. iterator_range<expr_op_iterator> expr_ops() const {
  2370. return {expr_op_begin(), expr_op_end()};
  2371. }
  2372. /// @}
  2373. bool isValid() const;
  2374. static bool classof(const Metadata *MD) {
  2375. return MD->getMetadataID() == DIExpressionKind;
  2376. }
  2377. /// Return whether the first element a DW_OP_deref.
  2378. bool startsWithDeref() const;
  2379. /// Holds the characteristics of one fragment of a larger variable.
  2380. struct FragmentInfo {
  2381. uint64_t SizeInBits;
  2382. uint64_t OffsetInBits;
  2383. };
  2384. /// Retrieve the details of this fragment expression.
  2385. static std::optional<FragmentInfo> getFragmentInfo(expr_op_iterator Start,
  2386. expr_op_iterator End);
  2387. /// Retrieve the details of this fragment expression.
  2388. std::optional<FragmentInfo> getFragmentInfo() const {
  2389. return getFragmentInfo(expr_op_begin(), expr_op_end());
  2390. }
  2391. /// Return whether this is a piece of an aggregate variable.
  2392. bool isFragment() const { return getFragmentInfo().has_value(); }
  2393. /// Return whether this is an implicit location description.
  2394. bool isImplicit() const;
  2395. /// Return whether the location is computed on the expression stack, meaning
  2396. /// it cannot be a simple register location.
  2397. bool isComplex() const;
  2398. /// Return whether the evaluated expression makes use of a single location at
  2399. /// the start of the expression, i.e. if it contains only a single
  2400. /// DW_OP_LLVM_arg op as its first operand, or if it contains none.
  2401. bool isSingleLocationExpression() const;
  2402. /// Removes all elements from \p Expr that do not apply to an undef debug
  2403. /// value, which includes every operator that computes the value/location on
  2404. /// the DWARF stack, including any DW_OP_LLVM_arg elements (making the result
  2405. /// of this function always a single-location expression) while leaving
  2406. /// everything that defines what the computed value applies to, i.e. the
  2407. /// fragment information.
  2408. static const DIExpression *convertToUndefExpression(const DIExpression *Expr);
  2409. /// If \p Expr is a non-variadic expression (i.e. one that does not contain
  2410. /// DW_OP_LLVM_arg), returns \p Expr converted to variadic form by adding a
  2411. /// leading [DW_OP_LLVM_arg, 0] to the expression; otherwise returns \p Expr.
  2412. static const DIExpression *
  2413. convertToVariadicExpression(const DIExpression *Expr);
  2414. /// If \p Expr is a valid single-location expression, i.e. it refers to only a
  2415. /// single debug operand at the start of the expression, then return that
  2416. /// expression in a non-variadic form by removing DW_OP_LLVM_arg from the
  2417. /// expression if it is present; otherwise returns std::nullopt.
  2418. static std::optional<const DIExpression *>
  2419. convertToNonVariadicExpression(const DIExpression *Expr);
  2420. /// Inserts the elements of \p Expr into \p Ops modified to a canonical form,
  2421. /// which uses DW_OP_LLVM_arg (i.e. is a variadic expression) and folds the
  2422. /// implied derefence from the \p IsIndirect flag into the expression. This
  2423. /// allows us to check equivalence between expressions with differing
  2424. /// directness or variadicness.
  2425. static void canonicalizeExpressionOps(SmallVectorImpl<uint64_t> &Ops,
  2426. const DIExpression *Expr,
  2427. bool IsIndirect);
  2428. /// Determines whether two debug values should produce equivalent DWARF
  2429. /// expressions, using their DIExpressions and directness, ignoring the
  2430. /// differences between otherwise identical expressions in variadic and
  2431. /// non-variadic form and not considering the debug operands.
  2432. /// \p FirstExpr is the DIExpression for the first debug value.
  2433. /// \p FirstIndirect should be true if the first debug value is indirect; in
  2434. /// IR this should be true for dbg.declare and dbg.addr intrinsics and false
  2435. /// for dbg.values, and in MIR this should be true only for DBG_VALUE
  2436. /// instructions whose second operand is an immediate value.
  2437. /// \p SecondExpr and \p SecondIndirect have the same meaning as the prior
  2438. /// arguments, but apply to the second debug value.
  2439. static bool isEqualExpression(const DIExpression *FirstExpr,
  2440. bool FirstIndirect,
  2441. const DIExpression *SecondExpr,
  2442. bool SecondIndirect);
  2443. /// Append \p Ops with operations to apply the \p Offset.
  2444. static void appendOffset(SmallVectorImpl<uint64_t> &Ops, int64_t Offset);
  2445. /// If this is a constant offset, extract it. If there is no expression,
  2446. /// return true with an offset of zero.
  2447. bool extractIfOffset(int64_t &Offset) const;
  2448. /// Returns true iff this DIExpression contains at least one instance of
  2449. /// `DW_OP_LLVM_arg, n` for all n in [0, N).
  2450. bool hasAllLocationOps(unsigned N) const;
  2451. /// Checks if the last 4 elements of the expression are DW_OP_constu <DWARF
  2452. /// Address Space> DW_OP_swap DW_OP_xderef and extracts the <DWARF Address
  2453. /// Space>.
  2454. static const DIExpression *extractAddressClass(const DIExpression *Expr,
  2455. unsigned &AddrClass);
  2456. /// Used for DIExpression::prepend.
  2457. enum PrependOps : uint8_t {
  2458. ApplyOffset = 0,
  2459. DerefBefore = 1 << 0,
  2460. DerefAfter = 1 << 1,
  2461. StackValue = 1 << 2,
  2462. EntryValue = 1 << 3
  2463. };
  2464. /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
  2465. /// into a stack value or/and an entry value.
  2466. static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
  2467. int64_t Offset = 0);
  2468. /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
  2469. /// stack value.
  2470. static DIExpression *prependOpcodes(const DIExpression *Expr,
  2471. SmallVectorImpl<uint64_t> &Ops,
  2472. bool StackValue = false,
  2473. bool EntryValue = false);
  2474. /// Append the opcodes \p Ops to \p DIExpr. Unlike \ref appendToStack, the
  2475. /// returned expression is a stack value only if \p DIExpr is a stack value.
  2476. /// If \p DIExpr describes a fragment, the returned expression will describe
  2477. /// the same fragment.
  2478. static DIExpression *append(const DIExpression *Expr, ArrayRef<uint64_t> Ops);
  2479. /// Convert \p DIExpr into a stack value if it isn't one already by appending
  2480. /// DW_OP_deref if needed, and appending \p Ops to the resulting expression.
  2481. /// If \p DIExpr describes a fragment, the returned expression will describe
  2482. /// the same fragment.
  2483. static DIExpression *appendToStack(const DIExpression *Expr,
  2484. ArrayRef<uint64_t> Ops);
  2485. /// Create a copy of \p Expr by appending the given list of \p Ops to each
  2486. /// instance of the operand `DW_OP_LLVM_arg, \p ArgNo`. This is used to
  2487. /// modify a specific location used by \p Expr, such as when salvaging that
  2488. /// location.
  2489. static DIExpression *appendOpsToArg(const DIExpression *Expr,
  2490. ArrayRef<uint64_t> Ops, unsigned ArgNo,
  2491. bool StackValue = false);
  2492. /// Create a copy of \p Expr with each instance of
  2493. /// `DW_OP_LLVM_arg, \p OldArg` replaced with `DW_OP_LLVM_arg, \p NewArg`,
  2494. /// and each instance of `DW_OP_LLVM_arg, Arg` with `DW_OP_LLVM_arg, Arg - 1`
  2495. /// for all Arg > \p OldArg.
  2496. /// This is used when replacing one of the operands of a debug value list
  2497. /// with another operand in the same list and deleting the old operand.
  2498. static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg,
  2499. uint64_t NewArg);
  2500. /// Create a DIExpression to describe one part of an aggregate variable that
  2501. /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
  2502. /// will be appended to the elements of \c Expr. If \c Expr already contains
  2503. /// a \c DW_OP_LLVM_fragment \c OffsetInBits is interpreted as an offset
  2504. /// into the existing fragment.
  2505. ///
  2506. /// \param OffsetInBits Offset of the piece in bits.
  2507. /// \param SizeInBits Size of the piece in bits.
  2508. /// \return Creating a fragment expression may fail if \c Expr
  2509. /// contains arithmetic operations that would be
  2510. /// truncated.
  2511. static std::optional<DIExpression *>
  2512. createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits,
  2513. unsigned SizeInBits);
  2514. /// Determine the relative position of the fragments passed in.
  2515. /// Returns -1 if this is entirely before Other, 0 if this and Other overlap,
  2516. /// 1 if this is entirely after Other.
  2517. static int fragmentCmp(const FragmentInfo &A, const FragmentInfo &B) {
  2518. uint64_t l1 = A.OffsetInBits;
  2519. uint64_t l2 = B.OffsetInBits;
  2520. uint64_t r1 = l1 + A.SizeInBits;
  2521. uint64_t r2 = l2 + B.SizeInBits;
  2522. if (r1 <= l2)
  2523. return -1;
  2524. else if (r2 <= l1)
  2525. return 1;
  2526. else
  2527. return 0;
  2528. }
  2529. using ExtOps = std::array<uint64_t, 6>;
  2530. /// Returns the ops for a zero- or sign-extension in a DIExpression.
  2531. static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed);
  2532. /// Append a zero- or sign-extension to \p Expr. Converts the expression to a
  2533. /// stack value if it isn't one already.
  2534. static DIExpression *appendExt(const DIExpression *Expr, unsigned FromSize,
  2535. unsigned ToSize, bool Signed);
  2536. /// Check if fragments overlap between a pair of FragmentInfos.
  2537. static bool fragmentsOverlap(const FragmentInfo &A, const FragmentInfo &B) {
  2538. return fragmentCmp(A, B) == 0;
  2539. }
  2540. /// Determine the relative position of the fragments described by this
  2541. /// DIExpression and \p Other. Calls static fragmentCmp implementation.
  2542. int fragmentCmp(const DIExpression *Other) const {
  2543. auto Fragment1 = *getFragmentInfo();
  2544. auto Fragment2 = *Other->getFragmentInfo();
  2545. return fragmentCmp(Fragment1, Fragment2);
  2546. }
  2547. /// Check if fragments overlap between this DIExpression and \p Other.
  2548. bool fragmentsOverlap(const DIExpression *Other) const {
  2549. if (!isFragment() || !Other->isFragment())
  2550. return true;
  2551. return fragmentCmp(Other) == 0;
  2552. }
  2553. /// Check if the expression consists of exactly one entry value operand.
  2554. /// (This is the only configuration of entry values that is supported.)
  2555. bool isEntryValue() const;
  2556. /// Try to shorten an expression with an initial constant operand.
  2557. /// Returns a new expression and constant on success, or the original
  2558. /// expression and constant on failure.
  2559. std::pair<DIExpression *, const ConstantInt *>
  2560. constantFold(const ConstantInt *CI);
  2561. };
  2562. inline bool operator==(const DIExpression::FragmentInfo &A,
  2563. const DIExpression::FragmentInfo &B) {
  2564. return std::tie(A.SizeInBits, A.OffsetInBits) ==
  2565. std::tie(B.SizeInBits, B.OffsetInBits);
  2566. }
  2567. inline bool operator<(const DIExpression::FragmentInfo &A,
  2568. const DIExpression::FragmentInfo &B) {
  2569. return std::tie(A.SizeInBits, A.OffsetInBits) <
  2570. std::tie(B.SizeInBits, B.OffsetInBits);
  2571. }
  2572. template <> struct DenseMapInfo<DIExpression::FragmentInfo> {
  2573. using FragInfo = DIExpression::FragmentInfo;
  2574. static const uint64_t MaxVal = std::numeric_limits<uint64_t>::max();
  2575. static inline FragInfo getEmptyKey() { return {MaxVal, MaxVal}; }
  2576. static inline FragInfo getTombstoneKey() { return {MaxVal - 1, MaxVal - 1}; }
  2577. static unsigned getHashValue(const FragInfo &Frag) {
  2578. return (Frag.SizeInBits & 0xffff) << 16 | (Frag.OffsetInBits & 0xffff);
  2579. }
  2580. static bool isEqual(const FragInfo &A, const FragInfo &B) { return A == B; }
  2581. };
  2582. /// Global variables.
  2583. ///
  2584. /// TODO: Remove DisplayName. It's always equal to Name.
  2585. class DIGlobalVariable : public DIVariable {
  2586. friend class LLVMContextImpl;
  2587. friend class MDNode;
  2588. bool IsLocalToUnit;
  2589. bool IsDefinition;
  2590. DIGlobalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  2591. bool IsLocalToUnit, bool IsDefinition, uint32_t AlignInBits,
  2592. ArrayRef<Metadata *> Ops)
  2593. : DIVariable(C, DIGlobalVariableKind, Storage, Line, Ops, AlignInBits),
  2594. IsLocalToUnit(IsLocalToUnit), IsDefinition(IsDefinition) {}
  2595. ~DIGlobalVariable() = default;
  2596. static DIGlobalVariable *
  2597. getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  2598. StringRef LinkageName, DIFile *File, unsigned Line, DIType *Type,
  2599. bool IsLocalToUnit, bool IsDefinition,
  2600. DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
  2601. uint32_t AlignInBits, DINodeArray Annotations, StorageType Storage,
  2602. bool ShouldCreate = true) {
  2603. return getImpl(Context, Scope, getCanonicalMDString(Context, Name),
  2604. getCanonicalMDString(Context, LinkageName), File, Line, Type,
  2605. IsLocalToUnit, IsDefinition, StaticDataMemberDeclaration,
  2606. cast_or_null<Metadata>(TemplateParams), AlignInBits,
  2607. Annotations.get(), Storage, ShouldCreate);
  2608. }
  2609. static DIGlobalVariable *
  2610. getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  2611. MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
  2612. bool IsLocalToUnit, bool IsDefinition,
  2613. Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
  2614. uint32_t AlignInBits, Metadata *Annotations, StorageType Storage,
  2615. bool ShouldCreate = true);
  2616. TempDIGlobalVariable cloneImpl() const {
  2617. return getTemporary(getContext(), getScope(), getName(), getLinkageName(),
  2618. getFile(), getLine(), getType(), isLocalToUnit(),
  2619. isDefinition(), getStaticDataMemberDeclaration(),
  2620. getTemplateParams(), getAlignInBits(),
  2621. getAnnotations());
  2622. }
  2623. public:
  2624. DEFINE_MDNODE_GET(
  2625. DIGlobalVariable,
  2626. (DIScope * Scope, StringRef Name, StringRef LinkageName, DIFile *File,
  2627. unsigned Line, DIType *Type, bool IsLocalToUnit, bool IsDefinition,
  2628. DIDerivedType *StaticDataMemberDeclaration, MDTuple *TemplateParams,
  2629. uint32_t AlignInBits, DINodeArray Annotations),
  2630. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
  2631. StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations))
  2632. DEFINE_MDNODE_GET(
  2633. DIGlobalVariable,
  2634. (Metadata * Scope, MDString *Name, MDString *LinkageName, Metadata *File,
  2635. unsigned Line, Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
  2636. Metadata *StaticDataMemberDeclaration, Metadata *TemplateParams,
  2637. uint32_t AlignInBits, Metadata *Annotations),
  2638. (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition,
  2639. StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations))
  2640. TempDIGlobalVariable clone() const { return cloneImpl(); }
  2641. bool isLocalToUnit() const { return IsLocalToUnit; }
  2642. bool isDefinition() const { return IsDefinition; }
  2643. StringRef getDisplayName() const { return getStringOperand(4); }
  2644. StringRef getLinkageName() const { return getStringOperand(5); }
  2645. DIDerivedType *getStaticDataMemberDeclaration() const {
  2646. return cast_or_null<DIDerivedType>(getRawStaticDataMemberDeclaration());
  2647. }
  2648. DINodeArray getAnnotations() const {
  2649. return cast_or_null<MDTuple>(getRawAnnotations());
  2650. }
  2651. MDString *getRawLinkageName() const { return getOperandAs<MDString>(5); }
  2652. Metadata *getRawStaticDataMemberDeclaration() const { return getOperand(6); }
  2653. Metadata *getRawTemplateParams() const { return getOperand(7); }
  2654. MDTuple *getTemplateParams() const { return getOperandAs<MDTuple>(7); }
  2655. Metadata *getRawAnnotations() const { return getOperand(8); }
  2656. static bool classof(const Metadata *MD) {
  2657. return MD->getMetadataID() == DIGlobalVariableKind;
  2658. }
  2659. };
  2660. class DICommonBlock : public DIScope {
  2661. unsigned LineNo;
  2662. friend class LLVMContextImpl;
  2663. friend class MDNode;
  2664. DICommonBlock(LLVMContext &Context, StorageType Storage, unsigned LineNo,
  2665. ArrayRef<Metadata *> Ops);
  2666. static DICommonBlock *getImpl(LLVMContext &Context, DIScope *Scope,
  2667. DIGlobalVariable *Decl, StringRef Name,
  2668. DIFile *File, unsigned LineNo,
  2669. StorageType Storage, bool ShouldCreate = true) {
  2670. return getImpl(Context, Scope, Decl, getCanonicalMDString(Context, Name),
  2671. File, LineNo, Storage, ShouldCreate);
  2672. }
  2673. static DICommonBlock *getImpl(LLVMContext &Context, Metadata *Scope,
  2674. Metadata *Decl, MDString *Name, Metadata *File,
  2675. unsigned LineNo, StorageType Storage,
  2676. bool ShouldCreate = true);
  2677. TempDICommonBlock cloneImpl() const {
  2678. return getTemporary(getContext(), getScope(), getDecl(), getName(),
  2679. getFile(), getLineNo());
  2680. }
  2681. public:
  2682. DEFINE_MDNODE_GET(DICommonBlock,
  2683. (DIScope * Scope, DIGlobalVariable *Decl, StringRef Name,
  2684. DIFile *File, unsigned LineNo),
  2685. (Scope, Decl, Name, File, LineNo))
  2686. DEFINE_MDNODE_GET(DICommonBlock,
  2687. (Metadata * Scope, Metadata *Decl, MDString *Name,
  2688. Metadata *File, unsigned LineNo),
  2689. (Scope, Decl, Name, File, LineNo))
  2690. TempDICommonBlock clone() const { return cloneImpl(); }
  2691. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2692. DIGlobalVariable *getDecl() const {
  2693. return cast_or_null<DIGlobalVariable>(getRawDecl());
  2694. }
  2695. StringRef getName() const { return getStringOperand(2); }
  2696. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2697. unsigned getLineNo() const { return LineNo; }
  2698. Metadata *getRawScope() const { return getOperand(0); }
  2699. Metadata *getRawDecl() const { return getOperand(1); }
  2700. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2701. Metadata *getRawFile() const { return getOperand(3); }
  2702. static bool classof(const Metadata *MD) {
  2703. return MD->getMetadataID() == DICommonBlockKind;
  2704. }
  2705. };
  2706. /// Local variable.
  2707. ///
  2708. /// TODO: Split up flags.
  2709. class DILocalVariable : public DIVariable {
  2710. friend class LLVMContextImpl;
  2711. friend class MDNode;
  2712. unsigned Arg : 16;
  2713. DIFlags Flags;
  2714. DILocalVariable(LLVMContext &C, StorageType Storage, unsigned Line,
  2715. unsigned Arg, DIFlags Flags, uint32_t AlignInBits,
  2716. ArrayRef<Metadata *> Ops)
  2717. : DIVariable(C, DILocalVariableKind, Storage, Line, Ops, AlignInBits),
  2718. Arg(Arg), Flags(Flags) {
  2719. assert(Arg < (1 << 16) && "DILocalVariable: Arg out of range");
  2720. }
  2721. ~DILocalVariable() = default;
  2722. static DILocalVariable *getImpl(LLVMContext &Context, DIScope *Scope,
  2723. StringRef Name, DIFile *File, unsigned Line,
  2724. DIType *Type, unsigned Arg, DIFlags Flags,
  2725. uint32_t AlignInBits, DINodeArray Annotations,
  2726. StorageType Storage,
  2727. bool ShouldCreate = true) {
  2728. return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
  2729. Line, Type, Arg, Flags, AlignInBits, Annotations.get(),
  2730. Storage, ShouldCreate);
  2731. }
  2732. static DILocalVariable *getImpl(LLVMContext &Context, Metadata *Scope,
  2733. MDString *Name, Metadata *File, unsigned Line,
  2734. Metadata *Type, unsigned Arg, DIFlags Flags,
  2735. uint32_t AlignInBits, Metadata *Annotations,
  2736. StorageType Storage,
  2737. bool ShouldCreate = true);
  2738. TempDILocalVariable cloneImpl() const {
  2739. return getTemporary(getContext(), getScope(), getName(), getFile(),
  2740. getLine(), getType(), getArg(), getFlags(),
  2741. getAlignInBits(), getAnnotations());
  2742. }
  2743. public:
  2744. DEFINE_MDNODE_GET(DILocalVariable,
  2745. (DILocalScope * Scope, StringRef Name, DIFile *File,
  2746. unsigned Line, DIType *Type, unsigned Arg, DIFlags Flags,
  2747. uint32_t AlignInBits, DINodeArray Annotations),
  2748. (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits,
  2749. Annotations))
  2750. DEFINE_MDNODE_GET(DILocalVariable,
  2751. (Metadata * Scope, MDString *Name, Metadata *File,
  2752. unsigned Line, Metadata *Type, unsigned Arg, DIFlags Flags,
  2753. uint32_t AlignInBits, Metadata *Annotations),
  2754. (Scope, Name, File, Line, Type, Arg, Flags, AlignInBits,
  2755. Annotations))
  2756. TempDILocalVariable clone() const { return cloneImpl(); }
  2757. /// Get the local scope for this variable.
  2758. ///
  2759. /// Variables must be defined in a local scope.
  2760. DILocalScope *getScope() const {
  2761. return cast<DILocalScope>(DIVariable::getScope());
  2762. }
  2763. bool isParameter() const { return Arg; }
  2764. unsigned getArg() const { return Arg; }
  2765. DIFlags getFlags() const { return Flags; }
  2766. DINodeArray getAnnotations() const {
  2767. return cast_or_null<MDTuple>(getRawAnnotations());
  2768. }
  2769. Metadata *getRawAnnotations() const { return getOperand(4); }
  2770. bool isArtificial() const { return getFlags() & FlagArtificial; }
  2771. bool isObjectPointer() const { return getFlags() & FlagObjectPointer; }
  2772. /// Check that a location is valid for this variable.
  2773. ///
  2774. /// Check that \c DL exists, is in the same subprogram, and has the same
  2775. /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
  2776. /// to a \a DbgInfoIntrinsic.)
  2777. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  2778. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  2779. }
  2780. static bool classof(const Metadata *MD) {
  2781. return MD->getMetadataID() == DILocalVariableKind;
  2782. }
  2783. };
  2784. /// Label.
  2785. ///
  2786. class DILabel : public DINode {
  2787. friend class LLVMContextImpl;
  2788. friend class MDNode;
  2789. unsigned Line;
  2790. DILabel(LLVMContext &C, StorageType Storage, unsigned Line,
  2791. ArrayRef<Metadata *> Ops);
  2792. ~DILabel() = default;
  2793. static DILabel *getImpl(LLVMContext &Context, DIScope *Scope, StringRef Name,
  2794. DIFile *File, unsigned Line, StorageType Storage,
  2795. bool ShouldCreate = true) {
  2796. return getImpl(Context, Scope, getCanonicalMDString(Context, Name), File,
  2797. Line, Storage, ShouldCreate);
  2798. }
  2799. static DILabel *getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
  2800. Metadata *File, unsigned Line, StorageType Storage,
  2801. bool ShouldCreate = true);
  2802. TempDILabel cloneImpl() const {
  2803. return getTemporary(getContext(), getScope(), getName(), getFile(),
  2804. getLine());
  2805. }
  2806. public:
  2807. DEFINE_MDNODE_GET(DILabel,
  2808. (DILocalScope * Scope, StringRef Name, DIFile *File,
  2809. unsigned Line),
  2810. (Scope, Name, File, Line))
  2811. DEFINE_MDNODE_GET(DILabel,
  2812. (Metadata * Scope, MDString *Name, Metadata *File,
  2813. unsigned Line),
  2814. (Scope, Name, File, Line))
  2815. TempDILabel clone() const { return cloneImpl(); }
  2816. /// Get the local scope for this label.
  2817. ///
  2818. /// Labels must be defined in a local scope.
  2819. DILocalScope *getScope() const {
  2820. return cast_or_null<DILocalScope>(getRawScope());
  2821. }
  2822. unsigned getLine() const { return Line; }
  2823. StringRef getName() const { return getStringOperand(1); }
  2824. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2825. Metadata *getRawScope() const { return getOperand(0); }
  2826. MDString *getRawName() const { return getOperandAs<MDString>(1); }
  2827. Metadata *getRawFile() const { return getOperand(2); }
  2828. /// Check that a location is valid for this label.
  2829. ///
  2830. /// Check that \c DL exists, is in the same subprogram, and has the same
  2831. /// inlined-at location as \c this. (Otherwise, it's not a valid attachment
  2832. /// to a \a DbgInfoIntrinsic.)
  2833. bool isValidLocationForIntrinsic(const DILocation *DL) const {
  2834. return DL && getScope()->getSubprogram() == DL->getScope()->getSubprogram();
  2835. }
  2836. static bool classof(const Metadata *MD) {
  2837. return MD->getMetadataID() == DILabelKind;
  2838. }
  2839. };
  2840. class DIObjCProperty : public DINode {
  2841. friend class LLVMContextImpl;
  2842. friend class MDNode;
  2843. unsigned Line;
  2844. unsigned Attributes;
  2845. DIObjCProperty(LLVMContext &C, StorageType Storage, unsigned Line,
  2846. unsigned Attributes, ArrayRef<Metadata *> Ops);
  2847. ~DIObjCProperty() = default;
  2848. static DIObjCProperty *
  2849. getImpl(LLVMContext &Context, StringRef Name, DIFile *File, unsigned Line,
  2850. StringRef GetterName, StringRef SetterName, unsigned Attributes,
  2851. DIType *Type, StorageType Storage, bool ShouldCreate = true) {
  2852. return getImpl(Context, getCanonicalMDString(Context, Name), File, Line,
  2853. getCanonicalMDString(Context, GetterName),
  2854. getCanonicalMDString(Context, SetterName), Attributes, Type,
  2855. Storage, ShouldCreate);
  2856. }
  2857. static DIObjCProperty *getImpl(LLVMContext &Context, MDString *Name,
  2858. Metadata *File, unsigned Line,
  2859. MDString *GetterName, MDString *SetterName,
  2860. unsigned Attributes, Metadata *Type,
  2861. StorageType Storage, bool ShouldCreate = true);
  2862. TempDIObjCProperty cloneImpl() const {
  2863. return getTemporary(getContext(), getName(), getFile(), getLine(),
  2864. getGetterName(), getSetterName(), getAttributes(),
  2865. getType());
  2866. }
  2867. public:
  2868. DEFINE_MDNODE_GET(DIObjCProperty,
  2869. (StringRef Name, DIFile *File, unsigned Line,
  2870. StringRef GetterName, StringRef SetterName,
  2871. unsigned Attributes, DIType *Type),
  2872. (Name, File, Line, GetterName, SetterName, Attributes,
  2873. Type))
  2874. DEFINE_MDNODE_GET(DIObjCProperty,
  2875. (MDString * Name, Metadata *File, unsigned Line,
  2876. MDString *GetterName, MDString *SetterName,
  2877. unsigned Attributes, Metadata *Type),
  2878. (Name, File, Line, GetterName, SetterName, Attributes,
  2879. Type))
  2880. TempDIObjCProperty clone() const { return cloneImpl(); }
  2881. unsigned getLine() const { return Line; }
  2882. unsigned getAttributes() const { return Attributes; }
  2883. StringRef getName() const { return getStringOperand(0); }
  2884. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2885. StringRef getGetterName() const { return getStringOperand(2); }
  2886. StringRef getSetterName() const { return getStringOperand(3); }
  2887. DIType *getType() const { return cast_or_null<DIType>(getRawType()); }
  2888. StringRef getFilename() const {
  2889. if (auto *F = getFile())
  2890. return F->getFilename();
  2891. return "";
  2892. }
  2893. StringRef getDirectory() const {
  2894. if (auto *F = getFile())
  2895. return F->getDirectory();
  2896. return "";
  2897. }
  2898. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  2899. Metadata *getRawFile() const { return getOperand(1); }
  2900. MDString *getRawGetterName() const { return getOperandAs<MDString>(2); }
  2901. MDString *getRawSetterName() const { return getOperandAs<MDString>(3); }
  2902. Metadata *getRawType() const { return getOperand(4); }
  2903. static bool classof(const Metadata *MD) {
  2904. return MD->getMetadataID() == DIObjCPropertyKind;
  2905. }
  2906. };
  2907. /// An imported module (C++ using directive or similar).
  2908. class DIImportedEntity : public DINode {
  2909. friend class LLVMContextImpl;
  2910. friend class MDNode;
  2911. unsigned Line;
  2912. DIImportedEntity(LLVMContext &C, StorageType Storage, unsigned Tag,
  2913. unsigned Line, ArrayRef<Metadata *> Ops)
  2914. : DINode(C, DIImportedEntityKind, Storage, Tag, Ops), Line(Line) {}
  2915. ~DIImportedEntity() = default;
  2916. static DIImportedEntity *getImpl(LLVMContext &Context, unsigned Tag,
  2917. DIScope *Scope, DINode *Entity, DIFile *File,
  2918. unsigned Line, StringRef Name,
  2919. DINodeArray Elements, StorageType Storage,
  2920. bool ShouldCreate = true) {
  2921. return getImpl(Context, Tag, Scope, Entity, File, Line,
  2922. getCanonicalMDString(Context, Name), Elements.get(), Storage,
  2923. ShouldCreate);
  2924. }
  2925. static DIImportedEntity *
  2926. getImpl(LLVMContext &Context, unsigned Tag, Metadata *Scope, Metadata *Entity,
  2927. Metadata *File, unsigned Line, MDString *Name, Metadata *Elements,
  2928. StorageType Storage, bool ShouldCreate = true);
  2929. TempDIImportedEntity cloneImpl() const {
  2930. return getTemporary(getContext(), getTag(), getScope(), getEntity(),
  2931. getFile(), getLine(), getName(), getElements());
  2932. }
  2933. public:
  2934. DEFINE_MDNODE_GET(DIImportedEntity,
  2935. (unsigned Tag, DIScope *Scope, DINode *Entity, DIFile *File,
  2936. unsigned Line, StringRef Name = "",
  2937. DINodeArray Elements = nullptr),
  2938. (Tag, Scope, Entity, File, Line, Name, Elements))
  2939. DEFINE_MDNODE_GET(DIImportedEntity,
  2940. (unsigned Tag, Metadata *Scope, Metadata *Entity,
  2941. Metadata *File, unsigned Line, MDString *Name,
  2942. Metadata *Elements = nullptr),
  2943. (Tag, Scope, Entity, File, Line, Name, Elements))
  2944. TempDIImportedEntity clone() const { return cloneImpl(); }
  2945. unsigned getLine() const { return Line; }
  2946. DIScope *getScope() const { return cast_or_null<DIScope>(getRawScope()); }
  2947. DINode *getEntity() const { return cast_or_null<DINode>(getRawEntity()); }
  2948. StringRef getName() const { return getStringOperand(2); }
  2949. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  2950. DINodeArray getElements() const {
  2951. return cast_or_null<MDTuple>(getRawElements());
  2952. }
  2953. Metadata *getRawScope() const { return getOperand(0); }
  2954. Metadata *getRawEntity() const { return getOperand(1); }
  2955. MDString *getRawName() const { return getOperandAs<MDString>(2); }
  2956. Metadata *getRawFile() const { return getOperand(3); }
  2957. Metadata *getRawElements() const { return getOperand(4); }
  2958. static bool classof(const Metadata *MD) {
  2959. return MD->getMetadataID() == DIImportedEntityKind;
  2960. }
  2961. };
  2962. /// A pair of DIGlobalVariable and DIExpression.
  2963. class DIGlobalVariableExpression : public MDNode {
  2964. friend class LLVMContextImpl;
  2965. friend class MDNode;
  2966. DIGlobalVariableExpression(LLVMContext &C, StorageType Storage,
  2967. ArrayRef<Metadata *> Ops)
  2968. : MDNode(C, DIGlobalVariableExpressionKind, Storage, Ops) {}
  2969. ~DIGlobalVariableExpression() = default;
  2970. static DIGlobalVariableExpression *
  2971. getImpl(LLVMContext &Context, Metadata *Variable, Metadata *Expression,
  2972. StorageType Storage, bool ShouldCreate = true);
  2973. TempDIGlobalVariableExpression cloneImpl() const {
  2974. return getTemporary(getContext(), getVariable(), getExpression());
  2975. }
  2976. public:
  2977. DEFINE_MDNODE_GET(DIGlobalVariableExpression,
  2978. (Metadata * Variable, Metadata *Expression),
  2979. (Variable, Expression))
  2980. TempDIGlobalVariableExpression clone() const { return cloneImpl(); }
  2981. Metadata *getRawVariable() const { return getOperand(0); }
  2982. DIGlobalVariable *getVariable() const {
  2983. return cast_or_null<DIGlobalVariable>(getRawVariable());
  2984. }
  2985. Metadata *getRawExpression() const { return getOperand(1); }
  2986. DIExpression *getExpression() const {
  2987. return cast<DIExpression>(getRawExpression());
  2988. }
  2989. static bool classof(const Metadata *MD) {
  2990. return MD->getMetadataID() == DIGlobalVariableExpressionKind;
  2991. }
  2992. };
  2993. /// Macro Info DWARF-like metadata node.
  2994. ///
  2995. /// A metadata node with a DWARF macro info (i.e., a constant named
  2996. /// \c DW_MACINFO_*, defined in llvm/BinaryFormat/Dwarf.h). Called \a
  2997. /// DIMacroNode
  2998. /// because it's potentially used for non-DWARF output.
  2999. class DIMacroNode : public MDNode {
  3000. friend class LLVMContextImpl;
  3001. friend class MDNode;
  3002. protected:
  3003. DIMacroNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned MIType,
  3004. ArrayRef<Metadata *> Ops1,
  3005. ArrayRef<Metadata *> Ops2 = std::nullopt)
  3006. : MDNode(C, ID, Storage, Ops1, Ops2) {
  3007. assert(MIType < 1u << 16);
  3008. SubclassData16 = MIType;
  3009. }
  3010. ~DIMacroNode() = default;
  3011. template <class Ty> Ty *getOperandAs(unsigned I) const {
  3012. return cast_or_null<Ty>(getOperand(I));
  3013. }
  3014. StringRef getStringOperand(unsigned I) const {
  3015. if (auto *S = getOperandAs<MDString>(I))
  3016. return S->getString();
  3017. return StringRef();
  3018. }
  3019. static MDString *getCanonicalMDString(LLVMContext &Context, StringRef S) {
  3020. if (S.empty())
  3021. return nullptr;
  3022. return MDString::get(Context, S);
  3023. }
  3024. public:
  3025. unsigned getMacinfoType() const { return SubclassData16; }
  3026. static bool classof(const Metadata *MD) {
  3027. switch (MD->getMetadataID()) {
  3028. default:
  3029. return false;
  3030. case DIMacroKind:
  3031. case DIMacroFileKind:
  3032. return true;
  3033. }
  3034. }
  3035. };
  3036. class DIMacro : public DIMacroNode {
  3037. friend class LLVMContextImpl;
  3038. friend class MDNode;
  3039. unsigned Line;
  3040. DIMacro(LLVMContext &C, StorageType Storage, unsigned MIType, unsigned Line,
  3041. ArrayRef<Metadata *> Ops)
  3042. : DIMacroNode(C, DIMacroKind, Storage, MIType, Ops), Line(Line) {}
  3043. ~DIMacro() = default;
  3044. static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
  3045. StringRef Name, StringRef Value, StorageType Storage,
  3046. bool ShouldCreate = true) {
  3047. return getImpl(Context, MIType, Line, getCanonicalMDString(Context, Name),
  3048. getCanonicalMDString(Context, Value), Storage, ShouldCreate);
  3049. }
  3050. static DIMacro *getImpl(LLVMContext &Context, unsigned MIType, unsigned Line,
  3051. MDString *Name, MDString *Value, StorageType Storage,
  3052. bool ShouldCreate = true);
  3053. TempDIMacro cloneImpl() const {
  3054. return getTemporary(getContext(), getMacinfoType(), getLine(), getName(),
  3055. getValue());
  3056. }
  3057. public:
  3058. DEFINE_MDNODE_GET(DIMacro,
  3059. (unsigned MIType, unsigned Line, StringRef Name,
  3060. StringRef Value = ""),
  3061. (MIType, Line, Name, Value))
  3062. DEFINE_MDNODE_GET(DIMacro,
  3063. (unsigned MIType, unsigned Line, MDString *Name,
  3064. MDString *Value),
  3065. (MIType, Line, Name, Value))
  3066. TempDIMacro clone() const { return cloneImpl(); }
  3067. unsigned getLine() const { return Line; }
  3068. StringRef getName() const { return getStringOperand(0); }
  3069. StringRef getValue() const { return getStringOperand(1); }
  3070. MDString *getRawName() const { return getOperandAs<MDString>(0); }
  3071. MDString *getRawValue() const { return getOperandAs<MDString>(1); }
  3072. static bool classof(const Metadata *MD) {
  3073. return MD->getMetadataID() == DIMacroKind;
  3074. }
  3075. };
  3076. class DIMacroFile : public DIMacroNode {
  3077. friend class LLVMContextImpl;
  3078. friend class MDNode;
  3079. unsigned Line;
  3080. DIMacroFile(LLVMContext &C, StorageType Storage, unsigned MIType,
  3081. unsigned Line, ArrayRef<Metadata *> Ops)
  3082. : DIMacroNode(C, DIMacroFileKind, Storage, MIType, Ops), Line(Line) {}
  3083. ~DIMacroFile() = default;
  3084. static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
  3085. unsigned Line, DIFile *File,
  3086. DIMacroNodeArray Elements, StorageType Storage,
  3087. bool ShouldCreate = true) {
  3088. return getImpl(Context, MIType, Line, static_cast<Metadata *>(File),
  3089. Elements.get(), Storage, ShouldCreate);
  3090. }
  3091. static DIMacroFile *getImpl(LLVMContext &Context, unsigned MIType,
  3092. unsigned Line, Metadata *File, Metadata *Elements,
  3093. StorageType Storage, bool ShouldCreate = true);
  3094. TempDIMacroFile cloneImpl() const {
  3095. return getTemporary(getContext(), getMacinfoType(), getLine(), getFile(),
  3096. getElements());
  3097. }
  3098. public:
  3099. DEFINE_MDNODE_GET(DIMacroFile,
  3100. (unsigned MIType, unsigned Line, DIFile *File,
  3101. DIMacroNodeArray Elements),
  3102. (MIType, Line, File, Elements))
  3103. DEFINE_MDNODE_GET(DIMacroFile,
  3104. (unsigned MIType, unsigned Line, Metadata *File,
  3105. Metadata *Elements),
  3106. (MIType, Line, File, Elements))
  3107. TempDIMacroFile clone() const { return cloneImpl(); }
  3108. void replaceElements(DIMacroNodeArray Elements) {
  3109. #ifndef NDEBUG
  3110. for (DIMacroNode *Op : getElements())
  3111. assert(is_contained(Elements->operands(), Op) &&
  3112. "Lost a macro node during macro node list replacement");
  3113. #endif
  3114. replaceOperandWith(1, Elements.get());
  3115. }
  3116. unsigned getLine() const { return Line; }
  3117. DIFile *getFile() const { return cast_or_null<DIFile>(getRawFile()); }
  3118. DIMacroNodeArray getElements() const {
  3119. return cast_or_null<MDTuple>(getRawElements());
  3120. }
  3121. Metadata *getRawFile() const { return getOperand(0); }
  3122. Metadata *getRawElements() const { return getOperand(1); }
  3123. static bool classof(const Metadata *MD) {
  3124. return MD->getMetadataID() == DIMacroFileKind;
  3125. }
  3126. };
  3127. /// List of ValueAsMetadata, to be used as an argument to a dbg.value
  3128. /// intrinsic.
  3129. class DIArgList : public MDNode {
  3130. friend class LLVMContextImpl;
  3131. friend class MDNode;
  3132. using iterator = SmallVectorImpl<ValueAsMetadata *>::iterator;
  3133. SmallVector<ValueAsMetadata *, 4> Args;
  3134. DIArgList(LLVMContext &C, StorageType Storage,
  3135. ArrayRef<ValueAsMetadata *> Args)
  3136. : MDNode(C, DIArgListKind, Storage, std::nullopt),
  3137. Args(Args.begin(), Args.end()) {
  3138. track();
  3139. }
  3140. ~DIArgList() { untrack(); }
  3141. static DIArgList *getImpl(LLVMContext &Context,
  3142. ArrayRef<ValueAsMetadata *> Args,
  3143. StorageType Storage, bool ShouldCreate = true);
  3144. TempDIArgList cloneImpl() const {
  3145. return getTemporary(getContext(), getArgs());
  3146. }
  3147. void track();
  3148. void untrack();
  3149. void dropAllReferences();
  3150. public:
  3151. DEFINE_MDNODE_GET(DIArgList, (ArrayRef<ValueAsMetadata *> Args), (Args))
  3152. TempDIArgList clone() const { return cloneImpl(); }
  3153. ArrayRef<ValueAsMetadata *> getArgs() const { return Args; }
  3154. iterator args_begin() { return Args.begin(); }
  3155. iterator args_end() { return Args.end(); }
  3156. static bool classof(const Metadata *MD) {
  3157. return MD->getMetadataID() == DIArgListKind;
  3158. }
  3159. void handleChangedOperand(void *Ref, Metadata *New);
  3160. };
  3161. /// Identifies a unique instance of a variable.
  3162. ///
  3163. /// Storage for identifying a potentially inlined instance of a variable,
  3164. /// or a fragment thereof. This guarantees that exactly one variable instance
  3165. /// may be identified by this class, even when that variable is a fragment of
  3166. /// an aggregate variable and/or there is another inlined instance of the same
  3167. /// source code variable nearby.
  3168. /// This class does not necessarily uniquely identify that variable: it is
  3169. /// possible that a DebugVariable with different parameters may point to the
  3170. /// same variable instance, but not that one DebugVariable points to multiple
  3171. /// variable instances.
  3172. class DebugVariable {
  3173. using FragmentInfo = DIExpression::FragmentInfo;
  3174. const DILocalVariable *Variable;
  3175. std::optional<FragmentInfo> Fragment;
  3176. const DILocation *InlinedAt;
  3177. /// Fragment that will overlap all other fragments. Used as default when
  3178. /// caller demands a fragment.
  3179. static const FragmentInfo DefaultFragment;
  3180. public:
  3181. DebugVariable(const DbgVariableIntrinsic *DII);
  3182. DebugVariable(const DILocalVariable *Var,
  3183. std::optional<FragmentInfo> FragmentInfo,
  3184. const DILocation *InlinedAt)
  3185. : Variable(Var), Fragment(FragmentInfo), InlinedAt(InlinedAt) {}
  3186. DebugVariable(const DILocalVariable *Var, const DIExpression *DIExpr,
  3187. const DILocation *InlinedAt)
  3188. : Variable(Var),
  3189. Fragment(DIExpr ? DIExpr->getFragmentInfo() : std::nullopt),
  3190. InlinedAt(InlinedAt) {}
  3191. const DILocalVariable *getVariable() const { return Variable; }
  3192. std::optional<FragmentInfo> getFragment() const { return Fragment; }
  3193. const DILocation *getInlinedAt() const { return InlinedAt; }
  3194. FragmentInfo getFragmentOrDefault() const {
  3195. return Fragment.value_or(DefaultFragment);
  3196. }
  3197. static bool isDefaultFragment(const FragmentInfo F) {
  3198. return F == DefaultFragment;
  3199. }
  3200. bool operator==(const DebugVariable &Other) const {
  3201. return std::tie(Variable, Fragment, InlinedAt) ==
  3202. std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
  3203. }
  3204. bool operator<(const DebugVariable &Other) const {
  3205. return std::tie(Variable, Fragment, InlinedAt) <
  3206. std::tie(Other.Variable, Other.Fragment, Other.InlinedAt);
  3207. }
  3208. };
  3209. template <> struct DenseMapInfo<DebugVariable> {
  3210. using FragmentInfo = DIExpression::FragmentInfo;
  3211. /// Empty key: no key should be generated that has no DILocalVariable.
  3212. static inline DebugVariable getEmptyKey() {
  3213. return DebugVariable(nullptr, std::nullopt, nullptr);
  3214. }
  3215. /// Difference in tombstone is that the Optional is meaningful.
  3216. static inline DebugVariable getTombstoneKey() {
  3217. return DebugVariable(nullptr, {{0, 0}}, nullptr);
  3218. }
  3219. static unsigned getHashValue(const DebugVariable &D) {
  3220. unsigned HV = 0;
  3221. const std::optional<FragmentInfo> Fragment = D.getFragment();
  3222. if (Fragment)
  3223. HV = DenseMapInfo<FragmentInfo>::getHashValue(*Fragment);
  3224. return hash_combine(D.getVariable(), HV, D.getInlinedAt());
  3225. }
  3226. static bool isEqual(const DebugVariable &A, const DebugVariable &B) {
  3227. return A == B;
  3228. }
  3229. };
  3230. } // end namespace llvm
  3231. #undef DEFINE_MDNODE_GET_UNPACK_IMPL
  3232. #undef DEFINE_MDNODE_GET_UNPACK
  3233. #undef DEFINE_MDNODE_GET
  3234. #endif // LLVM_IR_DEBUGINFOMETADATA_H
  3235. #ifdef __GNUC__
  3236. #pragma GCC diagnostic pop
  3237. #endif