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 | |
| 31 | /// As we resolve forward-referenced constants, we add information about them |
| 32 | /// to this vector. This allows us to resolve them in bulk instead of |
| 33 | /// resolving each reference at a time. See the code in |
| 34 | /// ResolveConstantForwardRefs for more information about this. |
| 35 | /// |
| 36 | /// The key of this vector is the placeholder constant, the value is the slot |
| 37 | /// number that holds the resolved value. |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 38 | using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>; |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 39 | ResolveConstantsTy ResolveConstants; |
| 40 | LLVMContext &Context; |
| 41 | |
| 42 | public: |
| 43 | BitcodeReaderValueList(LLVMContext &C) : Context(C) {} |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 44 | |
Mehdi Amini | ef27db8 | 2016-12-12 19:34:26 +0000 | [diff] [blame] | 45 | ~BitcodeReaderValueList() { |
| 46 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 47 | } |
| 48 | |
| 49 | // vector compatibility methods |
| 50 | unsigned size() const { return ValuePtrs.size(); } |
| 51 | void resize(unsigned N) { ValuePtrs.resize(N); } |
| 52 | void push_back(Value *V) { ValuePtrs.emplace_back(V); } |
| 53 | |
| 54 | void clear() { |
| 55 | assert(ResolveConstants.empty() && "Constants not resolved?"); |
| 56 | ValuePtrs.clear(); |
| 57 | } |
| 58 | |
| 59 | Value *operator[](unsigned i) const { |
| 60 | assert(i < ValuePtrs.size()); |
| 61 | return ValuePtrs[i]; |
| 62 | } |
| 63 | |
| 64 | Value *back() const { return ValuePtrs.back(); } |
| 65 | void pop_back() { ValuePtrs.pop_back(); } |
| 66 | bool empty() const { return ValuePtrs.empty(); } |
| 67 | |
| 68 | void shrinkTo(unsigned N) { |
| 69 | assert(N <= size() && "Invalid shrinkTo request!"); |
| 70 | ValuePtrs.resize(N); |
| 71 | } |
| 72 | |
| 73 | Constant *getConstantFwdRef(unsigned Idx, Type *Ty); |
| 74 | Value *getValueFwdRef(unsigned Idx, Type *Ty); |
| 75 | |
| 76 | void assignValue(Value *V, unsigned Idx); |
| 77 | |
| 78 | /// Once all constants are read, this method bulk resolves any forward |
| 79 | /// references. |
| 80 | void resolveConstantForwardRefs(); |
| 81 | }; |
| 82 | |
Eugene Zelenko | 975293f | 2017-09-07 23:28:24 +0000 | [diff] [blame] | 83 | } // end namespace llvm |
| 84 | |
| 85 | #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H |