xref: /freebsd/contrib/llvm-project/llvm/lib/Bitcode/Reader/ValueList.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 "llvm/Support/Error.h"
18 #include <cassert>
19 #include <utility>
20 #include <vector>
21 
22 namespace llvm {
23 
24 class Error;
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   /// Maximum number of valid references. Forward references exceeding the
33   /// maximum must be invalid.
34   unsigned RefsUpperBound;
35 
36   using MaterializeValueFnTy =
37       std::function<Expected<Value *>(unsigned, BasicBlock *)>;
38   MaterializeValueFnTy MaterializeValueFn;
39 
40 public:
41   BitcodeReaderValueList(size_t RefsUpperBound,
42                          MaterializeValueFnTy MaterializeValueFn)
43       : RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
44                                 RefsUpperBound)),
45         MaterializeValueFn(MaterializeValueFn) {}
46 
47   // vector compatibility methods
48   unsigned size() const { return ValuePtrs.size(); }
49   void resize(unsigned N) {
50     ValuePtrs.resize(N);
51   }
52   void push_back(Value *V, unsigned TypeID) {
53     ValuePtrs.emplace_back(V, TypeID);
54   }
55 
56   void clear() {
57     ValuePtrs.clear();
58   }
59 
60   Value *operator[](unsigned i) const {
61     assert(i < ValuePtrs.size());
62     return ValuePtrs[i].first;
63   }
64 
65   unsigned getTypeID(unsigned ValNo) const {
66     assert(ValNo < ValuePtrs.size());
67     return ValuePtrs[ValNo].second;
68   }
69 
70   Value *back() const { return ValuePtrs.back().first; }
71   void pop_back() {
72     ValuePtrs.pop_back();
73   }
74   bool empty() const { return ValuePtrs.empty(); }
75 
76   void shrinkTo(unsigned N) {
77     assert(N <= size() && "Invalid shrinkTo request!");
78     ValuePtrs.resize(N);
79   }
80 
81   void replaceValueWithoutRAUW(unsigned ValNo, Value *NewV) {
82     assert(ValNo < ValuePtrs.size());
83     ValuePtrs[ValNo].first = NewV;
84   }
85 
86   Value *getValueFwdRef(unsigned Idx, Type *Ty, unsigned TyID,
87                         BasicBlock *ConstExprInsertBB);
88 
89   Error assignValue(unsigned Idx, Value *V, unsigned TypeID);
90 };
91 
92 } // end namespace llvm
93 
94 #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H
95