Make ObjectFile and BitcodeReader always own the MemoryBuffer.
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.h
1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2 //
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 header defines the BitcodeReader class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
16
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>
26 #include <vector>
27
28 namespace llvm {
29   class MemoryBuffer;
30   class LLVMContext;
31
32 //===----------------------------------------------------------------------===//
33 //                          BitcodeReaderValueList Class
34 //===----------------------------------------------------------------------===//
35
36 class BitcodeReaderValueList {
37   std::vector<WeakVH> ValuePtrs;
38
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.
43   ///
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;
48   LLVMContext &Context;
49 public:
50   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
51   ~BitcodeReaderValueList() {
52     assert(ResolveConstants.empty() && "Constants not resolved?");
53   }
54
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);
60   }
61
62   void clear() {
63     assert(ResolveConstants.empty() && "Constants not resolved?");
64     ValuePtrs.clear();
65   }
66
67   Value *operator[](unsigned i) const {
68     assert(i < ValuePtrs.size());
69     return ValuePtrs[i];
70   }
71
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!");
77     ValuePtrs.resize(N);
78   }
79
80   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
81   Value *getValueFwdRef(unsigned Idx, Type *Ty);
82
83   void AssignValue(Value *V, unsigned Idx);
84
85   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
86   /// resolves any forward references.
87   void ResolveConstantForwardRefs();
88 };
89
90
91 //===----------------------------------------------------------------------===//
92 //                          BitcodeReaderMDValueList Class
93 //===----------------------------------------------------------------------===//
94
95 class BitcodeReaderMDValueList {
96   std::vector<WeakVH> MDValuePtrs;
97
98   LLVMContext &Context;
99 public:
100   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
101
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(); }
110
111   Value *operator[](unsigned i) const {
112     assert(i < MDValuePtrs.size());
113     return MDValuePtrs[i];
114   }
115
116   void shrinkTo(unsigned N) {
117     assert(N <= size() && "Invalid shrinkTo request!");
118     MDValuePtrs.resize(N);
119   }
120
121   Value *getValueFwdRef(unsigned Idx);
122   void AssignValue(Value *V, unsigned Idx);
123 };
124
125 class BitcodeReader : public GVMaterializer {
126   LLVMContext &Context;
127   Module *TheModule;
128   std::unique_ptr<MemoryBuffer> Buffer;
129   std::unique_ptr<BitstreamReader> StreamFile;
130   BitstreamCursor Stream;
131   DataStreamer *LazyStreamer;
132   uint64_t NextUnreadBit;
133   bool SeenValueSymbolTable;
134
135   std::vector<Type*> TypeList;
136   BitcodeReaderValueList ValueList;
137   BitcodeReaderMDValueList MDValueList;
138   SmallVector<Instruction *, 64> InstructionList;
139   SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
140
141   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
142   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
143   std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
144
145   SmallVector<Instruction*, 64> InstsWithTBAATag;
146
147   /// MAttributes - The set of attributes by index.  Index zero in the
148   /// file is for null, and is thus not represented here.  As such all indices
149   /// are off by one.
150   std::vector<AttributeSet> MAttributes;
151
152   /// \brief The set of attribute groups.
153   std::map<unsigned, AttributeSet> MAttributeGroups;
154
155   /// FunctionBBs - While parsing a function body, this is a list of the basic
156   /// blocks for the function.
157   std::vector<BasicBlock*> FunctionBBs;
158
159   // When reading the module header, this list is populated with functions that
160   // have bodies later in the file.
161   std::vector<Function*> FunctionsWithBodies;
162
163   // When intrinsic functions are encountered which require upgrading they are
164   // stored here with their replacement function.
165   typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
166   UpgradedIntrinsicMap UpgradedIntrinsics;
167
168   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
169   DenseMap<unsigned, unsigned> MDKindMap;
170
171   // Several operations happen after the module header has been read, but
172   // before function bodies are processed. This keeps track of whether
173   // we've done this yet.
174   bool SeenFirstFunctionBody;
175
176   /// DeferredFunctionInfo - When function bodies are initially scanned, this
177   /// map contains info about where to find deferred function body in the
178   /// stream.
179   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
180
181   /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
182   /// are resolved lazily when functions are loaded.
183   typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
184   DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
185
186   /// UseRelativeIDs - Indicates that we are using a new encoding for
187   /// instruction operands where most operands in the current
188   /// FUNCTION_BLOCK are encoded relative to the instruction number,
189   /// for a more compact encoding.  Some instruction operands are not
190   /// relative to the instruction ID: basic block numbers, and types.
191   /// Once the old style function blocks have been phased out, we would
192   /// not need this flag.
193   bool UseRelativeIDs;
194
195   static const std::error_category &BitcodeErrorCategory();
196
197 public:
198   enum ErrorType {
199     BitcodeStreamInvalidSize,
200     ConflictingMETADATA_KINDRecords,
201     CouldNotFindFunctionInStream,
202     ExpectedConstant,
203     InsufficientFunctionProtos,
204     InvalidBitcodeSignature,
205     InvalidBitcodeWrapperHeader,
206     InvalidConstantReference,
207     InvalidID, // A read identifier is not found in the table it should be in.
208     InvalidInstructionWithNoBB,
209     InvalidRecord, // A read record doesn't have the expected size or structure
210     InvalidTypeForValue, // Type read OK, but is invalid for its use
211     InvalidTYPETable,
212     InvalidType, // We were unable to read a type
213     MalformedBlock, // We are unable to advance in the stream.
214     MalformedGlobalInitializerSet,
215     InvalidMultipleBlocks, // We found multiple blocks of a kind that should
216                            // have only one
217     NeverResolvedValueFoundInFunction,
218     InvalidValue // Invalid version, inst number, attr number, etc
219   };
220
221   std::error_code Error(ErrorType E) {
222     return std::error_code(E, BitcodeErrorCategory());
223   }
224
225   explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
226       : Context(C), TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
227         NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
228         MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false) {}
229   explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
230       : Context(C), TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer),
231         NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
232         MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false) {}
233   ~BitcodeReader() { FreeState(); }
234
235   void materializeForwardReferencedFunctions();
236
237   void FreeState();
238
239   void releaseBuffer() override;
240
241   bool isMaterializable(const GlobalValue *GV) const override;
242   bool isDematerializable(const GlobalValue *GV) const override;
243   std::error_code Materialize(GlobalValue *GV) override;
244   std::error_code MaterializeModule(Module *M) override;
245   void Dematerialize(GlobalValue *GV) override;
246
247   /// @brief Main interface to parsing a bitcode buffer.
248   /// @returns true if an error occurred.
249   std::error_code ParseBitcodeInto(Module *M);
250
251   /// @brief Cheap mechanism to just extract module triple
252   /// @returns true if an error occurred.
253   std::error_code ParseTriple(std::string &Triple);
254
255   static uint64_t decodeSignRotatedValue(uint64_t V);
256
257 private:
258   Type *getTypeByID(unsigned ID);
259   Value *getFnValueByID(unsigned ID, Type *Ty) {
260     if (Ty && Ty->isMetadataTy())
261       return MDValueList.getValueFwdRef(ID);
262     return ValueList.getValueFwdRef(ID, Ty);
263   }
264   BasicBlock *getBasicBlock(unsigned ID) const {
265     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
266     return FunctionBBs[ID];
267   }
268   AttributeSet getAttributes(unsigned i) const {
269     if (i-1 < MAttributes.size())
270       return MAttributes[i-1];
271     return AttributeSet();
272   }
273
274   /// getValueTypePair - Read a value/type pair out of the specified record from
275   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
276   /// Return true on failure.
277   bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
278                         unsigned InstNum, Value *&ResVal) {
279     if (Slot == Record.size()) return true;
280     unsigned ValNo = (unsigned)Record[Slot++];
281     // Adjust the ValNo, if it was encoded relative to the InstNum.
282     if (UseRelativeIDs)
283       ValNo = InstNum - ValNo;
284     if (ValNo < InstNum) {
285       // If this is not a forward reference, just return the value we already
286       // have.
287       ResVal = getFnValueByID(ValNo, nullptr);
288       return ResVal == nullptr;
289     } else if (Slot == Record.size()) {
290       return true;
291     }
292
293     unsigned TypeNo = (unsigned)Record[Slot++];
294     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
295     return ResVal == nullptr;
296   }
297
298   /// popValue - Read a value out of the specified record from slot 'Slot'.
299   /// Increment Slot past the number of slots used by the value in the record.
300   /// Return true if there is an error.
301   bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
302                 unsigned InstNum, Type *Ty, Value *&ResVal) {
303     if (getValue(Record, Slot, InstNum, Ty, ResVal))
304       return true;
305     // All values currently take a single record slot.
306     ++Slot;
307     return false;
308   }
309
310   /// getValue -- Like popValue, but does not increment the Slot number.
311   bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
312                 unsigned InstNum, Type *Ty, Value *&ResVal) {
313     ResVal = getValue(Record, Slot, InstNum, Ty);
314     return ResVal == nullptr;
315   }
316
317   /// getValue -- Version of getValue that returns ResVal directly,
318   /// or 0 if there is an error.
319   Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
320                   unsigned InstNum, Type *Ty) {
321     if (Slot == Record.size()) return nullptr;
322     unsigned ValNo = (unsigned)Record[Slot];
323     // Adjust the ValNo, if it was encoded relative to the InstNum.
324     if (UseRelativeIDs)
325       ValNo = InstNum - ValNo;
326     return getFnValueByID(ValNo, Ty);
327   }
328
329   /// getValueSigned -- Like getValue, but decodes signed VBRs.
330   Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
331                         unsigned InstNum, Type *Ty) {
332     if (Slot == Record.size()) return nullptr;
333     unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
334     // Adjust the ValNo, if it was encoded relative to the InstNum.
335     if (UseRelativeIDs)
336       ValNo = InstNum - ValNo;
337     return getFnValueByID(ValNo, Ty);
338   }
339
340   std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
341   std::error_code ParseModule(bool Resume);
342   std::error_code ParseAttributeBlock();
343   std::error_code ParseAttributeGroupBlock();
344   std::error_code ParseTypeTable();
345   std::error_code ParseTypeTableBody();
346
347   std::error_code ParseValueSymbolTable();
348   std::error_code ParseConstants();
349   std::error_code RememberAndSkipFunctionBody();
350   std::error_code ParseFunctionBody(Function *F);
351   std::error_code GlobalCleanup();
352   std::error_code ResolveGlobalAndAliasInits();
353   std::error_code ParseMetadata();
354   std::error_code ParseMetadataAttachment();
355   std::error_code ParseModuleTriple(std::string &Triple);
356   std::error_code ParseUseLists();
357   std::error_code InitStream();
358   std::error_code InitStreamFromBuffer();
359   std::error_code InitLazyStream();
360   std::error_code FindFunctionInStream(
361       Function *F,
362       DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
363 };
364
365 } // End llvm namespace
366
367 #endif