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/ADT/DenseMap.h"
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/LLVMBitCodes.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/GVMaterializer.h"
22 #include "llvm/IR/OperandTraits.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include <system_error>
29 using std::error_code;
33 //===----------------------------------------------------------------------===//
34 // BitcodeReaderValueList Class
35 //===----------------------------------------------------------------------===//
37 class BitcodeReaderValueList {
38 std::vector<WeakVH> ValuePtrs;
40 /// ResolveConstants - As we resolve forward-referenced constants, we add
41 /// information about them to this vector. This allows us to resolve them in
42 /// bulk instead of resolving each reference at a time. See the code in
43 /// ResolveConstantForwardRefs for more information about this.
45 /// The key of this vector is the placeholder constant, the value is the slot
46 /// number that holds the resolved value.
47 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
48 ResolveConstantsTy ResolveConstants;
51 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
52 ~BitcodeReaderValueList() {
53 assert(ResolveConstants.empty() && "Constants not resolved?");
56 // vector compatibility methods
57 unsigned size() const { return ValuePtrs.size(); }
58 void resize(unsigned N) { ValuePtrs.resize(N); }
59 void push_back(Value *V) {
60 ValuePtrs.push_back(V);
64 assert(ResolveConstants.empty() && "Constants not resolved?");
68 Value *operator[](unsigned i) const {
69 assert(i < ValuePtrs.size());
73 Value *back() const { return ValuePtrs.back(); }
74 void pop_back() { ValuePtrs.pop_back(); }
75 bool empty() const { return ValuePtrs.empty(); }
76 void shrinkTo(unsigned N) {
77 assert(N <= size() && "Invalid shrinkTo request!");
81 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
82 Value *getValueFwdRef(unsigned Idx, Type *Ty);
84 void AssignValue(Value *V, unsigned Idx);
86 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
87 /// resolves any forward references.
88 void ResolveConstantForwardRefs();
92 //===----------------------------------------------------------------------===//
93 // BitcodeReaderMDValueList Class
94 //===----------------------------------------------------------------------===//
96 class BitcodeReaderMDValueList {
97 std::vector<WeakVH> MDValuePtrs;
101 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
103 // vector compatibility methods
104 unsigned size() const { return MDValuePtrs.size(); }
105 void resize(unsigned N) { MDValuePtrs.resize(N); }
106 void push_back(Value *V) { MDValuePtrs.push_back(V); }
107 void clear() { MDValuePtrs.clear(); }
108 Value *back() const { return MDValuePtrs.back(); }
109 void pop_back() { MDValuePtrs.pop_back(); }
110 bool empty() const { return MDValuePtrs.empty(); }
112 Value *operator[](unsigned i) const {
113 assert(i < MDValuePtrs.size());
114 return MDValuePtrs[i];
117 void shrinkTo(unsigned N) {
118 assert(N <= size() && "Invalid shrinkTo request!");
119 MDValuePtrs.resize(N);
122 Value *getValueFwdRef(unsigned Idx);
123 void AssignValue(Value *V, unsigned Idx);
126 class BitcodeReader : public GVMaterializer {
127 LLVMContext &Context;
129 MemoryBuffer *Buffer;
131 std::unique_ptr<BitstreamReader> StreamFile;
132 BitstreamCursor Stream;
133 DataStreamer *LazyStreamer;
134 uint64_t NextUnreadBit;
135 bool SeenValueSymbolTable;
137 std::vector<Type*> TypeList;
138 BitcodeReaderValueList ValueList;
139 BitcodeReaderMDValueList MDValueList;
140 SmallVector<Instruction *, 64> InstructionList;
141 SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
143 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
144 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
145 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
147 SmallVector<Instruction*, 64> InstsWithTBAATag;
149 /// MAttributes - The set of attributes by index. Index zero in the
150 /// file is for null, and is thus not represented here. As such all indices
152 std::vector<AttributeSet> MAttributes;
154 /// \brief The set of attribute groups.
155 std::map<unsigned, AttributeSet> MAttributeGroups;
157 /// FunctionBBs - While parsing a function body, this is a list of the basic
158 /// blocks for the function.
159 std::vector<BasicBlock*> FunctionBBs;
161 // When reading the module header, this list is populated with functions that
162 // have bodies later in the file.
163 std::vector<Function*> FunctionsWithBodies;
165 // When intrinsic functions are encountered which require upgrading they are
166 // stored here with their replacement function.
167 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
168 UpgradedIntrinsicMap UpgradedIntrinsics;
170 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
171 DenseMap<unsigned, unsigned> MDKindMap;
173 // Several operations happen after the module header has been read, but
174 // before function bodies are processed. This keeps track of whether
175 // we've done this yet.
176 bool SeenFirstFunctionBody;
178 /// DeferredFunctionInfo - When function bodies are initially scanned, this
179 /// map contains info about where to find deferred function body in the
181 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
183 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
184 /// are resolved lazily when functions are loaded.
185 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
186 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
188 /// UseRelativeIDs - Indicates that we are using a new encoding for
189 /// instruction operands where most operands in the current
190 /// FUNCTION_BLOCK are encoded relative to the instruction number,
191 /// for a more compact encoding. Some instruction operands are not
192 /// relative to the instruction ID: basic block numbers, and types.
193 /// Once the old style function blocks have been phased out, we would
194 /// not need this flag.
197 static const std::error_category &BitcodeErrorCategory();
201 BitcodeStreamInvalidSize,
202 ConflictingMETADATA_KINDRecords,
203 CouldNotFindFunctionInStream,
205 InsufficientFunctionProtos,
206 InvalidBitcodeSignature,
207 InvalidBitcodeWrapperHeader,
208 InvalidConstantReference,
209 InvalidID, // A read identifier is not found in the table it should be in.
210 InvalidInstructionWithNoBB,
211 InvalidRecord, // A read record doesn't have the expected size or structure
212 InvalidTypeForValue, // Type read OK, but is invalid for its use
214 InvalidType, // We were unable to read a type
215 MalformedBlock, // We are unable to advance in the stream.
216 MalformedGlobalInitializerSet,
217 InvalidMultipleBlocks, // We found multiple blocks of a kind that should
219 NeverResolvedValueFoundInFunction,
220 InvalidValue // Invalid version, inst number, attr number, etc
223 error_code Error(ErrorType E) {
224 return error_code(E, BitcodeErrorCategory());
227 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
228 : Context(C), TheModule(nullptr), Buffer(buffer), BufferOwned(false),
229 LazyStreamer(nullptr), NextUnreadBit(0), SeenValueSymbolTable(false),
230 ValueList(C), MDValueList(C),
231 SeenFirstFunctionBody(false), UseRelativeIDs(false) {
233 explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
234 : Context(C), TheModule(nullptr), Buffer(nullptr), BufferOwned(false),
235 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false),
236 ValueList(C), MDValueList(C),
237 SeenFirstFunctionBody(false), UseRelativeIDs(false) {
243 void materializeForwardReferencedFunctions();
247 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
248 /// when the reader is destroyed.
249 void setBufferOwned(bool Owned) { BufferOwned = Owned; }
251 bool isMaterializable(const GlobalValue *GV) const override;
252 bool isDematerializable(const GlobalValue *GV) const override;
253 error_code Materialize(GlobalValue *GV) override;
254 error_code MaterializeModule(Module *M) override;
255 void Dematerialize(GlobalValue *GV) override;
257 /// @brief Main interface to parsing a bitcode buffer.
258 /// @returns true if an error occurred.
259 error_code ParseBitcodeInto(Module *M);
261 /// @brief Cheap mechanism to just extract module triple
262 /// @returns true if an error occurred.
263 error_code ParseTriple(std::string &Triple);
265 static uint64_t decodeSignRotatedValue(uint64_t V);
268 Type *getTypeByID(unsigned ID);
269 Value *getFnValueByID(unsigned ID, Type *Ty) {
270 if (Ty && Ty->isMetadataTy())
271 return MDValueList.getValueFwdRef(ID);
272 return ValueList.getValueFwdRef(ID, Ty);
274 BasicBlock *getBasicBlock(unsigned ID) const {
275 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
276 return FunctionBBs[ID];
278 AttributeSet getAttributes(unsigned i) const {
279 if (i-1 < MAttributes.size())
280 return MAttributes[i-1];
281 return AttributeSet();
284 /// getValueTypePair - Read a value/type pair out of the specified record from
285 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
286 /// Return true on failure.
287 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
288 unsigned InstNum, Value *&ResVal) {
289 if (Slot == Record.size()) return true;
290 unsigned ValNo = (unsigned)Record[Slot++];
291 // Adjust the ValNo, if it was encoded relative to the InstNum.
293 ValNo = InstNum - ValNo;
294 if (ValNo < InstNum) {
295 // If this is not a forward reference, just return the value we already
297 ResVal = getFnValueByID(ValNo, nullptr);
298 return ResVal == nullptr;
299 } else if (Slot == Record.size()) {
303 unsigned TypeNo = (unsigned)Record[Slot++];
304 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
305 return ResVal == nullptr;
308 /// popValue - Read a value out of the specified record from slot 'Slot'.
309 /// Increment Slot past the number of slots used by the value in the record.
310 /// Return true if there is an error.
311 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
312 unsigned InstNum, Type *Ty, Value *&ResVal) {
313 if (getValue(Record, Slot, InstNum, Ty, ResVal))
315 // All values currently take a single record slot.
320 /// getValue -- Like popValue, but does not increment the Slot number.
321 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
322 unsigned InstNum, Type *Ty, Value *&ResVal) {
323 ResVal = getValue(Record, Slot, InstNum, Ty);
324 return ResVal == nullptr;
327 /// getValue -- Version of getValue that returns ResVal directly,
328 /// or 0 if there is an error.
329 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
330 unsigned InstNum, Type *Ty) {
331 if (Slot == Record.size()) return nullptr;
332 unsigned ValNo = (unsigned)Record[Slot];
333 // Adjust the ValNo, if it was encoded relative to the InstNum.
335 ValNo = InstNum - ValNo;
336 return getFnValueByID(ValNo, Ty);
339 /// getValueSigned -- Like getValue, but decodes signed VBRs.
340 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
341 unsigned InstNum, Type *Ty) {
342 if (Slot == Record.size()) return nullptr;
343 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
344 // Adjust the ValNo, if it was encoded relative to the InstNum.
346 ValNo = InstNum - ValNo;
347 return getFnValueByID(ValNo, Ty);
350 error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
351 error_code ParseModule(bool Resume);
352 error_code ParseAttributeBlock();
353 error_code ParseAttributeGroupBlock();
354 error_code ParseTypeTable();
355 error_code ParseTypeTableBody();
357 error_code ParseValueSymbolTable();
358 error_code ParseConstants();
359 error_code RememberAndSkipFunctionBody();
360 error_code ParseFunctionBody(Function *F);
361 error_code GlobalCleanup();
362 error_code ResolveGlobalAndAliasInits();
363 error_code ParseMetadata();
364 error_code ParseMetadataAttachment();
365 error_code ParseModuleTriple(std::string &Triple);
366 error_code ParseUseLists();
367 error_code InitStream();
368 error_code InitStreamFromBuffer();
369 error_code InitLazyStream();
370 error_code FindFunctionInStream(Function *F,
371 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator);
374 } // End llvm namespace