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