Metadata.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. //===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. /// @file
  11. /// This file contains the declarations for metadata subclasses.
  12. /// They represent the different flavors of metadata that live in LLVM.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_IR_METADATA_H
  16. #define LLVM_IR_METADATA_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/ilist_node.h"
  20. #include "llvm/ADT/iterator_range.h"
  21. #include "llvm/IR/Constant.h"
  22. #include "llvm/IR/MetadataTracking.h"
  23. #include "llvm/IR/Value.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include <type_traits>
  26. namespace llvm {
  27. class LLVMContext;
  28. class Module;
  29. class ModuleSlotTracker;
  30. template<typename ValueSubClass, typename ItemParentClass>
  31. class SymbolTableListTraits;
  32. enum LLVMConstants : uint32_t {
  33. DEBUG_METADATA_VERSION = 3 // Current debug info version number.
  34. };
  35. /// \brief Root of the metadata hierarchy.
  36. ///
  37. /// This is a root class for typeless data in the IR.
  38. class Metadata {
  39. friend class ReplaceableMetadataImpl;
  40. /// \brief RTTI.
  41. const unsigned char SubclassID;
  42. protected:
  43. /// \brief Active type of storage.
  44. enum StorageType { Uniqued, Distinct, Temporary };
  45. /// \brief Storage flag for non-uniqued, otherwise unowned, metadata.
  46. unsigned Storage : 2;
  47. // TODO: expose remaining bits to subclasses.
  48. unsigned short SubclassData16;
  49. unsigned SubclassData32;
  50. public:
  51. enum MetadataKind {
  52. MDTupleKind,
  53. DILocationKind,
  54. GenericDINodeKind,
  55. DISubrangeKind,
  56. DIEnumeratorKind,
  57. DIBasicTypeKind,
  58. DIDerivedTypeKind,
  59. DICompositeTypeKind,
  60. DISubroutineTypeKind,
  61. DIFileKind,
  62. DICompileUnitKind,
  63. DISubprogramKind,
  64. DILexicalBlockKind,
  65. DILexicalBlockFileKind,
  66. DINamespaceKind,
  67. DIModuleKind,
  68. DITemplateTypeParameterKind,
  69. DITemplateValueParameterKind,
  70. DIGlobalVariableKind,
  71. DILocalVariableKind,
  72. DIExpressionKind,
  73. DIObjCPropertyKind,
  74. DIImportedEntityKind,
  75. ConstantAsMetadataKind,
  76. LocalAsMetadataKind,
  77. MDStringKind
  78. };
  79. protected:
  80. Metadata(unsigned ID, StorageType Storage)
  81. : SubclassID(ID), Storage(Storage), SubclassData16(0), SubclassData32(0) {
  82. }
  83. ~Metadata() = default;
  84. /// \brief Default handling of a changed operand, which asserts.
  85. ///
  86. /// If subclasses pass themselves in as owners to a tracking node reference,
  87. /// they must provide an implementation of this method.
  88. void handleChangedOperand(void *, Metadata *) {
  89. llvm_unreachable("Unimplemented in Metadata subclass");
  90. }
  91. public:
  92. unsigned getMetadataID() const { return SubclassID; }
  93. /// \brief User-friendly dump.
  94. ///
  95. /// If \c M is provided, metadata nodes will be numbered canonically;
  96. /// otherwise, pointer addresses are substituted.
  97. ///
  98. /// Note: this uses an explicit overload instead of default arguments so that
  99. /// the nullptr version is easy to call from a debugger.
  100. ///
  101. /// @{
  102. void dump() const;
  103. void dump(const Module *M) const;
  104. /// @}
  105. /// \brief Print.
  106. ///
  107. /// Prints definition of \c this.
  108. ///
  109. /// If \c M is provided, metadata nodes will be numbered canonically;
  110. /// otherwise, pointer addresses are substituted.
  111. /// @{
  112. void print(raw_ostream &OS, const Module *M = nullptr) const;
  113. void print(raw_ostream &OS, ModuleSlotTracker &MST,
  114. const Module *M = nullptr) const;
  115. /// @}
  116. /// \brief Print as operand.
  117. ///
  118. /// Prints reference of \c this.
  119. ///
  120. /// If \c M is provided, metadata nodes will be numbered canonically;
  121. /// otherwise, pointer addresses are substituted.
  122. /// @{
  123. void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
  124. void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
  125. const Module *M = nullptr) const;
  126. /// @}
  127. };
  128. #define HANDLE_METADATA(CLASS) class CLASS;
  129. #include "llvm/IR/Metadata.def"
  130. // Provide specializations of isa so that we don't need definitions of
  131. // subclasses to see if the metadata is a subclass.
  132. #define HANDLE_METADATA_LEAF(CLASS) \
  133. template <> struct isa_impl<CLASS, Metadata> { \
  134. static inline bool doit(const Metadata &MD) { \
  135. return MD.getMetadataID() == Metadata::CLASS##Kind; \
  136. } \
  137. };
  138. #include "llvm/IR/Metadata.def"
  139. inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
  140. MD.print(OS);
  141. return OS;
  142. }
  143. /// \brief Metadata wrapper in the Value hierarchy.
  144. ///
  145. /// A member of the \a Value hierarchy to represent a reference to metadata.
  146. /// This allows, e.g., instrinsics to have metadata as operands.
  147. ///
  148. /// Notably, this is the only thing in either hierarchy that is allowed to
  149. /// reference \a LocalAsMetadata.
  150. class MetadataAsValue : public Value {
  151. friend class ReplaceableMetadataImpl;
  152. friend class LLVMContextImpl;
  153. Metadata *MD;
  154. MetadataAsValue(Type *Ty, Metadata *MD);
  155. ~MetadataAsValue() override;
  156. /// \brief Drop use of metadata (during teardown).
  157. void dropUse() { MD = nullptr; }
  158. public:
  159. static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
  160. static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
  161. Metadata *getMetadata() const { return MD; }
  162. static bool classof(const Value *V) {
  163. return V->getValueID() == MetadataAsValueVal;
  164. }
  165. private:
  166. void handleChangedMetadata(Metadata *MD);
  167. void track();
  168. void untrack();
  169. };
  170. /// \brief Shared implementation of use-lists for replaceable metadata.
  171. ///
  172. /// Most metadata cannot be RAUW'ed. This is a shared implementation of
  173. /// use-lists and associated API for the two that support it (\a ValueAsMetadata
  174. /// and \a TempMDNode).
  175. class ReplaceableMetadataImpl {
  176. friend class MetadataTracking;
  177. public:
  178. typedef MetadataTracking::OwnerTy OwnerTy;
  179. private:
  180. LLVMContext &Context;
  181. uint64_t NextIndex;
  182. SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
  183. public:
  184. ReplaceableMetadataImpl(LLVMContext &Context)
  185. : Context(Context), NextIndex(0) {}
  186. ~ReplaceableMetadataImpl() {
  187. assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
  188. }
  189. LLVMContext &getContext() const { return Context; }
  190. /// \brief Replace all uses of this with MD.
  191. ///
  192. /// Replace all uses of this with \c MD, which is allowed to be null.
  193. void replaceAllUsesWith(Metadata *MD);
  194. /// \brief Resolve all uses of this.
  195. ///
  196. /// Resolve all uses of this, turning off RAUW permanently. If \c
  197. /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
  198. /// is resolved.
  199. void resolveAllUses(bool ResolveUsers = true);
  200. private:
  201. void addRef(void *Ref, OwnerTy Owner);
  202. void dropRef(void *Ref);
  203. void moveRef(void *Ref, void *New, const Metadata &MD);
  204. static ReplaceableMetadataImpl *get(Metadata &MD);
  205. };
  206. /// \brief Value wrapper in the Metadata hierarchy.
  207. ///
  208. /// This is a custom value handle that allows other metadata to refer to
  209. /// classes in the Value hierarchy.
  210. ///
  211. /// Because of full uniquing support, each value is only wrapped by a single \a
  212. /// ValueAsMetadata object, so the lookup maps are far more efficient than
  213. /// those using ValueHandleBase.
  214. class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
  215. friend class ReplaceableMetadataImpl;
  216. friend class LLVMContextImpl;
  217. Value *V;
  218. /// \brief Drop users without RAUW (during teardown).
  219. void dropUsers() {
  220. ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
  221. }
  222. protected:
  223. ValueAsMetadata(unsigned ID, Value *V)
  224. : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
  225. assert(V && "Expected valid value");
  226. }
  227. ~ValueAsMetadata() = default;
  228. public:
  229. static ValueAsMetadata *get(Value *V);
  230. static ConstantAsMetadata *getConstant(Value *C) {
  231. return cast<ConstantAsMetadata>(get(C));
  232. }
  233. static LocalAsMetadata *getLocal(Value *Local) {
  234. return cast<LocalAsMetadata>(get(Local));
  235. }
  236. static ValueAsMetadata *getIfExists(Value *V);
  237. static ConstantAsMetadata *getConstantIfExists(Value *C) {
  238. return cast_or_null<ConstantAsMetadata>(getIfExists(C));
  239. }
  240. static LocalAsMetadata *getLocalIfExists(Value *Local) {
  241. return cast_or_null<LocalAsMetadata>(getIfExists(Local));
  242. }
  243. Value *getValue() const { return V; }
  244. Type *getType() const { return V->getType(); }
  245. LLVMContext &getContext() const { return V->getContext(); }
  246. static void handleDeletion(Value *V);
  247. static void handleRAUW(Value *From, Value *To);
  248. protected:
  249. /// \brief Handle collisions after \a Value::replaceAllUsesWith().
  250. ///
  251. /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
  252. /// \a Value gets RAUW'ed and the target already exists, this is used to
  253. /// merge the two metadata nodes.
  254. void replaceAllUsesWith(Metadata *MD) {
  255. ReplaceableMetadataImpl::replaceAllUsesWith(MD);
  256. }
  257. public:
  258. static bool classof(const Metadata *MD) {
  259. return MD->getMetadataID() == LocalAsMetadataKind ||
  260. MD->getMetadataID() == ConstantAsMetadataKind;
  261. }
  262. };
  263. class ConstantAsMetadata : public ValueAsMetadata {
  264. friend class ValueAsMetadata;
  265. ConstantAsMetadata(Constant *C)
  266. : ValueAsMetadata(ConstantAsMetadataKind, C) {}
  267. public:
  268. static ConstantAsMetadata *get(Constant *C) {
  269. return ValueAsMetadata::getConstant(C);
  270. }
  271. static ConstantAsMetadata *getIfExists(Constant *C) {
  272. return ValueAsMetadata::getConstantIfExists(C);
  273. }
  274. Constant *getValue() const {
  275. return cast<Constant>(ValueAsMetadata::getValue());
  276. }
  277. static bool classof(const Metadata *MD) {
  278. return MD->getMetadataID() == ConstantAsMetadataKind;
  279. }
  280. };
  281. class LocalAsMetadata : public ValueAsMetadata {
  282. friend class ValueAsMetadata;
  283. LocalAsMetadata(Value *Local)
  284. : ValueAsMetadata(LocalAsMetadataKind, Local) {
  285. assert(!isa<Constant>(Local) && "Expected local value");
  286. }
  287. public:
  288. static LocalAsMetadata *get(Value *Local) {
  289. return ValueAsMetadata::getLocal(Local);
  290. }
  291. static LocalAsMetadata *getIfExists(Value *Local) {
  292. return ValueAsMetadata::getLocalIfExists(Local);
  293. }
  294. static bool classof(const Metadata *MD) {
  295. return MD->getMetadataID() == LocalAsMetadataKind;
  296. }
  297. };
  298. /// \brief Transitional API for extracting constants from Metadata.
  299. ///
  300. /// This namespace contains transitional functions for metadata that points to
  301. /// \a Constants.
  302. ///
  303. /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
  304. /// operands could refer to any \a Value. There's was a lot of code like this:
  305. ///
  306. /// \code
  307. /// MDNode *N = ...;
  308. /// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
  309. /// \endcode
  310. ///
  311. /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
  312. /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
  313. /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
  314. /// cast in the \a Value hierarchy. Besides creating boiler-plate, this
  315. /// requires subtle control flow changes.
  316. ///
  317. /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
  318. /// so that metadata can refer to numbers without traversing a bridge to the \a
  319. /// Value hierarchy. In this final state, the code above would look like this:
  320. ///
  321. /// \code
  322. /// MDNode *N = ...;
  323. /// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
  324. /// \endcode
  325. ///
  326. /// The API in this namespace supports the transition. \a MDInt doesn't exist
  327. /// yet, and even once it does, changing each metadata schema to use it is its
  328. /// own mini-project. In the meantime this API prevents us from introducing
  329. /// complex and bug-prone control flow that will disappear in the end. In
  330. /// particular, the above code looks like this:
  331. ///
  332. /// \code
  333. /// MDNode *N = ...;
  334. /// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
  335. /// \endcode
  336. ///
  337. /// The full set of provided functions includes:
  338. ///
  339. /// mdconst::hasa <=> isa
  340. /// mdconst::extract <=> cast
  341. /// mdconst::extract_or_null <=> cast_or_null
  342. /// mdconst::dyn_extract <=> dyn_cast
  343. /// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
  344. ///
  345. /// The target of the cast must be a subclass of \a Constant.
  346. namespace mdconst {
  347. namespace detail {
  348. template <class T> T &make();
  349. template <class T, class Result> struct HasDereference {
  350. typedef char Yes[1];
  351. typedef char No[2];
  352. template <size_t N> struct SFINAE {};
  353. template <class U, class V>
  354. static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
  355. template <class U, class V> static No &hasDereference(...);
  356. static const bool value =
  357. sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
  358. };
  359. template <class V, class M> struct IsValidPointer {
  360. static const bool value = std::is_base_of<Constant, V>::value &&
  361. HasDereference<M, const Metadata &>::value;
  362. };
  363. template <class V, class M> struct IsValidReference {
  364. static const bool value = std::is_base_of<Constant, V>::value &&
  365. std::is_convertible<M, const Metadata &>::value;
  366. };
  367. } // end namespace detail
  368. /// \brief Check whether Metadata has a Value.
  369. ///
  370. /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
  371. /// type \c X.
  372. template <class X, class Y>
  373. inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, bool>::type
  374. hasa(Y &&MD) {
  375. assert(MD && "Null pointer sent into hasa");
  376. if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
  377. return isa<X>(V->getValue());
  378. return false;
  379. }
  380. template <class X, class Y>
  381. inline
  382. typename std::enable_if<detail::IsValidReference<X, Y &>::value, bool>::type
  383. hasa(Y &MD) {
  384. return hasa(&MD);
  385. }
  386. /// \brief Extract a Value from Metadata.
  387. ///
  388. /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
  389. template <class X, class Y>
  390. inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
  391. extract(Y &&MD) {
  392. return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
  393. }
  394. template <class X, class Y>
  395. inline
  396. typename std::enable_if<detail::IsValidReference<X, Y &>::value, X *>::type
  397. extract(Y &MD) {
  398. return extract(&MD);
  399. }
  400. /// \brief Extract a Value from Metadata, allowing null.
  401. ///
  402. /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
  403. /// from \c MD, allowing \c MD to be null.
  404. template <class X, class Y>
  405. inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
  406. extract_or_null(Y &&MD) {
  407. if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
  408. return cast<X>(V->getValue());
  409. return nullptr;
  410. }
  411. /// \brief Extract a Value from Metadata, if any.
  412. ///
  413. /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
  414. /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
  415. /// Value it does contain is of the wrong subclass.
  416. template <class X, class Y>
  417. inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
  418. dyn_extract(Y &&MD) {
  419. if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
  420. return dyn_cast<X>(V->getValue());
  421. return nullptr;
  422. }
  423. /// \brief Extract a Value from Metadata, if any, allowing null.
  424. ///
  425. /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
  426. /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
  427. /// Value it does contain is of the wrong subclass, allowing \c MD to be null.
  428. template <class X, class Y>
  429. inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
  430. dyn_extract_or_null(Y &&MD) {
  431. if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
  432. return dyn_cast<X>(V->getValue());
  433. return nullptr;
  434. }
  435. } // end namespace mdconst
  436. //===----------------------------------------------------------------------===//
  437. /// \brief A single uniqued string.
  438. ///
  439. /// These are used to efficiently contain a byte sequence for metadata.
  440. /// MDString is always unnamed.
  441. class MDString : public Metadata {
  442. friend class StringMapEntry<MDString>;
  443. MDString(const MDString &) = delete;
  444. MDString &operator=(MDString &&) = delete;
  445. MDString &operator=(const MDString &) = delete;
  446. StringMapEntry<MDString> *Entry;
  447. MDString() : Metadata(MDStringKind, Uniqued), Entry(nullptr) {}
  448. MDString(MDString &&) : Metadata(MDStringKind, Uniqued) {}
  449. public:
  450. static MDString *get(LLVMContext &Context, StringRef Str);
  451. static MDString *get(LLVMContext &Context, const char *Str) {
  452. return get(Context, Str ? StringRef(Str) : StringRef());
  453. }
  454. StringRef getString() const;
  455. unsigned getLength() const { return (unsigned)getString().size(); }
  456. typedef StringRef::iterator iterator;
  457. /// \brief Pointer to the first byte of the string.
  458. iterator begin() const { return getString().begin(); }
  459. /// \brief Pointer to one byte past the end of the string.
  460. iterator end() const { return getString().end(); }
  461. const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
  462. const unsigned char *bytes_end() const { return getString().bytes_end(); }
  463. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
  464. static bool classof(const Metadata *MD) {
  465. return MD->getMetadataID() == MDStringKind;
  466. }
  467. };
  468. /// \brief A collection of metadata nodes that might be associated with a
  469. /// memory access used by the alias-analysis infrastructure.
  470. struct AAMDNodes {
  471. explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
  472. MDNode *N = nullptr)
  473. : TBAA(T), Scope(S), NoAlias(N) {}
  474. bool operator==(const AAMDNodes &A) const {
  475. return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
  476. }
  477. bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
  478. explicit operator bool() const { return TBAA || Scope || NoAlias; }
  479. /// \brief The tag for type-based alias analysis.
  480. MDNode *TBAA;
  481. /// \brief The tag for alias scope specification (used with noalias).
  482. MDNode *Scope;
  483. /// \brief The tag specifying the noalias scope.
  484. MDNode *NoAlias;
  485. };
  486. // Specialize DenseMapInfo for AAMDNodes.
  487. template<>
  488. struct DenseMapInfo<AAMDNodes> {
  489. static inline AAMDNodes getEmptyKey() {
  490. return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
  491. }
  492. static inline AAMDNodes getTombstoneKey() {
  493. return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
  494. }
  495. static unsigned getHashValue(const AAMDNodes &Val) {
  496. return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
  497. DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
  498. DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
  499. }
  500. static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
  501. return LHS == RHS;
  502. }
  503. };
  504. /// \brief Tracking metadata reference owned by Metadata.
  505. ///
  506. /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
  507. /// of \a Metadata, which has the option of registering itself for callbacks to
  508. /// re-unique itself.
  509. ///
  510. /// In particular, this is used by \a MDNode.
  511. class MDOperand {
  512. MDOperand(MDOperand &&) = delete;
  513. MDOperand(const MDOperand &) = delete;
  514. MDOperand &operator=(MDOperand &&) = delete;
  515. MDOperand &operator=(const MDOperand &) = delete;
  516. Metadata *MD;
  517. public:
  518. MDOperand() : MD(nullptr) {}
  519. ~MDOperand() { untrack(); }
  520. Metadata *get() const { return MD; }
  521. operator Metadata *() const { return get(); }
  522. Metadata *operator->() const { return get(); }
  523. Metadata &operator*() const { return *get(); }
  524. void reset() {
  525. untrack();
  526. MD = nullptr;
  527. }
  528. void reset(Metadata *MD, Metadata *Owner) {
  529. untrack();
  530. this->MD = MD;
  531. track(Owner);
  532. }
  533. private:
  534. void track(Metadata *Owner) {
  535. if (MD) {
  536. if (Owner)
  537. MetadataTracking::track(this, *MD, *Owner);
  538. else
  539. MetadataTracking::track(MD);
  540. }
  541. }
  542. void untrack() {
  543. assert(static_cast<void *>(this) == &MD && "Expected same address");
  544. if (MD)
  545. MetadataTracking::untrack(MD);
  546. }
  547. };
  548. template <> struct simplify_type<MDOperand> {
  549. typedef Metadata *SimpleType;
  550. static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
  551. };
  552. template <> struct simplify_type<const MDOperand> {
  553. typedef Metadata *SimpleType;
  554. static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
  555. };
  556. /// \brief Pointer to the context, with optional RAUW support.
  557. ///
  558. /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
  559. /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
  560. class ContextAndReplaceableUses {
  561. PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
  562. ContextAndReplaceableUses() = delete;
  563. ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete;
  564. ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete;
  565. ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete;
  566. ContextAndReplaceableUses &
  567. operator=(const ContextAndReplaceableUses &) = delete;
  568. public:
  569. ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
  570. ContextAndReplaceableUses(
  571. std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
  572. : Ptr(ReplaceableUses.release()) {
  573. assert(getReplaceableUses() && "Expected non-null replaceable uses");
  574. }
  575. ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
  576. operator LLVMContext &() { return getContext(); }
  577. /// \brief Whether this contains RAUW support.
  578. bool hasReplaceableUses() const {
  579. return Ptr.is<ReplaceableMetadataImpl *>();
  580. }
  581. LLVMContext &getContext() const {
  582. if (hasReplaceableUses())
  583. return getReplaceableUses()->getContext();
  584. return *Ptr.get<LLVMContext *>();
  585. }
  586. ReplaceableMetadataImpl *getReplaceableUses() const {
  587. if (hasReplaceableUses())
  588. return Ptr.get<ReplaceableMetadataImpl *>();
  589. return nullptr;
  590. }
  591. /// \brief Assign RAUW support to this.
  592. ///
  593. /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
  594. /// not be null).
  595. void
  596. makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
  597. assert(ReplaceableUses && "Expected non-null replaceable uses");
  598. assert(&ReplaceableUses->getContext() == &getContext() &&
  599. "Expected same context");
  600. delete getReplaceableUses();
  601. Ptr = ReplaceableUses.release();
  602. }
  603. /// \brief Drop RAUW support.
  604. ///
  605. /// Cede ownership of RAUW support, returning it.
  606. std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
  607. assert(hasReplaceableUses() && "Expected to own replaceable uses");
  608. std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
  609. getReplaceableUses());
  610. Ptr = &ReplaceableUses->getContext();
  611. return ReplaceableUses;
  612. }
  613. };
  614. struct TempMDNodeDeleter {
  615. inline void operator()(MDNode *Node) const;
  616. };
  617. #define HANDLE_MDNODE_LEAF(CLASS) \
  618. typedef std::unique_ptr<CLASS, TempMDNodeDeleter> Temp##CLASS;
  619. #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
  620. #include "llvm/IR/Metadata.def"
  621. /// \brief Metadata node.
  622. ///
  623. /// Metadata nodes can be uniqued, like constants, or distinct. Temporary
  624. /// metadata nodes (with full support for RAUW) can be used to delay uniquing
  625. /// until forward references are known. The basic metadata node is an \a
  626. /// MDTuple.
  627. ///
  628. /// There is limited support for RAUW at construction time. At construction
  629. /// time, if any operand is a temporary node (or an unresolved uniqued node,
  630. /// which indicates a transitive temporary operand), the node itself will be
  631. /// unresolved. As soon as all operands become resolved, it will drop RAUW
  632. /// support permanently.
  633. ///
  634. /// If an unresolved node is part of a cycle, \a resolveCycles() needs
  635. /// to be called on some member of the cycle once all temporary nodes have been
  636. /// replaced.
  637. class MDNode : public Metadata {
  638. friend class ReplaceableMetadataImpl;
  639. friend class LLVMContextImpl;
  640. MDNode(const MDNode &) = delete;
  641. void operator=(const MDNode &) = delete;
  642. void *operator new(size_t) = delete;
  643. unsigned NumOperands;
  644. unsigned NumUnresolved;
  645. protected:
  646. ContextAndReplaceableUses Context;
  647. void *operator new(size_t Size, unsigned NumOps);
  648. void operator delete(void *Mem);
  649. /// \brief Required by std, but never called.
  650. void operator delete(void *Mem, unsigned) {
  651. //llvm_unreachable("Constructor throws?"); // HLSL Change - why, yes; yes it does (under OOM)
  652. MDNode::operator delete(Mem);
  653. }
  654. /// \brief Required by std, but never called.
  655. void operator delete(void *, unsigned, bool) {
  656. llvm_unreachable("Constructor throws?");
  657. }
  658. MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
  659. ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None);
  660. ~MDNode() = default;
  661. void dropAllReferences();
  662. MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
  663. MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
  664. typedef iterator_range<MDOperand *> mutable_op_range;
  665. mutable_op_range mutable_operands() {
  666. return mutable_op_range(mutable_begin(), mutable_end());
  667. }
  668. public:
  669. static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
  670. static inline MDTuple *getIfExists(LLVMContext &Context,
  671. ArrayRef<Metadata *> MDs);
  672. static inline MDTuple *getDistinct(LLVMContext &Context,
  673. ArrayRef<Metadata *> MDs);
  674. static inline TempMDTuple getTemporary(LLVMContext &Context,
  675. ArrayRef<Metadata *> MDs);
  676. /// \brief Create a (temporary) clone of this.
  677. TempMDNode clone() const;
  678. /// \brief Deallocate a node created by getTemporary.
  679. ///
  680. /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
  681. /// references will be reset.
  682. static void deleteTemporary(MDNode *N);
  683. LLVMContext &getContext() const { return Context.getContext(); }
  684. /// \brief Replace a specific operand.
  685. void replaceOperandWith(unsigned I, Metadata *New);
  686. /// \brief Check if node is fully resolved.
  687. ///
  688. /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
  689. /// this always returns \c true.
  690. ///
  691. /// If \a isUniqued(), returns \c true if this has already dropped RAUW
  692. /// support (because all operands are resolved).
  693. ///
  694. /// As forward declarations are resolved, their containers should get
  695. /// resolved automatically. However, if this (or one of its operands) is
  696. /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
  697. bool isResolved() const { return !Context.hasReplaceableUses(); }
  698. bool isUniqued() const { return Storage == Uniqued; }
  699. bool isDistinct() const { return Storage == Distinct; }
  700. bool isTemporary() const { return Storage == Temporary; }
  701. /// \brief RAUW a temporary.
  702. ///
  703. /// \pre \a isTemporary() must be \c true.
  704. void replaceAllUsesWith(Metadata *MD) {
  705. assert(isTemporary() && "Expected temporary node");
  706. assert(!isResolved() && "Expected RAUW support");
  707. Context.getReplaceableUses()->replaceAllUsesWith(MD);
  708. }
  709. /// \brief Resolve cycles.
  710. ///
  711. /// Once all forward declarations have been resolved, force cycles to be
  712. /// resolved.
  713. ///
  714. /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
  715. void resolveCycles();
  716. /// \brief Replace a temporary node with a permanent one.
  717. ///
  718. /// Try to create a uniqued version of \c N -- in place, if possible -- and
  719. /// return it. If \c N cannot be uniqued, return a distinct node instead.
  720. template <class T>
  721. static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
  722. replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
  723. return cast<T>(N.release()->replaceWithPermanentImpl());
  724. }
  725. /// \brief Replace a temporary node with a uniqued one.
  726. ///
  727. /// Create a uniqued version of \c N -- in place, if possible -- and return
  728. /// it. Takes ownership of the temporary node.
  729. ///
  730. /// \pre N does not self-reference.
  731. template <class T>
  732. static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
  733. replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
  734. return cast<T>(N.release()->replaceWithUniquedImpl());
  735. }
  736. /// \brief Replace a temporary node with a distinct one.
  737. ///
  738. /// Create a distinct version of \c N -- in place, if possible -- and return
  739. /// it. Takes ownership of the temporary node.
  740. template <class T>
  741. static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
  742. replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
  743. return cast<T>(N.release()->replaceWithDistinctImpl());
  744. }
  745. private:
  746. MDNode *replaceWithPermanentImpl();
  747. MDNode *replaceWithUniquedImpl();
  748. MDNode *replaceWithDistinctImpl();
  749. protected:
  750. /// \brief Set an operand.
  751. ///
  752. /// Sets the operand directly, without worrying about uniquing.
  753. void setOperand(unsigned I, Metadata *New);
  754. void storeDistinctInContext();
  755. template <class T, class StoreT>
  756. static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
  757. private:
  758. void handleChangedOperand(void *Ref, Metadata *New);
  759. void resolve();
  760. void resolveAfterOperandChange(Metadata *Old, Metadata *New);
  761. void decrementUnresolvedOperandCount();
  762. unsigned countUnresolvedOperands();
  763. /// \brief Mutate this to be "uniqued".
  764. ///
  765. /// Mutate this so that \a isUniqued().
  766. /// \pre \a isTemporary().
  767. /// \pre already added to uniquing set.
  768. void makeUniqued();
  769. /// \brief Mutate this to be "distinct".
  770. ///
  771. /// Mutate this so that \a isDistinct().
  772. /// \pre \a isTemporary().
  773. void makeDistinct();
  774. public: // HLSL Change - make deleteAsSubclass accessible
  775. void deleteAsSubclass();
  776. private:
  777. MDNode *uniquify();
  778. void eraseFromStore();
  779. template <class NodeTy> struct HasCachedHash;
  780. template <class NodeTy>
  781. static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
  782. N->recalculateHash();
  783. }
  784. template <class NodeTy>
  785. static void dispatchRecalculateHash(NodeTy *N, std::false_type) {}
  786. template <class NodeTy>
  787. static void dispatchResetHash(NodeTy *N, std::true_type) {
  788. N->setHash(0);
  789. }
  790. template <class NodeTy>
  791. static void dispatchResetHash(NodeTy *N, std::false_type) {}
  792. public:
  793. typedef const MDOperand *op_iterator;
  794. typedef iterator_range<op_iterator> op_range;
  795. op_iterator op_begin() const {
  796. return const_cast<MDNode *>(this)->mutable_begin();
  797. }
  798. op_iterator op_end() const {
  799. return const_cast<MDNode *>(this)->mutable_end();
  800. }
  801. op_range operands() const { return op_range(op_begin(), op_end()); }
  802. const MDOperand &getOperand(unsigned I) const {
  803. assert(I < NumOperands && "Out of range");
  804. return op_begin()[I];
  805. }
  806. /// \brief Return number of MDNode operands.
  807. unsigned getNumOperands() const { return NumOperands; }
  808. /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
  809. static bool classof(const Metadata *MD) {
  810. switch (MD->getMetadataID()) {
  811. default:
  812. return false;
  813. #define HANDLE_MDNODE_LEAF(CLASS) \
  814. case CLASS##Kind: \
  815. return true;
  816. #include "llvm/IR/Metadata.def"
  817. }
  818. }
  819. /// \brief Check whether MDNode is a vtable access.
  820. bool isTBAAVtableAccess() const;
  821. /// \brief Methods for metadata merging.
  822. static MDNode *concatenate(MDNode *A, MDNode *B);
  823. static MDNode *intersect(MDNode *A, MDNode *B);
  824. static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
  825. static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
  826. static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
  827. static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
  828. };
  829. /// \brief Tuple of metadata.
  830. ///
  831. /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
  832. /// default based on their operands.
  833. class MDTuple : public MDNode {
  834. friend class LLVMContextImpl;
  835. friend class MDNode;
  836. MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
  837. ArrayRef<Metadata *> Vals)
  838. : MDNode(C, MDTupleKind, Storage, Vals) {
  839. setHash(Hash);
  840. }
  841. ~MDTuple() { dropAllReferences(); }
  842. void setHash(unsigned Hash) { SubclassData32 = Hash; }
  843. void recalculateHash();
  844. static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
  845. StorageType Storage, bool ShouldCreate = true);
  846. TempMDTuple cloneImpl() const {
  847. return getTemporary(getContext(),
  848. SmallVector<Metadata *, 4>(op_begin(), op_end()));
  849. }
  850. public:
  851. /// \brief Get the hash, if any.
  852. unsigned getHash() const { return SubclassData32; }
  853. static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  854. return getImpl(Context, MDs, Uniqued);
  855. }
  856. static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  857. return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
  858. }
  859. /// \brief Return a distinct node.
  860. ///
  861. /// Return a distinct node -- i.e., a node that is not uniqued.
  862. static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  863. return getImpl(Context, MDs, Distinct);
  864. }
  865. /// \brief Return a temporary node.
  866. ///
  867. /// For use in constructing cyclic MDNode structures. A temporary MDNode is
  868. /// not uniqued, may be RAUW'd, and must be manually deleted with
  869. /// deleteTemporary.
  870. static TempMDTuple getTemporary(LLVMContext &Context,
  871. ArrayRef<Metadata *> MDs) {
  872. return TempMDTuple(getImpl(Context, MDs, Temporary));
  873. }
  874. /// \brief Return a (temporary) clone of this.
  875. TempMDTuple clone() const { return cloneImpl(); }
  876. static bool classof(const Metadata *MD) {
  877. return MD->getMetadataID() == MDTupleKind;
  878. }
  879. };
  880. MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  881. return MDTuple::get(Context, MDs);
  882. }
  883. MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  884. return MDTuple::getIfExists(Context, MDs);
  885. }
  886. MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
  887. return MDTuple::getDistinct(Context, MDs);
  888. }
  889. TempMDTuple MDNode::getTemporary(LLVMContext &Context,
  890. ArrayRef<Metadata *> MDs) {
  891. return MDTuple::getTemporary(Context, MDs);
  892. }
  893. void TempMDNodeDeleter::operator()(MDNode *Node) const {
  894. MDNode::deleteTemporary(Node);
  895. }
  896. /// \brief Typed iterator through MDNode operands.
  897. ///
  898. /// An iterator that transforms an \a MDNode::iterator into an iterator over a
  899. /// particular Metadata subclass.
  900. template <class T>
  901. class TypedMDOperandIterator
  902. : std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void, T *> {
  903. MDNode::op_iterator I = nullptr;
  904. public:
  905. TypedMDOperandIterator() = default;
  906. explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
  907. T *operator*() const { return cast_or_null<T>(*I); }
  908. TypedMDOperandIterator &operator++() {
  909. ++I;
  910. return *this;
  911. }
  912. TypedMDOperandIterator operator++(int) {
  913. TypedMDOperandIterator Temp(*this);
  914. ++I;
  915. return Temp;
  916. }
  917. bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
  918. bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
  919. };
  920. /// \brief Typed, array-like tuple of metadata.
  921. ///
  922. /// This is a wrapper for \a MDTuple that makes it act like an array holding a
  923. /// particular type of metadata.
  924. template <class T> class MDTupleTypedArrayWrapper {
  925. const MDTuple *N = nullptr;
  926. public:
  927. MDTupleTypedArrayWrapper() = default;
  928. MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
  929. template <class U>
  930. MDTupleTypedArrayWrapper(
  931. const MDTupleTypedArrayWrapper<U> &Other,
  932. typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
  933. nullptr)
  934. : N(Other.get()) {}
  935. template <class U>
  936. explicit MDTupleTypedArrayWrapper(
  937. const MDTupleTypedArrayWrapper<U> &Other,
  938. typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
  939. nullptr)
  940. : N(Other.get()) {}
  941. explicit operator bool() const { return get(); }
  942. explicit operator MDTuple *() const { return get(); }
  943. MDTuple *get() const { return const_cast<MDTuple *>(N); }
  944. MDTuple *operator->() const { return get(); }
  945. MDTuple &operator*() const { return *get(); }
  946. // FIXME: Fix callers and remove condition on N.
  947. unsigned size() const { return N ? N->getNumOperands() : 0u; }
  948. T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
  949. // FIXME: Fix callers and remove condition on N.
  950. typedef TypedMDOperandIterator<T> iterator;
  951. iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
  952. iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
  953. };
  954. #define HANDLE_METADATA(CLASS) \
  955. typedef MDTupleTypedArrayWrapper<CLASS> CLASS##Array;
  956. #include "llvm/IR/Metadata.def"
  957. // //
  958. ///////////////////////////////////////////////////////////////////////////////
  959. /// \brief A tuple of MDNodes.
  960. ///
  961. /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
  962. /// to modules, have names, and contain lists of MDNodes.
  963. ///
  964. /// TODO: Inherit from Metadata.
  965. class NamedMDNode : public ilist_node<NamedMDNode> {
  966. friend class SymbolTableListTraits<NamedMDNode, Module>;
  967. friend struct ilist_traits<NamedMDNode>;
  968. friend class LLVMContextImpl;
  969. friend class Module;
  970. NamedMDNode(const NamedMDNode &) = delete;
  971. std::string Name;
  972. Module *Parent;
  973. void *Operands; // SmallVector<TrackingMDRef, 4>
  974. void setParent(Module *M) { Parent = M; }
  975. explicit NamedMDNode(const Twine &N);
  976. template<class T1, class T2>
  977. class op_iterator_impl :
  978. public std::iterator<std::bidirectional_iterator_tag, T2> {
  979. const NamedMDNode *Node;
  980. unsigned Idx;
  981. op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
  982. friend class NamedMDNode;
  983. public:
  984. op_iterator_impl() : Node(nullptr), Idx(0) { }
  985. bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
  986. bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
  987. op_iterator_impl &operator++() {
  988. ++Idx;
  989. return *this;
  990. }
  991. op_iterator_impl operator++(int) {
  992. op_iterator_impl tmp(*this);
  993. operator++();
  994. return tmp;
  995. }
  996. op_iterator_impl &operator--() {
  997. --Idx;
  998. return *this;
  999. }
  1000. op_iterator_impl operator--(int) {
  1001. op_iterator_impl tmp(*this);
  1002. operator--();
  1003. return tmp;
  1004. }
  1005. T1 operator*() const { return Node->getOperand(Idx); }
  1006. };
  1007. public:
  1008. /// \brief Drop all references and remove the node from parent module.
  1009. void eraseFromParent();
  1010. /// \brief Remove all uses and clear node vector.
  1011. void dropAllReferences();
  1012. ~NamedMDNode();
  1013. /// \brief Get the module that holds this named metadata collection.
  1014. inline Module *getParent() { return Parent; }
  1015. inline const Module *getParent() const { return Parent; }
  1016. MDNode *getOperand(unsigned i) const;
  1017. unsigned getNumOperands() const;
  1018. void addOperand(MDNode *M);
  1019. void setOperand(unsigned I, MDNode *New);
  1020. StringRef getName() const;
  1021. void print(raw_ostream &ROS) const;
  1022. void dump() const;
  1023. // ---------------------------------------------------------------------------
  1024. // Operand Iterator interface...
  1025. //
  1026. typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
  1027. op_iterator op_begin() { return op_iterator(this, 0); }
  1028. op_iterator op_end() { return op_iterator(this, getNumOperands()); }
  1029. typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
  1030. const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
  1031. const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
  1032. inline iterator_range<op_iterator> operands() {
  1033. return iterator_range<op_iterator>(op_begin(), op_end());
  1034. }
  1035. inline iterator_range<const_op_iterator> operands() const {
  1036. return iterator_range<const_op_iterator>(op_begin(), op_end());
  1037. }
  1038. };
  1039. } // end llvm namespace
  1040. #endif