| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef LLVM_IR_VALUE_H |
| 14 |
#define LLVM_IR_VALUE_H |
| 15 |
|
| 16 |
#include "llvm-c/Types.h" |
| 17 |
#include "llvm/ADT/STLExtras.h" |
| 18 |
#include "llvm/ADT/StringRef.h" |
| 19 |
#include "llvm/ADT/iterator_range.h" |
| 20 |
#include "llvm/IR/Use.h" |
| 21 |
#include "llvm/Support/Alignment.h" |
| 22 |
#include "llvm/Support/CBindingWrapping.h" |
| 23 |
#include "llvm/Support/Casting.h" |
| 24 |
#include <cassert> |
| 25 |
#include <iterator> |
| 26 |
#include <memory> |
| 27 |
|
| 28 |
namespace llvm { |
| 29 |
|
| 30 |
class APInt; |
| 31 |
class Argument; |
| 32 |
class BasicBlock; |
| 33 |
class Constant; |
| 34 |
class ConstantData; |
| 35 |
class ConstantAggregate; |
| 36 |
class DataLayout; |
| 37 |
class Function; |
| 38 |
class GlobalAlias; |
| 39 |
class GlobalIFunc; |
| 40 |
class GlobalObject; |
| 41 |
class GlobalValue; |
| 42 |
class GlobalVariable; |
| 43 |
class InlineAsm; |
| 44 |
class Instruction; |
| 45 |
class LLVMContext; |
| 46 |
class MDNode; |
| 47 |
class Module; |
| 48 |
class ModuleSlotTracker; |
| 49 |
class raw_ostream; |
| 50 |
template<typename ValueTy> class StringMapEntry; |
| 51 |
class Twine; |
| 52 |
class Type; |
| 53 |
class User; |
| 54 |
|
| 55 |
using ValueName = StringMapEntry<Value *>; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
class Value { |
| 75 |
Type *VTy; |
| 76 |
Use *UseList; |
| 77 |
|
| 78 |
friend class ValueAsMetadata; |
| 79 |
friend class ValueHandleBase; |
| 80 |
|
| 81 |
const unsigned char SubclassID; |
| 82 |
unsigned char HasValueHandle : 1; |
| 83 |
|
| 84 |
protected: |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
unsigned char SubclassOptionalData : 7; |
| 91 |
|
| 92 |
private: |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
unsigned short SubclassData; |
| 99 |
|
| 100 |
protected: |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
enum : unsigned { NumUserOperandsBits = 27 }; |
| 114 |
unsigned NumUserOperands : NumUserOperandsBits; |
| 115 |
|
| 116 |
|
| 117 |
unsigned IsUsedByMD : 1; |
| 118 |
unsigned HasName : 1; |
| 119 |
unsigned HasMetadata : 1; |
| 120 |
unsigned HasHungOffUses : 1; |
| 121 |
unsigned HasDescriptor : 1; |
| 122 |
|
| 123 |
private: |
| 124 |
template <typename UseT> |
| 125 |
class use_iterator_impl { |
| 126 |
friend class Value; |
| 127 |
|
| 128 |
UseT *U; |
| 129 |
|
| 130 |
explicit use_iterator_impl(UseT *u) : U(u) {} |
| 131 |
|
| 132 |
public: |
| 133 |
using iterator_category = std::forward_iterator_tag; |
| 134 |
using value_type = UseT *; |
| 135 |
using difference_type = std::ptrdiff_t; |
| 136 |
using pointer = value_type *; |
| 137 |
using reference = value_type &; |
| 138 |
|
| 139 |
use_iterator_impl() : U() {} |
| 140 |
|
| 141 |
bool operator==(const use_iterator_impl &x) const { return U == x.U; } |
| 142 |
bool operator!=(const use_iterator_impl &x) const { return !operator==(x); } |
| 143 |
|
| 144 |
use_iterator_impl &operator++() { |
| 145 |
assert(U && "Cannot increment end iterator!"); |
| 146 |
U = U->getNext(); |
| 147 |
return *this; |
| 148 |
} |
| 149 |
|
| 150 |
use_iterator_impl operator++(int) { |
| 151 |
auto tmp = *this; |
| 152 |
++*this; |
| 153 |
return tmp; |
| 154 |
} |
| 155 |
|
| 156 |
UseT &operator*() const { |
| 157 |
assert(U && "Cannot dereference end iterator!"); |
| 158 |
return *U; |
| 159 |
} |
| 160 |
|
| 161 |
UseT *operator->() const { return &operator*(); } |
| 162 |
|
| 163 |
operator use_iterator_impl<const UseT>() const { |
| 164 |
return use_iterator_impl<const UseT>(U); |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
template <typename UserTy> |
| 169 |
class user_iterator_impl { |
| 170 |
use_iterator_impl<Use> UI; |
| 171 |
explicit user_iterator_impl(Use *U) : UI(U) {} |
| 172 |
friend class Value; |
| 173 |
|
| 174 |
public: |
| 175 |
using iterator_category = std::forward_iterator_tag; |
| 176 |
using value_type = UserTy *; |
| 177 |
using difference_type = std::ptrdiff_t; |
| 178 |
using pointer = value_type *; |
| 179 |
using reference = value_type &; |
| 180 |
|
| 181 |
user_iterator_impl() = default; |
| 182 |
|
| 183 |
bool operator==(const user_iterator_impl &x) const { return UI == x.UI; } |
| 184 |
bool operator!=(const user_iterator_impl &x) const { return !operator==(x); } |
| 185 |
|
| 186 |
|
| 187 |
bool atEnd() const { return *this == user_iterator_impl(); } |
| 188 |
|
| 189 |
user_iterator_impl &operator++() { |
| 190 |
++UI; |
| 191 |
return *this; |
| 192 |
} |
| 193 |
|
| 194 |
user_iterator_impl operator++(int) { |
| 195 |
auto tmp = *this; |
| 196 |
++*this; |
| 197 |
return tmp; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
UserTy *operator*() const { |
| 202 |
return UI->getUser(); |
| 203 |
} |
| 204 |
|
| 205 |
UserTy *operator->() const { return operator*(); } |
| 206 |
|
| 207 |
operator user_iterator_impl<const UserTy>() const { |
| 208 |
return user_iterator_impl<const UserTy>(*UI); |
| 209 |
} |
| 210 |
|
| 211 |
Use &getUse() const { return *UI; } |
| 212 |
}; |
| 213 |
|
| 214 |
protected: |
| 215 |
Value(Type *Ty, unsigned scid); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
~Value(); |
| 223 |
|
| 224 |
public: |
| 225 |
Value(const Value &) = delete; |
| 226 |
Value &operator=(const Value &) = delete; |
| 227 |
|
| 228 |
|
| 229 |
void deleteValue(); |
| 230 |
|
| 231 |
|
| 232 |
void dump() const; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
void print(raw_ostream &O, bool IsForDebug = false) const; |
| 237 |
void print(raw_ostream &O, ModuleSlotTracker &MST, |
| 238 |
bool IsForDebug = false) const; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
void printAsOperand(raw_ostream &O, bool PrintType = true, |
| 249 |
const Module *M = nullptr) const; |
| 250 |
void printAsOperand(raw_ostream &O, bool PrintType, |
| 251 |
ModuleSlotTracker &MST) const; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
Type *getType() const { return VTy; } |
| 256 |
|
| 257 |
|
| 258 |
LLVMContext &getContext() const; |
| 259 |
|
| 260 |
|
| 261 |
bool hasName() const { return HasName; } |
| 262 |
ValueName *getValueName() const; |
| 263 |
void setValueName(ValueName *VN); |
| 264 |
|
| 265 |
private: |
| 266 |
void destroyValueName(); |
| 267 |
enum class ReplaceMetadataUses { No, Yes }; |
| 268 |
void doRAUW(Value *New, ReplaceMetadataUses); |
| 269 |
void setNameImpl(const Twine &Name); |
| 270 |
|
| 271 |
public: |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
StringRef getName() const; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
void setName(const Twine &Name); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
void takeName(Value *V); |
| 292 |
|
| 293 |
#ifndef NDEBUG |
| 294 |
std::string getNameOrAsOperand() const; |
| 295 |
#endif |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
void replaceAllUsesWith(Value *V); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
void replaceNonMetadataUsesWith(Value *V); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
void replaceUsesWithIf(Value *New, |
| 315 |
llvm::function_ref<bool(Use &U)> ShouldReplace); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
void assertModuleIsMaterializedImpl() const; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
void assertModuleIsMaterialized() const { |
| 339 |
#ifndef NDEBUG |
| 340 |
assertModuleIsMaterializedImpl(); |
| 341 |
#endif |
| 342 |
} |
| 343 |
|
| 344 |
bool use_empty() const { |
| 345 |
assertModuleIsMaterialized(); |
| 346 |
return UseList == nullptr; |
| 347 |
} |
| 348 |
|
| 349 |
bool materialized_use_empty() const { |
| 350 |
return UseList == nullptr; |
| 351 |
} |
| 352 |
|
| 353 |
using use_iterator = use_iterator_impl<Use>; |
| 354 |
using const_use_iterator = use_iterator_impl<const Use>; |
| 355 |
|
| 356 |
use_iterator materialized_use_begin() { return use_iterator(UseList); } |
| 357 |
const_use_iterator materialized_use_begin() const { |
| 358 |
return const_use_iterator(UseList); |
| 359 |
} |
| 360 |
use_iterator use_begin() { |
| 361 |
assertModuleIsMaterialized(); |
| 362 |
return materialized_use_begin(); |
| 363 |
} |
| 364 |
const_use_iterator use_begin() const { |
| 365 |
assertModuleIsMaterialized(); |
| 366 |
return materialized_use_begin(); |
| 367 |
} |
| 368 |
use_iterator use_end() { return use_iterator(); } |
| 369 |
const_use_iterator use_end() const { return const_use_iterator(); } |
| 370 |
iterator_range<use_iterator> materialized_uses() { |
| 371 |
return make_range(materialized_use_begin(), use_end()); |
| 372 |
} |
| 373 |
iterator_range<const_use_iterator> materialized_uses() const { |
| 374 |
return make_range(materialized_use_begin(), use_end()); |
| 375 |
} |
| 376 |
iterator_range<use_iterator> uses() { |
| 377 |
assertModuleIsMaterialized(); |
| 378 |
return materialized_uses(); |
| 379 |
} |
| 380 |
iterator_range<const_use_iterator> uses() const { |
| 381 |
assertModuleIsMaterialized(); |
| 382 |
return materialized_uses(); |
| 383 |
} |
| 384 |
|
| 385 |
bool user_empty() const { |
| 386 |
assertModuleIsMaterialized(); |
| 387 |
return UseList == nullptr; |
| 388 |
} |
| 389 |
|
| 390 |
using user_iterator = user_iterator_impl<User>; |
| 391 |
using const_user_iterator = user_iterator_impl<const User>; |
| 392 |
|
| 393 |
user_iterator materialized_user_begin() { return user_iterator(UseList); } |
| 394 |
const_user_iterator materialized_user_begin() const { |
| 395 |
return const_user_iterator(UseList); |
| 396 |
} |
| 397 |
user_iterator user_begin() { |
| 398 |
assertModuleIsMaterialized(); |
| 399 |
return materialized_user_begin(); |
| 400 |
} |
| 401 |
const_user_iterator user_begin() const { |
| 402 |
assertModuleIsMaterialized(); |
| 403 |
return materialized_user_begin(); |
| 404 |
} |
| 405 |
user_iterator user_end() { return user_iterator(); } |
| 406 |
const_user_iterator user_end() const { return const_user_iterator(); } |
| 407 |
User *user_back() { |
| 408 |
assertModuleIsMaterialized(); |
| 409 |
return *materialized_user_begin(); |
| 410 |
} |
| 411 |
const User *user_back() const { |
| 412 |
assertModuleIsMaterialized(); |
| 413 |
return *materialized_user_begin(); |
| 414 |
} |
| 415 |
iterator_range<user_iterator> materialized_users() { |
| 416 |
return make_range(materialized_user_begin(), user_end()); |
| 417 |
} |
| 418 |
iterator_range<const_user_iterator> materialized_users() const { |
| 419 |
return make_range(materialized_user_begin(), user_end()); |
| 420 |
} |
| 421 |
iterator_range<user_iterator> users() { |
| 422 |
assertModuleIsMaterialized(); |
| 423 |
return materialized_users(); |
| 424 |
} |
| 425 |
iterator_range<const_user_iterator> users() const { |
| 426 |
assertModuleIsMaterialized(); |
| 427 |
return materialized_users(); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
bool hasOneUse() const { return hasSingleElement(uses()); } |
| 435 |
|
| 436 |
|
| 437 |
bool hasNUses(unsigned N) const; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
bool hasNUsesOrMore(unsigned N) const; |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
bool hasOneUser() const; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
Use *getSingleUndroppableUse(); |
| 457 |
const Use *getSingleUndroppableUse() const { |
| 458 |
return const_cast<Value *>(this)->getSingleUndroppableUse(); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
User *getUniqueUndroppableUser(); |
| 464 |
const User *getUniqueUndroppableUser() const { |
| 465 |
return const_cast<Value *>(this)->getUniqueUndroppableUser(); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
bool hasNUndroppableUses(unsigned N) const; |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
bool hasNUndroppableUsesOrMore(unsigned N) const; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
void dropDroppableUses(llvm::function_ref<bool(const Use *)> ShouldDrop = |
| 487 |
[](const Use *) { return true; }); |
| 488 |
|
| 489 |
|
| 490 |
void dropDroppableUsesIn(User &Usr); |
| 491 |
|
| 492 |
|
| 493 |
static void dropDroppableUse(Use &U); |
| 494 |
|
| 495 |
|
| 496 |
bool isUsedInBasicBlock(const BasicBlock *BB) const; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
unsigned getNumUses() const; |
| 503 |
|
| 504 |
|
| 505 |
void addUse(Use &U) { U.addToList(&UseList); } |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
enum ValueTy { |
| 514 |
#define HANDLE_VALUE(Name) Name##Val, |
| 515 |
#include "llvm/IR/Value.def" |
| 516 |
|
| 517 |
|
| 518 |
#define HANDLE_CONSTANT_MARKER(Marker, Constant) Marker = Constant##Val, |
| 519 |
#include "llvm/IR/Value.def" |
| 520 |
}; |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
unsigned getValueID() const { |
| 533 |
return SubclassID; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
unsigned getRawSubclassOptionalData() const { |
| 540 |
return SubclassOptionalData; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
void clearSubclassOptionalData() { |
| 545 |
SubclassOptionalData = 0; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
bool hasSameSubclassOptionalData(const Value *V) const { |
| 550 |
return SubclassOptionalData == V->SubclassOptionalData; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
bool hasValueHandle() const { return HasValueHandle; } |
| 555 |
|
| 556 |
|
| 557 |
bool isUsedByMetadata() const { return IsUsedByMD; } |
| 558 |
|
| 559 |
|
| 560 |
bool isTransitiveUsedByMetadataOnly() const; |
| 561 |
|
| 562 |
protected: |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
MDNode *getMetadata(unsigned KindID) const; |
| 569 |
MDNode *getMetadata(StringRef Kind) const; |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
void getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const; |
| 577 |
void getMetadata(StringRef Kind, SmallVectorImpl<MDNode *> &MDs) const; |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
void |
| 585 |
getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const; |
| 586 |
|
| 587 |
|
| 588 |
bool hasMetadata() const { return (bool)HasMetadata; } |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
bool hasMetadata(unsigned KindID) const { |
| 593 |
return getMetadata(KindID) != nullptr; |
| 594 |
} |
| 595 |
bool hasMetadata(StringRef Kind) const { |
| 596 |
return getMetadata(Kind) != nullptr; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
void setMetadata(unsigned KindID, MDNode *Node); |
| 606 |
void setMetadata(StringRef Kind, MDNode *Node); |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
void addMetadata(unsigned KindID, MDNode &MD); |
| 612 |
void addMetadata(StringRef Kind, MDNode &MD); |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
bool eraseMetadata(unsigned KindID); |
| 619 |
|
| 620 |
|
| 621 |
void clearMetadata(); |
| 622 |
|
| 623 |
public: |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
bool isSwiftError() const; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
const Value *stripPointerCasts() const; |
| 635 |
Value *stripPointerCasts() { |
| 636 |
return const_cast<Value *>( |
| 637 |
static_cast<const Value *>(this)->stripPointerCasts()); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
const Value *stripPointerCastsAndAliases() const; |
| 645 |
Value *stripPointerCastsAndAliases() { |
| 646 |
return const_cast<Value *>( |
| 647 |
static_cast<const Value *>(this)->stripPointerCastsAndAliases()); |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
const Value *stripPointerCastsSameRepresentation() const; |
| 656 |
Value *stripPointerCastsSameRepresentation() { |
| 657 |
return const_cast<Value *>(static_cast<const Value *>(this) |
| 658 |
->stripPointerCastsSameRepresentation()); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
const Value *stripPointerCastsForAliasAnalysis() const; |
| 668 |
Value *stripPointerCastsForAliasAnalysis() { |
| 669 |
return const_cast<Value *>(static_cast<const Value *>(this) |
| 670 |
->stripPointerCastsForAliasAnalysis()); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
const Value *stripInBoundsConstantOffsets() const; |
| 678 |
Value *stripInBoundsConstantOffsets() { |
| 679 |
return const_cast<Value *>( |
| 680 |
static_cast<const Value *>(this)->stripInBoundsConstantOffsets()); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
const Value *stripAndAccumulateConstantOffsets( |
| 713 |
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, |
| 714 |
bool AllowInvariantGroup = false, |
| 715 |
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis = |
| 716 |
nullptr) const; |
| 717 |
Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, |
| 718 |
bool AllowNonInbounds, |
| 719 |
bool AllowInvariantGroup = false) { |
| 720 |
return const_cast<Value *>( |
| 721 |
static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets( |
| 722 |
DL, Offset, AllowNonInbounds, AllowInvariantGroup)); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
const Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 728 |
APInt &Offset) const { |
| 729 |
return stripAndAccumulateConstantOffsets(DL, Offset, |
| 730 |
false); |
| 731 |
} |
| 732 |
Value *stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, |
| 733 |
APInt &Offset) { |
| 734 |
return stripAndAccumulateConstantOffsets(DL, Offset, |
| 735 |
false); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
const Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
| 743 |
[](const Value *) {}) const; |
| 744 |
inline Value *stripInBoundsOffsets(function_ref<void(const Value *)> Func = |
| 745 |
[](const Value *) {}) { |
| 746 |
return const_cast<Value *>( |
| 747 |
static_cast<const Value *>(this)->stripInBoundsOffsets(Func)); |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
bool canBeFreed() const; |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, |
| 766 |
bool &CanBeNull, |
| 767 |
bool &CanBeFreed) const; |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
Align getPointerAlignment(const DataLayout &DL) const; |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
const Value *DoPHITranslation(const BasicBlock *CurBB, |
| 782 |
const BasicBlock *PredBB) const; |
| 783 |
Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) { |
| 784 |
return const_cast<Value *>( |
| 785 |
static_cast<const Value *>(this)->DoPHITranslation(CurBB, PredBB)); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
static constexpr unsigned MaxAlignmentExponent = 32; |
| 793 |
static constexpr uint64_t MaximumAlignment = 1ULL << MaxAlignmentExponent; |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
void mutateType(Type *Ty) { |
| 802 |
VTy = Ty; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
template <class Compare> void sortUseList(Compare Cmp); |
| 810 |
|
| 811 |
|
| 812 |
void reverseUseList(); |
| 813 |
|
| 814 |
private: |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
template <class Compare> |
| 824 |
static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { |
| 825 |
Use *Merged; |
| 826 |
Use **Next = &Merged; |
| 827 |
|
| 828 |
while (true) { |
| 829 |
if (!L) { |
| 830 |
*Next = R; |
| 831 |
break; |
| 832 |
} |
| 833 |
if (!R) { |
| 834 |
*Next = L; |
| 835 |
break; |
| 836 |
} |
| 837 |
if (Cmp(*R, *L)) { |
| 838 |
*Next = R; |
| 839 |
Next = &R->Next; |
| 840 |
R = R->Next; |
| 841 |
} else { |
| 842 |
*Next = L; |
| 843 |
Next = &L->Next; |
| 844 |
L = L->Next; |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
return Merged; |
| 849 |
} |
| 850 |
|
| 851 |
protected: |
| 852 |
unsigned short getSubclassDataFromValue() const { return SubclassData; } |
| 853 |
void setValueSubclassData(unsigned short D) { SubclassData = D; } |
| 854 |
}; |
| 855 |
|
| 856 |
struct ValueDeleter { void operator()(Value *V) { V->deleteValue(); } }; |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
using unique_value = std::unique_ptr<Value, ValueDeleter>; |
| 862 |
|
| 863 |
inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { |
| 864 |
V.print(OS); |
| 865 |
return OS; |
| 866 |
} |
| 867 |
|
| 868 |
void Use::set(Value *V) { |
| 869 |
if (Val) removeFromList(); |
| 870 |
Val = V; |
| 871 |
if (V) V->addUse(*this); |
| 872 |
} |
| 873 |
|
| 874 |
Value *Use::operator=(Value *RHS) { |
| 875 |
set(RHS); |
| 876 |
return RHS; |
| 877 |
} |
| 878 |
|
| 879 |
const Use &Use::operator=(const Use &RHS) { |
| 880 |
set(RHS.Val); |
| 881 |
return *this; |
| 882 |
} |
| 883 |
|
| 884 |
template <class Compare> void Value::sortUseList(Compare Cmp) { |
| 885 |
if (!UseList || !UseList->Next) |
| 886 |
|
| 887 |
return; |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
const unsigned MaxSlots = 32; |
| 895 |
Use *Slots[MaxSlots]; |
| 896 |
|
| 897 |
|
| 898 |
Use *Next = UseList->Next; |
| 899 |
UseList->Next = nullptr; |
| 900 |
unsigned NumSlots = 1; |
| 901 |
Slots[0] = UseList; |
| 902 |
|
| 903 |
|
| 904 |
while (Next->Next) { |
| 905 |
Use *Current = Next; |
| 906 |
Next = Current->Next; |
| 907 |
|
| 908 |
|
| 909 |
Current->Next = nullptr; |
| 910 |
|
| 911 |
|
| 912 |
unsigned I; |
| 913 |
for (I = 0; I < NumSlots; ++I) { |
| 914 |
if (!Slots[I]) |
| 915 |
break; |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
Current = mergeUseLists(Slots[I], Current, Cmp); |
| 922 |
Slots[I] = nullptr; |
| 923 |
} |
| 924 |
|
| 925 |
if (I == NumSlots) { |
| 926 |
++NumSlots; |
| 927 |
assert(NumSlots <= MaxSlots && "Use list bigger than 2^32"); |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
Slots[I] = Current; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
assert(Next && "Expected one more Use"); |
| 936 |
assert(!Next->Next && "Expected only one Use"); |
| 937 |
UseList = Next; |
| 938 |
for (unsigned I = 0; I < NumSlots; ++I) |
| 939 |
if (Slots[I]) |
| 940 |
|
| 941 |
|
| 942 |
UseList = mergeUseLists(Slots[I], UseList, Cmp); |
| 943 |
|
| 944 |
|
| 945 |
for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { |
| 946 |
I->Prev = Prev; |
| 947 |
Prev = &I->Next; |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
template <> struct isa_impl<Constant, Value> { |
| 955 |
static inline bool doit(const Value &Val) { |
| 956 |
static_assert(Value::ConstantFirstVal == 0, "Val.getValueID() >= Value::ConstantFirstVal"); |
| 957 |
return Val.getValueID() <= Value::ConstantLastVal; |
| 958 |
} |
| 959 |
}; |
| 960 |
|
| 961 |
template <> struct isa_impl<ConstantData, Value> { |
| 962 |
static inline bool doit(const Value &Val) { |
| 963 |
return Val.getValueID() >= Value::ConstantDataFirstVal && |
| 964 |
Val.getValueID() <= Value::ConstantDataLastVal; |
| 965 |
} |
| 966 |
}; |
| 967 |
|
| 968 |
template <> struct isa_impl<ConstantAggregate, Value> { |
| 969 |
static inline bool doit(const Value &Val) { |
| 970 |
return Val.getValueID() >= Value::ConstantAggregateFirstVal && |
| 971 |
Val.getValueID() <= Value::ConstantAggregateLastVal; |
| 972 |
} |
| 973 |
}; |
| 974 |
|
| 975 |
template <> struct isa_impl<Argument, Value> { |
| 976 |
static inline bool doit (const Value &Val) { |
| 977 |
return Val.getValueID() == Value::ArgumentVal; |
| 978 |
} |
| 979 |
}; |
| 980 |
|
| 981 |
template <> struct isa_impl<InlineAsm, Value> { |
| 982 |
static inline bool doit(const Value &Val) { |
| 983 |
return Val.getValueID() == Value::InlineAsmVal; |
| 984 |
} |
| 985 |
}; |
| 986 |
|
| 987 |
template <> struct isa_impl<Instruction, Value> { |
| 988 |
static inline bool doit(const Value &Val) { |
| 989 |
return Val.getValueID() >= Value::InstructionVal; |
| 990 |
} |
| 991 |
}; |
| 992 |
|
| 993 |
template <> struct isa_impl<BasicBlock, Value> { |
| 994 |
static inline bool doit(const Value &Val) { |
| 995 |
return Val.getValueID() == Value::BasicBlockVal; |
| 996 |
} |
| 997 |
}; |
| 998 |
|
| 999 |
template <> struct isa_impl<Function, Value> { |
| 1000 |
static inline bool doit(const Value &Val) { |
| 1001 |
return Val.getValueID() == Value::FunctionVal; |
| 1002 |
} |
| 1003 |
}; |
| 1004 |
|
| 1005 |
template <> struct isa_impl<GlobalVariable, Value> { |
| 1006 |
static inline bool doit(const Value &Val) { |
| 1007 |
return Val.getValueID() == Value::GlobalVariableVal; |
| 1008 |
} |
| 1009 |
}; |
| 1010 |
|
| 1011 |
template <> struct isa_impl<GlobalAlias, Value> { |
| 1012 |
static inline bool doit(const Value &Val) { |
| 1013 |
return Val.getValueID() == Value::GlobalAliasVal; |
| 1014 |
} |
| 1015 |
}; |
| 1016 |
|
| 1017 |
template <> struct isa_impl<GlobalIFunc, Value> { |
| 1018 |
static inline bool doit(const Value &Val) { |
| 1019 |
return Val.getValueID() == Value::GlobalIFuncVal; |
| 1020 |
} |
| 1021 |
}; |
| 1022 |
|
| 1023 |
template <> struct isa_impl<GlobalValue, Value> { |
| 1024 |
static inline bool doit(const Value &Val) { |
| 1025 |
return isa<GlobalObject>(Val) || isa<GlobalAlias>(Val); |
| 1026 |
} |
| 1027 |
}; |
| 1028 |
|
| 1029 |
template <> struct isa_impl<GlobalObject, Value> { |
| 1030 |
static inline bool doit(const Value &Val) { |
| 1031 |
return isa<GlobalVariable>(Val) || isa<Function>(Val) || |
| 1032 |
isa<GlobalIFunc>(Val); |
| 1033 |
} |
| 1034 |
}; |
| 1035 |
|
| 1036 |
|
| 1037 |
DEFINE_ISA_CONVERSION_FUNCTIONS(Value, LLVMValueRef) |
| 1038 |
|
| 1039 |
|
| 1040 |
inline Value **unwrap(LLVMValueRef *Vals) { |
| 1041 |
return reinterpret_cast<Value**>(Vals); |
| 1042 |
} |
| 1043 |
|
| 1044 |
template<typename T> |
| 1045 |
inline T **unwrap(LLVMValueRef *Vals, unsigned Length) { |
| 1046 |
#ifndef NDEBUG |
| 1047 |
for (LLVMValueRef *I = Vals, *E = Vals + Length; I != E; ++I) |
| 1048 |
unwrap<T>(*I); |
| 1049 |
#endif |
| 1050 |
(void)Length; |
| 1051 |
return reinterpret_cast<T**>(Vals); |
| 1052 |
} |
| 1053 |
|
| 1054 |
inline LLVMValueRef *wrap(const Value **Vals) { |
| 1055 |
return reinterpret_cast<LLVMValueRef*>(const_cast<Value**>(Vals)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
} |
| 1059 |
|
| 1060 |
#endif |