User.cpp 7.2 KB

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