LLVM  10.0.0
Metadata.h
Go to the documentation of this file.
1 //===- llvm/IR/Metadata.h - Metadata definitions ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// @file
10 /// This file contains the declarations for metadata subclasses.
11 /// They represent the different flavors of metadata that live in LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_METADATA_H
16 #define LLVM_IR_METADATA_H
17 
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/DenseMapInfo.h"
21 #include "llvm/ADT/None.h"
22 #include "llvm/ADT/PointerUnion.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringMap.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/ilist_node.h"
29 #include "llvm/IR/Constant.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Value.h"
33 #include "llvm/Support/Casting.h"
35 #include <cassert>
36 #include <cstddef>
37 #include <cstdint>
38 #include <iterator>
39 #include <memory>
40 #include <string>
41 #include <type_traits>
42 #include <utility>
43 
44 namespace llvm {
45 
46 class Module;
47 class ModuleSlotTracker;
48 class raw_ostream;
49 class Type;
50 
52  DEBUG_METADATA_VERSION = 3 // Current debug info version number.
53 };
54 
55 /// Root of the metadata hierarchy.
56 ///
57 /// This is a root class for typeless data in the IR.
58 class Metadata {
60 
61  /// RTTI.
62  const unsigned char SubclassID;
63 
64 protected:
65  /// Active type of storage.
67 
68  /// Storage flag for non-uniqued, otherwise unowned, metadata.
69  unsigned char Storage : 7;
70  // TODO: expose remaining bits to subclasses.
71 
72  unsigned char ImplicitCode : 1;
73 
74  unsigned short SubclassData16 = 0;
75  unsigned SubclassData32 = 0;
76 
77 public:
78  enum MetadataKind {
79 #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
80 #include "llvm/IR/Metadata.def"
81  };
82 
83 protected:
85  : SubclassID(ID), Storage(Storage), ImplicitCode(false) {
86  static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
87  }
88 
89  ~Metadata() = default;
90 
91  /// Default handling of a changed operand, which asserts.
92  ///
93  /// If subclasses pass themselves in as owners to a tracking node reference,
94  /// they must provide an implementation of this method.
95  void handleChangedOperand(void *, Metadata *) {
96  llvm_unreachable("Unimplemented in Metadata subclass");
97  }
98 
99 public:
100  unsigned getMetadataID() const { return SubclassID; }
101 
102  /// User-friendly dump.
103  ///
104  /// If \c M is provided, metadata nodes will be numbered canonically;
105  /// otherwise, pointer addresses are substituted.
106  ///
107  /// Note: this uses an explicit overload instead of default arguments so that
108  /// the nullptr version is easy to call from a debugger.
109  ///
110  /// @{
111  void dump() const;
112  void dump(const Module *M) const;
113  /// @}
114 
115  /// Print.
116  ///
117  /// Prints definition of \c this.
118  ///
119  /// If \c M is provided, metadata nodes will be numbered canonically;
120  /// otherwise, pointer addresses are substituted.
121  /// @{
122  void print(raw_ostream &OS, const Module *M = nullptr,
123  bool IsForDebug = false) const;
124  void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr,
125  bool IsForDebug = false) const;
126  /// @}
127 
128  /// Print as operand.
129  ///
130  /// Prints reference of \c this.
131  ///
132  /// If \c M is provided, metadata nodes will be numbered canonically;
133  /// otherwise, pointer addresses are substituted.
134  /// @{
135  void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const;
137  const Module *M = nullptr) const;
138  /// @}
139 };
140 
141 // Create wrappers for C Binding types (see CBindingWrapping.h).
143 
144 // Specialized opaque metadata conversions.
146  return reinterpret_cast<Metadata**>(MDs);
147 }
148 
149 #define HANDLE_METADATA(CLASS) class CLASS;
150 #include "llvm/IR/Metadata.def"
151 
152 // Provide specializations of isa so that we don't need definitions of
153 // subclasses to see if the metadata is a subclass.
154 #define HANDLE_METADATA_LEAF(CLASS) \
155  template <> struct isa_impl<CLASS, Metadata> { \
156  static inline bool doit(const Metadata &MD) { \
157  return MD.getMetadataID() == Metadata::CLASS##Kind; \
158  } \
159  };
160 #include "llvm/IR/Metadata.def"
161 
162 inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) {
163  MD.print(OS);
164  return OS;
165 }
166 
167 /// Metadata wrapper in the Value hierarchy.
168 ///
169 /// A member of the \a Value hierarchy to represent a reference to metadata.
170 /// This allows, e.g., instrinsics to have metadata as operands.
171 ///
172 /// Notably, this is the only thing in either hierarchy that is allowed to
173 /// reference \a LocalAsMetadata.
174 class MetadataAsValue : public Value {
176  friend class LLVMContextImpl;
177 
178  Metadata *MD;
179 
180  MetadataAsValue(Type *Ty, Metadata *MD);
181 
182  /// Drop use of metadata (during teardown).
183  void dropUse() { MD = nullptr; }
184 
185 public:
186  ~MetadataAsValue();
187 
188  static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
189  static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD);
190 
191  Metadata *getMetadata() const { return MD; }
192 
193  static bool classof(const Value *V) {
194  return V->getValueID() == MetadataAsValueVal;
195  }
196 
197 private:
198  void handleChangedMetadata(Metadata *MD);
199  void track();
200  void untrack();
201 };
202 
203 /// API for tracking metadata references through RAUW and deletion.
204 ///
205 /// Shared API for updating \a Metadata pointers in subclasses that support
206 /// RAUW.
207 ///
208 /// This API is not meant to be used directly. See \a TrackingMDRef for a
209 /// user-friendly tracking reference.
211 public:
212  /// Track the reference to metadata.
213  ///
214  /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
215  /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
216  /// deleted, \c MD will be set to \c nullptr.
217  ///
218  /// If tracking isn't supported, \c *MD will not change.
219  ///
220  /// \return true iff tracking is supported by \c MD.
221  static bool track(Metadata *&MD) {
222  return track(&MD, *MD, static_cast<Metadata *>(nullptr));
223  }
224 
225  /// Track the reference to metadata for \a Metadata.
226  ///
227  /// As \a track(Metadata*&), but with support for calling back to \c Owner to
228  /// tell it that its operand changed. This could trigger \c Owner being
229  /// re-uniqued.
230  static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
231  return track(Ref, MD, &Owner);
232  }
233 
234  /// Track the reference to metadata for \a MetadataAsValue.
235  ///
236  /// As \a track(Metadata*&), but with support for calling back to \c Owner to
237  /// tell it that its operand changed. This could trigger \c Owner being
238  /// re-uniqued.
239  static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
240  return track(Ref, MD, &Owner);
241  }
242 
243  /// Stop tracking a reference to metadata.
244  ///
245  /// Stops \c *MD from tracking \c MD.
246  static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
247  static void untrack(void *Ref, Metadata &MD);
248 
249  /// Move tracking from one reference to another.
250  ///
251  /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
252  /// except that ownership callbacks are maintained.
253  ///
254  /// Note: it is an error if \c *MD does not equal \c New.
255  ///
256  /// \return true iff tracking is supported by \c MD.
257  static bool retrack(Metadata *&MD, Metadata *&New) {
258  return retrack(&MD, *MD, &New);
259  }
260  static bool retrack(void *Ref, Metadata &MD, void *New);
261 
262  /// Check whether metadata is replaceable.
263  static bool isReplaceable(const Metadata &MD);
264 
266 
267 private:
268  /// Track a reference to metadata for an owner.
269  ///
270  /// Generalized version of tracking.
271  static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
272 };
273 
274 /// Shared implementation of use-lists for replaceable metadata.
275 ///
276 /// Most metadata cannot be RAUW'ed. This is a shared implementation of
277 /// use-lists and associated API for the two that support it (\a ValueAsMetadata
278 /// and \a TempMDNode).
280  friend class MetadataTracking;
281 
282 public:
284 
285 private:
287  uint64_t NextIndex = 0;
289 
290 public:
291  ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
292 
294  assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
295  }
296 
297  LLVMContext &getContext() const { return Context; }
298 
299  /// Replace all uses of this with MD.
300  ///
301  /// Replace all uses of this with \c MD, which is allowed to be null.
302  void replaceAllUsesWith(Metadata *MD);
303 
304  /// Resolve all uses of this.
305  ///
306  /// Resolve all uses of this, turning off RAUW permanently. If \c
307  /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
308  /// is resolved.
309  void resolveAllUses(bool ResolveUsers = true);
310 
311 private:
312  void addRef(void *Ref, OwnerTy Owner);
313  void dropRef(void *Ref);
314  void moveRef(void *Ref, void *New, const Metadata &MD);
315 
316  /// Lazily construct RAUW support on MD.
317  ///
318  /// If this is an unresolved MDNode, RAUW support will be created on-demand.
319  /// ValueAsMetadata always has RAUW support.
320  static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
321 
322  /// Get RAUW support on MD, if it exists.
323  static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
324 
325  /// Check whether this node will support RAUW.
326  ///
327  /// Returns \c true unless getOrCreate() would return null.
328  static bool isReplaceable(const Metadata &MD);
329 };
330 
331 /// Value wrapper in the Metadata hierarchy.
332 ///
333 /// This is a custom value handle that allows other metadata to refer to
334 /// classes in the Value hierarchy.
335 ///
336 /// Because of full uniquing support, each value is only wrapped by a single \a
337 /// ValueAsMetadata object, so the lookup maps are far more efficient than
338 /// those using ValueHandleBase.
341  friend class LLVMContextImpl;
342 
343  Value *V;
344 
345  /// Drop users without RAUW (during teardown).
346  void dropUsers() {
347  ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
348  }
349 
350 protected:
351  ValueAsMetadata(unsigned ID, Value *V)
352  : Metadata(ID, Uniqued), ReplaceableMetadataImpl(V->getContext()), V(V) {
353  assert(V && "Expected valid value");
354  }
355 
356  ~ValueAsMetadata() = default;
357 
358 public:
359  static ValueAsMetadata *get(Value *V);
360 
362  return cast<ConstantAsMetadata>(get(C));
363  }
364 
365  static LocalAsMetadata *getLocal(Value *Local) {
366  return cast<LocalAsMetadata>(get(Local));
367  }
368 
369  static ValueAsMetadata *getIfExists(Value *V);
370 
372  return cast_or_null<ConstantAsMetadata>(getIfExists(C));
373  }
374 
376  return cast_or_null<LocalAsMetadata>(getIfExists(Local));
377  }
378 
379  Value *getValue() const { return V; }
380  Type *getType() const { return V->getType(); }
381  LLVMContext &getContext() const { return V->getContext(); }
382 
383  static void handleDeletion(Value *V);
384  static void handleRAUW(Value *From, Value *To);
385 
386 protected:
387  /// Handle collisions after \a Value::replaceAllUsesWith().
388  ///
389  /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
390  /// \a Value gets RAUW'ed and the target already exists, this is used to
391  /// merge the two metadata nodes.
394  }
395 
396 public:
397  static bool classof(const Metadata *MD) {
398  return MD->getMetadataID() == LocalAsMetadataKind ||
399  MD->getMetadataID() == ConstantAsMetadataKind;
400  }
401 };
402 
404  friend class ValueAsMetadata;
405 
407  : ValueAsMetadata(ConstantAsMetadataKind, C) {}
408 
409 public:
410  static ConstantAsMetadata *get(Constant *C) {
412  }
413 
416  }
417 
418  Constant *getValue() const {
419  return cast<Constant>(ValueAsMetadata::getValue());
420  }
421 
422  static bool classof(const Metadata *MD) {
423  return MD->getMetadataID() == ConstantAsMetadataKind;
424  }
425 };
426 
428  friend class ValueAsMetadata;
429 
430  LocalAsMetadata(Value *Local)
431  : ValueAsMetadata(LocalAsMetadataKind, Local) {
432  assert(!isa<Constant>(Local) && "Expected local value");
433  }
434 
435 public:
436  static LocalAsMetadata *get(Value *Local) {
437  return ValueAsMetadata::getLocal(Local);
438  }
439 
441  return ValueAsMetadata::getLocalIfExists(Local);
442  }
443 
444  static bool classof(const Metadata *MD) {
445  return MD->getMetadataID() == LocalAsMetadataKind;
446  }
447 };
448 
449 /// Transitional API for extracting constants from Metadata.
450 ///
451 /// This namespace contains transitional functions for metadata that points to
452 /// \a Constants.
453 ///
454 /// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
455 /// operands could refer to any \a Value. There's was a lot of code like this:
456 ///
457 /// \code
458 /// MDNode *N = ...;
459 /// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
460 /// \endcode
461 ///
462 /// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
463 /// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
464 /// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
465 /// cast in the \a Value hierarchy. Besides creating boiler-plate, this
466 /// requires subtle control flow changes.
467 ///
468 /// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
469 /// so that metadata can refer to numbers without traversing a bridge to the \a
470 /// Value hierarchy. In this final state, the code above would look like this:
471 ///
472 /// \code
473 /// MDNode *N = ...;
474 /// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
475 /// \endcode
476 ///
477 /// The API in this namespace supports the transition. \a MDInt doesn't exist
478 /// yet, and even once it does, changing each metadata schema to use it is its
479 /// own mini-project. In the meantime this API prevents us from introducing
480 /// complex and bug-prone control flow that will disappear in the end. In
481 /// particular, the above code looks like this:
482 ///
483 /// \code
484 /// MDNode *N = ...;
485 /// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
486 /// \endcode
487 ///
488 /// The full set of provided functions includes:
489 ///
490 /// mdconst::hasa <=> isa
491 /// mdconst::extract <=> cast
492 /// mdconst::extract_or_null <=> cast_or_null
493 /// mdconst::dyn_extract <=> dyn_cast
494 /// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
495 ///
496 /// The target of the cast must be a subclass of \a Constant.
497 namespace mdconst {
498 
499 namespace detail {
500 
501 template <class T> T &make();
502 template <class T, class Result> struct HasDereference {
503  using Yes = char[1];
504  using No = char[2];
505  template <size_t N> struct SFINAE {};
506 
507  template <class U, class V>
508  static Yes &hasDereference(SFINAE<sizeof(static_cast<V>(*make<U>()))> * = 0);
509  template <class U, class V> static No &hasDereference(...);
510 
511  static const bool value =
512  sizeof(hasDereference<T, Result>(nullptr)) == sizeof(Yes);
513 };
514 template <class V, class M> struct IsValidPointer {
515  static const bool value = std::is_base_of<Constant, V>::value &&
517 };
518 template <class V, class M> struct IsValidReference {
519  static const bool value = std::is_base_of<Constant, V>::value &&
520  std::is_convertible<M, const Metadata &>::value;
521 };
522 
523 } // end namespace detail
524 
525 /// Check whether Metadata has a Value.
526 ///
527 /// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
528 /// type \c X.
529 template <class X, class Y>
530 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, bool>::type
531 hasa(Y &&MD) {
532  assert(MD && "Null pointer sent into hasa");
533  if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
534  return isa<X>(V->getValue());
535  return false;
536 }
537 template <class X, class Y>
538 inline
539  typename std::enable_if<detail::IsValidReference<X, Y &>::value, bool>::type
540  hasa(Y &MD) {
541  return hasa(&MD);
542 }
543 
544 /// Extract a Value from Metadata.
545 ///
546 /// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
547 template <class X, class Y>
548 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
549 extract(Y &&MD) {
550  return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
551 }
552 template <class X, class Y>
553 inline
554  typename std::enable_if<detail::IsValidReference<X, Y &>::value, X *>::type
555  extract(Y &MD) {
556  return extract(&MD);
557 }
558 
559 /// Extract a Value from Metadata, allowing null.
560 ///
561 /// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
562 /// from \c MD, allowing \c MD to be null.
563 template <class X, class Y>
564 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
566  if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
567  return cast<X>(V->getValue());
568  return nullptr;
569 }
570 
571 /// Extract a Value from Metadata, if any.
572 ///
573 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
574 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
575 /// Value it does contain is of the wrong subclass.
576 template <class X, class Y>
577 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
578 dyn_extract(Y &&MD) {
579  if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
580  return dyn_cast<X>(V->getValue());
581  return nullptr;
582 }
583 
584 /// Extract a Value from Metadata, if any, allowing null.
585 ///
586 /// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
587 /// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
588 /// Value it does contain is of the wrong subclass, allowing \c MD to be null.
589 template <class X, class Y>
590 inline typename std::enable_if<detail::IsValidPointer<X, Y>::value, X *>::type
592  if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
593  return dyn_cast<X>(V->getValue());
594  return nullptr;
595 }
596 
597 } // end namespace mdconst
598 
599 //===----------------------------------------------------------------------===//
600 /// A single uniqued string.
601 ///
602 /// These are used to efficiently contain a byte sequence for metadata.
603 /// MDString is always unnamed.
604 class MDString : public Metadata {
606 
607  StringMapEntry<MDString> *Entry = nullptr;
608 
609  MDString() : Metadata(MDStringKind, Uniqued) {}
610 
611 public:
612  MDString(const MDString &) = delete;
613  MDString &operator=(MDString &&) = delete;
614  MDString &operator=(const MDString &) = delete;
615 
616  static MDString *get(LLVMContext &Context, StringRef Str);
617  static MDString *get(LLVMContext &Context, const char *Str) {
618  return get(Context, Str ? StringRef(Str) : StringRef());
619  }
620 
621  StringRef getString() const;
622 
623  unsigned getLength() const { return (unsigned)getString().size(); }
624 
626 
627  /// Pointer to the first byte of the string.
628  iterator begin() const { return getString().begin(); }
629 
630  /// Pointer to one byte past the end of the string.
631  iterator end() const { return getString().end(); }
632 
633  const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
634  const unsigned char *bytes_end() const { return getString().bytes_end(); }
635 
636  /// Methods for support type inquiry through isa, cast, and dyn_cast.
637  static bool classof(const Metadata *MD) {
638  return MD->getMetadataID() == MDStringKind;
639  }
640 };
641 
642 /// A collection of metadata nodes that might be associated with a
643 /// memory access used by the alias-analysis infrastructure.
644 struct AAMDNodes {
645  explicit AAMDNodes() = default;
646  explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
647  : TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N) {}
648 
649  bool operator==(const AAMDNodes &A) const {
650  return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
651  NoAlias == A.NoAlias;
652  }
653 
654  bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
655 
656  explicit operator bool() const {
657  return TBAA || TBAAStruct || Scope || NoAlias;
658  }
659 
660  /// The tag for type-based alias analysis.
661  MDNode *TBAA = nullptr;
662 
663  /// The tag for type-based alias analysis (tbaa struct).
664  MDNode *TBAAStruct = nullptr;
665 
666  /// The tag for alias scope specification (used with noalias).
667  MDNode *Scope = nullptr;
668 
669  /// The tag specifying the noalias scope.
670  MDNode *NoAlias = nullptr;
671 
672  /// Given two sets of AAMDNodes that apply to the same pointer,
673  /// give the best AAMDNodes that are compatible with both (i.e. a set of
674  /// nodes whose allowable aliasing conclusions are a subset of those
675  /// allowable by both of the inputs). However, for efficiency
676  /// reasons, do not create any new MDNodes.
678  AAMDNodes Result;
679  Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
680  Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
681  Result.Scope = Other.Scope == Scope ? Scope : nullptr;
682  Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
683  return Result;
684  }
685 };
686 
687 // Specialize DenseMapInfo for AAMDNodes.
688 template<>
690  static inline AAMDNodes getEmptyKey() {
692  nullptr, nullptr, nullptr);
693  }
694 
695  static inline AAMDNodes getTombstoneKey() {
697  nullptr, nullptr, nullptr);
698  }
699 
700  static unsigned getHashValue(const AAMDNodes &Val) {
705  }
706 
707  static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
708  return LHS == RHS;
709  }
710 };
711 
712 /// Tracking metadata reference owned by Metadata.
713 ///
714 /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
715 /// of \a Metadata, which has the option of registering itself for callbacks to
716 /// re-unique itself.
717 ///
718 /// In particular, this is used by \a MDNode.
719 class MDOperand {
720  Metadata *MD = nullptr;
721 
722 public:
723  MDOperand() = default;
724  MDOperand(MDOperand &&) = delete;
725  MDOperand(const MDOperand &) = delete;
726  MDOperand &operator=(MDOperand &&) = delete;
727  MDOperand &operator=(const MDOperand &) = delete;
728  ~MDOperand() { untrack(); }
729 
730  Metadata *get() const { return MD; }
731  operator Metadata *() const { return get(); }
732  Metadata *operator->() const { return get(); }
733  Metadata &operator*() const { return *get(); }
734 
735  void reset() {
736  untrack();
737  MD = nullptr;
738  }
739  void reset(Metadata *MD, Metadata *Owner) {
740  untrack();
741  this->MD = MD;
742  track(Owner);
743  }
744 
745 private:
746  void track(Metadata *Owner) {
747  if (MD) {
748  if (Owner)
749  MetadataTracking::track(this, *MD, *Owner);
750  else
752  }
753  }
754 
755  void untrack() {
756  assert(static_cast<void *>(this) == &MD && "Expected same address");
757  if (MD)
759  }
760 };
761 
762 template <> struct simplify_type<MDOperand> {
763  using SimpleType = Metadata *;
764 
765  static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
766 };
767 
768 template <> struct simplify_type<const MDOperand> {
769  using SimpleType = Metadata *;
770 
771  static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
772 };
773 
774 /// Pointer to the context, with optional RAUW support.
775 ///
776 /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
777 /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
780 
781 public:
784  std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
785  : Ptr(ReplaceableUses.release()) {
786  assert(getReplaceableUses() && "Expected non-null replaceable uses");
787  }
788  ContextAndReplaceableUses() = delete;
793  operator=(const ContextAndReplaceableUses &) = delete;
794  ~ContextAndReplaceableUses() { delete getReplaceableUses(); }
795 
796  operator LLVMContext &() { return getContext(); }
797 
798  /// Whether this contains RAUW support.
799  bool hasReplaceableUses() const {
800  return Ptr.is<ReplaceableMetadataImpl *>();
801  }
802 
804  if (hasReplaceableUses())
805  return getReplaceableUses()->getContext();
806  return *Ptr.get<LLVMContext *>();
807  }
808 
810  if (hasReplaceableUses())
811  return Ptr.get<ReplaceableMetadataImpl *>();
812  return nullptr;
813  }
814 
815  /// Ensure that this has RAUW support, and then return it.
817  if (!hasReplaceableUses())
818  makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext()));
819  return getReplaceableUses();
820  }
821 
822  /// Assign RAUW support to this.
823  ///
824  /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
825  /// not be null).
826  void
827  makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
828  assert(ReplaceableUses && "Expected non-null replaceable uses");
829  assert(&ReplaceableUses->getContext() == &getContext() &&
830  "Expected same context");
831  delete getReplaceableUses();
832  Ptr = ReplaceableUses.release();
833  }
834 
835  /// Drop RAUW support.
836  ///
837  /// Cede ownership of RAUW support, returning it.
838  std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
839  assert(hasReplaceableUses() && "Expected to own replaceable uses");
840  std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
841  getReplaceableUses());
842  Ptr = &ReplaceableUses->getContext();
843  return ReplaceableUses;
844  }
845 };
846 
848  inline void operator()(MDNode *Node) const;
849 };
850 
851 #define HANDLE_MDNODE_LEAF(CLASS) \
852  using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
853 #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
854 #include "llvm/IR/Metadata.def"
855 
856 /// Metadata node.
857 ///
858 /// Metadata nodes can be uniqued, like constants, or distinct. Temporary
859 /// metadata nodes (with full support for RAUW) can be used to delay uniquing
860 /// until forward references are known. The basic metadata node is an \a
861 /// MDTuple.
862 ///
863 /// There is limited support for RAUW at construction time. At construction
864 /// time, if any operand is a temporary node (or an unresolved uniqued node,
865 /// which indicates a transitive temporary operand), the node itself will be
866 /// unresolved. As soon as all operands become resolved, it will drop RAUW
867 /// support permanently.
868 ///
869 /// If an unresolved node is part of a cycle, \a resolveCycles() needs
870 /// to be called on some member of the cycle once all temporary nodes have been
871 /// replaced.
872 class MDNode : public Metadata {
874  friend class LLVMContextImpl;
875 
876  unsigned NumOperands;
877  unsigned NumUnresolved;
878 
880 
881 protected:
882  MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
884  ~MDNode() = default;
885 
886  void *operator new(size_t Size, unsigned NumOps);
887  void operator delete(void *Mem);
888 
889  /// Required by std, but never called.
890  void operator delete(void *, unsigned) {
891  llvm_unreachable("Constructor throws?");
892  }
893 
894  /// Required by std, but never called.
895  void operator delete(void *, unsigned, bool) {
896  llvm_unreachable("Constructor throws?");
897  }
898 
899  void dropAllReferences();
900 
901  MDOperand *mutable_begin() { return mutable_end() - NumOperands; }
902  MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); }
903 
905 
907  return mutable_op_range(mutable_begin(), mutable_end());
908  }
909 
910 public:
911  MDNode(const MDNode &) = delete;
912  void operator=(const MDNode &) = delete;
913  void *operator new(size_t) = delete;
914 
915  static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
916  static inline MDTuple *getIfExists(LLVMContext &Context,
918  static inline MDTuple *getDistinct(LLVMContext &Context,
920  static inline TempMDTuple getTemporary(LLVMContext &Context,
922 
923  /// Create a (temporary) clone of this.
924  TempMDNode clone() const;
925 
926  /// Deallocate a node created by getTemporary.
927  ///
928  /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
929  /// references will be reset.
930  static void deleteTemporary(MDNode *N);
931 
932  LLVMContext &getContext() const { return Context.getContext(); }
933 
934  /// Replace a specific operand.
935  void replaceOperandWith(unsigned I, Metadata *New);
936 
937  /// Check if node is fully resolved.
938  ///
939  /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
940  /// this always returns \c true.
941  ///
942  /// If \a isUniqued(), returns \c true if this has already dropped RAUW
943  /// support (because all operands are resolved).
944  ///
945  /// As forward declarations are resolved, their containers should get
946  /// resolved automatically. However, if this (or one of its operands) is
947  /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
948  bool isResolved() const { return !isTemporary() && !NumUnresolved; }
949 
950  bool isUniqued() const { return Storage == Uniqued; }
951  bool isDistinct() const { return Storage == Distinct; }
952  bool isTemporary() const { return Storage == Temporary; }
953 
954  /// RAUW a temporary.
955  ///
956  /// \pre \a isTemporary() must be \c true.
958  assert(isTemporary() && "Expected temporary node");
959  if (Context.hasReplaceableUses())
960  Context.getReplaceableUses()->replaceAllUsesWith(MD);
961  }
962 
963  /// Resolve cycles.
964  ///
965  /// Once all forward declarations have been resolved, force cycles to be
966  /// resolved.
967  ///
968  /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
969  void resolveCycles();
970 
971  /// Resolve a unique, unresolved node.
972  void resolve();
973 
974  /// Replace a temporary node with a permanent one.
975  ///
976  /// Try to create a uniqued version of \c N -- in place, if possible -- and
977  /// return it. If \c N cannot be uniqued, return a distinct node instead.
978  template <class T>
979  static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
980  replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
981  return cast<T>(N.release()->replaceWithPermanentImpl());
982  }
983 
984  /// Replace a temporary node with a uniqued one.
985  ///
986  /// Create a uniqued version of \c N -- in place, if possible -- and return
987  /// it. Takes ownership of the temporary node.
988  ///
989  /// \pre N does not self-reference.
990  template <class T>
991  static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
992  replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
993  return cast<T>(N.release()->replaceWithUniquedImpl());
994  }
995 
996  /// Replace a temporary node with a distinct one.
997  ///
998  /// Create a distinct version of \c N -- in place, if possible -- and return
999  /// it. Takes ownership of the temporary node.
1000  template <class T>
1001  static typename std::enable_if<std::is_base_of<MDNode, T>::value, T *>::type
1002  replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
1003  return cast<T>(N.release()->replaceWithDistinctImpl());
1004  }
1005 
1006 private:
1007  MDNode *replaceWithPermanentImpl();
1008  MDNode *replaceWithUniquedImpl();
1009  MDNode *replaceWithDistinctImpl();
1010 
1011 protected:
1012  /// Set an operand.
1013  ///
1014  /// Sets the operand directly, without worrying about uniquing.
1015  void setOperand(unsigned I, Metadata *New);
1016 
1017  void storeDistinctInContext();
1018  template <class T, class StoreT>
1019  static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
1020  template <class T> static T *storeImpl(T *N, StorageType Storage);
1021 
1022 private:
1023  void handleChangedOperand(void *Ref, Metadata *New);
1024 
1025  /// Drop RAUW support, if any.
1026  void dropReplaceableUses();
1027 
1028  void resolveAfterOperandChange(Metadata *Old, Metadata *New);
1029  void decrementUnresolvedOperandCount();
1030  void countUnresolvedOperands();
1031 
1032  /// Mutate this to be "uniqued".
1033  ///
1034  /// Mutate this so that \a isUniqued().
1035  /// \pre \a isTemporary().
1036  /// \pre already added to uniquing set.
1037  void makeUniqued();
1038 
1039  /// Mutate this to be "distinct".
1040  ///
1041  /// Mutate this so that \a isDistinct().
1042  /// \pre \a isTemporary().
1043  void makeDistinct();
1044 
1045  void deleteAsSubclass();
1046  MDNode *uniquify();
1047  void eraseFromStore();
1048 
1049  template <class NodeTy> struct HasCachedHash;
1050  template <class NodeTy>
1051  static void dispatchRecalculateHash(NodeTy *N, std::true_type) {
1052  N->recalculateHash();
1053  }
1054  template <class NodeTy>
1055  static void dispatchRecalculateHash(NodeTy *, std::false_type) {}
1056  template <class NodeTy>
1057  static void dispatchResetHash(NodeTy *N, std::true_type) {
1058  N->setHash(0);
1059  }
1060  template <class NodeTy>
1061  static void dispatchResetHash(NodeTy *, std::false_type) {}
1062 
1063 public:
1064  using op_iterator = const MDOperand *;
1066 
1068  return const_cast<MDNode *>(this)->mutable_begin();
1069  }
1070 
1072  return const_cast<MDNode *>(this)->mutable_end();
1073  }
1074 
1075  op_range operands() const { return op_range(op_begin(), op_end()); }
1076 
1077  const MDOperand &getOperand(unsigned I) const {
1078  assert(I < NumOperands && "Out of range");
1079  return op_begin()[I];
1080  }
1081 
1082  /// Return number of MDNode operands.
1083  unsigned getNumOperands() const { return NumOperands; }
1084 
1085  /// Methods for support type inquiry through isa, cast, and dyn_cast:
1086  static bool classof(const Metadata *MD) {
1087  switch (MD->getMetadataID()) {
1088  default:
1089  return false;
1090 #define HANDLE_MDNODE_LEAF(CLASS) \
1091  case CLASS##Kind: \
1092  return true;
1093 #include "llvm/IR/Metadata.def"
1094  }
1095  }
1096 
1097  /// Check whether MDNode is a vtable access.
1098  bool isTBAAVtableAccess() const;
1099 
1100  /// Methods for metadata merging.
1101  static MDNode *concatenate(MDNode *A, MDNode *B);
1102  static MDNode *intersect(MDNode *A, MDNode *B);
1103  static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B);
1104  static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B);
1105  static MDNode *getMostGenericRange(MDNode *A, MDNode *B);
1106  static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B);
1107  static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B);
1108 };
1109 
1110 /// Tuple of metadata.
1111 ///
1112 /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
1113 /// default based on their operands.
1114 class MDTuple : public MDNode {
1115  friend class LLVMContextImpl;
1116  friend class MDNode;
1117 
1118  MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
1119  ArrayRef<Metadata *> Vals)
1120  : MDNode(C, MDTupleKind, Storage, Vals) {
1121  setHash(Hash);
1122  }
1123 
1124  ~MDTuple() { dropAllReferences(); }
1125 
1126  void setHash(unsigned Hash) { SubclassData32 = Hash; }
1127  void recalculateHash();
1128 
1130  StorageType Storage, bool ShouldCreate = true);
1131 
1132  TempMDTuple cloneImpl() const {
1133  return getTemporary(getContext(),
1134  SmallVector<Metadata *, 4>(op_begin(), op_end()));
1135  }
1136 
1137 public:
1138  /// Get the hash, if any.
1139  unsigned getHash() const { return SubclassData32; }
1140 
1142  return getImpl(Context, MDs, Uniqued);
1143  }
1144 
1146  return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
1147  }
1148 
1149  /// Return a distinct node.
1150  ///
1151  /// Return a distinct node -- i.e., a node that is not uniqued.
1153  return getImpl(Context, MDs, Distinct);
1154  }
1155 
1156  /// Return a temporary node.
1157  ///
1158  /// For use in constructing cyclic MDNode structures. A temporary MDNode is
1159  /// not uniqued, may be RAUW'd, and must be manually deleted with
1160  /// deleteTemporary.
1161  static TempMDTuple getTemporary(LLVMContext &Context,
1162  ArrayRef<Metadata *> MDs) {
1163  return TempMDTuple(getImpl(Context, MDs, Temporary));
1164  }
1165 
1166  /// Return a (temporary) clone of this.
1167  TempMDTuple clone() const { return cloneImpl(); }
1168 
1169  static bool classof(const Metadata *MD) {
1170  return MD->getMetadataID() == MDTupleKind;
1171  }
1172 };
1173 
1175  return MDTuple::get(Context, MDs);
1176 }
1177 
1179  return MDTuple::getIfExists(Context, MDs);
1180 }
1181 
1183  return MDTuple::getDistinct(Context, MDs);
1184 }
1185 
1187  ArrayRef<Metadata *> MDs) {
1188  return MDTuple::getTemporary(Context, MDs);
1189 }
1190 
1193 }
1194 
1195 /// Typed iterator through MDNode operands.
1196 ///
1197 /// An iterator that transforms an \a MDNode::iterator into an iterator over a
1198 /// particular Metadata subclass.
1199 template <class T>
1201  : public std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void,
1202  T *> {
1203  MDNode::op_iterator I = nullptr;
1204 
1205 public:
1206  TypedMDOperandIterator() = default;
1208 
1209  T *operator*() const { return cast_or_null<T>(*I); }
1210 
1212  ++I;
1213  return *this;
1214  }
1215 
1217  TypedMDOperandIterator Temp(*this);
1218  ++I;
1219  return Temp;
1220  }
1221 
1222  bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
1223  bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
1224 };
1225 
1226 /// Typed, array-like tuple of metadata.
1227 ///
1228 /// This is a wrapper for \a MDTuple that makes it act like an array holding a
1229 /// particular type of metadata.
1230 template <class T> class MDTupleTypedArrayWrapper {
1231  const MDTuple *N = nullptr;
1232 
1233 public:
1234  MDTupleTypedArrayWrapper() = default;
1235  MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
1236 
1237  template <class U>
1240  typename std::enable_if<std::is_convertible<U *, T *>::value>::type * =
1241  nullptr)
1242  : N(Other.get()) {}
1243 
1244  template <class U>
1247  typename std::enable_if<!std::is_convertible<U *, T *>::value>::type * =
1248  nullptr)
1249  : N(Other.get()) {}
1250 
1251  explicit operator bool() const { return get(); }
1252  explicit operator MDTuple *() const { return get(); }
1253 
1254  MDTuple *get() const { return const_cast<MDTuple *>(N); }
1255  MDTuple *operator->() const { return get(); }
1256  MDTuple &operator*() const { return *get(); }
1257 
1258  // FIXME: Fix callers and remove condition on N.
1259  unsigned size() const { return N ? N->getNumOperands() : 0u; }
1260  bool empty() const { return N ? N->getNumOperands() == 0 : true; }
1261  T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
1262 
1263  // FIXME: Fix callers and remove condition on N.
1265 
1266  iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
1267  iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
1268 };
1269 
1270 #define HANDLE_METADATA(CLASS) \
1271  using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1272 #include "llvm/IR/Metadata.def"
1273 
1274 /// Placeholder metadata for operands of distinct MDNodes.
1275 ///
1276 /// This is a lightweight placeholder for an operand of a distinct node. It's
1277 /// purpose is to help track forward references when creating a distinct node.
1278 /// This allows distinct nodes involved in a cycle to be constructed before
1279 /// their operands without requiring a heavyweight temporary node with
1280 /// full-blown RAUW support.
1281 ///
1282 /// Each placeholder supports only a single MDNode user. Clients should pass
1283 /// an ID, retrieved via \a getID(), to indicate the "real" operand that this
1284 /// should be replaced with.
1285 ///
1286 /// While it would be possible to implement move operators, they would be
1287 /// fairly expensive. Leave them unimplemented to discourage their use
1288 /// (clients can use std::deque, std::list, BumpPtrAllocator, etc.).
1290  friend class MetadataTracking;
1291 
1292  Metadata **Use = nullptr;
1293 
1294 public:
1296  : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
1297  SubclassData32 = ID;
1298  }
1299 
1300  DistinctMDOperandPlaceholder() = delete;
1303 
1305  if (Use)
1306  *Use = nullptr;
1307  }
1308 
1309  unsigned getID() const { return SubclassData32; }
1310 
1311  /// Replace the use of this with MD.
1313  if (!Use)
1314  return;
1315  *Use = MD;
1316 
1317  if (*Use)
1319 
1320  Metadata *T = cast<Metadata>(this);
1322  assert(!Use && "Use is still being tracked despite being untracked!");
1323  }
1324 };
1325 
1326 //===----------------------------------------------------------------------===//
1327 /// A tuple of MDNodes.
1328 ///
1329 /// Despite its name, a NamedMDNode isn't itself an MDNode.
1330 ///
1331 /// NamedMDNodes are named module-level entities that contain lists of MDNodes.
1332 ///
1333 /// It is illegal for a NamedMDNode to appear as an operand of an MDNode.
1334 class NamedMDNode : public ilist_node<NamedMDNode> {
1335  friend class LLVMContextImpl;
1336  friend class Module;
1337 
1338  std::string Name;
1339  Module *Parent = nullptr;
1340  void *Operands; // SmallVector<TrackingMDRef, 4>
1341 
1342  void setParent(Module *M) { Parent = M; }
1343 
1344  explicit NamedMDNode(const Twine &N);
1345 
1346  template<class T1, class T2>
1347  class op_iterator_impl :
1348  public std::iterator<std::bidirectional_iterator_tag, T2> {
1349  friend class NamedMDNode;
1350 
1351  const NamedMDNode *Node = nullptr;
1352  unsigned Idx = 0;
1353 
1354  op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {}
1355 
1356  public:
1357  op_iterator_impl() = default;
1358 
1359  bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
1360  bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
1361 
1362  op_iterator_impl &operator++() {
1363  ++Idx;
1364  return *this;
1365  }
1366 
1367  op_iterator_impl operator++(int) {
1368  op_iterator_impl tmp(*this);
1369  operator++();
1370  return tmp;
1371  }
1372 
1373  op_iterator_impl &operator--() {
1374  --Idx;
1375  return *this;
1376  }
1377 
1378  op_iterator_impl operator--(int) {
1379  op_iterator_impl tmp(*this);
1380  operator--();
1381  return tmp;
1382  }
1383 
1384  T1 operator*() const { return Node->getOperand(Idx); }
1385  };
1386 
1387 public:
1388  NamedMDNode(const NamedMDNode &) = delete;
1389  ~NamedMDNode();
1390 
1391  /// Drop all references and remove the node from parent module.
1392  void eraseFromParent();
1393 
1394  /// Remove all uses and clear node vector.
1395  void dropAllReferences() { clearOperands(); }
1396  /// Drop all references to this node's operands.
1397  void clearOperands();
1398 
1399  /// Get the module that holds this named metadata collection.
1400  inline Module *getParent() { return Parent; }
1401  inline const Module *getParent() const { return Parent; }
1402 
1403  MDNode *getOperand(unsigned i) const;
1404  unsigned getNumOperands() const;
1405  void addOperand(MDNode *M);
1406  void setOperand(unsigned I, MDNode *New);
1407  StringRef getName() const;
1408  void print(raw_ostream &ROS, bool IsForDebug = false) const;
1409  void print(raw_ostream &ROS, ModuleSlotTracker &MST,
1410  bool IsForDebug = false) const;
1411  void dump() const;
1412 
1413  // ---------------------------------------------------------------------------
1414  // Operand Iterator interface...
1415  //
1416  using op_iterator = op_iterator_impl<MDNode *, MDNode>;
1417 
1418  op_iterator op_begin() { return op_iterator(this, 0); }
1419  op_iterator op_end() { return op_iterator(this, getNumOperands()); }
1420 
1421  using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>;
1422 
1423  const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
1424  const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); }
1425 
1427  return make_range(op_begin(), op_end());
1428  }
1430  return make_range(op_begin(), op_end());
1431  }
1432 };
1433 
1434 // Create wrappers for C Binding types (see CBindingWrapping.h).
1436 
1437 } // end namespace llvm
1438 
1439 #endif // LLVM_IR_METADATA_H
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.h:1395
uint64_t CallInst * C
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:832
bool isUniqued() const
Definition: Metadata.h:950
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:719
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:69
ContextAndReplaceableUses(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Definition: Metadata.h:783
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1141
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
bool isDistinct() const
Definition: Metadata.h:951
static bool classof(const Metadata *MD)
Definition: Metadata.h:1169
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:484
LLVMContext & Context
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:667
const unsigned char * bytes_end() const
Definition: Metadata.h:634
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1082
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:661
const unsigned char * bytes_end() const
Definition: StringRef.h:129
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:158
const char * iterator
Definition: StringRef.h:58
TypedMDOperandIterator operator++(int)
Definition: Metadata.h:1216
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithPermanent(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a permanent one.
Definition: Metadata.h:980
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
iterator begin() const
Pointer to the first byte of the string.
Definition: Metadata.h:628
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
unsigned getLength() const
Definition: Metadata.h:623
static bool classof(const Metadata *MD)
Definition: Metadata.h:422
std::unique_ptr< ReplaceableMetadataImpl > takeReplaceableUses()
Drop RAUW support.
Definition: Metadata.h:838
Metadata * getMetadata() const
Definition: Metadata.h:191
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Metadata * operator->() const
Definition: Metadata.h:732
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
ContextAndReplaceableUses(LLVMContext &Context)
Definition: Metadata.h:782
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:744
static ConstantAsMetadata * getConstantIfExists(Value *C)
Definition: Metadata.h:371
MDTupleTypedArrayWrapper(const MDTuple *N)
Definition: Metadata.h:1235
Shared implementation of use-lists for replaceable metadata.
Definition: Metadata.h:279
Metadata node.
Definition: Metadata.h:872
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1077
LLVMContext & getContext() const
Definition: Metadata.h:803
Manage lifetime of a slot tracker for printing IR.
Typed, array-like tuple of metadata.
Definition: Metadata.h:1230
ReplaceableMetadataImpl(LLVMContext &Context)
Definition: Metadata.h:291
void replaceUseWith(Metadata *MD)
Replace the use of this with MD.
Definition: Metadata.h:1312
static bool track(void *Ref, Metadata &MD, Metadata &Owner)
Track the reference to metadata for Metadata.
Definition: Metadata.h:230
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:194
op_iterator op_end() const
Definition: Metadata.h:1071
Tuple of metadata.
Definition: Metadata.h:1114
bool operator!=(const AAMDNodes &A) const
Definition: Metadata.h:654
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithDistinct(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a distinct one.
Definition: Metadata.h:1002
Metadata & operator*() const
Definition: Metadata.h:733
static AAMDNodes getEmptyKey()
Definition: Metadata.h:690
static MCDisassembler::DecodeStatus addOperand(MCInst &Inst, const MCOperand &Opnd)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
A tuple of MDNodes.
Definition: Metadata.h:1334
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
struct LLVMOpaqueNamedMDNode * LLVMNamedMDNodeRef
Represents an LLVM Named Metadata Node.
Definition: Types.h:96
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:204
void makeReplaceable(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Assign RAUW support to this.
Definition: Metadata.h:827
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:948
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition: Metadata.h:221
The access may reference the value stored in memory.
unsigned getHash() const
Get the hash, if any.
Definition: Metadata.h:1139
unsigned short SubclassData16
Definition: Metadata.h:74
Typed iterator through MDNode operands.
Definition: Metadata.h:1200
static StringRef getName(Value *V)
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1145
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1186
Placeholder metadata for operands of distinct MDNodes.
Definition: Metadata.h:1289
TypedMDOperandIterator & operator++()
Definition: Metadata.h:1211
mir Rename Register Operands
op_iterator_impl< const MDNode *, MDNode > const_op_iterator
Definition: Metadata.h:1421
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2148
API for tracking metadata references through RAUW and deletion.
Definition: Metadata.h:210
op_iterator op_begin() const
Definition: Metadata.h:1067
unsigned char ImplicitCode
Definition: Metadata.h:72
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition: Metadata.h:1086
PointerUnion< MetadataAsValue *, Metadata * > OwnerTy
Definition: Metadata.h:265
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:246
op_range operands() const
Definition: Metadata.h:1075
MDTuple * operator->() const
Definition: Metadata.h:1255
LLVMContext & getContext() const
Definition: Metadata.h:932
const_op_iterator op_end() const
Definition: Metadata.h:1424
unsigned getMetadataID() const
Definition: Metadata.h:100
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
Definition: Metadata.h:646
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
const Module * getParent() const
Definition: Metadata.h:1401
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:339
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:957
unsigned SubclassData32
Definition: Metadata.h:75
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1178
iterator_range< op_iterator > operands()
Definition: Metadata.h:1426
LLVMConstants
Definition: Metadata.h:51
static bool classof(const Metadata *MD)
Definition: Metadata.h:444
static bool classof(const Value *V)
Definition: Metadata.h:193
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Return a temporary node.
Definition: Metadata.h:1161
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1174
static std::enable_if< std::is_base_of< MDNode, T >::value, T * >::type replaceWithUniqued(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a uniqued one.
Definition: Metadata.h:992
ValueAsMetadata(unsigned ID, Value *V)
Definition: Metadata.h:351
void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition: Metadata.cpp:280
MDOperand * mutable_end()
Definition: Metadata.h:902
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static LocalAsMetadata * getLocal(Value *Local)
Definition: Metadata.h:365
iterator end() const
Pointer to one byte past the end of the string.
Definition: Metadata.h:631
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
LLVMContext & getContext() const
Definition: Metadata.h:381
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
This is an important base class in LLVM.
Definition: Constant.h:41
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Definition: Metadata.h:1182
amdgpu inline
StorageType
Active type of storage.
Definition: Metadata.h:66
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if<!std::is_convertible< U *, T *>::value >::type *=nullptr)
Definition: Metadata.h:1245
TempMDTuple clone() const
Return a (temporary) clone of this.
Definition: Metadata.h:1167
static ConstantAsMetadata * getIfExists(Constant *C)
Definition: Metadata.h:414
const_op_iterator op_begin() const
Definition: Metadata.h:1423
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition: Metadata.h:257
const unsigned char * bytes_begin() const
Definition: Metadata.h:633
op_iterator_impl< MDNode *, MDNode > op_iterator
Definition: Metadata.h:1416
static SimpleType getSimplifiedValue(MDOperand &MD)
Definition: Metadata.h:765
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:174
void handleChangedOperand(void *, Metadata *)
Default handling of a changed operand, which asserts.
Definition: Metadata.h:95
Value * getValue() const
Definition: Metadata.h:379
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
mutable_op_range mutable_operands()
Definition: Metadata.h:906
op_iterator op_end()
Definition: Metadata.h:1419
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition: Metadata.h:637
static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS)
Definition: Metadata.h:707
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata *> MDs)
Return a distinct node.
Definition: Metadata.h:1152
bool hasReplaceableUses() const
Whether this contains RAUW support.
Definition: Metadata.h:799
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
Definition: AsmWriter.cpp:4445
BlockVerifier::State From
static bool classof(const Metadata *MD)
Definition: Metadata.h:397
Type * getType() const
Definition: Metadata.h:380
iterator_range< const_op_iterator > operands() const
Definition: Metadata.h:1429
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:361
Constant * getValue() const
Definition: Metadata.h:418
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
static LocalAsMetadata * getIfExists(Value *Local)
Definition: Metadata.h:440
~Metadata()=default
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:644
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:670
static LocalAsMetadata * getLocalIfExists(Value *Local)
Definition: Metadata.h:375
void reset()
Definition: Metadata.h:735
A range adaptor for a pair of iterators.
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition: Metadata.h:246
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition: Metadata.h:565
T * operator[](unsigned I) const
Definition: Metadata.h:1261
loop extract
iterator begin() const
Definition: StringRef.h:122
std::enable_if< detail::IsValidReference< X, Y & >::value, bool >::type hasa(Y &MD)
Definition: Metadata.h:540
bool operator==(const AAMDNodes &A) const
Definition: Metadata.h:649
Pointer to the context, with optional RAUW support.
Definition: Metadata.h:778
MDOperand * mutable_begin()
Definition: Metadata.h:901
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type dyn_extract_or_null(Y &&MD)
Extract a Value from Metadata, if any, allowing null.
Definition: Metadata.h:591
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:2026
LLVMContext & getContext() const
Definition: Metadata.h:297
const unsigned char * bytes_begin() const
Definition: StringRef.h:126
DistinctMDOperandPlaceholder(unsigned ID)
Definition: Metadata.h:1295
static ValueLatticeElement intersect(const ValueLatticeElement &A, const ValueLatticeElement &B)
Combine two sets of facts about the same value into a single set of facts.
static AAMDNodes getTombstoneKey()
Definition: Metadata.h:695
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition: Metadata.h:392
static SimpleType getSimplifiedValue(const MDOperand &MD)
Definition: Metadata.h:771
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
void dump() const
User-friendly dump.
Definition: AsmWriter.cpp:4488
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, typename std::enable_if< std::is_convertible< U *, T *>::value >::type *=nullptr)
Definition: Metadata.h:1238
int is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:184
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
uint32_t Size
Definition: Profile.cpp:46
void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition: Metadata.cpp:232
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
op_iterator op_begin()
Definition: Metadata.h:1418
void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
Definition: AsmWriter.cpp:4435
struct LLVMOpaqueMetadata * LLVMMetadataRef
Represents an LLVM Metadata.
Definition: Types.h:89
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ReplaceableMetadataImpl * getOrCreateReplaceableUses()
Ensure that this has RAUW support, and then return it.
Definition: Metadata.h:816
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:74
void reset(Metadata *MD, Metadata *Owner)
Definition: Metadata.h:739
static unsigned getHashValue(const AAMDNodes &Val)
Definition: Metadata.h:700
Metadata(unsigned ID, StorageType Storage)
Definition: Metadata.h:84
AAMDNodes intersect(const AAMDNodes &Other)
Given two sets of AAMDNodes that apply to the same pointer, give the best AAMDNodes that are compatib...
Definition: Metadata.h:677
Metadata * get() const
Definition: Metadata.h:730
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool operator==(const TypedMDOperandIterator &X) const
Definition: Metadata.h:1222
bool isTemporary() const
Definition: Metadata.h:952
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
A single uniqued string.
Definition: Metadata.h:604
std::enable_if< detail::IsValidPointer< X, Y >::value, X * >::type dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition: Metadata.h:578
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:2024
MDTuple & operator*() const
Definition: Metadata.h:1256
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1083
static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner)
Track the reference to metadata for MetadataAsValue.
Definition: Metadata.h:239
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition: Metadata.h:664
TypedMDOperandIterator(MDNode::op_iterator I)
Definition: Metadata.h:1207
ReplaceableMetadataImpl * getReplaceableUses() const
Definition: Metadata.h:809
iterator end() const
Definition: StringRef.h:124
void operator()(MDNode *Node) const
Definition: Metadata.h:1191
Root of the metadata hierarchy.
Definition: Metadata.h:58
#define T1
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1400
bool operator!=(const TypedMDOperandIterator &X) const
Definition: Metadata.h:1223
StringMapEntryStorage - Holds the value in a StringMapEntry.
Definition: StringMap.h:127
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:156