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 LLVM_LIB_BITCODE_READER_BITCODEREADER_H
15 #define LLVM_LIB_BITCODE_READER_BITCODEREADER_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"
26 #include <system_error>
34 //===----------------------------------------------------------------------===//
35 // BitcodeReaderValueList Class
36 //===----------------------------------------------------------------------===//
38 class BitcodeReaderValueList {
39 std::vector<WeakVH> ValuePtrs;
41 /// ResolveConstants - As we resolve forward-referenced constants, we add
42 /// information about them to this vector. This allows us to resolve them in
43 /// bulk instead of resolving each reference at a time. See the code in
44 /// ResolveConstantForwardRefs for more information about this.
46 /// The key of this vector is the placeholder constant, the value is the slot
47 /// number that holds the resolved value.
48 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
49 ResolveConstantsTy ResolveConstants;
52 BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
53 ~BitcodeReaderValueList() {
54 assert(ResolveConstants.empty() && "Constants not resolved?");
57 // vector compatibility methods
58 unsigned size() const { return ValuePtrs.size(); }
59 void resize(unsigned N) { ValuePtrs.resize(N); }
60 void push_back(Value *V) {
61 ValuePtrs.push_back(V);
65 assert(ResolveConstants.empty() && "Constants not resolved?");
69 Value *operator[](unsigned i) const {
70 assert(i < ValuePtrs.size());
74 Value *back() const { return ValuePtrs.back(); }
75 void pop_back() { ValuePtrs.pop_back(); }
76 bool empty() const { return ValuePtrs.empty(); }
77 void shrinkTo(unsigned N) {
78 assert(N <= size() && "Invalid shrinkTo request!");
82 Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
83 Value *getValueFwdRef(unsigned Idx, Type *Ty);
85 void AssignValue(Value *V, unsigned Idx);
87 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
88 /// resolves any forward references.
89 void ResolveConstantForwardRefs();
93 //===----------------------------------------------------------------------===//
94 // BitcodeReaderMDValueList Class
95 //===----------------------------------------------------------------------===//
97 class BitcodeReaderMDValueList {
98 std::vector<WeakVH> MDValuePtrs;
100 LLVMContext &Context;
102 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
104 // vector compatibility methods
105 unsigned size() const { return MDValuePtrs.size(); }
106 void resize(unsigned N) { MDValuePtrs.resize(N); }
107 void push_back(Value *V) { MDValuePtrs.push_back(V); }
108 void clear() { MDValuePtrs.clear(); }
109 Value *back() const { return MDValuePtrs.back(); }
110 void pop_back() { MDValuePtrs.pop_back(); }
111 bool empty() const { return MDValuePtrs.empty(); }
113 Value *operator[](unsigned i) const {
114 assert(i < MDValuePtrs.size());
115 return MDValuePtrs[i];
118 void shrinkTo(unsigned N) {
119 assert(N <= size() && "Invalid shrinkTo request!");
120 MDValuePtrs.resize(N);
123 Value *getValueFwdRef(unsigned Idx);
124 void AssignValue(Value *V, unsigned Idx);
127 class BitcodeReader : public GVMaterializer {
128 LLVMContext &Context;
130 std::unique_ptr<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 std::vector<Comdat *> ComdatList;
141 SmallVector<Instruction *, 64> InstructionList;
143 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
144 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
145 std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
146 std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
148 SmallVector<Instruction*, 64> InstsWithTBAATag;
150 /// MAttributes - The set of attributes by index. Index zero in the
151 /// file is for null, and is thus not represented here. As such all indices
153 std::vector<AttributeSet> MAttributes;
155 /// \brief The set of attribute groups.
156 std::map<unsigned, AttributeSet> MAttributeGroups;
158 /// FunctionBBs - While parsing a function body, this is a list of the basic
159 /// blocks for the function.
160 std::vector<BasicBlock*> FunctionBBs;
162 // When reading the module header, this list is populated with functions that
163 // have bodies later in the file.
164 std::vector<Function*> FunctionsWithBodies;
166 // When intrinsic functions are encountered which require upgrading they are
167 // stored here with their replacement function.
168 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
169 UpgradedIntrinsicMap UpgradedIntrinsics;
171 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
172 DenseMap<unsigned, unsigned> MDKindMap;
174 // Several operations happen after the module header has been read, but
175 // before function bodies are processed. This keeps track of whether
176 // we've done this yet.
177 bool SeenFirstFunctionBody;
179 /// DeferredFunctionInfo - When function bodies are initially scanned, this
180 /// map contains info about where to find deferred function body in the
182 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
184 /// These are basic blocks forward-referenced by block addresses. They are
185 /// inserted lazily into functions when they're loaded. The basic block ID is
186 /// its index into the vector.
187 DenseMap<Function *, std::vector<BasicBlock *>> BasicBlockFwdRefs;
188 std::deque<Function *> BasicBlockFwdRefQueue;
190 /// UseRelativeIDs - Indicates that we are using a new encoding for
191 /// instruction operands where most operands in the current
192 /// FUNCTION_BLOCK are encoded relative to the instruction number,
193 /// for a more compact encoding. Some instruction operands are not
194 /// relative to the instruction ID: basic block numbers, and types.
195 /// Once the old style function blocks have been phased out, we would
196 /// not need this flag.
199 /// True if all functions will be materialized, negating the need to process
200 /// (e.g.) blockaddress forward references.
201 bool WillMaterializeAllForwardRefs;
203 /// Functions that have block addresses taken. This is usually empty.
204 SmallPtrSet<const Function *, 4> BlockAddressesTaken;
207 std::error_code Error(BitcodeError E) { return make_error_code(E); }
209 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
210 : Context(C), TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
211 NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
212 MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
213 WillMaterializeAllForwardRefs(false) {}
214 explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
215 : Context(C), TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer),
216 NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
217 MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false),
218 WillMaterializeAllForwardRefs(false) {}
219 ~BitcodeReader() { FreeState(); }
221 std::error_code materializeForwardReferencedFunctions();
225 void releaseBuffer();
227 bool isDematerializable(const GlobalValue *GV) const override;
228 std::error_code materialize(GlobalValue *GV) override;
229 std::error_code MaterializeModule(Module *M) override;
230 std::vector<StructType *> getIdentifiedStructTypes() const override;
231 void Dematerialize(GlobalValue *GV) override;
233 /// @brief Main interface to parsing a bitcode buffer.
234 /// @returns true if an error occurred.
235 std::error_code ParseBitcodeInto(Module *M);
237 /// @brief Cheap mechanism to just extract module triple
238 /// @returns true if an error occurred.
239 ErrorOr<std::string> parseTriple();
241 static uint64_t decodeSignRotatedValue(uint64_t V);
244 std::vector<StructType *> IdentifiedStructTypes;
245 StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
246 StructType *createIdentifiedStructType(LLVMContext &Context);
248 Type *getTypeByID(unsigned ID);
249 Value *getFnValueByID(unsigned ID, Type *Ty) {
250 if (Ty && Ty->isMetadataTy())
251 return MDValueList.getValueFwdRef(ID);
252 return ValueList.getValueFwdRef(ID, Ty);
254 BasicBlock *getBasicBlock(unsigned ID) const {
255 if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
256 return FunctionBBs[ID];
258 AttributeSet getAttributes(unsigned i) const {
259 if (i-1 < MAttributes.size())
260 return MAttributes[i-1];
261 return AttributeSet();
264 /// getValueTypePair - Read a value/type pair out of the specified record from
265 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
266 /// Return true on failure.
267 bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
268 unsigned InstNum, Value *&ResVal) {
269 if (Slot == Record.size()) return true;
270 unsigned ValNo = (unsigned)Record[Slot++];
271 // Adjust the ValNo, if it was encoded relative to the InstNum.
273 ValNo = InstNum - ValNo;
274 if (ValNo < InstNum) {
275 // If this is not a forward reference, just return the value we already
277 ResVal = getFnValueByID(ValNo, nullptr);
278 return ResVal == nullptr;
279 } else if (Slot == Record.size()) {
283 unsigned TypeNo = (unsigned)Record[Slot++];
284 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
285 return ResVal == nullptr;
288 /// popValue - Read a value out of the specified record from slot 'Slot'.
289 /// Increment Slot past the number of slots used by the value in the record.
290 /// Return true if there is an error.
291 bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
292 unsigned InstNum, Type *Ty, Value *&ResVal) {
293 if (getValue(Record, Slot, InstNum, Ty, ResVal))
295 // All values currently take a single record slot.
300 /// getValue -- Like popValue, but does not increment the Slot number.
301 bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
302 unsigned InstNum, Type *Ty, Value *&ResVal) {
303 ResVal = getValue(Record, Slot, InstNum, Ty);
304 return ResVal == nullptr;
307 /// getValue -- Version of getValue that returns ResVal directly,
308 /// or 0 if there is an error.
309 Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
310 unsigned InstNum, Type *Ty) {
311 if (Slot == Record.size()) return nullptr;
312 unsigned ValNo = (unsigned)Record[Slot];
313 // Adjust the ValNo, if it was encoded relative to the InstNum.
315 ValNo = InstNum - ValNo;
316 return getFnValueByID(ValNo, Ty);
319 /// getValueSigned -- Like getValue, but decodes signed VBRs.
320 Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
321 unsigned InstNum, Type *Ty) {
322 if (Slot == Record.size()) return nullptr;
323 unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
324 // Adjust the ValNo, if it was encoded relative to the InstNum.
326 ValNo = InstNum - ValNo;
327 return getFnValueByID(ValNo, Ty);
330 std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
331 std::error_code ParseModule(bool Resume);
332 std::error_code ParseAttributeBlock();
333 std::error_code ParseAttributeGroupBlock();
334 std::error_code ParseTypeTable();
335 std::error_code ParseTypeTableBody();
337 std::error_code ParseValueSymbolTable();
338 std::error_code ParseConstants();
339 std::error_code RememberAndSkipFunctionBody();
340 std::error_code ParseFunctionBody(Function *F);
341 std::error_code GlobalCleanup();
342 std::error_code ResolveGlobalAndAliasInits();
343 std::error_code ParseMetadata();
344 std::error_code ParseMetadataAttachment();
345 ErrorOr<std::string> parseModuleTriple();
346 std::error_code ParseUseLists();
347 std::error_code InitStream();
348 std::error_code InitStreamFromBuffer();
349 std::error_code InitLazyStream();
350 std::error_code FindFunctionInStream(
352 DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
355 } // End llvm namespace