Don't use 'using std::error_code' in include/llvm.
[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 using std::error_code;
30   class MemoryBuffer;
31   class LLVMContext;
32
33 //===----------------------------------------------------------------------===//
34 //                          BitcodeReaderValueList Class
35 //===----------------------------------------------------------------------===//
36
37 class BitcodeReaderValueList {
38   std::vector<WeakVH> ValuePtrs;
39
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.
44   ///
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;
49   LLVMContext &Context;
50 public:
51   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
52   ~BitcodeReaderValueList() {
53     assert(ResolveConstants.empty() && "Constants not resolved?");
54   }
55
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);
61   }
62
63   void clear() {
64     assert(ResolveConstants.empty() && "Constants not resolved?");
65     ValuePtrs.clear();
66   }
67
68   Value *operator[](unsigned i) const {
69     assert(i < ValuePtrs.size());
70     return ValuePtrs[i];
71   }
72
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!");
78     ValuePtrs.resize(N);
79   }
80
81   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
82   Value *getValueFwdRef(unsigned Idx, Type *Ty);
83
84   void AssignValue(Value *V, unsigned Idx);
85
86   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
87   /// resolves any forward references.
88   void ResolveConstantForwardRefs();
89 };
90
91
92 //===----------------------------------------------------------------------===//
93 //                          BitcodeReaderMDValueList Class
94 //===----------------------------------------------------------------------===//
95
96 class BitcodeReaderMDValueList {
97   std::vector<WeakVH> MDValuePtrs;
98
99   LLVMContext &Context;
100 public:
101   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
102
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(); }
111
112   Value *operator[](unsigned i) const {
113     assert(i < MDValuePtrs.size());
114     return MDValuePtrs[i];
115   }
116
117   void shrinkTo(unsigned N) {
118     assert(N <= size() && "Invalid shrinkTo request!");
119     MDValuePtrs.resize(N);
120   }
121
122   Value *getValueFwdRef(unsigned Idx);
123   void AssignValue(Value *V, unsigned Idx);
124 };
125
126 class BitcodeReader : public GVMaterializer {
127   LLVMContext &Context;
128   Module *TheModule;
129   MemoryBuffer *Buffer;
130   bool BufferOwned;
131   std::unique_ptr<BitstreamReader> StreamFile;
132   BitstreamCursor Stream;
133   DataStreamer *LazyStreamer;
134   uint64_t NextUnreadBit;
135   bool SeenValueSymbolTable;
136
137   std::vector<Type*> TypeList;
138   BitcodeReaderValueList ValueList;
139   BitcodeReaderMDValueList MDValueList;
140   SmallVector<Instruction *, 64> InstructionList;
141   SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
142
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
147   SmallVector<Instruction*, 64> InstsWithTBAATag;
148
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
151   /// are off by one.
152   std::vector<AttributeSet> MAttributes;
153
154   /// \brief The set of attribute groups.
155   std::map<unsigned, AttributeSet> MAttributeGroups;
156
157   /// FunctionBBs - While parsing a function body, this is a list of the basic
158   /// blocks for the function.
159   std::vector<BasicBlock*> FunctionBBs;
160
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;
164
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;
169
170   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
171   DenseMap<unsigned, unsigned> MDKindMap;
172
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;
177
178   /// DeferredFunctionInfo - When function bodies are initially scanned, this
179   /// map contains info about where to find deferred function body in the
180   /// stream.
181   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
182
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;
187
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.
195   bool UseRelativeIDs;
196
197   static const std::error_category &BitcodeErrorCategory();
198
199 public:
200   enum ErrorType {
201     BitcodeStreamInvalidSize,
202     ConflictingMETADATA_KINDRecords,
203     CouldNotFindFunctionInStream,
204     ExpectedConstant,
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
213     InvalidTYPETable,
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
218                            // have only one
219     NeverResolvedValueFoundInFunction,
220     InvalidValue // Invalid version, inst number, attr number, etc
221   };
222
223   error_code Error(ErrorType E) {
224     return error_code(E, BitcodeErrorCategory());
225   }
226
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) {
232   }
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) {
238   }
239   ~BitcodeReader() {
240     FreeState();
241   }
242
243   void materializeForwardReferencedFunctions();
244
245   void FreeState();
246
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; }
250
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;
256
257   /// @brief Main interface to parsing a bitcode buffer.
258   /// @returns true if an error occurred.
259   error_code ParseBitcodeInto(Module *M);
260
261   /// @brief Cheap mechanism to just extract module triple
262   /// @returns true if an error occurred.
263   error_code ParseTriple(std::string &Triple);
264
265   static uint64_t decodeSignRotatedValue(uint64_t V);
266
267 private:
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);
273   }
274   BasicBlock *getBasicBlock(unsigned ID) const {
275     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
276     return FunctionBBs[ID];
277   }
278   AttributeSet getAttributes(unsigned i) const {
279     if (i-1 < MAttributes.size())
280       return MAttributes[i-1];
281     return AttributeSet();
282   }
283
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.
292     if (UseRelativeIDs)
293       ValNo = InstNum - ValNo;
294     if (ValNo < InstNum) {
295       // If this is not a forward reference, just return the value we already
296       // have.
297       ResVal = getFnValueByID(ValNo, nullptr);
298       return ResVal == nullptr;
299     } else if (Slot == Record.size()) {
300       return true;
301     }
302
303     unsigned TypeNo = (unsigned)Record[Slot++];
304     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
305     return ResVal == nullptr;
306   }
307
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))
314       return true;
315     // All values currently take a single record slot.
316     ++Slot;
317     return false;
318   }
319
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;
325   }
326
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.
334     if (UseRelativeIDs)
335       ValNo = InstNum - ValNo;
336     return getFnValueByID(ValNo, Ty);
337   }
338
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.
345     if (UseRelativeIDs)
346       ValNo = InstNum - ValNo;
347     return getFnValueByID(ValNo, Ty);
348   }
349
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();
356
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);
372 };
373
374 } // End llvm namespace
375
376 #endif