ValueList.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /// As we resolve forward-referenced constants, we add information about them
  26. /// to this vector. This allows us to resolve them in bulk instead of
  27. /// resolving each reference at a time. See the code in
  28. /// ResolveConstantForwardRefs for more information about this.
  29. ///
  30. /// The key of this vector is the placeholder constant, the value is the slot
  31. /// number that holds the resolved value.
  32. using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>;
  33. ResolveConstantsTy ResolveConstants;
  34. LLVMContext &Context;
  35. /// Maximum number of valid references. Forward references exceeding the
  36. /// maximum must be invalid.
  37. unsigned RefsUpperBound;
  38. public:
  39. BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound)
  40. : Context(C),
  41. RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
  42. RefsUpperBound)) {}
  43. ~BitcodeReaderValueList() {
  44. assert(ResolveConstants.empty() && "Constants not resolved?");
  45. }
  46. // vector compatibility methods
  47. unsigned size() const { return ValuePtrs.size(); }
  48. void resize(unsigned N) {
  49. ValuePtrs.resize(N);
  50. }
  51. void push_back(Value *V) { ValuePtrs.emplace_back(V); }
  52. void clear() {
  53. assert(ResolveConstants.empty() && "Constants not resolved?");
  54. ValuePtrs.clear();
  55. }
  56. Value *operator[](unsigned i) const {
  57. assert(i < ValuePtrs.size());
  58. return ValuePtrs[i];
  59. }
  60. Value *back() const { return ValuePtrs.back(); }
  61. void pop_back() {
  62. ValuePtrs.pop_back();
  63. }
  64. bool empty() const { return ValuePtrs.empty(); }
  65. void shrinkTo(unsigned N) {
  66. assert(N <= size() && "Invalid shrinkTo request!");
  67. ValuePtrs.resize(N);
  68. }
  69. Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
  70. Value *getValueFwdRef(unsigned Idx, Type *Ty);
  71. void assignValue(Value *V, unsigned Idx);
  72. /// Once all constants are read, this method bulk resolves any forward
  73. /// references.
  74. void resolveConstantForwardRefs();
  75. };
  76. } // end namespace llvm
  77. #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H