10f870b59b809b7bd262a4cff941f4ee4ebd7859
[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/OperandTraits.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include <deque>
26 #include <system_error>
27 #include <vector>
28
29 namespace llvm {
30   class Comdat;
31   class MemoryBuffer;
32   class LLVMContext;
33
34 //===----------------------------------------------------------------------===//
35 //                          BitcodeReaderValueList Class
36 //===----------------------------------------------------------------------===//
37
38 class BitcodeReaderValueList {
39   std::vector<WeakVH> ValuePtrs;
40
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.
45   ///
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;
50   LLVMContext &Context;
51 public:
52   BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
53   ~BitcodeReaderValueList() {
54     assert(ResolveConstants.empty() && "Constants not resolved?");
55   }
56
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);
62   }
63
64   void clear() {
65     assert(ResolveConstants.empty() && "Constants not resolved?");
66     ValuePtrs.clear();
67   }
68
69   Value *operator[](unsigned i) const {
70     assert(i < ValuePtrs.size());
71     return ValuePtrs[i];
72   }
73
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!");
79     ValuePtrs.resize(N);
80   }
81
82   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
83   Value *getValueFwdRef(unsigned Idx, Type *Ty);
84
85   void AssignValue(Value *V, unsigned Idx);
86
87   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
88   /// resolves any forward references.
89   void ResolveConstantForwardRefs();
90 };
91
92
93 //===----------------------------------------------------------------------===//
94 //                          BitcodeReaderMDValueList Class
95 //===----------------------------------------------------------------------===//
96
97 class BitcodeReaderMDValueList {
98   std::vector<WeakVH> MDValuePtrs;
99
100   LLVMContext &Context;
101 public:
102   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
103
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(); }
112
113   Value *operator[](unsigned i) const {
114     assert(i < MDValuePtrs.size());
115     return MDValuePtrs[i];
116   }
117
118   void shrinkTo(unsigned N) {
119     assert(N <= size() && "Invalid shrinkTo request!");
120     MDValuePtrs.resize(N);
121   }
122
123   Value *getValueFwdRef(unsigned Idx);
124   void AssignValue(Value *V, unsigned Idx);
125 };
126
127 class BitcodeReader : public GVMaterializer {
128   LLVMContext &Context;
129   Module *TheModule;
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;
136
137   std::vector<Type*> TypeList;
138   BitcodeReaderValueList ValueList;
139   BitcodeReaderMDValueList MDValueList;
140   std::vector<Comdat *> ComdatList;
141   SmallVector<Instruction *, 64> InstructionList;
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   std::vector<std::pair<Function*, unsigned> > FunctionPrologues;
147
148   SmallVector<Instruction*, 64> InstsWithTBAATag;
149
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
152   /// are off by one.
153   std::vector<AttributeSet> MAttributes;
154
155   /// \brief The set of attribute groups.
156   std::map<unsigned, AttributeSet> MAttributeGroups;
157
158   /// FunctionBBs - While parsing a function body, this is a list of the basic
159   /// blocks for the function.
160   std::vector<BasicBlock*> FunctionBBs;
161
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;
165
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;
170
171   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
172   DenseMap<unsigned, unsigned> MDKindMap;
173
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;
178
179   /// DeferredFunctionInfo - When function bodies are initially scanned, this
180   /// map contains info about where to find deferred function body in the
181   /// stream.
182   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
183
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;
189
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.
197   bool UseRelativeIDs;
198
199   /// True if all functions will be materialized, negating the need to process
200   /// (e.g.) blockaddress forward references.
201   bool WillMaterializeAllForwardRefs;
202
203   /// Functions that have block addresses taken.  This is usually empty.
204   SmallPtrSet<const Function *, 4> BlockAddressesTaken;
205
206 public:
207   std::error_code Error(BitcodeError E) { return make_error_code(E); }
208
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(); }
220
221   std::error_code materializeForwardReferencedFunctions();
222
223   void FreeState();
224
225   void releaseBuffer();
226
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;
232
233   /// @brief Main interface to parsing a bitcode buffer.
234   /// @returns true if an error occurred.
235   std::error_code ParseBitcodeInto(Module *M);
236
237   /// @brief Cheap mechanism to just extract module triple
238   /// @returns true if an error occurred.
239   ErrorOr<std::string> parseTriple();
240
241   static uint64_t decodeSignRotatedValue(uint64_t V);
242
243 private:
244   std::vector<StructType *> IdentifiedStructTypes;
245   StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
246   StructType *createIdentifiedStructType(LLVMContext &Context);
247
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);
253   }
254   BasicBlock *getBasicBlock(unsigned ID) const {
255     if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
256     return FunctionBBs[ID];
257   }
258   AttributeSet getAttributes(unsigned i) const {
259     if (i-1 < MAttributes.size())
260       return MAttributes[i-1];
261     return AttributeSet();
262   }
263
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.
272     if (UseRelativeIDs)
273       ValNo = InstNum - ValNo;
274     if (ValNo < InstNum) {
275       // If this is not a forward reference, just return the value we already
276       // have.
277       ResVal = getFnValueByID(ValNo, nullptr);
278       return ResVal == nullptr;
279     } else if (Slot == Record.size()) {
280       return true;
281     }
282
283     unsigned TypeNo = (unsigned)Record[Slot++];
284     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
285     return ResVal == nullptr;
286   }
287
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))
294       return true;
295     // All values currently take a single record slot.
296     ++Slot;
297     return false;
298   }
299
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;
305   }
306
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.
314     if (UseRelativeIDs)
315       ValNo = InstNum - ValNo;
316     return getFnValueByID(ValNo, Ty);
317   }
318
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.
325     if (UseRelativeIDs)
326       ValNo = InstNum - ValNo;
327     return getFnValueByID(ValNo, Ty);
328   }
329
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();
336
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(
351       Function *F,
352       DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
353 };
354
355 } // End llvm namespace
356
357 #endif