Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
14 | pmbaty | 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/PointerUnion.h" |
||
22 | #include "llvm/ADT/SmallVector.h" |
||
23 | #include "llvm/ADT/StringRef.h" |
||
24 | #include "llvm/ADT/ilist_node.h" |
||
25 | #include "llvm/ADT/iterator_range.h" |
||
26 | #include "llvm/IR/Constant.h" |
||
27 | #include "llvm/IR/LLVMContext.h" |
||
28 | #include "llvm/IR/Value.h" |
||
29 | #include "llvm/Support/CBindingWrapping.h" |
||
30 | #include "llvm/Support/Casting.h" |
||
31 | #include "llvm/Support/ErrorHandling.h" |
||
32 | #include <cassert> |
||
33 | #include <cstddef> |
||
34 | #include <cstdint> |
||
35 | #include <iterator> |
||
36 | #include <memory> |
||
37 | #include <string> |
||
38 | #include <type_traits> |
||
39 | #include <utility> |
||
40 | |||
41 | namespace llvm { |
||
42 | |||
43 | class Module; |
||
44 | class ModuleSlotTracker; |
||
45 | class raw_ostream; |
||
46 | template <typename T> class StringMapEntry; |
||
47 | template <typename ValueTy> class StringMapEntryStorage; |
||
48 | class Type; |
||
49 | |||
50 | enum LLVMConstants : uint32_t { |
||
51 | DEBUG_METADATA_VERSION = 3 // Current debug info version number. |
||
52 | }; |
||
53 | |||
54 | /// Magic number in the value profile metadata showing a target has been |
||
55 | /// promoted for the instruction and shouldn't be promoted again. |
||
56 | const uint64_t NOMORE_ICP_MAGICNUM = -1; |
||
57 | |||
58 | /// Root of the metadata hierarchy. |
||
59 | /// |
||
60 | /// This is a root class for typeless data in the IR. |
||
61 | class Metadata { |
||
62 | friend class ReplaceableMetadataImpl; |
||
63 | |||
64 | /// RTTI. |
||
65 | const unsigned char SubclassID; |
||
66 | |||
67 | protected: |
||
68 | /// Active type of storage. |
||
69 | enum StorageType { Uniqued, Distinct, Temporary }; |
||
70 | |||
71 | /// Storage flag for non-uniqued, otherwise unowned, metadata. |
||
72 | unsigned char Storage : 7; |
||
73 | |||
74 | unsigned char SubclassData1 : 1; |
||
75 | unsigned short SubclassData16 = 0; |
||
76 | unsigned SubclassData32 = 0; |
||
77 | |||
78 | public: |
||
79 | enum MetadataKind { |
||
80 | #define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind, |
||
81 | #include "llvm/IR/Metadata.def" |
||
82 | }; |
||
83 | |||
84 | protected: |
||
85 | Metadata(unsigned ID, StorageType Storage) |
||
86 | : SubclassID(ID), Storage(Storage), SubclassData1(false) { |
||
87 | static_assert(sizeof(*this) == 8, "Metadata fields poorly packed"); |
||
88 | } |
||
89 | |||
90 | ~Metadata() = default; |
||
91 | |||
92 | /// Default handling of a changed operand, which asserts. |
||
93 | /// |
||
94 | /// If subclasses pass themselves in as owners to a tracking node reference, |
||
95 | /// they must provide an implementation of this method. |
||
96 | void handleChangedOperand(void *, Metadata *) { |
||
97 | llvm_unreachable("Unimplemented in Metadata subclass"); |
||
98 | } |
||
99 | |||
100 | public: |
||
101 | unsigned getMetadataID() const { return SubclassID; } |
||
102 | |||
103 | /// User-friendly dump. |
||
104 | /// |
||
105 | /// If \c M is provided, metadata nodes will be numbered canonically; |
||
106 | /// otherwise, pointer addresses are substituted. |
||
107 | /// |
||
108 | /// Note: this uses an explicit overload instead of default arguments so that |
||
109 | /// the nullptr version is easy to call from a debugger. |
||
110 | /// |
||
111 | /// @{ |
||
112 | void dump() const; |
||
113 | void dump(const Module *M) const; |
||
114 | /// @} |
||
115 | |||
116 | /// Print. |
||
117 | /// |
||
118 | /// Prints definition of \c this. |
||
119 | /// |
||
120 | /// If \c M is provided, metadata nodes will be numbered canonically; |
||
121 | /// otherwise, pointer addresses are substituted. |
||
122 | /// @{ |
||
123 | void print(raw_ostream &OS, const Module *M = nullptr, |
||
124 | bool IsForDebug = false) const; |
||
125 | void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr, |
||
126 | bool IsForDebug = false) const; |
||
127 | /// @} |
||
128 | |||
129 | /// Print as operand. |
||
130 | /// |
||
131 | /// Prints reference of \c this. |
||
132 | /// |
||
133 | /// If \c M is provided, metadata nodes will be numbered canonically; |
||
134 | /// otherwise, pointer addresses are substituted. |
||
135 | /// @{ |
||
136 | void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const; |
||
137 | void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST, |
||
138 | const Module *M = nullptr) const; |
||
139 | /// @} |
||
140 | }; |
||
141 | |||
142 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
||
143 | DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef) |
||
144 | |||
145 | // Specialized opaque metadata conversions. |
||
146 | inline Metadata **unwrap(LLVMMetadataRef *MDs) { |
||
147 | return reinterpret_cast<Metadata**>(MDs); |
||
148 | } |
||
149 | |||
150 | #define HANDLE_METADATA(CLASS) class CLASS; |
||
151 | #include "llvm/IR/Metadata.def" |
||
152 | |||
153 | // Provide specializations of isa so that we don't need definitions of |
||
154 | // subclasses to see if the metadata is a subclass. |
||
155 | #define HANDLE_METADATA_LEAF(CLASS) \ |
||
156 | template <> struct isa_impl<CLASS, Metadata> { \ |
||
157 | static inline bool doit(const Metadata &MD) { \ |
||
158 | return MD.getMetadataID() == Metadata::CLASS##Kind; \ |
||
159 | } \ |
||
160 | }; |
||
161 | #include "llvm/IR/Metadata.def" |
||
162 | |||
163 | inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) { |
||
164 | MD.print(OS); |
||
165 | return OS; |
||
166 | } |
||
167 | |||
168 | /// Metadata wrapper in the Value hierarchy. |
||
169 | /// |
||
170 | /// A member of the \a Value hierarchy to represent a reference to metadata. |
||
171 | /// This allows, e.g., intrinsics to have metadata as operands. |
||
172 | /// |
||
173 | /// Notably, this is the only thing in either hierarchy that is allowed to |
||
174 | /// reference \a LocalAsMetadata. |
||
175 | class MetadataAsValue : public Value { |
||
176 | friend class ReplaceableMetadataImpl; |
||
177 | friend class LLVMContextImpl; |
||
178 | |||
179 | Metadata *MD; |
||
180 | |||
181 | MetadataAsValue(Type *Ty, Metadata *MD); |
||
182 | |||
183 | /// Drop use of metadata (during teardown). |
||
184 | void dropUse() { MD = nullptr; } |
||
185 | |||
186 | public: |
||
187 | ~MetadataAsValue(); |
||
188 | |||
189 | static MetadataAsValue *get(LLVMContext &Context, Metadata *MD); |
||
190 | static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD); |
||
191 | |||
192 | Metadata *getMetadata() const { return MD; } |
||
193 | |||
194 | static bool classof(const Value *V) { |
||
195 | return V->getValueID() == MetadataAsValueVal; |
||
196 | } |
||
197 | |||
198 | private: |
||
199 | void handleChangedMetadata(Metadata *MD); |
||
200 | void track(); |
||
201 | void untrack(); |
||
202 | }; |
||
203 | |||
204 | /// API for tracking metadata references through RAUW and deletion. |
||
205 | /// |
||
206 | /// Shared API for updating \a Metadata pointers in subclasses that support |
||
207 | /// RAUW. |
||
208 | /// |
||
209 | /// This API is not meant to be used directly. See \a TrackingMDRef for a |
||
210 | /// user-friendly tracking reference. |
||
211 | class MetadataTracking { |
||
212 | public: |
||
213 | /// Track the reference to metadata. |
||
214 | /// |
||
215 | /// Register \c MD with \c *MD, if the subclass supports tracking. If \c *MD |
||
216 | /// gets RAUW'ed, \c MD will be updated to the new address. If \c *MD gets |
||
217 | /// deleted, \c MD will be set to \c nullptr. |
||
218 | /// |
||
219 | /// If tracking isn't supported, \c *MD will not change. |
||
220 | /// |
||
221 | /// \return true iff tracking is supported by \c MD. |
||
222 | static bool track(Metadata *&MD) { |
||
223 | return track(&MD, *MD, static_cast<Metadata *>(nullptr)); |
||
224 | } |
||
225 | |||
226 | /// Track the reference to metadata for \a Metadata. |
||
227 | /// |
||
228 | /// As \a track(Metadata*&), but with support for calling back to \c Owner to |
||
229 | /// tell it that its operand changed. This could trigger \c Owner being |
||
230 | /// re-uniqued. |
||
231 | static bool track(void *Ref, Metadata &MD, Metadata &Owner) { |
||
232 | return track(Ref, MD, &Owner); |
||
233 | } |
||
234 | |||
235 | /// Track the reference to metadata for \a MetadataAsValue. |
||
236 | /// |
||
237 | /// As \a track(Metadata*&), but with support for calling back to \c Owner to |
||
238 | /// tell it that its operand changed. This could trigger \c Owner being |
||
239 | /// re-uniqued. |
||
240 | static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) { |
||
241 | return track(Ref, MD, &Owner); |
||
242 | } |
||
243 | |||
244 | /// Stop tracking a reference to metadata. |
||
245 | /// |
||
246 | /// Stops \c *MD from tracking \c MD. |
||
247 | static void untrack(Metadata *&MD) { untrack(&MD, *MD); } |
||
248 | static void untrack(void *Ref, Metadata &MD); |
||
249 | |||
250 | /// Move tracking from one reference to another. |
||
251 | /// |
||
252 | /// Semantically equivalent to \c untrack(MD) followed by \c track(New), |
||
253 | /// except that ownership callbacks are maintained. |
||
254 | /// |
||
255 | /// Note: it is an error if \c *MD does not equal \c New. |
||
256 | /// |
||
257 | /// \return true iff tracking is supported by \c MD. |
||
258 | static bool retrack(Metadata *&MD, Metadata *&New) { |
||
259 | return retrack(&MD, *MD, &New); |
||
260 | } |
||
261 | static bool retrack(void *Ref, Metadata &MD, void *New); |
||
262 | |||
263 | /// Check whether metadata is replaceable. |
||
264 | static bool isReplaceable(const Metadata &MD); |
||
265 | |||
266 | using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *>; |
||
267 | |||
268 | private: |
||
269 | /// Track a reference to metadata for an owner. |
||
270 | /// |
||
271 | /// Generalized version of tracking. |
||
272 | static bool track(void *Ref, Metadata &MD, OwnerTy Owner); |
||
273 | }; |
||
274 | |||
275 | /// Shared implementation of use-lists for replaceable metadata. |
||
276 | /// |
||
277 | /// Most metadata cannot be RAUW'ed. This is a shared implementation of |
||
278 | /// use-lists and associated API for the two that support it (\a ValueAsMetadata |
||
279 | /// and \a TempMDNode). |
||
280 | class ReplaceableMetadataImpl { |
||
281 | friend class MetadataTracking; |
||
282 | |||
283 | public: |
||
284 | using OwnerTy = MetadataTracking::OwnerTy; |
||
285 | |||
286 | private: |
||
287 | LLVMContext &Context; |
||
288 | uint64_t NextIndex = 0; |
||
289 | SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap; |
||
290 | |||
291 | public: |
||
292 | ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {} |
||
293 | |||
294 | ~ReplaceableMetadataImpl() { |
||
295 | assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata"); |
||
296 | } |
||
297 | |||
298 | LLVMContext &getContext() const { return Context; } |
||
299 | |||
300 | /// Replace all uses of this with MD. |
||
301 | /// |
||
302 | /// Replace all uses of this with \c MD, which is allowed to be null. |
||
303 | void replaceAllUsesWith(Metadata *MD); |
||
304 | /// Replace all uses of the constant with Undef in debug info metadata |
||
305 | static void SalvageDebugInfo(const Constant &C); |
||
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). |
||
351 | void dropUsers() { |
||
352 | ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false); |
||
353 | } |
||
354 | |||
355 | protected: |
||
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 | |||
366 | static ConstantAsMetadata *getConstant(Value *C) { |
||
367 | return cast<ConstantAsMetadata>(get(C)); |
||
368 | } |
||
369 | |||
370 | static LocalAsMetadata *getLocal(Value *Local) { |
||
371 | return cast<LocalAsMetadata>(get(Local)); |
||
372 | } |
||
373 | |||
374 | static ValueAsMetadata *getIfExists(Value *V); |
||
375 | |||
376 | static ConstantAsMetadata *getConstantIfExists(Value *C) { |
||
377 | return cast_or_null<ConstantAsMetadata>(getIfExists(C)); |
||
378 | } |
||
379 | |||
380 | static LocalAsMetadata *getLocalIfExists(Value *Local) { |
||
381 | return cast_or_null<LocalAsMetadata>(getIfExists(Local)); |
||
382 | } |
||
383 | |||
384 | Value *getValue() const { return V; } |
||
385 | Type *getType() const { return V->getType(); } |
||
386 | LLVMContext &getContext() const { return V->getContext(); } |
||
387 | |||
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. |
||
401 | void replaceAllUsesWith(Metadata *MD) { |
||
402 | ReplaceableMetadataImpl::replaceAllUsesWith(MD); |
||
403 | } |
||
404 | |||
405 | public: |
||
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 | |||
415 | ConstantAsMetadata(Constant *C) |
||
416 | : ValueAsMetadata(ConstantAsMetadataKind, C) {} |
||
417 | |||
418 | public: |
||
419 | static ConstantAsMetadata *get(Constant *C) { |
||
420 | return ValueAsMetadata::getConstant(C); |
||
421 | } |
||
422 | |||
423 | static ConstantAsMetadata *getIfExists(Constant *C) { |
||
424 | return ValueAsMetadata::getConstantIfExists(C); |
||
425 | } |
||
426 | |||
427 | Constant *getValue() const { |
||
428 | return cast<Constant>(ValueAsMetadata::getValue()); |
||
429 | } |
||
430 | |||
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 | |||
439 | LocalAsMetadata(Value *Local) |
||
440 | : ValueAsMetadata(LocalAsMetadataKind, Local) { |
||
441 | assert(!isa<Constant>(Local) && "Expected local value"); |
||
442 | } |
||
443 | |||
444 | public: |
||
445 | static LocalAsMetadata *get(Value *Local) { |
||
446 | return ValueAsMetadata::getLocal(Local); |
||
447 | } |
||
448 | |||
449 | static LocalAsMetadata *getIfExists(Value *Local) { |
||
450 | return ValueAsMetadata::getLocalIfExists(Local); |
||
451 | } |
||
452 | |||
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> |
||
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> |
||
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 *> |
||
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 *> |
||
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 *> |
||
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 *> |
||
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 *> |
||
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 | |||
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); |
||
624 | static MDString *get(LLVMContext &Context, const char *Str) { |
||
625 | return get(Context, Str ? StringRef(Str) : StringRef()); |
||
626 | } |
||
627 | |||
628 | StringRef getString() const; |
||
629 | |||
630 | unsigned getLength() const { return (unsigned)getString().size(); } |
||
631 | |||
632 | using iterator = StringRef::iterator; |
||
633 | |||
634 | /// Pointer to the first byte of the string. |
||
635 | iterator begin() const { return getString().begin(); } |
||
636 | |||
637 | /// Pointer to one byte past the end of the string. |
||
638 | iterator end() const { return getString().end(); } |
||
639 | |||
640 | const unsigned char *bytes_begin() const { return getString().bytes_begin(); } |
||
641 | const unsigned char *bytes_end() const { return getString().bytes_end(); } |
||
642 | |||
643 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
||
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; |
||
653 | 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 | // Extend tbaa Metadata node to apply to a series of bytes of length len. |
||
686 | // A size of -1 denotes an unknown size. |
||
687 | static MDNode *extendToTBAA(MDNode *TBAA, ssize_t len); |
||
688 | |||
689 | /// Given two sets of AAMDNodes that apply to the same pointer, |
||
690 | /// give the best AAMDNodes that are compatible with both (i.e. a set of |
||
691 | /// nodes whose allowable aliasing conclusions are a subset of those |
||
692 | /// allowable by both of the inputs). However, for efficiency |
||
693 | /// reasons, do not create any new MDNodes. |
||
694 | AAMDNodes intersect(const AAMDNodes &Other) const { |
||
695 | AAMDNodes Result; |
||
696 | Result.TBAA = Other.TBAA == TBAA ? TBAA : nullptr; |
||
697 | Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr; |
||
698 | Result.Scope = Other.Scope == Scope ? Scope : nullptr; |
||
699 | Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr; |
||
700 | return Result; |
||
701 | } |
||
702 | |||
703 | /// Create a new AAMDNode that describes this AAMDNode after applying a |
||
704 | /// constant offset to the start of the pointer. |
||
705 | AAMDNodes shift(size_t Offset) const { |
||
706 | AAMDNodes Result; |
||
707 | Result.TBAA = TBAA ? shiftTBAA(TBAA, Offset) : nullptr; |
||
708 | Result.TBAAStruct = |
||
709 | TBAAStruct ? shiftTBAAStruct(TBAAStruct, Offset) : nullptr; |
||
710 | Result.Scope = Scope; |
||
711 | Result.NoAlias = NoAlias; |
||
712 | return Result; |
||
713 | } |
||
714 | |||
715 | /// Create a new AAMDNode that describes this AAMDNode after extending it to |
||
716 | /// apply to a series of bytes of length Len. A size of -1 denotes an unknown |
||
717 | /// size. |
||
718 | AAMDNodes extendTo(ssize_t Len) const { |
||
719 | AAMDNodes Result; |
||
720 | Result.TBAA = TBAA ? extendToTBAA(TBAA, Len) : nullptr; |
||
721 | // tbaa.struct contains (offset, size, type) triples. Extending the length |
||
722 | // of the tbaa.struct doesn't require changing this (though more information |
||
723 | // could be provided by adding more triples at subsequent lengths). |
||
724 | Result.TBAAStruct = TBAAStruct; |
||
725 | Result.Scope = Scope; |
||
726 | Result.NoAlias = NoAlias; |
||
727 | return Result; |
||
728 | } |
||
729 | |||
730 | /// Given two sets of AAMDNodes applying to potentially different locations, |
||
731 | /// determine the best AAMDNodes that apply to both. |
||
732 | AAMDNodes merge(const AAMDNodes &Other) const; |
||
733 | |||
734 | /// Determine the best AAMDNodes after concatenating two different locations |
||
735 | /// together. Different from `merge`, where different locations should |
||
736 | /// overlap each other, `concat` puts non-overlapping locations together. |
||
737 | AAMDNodes concat(const AAMDNodes &Other) const; |
||
738 | }; |
||
739 | |||
740 | // Specialize DenseMapInfo for AAMDNodes. |
||
741 | template<> |
||
742 | struct DenseMapInfo<AAMDNodes> { |
||
743 | static inline AAMDNodes getEmptyKey() { |
||
744 | return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(), |
||
745 | nullptr, nullptr, nullptr); |
||
746 | } |
||
747 | |||
748 | static inline AAMDNodes getTombstoneKey() { |
||
749 | return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(), |
||
750 | nullptr, nullptr, nullptr); |
||
751 | } |
||
752 | |||
753 | static unsigned getHashValue(const AAMDNodes &Val) { |
||
754 | return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^ |
||
755 | DenseMapInfo<MDNode *>::getHashValue(Val.TBAAStruct) ^ |
||
756 | DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^ |
||
757 | DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias); |
||
758 | } |
||
759 | |||
760 | static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) { |
||
761 | return LHS == RHS; |
||
762 | } |
||
763 | }; |
||
764 | |||
765 | /// Tracking metadata reference owned by Metadata. |
||
766 | /// |
||
767 | /// Similar to \a TrackingMDRef, but it's expected to be owned by an instance |
||
768 | /// of \a Metadata, which has the option of registering itself for callbacks to |
||
769 | /// re-unique itself. |
||
770 | /// |
||
771 | /// In particular, this is used by \a MDNode. |
||
772 | class MDOperand { |
||
773 | Metadata *MD = nullptr; |
||
774 | |||
775 | public: |
||
776 | MDOperand() = default; |
||
777 | MDOperand(const MDOperand &) = delete; |
||
778 | MDOperand(MDOperand &&Op) { |
||
779 | MD = Op.MD; |
||
780 | if (MD) |
||
781 | (void)MetadataTracking::retrack(Op.MD, MD); |
||
782 | Op.MD = nullptr; |
||
783 | } |
||
784 | MDOperand &operator=(const MDOperand &) = delete; |
||
785 | MDOperand &operator=(MDOperand &&Op) { |
||
786 | MD = Op.MD; |
||
787 | if (MD) |
||
788 | (void)MetadataTracking::retrack(Op.MD, MD); |
||
789 | Op.MD = nullptr; |
||
790 | return *this; |
||
791 | } |
||
792 | ~MDOperand() { untrack(); } |
||
793 | |||
794 | Metadata *get() const { return MD; } |
||
795 | operator Metadata *() const { return get(); } |
||
796 | Metadata *operator->() const { return get(); } |
||
797 | Metadata &operator*() const { return *get(); } |
||
798 | |||
799 | void reset() { |
||
800 | untrack(); |
||
801 | MD = nullptr; |
||
802 | } |
||
803 | void reset(Metadata *MD, Metadata *Owner) { |
||
804 | untrack(); |
||
805 | this->MD = MD; |
||
806 | track(Owner); |
||
807 | } |
||
808 | |||
809 | private: |
||
810 | void track(Metadata *Owner) { |
||
811 | if (MD) { |
||
812 | if (Owner) |
||
813 | MetadataTracking::track(this, *MD, *Owner); |
||
814 | else |
||
815 | MetadataTracking::track(MD); |
||
816 | } |
||
817 | } |
||
818 | |||
819 | void untrack() { |
||
820 | assert(static_cast<void *>(this) == &MD && "Expected same address"); |
||
821 | if (MD) |
||
822 | MetadataTracking::untrack(MD); |
||
823 | } |
||
824 | }; |
||
825 | |||
826 | template <> struct simplify_type<MDOperand> { |
||
827 | using SimpleType = Metadata *; |
||
828 | |||
829 | static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); } |
||
830 | }; |
||
831 | |||
832 | template <> struct simplify_type<const MDOperand> { |
||
833 | using SimpleType = Metadata *; |
||
834 | |||
835 | static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); } |
||
836 | }; |
||
837 | |||
838 | /// Pointer to the context, with optional RAUW support. |
||
839 | /// |
||
840 | /// Either a raw (non-null) pointer to the \a LLVMContext, or an owned pointer |
||
841 | /// to \a ReplaceableMetadataImpl (which has a reference to \a LLVMContext). |
||
842 | class ContextAndReplaceableUses { |
||
843 | PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr; |
||
844 | |||
845 | public: |
||
846 | ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {} |
||
847 | ContextAndReplaceableUses( |
||
848 | std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) |
||
849 | : Ptr(ReplaceableUses.release()) { |
||
850 | assert(getReplaceableUses() && "Expected non-null replaceable uses"); |
||
851 | } |
||
852 | ContextAndReplaceableUses() = delete; |
||
853 | ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete; |
||
854 | ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete; |
||
855 | ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete; |
||
856 | ContextAndReplaceableUses & |
||
857 | operator=(const ContextAndReplaceableUses &) = delete; |
||
858 | ~ContextAndReplaceableUses() { delete getReplaceableUses(); } |
||
859 | |||
860 | operator LLVMContext &() { return getContext(); } |
||
861 | |||
862 | /// Whether this contains RAUW support. |
||
863 | bool hasReplaceableUses() const { |
||
864 | return Ptr.is<ReplaceableMetadataImpl *>(); |
||
865 | } |
||
866 | |||
867 | LLVMContext &getContext() const { |
||
868 | if (hasReplaceableUses()) |
||
869 | return getReplaceableUses()->getContext(); |
||
870 | return *Ptr.get<LLVMContext *>(); |
||
871 | } |
||
872 | |||
873 | ReplaceableMetadataImpl *getReplaceableUses() const { |
||
874 | if (hasReplaceableUses()) |
||
875 | return Ptr.get<ReplaceableMetadataImpl *>(); |
||
876 | return nullptr; |
||
877 | } |
||
878 | |||
879 | /// Ensure that this has RAUW support, and then return it. |
||
880 | ReplaceableMetadataImpl *getOrCreateReplaceableUses() { |
||
881 | if (!hasReplaceableUses()) |
||
882 | makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext())); |
||
883 | return getReplaceableUses(); |
||
884 | } |
||
885 | |||
886 | /// Assign RAUW support to this. |
||
887 | /// |
||
888 | /// Make this replaceable, taking ownership of \c ReplaceableUses (which must |
||
889 | /// not be null). |
||
890 | void |
||
891 | makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) { |
||
892 | assert(ReplaceableUses && "Expected non-null replaceable uses"); |
||
893 | assert(&ReplaceableUses->getContext() == &getContext() && |
||
894 | "Expected same context"); |
||
895 | delete getReplaceableUses(); |
||
896 | Ptr = ReplaceableUses.release(); |
||
897 | } |
||
898 | |||
899 | /// Drop RAUW support. |
||
900 | /// |
||
901 | /// Cede ownership of RAUW support, returning it. |
||
902 | std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() { |
||
903 | assert(hasReplaceableUses() && "Expected to own replaceable uses"); |
||
904 | std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses( |
||
905 | getReplaceableUses()); |
||
906 | Ptr = &ReplaceableUses->getContext(); |
||
907 | return ReplaceableUses; |
||
908 | } |
||
909 | }; |
||
910 | |||
911 | struct TempMDNodeDeleter { |
||
912 | inline void operator()(MDNode *Node) const; |
||
913 | }; |
||
914 | |||
915 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
||
916 | using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>; |
||
917 | #define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS) |
||
918 | #include "llvm/IR/Metadata.def" |
||
919 | |||
920 | /// Metadata node. |
||
921 | /// |
||
922 | /// Metadata nodes can be uniqued, like constants, or distinct. Temporary |
||
923 | /// metadata nodes (with full support for RAUW) can be used to delay uniquing |
||
924 | /// until forward references are known. The basic metadata node is an \a |
||
925 | /// MDTuple. |
||
926 | /// |
||
927 | /// There is limited support for RAUW at construction time. At construction |
||
928 | /// time, if any operand is a temporary node (or an unresolved uniqued node, |
||
929 | /// which indicates a transitive temporary operand), the node itself will be |
||
930 | /// unresolved. As soon as all operands become resolved, it will drop RAUW |
||
931 | /// support permanently. |
||
932 | /// |
||
933 | /// If an unresolved node is part of a cycle, \a resolveCycles() needs |
||
934 | /// to be called on some member of the cycle once all temporary nodes have been |
||
935 | /// replaced. |
||
936 | /// |
||
937 | /// MDNodes can be large or small, as well as resizable or non-resizable. |
||
938 | /// Large MDNodes' operands are allocated in a separate storage vector, |
||
939 | /// whereas small MDNodes' operands are co-allocated. Distinct and temporary |
||
940 | /// MDnodes are resizable, but only MDTuples support this capability. |
||
941 | /// |
||
942 | /// Clients can add operands to resizable MDNodes using push_back(). |
||
943 | class MDNode : public Metadata { |
||
944 | friend class ReplaceableMetadataImpl; |
||
945 | friend class LLVMContextImpl; |
||
946 | friend class DIArgList; |
||
947 | |||
948 | /// The header that is coallocated with an MDNode along with its "small" |
||
949 | /// operands. It is located immediately before the main body of the node. |
||
950 | /// The operands are in turn located immediately before the header. |
||
951 | /// For resizable MDNodes, the space for the storage vector is also allocated |
||
952 | /// immediately before the header, overlapping with the operands. |
||
953 | /// Explicity set alignment because bitfields by default have an |
||
954 | /// alignment of 1 on z/OS. |
||
955 | struct alignas(alignof(size_t)) Header { |
||
956 | bool IsResizable : 1; |
||
957 | bool IsLarge : 1; |
||
958 | size_t SmallSize : 4; |
||
959 | size_t SmallNumOps : 4; |
||
960 | size_t : sizeof(size_t) * CHAR_BIT - 10; |
||
961 | |||
962 | unsigned NumUnresolved = 0; |
||
963 | using LargeStorageVector = SmallVector<MDOperand, 0>; |
||
964 | |||
965 | static constexpr size_t NumOpsFitInVector = |
||
966 | sizeof(LargeStorageVector) / sizeof(MDOperand); |
||
967 | static_assert( |
||
968 | NumOpsFitInVector * sizeof(MDOperand) == sizeof(LargeStorageVector), |
||
969 | "sizeof(LargeStorageVector) must be a multiple of sizeof(MDOperand)"); |
||
970 | |||
971 | static constexpr size_t MaxSmallSize = 15; |
||
972 | |||
973 | static constexpr size_t getOpSize(unsigned NumOps) { |
||
974 | return sizeof(MDOperand) * NumOps; |
||
975 | } |
||
976 | /// Returns the number of operands the node has space for based on its |
||
977 | /// allocation characteristics. |
||
978 | static size_t getSmallSize(size_t NumOps, bool IsResizable, bool IsLarge) { |
||
979 | return IsLarge ? NumOpsFitInVector |
||
980 | : std::max(NumOps, NumOpsFitInVector * IsResizable); |
||
981 | } |
||
982 | /// Returns the number of bytes allocated for operands and header. |
||
983 | static size_t getAllocSize(StorageType Storage, size_t NumOps) { |
||
984 | return getOpSize( |
||
985 | getSmallSize(NumOps, isResizable(Storage), isLarge(NumOps))) + |
||
986 | sizeof(Header); |
||
987 | } |
||
988 | |||
989 | /// Only temporary and distinct nodes are resizable. |
||
990 | static bool isResizable(StorageType Storage) { return Storage != Uniqued; } |
||
991 | static bool isLarge(size_t NumOps) { return NumOps > MaxSmallSize; } |
||
992 | |||
993 | size_t getAllocSize() const { |
||
994 | return getOpSize(SmallSize) + sizeof(Header); |
||
995 | } |
||
996 | void *getAllocation() { |
||
997 | return reinterpret_cast<char *>(this + 1) - |
||
998 | alignTo(getAllocSize(), alignof(uint64_t)); |
||
999 | } |
||
1000 | |||
1001 | void *getLargePtr() const { |
||
1002 | static_assert(alignof(LargeStorageVector) <= alignof(Header), |
||
1003 | "LargeStorageVector too strongly aligned"); |
||
1004 | return reinterpret_cast<char *>(const_cast<Header *>(this)) - |
||
1005 | sizeof(LargeStorageVector); |
||
1006 | } |
||
1007 | |||
1008 | void *getSmallPtr(); |
||
1009 | |||
1010 | LargeStorageVector &getLarge() { |
||
1011 | assert(IsLarge); |
||
1012 | return *reinterpret_cast<LargeStorageVector *>(getLargePtr()); |
||
1013 | } |
||
1014 | |||
1015 | const LargeStorageVector &getLarge() const { |
||
1016 | assert(IsLarge); |
||
1017 | return *reinterpret_cast<const LargeStorageVector *>(getLargePtr()); |
||
1018 | } |
||
1019 | |||
1020 | void resizeSmall(size_t NumOps); |
||
1021 | void resizeSmallToLarge(size_t NumOps); |
||
1022 | void resize(size_t NumOps); |
||
1023 | |||
1024 | explicit Header(size_t NumOps, StorageType Storage); |
||
1025 | ~Header(); |
||
1026 | |||
1027 | MutableArrayRef<MDOperand> operands() { |
||
1028 | if (IsLarge) |
||
1029 | return getLarge(); |
||
1030 | return MutableArrayRef( |
||
1031 | reinterpret_cast<MDOperand *>(this) - SmallSize, SmallNumOps); |
||
1032 | } |
||
1033 | |||
1034 | ArrayRef<MDOperand> operands() const { |
||
1035 | if (IsLarge) |
||
1036 | return getLarge(); |
||
1037 | return ArrayRef(reinterpret_cast<const MDOperand *>(this) - SmallSize, |
||
1038 | SmallNumOps); |
||
1039 | } |
||
1040 | |||
1041 | unsigned getNumOperands() const { |
||
1042 | if (!IsLarge) |
||
1043 | return SmallNumOps; |
||
1044 | return getLarge().size(); |
||
1045 | } |
||
1046 | }; |
||
1047 | |||
1048 | Header &getHeader() { return *(reinterpret_cast<Header *>(this) - 1); } |
||
1049 | |||
1050 | const Header &getHeader() const { |
||
1051 | return *(reinterpret_cast<const Header *>(this) - 1); |
||
1052 | } |
||
1053 | |||
1054 | ContextAndReplaceableUses Context; |
||
1055 | |||
1056 | protected: |
||
1057 | MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, |
||
1058 | ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = std::nullopt); |
||
1059 | ~MDNode() = default; |
||
1060 | |||
1061 | void *operator new(size_t Size, size_t NumOps, StorageType Storage); |
||
1062 | void operator delete(void *Mem); |
||
1063 | |||
1064 | /// Required by std, but never called. |
||
1065 | void operator delete(void *, unsigned) { |
||
1066 | llvm_unreachable("Constructor throws?"); |
||
1067 | } |
||
1068 | |||
1069 | /// Required by std, but never called. |
||
1070 | void operator delete(void *, unsigned, bool) { |
||
1071 | llvm_unreachable("Constructor throws?"); |
||
1072 | } |
||
1073 | |||
1074 | void dropAllReferences(); |
||
1075 | |||
1076 | MDOperand *mutable_begin() { return getHeader().operands().begin(); } |
||
1077 | MDOperand *mutable_end() { return getHeader().operands().end(); } |
||
1078 | |||
1079 | using mutable_op_range = iterator_range<MDOperand *>; |
||
1080 | |||
1081 | mutable_op_range mutable_operands() { |
||
1082 | return mutable_op_range(mutable_begin(), mutable_end()); |
||
1083 | } |
||
1084 | |||
1085 | public: |
||
1086 | MDNode(const MDNode &) = delete; |
||
1087 | void operator=(const MDNode &) = delete; |
||
1088 | void *operator new(size_t) = delete; |
||
1089 | |||
1090 | static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs); |
||
1091 | static inline MDTuple *getIfExists(LLVMContext &Context, |
||
1092 | ArrayRef<Metadata *> MDs); |
||
1093 | static inline MDTuple *getDistinct(LLVMContext &Context, |
||
1094 | ArrayRef<Metadata *> MDs); |
||
1095 | static inline TempMDTuple getTemporary(LLVMContext &Context, |
||
1096 | ArrayRef<Metadata *> MDs); |
||
1097 | |||
1098 | /// Create a (temporary) clone of this. |
||
1099 | TempMDNode clone() const; |
||
1100 | |||
1101 | /// Deallocate a node created by getTemporary. |
||
1102 | /// |
||
1103 | /// Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining |
||
1104 | /// references will be reset. |
||
1105 | static void deleteTemporary(MDNode *N); |
||
1106 | |||
1107 | LLVMContext &getContext() const { return Context.getContext(); } |
||
1108 | |||
1109 | /// Replace a specific operand. |
||
1110 | void replaceOperandWith(unsigned I, Metadata *New); |
||
1111 | |||
1112 | /// Check if node is fully resolved. |
||
1113 | /// |
||
1114 | /// If \a isTemporary(), this always returns \c false; if \a isDistinct(), |
||
1115 | /// this always returns \c true. |
||
1116 | /// |
||
1117 | /// If \a isUniqued(), returns \c true if this has already dropped RAUW |
||
1118 | /// support (because all operands are resolved). |
||
1119 | /// |
||
1120 | /// As forward declarations are resolved, their containers should get |
||
1121 | /// resolved automatically. However, if this (or one of its operands) is |
||
1122 | /// involved in a cycle, \a resolveCycles() needs to be called explicitly. |
||
1123 | bool isResolved() const { return !isTemporary() && !getNumUnresolved(); } |
||
1124 | |||
1125 | bool isUniqued() const { return Storage == Uniqued; } |
||
1126 | bool isDistinct() const { return Storage == Distinct; } |
||
1127 | bool isTemporary() const { return Storage == Temporary; } |
||
1128 | |||
1129 | /// RAUW a temporary. |
||
1130 | /// |
||
1131 | /// \pre \a isTemporary() must be \c true. |
||
1132 | void replaceAllUsesWith(Metadata *MD) { |
||
1133 | assert(isTemporary() && "Expected temporary node"); |
||
1134 | if (Context.hasReplaceableUses()) |
||
1135 | Context.getReplaceableUses()->replaceAllUsesWith(MD); |
||
1136 | } |
||
1137 | |||
1138 | /// Resolve cycles. |
||
1139 | /// |
||
1140 | /// Once all forward declarations have been resolved, force cycles to be |
||
1141 | /// resolved. |
||
1142 | /// |
||
1143 | /// \pre No operands (or operands' operands, etc.) have \a isTemporary(). |
||
1144 | void resolveCycles(); |
||
1145 | |||
1146 | /// Resolve a unique, unresolved node. |
||
1147 | void resolve(); |
||
1148 | |||
1149 | /// Replace a temporary node with a permanent one. |
||
1150 | /// |
||
1151 | /// Try to create a uniqued version of \c N -- in place, if possible -- and |
||
1152 | /// return it. If \c N cannot be uniqued, return a distinct node instead. |
||
1153 | template <class T> |
||
1154 | static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
||
1155 | replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) { |
||
1156 | return cast<T>(N.release()->replaceWithPermanentImpl()); |
||
1157 | } |
||
1158 | |||
1159 | /// Replace a temporary node with a uniqued one. |
||
1160 | /// |
||
1161 | /// Create a uniqued version of \c N -- in place, if possible -- and return |
||
1162 | /// it. Takes ownership of the temporary node. |
||
1163 | /// |
||
1164 | /// \pre N does not self-reference. |
||
1165 | template <class T> |
||
1166 | static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
||
1167 | replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) { |
||
1168 | return cast<T>(N.release()->replaceWithUniquedImpl()); |
||
1169 | } |
||
1170 | |||
1171 | /// Replace a temporary node with a distinct one. |
||
1172 | /// |
||
1173 | /// Create a distinct version of \c N -- in place, if possible -- and return |
||
1174 | /// it. Takes ownership of the temporary node. |
||
1175 | template <class T> |
||
1176 | static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
||
1177 | replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) { |
||
1178 | return cast<T>(N.release()->replaceWithDistinctImpl()); |
||
1179 | } |
||
1180 | |||
1181 | /// Print in tree shape. |
||
1182 | /// |
||
1183 | /// Prints definition of \c this in tree shape. |
||
1184 | /// |
||
1185 | /// If \c M is provided, metadata nodes will be numbered canonically; |
||
1186 | /// otherwise, pointer addresses are substituted. |
||
1187 | /// @{ |
||
1188 | void printTree(raw_ostream &OS, const Module *M = nullptr) const; |
||
1189 | void printTree(raw_ostream &OS, ModuleSlotTracker &MST, |
||
1190 | const Module *M = nullptr) const; |
||
1191 | /// @} |
||
1192 | |||
1193 | /// User-friendly dump in tree shape. |
||
1194 | /// |
||
1195 | /// If \c M is provided, metadata nodes will be numbered canonically; |
||
1196 | /// otherwise, pointer addresses are substituted. |
||
1197 | /// |
||
1198 | /// Note: this uses an explicit overload instead of default arguments so that |
||
1199 | /// the nullptr version is easy to call from a debugger. |
||
1200 | /// |
||
1201 | /// @{ |
||
1202 | void dumpTree() const; |
||
1203 | void dumpTree(const Module *M) const; |
||
1204 | /// @} |
||
1205 | |||
1206 | private: |
||
1207 | MDNode *replaceWithPermanentImpl(); |
||
1208 | MDNode *replaceWithUniquedImpl(); |
||
1209 | MDNode *replaceWithDistinctImpl(); |
||
1210 | |||
1211 | protected: |
||
1212 | /// Set an operand. |
||
1213 | /// |
||
1214 | /// Sets the operand directly, without worrying about uniquing. |
||
1215 | void setOperand(unsigned I, Metadata *New); |
||
1216 | |||
1217 | unsigned getNumUnresolved() const { return getHeader().NumUnresolved; } |
||
1218 | |||
1219 | void setNumUnresolved(unsigned N) { getHeader().NumUnresolved = N; } |
||
1220 | void storeDistinctInContext(); |
||
1221 | template <class T, class StoreT> |
||
1222 | static T *storeImpl(T *N, StorageType Storage, StoreT &Store); |
||
1223 | template <class T> static T *storeImpl(T *N, StorageType Storage); |
||
1224 | |||
1225 | /// Resize the node to hold \a NumOps operands. |
||
1226 | /// |
||
1227 | /// \pre \a isTemporary() or \a isDistinct() |
||
1228 | /// \pre MetadataID == MDTupleKind |
||
1229 | void resize(size_t NumOps) { |
||
1230 | assert(!isUniqued() && "Resizing is not supported for uniqued nodes"); |
||
1231 | assert(getMetadataID() == MDTupleKind && |
||
1232 | "Resizing is not supported for this node kind"); |
||
1233 | getHeader().resize(NumOps); |
||
1234 | } |
||
1235 | |||
1236 | private: |
||
1237 | void handleChangedOperand(void *Ref, Metadata *New); |
||
1238 | |||
1239 | /// Drop RAUW support, if any. |
||
1240 | void dropReplaceableUses(); |
||
1241 | |||
1242 | void resolveAfterOperandChange(Metadata *Old, Metadata *New); |
||
1243 | void decrementUnresolvedOperandCount(); |
||
1244 | void countUnresolvedOperands(); |
||
1245 | |||
1246 | /// Mutate this to be "uniqued". |
||
1247 | /// |
||
1248 | /// Mutate this so that \a isUniqued(). |
||
1249 | /// \pre \a isTemporary(). |
||
1250 | /// \pre already added to uniquing set. |
||
1251 | void makeUniqued(); |
||
1252 | |||
1253 | /// Mutate this to be "distinct". |
||
1254 | /// |
||
1255 | /// Mutate this so that \a isDistinct(). |
||
1256 | /// \pre \a isTemporary(). |
||
1257 | void makeDistinct(); |
||
1258 | |||
1259 | void deleteAsSubclass(); |
||
1260 | MDNode *uniquify(); |
||
1261 | void eraseFromStore(); |
||
1262 | |||
1263 | template <class NodeTy> struct HasCachedHash; |
||
1264 | template <class NodeTy> |
||
1265 | static void dispatchRecalculateHash(NodeTy *N, std::true_type) { |
||
1266 | N->recalculateHash(); |
||
1267 | } |
||
1268 | template <class NodeTy> |
||
1269 | static void dispatchRecalculateHash(NodeTy *, std::false_type) {} |
||
1270 | template <class NodeTy> |
||
1271 | static void dispatchResetHash(NodeTy *N, std::true_type) { |
||
1272 | N->setHash(0); |
||
1273 | } |
||
1274 | template <class NodeTy> |
||
1275 | static void dispatchResetHash(NodeTy *, std::false_type) {} |
||
1276 | |||
1277 | public: |
||
1278 | using op_iterator = const MDOperand *; |
||
1279 | using op_range = iterator_range<op_iterator>; |
||
1280 | |||
1281 | op_iterator op_begin() const { |
||
1282 | return const_cast<MDNode *>(this)->mutable_begin(); |
||
1283 | } |
||
1284 | |||
1285 | op_iterator op_end() const { |
||
1286 | return const_cast<MDNode *>(this)->mutable_end(); |
||
1287 | } |
||
1288 | |||
1289 | ArrayRef<MDOperand> operands() const { return getHeader().operands(); } |
||
1290 | |||
1291 | const MDOperand &getOperand(unsigned I) const { |
||
1292 | assert(I < getNumOperands() && "Out of range"); |
||
1293 | return getHeader().operands()[I]; |
||
1294 | } |
||
1295 | |||
1296 | /// Return number of MDNode operands. |
||
1297 | unsigned getNumOperands() const { return getHeader().getNumOperands(); } |
||
1298 | |||
1299 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
||
1300 | static bool classof(const Metadata *MD) { |
||
1301 | switch (MD->getMetadataID()) { |
||
1302 | default: |
||
1303 | return false; |
||
1304 | #define HANDLE_MDNODE_LEAF(CLASS) \ |
||
1305 | case CLASS##Kind: \ |
||
1306 | return true; |
||
1307 | #include "llvm/IR/Metadata.def" |
||
1308 | } |
||
1309 | } |
||
1310 | |||
1311 | /// Check whether MDNode is a vtable access. |
||
1312 | bool isTBAAVtableAccess() const; |
||
1313 | |||
1314 | /// Methods for metadata merging. |
||
1315 | static MDNode *concatenate(MDNode *A, MDNode *B); |
||
1316 | static MDNode *intersect(MDNode *A, MDNode *B); |
||
1317 | static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B); |
||
1318 | static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B); |
||
1319 | static MDNode *getMostGenericRange(MDNode *A, MDNode *B); |
||
1320 | static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B); |
||
1321 | static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B); |
||
1322 | }; |
||
1323 | |||
1324 | /// Tuple of metadata. |
||
1325 | /// |
||
1326 | /// This is the simple \a MDNode arbitrary tuple. Nodes are uniqued by |
||
1327 | /// default based on their operands. |
||
1328 | class MDTuple : public MDNode { |
||
1329 | friend class LLVMContextImpl; |
||
1330 | friend class MDNode; |
||
1331 | |||
1332 | MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash, |
||
1333 | ArrayRef<Metadata *> Vals) |
||
1334 | : MDNode(C, MDTupleKind, Storage, Vals) { |
||
1335 | setHash(Hash); |
||
1336 | } |
||
1337 | |||
1338 | ~MDTuple() { dropAllReferences(); } |
||
1339 | |||
1340 | void setHash(unsigned Hash) { SubclassData32 = Hash; } |
||
1341 | void recalculateHash(); |
||
1342 | |||
1343 | static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, |
||
1344 | StorageType Storage, bool ShouldCreate = true); |
||
1345 | |||
1346 | TempMDTuple cloneImpl() const { |
||
1347 | ArrayRef<MDOperand> Operands = operands(); |
||
1348 | return getTemporary(getContext(), SmallVector<Metadata *, 4>( |
||
1349 | Operands.begin(), Operands.end())); |
||
1350 | } |
||
1351 | |||
1352 | public: |
||
1353 | /// Get the hash, if any. |
||
1354 | unsigned getHash() const { return SubclassData32; } |
||
1355 | |||
1356 | static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1357 | return getImpl(Context, MDs, Uniqued); |
||
1358 | } |
||
1359 | |||
1360 | static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1361 | return getImpl(Context, MDs, Uniqued, /* ShouldCreate */ false); |
||
1362 | } |
||
1363 | |||
1364 | /// Return a distinct node. |
||
1365 | /// |
||
1366 | /// Return a distinct node -- i.e., a node that is not uniqued. |
||
1367 | static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1368 | return getImpl(Context, MDs, Distinct); |
||
1369 | } |
||
1370 | |||
1371 | /// Return a temporary node. |
||
1372 | /// |
||
1373 | /// For use in constructing cyclic MDNode structures. A temporary MDNode is |
||
1374 | /// not uniqued, may be RAUW'd, and must be manually deleted with |
||
1375 | /// deleteTemporary. |
||
1376 | static TempMDTuple getTemporary(LLVMContext &Context, |
||
1377 | ArrayRef<Metadata *> MDs) { |
||
1378 | return TempMDTuple(getImpl(Context, MDs, Temporary)); |
||
1379 | } |
||
1380 | |||
1381 | /// Return a (temporary) clone of this. |
||
1382 | TempMDTuple clone() const { return cloneImpl(); } |
||
1383 | |||
1384 | /// Append an element to the tuple. This will resize the node. |
||
1385 | void push_back(Metadata *MD) { |
||
1386 | size_t NumOps = getNumOperands(); |
||
1387 | resize(NumOps + 1); |
||
1388 | setOperand(NumOps, MD); |
||
1389 | } |
||
1390 | |||
1391 | /// Shrink the operands by 1. |
||
1392 | void pop_back() { resize(getNumOperands() - 1); } |
||
1393 | |||
1394 | static bool classof(const Metadata *MD) { |
||
1395 | return MD->getMetadataID() == MDTupleKind; |
||
1396 | } |
||
1397 | }; |
||
1398 | |||
1399 | MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1400 | return MDTuple::get(Context, MDs); |
||
1401 | } |
||
1402 | |||
1403 | MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1404 | return MDTuple::getIfExists(Context, MDs); |
||
1405 | } |
||
1406 | |||
1407 | MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
||
1408 | return MDTuple::getDistinct(Context, MDs); |
||
1409 | } |
||
1410 | |||
1411 | TempMDTuple MDNode::getTemporary(LLVMContext &Context, |
||
1412 | ArrayRef<Metadata *> MDs) { |
||
1413 | return MDTuple::getTemporary(Context, MDs); |
||
1414 | } |
||
1415 | |||
1416 | void TempMDNodeDeleter::operator()(MDNode *Node) const { |
||
1417 | MDNode::deleteTemporary(Node); |
||
1418 | } |
||
1419 | |||
1420 | /// This is a simple wrapper around an MDNode which provides a higher-level |
||
1421 | /// interface by hiding the details of how alias analysis information is encoded |
||
1422 | /// in its operands. |
||
1423 | class AliasScopeNode { |
||
1424 | const MDNode *Node = nullptr; |
||
1425 | |||
1426 | public: |
||
1427 | AliasScopeNode() = default; |
||
1428 | explicit AliasScopeNode(const MDNode *N) : Node(N) {} |
||
1429 | |||
1430 | /// Get the MDNode for this AliasScopeNode. |
||
1431 | const MDNode *getNode() const { return Node; } |
||
1432 | |||
1433 | /// Get the MDNode for this AliasScopeNode's domain. |
||
1434 | const MDNode *getDomain() const { |
||
1435 | if (Node->getNumOperands() < 2) |
||
1436 | return nullptr; |
||
1437 | return dyn_cast_or_null<MDNode>(Node->getOperand(1)); |
||
1438 | } |
||
1439 | StringRef getName() const { |
||
1440 | if (Node->getNumOperands() > 2) |
||
1441 | if (MDString *N = dyn_cast_or_null<MDString>(Node->getOperand(2))) |
||
1442 | return N->getString(); |
||
1443 | return StringRef(); |
||
1444 | } |
||
1445 | }; |
||
1446 | |||
1447 | /// Typed iterator through MDNode operands. |
||
1448 | /// |
||
1449 | /// An iterator that transforms an \a MDNode::iterator into an iterator over a |
||
1450 | /// particular Metadata subclass. |
||
1451 | template <class T> class TypedMDOperandIterator { |
||
1452 | MDNode::op_iterator I = nullptr; |
||
1453 | |||
1454 | public: |
||
1455 | using iterator_category = std::input_iterator_tag; |
||
1456 | using value_type = T *; |
||
1457 | using difference_type = std::ptrdiff_t; |
||
1458 | using pointer = void; |
||
1459 | using reference = T *; |
||
1460 | |||
1461 | TypedMDOperandIterator() = default; |
||
1462 | explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {} |
||
1463 | |||
1464 | T *operator*() const { return cast_or_null<T>(*I); } |
||
1465 | |||
1466 | TypedMDOperandIterator &operator++() { |
||
1467 | ++I; |
||
1468 | return *this; |
||
1469 | } |
||
1470 | |||
1471 | TypedMDOperandIterator operator++(int) { |
||
1472 | TypedMDOperandIterator Temp(*this); |
||
1473 | ++I; |
||
1474 | return Temp; |
||
1475 | } |
||
1476 | |||
1477 | bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; } |
||
1478 | bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; } |
||
1479 | }; |
||
1480 | |||
1481 | /// Typed, array-like tuple of metadata. |
||
1482 | /// |
||
1483 | /// This is a wrapper for \a MDTuple that makes it act like an array holding a |
||
1484 | /// particular type of metadata. |
||
1485 | template <class T> class MDTupleTypedArrayWrapper { |
||
1486 | const MDTuple *N = nullptr; |
||
1487 | |||
1488 | public: |
||
1489 | MDTupleTypedArrayWrapper() = default; |
||
1490 | MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {} |
||
1491 | |||
1492 | template <class U> |
||
1493 | MDTupleTypedArrayWrapper( |
||
1494 | const MDTupleTypedArrayWrapper<U> &Other, |
||
1495 | std::enable_if_t<std::is_convertible<U *, T *>::value> * = nullptr) |
||
1496 | : N(Other.get()) {} |
||
1497 | |||
1498 | template <class U> |
||
1499 | explicit MDTupleTypedArrayWrapper( |
||
1500 | const MDTupleTypedArrayWrapper<U> &Other, |
||
1501 | std::enable_if_t<!std::is_convertible<U *, T *>::value> * = nullptr) |
||
1502 | : N(Other.get()) {} |
||
1503 | |||
1504 | explicit operator bool() const { return get(); } |
||
1505 | explicit operator MDTuple *() const { return get(); } |
||
1506 | |||
1507 | MDTuple *get() const { return const_cast<MDTuple *>(N); } |
||
1508 | MDTuple *operator->() const { return get(); } |
||
1509 | MDTuple &operator*() const { return *get(); } |
||
1510 | |||
1511 | // FIXME: Fix callers and remove condition on N. |
||
1512 | unsigned size() const { return N ? N->getNumOperands() : 0u; } |
||
1513 | bool empty() const { return N ? N->getNumOperands() == 0 : true; } |
||
1514 | T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); } |
||
1515 | |||
1516 | // FIXME: Fix callers and remove condition on N. |
||
1517 | using iterator = TypedMDOperandIterator<T>; |
||
1518 | |||
1519 | iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); } |
||
1520 | iterator end() const { return N ? iterator(N->op_end()) : iterator(); } |
||
1521 | }; |
||
1522 | |||
1523 | #define HANDLE_METADATA(CLASS) \ |
||
1524 | using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>; |
||
1525 | #include "llvm/IR/Metadata.def" |
||
1526 | |||
1527 | /// Placeholder metadata for operands of distinct MDNodes. |
||
1528 | /// |
||
1529 | /// This is a lightweight placeholder for an operand of a distinct node. It's |
||
1530 | /// purpose is to help track forward references when creating a distinct node. |
||
1531 | /// This allows distinct nodes involved in a cycle to be constructed before |
||
1532 | /// their operands without requiring a heavyweight temporary node with |
||
1533 | /// full-blown RAUW support. |
||
1534 | /// |
||
1535 | /// Each placeholder supports only a single MDNode user. Clients should pass |
||
1536 | /// an ID, retrieved via \a getID(), to indicate the "real" operand that this |
||
1537 | /// should be replaced with. |
||
1538 | /// |
||
1539 | /// While it would be possible to implement move operators, they would be |
||
1540 | /// fairly expensive. Leave them unimplemented to discourage their use |
||
1541 | /// (clients can use std::deque, std::list, BumpPtrAllocator, etc.). |
||
1542 | class DistinctMDOperandPlaceholder : public Metadata { |
||
1543 | friend class MetadataTracking; |
||
1544 | |||
1545 | Metadata **Use = nullptr; |
||
1546 | |||
1547 | public: |
||
1548 | explicit DistinctMDOperandPlaceholder(unsigned ID) |
||
1549 | : Metadata(DistinctMDOperandPlaceholderKind, Distinct) { |
||
1550 | SubclassData32 = ID; |
||
1551 | } |
||
1552 | |||
1553 | DistinctMDOperandPlaceholder() = delete; |
||
1554 | DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete; |
||
1555 | DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete; |
||
1556 | |||
1557 | ~DistinctMDOperandPlaceholder() { |
||
1558 | if (Use) |
||
1559 | *Use = nullptr; |
||
1560 | } |
||
1561 | |||
1562 | unsigned getID() const { return SubclassData32; } |
||
1563 | |||
1564 | /// Replace the use of this with MD. |
||
1565 | void replaceUseWith(Metadata *MD) { |
||
1566 | if (!Use) |
||
1567 | return; |
||
1568 | *Use = MD; |
||
1569 | |||
1570 | if (*Use) |
||
1571 | MetadataTracking::track(*Use); |
||
1572 | |||
1573 | Metadata *T = cast<Metadata>(this); |
||
1574 | MetadataTracking::untrack(T); |
||
1575 | assert(!Use && "Use is still being tracked despite being untracked!"); |
||
1576 | } |
||
1577 | }; |
||
1578 | |||
1579 | //===----------------------------------------------------------------------===// |
||
1580 | /// A tuple of MDNodes. |
||
1581 | /// |
||
1582 | /// Despite its name, a NamedMDNode isn't itself an MDNode. |
||
1583 | /// |
||
1584 | /// NamedMDNodes are named module-level entities that contain lists of MDNodes. |
||
1585 | /// |
||
1586 | /// It is illegal for a NamedMDNode to appear as an operand of an MDNode. |
||
1587 | class NamedMDNode : public ilist_node<NamedMDNode> { |
||
1588 | friend class LLVMContextImpl; |
||
1589 | friend class Module; |
||
1590 | |||
1591 | std::string Name; |
||
1592 | Module *Parent = nullptr; |
||
1593 | void *Operands; // SmallVector<TrackingMDRef, 4> |
||
1594 | |||
1595 | void setParent(Module *M) { Parent = M; } |
||
1596 | |||
1597 | explicit NamedMDNode(const Twine &N); |
||
1598 | |||
1599 | template <class T1, class T2> class op_iterator_impl { |
||
1600 | friend class NamedMDNode; |
||
1601 | |||
1602 | const NamedMDNode *Node = nullptr; |
||
1603 | unsigned Idx = 0; |
||
1604 | |||
1605 | op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {} |
||
1606 | |||
1607 | public: |
||
1608 | using iterator_category = std::bidirectional_iterator_tag; |
||
1609 | using value_type = T2; |
||
1610 | using difference_type = std::ptrdiff_t; |
||
1611 | using pointer = value_type *; |
||
1612 | using reference = value_type &; |
||
1613 | |||
1614 | op_iterator_impl() = default; |
||
1615 | |||
1616 | bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; } |
||
1617 | bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; } |
||
1618 | |||
1619 | op_iterator_impl &operator++() { |
||
1620 | ++Idx; |
||
1621 | return *this; |
||
1622 | } |
||
1623 | |||
1624 | op_iterator_impl operator++(int) { |
||
1625 | op_iterator_impl tmp(*this); |
||
1626 | operator++(); |
||
1627 | return tmp; |
||
1628 | } |
||
1629 | |||
1630 | op_iterator_impl &operator--() { |
||
1631 | --Idx; |
||
1632 | return *this; |
||
1633 | } |
||
1634 | |||
1635 | op_iterator_impl operator--(int) { |
||
1636 | op_iterator_impl tmp(*this); |
||
1637 | operator--(); |
||
1638 | return tmp; |
||
1639 | } |
||
1640 | |||
1641 | T1 operator*() const { return Node->getOperand(Idx); } |
||
1642 | }; |
||
1643 | |||
1644 | public: |
||
1645 | NamedMDNode(const NamedMDNode &) = delete; |
||
1646 | ~NamedMDNode(); |
||
1647 | |||
1648 | /// Drop all references and remove the node from parent module. |
||
1649 | void eraseFromParent(); |
||
1650 | |||
1651 | /// Remove all uses and clear node vector. |
||
1652 | void dropAllReferences() { clearOperands(); } |
||
1653 | /// Drop all references to this node's operands. |
||
1654 | void clearOperands(); |
||
1655 | |||
1656 | /// Get the module that holds this named metadata collection. |
||
1657 | inline Module *getParent() { return Parent; } |
||
1658 | inline const Module *getParent() const { return Parent; } |
||
1659 | |||
1660 | MDNode *getOperand(unsigned i) const; |
||
1661 | unsigned getNumOperands() const; |
||
1662 | void addOperand(MDNode *M); |
||
1663 | void setOperand(unsigned I, MDNode *New); |
||
1664 | StringRef getName() const; |
||
1665 | void print(raw_ostream &ROS, bool IsForDebug = false) const; |
||
1666 | void print(raw_ostream &ROS, ModuleSlotTracker &MST, |
||
1667 | bool IsForDebug = false) const; |
||
1668 | void dump() const; |
||
1669 | |||
1670 | // --------------------------------------------------------------------------- |
||
1671 | // Operand Iterator interface... |
||
1672 | // |
||
1673 | using op_iterator = op_iterator_impl<MDNode *, MDNode>; |
||
1674 | |||
1675 | op_iterator op_begin() { return op_iterator(this, 0); } |
||
1676 | op_iterator op_end() { return op_iterator(this, getNumOperands()); } |
||
1677 | |||
1678 | using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>; |
||
1679 | |||
1680 | const_op_iterator op_begin() const { return const_op_iterator(this, 0); } |
||
1681 | const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); } |
||
1682 | |||
1683 | inline iterator_range<op_iterator> operands() { |
||
1684 | return make_range(op_begin(), op_end()); |
||
1685 | } |
||
1686 | inline iterator_range<const_op_iterator> operands() const { |
||
1687 | return make_range(op_begin(), op_end()); |
||
1688 | } |
||
1689 | }; |
||
1690 | |||
1691 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
||
1692 | DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef) |
||
1693 | |||
1694 | } // end namespace llvm |
||
1695 | |||
1696 | #endif // LLVM_IR_METADATA_H |