14cc499d6e5ec602309501873449c938af61170b2Stephen Hines//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
24cc499d6e5ec602309501873449c938af61170b2Stephen Hines//
34cc499d6e5ec602309501873449c938af61170b2Stephen Hines//                     The LLVM Compiler Infrastructure
44cc499d6e5ec602309501873449c938af61170b2Stephen Hines//
54cc499d6e5ec602309501873449c938af61170b2Stephen Hines// This file is distributed under the University of Illinois Open Source
64cc499d6e5ec602309501873449c938af61170b2Stephen Hines// License. See LICENSE.TXT for details.
74cc499d6e5ec602309501873449c938af61170b2Stephen Hines//
84cc499d6e5ec602309501873449c938af61170b2Stephen Hines//===----------------------------------------------------------------------===//
94cc499d6e5ec602309501873449c938af61170b2Stephen Hines//
104cc499d6e5ec602309501873449c938af61170b2Stephen Hines// Bitcode writer implementation.
114cc499d6e5ec602309501873449c938af61170b2Stephen Hines//
124cc499d6e5ec602309501873449c938af61170b2Stephen Hines//===----------------------------------------------------------------------===//
134cc499d6e5ec602309501873449c938af61170b2Stephen Hines
144cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "ReaderWriter_2_9.h"
158bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines#include "legacy_bitcode.h"
164cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "ValueEnumerator.h"
174cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "llvm/ADT/Triple.h"
1823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/Bitcode/BitstreamWriter.h"
1923c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/Bitcode/LLVMBitCodes.h"
2023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/Constants.h"
21c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#include "llvm/IR/DebugInfoMetadata.h"
2223c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/DerivedTypes.h"
2323c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/InlineAsm.h"
2423c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/Instructions.h"
2523c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/Module.h"
2623c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/Operator.h"
2723c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/IR/ValueSymbolTable.h"
284cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "llvm/Support/ErrorHandling.h"
294cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "llvm/Support/MathExtras.h"
304cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include "llvm/Support/Program.h"
3123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/Support/raw_ostream.h"
324cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include <cctype>
334cc499d6e5ec602309501873449c938af61170b2Stephen Hines#include <map>
344cc499d6e5ec602309501873449c938af61170b2Stephen Hinesusing namespace llvm;
354cc499d6e5ec602309501873449c938af61170b2Stephen Hines
36a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines// Redefine older bitcode opcodes for use here. Note that these come from
37a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines// LLVM 2.7 (which is what HC shipped with).
38a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define METADATA_NODE_2_7             2
39a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define METADATA_FN_NODE_2_7          3
40a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define METADATA_NAMED_NODE_2_7       5
41a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define METADATA_ATTACHMENT_2_7       7
42a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define FUNC_CODE_INST_CALL_2_7       22
43a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines#define FUNC_CODE_DEBUG_LOC_2_7       32
44a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines
45ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien// Redefine older bitcode opcodes for use here. Note that these come from
46ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien// LLVM 2.7 - 3.0.
47ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien#define TYPE_BLOCK_ID_OLD_3_0 10
48ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien#define TYPE_SYMTAB_BLOCK_ID_OLD_3_0 13
49ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien#define TYPE_CODE_STRUCT_OLD_3_0 10
50ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien
514cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// These are manifest constants used by the bitcode writer. They do not need to
524cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// be kept in sync with the reader, but need to be consistent within this file.
534cc499d6e5ec602309501873449c938af61170b2Stephen Hinesenum {
544cc499d6e5ec602309501873449c938af61170b2Stephen Hines  CurVersion = 0,
554cc499d6e5ec602309501873449c938af61170b2Stephen Hines
564cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // VALUE_SYMTAB_BLOCK abbrev id's.
574cc499d6e5ec602309501873449c938af61170b2Stephen Hines  VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
584cc499d6e5ec602309501873449c938af61170b2Stephen Hines  VST_ENTRY_7_ABBREV,
594cc499d6e5ec602309501873449c938af61170b2Stephen Hines  VST_ENTRY_6_ABBREV,
604cc499d6e5ec602309501873449c938af61170b2Stephen Hines  VST_BBENTRY_6_ABBREV,
614cc499d6e5ec602309501873449c938af61170b2Stephen Hines
624cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // CONSTANTS_BLOCK abbrev id's.
634cc499d6e5ec602309501873449c938af61170b2Stephen Hines  CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
644cc499d6e5ec602309501873449c938af61170b2Stephen Hines  CONSTANTS_INTEGER_ABBREV,
654cc499d6e5ec602309501873449c938af61170b2Stephen Hines  CONSTANTS_CE_CAST_Abbrev,
664cc499d6e5ec602309501873449c938af61170b2Stephen Hines  CONSTANTS_NULL_Abbrev,
674cc499d6e5ec602309501873449c938af61170b2Stephen Hines
684cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // FUNCTION_BLOCK abbrev id's.
694cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
704cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_BINOP_ABBREV,
714cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_BINOP_FLAGS_ABBREV,
724cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_CAST_ABBREV,
734cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_RET_VOID_ABBREV,
744cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_RET_VAL_ABBREV,
754cc499d6e5ec602309501873449c938af61170b2Stephen Hines  FUNCTION_INST_UNREACHABLE_ABBREV
764cc499d6e5ec602309501873449c938af61170b2Stephen Hines};
774cc499d6e5ec602309501873449c938af61170b2Stephen Hines
784cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic unsigned GetEncodedCastOpcode(unsigned Opcode) {
794cc499d6e5ec602309501873449c938af61170b2Stephen Hines  switch (Opcode) {
804cc499d6e5ec602309501873449c938af61170b2Stephen Hines  default: llvm_unreachable("Unknown cast instruction!");
814cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Trunc   : return bitc::CAST_TRUNC;
824cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::ZExt    : return bitc::CAST_ZEXT;
834cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::SExt    : return bitc::CAST_SEXT;
844cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
854cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
864cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::UIToFP  : return bitc::CAST_UITOFP;
874cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::SIToFP  : return bitc::CAST_SITOFP;
884cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
894cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FPExt   : return bitc::CAST_FPEXT;
904cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
914cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
924cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::BitCast : return bitc::CAST_BITCAST;
934cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
944cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
954cc499d6e5ec602309501873449c938af61170b2Stephen Hines
964cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
974cc499d6e5ec602309501873449c938af61170b2Stephen Hines  switch (Opcode) {
984cc499d6e5ec602309501873449c938af61170b2Stephen Hines  default: llvm_unreachable("Unknown binary instruction!");
994cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Add:
1004cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FAdd: return bitc::BINOP_ADD;
1014cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Sub:
1024cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FSub: return bitc::BINOP_SUB;
1034cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Mul:
1044cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FMul: return bitc::BINOP_MUL;
1054cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::UDiv: return bitc::BINOP_UDIV;
1064cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FDiv:
1074cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::SDiv: return bitc::BINOP_SDIV;
1084cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::URem: return bitc::BINOP_UREM;
1094cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::FRem:
1104cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::SRem: return bitc::BINOP_SREM;
1114cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Shl:  return bitc::BINOP_SHL;
1124cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::LShr: return bitc::BINOP_LSHR;
1134cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::AShr: return bitc::BINOP_ASHR;
1144cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::And:  return bitc::BINOP_AND;
1154cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Or:   return bitc::BINOP_OR;
1164cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case Instruction::Xor:  return bitc::BINOP_XOR;
1174cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
1184cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
1194cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1204cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteStringRecord(unsigned Code, StringRef Str,
1214cc499d6e5ec602309501873449c938af61170b2Stephen Hines                              unsigned AbbrevToUse, BitstreamWriter &Stream) {
1224cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<unsigned, 64> Vals;
1234cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1244cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Code: [strchar x N]
1254cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1264cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
1274cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = 0;
1284cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.push_back(Str[i]);
1294cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
1304cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1314cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit the finished record.
1324cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EmitRecord(Code, Vals, AbbrevToUse);
1334cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
1344cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1354cc499d6e5ec602309501873449c938af61170b2Stephen Hines// Emit information about parameter attributes.
1369937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteAttributeTable(const llvm_2_9::ValueEnumerator &VE,
1374cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                BitstreamWriter &Stream) {
13823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines  const std::vector<AttributeSet> &Attrs = VE.getAttributes();
1394cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (Attrs.empty()) return;
1404cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1414cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
1424cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1434cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> Record;
1444cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
14523c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines    const AttributeSet &A = Attrs[i];
1468bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
1478bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines      Record.push_back(A.getSlotIndex(i));
1488bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines      Record.push_back(encodeLLVMAttributesForBitcode(A, A.getSlotIndex(i)));
1498bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    }
1504cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1518bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    // This needs to use the 3.2 entry type
1528bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY_OLD, Record);
1534cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.clear();
1544cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
1554cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1564cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
1574cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
1584cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1599937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteTypeSymbolTable(const llvm_2_9::ValueEnumerator &VE,
1604cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                 BitstreamWriter &Stream) {
1619937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9::ValueEnumerator::TypeList &TypeList = VE.getTypes();
162ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien  Stream.EnterSubblock(TYPE_SYMTAB_BLOCK_ID_OLD_3_0, 3);
1634cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1644cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // 7-bit fixed width VST_CODE_ENTRY strings.
1654cc499d6e5ec602309501873449c938af61170b2Stephen Hines  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1664cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1674cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1684cc499d6e5ec602309501873449c938af61170b2Stephen Hines                            Log2_32_Ceil(VE.getTypes().size()+1)));
1694cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1704cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1714cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
1724cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1734cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<unsigned, 64> NameVals;
1744cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1754cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
1764cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Type *T = TypeList[i];
1774cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1784cc499d6e5ec602309501873449c938af61170b2Stephen Hines    switch (T->getTypeID()) {
1794cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::StructTyID: {
1804cc499d6e5ec602309501873449c938af61170b2Stephen Hines      StructType *ST = cast<StructType>(T);
1819207a2e495c8363606861e4f034504ec5c153dabLogan Chien      if (ST->isLiteral()) {
1824cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // Skip anonymous struct definitions in type symbol table
1834cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // FIXME(srhines)
1844cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
1854cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
1864cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1874cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // TST_ENTRY: [typeid, namechar x N]
1884cc499d6e5ec602309501873449c938af61170b2Stephen Hines      NameVals.push_back(i);
1894cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1904cc499d6e5ec602309501873449c938af61170b2Stephen Hines      const std::string &Str = ST->getName();
1914cc499d6e5ec602309501873449c938af61170b2Stephen Hines      bool is7Bit = true;
1924cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1934cc499d6e5ec602309501873449c938af61170b2Stephen Hines        NameVals.push_back((unsigned char)Str[i]);
1944cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if (Str[i] & 128)
1954cc499d6e5ec602309501873449c938af61170b2Stephen Hines          is7Bit = false;
1964cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
1974cc499d6e5ec602309501873449c938af61170b2Stephen Hines
1984cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Emit the finished record.
1994cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
2004cc499d6e5ec602309501873449c938af61170b2Stephen Hines      NameVals.clear();
2014cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2024cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
2034cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
2044cc499d6e5ec602309501873449c938af61170b2Stephen Hines    default: break;
2054cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
2064cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
2074cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2084cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
2094cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
2104cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2114cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// WriteTypeTable - Write out the type table for a module.
2129937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteTypeTable(const llvm_2_9::ValueEnumerator &VE,
2139937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                           BitstreamWriter &Stream) {
2149937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9::ValueEnumerator::TypeList &TypeList = VE.getTypes();
2154cc499d6e5ec602309501873449c938af61170b2Stephen Hines
216ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien  Stream.EnterSubblock(TYPE_BLOCK_ID_OLD_3_0, 4 /*count from # abbrevs */);
2174cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> TypeVals;
2184cc499d6e5ec602309501873449c938af61170b2Stephen Hines
219c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
220c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
2214cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Abbrev for TYPE_CODE_POINTER.
2224cc499d6e5ec602309501873449c938af61170b2Stephen Hines  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2234cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
224c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2254cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
2264cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
2274cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2284cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Abbrev for TYPE_CODE_FUNCTION.
2294cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv = new BitCodeAbbrev();
2308ec3bc862ac51f49e06bc465a6eb51871ce9d5daStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION_OLD));
2314cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
2324cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(0));  // FIXME: DEAD value, remove in LLVM 3.0
2334cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
234c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2354cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
2364cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2374cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2384cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Abbrev for TYPE_CODE_STRUCT.
2394cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv = new BitCodeAbbrev();
240ac4e18584b8768b3f68535fa5f16232e03974323Logan Chien  Abbv->Add(BitCodeAbbrevOp(TYPE_CODE_STRUCT_OLD_3_0));
2414cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
2424cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
243c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2444cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
2454cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2464cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Abbrev for TYPE_CODE_ARRAY.
2474cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv = new BitCodeAbbrev();
2484cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
2494cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
250c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
251c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
2524cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
2534cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2544cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit an entry count so the reader can reserve space.
2554cc499d6e5ec602309501873449c938af61170b2Stephen Hines  TypeVals.push_back(TypeList.size());
2564cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
2574cc499d6e5ec602309501873449c938af61170b2Stephen Hines  TypeVals.clear();
2584cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2594cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Loop over all of the types, emitting each in turn.
2604cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
2614cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Type *T = TypeList[i];
2624cc499d6e5ec602309501873449c938af61170b2Stephen Hines    int AbbrevToUse = 0;
2634cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned Code = 0;
2644cc499d6e5ec602309501873449c938af61170b2Stephen Hines
2654cc499d6e5ec602309501873449c938af61170b2Stephen Hines    switch (T->getTypeID()) {
2664cc499d6e5ec602309501873449c938af61170b2Stephen Hines    default: llvm_unreachable("Unknown type!");
2674cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;   break;
2684cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;  break;
2694cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE; break;
2704cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80; break;
2714cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128; break;
2724cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
2734cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;  break;
2744cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA; break;
2754cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX; break;
2764cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::IntegerTyID:
2774cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // INTEGER: [width]
2784cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::TYPE_CODE_INTEGER;
2794cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
2804cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
2814cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::PointerTyID: {
2824cc499d6e5ec602309501873449c938af61170b2Stephen Hines      PointerType *PTy = cast<PointerType>(T);
2834cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // POINTER: [pointee type, address space]
2844cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::TYPE_CODE_POINTER;
2854cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
2864cc499d6e5ec602309501873449c938af61170b2Stephen Hines      unsigned AddressSpace = PTy->getAddressSpace();
2874cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(AddressSpace);
2884cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
2894cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
2904cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
2914cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::FunctionTyID: {
2924cc499d6e5ec602309501873449c938af61170b2Stephen Hines      FunctionType *FT = cast<FunctionType>(T);
2934cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // FUNCTION: [isvararg, attrid, retty, paramty x N]
2948ec3bc862ac51f49e06bc465a6eb51871ce9d5daStephen Hines      Code = bitc::TYPE_CODE_FUNCTION_OLD;
2954cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(FT->isVarArg());
2964cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(0);  // FIXME: DEAD: remove in llvm 3.0
2974cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
2984cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
2994cc499d6e5ec602309501873449c938af61170b2Stephen Hines        TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
3004cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = FunctionAbbrev;
3014cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
3024cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
3034cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::StructTyID: {
3044cc499d6e5ec602309501873449c938af61170b2Stephen Hines      StructType *ST = cast<StructType>(T);
3054cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // STRUCT: [ispacked, eltty x N]
3064cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(ST->isPacked());
3074cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Output all of the element types.
3084cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (StructType::element_iterator I = ST->element_begin(),
3094cc499d6e5ec602309501873449c938af61170b2Stephen Hines           E = ST->element_end(); I != E; ++I)
3104cc499d6e5ec602309501873449c938af61170b2Stephen Hines        TypeVals.push_back(VE.getTypeID(*I));
311d1d6a832ab9571fedabac37d476d38ca9beb0e4aStephen Hines      Code = TYPE_CODE_STRUCT_OLD_3_0;
3124cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = StructAbbrev;
3134cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
3144cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
3154cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::ArrayTyID: {
3164cc499d6e5ec602309501873449c938af61170b2Stephen Hines      ArrayType *AT = cast<ArrayType>(T);
3174cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // ARRAY: [numelts, eltty]
3184cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::TYPE_CODE_ARRAY;
3194cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(AT->getNumElements());
3204cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(VE.getTypeID(AT->getElementType()));
3214cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = ArrayAbbrev;
3224cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
3234cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
3244cc499d6e5ec602309501873449c938af61170b2Stephen Hines    case Type::VectorTyID: {
3254cc499d6e5ec602309501873449c938af61170b2Stephen Hines      VectorType *VT = cast<VectorType>(T);
3264cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // VECTOR [numelts, eltty]
3274cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::TYPE_CODE_VECTOR;
3284cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(VT->getNumElements());
3294cc499d6e5ec602309501873449c938af61170b2Stephen Hines      TypeVals.push_back(VE.getTypeID(VT->getElementType()));
3304cc499d6e5ec602309501873449c938af61170b2Stephen Hines      break;
3314cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
3324cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
3334cc499d6e5ec602309501873449c938af61170b2Stephen Hines
3344cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Emit the finished record.
3354cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
3364cc499d6e5ec602309501873449c938af61170b2Stephen Hines    TypeVals.clear();
3374cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
3384cc499d6e5ec602309501873449c938af61170b2Stephen Hines
3394cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
3404cc499d6e5ec602309501873449c938af61170b2Stephen Hines
3414cc499d6e5ec602309501873449c938af61170b2Stephen Hines  WriteTypeSymbolTable(VE, Stream);
3424cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
3434cc499d6e5ec602309501873449c938af61170b2Stephen Hines
344c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic unsigned getEncodedLinkage(const GlobalValue &GV) {
345c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  switch (GV.getLinkage()) {
346c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::ExternalLinkage:
347c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 0;
348c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::WeakAnyLinkage:
349c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 1;
350c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::AppendingLinkage:
351c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 2;
352c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::InternalLinkage:
353c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 3;
354c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::LinkOnceAnyLinkage:
355c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 4;
356c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::ExternalWeakLinkage:
357c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 7;
358c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::CommonLinkage:
359c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 8;
360c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::PrivateLinkage:
361c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 9;
362c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::WeakODRLinkage:
363c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 10;
364c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::LinkOnceODRLinkage:
365c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 11;
366c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::AvailableExternallyLinkage:
367c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 12;
3684cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
369376dc03c6bb42050ddd8a56d1a3e3c6f5d6f6e1dStephen Hines  llvm_unreachable("Invalid linkage");
3704cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
3714cc499d6e5ec602309501873449c938af61170b2Stephen Hines
372c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic unsigned getEncodedVisibility(const GlobalValue &GV) {
373c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  switch (GV.getVisibility()) {
3744cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case GlobalValue::DefaultVisibility:   return 0;
3754cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case GlobalValue::HiddenVisibility:    return 1;
3764cc499d6e5ec602309501873449c938af61170b2Stephen Hines  case GlobalValue::ProtectedVisibility: return 2;
3774cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
378c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  llvm_unreachable("Invalid visibility");
3794cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
3804cc499d6e5ec602309501873449c938af61170b2Stephen Hines
3814cc499d6e5ec602309501873449c938af61170b2Stephen Hines// Emit top-level description of module, including target triple, inline asm,
3824cc499d6e5ec602309501873449c938af61170b2Stephen Hines// descriptors for global variables, and function prototype info.
3839937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteModuleInfo(const Module *M,
3849937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                            const llvm_2_9::ValueEnumerator &VE,
3854cc499d6e5ec602309501873449c938af61170b2Stephen Hines                            BitstreamWriter &Stream) {
3864cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit various pieces of data attached to a module.
3874cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (!M->getTargetTriple().empty())
3884cc499d6e5ec602309501873449c938af61170b2Stephen Hines    WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
3894cc499d6e5ec602309501873449c938af61170b2Stephen Hines                      0/*TODO*/, Stream);
390c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const std::string &DL = M->getDataLayoutStr();
391c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (!DL.empty())
392c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
3934cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (!M->getModuleInlineAsm().empty())
3944cc499d6e5ec602309501873449c938af61170b2Stephen Hines    WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
3954cc499d6e5ec602309501873449c938af61170b2Stephen Hines                      0/*TODO*/, Stream);
3964cc499d6e5ec602309501873449c938af61170b2Stephen Hines
3974cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit information about sections and GC, computing how many there are. Also
3984cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // compute the maximum alignment value.
3994cc499d6e5ec602309501873449c938af61170b2Stephen Hines  std::map<std::string, unsigned> SectionMap;
4004cc499d6e5ec602309501873449c938af61170b2Stephen Hines  std::map<std::string, unsigned> GCMap;
4014cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned MaxAlignment = 0;
4024cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned MaxGlobalType = 0;
403c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalValue &GV : M->globals()) {
404c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
405c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
406c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (GV.hasSection()) {
407c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      // Give section names unique ID's.
408c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = SectionMap[GV.getSection()];
409c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      if (!Entry) {
410c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
411c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                          0/*TODO*/, Stream);
412c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        Entry = SectionMap.size();
413c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      }
414c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
4154cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
416c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Function &F : *M) {
417c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxAlignment = std::max(MaxAlignment, F.getAlignment());
418c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (F.hasSection()) {
4194cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Give section names unique ID's.
420c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = SectionMap[F.getSection()];
4214cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (!Entry) {
422c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
4234cc499d6e5ec602309501873449c938af61170b2Stephen Hines                          0/*TODO*/, Stream);
4244cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Entry = SectionMap.size();
4254cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
4264cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
427c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (F.hasGC()) {
4284cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Same for GC names.
429c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = GCMap[F.getGC()];
4304cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (!Entry) {
431c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
4324cc499d6e5ec602309501873449c938af61170b2Stephen Hines                          0/*TODO*/, Stream);
4334cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Entry = GCMap.size();
4344cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
4354cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
4364cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
4374cc499d6e5ec602309501873449c938af61170b2Stephen Hines
4384cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit abbrev for globals, now that we know # sections and max alignment.
4394cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned SimpleGVarAbbrev = 0;
4404cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (!M->global_empty()) {
4414cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Add an abbrev for common globals with no visibility or thread localness.
4424cc499d6e5ec602309501873449c938af61170b2Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
4434cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
4444cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4454cc499d6e5ec602309501873449c938af61170b2Stephen Hines                              Log2_32_Ceil(MaxGlobalType+1)));
4464cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
4474cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
4484cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));      // Linkage.
4494cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (MaxAlignment == 0)                                      // Alignment.
4504cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Abbv->Add(BitCodeAbbrevOp(0));
4514cc499d6e5ec602309501873449c938af61170b2Stephen Hines    else {
4524cc499d6e5ec602309501873449c938af61170b2Stephen Hines      unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
4534cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4544cc499d6e5ec602309501873449c938af61170b2Stephen Hines                               Log2_32_Ceil(MaxEncAlignment+1)));
4554cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
4564cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (SectionMap.empty())                                    // Section.
4574cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Abbv->Add(BitCodeAbbrevOp(0));
4584cc499d6e5ec602309501873449c938af61170b2Stephen Hines    else
4594cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4604cc499d6e5ec602309501873449c938af61170b2Stephen Hines                               Log2_32_Ceil(SectionMap.size()+1)));
4614cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Don't bother emitting vis + thread local.
4624cc499d6e5ec602309501873449c938af61170b2Stephen Hines    SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
4634cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
4644cc499d6e5ec602309501873449c938af61170b2Stephen Hines
4654cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit the global variable information.
4664cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<unsigned, 64> Vals;
467c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalVariable &GV : M->globals()) {
4684cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned AbbrevToUse = 0;
4694cc499d6e5ec602309501873449c938af61170b2Stephen Hines
4704cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // GLOBALVAR: [type, isconst, initid,
4714cc499d6e5ec602309501873449c938af61170b2Stephen Hines    //             linkage, alignment, section, visibility, threadlocal,
4724cc499d6e5ec602309501873449c938af61170b2Stephen Hines    //             unnamed_addr]
473c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(GV.getType()));
474c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.isConstant());
475c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.isDeclaration() ? 0 :
476c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                   (VE.getValueID(GV.getInitializer()) + 1));
4774cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.push_back(getEncodedLinkage(GV));
478c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(Log2_32(GV.getAlignment())+1);
479c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
480c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (GV.isThreadLocal() ||
481c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        GV.getVisibility() != GlobalValue::DefaultVisibility ||
482c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        GV.hasUnnamedAddr()) {
4834cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Vals.push_back(getEncodedVisibility(GV));
484c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Vals.push_back(GV.isThreadLocal());
485c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Vals.push_back(GV.hasUnnamedAddr());
4864cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else {
4874cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = SimpleGVarAbbrev;
4884cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
4894cc499d6e5ec602309501873449c938af61170b2Stephen Hines
4904cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
4914cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.clear();
4924cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
4934cc499d6e5ec602309501873449c938af61170b2Stephen Hines
4944cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit the function proto information.
495c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Function &F : *M) {
4964cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // FUNCTION:  [type, callingconv, isproto, paramattr,
4974cc499d6e5ec602309501873449c938af61170b2Stephen Hines    //             linkage, alignment, section, visibility, gc, unnamed_addr]
498c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(F.getType()));
499c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.getCallingConv());
500c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.isDeclaration());
5014cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.push_back(getEncodedLinkage(F));
502c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getAttributeID(F.getAttributes()));
503c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(Log2_32(F.getAlignment())+1);
504c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
5054cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.push_back(getEncodedVisibility(F));
506c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
507c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasUnnamedAddr());
5084cc499d6e5ec602309501873449c938af61170b2Stephen Hines
5094cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned AbbrevToUse = 0;
5104cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
5114cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.clear();
5124cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
5134cc499d6e5ec602309501873449c938af61170b2Stephen Hines
5144cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Emit the alias information.
515c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalAlias &A : M->aliases()) {
516c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(A.getType()));
517c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getValueID(A.getAliasee()));
518c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(getEncodedLinkage(A));
519c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(getEncodedVisibility(A));
5204cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned AbbrevToUse = 0;
5218f093e05e28046b6fc74175b66a06152f72e0c66Pirama Arumuga Nainar    Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
5224cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.clear();
5234cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
5244cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
5254cc499d6e5ec602309501873449c938af61170b2Stephen Hines
5264cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic uint64_t GetOptimizationFlags(const Value *V) {
5274cc499d6e5ec602309501873449c938af61170b2Stephen Hines  uint64_t Flags = 0;
5284cc499d6e5ec602309501873449c938af61170b2Stephen Hines
529c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
5304cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (OBO->hasNoSignedWrap())
5314cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
5324cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (OBO->hasNoUnsignedWrap())
5334cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
534c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
5354cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (PEO->isExact())
5364cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Flags |= 1 << bitc::PEO_EXACT;
5374cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
5384cc499d6e5ec602309501873449c938af61170b2Stephen Hines
5394cc499d6e5ec602309501873449c938af61170b2Stephen Hines  return Flags;
5404cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
5414cc499d6e5ec602309501873449c938af61170b2Stephen Hines
542c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteValueAsMetadata(const ValueAsMetadata *MD,
543c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 const llvm_2_9::ValueEnumerator &VE,
544c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 BitstreamWriter &Stream,
545c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 SmallVectorImpl<uint64_t> &Record) {
546c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  // Mimic an MDNode with a value as one operand.
547c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Value *V = MD->getValue();
548c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getTypeID(V->getType()));
549c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getValueID(V));
550c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(METADATA_NODE_2_7, Record, 0);
551c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.clear();
552c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}
553c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
554c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteMDTuple(const MDTuple *N, const llvm_2_9::ValueEnumerator &VE,
555c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                         BitstreamWriter &Stream,
556c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                         SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
5574cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
558c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Metadata *MD = N->getOperand(i);
559c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    assert(!(MD && isa<LocalAsMetadata>(MD)) &&
560c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines           "Unexpected function-local metadata");
561c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (!MD) {
562c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      // TODO(srhines): I don't believe this case can exist for RS.
563c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(llvm::Type::getVoidTy(N->getContext())));
5644cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(0);
565c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    } else if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
566c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(MDC->getType()));
567c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getValueID(MDC->getValue()));
568c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    } else {
569c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(
570c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines          llvm::Type::getMetadataTy(N->getContext())));
571c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getMetadataID(MD));
5724cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
5734cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
574c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(METADATA_NODE_2_7, Record, Abbrev);
5754cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Record.clear();
5764cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
5774cc499d6e5ec602309501873449c938af61170b2Stephen Hines
578c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines/*static void WriteMDLocation(const MDLocation *N, const llvm_2_9::ValueEnumerator &VE,
579c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            BitstreamWriter &Stream,
580c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            SmallVectorImpl<uint64_t> &Record,
581c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            unsigned Abbrev) {
582c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->isDistinct());
583c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->getLine());
584c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->getColumn());
585c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getMetadataID(N->getScope()));
586c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
587c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
588c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
589c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.clear();
590c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}
591c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
592c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteGenericDebugNode(const GenericDebugNode *,
593c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                  const llvm_2_9::ValueEnumerator &, BitstreamWriter &,
594c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                  SmallVectorImpl<uint64_t> &, unsigned) {
595c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  llvm_unreachable("unimplemented");
596c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}*/
597c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
5984cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteModuleMetadata(const Module *M,
5999937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                const llvm_2_9::ValueEnumerator &VE,
6004cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                BitstreamWriter &Stream) {
601c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const auto &MDs = VE.getMDs();
602c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (MDs.empty() && M->named_metadata_empty())
603c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return;
604c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
605c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  // RenderScript files *ALWAYS* have metadata!
606c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
607c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
6084cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned MDSAbbrev = 0;
609c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (VE.hasMDString()) {
610c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_STRING.
611c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
612c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
613c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
614c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
615c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MDSAbbrev = Stream.EmitAbbrev(Abbv);
616c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6174cc499d6e5ec602309501873449c938af61170b2Stephen Hines
618c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned MDLocationAbbrev = 0;
6191906a00dce8e32fe3bb8a957e333ebbbee0888e3Pirama Arumuga Nainar  if (VE.hasDILocation()) {
620c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // TODO(srhines): Should be unreachable for RenderScript.
621c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_LOCATION.
622c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    //
623c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Assume the column is usually under 128, and always output the inlined-at
624c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // location (it's never more expensive than building an array size 1).
625c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
626c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
627c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
628c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
629c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
630c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
631c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
632c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MDLocationAbbrev = Stream.EmitAbbrev(Abbv);
633c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6344cc499d6e5ec602309501873449c938af61170b2Stephen Hines
635c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned NameAbbrev = 0;
636c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (!M->named_metadata_empty()) {
637c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_NAME.
638c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
639c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
640c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
641c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
642c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    NameAbbrev = Stream.EmitAbbrev(Abbv);
643c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6444cc499d6e5ec602309501873449c938af61170b2Stephen Hines
645c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned MDTupleAbbrev = 0;
646c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  //unsigned GenericDebugNodeAbbrev = 0;
647c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  SmallVector<uint64_t, 64> Record;
648c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Metadata *MD : MDs) {
649c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (const MDNode *N = dyn_cast<MDNode>(MD)) {
650c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      switch (N->getMetadataID()) {
651c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      default:
652c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        llvm_unreachable("Invalid MDNode subclass");
653c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)
654c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#define HANDLE_MDNODE_LEAF(CLASS)                                              \
655c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case Metadata::CLASS##Kind:                                                  \
656c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev);           \
657c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    continue;
658c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#include "llvm/IR/Metadata.def"
659c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      }
660c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
661c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
662c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      WriteValueAsMetadata(MDC, VE, Stream, Record);
663c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      continue;
6644cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
665c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    const MDString *MDS = cast<MDString>(MD);
666c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Code: [strchar x N]
667c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.append(MDS->bytes_begin(), MDS->bytes_end());
668c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
669c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Emit the finished record.
670c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
671c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.clear();
6724cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
6734cc499d6e5ec602309501873449c938af61170b2Stephen Hines
6744cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Write named metadata.
675c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const NamedMDNode &NMD : M->named_metadata()) {
6764cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Write name.
677c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    StringRef Str = NMD.getName();
678c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.append(Str.bytes_begin(), Str.bytes_end());
679c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
6804cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.clear();
6814cc499d6e5ec602309501873449c938af61170b2Stephen Hines
6824cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Write named metadata operands.
683c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    for (const MDNode *N : NMD.operands())
684c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getMetadataID(N));
685a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines    Stream.EmitRecord(METADATA_NAMED_NODE_2_7, Record, 0);
6864cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.clear();
6874cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
6884cc499d6e5ec602309501873449c938af61170b2Stephen Hines
689c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.ExitBlock();
6904cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
6914cc499d6e5ec602309501873449c938af61170b2Stephen Hines
6924cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteFunctionLocalMetadata(const Function &F,
6939937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                       const llvm_2_9::ValueEnumerator &VE,
6944cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                       BitstreamWriter &Stream) {
6954cc499d6e5ec602309501873449c938af61170b2Stephen Hines  bool StartedMetadataBlock = false;
6964cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> Record;
697c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const SmallVectorImpl<const LocalAsMetadata *> &MDs =
698c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      VE.getFunctionLocalMDs();
699c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
700c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    assert(MDs[i] && "Expected valid function-local metadata");
701c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (!StartedMetadataBlock) {
702c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
703c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      StartedMetadataBlock = true;
704c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
705c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    WriteValueAsMetadata(MDs[i], VE, Stream, Record);
706c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
7074cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7084cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (StartedMetadataBlock)
7094cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.ExitBlock();
7104cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
7114cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7124cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteMetadataAttachment(const Function &F,
7139937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                    const llvm_2_9::ValueEnumerator &VE,
7144cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                    BitstreamWriter &Stream) {
7154cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
7164cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7174cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> Record;
7184cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7194cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Write metadata attachments
7204cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
721c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
7224cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7234cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
7244cc499d6e5ec602309501873449c938af61170b2Stephen Hines    for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
7254cc499d6e5ec602309501873449c938af61170b2Stephen Hines         I != E; ++I) {
7264cc499d6e5ec602309501873449c938af61170b2Stephen Hines      MDs.clear();
7274cc499d6e5ec602309501873449c938af61170b2Stephen Hines      I->getAllMetadataOtherThanDebugLoc(MDs);
7284cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7294cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // If no metadata, ignore instruction.
7304cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (MDs.empty()) continue;
7314cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7328f093e05e28046b6fc74175b66a06152f72e0c66Pirama Arumuga Nainar      Record.push_back(VE.getInstructionID(&*I));
7334cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7344cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
7354cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(MDs[i].first);
736c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        Record.push_back(VE.getMetadataID(MDs[i].second));
7374cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
738a546cb661a8ab70577827b8310e2b56a0773c1e1Stephen Hines      Stream.EmitRecord(METADATA_ATTACHMENT_2_7, Record, 0);
7394cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.clear();
7404cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
7414cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7424cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
7434cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
7444cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7454cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
7464cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> Record;
7474cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7484cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Write metadata kinds
7494cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // METADATA_KIND - [n x [id, name]]
7504cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<StringRef, 4> Names;
7514cc499d6e5ec602309501873449c938af61170b2Stephen Hines  M->getMDKindNames(Names);
7524cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7534cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (Names.empty()) return;
7544cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7554cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
7564cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7574cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
7584cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.push_back(MDKindID);
7594cc499d6e5ec602309501873449c938af61170b2Stephen Hines    StringRef KName = Names[MDKindID];
7604cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.append(KName.begin(), KName.end());
7614cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7624cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
7634cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.clear();
7644cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
7654cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7664cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
7674cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
7684cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7694cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteConstants(unsigned FirstVal, unsigned LastVal,
7709937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                           const llvm_2_9::ValueEnumerator &VE,
7714cc499d6e5ec602309501873449c938af61170b2Stephen Hines                           BitstreamWriter &Stream, bool isGlobal) {
7724cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (FirstVal == LastVal) return;
7734cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7744cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
7754cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7764cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned AggregateAbbrev = 0;
7774cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned String8Abbrev = 0;
7784cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned CString7Abbrev = 0;
7794cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned CString6Abbrev = 0;
7804cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // If this is a constant pool for the module, emit module-specific abbrevs.
7814cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (isGlobal) {
7824cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Abbrev for CST_CODE_AGGREGATE.
7834cc499d6e5ec602309501873449c938af61170b2Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
7844cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
7854cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7864cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
7874cc499d6e5ec602309501873449c938af61170b2Stephen Hines    AggregateAbbrev = Stream.EmitAbbrev(Abbv);
7884cc499d6e5ec602309501873449c938af61170b2Stephen Hines
7894cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Abbrev for CST_CODE_STRING.
7904cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv = new BitCodeAbbrev();
7914cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
7924cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7934cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
7944cc499d6e5ec602309501873449c938af61170b2Stephen Hines    String8Abbrev = Stream.EmitAbbrev(Abbv);
7954cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Abbrev for CST_CODE_CSTRING.
7964cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv = new BitCodeAbbrev();
7974cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
7984cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7994cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
8004cc499d6e5ec602309501873449c938af61170b2Stephen Hines    CString7Abbrev = Stream.EmitAbbrev(Abbv);
8014cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // Abbrev for CST_CODE_CSTRING.
8024cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv = new BitCodeAbbrev();
8034cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
8044cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
8054cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
8064cc499d6e5ec602309501873449c938af61170b2Stephen Hines    CString6Abbrev = Stream.EmitAbbrev(Abbv);
8074cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
8084cc499d6e5ec602309501873449c938af61170b2Stephen Hines
8094cc499d6e5ec602309501873449c938af61170b2Stephen Hines  SmallVector<uint64_t, 64> Record;
8104cc499d6e5ec602309501873449c938af61170b2Stephen Hines
8119937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9::ValueEnumerator::ValueList &Vals = VE.getValues();
812c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Type *LastTy = nullptr;
8134cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = FirstVal; i != LastVal; ++i) {
8144cc499d6e5ec602309501873449c938af61170b2Stephen Hines    const Value *V = Vals[i].first;
8154cc499d6e5ec602309501873449c938af61170b2Stephen Hines    // If we need to switch types, do so now.
8164cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (V->getType() != LastTy) {
8174cc499d6e5ec602309501873449c938af61170b2Stephen Hines      LastTy = V->getType();
8184cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(VE.getTypeID(LastTy));
8194cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
8204cc499d6e5ec602309501873449c938af61170b2Stephen Hines                        CONSTANTS_SETTYPE_ABBREV);
8214cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.clear();
8224cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
8234cc499d6e5ec602309501873449c938af61170b2Stephen Hines
8244cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
8254cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(unsigned(IA->hasSideEffects()) |
8264cc499d6e5ec602309501873449c938af61170b2Stephen Hines                       unsigned(IA->isAlignStack()) << 1);
8274cc499d6e5ec602309501873449c938af61170b2Stephen Hines
8284cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Add the asm string.
8294cc499d6e5ec602309501873449c938af61170b2Stephen Hines      const std::string &AsmStr = IA->getAsmString();
8304cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(AsmStr.size());
8314cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
8324cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(AsmStr[i]);
8334cc499d6e5ec602309501873449c938af61170b2Stephen Hines
8344cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Add the constraint string.
8354cc499d6e5ec602309501873449c938af61170b2Stephen Hines      const std::string &ConstraintStr = IA->getConstraintString();
8364cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(ConstraintStr.size());
8374cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
8384cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(ConstraintStr[i]);
8394cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
8404cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.clear();
8414cc499d6e5ec602309501873449c938af61170b2Stephen Hines      continue;
8424cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
8434cc499d6e5ec602309501873449c938af61170b2Stephen Hines    const Constant *C = cast<Constant>(V);
8444cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned Code = -1U;
8454cc499d6e5ec602309501873449c938af61170b2Stephen Hines    unsigned AbbrevToUse = 0;
8464cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (C->isNullValue()) {
8474cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::CST_CODE_NULL;
8484cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else if (isa<UndefValue>(C)) {
8494cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::CST_CODE_UNDEF;
8504cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
8514cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (IV->getBitWidth() <= 64) {
8524cc499d6e5ec602309501873449c938af61170b2Stephen Hines        uint64_t V = IV->getSExtValue();
8534cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if ((int64_t)V >= 0)
8544cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(V << 1);
8554cc499d6e5ec602309501873449c938af61170b2Stephen Hines        else
8564cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back((-V << 1) | 1);
8574cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_INTEGER;
8584cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
8594cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else {                             // Wide integers, > 64 bits in size.
8604cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // We have an arbitrary precision integer value to write whose
8614cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // bit width is > 64. However, in canonical unsigned integer
8624cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // format it is likely that the high bits are going to be zero.
8634cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // So, we only write the number of active words.
8644cc499d6e5ec602309501873449c938af61170b2Stephen Hines        unsigned NWords = IV->getValue().getActiveWords();
8654cc499d6e5ec602309501873449c938af61170b2Stephen Hines        const uint64_t *RawWords = IV->getValue().getRawData();
8664cc499d6e5ec602309501873449c938af61170b2Stephen Hines        for (unsigned i = 0; i != NWords; ++i) {
8674cc499d6e5ec602309501873449c938af61170b2Stephen Hines          int64_t V = RawWords[i];
8684cc499d6e5ec602309501873449c938af61170b2Stephen Hines          if (V >= 0)
8694cc499d6e5ec602309501873449c938af61170b2Stephen Hines            Record.push_back(V << 1);
8704cc499d6e5ec602309501873449c938af61170b2Stephen Hines          else
8714cc499d6e5ec602309501873449c938af61170b2Stephen Hines            Record.push_back((-V << 1) | 1);
8724cc499d6e5ec602309501873449c938af61170b2Stephen Hines        }
8734cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_WIDE_INTEGER;
8744cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
8754cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
8764cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::CST_CODE_FLOAT;
8774cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Type *Ty = CFP->getType();
8784cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (Ty->isFloatTy() || Ty->isDoubleTy()) {
8794cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
8804cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else if (Ty->isX86_FP80Ty()) {
8814cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // api needed to prevent premature destruction
8824cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // bits are not in the same order as a normal i80 APInt, compensate.
8834cc499d6e5ec602309501873449c938af61170b2Stephen Hines        APInt api = CFP->getValueAPF().bitcastToAPInt();
8844cc499d6e5ec602309501873449c938af61170b2Stephen Hines        const uint64_t *p = api.getRawData();
8854cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back((p[1] << 48) | (p[0] >> 16));
8864cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(p[0] & 0xffffLL);
8874cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
8884cc499d6e5ec602309501873449c938af61170b2Stephen Hines        APInt api = CFP->getValueAPF().bitcastToAPInt();
8894cc499d6e5ec602309501873449c938af61170b2Stephen Hines        const uint64_t *p = api.getRawData();
8904cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(p[0]);
8914cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(p[1]);
8924cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else {
8934cc499d6e5ec602309501873449c938af61170b2Stephen Hines        assert (0 && "Unknown FP type!");
8944cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
8950444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines    } else if (isa<ConstantDataSequential>(C) &&
8960444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines               cast<ConstantDataSequential>(C)->isString()) {
8970444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
8984cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // Emit constant strings specially.
8990444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      unsigned NumElts = Str->getNumElements();
9004cc499d6e5ec602309501873449c938af61170b2Stephen Hines      // If this is a null-terminated string, use the denser CSTRING encoding.
9010444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      if (Str->isCString()) {
9024cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CSTRING;
9030444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines        --NumElts;  // Don't encode the null, which isn't allowed by char6.
9044cc499d6e5ec602309501873449c938af61170b2Stephen Hines      } else {
9054cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_STRING;
9064cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = String8Abbrev;
9074cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
9084cc499d6e5ec602309501873449c938af61170b2Stephen Hines      bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
9094cc499d6e5ec602309501873449c938af61170b2Stephen Hines      bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
9100444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      for (unsigned i = 0; i != NumElts; ++i) {
9110444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines        unsigned char V = Str->getElementAsInteger(i);
9124cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(V);
9134cc499d6e5ec602309501873449c938af61170b2Stephen Hines        isCStr7 &= (V & 128) == 0;
9144cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if (isCStrChar6)
9154cc499d6e5ec602309501873449c938af61170b2Stephen Hines          isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
9164cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
91723c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
9184cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (isCStrChar6)
9194cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = CString6Abbrev;
9204cc499d6e5ec602309501873449c938af61170b2Stephen Hines      else if (isCStr7)
9214cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = CString7Abbrev;
92223c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines    } else if (const ConstantDataSequential *CDS =
9230444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines                  dyn_cast<ConstantDataSequential>(C)) {
924db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      // We must replace ConstantDataSequential's representation with the
925db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      // legacy ConstantArray/ConstantVector/ConstantStruct version.
926db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      // ValueEnumerator is similarly modified to mark the appropriate
927db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      // Constants as used (so they are emitted).
928db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      Code = bitc::CST_CODE_AGGREGATE;
929db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
930db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines        Record.push_back(VE.getValueID(CDS->getElementAsConstant(i)));
931db37c900ebe6f0ae62a1f77327dd9e5a469ff11eStephen Hines      AbbrevToUse = AggregateAbbrev;
9320444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines    } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
9330444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines               isa<ConstantVector>(C)) {
9344cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::CST_CODE_AGGREGATE;
9354cc499d6e5ec602309501873449c938af61170b2Stephen Hines      for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
9364cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(i)));
9374cc499d6e5ec602309501873449c938af61170b2Stephen Hines      AbbrevToUse = AggregateAbbrev;
9384cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
9394cc499d6e5ec602309501873449c938af61170b2Stephen Hines      switch (CE->getOpcode()) {
9404cc499d6e5ec602309501873449c938af61170b2Stephen Hines      default:
9414cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if (Instruction::isCast(CE->getOpcode())) {
9424cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Code = bitc::CST_CODE_CE_CAST;
9434cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
9444cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
9454cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getValueID(C->getOperand(0)));
9464cc499d6e5ec602309501873449c938af61170b2Stephen Hines          AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
9474cc499d6e5ec602309501873449c938af61170b2Stephen Hines        } else {
9484cc499d6e5ec602309501873449c938af61170b2Stephen Hines          assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
9494cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Code = bitc::CST_CODE_CE_BINOP;
9504cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
9514cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getValueID(C->getOperand(0)));
9524cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getValueID(C->getOperand(1)));
9534cc499d6e5ec602309501873449c938af61170b2Stephen Hines          uint64_t Flags = GetOptimizationFlags(CE);
9544cc499d6e5ec602309501873449c938af61170b2Stephen Hines          if (Flags != 0)
9554cc499d6e5ec602309501873449c938af61170b2Stephen Hines            Record.push_back(Flags);
9564cc499d6e5ec602309501873449c938af61170b2Stephen Hines        }
9574cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
9584cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::GetElementPtr:
9594cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CE_GEP;
9604cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if (cast<GEPOperator>(C)->isInBounds())
9614cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
9624cc499d6e5ec602309501873449c938af61170b2Stephen Hines        for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
9634cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
9644cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getValueID(C->getOperand(i)));
9654cc499d6e5ec602309501873449c938af61170b2Stephen Hines        }
9664cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
9674cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::Select:
9684cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CE_SELECT;
9694cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(0)));
9704cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(1)));
9714cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(2)));
9724cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
9734cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::ExtractElement:
9744cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CE_EXTRACTELT;
9754cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
9764cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(0)));
9774cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(1)));
9784cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
9794cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::InsertElement:
9804cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CE_INSERTELT;
9814cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(0)));
9824cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(1)));
9834cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(2)));
9844cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
9854cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::ShuffleVector:
9864cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // If the return type and argument types are the same, this is a
9874cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // standard shufflevector instruction.  If the types are different,
9884cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // then the shuffle is widening or truncating the input vectors, and
9894cc499d6e5ec602309501873449c938af61170b2Stephen Hines        // the argument type must also be encoded.
9904cc499d6e5ec602309501873449c938af61170b2Stephen Hines        if (C->getType() == C->getOperand(0)->getType()) {
9914cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Code = bitc::CST_CODE_CE_SHUFFLEVEC;
9924cc499d6e5ec602309501873449c938af61170b2Stephen Hines        } else {
9934cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Code = bitc::CST_CODE_CE_SHUFVEC_EX;
9944cc499d6e5ec602309501873449c938af61170b2Stephen Hines          Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
9954cc499d6e5ec602309501873449c938af61170b2Stephen Hines        }
9964cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(0)));
9974cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(1)));
9984cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(2)));
9994cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
10004cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::ICmp:
10014cc499d6e5ec602309501873449c938af61170b2Stephen Hines      case Instruction::FCmp:
10024cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Code = bitc::CST_CODE_CE_CMP;
10034cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
10044cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(0)));
10054cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(VE.getValueID(C->getOperand(1)));
10064cc499d6e5ec602309501873449c938af61170b2Stephen Hines        Record.push_back(CE->getPredicate());
10074cc499d6e5ec602309501873449c938af61170b2Stephen Hines        break;
10084cc499d6e5ec602309501873449c938af61170b2Stephen Hines      }
10094cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
10104cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::CST_CODE_BLOCKADDRESS;
10114cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
10124cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(VE.getValueID(BA->getFunction()));
10134cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
10144cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else {
10154cc499d6e5ec602309501873449c938af61170b2Stephen Hines#ifndef NDEBUG
10164cc499d6e5ec602309501873449c938af61170b2Stephen Hines      C->dump();
10174cc499d6e5ec602309501873449c938af61170b2Stephen Hines#endif
10184cc499d6e5ec602309501873449c938af61170b2Stephen Hines      llvm_unreachable("Unknown constant!");
10194cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
10204cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Stream.EmitRecord(Code, Record, AbbrevToUse);
10214cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Record.clear();
10224cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
10234cc499d6e5ec602309501873449c938af61170b2Stephen Hines
10244cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Stream.ExitBlock();
10254cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
10264cc499d6e5ec602309501873449c938af61170b2Stephen Hines
10279937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteModuleConstants(const llvm_2_9::ValueEnumerator &VE,
10284cc499d6e5ec602309501873449c938af61170b2Stephen Hines                                 BitstreamWriter &Stream) {
10299937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9::ValueEnumerator::ValueList &Vals = VE.getValues();
10304cc499d6e5ec602309501873449c938af61170b2Stephen Hines
10314cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // Find the first constant to emit, which is the first non-globalvalue value.
10324cc499d6e5ec602309501873449c938af61170b2Stephen Hines  // We know globalvalues have been emitted by WriteModuleInfo.
10334cc499d6e5ec602309501873449c938af61170b2Stephen Hines  for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
10344cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (!isa<GlobalValue>(Vals[i].first)) {
10354cc499d6e5ec602309501873449c938af61170b2Stephen Hines      WriteConstants(i, Vals.size(), VE, Stream, true);
10364cc499d6e5ec602309501873449c938af61170b2Stephen Hines      return;
10374cc499d6e5ec602309501873449c938af61170b2Stephen Hines    }
10384cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
10394cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
10404cc499d6e5ec602309501873449c938af61170b2Stephen Hines
10414cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// PushValueAndType - The file has to encode both the value and type id for
10424cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// many values, because we need to know what type to create for forward
10434cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// references.  However, most operands are not forward references, so this type
10444cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// field is not needed.
10454cc499d6e5ec602309501873449c938af61170b2Stephen Hines///
10464cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// This function adds V's value ID to Vals.  If the value ID is higher than the
10474cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// instruction ID, then it is a forward reference, and it also includes the
10484cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// type ID.
10494cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic bool PushValueAndType(const Value *V, unsigned InstID,
10504cc499d6e5ec602309501873449c938af61170b2Stephen Hines                             SmallVector<unsigned, 64> &Vals,
10519937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                             llvm_2_9::ValueEnumerator &VE) {
10524cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned ValID = VE.getValueID(V);
10534cc499d6e5ec602309501873449c938af61170b2Stephen Hines  Vals.push_back(ValID);
10544cc499d6e5ec602309501873449c938af61170b2Stephen Hines  if (ValID >= InstID) {
10554cc499d6e5ec602309501873449c938af61170b2Stephen Hines    Vals.push_back(VE.getTypeID(V->getType()));
10564cc499d6e5ec602309501873449c938af61170b2Stephen Hines    return true;
10574cc499d6e5ec602309501873449c938af61170b2Stephen Hines  }
10584cc499d6e5ec602309501873449c938af61170b2Stephen Hines  return false;
10594cc499d6e5ec602309501873449c938af61170b2Stephen Hines}
10604cc499d6e5ec602309501873449c938af61170b2Stephen Hines
10614cc499d6e5ec602309501873449c938af61170b2Stephen Hines/// WriteInstruction - Emit an instruction to the specified stream.
10624cc499d6e5ec602309501873449c938af61170b2Stephen Hinesstatic void WriteInstruction(const Instruction &I, unsigned InstID,
10639937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                             llvm_2_9::ValueEnumerator &VE,
10649937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                             BitstreamWriter &Stream,
10654cc499d6e5ec602309501873449c938af61170b2Stephen Hines                             SmallVector<unsigned, 64> &Vals) {
10664cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned Code = 0;
10674cc499d6e5ec602309501873449c938af61170b2Stephen Hines  unsigned AbbrevToUse = 0;
10684cc499d6e5ec602309501873449c938af61170b2Stephen Hines  VE.setInstructionID(&I);
10694cc499d6e5ec602309501873449c938af61170b2Stephen Hines  switch (I.getOpcode()) {
10704cc499d6e5ec602309501873449c938af61170b2Stephen Hines  default:
10714cc499d6e5ec602309501873449c938af61170b2Stephen Hines    if (Instruction::isCast(I.getOpcode())) {
10724cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::FUNC_CODE_INST_CAST;
10734cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
10744cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
10754cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Vals.push_back(VE.getTypeID(I.getType()));
10764cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
10774cc499d6e5ec602309501873449c938af61170b2Stephen Hines    } else {
10784cc499d6e5ec602309501873449c938af61170b2Stephen Hines      assert(isa<BinaryOperator>(I) && "Unknown instruction!");
10794cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Code = bitc::FUNC_CODE_INST_BINOP;
10804cc499d6e5ec602309501873449c938af61170b2Stephen Hines      if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
10814cc499d6e5ec602309501873449c938af61170b2Stephen Hines        AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
10824cc499d6e5ec602309501873449c938af61170b2Stephen Hines      Vals.push_back(VE.getValueID(I.getOperand(1)));
1083