ValueList.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //===- ValueList.cpp - Internal BitcodeReader implementation --------------===//
  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 "ValueList.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/IR/Argument.h"
  11. #include "llvm/IR/Constant.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/IR/GlobalValue.h"
  14. #include "llvm/IR/Instruction.h"
  15. #include "llvm/IR/Type.h"
  16. #include "llvm/IR/User.h"
  17. #include "llvm/IR/Value.h"
  18. #include "llvm/Support/Casting.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <algorithm>
  21. #include <cstddef>
  22. #include <limits>
  23. using namespace llvm;
  24. namespace llvm {
  25. namespace {
  26. /// A class for maintaining the slot number definition
  27. /// as a placeholder for the actual definition for forward constants defs.
  28. class ConstantPlaceHolder : public ConstantExpr {
  29. public:
  30. explicit ConstantPlaceHolder(Type *Ty, LLVMContext &Context)
  31. : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
  32. Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
  33. }
  34. ConstantPlaceHolder &operator=(const ConstantPlaceHolder &) = delete;
  35. // allocate space for exactly one operand
  36. void *operator new(size_t s) { return User::operator new(s, 1); }
  37. /// Methods to support type inquiry through isa, cast, and dyn_cast.
  38. static bool classof(const Value *V) {
  39. return isa<ConstantExpr>(V) &&
  40. cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
  41. }
  42. /// Provide fast operand accessors
  43. DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
  44. };
  45. } // end anonymous namespace
  46. // FIXME: can we inherit this from ConstantExpr?
  47. template <>
  48. struct OperandTraits<ConstantPlaceHolder>
  49. : public FixedNumOperandTraits<ConstantPlaceHolder, 1> {};
  50. DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
  51. } // end namespace llvm
  52. void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
  53. if (Idx == size()) {
  54. push_back(V);
  55. return;
  56. }
  57. if (Idx >= size())
  58. resize(Idx + 1);
  59. WeakTrackingVH &OldV = ValuePtrs[Idx];
  60. if (!OldV) {
  61. OldV = V;
  62. return;
  63. }
  64. // Handle constants and non-constants (e.g. instrs) differently for
  65. // efficiency.
  66. if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
  67. ResolveConstants.push_back(std::make_pair(PHC, Idx));
  68. OldV = V;
  69. } else {
  70. // If there was a forward reference to this value, replace it.
  71. Value *PrevVal = OldV;
  72. OldV->replaceAllUsesWith(V);
  73. PrevVal->deleteValue();
  74. }
  75. }
  76. Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, Type *Ty) {
  77. // Bail out for a clearly invalid value.
  78. if (Idx >= RefsUpperBound)
  79. return nullptr;
  80. if (Idx >= size())
  81. resize(Idx + 1);
  82. if (Value *V = ValuePtrs[Idx]) {
  83. if (Ty != V->getType())
  84. report_fatal_error("Type mismatch in constant table!");
  85. return cast<Constant>(V);
  86. }
  87. // Create and return a placeholder, which will later be RAUW'd.
  88. Constant *C = new ConstantPlaceHolder(Ty, Context);
  89. ValuePtrs[Idx] = C;
  90. return C;
  91. }
  92. Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
  93. // Bail out for a clearly invalid value.
  94. if (Idx >= RefsUpperBound)
  95. return nullptr;
  96. if (Idx >= size())
  97. resize(Idx + 1);
  98. if (Value *V = ValuePtrs[Idx]) {
  99. // If the types don't match, it's invalid.
  100. if (Ty && Ty != V->getType())
  101. return nullptr;
  102. return V;
  103. }
  104. // No type specified, must be invalid reference.
  105. if (!Ty)
  106. return nullptr;
  107. // Create and return a placeholder, which will later be RAUW'd.
  108. Value *V = new Argument(Ty);
  109. ValuePtrs[Idx] = V;
  110. return V;
  111. }
  112. /// Once all constants are read, this method bulk resolves any forward
  113. /// references. The idea behind this is that we sometimes get constants (such
  114. /// as large arrays) which reference *many* forward ref constants. Replacing
  115. /// each of these causes a lot of thrashing when building/reuniquing the
  116. /// constant. Instead of doing this, we look at all the uses and rewrite all
  117. /// the place holders at once for any constant that uses a placeholder.
  118. void BitcodeReaderValueList::resolveConstantForwardRefs() {
  119. // Sort the values by-pointer so that they are efficient to look up with a
  120. // binary search.
  121. llvm::sort(ResolveConstants);
  122. SmallVector<Constant *, 64> NewOps;
  123. while (!ResolveConstants.empty()) {
  124. Value *RealVal = operator[](ResolveConstants.back().second);
  125. Constant *Placeholder = ResolveConstants.back().first;
  126. ResolveConstants.pop_back();
  127. // Loop over all users of the placeholder, updating them to reference the
  128. // new value. If they reference more than one placeholder, update them all
  129. // at once.
  130. while (!Placeholder->use_empty()) {
  131. auto UI = Placeholder->user_begin();
  132. User *U = *UI;
  133. // If the using object isn't uniqued, just update the operands. This
  134. // handles instructions and initializers for global variables.
  135. if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
  136. UI.getUse().set(RealVal);
  137. continue;
  138. }
  139. // Otherwise, we have a constant that uses the placeholder. Replace that
  140. // constant with a new constant that has *all* placeholder uses updated.
  141. Constant *UserC = cast<Constant>(U);
  142. for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end(); I != E;
  143. ++I) {
  144. Value *NewOp;
  145. if (!isa<ConstantPlaceHolder>(*I)) {
  146. // Not a placeholder reference.
  147. NewOp = *I;
  148. } else if (*I == Placeholder) {
  149. // Common case is that it just references this one placeholder.
  150. NewOp = RealVal;
  151. } else {
  152. // Otherwise, look up the placeholder in ResolveConstants.
  153. ResolveConstantsTy::iterator It = llvm::lower_bound(
  154. ResolveConstants,
  155. std::pair<Constant *, unsigned>(cast<Constant>(*I), 0));
  156. assert(It != ResolveConstants.end() && It->first == *I);
  157. NewOp = operator[](It->second);
  158. }
  159. NewOps.push_back(cast<Constant>(NewOp));
  160. }
  161. // Make the new constant.
  162. Constant *NewC;
  163. if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
  164. NewC = ConstantArray::get(UserCA->getType(), NewOps);
  165. } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
  166. NewC = ConstantStruct::get(UserCS->getType(), NewOps);
  167. } else if (isa<ConstantVector>(UserC)) {
  168. NewC = ConstantVector::get(NewOps);
  169. } else {
  170. assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
  171. NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
  172. }
  173. UserC->replaceAllUsesWith(NewC);
  174. UserC->destroyConstant();
  175. NewOps.clear();
  176. }
  177. // Update all ValueHandles, they should be the only users at this point.
  178. Placeholder->replaceAllUsesWith(RealVal);
  179. delete cast<ConstantPlaceHolder>(Placeholder);
  180. }
  181. }