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 "llvm/Support/system_error.h"
32 //===----------------------------------------------------------------------===//
33 // BitcodeReaderValueList Class
34 //===----------------------------------------------------------------------===//
36 class BitcodeReaderValueList {
37 std::vector<WeakVH> ValuePtrs;
39 /// ResolveConstants - As we resolve forward-referenced constants, we add
40 /// information about them to this vector. This allows us to resolve them in
41 /// bulk instead of resolving each reference at a time. See the code in
42 /// ResolveConstantForwardRefs for more information about this.
44 /// The key of this vector is the placeholder constant, the value is the slot
45 /// number that holds the resolved value.
46 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
47 ResolveConstantsTy ResolveConstants;
50 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
51 ~BitcodeReaderValueList() {
52 assert(ResolveConstants.empty() && "Constants not resolved?");
55 // vector compatibility methods
56 unsigned size() const { return ValuePtrs.size(); }
57 void resize(unsigned N) { ValuePtrs.resize(N); }
58 void push_back(Value *V) {
59 ValuePtrs.push_back(V);
63 assert(ResolveConstants.empty() && "Constants not resolved?");
67 Value *operator[](unsigned i) const {
68 assert(i < ValuePtrs.size());
72 Value *back() const { return ValuePtrs.back(); }
73 void pop_back() { ValuePtrs.pop_back(); }
74 bool empty() const { return ValuePtrs.empty(); }
75 void shrinkTo(unsigned N) {
76 assert(N <= size() && "Invalid shrinkTo request!");
80 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
81 Value *getValueFwdRef(unsigned Idx, Type *Ty);
83 void AssignValue(Value *V, unsigned Idx);
85 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
86 /// resolves any forward references.
87 void ResolveConstantForwardRefs();
91 //===----------------------------------------------------------------------===//
92 // BitcodeReaderMDValueList Class
93 //===----------------------------------------------------------------------===//
95 class BitcodeReaderMDValueList {
96 std::vector<WeakVH> MDValuePtrs;
100 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
102 // vector compatibility methods
103 unsigned size() const { return MDValuePtrs.size(); }
104 void resize(unsigned N) { MDValuePtrs.resize(N); }
105 void push_back(Value *V) { MDValuePtrs.push_back(V); }
106 void clear() { MDValuePtrs.clear(); }
107 Value *back() const { return MDValuePtrs.back(); }
108 void pop_back() { MDValuePtrs.pop_back(); }
109 bool empty() const { return MDValuePtrs.empty(); }
111 Value *operator[](unsigned i) const {
112 assert(i < MDValuePtrs.size());
113 return MDValuePtrs[i];
116 void shrinkTo(unsigned N) {
117 assert(N <= size() && "Invalid shrinkTo request!");
118 MDValuePtrs.resize(N);
121 Value *getValueFwdRef(unsigned Idx);
122 void AssignValue(Value *V, unsigned Idx);
125 class BitcodeReader : public GVMaterializer {
126 LLVMContext &Context;
128 MemoryBuffer *Buffer;
130 OwningPtr<BitstreamReader> StreamFile;
131 BitstreamCursor Stream;
132 DataStreamer *LazyStreamer;
133 uint64_t NextUnreadBit;
134 bool SeenValueSymbolTable;
136 std::vector<Type*> TypeList;
137 BitcodeReaderValueList ValueList;
138 BitcodeReaderMDValueList MDValueList;
139 SmallVector<Instruction *, 64> InstructionList;
140 SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
142 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
143 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
144 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
146 SmallVector<Instruction*, 64> InstsWithTBAATag;
148 /// MAttributes - The set of attributes by index. Index zero in the
149 /// file is for null, and is thus not represented here. As such all indices
151 std::vector<AttributeSet> MAttributes;
153 /// \brief The set of attribute groups.
154 std::map<unsigned, AttributeSet> MAttributeGroups;
156 /// FunctionBBs - While parsing a function body, this is a list of the basic
157 /// blocks for the function.
158 std::vector<BasicBlock*> FunctionBBs;
160 // When reading the module header, this list is populated with functions that
161 // have bodies later in the file.
162 std::vector<Function*> FunctionsWithBodies;
164 // When intrinsic functions are encountered which require upgrading they are
165 // stored here with their replacement function.
166 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
167 UpgradedIntrinsicMap UpgradedIntrinsics;
169 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
170 DenseMap<unsigned, unsigned> MDKindMap;
172 // Several operations happen after the module header has been read, but
173 // before function bodies are processed. This keeps track of whether
174 // we've done this yet.
175 bool SeenFirstFunctionBody;
177 /// DeferredFunctionInfo - When function bodies are initially scanned, this
178 /// map contains info about where to find deferred function body in the
180 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
182 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
183 /// are resolved lazily when functions are loaded.
184 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
185 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
187 /// UseRelativeIDs - Indicates that we are using a new encoding for
188 /// instruction operands where most operands in the current
189 /// FUNCTION_BLOCK are encoded relative to the instruction number,
190 /// for a more compact encoding. Some instruction operands are not
191 /// relative to the instruction ID: basic block numbers, and types.
192 /// Once the old style function blocks have been phased out, we would
193 /// not need this flag.
196 static const error_category &BitcodeErrorCategory();
200 BitcodeStreamInvalidSize,
201 ConflictingMETADATA_KINDRecords,
202 CouldNotFindFunctionInStream,
204 InsufficientFunctionProtos,
205 InvalidBitcodeSignature,
206 InvalidBitcodeWrapperHeader,
207 InvalidConstantReference,
208 InvalidID, // A read identifier is not found in the table it should be in.
209 InvalidInstructionWithNoBB,
210 InvalidRecord, // A read record doesn't have the expected size or structure
211 InvalidTypeForValue, // Type read OK, but is invalid for its use
213 InvalidType, // We were unable to read a type
214 MalformedBlock, // We are unable to advance in the stream.
215 MalformedGlobalInitializerSet,
216 InvalidMultipleBlocks, // We found multiple blocks of a kind that should
218 NeverResolvedValueFoundInFunction,
219 InvalidValue // Invalid version, inst number, attr number, etc
222 error_code Error(ErrorType E) {
223 return error_code(E, BitcodeErrorCategory());
226 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
227 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
228 LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false),
229 ValueList(C), MDValueList(C),
230 SeenFirstFunctionBody(false), UseRelativeIDs(false) {
232 explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
233 : Context(C), TheModule(0), Buffer(0), BufferOwned(false),
234 LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false),
235 ValueList(C), MDValueList(C),
236 SeenFirstFunctionBody(false), UseRelativeIDs(false) {
242 void materializeForwardReferencedFunctions();
246 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
247 /// when the reader is destroyed.
248 void setBufferOwned(bool Owned) { BufferOwned = Owned; }
250 bool isMaterializable(const GlobalValue *GV) const override;
251 bool isDematerializable(const GlobalValue *GV) const override;
252 error_code Materialize(GlobalValue *GV) override;
253 error_code MaterializeModule(Module *M) override;
254 void Dematerialize(GlobalValue *GV) override;
256 /// @brief Main interface to parsing a bitcode buffer.
257 /// @returns true if an error occurred.
258 error_code ParseBitcodeInto(Module *M);
260 /// @brief Cheap mechanism to just extract module triple
261 /// @returns true if an error occurred.
262 error_code ParseTriple(std::string &Triple);
264 static uint64_t decodeSignRotatedValue(uint64_t V);
267 Type *getTypeByID(unsigned ID);
268 Value *getFnValueByID(unsigned ID, Type *Ty) {
269 if (Ty && Ty->isMetadataTy())
270 return MDValueList.getValueFwdRef(ID);
271 return ValueList.getValueFwdRef(ID, Ty);
273 BasicBlock *getBasicBlock(unsigned ID) const {
274 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
275 return FunctionBBs[ID];
277 AttributeSet getAttributes(unsigned i) const {
278 if (i-1 < MAttributes.size())
279 return MAttributes[i-1];
280 return AttributeSet();
283 /// getValueTypePair - Read a value/type pair out of the specified record from
284 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
285 /// Return true on failure.
286 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
287 unsigned InstNum, Value *&ResVal) {
288 if (Slot == Record.size()) return true;
289 unsigned ValNo = (unsigned)Record[Slot++];
290 // Adjust the ValNo, if it was encoded relative to the InstNum.
292 ValNo = InstNum - ValNo;
293 if (ValNo < InstNum) {
294 // If this is not a forward reference, just return the value we already
296 ResVal = getFnValueByID(ValNo, 0);
298 } else if (Slot == Record.size()) {
302 unsigned TypeNo = (unsigned)Record[Slot++];
303 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
307 /// popValue - Read a value out of the specified record from slot 'Slot'.
308 /// Increment Slot past the number of slots used by the value in the record.
309 /// Return true if there is an error.
310 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
311 unsigned InstNum, Type *Ty, Value *&ResVal) {
312 if (getValue(Record, Slot, InstNum, Ty, ResVal))
314 // All values currently take a single record slot.
319 /// getValue -- Like popValue, but does not increment the Slot number.
320 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
321 unsigned InstNum, Type *Ty, Value *&ResVal) {
322 ResVal = getValue(Record, Slot, InstNum, Ty);
326 /// getValue -- Version of getValue that returns ResVal directly,
327 /// or 0 if there is an error.
328 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
329 unsigned InstNum, Type *Ty) {
330 if (Slot == Record.size()) return 0;
331 unsigned ValNo = (unsigned)Record[Slot];
332 // Adjust the ValNo, if it was encoded relative to the InstNum.
334 ValNo = InstNum - ValNo;
335 return getFnValueByID(ValNo, Ty);
338 /// getValueSigned -- Like getValue, but decodes signed VBRs.
339 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
340 unsigned InstNum, Type *Ty) {
341 if (Slot == Record.size()) return 0;
342 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
343 // Adjust the ValNo, if it was encoded relative to the InstNum.
345 ValNo = InstNum - ValNo;
346 return getFnValueByID(ValNo, Ty);
349 error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
350 error_code ParseModule(bool Resume);
351 error_code ParseAttributeBlock();
352 error_code ParseAttributeGroupBlock();
353 error_code ParseTypeTable();
354 error_code ParseTypeTableBody();
356 error_code ParseValueSymbolTable();
357 error_code ParseConstants();
358 error_code RememberAndSkipFunctionBody();
359 error_code ParseFunctionBody(Function *F);
360 error_code GlobalCleanup();
361 error_code ResolveGlobalAndAliasInits();
362 error_code ParseMetadata();
363 error_code ParseMetadataAttachment();
364 error_code ParseModuleTriple(std::string &Triple);
365 error_code ParseUseLists();
366 error_code InitStream();
367 error_code InitStreamFromBuffer();
368 error_code InitLazyStream();
369 error_code FindFunctionInStream(Function *F,
370 DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator);
373 } // End llvm namespace