1caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//
3caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//                     The LLVM Compiler Infrastructure
4caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//
8caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//===----------------------------------------------------------------------===//
9caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//
10caee0dccffb77a003681345ab3281bcf8684526cChris Lattner// This header defines the BitcodeReader class.
11caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//
12caee0dccffb77a003681345ab3281bcf8684526cChris Lattner//===----------------------------------------------------------------------===//
13caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
14caee0dccffb77a003681345ab3281bcf8684526cChris Lattner#ifndef BITCODE_READER_H
15caee0dccffb77a003681345ab3281bcf8684526cChris Lattner#define BITCODE_READER_H
16caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
17a1514e24cc24b050f53a12650e047799358833a1Chandler Carruth#include "llvm/ADT/DenseMap.h"
1848f848716e80d01619b239111db48bfac77baad1Chris Lattner#include "llvm/Bitcode/BitstreamReader.h"
1947f96bf24687b5068aec7166cb8b3ac33ae964aeChris Lattner#include "llvm/Bitcode/LLVMBitCodes.h"
200b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Attributes.h"
2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/GVMaterializer.h"
220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/OperandTraits.h"
230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Type.h"
2436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/ValueHandle.h"
25cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines#include <system_error>
26caee0dccffb77a003681345ab3281bcf8684526cChris Lattner#include <vector>
27caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
28caee0dccffb77a003681345ab3281bcf8684526cChris Lattnernamespace llvm {
29cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  class Comdat;
30c453f76e2b4d7fd1e042b5b6d4c20556779186dfChris Lattner  class MemoryBuffer;
3112ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramer  class LLVMContext;
32170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
33efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif//===----------------------------------------------------------------------===//
34efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif//                          BitcodeReaderValueList Class
35efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif//===----------------------------------------------------------------------===//
36efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
3755c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerclass BitcodeReaderValueList {
3846e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  std::vector<WeakVH> ValuePtrs;
39170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
40ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// ResolveConstants - As we resolve forward-referenced constants, we add
41ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// information about them to this vector.  This allows us to resolve them in
42ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// bulk instead of resolving each reference at a time.  See the code in
43ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// ResolveConstantForwardRefs for more information about this.
44ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  ///
45ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// The key of this vector is the placeholder constant, the value is the slot
46ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// number that holds the resolved value.
47ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
48ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  ResolveConstantsTy ResolveConstants;
497af453a3bdc069ef769c3d3ba02f57b4271c56d0Chris Lattner  LLVMContext &Context;
50522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattnerpublic:
517af453a3bdc069ef769c3d3ba02f57b4271c56d0Chris Lattner  BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
52ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  ~BitcodeReaderValueList() {
53ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner    assert(ResolveConstants.empty() && "Constants not resolved?");
54ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  }
55efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
56522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  // vector compatibility methods
5746e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  unsigned size() const { return ValuePtrs.size(); }
5846e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  void resize(unsigned N) { ValuePtrs.resize(N); }
59522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  void push_back(Value *V) {
6046e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    ValuePtrs.push_back(V);
61522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  }
62170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
63b348bb81253a1105d23ab1a1771f8d2a6546aa1bChris Lattner  void clear() {
64ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner    assert(ResolveConstants.empty() && "Constants not resolved?");
6546e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    ValuePtrs.clear();
66b348bb81253a1105d23ab1a1771f8d2a6546aa1bChris Lattner  }
67170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
6846e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  Value *operator[](unsigned i) const {
6946e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    assert(i < ValuePtrs.size());
7046e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    return ValuePtrs[i];
7146e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  }
72170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
7346e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  Value *back() const { return ValuePtrs.back(); }
7446e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    void pop_back() { ValuePtrs.pop_back(); }
7546e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  bool empty() const { return ValuePtrs.empty(); }
76198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattner  void shrinkTo(unsigned N) {
7746e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    assert(N <= size() && "Invalid shrinkTo request!");
7846e7740a4433383e6e5561f089a091c22125bd07Chris Lattner    ValuePtrs.resize(N);
79198f34ac359c48018c6e1f784cf3770ead63b253Chris Lattner  }
80170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
81db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
82db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Value *getValueFwdRef(unsigned Idx, Type *Ty);
83170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
8446e7740a4433383e6e5561f089a091c22125bd07Chris Lattner  void AssignValue(Value *V, unsigned Idx);
85170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
86ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
87ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  /// resolves any forward references.
88ea693dfab48ee6fc07cc21abc20f487df5057a6bChris Lattner  void ResolveConstantForwardRefs();
89efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif};
90efe65369a74871c3140a540a6c95ce5d1f080954Gabor Greif
91d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel
92d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel//===----------------------------------------------------------------------===//
93d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel//                          BitcodeReaderMDValueList Class
94d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel//===----------------------------------------------------------------------===//
95d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel
9655c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerclass BitcodeReaderMDValueList {
97d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  std::vector<WeakVH> MDValuePtrs;
98170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
9950b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  LLVMContext &Context;
100d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patelpublic:
101d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
102d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel
103d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  // vector compatibility methods
104d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  unsigned size() const       { return MDValuePtrs.size(); }
105d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void resize(unsigned N)     { MDValuePtrs.resize(N); }
106d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void push_back(Value *V)    { MDValuePtrs.push_back(V);  }
107d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void clear()                { MDValuePtrs.clear();  }
108d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  Value *back() const         { return MDValuePtrs.back(); }
109d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void pop_back()             { MDValuePtrs.pop_back(); }
110d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  bool empty() const          { return MDValuePtrs.empty(); }
111170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
112d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  Value *operator[](unsigned i) const {
113d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel    assert(i < MDValuePtrs.size());
114d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel    return MDValuePtrs[i];
115d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  }
116170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
117d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void shrinkTo(unsigned N) {
118d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel    assert(N <= size() && "Invalid shrinkTo request!");
119d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel    MDValuePtrs.resize(N);
120d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  }
121d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel
122d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  Value *getValueFwdRef(unsigned Idx);
123d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  void AssignValue(Value *V, unsigned Idx);
124d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel};
125d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel
12655c06ae7afa3f862a6bb4a4441fe485c135f5b5eBenjamin Kramerclass BitcodeReader : public GVMaterializer {
12750b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  LLVMContext &Context;
128f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  Module *TheModule;
129cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::unique_ptr<MemoryBuffer> Buffer;
13036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<BitstreamReader> StreamFile;
131962dde3cef3184f1683d5070c298c9a29509d62eChris Lattner  BitstreamCursor Stream;
1322ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  DataStreamer *LazyStreamer;
1332ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  uint64_t NextUnreadBit;
1342ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  bool SeenValueSymbolTable;
135170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
1361afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  std::vector<Type*> TypeList;
137522b7b104c864da81c19d8b16c43b7a1f6a2fc40Chris Lattner  BitcodeReaderValueList ValueList;
138d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel  BitcodeReaderMDValueList MDValueList;
139cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::vector<Comdat *> ComdatList;
140e8e0213cc3daa2d0457c22e4c12e6973f21fc942Devang Patel  SmallVector<Instruction *, 64> InstructionList;
141cbbb09687f563fad96fd815a3a89920f436373e4Chad Rosier  SmallVector<SmallVector<uint64_t, 64>, 64> UseListRecords;
142e8e0213cc3daa2d0457c22e4c12e6973f21fc942Devang Patel
143e16504eb4ef8f09611cdf6e9a0be9eb886b4ed89Chris Lattner  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
14407d98b4afbdcbb4eed048400d9116de1ec83e866Chris Lattner  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
1451e3037f0be430ef2339838bbdede11f45658bd82Peter Collingbourne  std::vector<std::pair<Function*, unsigned> > FunctionPrefixes;
146170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
147804f034bd18789e9bbf4c70c10189dd6dbf04128Manman Ren  SmallVector<Instruction*, 64> InstsWithTBAATag;
148804f034bd18789e9bbf4c70c10189dd6dbf04128Manman Ren
14919c874638d9478a5d5028854817a5ee72293bb2bDevang Patel  /// MAttributes - The set of attributes by index.  Index zero in the
15048c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  /// file is for null, and is thus not represented here.  As such all indices
15148c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  /// are off by one.
15299faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  std::vector<AttributeSet> MAttributes;
153170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
154c3ba0a821b359060d76453023f3852954e6a5ee3Bill Wendling  /// \brief The set of attribute groups.
15504ef4be048934f8acf15c4ed6e3ebdd410c252bbBill Wendling  std::map<unsigned, AttributeSet> MAttributeGroups;
156c3ba0a821b359060d76453023f3852954e6a5ee3Bill Wendling
157980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  /// FunctionBBs - While parsing a function body, this is a list of the basic
158980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  /// blocks for the function.
159980e5aad4cfaa32e13b297f4201eb1088ca96cc4Chris Lattner  std::vector<BasicBlock*> FunctionBBs;
160170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
16148f848716e80d01619b239111db48bfac77baad1Chris Lattner  // When reading the module header, this list is populated with functions that
16248f848716e80d01619b239111db48bfac77baad1Chris Lattner  // have bodies later in the file.
16348f848716e80d01619b239111db48bfac77baad1Chris Lattner  std::vector<Function*> FunctionsWithBodies;
1646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth
165170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey  // When intrinsic functions are encountered which require upgrading they are
1666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  // stored here with their replacement function.
1676994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
1686994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth  UpgradedIntrinsicMap UpgradedIntrinsics;
16919538d1e9fb4f8198b94f8450c6fc43528d8b868Dan Gohman
17019538d1e9fb4f8198b94f8450c6fc43528d8b868Dan Gohman  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
17119538d1e9fb4f8198b94f8450c6fc43528d8b868Dan Gohman  DenseMap<unsigned, unsigned> MDKindMap;
172170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
1732ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  // Several operations happen after the module header has been read, but
1742ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  // before function bodies are processed. This keeps track of whether
1752ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  // we've done this yet.
1762ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  bool SeenFirstFunctionBody;
177170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
17848f848716e80d01619b239111db48bfac77baad1Chris Lattner  /// DeferredFunctionInfo - When function bodies are initially scanned, this
179f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// map contains info about where to find deferred function body in the
180f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  /// stream.
181f0356fe140af1a30587b9a86bcfb1b2c51b8ce20Jeffrey Yasskin  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
182170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
18350b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
18450b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  /// are resolved lazily when functions are loaded.
18550b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
18650b136dae90eb37f9fc9517a8638020db4f04f9cChris Lattner  DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
1879b10dfb7d6ffb0e4466f908cd7d18212a45cfdacDan Gohman
188d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// UseRelativeIDs - Indicates that we are using a new encoding for
1897b8d94933d6c04ae11bc4607cab61b7d712c5c10Jan Wen Voung  /// instruction operands where most operands in the current
190d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// FUNCTION_BLOCK are encoded relative to the instruction number,
191d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// for a more compact encoding.  Some instruction operands are not
192d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// relative to the instruction ID: basic block numbers, and types.
193d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// Once the old style function blocks have been phased out, we would
194d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// not need this flag.
195d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  bool UseRelativeIDs;
196d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
197cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  static const std::error_category &BitcodeErrorCategory();
198e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola
199caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerpublic:
200e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola  enum ErrorType {
201e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    BitcodeStreamInvalidSize,
202e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    ConflictingMETADATA_KINDRecords,
203e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    CouldNotFindFunctionInStream,
204e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    ExpectedConstant,
205e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InsufficientFunctionProtos,
206e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidBitcodeSignature,
207e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidBitcodeWrapperHeader,
208e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidConstantReference,
209e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidID, // A read identifier is not found in the table it should be in.
210e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidInstructionWithNoBB,
211e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidRecord, // A read record doesn't have the expected size or structure
212e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidTypeForValue, // Type read OK, but is invalid for its use
213e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidTYPETable,
214e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidType, // We were unable to read a type
215e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    MalformedBlock, // We are unable to advance in the stream.
216e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    MalformedGlobalInitializerSet,
217e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidMultipleBlocks, // We found multiple blocks of a kind that should
218e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola                           // have only one
219e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    NeverResolvedValueFoundInFunction,
220e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola    InvalidValue // Invalid version, inst number, attr number, etc
221e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola  };
222e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola
223cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code Error(ErrorType E) {
224cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    return std::error_code(E, BitcodeErrorCategory());
225e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola  }
226e076b5338a8a004b4ea3694c469760d87194a5d7Rafael Espindola
227081134741b40b342fb2f85722c9cea5d412489a8Chris Lattner  explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
228cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      : Context(C), TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
229cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
230cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false) {}
2312ea93875b2f2900b9d244dfd7649c9ed02a34cd7Derek Schuff  explicit BitcodeReader(DataStreamer *streamer, LLVMContext &C)
232cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      : Context(C), TheModule(nullptr), Buffer(nullptr), LazyStreamer(streamer),
233cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
234cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        MDValueList(C), SeenFirstFunctionBody(false), UseRelativeIDs(false) {}
235cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  ~BitcodeReader() { FreeState(); }
23647f79bb58e42f1a08a7f388b8b1596ded7d49bbbRafael Espindola
23747f79bb58e42f1a08a7f388b8b1596ded7d49bbbRafael Espindola  void materializeForwardReferencedFunctions();
23847f79bb58e42f1a08a7f388b8b1596ded7d49bbbRafael Espindola
239b348bb81253a1105d23ab1a1771f8d2a6546aa1bChris Lattner  void FreeState();
240170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
241cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void releaseBuffer() override;
242170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
24336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool isMaterializable(const GlobalValue *GV) const override;
24436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool isDematerializable(const GlobalValue *GV) const override;
245cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code Materialize(GlobalValue *GV) override;
246cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code MaterializeModule(Module *M) override;
24736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void Dematerialize(GlobalValue *GV) override;
248d67c632d968157e228cf42b588f8759059730ec0Chris Lattner
249caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  /// @brief Main interface to parsing a bitcode buffer.
250caee0dccffb77a003681345ab3281bcf8684526cChris Lattner  /// @returns true if an error occurred.
251cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseBitcodeInto(Module *M);
25234711747a1d2c8713e69333bacef1c880810e371Bill Wendling
25334711747a1d2c8713e69333bacef1c880810e371Bill Wendling  /// @brief Cheap mechanism to just extract module triple
25434711747a1d2c8713e69333bacef1c880810e371Bill Wendling  /// @returns true if an error occurred.
255cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  ErrorOr<std::string> parseTriple();
256d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
257d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  static uint64_t decodeSignRotatedValue(uint64_t V);
258d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
259caee0dccffb77a003681345ab3281bcf8684526cChris Lattnerprivate:
2601afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner  Type *getTypeByID(unsigned ID);
261db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  Value *getFnValueByID(unsigned ID, Type *Ty) {
262cbd40f8357437a15c653cb8cccd7124a1bb55ae2Chris Lattner    if (Ty && Ty->isMetadataTy())
263d5ac40457b62f37f0abfb1d61064f7c7300e91eeDevang Patel      return MDValueList.getValueFwdRef(ID);
2647af453a3bdc069ef769c3d3ba02f57b4271c56d0Chris Lattner    return ValueList.getValueFwdRef(ID, Ty);
265a7c49aac984fafa5dfdfcc2762d4d51b26788e38Chris Lattner  }
266f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner  BasicBlock *getBasicBlock(unsigned ID) const {
267dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
268f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner    return FunctionBBs[ID];
269f4c8e5243376af58e52c4a0930d838509bbbea2fChris Lattner  }
27099faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling  AttributeSet getAttributes(unsigned i) const {
27119c874638d9478a5d5028854817a5ee72293bb2bDevang Patel    if (i-1 < MAttributes.size())
27219c874638d9478a5d5028854817a5ee72293bb2bDevang Patel      return MAttributes[i-1];
27399faa3b4ec6d03ac7808fe4ff3fbf3d04e375502Bill Wendling    return AttributeSet();
27448c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner  }
275170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
2767337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  /// getValueTypePair - Read a value/type pair out of the specified record from
2777337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
2787337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  /// Return true on failure.
2799e639e8fd95488cb4c8ef2f7f3a41919acb29ac4Craig Topper  bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
2807337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner                        unsigned InstNum, Value *&ResVal) {
2817337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner    if (Slot == Record.size()) return true;
282650c938c41adc95e0935c75d7ed4349c46d280d1Jeff Cohen    unsigned ValNo = (unsigned)Record[Slot++];
283d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    // Adjust the ValNo, if it was encoded relative to the InstNum.
284d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    if (UseRelativeIDs)
285d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung      ValNo = InstNum - ValNo;
2867337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner    if (ValNo < InstNum) {
2877337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner      // If this is not a forward reference, just return the value we already
2887337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner      // have.
289dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      ResVal = getFnValueByID(ValNo, nullptr);
290dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return ResVal == nullptr;
2917337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner    } else if (Slot == Record.size()) {
2927337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner      return true;
2937337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner    }
294d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
295650c938c41adc95e0935c75d7ed4349c46d280d1Jeff Cohen    unsigned TypeNo = (unsigned)Record[Slot++];
2967337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
297dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return ResVal == nullptr;
2987337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  }
299d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
300d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// popValue - Read a value out of the specified record from slot 'Slot'.
301d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// Increment Slot past the number of slots used by the value in the record.
302d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// Return true if there is an error.
3039e639e8fd95488cb4c8ef2f7f3a41919acb29ac4Craig Topper  bool popValue(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
304d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung                unsigned InstNum, Type *Ty, Value *&ResVal) {
305d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    if (getValue(Record, Slot, InstNum, Ty, ResVal))
306d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung      return true;
307d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    // All values currently take a single record slot.
308d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    ++Slot;
309d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    return false;
310d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  }
311d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
312d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// getValue -- Like popValue, but does not increment the Slot number.
3139e639e8fd95488cb4c8ef2f7f3a41919acb29ac4Craig Topper  bool getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
314d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung                unsigned InstNum, Type *Ty, Value *&ResVal) {
315d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    ResVal = getValue(Record, Slot, InstNum, Ty);
316dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return ResVal == nullptr;
3177337ab9e929a4a787f90d37ee0ed8f4cc27170abChris Lattner  }
31848c85b84c1b66fb6a1b0d2afddf33da5bd82960dChris Lattner
319d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// getValue -- Version of getValue that returns ResVal directly,
320d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// or 0 if there is an error.
3219e639e8fd95488cb4c8ef2f7f3a41919acb29ac4Craig Topper  Value *getValue(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
322d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung                  unsigned InstNum, Type *Ty) {
323dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Slot == Record.size()) return nullptr;
324d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    unsigned ValNo = (unsigned)Record[Slot];
325d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    // Adjust the ValNo, if it was encoded relative to the InstNum.
326d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    if (UseRelativeIDs)
327d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung      ValNo = InstNum - ValNo;
328d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    return getFnValueByID(ValNo, Ty);
329d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  }
330d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
331d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  /// getValueSigned -- Like getValue, but decodes signed VBRs.
3329e639e8fd95488cb4c8ef2f7f3a41919acb29ac4Craig Topper  Value *getValueSigned(SmallVectorImpl<uint64_t> &Record, unsigned Slot,
333d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung                        unsigned InstNum, Type *Ty) {
334dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Slot == Record.size()) return nullptr;
335d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    unsigned ValNo = (unsigned)decodeSignRotatedValue(Record[Slot]);
336d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    // Adjust the ValNo, if it was encoded relative to the InstNum.
337d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    if (UseRelativeIDs)
338d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung      ValNo = InstNum - ValNo;
339d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung    return getFnValueByID(ValNo, Ty);
340d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung  }
341d9a3bad4487dee0b9ed1a0f5555dffe605826158Jan Wen Voung
342cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseAttrKind(uint64_t Code, Attribute::AttrKind *Kind);
343cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseModule(bool Resume);
344cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseAttributeBlock();
345cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseAttributeGroupBlock();
346cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseTypeTable();
347cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseTypeTableBody();
348cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
349cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseValueSymbolTable();
350cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseConstants();
351cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code RememberAndSkipFunctionBody();
352cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseFunctionBody(Function *F);
353cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code GlobalCleanup();
354cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ResolveGlobalAndAliasInits();
355cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseMetadata();
356cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseMetadataAttachment();
357cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  ErrorOr<std::string> parseModuleTriple();
358cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code ParseUseLists();
359cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code InitStream();
360cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code InitStreamFromBuffer();
361cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code InitLazyStream();
362cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  std::error_code FindFunctionInStream(
363cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      Function *F,
364cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      DenseMap<Function *, uint64_t>::iterator DeferredFunctionInfoIterator);
365caee0dccffb77a003681345ab3281bcf8684526cChris Lattner};
366170a15e98dc6900df1ae40d03c5f0622d792fb45Joe Abbey
367caee0dccffb77a003681345ab3281bcf8684526cChris Lattner} // End llvm namespace
368caee0dccffb77a003681345ab3281bcf8684526cChris Lattner
369caee0dccffb77a003681345ab3281bcf8684526cChris Lattner#endif
370