ValueList.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===-- Bitcode/Reader/ValueList.h - Number values --------------*- C++ -*-===//
  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. //
  9. // This class gives values and types Unique ID's.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H
  13. #define LLVM_LIB_BITCODE_READER_VALUELIST_H
  14. #include "llvm/IR/ValueHandle.h"
  15. #include <cassert>
  16. #include <utility>
  17. #include <vector>
  18. namespace llvm {
  19. class Constant;
  20. class LLVMContext;
  21. class Type;
  22. class Value;
  23. class BitcodeReaderValueList {
  24. std::vector<WeakTrackingVH> ValuePtrs;
  25. /// Struct containing fully-specified copies of the type of each
  26. /// value. When pointers are opaque, this will be contain non-opaque
  27. /// variants so that restructuring instructions can determine their
  28. /// type correctly even if being loaded from old bitcode where some
  29. /// types are implicit.
  30. std::vector<Type *> FullTypes;
  31. /// As we resolve forward-referenced constants, we add information about them
  32. /// to this vector. This allows us to resolve them in bulk instead of
  33. /// resolving each reference at a time. See the code in
  34. /// ResolveConstantForwardRefs for more information about this.
  35. ///
  36. /// The key of this vector is the placeholder constant, the value is the slot
  37. /// number that holds the resolved value.
  38. using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>;
  39. ResolveConstantsTy ResolveConstants;
  40. LLVMContext &Context;
  41. /// Maximum number of valid references. Forward references exceeding the
  42. /// maximum must be invalid.
  43. unsigned RefsUpperBound;
  44. public:
  45. BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound)
  46. : Context(C),
  47. RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
  48. RefsUpperBound)) {}
  49. ~BitcodeReaderValueList() {
  50. assert(ResolveConstants.empty() && "Constants not resolved?");
  51. }
  52. // vector compatibility methods
  53. unsigned size() const { return ValuePtrs.size(); }
  54. void resize(unsigned N) {
  55. ValuePtrs.resize(N);
  56. FullTypes.resize(N);
  57. }
  58. void push_back(Value *V, Type *Ty) {
  59. ValuePtrs.emplace_back(V);
  60. FullTypes.emplace_back(Ty);
  61. }
  62. void clear() {
  63. assert(ResolveConstants.empty() && "Constants not resolved?");
  64. ValuePtrs.clear();
  65. FullTypes.clear();
  66. }
  67. Value *operator[](unsigned i) const {
  68. assert(i < ValuePtrs.size());
  69. return ValuePtrs[i];
  70. }
  71. Value *back() const { return ValuePtrs.back(); }
  72. void pop_back() {
  73. ValuePtrs.pop_back();
  74. FullTypes.pop_back();
  75. }
  76. bool empty() const { return ValuePtrs.empty(); }
  77. void shrinkTo(unsigned N) {
  78. assert(N <= size() && "Invalid shrinkTo request!");
  79. ValuePtrs.resize(N);
  80. FullTypes.resize(N);
  81. }
  82. Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
  83. Value *getValueFwdRef(unsigned Idx, Type *Ty, Type **FullTy = nullptr);
  84. void assignValue(Value *V, unsigned Idx, Type *FullTy);
  85. /// Once all constants are read, this method bulk resolves any forward
  86. /// references.
  87. void resolveConstantForwardRefs();
  88. };
  89. } // end namespace llvm
  90. #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H