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 
13 #ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H
14 #define LLVM_LIB_BITCODE_READER_VALUELIST_H
15 
16 #include "llvm/IR/ValueHandle.h"
17 #include <cassert>
18 #include <utility>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class Constant;
24 class LLVMContext;
25 class Type;
26 class Value;
27 
28 class BitcodeReaderValueList {
29   /// Maps Value ID to pair of Value* and Type ID.
30   std::vector<std::pair<WeakTrackingVH, unsigned>> ValuePtrs;
31 
32   /// As we resolve forward-referenced constants, we add information about them
33   /// to this vector.  This allows us to resolve them in bulk instead of
34   /// resolving each reference at a time.  See the code in
35   /// ResolveConstantForwardRefs for more information about this.
36   ///
37   /// The key of this vector is the placeholder constant, the value is the slot
38   /// number that holds the resolved value.
39   using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>;
40   ResolveConstantsTy ResolveConstants;
41   LLVMContext &Context;
42 
43   /// Maximum number of valid references. Forward references exceeding the
44   /// maximum must be invalid.
45   unsigned RefsUpperBound;
46 
47 public:
48   BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound)
49       : Context(C),
50         RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
51                                 RefsUpperBound)) {}
52 
53   ~BitcodeReaderValueList() {
54     assert(ResolveConstants.empty() && "Constants not resolved?");
55   }
56 
57   // vector compatibility methods
58   unsigned size() const { return ValuePtrs.size(); }
59   void resize(unsigned N) {
60     ValuePtrs.resize(N);
61   }
62   void push_back(Value *V, unsigned TypeID) {
63     ValuePtrs.emplace_back(V, TypeID);
64   }
65 
66   void clear() {
67     assert(ResolveConstants.empty() && "Constants not resolved?");
68     ValuePtrs.clear();
69   }
70 
71   Value *operator[](unsigned i) const {
72     assert(i < ValuePtrs.size());
73     return ValuePtrs[i].first;
74   }
75 
76   unsigned getTypeID(unsigned ValNo) const {
77     assert(ValNo < ValuePtrs.size());
78     return ValuePtrs[ValNo].second;
79   }
80 
81   Value *back() const { return ValuePtrs.back().first; }
82   void pop_back() {
83     ValuePtrs.pop_back();
84   }
85   bool empty() const { return ValuePtrs.empty(); }
86 
87   void shrinkTo(unsigned N) {
88     assert(N <= size() && "Invalid shrinkTo request!");
89     ValuePtrs.resize(N);
90   }
91 
92   Constant *getConstantFwdRef(unsigned Idx, Type *Ty, unsigned TyID);
93   Value *getValueFwdRef(unsigned Idx, Type *Ty, unsigned TyID);
94 
95   void assignValue(unsigned Idx, Value *V, unsigned TypeID);
96 
97   /// Once all constants are read, this method bulk resolves any forward
98   /// references.
99   void resolveConstantForwardRefs();
100 };
101 
102 } // end namespace llvm
103 
104 #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H
105