| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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/SmallVector.h" |
| 24 |
#include "llvm/ADT/StringRef.h" |
| 25 |
#include "llvm/ADT/ilist_node.h" |
| 26 |
#include "llvm/ADT/iterator_range.h" |
| 27 |
#include "llvm/IR/Constant.h" |
| 28 |
#include "llvm/IR/LLVMContext.h" |
| 29 |
#include "llvm/IR/Value.h" |
| 30 |
#include "llvm/Support/CBindingWrapping.h" |
| 31 |
#include "llvm/Support/Casting.h" |
| 32 |
#include "llvm/Support/ErrorHandling.h" |
| 33 |
#include <cassert> |
| 34 |
#include <cstddef> |
| 35 |
#include <cstdint> |
| 36 |
#include <iterator> |
| 37 |
#include <memory> |
| 38 |
#include <string> |
| 39 |
#include <type_traits> |
| 40 |
#include <utility> |
| 41 |
|
| 42 |
namespace llvm { |
| 43 |
|
| 44 |
class Module; |
| 45 |
class ModuleSlotTracker; |
| 46 |
class raw_ostream; |
| 47 |
template <typename T> class StringMapEntry; |
| 48 |
template <typename ValueTy> class StringMapEntryStorage; |
| 49 |
class Type; |
| 50 |
|
| 51 |
enum LLVMConstants : uint32_t { |
| 52 |
DEBUG_METADATA_VERSION = 3 |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
const uint64_t NOMORE_ICP_MAGICNUM = -1; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
class Metadata { |
| 63 |
friend class ReplaceableMetadataImpl; |
| 64 |
|
| 65 |
|
| 66 |
const unsigned char SubclassID; |
| 67 |
|
| 68 |
protected: |
| 69 |
|
| 70 |
enum StorageType { Uniqued, Distinct, Temporary }; |
| 71 |
|
| 72 |
|
| 73 |
unsigned char Storage : 7; |
| 74 |
|
| 75 |
unsigned char SubclassData1 : 1; |
| 76 |
unsigned short SubclassData16 = 0; |
| 77 |
unsigned SubclassData32 = 0; |
| 78 |
|
| 79 |
public: |
| 80 |
enum MetadataKind { |
| 81 |
#define HANDLE_METADATA_LEAF(CLASS) CLASS##Kind, |
| 82 |
#include "llvm/IR/Metadata.def" |
| 83 |
}; |
| 84 |
|
| 85 |
protected: |
| 86 |
Metadata(unsigned ID, StorageType Storage) |
| 87 |
: SubclassID(ID), Storage(Storage), SubclassData1(false) { |
| 88 |
static_assert(sizeof(*this) == 8, "Metadata fields poorly packed"); |
| 89 |
} |
| 90 |
|
| 91 |
~Metadata() = default; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
void handleChangedOperand(void *, Metadata *) { |
| 98 |
llvm_unreachable("Unimplemented in Metadata subclass"); |
| 99 |
} |
| 100 |
|
| 101 |
public: |
| 102 |
unsigned getMetadataID() const { return SubclassID; } |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void dump() const; |
| 114 |
void dump(const Module *M) const; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void print(raw_ostream &OS, const Module *M = nullptr, |
| 125 |
bool IsForDebug = false) const; |
| 126 |
void print(raw_ostream &OS, ModuleSlotTracker &MST, const Module *M = nullptr, |
| 127 |
bool IsForDebug = false) const; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
void printAsOperand(raw_ostream &OS, const Module *M = nullptr) const; |
| 138 |
void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST, |
| 139 |
const Module *M = nullptr) const; |
| 140 |
|
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
DEFINE_ISA_CONVERSION_FUNCTIONS(Metadata, LLVMMetadataRef) |
| 145 |
|
| 146 |
|
| 147 |
inline Metadata **unwrap(LLVMMetadataRef *MDs) { |
| 148 |
return reinterpret_cast<Metadata**>(MDs); |
| 149 |
} |
| 150 |
|
| 151 |
#define HANDLE_METADATA(CLASS) class CLASS; |
| 152 |
#include "llvm/IR/Metadata.def" |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#define HANDLE_METADATA_LEAF(CLASS) \ |
| 157 |
template <> struct isa_impl<CLASS, Metadata> { \ |
| 158 |
static inline bool doit(const Metadata &MD) { \ |
| 159 |
return MD.getMetadataID() == Metadata::CLASS##Kind; \ |
| 160 |
} \ |
| 161 |
}; |
| 162 |
#include "llvm/IR/Metadata.def" |
| 163 |
|
| 164 |
inline raw_ostream &operator<<(raw_ostream &OS, const Metadata &MD) { |
| 165 |
MD.print(OS); |
| 166 |
return OS; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
class MetadataAsValue : public Value { |
| 177 |
friend class ReplaceableMetadataImpl; |
| 178 |
friend class LLVMContextImpl; |
| 179 |
|
| 180 |
Metadata *MD; |
| 181 |
|
| 182 |
MetadataAsValue(Type *Ty, Metadata *MD); |
| 183 |
|
| 184 |
|
| 185 |
void dropUse() { MD = nullptr; } |
| 186 |
|
| 187 |
public: |
| 188 |
~MetadataAsValue(); |
| 189 |
|
| 190 |
static MetadataAsValue *get(LLVMContext &Context, Metadata *MD); |
| 191 |
static MetadataAsValue *getIfExists(LLVMContext &Context, Metadata *MD); |
| 192 |
|
| 193 |
Metadata *getMetadata() const { return MD; } |
| 194 |
|
| 195 |
static bool classof(const Value *V) { |
| 196 |
return V->getValueID() == MetadataAsValueVal; |
| 197 |
} |
| 198 |
|
| 199 |
private: |
| 200 |
void handleChangedMetadata(Metadata *MD); |
| 201 |
void track(); |
| 202 |
void untrack(); |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
class MetadataTracking { |
| 213 |
public: |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
static bool track(Metadata *&MD) { |
| 224 |
return track(&MD, *MD, static_cast<Metadata *>(nullptr)); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
static bool track(void *Ref, Metadata &MD, Metadata &Owner) { |
| 233 |
return track(Ref, MD, &Owner); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
static bool track(void *Ref, Metadata &MD, MetadataAsValue &Owner) { |
| 242 |
return track(Ref, MD, &Owner); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
static void untrack(Metadata *&MD) { untrack(&MD, *MD); } |
| 249 |
static void untrack(void *Ref, Metadata &MD); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
static bool retrack(Metadata *&MD, Metadata *&New) { |
| 260 |
return retrack(&MD, *MD, &New); |
| 261 |
} |
| 262 |
static bool retrack(void *Ref, Metadata &MD, void *New); |
| 263 |
|
| 264 |
|
| 265 |
static bool isReplaceable(const Metadata &MD); |
| 266 |
|
| 267 |
using OwnerTy = PointerUnion<MetadataAsValue *, Metadata *>; |
| 268 |
|
| 269 |
private: |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
static bool track(void *Ref, Metadata &MD, OwnerTy Owner); |
| 274 |
}; |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
class ReplaceableMetadataImpl { |
| 282 |
friend class MetadataTracking; |
| 283 |
|
| 284 |
public: |
| 285 |
using OwnerTy = MetadataTracking::OwnerTy; |
| 286 |
|
| 287 |
private: |
| 288 |
LLVMContext &Context; |
| 289 |
uint64_t NextIndex = 0; |
| 290 |
SmallDenseMap<void *, std::pair<OwnerTy, uint64_t>, 4> UseMap; |
| 291 |
|
| 292 |
public: |
| 293 |
ReplaceableMetadataImpl(LLVMContext &Context) : Context(Context) {} |
| 294 |
|
| 295 |
~ReplaceableMetadataImpl() { |
| 296 |
assert(UseMap.empty() && "Cannot destroy in-use replaceable metadata"); |
| 297 |
} |
| 298 |
|
| 299 |
LLVMContext &getContext() const { return Context; } |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
void replaceAllUsesWith(Metadata *MD); |
| 305 |
|
| 306 |
|
| 307 |
SmallVector<Metadata *> getAllArgListUsers(); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 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 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static ReplaceableMetadataImpl *getOrCreate(Metadata &MD); |
| 326 |
|
| 327 |
|
| 328 |
static ReplaceableMetadataImpl *getIfExists(Metadata &MD); |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static bool isReplaceable(const Metadata &MD); |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
class ValueAsMetadata : public Metadata, ReplaceableMetadataImpl { |
| 345 |
friend class ReplaceableMetadataImpl; |
| 346 |
friend class LLVMContextImpl; |
| 347 |
|
| 348 |
Value *V; |
| 349 |
|
| 350 |
|
| 351 |
void dropUsers() { |
| 352 |
ReplaceableMetadataImpl::resolveAllUses( 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 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 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 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 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 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 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 |
|
| 553 |
|
| 554 |
|
| 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 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 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 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 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 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 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 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 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 |
|
| 635 |
iterator begin() const { return getString().begin(); } |
| 636 |
|
| 637 |
|
| 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 |
|
| 644 |
static bool classof(const Metadata *MD) { |
| 645 |
return MD->getMetadataID() == MDStringKind; |
| 646 |
} |
| 647 |
}; |
| 648 |
|
| 649 |
|
| 650 |
|
| 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 |
|
| 668 |
MDNode *TBAA = nullptr; |
| 669 |
|
| 670 |
|
| 671 |
MDNode *TBAAStruct = nullptr; |
| 672 |
|
| 673 |
|
| 674 |
MDNode *Scope = nullptr; |
| 675 |
|
| 676 |
|
| 677 |
MDNode *NoAlias = nullptr; |
| 678 |
|
| 679 |
|
| 680 |
static MDNode *shiftTBAA(MDNode *M, size_t off); |
| 681 |
|
| 682 |
|
| 683 |
static MDNode *shiftTBAAStruct(MDNode *M, size_t off); |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
static MDNode *extendToTBAA(MDNode *TBAA, ssize_t len); |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 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 |
|
| 704 |
|
| 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 |
|
| 716 |
|
| 717 |
|
| 718 |
AAMDNodes extendTo(ssize_t Len) const { |
| 719 |
AAMDNodes Result; |
| 720 |
Result.TBAA = TBAA ? extendToTBAA(TBAA, Len) : nullptr; |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
Result.TBAAStruct = TBAAStruct; |
| 725 |
Result.Scope = Scope; |
| 726 |
Result.NoAlias = NoAlias; |
| 727 |
return Result; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
AAMDNodes merge(const AAMDNodes &Other) const; |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
AAMDNodes concat(const AAMDNodes &Other) const; |
| 738 |
}; |
| 739 |
|
| 740 |
|
| 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 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
class MDOperand { |
| 773 |
Metadata *MD = nullptr; |
| 774 |
|
| 775 |
public: |
| 776 |
MDOperand() = default; |
| 777 |
MDOperand(MDOperand &&) = delete; |
| 778 |
MDOperand(const MDOperand &) = delete; |
| 779 |
MDOperand &operator=(MDOperand &&) = delete; |
| 780 |
MDOperand &operator=(const MDOperand &) = delete; |
| 781 |
~MDOperand() { untrack(); } |
| 782 |
|
| 783 |
Metadata *get() const { return MD; } |
| 784 |
operator Metadata *() const { return get(); } |
| 785 |
Metadata *operator->() const { return get(); } |
| 786 |
Metadata &operator*() const { return *get(); } |
| 787 |
|
| 788 |
void reset() { |
| 789 |
untrack(); |
| 790 |
MD = nullptr; |
| 791 |
} |
| 792 |
void reset(Metadata *MD, Metadata *Owner) { |
| 793 |
untrack(); |
| 794 |
this->MD = MD; |
| 795 |
track(Owner); |
| 796 |
} |
| 797 |
|
| 798 |
private: |
| 799 |
void track(Metadata *Owner) { |
| 800 |
if (MD) { |
| 801 |
if (Owner) |
| 802 |
MetadataTracking::track(this, *MD, *Owner); |
| 803 |
else |
| 804 |
MetadataTracking::track(MD); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
void untrack() { |
| 809 |
assert(static_cast<void *>(this) == &MD && "Expected same address"); |
| 810 |
if (MD) |
| 811 |
MetadataTracking::untrack(MD); |
| 812 |
} |
| 813 |
}; |
| 814 |
|
| 815 |
template <> struct simplify_type<MDOperand> { |
| 816 |
using SimpleType = Metadata *; |
| 817 |
|
| 818 |
static SimpleType getSimplifiedValue(MDOperand &MD) { return MD.get(); } |
| 819 |
}; |
| 820 |
|
| 821 |
template <> struct simplify_type<const MDOperand> { |
| 822 |
using SimpleType = Metadata *; |
| 823 |
|
| 824 |
static SimpleType getSimplifiedValue(const MDOperand &MD) { return MD.get(); } |
| 825 |
}; |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
class ContextAndReplaceableUses { |
| 832 |
PointerUnion<LLVMContext *, ReplaceableMetadataImpl *> Ptr; |
| 833 |
|
| 834 |
public: |
| 835 |
ContextAndReplaceableUses(LLVMContext &Context) : Ptr(&Context) {} |
| 836 |
ContextAndReplaceableUses( |
| 837 |
std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) |
| 838 |
: Ptr(ReplaceableUses.release()) { |
| 839 |
assert(getReplaceableUses() && "Expected non-null replaceable uses"); |
| 840 |
} |
| 841 |
ContextAndReplaceableUses() = delete; |
| 842 |
ContextAndReplaceableUses(ContextAndReplaceableUses &&) = delete; |
| 843 |
ContextAndReplaceableUses(const ContextAndReplaceableUses &) = delete; |
| 844 |
ContextAndReplaceableUses &operator=(ContextAndReplaceableUses &&) = delete; |
| 845 |
ContextAndReplaceableUses & |
| 846 |
operator=(const ContextAndReplaceableUses &) = delete; |
| 847 |
~ContextAndReplaceableUses() { delete getReplaceableUses(); } |
| 848 |
|
| 849 |
operator LLVMContext &() { return getContext(); } |
| 850 |
|
| 851 |
|
| 852 |
bool hasReplaceableUses() const { |
| 853 |
return Ptr.is<ReplaceableMetadataImpl *>(); |
| 854 |
} |
| 855 |
|
| 856 |
LLVMContext &getContext() const { |
| 857 |
if (hasReplaceableUses()) |
| 858 |
return getReplaceableUses()->getContext(); |
| 859 |
return *Ptr.get<LLVMContext *>(); |
| 860 |
} |
| 861 |
|
| 862 |
ReplaceableMetadataImpl *getReplaceableUses() const { |
| 863 |
if (hasReplaceableUses()) |
| 864 |
return Ptr.get<ReplaceableMetadataImpl *>(); |
| 865 |
return nullptr; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
ReplaceableMetadataImpl *getOrCreateReplaceableUses() { |
| 870 |
if (!hasReplaceableUses()) |
| 871 |
makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(getContext())); |
| 872 |
return getReplaceableUses(); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
void |
| 880 |
makeReplaceable(std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses) { |
| 881 |
assert(ReplaceableUses && "Expected non-null replaceable uses"); |
| 882 |
assert(&ReplaceableUses->getContext() == &getContext() && |
| 883 |
"Expected same context"); |
| 884 |
delete getReplaceableUses(); |
| 885 |
Ptr = ReplaceableUses.release(); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
std::unique_ptr<ReplaceableMetadataImpl> takeReplaceableUses() { |
| 892 |
assert(hasReplaceableUses() && "Expected to own replaceable uses"); |
| 893 |
std::unique_ptr<ReplaceableMetadataImpl> ReplaceableUses( |
| 894 |
getReplaceableUses()); |
| 895 |
Ptr = &ReplaceableUses->getContext(); |
| 896 |
return ReplaceableUses; |
| 897 |
} |
| 898 |
}; |
| 899 |
|
| 900 |
struct TempMDNodeDeleter { |
| 901 |
inline void operator()(MDNode *Node) const; |
| 902 |
}; |
| 903 |
|
| 904 |
#define HANDLE_MDNODE_LEAF(CLASS) \ |
| 905 |
using Temp##CLASS = std::unique_ptr<CLASS, TempMDNodeDeleter>; |
| 906 |
#define HANDLE_MDNODE_BRANCH(CLASS) HANDLE_MDNODE_LEAF(CLASS) |
| 907 |
#include "llvm/IR/Metadata.def" |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
class MDNode : public Metadata { |
| 926 |
friend class ReplaceableMetadataImpl; |
| 927 |
friend class LLVMContextImpl; |
| 928 |
friend class DIArgList; |
| 929 |
|
| 930 |
unsigned NumOperands; |
| 931 |
unsigned NumUnresolved; |
| 932 |
|
| 933 |
ContextAndReplaceableUses Context; |
| 934 |
|
| 935 |
protected: |
| 936 |
MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, |
| 937 |
ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2 = None); |
| 938 |
~MDNode() = default; |
| 939 |
|
| 940 |
void *operator new(size_t Size, unsigned NumOps); |
| 941 |
void operator delete(void *Mem); |
| 942 |
|
| 943 |
|
| 944 |
void operator delete(void *, unsigned) { |
| 945 |
llvm_unreachable("Constructor throws?"); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
void operator delete(void *, unsigned, bool) { |
| 950 |
llvm_unreachable("Constructor throws?"); |
| 951 |
} |
| 952 |
|
| 953 |
void dropAllReferences(); |
| 954 |
|
| 955 |
MDOperand *mutable_begin() { return mutable_end() - NumOperands; } |
| 956 |
MDOperand *mutable_end() { return reinterpret_cast<MDOperand *>(this); } |
| 957 |
|
| 958 |
using mutable_op_range = iterator_range<MDOperand *>; |
| 959 |
|
| 960 |
mutable_op_range mutable_operands() { |
| 961 |
return mutable_op_range(mutable_begin(), mutable_end()); |
| 962 |
} |
| 963 |
|
| 964 |
public: |
| 965 |
MDNode(const MDNode &) = delete; |
| 966 |
void operator=(const MDNode &) = delete; |
| 967 |
void *operator new(size_t) = delete; |
| 968 |
|
| 969 |
static inline MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs); |
| 970 |
static inline MDTuple *getIfExists(LLVMContext &Context, |
| 971 |
ArrayRef<Metadata *> MDs); |
| 972 |
static inline MDTuple *getDistinct(LLVMContext &Context, |
| 973 |
ArrayRef<Metadata *> MDs); |
| 974 |
static inline TempMDTuple getTemporary(LLVMContext &Context, |
| 975 |
ArrayRef<Metadata *> MDs); |
| 976 |
|
| 977 |
|
| 978 |
TempMDNode clone() const; |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static void deleteTemporary(MDNode *N); |
| 985 |
|
| 986 |
LLVMContext &getContext() const { return Context.getContext(); } |
| 987 |
|
| 988 |
|
| 989 |
void replaceOperandWith(unsigned I, Metadata *New); |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
bool isResolved() const { return !isTemporary() && !NumUnresolved; } |
| 1003 |
|
| 1004 |
bool isUniqued() const { return Storage == Uniqued; } |
| 1005 |
bool isDistinct() const { return Storage == Distinct; } |
| 1006 |
bool isTemporary() const { return Storage == Temporary; } |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
void replaceAllUsesWith(Metadata *MD) { |
| 1012 |
assert(isTemporary() && "Expected temporary node"); |
| 1013 |
if (Context.hasReplaceableUses()) |
| 1014 |
Context.getReplaceableUses()->replaceAllUsesWith(MD); |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
void resolveCycles(); |
| 1024 |
|
| 1025 |
|
| 1026 |
void resolve(); |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
template <class T> |
| 1033 |
static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
| 1034 |
replaceWithPermanent(std::unique_ptr<T, TempMDNodeDeleter> N) { |
| 1035 |
return cast<T>(N.release()->replaceWithPermanentImpl()); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
template <class T> |
| 1045 |
static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
| 1046 |
replaceWithUniqued(std::unique_ptr<T, TempMDNodeDeleter> N) { |
| 1047 |
return cast<T>(N.release()->replaceWithUniquedImpl()); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
template <class T> |
| 1055 |
static std::enable_if_t<std::is_base_of<MDNode, T>::value, T *> |
| 1056 |
replaceWithDistinct(std::unique_ptr<T, TempMDNodeDeleter> N) { |
| 1057 |
return cast<T>(N.release()->replaceWithDistinctImpl()); |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
void printTree(raw_ostream &OS, const Module *M = nullptr) const; |
| 1068 |
void printTree(raw_ostream &OS, ModuleSlotTracker &MST, |
| 1069 |
const Module *M = nullptr) const; |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
void dumpTree() const; |
| 1082 |
void dumpTree(const Module *M) const; |
| 1083 |
|
| 1084 |
|
| 1085 |
private: |
| 1086 |
MDNode *replaceWithPermanentImpl(); |
| 1087 |
MDNode *replaceWithUniquedImpl(); |
| 1088 |
MDNode *replaceWithDistinctImpl(); |
| 1089 |
|
| 1090 |
protected: |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
void setOperand(unsigned I, Metadata *New); |
| 1095 |
|
| 1096 |
void storeDistinctInContext(); |
| 1097 |
template <class T, class StoreT> |
| 1098 |
static T *storeImpl(T *N, StorageType Storage, StoreT &Store); |
| 1099 |
template <class T> static T *storeImpl(T *N, StorageType Storage); |
| 1100 |
|
| 1101 |
private: |
| 1102 |
void handleChangedOperand(void *Ref, Metadata *New); |
| 1103 |
|
| 1104 |
|
| 1105 |
void dropReplaceableUses(); |
| 1106 |
|
| 1107 |
void resolveAfterOperandChange(Metadata *Old, Metadata *New); |
| 1108 |
void decrementUnresolvedOperandCount(); |
| 1109 |
void countUnresolvedOperands(); |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
void makeUniqued(); |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
void makeDistinct(); |
| 1123 |
|
| 1124 |
void deleteAsSubclass(); |
| 1125 |
MDNode *uniquify(); |
| 1126 |
void eraseFromStore(); |
| 1127 |
|
| 1128 |
template <class NodeTy> struct HasCachedHash; |
| 1129 |
template <class NodeTy> |
| 1130 |
static void dispatchRecalculateHash(NodeTy *N, std::true_type) { |
| 1131 |
N->recalculateHash(); |
| 1132 |
} |
| 1133 |
template <class NodeTy> |
| 1134 |
static void dispatchRecalculateHash(NodeTy *, std::false_type) {} |
| 1135 |
template <class NodeTy> |
| 1136 |
static void dispatchResetHash(NodeTy *N, std::true_type) { |
| 1137 |
N->setHash(0); |
| 1138 |
} |
| 1139 |
template <class NodeTy> |
| 1140 |
static void dispatchResetHash(NodeTy *, std::false_type) {} |
| 1141 |
|
| 1142 |
public: |
| 1143 |
using op_iterator = const MDOperand *; |
| 1144 |
using op_range = iterator_range<op_iterator>; |
| 1145 |
|
| 1146 |
op_iterator op_begin() const { |
| 1147 |
return const_cast<MDNode *>(this)->mutable_begin(); |
| 1148 |
} |
| 1149 |
|
| 1150 |
op_iterator op_end() const { |
| 1151 |
return const_cast<MDNode *>(this)->mutable_end(); |
| 1152 |
} |
| 1153 |
|
| 1154 |
op_range operands() const { return op_range(op_begin(), op_end()); } |
| 1155 |
|
| 1156 |
const MDOperand &getOperand(unsigned I) const { |
| 1157 |
assert(I < NumOperands && "Out of range"); |
| 1158 |
return op_begin()[I]; |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
unsigned getNumOperands() const { return NumOperands; } |
| 1163 |
|
| 1164 |
|
| 1165 |
static bool classof(const Metadata *MD) { |
| 1166 |
switch (MD->getMetadataID()) { |
| 1167 |
default: |
| 1168 |
return false; |
| 1169 |
#define HANDLE_MDNODE_LEAF(CLASS) \ |
| 1170 |
case CLASS##Kind: \ |
| 1171 |
return true; |
| 1172 |
#include "llvm/IR/Metadata.def" |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
bool isTBAAVtableAccess() const; |
| 1178 |
|
| 1179 |
|
| 1180 |
static MDNode *concatenate(MDNode *A, MDNode *B); |
| 1181 |
static MDNode *intersect(MDNode *A, MDNode *B); |
| 1182 |
static MDNode *getMostGenericTBAA(MDNode *A, MDNode *B); |
| 1183 |
static MDNode *getMostGenericFPMath(MDNode *A, MDNode *B); |
| 1184 |
static MDNode *getMostGenericRange(MDNode *A, MDNode *B); |
| 1185 |
static MDNode *getMostGenericAliasScope(MDNode *A, MDNode *B); |
| 1186 |
static MDNode *getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B); |
| 1187 |
}; |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
class MDTuple : public MDNode { |
| 1194 |
friend class LLVMContextImpl; |
| 1195 |
friend class MDNode; |
| 1196 |
|
| 1197 |
MDTuple(LLVMContext &C, StorageType Storage, unsigned Hash, |
| 1198 |
ArrayRef<Metadata *> Vals) |
| 1199 |
: MDNode(C, MDTupleKind, Storage, Vals) { |
| 1200 |
setHash(Hash); |
| 1201 |
} |
| 1202 |
|
| 1203 |
~MDTuple() { dropAllReferences(); } |
| 1204 |
|
| 1205 |
void setHash(unsigned Hash) { SubclassData32 = Hash; } |
| 1206 |
void recalculateHash(); |
| 1207 |
|
| 1208 |
static MDTuple *getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, |
| 1209 |
StorageType Storage, bool ShouldCreate = true); |
| 1210 |
|
| 1211 |
TempMDTuple cloneImpl() const { |
| 1212 |
return getTemporary(getContext(), SmallVector<Metadata *, 4>(operands())); |
| 1213 |
} |
| 1214 |
|
| 1215 |
public: |
| 1216 |
|
| 1217 |
unsigned getHash() const { return SubclassData32; } |
| 1218 |
|
| 1219 |
static MDTuple *get(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1220 |
return getImpl(Context, MDs, Uniqued); |
| 1221 |
} |
| 1222 |
|
| 1223 |
static MDTuple *getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1224 |
return getImpl(Context, MDs, Uniqued, false); |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
static MDTuple *getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1231 |
return getImpl(Context, MDs, Distinct); |
| 1232 |
} |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
static TempMDTuple getTemporary(LLVMContext &Context, |
| 1240 |
ArrayRef<Metadata *> MDs) { |
| 1241 |
return TempMDTuple(getImpl(Context, MDs, Temporary)); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
TempMDTuple clone() const { return cloneImpl(); } |
| 1246 |
|
| 1247 |
static bool classof(const Metadata *MD) { |
| 1248 |
return MD->getMetadataID() == MDTupleKind; |
| 1249 |
} |
| 1250 |
}; |
| 1251 |
|
| 1252 |
MDTuple *MDNode::get(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1253 |
return MDTuple::get(Context, MDs); |
| 1254 |
} |
| 1255 |
|
| 1256 |
MDTuple *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1257 |
return MDTuple::getIfExists(Context, MDs); |
| 1258 |
} |
| 1259 |
|
| 1260 |
MDTuple *MDNode::getDistinct(LLVMContext &Context, ArrayRef<Metadata *> MDs) { |
| 1261 |
return MDTuple::getDistinct(Context, MDs); |
| 1262 |
} |
| 1263 |
|
| 1264 |
TempMDTuple MDNode::getTemporary(LLVMContext &Context, |
| 1265 |
ArrayRef<Metadata *> MDs) { |
| 1266 |
return MDTuple::getTemporary(Context, MDs); |
| 1267 |
} |
| 1268 |
|
| 1269 |
void TempMDNodeDeleter::operator()(MDNode *Node) const { |
| 1270 |
MDNode::deleteTemporary(Node); |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
class AliasScopeNode { |
| 1277 |
const MDNode *Node = nullptr; |
| 1278 |
|
| 1279 |
public: |
| 1280 |
AliasScopeNode() = default; |
| 1281 |
explicit AliasScopeNode(const MDNode *N) : Node(N) {} |
| 1282 |
|
| 1283 |
|
| 1284 |
const MDNode *getNode() const { return Node; } |
| 1285 |
|
| 1286 |
|
| 1287 |
const MDNode *getDomain() const { |
| 1288 |
if (Node->getNumOperands() < 2) |
| 1289 |
return nullptr; |
| 1290 |
return dyn_cast_or_null<MDNode>(Node->getOperand(1)); |
| 1291 |
} |
| 1292 |
StringRef getName() const { |
| 1293 |
if (Node->getNumOperands() > 2) |
| 1294 |
if (MDString *N = dyn_cast_or_null<MDString>(Node->getOperand(2))) |
| 1295 |
return N->getString(); |
| 1296 |
return StringRef(); |
| 1297 |
} |
| 1298 |
}; |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
template <class T> class TypedMDOperandIterator { |
| 1305 |
MDNode::op_iterator I = nullptr; |
| 1306 |
|
| 1307 |
public: |
| 1308 |
using iterator_category = std::input_iterator_tag; |
| 1309 |
using value_type = T *; |
| 1310 |
using difference_type = std::ptrdiff_t; |
| 1311 |
using pointer = void; |
| 1312 |
using reference = T *; |
| 1313 |
|
| 1314 |
TypedMDOperandIterator() = default; |
| 1315 |
explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {} |
| 1316 |
|
| 1317 |
T *operator*() const { return cast_or_null<T>(*I); } |
| 1318 |
|
| 1319 |
TypedMDOperandIterator &operator++() { |
| 1320 |
++I; |
| 1321 |
return *this; |
| 1322 |
} |
| 1323 |
|
| 1324 |
TypedMDOperandIterator operator++(int) { |
| 1325 |
TypedMDOperandIterator Temp(*this); |
| 1326 |
++I; |
| 1327 |
return Temp; |
| 1328 |
} |
| 1329 |
|
| 1330 |
bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; } |
| 1331 |
bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; } |
| 1332 |
}; |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
template <class T> class MDTupleTypedArrayWrapper { |
| 1339 |
const MDTuple *N = nullptr; |
| 1340 |
|
| 1341 |
public: |
| 1342 |
MDTupleTypedArrayWrapper() = default; |
| 1343 |
MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {} |
| 1344 |
|
| 1345 |
template <class U> |
| 1346 |
MDTupleTypedArrayWrapper( |
| 1347 |
const MDTupleTypedArrayWrapper<U> &Other, |
| 1348 |
std::enable_if_t<std::is_convertible<U *, T *>::value> * = nullptr) |
| 1349 |
: N(Other.get()) {} |
| 1350 |
|
| 1351 |
template <class U> |
| 1352 |
explicit MDTupleTypedArrayWrapper( |
| 1353 |
const MDTupleTypedArrayWrapper<U> &Other, |
| 1354 |
std::enable_if_t<!std::is_convertible<U *, T *>::value> * = nullptr) |
| 1355 |
: N(Other.get()) {} |
| 1356 |
|
| 1357 |
explicit operator bool() const { return get(); } |
| 1358 |
explicit operator MDTuple *() const { return get(); } |
| 1359 |
|
| 1360 |
MDTuple *get() const { return const_cast<MDTuple *>(N); } |
| 1361 |
MDTuple *operator->() const { return get(); } |
| 1362 |
MDTuple &operator*() const { return *get(); } |
| 1363 |
|
| 1364 |
|
| 1365 |
unsigned size() const { return N ? N->getNumOperands() : 0u; } |
| 1366 |
bool empty() const { return N ? N->getNumOperands() == 0 : true; } |
| 1367 |
T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); } |
| 1368 |
|
| 1369 |
|
| 1370 |
using iterator = TypedMDOperandIterator<T>; |
| 1371 |
|
| 1372 |
iterator begin() const { return N ? iterator(N->op_begin()) : iterator(); } |
| 1373 |
iterator end() const { return N ? iterator(N->op_end()) : iterator(); } |
| 1374 |
}; |
| 1375 |
|
| 1376 |
#define HANDLE_METADATA(CLASS) \ |
| 1377 |
using CLASS##Array = MDTupleTypedArrayWrapper<CLASS>; |
| 1378 |
#include "llvm/IR/Metadata.def" |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
class DistinctMDOperandPlaceholder : public Metadata { |
| 1396 |
friend class MetadataTracking; |
| 1397 |
|
| 1398 |
Metadata **Use = nullptr; |
| 1399 |
|
| 1400 |
public: |
| 1401 |
explicit DistinctMDOperandPlaceholder(unsigned ID) |
| 1402 |
: Metadata(DistinctMDOperandPlaceholderKind, Distinct) { |
| 1403 |
SubclassData32 = ID; |
| 1404 |
} |
| 1405 |
|
| 1406 |
DistinctMDOperandPlaceholder() = delete; |
| 1407 |
DistinctMDOperandPlaceholder(DistinctMDOperandPlaceholder &&) = delete; |
| 1408 |
DistinctMDOperandPlaceholder(const DistinctMDOperandPlaceholder &) = delete; |
| 1409 |
|
| 1410 |
~DistinctMDOperandPlaceholder() { |
| 1411 |
if (Use) |
| 1412 |
*Use = nullptr; |
| 1413 |
} |
| 1414 |
|
| 1415 |
unsigned getID() const { return SubclassData32; } |
| 1416 |
|
| 1417 |
|
| 1418 |
void replaceUseWith(Metadata *MD) { |
| 1419 |
if (!Use) |
| 1420 |
return; |
| 1421 |
*Use = MD; |
| 1422 |
|
| 1423 |
if (*Use) |
| 1424 |
MetadataTracking::track(*Use); |
| 1425 |
|
| 1426 |
Metadata *T = cast<Metadata>(this); |
| 1427 |
MetadataTracking::untrack(T); |
| 1428 |
assert(!Use && "Use is still being tracked despite being untracked!"); |
| 1429 |
} |
| 1430 |
}; |
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
class NamedMDNode : public ilist_node<NamedMDNode> { |
| 1441 |
friend class LLVMContextImpl; |
| 1442 |
friend class Module; |
| 1443 |
|
| 1444 |
std::string Name; |
| 1445 |
Module *Parent = nullptr; |
| 1446 |
void *Operands; |
| 1447 |
|
| 1448 |
void setParent(Module *M) { Parent = M; } |
| 1449 |
|
| 1450 |
explicit NamedMDNode(const Twine &N); |
| 1451 |
|
| 1452 |
template <class T1, class T2> class op_iterator_impl { |
| 1453 |
friend class NamedMDNode; |
| 1454 |
|
| 1455 |
const NamedMDNode *Node = nullptr; |
| 1456 |
unsigned Idx = 0; |
| 1457 |
|
| 1458 |
op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {} |
| 1459 |
|
| 1460 |
public: |
| 1461 |
using iterator_category = std::bidirectional_iterator_tag; |
| 1462 |
using value_type = T2; |
| 1463 |
using difference_type = std::ptrdiff_t; |
| 1464 |
using pointer = value_type *; |
| 1465 |
using reference = value_type &; |
| 1466 |
|
| 1467 |
op_iterator_impl() = default; |
| 1468 |
|
| 1469 |
bool operator==(const op_iterator_impl &o) const { return Idx == o.Idx; } |
| 1470 |
bool operator!=(const op_iterator_impl &o) const { return Idx != o.Idx; } |
| 1471 |
|
| 1472 |
op_iterator_impl &operator++() { |
| 1473 |
++Idx; |
| 1474 |
return *this; |
| 1475 |
} |
| 1476 |
|
| 1477 |
op_iterator_impl operator++(int) { |
| 1478 |
op_iterator_impl tmp(*this); |
| 1479 |
operator++(); |
| 1480 |
return tmp; |
| 1481 |
} |
| 1482 |
|
| 1483 |
op_iterator_impl &operator--() { |
| 1484 |
--Idx; |
| 1485 |
return *this; |
| 1486 |
} |
| 1487 |
|
| 1488 |
op_iterator_impl operator--(int) { |
| 1489 |
op_iterator_impl tmp(*this); |
| 1490 |
operator--(); |
| 1491 |
return tmp; |
| 1492 |
} |
| 1493 |
|
| 1494 |
T1 operator*() const { return Node->getOperand(Idx); } |
| 1495 |
}; |
| 1496 |
|
| 1497 |
public: |
| 1498 |
NamedMDNode(const NamedMDNode &) = delete; |
| 1499 |
~NamedMDNode(); |
| 1500 |
|
| 1501 |
|
| 1502 |
void eraseFromParent(); |
| 1503 |
|
| 1504 |
|
| 1505 |
void dropAllReferences() { clearOperands(); } |
| 1506 |
|
| 1507 |
void clearOperands(); |
| 1508 |
|
| 1509 |
|
| 1510 |
inline Module *getParent() { return Parent; } |
| 1511 |
inline const Module *getParent() const { return Parent; } |
| 1512 |
|
| 1513 |
MDNode *getOperand(unsigned i) const; |
| 1514 |
unsigned getNumOperands() const; |
| 1515 |
void addOperand(MDNode *M); |
| 1516 |
void setOperand(unsigned I, MDNode *New); |
| 1517 |
StringRef getName() const; |
| 1518 |
void print(raw_ostream &ROS, bool IsForDebug = false) const; |
| 1519 |
void print(raw_ostream &ROS, ModuleSlotTracker &MST, |
| 1520 |
bool IsForDebug = false) const; |
| 1521 |
void dump() const; |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
using op_iterator = op_iterator_impl<MDNode *, MDNode>; |
| 1527 |
|
| 1528 |
op_iterator op_begin() { return op_iterator(this, 0); } |
| 1529 |
op_iterator op_end() { return op_iterator(this, getNumOperands()); } |
| 1530 |
|
| 1531 |
using const_op_iterator = op_iterator_impl<const MDNode *, MDNode>; |
| 1532 |
|
| 1533 |
const_op_iterator op_begin() const { return const_op_iterator(this, 0); } |
| 1534 |
const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); } |
| 1535 |
|
| 1536 |
inline iterator_range<op_iterator> operands() { |
| 1537 |
return make_range(op_begin(), op_end()); |
| 1538 |
} |
| 1539 |
inline iterator_range<const_op_iterator> operands() const { |
| 1540 |
return make_range(op_begin(), op_end()); |
| 1541 |
} |
| 1542 |
}; |
| 1543 |
|
| 1544 |
|
| 1545 |
DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef) |
| 1546 |
|
| 1547 |
} |
| 1548 |
|
| 1549 |
#endif |