User.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //===-- User.cpp - Implement the User class -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/IR/User.h"
  9. #include "llvm/IR/Constant.h"
  10. #include "llvm/IR/GlobalValue.h"
  11. namespace llvm {
  12. class BasicBlock;
  13. //===----------------------------------------------------------------------===//
  14. // User Class
  15. //===----------------------------------------------------------------------===//
  16. void User::replaceUsesOfWith(Value *From, Value *To) {
  17. if (From == To) return; // Duh what?
  18. assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&
  19. "Cannot call User::replaceUsesOfWith on a constant!");
  20. for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
  21. if (getOperand(i) == From) { // Is This operand is pointing to oldval?
  22. // The side effects of this setOperand call include linking to
  23. // "To", adding "this" to the uses list of To, and
  24. // most importantly, removing "this" from the use list of "From".
  25. setOperand(i, To); // Fix it now...
  26. }
  27. }
  28. //===----------------------------------------------------------------------===//
  29. // User allocHungoffUses Implementation
  30. //===----------------------------------------------------------------------===//
  31. void User::allocHungoffUses(unsigned N, bool IsPhi) {
  32. assert(HasHungOffUses && "alloc must have hung off uses");
  33. static_assert(alignof(Use) >= alignof(Use::UserRef),
  34. "Alignment is insufficient for 'hung-off-uses' pieces");
  35. static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
  36. "Alignment is insufficient for 'hung-off-uses' pieces");
  37. // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
  38. // the User.
  39. size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
  40. if (IsPhi)
  41. size += N * sizeof(BasicBlock *);
  42. Use *Begin = static_cast<Use*>(::operator new(size));
  43. Use *End = Begin + N;
  44. (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
  45. setOperandList(Use::initTags(Begin, End));
  46. }
  47. void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
  48. assert(HasHungOffUses && "realloc must have hung off uses");
  49. unsigned OldNumUses = getNumOperands();
  50. // We don't support shrinking the number of uses. We wouldn't have enough
  51. // space to copy the old uses in to the new space.
  52. assert(NewNumUses > OldNumUses && "realloc must grow num uses");
  53. Use *OldOps = getOperandList();
  54. allocHungoffUses(NewNumUses, IsPhi);
  55. Use *NewOps = getOperandList();
  56. // Now copy from the old operands list to the new one.
  57. std::copy(OldOps, OldOps + OldNumUses, NewOps);
  58. // If this is a Phi, then we need to copy the BB pointers too.
  59. if (IsPhi) {
  60. auto *OldPtr =
  61. reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
  62. auto *NewPtr =
  63. reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
  64. std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
  65. }
  66. Use::zap(OldOps, OldOps + OldNumUses, true);
  67. }
  68. // This is a private struct used by `User` to track the co-allocated descriptor
  69. // section.
  70. struct DescriptorInfo {
  71. intptr_t SizeInBytes;
  72. };
  73. ArrayRef<const uint8_t> User::getDescriptor() const {
  74. auto MutableARef = const_cast<User *>(this)->getDescriptor();
  75. return {MutableARef.begin(), MutableARef.end()};
  76. }
  77. MutableArrayRef<uint8_t> User::getDescriptor() {
  78. assert(HasDescriptor && "Don't call otherwise!");
  79. assert(!HasHungOffUses && "Invariant!");
  80. auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
  81. assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!");
  82. return MutableArrayRef<uint8_t>(
  83. reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
  84. }
  85. //===----------------------------------------------------------------------===//
  86. // User operator new Implementations
  87. //===----------------------------------------------------------------------===//
  88. void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
  89. unsigned DescBytes) {
  90. assert(Us < (1u << NumUserOperandsBits) && "Too many operands");
  91. static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
  92. unsigned DescBytesToAllocate =
  93. DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
  94. assert(DescBytesToAllocate % sizeof(void *) == 0 &&
  95. "We need this to satisfy alignment constraints for Uses");
  96. uint8_t *Storage = static_cast<uint8_t *>(
  97. ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
  98. Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
  99. Use *End = Start + Us;
  100. User *Obj = reinterpret_cast<User*>(End);
  101. Obj->NumUserOperands = Us;
  102. Obj->HasHungOffUses = false;
  103. Obj->HasDescriptor = DescBytes != 0;
  104. Use::initTags(Start, End);
  105. if (DescBytes != 0) {
  106. auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
  107. DescInfo->SizeInBytes = DescBytes;
  108. }
  109. return Obj;
  110. }
  111. void *User::operator new(size_t Size, unsigned Us) {
  112. return allocateFixedOperandUser(Size, Us, 0);
  113. }
  114. void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
  115. return allocateFixedOperandUser(Size, Us, DescBytes);
  116. }
  117. void *User::operator new(size_t Size) {
  118. // Allocate space for a single Use*
  119. void *Storage = ::operator new(Size + sizeof(Use *));
  120. Use **HungOffOperandList = static_cast<Use **>(Storage);
  121. User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
  122. Obj->NumUserOperands = 0;
  123. Obj->HasHungOffUses = true;
  124. Obj->HasDescriptor = false;
  125. *HungOffOperandList = nullptr;
  126. return Obj;
  127. }
  128. //===----------------------------------------------------------------------===//
  129. // User operator delete Implementation
  130. //===----------------------------------------------------------------------===//
  131. void User::operator delete(void *Usr) {
  132. // Hung off uses use a single Use* before the User, while other subclasses
  133. // use a Use[] allocated prior to the user.
  134. User *Obj = static_cast<User *>(Usr);
  135. if (Obj->HasHungOffUses) {
  136. assert(!Obj->HasDescriptor && "not supported!");
  137. Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
  138. // drop the hung off uses.
  139. Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
  140. /* Delete */ true);
  141. ::operator delete(HungOffOperandList);
  142. } else if (Obj->HasDescriptor) {
  143. Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
  144. Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
  145. auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
  146. uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
  147. ::operator delete(Storage);
  148. } else {
  149. Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
  150. Use::zap(Storage, Storage + Obj->NumUserOperands,
  151. /* Delete */ false);
  152. ::operator delete(Storage);
  153. }
  154. }
  155. } // End llvm namespace