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