blob: 49900498c2944295a8db566a91a26f9516800e4d [file] [log] [blame]
Eugene Zelenko975293f2017-09-07 23:28:24 +00001//===-- Bitcode/Reader/ValueList.h - Number values --------------*- C++ -*-===//
Mehdi Aminief27db82016-12-12 19:34:26 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mehdi Aminief27db82016-12-12 19:34:26 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class gives values and types Unique ID's.
10//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko975293f2017-09-07 23:28:24 +000013#ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H
14#define LLVM_LIB_BITCODE_READER_VALUELIST_H
Mehdi Aminief27db82016-12-12 19:34:26 +000015
Eugene Zelenko975293f2017-09-07 23:28:24 +000016#include "llvm/IR/ValueHandle.h"
17#include <cassert>
18#include <utility>
Mehdi Aminief27db82016-12-12 19:34:26 +000019#include <vector>
20
21namespace llvm {
Eugene Zelenko975293f2017-09-07 23:28:24 +000022
Mehdi Aminief27db82016-12-12 19:34:26 +000023class Constant;
Eugene Zelenko975293f2017-09-07 23:28:24 +000024class LLVMContext;
25class Type;
26class Value;
Mehdi Aminief27db82016-12-12 19:34:26 +000027
28class BitcodeReaderValueList {
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000029 std::vector<WeakTrackingVH> ValuePtrs;
Mehdi Aminief27db82016-12-12 19:34:26 +000030
Tim Northovera4771e92019-06-27 14:46:51 +000031 /// Struct containing fully-specified copies of the type of each
32 /// value. When pointers are opaque, this will be contain non-opaque
33 /// variants so that restructuring instructions can determine their
34 /// type correctly even if being loaded from old bitcode where some
35 /// types are implicit.
36 std::vector<Type *> FullTypes;
37
Mehdi Aminief27db82016-12-12 19:34:26 +000038 /// As we resolve forward-referenced constants, we add information about them
39 /// to this vector. This allows us to resolve them in bulk instead of
40 /// resolving each reference at a time. See the code in
41 /// ResolveConstantForwardRefs for more information about this.
42 ///
43 /// The key of this vector is the placeholder constant, the value is the slot
44 /// number that holds the resolved value.
Eugene Zelenko975293f2017-09-07 23:28:24 +000045 using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>;
Mehdi Aminief27db82016-12-12 19:34:26 +000046 ResolveConstantsTy ResolveConstants;
47 LLVMContext &Context;
48
Florian Hahn864474c2019-07-14 12:35:50 +000049 /// Maximum number of valid references. Forward references exceeding the
50 /// maximum must be invalid.
51 unsigned RefsUpperBound;
52
Mehdi Aminief27db82016-12-12 19:34:26 +000053public:
Florian Hahn864474c2019-07-14 12:35:50 +000054 BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound)
55 : Context(C),
56 RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(),
57 RefsUpperBound)) {}
Eugene Zelenko975293f2017-09-07 23:28:24 +000058
Mehdi Aminief27db82016-12-12 19:34:26 +000059 ~BitcodeReaderValueList() {
60 assert(ResolveConstants.empty() && "Constants not resolved?");
61 }
62
63 // vector compatibility methods
64 unsigned size() const { return ValuePtrs.size(); }
Tim Northovera4771e92019-06-27 14:46:51 +000065 void resize(unsigned N) {
66 ValuePtrs.resize(N);
67 FullTypes.resize(N);
68 }
69 void push_back(Value *V, Type *Ty) {
70 ValuePtrs.emplace_back(V);
71 FullTypes.emplace_back(Ty);
72 }
Mehdi Aminief27db82016-12-12 19:34:26 +000073
74 void clear() {
75 assert(ResolveConstants.empty() && "Constants not resolved?");
76 ValuePtrs.clear();
Tim Northovera4771e92019-06-27 14:46:51 +000077 FullTypes.clear();
Mehdi Aminief27db82016-12-12 19:34:26 +000078 }
79
80 Value *operator[](unsigned i) const {
81 assert(i < ValuePtrs.size());
82 return ValuePtrs[i];
83 }
84
85 Value *back() const { return ValuePtrs.back(); }
Tim Northovera4771e92019-06-27 14:46:51 +000086 void pop_back() {
87 ValuePtrs.pop_back();
88 FullTypes.pop_back();
89 }
Mehdi Aminief27db82016-12-12 19:34:26 +000090 bool empty() const { return ValuePtrs.empty(); }
91
92 void shrinkTo(unsigned N) {
93 assert(N <= size() && "Invalid shrinkTo request!");
94 ValuePtrs.resize(N);
Tim Northovera4771e92019-06-27 14:46:51 +000095 FullTypes.resize(N);
Mehdi Aminief27db82016-12-12 19:34:26 +000096 }
97
98 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
Tim Northovera4771e92019-06-27 14:46:51 +000099 Value *getValueFwdRef(unsigned Idx, Type *Ty, Type **FullTy = nullptr);
Mehdi Aminief27db82016-12-12 19:34:26 +0000100
Tim Northovera4771e92019-06-27 14:46:51 +0000101 void assignValue(Value *V, unsigned Idx, Type *FullTy);
Mehdi Aminief27db82016-12-12 19:34:26 +0000102
103 /// Once all constants are read, this method bulk resolves any forward
104 /// references.
105 void resolveConstantForwardRefs();
106};
107
Eugene Zelenko975293f2017-09-07 23:28:24 +0000108} // end namespace llvm
109
110#endif // LLVM_LIB_BITCODE_READER_VALUELIST_H