Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
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"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/ilist_node.h"
26#include "llvm/IR/Constant.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Value.h"
33#include <cassert>
34#include <cstddef>
35#include <cstdint>
36#include <iterator>
37#include <memory>
38#include <string>
39#include <type_traits>
40#include <utility>
41
42namespace llvm {
43
44enum class CaptureComponents : uint8_t;
45class Module;
47class raw_ostream;
49template <typename T> class StringMapEntry;
50template <typename ValueTy> class StringMapEntryStorage;
51class Type;
52
54 DEBUG_METADATA_VERSION = 3 // Current debug info version number.
55};
56
57/// Magic number in the value profile metadata showing a target has been
58/// promoted for the instruction and shouldn't be promoted again.
60
61/// Root of the metadata hierarchy.
62///
63/// This is a root class for typeless data in the IR.
64class Metadata {
66
67 /// RTTI.
68 const unsigned char SubclassID;
69
70protected:
71 /// Active type of storage.
73
74 /// Storage flag for non-uniqued, otherwise unowned, metadata.
75 unsigned char Storage : 7;
76
77 unsigned char SubclassData1 : 1;
78 unsigned short SubclassData16 = 0;
79 unsigned SubclassData32 = 0;
80
81public:
83#define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind,
84#include "llvm/IR/Metadata.def"
85 };
86
87protected:
89 : SubclassID(ID), Storage(Storage), SubclassData1(false) {
90 static_assert(sizeof(*this) == 8, "Metadata fields poorly packed");
91 }
92
93 ~Metadata() = default;
94
95 /// Default handling of a changed operand, which asserts.
96 ///
97 /// If subclasses pass themselves in as owners to a tracking node reference,
98 /// they must provide an implementation of this method.
100 llvm_unreachable("Unimplemented in Metadata subclass");
101 }
102
103public:
104 unsigned getMetadataID() const { return SubclassID; }
105
106 /// User-friendly dump.
107 ///
108 /// If \c M is provided, metadata nodes will be numbered canonically;
109 /// otherwise, pointer addresses are substituted.
110 ///
111 /// Note: this uses an explicit overload instead of default arguments so that
112 /// the nullptr version is easy to call from a debugger.
113 ///
114 /// @{
115 LLVM_ABI void dump() const;
116 LLVM_ABI void dump(const Module *M) const;
117 /// @}
118
119 /// Print.
120 ///
121 /// Prints definition of \c this.
122 ///
123 /// If \c M is provided, metadata nodes will be numbered canonically;
124 /// otherwise, pointer addresses are substituted.
125 /// @{
126 LLVM_ABI void print(raw_ostream &OS, const Module *M = nullptr,
127 bool IsForDebug = false) const;
129 const Module *M = nullptr, bool IsForDebug = false) const;
130 /// @}
131
132 /// Print as operand.
133 ///
134 /// Prints reference of \c this.
135 ///
136 /// If \c M is provided, metadata nodes will be numbered canonically;
137 /// otherwise, pointer addresses are substituted.
138 /// @{
140 const Module *M = nullptr) const;
142 const Module *M = nullptr) const;
143 /// @}
144
145 /// Metadata IDs that may generate poison.
146 constexpr static const unsigned PoisonGeneratingIDs[] = {
147 LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
148};
149
150// Create wrappers for C Binding types (see CBindingWrapping.h).
152
153// Specialized opaque metadata conversions.
155 return reinterpret_cast<Metadata**>(MDs);
156}
157
158#define HANDLE_METADATA(CLASS) class CLASS;
159#include "llvm/IR/Metadata.def"
160
161// Provide specializations of isa so that we don't need definitions of
162// subclasses to see if the metadata is a subclass.
163#define HANDLE_METADATA_LEAF(CLASS) \
164 template <> struct isa_impl<CLASS, Metadata> { \
165 static inline bool doit(const Metadata &MD) { \
166 return MD.getMetadataID() == Metadata::CLASS##Kind; \
167 } \
168 };
169#include "llvm/IR/Metadata.def"
170
172 MD.print(OS);
173 return OS;
174}
175
176/// Metadata wrapper in the Value hierarchy.
177///
178/// A member of the \a Value hierarchy to represent a reference to metadata.
179/// This allows, e.g., intrinsics to have metadata as operands.
180///
181/// Notably, this is the only thing in either hierarchy that is allowed to
182/// reference \a LocalAsMetadata.
183class MetadataAsValue : public Value {
185 friend class LLVMContextImpl;
186
187 Metadata *MD;
188
189 MetadataAsValue(Type *Ty, Metadata *MD);
190
191 /// Drop use of metadata (during teardown).
192 void dropUse() { MD = nullptr; }
193
194public:
196
197 LLVM_ABI static MetadataAsValue *get(LLVMContext &Context, Metadata *MD);
199 Metadata *MD);
200
201 Metadata *getMetadata() const { return MD; }
202
203 static bool classof(const Value *V) {
204 return V->getValueID() == MetadataAsValueVal;
205 }
206
207private:
208 void handleChangedMetadata(Metadata *MD);
209 void track();
210 void untrack();
211};
212
213/// Base class for tracking ValueAsMetadata/DIArgLists with user lookups and
214/// Owner callbacks outside of ValueAsMetadata.
215///
216/// Currently only inherited by DbgVariableRecord; if other classes need to use
217/// it, then a SubclassID will need to be added (either as a new field or by
218/// making DebugValue into a PointerIntUnion) to discriminate between the
219/// subclasses in lookup and callback handling.
221protected:
222 // Capacity to store 3 debug values.
223 // TODO: Not all DebugValueUser instances need all 3 elements, if we
224 // restructure the DbgVariableRecord class then we can template parameterize
225 // this array size.
226 std::array<Metadata *, 3> DebugValues;
227
229
230public:
232 LLVM_ABI const DbgVariableRecord *getUser() const;
233 /// To be called by ReplaceableMetadataImpl::replaceAllUsesWith, where `Old`
234 /// is a pointer to one of the pointers in `DebugValues` (so should be type
235 /// Metadata**), and `NewDebugValue` is the new Metadata* that is replacing
236 /// *Old.
237 /// For manually replacing elements of DebugValues,
238 /// `resetDebugValue(Idx, NewDebugValue)` should be used instead.
239 LLVM_ABI void handleChangedValue(void *Old, Metadata *NewDebugValue);
240 DebugValueUser() = default;
241 explicit DebugValueUser(std::array<Metadata *, 3> DebugValues)
243 trackDebugValues();
244 }
246 DebugValues = X.DebugValues;
247 retrackDebugValues(X);
248 }
250 DebugValues = X.DebugValues;
251 trackDebugValues();
252 }
253
255 if (&X == this)
256 return *this;
257
258 untrackDebugValues();
259 DebugValues = X.DebugValues;
260 retrackDebugValues(X);
261 return *this;
262 }
263
265 if (&X == this)
266 return *this;
267
268 untrackDebugValues();
269 DebugValues = X.DebugValues;
270 trackDebugValues();
271 return *this;
272 }
273
274 ~DebugValueUser() { untrackDebugValues(); }
275
277 untrackDebugValues();
278 DebugValues.fill(nullptr);
279 }
280
281 void resetDebugValue(size_t Idx, Metadata *DebugValue) {
282 assert(Idx < 3 && "Invalid debug value index.");
283 untrackDebugValue(Idx);
284 DebugValues[Idx] = DebugValue;
285 trackDebugValue(Idx);
286 }
287
288 bool operator==(const DebugValueUser &X) const {
289 return DebugValues == X.DebugValues;
290 }
291 bool operator!=(const DebugValueUser &X) const {
292 return DebugValues != X.DebugValues;
293 }
294
295private:
296 LLVM_ABI void trackDebugValue(size_t Idx);
297 LLVM_ABI void trackDebugValues();
298
299 LLVM_ABI void untrackDebugValue(size_t Idx);
300 LLVM_ABI void untrackDebugValues();
301
302 LLVM_ABI void retrackDebugValues(DebugValueUser &X);
303};
304
305/// API for tracking metadata references through RAUW and deletion.
306///
307/// Shared API for updating \a Metadata pointers in subclasses that support
308/// RAUW.
309///
310/// This API is not meant to be used directly. See \a TrackingMDRef for a
311/// user-friendly tracking reference.
313public:
314 /// Track the reference to metadata.
315 ///
316 /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD
317 /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets
318 /// deleted, \c MD will be set to \c nullptr.
319 ///
320 /// If tracking isn't supported, \c *MD will not change.
321 ///
322 /// \return true iff tracking is supported by \c MD.
323 static bool track(Metadata *&MD) {
324 return track(&MD, *MD, static_cast<Metadata *>(nullptr));
325 }
326
327 /// Track the reference to metadata for \a Metadata.
328 ///
329 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
330 /// tell it that its operand changed. This could trigger \c Owner being
331 /// re-uniqued.
332 static bool track(void *Ref, Metadata &MD, Metadata &Owner) {
333 return track(Ref, MD, &Owner);
334 }
335
336 /// Track the reference to metadata for \a MetadataAsValue.
337 ///
338 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
339 /// tell it that its operand changed. This could trigger \c Owner being
340 /// re-uniqued.
341 static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) {
342 return track(Ref, MD, &Owner);
343 }
344
345 /// Track the reference to metadata for \a DebugValueUser.
346 ///
347 /// As \a track(Metadata*&), but with support for calling back to \c Owner to
348 /// tell it that its operand changed. This could trigger \c Owner being
349 /// re-uniqued.
350 static bool track(void *Ref, Metadata &MD, DebugValueUser &Owner) {
351 return track(Ref, MD, &Owner);
352 }
353
354 /// Stop tracking a reference to metadata.
355 ///
356 /// Stops \c *MD from tracking \c MD.
357 static void untrack(Metadata *&MD) { untrack(&MD, *MD); }
358 LLVM_ABI static void untrack(void *Ref, Metadata &MD);
359
360 /// Move tracking from one reference to another.
361 ///
362 /// Semantically equivalent to \c untrack(MD) followed by \c track(New),
363 /// except that ownership callbacks are maintained.
364 ///
365 /// Note: it is an error if \c *MD does not equal \c New.
366 ///
367 /// \return true iff tracking is supported by \c MD.
368 static bool retrack(Metadata *&MD, Metadata *&New) {
369 return retrack(&MD, *MD, &New);
370 }
371 LLVM_ABI static bool retrack(void *Ref, Metadata &MD, void *New);
372
373 /// Check whether metadata is replaceable.
374 LLVM_ABI static bool isReplaceable(const Metadata &MD);
375
377
378private:
379 /// Track a reference to metadata for an owner.
380 ///
381 /// Generalized version of tracking.
382 LLVM_ABI static bool track(void *Ref, Metadata &MD, OwnerTy Owner);
383};
384
385/// Shared implementation of use-lists for replaceable metadata.
386///
387/// Most metadata cannot be RAUW'ed. This is a shared implementation of
388/// use-lists and associated API for the three that support it (
389/// \a ValueAsMetadata, \a TempMDNode, and \a DIArgList).
391 friend class MetadataTracking;
392
393public:
395
396private:
397 LLVMContext &Context;
398 uint64_t NextIndex = 0;
400
401public:
402 ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {}
403
405 assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata");
406 }
407
408 LLVMContext &getContext() const { return Context; }
409
410 /// Replace all uses of this with MD.
411 ///
412 /// Replace all uses of this with \c MD, which is allowed to be null.
414 /// Replace all uses of the constant with Undef in debug info metadata
415 LLVM_ABI static void SalvageDebugInfo(const Constant &C);
416 /// Returns the list of all DIArgList users of this.
418 /// Returns the list of all DbgVariableRecord users of this.
420
421 /// Resolve all uses of this.
422 ///
423 /// Resolve all uses of this, turning off RAUW permanently. If \c
424 /// ResolveUsers, call \a MDNode::resolve() on any users whose last operand
425 /// is resolved.
426 LLVM_ABI void resolveAllUses(bool ResolveUsers = true);
427
428 unsigned getNumUses() const { return UseMap.size(); }
429
430private:
431 void addRef(void *Ref, OwnerTy Owner);
432 void dropRef(void *Ref);
433 void moveRef(void *Ref, void *New, const Metadata &MD);
434
435 /// Lazily construct RAUW support on MD.
436 ///
437 /// If this is an unresolved MDNode, RAUW support will be created on-demand.
438 /// ValueAsMetadata always has RAUW support.
439 static ReplaceableMetadataImpl *getOrCreate(Metadata &MD);
440
441 /// Get RAUW support on MD, if it exists.
442 static ReplaceableMetadataImpl *getIfExists(Metadata &MD);
443
444 /// Check whether this node will support RAUW.
445 ///
446 /// Returns \c true unless getOrCreate() would return null.
447 static bool isReplaceable(const Metadata &MD);
448};
449
450/// Value wrapper in the Metadata hierarchy.
451///
452/// This is a custom value handle that allows other metadata to refer to
453/// classes in the Value hierarchy.
454///
455/// Because of full uniquing support, each value is only wrapped by a single \a
456/// ValueAsMetadata object, so the lookup maps are far more efficient than
457/// those using ValueHandleBase.
460 friend class LLVMContextImpl;
461
462 Value *V;
463
464 /// Drop users without RAUW (during teardown).
465 void dropUsers() {
466 ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false);
467 }
468
469protected:
472 assert(V && "Expected valid value");
473 }
474
475 ~ValueAsMetadata() = default;
476
477public:
478 LLVM_ABI static ValueAsMetadata *get(Value *V);
479
483
487
489
493
497
498 Value *getValue() const { return V; }
499 Type *getType() const { return V->getType(); }
500 LLVMContext &getContext() const { return V->getContext(); }
501
508
509 LLVM_ABI static void handleDeletion(Value *V);
510 LLVM_ABI static void handleRAUW(Value *From, Value *To);
511
512protected:
513 /// Handle collisions after \a Value::replaceAllUsesWith().
514 ///
515 /// RAUW isn't supported directly for \a ValueAsMetadata, but if the wrapped
516 /// \a Value gets RAUW'ed and the target already exists, this is used to
517 /// merge the two metadata nodes.
521
522public:
523 static bool classof(const Metadata *MD) {
524 return MD->getMetadataID() == LocalAsMetadataKind ||
525 MD->getMetadataID() == ConstantAsMetadataKind;
526 }
527};
528
529class ConstantAsMetadata : public ValueAsMetadata {
530 friend class ValueAsMetadata;
531
532 ConstantAsMetadata(Constant *C)
533 : ValueAsMetadata(ConstantAsMetadataKind, C) {}
534
535public:
536 static ConstantAsMetadata *get(Constant *C) {
538 }
539
540 static ConstantAsMetadata *getIfExists(Constant *C) {
542 }
543
547
548 static bool classof(const Metadata *MD) {
549 return MD->getMetadataID() == ConstantAsMetadataKind;
550 }
551};
552
553class LocalAsMetadata : public ValueAsMetadata {
554 friend class ValueAsMetadata;
555
556 LocalAsMetadata(Value *Local)
557 : ValueAsMetadata(LocalAsMetadataKind, Local) {
558 assert(!isa<Constant>(Local) && "Expected local value");
559 }
560
561public:
562 static LocalAsMetadata *get(Value *Local) {
564 }
565
566 static LocalAsMetadata *getIfExists(Value *Local) {
568 }
569
570 static bool classof(const Metadata *MD) {
571 return MD->getMetadataID() == LocalAsMetadataKind;
572 }
573};
574
575/// Transitional API for extracting constants from Metadata.
576///
577/// This namespace contains transitional functions for metadata that points to
578/// \a Constants.
579///
580/// In prehistory -- when metadata was a subclass of \a Value -- \a MDNode
581/// operands could refer to any \a Value. There's was a lot of code like this:
582///
583/// \code
584/// MDNode *N = ...;
585/// auto *CI = dyn_cast<ConstantInt>(N->getOperand(2));
586/// \endcode
587///
588/// Now that \a Value and \a Metadata are in separate hierarchies, maintaining
589/// the semantics for \a isa(), \a cast(), \a dyn_cast() (etc.) requires three
590/// steps: cast in the \a Metadata hierarchy, extraction of the \a Value, and
591/// cast in the \a Value hierarchy. Besides creating boiler-plate, this
592/// requires subtle control flow changes.
593///
594/// The end-goal is to create a new type of metadata, called (e.g.) \a MDInt,
595/// so that metadata can refer to numbers without traversing a bridge to the \a
596/// Value hierarchy. In this final state, the code above would look like this:
597///
598/// \code
599/// MDNode *N = ...;
600/// auto *MI = dyn_cast<MDInt>(N->getOperand(2));
601/// \endcode
602///
603/// The API in this namespace supports the transition. \a MDInt doesn't exist
604/// yet, and even once it does, changing each metadata schema to use it is its
605/// own mini-project. In the meantime this API prevents us from introducing
606/// complex and bug-prone control flow that will disappear in the end. In
607/// particular, the above code looks like this:
608///
609/// \code
610/// MDNode *N = ...;
611/// auto *CI = mdconst::dyn_extract<ConstantInt>(N->getOperand(2));
612/// \endcode
613///
614/// The full set of provided functions includes:
615///
616/// mdconst::hasa <=> isa
617/// mdconst::extract <=> cast
618/// mdconst::extract_or_null <=> cast_or_null
619/// mdconst::dyn_extract <=> dyn_cast
620/// mdconst::dyn_extract_or_null <=> dyn_cast_or_null
621///
622/// The target of the cast must be a subclass of \a Constant.
623namespace mdconst {
624
625namespace detail {
626template <typename U, typename V>
627using check_has_dereference = decltype(static_cast<V>(*std::declval<U &>()));
628
629template <typename U, typename V>
630static constexpr bool HasDereference =
632
633template <class V, class M> struct IsValidPointer {
634 static const bool value = std::is_base_of<Constant, V>::value &&
636};
637template <class V, class M> struct IsValidReference {
638 static const bool value = std::is_base_of<Constant, V>::value &&
639 std::is_convertible<M, const Metadata &>::value;
640};
641
642} // end namespace detail
643
644/// Check whether Metadata has a Value.
645///
646/// As an analogue to \a isa(), check whether \c MD has an \a Value inside of
647/// type \c X.
648template <class X, class Y>
649inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, bool>
650hasa(Y &&MD) {
651 assert(MD && "Null pointer sent into hasa");
652 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
653 return isa<X>(V->getValue());
654 return false;
655}
656template <class X, class Y>
657inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, bool>
658hasa(Y &MD) {
659 return hasa(&MD);
660}
661
662/// Extract a Value from Metadata.
663///
664/// As an analogue to \a cast(), extract the \a Value subclass \c X from \c MD.
665template <class X, class Y>
666inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
667extract(Y &&MD) {
668 return cast<X>(cast<ConstantAsMetadata>(MD)->getValue());
669}
670template <class X, class Y>
671inline std::enable_if_t<detail::IsValidReference<X, Y &>::value, X *>
672extract(Y &MD) {
673 return extract(&MD);
674}
675
676/// Extract a Value from Metadata, allowing null.
677///
678/// As an analogue to \a cast_or_null(), extract the \a Value subclass \c X
679/// from \c MD, allowing \c MD to be null.
680template <class X, class Y>
681inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
683 if (auto *V = cast_or_null<ConstantAsMetadata>(MD))
684 return cast<X>(V->getValue());
685 return nullptr;
686}
687
688/// Extract a Value from Metadata, if any.
689///
690/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
691/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
692/// Value it does contain is of the wrong subclass.
693template <class X, class Y>
694inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
696 if (auto *V = dyn_cast<ConstantAsMetadata>(MD))
697 return dyn_cast<X>(V->getValue());
698 return nullptr;
699}
700
701/// Extract a Value from Metadata, if any, allowing null.
702///
703/// As an analogue to \a dyn_cast_or_null(), extract the \a Value subclass \c X
704/// from \c MD, return null if \c MD doesn't contain a \a Value or if the \a
705/// Value it does contain is of the wrong subclass, allowing \c MD to be null.
706template <class X, class Y>
707inline std::enable_if_t<detail::IsValidPointer<X, Y>::value, X *>
709 if (auto *V = dyn_cast_or_null<ConstantAsMetadata>(MD))
710 return dyn_cast<X>(V->getValue());
711 return nullptr;
712}
713
714} // end namespace mdconst
715
716//===----------------------------------------------------------------------===//
717/// A single uniqued string.
718///
719/// These are used to efficiently contain a byte sequence for metadata.
720/// MDString is always unnamed.
721class MDString : public Metadata {
722 friend class StringMapEntryStorage<MDString>;
723
724 StringMapEntry<MDString> *Entry = nullptr;
725
726 MDString() : Metadata(MDStringKind, Uniqued) {}
727
728public:
729 MDString(const MDString &) = delete;
730 MDString &operator=(MDString &&) = delete;
731 MDString &operator=(const MDString &) = delete;
732
733 LLVM_ABI static MDString *get(LLVMContext &Context, StringRef Str);
734 static MDString *get(LLVMContext &Context, const char *Str) {
735 return get(Context, Str ? StringRef(Str) : StringRef());
736 }
737
739
740 unsigned getLength() const { return (unsigned)getString().size(); }
741
743
744 /// Pointer to the first byte of the string.
745 iterator begin() const { return getString().begin(); }
746
747 /// Pointer to one byte past the end of the string.
748 iterator end() const { return getString().end(); }
749
750 const unsigned char *bytes_begin() const { return getString().bytes_begin(); }
751 const unsigned char *bytes_end() const { return getString().bytes_end(); }
752
753 /// Methods for support type inquiry through isa, cast, and dyn_cast.
754 static bool classof(const Metadata *MD) {
755 return MD->getMetadataID() == MDStringKind;
756 }
757};
758
759/// A collection of metadata nodes that might be associated with a
760/// memory access used by the alias-analysis infrastructure.
761struct AAMDNodes {
762 explicit AAMDNodes() = default;
763 explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N, MDNode *NAS)
764 : TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N), NoAliasAddrSpace(NAS) {}
765
766 bool operator==(const AAMDNodes &A) const {
767 return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
768 NoAlias == A.NoAlias && NoAliasAddrSpace == A.NoAliasAddrSpace;
769 }
770
771 bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
772
773 explicit operator bool() const {
774 return TBAA || TBAAStruct || Scope || NoAlias || NoAliasAddrSpace;
775 }
776
777 /// The tag for type-based alias analysis.
778 MDNode *TBAA = nullptr;
779
780 /// The tag for type-based alias analysis (tbaa struct).
781 MDNode *TBAAStruct = nullptr;
782
783 /// The tag for alias scope specification (used with noalias).
784 MDNode *Scope = nullptr;
785
786 /// The tag specifying the noalias scope.
787 MDNode *NoAlias = nullptr;
788
789 /// The tag specifying the noalias address spaces.
791
792 // Shift tbaa Metadata node to start off bytes later
793 LLVM_ABI static MDNode *shiftTBAA(MDNode *M, size_t off);
794
795 // Shift tbaa.struct Metadata node to start off bytes later
796 LLVM_ABI static MDNode *shiftTBAAStruct(MDNode *M, size_t off);
797
798 // Extend tbaa Metadata node to apply to a series of bytes of length len.
799 // A size of -1 denotes an unknown size.
800 LLVM_ABI static MDNode *extendToTBAA(MDNode *TBAA, ssize_t len);
801
802 /// Given two sets of AAMDNodes that apply to the same pointer,
803 /// give the best AAMDNodes that are compatible with both (i.e. a set of
804 /// nodes whose allowable aliasing conclusions are a subset of those
805 /// allowable by both of the inputs). However, for efficiency
806 /// reasons, do not create any new MDNodes.
808 AAMDNodes Result;
809 Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr;
810 Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
811 Result.Scope = Other.Scope == Scope ? Scope : nullptr;
812 Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
813 Result.NoAliasAddrSpace =
814 Other.NoAliasAddrSpace == NoAliasAddrSpace ? NoAliasAddrSpace : nullptr;
815 return Result;
816 }
817
818 /// Create a new AAMDNode that describes this AAMDNode after applying a
819 /// constant offset to the start of the pointer.
820 AAMDNodes shift(size_t Offset) const {
821 AAMDNodes Result;
822 Result.TBAA = TBAA ? shiftTBAA(TBAA, Offset) : nullptr;
823 Result.TBAAStruct =
825 Result.Scope = Scope;
826 Result.NoAlias = NoAlias;
827 Result.NoAliasAddrSpace = NoAliasAddrSpace;
828 return Result;
829 }
830
831 /// Create a new AAMDNode that describes this AAMDNode after extending it to
832 /// apply to a series of bytes of length Len. A size of -1 denotes an unknown
833 /// size.
834 AAMDNodes extendTo(ssize_t Len) const {
835 AAMDNodes Result;
836 Result.TBAA = TBAA ? extendToTBAA(TBAA, Len) : nullptr;
837 // tbaa.struct contains (offset, size, type) triples. Extending the length
838 // of the tbaa.struct doesn't require changing this (though more information
839 // could be provided by adding more triples at subsequent lengths).
840 Result.TBAAStruct = TBAAStruct;
841 Result.Scope = Scope;
842 Result.NoAlias = NoAlias;
843 Result.NoAliasAddrSpace = NoAliasAddrSpace;
844 return Result;
845 }
846
847 /// Given two sets of AAMDNodes applying to potentially different locations,
848 /// determine the best AAMDNodes that apply to both.
849 LLVM_ABI AAMDNodes merge(const AAMDNodes &Other) const;
850
851 /// Determine the best AAMDNodes after concatenating two different locations
852 /// together. Different from `merge`, where different locations should
853 /// overlap each other, `concat` puts non-overlapping locations together.
855
856 /// Create a new AAMDNode for accessing \p AccessSize bytes of this AAMDNode.
857 /// If this AAMDNode has !tbaa.struct and \p AccessSize matches the size of
858 /// the field at offset 0, get the TBAA tag describing the accessed field.
859 /// If such an AAMDNode already embeds !tbaa, the existing one is retrieved.
860 /// Finally, !tbaa.struct is zeroed out.
861 LLVM_ABI AAMDNodes adjustForAccess(unsigned AccessSize);
862 LLVM_ABI AAMDNodes adjustForAccess(size_t Offset, Type *AccessTy,
863 const DataLayout &DL);
864 LLVM_ABI AAMDNodes adjustForAccess(size_t Offset, unsigned AccessSize);
865};
866
867// Specialize DenseMapInfo for AAMDNodes.
868template<>
870 static inline AAMDNodes getEmptyKey() {
871 return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), nullptr, nullptr,
872 nullptr, nullptr);
873 }
874
875 static inline AAMDNodes getTombstoneKey() {
877 nullptr, nullptr, nullptr);
878 }
879
887
888 static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {
889 return LHS == RHS;
890 }
891};
892
893/// Tracking metadata reference owned by Metadata.
894///
895/// Similar to \a TrackingMDRef, but it's expected to be owned by an instance
896/// of \a Metadata, which has the option of registering itself for callbacks to
897/// re-unique itself.
898///
899/// In particular, this is used by \a MDNode.
901 Metadata *MD = nullptr;
902
903public:
904 MDOperand() = default;
905 MDOperand(const MDOperand &) = delete;
907 MD = Op.MD;
908 if (MD)
909 (void)MetadataTracking::retrack(Op.MD, MD);
910 Op.MD = nullptr;
911 }
912 MDOperand &operator=(const MDOperand &) = delete;
914 MD = Op.MD;
915 if (MD)
916 (void)MetadataTracking::retrack(Op.MD, MD);
917 Op.MD = nullptr;
918 return *this;
919 }
920
921 // Check if MDOperand is of type MDString and equals `Str`.
922 bool equalsStr(StringRef Str) const {
923 return isa_and_nonnull<MDString>(get()) &&
924 cast<MDString>(get())->getString() == Str;
925 }
926
927 ~MDOperand() { untrack(); }
928
929 Metadata *get() const { return MD; }
930 operator Metadata *() const { return get(); }
931 Metadata *operator->() const { return get(); }
932 Metadata &operator*() const { return *get(); }
933
934 void reset() {
935 untrack();
936 MD = nullptr;
937 }
938 void reset(Metadata *MD, Metadata *Owner) {
939 untrack();
940 this->MD = MD;
941 track(Owner);
942 }
943
944private:
945 void track(Metadata *Owner) {
946 if (MD) {
947 if (Owner)
948 MetadataTracking::track(this, *MD, *Owner);
949 else
951 }
952 }
953
954 void untrack() {
955 assert(static_cast<void *>(this) == &MD && "Expected same address");
956 if (MD)
958 }
959};
960
961template <> struct simplify_type<MDOperand> {
963
964 static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); }
965};
966
967template <> struct simplify_type<const MDOperand> {
969
970 static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); }
971};
972
973/// Pointer to the context, with optional RAUW support.
974///
975/// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer
976/// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext).
979
980public:
981 ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {}
983 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses)
984 : Ptr(ReplaceableUses.release()) {
985 assert(getReplaceableUses() && "Expected non-null replaceable uses");
986 }
994
995 operator LLVMContext &() { return getContext(); }
996
997 /// Whether this contains RAUW support.
998 bool hasReplaceableUses() const {
1000 }
1001
1003 if (hasReplaceableUses())
1004 return getReplaceableUses()->getContext();
1005 return *cast<LLVMContext *>(Ptr);
1006 }
1007
1009 if (hasReplaceableUses())
1011 return nullptr;
1012 }
1013
1014 /// Ensure that this has RAUW support, and then return it.
1016 if (!hasReplaceableUses())
1017 makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext()));
1018 return getReplaceableUses();
1019 }
1020
1021 /// Assign RAUW support to this.
1022 ///
1023 /// Make this replaceable, taking ownership of \c ReplaceableUses (which must
1024 /// not be null).
1025 void
1026 makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) {
1027 assert(ReplaceableUses && "Expected non-null replaceable uses");
1028 assert(&ReplaceableUses->getContext() == &getContext() &&
1029 "Expected same context");
1030 delete getReplaceableUses();
1031 Ptr = ReplaceableUses.release();
1032 }
1033
1034 /// Drop RAUW support.
1035 ///
1036 /// Cede ownership of RAUW support, returning it.
1037 std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() {
1038 assert(hasReplaceableUses() && "Expected to own replaceable uses");
1039 std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses(
1041 Ptr = &ReplaceableUses->getContext();
1042 return ReplaceableUses;
1043 }
1044};
1045
1047 inline void operator()(MDNode *Node) const;
1048};
1049
1050#define HANDLE_MDNODE_LEAF(CLASS) \
1051 using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>;
1052#define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS)
1053#include "llvm/IR/Metadata.def"
1054
1055/// Metadata node.
1056///
1057/// Metadata nodes can be uniqued, like constants, or distinct. Temporary
1058/// metadata nodes (with full support for RAUW) can be used to delay uniquing
1059/// until forward references are known. The basic metadata node is an \a
1060/// MDTuple.
1061///
1062/// There is limited support for RAUW at construction time. At construction
1063/// time, if any operand is a temporary node (or an unresolved uniqued node,
1064/// which indicates a transitive temporary operand), the node itself will be
1065/// unresolved. As soon as all operands become resolved, it will drop RAUW
1066/// support permanently.
1067///
1068/// If an unresolved node is part of a cycle, \a resolveCycles() needs
1069/// to be called on some member of the cycle once all temporary nodes have been
1070/// replaced.
1071///
1072/// MDNodes can be large or small, as well as resizable or non-resizable.
1073/// Large MDNodes' operands are allocated in a separate storage vector,
1074/// whereas small MDNodes' operands are co-allocated. Distinct and temporary
1075/// MDnodes are resizable, but only MDTuples support this capability.
1076///
1077/// Clients can add operands to resizable MDNodes using push_back().
1078class MDNode : public Metadata {
1080 friend class LLVMContextImpl;
1081 friend class DIAssignID;
1082
1083 /// The header that is coallocated with an MDNode along with its "small"
1084 /// operands. It is located immediately before the main body of the node.
1085 /// The operands are in turn located immediately before the header.
1086 /// For resizable MDNodes, the space for the storage vector is also allocated
1087 /// immediately before the header, overlapping with the operands.
1088 /// Explicity set alignment because bitfields by default have an
1089 /// alignment of 1 on z/OS.
1090 struct alignas(alignof(size_t)) Header {
1091 size_t IsResizable : 1;
1092 size_t IsLarge : 1;
1093 size_t SmallSize : 4;
1094 size_t SmallNumOps : 4;
1095 size_t : sizeof(size_t) * CHAR_BIT - 10;
1096
1097 unsigned NumUnresolved = 0;
1098 using LargeStorageVector = SmallVector<MDOperand, 0>;
1099
1100 static constexpr size_t NumOpsFitInVector =
1101 sizeof(LargeStorageVector) / sizeof(MDOperand);
1102 static_assert(
1103 NumOpsFitInVector * sizeof(MDOperand) == sizeof(LargeStorageVector),
1104 "sizeof(LargeStorageVector) must be a multiple of sizeof(MDOperand)");
1105
1106 static constexpr size_t MaxSmallSize = 15;
1107
1108 static constexpr size_t getOpSize(unsigned NumOps) {
1109 return sizeof(MDOperand) * NumOps;
1110 }
1111 /// Returns the number of operands the node has space for based on its
1112 /// allocation characteristics.
1113 static size_t getSmallSize(size_t NumOps, bool IsResizable, bool IsLarge) {
1114 return IsLarge ? NumOpsFitInVector
1115 : std::max(NumOps, NumOpsFitInVector * IsResizable);
1116 }
1117 /// Returns the number of bytes allocated for operands and header.
1118 static size_t getAllocSize(StorageType Storage, size_t NumOps) {
1119 return getOpSize(
1120 getSmallSize(NumOps, isResizable(Storage), isLarge(NumOps))) +
1121 sizeof(Header);
1122 }
1123
1124 /// Only temporary and distinct nodes are resizable.
1125 static bool isResizable(StorageType Storage) { return Storage != Uniqued; }
1126 static bool isLarge(size_t NumOps) { return NumOps > MaxSmallSize; }
1127
1128 size_t getAllocSize() const {
1129 return getOpSize(SmallSize) + sizeof(Header);
1130 }
1131 void *getAllocation() {
1132 return reinterpret_cast<char *>(this + 1) -
1133 alignTo(getAllocSize(), alignof(uint64_t));
1134 }
1135
1136 void *getLargePtr() const {
1137 static_assert(alignof(LargeStorageVector) <= alignof(Header),
1138 "LargeStorageVector too strongly aligned");
1139 return reinterpret_cast<char *>(const_cast<Header *>(this)) -
1140 sizeof(LargeStorageVector);
1141 }
1142
1143 LLVM_ABI void *getSmallPtr();
1144
1145 LargeStorageVector &getLarge() {
1146 assert(IsLarge);
1147 return *reinterpret_cast<LargeStorageVector *>(getLargePtr());
1148 }
1149
1150 const LargeStorageVector &getLarge() const {
1151 assert(IsLarge);
1152 return *reinterpret_cast<const LargeStorageVector *>(getLargePtr());
1153 }
1154
1155 LLVM_ABI void resizeSmall(size_t NumOps);
1156 LLVM_ABI void resizeSmallToLarge(size_t NumOps);
1157 LLVM_ABI void resize(size_t NumOps);
1158
1159 LLVM_ABI explicit Header(size_t NumOps, StorageType Storage);
1160 LLVM_ABI ~Header();
1161
1163 if (IsLarge)
1164 return getLarge();
1165 return MutableArrayRef(
1166 reinterpret_cast<MDOperand *>(this) - SmallSize, SmallNumOps);
1167 }
1168
1170 if (IsLarge)
1171 return getLarge();
1172 return ArrayRef(reinterpret_cast<const MDOperand *>(this) - SmallSize,
1173 SmallNumOps);
1174 }
1175
1176 unsigned getNumOperands() const {
1177 if (!IsLarge)
1178 return SmallNumOps;
1179 return getLarge().size();
1180 }
1181 };
1182
1183 Header &getHeader() { return *(reinterpret_cast<Header *>(this) - 1); }
1184
1185 const Header &getHeader() const {
1186 return *(reinterpret_cast<const Header *>(this) - 1);
1187 }
1188
1189 ContextAndReplaceableUses Context;
1190
1191protected:
1192 LLVM_ABI MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
1194 ~MDNode() = default;
1195
1196 LLVM_ABI void *operator new(size_t Size, size_t NumOps, StorageType Storage);
1197 LLVM_ABI void operator delete(void *Mem);
1198
1199 /// Required by std, but never called.
1200 void operator delete(void *, unsigned) {
1201 llvm_unreachable("Constructor throws?");
1202 }
1203
1204 /// Required by std, but never called.
1205 void operator delete(void *, unsigned, bool) {
1206 llvm_unreachable("Constructor throws?");
1207 }
1208
1210
1211 MDOperand *mutable_begin() { return getHeader().operands().begin(); }
1212 MDOperand *mutable_end() { return getHeader().operands().end(); }
1213
1215
1219
1220public:
1221 MDNode(const MDNode &) = delete;
1222 void operator=(const MDNode &) = delete;
1223 void *operator new(size_t) = delete;
1224
1225 static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs);
1226 static inline MDTuple *getIfExists(LLVMContext &Context,
1228 static inline MDTuple *getDistinct(LLVMContext &Context,
1230 static inline TempMDTuple getTemporary(LLVMContext &Context,
1232
1233 /// Create a (temporary) clone of this.
1234 LLVM_ABI TempMDNode clone() const;
1235
1236 /// Deallocate a node created by getTemporary.
1237 ///
1238 /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
1239 /// references will be reset.
1240 LLVM_ABI static void deleteTemporary(MDNode *N);
1241
1242 LLVMContext &getContext() const { return Context.getContext(); }
1243
1244 /// Replace a specific operand.
1245 LLVM_ABI void replaceOperandWith(unsigned I, Metadata *New);
1246
1247 /// Check if node is fully resolved.
1248 ///
1249 /// If \a isTemporary(), this always returns \c false; if \a isDistinct(),
1250 /// this always returns \c true.
1251 ///
1252 /// If \a isUniqued(), returns \c true if this has already dropped RAUW
1253 /// support (because all operands are resolved).
1254 ///
1255 /// As forward declarations are resolved, their containers should get
1256 /// resolved automatically. However, if this (or one of its operands) is
1257 /// involved in a cycle, \a resolveCycles() needs to be called explicitly.
1258 bool isResolved() const { return !isTemporary() && !getNumUnresolved(); }
1259
1260 bool isUniqued() const { return Storage == Uniqued; }
1261 bool isDistinct() const { return Storage == Distinct; }
1262 bool isTemporary() const { return Storage == Temporary; }
1263
1264 bool isReplaceable() const { return isTemporary() || isAlwaysReplaceable(); }
1265 bool isAlwaysReplaceable() const { return getMetadataID() == DIAssignIDKind; }
1266
1267 /// Check if this is a valid generalized type metadata node.
1269 if (getNumOperands() < 2 || !isa<MDString>(getOperand(1)))
1270 return false;
1271 return cast<MDString>(getOperand(1))->getString().ends_with(".generalized");
1272 }
1273
1274 unsigned getNumTemporaryUses() const {
1275 assert(isTemporary() && "Only for temporaries");
1276 return Context.getReplaceableUses()->getNumUses();
1277 }
1278
1279 /// RAUW a temporary.
1280 ///
1281 /// \pre \a isTemporary() must be \c true.
1283 assert(isReplaceable() && "Expected temporary/replaceable node");
1284 if (Context.hasReplaceableUses())
1285 Context.getReplaceableUses()->replaceAllUsesWith(MD);
1286 }
1287
1288 /// Resolve cycles.
1289 ///
1290 /// Once all forward declarations have been resolved, force cycles to be
1291 /// resolved.
1292 ///
1293 /// \pre No operands (or operands' operands, etc.) have \a isTemporary().
1294 LLVM_ABI void resolveCycles();
1295
1296 /// Resolve a unique, unresolved node.
1297 LLVM_ABI void resolve();
1298
1299 /// Replace a temporary node with a permanent one.
1300 ///
1301 /// Try to create a uniqued version of \c N -- in place, if possible -- and
1302 /// return it. If \c N cannot be uniqued, return a distinct node instead.
1303 template <class T>
1304 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1305 replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) {
1306 return cast<T>(N.release()->replaceWithPermanentImpl());
1307 }
1308
1309 /// Replace a temporary node with a uniqued one.
1310 ///
1311 /// Create a uniqued version of \c N -- in place, if possible -- and return
1312 /// it. Takes ownership of the temporary node.
1313 ///
1314 /// \pre N does not self-reference.
1315 template <class T>
1316 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1317 replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) {
1318 return cast<T>(N.release()->replaceWithUniquedImpl());
1319 }
1320
1321 /// Replace a temporary node with a distinct one.
1322 ///
1323 /// Create a distinct version of \c N -- in place, if possible -- and return
1324 /// it. Takes ownership of the temporary node.
1325 template <class T>
1326 static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *>
1327 replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) {
1328 return cast<T>(N.release()->replaceWithDistinctImpl());
1329 }
1330
1331 /// Print in tree shape.
1332 ///
1333 /// Prints definition of \c this in tree shape.
1334 ///
1335 /// If \c M is provided, metadata nodes will be numbered canonically;
1336 /// otherwise, pointer addresses are substituted.
1337 /// @{
1338 LLVM_ABI void printTree(raw_ostream &OS, const Module *M = nullptr) const;
1340 const Module *M = nullptr) const;
1341 /// @}
1342
1343 /// User-friendly dump in tree shape.
1344 ///
1345 /// If \c M is provided, metadata nodes will be numbered canonically;
1346 /// otherwise, pointer addresses are substituted.
1347 ///
1348 /// Note: this uses an explicit overload instead of default arguments so that
1349 /// the nullptr version is easy to call from a debugger.
1350 ///
1351 /// @{
1352 LLVM_ABI void dumpTree() const;
1353 LLVM_ABI void dumpTree(const Module *M) const;
1354 /// @}
1355
1356private:
1357 LLVM_ABI MDNode *replaceWithPermanentImpl();
1358 LLVM_ABI MDNode *replaceWithUniquedImpl();
1359 LLVM_ABI MDNode *replaceWithDistinctImpl();
1360
1361protected:
1362 /// Set an operand.
1363 ///
1364 /// Sets the operand directly, without worrying about uniquing.
1365 LLVM_ABI void setOperand(unsigned I, Metadata *New);
1366
1367 unsigned getNumUnresolved() const { return getHeader().NumUnresolved; }
1368
1369 void setNumUnresolved(unsigned N) { getHeader().NumUnresolved = N; }
1371 template <class T, class StoreT>
1372 static T *storeImpl(T *N, StorageType Storage, StoreT &Store);
1373 template <class T> static T *storeImpl(T *N, StorageType Storage);
1374
1375 /// Resize the node to hold \a NumOps operands.
1376 ///
1377 /// \pre \a isTemporary() or \a isDistinct()
1378 /// \pre MetadataID == MDTupleKind
1379 void resize(size_t NumOps) {
1380 assert(!isUniqued() && "Resizing is not supported for uniqued nodes");
1381 assert(getMetadataID() == MDTupleKind &&
1382 "Resizing is not supported for this node kind");
1383 getHeader().resize(NumOps);
1384 }
1385
1386private:
1387 void handleChangedOperand(void *Ref, Metadata *New);
1388
1389 /// Drop RAUW support, if any.
1390 void dropReplaceableUses();
1391
1392 void resolveAfterOperandChange(Metadata *Old, Metadata *New);
1393 void decrementUnresolvedOperandCount();
1394 void countUnresolvedOperands();
1395
1396 /// Mutate this to be "uniqued".
1397 ///
1398 /// Mutate this so that \a isUniqued().
1399 /// \pre \a isTemporary().
1400 /// \pre already added to uniquing set.
1401 void makeUniqued();
1402
1403 /// Mutate this to be "distinct".
1404 ///
1405 /// Mutate this so that \a isDistinct().
1406 /// \pre \a isTemporary().
1407 void makeDistinct();
1408
1409 void deleteAsSubclass();
1410 MDNode *uniquify();
1411 void eraseFromStore();
1412
1413 template <class NodeTy> struct HasCachedHash;
1414 template <class NodeTy> static void dispatchRecalculateHash(NodeTy *N) {
1415 if constexpr (HasCachedHash<NodeTy>::value)
1416 N->recalculateHash();
1417 }
1418 template <class NodeTy> static void dispatchResetHash(NodeTy *N) {
1419 if constexpr (HasCachedHash<NodeTy>::value)
1420 N->setHash(0);
1421 }
1422
1423 /// Merge branch weights from two direct callsites.
1424 static MDNode *mergeDirectCallProfMetadata(MDNode *A, MDNode *B,
1425 const Instruction *AInstr,
1426 const Instruction *BInstr);
1427
1428public:
1429 using op_iterator = const MDOperand *;
1431
1433 return const_cast<MDNode *>(this)->mutable_begin();
1434 }
1435
1437 return const_cast<MDNode *>(this)->mutable_end();
1438 }
1439
1440 ArrayRef<MDOperand> operands() const { return getHeader().operands(); }
1441
1442 const MDOperand &getOperand(unsigned I) const {
1443 assert(I < getNumOperands() && "Out of range");
1444 return getHeader().operands()[I];
1445 }
1446
1447 /// Return number of MDNode operands.
1448 unsigned getNumOperands() const { return getHeader().getNumOperands(); }
1449
1450 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1451 static bool classof(const Metadata *MD) {
1452 switch (MD->getMetadataID()) {
1453 default:
1454 return false;
1455#define HANDLE_MDNODE_LEAF(CLASS) \
1456 case CLASS##Kind: \
1457 return true;
1458#include "llvm/IR/Metadata.def"
1459 }
1460 }
1461
1462 /// Check whether MDNode is a vtable access.
1463 LLVM_ABI bool isTBAAVtableAccess() const;
1464
1465 /// Methods for metadata merging.
1467 LLVM_ABI static MDNode *intersect(MDNode *A, MDNode *B);
1474 MDNode *B);
1475 /// Merge !prof metadata from two instructions.
1476 /// Currently only implemented with direct callsites with branch weights.
1478 const Instruction *AInstr,
1479 const Instruction *BInstr);
1483 const MDNode *B);
1484
1485 /// Convert !captures metadata to CaptureComponents. MD may be nullptr.
1487 /// Convert CaptureComponents to !captures metadata. The return value may be
1488 /// nullptr.
1491};
1492
1493/// Tuple of metadata.
1494///
1495/// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by
1496/// default based on their operands.
1497class MDTuple : public MDNode {
1498 friend class LLVMContextImpl;
1499 friend class MDNode;
1500
1501 MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash,
1503 : MDNode(C, MDTupleKind, Storage, Vals) {
1504 setHash(Hash);
1505 }
1506
1508
1509 void setHash(unsigned Hash) { SubclassData32 = Hash; }
1510 void recalculateHash();
1511
1512 LLVM_ABI static MDTuple *getImpl(LLVMContext &Context,
1515 bool ShouldCreate = true);
1516
1517 TempMDTuple cloneImpl() const {
1520 }
1521
1522public:
1523 /// Get the hash, if any.
1524 unsigned getHash() const { return SubclassData32; }
1525
1526 static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1527 return getImpl(Context, MDs, Uniqued);
1528 }
1529
1530 static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1531 return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false);
1532 }
1533
1534 /// Return a distinct node.
1535 ///
1536 /// Return a distinct node -- i.e., a node that is not uniqued.
1537 static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) {
1538 return getImpl(Context, MDs, Distinct);
1539 }
1540
1541 /// Return a temporary node.
1542 ///
1543 /// For use in constructing cyclic MDNode structures. A temporary MDNode is
1544 /// not uniqued, may be RAUW'd, and must be manually deleted with
1545 /// deleteTemporary.
1546 static TempMDTuple getTemporary(LLVMContext &Context,
1548 return TempMDTuple(getImpl(Context, MDs, Temporary));
1549 }
1550
1551 /// Return a (temporary) clone of this.
1552 TempMDTuple clone() const { return cloneImpl(); }
1553
1554 /// Append an element to the tuple. This will resize the node.
1556 size_t NumOps = getNumOperands();
1557 resize(NumOps + 1);
1558 setOperand(NumOps, MD);
1559 }
1560
1561 /// Shrink the operands by 1.
1562 void pop_back() { resize(getNumOperands() - 1); }
1563
1564 static bool classof(const Metadata *MD) {
1565 return MD->getMetadataID() == MDTupleKind;
1566 }
1567};
1568
1570 return MDTuple::get(Context, MDs);
1571}
1572
1574 return MDTuple::getIfExists(Context, MDs);
1575}
1576
1578 return MDTuple::getDistinct(Context, MDs);
1579}
1580
1583 return MDTuple::getTemporary(Context, MDs);
1584}
1585
1589
1590/// This is a simple wrapper around an MDNode which provides a higher-level
1591/// interface by hiding the details of how alias analysis information is encoded
1592/// in its operands.
1594 const MDNode *Node = nullptr;
1595
1596public:
1597 AliasScopeNode() = default;
1598 explicit AliasScopeNode(const MDNode *N) : Node(N) {}
1599
1600 /// Get the MDNode for this AliasScopeNode.
1601 const MDNode *getNode() const { return Node; }
1602
1603 /// Get the MDNode for this AliasScopeNode's domain.
1604 const MDNode *getDomain() const {
1605 if (Node->getNumOperands() < 2)
1606 return nullptr;
1607 return dyn_cast_or_null<MDNode>(Node->getOperand(1));
1608 }
1610 if (Node->getNumOperands() > 2)
1611 if (MDString *N = dyn_cast_or_null<MDString>(Node->getOperand(2)))
1612 return N->getString();
1613 return StringRef();
1614 }
1615};
1616
1617/// Typed iterator through MDNode operands.
1618///
1619/// An iterator that transforms an \a MDNode::iterator into an iterator over a
1620/// particular Metadata subclass.
1621template <class T> class TypedMDOperandIterator {
1622 MDNode::op_iterator I = nullptr;
1623
1624public:
1625 using iterator_category = std::input_iterator_tag;
1626 using value_type = T *;
1627 using difference_type = std::ptrdiff_t;
1628 using pointer = void;
1629 using reference = T *;
1630
1633
1634 T *operator*() const { return cast_or_null<T>(*I); }
1635
1637 ++I;
1638 return *this;
1639 }
1640
1642 TypedMDOperandIterator Temp(*this);
1643 ++I;
1644 return Temp;
1645 }
1646
1647 bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
1648 bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
1649};
1650
1651/// Typed, array-like tuple of metadata.
1652///
1653/// This is a wrapper for \a MDTuple that makes it act like an array holding a
1654/// particular type of metadata.
1655template <class T> class MDTupleTypedArrayWrapper {
1656 const MDTuple *N = nullptr;
1657
1658public:
1661
1662 template <class U>
1665 std::enable_if_t<std::is_convertible<U *, T *>::value> * = nullptr)
1666 : N(Other.get()) {}
1667
1668 template <class U>
1671 std::enable_if_t<!std::is_convertible<U *, T *>::value> * = nullptr)
1672 : N(Other.get()) {}
1673
1674 explicit operator bool() const { return get(); }
1675 explicit operator MDTuple *() const { return get(); }
1676
1677 MDTuple *get() const { return const_cast<MDTuple *>(N); }
1678 MDTuple *operator->() const { return get(); }
1679 MDTuple &operator*() const { return *get(); }
1680
1681 // FIXME: Fix callers and remove condition on N.
1682 unsigned size() const { return N ? N->getNumOperands() : 0u; }
1683 bool empty() const { return N ? N->getNumOperands() == 0 : true; }
1684 T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
1685
1686 // FIXME: Fix callers and remove condition on N.
1688
1689 iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); }
1690 iterator end() const { return N ? iterator(N->op_end()) : iterator(); }
1691};
1692
1693#define HANDLE_METADATA(CLASS) \
1694 using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>;
1695#include "llvm/IR/Metadata.def"
1696
1697/// Placeholder metadata for operands of distinct MDNodes.
1698///
1699/// This is a lightweight placeholder for an operand of a distinct node. It's
1700/// purpose is to help track forward references when creating a distinct node.
1701/// This allows distinct nodes involved in a cycle to be constructed before
1702/// their operands without requiring a heavyweight temporary node with
1703/// full-blown RAUW support.
1704///
1705/// Each placeholder supports only a single MDNode user. Clients should pass
1706/// an ID, retrieved via \a getID(), to indicate the "real" operand that this
1707/// should be replaced with.
1708///
1709/// While it would be possible to implement move operators, they would be
1710/// fairly expensive. Leave them unimplemented to discourage their use
1711/// (clients can use std::deque, std::list, BumpPtrAllocator, etc.).
1713 friend class MetadataTracking;
1714
1715 Metadata **Use = nullptr;
1716
1717public:
1719 : Metadata(DistinctMDOperandPlaceholderKind, Distinct) {
1721 }
1722
1726
1728 if (Use)
1729 *Use = nullptr;
1730 }
1731
1732 unsigned getID() const { return SubclassData32; }
1733
1734 /// Replace the use of this with MD.
1736 if (!Use)
1737 return;
1738 *Use = MD;
1739
1740 if (*Use)
1742
1743 Metadata *T = cast<Metadata>(this);
1745 assert(!Use && "Use is still being tracked despite being untracked!");
1746 }
1747};
1748
1749//===----------------------------------------------------------------------===//
1750/// A tuple of MDNodes.
1751///
1752/// Despite its name, a NamedMDNode isn't itself an MDNode.
1753///
1754/// NamedMDNodes are named module-level entities that contain lists of MDNodes.
1755///
1756/// It is illegal for a NamedMDNode to appear as an operand of an MDNode.
1757class NamedMDNode : public ilist_node<NamedMDNode> {
1758 friend class LLVMContextImpl;
1759 friend class Module;
1760
1761 std::string Name;
1762 Module *Parent = nullptr;
1763 void *Operands; // SmallVector<TrackingMDRef, 4>
1764
1765 void setParent(Module *M) { Parent = M; }
1766
1767 explicit NamedMDNode(const Twine &N);
1768
1769 template <class T1> class op_iterator_impl {
1770 friend class NamedMDNode;
1771
1772 const NamedMDNode *Node = nullptr;
1773 unsigned Idx = 0;
1774
1775 op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {}
1776
1777 public:
1778 using iterator_category = std::bidirectional_iterator_tag;
1779 using value_type = T1;
1780 using difference_type = std::ptrdiff_t;
1781 using pointer = value_type *;
1782 using reference = value_type;
1783
1784 op_iterator_impl() = default;
1785
1786 bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; }
1787 bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; }
1788
1789 op_iterator_impl &operator++() {
1790 ++Idx;
1791 return *this;
1792 }
1793
1794 op_iterator_impl operator++(int) {
1795 op_iterator_impl tmp(*this);
1796 operator++();
1797 return tmp;
1798 }
1799
1800 op_iterator_impl &operator--() {
1801 --Idx;
1802 return *this;
1803 }
1804
1805 op_iterator_impl operator--(int) {
1806 op_iterator_impl tmp(*this);
1807 operator--();
1808 return tmp;
1809 }
1810
1811 T1 operator*() const { return Node->getOperand(Idx); }
1812 };
1813
1814public:
1815 NamedMDNode(const NamedMDNode &) = delete;
1817
1818 /// Drop all references and remove the node from parent module.
1820
1821 /// Remove all uses and clear node vector.
1823 /// Drop all references to this node's operands.
1824 LLVM_ABI void clearOperands();
1825
1826 /// Get the module that holds this named metadata collection.
1827 inline Module *getParent() { return Parent; }
1828 inline const Module *getParent() const { return Parent; }
1829
1830 LLVM_ABI MDNode *getOperand(unsigned i) const;
1831 LLVM_ABI unsigned getNumOperands() const;
1832 LLVM_ABI void addOperand(MDNode *M);
1833 LLVM_ABI void setOperand(unsigned I, MDNode *New);
1834 LLVM_ABI StringRef getName() const;
1835 LLVM_ABI void print(raw_ostream &ROS, bool IsForDebug = false) const;
1837 bool IsForDebug = false) const;
1838 LLVM_ABI void dump() const;
1839
1840 // ---------------------------------------------------------------------------
1841 // Operand Iterator interface...
1842 //
1843 using op_iterator = op_iterator_impl<MDNode *>;
1844
1845 op_iterator op_begin() { return op_iterator(this, 0); }
1847
1848 using const_op_iterator = op_iterator_impl<const MDNode *>;
1849
1850 const_op_iterator op_begin() const { return const_op_iterator(this, 0); }
1852
1854 return make_range(op_begin(), op_end());
1855 }
1857 return make_range(op_begin(), op_end());
1858 }
1859};
1860
1861// Create wrappers for C Binding types (see CBindingWrapping.h).
1863
1864} // end namespace llvm
1865
1866#endif // LLVM_IR_METADATA_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
always inline
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
#define LLVM_ABI
Definition Compiler.h:213
dxil translate DXIL Translate Metadata
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
loop extract
#define I(x, y, z)
Definition MD5.cpp:58
mir Rename Register Operands
bool operator==(const MergedFunctionsInfo &LHS, const MergedFunctionsInfo &RHS)
#define T
#define T1
This file defines the PointerUnion class, which is a discriminated union of pointer types.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
AliasScopeNode()=default
AliasScopeNode(const MDNode *N)
Definition Metadata.h:1598
const MDNode * getNode() const
Get the MDNode for this AliasScopeNode.
Definition Metadata.h:1601
const MDNode * getDomain() const
Get the MDNode for this AliasScopeNode's domain.
Definition Metadata.h:1604
StringRef getName() const
Definition Metadata.h:1609
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
friend class ValueAsMetadata
Definition Metadata.h:530
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:536
Constant * getValue() const
Definition Metadata.h:544
static ConstantAsMetadata * getIfExists(Constant *C)
Definition Metadata.h:540
static bool classof(const Metadata *MD)
Definition Metadata.h:548
This is an important base class in LLVM.
Definition Constant.h:43
ContextAndReplaceableUses & operator=(const ContextAndReplaceableUses &)=delete
ReplaceableMetadataImpl * getReplaceableUses() const
Definition Metadata.h:1008
std::unique_ptr< ReplaceableMetadataImpl > takeReplaceableUses()
Drop RAUW support.
Definition Metadata.h:1037
ContextAndReplaceableUses & operator=(ContextAndReplaceableUses &&)=delete
ReplaceableMetadataImpl * getOrCreateReplaceableUses()
Ensure that this has RAUW support, and then return it.
Definition Metadata.h:1015
void makeReplaceable(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Assign RAUW support to this.
Definition Metadata.h:1026
ContextAndReplaceableUses(ContextAndReplaceableUses &&)=delete
ContextAndReplaceableUses(const ContextAndReplaceableUses &)=delete
LLVMContext & getContext() const
Definition Metadata.h:1002
bool hasReplaceableUses() const
Whether this contains RAUW support.
Definition Metadata.h:998
ContextAndReplaceableUses(LLVMContext &Context)
Definition Metadata.h:981
ContextAndReplaceableUses(std::unique_ptr< ReplaceableMetadataImpl > ReplaceableUses)
Definition Metadata.h:982
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Base class for tracking ValueAsMetadata/DIArgLists with user lookups and Owner callbacks outside of V...
Definition Metadata.h:220
DebugValueUser(const DebugValueUser &X)
Definition Metadata.h:249
DebugValueUser & operator=(DebugValueUser &&X)
Definition Metadata.h:254
DebugValueUser()=default
LLVM_ABI void handleChangedValue(void *Old, Metadata *NewDebugValue)
To be called by ReplaceableMetadataImpl::replaceAllUsesWith, where Old is a pointer to one of the poi...
Definition Metadata.cpp:159
bool operator!=(const DebugValueUser &X) const
Definition Metadata.h:291
DebugValueUser(std::array< Metadata *, 3 > DebugValues)
Definition Metadata.h:241
bool operator==(const DebugValueUser &X) const
Definition Metadata.h:288
ArrayRef< Metadata * > getDebugValues() const
Definition Metadata.h:228
DebugValueUser & operator=(const DebugValueUser &X)
Definition Metadata.h:264
std::array< Metadata *, 3 > DebugValues
Definition Metadata.h:226
void resetDebugValue(size_t Idx, Metadata *DebugValue)
Definition Metadata.h:281
LLVM_ABI DbgVariableRecord * getUser()
Definition Metadata.cpp:152
DebugValueUser(DebugValueUser &&X)
Definition Metadata.h:245
void replaceUseWith(Metadata *MD)
Replace the use of this with MD.
Definition Metadata.h:1735
DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &)=delete
DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&)=delete
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
friend class ValueAsMetadata
Definition Metadata.h:554
static LocalAsMetadata * getIfExists(Value *Local)
Definition Metadata.h:566
static LocalAsMetadata * get(Value *Local)
Definition Metadata.h:562
static bool classof(const Metadata *MD)
Definition Metadata.h:570
Metadata node.
Definition Metadata.h:1078
friend class DIAssignID
Definition Metadata.h:1081
static LLVM_ABI MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
static LLVM_ABI MDNode * getMergedCallsiteMetadata(MDNode *A, MDNode *B)
LLVM_ABI void printTree(raw_ostream &OS, const Module *M=nullptr) const
Print in tree shape.
LLVM_ABI void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
iterator_range< MDOperand * > mutable_op_range
Definition Metadata.h:1214
LLVM_ABI void resolveCycles()
Resolve cycles.
Definition Metadata.cpp:843
LLVM_ABI bool isTBAAVtableAccess() const
Check whether MDNode is a vtable access.
static LLVM_ABI CaptureComponents toCaptureComponents(const MDNode *MD)
Convert !captures metadata to CaptureComponents. MD may be nullptr.
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1577
mutable_op_range mutable_operands()
Definition Metadata.h:1216
static LLVM_ABI MDNode * getMergedCalleeTypeMetadata(const MDNode *A, const MDNode *B)
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition Metadata.h:1282
static LLVM_ABI MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
static LLVM_ABI void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
LLVM_ABI void resolve()
Resolve a unique, unresolved node.
Definition Metadata.cpp:797
static LLVM_ABI MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
static LLVM_ABI MDNode * getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B)
LLVM_ABI void storeDistinctInContext()
bool isTemporary() const
Definition Metadata.h:1262
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1581
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1440
op_iterator op_end() const
Definition Metadata.h:1436
bool hasGeneralizedMDString()
Check if this is a valid generalized type metadata node.
Definition Metadata.h:1268
MDNode(const MDNode &)=delete
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1569
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithDistinct(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a distinct one.
Definition Metadata.h:1327
static LLVM_ABI MDNode * getMergedProfMetadata(MDNode *A, MDNode *B, const Instruction *AInstr, const Instruction *BInstr)
Merge !prof metadata from two instructions.
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition Metadata.h:1451
friend class ReplaceableMetadataImpl
Definition Metadata.h:1079
bool isUniqued() const
Definition Metadata.h:1260
static LLVM_ABI MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
void setNumUnresolved(unsigned N)
Definition Metadata.h:1369
void resize(size_t NumOps)
Resize the node to hold NumOps operands.
Definition Metadata.h:1379
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
MDOperand * mutable_begin()
Definition Metadata.h:1211
LLVM_ABI MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2={})
Definition Metadata.cpp:652
iterator_range< op_iterator > op_range
Definition Metadata.h:1430
friend class LLVMContextImpl
Definition Metadata.h:1080
LLVM_ABI TempMDNode clone() const
Create a (temporary) clone of this.
Definition Metadata.cpp:669
static LLVM_ABI MDNode * getMostGenericRange(MDNode *A, MDNode *B)
bool isDistinct() const
Definition Metadata.h:1261
unsigned getNumTemporaryUses() const
Definition Metadata.h:1274
static LLVM_ABI MDNode * getMergedMemProfMetadata(MDNode *A, MDNode *B)
bool isReplaceable() const
Definition Metadata.h:1264
LLVM_ABI void setOperand(unsigned I, Metadata *New)
Set an operand.
bool isResolved() const
Check if node is fully resolved.
Definition Metadata.h:1258
op_iterator op_begin() const
Definition Metadata.h:1432
static LLVM_ABI MDNode * intersect(MDNode *A, MDNode *B)
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
LLVMContext & getContext() const
Definition Metadata.h:1242
MDOperand * mutable_end()
Definition Metadata.h:1212
~MDNode()=default
static LLVM_ABI MDNode * fromCaptureComponents(LLVMContext &Ctx, CaptureComponents CC)
Convert CaptureComponents to !captures metadata.
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1573
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithPermanent(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a permanent one.
Definition Metadata.h:1305
LLVM_ABI void dropAllReferences()
Definition Metadata.cpp:909
void operator=(const MDNode &)=delete
const MDOperand * op_iterator
Definition Metadata.h:1429
static std::enable_if_t< std::is_base_of< MDNode, T >::value, T * > replaceWithUniqued(std::unique_ptr< T, TempMDNodeDeleter > N)
Replace a temporary node with a uniqued one.
Definition Metadata.h:1317
LLVM_ABI void dumpTree() const
User-friendly dump in tree shape.
static LLVM_ABI MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
unsigned getNumUnresolved() const
Definition Metadata.h:1367
bool isAlwaysReplaceable() const
Definition Metadata.h:1265
Tracking metadata reference owned by Metadata.
Definition Metadata.h:900
MDOperand()=default
bool equalsStr(StringRef Str) const
Definition Metadata.h:922
void reset(Metadata *MD, Metadata *Owner)
Definition Metadata.h:938
Metadata * operator->() const
Definition Metadata.h:931
MDOperand & operator=(const MDOperand &)=delete
Metadata & operator*() const
Definition Metadata.h:932
Metadata * get() const
Definition Metadata.h:929
MDOperand(const MDOperand &)=delete
MDOperand & operator=(MDOperand &&Op)
Definition Metadata.h:913
MDOperand(MDOperand &&Op)
Definition Metadata.h:906
A single uniqued string.
Definition Metadata.h:721
unsigned getLength() const
Definition Metadata.h:740
const unsigned char * bytes_begin() const
Definition Metadata.h:750
MDString(const MDString &)=delete
static MDString * get(LLVMContext &Context, const char *Str)
Definition Metadata.h:734
MDString & operator=(MDString &&)=delete
static bool classof(const Metadata *MD)
Methods for support type inquiry through isa, cast, and dyn_cast.
Definition Metadata.h:754
const unsigned char * bytes_end() const
Definition Metadata.h:751
iterator begin() const
Pointer to the first byte of the string.
Definition Metadata.h:745
MDString & operator=(const MDString &)=delete
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:618
StringRef::iterator iterator
Definition Metadata.h:742
iterator end() const
Pointer to one byte past the end of the string.
Definition Metadata.h:748
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:608
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, std::enable_if_t<!std::is_convertible< U *, T * >::value > *=nullptr)
Definition Metadata.h:1669
MDTupleTypedArrayWrapper(const MDTuple *N)
Definition Metadata.h:1660
T * operator[](unsigned I) const
Definition Metadata.h:1684
MDTuple * operator->() const
Definition Metadata.h:1678
MDTuple & operator*() const
Definition Metadata.h:1679
MDTupleTypedArrayWrapper(const MDTupleTypedArrayWrapper< U > &Other, std::enable_if_t< std::is_convertible< U *, T * >::value > *=nullptr)
Definition Metadata.h:1663
TypedMDOperandIterator< T > iterator
Definition Metadata.h:1687
Tuple of metadata.
Definition Metadata.h:1497
TempMDTuple clone() const
Return a (temporary) clone of this.
Definition Metadata.h:1552
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition Metadata.h:1537
static bool classof(const Metadata *MD)
Definition Metadata.h:1564
void push_back(Metadata *MD)
Append an element to the tuple. This will resize the node.
Definition Metadata.h:1555
unsigned getHash() const
Get the hash, if any.
Definition Metadata.h:1524
friend class LLVMContextImpl
Definition Metadata.h:1498
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1526
static MDTuple * getIfExists(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1530
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition Metadata.h:1546
friend class MDNode
Definition Metadata.h:1499
void pop_back()
Shrink the operands by 1.
Definition Metadata.h:1562
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:183
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:104
static LLVM_ABI MetadataAsValue * getIfExists(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:112
friend class ReplaceableMetadataImpl
Definition Metadata.h:184
friend class LLVMContextImpl
Definition Metadata.h:185
LLVM_ABI ~MetadataAsValue()
Definition Metadata.cpp:66
static bool classof(const Value *V)
Definition Metadata.h:203
Metadata * getMetadata() const
Definition Metadata.h:201
API for tracking metadata references through RAUW and deletion.
Definition Metadata.h:312
static LLVM_ABI bool isReplaceable(const Metadata &MD)
Check whether metadata is replaceable.
Definition Metadata.cpp:247
static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner)
Track the reference to metadata for MetadataAsValue.
Definition Metadata.h:341
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition Metadata.h:357
PointerUnion< MetadataAsValue *, Metadata *, DebugValueUser * > OwnerTy
Definition Metadata.h:376
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition Metadata.h:368
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition Metadata.h:323
static bool track(void *Ref, Metadata &MD, Metadata &Owner)
Track the reference to metadata for Metadata.
Definition Metadata.h:332
static bool track(void *Ref, Metadata &MD, DebugValueUser &Owner)
Track the reference to metadata for DebugValueUser.
Definition Metadata.h:350
Root of the metadata hierarchy.
Definition Metadata.h:64
void handleChangedOperand(void *, Metadata *)
Default handling of a changed operand, which asserts.
Definition Metadata.h:99
StorageType
Active type of storage.
Definition Metadata.h:72
LLVM_ABI void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
static constexpr const unsigned PoisonGeneratingIDs[]
Metadata IDs that may generate poison.
Definition Metadata.h:146
unsigned short SubclassData16
Definition Metadata.h:78
unsigned SubclassData32
Definition Metadata.h:79
~Metadata()=default
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition Metadata.h:75
friend class ReplaceableMetadataImpl
Definition Metadata.h:65
unsigned getMetadataID() const
Definition Metadata.h:104
unsigned char SubclassData1
Definition Metadata.h:77
LLVM_ABI void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
Metadata(unsigned ID, StorageType Storage)
Definition Metadata.h:88
LLVM_ABI void dump() const
User-friendly dump.
Manage lifetime of a slot tracker for printing IR.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A tuple of MDNodes.
Definition Metadata.h:1757
const_op_iterator op_begin() const
Definition Metadata.h:1850
NamedMDNode(const NamedMDNode &)=delete
op_iterator_impl< const MDNode * > const_op_iterator
Definition Metadata.h:1848
friend class Module
Definition Metadata.h:1759
LLVM_ABI void dump() const
LLVM_ABI void setOperand(unsigned I, MDNode *New)
LLVM_ABI ~NamedMDNode()
LLVM_ABI StringRef getName() const
void dropAllReferences()
Remove all uses and clear node vector.
Definition Metadata.h:1822
LLVM_ABI void print(raw_ostream &ROS, bool IsForDebug=false) const
LLVM_ABI void eraseFromParent()
Drop all references and remove the node from parent module.
const_op_iterator op_end() const
Definition Metadata.h:1851
iterator_range< const_op_iterator > operands() const
Definition Metadata.h:1856
op_iterator op_end()
Definition Metadata.h:1846
LLVM_ABI MDNode * getOperand(unsigned i) const
friend class LLVMContextImpl
Definition Metadata.h:1758
op_iterator op_begin()
Definition Metadata.h:1845
op_iterator_impl< MDNode * > op_iterator
Definition Metadata.h:1843
LLVM_ABI unsigned getNumOperands() const
const Module * getParent() const
Definition Metadata.h:1828
LLVM_ABI void clearOperands()
Drop all references to this node's operands.
iterator_range< op_iterator > operands()
Definition Metadata.h:1853
Module * getParent()
Get the module that holds this named metadata collection.
Definition Metadata.h:1827
LLVM_ABI void addOperand(MDNode *M)
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Shared implementation of use-lists for replaceable metadata.
Definition Metadata.h:390
static LLVM_ABI void SalvageDebugInfo(const Constant &C)
Replace all uses of the constant with Undef in debug info metadata.
Definition Metadata.cpp:332
LLVM_ABI void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition Metadata.cpp:369
LLVM_ABI SmallVector< DbgVariableRecord * > getAllDbgVariableRecordUsers()
Returns the list of all DbgVariableRecord users of this.
Definition Metadata.cpp:273
ReplaceableMetadataImpl(LLVMContext &Context)
Definition Metadata.h:402
LLVMContext & getContext() const
Definition Metadata.h:408
unsigned getNumUses() const
Definition Metadata.h:428
LLVM_ABI void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition Metadata.cpp:422
LLVM_ABI SmallVector< Metadata * > getAllArgListUsers()
Returns the list of all DIArgList users of this.
Definition Metadata.cpp:251
MetadataTracking::OwnerTy OwnerTy
Definition Metadata.h:394
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMapEntryStorage - Holds the value in a StringMapEntry.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
const char * iterator
Definition StringRef.h:59
const unsigned char * bytes_end() const
Definition StringRef.h:127
iterator begin() const
Definition StringRef.h:112
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
iterator end() const
Definition StringRef.h:114
const unsigned char * bytes_begin() const
Definition StringRef.h:124
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
Typed iterator through MDNode operands.
Definition Metadata.h:1621
TypedMDOperandIterator operator++(int)
Definition Metadata.h:1641
std::ptrdiff_t difference_type
Definition Metadata.h:1627
std::input_iterator_tag iterator_category
Definition Metadata.h:1625
bool operator==(const TypedMDOperandIterator &X) const
Definition Metadata.h:1647
TypedMDOperandIterator & operator++()
Definition Metadata.h:1636
TypedMDOperandIterator(MDNode::op_iterator I)
Definition Metadata.h:1632
bool operator!=(const TypedMDOperandIterator &X) const
Definition Metadata.h:1648
Value wrapper in the Metadata hierarchy.
Definition Metadata.h:458
Type * getType() const
Definition Metadata.h:499
static LocalAsMetadata * getLocalIfExists(Value *Local)
Definition Metadata.h:494
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition Metadata.h:518
SmallVector< DbgVariableRecord * > getAllDbgVariableRecordUsers()
Definition Metadata.h:505
LLVMContext & getContext() const
Definition Metadata.h:500
static LLVM_ABI void handleDeletion(Value *V)
Definition Metadata.cpp:527
static LocalAsMetadata * getLocal(Value *Local)
Definition Metadata.h:484
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:503
static ConstantAsMetadata * getConstantIfExists(Value *C)
Definition Metadata.h:490
static ConstantAsMetadata * getConstant(Value *C)
Definition Metadata.h:480
static LLVM_ABI ValueAsMetadata * getIfExists(Value *V)
Definition Metadata.cpp:522
static LLVM_ABI void handleRAUW(Value *From, Value *To)
Definition Metadata.cpp:546
friend class ReplaceableMetadataImpl
Definition Metadata.h:459
static bool classof(const Metadata *MD)
Definition Metadata.h:523
friend class LLVMContextImpl
Definition Metadata.h:460
SmallVector< Metadata * > getAllArgListUsers()
Definition Metadata.h:502
ValueAsMetadata(unsigned ID, Value *V)
Definition Metadata.h:470
Value * getValue() const
Definition Metadata.h:498
~ValueAsMetadata()=default
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Value(Type *Ty, unsigned scid)
Definition Value.cpp:53
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
struct LLVMOpaqueNamedMDNode * LLVMNamedMDNodeRef
Represents an LLVM Named Metadata Node.
Definition Types.h:96
struct LLVMOpaqueMetadata * LLVMMetadataRef
Represents an LLVM Metadata.
Definition Types.h:89
This file defines the ilist_node class template, which is a convenient base class for creating classe...
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
static constexpr bool HasDereference
Definition Metadata.h:630
decltype(static_cast< V >(*std::declval< U & >())) check_has_dereference
Definition Metadata.h:627
Transitional API for extracting constants from Metadata.
Definition Metadata.h:623
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract_or_null(Y &&MD)
Extract a Value from Metadata, if any, allowing null.
Definition Metadata.h:708
std::enable_if_t< detail::IsValidPointer< X, Y >::value, bool > hasa(Y &&MD)
Check whether Metadata has a Value.
Definition Metadata.h:650
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:682
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:695
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto cast_or_null(const Y &Val)
Definition Casting.h:715
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:677
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:305
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ Other
Any other memory.
Definition ModRef.h:68
Attribute unwrap(LLVMAttributeRef Attr)
Definition Attributes.h:351
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
const uint64_t NOMORE_ICP_MAGICNUM
Magic number in the value profile metadata showing a target has been promoted for the instruction and...
Definition Metadata.h:59
LLVMConstants
Definition Metadata.h:53
@ DEBUG_METADATA_VERSION
Definition Metadata.h:54
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
LLVM_ABI AAMDNodes concat(const AAMDNodes &Other) const
Determine the best AAMDNodes after concatenating two different locations together.
static LLVM_ABI MDNode * shiftTBAAStruct(MDNode *M, size_t off)
bool operator!=(const AAMDNodes &A) const
Definition Metadata.h:771
MDNode * NoAliasAddrSpace
The tag specifying the noalias address spaces.
Definition Metadata.h:790
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:784
static LLVM_ABI MDNode * extendToTBAA(MDNode *TBAA, ssize_t len)
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
AAMDNodes shift(size_t Offset) const
Create a new AAMDNode that describes this AAMDNode after applying a constant offset to the start of t...
Definition Metadata.h:820
LLVM_ABI AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:787
AAMDNodes intersect(const AAMDNodes &Other) const
Given two sets of AAMDNodes that apply to the same pointer, give the best AAMDNodes that are compatib...
Definition Metadata.h:807
LLVM_ABI AAMDNodes adjustForAccess(unsigned AccessSize)
Create a new AAMDNode for accessing AccessSize bytes of this AAMDNode.
AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N, MDNode *NAS)
Definition Metadata.h:763
AAMDNodes extendTo(ssize_t Len) const
Create a new AAMDNode that describes this AAMDNode after extending it to apply to a series of bytes o...
Definition Metadata.h:834
bool operator==(const AAMDNodes &A) const
Definition Metadata.h:766
AAMDNodes()=default
static LLVM_ABI MDNode * shiftTBAA(MDNode *M, size_t off)
static AAMDNodes getEmptyKey()
Definition Metadata.h:870
static unsigned getHashValue(const AAMDNodes &Val)
Definition Metadata.h:880
static AAMDNodes getTombstoneKey()
Definition Metadata.h:875
static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS)
Definition Metadata.h:888
An information struct used to provide DenseMap with the various necessary components for a given valu...
void operator()(MDNode *Node) const
Definition Metadata.h:1586
static SimpleType getSimplifiedValue(MDOperand &MD)
Definition Metadata.h:964
static SimpleType getSimplifiedValue(const MDOperand &MD)
Definition Metadata.h:970
Define a template that can be specialized by smart pointers to reflect the fact that they are automat...
Definition Casting.h:34