BitcodeReader.h revision d9a3bad4487dee0b9ed1a0f5555dffe605826158
1c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
3c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//                     The LLVM Compiler Infrastructure
4c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is distributed under the University of Illinois Open Source
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// License. See LICENSE.TXT for details.
7c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
8c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===----------------------------------------------------------------------===//
9c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
10c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// This header defines the BitcodeReader class.
11c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//
121468b668aa964beb1220e9b36162b092fb54952bHoward Hinnant//===----------------------------------------------------------------------===//
13c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
14c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#ifndef BITCODE_READER_H
15c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#define BITCODE_READER_H
16933afa9761c1c1f916161278a99284d50a594939Marshall Clow
17933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/GVMaterializer.h"
18c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant#include "llvm/Attributes.h"
19933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/Type.h"
20933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/OperandTraits.h"
21933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/Bitcode/BitstreamReader.h"
22933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/Bitcode/LLVMBitCodes.h"
23933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/Support/ValueHandle.h"
24933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include "llvm/ADT/DenseMap.h"
25933afa9761c1c1f916161278a99284d50a594939Marshall Clow#include <vector>
26c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
27c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnantnamespace llvm {
28c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  class MemoryBuffer;
29c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  class LLVMContext;
30c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
31c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===----------------------------------------------------------------------===//
32c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//                          BitcodeReaderValueList Class
33c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant//===----------------------------------------------------------------------===//
34c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
35c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnantclass BitcodeReaderValueList {
36c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  std::vector<WeakVH> ValuePtrs;
37c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
38c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// ResolveConstants - As we resolve forward-referenced constants, we add
39c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// information about them to this vector.  This allows us to resolve them in
40c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// bulk instead of resolving each reference at a time.  See the code in
41c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// ResolveConstantForwardRefs for more information about this.
42c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  ///
43c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// The key of this vector is the placeholder constant, the value is the slot
44c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  /// number that holds the resolved value.
45c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
46c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  ResolveConstantsTy ResolveConstants;
47c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  LLVMContext &Context;
48c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnantpublic:
49c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
50c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  ~BitcodeReaderValueList() {
51c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant    assert(ResolveConstants.empty() && "Constants not resolved?");
52c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  }
53c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
54c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  // vector compatibility methods
55c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  unsigned size() const { return ValuePtrs.size(); }
56933afa9761c1c1f916161278a99284d50a594939Marshall Clow  void resize(unsigned N) { ValuePtrs.resize(N); }
57933afa9761c1c1f916161278a99284d50a594939Marshall Clow  void push_back(Value *V) {
58933afa9761c1c1f916161278a99284d50a594939Marshall Clow    ValuePtrs.push_back(V);
59933afa9761c1c1f916161278a99284d50a594939Marshall Clow  }
60933afa9761c1c1f916161278a99284d50a594939Marshall Clow
61933afa9761c1c1f916161278a99284d50a594939Marshall Clow  void clear() {
62933afa9761c1c1f916161278a99284d50a594939Marshall Clow    assert(ResolveConstants.empty() && "Constants not resolved?");
63933afa9761c1c1f916161278a99284d50a594939Marshall Clow    ValuePtrs.clear();
64933afa9761c1c1f916161278a99284d50a594939Marshall Clow  }
65933afa9761c1c1f916161278a99284d50a594939Marshall Clow
66933afa9761c1c1f916161278a99284d50a594939Marshall Clow  Value *operator[](unsigned i) const {
67933afa9761c1c1f916161278a99284d50a594939Marshall Clow    assert(i < ValuePtrs.size());
68933afa9761c1c1f916161278a99284d50a594939Marshall Clow    return ValuePtrs[i];
69933afa9761c1c1f916161278a99284d50a594939Marshall Clow  }
70c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant
71c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant  Value *back() const { return ValuePtrs.back(); }
72    void pop_back() { ValuePtrs.pop_back(); }
73  bool empty() const { return ValuePtrs.empty(); }
74  void shrinkTo(unsigned N) {
75    assert(N <= size() && "Invalid shrinkTo request!");
76    ValuePtrs.resize(N);
77  }
78
79  Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
80  Value *getValueFwdRef(unsigned Idx, Type *Ty);
81
82  void AssignValue(Value *V, unsigned Idx);
83
84  /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
85  /// resolves any forward references.
86  void ResolveConstantForwardRefs();
87};
88
89
90//===----------------------------------------------------------------------===//
91//                          BitcodeReaderMDValueList Class
92//===----------------------------------------------------------------------===//
93
94class BitcodeReaderMDValueList {
95  std::vector<WeakVH> MDValuePtrs;
96
97  LLVMContext &Context;
98public:
99  BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
100
101  // vector compatibility methods
102  unsigned size() const       { return MDValuePtrs.size(); }
103  void resize(unsigned N)     { MDValuePtrs.resize(N); }
104  void push_back(Value *V)    { MDValuePtrs.push_back(V);  }
105  void clear()                { MDValuePtrs.clear();  }
106  Value *back() const         { return MDValuePtrs.back(); }
107  void pop_back()             { MDValuePtrs.pop_back(); }
108  bool empty() const          { return MDValuePtrs.empty(); }
109
110  Value *operator[](unsigned i) const {
111    assert(i < MDValuePtrs.size());
112    return MDValuePtrs[i];
113  }
114
115  void shrinkTo(unsigned N) {
116    assert(N <= size() && "Invalid shrinkTo request!");
117    MDValuePtrs.resize(N);
118  }
119
120  Value *getValueFwdRef(unsigned Idx);
121  void AssignValue(Value *V, unsigned Idx);
122};
123
124class BitcodeReader : public GVMaterializer {
125  LLVMContext &Context;
126  Module *TheModule;
127  MemoryBuffer *Buffer;
128  bool BufferOwned;
129  OwningPtr<BitstreamReader> StreamFile;
130  BitstreamCursor Stream;
131  DataStreamer *LazyStreamer;
132  uint64_t NextUnreadBit;
133  bool SeenValueSymbolTable;
134
135  const char *ErrorString;
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
146  /// MAttributes - The set of attributes by index.  Index zero in the
147  /// file is for null, and is thus not represented here.  As such all indices
148  /// are off by one.
149  std::vector<AttrListPtr> MAttributes;
150
151  /// FunctionBBs - While parsing a function body, this is a list of the basic
152  /// blocks for the function.
153  std::vector<BasicBlock*> FunctionBBs;
154
155  // When reading the module header, this list is populated with functions that
156  // have bodies later in the file.
157  std::vector<Function*> FunctionsWithBodies;
158
159  // When intrinsic functions are encountered which require upgrading they are
160  // stored here with their replacement function.
161  typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
162  UpgradedIntrinsicMap UpgradedIntrinsics;
163
164  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
165  DenseMap<unsigned, unsigned> MDKindMap;
166
167  // Several operations happen after the module header has been read, but
168  // before function bodies are processed. This keeps track of whether
169  // we've done this yet.
170  bool SeenFirstFunctionBody;
171
172  /// DeferredFunctionInfo - When function bodies are initially scanned, this
173  /// map contains info about where to find deferred function body in the
174  /// stream.
175  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
176
177  /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
178  /// are resolved lazily when functions are loaded.
179  typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
180  DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
181
182  /// UseRelativeIDs - Indicates that we are using a new encoding for
183  /// instrunction operands where most operands in the current
184  /// FUNCTION_BLOCK are encoded relative to the instruction number,
185  /// for a more compact encoding.  Some instruction operands are not
186  /// relative to the instruction ID: basic block numbers, and types.
187  /// Once the old style function blocks have been phased out, we would
188  /// not need this flag.
189  bool UseRelativeIDs;
190
191public:
192  explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
193    : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
194      LazyStreamer(0), NextUnreadBit(0), SeenValueSymbolTable(false),
195      ErrorString(0), ValueList(C), MDValueList(C),
196      SeenFirstFunctionBody(false), UseRelativeIDs(false) {
197  }
198  explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
199    : Context(C), TheModule(0), Buffer(0), BufferOwned(false),
200      LazyStreamer(streamer), NextUnreadBit(0), SeenValueSymbolTable(false),
201      ErrorString(0), ValueList(C), MDValueList(C),
202      SeenFirstFunctionBody(false), UseRelativeIDs(false) {
203  }
204  ~BitcodeReader() {
205    FreeState();
206  }
207
208  void materializeForwardReferencedFunctions();
209
210  void FreeState();
211
212  /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
213  /// when the reader is destroyed.
214  void setBufferOwned(bool Owned) { BufferOwned = Owned; }
215
216  virtual bool isMaterializable(const GlobalValue *GV) const;
217  virtual bool isDematerializable(const GlobalValue *GV) const;
218  virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
219  virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
220  virtual void Dematerialize(GlobalValue *GV);
221
222  bool Error(const char *Str) {
223    ErrorString = Str;
224    return true;
225  }
226  const char *getErrorString() const { return ErrorString; }
227
228  /// @brief Main interface to parsing a bitcode buffer.
229  /// @returns true if an error occurred.
230  bool ParseBitcodeInto(Module *M);
231
232  /// @brief Cheap mechanism to just extract module triple
233  /// @returns true if an error occurred.
234  bool ParseTriple(std::string &Triple);
235
236  static uint64_t decodeSignRotatedValue(uint64_t V);
237
238private:
239  Type *getTypeByID(unsigned ID);
240  Value *getFnValueByID(unsigned ID, Type *Ty) {
241    if (Ty && Ty->isMetadataTy())
242      return MDValueList.getValueFwdRef(ID);
243    return ValueList.getValueFwdRef(ID, Ty);
244  }
245  BasicBlock *getBasicBlock(unsigned ID) const {
246    if (ID >= FunctionBBs.size()) return 0; // Invalid ID
247    return FunctionBBs[ID];
248  }
249  AttrListPtr getAttributes(unsigned i) const {
250    if (i-1 < MAttributes.size())
251      return MAttributes[i-1];
252    return AttrListPtr();
253  }
254
255  /// getValueTypePair - Read a value/type pair out of the specified record from
256  /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
257  /// Return true on failure.
258  bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
259                        unsigned InstNum, Value *&ResVal) {
260    if (Slot == Record.size()) return true;
261    unsigned ValNo = (unsigned)Record[Slot++];
262    // Adjust the ValNo, if it was encoded relative to the InstNum.
263    if (UseRelativeIDs)
264      ValNo = InstNum - ValNo;
265    if (ValNo < InstNum) {
266      // If this is not a forward reference, just return the value we already
267      // have.
268      ResVal = getFnValueByID(ValNo, 0);
269      return ResVal == 0;
270    } else if (Slot == Record.size()) {
271      return true;
272    }
273
274    unsigned TypeNo = (unsigned)Record[Slot++];
275    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
276    return ResVal == 0;
277  }
278
279  /// popValue - Read a value out of the specified record from slot 'Slot'.
280  /// Increment Slot past the number of slots used by the value in the record.
281  /// Return true if there is an error.
282  bool popValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
283                unsigned InstNum, Type *Ty, Value *&ResVal) {
284    if (getValue(Record, Slot, InstNum, Ty, ResVal))
285      return true;
286    // All values currently take a single record slot.
287    ++Slot;
288    return false;
289  }
290
291  /// getValue -- Like popValue, but does not increment the Slot number.
292  bool getValue(SmallVector<uint64_t, 64> &Record, unsigned Slot,
293                unsigned InstNum, Type *Ty, Value *&ResVal) {
294    ResVal = getValue(Record, Slot, InstNum, Ty);
295    return ResVal == 0;
296  }
297
298  /// getValue -- Version of getValue that returns ResVal directly,
299  /// or 0 if there is an error.
300  Value *getValue(SmallVector<uint64_t, 64> &Record, unsigned Slot,
301                  unsigned InstNum, Type *Ty) {
302    if (Slot == Record.size()) return 0;
303    unsigned ValNo = (unsigned)Record[Slot];
304    // Adjust the ValNo, if it was encoded relative to the InstNum.
305    if (UseRelativeIDs)
306      ValNo = InstNum - ValNo;
307    return getFnValueByID(ValNo, Ty);
308  }
309
310  /// getValueSigned -- Like getValue, but decodes signed VBRs.
311  Value *getValueSigned(SmallVector<uint64_t, 64> &Record, unsigned Slot,
312                        unsigned InstNum, Type *Ty) {
313    if (Slot == Record.size()) return 0;
314    unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
315    // Adjust the ValNo, if it was encoded relative to the InstNum.
316    if (UseRelativeIDs)
317      ValNo = InstNum - ValNo;
318    return getFnValueByID(ValNo, Ty);
319  }
320
321  bool ParseModule(bool Resume);
322  bool ParseAttributeBlock();
323  bool ParseTypeTable();
324  bool ParseTypeTableBody();
325
326  bool ParseValueSymbolTable();
327  bool ParseConstants();
328  bool RememberAndSkipFunctionBody();
329  bool ParseFunctionBody(Function *F);
330  bool GlobalCleanup();
331  bool ResolveGlobalAndAliasInits();
332  bool ParseMetadata();
333  bool ParseMetadataAttachment();
334  bool ParseModuleTriple(std::string &Triple);
335  bool ParseUseLists();
336  bool InitStream();
337  bool InitStreamFromBuffer();
338  bool InitLazyStream();
339  bool FindFunctionInStream(Function *F,
340         DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator);
341};
342
343} // End llvm namespace
344
345#endif
346