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:
1067e920a716693033edf32a6fedd03798bbfbd85ebChih-Hung Hsieh  explicit 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:
1587e920a716693033edf32a6fedd03798bbfbd85ebChih-Hung Hsieh  explicit 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;
192f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  std::unique_ptr<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
263f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  bool isDematerializable(const GlobalValue *GV) const;
2645cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::error_code materialize(GlobalValue *GV) override;
265f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  std::error_code materializeModule() override;
2665cb1f3949c5bdd43c84647920d147a6b8509e256Stephen Hines  std::vector<StructType *> getIdentifiedStructTypes() const override;
267f229c40f0d2da19985e68955aef1a06ce4121e63Pirama Arumuga Nainar  void dematerialize(GlobalValue *GV);
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
3608a019dd0040bedf5078e4d18e06a244a675b80e8Chih-Hung Hsiehstatic std::error_code Error(const 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
3678a019dd0040bedf5078e4d18e06a244a675b80e8Chih-Hung Hsiehstatic std::error_code Error(const 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
490f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainarstatic GlobalVariable::UnnamedAddr getDecodedUnnamedAddrType(unsigned Val) {
491f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  switch (Val) {
492f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    default: // Map unknown to UnnamedAddr::None.
493f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 0: return GlobalVariable::UnnamedAddr::None;
494f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 1: return GlobalVariable::UnnamedAddr::Global;
495f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar    case 2: return GlobalVariable::UnnamedAddr::Local;
496f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar  }
497f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar}
498f5b49a0ca149b09c8306b86db9f3aca703c4acd5Pirama Arumuga Nainar
499932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstatic int GetDecodedCastOpcode(unsigned Val) {
500932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
501932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: return -1;
502932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_TRUNC   : return Instruction::Trunc;
503932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_ZEXT    : return Instruction::ZExt;
504932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_SEXT    : return Instruction::SExt;
505932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTOUI  : return Instruction::FPToUI;
506932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTOSI  : return Instruction::FPToSI;
507932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_UITOFP  : return Instruction::UIToFP;
508932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_SITOFP  : return Instruction::SIToFP;
509932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPTRUNC : return Instruction::FPTrunc;
510932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_FPEXT   : return Instruction::FPExt;
511932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
512932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
513932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::CAST_BITCAST : return Instruction::BitCast;
514932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
515932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
516932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstatic int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
517932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  switch (Val) {
518932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  default: return -1;
519932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_ADD:
520932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add;
521932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SUB:
522932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub;
523932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_MUL:
524932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul;
525932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_UDIV: return Instruction::UDiv;
526932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SDIV:
527932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv;
528932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_UREM: return Instruction::URem;
529932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SREM:
530932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem;
531932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_SHL:  return Instruction::Shl;
532932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_LSHR: return Instruction::LShr;
533932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_ASHR: return Instruction::AShr;
534932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_AND:  return Instruction::And;
535932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_OR:   return Instruction::Or;
536932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  case bitc::BINOP_XOR:  return Instruction::Xor;
537932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
538932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
539932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
540932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesnamespace llvm {
541932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesnamespace {
542932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  /// @brief A class for maintaining the slot number definition
543932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  /// as a placeholder for the actual definition for forward constants defs.
544932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  class ConstantPlaceHolder : public ConstantExpr {
5451bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    void operator=(const ConstantPlaceHolder &) = delete;
546932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  public:
547932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // allocate space for exactly one operand
548932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    void *operator new(size_t s) {
549932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      return User::operator new(s, 1);
550932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
551932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    explicit ConstantPlaceHolder(Type *Ty, LLVMContext& Context)
552932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) {
553932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Op<0>() = UndefValue::get(Type::getInt32Ty(Context));
554932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
555932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
556932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    /// @brief Methods to support type inquiry through isa, cast, and dyn_cast.
557932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    static bool classof(const Value *V) {
558932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      return isa<ConstantExpr>(V) &&
559932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines             cast<ConstantExpr>(V)->getOpcode() == Instruction::UserOp1;
560932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
561932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
562932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
563932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    /// Provide fast operand accessors
5641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
565932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  };
566932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
567932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
568932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines// FIXME: can we inherit this from ConstantExpr?
569932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinestemplate <>
570932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesstruct OperandTraits<ConstantPlaceHolder> :
571932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  public FixedNumOperandTraits<ConstantPlaceHolder, 1> {
572932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines};
5731bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesDEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value)
574932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
575932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
576932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
577932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReaderValueList::AssignValue(Value *V, unsigned Idx) {
578932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx == size()) {
579932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    push_back(V);
580932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
581932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
582932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
583932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
584932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx+1);
585932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
586932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  WeakVH &OldV = ValuePtrs[Idx];
587579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (!OldV) {
588932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV = V;
589932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
590932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
591932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
592932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Handle constants and non-constants (e.g. instrs) differently for
593932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // efficiency.
594932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Constant *PHC = dyn_cast<Constant>(&*OldV)) {
595932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ResolveConstants.push_back(std::make_pair(PHC, Idx));
596932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV = V;
597932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  } else {
598932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // If there was a forward reference to this value, replace it.
599932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Value *PrevVal = OldV;
600932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    OldV->replaceAllUsesWith(V);
601932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete PrevVal;
602932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
603932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
604932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
605932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
606932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesConstant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx,
607932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                    Type *Ty) {
608932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
609932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
610932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
611932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Value *V = ValuePtrs[Idx]) {
612932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    assert(Ty == V->getType() && "Type mismatch in constant table!");
613932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return cast<Constant>(V);
614932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
615932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
616932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
617932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Constant *C = new ConstantPlaceHolder(Ty, Context);
618932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValuePtrs[Idx] = C;
619932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return C;
620932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
621932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
622932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesValue *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty) {
623932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
624932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
625932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
626932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Value *V = ValuePtrs[Idx]) {
627579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines    assert((!Ty || Ty == V->getType()) && "Type mismatch in value table!");
628932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return V;
629932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
630932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
631932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // No type specified, must be invalid reference.
632579361346abc6696c805e3904a18178ebce4e4a3Stephen Hines  if (!Ty) return nullptr;
633932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
634932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
635932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  Value *V = new Argument(Ty);
636932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  ValuePtrs[Idx] = V;
637932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return V;
638932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
639932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
640932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// ResolveConstantForwardRefs - Once all constants are read, this method bulk
641932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// resolves any forward references.  The idea behind this is that we sometimes
642932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// get constants (such as large arrays) which reference *many* forward ref
643932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// constants.  Replacing each of these causes a lot of thrashing when
644932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// building/reuniquing the constant.  Instead of doing this, we look at all the
645932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// uses and rewrite all the place holders at once for any constant that uses
646932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// a placeholder.
647932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hinesvoid BitcodeReaderValueList::ResolveConstantForwardRefs() {
648932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Sort the values by-pointer so that they are efficient to look up with a
649932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // binary search.
650932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  std::sort(ResolveConstants.begin(), ResolveConstants.end());
651932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
652932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<Constant*, 64> NewOps;
653932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
654932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (!ResolveConstants.empty()) {
655932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Value *RealVal = operator[](ResolveConstants.back().second);
656932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Constant *Placeholder = ResolveConstants.back().first;
657932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    ResolveConstants.pop_back();
658932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
659932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Loop over all users of the placeholder, updating them to reference the
660932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // new value.  If they reference more than one placeholder, update them all
661932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // at once.
662932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    while (!Placeholder->use_empty()) {
6631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      auto UI = Placeholder->user_begin();
6641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      User *U = *UI;
665932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
666932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // If the using object isn't uniqued, just update the operands.  This
667932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // handles instructions and initializers for global variables.
668932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (!isa<Constant>(U) || isa<GlobalValue>(U)) {
6691bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        UI.getUse().set(RealVal);
670932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        continue;
671932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
672932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
673932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Otherwise, we have a constant that uses the placeholder.  Replace that
674932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // constant with a new constant that has *all* placeholder uses updated.
675932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *UserC = cast<Constant>(U);
676932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      for (User::op_iterator I = UserC->op_begin(), E = UserC->op_end();
677932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines           I != E; ++I) {
678932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        Value *NewOp;
679932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        if (!isa<ConstantPlaceHolder>(*I)) {
680932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Not a placeholder reference.
681932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = *I;
682932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else if (*I == Placeholder) {
683932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Common case is that it just references this one placeholder.
684932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = RealVal;
685932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        } else {
686932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          // Otherwise, look up the placeholder in ResolveConstants.
687932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          ResolveConstantsTy::iterator It =
688932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines            std::lower_bound(ResolveConstants.begin(), ResolveConstants.end(),
689932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                             std::pair<Constant*, unsigned>(cast<Constant>(*I),
690932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines                                                            0));
691932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          assert(It != ResolveConstants.end() && It->first == *I);
692932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines          NewOp = operator[](It->second);
693932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        }
694932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
695932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewOps.push_back(cast<Constant>(NewOp));
696932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
697932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
698932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      // Make the new constant.
699932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      Constant *NewC;
700932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (ConstantArray *UserCA = dyn_cast<ConstantArray>(UserC)) {
701932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantArray::get(UserCA->getType(), NewOps);
702932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
703932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantStruct::get(UserCS->getType(), NewOps);
704932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else if (isa<ConstantVector>(UserC)) {
705932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = ConstantVector::get(NewOps);
706932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      } else {
707932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        assert(isa<ConstantExpr>(UserC) && "Must be a ConstantExpr.");
708932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines        NewC = cast<ConstantExpr>(UserC)->getWithOperands(NewOps);
709932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
710932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
711932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      UserC->replaceAllUsesWith(NewC);
712932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      UserC->destroyConstant();
713932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      NewOps.clear();
714932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
715932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
716932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Update all ValueHandles, they should be the only users at this point.
717932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Placeholder->replaceAllUsesWith(RealVal);
718932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    delete Placeholder;
719932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
720932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
721932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7221bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesvoid BitcodeReaderMDValueList::AssignValue(Metadata *MD, unsigned Idx) {
723932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx == size()) {
7241bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    push_back(MD);
725932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
726932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
727932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
728932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
729932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx+1);
730932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7311bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  TrackingMDRef &OldMD = MDValuePtrs[Idx];
7321bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (!OldMD) {
7331bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    OldMD.reset(MD);
734932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return;
735932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  }
736932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
737932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If there was a forward reference to this value, replace it.
7381bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  TempMDTuple PrevMD(cast<MDTuple>(OldMD.get()));
7391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  PrevMD->replaceAllUsesWith(MD);
7401bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  --NumFwdRefs;
741932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
742932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7431bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesMetadata *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
744932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Idx >= size())
745932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    resize(Idx + 1);
746932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7471bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (Metadata *MD = MDValuePtrs[Idx])
7481bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return MD;
749932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
750932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Create and return a placeholder, which will later be RAUW'd.
7511bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  AnyFwdRefs = true;
7521bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  ++NumFwdRefs;
7531bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  Metadata *MD = MDNode::getTemporary(Context, None).release();
7541bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  MDValuePtrs[Idx].reset(MD);
7551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return MD;
7561bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
7571bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7581bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hinesvoid BitcodeReaderMDValueList::tryToResolveCycles() {
7591bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (!AnyFwdRefs)
7601bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    // Nothing to do.
7611bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return;
7621bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7631bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  if (NumFwdRefs)
7641bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    // Still forward references... can't resolve cycles.
7651bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return;
7661bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7671bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  // Resolve any cycles.
7681bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  for (auto &MD : MDValuePtrs) {
7691bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    auto *N = dyn_cast_or_null<MDNode>(MD);
7701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    if (!N)
7711bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      continue;
7721bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7731bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    assert(!N->isTemporary() && "Unexpected forward reference");
7741bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    N->resolveCycles();
7751bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  }
776932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
777932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
778932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesType *BitcodeReader::getTypeByID(unsigned ID) {
779932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // The type table size is always specified correctly.
780932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (ID >= TypeList.size())
78134edb8ad024934c13741550bc825c9b352453ad8Stephen Hines    return nullptr;
782b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
783932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Type *Ty = TypeList[ID])
784932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    return Ty;
785932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
786932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // If we have a forward reference, the only possible case is when it is to a
787932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // named struct.  Just create a placeholder for now.
7881bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return TypeList[ID] = createIdentifiedStructType(Context);
7891bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
7901bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
7911bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesStructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context,
7921bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines                                                      StringRef Name) {
7931bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto *Ret = StructType::create(Context, Name);
7941bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  IdentifiedStructTypes.push_back(Ret);
7951bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Ret;
796932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
797932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
7981bd9f627fa0affb457507e86b0b6684c695fe726Stephen HinesStructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) {
7991bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  auto *Ret = StructType::create(Context);
8001bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  IdentifiedStructTypes.push_back(Ret);
8011bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines  return Ret;
8021bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines}
8031bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
8041bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines
805932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines/// FIXME: Remove in LLVM 3.1, only used by ParseOldTypeTable.
806932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen HinesType *BitcodeReader::getTypeByIDOrNull(unsigned ID) {
807932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (ID >= TypeList.size())
808932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    TypeList.resize(ID+1);
809b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines
810932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  return TypeList[ID];
811932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines}
812932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
813932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
814932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//  Functions for parsing blocks from the bitcode file
815932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines//===----------------------------------------------------------------------===//
816932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
817d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
818d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// \brief This fills an AttrBuilder object with the LLVM attributes that have
819d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// been decoded from the given integer. This function must stay in sync with
820d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines/// 'encodeLLVMAttributesForBitcode'.
821d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hinesstatic void decodeLLVMAttributesForBitcode(AttrBuilder &B,
822d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                                           uint64_t EncodedAttrs) {
823d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // FIXME: Remove in 4.0.
824d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
825d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // The alignment is stored as a 16-bit raw value from bits 31--16.  We shift
826d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  // the bits above 31 down by 11 bits.
827d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
828d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  assert((!Alignment || isPowerOf2_32(Alignment)) &&
829d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines         "Alignment must be a power of two.");
830d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
831d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  if (Alignment)
832d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    B.addAlignmentAttr(Alignment);
833d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  B.addRawValue(((EncodedAttrs & (0xfffffULL << 32)) >> 11) |
834d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines                (EncodedAttrs & 0xffff));
835d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines}
836d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines
837d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hinesstd::error_code BitcodeReader::ParseAttributeBlock() {
838932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (Stream.EnterSubBlock(bitc::PARAMATTR_BLOCK_ID))
8391bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid record");
840932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
841932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  if (!MAttributes.empty())
8421bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines    return Error("Invalid multiple blocks");
843932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
844932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  SmallVector<uint64_t, 64> Record;
845932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
846d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines  SmallVector<AttributeSet, 8> Attrs;
847932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
848932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  // Read all the records.
849932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines  while (1) {
850d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    BitstreamEntry Entry = Stream.advanceSkippingSubblocks();
851932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
852d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Entry.Kind) {
853d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::SubBlock: // Handled for us already.
854d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Error:
8551bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines      return Error("Malformed block");
856d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::EndBlock:
857d0993af5b1337f8186dd9aedea2e138a919e02e9Stephen Hines      return std::error_code();
858d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case BitstreamEntry::Record:
859d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      // The interesting case.
860d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      break;
861932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    }
862932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
863932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    // Read a record.
864932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    Record.clear();
865d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    switch (Stream.readRecord(Entry.ID, Record)) {
866932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines    default:  // Default behavior: ignore.
867932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      break;
868d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines    case bitc::PARAMATTR_CODE_ENTRY_OLD: { // ENTRY: [paramidx0, attr0, ...]
869932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      if (Record.size() & 1)
8701bd9f627fa0affb457507e86b0b6684c695fe726Stephen Hines        return Error("Invalid record");
871932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
872b730e239619a546d93e5926ea92d698ab77ec7f6Stephen Hines      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
873d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        AttrBuilder B;
874d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        decodeLLVMAttributesForBitcode(B, Record[i+1]);
875d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines        Attrs.push_back(AttributeSet::get(Context, Record[i], B));
876932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines      }
877932bc6e35bcef7adff05d890a9dcc7212426fb6aStephen Hines
878d9216ebb9c114242b713cd4dad33b3a83eb86761Stephen Hines      MAttributes.push_back(AttributeSet::get(Context, Attrs));
879