LLVM API Documentation

Metadata.h
Go to the documentation of this file.
00001 //===-- llvm/Metadata.h - Metadata definitions ------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 /// @file
00011 /// This file contains the declarations for metadata subclasses.
00012 /// They represent the different flavors of metadata that live in LLVM.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_IR_METADATA_H
00017 #define LLVM_IR_METADATA_H
00018 
00019 #include "llvm/ADT/ArrayRef.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/ilist_node.h"
00022 #include "llvm/ADT/iterator_range.h"
00023 #include "llvm/IR/Constant.h"
00024 #include "llvm/IR/MetadataTracking.h"
00025 #include "llvm/IR/Value.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include <type_traits>
00028 
00029 namespace llvm {
00030 class LLVMContext;
00031 class Module;
00032 template<typename ValueSubClass, typename ItemParentClass>
00033   class SymbolTableListTraits;
00034 
00035 
00036 enum LLVMConstants : uint32_t {
00037   DEBUG_METADATA_VERSION = 2  // Current debug info version number.
00038 };
00039 
00040 /// \brief Root of the metadata hierarchy.
00041 ///
00042 /// This is a root class for typeless data in the IR.
00043 class Metadata {
00044   friend class ReplaceableMetadataImpl;
00045 
00046   /// \brief RTTI.
00047   const unsigned char SubclassID;
00048 
00049 protected:
00050   /// \brief Active type of storage.
00051   enum StorageType { Uniqued, Distinct, Temporary };
00052 
00053   /// \brief Storage flag for non-uniqued, otherwise unowned, metadata.
00054   unsigned Storage : 2;
00055   // TODO: expose remaining bits to subclasses.
00056 
00057   unsigned short SubclassData16;
00058   unsigned SubclassData32;
00059 
00060 public:
00061   enum MetadataKind {
00062     MDTupleKind,
00063     MDLocationKind,
00064     GenericDebugNodeKind,
00065     ConstantAsMetadataKind,
00066     LocalAsMetadataKind,
00067     MDStringKind
00068   };
00069 
00070 protected:
00071   Metadata(unsigned ID, StorageType Storage)
00072       : SubclassID(ID), Storage(Storage), SubclassData16(0), SubclassData32(0) {
00073   }
00074   ~Metadata() {}
00075 
00076   /// \brief Default handling of a changed operand, which asserts.
00077   ///
00078   /// If subclasses pass themselves in as owners to a tracking node reference,
00079   /// they must provide an implementation of this method.
00080   void handleChangedOperand(void *, Metadata *) {
00081     llvm_unreachable("Unimplemented in Metadata subclass");
00082   }
00083 
00084 public:
00085   unsigned getMetadataID() const { return SubclassID; }
00086 
00087   /// \brief User-friendly dump.
00088   void dump() const;
00089   void print(raw_ostream &OS) const;
00090   void printAsOperand(raw_ostream &OS, bool PrintType = true,
00091                       const Module *M = nullptr) const;
00092 };
00093 
00094 #define HANDLE_METADATA(CLASS) class CLASS;
00095 #include "llvm/IR/Metadata.def"
00096 
00097 inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
00098   MD.print(OS);
00099   return OS;
00100 }
00101 
00102 /// \brief Metadata wrapper in the Value hierarchy.
00103 ///
00104 /// A member of the \a Value hierarchy to represent a reference to metadata.
00105 /// This allows, e.g., instrinsics to have metadata as operands.
00106 ///
00107 /// Notably, this is the only thing in either hierarchy that is allowed to
00108 /// reference \a LocalAsMetadata.
00109 class MetadataAsValue : public Value {
00110   friend class ReplaceableMetadataImpl;
00111   friend class LLVMContextImpl;
00112 
00113   Metadata *MD;
00114 
00115   MetadataAsValue(Type *Ty, Metadata *MD);
00116   ~MetadataAsValue();
00117 
00118   /// \brief Drop use of metadata (during teardown).
00119   void dropUse() { MD = nullptr; }
00120 
00121 public:
00122   static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
00123   static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
00124   Metadata *getMetadata() const { return MD; }
00125 
00126   static bool classof(const Value *V) {
00127     return V->getValueID() == MetadataAsValueVal;
00128   }
00129 
00130 private:
00131   void handleChangedMetadata(Metadata *MD);
00132   void track();
00133   void untrack();
00134 };
00135 
00136 /// \brief Shared implementation of use-lists for replaceable metadata.
00137 ///
00138 /// Most metadata cannot be RAUW'ed.  This is a shared implementation of
00139 /// use-lists and associated API for the two that support it (\a ValueAsMetadata
00140 /// and \a TempMDNode).
00141 class ReplaceableMetadataImpl {
00142   friend class MetadataTracking;
00143 
00144 public:
00145   typedef MetadataTracking::OwnerTy OwnerTy;
00146 
00147 private:
00148   LLVMContext &Context;
00149   uint64_t NextIndex;
00150   SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap;
00151 
00152 public:
00153   ReplaceableMetadataImpl(LLVMContext &Context)
00154       : Context(Context), NextIndex(0) {}
00155   ~ReplaceableMetadataImpl() {
00156     assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
00157   }
00158 
00159   LLVMContext &getContext() const { return Context; }
00160 
00161   /// \brief Replace all uses of this with MD.
00162   ///
00163   /// Replace all uses of this with \c MD, which is allowed to be null.
00164   void replaceAllUsesWith(Metadata *MD);
00165 
00166   /// \brief Resolve all uses of this.
00167   ///
00168   /// Resolve all uses of this, turning off RAUW permanently.  If \c
00169   /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
00170   /// is resolved.
00171   void resolveAllUses(bool ResolveUsers = true);
00172 
00173 private:
00174   void addRef(void *Ref, OwnerTy Owner);
00175   void dropRef(void *Ref);
00176   void moveRef(void *Ref, void *New, const Metadata &MD);
00177 
00178   static ReplaceableMetadataImpl *get(Metadata &MD);
00179 };
00180 
00181 /// \brief Value wrapper in the Metadata hierarchy.
00182 ///
00183 /// This is a custom value handle that allows other metadata to refer to
00184 /// classes in the Value hierarchy.
00185 ///
00186 /// Because of full uniquing support, each value is only wrapped by a single \a
00187 /// ValueAsMetadata object, so the lookup maps are far more efficient than
00188 /// those using ValueHandleBase.
00189 class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl {
00190   friend class ReplaceableMetadataImpl;
00191   friend class LLVMContextImpl;
00192 
00193   Value *V;
00194 
00195   /// \brief Drop users without RAUW (during teardown).
00196   void dropUsers() {
00197     ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
00198   }
00199 
00200 protected:
00201   ValueAsMetadata(unsigned ID, Value *V)
00202       : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
00203     assert(V && "Expected valid value");
00204   }
00205   ~ValueAsMetadata() {}
00206 
00207 public:
00208   static ValueAsMetadata *get(Value *V);
00209   static ConstantAsMetadata *getConstant(Value *C) {
00210     return cast<ConstantAsMetadata>(get(C));
00211   }
00212   static LocalAsMetadata *getLocal(Value *Local) {
00213     return cast<LocalAsMetadata>(get(Local));
00214   }
00215 
00216   static ValueAsMetadata *getIfExists(Value *V);
00217   static ConstantAsMetadata *getConstantIfExists(Value *C) {
00218     return cast_or_null<ConstantAsMetadata>(getIfExists(C));
00219   }
00220   static LocalAsMetadata *getLocalIfExists(Value *Local) {
00221     return cast_or_null<LocalAsMetadata>(getIfExists(Local));
00222   }
00223 
00224   Value *getValue() const { return V; }
00225   Type *getType() const { return V->getType(); }
00226   LLVMContext &getContext() const { return V->getContext(); }
00227 
00228   static void handleDeletion(Value *V);
00229   static void handleRAUW(Value *From, Value *To);
00230 
00231 protected:
00232   /// \brief Handle collisions after \a Value::replaceAllUsesWith().
00233   ///
00234   /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
00235   /// \a Value gets RAUW'ed and the target already exists, this is used to
00236   /// merge the two metadata nodes.
00237   void replaceAllUsesWith(Metadata *MD) {
00238     ReplaceableMetadataImpl::replaceAllUsesWith(MD);
00239   }
00240 
00241 public:
00242   static bool classof(const Metadata *MD) {
00243     return MD->getMetadataID() == LocalAsMetadataKind ||
00244            MD->getMetadataID() == ConstantAsMetadataKind;
00245   }
00246 };
00247 
00248 class ConstantAsMetadata : public ValueAsMetadata {
00249   friend class ValueAsMetadata;
00250 
00251   ConstantAsMetadata(Constant *C)
00252       : ValueAsMetadata(ConstantAsMetadataKind, C) {}
00253 
00254 public:
00255   static ConstantAsMetadata *get(Constant *C) {
00256     return ValueAsMetadata::getConstant(C);
00257   }
00258   static ConstantAsMetadata *getIfExists(Constant *C) {
00259     return ValueAsMetadata::getConstantIfExists(C);
00260   }
00261 
00262   Constant *getValue() const {
00263     return cast<Constant>(ValueAsMetadata::getValue());
00264   }
00265 
00266   static bool classof(const Metadata *MD) {
00267     return MD->getMetadataID() == ConstantAsMetadataKind;
00268   }
00269 };
00270 
00271 class LocalAsMetadata : public ValueAsMetadata {
00272   friend class ValueAsMetadata;
00273 
00274   LocalAsMetadata(Value *Local)
00275       : ValueAsMetadata(LocalAsMetadataKind, Local) {
00276     assert(!isa<Constant>(Local) && "Expected local value");
00277   }
00278 
00279 public:
00280   static LocalAsMetadata *get(Value *Local) {
00281     return ValueAsMetadata::getLocal(Local);
00282   }
00283   static LocalAsMetadata *getIfExists(Value *Local) {
00284     return ValueAsMetadata::getLocalIfExists(Local);
00285   }
00286 
00287   static bool classof(const Metadata *MD) {
00288     return MD->getMetadataID() == LocalAsMetadataKind;
00289   }
00290 };
00291 
00292 /// \brief Transitional API for extracting constants from Metadata.
00293 ///
00294 /// This namespace contains transitional functions for metadata that points to
00295 /// \a Constants.
00296 ///
00297 /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
00298 /// operands could refer to any \a Value.  There's was a lot of code like this:
00299 ///
00300 /// \code
00301 ///     MDNode *N = ...;
00302 ///     auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
00303 /// \endcode
00304 ///
00305 /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
00306 /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
00307 /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
00308 /// cast in the \a Value hierarchy.  Besides creating boiler-plate, this
00309 /// requires subtle control flow changes.
00310 ///
00311 /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
00312 /// so that metadata can refer to numbers without traversing a bridge to the \a
00313 /// Value hierarchy.  In this final state, the code above would look like this:
00314 ///
00315 /// \code
00316 ///     MDNode *N = ...;
00317 ///     auto *MI = dyn_cast<MDInt>(N->getOperand(2));
00318 /// \endcode
00319 ///
00320 /// The API in this namespace supports the transition.  \a MDInt doesn't exist
00321 /// yet, and even once it does, changing each metadata schema to use it is its
00322 /// own mini-project.  In the meantime this API prevents us from introducing
00323 /// complex and bug-prone control flow that will disappear in the end.  In
00324 /// particular, the above code looks like this:
00325 ///
00326 /// \code
00327 ///     MDNode *N = ...;
00328 ///     auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
00329 /// \endcode
00330 ///
00331 /// The full set of provided functions includes:
00332 ///
00333 ///   mdconst::hasa                <=> isa
00334 ///   mdconst::extract             <=> cast
00335 ///   mdconst::extract_or_null     <=> cast_or_null
00336 ///   mdconst::dyn_extract         <=> dyn_cast
00337 ///   mdconst::dyn_extract_or_null <=> dyn_cast_or_null
00338 ///
00339 /// The target of the cast must be a subclass of \a Constant.
00340 namespace mdconst {
00341 
00342 namespace detail {
00343 template <class T> T &make();
00344 template <class T, class Result> struct HasDereference {
00345   typedef char Yes[1];
00346   typedef char No[2];
00347   template <size_t N> struct SFINAE {};
00348 
00349   template <class U, class V>
00350   static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
00351   template <class U, class V> static No &hasDereference(...);
00352 
00353   static const bool value =
00354       sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
00355 };
00356 template <class V, class M> struct IsValidPointer {
00357   static const bool value = std::is_base_of<Constant, V>::value &&
00358                             HasDereference<M, const Metadata &>::value;
00359 };
00360 template <class V, class M> struct IsValidReference {
00361   static const bool value = std::is_base_of<Constant, V>::value &&
00362                             std::is_convertible<M, const Metadata &>::value;
00363 };
00364 } // end namespace detail
00365 
00366 /// \brief Check whether Metadata has a Value.
00367 ///
00368 /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
00369 /// type \c X.
00370 template <class X, class Y>
00371 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, bool>::type
00372 hasa(Y &&MD) {
00373   assert(MD && "Null pointer sent into hasa");
00374   if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
00375     return isa<X>(V->getValue());
00376   return false;
00377 }
00378 template <class X, class Y>
00379 inline
00380     typename std::enable_if<detail::IsValidReference<X, Y &>::value, bool>::type
00381     hasa(Y &MD) {
00382   return hasa(&MD);
00383 }
00384 
00385 /// \brief Extract a Value from Metadata.
00386 ///
00387 /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
00388 template <class X, class Y>
00389 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
00390 extract(Y &&MD) {
00391   return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
00392 }
00393 template <class X, class Y>
00394 inline
00395     typename std::enable_if<detail::IsValidReference<X, Y &>::value, X *>::type
00396     extract(Y &MD) {
00397   return extract(&MD);
00398 }
00399 
00400 /// \brief Extract a Value from Metadata, allowing null.
00401 ///
00402 /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
00403 /// from \c MD, allowing \c MD to be null.
00404 template <class X, class Y>
00405 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
00406 extract_or_null(Y &&MD) {
00407   if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
00408     return cast<X>(V->getValue());
00409   return nullptr;
00410 }
00411 
00412 /// \brief Extract a Value from Metadata, if any.
00413 ///
00414 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
00415 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
00416 /// Value it does contain is of the wrong subclass.
00417 template <class X, class Y>
00418 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
00419 dyn_extract(Y &&MD) {
00420   if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
00421     return dyn_cast<X>(V->getValue());
00422   return nullptr;
00423 }
00424 
00425 /// \brief Extract a Value from Metadata, if any, allowing null.
00426 ///
00427 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
00428 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
00429 /// Value it does contain is of the wrong subclass, allowing \c MD to be null.
00430 template <class X, class Y>
00431 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
00432 dyn_extract_or_null(Y &&MD) {
00433   if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
00434     return dyn_cast<X>(V->getValue());
00435   return nullptr;
00436 }
00437 
00438 } // end namespace mdconst
00439 
00440 //===----------------------------------------------------------------------===//
00441 /// \brief A single uniqued string.
00442 ///
00443 /// These are used to efficiently contain a byte sequence for metadata.
00444 /// MDString is always unnamed.
00445 class MDString : public Metadata {
00446   friend class StringMapEntry<MDString>;
00447 
00448   MDString(const MDString &) LLVM_DELETED_FUNCTION;
00449   MDString &operator=(MDString &&) LLVM_DELETED_FUNCTION;
00450   MDString &operator=(const MDString &) LLVM_DELETED_FUNCTION;
00451 
00452   StringMapEntry<MDString> *Entry;
00453   MDString() : Metadata(MDStringKind, Uniqued), Entry(nullptr) {}
00454   MDString(MDString &&) : Metadata(MDStringKind, Uniqued) {}
00455 
00456 public:
00457   static MDString *get(LLVMContext &Context, StringRef Str);
00458   static MDString *get(LLVMContext &Context, const char *Str) {
00459     return get(Context, Str ? StringRef(Str) : StringRef());
00460   }
00461 
00462   StringRef getString() const;
00463 
00464   unsigned getLength() const { return (unsigned)getString().size(); }
00465 
00466   typedef StringRef::iterator iterator;
00467 
00468   /// \brief Pointer to the first byte of the string.
00469   iterator begin() const { return getString().begin(); }
00470 
00471   /// \brief Pointer to one byte past the end of the string.
00472   iterator end() const { return getString().end(); }
00473 
00474   const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
00475   const unsigned char *bytes_end() const { return getString().bytes_end(); }
00476 
00477   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast.
00478   static bool classof(const Metadata *MD) {
00479     return MD->getMetadataID() == MDStringKind;
00480   }
00481 };
00482 
00483 /// \brief A collection of metadata nodes that might be associated with a
00484 /// memory access used by the alias-analysis infrastructure.
00485 struct AAMDNodes {
00486   explicit AAMDNodes(MDNode *T = nullptr, MDNode *S = nullptr,
00487                      MDNode *N = nullptr)
00488       : TBAA(T), Scope(S), NoAlias(N) {}
00489 
00490   bool operator==(const AAMDNodes &A) const {
00491     return TBAA == A.TBAA && Scope == A.Scope && NoAlias == A.NoAlias;
00492   }
00493 
00494   bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
00495 
00496   LLVM_EXPLICIT operator bool() const { return TBAA || Scope || NoAlias; }
00497 
00498   /// \brief The tag for type-based alias analysis.
00499   MDNode *TBAA;
00500 
00501   /// \brief The tag for alias scope specification (used with noalias).
00502   MDNode *Scope;
00503 
00504   /// \brief The tag specifying the noalias scope.
00505   MDNode *NoAlias;
00506 };
00507 
00508 // Specialize DenseMapInfo for AAMDNodes.
00509 template<>
00510 struct DenseMapInfo<AAMDNodes> {
00511   static inline AAMDNodes getEmptyKey() {
00512     return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), 0, 0);
00513   }
00514   static inline AAMDNodes getTombstoneKey() {
00515     return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), 0, 0);
00516   }
00517   static unsigned getHashValue(const AAMDNodes &Val) {
00518     return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
00519            DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
00520            DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
00521   }
00522   static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
00523     return LHS == RHS;
00524   }
00525 };
00526 
00527 /// \brief Tracking metadata reference owned by Metadata.
00528 ///
00529 /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
00530 /// of \a Metadata, which has the option of registering itself for callbacks to
00531 /// re-unique itself.
00532 ///
00533 /// In particular, this is used by \a MDNode.
00534 class MDOperand {
00535   MDOperand(MDOperand &&) LLVM_DELETED_FUNCTION;
00536   MDOperand(const MDOperand &) LLVM_DELETED_FUNCTION;
00537   MDOperand &operator=(MDOperand &&) LLVM_DELETED_FUNCTION;
00538   MDOperand &operator=(const MDOperand &) LLVM_DELETED_FUNCTION;
00539 
00540   Metadata *MD;
00541 
00542 public:
00543   MDOperand() : MD(nullptr) {}
00544   ~MDOperand() { untrack(); }
00545 
00546   Metadata *get() const { return MD; }
00547   operator Metadata *() const { return get(); }
00548   Metadata *operator->() const { return get(); }
00549   Metadata &operator*() const { return *get(); }
00550 
00551   void reset() {
00552     untrack();
00553     MD = nullptr;
00554   }
00555   void reset(Metadata *MD, Metadata *Owner) {
00556     untrack();
00557     this->MD = MD;
00558     track(Owner);
00559   }
00560 
00561 private:
00562   void track(Metadata *Owner) {
00563     if (MD) {
00564       if (Owner)
00565         MetadataTracking::track(this, *MD, *Owner);
00566       else
00567         MetadataTracking::track(MD);
00568     }
00569   }
00570   void untrack() {
00571     assert(static_cast<void *>(this) == &MD && "Expected same address");
00572     if (MD)
00573       MetadataTracking::untrack(MD);
00574   }
00575 };
00576 
00577 template <> struct simplify_type<MDOperand> {
00578   typedef Metadata *SimpleType;
00579   static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
00580 };
00581 
00582 template <> struct simplify_type<const MDOperand> {
00583   typedef Metadata *SimpleType;
00584   static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
00585 };
00586 
00587 /// \brief Pointer to the context, with optional RAUW support.
00588 ///
00589 /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
00590 /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
00591 class ContextAndReplaceableUses {
00592   PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr;
00593 
00594   ContextAndReplaceableUses() LLVM_DELETED_FUNCTION;
00595   ContextAndReplaceableUses(ContextAndReplaceableUses &&)
00596       LLVM_DELETED_FUNCTION;
00597   ContextAndReplaceableUses(const ContextAndReplaceableUses &)
00598       LLVM_DELETED_FUNCTION;
00599   ContextAndReplaceableUses &
00600   operator=(ContextAndReplaceableUses &&) LLVM_DELETED_FUNCTION;
00601   ContextAndReplaceableUses &
00602   operator=(const ContextAndReplaceableUses &) LLVM_DELETED_FUNCTION;
00603 
00604 public:
00605   ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
00606   ContextAndReplaceableUses(
00607       std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
00608       : Ptr(ReplaceableUses.release()) {
00609     assert(getReplaceableUses() && "Expected non-null replaceable uses");
00610   }
00611   ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
00612 
00613   operator LLVMContext &() { return getContext(); }
00614 
00615   /// \brief Whether this contains RAUW support.
00616   bool hasReplaceableUses() const {
00617     return Ptr.is<ReplaceableMetadataImpl *>();
00618   }
00619   LLVMContext &getContext() const {
00620     if (hasReplaceableUses())
00621       return getReplaceableUses()->getContext();
00622     return *Ptr.get<LLVMContext *>();
00623   }
00624   ReplaceableMetadataImpl *getReplaceableUses() const {
00625     if (hasReplaceableUses())
00626       return Ptr.get<ReplaceableMetadataImpl *>();
00627     return nullptr;
00628   }
00629 
00630   /// \brief Assign RAUW support to this.
00631   ///
00632   /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
00633   /// not be null).
00634   void
00635   makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
00636     assert(ReplaceableUses && "Expected non-null replaceable uses");
00637     assert(&ReplaceableUses->getContext() == &getContext() &&
00638            "Expected same context");
00639     delete getReplaceableUses();
00640     Ptr = ReplaceableUses.release();
00641   }
00642 
00643   /// \brief Drop RAUW support.
00644   ///
00645   /// Cede ownership of RAUW support, returning it.
00646   std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
00647     assert(hasReplaceableUses() && "Expected to own replaceable uses");
00648     std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
00649         getReplaceableUses());
00650     Ptr = &ReplaceableUses->getContext();
00651     return ReplaceableUses;
00652   }
00653 };
00654 
00655 struct TempMDNodeDeleter {
00656   inline void operator()(MDNode *Node) const;
00657 };
00658 
00659 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
00660   typedef std::unique_ptr<CLASS, TempMDNodeDeleter> Temp##CLASS;
00661 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
00662 #include "llvm/IR/Metadata.def"
00663 
00664 /// \brief Metadata node.
00665 ///
00666 /// Metadata nodes can be uniqued, like constants, or distinct.  Temporary
00667 /// metadata nodes (with full support for RAUW) can be used to delay uniquing
00668 /// until forward references are known.  The basic metadata node is an \a
00669 /// MDTuple.
00670 ///
00671 /// There is limited support for RAUW at construction time.  At construction
00672 /// time, if any operand is a temporary node (or an unresolved uniqued node,
00673 /// which indicates a transitive temporary operand), the node itself will be
00674 /// unresolved.  As soon as all operands become resolved, it will drop RAUW
00675 /// support permanently.
00676 ///
00677 /// If an unresolved node is part of a cycle, \a resolveCycles() needs
00678 /// to be called on some member of the cycle once all temporary nodes have been
00679 /// replaced.
00680 class MDNode : public Metadata {
00681   friend class ReplaceableMetadataImpl;
00682   friend class LLVMContextImpl;
00683 
00684   MDNode(const MDNode &) LLVM_DELETED_FUNCTION;
00685   void operator=(const MDNode &) LLVM_DELETED_FUNCTION;
00686   void *operator new(size_t) LLVM_DELETED_FUNCTION;
00687 
00688   unsigned NumOperands;
00689   unsigned NumUnresolved;
00690 
00691 protected:
00692   ContextAndReplaceableUses Context;
00693 
00694   void *operator new(size_t Size, unsigned NumOps);
00695   void operator delete(void *Mem);
00696 
00697   /// \brief Required by std, but never called.
00698   void operator delete(void *, unsigned) {
00699     llvm_unreachable("Constructor throws?");
00700   }
00701 
00702   /// \brief Required by std, but never called.
00703   void operator delete(void *, unsigned, bool) {
00704     llvm_unreachable("Constructor throws?");
00705   }
00706 
00707   MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
00708          ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None);
00709   ~MDNode() {}
00710 
00711   void dropAllReferences();
00712 
00713   MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
00714   MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
00715 
00716 public:
00717   static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
00718   static inline MDTuple *getIfExists(LLVMContext &Context,
00719                                      ArrayRef<Metadata *> MDs);
00720   static inline MDTuple *getDistinct(LLVMContext &Context,
00721                                      ArrayRef<Metadata *> MDs);
00722   static inline TempMDTuple getTemporary(LLVMContext &Context,
00723                                          ArrayRef<Metadata *> MDs);
00724 
00725   /// \brief Create a (temporary) clone of this.
00726   TempMDNode clone() const;
00727 
00728   /// \brief Deallocate a node created by getTemporary.
00729   ///
00730   /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
00731   /// references will be reset.
00732   static void deleteTemporary(MDNode *N);
00733 
00734   LLVMContext &getContext() const { return Context.getContext(); }
00735 
00736   /// \brief Replace a specific operand.
00737   void replaceOperandWith(unsigned I, Metadata *New);
00738 
00739   /// \brief Check if node is fully resolved.
00740   ///
00741   /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
00742   /// this always returns \c true.
00743   ///
00744   /// If \a isUniqued(), returns \c true if this has already dropped RAUW
00745   /// support (because all operands are resolved).
00746   ///
00747   /// As forward declarations are resolved, their containers should get
00748   /// resolved automatically.  However, if this (or one of its operands) is
00749   /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
00750   bool isResolved() const { return !Context.hasReplaceableUses(); }
00751 
00752   bool isUniqued() const { return Storage == Uniqued; }
00753   bool isDistinct() const { return Storage == Distinct; }
00754   bool isTemporary() const { return Storage == Temporary; }
00755 
00756   /// \brief RAUW a temporary.
00757   ///
00758   /// \pre \a isTemporary() must be \c true.
00759   void replaceAllUsesWith(Metadata *MD) {
00760     assert(isTemporary() && "Expected temporary node");
00761     assert(!isResolved() && "Expected RAUW support");
00762     Context.getReplaceableUses()->replaceAllUsesWith(MD);
00763   }
00764 
00765   /// \brief Resolve cycles.
00766   ///
00767   /// Once all forward declarations have been resolved, force cycles to be
00768   /// resolved.
00769   ///
00770   /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
00771   void resolveCycles();
00772 
00773   /// \brief Replace a temporary node with a uniqued one.
00774   ///
00775   /// Create a uniqued version of \c N -- in place, if possible -- and return
00776   /// it.  Takes ownership of the temporary node.
00777   template <class T>
00778   static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
00779   replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
00780     return cast<T>(N.release()->replaceWithUniquedImpl());
00781   }
00782 
00783   /// \brief Replace a temporary node with a distinct one.
00784   ///
00785   /// Create a distinct version of \c N -- in place, if possible -- and return
00786   /// it.  Takes ownership of the temporary node.
00787   template <class T>
00788   static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
00789   replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
00790     return cast<T>(N.release()->replaceWithDistinctImpl());
00791   }
00792 
00793 private:
00794   MDNode *replaceWithUniquedImpl();
00795   MDNode *replaceWithDistinctImpl();
00796 
00797 protected:
00798   /// \brief Set an operand.
00799   ///
00800   /// Sets the operand directly, without worrying about uniquing.
00801   void setOperand(unsigned I, Metadata *New);
00802 
00803   void storeDistinctInContext();
00804   template <class T, class StoreT>
00805   static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
00806 
00807 private:
00808   void handleChangedOperand(void *Ref, Metadata *New);
00809 
00810   void resolve();
00811   void resolveAfterOperandChange(Metadata *Old, Metadata *New);
00812   void decrementUnresolvedOperandCount();
00813   unsigned countUnresolvedOperands();
00814 
00815   /// \brief Mutate this to be "uniqued".
00816   ///
00817   /// Mutate this so that \a isUniqued().
00818   /// \pre \a isTemporary().
00819   /// \pre already added to uniquing set.
00820   void makeUniqued();
00821 
00822   /// \brief Mutate this to be "distinct".
00823   ///
00824   /// Mutate this so that \a isDistinct().
00825   /// \pre \a isTemporary().
00826   void makeDistinct();
00827 
00828   void deleteAsSubclass();
00829   MDNode *uniquify();
00830   void eraseFromStore();
00831 
00832   template <class NodeTy> struct HasCachedHash;
00833   template <class NodeTy>
00834   static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
00835     N->recalculateHash();
00836   }
00837   template <class NodeTy>
00838   static void dispatchRecalculateHash(NodeTy *N, std::false_type) {}
00839   template <class NodeTy>
00840   static void dispatchResetHash(NodeTy *N, std::true_type) {
00841     N->setHash(0);
00842   }
00843   template <class NodeTy>
00844   static void dispatchResetHash(NodeTy *N, std::false_type) {}
00845 
00846 public:
00847   typedef const MDOperand *op_iterator;
00848   typedef iterator_range<op_iterator> op_range;
00849 
00850   op_iterator op_begin() const {
00851     return const_cast<MDNode *>(this)->mutable_begin();
00852   }
00853   op_iterator op_end() const {
00854     return const_cast<MDNode *>(this)->mutable_end();
00855   }
00856   op_range operands() const { return op_range(op_begin(), op_end()); }
00857 
00858   const MDOperand &getOperand(unsigned I) const {
00859     assert(I < NumOperands && "Out of range");
00860     return op_begin()[I];
00861   }
00862 
00863   /// \brief Return number of MDNode operands.
00864   unsigned getNumOperands() const { return NumOperands; }
00865 
00866   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
00867   static bool classof(const Metadata *MD) {
00868     return MD->getMetadataID() == MDTupleKind ||
00869            MD->getMetadataID() == MDLocationKind ||
00870            MD->getMetadataID() == GenericDebugNodeKind;
00871   }
00872 
00873   /// \brief Check whether MDNode is a vtable access.
00874   bool isTBAAVtableAccess() const;
00875 
00876   /// \brief Methods for metadata merging.
00877   static MDNode *concatenate(MDNode *A, MDNode *B);
00878   static MDNode *intersect(MDNode *A, MDNode *B);
00879   static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
00880   static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
00881   static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
00882 };
00883 
00884 /// \brief Tuple of metadata.
00885 ///
00886 /// This is the simple \a MDNode arbitrary tuple.  Nodes are uniqued by
00887 /// default based on their operands.
00888 class MDTuple : public MDNode {
00889   friend class LLVMContextImpl;
00890   friend class MDNode;
00891 
00892   MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
00893           ArrayRef<Metadata *> Vals)
00894       : MDNode(C, MDTupleKind, Storage, Vals) {
00895     setHash(Hash);
00896   }
00897   ~MDTuple() { dropAllReferences(); }
00898 
00899   void setHash(unsigned Hash) { SubclassData32 = Hash; }
00900   void recalculateHash();
00901 
00902   static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
00903                           StorageType Storage, bool ShouldCreate = true);
00904 
00905   TempMDTuple cloneImpl() const {
00906     return getTemporary(getContext(),
00907                         SmallVector<Metadata *, 4>(op_begin(), op_end()));
00908   }
00909 
00910 public:
00911   /// \brief Get the hash, if any.
00912   unsigned getHash() const { return SubclassData32; }
00913 
00914   static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00915     return getImpl(Context, MDs, Uniqued);
00916   }
00917   static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00918     return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
00919   }
00920 
00921   /// \brief Return a distinct node.
00922   ///
00923   /// Return a distinct node -- i.e., a node that is not uniqued.
00924   static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00925     return getImpl(Context, MDs, Distinct);
00926   }
00927 
00928   /// \brief Return a temporary node.
00929   ///
00930   /// For use in constructing cyclic MDNode structures. A temporary MDNode is
00931   /// not uniqued, may be RAUW'd, and must be manually deleted with
00932   /// deleteTemporary.
00933   static TempMDTuple getTemporary(LLVMContext &Context,
00934                                   ArrayRef<Metadata *> MDs) {
00935     return TempMDTuple(getImpl(Context, MDs, Temporary));
00936   }
00937 
00938   /// \brief Return a (temporary) clone of this.
00939   TempMDTuple clone() const { return cloneImpl(); }
00940 
00941   static bool classof(const Metadata *MD) {
00942     return MD->getMetadataID() == MDTupleKind;
00943   }
00944 };
00945 
00946 MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00947   return MDTuple::get(Context, MDs);
00948 }
00949 MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00950   return MDTuple::getIfExists(Context, MDs);
00951 }
00952 MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
00953   return MDTuple::getDistinct(Context, MDs);
00954 }
00955 TempMDTuple MDNode::getTemporary(LLVMContext &Context,
00956                                  ArrayRef<Metadata *> MDs) {
00957   return MDTuple::getTemporary(Context, MDs);
00958 }
00959 
00960 void TempMDNodeDeleter::operator()(MDNode *Node) const {
00961   MDNode::deleteTemporary(Node);
00962 }
00963 
00964 /// \brief Debug location.
00965 ///
00966 /// A debug location in source code, used for debug info and otherwise.
00967 class MDLocation : public MDNode {
00968   friend class LLVMContextImpl;
00969   friend class MDNode;
00970 
00971   MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
00972              unsigned Column, ArrayRef<Metadata *> MDs);
00973   ~MDLocation() { dropAllReferences(); }
00974 
00975   static MDLocation *getImpl(LLVMContext &Context, unsigned Line,
00976                              unsigned Column, Metadata *Scope,
00977                              Metadata *InlinedAt, StorageType Storage,
00978                              bool ShouldCreate = true);
00979 
00980   TempMDLocation cloneImpl() const {
00981     return getTemporary(getContext(), getLine(), getColumn(), getScope(),
00982                         getInlinedAt());
00983   }
00984 
00985   // Disallow replacing operands.
00986   void replaceOperandWith(unsigned I, Metadata *New) LLVM_DELETED_FUNCTION;
00987 
00988 public:
00989   static MDLocation *get(LLVMContext &Context, unsigned Line, unsigned Column,
00990                          Metadata *Scope, Metadata *InlinedAt = nullptr) {
00991     return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued);
00992   }
00993   static MDLocation *getIfExists(LLVMContext &Context, unsigned Line,
00994                                  unsigned Column, Metadata *Scope,
00995                                  Metadata *InlinedAt = nullptr) {
00996     return getImpl(Context, Line, Column, Scope, InlinedAt, Uniqued,
00997                    /* ShouldCreate */ false);
00998   }
00999   static MDLocation *getDistinct(LLVMContext &Context, unsigned Line,
01000                                  unsigned Column, Metadata *Scope,
01001                                  Metadata *InlinedAt = nullptr) {
01002     return getImpl(Context, Line, Column, Scope, InlinedAt, Distinct);
01003   }
01004   static TempMDLocation getTemporary(LLVMContext &Context, unsigned Line,
01005                                      unsigned Column, Metadata *Scope,
01006                                      Metadata *InlinedAt = nullptr) {
01007     return TempMDLocation(
01008         getImpl(Context, Line, Column, Scope, InlinedAt, Temporary));
01009   }
01010 
01011   /// \brief Return a (temporary) clone of this.
01012   TempMDLocation clone() const { return cloneImpl(); }
01013 
01014   unsigned getLine() const { return SubclassData32; }
01015   unsigned getColumn() const { return SubclassData16; }
01016   Metadata *getScope() const { return getOperand(0); }
01017   Metadata *getInlinedAt() const {
01018     if (getNumOperands() == 2)
01019       return getOperand(1);
01020     return nullptr;
01021   }
01022 
01023   static bool classof(const Metadata *MD) {
01024     return MD->getMetadataID() == MDLocationKind;
01025   }
01026 };
01027 
01028 /// \brief Tagged DWARF-like metadata node.
01029 ///
01030 /// A metadata node with a DWARF tag (i.e., a constant named \c DW_TAG_*,
01031 /// defined in llvm/Support/Dwarf.h).  Called \a DebugNode because it's
01032 /// potentially used for non-DWARF output.
01033 class DebugNode : public MDNode {
01034   friend class LLVMContextImpl;
01035   friend class MDNode;
01036 
01037 protected:
01038   DebugNode(LLVMContext &C, unsigned ID, StorageType Storage, unsigned Tag,
01039             ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None)
01040       : MDNode(C, ID, Storage, Ops1, Ops2) {
01041     assert(Tag < 1u << 16);
01042     SubclassData16 = Tag;
01043   }
01044   ~DebugNode() {}
01045 
01046 public:
01047   unsigned getTag() const { return SubclassData16; }
01048 
01049   static bool classof(const Metadata *MD) {
01050     return MD->getMetadataID() == GenericDebugNodeKind;
01051   }
01052 };
01053 
01054 /// \brief Generic tagged DWARF-like metadata node.
01055 ///
01056 /// An un-specialized DWARF-like metadata node.  The first operand is a
01057 /// (possibly empty) null-separated \a MDString header that contains arbitrary
01058 /// fields.  The remaining operands are \a dwarf_operands(), and are pointers
01059 /// to other metadata.
01060 class GenericDebugNode : public DebugNode {
01061   friend class LLVMContextImpl;
01062   friend class MDNode;
01063 
01064   GenericDebugNode(LLVMContext &C, StorageType Storage, unsigned Hash,
01065                    unsigned Tag, ArrayRef<Metadata *> Ops1,
01066                    ArrayRef<Metadata *> Ops2)
01067       : DebugNode(C, GenericDebugNodeKind, Storage, Tag, Ops1, Ops2) {
01068     setHash(Hash);
01069   }
01070   ~GenericDebugNode() { dropAllReferences(); }
01071 
01072   void setHash(unsigned Hash) { SubclassData32 = Hash; }
01073   void recalculateHash();
01074 
01075   static GenericDebugNode *getImpl(LLVMContext &Context, unsigned Tag,
01076                                    StringRef Header,
01077                                    ArrayRef<Metadata *> DwarfOps,
01078                                    StorageType Storage,
01079                                    bool ShouldCreate = true);
01080 
01081   TempGenericDebugNode cloneImpl() const {
01082     return getTemporary(
01083         getContext(), getTag(), getHeader(),
01084         SmallVector<Metadata *, 4>(dwarf_op_begin(), dwarf_op_end()));
01085   }
01086 
01087 public:
01088   unsigned getHash() const { return SubclassData32; }
01089 
01090   static GenericDebugNode *get(LLVMContext &Context, unsigned Tag,
01091                                StringRef Header,
01092                                ArrayRef<Metadata *> DwarfOps) {
01093     return getImpl(Context, Tag, Header, DwarfOps, Uniqued);
01094   }
01095   static GenericDebugNode *getIfExists(LLVMContext &Context, unsigned Tag,
01096                                        StringRef Header,
01097                                        ArrayRef<Metadata *> DwarfOps) {
01098     return getImpl(Context, Tag, Header, DwarfOps, Uniqued,
01099                    /* ShouldCreate */ false);
01100   }
01101   static GenericDebugNode *getDistinct(LLVMContext &Context, unsigned Tag,
01102                                        StringRef Header,
01103                                        ArrayRef<Metadata *> DwarfOps) {
01104     return getImpl(Context, Tag, Header, DwarfOps, Distinct);
01105   }
01106   static TempGenericDebugNode getTemporary(LLVMContext &Context, unsigned Tag,
01107                                            StringRef Header,
01108                                            ArrayRef<Metadata *> DwarfOps) {
01109     return TempGenericDebugNode(
01110         getImpl(Context, Tag, Header, DwarfOps, Temporary));
01111   }
01112 
01113   /// \brief Return a (temporary) clone of this.
01114   TempGenericDebugNode clone() const { return cloneImpl(); }
01115 
01116   unsigned getTag() const { return SubclassData16; }
01117   StringRef getHeader() const {
01118     if (auto *S = cast_or_null<MDString>(getOperand(0)))
01119       return S->getString();
01120     return StringRef();
01121   }
01122 
01123   op_iterator dwarf_op_begin() const { return op_begin() + 1; }
01124   op_iterator dwarf_op_end() const { return op_end(); }
01125   op_range dwarf_operands() const {
01126     return op_range(dwarf_op_begin(), dwarf_op_end());
01127   }
01128 
01129   unsigned getNumDwarfOperands() const { return getNumOperands() - 1; }
01130   const MDOperand &getDwarfOperand(unsigned I) const {
01131     return getOperand(I + 1);
01132   }
01133   void replaceDwarfOperandWith(unsigned I, Metadata *New) {
01134     replaceOperandWith(I + 1, New);
01135   }
01136 
01137   static bool classof(const Metadata *MD) {
01138     return MD->getMetadataID() == GenericDebugNodeKind;
01139   }
01140 };
01141 
01142 //===----------------------------------------------------------------------===//
01143 /// \brief A tuple of MDNodes.
01144 ///
01145 /// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
01146 /// to modules, have names, and contain lists of MDNodes.
01147 ///
01148 /// TODO: Inherit from Metadata.
01149 class NamedMDNode : public ilist_node<NamedMDNode> {
01150   friend class SymbolTableListTraits<NamedMDNode, Module>;
01151   friend struct ilist_traits<NamedMDNode>;
01152   friend class LLVMContextImpl;
01153   friend class Module;
01154   NamedMDNode(const NamedMDNode &) LLVM_DELETED_FUNCTION;
01155 
01156   std::string Name;
01157   Module *Parent;
01158   void *Operands; // SmallVector<TrackingMDRef, 4>
01159 
01160   void setParent(Module *M) { Parent = M; }
01161 
01162   explicit NamedMDNode(const Twine &N);
01163 
01164   template<class T1, class T2>
01165   class op_iterator_impl :
01166       public std::iterator<std::bidirectional_iterator_tag, T2> {
01167     const NamedMDNode *Node;
01168     unsigned Idx;
01169     op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { }
01170 
01171     friend class NamedMDNode;
01172 
01173   public:
01174     op_iterator_impl() : Node(nullptr), Idx(0) { }
01175 
01176     bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
01177     bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
01178     op_iterator_impl &operator++() {
01179       ++Idx;
01180       return *this;
01181     }
01182     op_iterator_impl operator++(int) {
01183       op_iterator_impl tmp(*this);
01184       operator++();
01185       return tmp;
01186     }
01187     op_iterator_impl &operator--() {
01188       --Idx;
01189       return *this;
01190     }
01191     op_iterator_impl operator--(int) {
01192       op_iterator_impl tmp(*this);
01193       operator--();
01194       return tmp;
01195     }
01196 
01197     T1 operator*() const { return Node->getOperand(Idx); }
01198   };
01199 
01200 public:
01201   /// \brief Drop all references and remove the node from parent module.
01202   void eraseFromParent();
01203 
01204   /// \brief Remove all uses and clear node vector.
01205   void dropAllReferences();
01206 
01207   ~NamedMDNode();
01208 
01209   /// \brief Get the module that holds this named metadata collection.
01210   inline Module *getParent() { return Parent; }
01211   inline const Module *getParent() const { return Parent; }
01212 
01213   MDNode *getOperand(unsigned i) const;
01214   unsigned getNumOperands() const;
01215   void addOperand(MDNode *M);
01216   void setOperand(unsigned I, MDNode *New);
01217   StringRef getName() const;
01218   void print(raw_ostream &ROS) const;
01219   void dump() const;
01220 
01221   // ---------------------------------------------------------------------------
01222   // Operand Iterator interface...
01223   //
01224   typedef op_iterator_impl<MDNode *, MDNode> op_iterator;
01225   op_iterator op_begin() { return op_iterator(this, 0); }
01226   op_iterator op_end()   { return op_iterator(this, getNumOperands()); }
01227 
01228   typedef op_iterator_impl<const MDNode *, MDNode> const_op_iterator;
01229   const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
01230   const_op_iterator op_end()   const { return const_op_iterator(this, getNumOperands()); }
01231 
01232   inline iterator_range<op_iterator>  operands() {
01233     return iterator_range<op_iterator>(op_begin(), op_end());
01234   }
01235   inline iterator_range<const_op_iterator> operands() const {
01236     return iterator_range<const_op_iterator>(op_begin(), op_end());
01237   }
01238 };
01239 
01240 } // end llvm namespace
01241 
01242 #endif