ValueList.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "llvm/Support/Error.h"
  16. #include <cassert>
  17. #include <utility>
  18. #include <vector>
  19. namespace llvm {
  20. class Error;
  21. class Type;
  22. class Value;
  23. class BitcodeReaderValueList {
  24. /// Maps Value ID to pair of Value* and Type ID.
  25. std::vector<std::pair<WeakTrackingVH, unsigned>> ValuePtrs;
  26. /// Maximum number of valid references. Forward references exceeding the
  27. /// maximum must be invalid.
  28. unsigned RefsUpperBound;
  29. using MaterializeValueFnTy =
  30. std::function<Expected<Value *>(unsigned, BasicBlock *)>;
  31. MaterializeValueFnTy MaterializeValueFn;
  32. public:
  33. BitcodeReaderValueList(size_t RefsUpperBound,
  34. MaterializeValueFnTy MaterializeValueFn)
  35. : RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
  36. RefsUpperBound)),
  37. MaterializeValueFn(MaterializeValueFn) {}
  38. // vector compatibility methods
  39. unsigned size() const { return ValuePtrs.size(); }
  40. void resize(unsigned N) {
  41. ValuePtrs.resize(N);
  42. }
  43. void push_back(Value *V, unsigned TypeID) {
  44. ValuePtrs.emplace_back(V, TypeID);
  45. }
  46. void clear() {
  47. ValuePtrs.clear();
  48. }
  49. Value *operator[](unsigned i) const {
  50. assert(i < ValuePtrs.size());
  51. return ValuePtrs[i].first;
  52. }
  53. unsigned getTypeID(unsigned ValNo) const {
  54. assert(ValNo < ValuePtrs.size());
  55. return ValuePtrs[ValNo].second;
  56. }
  57. Value *back() const { return ValuePtrs.back().first; }
  58. void pop_back() {
  59. ValuePtrs.pop_back();
  60. }
  61. bool empty() const { return ValuePtrs.empty(); }
  62. void shrinkTo(unsigned N) {
  63. assert(N <= size() && "Invalid shrinkTo request!");
  64. ValuePtrs.resize(N);
  65. }
  66. void replaceValueWithoutRAUW(unsigned ValNo, Value *NewV) {
  67. assert(ValNo < ValuePtrs.size());
  68. ValuePtrs[ValNo].first = NewV;
  69. }
  70. Value *getValueFwdRef(unsigned Idx, Type *Ty, unsigned TyID,
  71. BasicBlock *ConstExprInsertBB);
  72. Error assignValue(unsigned Idx, Value *V, unsigned TypeID);
  73. };
  74. } // end namespace llvm
  75. #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H