User.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===-- User.cpp - Implement the User class -------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/IR/User.h"
  10. #include "llvm/IR/Constant.h"
  11. #include "llvm/IR/GlobalValue.h"
  12. #include "llvm/IR/Operator.h"
  13. namespace llvm {
  14. class BasicBlock;
  15. //===----------------------------------------------------------------------===//
  16. // User Class
  17. //===----------------------------------------------------------------------===//
  18. void User::anchor() {}
  19. void User::replaceUsesOfWith(Value *From, Value *To) {
  20. if (From == To) return; // Duh what?
  21. assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
  22. "Cannot call User::replaceUsesOfWith on a constant!");
  23. for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
  24. if (getOperand(i) == From) { // Is This operand is pointing to oldval?
  25. // The side effects of this setOperand call include linking to
  26. // "To", adding "this" to the uses list of To, and
  27. // most importantly, removing "this" from the use list of "From".
  28. setOperand(i, To); // Fix it now...
  29. }
  30. }
  31. //===----------------------------------------------------------------------===//
  32. // User allocHungoffUses Implementation
  33. //===----------------------------------------------------------------------===//
  34. void User::allocHungoffUses(unsigned N, bool IsPhi) {
  35. assert(HasHungOffUses && "alloc must have hung off uses");
  36. static_assert(AlignOf<Use>::Alignment >= AlignOf<Use::UserRef>::Alignment,
  37. "Alignment is insufficient for 'hung-off-uses' pieces");
  38. static_assert(AlignOf<Use::UserRef>::Alignment >=
  39. AlignOf<BasicBlock *>::Alignment,
  40. "Alignment is insufficient for 'hung-off-uses' pieces");
  41. // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
  42. // the User.
  43. size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
  44. if (IsPhi)
  45. size += N * sizeof(BasicBlock *);
  46. Use *Begin = static_cast<Use*>(::operator new(size));
  47. Use *End = Begin + N;
  48. (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
  49. setOperandList(Use::initTags(Begin, End));
  50. }
  51. void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
  52. assert(HasHungOffUses && "realloc must have hung off uses");
  53. unsigned OldNumUses = getNumOperands();
  54. // We don't support shrinking the number of uses. We wouldn't have enough
  55. // space to copy the old uses in to the new space.
  56. assert(NewNumUses > OldNumUses && "realloc must grow num uses");
  57. Use *OldOps = getOperandList();
  58. allocHungoffUses(NewNumUses, IsPhi);
  59. Use *NewOps = getOperandList();
  60. // Now copy from the old operands list to the new one.
  61. std::copy(OldOps, OldOps + OldNumUses, NewOps);
  62. // If this is a Phi, then we need to copy the BB pointers too.
  63. if (IsPhi) {
  64. auto *OldPtr =
  65. reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
  66. auto *NewPtr =
  67. reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
  68. std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
  69. }
  70. Use::zap(OldOps, OldOps + OldNumUses, true);
  71. }
  72. //===----------------------------------------------------------------------===//
  73. // User operator new Implementations
  74. //===----------------------------------------------------------------------===//
  75. void *User::operator new(size_t Size, unsigned Us) {
  76. assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
  77. void *Storage = ::operator new(Size + sizeof(Use) * Us);
  78. Use *Start = static_cast<Use*>(Storage);
  79. Use *End = Start + Us;
  80. User *Obj = reinterpret_cast<User*>(End);
  81. Obj->NumUserOperands = Us;
  82. Obj->HasHungOffUses = false;
  83. Use::initTags(Start, End);
  84. return Obj;
  85. }
  86. void *User::operator new(size_t Size) {
  87. // Allocate space for a single Use*
  88. void *Storage = ::operator new(Size + sizeof(Use *));
  89. Use **HungOffOperandList = static_cast<Use **>(Storage);
  90. User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
  91. Obj->NumUserOperands = 0;
  92. Obj->HasHungOffUses = true;
  93. *HungOffOperandList = nullptr;
  94. return Obj;
  95. }
  96. //===----------------------------------------------------------------------===//
  97. // User operator delete Implementation
  98. //===----------------------------------------------------------------------===//
  99. void User::operator delete(void *Usr) {
  100. // Hung off uses use a single Use* before the User, while other subclasses
  101. // use a Use[] allocated prior to the user.
  102. User *Obj = static_cast<User *>(Usr);
  103. if (Obj->HasHungOffUses) {
  104. Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
  105. // drop the hung off uses.
  106. Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
  107. /* Delete */ true);
  108. ::operator delete(HungOffOperandList);
  109. } else {
  110. Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
  111. Use::zap(Storage, Storage + Obj->NumUserOperands,
  112. /* Delete */ false);
  113. ::operator delete(Storage);
  114. }
  115. }
  116. // HLSL Change Starts
  117. void User::operator delete(void *Usr, unsigned NumUserOperands) {
  118. // Fun fact: during construction Obj->NumUserOperands is overwritten
  119. Use *Storage = static_cast<Use *>(Usr) - NumUserOperands;
  120. Use::zap(Storage, Storage + NumUserOperands, /* Delete */ false);
  121. ::operator delete(Storage);
  122. }
  123. // HLSL Change Ends
  124. //===----------------------------------------------------------------------===//
  125. // Operator Class
  126. //===----------------------------------------------------------------------===//
  127. Operator::~Operator() {
  128. llvm_unreachable("should never destroy an Operator");
  129. }
  130. } // End llvm namespace