blob: 5ad7899347ad97f95a99de8a2f7e7f01e21b5219 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class gives values and types Unique ID's.
11//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenko975293f2017-09-07 23:28:24 +000014#ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H
15#define LLVM_LIB_BITCODE_READER_VALUELIST_H
Mehdi Aminief27db82016-12-12 19:34:26 +000016
Eugene Zelenko975293f2017-09-07 23:28:24 +000017#include "llvm/IR/ValueHandle.h"
18#include <cassert>
19#include <utility>
Mehdi Aminief27db82016-12-12 19:34:26 +000020#include <vector>
21
22namespace llvm {
Eugene Zelenko975293f2017-09-07 23:28:24 +000023
Mehdi Aminief27db82016-12-12 19:34:26 +000024class Constant;
Eugene Zelenko975293f2017-09-07 23:28:24 +000025class LLVMContext;
26class Type;
27class Value;
Mehdi Aminief27db82016-12-12 19:34:26 +000028
29class BitcodeReaderValueList {
Sanjoy Dase6bca0e2017-05-01 17:07:49 +000030 std::vector<WeakTrackingVH> ValuePtrs;
Mehdi Aminief27db82016-12-12 19:34:26 +000031
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.
Eugene Zelenko975293f2017-09-07 23:28:24 +000039 using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>;
Mehdi Aminief27db82016-12-12 19:34:26 +000040 ResolveConstantsTy ResolveConstants;
41 LLVMContext &Context;
42
43public:
44 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
Eugene Zelenko975293f2017-09-07 23:28:24 +000045
Mehdi Aminief27db82016-12-12 19:34:26 +000046 ~BitcodeReaderValueList() {
47 assert(ResolveConstants.empty() && "Constants not resolved?");
48 }
49
50 // vector compatibility methods
51 unsigned size() const { return ValuePtrs.size(); }
52 void resize(unsigned N) { ValuePtrs.resize(N); }
53 void push_back(Value *V) { ValuePtrs.emplace_back(V); }
54
55 void clear() {
56 assert(ResolveConstants.empty() && "Constants not resolved?");
57 ValuePtrs.clear();
58 }
59
60 Value *operator[](unsigned i) const {
61 assert(i < ValuePtrs.size());
62 return ValuePtrs[i];
63 }
64
65 Value *back() const { return ValuePtrs.back(); }
66 void pop_back() { ValuePtrs.pop_back(); }
67 bool empty() const { return ValuePtrs.empty(); }
68
69 void shrinkTo(unsigned N) {
70 assert(N <= size() && "Invalid shrinkTo request!");
71 ValuePtrs.resize(N);
72 }
73
74 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
75 Value *getValueFwdRef(unsigned Idx, Type *Ty);
76
77 void assignValue(Value *V, unsigned Idx);
78
79 /// Once all constants are read, this method bulk resolves any forward
80 /// references.
81 void resolveConstantForwardRefs();
82};
83
Eugene Zelenko975293f2017-09-07 23:28:24 +000084} // end namespace llvm
85
86#endif // LLVM_LIB_BITCODE_READER_VALUELIST_H