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