1
//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This header defines the BitcodeReader class.
12
//===----------------------------------------------------------------------===//
14
#ifndef BITCODE_READER_H
15
#define BITCODE_READER_H
17
#include "llvm/GVMaterializer.h"
18
#include "llvm/Attributes.h"
19
#include "llvm/Type.h"
20
#include "llvm/OperandTraits.h"
21
#include "llvm/Bitcode/BitstreamReader.h"
22
#include "llvm/Bitcode/LLVMBitCodes.h"
23
#include "llvm/Support/ValueHandle.h"
24
#include "llvm/ADT/DenseMap.h"
31
//===----------------------------------------------------------------------===//
32
// BitcodeReaderValueList Class
33
//===----------------------------------------------------------------------===//
35
class BitcodeReaderValueList {
36
std::vector<WeakVH> ValuePtrs;
38
/// ResolveConstants - As we resolve forward-referenced constants, we add
39
/// information about them to this vector. This allows us to resolve them in
40
/// bulk instead of resolving each reference at a time. See the code in
41
/// ResolveConstantForwardRefs for more information about this.
43
/// The key of this vector is the placeholder constant, the value is the slot
44
/// number that holds the resolved value.
45
typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
46
ResolveConstantsTy ResolveConstants;
49
BitcodeReaderValueList(LLVMContext& C) : Context(C) {}
50
~BitcodeReaderValueList() {
51
assert(ResolveConstants.empty() && "Constants not resolved?");
54
// vector compatibility methods
55
unsigned size() const { return ValuePtrs.size(); }
56
void resize(unsigned N) { ValuePtrs.resize(N); }
57
void push_back(Value *V) {
58
ValuePtrs.push_back(V);
62
assert(ResolveConstants.empty() && "Constants not resolved?");
66
Value *operator[](unsigned i) const {
67
assert(i < ValuePtrs.size());
71
Value *back() const { return ValuePtrs.back(); }
72
void pop_back() { ValuePtrs.pop_back(); }
73
bool empty() const { return ValuePtrs.empty(); }
74
void shrinkTo(unsigned N) {
75
assert(N <= size() && "Invalid shrinkTo request!");
79
Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
80
Value *getValueFwdRef(unsigned Idx, const Type *Ty);
82
void AssignValue(Value *V, unsigned Idx);
84
/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
85
/// resolves any forward references.
86
void ResolveConstantForwardRefs();
90
//===----------------------------------------------------------------------===//
91
// BitcodeReaderMDValueList Class
92
//===----------------------------------------------------------------------===//
94
class BitcodeReaderMDValueList {
95
std::vector<WeakVH> MDValuePtrs;
99
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
101
// vector compatibility methods
102
unsigned size() const { return MDValuePtrs.size(); }
103
void resize(unsigned N) { MDValuePtrs.resize(N); }
104
void push_back(Value *V) { MDValuePtrs.push_back(V); }
105
void clear() { MDValuePtrs.clear(); }
106
Value *back() const { return MDValuePtrs.back(); }
107
void pop_back() { MDValuePtrs.pop_back(); }
108
bool empty() const { return MDValuePtrs.empty(); }
110
Value *operator[](unsigned i) const {
111
assert(i < MDValuePtrs.size());
112
return MDValuePtrs[i];
115
void shrinkTo(unsigned N) {
116
assert(N <= size() && "Invalid shrinkTo request!");
117
MDValuePtrs.resize(N);
120
Value *getValueFwdRef(unsigned Idx);
121
void AssignValue(Value *V, unsigned Idx);
124
class BitcodeReader : public GVMaterializer {
125
LLVMContext &Context;
127
MemoryBuffer *Buffer;
129
BitstreamReader StreamFile;
130
BitstreamCursor Stream;
132
const char *ErrorString;
134
std::vector<PATypeHolder> TypeList;
135
BitcodeReaderValueList ValueList;
136
BitcodeReaderMDValueList MDValueList;
137
SmallVector<Instruction *, 64> InstructionList;
139
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
140
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
142
/// MAttributes - The set of attributes by index. Index zero in the
143
/// file is for null, and is thus not represented here. As such all indices
145
std::vector<AttrListPtr> MAttributes;
147
/// FunctionBBs - While parsing a function body, this is a list of the basic
148
/// blocks for the function.
149
std::vector<BasicBlock*> FunctionBBs;
151
// When reading the module header, this list is populated with functions that
152
// have bodies later in the file.
153
std::vector<Function*> FunctionsWithBodies;
155
// When intrinsic functions are encountered which require upgrading they are
156
// stored here with their replacement function.
157
typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
158
UpgradedIntrinsicMap UpgradedIntrinsics;
160
// After the module header has been read, the FunctionsWithBodies list is
161
// reversed. This keeps track of whether we've done this yet.
162
bool HasReversedFunctionsWithBodies;
164
/// DeferredFunctionInfo - When function bodies are initially scanned, this
165
/// map contains info about where to find deferred function body in the
167
DenseMap<Function*, uint64_t> DeferredFunctionInfo;
169
/// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
170
/// are resolved lazily when functions are loaded.
171
typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
172
DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
175
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
176
: Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
177
ErrorString(0), ValueList(C), MDValueList(C) {
178
HasReversedFunctionsWithBodies = false;
186
/// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
187
/// when the reader is destroyed.
188
void setBufferOwned(bool Owned) { BufferOwned = Owned; }
190
virtual bool isMaterializable(const GlobalValue *GV) const;
191
virtual bool isDematerializable(const GlobalValue *GV) const;
192
virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
193
virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
194
virtual void Dematerialize(GlobalValue *GV);
196
bool Error(const char *Str) {
200
const char *getErrorString() const { return ErrorString; }
202
/// @brief Main interface to parsing a bitcode buffer.
203
/// @returns true if an error occurred.
204
bool ParseBitcodeInto(Module *M);
206
const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
207
Value *getFnValueByID(unsigned ID, const Type *Ty) {
208
if (Ty == Type::getMetadataTy(Context))
209
return MDValueList.getValueFwdRef(ID);
211
return ValueList.getValueFwdRef(ID, Ty);
213
BasicBlock *getBasicBlock(unsigned ID) const {
214
if (ID >= FunctionBBs.size()) return 0; // Invalid ID
215
return FunctionBBs[ID];
217
AttrListPtr getAttributes(unsigned i) const {
218
if (i-1 < MAttributes.size())
219
return MAttributes[i-1];
220
return AttrListPtr();
223
/// getValueTypePair - Read a value/type pair out of the specified record from
224
/// slot 'Slot'. Increment Slot past the number of slots used in the record.
225
/// Return true on failure.
226
bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
227
unsigned InstNum, Value *&ResVal) {
228
if (Slot == Record.size()) return true;
229
unsigned ValNo = (unsigned)Record[Slot++];
230
if (ValNo < InstNum) {
231
// If this is not a forward reference, just return the value we already
233
ResVal = getFnValueByID(ValNo, 0);
235
} else if (Slot == Record.size()) {
239
unsigned TypeNo = (unsigned)Record[Slot++];
240
ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
243
bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
244
const Type *Ty, Value *&ResVal) {
245
if (Slot == Record.size()) return true;
246
unsigned ValNo = (unsigned)Record[Slot++];
247
ResVal = getFnValueByID(ValNo, Ty);
253
bool ParseAttributeBlock();
254
bool ParseTypeTable();
255
bool ParseTypeSymbolTable();
256
bool ParseValueSymbolTable();
257
bool ParseConstants();
258
bool RememberAndSkipFunctionBody();
259
bool ParseFunctionBody(Function *F);
260
bool ResolveGlobalAndAliasInits();
261
bool ParseMetadata();
262
bool ParseMetadataAttachment();
265
} // End llvm namespace