Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 1 | //===-- Bitcode/Reader/ValueList.h - Number values --------------*- C++ -*-===// |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This class gives values and types Unique ID's. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 13 | #ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H |
| 14 | #define LLVM_LIB_BITCODE_READER_VALUELIST_H |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 15 | |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 16 | #include "llvm/IR/ValueHandle.h" |
| 17 | #include <cassert> |
| 18 | #include <utility> |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | namespace llvm { |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 22 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 23 | class Constant; |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 24 | class LLVMContext; |
| 25 | class Type; |
| 26 | class Value; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 27 | |
| 28 | class BitcodeReaderValueList { |
Sanjoy Das | e6bca0e | 2017-05-01 17:07:49 +0000 | [diff] [blame] | 29 | std::vector<WeakTrackingVH> ValuePtrs; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 30 | |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 31 | /// 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 Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 38 | /// 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 Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 45 | using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 46 | ResolveConstantsTy ResolveConstants; |
| 47 | LLVMContext &Context; |
| 48 | |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 49 | /// Maximum number of valid references. Forward references exceeding the |
| 50 | /// maximum must be invalid. |
| 51 | unsigned RefsUpperBound; |
| 52 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 53 | public: |
Florian Hahn | 864474c | 2019-07-14 12:35:50 +0000 | [diff] [blame] | 54 | BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound) |
| 55 | : Context(C), |
| 56 | RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), |
| 57 | RefsUpperBound)) {} |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 58 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 59 | ~BitcodeReaderValueList() { |
| 60 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 61 | } |
| 62 | |
| 63 | // vector compatibility methods |
| 64 | unsigned size() const { return ValuePtrs.size(); } |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 65 | 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 Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 73 | |
| 74 | void clear() { |
| 75 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 76 | ValuePtrs.clear(); |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 77 | FullTypes.clear(); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 78 | } |
| 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 Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 86 | void pop_back() { |
| 87 | ValuePtrs.pop_back(); |
| 88 | FullTypes.pop_back(); |
| 89 | } |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 90 | bool empty() const { return ValuePtrs.empty(); } |
| 91 | |
| 92 | void shrinkTo(unsigned N) { |
| 93 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 94 | ValuePtrs.resize(N); |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 95 | FullTypes.resize(N); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Constant *getConstantFwdRef(unsigned Idx, Type *Ty); |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 99 | Value *getValueFwdRef(unsigned Idx, Type *Ty, Type **FullTy = nullptr); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 100 | |
Tim Northover | a4771e9 | 2019-06-27 14:46:51 +0000 | [diff] [blame] | 101 | void assignValue(Value *V, unsigned Idx, Type *FullTy); |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 102 | |
| 103 | /// Once all constants are read, this method bulk resolves any forward |
| 104 | /// references. |
| 105 | void resolveConstantForwardRefs(); |
| 106 | }; |
| 107 | |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 108 | } // end namespace llvm |
| 109 | |
| 110 | #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H |