BitcodeReader.cpp revision 98137cca7eebca946b869b010fef2821c9bf4971
1932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===- BitcodeReader.cpp - Internal BitcodeReader implementation ----------===//
2932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//
3932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//                     The LLVM Compiler Infrastructure
4932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//
5932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// This file is distributed under the University of Illinois Open Source
6932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// License. See LICENSE.TXT for details.
7932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//
8932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
9932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//
10932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// This header defines the BitcodeReader class.
11932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//
12932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
13932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
14c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao#include "llvm/Bitcode/ReaderWriter.h"
15d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines#include "BitReader_2_7.h"
165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines#include "llvm/ADT/STLExtras.h"
17932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#include "llvm/ADT/SmallString.h"
18932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#include "llvm/ADT/SmallVector.h"
19c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray#include "llvm/IR/AutoUpgrade.h"
20b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Constants.h"
21b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/DerivedTypes.h"
221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/IR/DiagnosticPrinter.h"
235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines#include "llvm/IR/GVMaterializer.h"
24b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/InlineAsm.h"
25b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/IntrinsicInst.h"
261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/IR/LLVMContext.h"
27b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Module.h"
28b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/OperandTraits.h"
29b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines#include "llvm/IR/Operator.h"
301bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines#include "llvm/Support/ManagedStatic.h"
31932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#include "llvm/Support/MathExtras.h"
32932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#include "llvm/Support/MemoryBuffer.h"
33c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao
34932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesusing namespace llvm;
35932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesusing namespace llvm_2_7;
36932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
37932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define METADATA_NODE_2_7             2
38932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define METADATA_FN_NODE_2_7          3
39932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define METADATA_NAMED_NODE_2_7       5
40932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define METADATA_ATTACHMENT_2_7       7
41c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao#define FUNC_CODE_INST_UNWIND_2_7     14
42932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_INST_MALLOC_2_7     17
43932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_INST_FREE_2_7       18
44932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_INST_STORE_2_7      21
45932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_INST_CALL_2_7       22
46932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_INST_GETRESULT_2_7  25
47932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines#define FUNC_CODE_DEBUG_LOC_2_7       32
48932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
49be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien#define TYPE_BLOCK_ID_OLD_3_0         10
50be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien#define TYPE_SYMTAB_BLOCK_ID_OLD_3_0  13
51be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien#define TYPE_CODE_STRUCT_OLD_3_0      10
52be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien
53be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chiennamespace {
54c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray
55c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  void StripDebugInfoOfFunction(Module* M, const char* name) {
56c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    if (Function* FuncStart = M->getFunction(name)) {
57c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      while (!FuncStart->use_empty()) {
58c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray        cast<CallInst>(*FuncStart->use_begin())->eraseFromParent();
59c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      }
60c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray      FuncStart->eraseFromParent();
61c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    }
62c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  }
63c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray
64be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
65be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
66be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  /// strips that use.
67be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  void CheckDebugInfoIntrinsics(Module *M) {
68c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.func.start");
69c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.stoppoint");
70c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.region.start");
71c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray    StripDebugInfoOfFunction(M, "llvm.dbg.region.end");
72b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
73be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien    if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
74be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien      if (!Declare->use_empty()) {
75c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray        DbgDeclareInst *DDI = cast<DbgDeclareInst>(*Declare->use_begin());
761bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        if (!isa<MDNode>(ValueAsMetadata::get(DDI->getArgOperand(0))) ||
771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines            !isa<MDNode>(ValueAsMetadata::get(DDI->getArgOperand(1)))) {
78be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien          while (!Declare->use_empty()) {
79c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray            CallInst *CI = cast<CallInst>(*Declare->use_begin());
80be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien            CI->eraseFromParent();
81be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien          }
82be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien          Declare->eraseFromParent();
83be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien        }
84be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien      }
85be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien    }
86be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  }
875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//                          BitcodeReaderValueList Class
905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReaderValueList {
935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<WeakVH> ValuePtrs;
945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstants - As we resolve forward-referenced constants, we add
965cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// information about them to this vector.  This allows us to resolve them in
975cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// bulk instead of resolving each reference at a time.  See the code in
985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstantForwardRefs for more information about this.
995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ///
1005cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// The key of this vector is the placeholder constant, the value is the slot
1015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// number that holds the resolved value.
1025cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
1035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ResolveConstantsTy ResolveConstants;
1045cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
1055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
1065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderValueList(LLVMContext &C) : Context(C) {}
1075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ~BitcodeReaderValueList() {
1085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(ResolveConstants.empty() && "Constants not resolved?");
1095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // vector compatibility methods
1125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned size() const { return ValuePtrs.size(); }
1135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void resize(unsigned N) { ValuePtrs.resize(N); }
1145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void push_back(Value *V) {
1155cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.push_back(V);
1165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void clear() {
1195cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(ResolveConstants.empty() && "Constants not resolved?");
1205cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.clear();
1215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *operator[](unsigned i) const {
1245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(i < ValuePtrs.size());
1255cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ValuePtrs[i];
1265cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1275cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *back() const { return ValuePtrs.back(); }
1295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    void pop_back() { ValuePtrs.pop_back(); }
1305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool empty() const { return ValuePtrs.empty(); }
1315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void shrinkTo(unsigned N) {
1325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(N <= size() && "Invalid shrinkTo request!");
1335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ValuePtrs.resize(N);
1345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
1375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *getValueFwdRef(unsigned Idx, Type *Ty);
1385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void AssignValue(Value *V, unsigned Idx);
1405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
1425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// resolves any forward references.
1435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void ResolveConstantForwardRefs();
1445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
1455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
1485cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//                          BitcodeReaderMDValueList Class
1495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines//===----------------------------------------------------------------------===//
1505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReaderMDValueList {
1525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned NumFwdRefs;
1535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool AnyFwdRefs;
1545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<TrackingMDRef> MDValuePtrs;
1555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
1575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
1585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderMDValueList(LLVMContext &C)
1595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      : NumFwdRefs(0), AnyFwdRefs(false), Context(C) {}
1605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // vector compatibility methods
1625cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  unsigned size() const       { return MDValuePtrs.size(); }
1635cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void resize(unsigned N)     { MDValuePtrs.resize(N); }
1645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void push_back(Metadata *MD) { MDValuePtrs.emplace_back(MD); }
1655cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void clear()                { MDValuePtrs.clear();  }
1665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *back() const      { return MDValuePtrs.back(); }
1675cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void pop_back()             { MDValuePtrs.pop_back(); }
1685cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool empty() const          { return MDValuePtrs.empty(); }
1695cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1705cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *operator[](unsigned i) const {
1715cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(i < MDValuePtrs.size());
1725cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return MDValuePtrs[i];
1735cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1745cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1755cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void shrinkTo(unsigned N) {
1765cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    assert(N <= size() && "Invalid shrinkTo request!");
1775cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    MDValuePtrs.resize(N);
1785cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
1795cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *getValueFwdRef(unsigned Idx);
1815cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void AssignValue(Metadata *MD, unsigned Idx);
1825cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void tryToResolveCycles();
1835cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
1845cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1855cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesclass BitcodeReader : public GVMaterializer {
1865cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  LLVMContext &Context;
1875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DiagnosticHandlerFunction DiagnosticHandler;
1885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Module *TheModule;
1895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::unique_ptr<MemoryBuffer> Buffer;
1905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::unique_ptr<BitstreamReader> StreamFile;
1915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitstreamCursor Stream;
1925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DataStreamer *LazyStreamer;
1935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  uint64_t NextUnreadBit;
1945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool SeenValueSymbolTable;
1955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
1965cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<Type*> TypeList;
1975cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderValueList ValueList;
1985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BitcodeReaderMDValueList MDValueList;
1995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  SmallVector<Instruction *, 64> InstructionList;
2005cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
2025cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
2035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2045cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// MAttributes - The set of attributes by index.  Index zero in the
2055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// file is for null, and is thus not represented here.  As such all indices
2065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// are off by one.
2075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<AttributeSet> MAttributes;
2085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// \brief The set of attribute groups.
2105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::map<unsigned, AttributeSet> MAttributeGroups;
2115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// FunctionBBs - While parsing a function body, this is a list of the basic
2135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// blocks for the function.
2145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<BasicBlock*> FunctionBBs;
2155cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // When reading the module header, this list is populated with functions that
2175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // have bodies later in the file.
2185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<Function*> FunctionsWithBodies;
2195cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2205cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // When intrinsic functions are encountered which require upgrading they are
2215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // stored here with their replacement function.
2225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
2235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  UpgradedIntrinsicMap UpgradedIntrinsics;
2245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2255cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
2265cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<unsigned, unsigned> MDKindMap;
2275cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // Several operations happen after the module header has been read, but
2295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // before function bodies are processed. This keeps track of whether
2305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  // we've done this yet.
2315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool SeenFirstFunctionBody;
2325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// DeferredFunctionInfo - When function bodies are initially scanned, this
2345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// map contains info about where to find deferred function body in the
2355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// stream.
2365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<Function*, uint64_t> DeferredFunctionInfo;
2375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
2395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// are resolved lazily when functions are loaded.
2405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
2415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
2425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// LLVM2_7MetadataDetected - True if metadata produced by LLVM 2.7 or
2445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// earlier was detected, in which case we behave slightly differently,
2455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// for compatibility.
2465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// FIXME: Remove in LLVM 3.0.
2475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool LLVM2_7MetadataDetected;
2485cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  static const std::error_category &BitcodeErrorCategory();
2495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinespublic:
2515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(BitcodeError E, const Twine &Message);
2525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(BitcodeError E);
2535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code Error(const Twine &Message);
2545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
2565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                         DiagnosticHandlerFunction DiagnosticHandler);
2575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  ~BitcodeReader() { FreeState(); }
2585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void FreeState();
2605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void releaseBuffer();
2625cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2635cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool isDematerializable(const GlobalValue *GV) const override;
2645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code materialize(GlobalValue *GV) override;
2655cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code MaterializeModule(Module *M) override;
2665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<StructType *> getIdentifiedStructTypes() const override;
2675cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  void Dematerialize(GlobalValue *GV) override;
2685cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2695cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @brief Main interface to parsing a bitcode buffer.
2705cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @returns true if an error occurred.
2715cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseBitcodeInto(Module *M);
2725cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2735cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @brief Cheap mechanism to just extract module triple
2745cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// @returns true if an error occurred.
2755cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  llvm::ErrorOr<std::string> parseTriple();
2765cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2775cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  static uint64_t decodeSignRotatedValue(uint64_t V);
2785cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2795cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// Materialize any deferred Metadata block.
2805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code materializeMetadata() override;
2815cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
28298137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar  void setStripDebugInfo() override;
28398137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar
2845cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesprivate:
2855cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<StructType *> IdentifiedStructTypes;
2865cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  StructType *createIdentifiedStructType(LLVMContext &Context, StringRef Name);
2875cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  StructType *createIdentifiedStructType(LLVMContext &Context);
2885cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2895cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Type *getTypeByID(unsigned ID);
2905cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Type *getTypeByIDOrNull(unsigned ID);
2915cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Value *getFnValueByID(unsigned ID, Type *Ty) {
2925cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Ty && Ty->isMetadataTy())
2935cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return MetadataAsValue::get(Ty->getContext(), getFnMetadataByID(ID));
2945cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ValueList.getValueFwdRef(ID, Ty);
2955cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
2965cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  Metadata *getFnMetadataByID(unsigned ID) {
2975cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return MDValueList.getValueFwdRef(ID);
2985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
2995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  BasicBlock *getBasicBlock(unsigned ID) const {
3005cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (ID >= FunctionBBs.size()) return nullptr; // Invalid ID
3015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return FunctionBBs[ID];
3025cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  AttributeSet getAttributes(unsigned i) const {
3045cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (i-1 < MAttributes.size())
3055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return MAttributes[i-1];
3065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return AttributeSet();
3075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// getValueTypePair - Read a value/type pair out of the specified record from
3105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
3115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  /// Return true on failure.
3125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool getValueTypePair(SmallVectorImpl<uint64_t> &Record, unsigned &Slot,
3135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                        unsigned InstNum, Value *&ResVal) {
3145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Slot == Record.size()) return true;
3155cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned ValNo = (unsigned)Record[Slot++];
3165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (ValNo < InstNum) {
3175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      // If this is not a forward reference, just return the value we already
3185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      // have.
3195cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      ResVal = getFnValueByID(ValNo, nullptr);
3205cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return ResVal == nullptr;
3215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    } else if (Slot == Record.size()) {
3225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      return true;
3235cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    }
3245cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3255cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned TypeNo = (unsigned)Record[Slot++];
3265cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
3275cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ResVal == nullptr;
3285cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3295cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
3305cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                Type *Ty, Value *&ResVal) {
3315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    if (Slot == Record.size()) return true;
3325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    unsigned ValNo = (unsigned)Record[Slot++];
3335cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    ResVal = getFnValueByID(ValNo, Ty);
3345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return ResVal == 0;
3355cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  }
3365cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3375cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3385cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseModule(bool Resume);
3395cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseAttributeBlock();
3405cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseTypeTable();
3415cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseOldTypeTable();         // FIXME: Remove in LLVM 3.1
3425cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseTypeTableBody();
3435cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3445cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseOldTypeSymbolTable();   // FIXME: Remove in LLVM 3.1
3455cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseValueSymbolTable();
3465cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseConstants();
3475cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code RememberAndSkipFunctionBody();
3485cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseFunctionBody(Function *F);
3495cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code GlobalCleanup();
3505cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ResolveGlobalAndAliasInits();
3515cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseMetadata();
3525cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code ParseMetadataAttachment();
3535cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  llvm::ErrorOr<std::string> parseModuleTriple();
3545cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitStream();
3555cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitStreamFromBuffer();
3565cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code InitLazyStream();
3575cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines};
358be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien} // end anonymous namespace
359be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien
3601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
3611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             std::error_code EC, const Twine &Message) {
3621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  BitcodeDiagnosticInfo DI(EC, DS_Error, Message);
3631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  DiagnosticHandler(DI);
3641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return EC;
3651bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3671bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic std::error_code Error(DiagnosticHandlerFunction DiagnosticHandler,
3681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             std::error_code EC) {
3691bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Error(DiagnosticHandler, EC, EC.message());
3701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3711bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3721bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(BitcodeError E, const Twine &Message) {
3731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler, make_error_code(E), Message);
3741bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3751bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3761bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(const Twine &Message) {
3771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler,
3781bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                 make_error_code(BitcodeError::CorruptedBitcode), Message);
3791bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3801bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3811bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::error_code BitcodeReader::Error(BitcodeError E) {
3821bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return ::Error(DiagnosticHandler, make_error_code(E));
3831bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3841bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic DiagnosticHandlerFunction getDiagHandler(DiagnosticHandlerFunction F,
3861bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                                LLVMContext &C) {
3871bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (F)
3881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return F;
3891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return [&C](const DiagnosticInfo &DI) { C.diagnose(DI); };
3901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
3911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3921bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesBitcodeReader::BitcodeReader(MemoryBuffer *buffer, LLVMContext &C,
3931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             DiagnosticHandlerFunction DiagnosticHandler)
3941bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    : Context(C), DiagnosticHandler(getDiagHandler(DiagnosticHandler, C)),
3951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      TheModule(nullptr), Buffer(buffer), LazyStreamer(nullptr),
3961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      NextUnreadBit(0), SeenValueSymbolTable(false), ValueList(C),
3971bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      MDValueList(C), SeenFirstFunctionBody(false),
3981bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      LLVM2_7MetadataDetected(false) {}
3991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
4001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
401932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReader::FreeState() {
402579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Buffer = nullptr;
403932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<Type*>().swap(TypeList);
404932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValueList.clear();
405932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  MDValueList.clear();
406932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
407b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines  std::vector<AttributeSet>().swap(MAttributes);
408932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<BasicBlock*>().swap(FunctionBBs);
409932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<Function*>().swap(FunctionsWithBodies);
410932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  DeferredFunctionInfo.clear();
411932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  MDKindMap.clear();
412932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
413932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
414932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
415932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//  Helper functions to implement forward reference resolution, etc.
416932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
417932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
418932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ConvertToString - Convert a string from a record into an std::string, return
419932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// true on failure.
420932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinestemplate<typename StrTy>
4211bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic bool ConvertToString(ArrayRef<uint64_t> Record, unsigned Idx,
422932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                            StrTy &Result) {
423932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx > Record.size())
424932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return true;
425932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
426932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  for (unsigned i = Idx, e = Record.size(); i != e; ++i)
427932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Result += (char)Record[i];
428932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return false;
429932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
430932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
4311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic GlobalValue::LinkageTypes getDecodedLinkage(unsigned Val) {
432932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
433932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: // Map unknown/new linkages to external
4341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 0:
4351bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage;
4361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 1:
4371bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::WeakAnyLinkage;
4381bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 2:
4391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::AppendingLinkage;
4401bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 3:
4411bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::InternalLinkage;
4421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 4:
4431bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceAnyLinkage;
4441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 5:
4451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage; // Obsolete DLLImportLinkage
4461bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 6:
4471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalLinkage; // Obsolete DLLExportLinkage
4481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 7:
4491bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalWeakLinkage;
4501bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 8:
4511bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::CommonLinkage;
4521bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 9:
4531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::PrivateLinkage;
4541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 10:
4551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::WeakODRLinkage;
4561bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 11:
4571bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceODRLinkage;
4581bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 12:
4591bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::AvailableExternallyLinkage;
4601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 13:
4611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::PrivateLinkage; // Obsolete LinkerPrivateLinkage
4621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 14:
4631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::ExternalWeakLinkage; // Obsolete LinkerPrivateWeakLinkage
464d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  //ANDROID: convert LinkOnceODRAutoHideLinkage -> LinkOnceODRLinkage
4651bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  case 15:
4661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return GlobalValue::LinkOnceODRLinkage;
467932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
468932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
469932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
470932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstatic GlobalValue::VisibilityTypes GetDecodedVisibility(unsigned Val) {
471932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
472932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: // Map unknown visibilities to default.
473932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case 0: return GlobalValue::DefaultVisibility;
474932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case 1: return GlobalValue::HiddenVisibility;
475932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case 2: return GlobalValue::ProtectedVisibility;
476932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
477932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
478932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
4798b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liaostatic GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
4808b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao  switch (Val) {
4818b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 0: return GlobalVariable::NotThreadLocal;
4828b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    default: // Map unknown non-zero value to general dynamic.
4838b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 1: return GlobalVariable::GeneralDynamicTLSModel;
4848b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 2: return GlobalVariable::LocalDynamicTLSModel;
4858b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 3: return GlobalVariable::InitialExecTLSModel;
4868b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao    case 4: return GlobalVariable::LocalExecTLSModel;
4878b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao  }
4888b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao}
4898b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao
490932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstatic int GetDecodedCastOpcode(unsigned Val) {
491932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
492932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: return -1;
493932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_TRUNC   : return Instruction::Trunc;
494932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_ZEXT    : return Instruction::ZExt;
495932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_SEXT    : return Instruction::SExt;
496932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
497932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
498932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_UITOFP  : return Instruction::UIToFP;
499932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_SITOFP  : return Instruction::SIToFP;
500932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
501932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPEXT   : return Instruction::FPExt;
502932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
503932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
504932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_BITCAST : return Instruction::BitCast;
505932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
506932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
507932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstatic int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
508932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
509932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: return -1;
510932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_ADD:
511932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
512932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SUB:
513932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
514932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_MUL:
515932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
516932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_UDIV: return Instruction::UDiv;
517932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SDIV:
518932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
519932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_UREM: return Instruction::URem;
520932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SREM:
521932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
522932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SHL:  return Instruction::Shl;
523932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_LSHR: return Instruction::LShr;
524932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_ASHR: return Instruction::AShr;
525932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_AND:  return Instruction::And;
526932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_OR:   return Instruction::Or;
527932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_XOR:  return Instruction::Xor;
528932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
529932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
530932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
531932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesnamespace llvm {
532932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesnamespace {
533932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  /// @brief A class for maintaining the slot number definition
534932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  /// as a placeholder for the actual definition for forward constants defs.
535932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  class ConstantPlaceHolder : public ConstantExpr {
5361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    void operator=(const ConstantPlaceHolder &) = delete;
537932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  public:
538932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // allocate space for exactly one operand
539932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    void *operator new(size_t s) {
540932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      return User::operator new(s, 1);
541932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
542932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
543932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
544932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
545932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
546932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
547932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
548932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    static bool classof(const Value *V) {
549932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      return isa<ConstantExpr>(V) &&
550932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines             cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
551932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
552932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
553932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
554932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    /// Provide fast operand accessors
5551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
556932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  };
557932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
558932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
559932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// FIXME: can we inherit this from ConstantExpr?
560932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinestemplate <>
561932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstruct OperandTraits<ConstantPlaceHolder> :
562932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
563932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines};
5641bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
565932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
566932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
567932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
568932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
569932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx == size()) {
570932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    push_back(V);
571932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
572932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
573932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
574932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
575932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx+1);
576932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
577932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  WeakVH &OldV = ValuePtrs[Idx];
578579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (!OldV) {
579932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV = V;
580932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
581932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
582932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
583932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Handle constants and non-constants (e.g. instrs) differently for
584932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // efficiency.
585932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
586932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ResolveConstants.push_back(std::make_pair(PHC, Idx));
587932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV = V;
588932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  } else {
589932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // If there was a forward reference to this value, replace it.
590932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Value *PrevVal = OldV;
591932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV->replaceAllUsesWith(V);
592932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete PrevVal;
593932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
594932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
595932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
596932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
597932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesConstant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
598932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                    Type *Ty) {
599932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
600932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
601932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
602932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Value *V = ValuePtrs[Idx]) {
603932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    assert(Ty == V->getType() && "Type mismatch in constant table!");
604932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return cast<Constant>(V);
605932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
606932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
607932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
608932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Constant *C = new ConstantPlaceHolder(Ty, Context);
609932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValuePtrs[Idx] = C;
610932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return C;
611932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
612932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
613932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesValue *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
614932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
615932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
616932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
617932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Value *V = ValuePtrs[Idx]) {
618579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
619932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return V;
620932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
621932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
622932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // No type specified, must be invalid reference.
623579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (!Ty) return nullptr;
624932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
625932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
626932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Value *V = new Argument(Ty);
627932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValuePtrs[Idx] = V;
628932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return V;
629932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
630932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
631932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
632932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// resolves any forward references.  The idea behind this is that we sometimes
633932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// get constants (such as large arrays) which reference *many* forward ref
634932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// constants.  Replacing each of these causes a lot of thrashing when
635932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// building/reuniquing the constant.  Instead of doing this, we look at all the
636932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// uses and rewrite all the place holders at once for any constant that uses
637932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// a placeholder.
638932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReaderValueList::ResolveConstantForwardRefs() {
639932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Sort the values by-pointer so that they are efficient to look up with a
640932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // binary search.
641932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::sort(ResolveConstants.begin(), ResolveConstants.end());
642932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
643932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<Constant*, 64> NewOps;
644932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
645932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (!ResolveConstants.empty()) {
646932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Value *RealVal = operator[](ResolveConstants.back().second);
647932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Constant *Placeholder = ResolveConstants.back().first;
648932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ResolveConstants.pop_back();
649932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
650932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Loop over all users of the placeholder, updating them to reference the
651932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // new value.  If they reference more than one placeholder, update them all
652932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // at once.
653932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    while (!Placeholder->use_empty()) {
6541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      auto UI = Placeholder->user_begin();
6551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      User *U = *UI;
656932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
657932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // If the using object isn't uniqued, just update the operands.  This
658932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // handles instructions and initializers for global variables.
659932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
6601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        UI.getUse().set(RealVal);
661932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        continue;
662932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
663932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
664932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Otherwise, we have a constant that uses the placeholder.  Replace that
665932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // constant with a new constant that has *all* placeholder uses updated.
666932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *UserC = cast<Constant>(U);
667932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
668932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           I != E; ++I) {
669932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Value *NewOp;
670932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (!isa<ConstantPlaceHolder>(*I)) {
671932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Not a placeholder reference.
672932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = *I;
673932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else if (*I == Placeholder) {
674932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Common case is that it just references this one placeholder.
675932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = RealVal;
676932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else {
677932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Otherwise, look up the placeholder in ResolveConstants.
678932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          ResolveConstantsTy::iterator It =
679932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
680932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                             std::pair<Constant*, unsigned>(cast<Constant>(*I),
681932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                            0));
682932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          assert(It != ResolveConstants.end() && It->first == *I);
683932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = operator[](It->second);
684932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
685932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
686932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewOps.push_back(cast<Constant>(NewOp));
687932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
688932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
689932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Make the new constant.
690932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *NewC;
691932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
692932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantArray::get(UserCA->getType(), NewOps);
693932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
694932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantStruct::get(UserCS->getType(), NewOps);
695932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (isa<ConstantVector>(UserC)) {
696932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantVector::get(NewOps);
697932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
698932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
699932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
700932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
701932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
702932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      UserC->replaceAllUsesWith(NewC);
703932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      UserC->destroyConstant();
704932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NewOps.clear();
705932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
706932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
707932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Update all ValueHandles, they should be the only users at this point.
708932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Placeholder->replaceAllUsesWith(RealVal);
709932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete Placeholder;
710932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
711932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
712932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7131bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesvoid BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
714932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx == size()) {
7151bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    push_back(MD);
716932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
717932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
718932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
719932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
720932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx+1);
721932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  TrackingMDRef &OldMD = MDValuePtrs[Idx];
7231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (!OldMD) {
7241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    OldMD.reset(MD);
725932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
726932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
727932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
728932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If there was a forward reference to this value, replace it.
7291bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
7301bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  PrevMD->replaceAllUsesWith(MD);
7311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  --NumFwdRefs;
732932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
733932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7341bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesMetadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
735932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
736932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
737932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7381bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (Metadata *MD = MDValuePtrs[Idx])
7391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return MD;
740932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
741932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
7421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  AnyFwdRefs = true;
7431bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  ++NumFwdRefs;
7441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  Metadata *MD = MDNode::getTemporary(Context, None).release();
7451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  MDValuePtrs[Idx].reset(MD);
7461bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return MD;
7471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
7481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7491bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesvoid BitcodeReaderMDValueList::tryToResolveCycles() {
7501bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (!AnyFwdRefs)
7511bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    // Nothing to do.
7521bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return;
7531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (NumFwdRefs)
7551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    // Still forward references... can't resolve cycles.
7561bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return;
7571bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7581bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  // Resolve any cycles.
7591bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  for (auto &MD : MDValuePtrs) {
7601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    auto *N = dyn_cast_or_null<MDNode>(MD);
7611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    if (!N)
7621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      continue;
7631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    assert(!N->isTemporary() && "Unexpected forward reference");
7651bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    N->resolveCycles();
7661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  }
767932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
768932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
769932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesType *BitcodeReader::getTypeByID(unsigned ID) {
770932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // The type table size is always specified correctly.
771932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (ID >= TypeList.size())
77234edb8ad024934c13741550bc825c9b352453ad8Stephen Hines    return nullptr;
773b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
774932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Type *Ty = TypeList[ID])
775932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty;
776932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
777932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If we have a forward reference, the only possible case is when it is to a
778932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // named struct.  Just create a placeholder for now.
7791bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return TypeList[ID] = createIdentifiedStructType(Context);
7801bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
7811bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7821bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesStructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
7831bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                                      StringRef Name) {
7841bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto *Ret = StructType::create(Context, Name);
7851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  IdentifiedStructTypes.push_back(Ret);
7861bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Ret;
787932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
788932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7891bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesStructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
7901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto *Ret = StructType::create(Context);
7911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  IdentifiedStructTypes.push_back(Ret);
7921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Ret;
7931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
7941bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
796932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// FIXME: Remove in LLVM 3.1, only used by ParseOldTypeTable.
797932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesType *BitcodeReader::getTypeByIDOrNull(unsigned ID) {
798932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (ID >= TypeList.size())
799932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    TypeList.resize(ID+1);
800b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
801932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return TypeList[ID];
802932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
803932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
804932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
805932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//  Functions for parsing blocks from the bitcode file
806932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
807932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
808d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
809d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// \brief This fills an AttrBuilder object with the LLVM attributes that have
810d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// been decoded from the given integer. This function must stay in sync with
811d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// 'encodeLLVMAttributesForBitcode'.
812d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hinesstatic void decodeLLVMAttributesForBitcode(AttrBuilder &B,
813d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                           uint64_t EncodedAttrs) {
814d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // FIXME: Remove in 4.0.
815d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
816d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
817d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // the bits above 31 down by 11 bits.
818d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
819d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  assert((!Alignment || isPowerOf2_32(Alignment)) &&
820d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines         "Alignment must be a power of two.");
821d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
822d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (Alignment)
823d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    B.addAlignmentAttr(Alignment);
824d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
825d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                (EncodedAttrs & 0xffff));
826d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
827d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
828d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseAttributeBlock() {
829932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
8301bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
831932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
832932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!MAttributes.empty())
8331bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid multiple blocks");
834932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
835932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
836932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
837d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  SmallVector<AttributeSet, 8> Attrs;
838932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
839932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records.
840932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
841d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
842932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
843d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
844d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
845d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
8461bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
847d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
848d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
849d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
850d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
851d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
852932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
853932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
854932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
855932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
856d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Entry.ID, Record)) {
857932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: ignore.
858932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
859d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
860932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() & 1)
8611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
862932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
863b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
864d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        AttrBuilder B;
865d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        decodeLLVMAttributesForBitcode(B, Record[i+1]);
866d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        Attrs.push_back(AttributeSet::get(Context, Record[i], B));
867932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
868932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
869d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      MAttributes.push_back(AttributeSet::get(Context, Attrs));
870d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      Attrs.clear();
871d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
872d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    }
873d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case bitc::PARAMATTR_CODE_ENTRY: { // ENTRY: [attrgrp0, attrgrp1, ...]
874d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      for (unsigned i = 0, e = Record.size(); i != e; ++i)
875d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        Attrs.push_back(MAttributeGroups[Record[i]]);
876932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
877b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      MAttributes.push_back(AttributeSet::get(Context, Attrs));
878932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Attrs.clear();
879932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
880932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
881932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
882932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
883932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
884932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
885d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
886d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseTypeTable() {
887932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
8881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
889b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
890932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return ParseTypeTableBody();
891932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
892932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
893d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseTypeTableBody() {
894932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!TypeList.empty())
8951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid multiple blocks");
896932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
897932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
898932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NumRecords = 0;
899932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
900932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallString<64> TypeName;
901b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
902932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this type table.
903932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
904d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
905d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
906d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
907d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
908d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
9091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
910d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
911932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NumRecords != TypeList.size())
9121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
913d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
914d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
915d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
916d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
917932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
918932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
919932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
920932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
921579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    Type *ResultTy = nullptr;
922d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Entry.ID, Record)) {
923d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    default:
9241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid value");
925932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
926932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // TYPE_CODE_NUMENTRY contains a count of the number of types in the
927932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // type list.  This allows us to reserve space.
928932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
9291bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
930932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeList.resize(Record[0]);
931932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
932932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_VOID:      // VOID
933932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getVoidTy(Context);
934932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
935d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case bitc::TYPE_CODE_HALF:     // HALF
936d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      ResultTy = Type::getHalfTy(Context);
937d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
938932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_FLOAT:     // FLOAT
939932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getFloatTy(Context);
940932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
941932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
942932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getDoubleTy(Context);
943932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
944932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
945932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getX86_FP80Ty(Context);
946932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
947932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_FP128:     // FP128
948932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getFP128Ty(Context);
949932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
950932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
951932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getPPC_FP128Ty(Context);
952932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
953932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_LABEL:     // LABEL
954932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getLabelTy(Context);
955932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
956932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_METADATA:  // METADATA
957932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getMetadataTy(Context);
958932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
959932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
960932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getX86_MMXTy(Context);
961932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
962932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_INTEGER:   // INTEGER: [width]
963932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
9641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
965932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
966932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = IntegerType::get(Context, Record[0]);
967932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
968932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
969932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                    //          [pointee type, address space]
970932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
9711bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
972932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned AddressSpace = 0;
973932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() == 2)
974932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        AddressSpace = Record[1];
975932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = getTypeByID(Record[0]);
976579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!ResultTy)
9771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type");
978932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = PointerType::get(ResultTy, AddressSpace);
979932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
980932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
9813e88bf7dfe2af63827099a7d5be0173cf4c74f4dStephen Hines    case bitc::TYPE_CODE_FUNCTION_OLD: {
982b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      // FIXME: attrid is dead, remove it in LLVM 4.0
983932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // FUNCTION: [vararg, attrid, retty, paramty x N]
984932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3)
9851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
986b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      SmallVector<Type*, 8> ArgTys;
987932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 3, e = Record.size(); i != e; ++i) {
988932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Type *T = getTypeByID(Record[i]))
989932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          ArgTys.push_back(T);
990932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
991932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
992932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
993b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
994932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = getTypeByID(Record[2]);
995579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!ResultTy || ArgTys.size() < Record.size()-3)
9961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type");
997932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
998932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
999932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1000932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1001932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
1002932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
10031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1004b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      SmallVector<Type*, 8> EltTys;
1005932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1006932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Type *T = getTypeByID(Record[i]))
1007932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          EltTys.push_back(T);
1008932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
1009932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
1010932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1011932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (EltTys.size() != Record.size()-1)
10121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type");
1013932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = StructType::get(Context, EltTys, Record[0]);
1014932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1015932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1016932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_STRUCT_NAME:   // STRUCT_NAME: [strchr x N]
1017932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, TypeName))
10181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1019932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1020932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1021932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
1022932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
10231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1024b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1025932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NumRecords >= TypeList.size())
10261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1027b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1028932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Check to see if this was forward referenced, if so fill in the temp.
1029932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1030932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Res) {
1031932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Res->setName(TypeName);
1032579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        TypeList[NumRecords] = nullptr;
1033932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else  // Otherwise, create a new struct.
10341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        Res = createIdentifiedStructType(Context, TypeName);
1035932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeName.clear();
1036b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1037932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<Type*, 8> EltTys;
1038932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1039932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Type *T = getTypeByID(Record[i]))
1040932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          EltTys.push_back(T);
1041932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
1042932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
1043932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1044932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (EltTys.size() != Record.size()-1)
10451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1046932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Res->setBody(EltTys, Record[0]);
1047932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Res;
1048932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1049932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1050932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_OPAQUE: {       // OPAQUE: []
1051932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() != 1)
10521bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1053932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1054932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NumRecords >= TypeList.size())
10551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1056b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1057932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Check to see if this was forward referenced, if so fill in the temp.
1058932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
1059932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Res) {
1060932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Res->setName(TypeName);
1061579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        TypeList[NumRecords] = nullptr;
1062932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else  // Otherwise, create a new struct with no body.
10631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        Res = createIdentifiedStructType(Context, TypeName);
1064932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeName.clear();
1065932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Res;
1066932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1067b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines    }
1068932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
1069932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 2)
10701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1071932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByID(Record[1])))
1072932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = ArrayType::get(ResultTy, Record[0]);
1073932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
10741bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type");
1075932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1076932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
1077932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 2)
10781bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1079932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByID(Record[1])))
1080932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = VectorType::get(ResultTy, Record[0]);
1081932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
10821bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type");
1083932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1084932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1085932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1086932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (NumRecords >= TypeList.size())
10871bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid TYPE table");
1088932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    assert(ResultTy && "Didn't read a type?");
1089579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    assert(!TypeList[NumRecords] && "Already read type?");
1090932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    TypeList[NumRecords++] = ResultTy;
1091932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1092932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1093932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1094932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// FIXME: Remove in LLVM 3.1
1095d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseOldTypeTable() {
1096be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  if (Stream.EnterSubBlock(TYPE_BLOCK_ID_OLD_3_0))
10971bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Malformed block");
1098932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1099932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!TypeList.empty())
11001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid TYPE table");
1101b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1102b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1103932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // While horrible, we have no good ordering of types in the bc file.  Just
1104932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // iteratively parse types out of the bc file in multiple passes until we get
1105932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // them all.  Do this by saving a cursor for the start of the type block.
1106932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  BitstreamCursor StartOfTypeBlockCursor(Stream);
1107b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1108932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NumTypesRead = 0;
1109b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1110932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
1111932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesRestartScan:
1112932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NextTypeID = 0;
1113932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  bool ReadAnyTypes = false;
1114b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1115932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this type table.
1116932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
1117932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
1118932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
1119932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NextTypeID != TypeList.size())
11201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1121b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1122932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // If we haven't read all of the types yet, iterate again.
1123932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NumTypesRead != TypeList.size()) {
1124932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // If we didn't successfully read any types in this pass, then we must
1125932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // have an unhandled forward reference.
1126932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (!ReadAnyTypes)
11271bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid TYPE table");
1128b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1129932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Stream = StartOfTypeBlockCursor;
1130932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        goto RestartScan;
1131932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1132b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1133932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
11341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1135d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
1136932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1137b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1138932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
1139932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // No known subblocks, always skip them.
1140932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadSubBlockID();
1141932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.SkipBlock())
11421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1143932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1144932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1145b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1146932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
1147932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
1148932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1149932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1150b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1151932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
1152932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
1153579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    Type *ResultTy = nullptr;
1154d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Code, Record)) {
11551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    default: return Error("Invalid TYPE table");
1156932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_NUMENTRY: // TYPE_CODE_NUMENTRY: [numentries]
1157932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // TYPE_CODE_NUMENTRY contains a count of the number of types in the
1158932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // type list.  This allows us to reserve space.
1159932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
11601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1161932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeList.resize(Record[0]);
1162932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1163932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_VOID:      // VOID
1164932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getVoidTy(Context);
1165932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1166932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_FLOAT:     // FLOAT
1167932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getFloatTy(Context);
1168932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1169932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_DOUBLE:    // DOUBLE
1170932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getDoubleTy(Context);
1171932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1172932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_X86_FP80:  // X86_FP80
1173932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getX86_FP80Ty(Context);
1174932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1175932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_FP128:     // FP128
1176932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getFP128Ty(Context);
1177932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1178932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_PPC_FP128: // PPC_FP128
1179932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getPPC_FP128Ty(Context);
1180932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1181932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_LABEL:     // LABEL
1182932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getLabelTy(Context);
1183932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1184932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_METADATA:  // METADATA
1185932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getMetadataTy(Context);
1186932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1187932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_X86_MMX:   // X86_MMX
1188932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = Type::getX86_MMXTy(Context);
1189932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1190932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_INTEGER:   // INTEGER: [width]
1191932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
11921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1193932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = IntegerType::get(Context, Record[0]);
1194932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1195932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_OPAQUE:    // OPAQUE
1196932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NextTypeID < TypeList.size() && TypeList[NextTypeID] == 0)
1197bc9eb8fa13ee44d7bb46285e4c30da1236aefddfLogan Chien        ResultTy = StructType::create(Context, "");
1198932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1199be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien    case TYPE_CODE_STRUCT_OLD_3_0: {// STRUCT_OLD
1200932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NextTypeID >= TypeList.size()) break;
1201932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // If we already read it, don't reprocess.
1202932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (TypeList[NextTypeID] &&
1203932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          !cast<StructType>(TypeList[NextTypeID])->isOpaque())
1204932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
1205932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1206932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Set a type.
1207932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (TypeList[NextTypeID] == 0)
1208bc9eb8fa13ee44d7bb46285e4c30da1236aefddfLogan Chien        TypeList[NextTypeID] = StructType::create(Context, "");
1209932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1210932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<Type*> EltTys;
1211932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 1, e = Record.size(); i != e; ++i) {
1212932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Type *Elt = getTypeByIDOrNull(Record[i]))
1213932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          EltTys.push_back(Elt);
1214932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
1215932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
1216932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1217932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1218932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (EltTys.size() != Record.size()-1)
1219932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;      // Not all elements are ready.
1220b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1221932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<StructType>(TypeList[NextTypeID])->setBody(EltTys, Record[0]);
1222932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResultTy = TypeList[NextTypeID];
1223932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeList[NextTypeID] = 0;
1224932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1225932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1226932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_POINTER: { // POINTER: [pointee type] or
1227932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      //          [pointee type, address space]
1228932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
12291bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1230932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned AddressSpace = 0;
1231932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() == 2)
1232932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        AddressSpace = Record[1];
1233932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByIDOrNull(Record[0])))
1234932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = PointerType::get(ResultTy, AddressSpace);
1235932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1236932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
12373e88bf7dfe2af63827099a7d5be0173cf4c74f4dStephen Hines    case bitc::TYPE_CODE_FUNCTION_OLD: {
1238932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // FIXME: attrid is dead, remove it in LLVM 3.0
1239932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // FUNCTION: [vararg, attrid, retty, paramty x N]
1240932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3)
12411bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1242932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<Type*> ArgTys;
1243932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 3, e = Record.size(); i != e; ++i) {
1244932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Type *Elt = getTypeByIDOrNull(Record[i]))
1245932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          ArgTys.push_back(Elt);
1246932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
1247932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
1248932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1249932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ArgTys.size()+3 != Record.size())
1250932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;  // Something was null.
1251932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByIDOrNull(Record[2])))
1252932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = FunctionType::get(ResultTy, ArgTys, Record[0]);
1253932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1254932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1255932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
1256932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 2)
12571bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1258932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByIDOrNull(Record[1])))
1259932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = ArrayType::get(ResultTy, Record[0]);
1260932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1261932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TYPE_CODE_VECTOR:    // VECTOR: [numelts, eltty]
1262932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 2)
12631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid TYPE table");
1264932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if ((ResultTy = getTypeByIDOrNull(Record[1])))
1265932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ResultTy = VectorType::get(ResultTy, Record[0]);
1266932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1267932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1268b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1269932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (NextTypeID >= TypeList.size())
12701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid TYPE table");
1271b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1272932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (ResultTy && TypeList[NextTypeID] == 0) {
1273932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ++NumTypesRead;
1274932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ReadAnyTypes = true;
1275b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1276932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeList[NextTypeID] = ResultTy;
1277932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1278b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1279932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ++NextTypeID;
1280932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1281932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1282932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1283932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1284d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseOldTypeSymbolTable() {
1285be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien  if (Stream.EnterSubBlock(TYPE_SYMTAB_BLOCK_ID_OLD_3_0))
12861bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Malformed block");
1287932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1288932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
1289932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1290932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this type table.
1291932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::string TypeName;
1292932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
1293932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
1294932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
1295932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
12961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1297d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
1298932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1299932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1300932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
1301932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // No known subblocks, always skip them.
1302932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadSubBlockID();
1303932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.SkipBlock())
13041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1305932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1306932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1307932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1308932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
1309932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
1310932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1311932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1312932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1313932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
1314932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
1315d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Code, Record)) {
1316932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: unknown type.
1317932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1318932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::TST_CODE_ENTRY:    // TST_ENTRY: [typeid, namechar x N]
1319932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 1, TypeName))
13201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1321932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned TypeID = Record[0];
1322932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (TypeID >= TypeList.size())
13231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1324932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1325932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Only apply the type name to a struct type with no name.
1326932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (StructType *STy = dyn_cast<StructType>(TypeList[TypeID]))
1327bc9eb8fa13ee44d7bb46285e4c30da1236aefddfLogan Chien        if (!STy->isLiteral() && !STy->hasName())
1328932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          STy->setName(TypeName);
1329932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TypeName.clear();
1330932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1331932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1332932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1333932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1334932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1335d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseValueSymbolTable() {
1336932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::VALUE_SYMTAB_BLOCK_ID))
13371bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
1338932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1339932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
1340932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1341932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this value table.
1342932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallString<128> ValueName;
1343932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
1344932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
1345932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
1346932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
13471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1348d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
1349932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1350932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
1351932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // No known subblocks, always skip them.
1352932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadSubBlockID();
1353932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.SkipBlock())
13541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1355932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1356932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1357932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1358932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
1359932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
1360932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1361932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1362932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1363932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
1364932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
1365d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Code, Record)) {
1366932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: unknown type.
1367932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1368932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::VST_CODE_ENTRY: {  // VST_ENTRY: [valueid, namechar x N]
1369932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 1, ValueName))
13701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1371932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned ValueID = Record[0];
1372932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ValueID >= ValueList.size())
13731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1374932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *V = ValueList[ValueID];
1375932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1376932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V->setName(StringRef(ValueName.data(), ValueName.size()));
1377932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueName.clear();
1378932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1379932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1380932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::VST_CODE_BBENTRY: {
1381932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 1, ValueName))
13821bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1383932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BasicBlock *BB = getBasicBlock(Record[0]);
1384579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!BB)
13851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1386932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1387932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BB->setName(StringRef(ValueName.data(), ValueName.size()));
1388932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueName.clear();
1389932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1390932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1391932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1392932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1393932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1394932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1395d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseMetadata() {
1396932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NextMDValueNo = MDValueList.size();
1397932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1398932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
13991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
1400932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1401932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
1402932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1403932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records.
1404932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
1405932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
1406932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
1407932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
14081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1409d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
1410932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1411932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1412932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
1413932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // No known subblocks, always skip them.
1414932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadSubBlockID();
1415932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.SkipBlock())
14161bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
1417932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1418932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1419932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1420932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
1421932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
1422932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
1423932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1424932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1425932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    bool IsFunctionLocal = false;
1426932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
1427932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
1428d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    Code = Stream.readRecord(Code, Record);
1429932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    switch (Code) {
1430932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: ignore.
1431932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1432932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::METADATA_NAME: {
1433932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Read named of the named metadata.
1434932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned NameLength = Record.size();
1435932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallString<8> Name;
1436932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Name.resize(NameLength);
1437932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != NameLength; ++i)
1438932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Name[i] = Record[i];
1439932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Record.clear();
1440932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Code = Stream.ReadCode();
1441932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1442932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // METADATA_NAME is always followed by METADATA_NAMED_NODE.
1443d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      unsigned NextBitCode = Stream.readRecord(Code, Record);
1444932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (NextBitCode == METADATA_NAMED_NODE_2_7) {
1445932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        LLVM2_7MetadataDetected = true;
1446932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (NextBitCode != bitc::METADATA_NAMED_NODE) {
1447932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        assert(!"Invalid Named Metadata record.");  (void)NextBitCode;
1448932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1449932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1450932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Read named metadata elements.
1451932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Size = Record.size();
1452932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NamedMDNode *NMD = TheModule->getOrInsertNamedMetadata(Name);
1453932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != Size; ++i) {
1454579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        MDNode *MD = dyn_cast_or_null<MDNode>(MDValueList.getValueFwdRef(Record[i]));
1455579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if (!MD)
14561bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
1457932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NMD->addOperand(MD);
1458932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1459932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1460932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (LLVM2_7MetadataDetected) {
1461932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        MDValueList.AssignValue(0, NextMDValueNo++);
1462932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1463932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1464932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1465932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case METADATA_FN_NODE_2_7:
14661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case bitc::METADATA_OLD_FN_NODE:
1467932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      IsFunctionLocal = true;
1468932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // fall-through
1469932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case METADATA_NODE_2_7:
14701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case bitc::METADATA_OLD_NODE: {
1471932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Code == METADATA_FN_NODE_2_7 ||
1472932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Code == METADATA_NODE_2_7) {
1473932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        LLVM2_7MetadataDetected = true;
1474932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1475932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1476932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() % 2 == 1)
14771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1478932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1479932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Size = Record.size();
14801bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      SmallVector<Metadata *, 8> Elts;
1481932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != Size; i += 2) {
1482932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Type *Ty = getTypeByID(Record[i]);
1483d724d097437f40a5689464429f948ec41e4a2415Stephen Hines        if (!Ty)
14841bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
1485932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Ty->isMetadataTy())
1486932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
14871bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        else if (!Ty->isVoidTy()) {
14881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          auto *MD =
14891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines              ValueAsMetadata::get(ValueList.getValueFwdRef(Record[i + 1], Ty));
14901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          assert(isa<ConstantAsMetadata>(MD) &&
14911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                 "Expected non-function-local metadata");
14921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          Elts.push_back(MD);
14931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        } else
1494900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes          Elts.push_back(nullptr);
1495932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
14961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++);
1497932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1498932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1499932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::METADATA_STRING: {
15001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      std::string String(Record.begin(), Record.end());
15011bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      llvm::UpgradeMDStringConstant(String);
15021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      Metadata *MD = MDString::get(Context, String);
15031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      MDValueList.AssignValue(MD, NextMDValueNo++);
1504932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1505932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1506932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::METADATA_KIND: {
15071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      if (Record.size() < 2)
15081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
15091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
1510932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Kind = Record[0];
15111bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      SmallString<8> Name(Record.begin()+1, Record.end());
1512b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1513932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned NewKind = TheModule->getMDKindID(Name.str());
1514932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
15151bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Conflicting METADATA_KIND records");
1516932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1517932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1518932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1519932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1520932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1521932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1522d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// decodeSignRotatedValue - Decode a signed value stored with the sign bit in
1523932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// the LSB for dense VBR encoding.
1524d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hinesuint64_t BitcodeReader::decodeSignRotatedValue(uint64_t V) {
1525932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if ((V & 1) == 0)
1526932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return V >> 1;
1527932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (V != 1)
1528932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return -(V >> 1);
1529932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // There is no such thing as -0 with integers.  "-0" really means MININT.
1530932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return 1ULL << 63;
1531932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1532932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1533a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines// FIXME: Delete this in LLVM 4.0 and just assert that the aliasee is a
1534a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines// GlobalObject.
1535a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hinesstatic GlobalObject &
1536a630078b32eb37a8de91ae09e26babf235d4fc9fStephen HinesgetGlobalObjectInExpr(const DenseMap<GlobalAlias *, Constant *> &Map,
1537a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines                      Constant &C) {
1538a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  auto *GO = dyn_cast<GlobalObject>(&C);
1539a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  if (GO)
1540a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines    return *GO;
1541a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1542a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  auto *GA = dyn_cast<GlobalAlias>(&C);
1543a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  if (GA)
1544a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines    return getGlobalObjectInExpr(Map, *Map.find(GA)->second);
1545a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1546a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  auto &CE = cast<ConstantExpr>(C);
1547a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  assert(CE.getOpcode() == Instruction::BitCast ||
1548a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines         CE.getOpcode() == Instruction::GetElementPtr ||
1549a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines         CE.getOpcode() == Instruction::AddrSpaceCast);
1550a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  if (CE.getOpcode() == Instruction::GetElementPtr)
1551a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines    assert(cast<GEPOperator>(CE).hasAllZeroIndices());
1552a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  return getGlobalObjectInExpr(Map, *CE.getOperand(0));
1553a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines}
1554a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1555932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ResolveGlobalAndAliasInits - Resolve all of the initializers for global
1556932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// values and aliases that we can.
1557d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ResolveGlobalAndAliasInits() {
1558932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInitWorklist;
1559932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<std::pair<GlobalAlias*, unsigned> > AliasInitWorklist;
1560932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1561932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  GlobalInitWorklist.swap(GlobalInits);
1562932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  AliasInitWorklist.swap(AliasInits);
1563932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1564932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (!GlobalInitWorklist.empty()) {
1565932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned ValID = GlobalInitWorklist.back().second;
1566932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (ValID >= ValueList.size()) {
1567932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Not ready to resolve this yet, it requires something later in the file.
1568932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GlobalInits.push_back(GlobalInitWorklist.back());
1569932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    } else {
1570579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1571932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        GlobalInitWorklist.back().first->setInitializer(C);
1572932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
15731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Expected a constant");
1574932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1575932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    GlobalInitWorklist.pop_back();
1576932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1577932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1578a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  // FIXME: Delete this in LLVM 4.0
1579a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  // Older versions of llvm could write an alias pointing to another. We cannot
1580a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  // construct those aliases, so we first collect an alias to aliasee expression
1581a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  // and then compute the actual aliasee.
1582a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  DenseMap<GlobalAlias *, Constant *> AliasInit;
1583a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1584932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (!AliasInitWorklist.empty()) {
1585932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned ValID = AliasInitWorklist.back().second;
1586932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (ValID >= ValueList.size()) {
1587932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      AliasInits.push_back(AliasInitWorklist.back());
1588932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    } else {
1589579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Constant *C = dyn_cast_or_null<Constant>(ValueList[ValID]))
1590a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines        AliasInit.insert(std::make_pair(AliasInitWorklist.back().first, C));
1591932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
15921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Expected a constant");
1593932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1594932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    AliasInitWorklist.pop_back();
1595932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1596a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1597a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  for (auto &Pair : AliasInit) {
1598a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines    auto &GO = getGlobalObjectInExpr(AliasInit, *Pair.second);
1599a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines    Pair.first->setAliasee(&GO);
1600a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines  }
1601a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines
1602d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
1603932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1604932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1605d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hinesstatic APInt ReadWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) {
1606d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  SmallVector<uint64_t, 8> Words(Vals.size());
1607d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  std::transform(Vals.begin(), Vals.end(), Words.begin(),
1608d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                 BitcodeReader::decodeSignRotatedValue);
1609d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1610d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  return APInt(TypeBits, Words);
1611d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
1612d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1613d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseConstants() {
1614932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::CONSTANTS_BLOCK_ID))
16151bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
1616932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1617932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
1618932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1619932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this value table.
1620932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Type *CurTy = Type::getInt32Ty(Context);
1621932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NextCstNo = ValueList.size();
1622932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
1623d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
1624d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1625d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
1626d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
1627d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
16281bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
1629d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
1630d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      if (NextCstNo != ValueList.size())
16311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid constant reference");
1632d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1633d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // Once all the constants have been read, go through and resolve forward
1634d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // references.
1635d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      ValueList.ResolveConstantForwardRefs();
1636d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
1637d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
1638d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
1639932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1640932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1641932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1642932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
1643932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
1644579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    Value *V = nullptr;
1645d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    unsigned BitCode = Stream.readRecord(Entry.ID, Record);
1646932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    switch (BitCode) {
1647932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: unknown constant
1648932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_UNDEF:     // UNDEF
1649932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = UndefValue::get(CurTy);
1650932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1651932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_SETTYPE:   // SETTYPE: [typeid]
1652932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty())
16531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1654932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record[0] >= TypeList.size())
16551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1656932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      CurTy = TypeList[Record[0]];
1657932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;  // Skip the ValueList manipulation.
1658932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_NULL:      // NULL
1659932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = Constant::getNullValue(CurTy);
1660932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1661932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_INTEGER:   // INTEGER: [intval]
1662932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!CurTy->isIntegerTy() || Record.empty())
16631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1664d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      V = ConstantInt::get(CurTy, decodeSignRotatedValue(Record[0]));
1665932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1666932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
1667932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!CurTy->isIntegerTy() || Record.empty())
16681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1669932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1670d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      APInt VInt = ReadWideAPInt(Record,
1671d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                 cast<IntegerType>(CurTy)->getBitWidth());
1672d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      V = ConstantInt::get(Context, VInt);
1673d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1674932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1675932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1676932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_FLOAT: {    // FLOAT: [fpval]
1677932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty())
16781bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1679d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      if (CurTy->isHalfTy())
1680d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf,
1681d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(16, (uint16_t)Record[0])));
1682d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      else if (CurTy->isFloatTy())
1683d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle,
1684d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(32, (uint32_t)Record[0])));
1685932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else if (CurTy->isDoubleTy())
1686d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::IEEEdouble,
1687d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(64, Record[0])));
1688932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else if (CurTy->isX86_FP80Ty()) {
1689932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // Bits are not stored the same way as a normal i80 APInt, compensate.
1690932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        uint64_t Rearrange[2];
1691932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Rearrange[0] = (Record[1] & 0xffffLL) | (Record[0] << 16);
1692932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Rearrange[1] = Record[0] >> 48;
1693d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::x87DoubleExtended,
1694d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(80, Rearrange)));
1695932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (CurTy->isFP128Ty())
1696d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::IEEEquad,
1697d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(128, Record)));
1698932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else if (CurTy->isPPC_FP128Ty())
1699d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        V = ConstantFP::get(Context, APFloat(APFloat::PPCDoubleDouble,
1700d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                             APInt(128, Record)));
1701932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
1702932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = UndefValue::get(CurTy);
1703932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1704932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1705932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1706932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_AGGREGATE: {// AGGREGATE: [n x value number]
1707932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty())
17081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1709932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1710932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Size = Record.size();
1711b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      SmallVector<Constant*, 16> Elts;
1712932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1713932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (StructType *STy = dyn_cast<StructType>(CurTy)) {
1714932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        for (unsigned i = 0; i != Size; ++i)
1715932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Elts.push_back(ValueList.getConstantFwdRef(Record[i],
1716932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                     STy->getElementType(i)));
1717932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantStruct::get(STy, Elts);
1718932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (ArrayType *ATy = dyn_cast<ArrayType>(CurTy)) {
1719932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Type *EltTy = ATy->getElementType();
1720932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        for (unsigned i = 0; i != Size; ++i)
1721932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1722932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantArray::get(ATy, Elts);
1723932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) {
1724932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Type *EltTy = VTy->getElementType();
1725932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        for (unsigned i = 0; i != Size; ++i)
1726932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Elts.push_back(ValueList.getConstantFwdRef(Record[i], EltTy));
1727932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantVector::get(Elts);
1728932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
1729932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = UndefValue::get(CurTy);
1730932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1731932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1732932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1733932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_STRING: { // STRING: [values]
1734932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty())
17351bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1736932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1737932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ArrayType *ATy = cast<ArrayType>(CurTy);
1738932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *EltTy = ATy->getElementType();
1739932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1740932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Size = Record.size();
1741932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<Constant*> Elts;
1742932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != Size; ++i)
1743932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Elts.push_back(ConstantInt::get(EltTy, Record[i]));
1744932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantArray::get(ATy, Elts);
1745932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1746932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1747932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CSTRING: { // CSTRING: [values]
1748932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty())
17491bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1750932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1751932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ArrayType *ATy = cast<ArrayType>(CurTy);
1752932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *EltTy = ATy->getElementType();
1753932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1754932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Size = Record.size();
1755932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<Constant*> Elts;
1756932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != Size; ++i)
1757932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Elts.push_back(ConstantInt::get(EltTy, Record[i]));
1758932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Elts.push_back(Constant::getNullValue(EltTy));
1759932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantArray::get(ATy, Elts);
1760932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1761932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1762932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_BINOP: {  // CE_BINOP: [opcode, opval, opval]
1763d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 3)
17641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1765932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      int Opc = GetDecodedBinaryOpcode(Record[0], CurTy);
1766932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Opc < 0) {
1767932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = UndefValue::get(CurTy);  // Unknown binop.
1768932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
1769932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Constant *LHS = ValueList.getConstantFwdRef(Record[1], CurTy);
1770932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Constant *RHS = ValueList.getConstantFwdRef(Record[2], CurTy);
1771932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        unsigned Flags = 0;
1772932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Record.size() >= 4) {
1773932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (Opc == Instruction::Add ||
1774932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Opc == Instruction::Sub ||
1775932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Opc == Instruction::Mul ||
1776932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Opc == Instruction::Shl) {
1777932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            if (Record[3] & (1 << bitc::OBO_NO_SIGNED_WRAP))
1778932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Flags |= OverflowingBinaryOperator::NoSignedWrap;
1779932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
1780932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
1781932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          } else if (Opc == Instruction::SDiv ||
1782932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                     Opc == Instruction::UDiv ||
1783932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                     Opc == Instruction::LShr ||
1784932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                     Opc == Instruction::AShr) {
1785932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            if (Record[3] & (1 << bitc::PEO_EXACT))
1786932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines              Flags |= SDivOperator::IsExact;
1787932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          }
1788932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
1789932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantExpr::get(Opc, LHS, RHS, Flags);
1790932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1791932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1792932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1793932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_CAST: {  // CE_CAST: [opcode, opty, opval]
1794d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 3)
17951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1796932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      int Opc = GetDecodedCastOpcode(Record[0]);
1797932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Opc < 0) {
1798932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = UndefValue::get(CurTy);  // Unknown cast.
1799932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
1800932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Type *OpTy = getTypeByID(Record[1]);
1801d724d097437f40a5689464429f948ec41e4a2415Stephen Hines        if (!OpTy)
18021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
1803932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
1804932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantExpr::getCast(Opc, Op, CurTy);
1805932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
1806932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1807932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1808932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_INBOUNDS_GEP:
1809932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_GEP: {  // CE_GEP:        [n x operands]
181098137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar      Type *PointeeType = nullptr;
1811d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() & 1)
18121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1813932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<Constant*, 16> Elts;
1814932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
1815932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Type *ElTy = getTypeByID(Record[i]);
1816d724d097437f40a5689464429f948ec41e4a2415Stephen Hines        if (!ElTy)
18171bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
1818932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Elts.push_back(ValueList.getConstantFwdRef(Record[i+1], ElTy));
1819932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
18201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
182198137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar      V = ConstantExpr::getGetElementPtr(PointeeType, Elts[0], Indices,
18221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                         BitCode ==
18231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                           bitc::CST_CODE_CE_INBOUNDS_GEP);
1824932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1825932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1826932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_SELECT:  // CE_SELECT: [opval#, opval#, opval#]
1827d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 3)
18281bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1829932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0],
1830932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                              Type::getInt1Ty(Context)),
1831932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                  ValueList.getConstantFwdRef(Record[1],CurTy),
1832932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                  ValueList.getConstantFwdRef(Record[2],CurTy));
1833932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1834932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_EXTRACTELT: { // CE_EXTRACTELT: [opty, opval, opval]
1835d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 3)
18361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1837932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      VectorType *OpTy =
1838932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
1839579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!OpTy)
18401bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1841932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1842932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1843932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantExpr::getExtractElement(Op0, Op1);
1844932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1845932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1846932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_INSERTELT: { // CE_INSERTELT: [opval, opval, opval]
1847932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      VectorType *OpTy = dyn_cast<VectorType>(CurTy);
1848579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Record.size() < 3 || !OpTy)
18491bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1850932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1851932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op1 = ValueList.getConstantFwdRef(Record[1],
1852932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                  OpTy->getElementType());
1853932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context));
1854932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantExpr::getInsertElement(Op0, Op1, Op2);
1855932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1856932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1857932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_SHUFFLEVEC: { // CE_SHUFFLEVEC: [opval, opval, opval]
1858932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      VectorType *OpTy = dyn_cast<VectorType>(CurTy);
1859579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Record.size() < 3 || !OpTy)
18601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1861932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy);
1862932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy);
1863932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
1864932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                 OpTy->getNumElements());
1865932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy);
1866932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
1867932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1868932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1869932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval]
1870932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      VectorType *RTy = dyn_cast<VectorType>(CurTy);
1871932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      VectorType *OpTy =
1872932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast_or_null<VectorType>(getTypeByID(Record[0]));
1873579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Record.size() < 4 || !RTy || !OpTy)
18741bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1875932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1876932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1877932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *ShufTy = VectorType::get(Type::getInt32Ty(Context),
1878932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                 RTy->getNumElements());
1879932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy);
1880932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = ConstantExpr::getShuffleVector(Op0, Op1, Op2);
1881932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1882932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1883932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_CE_CMP: {     // CE_CMP: [opty, opval, opval, pred]
1884d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 4)
18851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1886932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *OpTy = getTypeByID(Record[0]);
1887579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!OpTy)
18881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1889932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy);
1890932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy);
1891932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1892932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (OpTy->isFPOrFPVectorTy())
1893932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantExpr::getFCmp(Record[3], Op0, Op1);
1894932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
1895932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        V = ConstantExpr::getICmp(Record[3], Op0, Op1);
1896932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1897932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
18985cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    case bitc::CST_CODE_INLINEASM:
18995cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    case bitc::CST_CODE_INLINEASM_OLD: {
1900d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 2)
19011bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1902932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string AsmStr, ConstrStr;
1903932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool HasSideEffects = Record[0] & 1;
1904932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool IsAlignStack = Record[0] >> 1;
1905932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned AsmStrSize = Record[1];
1906932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (2+AsmStrSize >= Record.size())
19071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1908932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned ConstStrSize = Record[2+AsmStrSize];
1909932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (3+AsmStrSize+ConstStrSize > Record.size())
19101bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1911932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1912932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != AsmStrSize; ++i)
1913932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        AsmStr += (char)Record[2+i];
1914932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0; i != ConstStrSize; ++i)
1915932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ConstrStr += (char)Record[3+AsmStrSize+i];
1916932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PointerType *PTy = cast<PointerType>(CurTy);
1917932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = InlineAsm::get(cast<FunctionType>(PTy->getElementType()),
1918932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                         AsmStr, ConstrStr, HasSideEffects, IsAlignStack);
1919932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1920932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1921932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::CST_CODE_BLOCKADDRESS:{
1922d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 3)
19231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1924932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *FnTy = getTypeByID(Record[0]);
1925579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!FnTy)
19261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1927932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Function *Fn =
1928932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
1929579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!Fn)
19301bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
1931b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
1932932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
1933932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                  Type::getInt8Ty(Context),
1934932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                            false, GlobalValue::InternalLinkage,
1935932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                  0, "");
1936932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
1937932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      V = FwdRef;
1938932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
1939b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines    }
1940932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
1941932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1942932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ValueList.AssignValue(V, NextCstNo);
1943932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ++NextCstNo;
1944932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
1945932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1946932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (NextCstNo != ValueList.size())
19471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid constant reference");
1948932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1949932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.ReadBlockEnd())
19501bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Expected a constant");
1951932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1952932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Once all the constants have been read, go through and resolve forward
1953932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // references.
1954932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValueList.ResolveConstantForwardRefs();
1955d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
1956932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1957932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
19585cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesstd::error_code BitcodeReader::materializeMetadata() {
19595cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  return std::error_code();
19605cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines}
19615cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
196298137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainarvoid BitcodeReader::setStripDebugInfo() { }
196398137cca7eebca946b869b010fef2821c9bf4971Pirama Arumuga Nainar
1964932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// RememberAndSkipFunctionBody - When we see the block for a function body,
1965932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// remember where it is and then skip it.  This lets us lazily deserialize the
1966932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// functions.
1967d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::RememberAndSkipFunctionBody() {
1968932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Get the function we are talking about.
1969932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (FunctionsWithBodies.empty())
19701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Insufficient function protos");
1971932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1972932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Function *Fn = FunctionsWithBodies.back();
1973932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  FunctionsWithBodies.pop_back();
1974932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1975932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Save the current stream state.
1976932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  uint64_t CurBit = Stream.GetCurrentBitNo();
1977932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  DeferredFunctionInfo[Fn] = CurBit;
1978932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1979932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Skip over the function block for now.
1980932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.SkipBlock())
19811bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
1982d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
1983932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
1984932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
1985d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::GlobalCleanup() {
1986d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // Patch the initializers for globals and aliases up.
1987d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  ResolveGlobalAndAliasInits();
1988d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (!GlobalInits.empty() || !AliasInits.empty())
19891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Malformed global initializer set");
1990d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1991d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // Look for intrinsic functions which need to be upgraded at some point
1992d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
1993d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines       FI != FE; ++FI) {
1994d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    Function *NewFn;
1995d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    if (UpgradeIntrinsicFunction(FI, NewFn))
1996d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
1997d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  }
1998d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
1999d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // Look for global variables which need to be renamed.
2000d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  for (Module::global_iterator
2001d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines         GI = TheModule->global_begin(), GE = TheModule->global_end();
20021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines       GI != GE;) {
20031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    GlobalVariable *GV = GI++;
20041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    UpgradeGlobalVariable(GV);
20051bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  }
20061bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
2007d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // Force deallocation of memory for these vectors to favor the client that
2008d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // want lazy deserialization.
2009d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
2010d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
2011d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
2012d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
2013d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2014d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseModule(bool Resume) {
2015d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (Resume)
2016d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    Stream.JumpToBit(NextUnreadBit);
2017d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  else if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
20181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
2019932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2020932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
2021932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<std::string> SectionTable;
2022932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<std::string> GCTable;
2023932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2024932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this module.
2025932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (!Stream.AtEndOfStream()) {
2026932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
2027932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
2028932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
20291bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
2030932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2031932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Patch the initializers for globals and aliases up.
2032932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ResolveGlobalAndAliasInits();
2033932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!GlobalInits.empty() || !AliasInits.empty())
20341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed global initializer set");
2035932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!FunctionsWithBodies.empty())
20361bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Insufficient function protos");
2037932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2038932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Look for intrinsic functions which need to be upgraded at some point
2039932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
2040932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           FI != FE; ++FI) {
2041932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Function* NewFn;
2042932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (UpgradeIntrinsicFunction(FI, NewFn))
2043932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
2044932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2045932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2046932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Look for global variables which need to be renamed.
2047932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (Module::global_iterator
2048932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines             GI = TheModule->global_begin(), GE = TheModule->global_end();
2049932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           GI != GE; ++GI)
2050932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        UpgradeGlobalVariable(GI);
2051932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2052932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Force deallocation of memory for these vectors to favor the client that
2053932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // want lazy deserialization.
2054932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<std::pair<GlobalVariable*, unsigned> >().swap(GlobalInits);
2055932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<std::pair<GlobalAlias*, unsigned> >().swap(AliasInits);
2056932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::vector<Function*>().swap(FunctionsWithBodies);
2057d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
2058932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2059932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2060932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
2061932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      switch (Stream.ReadSubBlockID()) {
2062932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      default:  // Skip unknown content.
2063932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Stream.SkipBlock())
20641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2065932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2066932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::BLOCKINFO_BLOCK_ID:
2067932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Stream.ReadBlockInfoBlock())
20681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Malformed block");
2069932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2070932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::PARAMATTR_BLOCK_ID:
2071d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseAttributeBlock())
2072d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2073932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2074932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::TYPE_BLOCK_ID_NEW:
2075d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseTypeTable())
2076d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2077932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2078be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien      case TYPE_BLOCK_ID_OLD_3_0:
2079d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseOldTypeTable())
2080d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2081932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2082be81e1078cd32c1c4a2b8f60e16e2c7760c2a353Logan Chien      case TYPE_SYMTAB_BLOCK_ID_OLD_3_0:
2083d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseOldTypeSymbolTable())
2084d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2085932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2086932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::VALUE_SYMTAB_BLOCK_ID:
2087d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseValueSymbolTable())
2088d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2089d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        SeenValueSymbolTable = true;
2090932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2091932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::CONSTANTS_BLOCK_ID:
2092d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseConstants())
2093d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2094d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ResolveGlobalAndAliasInits())
2095d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2096932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2097932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::METADATA_BLOCK_ID:
2098d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseMetadata())
2099d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2100932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2101932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::FUNCTION_BLOCK_ID:
2102932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // If this is the first function body we've seen, reverse the
2103932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // FunctionsWithBodies list.
2104d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        if (!SeenFirstFunctionBody) {
2105932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          std::reverse(FunctionsWithBodies.begin(), FunctionsWithBodies.end());
2106d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines          if (std::error_code EC = GlobalCleanup())
2107d724d097437f40a5689464429f948ec41e4a2415Stephen Hines            return EC;
2108d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines          SeenFirstFunctionBody = true;
2109932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
2110932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2111d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = RememberAndSkipFunctionBody())
2112d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2113d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // For streaming bitcode, suspend parsing when we reach the function
2114d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // bodies. Subsequent materialization calls will resume it when
2115d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // necessary. For streaming, the function bodies must be at the end of
2116d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // the bitcode. If the bitcode file is old, the symbol table will be
2117d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // at the end instead and will not have been seen yet. In this case,
2118d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // just finish the parse now.
2119d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        if (LazyStreamer && SeenValueSymbolTable) {
2120d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines          NextUnreadBit = Stream.GetCurrentBitNo();
2121d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines          return std::error_code();
2122d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        }
2123d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        break;
2124932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2125932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2126932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2127932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2128932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2129932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
2130932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
2131932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2132932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2133932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2134932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
2135d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Code, Record)) {
2136932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default: break;  // Default behavior, ignore unknown content.
2137b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines    case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
2138932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
21391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2140932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Only version #0 is supported so far.
2141932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record[0] != 0)
21421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid value");
2143932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2144b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines    }
2145932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
2146932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2147932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2149932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TheModule->setTargetTriple(S);
2150932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2151932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2152932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_DATALAYOUT: {  // DATALAYOUT: [strchr x N]
2153932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2154932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2156932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TheModule->setDataLayout(S);
2157932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2158932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2159932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_ASM: {  // ASM: [strchr x N]
2160932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2161932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2163932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      TheModule->setModuleInlineAsm(S);
2164932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2165932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2166932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_DEPLIB: {  // DEPLIB: [strchr x N]
2167932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2168932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21691bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2170b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      // ANDROID: Ignore value, since we never used it anyways.
2171b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      // TheModule->addLibrary(S);
2172932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2173932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2174932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
2175932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2176932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21771bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2178932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SectionTable.push_back(S);
2179932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2180932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2181932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_GCNAME: {  // SECTIONNAME: [strchr x N]
2182932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2183932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
21841bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2185932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GCTable.push_back(S);
2186932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2187932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2188932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // GLOBALVAR: [pointer type, isconst, initid,
2189932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    //             linkage, alignment, section, visibility, threadlocal,
2190932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    //             unnamed_addr]
2191932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_GLOBALVAR: {
2192932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 6)
21931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2194932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *Ty = getTypeByID(Record[0]);
2195d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty)
21961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2197932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!Ty->isPointerTy())
21981bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type for value");
2199932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned AddressSpace = cast<PointerType>(Ty)->getAddressSpace();
2200932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Ty = cast<PointerType>(Ty)->getElementType();
2201932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2202932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool isConstant = Record[1];
22031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      uint64_t RawLinkage = Record[3];
22041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      GlobalValue::LinkageTypes Linkage = getDecodedLinkage(RawLinkage);
2205932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Alignment = (1 << Record[4]) >> 1;
2206932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string Section;
2207932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record[5]) {
2208932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Record[5]-1 >= SectionTable.size())
22091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid ID");
2210932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Section = SectionTable[Record[5]-1];
2211932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2212932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
2213932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 6)
2214932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Visibility = GetDecodedVisibility(Record[6]);
22158b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao
22168b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao      GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
2217932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 7)
22188b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao        TLM = GetDecodedThreadLocalMode(Record[7]);
2219932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2220932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool UnnamedAddr = false;
2221932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 8)
2222932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        UnnamedAddr = Record[8];
2223932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2224932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GlobalVariable *NewGV =
22251bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        new GlobalVariable(*TheModule, Ty, isConstant, Linkage, nullptr, "", nullptr,
22268b5be8604307d0dd342235803dd45775830b84c6Shih-wei Liao                           TLM, AddressSpace);
2227932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NewGV->setAlignment(Alignment);
2228932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!Section.empty())
2229932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewGV->setSection(Section);
2230932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NewGV->setVisibility(Visibility);
2231932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NewGV->setUnnamedAddr(UnnamedAddr);
2232932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2233932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueList.push_back(NewGV);
2234932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2235932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Remember which value to use for the global initializer.
2236932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (unsigned InitID = Record[2])
2237932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        GlobalInits.push_back(std::make_pair(NewGV, InitID-1));
2238932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2239932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2240932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // FUNCTION:  [type, callingconv, isproto, linkage, paramattr,
2241932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    //             alignment, section, visibility, gc, unnamed_addr]
2242932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_FUNCTION: {
2243932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 8)
22441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2245932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *Ty = getTypeByID(Record[0]);
2246d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty)
22471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2248932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!Ty->isPointerTy())
22491bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type for value");
2250932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      FunctionType *FTy =
2251932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
2252932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!FTy)
22531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type for value");
2254932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2255932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Function *Func = Function::Create(FTy, GlobalValue::ExternalLinkage,
2256932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                        "", TheModule);
2257932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2258932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Func->setCallingConv(static_cast<CallingConv::ID>(Record[1]));
2259932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool isProto = Record[2];
22601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      uint64_t RawLinkage = Record[3];
22611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      Func->setLinkage(getDecodedLinkage(RawLinkage));
2262932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Func->setAttributes(getAttributes(Record[4]));
2263932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2264932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Func->setAlignment((1 << Record[5]) >> 1);
2265932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record[6]) {
2266932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Record[6]-1 >= SectionTable.size())
22671bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid ID");
2268932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Func->setSection(SectionTable[Record[6]-1]);
2269932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2270932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Func->setVisibility(GetDecodedVisibility(Record[7]));
2271932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 8 && Record[8]) {
2272932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Record[8]-1 > GCTable.size())
22731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid ID");
2274932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Func->setGC(GCTable[Record[8]-1].c_str());
2275932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2276932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      bool UnnamedAddr = false;
2277932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 9)
2278932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        UnnamedAddr = Record[9];
2279932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Func->setUnnamedAddr(UnnamedAddr);
2280932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueList.push_back(Func);
2281932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2282932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // If this is a function with a body, remember the prototype we are
2283932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // creating now, so that we can match up the body with them later.
2284d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      if (!isProto) {
228534edb8ad024934c13741550bc825c9b352453ad8Stephen Hines        Func->setIsMaterializable(true);
2286932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        FunctionsWithBodies.push_back(Func);
22871bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        if (LazyStreamer)
22881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          DeferredFunctionInfo[Func] = 0;
2289d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      }
2290932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2291932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2292932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // ALIAS: [alias type, aliasee val#, linkage]
2293932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // ALIAS: [alias type, aliasee val#, linkage, visibility]
2294932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_ALIAS: {
2295932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3)
22961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2297932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *Ty = getTypeByID(Record[0]);
2298d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty)
22991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2300a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines      auto *PTy = dyn_cast<PointerType>(Ty);
2301a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines      if (!PTy)
23021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid type for value");
2303932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
23041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      auto *NewGA =
2305a630078b32eb37a8de91ae09e26babf235d4fc9fStephen Hines          GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
23061bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                              getDecodedLinkage(Record[2]), "", TheModule);
2307932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Old bitcode files didn't have visibility field.
2308932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() > 3)
2309932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewGA->setVisibility(GetDecodedVisibility(Record[3]));
2310932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueList.push_back(NewGA);
2311932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      AliasInits.push_back(std::make_pair(NewGA, Record[1]));
2312932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2313932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2314932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    /// MODULE_CODE_PURGEVALS: [numvals]
2315932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_PURGEVALS:
2316932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Trim down the value list to the specified size.
2317932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1 || Record[0] > ValueList.size())
23181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2319932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueList.shrinkTo(Record[0]);
2320932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2321932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2322932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
2323932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
2324932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
23251bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Error("Invalid bitcode signature");
2326932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
2327932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2328d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseBitcodeInto(Module *M) {
2329579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  TheModule = nullptr;
2330932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2331d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  if (std::error_code EC = InitStream())
2332d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    return EC;
2333932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2334932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Sniff for the signature.
2335932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.Read(8) != 'B' ||
2336932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(8) != 'C' ||
2337932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0x0 ||
2338932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xC ||
2339932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xE ||
2340932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xD)
23411bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid bitcode signature");
2342932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2343932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // We expect a number of well-defined blocks, though we don't necessarily
2344932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // need to understand them all.
2345d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  while (1) {
2346d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    if (Stream.AtEndOfStream())
2347d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
2348d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2349d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry =
2350d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
2351932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2352d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
2353d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
23541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
2355d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
2356d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
2357932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2358d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock:
2359d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      switch (Entry.ID) {
2360d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      case bitc::BLOCKINFO_BLOCK_ID:
2361d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        if (Stream.ReadBlockInfoBlock())
23621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Malformed block");
2363d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        break;
2364d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      case bitc::MODULE_BLOCK_ID:
2365d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        // Reject multiple MODULE_BLOCK's in a single bitstream.
2366d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        if (TheModule)
23671bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid multiple blocks");
2368d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        TheModule = M;
2369d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseModule(false))
2370d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2371d724d097437f40a5689464429f948ec41e4a2415Stephen Hines        if (LazyStreamer)
2372d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines          return std::error_code();
2373d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        break;
2374d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      default:
2375d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        if (Stream.SkipBlock())
23761bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2377d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        break;
2378d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      }
2379d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      continue;
2380d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
2381d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // There should be no records in the top-level of blocks.
2382d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2383d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The ranlib in Xcode 4 will align archive members by appending newlines
2384d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // to the end of them. If this file size is a multiple of 4 but not 8, we
2385d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // have to read and ignore these final 4 bytes :-(
2386d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      if (Stream.getAbbrevIDWidth() == 2 && Entry.ID == 2 &&
2387932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
2388b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines          Stream.AtEndOfStream())
2389d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        return std::error_code();
2390932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
23911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid record");
2392932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2393932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
2394932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
2395932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2396579361346abc6696c805e3904a18178ebce4e4a3Stephen Hinesllvm::ErrorOr<std::string> BitcodeReader::parseModuleTriple() {
2397932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::MODULE_BLOCK_ID))
23981bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
2399932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2400932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
2401932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2402579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  std::string Triple;
2403932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records for this module.
2404d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  while (1) {
2405d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2406932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2407d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
2408d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
2409d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
24101bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
2411d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
2412579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      return Triple;
2413d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
2414d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
2415d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
2416932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2417932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2418932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
2419d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Entry.ID, Record)) {
2420932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default: break;  // Default behavior, ignore unknown content.
2421932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_VERSION:  // VERSION: [version#]
2422932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1)
24231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2424932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Only version #0 is supported so far.
2425932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record[0] != 0)
24261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2427932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2428932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
2429932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      std::string S;
2430932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConvertToString(Record, 0, S))
24311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2432932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Triple = S;
2433932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2434932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2435932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2436932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
2437932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
2438932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
24391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Error("Invalid bitcode signature");
2440932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
2441932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2442579361346abc6696c805e3904a18178ebce4e4a3Stephen Hinesllvm::ErrorOr<std::string> BitcodeReader::parseTriple() {
2443d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  if (std::error_code EC = InitStream())
2444d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    return EC;
2445932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2446932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Sniff for the signature.
2447932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.Read(8) != 'B' ||
2448932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(8) != 'C' ||
2449932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0x0 ||
2450932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xC ||
2451932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xE ||
2452932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.Read(4) != 0xD)
24531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid bitcode signature");
2454932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2455932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // We expect a number of well-defined blocks, though we don't necessarily
2456932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // need to understand them all.
2457d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  while (1) {
2458d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advance();
2459932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2460d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
2461d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
24621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
2463d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
2464d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
2465932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2466d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock:
2467d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      if (Entry.ID == bitc::MODULE_BLOCK_ID)
2468579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        return parseModuleTriple();
2469932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2470d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // Ignore other sub-blocks.
2471d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Stream.SkipBlock())
24721bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
2473d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      continue;
2474d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2475d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
2476d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      Stream.skipRecord(Entry.ID);
2477d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      continue;
2478932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2479932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
2480932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
2481932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2482932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ParseMetadataAttachment - Parse metadata attachments.
2483d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseMetadataAttachment() {
2484932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
24851bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
2486932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2487932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
2488d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  while (1) {
2489d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
2490d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2491d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
2492d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
2493d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
24941bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
2495d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
2496d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
2497d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
2498d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
2499932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2500932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2501d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
2502932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a metadata attachment record.
2503932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
2504d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Entry.ID, Record)) {
2505932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: ignore.
2506932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2507932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case METADATA_ATTACHMENT_2_7:
2508932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      LLVM2_7MetadataDetected = true;
2509932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::METADATA_ATTACHMENT: {
2510932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned RecordLength = Record.size();
2511932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.empty() || (RecordLength - 1) % 2 == 1)
25121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2513932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Instruction *Inst = InstructionList[Record[0]];
2514932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 1; i != RecordLength; i = i+2) {
2515932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        unsigned Kind = Record[i];
2516932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        DenseMap<unsigned, unsigned>::iterator I =
2517932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          MDKindMap.find(Kind);
2518932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (I == MDKindMap.end())
25191bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid ID");
25201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        Metadata *Node = MDValueList.getValueFwdRef(Record[i + 1]);
2521932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Inst->setMetadata(I->second, cast<MDNode>(Node));
2522932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2523932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2524932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2525932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2526932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
2527932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
2528932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2529932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ParseFunctionBody - Lazily parse the specified function body block.
2530d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseFunctionBody(Function *F) {
2531932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::FUNCTION_BLOCK_ID))
25321bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
2533932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2534932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  InstructionList.clear();
2535932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned ModuleValueListSize = ValueList.size();
2536932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned ModuleMDValueListSize = MDValueList.size();
2537932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2538932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Add all the function arguments to the value table.
2539932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
2540932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ValueList.push_back(I);
2541932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2542932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NextValueNo = ValueList.size();
2543579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  BasicBlock *CurBB = nullptr;
2544932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned CurBBNo = 0;
2545932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2546932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  DebugLoc LastLoc;
2547b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2548932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records.
2549932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
2550932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
2551932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    unsigned Code = Stream.ReadCode();
2552932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::END_BLOCK) {
2553932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Stream.ReadBlockEnd())
25541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Malformed block");
2555932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2556932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2557932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2558932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::ENTER_SUBBLOCK) {
2559932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      switch (Stream.ReadSubBlockID()) {
2560932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      default:  // Skip unknown content.
2561932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Stream.SkipBlock())
25621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2563932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2564932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::CONSTANTS_BLOCK_ID:
2565d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseConstants())
2566d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2567932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NextValueNo = ValueList.size();
2568932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2569932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::VALUE_SYMTAB_BLOCK_ID:
2570d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseValueSymbolTable())
2571d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2572932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2573932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::METADATA_ATTACHMENT_ID:
2574d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseMetadataAttachment())
2575d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2576932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2577932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      case bitc::METADATA_BLOCK_ID:
2578d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines        if (std::error_code EC = ParseMetadata())
2579d724d097437f40a5689464429f948ec41e4a2415Stephen Hines          return EC;
2580932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2581932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2582932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2583932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2584932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2585932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (Code == bitc::DEFINE_ABBREV) {
2586932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Stream.ReadAbbrevRecord();
2587932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2588932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2589932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2590932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
2591932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
2592579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    Instruction *I = nullptr;
2593d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    unsigned BitCode = Stream.readRecord(Code, Record);
2594932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    switch (BitCode) {
2595932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default: // Default behavior: reject
25961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid value");
2597932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_DECLAREBLOCKS:     // DECLAREBLOCKS: [nblocks]
2598932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1 || Record[0] == 0)
25991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2600932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Create all the basic blocks for the function.
2601932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      FunctionBBs.resize(Record[0]);
2602932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = FunctionBBs.size(); i != e; ++i)
2603932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        FunctionBBs[i] = BasicBlock::Create(Context, "", F);
2604932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      CurBB = FunctionBBs[0];
2605932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2606b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2607932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
2608932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // This record indicates that the last instruction is at the same
2609932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // location as the previous instruction with a location.
2610579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      I = nullptr;
2611b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2612932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Get the last instruction emitted.
2613932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (CurBB && !CurBB->empty())
2614932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = &CurBB->back();
2615932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2616932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines               !FunctionBBs[CurBBNo-1]->empty())
2617932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = &FunctionBBs[CurBBNo-1]->back();
2618b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2619579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!I)
26201bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2621932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I->setDebugLoc(LastLoc);
2622579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      I = nullptr;
2623932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2624b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2625932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_DEBUG_LOC_2_7:
2626932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      LLVM2_7MetadataDetected = true;
2627932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
2628579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      I = nullptr;     // Get the last instruction emitted.
2629932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (CurBB && !CurBB->empty())
2630932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = &CurBB->back();
2631932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
2632932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines               !FunctionBBs[CurBBNo-1]->empty())
2633932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = &FunctionBBs[CurBBNo-1]->back();
2634579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!I || Record.size() < 4)
26351bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2636b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2637932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Line = Record[0], Col = Record[1];
2638932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned ScopeID = Record[2], IAID = Record[3];
2639b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2640579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      MDNode *Scope = nullptr, *IA = nullptr;
2641932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
2642932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (IAID)    IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
2643932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      LastLoc = DebugLoc::get(Line, Col, Scope, IA);
2644932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I->setDebugLoc(LastLoc);
2645579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      I = nullptr;
2646932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      continue;
2647932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2648932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2649932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
2650932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2651932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *LHS, *RHS;
2652932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
2653932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, LHS->getType(), RHS) ||
2654932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+1 > Record.size())
26551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2656932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2657932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      int Opc = GetDecodedBinaryOpcode(Record[OpNum++], LHS->getType());
2658d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Opc == -1)
26591bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2660932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
2661932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2662932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (OpNum < Record.size()) {
2663932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Opc == Instruction::Add ||
2664932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            Opc == Instruction::Sub ||
2665932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            Opc == Instruction::Mul ||
2666932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            Opc == Instruction::Shl) {
2667932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (Record[OpNum] & (1 << bitc::OBO_NO_SIGNED_WRAP))
2668932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            cast<BinaryOperator>(I)->setHasNoSignedWrap(true);
2669932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
2670932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            cast<BinaryOperator>(I)->setHasNoUnsignedWrap(true);
2671932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else if (Opc == Instruction::SDiv ||
2672932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                   Opc == Instruction::UDiv ||
2673932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                   Opc == Instruction::LShr ||
2674932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                   Opc == Instruction::AShr) {
2675932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (Record[OpNum] & (1 << bitc::PEO_EXACT))
2676932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            cast<BinaryOperator>(I)->setIsExact(true);
2677932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
2678932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2679932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2680932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2681932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_CAST: {    // CAST: [opval, opty, destty, castopc]
2682932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2683932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Op;
2684932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
2685932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+2 != Record.size())
26861bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2687932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2688932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *ResTy = getTypeByID(Record[OpNum]);
2689932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
2690579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (Opc == -1 || !ResTy)
26911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2692932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
2693932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2694932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2695932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
26961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD:
26971bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case bitc::FUNC_CODE_INST_GEP_OLD: // GEP: [n x operands]
2698932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
2699932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
27005cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
27015cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      Type *Ty;
27025cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      bool InBounds;
27035cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
27045cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      if (BitCode == bitc::FUNC_CODE_INST_GEP) {
27055cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines        InBounds = Record[OpNum++];
27065cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines        Ty = getTypeByID(Record[OpNum++]);
27075cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      } else {
27085cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines        InBounds = BitCode == bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
27095cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines        Ty = nullptr;
27105cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      }
27115cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2712932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *BasePtr;
2713932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, BasePtr))
27141bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2715932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
27165cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      if (Ty &&
27175cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines          Ty !=
27185cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines              cast<SequentialType>(BasePtr->getType()->getScalarType())
27195cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines                  ->getElementType())
27205cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines        return Error(
27215cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines            "Explicit gep type does not match pointee type of pointer operand");
27225cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2723932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<Value*, 16> GEPIdx;
2724932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      while (OpNum != Record.size()) {
2725932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Value *Op;
2726932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (getValueTypePair(Record, OpNum, NextValueNo, Op))
27271bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2728932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        GEPIdx.push_back(Op);
2729932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2730932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
27315cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      I = GetElementPtrInst::Create(Ty, BasePtr, GEPIdx);
27325cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
2733932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
27345cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines      if (InBounds)
2735932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        cast<GetElementPtrInst>(I)->setIsInBounds(true);
2736932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2737932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2738932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2739932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_EXTRACTVAL: {
2740932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                       // EXTRACTVAL: [opty, opval, n x indices]
2741932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2742932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Agg;
2743932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
27441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2745932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2746932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<unsigned, 4> EXTRACTVALIdx;
2747932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned RecSize = Record.size();
2748932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           OpNum != RecSize; ++OpNum) {
2749932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        uint64_t Index = Record[OpNum];
2750932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if ((unsigned)Index != Index)
27511bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid value");
2752932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        EXTRACTVALIdx.push_back((unsigned)Index);
2753932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2754932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2755932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = ExtractValueInst::Create(Agg, EXTRACTVALIdx);
2756932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2757932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2758932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2759932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2760932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_INSERTVAL: {
2761932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                           // INSERTVAL: [opty, opval, opty, opval, n x indices]
2762932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2763932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Agg;
2764932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Agg))
27651bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2766932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Val;
2767932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Val))
27681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2769932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2770932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<unsigned, 4> INSERTVALIdx;
2771932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned RecSize = Record.size();
2772932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           OpNum != RecSize; ++OpNum) {
2773932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        uint64_t Index = Record[OpNum];
2774932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if ((unsigned)Index != Index)
27751bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid value");
2776932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        INSERTVALIdx.push_back((unsigned)Index);
2777932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2778932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2779932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = InsertValueInst::Create(Agg, Val, INSERTVALIdx);
2780932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2781932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2782932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2783932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2784932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_SELECT: { // SELECT: [opval, ty, opval, opval]
2785932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // obsolete form of select
2786932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // handles select i1 ... in old bitcode
2787932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2788932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *TrueVal, *FalseVal, *Cond;
2789932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2790932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
2791932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, Type::getInt1Ty(Context), Cond))
27921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2793932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2794932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = SelectInst::Create(Cond, TrueVal, FalseVal);
2795932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2796932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2797932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2798932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2799932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_VSELECT: {// VSELECT: [ty,opval,opval,predty,pred]
2800932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // new form of select
2801932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // handles select i1 or select [N x i1]
2802932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2803932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *TrueVal, *FalseVal, *Cond;
2804932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, TrueVal) ||
2805932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, TrueVal->getType(), FalseVal) ||
2806932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValueTypePair(Record, OpNum, NextValueNo, Cond))
28071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2808932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2809932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // select condition can be either i1 or [N x i1]
2810932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (VectorType* vector_type =
2811932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          dyn_cast<VectorType>(Cond->getType())) {
2812932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // expect <n x i1>
2813932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (vector_type->getElementType() != Type::getInt1Ty(Context))
28141bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid type for value");
2815932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
2816932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // expect i1
2817932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Cond->getType() != Type::getInt1Ty(Context))
28181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid type for value");
2819932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2820932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2821932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = SelectInst::Create(Cond, TrueVal, FalseVal);
2822932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2823932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2824932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2825932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2826932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_EXTRACTELT: { // EXTRACTELT: [opty, opval, opval]
2827932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2828932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Vec, *Idx;
2829932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
2830932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
28311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2832932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = ExtractElementInst::Create(Vec, Idx);
2833932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2834932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2835932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2836932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2837932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_INSERTELT: { // INSERTELT: [ty, opval,opval,opval]
2838932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2839932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Vec, *Elt, *Idx;
2840932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Vec) ||
2841932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum,
2842932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                   cast<VectorType>(Vec->getType())->getElementType(), Elt) ||
2843932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, Type::getInt32Ty(Context), Idx))
28441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2845932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = InsertElementInst::Create(Vec, Elt, Idx);
2846932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2847932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2848932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2849932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2850932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_SHUFFLEVEC: {// SHUFFLEVEC: [opval,ty,opval,opval]
2851932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2852932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Vec1, *Vec2, *Mask;
2853932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Vec1) ||
2854932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, Vec1->getType(), Vec2))
28551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2856932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2857932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Mask))
28581bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2859932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new ShuffleVectorInst(Vec1, Vec2, Mask);
2860932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2861932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2862932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2863932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2864932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_CMP:   // CMP: [opty, opval, opval, pred]
2865932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Old form of ICmp/FCmp returning bool
2866932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Existed to differentiate between icmp/fcmp and vicmp/vfcmp which were
2867932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // both legal on vectors but had different behaviour.
2868932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_CMP2: { // CMP2: [opty, opval, opval, pred]
2869932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // FCmp/ICmp returning bool or vector of bool
2870932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2871932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2872932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *LHS, *RHS;
2873932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
2874932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum, LHS->getType(), RHS) ||
2875932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+1 != Record.size())
28761bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2877932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2878932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (LHS->getType()->isFPOrFPVectorTy())
2879932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = new FCmpInst((FCmpInst::Predicate)Record[OpNum], LHS, RHS);
2880932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else
2881932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = new ICmpInst((ICmpInst::Predicate)Record[OpNum], LHS, RHS);
2882932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2883932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2884932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2885932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2886932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_INST_GETRESULT_2_7: {
2887932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() != 2) {
28881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2889932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2890932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
2891932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Op;
2892932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      getValueTypePair(Record, OpNum, NextValueNo, Op);
2893932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Index = Record[1];
2894932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = ExtractValueInst::Create(Op, Index);
2895932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
2896932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2897932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2898932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2899932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_RET: // RET: [opty,opval<optional>]
2900932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      {
2901932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        unsigned Size = Record.size();
2902932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Size == 0) {
2903932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          I = ReturnInst::Create(Context);
2904932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          InstructionList.push_back(I);
2905932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          break;
2906932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
2907932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2908932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        unsigned OpNum = 0;
2909900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes        Value *Op = nullptr;
2910932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (getValueTypePair(Record, OpNum, NextValueNo, Op))
29111bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2912932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (OpNum != Record.size())
29131bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2914932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2915932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = ReturnInst::Create(Context, Op);
2916932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        InstructionList.push_back(I);
2917932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        break;
2918932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2919932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_BR: { // BR: [bb#, bb#, opval] or [bb#]
2920932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() != 1 && Record.size() != 3)
29211bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2922932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BasicBlock *TrueDest = getBasicBlock(Record[0]);
2923579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!TrueDest)
29241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2925932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2926932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() == 1) {
2927932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = BranchInst::Create(TrueDest);
2928932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        InstructionList.push_back(I);
2929932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2930932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      else {
2931932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        BasicBlock *FalseDest = getBasicBlock(Record[1]);
2932932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Value *Cond = getFnValueByID(Record[2], Type::getInt1Ty(Context));
2933579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if (!FalseDest || !Cond)
29341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2935932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I = BranchInst::Create(TrueDest, FalseDest, Cond);
2936932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        InstructionList.push_back(I);
2937932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2938932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2939932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2940932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
2941932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3 || (Record.size() & 1) == 0)
29421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2943932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *OpTy = getTypeByID(Record[0]);
2944932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Cond = getFnValueByID(Record[1], OpTy);
2945932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BasicBlock *Default = getBasicBlock(Record[2]);
2946579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!OpTy || !Cond || !Default)
29471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2948932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned NumCases = (Record.size()-3)/2;
2949932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SwitchInst *SI = SwitchInst::Create(Cond, Default, NumCases);
2950932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(SI);
2951932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = NumCases; i != e; ++i) {
2952932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        ConstantInt *CaseVal =
2953932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          dyn_cast_or_null<ConstantInt>(getFnValueByID(Record[3+i*2], OpTy));
2954932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        BasicBlock *DestBB = getBasicBlock(Record[1+3+i*2]);
2955579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if (!CaseVal || !DestBB) {
2956932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          delete SI;
29571bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2958932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
2959932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        SI->addCase(CaseVal, DestBB);
2960932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2961932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = SI;
2962932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2963932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2964932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_INDIRECTBR: { // INDIRECTBR: [opty, op0, op1, ...]
2965932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 2)
29661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2967932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *OpTy = getTypeByID(Record[0]);
2968932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Address = getFnValueByID(Record[1], OpTy);
2969579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!OpTy || !Address)
29701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2971932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned NumDests = Record.size()-2;
2972932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests);
2973932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(IBI);
2974932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = NumDests; i != e; ++i) {
2975932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
2976932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          IBI->addDestination(DestBB);
2977932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else {
2978932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          delete IBI;
29791bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
2980932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
2981932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
2982932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = IBI;
2983932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
2984932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
2985b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
2986932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_INVOKE: {
2987932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
2988d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (Record.size() < 4)
29891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2990b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      AttributeSet PAL = getAttributes(Record[0]);
2991932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned CCInfo = Record[1];
2992932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BasicBlock *NormalBB = getBasicBlock(Record[2]);
2993932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      BasicBlock *UnwindBB = getBasicBlock(Record[3]);
2994932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
2995932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 4;
2996932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Callee;
2997932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
29981bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
2999932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3000932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PointerType *CalleeTy = dyn_cast<PointerType>(Callee->getType());
3001579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      FunctionType *FTy = !CalleeTy ? nullptr :
3002932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast<FunctionType>(CalleeTy->getElementType());
3003932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3004932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Check that the right number of fixed parameters are here.
3005579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      if (!FTy || !NormalBB || !UnwindBB ||
3006932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Record.size() < OpNum+FTy->getNumParams())
30071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3008932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3009932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<Value*, 16> Ops;
3010932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
3011932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Ops.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
3012579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if (!Ops.back())
30131bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
3014932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3015932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3016932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!FTy->isVarArg()) {
3017932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (Record.size() != OpNum)
30181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
3019932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
3020932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        // Read type/value pairs for varargs params.
3021932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        while (OpNum != Record.size()) {
3022932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Value *Op;
3023932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (getValueTypePair(Record, OpNum, NextValueNo, Op))
30241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines            return Error("Invalid record");
3025932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Ops.push_back(Op);
3026932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
3027932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3028932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3029932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = InvokeInst::Create(Callee, NormalBB, UnwindBB, Ops);
3030932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3031932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<InvokeInst>(I)->setCallingConv(
3032932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        static_cast<CallingConv::ID>(CCInfo));
3033932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<InvokeInst>(I)->setAttributes(PAL);
3034932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3035932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3036c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao    case FUNC_CODE_INST_UNWIND_2_7: { // UNWIND_OLD
3037c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      // 'unwind' instruction has been removed in LLVM 3.1
3038c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      // Replace 'unwind' with 'landingpad' and 'resume'.
3039c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      Type *ExnTy = StructType::get(Type::getInt8PtrTy(Context),
3040900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes                                    Type::getInt32Ty(Context), nullptr);
3041c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      Constant *PersFn =
3042c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao        F->getParent()->
3043c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao        getOrInsertFunction("__gcc_personality_v0",
3044c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao                          FunctionType::get(Type::getInt32Ty(Context), true));
3045c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao
3046c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      LandingPadInst *LP = LandingPadInst::Create(ExnTy, PersFn, 1);
3047c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      LP->setCleanup(true);
3048c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao
3049c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      CurBB->getInstList().push_back(LP);
3050c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao      I = ResumeInst::Create(LP);
3051932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3052932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3053c73b5214fa71ef6e3378fa121bb8b6312d2e6d3bShih-wei Liao    }
3054932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_UNREACHABLE: // UNREACHABLE
3055932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new UnreachableInst(Context);
3056932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3057932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3058932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_PHI: { // PHI: [ty, val0,bb0, ...]
3059932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 1 || ((Record.size()-1)&1))
30601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3061932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *Ty = getTypeByID(Record[0]);
3062d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty)
30631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3064932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3065932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PHINode *PN = PHINode::Create(Ty, (Record.size()-1)/2);
3066932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(PN);
3067932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3068932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = Record.size()-1; i != e; i += 2) {
3069932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Value *V = getFnValueByID(Record[1+i], Ty);
3070932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        BasicBlock *BB = getBasicBlock(Record[2+i]);
3071d724d097437f40a5689464429f948ec41e4a2415Stephen Hines        if (!V || !BB)
30721bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
3073932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        PN->addIncoming(V, BB);
3074932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3075932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = PN;
3076932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3077932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3078932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3079932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_INST_MALLOC_2_7: { // MALLOC: [instty, op, align]
3080932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Autoupgrade malloc instruction to malloc call.
3081932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // FIXME: Remove in LLVM 3.0.
3082932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3) {
30831bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3084932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3085932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PointerType *Ty =
3086932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          dyn_cast_or_null<PointerType>(getTypeByID(Record[0]));
3087932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Size = getFnValueByID(Record[1], Type::getInt32Ty(Context));
3088d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty || !Size)
30891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3090d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!CurBB)
30911bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid instruction with no BB");
3092932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *Int32Ty = IntegerType::getInt32Ty(CurBB->getContext());
3093932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *AllocSize = ConstantExpr::getSizeOf(Ty->getElementType());
3094932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, Int32Ty);
3095932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = CallInst::CreateMalloc(CurBB, Int32Ty, Ty->getElementType(),
3096900c6c1f08f7c572125d7d39abe0f0f9eafbfa14Chris Wailes                                 AllocSize, Size, nullptr);
3097932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3098932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3099932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3100932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_INST_FREE_2_7: { // FREE: [op, opty]
3101932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
3102932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Op;
3103932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3104932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum != Record.size()) {
31051bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3106932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3107d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!CurBB)
31081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid instruction with no BB");
3109932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = CallInst::CreateFree(Op, CurBB);
3110932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3111932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3112932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3113932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3114932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
3115932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // For backward compatibility, tolerate a lack of an opty, and use i32.
3116932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Remove this in LLVM 3.0.
3117932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3 || Record.size() > 4) {
31181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3119932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3120932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
3121932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PointerType *Ty =
3122932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        dyn_cast_or_null<PointerType>(getTypeByID(Record[OpNum++]));
3123932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *OpTy = Record.size() == 4 ? getTypeByID(Record[OpNum++]) :
3124932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                              Type::getInt32Ty(Context);
3125932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Size = getFnValueByID(Record[OpNum++], OpTy);
3126932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned Align = Record[OpNum++];
3127d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      if (!Ty || !Size)
31281bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3129932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new AllocaInst(Ty->getElementType(), Size, (1 << Align) >> 1);
3130932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3131932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3132932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3133932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_LOAD: { // LOAD: [opty, op, align, vol]
3134932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
3135932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Op;
3136932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Op) ||
3137932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+2 != Record.size())
31381bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3139932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3140932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new LoadInst(Op, "", Record[OpNum+1], (1 << Record[OpNum]) >> 1);
3141932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3142932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3143932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3144932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_STORE: { // STORE2:[ptrty, ptr, val, align, vol]
3145932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
3146932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Val, *Ptr;
3147932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Ptr) ||
3148932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum,
3149932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                    cast<PointerType>(Ptr->getType())->getElementType(), Val) ||
3150932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+2 != Record.size())
31511bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3152932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3153932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
3154932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3155932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3156932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3157932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_INST_STORE_2_7: {
3158932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 0;
3159932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Val, *Ptr;
3160932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Val) ||
3161932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          getValue(Record, OpNum,
3162932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                   PointerType::getUnqual(Val->getType()), Ptr)||
3163932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          OpNum+2 != Record.size()) {
31641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3165932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3166932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new StoreInst(Val, Ptr, Record[OpNum+1], (1 << Record[OpNum]) >> 1);
3167932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3168932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3169932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3170932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case FUNC_CODE_INST_CALL_2_7:
3171932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      LLVM2_7MetadataDetected = true;
3172932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_CALL: {
3173932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // CALL: [paramattrs, cc, fnty, fnid, arg0, arg1...]
3174932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3)
31751bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3176932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3177b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      AttributeSet PAL = getAttributes(Record[0]);
3178932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned CCInfo = Record[1];
3179932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3180932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned OpNum = 2;
3181932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Callee;
3182932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (getValueTypePair(Record, OpNum, NextValueNo, Callee))
31831bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3184932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3185932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      PointerType *OpTy = dyn_cast<PointerType>(Callee->getType());
3186579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines      FunctionType *FTy = nullptr;
3187932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (OpTy) FTy = dyn_cast<FunctionType>(OpTy->getElementType());
3188932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!FTy || Record.size() < FTy->getNumParams()+OpNum)
31891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3190932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3191932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      SmallVector<Value*, 16> Args;
3192932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Read the fixed params.
3193932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i, ++OpNum) {
3194932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (FTy->getParamType(i)->isLabelTy())
3195932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Args.push_back(getBasicBlock(Record[OpNum]));
3196932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        else
3197932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Args.push_back(getFnValueByID(Record[OpNum], FTy->getParamType(i)));
3198579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if (!Args.back())
31991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
3200932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3201932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3202932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Read type/value pairs for varargs params.
3203932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!FTy->isVarArg()) {
3204932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (OpNum != Record.size())
32051bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines          return Error("Invalid record");
3206932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
3207932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        while (OpNum != Record.size()) {
3208932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Value *Op;
3209932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          if (getValueTypePair(Record, OpNum, NextValueNo, Op))
32101bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines            return Error("Invalid record");
3211932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          Args.push_back(Op);
3212932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
3213932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3214932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3215932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = CallInst::Create(Callee, Args);
3216932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3217932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<CallInst>(I)->setCallingConv(
3218932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        static_cast<CallingConv::ID>(CCInfo>>1));
3219932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<CallInst>(I)->setTailCall(CCInfo & 1);
3220932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      cast<CallInst>(I)->setAttributes(PAL);
3221932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3222932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3223932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    case bitc::FUNC_CODE_INST_VAARG: { // VAARG: [valistty, valist, instty]
3224932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() < 3)
32251bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3226932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *OpTy = getTypeByID(Record[0]);
3227932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Value *Op = getFnValueByID(Record[1], OpTy);
3228932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Type *ResTy = getTypeByID(Record[2]);
3229932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!OpTy || !Op || !ResTy)
32301bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
3231932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I = new VAArgInst(Op, ResTy);
3232932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      InstructionList.push_back(I);
3233932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
3234932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3235932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3236932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3237932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Add instruction to end of current BB.  If there is no current BB, reject
3238932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // this file.
3239579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    if (!CurBB) {
3240932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      delete I;
32411bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid instruction with no BB");
3242932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3243932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    CurBB->getInstList().push_back(I);
3244932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3245932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // If this was a terminator instruction, move to the next block.
3246932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (isa<TerminatorInst>(I)) {
3247932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ++CurBBNo;
32481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      CurBB = CurBBNo < FunctionBBs.size() ? FunctionBBs[CurBBNo] : nullptr;
3249932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3250932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3251932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Non-void values get registered in the value table for future use.
3252932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (I && !I->getType()->isVoidTy())
3253932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      ValueList.AssignValue(I, NextValueNo++);
3254932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3255932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3256932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Check the function list for unresolved values.
3257932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Argument *A = dyn_cast<Argument>(ValueList.back())) {
3258579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    if (!A->getParent()) {
3259932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // We found at least one unresolved value.  Nuke them all to avoid leaks.
3260932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (unsigned i = ModuleValueListSize, e = ValueList.size(); i != e; ++i){
3261579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines        if ((A = dyn_cast_or_null<Argument>(ValueList[i])) && !A->getParent()) {
3262932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          A->replaceAllUsesWith(UndefValue::get(A->getType()));
3263932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          delete A;
3264932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
3265932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
32661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Never resolved value found in function");
3267932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3268932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3269932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3270932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // FIXME: Check for unresolved forward-declared metadata references
3271932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // and clean up leaks.
3272932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3273932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // See if anything took the address of blocks in this function.  If so,
3274932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // resolve them now.
3275932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  DenseMap<Function*, std::vector<BlockAddrRefTy> >::iterator BAFRI =
3276932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    BlockAddrFwdRefs.find(F);
3277932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (BAFRI != BlockAddrFwdRefs.end()) {
3278932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
3279932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
3280932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      unsigned BlockIdx = RefList[i].first;
3281932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (BlockIdx >= FunctionBBs.size())
32821bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid ID");
3283b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
3284932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      GlobalVariable *FwdRef = RefList[i].second;
3285932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
3286932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      FwdRef->eraseFromParent();
3287932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3288b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
3289932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    BlockAddrFwdRefs.erase(BAFRI);
3290932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3291932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3292932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  unsigned NewMDValueListSize = MDValueList.size();
3293932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Trim the value list down to the size it was before we parsed this function.
3294932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValueList.shrinkTo(ModuleValueListSize);
3295932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  MDValueList.shrinkTo(ModuleMDValueListSize);
3296932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3297932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (LLVM2_7MetadataDetected) {
3298932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    MDValueList.resize(NewMDValueListSize);
3299932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3300932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3301932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<BasicBlock*>().swap(FunctionBBs);
3302d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
3303932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3304932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3305932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
3306932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// GVMaterializer implementation
3307932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
3308932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
33091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesvoid BitcodeReader::releaseBuffer() { Buffer.release(); }
3310932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3311579361346abc6696c805e3904a18178ebce4e4a3Stephen Hinesstd::error_code BitcodeReader::materialize(GlobalValue *GV) {
33125cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  if (std::error_code EC = materializeMetadata())
33135cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines    return EC;
33145cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines
3315932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Function *F = dyn_cast<Function>(GV);
3316932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If it's not a function or is already material, ignore the request.
3317d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  if (!F || !F->isMaterializable())
3318d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines    return std::error_code();
3319932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3320932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
3321932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
3322932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3323932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Move the bit stream to the saved position of the deferred function body.
3324932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Stream.JumpToBit(DFII->second);
3325932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3326d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  if (std::error_code EC = ParseFunctionBody(F))
3327d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    return EC;
332834edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  F->setIsMaterializable(false);
3329932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3330932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Upgrade any old intrinsic calls in the function.
3331932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
3332932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines       E = UpgradedIntrinsics.end(); I != E; ++I) {
3333932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (I->first != I->second) {
33341bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      for (auto UI = I->first->user_begin(), UE = I->first->user_end();
33351bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines           UI != UE;) {
3336932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3337932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          UpgradeIntrinsicCall(CI, I->second);
3338932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3339932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3340932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3341932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3342d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
3343932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3344932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3345932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesbool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
3346932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  const Function *F = dyn_cast<Function>(GV);
3347932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!F || F->isDeclaration())
3348932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return false;
3349932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return DeferredFunctionInfo.count(const_cast<Function*>(F));
3350932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3351932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3352932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReader::Dematerialize(GlobalValue *GV) {
3353932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Function *F = dyn_cast<Function>(GV);
3354932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If this function isn't dematerializable, this is a noop.
3355932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!F || !isDematerializable(F))
3356932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
3357932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3358932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  assert(DeferredFunctionInfo.count(F) && "No info to read function later?");
3359932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3360932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Just forget the function body, we can remat it later.
3361932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  F->deleteBody();
336234edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  F->setIsMaterializable(true);
3363932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3364932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3365d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::MaterializeModule(Module *M) {
3366932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  assert(M == TheModule &&
3367932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines         "Can only Materialize the Module this BitcodeReader is attached to.");
3368932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Iterate over the module, deserializing any functions that are still on
3369932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // disk.
3370932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  for (Module::iterator F = TheModule->begin(), E = TheModule->end();
3371d724d097437f40a5689464429f948ec41e4a2415Stephen Hines       F != E; ++F) {
337234edb8ad024934c13741550bc825c9b352453ad8Stephen Hines    if (std::error_code EC = materialize(F))
337334edb8ad024934c13741550bc825c9b352453ad8Stephen Hines      return EC;
3374d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  }
337534edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  // At this point, if there are any function bodies, the current bit is
337634edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  // pointing to the END_BLOCK record after them. Now make sure the rest
337734edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  // of the bits in the module have been read.
337834edb8ad024934c13741550bc825c9b352453ad8Stephen Hines  if (NextUnreadBit)
337934edb8ad024934c13741550bc825c9b352453ad8Stephen Hines    ParseModule(true);
3380932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3381932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Upgrade any intrinsic calls that slipped through (should not happen!) and
3382932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // delete the old functions to clean up. We can't do this unless the entire
3383932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // module is materialized because there could always be another function body
3384932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // with calls to the old function.
3385932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  for (std::vector<std::pair<Function*, Function*> >::iterator I =
3386932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines       UpgradedIntrinsics.begin(), E = UpgradedIntrinsics.end(); I != E; ++I) {
3387932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    if (I->first != I->second) {
33881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      for (auto UI = I->first->user_begin(), UE = I->first->user_end();
33891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines           UI != UE;) {
3390932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (CallInst* CI = dyn_cast<CallInst>(*UI++))
3391932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          UpgradeIntrinsicCall(CI, I->second);
3392932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
3393932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!I->first->use_empty())
3394932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        I->first->replaceAllUsesWith(I->second);
3395932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      I->first->eraseFromParent();
3396932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
3397932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3398932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
3399932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3400932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Check debug info intrinsics.
3401932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  CheckDebugInfoIntrinsics(TheModule);
3402932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3403d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
3404932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3405932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
34061bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::vector<StructType *> BitcodeReader::getIdentifiedStructTypes() const {
34071bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return IdentifiedStructTypes;
34081bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
34091bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3410d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::InitStream() {
3411d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  if (LazyStreamer)
3412d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    return InitLazyStream();
3413d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  return InitStreamFromBuffer();
3414d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
3415d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3416d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::InitStreamFromBuffer() {
3417d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  const unsigned char *BufPtr = (const unsigned char*)Buffer->getBufferStart();
3418d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  const unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
3419d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3420579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (Buffer->getBufferSize() & 3)
34211bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid bitcode signature");
3422d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3423d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // If we have a wrapper header, parse it and ignore the non-bc file contents.
3424d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // The magic number is 0x0B17C0DE stored in little endian.
3425d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (isBitcodeWrapper(BufPtr, BufEnd))
3426d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    if (SkipBitcodeWrapperHeader(BufPtr, BufEnd, true))
34271bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Invalid bitcode wrapper header");
3428d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3429d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  StreamFile.reset(new BitstreamReader(BufPtr, BufEnd));
3430579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Stream.init(&*StreamFile);
3431d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3432d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
3433d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
3434d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3435d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::InitLazyStream() {
3436d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // Check and strip off the bitcode wrapper; BitstreamReader expects never to
3437d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // see it.
34381bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer);
34391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  StreamingMemoryObject &Bytes = *OwnedBytes;
34401bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
3441579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Stream.init(&*StreamFile);
3442d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3443d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  unsigned char buf[16];
34441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (Bytes.readBytes(buf, 16, 0) != 16)
34451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid bitcode signature");
3446d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3447d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (!isBitcode(buf, buf + 16))
34481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid bitcode signature");
3449d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
3450d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (isBitcodeWrapper(buf, buf + 4)) {
3451d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    const unsigned char *bitcodeStart = buf;
3452d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    const unsigned char *bitcodeEnd = buf + 16;
3453d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    SkipBitcodeWrapperHeader(bitcodeStart, bitcodeEnd, false);
34541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    Bytes.dropLeadingBytes(bitcodeStart - buf);
34551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    Bytes.setKnownObjectSize(bitcodeEnd - bitcodeStart);
3456d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  }
3457d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines  return std::error_code();
3458d724d097437f40a5689464429f948ec41e4a2415Stephen Hines}
3459d724d097437f40a5689464429f948ec41e4a2415Stephen Hines
3460d724d097437f40a5689464429f948ec41e4a2415Stephen Hinesnamespace {
34618f5c2a1c1392a6d6bdb5700f73eeadd7b01ea0b7Stephen Hinesclass BitcodeErrorCategoryType : public std::error_category {
34628f5c2a1c1392a6d6bdb5700f73eeadd7b01ea0b7Stephen Hines  const char *name() const LLVM_NOEXCEPT override {
3463d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    return "llvm.bitcode";
3464d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  }
3465c2074caf075818abb6d3689ad924ca09f4a5ba1fTim Murray  std::string message(int IE) const override {
34661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    BitcodeError E = static_cast<BitcodeError>(IE);
3467d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    switch (E) {
34681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case BitcodeError::InvalidBitcodeSignature:
3469d724d097437f40a5689464429f948ec41e4a2415Stephen Hines      return "Invalid bitcode signature";
34701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    case BitcodeError::CorruptedBitcode:
34711bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return "Corrupted bitcode";
3472d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    }
3473d724d097437f40a5689464429f948ec41e4a2415Stephen Hines    llvm_unreachable("Unknown error type!");
3474d724d097437f40a5689464429f948ec41e4a2415Stephen Hines  }
3475d724d097437f40a5689464429f948ec41e4a2415Stephen Hines};
3476d724d097437f40a5689464429f948ec41e4a2415Stephen Hines}
3477d724d097437f40a5689464429f948ec41e4a2415Stephen Hines
34781bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic ManagedStatic<BitcodeErrorCategoryType> ErrorCategory;
34791bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
34805cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hinesconst std::error_category &BitcodeReader::BitcodeErrorCategory() {
34811bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return *ErrorCategory;
3482d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
3483932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3484932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
3485932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// External interface
3486932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
3487932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3488932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
3489932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines///
34901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstatic llvm::ErrorOr<llvm::Module *>
34911bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesgetLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
34921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                         LLVMContext &Context, bool WillMaterializeAll,
34931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                         DiagnosticHandlerFunction DiagnosticHandler) {
3494932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Module *M = new Module(Buffer->getBufferIdentifier(), Context);
34951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  BitcodeReader *R =
34961bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
3497932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  M->setMaterializer(R);
3498932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3499579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  auto cleanupOnError = [&](std::error_code EC) {
3500579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    R->releaseBuffer(); // Never take ownership on error.
3501932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete M;  // Also deletes R.
3502579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    return EC;
3503579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  };
3504579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines
3505579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (std::error_code EC = R->ParseBitcodeInto(M))
3506579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    return cleanupOnError(EC);
3507579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines
3508579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Buffer.release(); // The BitcodeReader owns it now.
3509932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return M;
3510932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3511932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
35121bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesllvm::ErrorOr<Module *>
35131bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesllvm_2_7::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
35141bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                           LLVMContext &Context,
35151bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                           DiagnosticHandlerFunction DiagnosticHandler) {
35161bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
35171bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                  DiagnosticHandler);
35181bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
35191bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
3520932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ParseBitcodeFile - Read the specified bitcode file, returning the module.
3521932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// If an error occurs, return null and fill in *ErrMsg if non-null.
35221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesllvm::ErrorOr<llvm::Module *>
35231bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesllvm_2_7::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
35241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                       DiagnosticHandlerFunction DiagnosticHandler) {
35251bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
35261bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl(
35271bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      std::move(Buf), Context, true, DiagnosticHandler);
3528579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (!ModuleOrErr)
3529579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    return ModuleOrErr;
3530579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  Module *M = ModuleOrErr.get();
3531932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read in the entire module, and destroy the BitcodeReader.
3532579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (std::error_code EC = M->materializeAllPermanently()) {
3533932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete M;
3534579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    return EC;
3535932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
3536932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
3537932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return M;
3538932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3539932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
35401bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesstd::string
35411bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesllvm_2_7::getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
35421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                             DiagnosticHandlerFunction DiagnosticHandler) {
3543579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
35441bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto R = llvm::make_unique<BitcodeReader>(Buf.release(), Context,
35451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                            DiagnosticHandler);
3546579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  ErrorOr<std::string> Triple = R->parseTriple();
3547579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (Triple.getError())
3548579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    return "";
3549579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  return Triple.get();
3550932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
3551