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