User.cpp 7.3 KB

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