19b044ec938fd56355012851890c63974c8042c9fStephen Hines//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
29b044ec938fd56355012851890c63974c8042c9fStephen Hines//
39b044ec938fd56355012851890c63974c8042c9fStephen Hines//                     The LLVM Compiler Infrastructure
49b044ec938fd56355012851890c63974c8042c9fStephen Hines//
59b044ec938fd56355012851890c63974c8042c9fStephen Hines// This file is distributed under the University of Illinois Open Source
69b044ec938fd56355012851890c63974c8042c9fStephen Hines// License. See LICENSE.TXT for details.
79b044ec938fd56355012851890c63974c8042c9fStephen Hines//
89b044ec938fd56355012851890c63974c8042c9fStephen Hines//===----------------------------------------------------------------------===//
99b044ec938fd56355012851890c63974c8042c9fStephen Hines//
109b044ec938fd56355012851890c63974c8042c9fStephen Hines// Bitcode writer implementation.
119b044ec938fd56355012851890c63974c8042c9fStephen Hines//
129b044ec938fd56355012851890c63974c8042c9fStephen Hines//===----------------------------------------------------------------------===//
139b044ec938fd56355012851890c63974c8042c9fStephen Hines
149b044ec938fd56355012851890c63974c8042c9fStephen Hines#include "ReaderWriter_2_9_func.h"
158bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines#include "legacy_bitcode.h"
169b044ec938fd56355012851890c63974c8042c9fStephen Hines#include "ValueEnumerator.h"
179b044ec938fd56355012851890c63974c8042c9fStephen 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"
289b044ec938fd56355012851890c63974c8042c9fStephen Hines#include "llvm/Support/ErrorHandling.h"
299b044ec938fd56355012851890c63974c8042c9fStephen Hines#include "llvm/Support/MathExtras.h"
309b044ec938fd56355012851890c63974c8042c9fStephen Hines#include "llvm/Support/Program.h"
3123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines#include "llvm/Support/raw_ostream.h"
329b044ec938fd56355012851890c63974c8042c9fStephen Hines#include <cctype>
339b044ec938fd56355012851890c63974c8042c9fStephen Hines#include <map>
349b044ec938fd56355012851890c63974c8042c9fStephen Hinesusing namespace llvm;
359b044ec938fd56355012851890c63974c8042c9fStephen Hines
369b044ec938fd56355012851890c63974c8042c9fStephen Hines/// These are manifest constants used by the bitcode writer. They do not need to
379b044ec938fd56355012851890c63974c8042c9fStephen Hines/// be kept in sync with the reader, but need to be consistent within this file.
389b044ec938fd56355012851890c63974c8042c9fStephen Hinesenum {
399b044ec938fd56355012851890c63974c8042c9fStephen Hines  CurVersion = 0,
409b044ec938fd56355012851890c63974c8042c9fStephen Hines
419b044ec938fd56355012851890c63974c8042c9fStephen Hines  // VALUE_SYMTAB_BLOCK abbrev id's.
429b044ec938fd56355012851890c63974c8042c9fStephen Hines  VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
439b044ec938fd56355012851890c63974c8042c9fStephen Hines  VST_ENTRY_7_ABBREV,
449b044ec938fd56355012851890c63974c8042c9fStephen Hines  VST_ENTRY_6_ABBREV,
459b044ec938fd56355012851890c63974c8042c9fStephen Hines  VST_BBENTRY_6_ABBREV,
469b044ec938fd56355012851890c63974c8042c9fStephen Hines
479b044ec938fd56355012851890c63974c8042c9fStephen Hines  // CONSTANTS_BLOCK abbrev id's.
489b044ec938fd56355012851890c63974c8042c9fStephen Hines  CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
499b044ec938fd56355012851890c63974c8042c9fStephen Hines  CONSTANTS_INTEGER_ABBREV,
509b044ec938fd56355012851890c63974c8042c9fStephen Hines  CONSTANTS_CE_CAST_Abbrev,
519b044ec938fd56355012851890c63974c8042c9fStephen Hines  CONSTANTS_NULL_Abbrev,
529b044ec938fd56355012851890c63974c8042c9fStephen Hines
539b044ec938fd56355012851890c63974c8042c9fStephen Hines  // FUNCTION_BLOCK abbrev id's.
549b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
559b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_BINOP_ABBREV,
569b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_BINOP_FLAGS_ABBREV,
579b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_CAST_ABBREV,
589b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_RET_VOID_ABBREV,
599b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_RET_VAL_ABBREV,
609b044ec938fd56355012851890c63974c8042c9fStephen Hines  FUNCTION_INST_UNREACHABLE_ABBREV
619b044ec938fd56355012851890c63974c8042c9fStephen Hines};
629b044ec938fd56355012851890c63974c8042c9fStephen Hines
639b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic unsigned GetEncodedCastOpcode(unsigned Opcode) {
649b044ec938fd56355012851890c63974c8042c9fStephen Hines  switch (Opcode) {
659b044ec938fd56355012851890c63974c8042c9fStephen Hines  default: llvm_unreachable("Unknown cast instruction!");
669b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Trunc   : return bitc::CAST_TRUNC;
679b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::ZExt    : return bitc::CAST_ZEXT;
689b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::SExt    : return bitc::CAST_SEXT;
699b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
709b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
719b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::UIToFP  : return bitc::CAST_UITOFP;
729b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::SIToFP  : return bitc::CAST_SITOFP;
739b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
749b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FPExt   : return bitc::CAST_FPEXT;
759b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
769b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
779b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::BitCast : return bitc::CAST_BITCAST;
789b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
799b044ec938fd56355012851890c63974c8042c9fStephen Hines}
809b044ec938fd56355012851890c63974c8042c9fStephen Hines
819b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
829b044ec938fd56355012851890c63974c8042c9fStephen Hines  switch (Opcode) {
839b044ec938fd56355012851890c63974c8042c9fStephen Hines  default: llvm_unreachable("Unknown binary instruction!");
849b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Add:
859b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FAdd: return bitc::BINOP_ADD;
869b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Sub:
879b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FSub: return bitc::BINOP_SUB;
889b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Mul:
899b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FMul: return bitc::BINOP_MUL;
909b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::UDiv: return bitc::BINOP_UDIV;
919b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FDiv:
929b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::SDiv: return bitc::BINOP_SDIV;
939b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::URem: return bitc::BINOP_UREM;
949b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::FRem:
959b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::SRem: return bitc::BINOP_SREM;
969b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Shl:  return bitc::BINOP_SHL;
979b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::LShr: return bitc::BINOP_LSHR;
989b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::AShr: return bitc::BINOP_ASHR;
999b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::And:  return bitc::BINOP_AND;
1009b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Or:   return bitc::BINOP_OR;
1019b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Instruction::Xor:  return bitc::BINOP_XOR;
1029b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1039b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1049b044ec938fd56355012851890c63974c8042c9fStephen Hines
1059b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
1069b044ec938fd56355012851890c63974c8042c9fStephen Hines  switch (Op) {
1079b044ec938fd56355012851890c63974c8042c9fStephen Hines  default: llvm_unreachable("Unknown RMW operation!");
1089b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
1099b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Add: return bitc::RMW_ADD;
1109b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Sub: return bitc::RMW_SUB;
1119b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::And: return bitc::RMW_AND;
1129b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Nand: return bitc::RMW_NAND;
1139b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Or: return bitc::RMW_OR;
1149b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Xor: return bitc::RMW_XOR;
1159b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Max: return bitc::RMW_MAX;
1169b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::Min: return bitc::RMW_MIN;
1179b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
1189b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
1199b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1209b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1219b044ec938fd56355012851890c63974c8042c9fStephen Hines
1229b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
1239b044ec938fd56355012851890c63974c8042c9fStephen Hines  switch (Ordering) {
1249b044ec938fd56355012851890c63974c8042c9fStephen Hines  default: llvm_unreachable("Unknown atomic ordering");
1259b044ec938fd56355012851890c63974c8042c9fStephen Hines  case NotAtomic: return bitc::ORDERING_NOTATOMIC;
1269b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Unordered: return bitc::ORDERING_UNORDERED;
1279b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Monotonic: return bitc::ORDERING_MONOTONIC;
1289b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Acquire: return bitc::ORDERING_ACQUIRE;
1299b044ec938fd56355012851890c63974c8042c9fStephen Hines  case Release: return bitc::ORDERING_RELEASE;
1309b044ec938fd56355012851890c63974c8042c9fStephen Hines  case AcquireRelease: return bitc::ORDERING_ACQREL;
1319b044ec938fd56355012851890c63974c8042c9fStephen Hines  case SequentiallyConsistent: return bitc::ORDERING_SEQCST;
1329b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1339b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1349b044ec938fd56355012851890c63974c8042c9fStephen Hines
1359b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
1369b044ec938fd56355012851890c63974c8042c9fStephen Hines  switch (SynchScope) {
1379b044ec938fd56355012851890c63974c8042c9fStephen Hines  default: llvm_unreachable("Unknown synchronization scope");
1389b044ec938fd56355012851890c63974c8042c9fStephen Hines  case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
1399b044ec938fd56355012851890c63974c8042c9fStephen Hines  case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
1409b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1419b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1429b044ec938fd56355012851890c63974c8042c9fStephen Hines
1439b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteStringRecord(unsigned Code, StringRef Str,
1449b044ec938fd56355012851890c63974c8042c9fStephen Hines                              unsigned AbbrevToUse, BitstreamWriter &Stream) {
1459b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<unsigned, 64> Vals;
1469b044ec938fd56355012851890c63974c8042c9fStephen Hines
1479b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Code: [strchar x N]
1489b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1499b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
1509b044ec938fd56355012851890c63974c8042c9fStephen Hines      AbbrevToUse = 0;
1519b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.push_back(Str[i]);
1529b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1539b044ec938fd56355012851890c63974c8042c9fStephen Hines
1549b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit the finished record.
1559b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EmitRecord(Code, Vals, AbbrevToUse);
1569b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1579b044ec938fd56355012851890c63974c8042c9fStephen Hines
1589b044ec938fd56355012851890c63974c8042c9fStephen Hines// Emit information about parameter attributes.
1599937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteAttributeTable(const llvm_2_9_func::ValueEnumerator &VE,
1609b044ec938fd56355012851890c63974c8042c9fStephen Hines                                BitstreamWriter &Stream) {
16123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines  const std::vector<AttributeSet> &Attrs = VE.getAttributes();
1629b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (Attrs.empty()) return;
1639b044ec938fd56355012851890c63974c8042c9fStephen Hines
1649b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
1659b044ec938fd56355012851890c63974c8042c9fStephen Hines
1669b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> Record;
1679b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
16823c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines    const AttributeSet &A = Attrs[i];
1698bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
1708bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines      Record.push_back(A.getSlotIndex(i));
1718bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines      Record.push_back(encodeLLVMAttributesForBitcode(A, A.getSlotIndex(i)));
1728bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    }
1739b044ec938fd56355012851890c63974c8042c9fStephen Hines
1748bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    // This needs to use the 3.2 entry type
1758bb06b7c40b14ce2dae414b0aa034e9b23b278c9Stephen Hines    Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY_OLD, Record);
1769b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.clear();
1779b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
1789b044ec938fd56355012851890c63974c8042c9fStephen Hines
1799b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.ExitBlock();
1809b044ec938fd56355012851890c63974c8042c9fStephen Hines}
1819b044ec938fd56355012851890c63974c8042c9fStephen Hines
1829b044ec938fd56355012851890c63974c8042c9fStephen Hines/// WriteTypeTable - Write out the type table for a module.
1839937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteTypeTable(const llvm_2_9_func::ValueEnumerator &VE,
1849937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                           BitstreamWriter &Stream) {
1859937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9_func::ValueEnumerator::TypeList &TypeList = VE.getTypes();
1869b044ec938fd56355012851890c63974c8042c9fStephen Hines
1879b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
1889b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> TypeVals;
1899b044ec938fd56355012851890c63974c8042c9fStephen Hines
190c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
191c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
1929b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_POINTER.
1939b044ec938fd56355012851890c63974c8042c9fStephen Hines  BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1949b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
195c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
1969b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
1979b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
1989b044ec938fd56355012851890c63974c8042c9fStephen Hines
1999b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_FUNCTION.
2009b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv = new BitCodeAbbrev();
2019b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION_OLD));
2029b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
2039b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(0));  // FIXME: DEAD value, remove in LLVM 3.0
2049b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
205c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2069b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
2079b044ec938fd56355012851890c63974c8042c9fStephen Hines
2089b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_STRUCT_ANON.
2099b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv = new BitCodeAbbrev();
2109b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
2119b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
2129b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
213c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2149b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
2159b044ec938fd56355012851890c63974c8042c9fStephen Hines
2169b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_STRUCT_NAME.
2179b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv = new BitCodeAbbrev();
2189b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
2199b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2209b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2219b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
2229b044ec938fd56355012851890c63974c8042c9fStephen Hines
2239b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_STRUCT_NAMED.
2249b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv = new BitCodeAbbrev();
2259b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
2269b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
2279b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
228c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
2299b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
23023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
2319b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Abbrev for TYPE_CODE_ARRAY.
2329b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv = new BitCodeAbbrev();
2339b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
2349b044ec938fd56355012851890c63974c8042c9fStephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
235c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
236c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
2379b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
2389b044ec938fd56355012851890c63974c8042c9fStephen Hines
2399b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit an entry count so the reader can reserve space.
2409b044ec938fd56355012851890c63974c8042c9fStephen Hines  TypeVals.push_back(TypeList.size());
2419b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
2429b044ec938fd56355012851890c63974c8042c9fStephen Hines  TypeVals.clear();
2439b044ec938fd56355012851890c63974c8042c9fStephen Hines
2449b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Loop over all of the types, emitting each in turn.
2459b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
2469b044ec938fd56355012851890c63974c8042c9fStephen Hines    Type *T = TypeList[i];
2479b044ec938fd56355012851890c63974c8042c9fStephen Hines    int AbbrevToUse = 0;
2489b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned Code = 0;
2499b044ec938fd56355012851890c63974c8042c9fStephen Hines
2509b044ec938fd56355012851890c63974c8042c9fStephen Hines    switch (T->getTypeID()) {
2519b044ec938fd56355012851890c63974c8042c9fStephen Hines    default: llvm_unreachable("Unknown type!");
2529b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;   break;
2539b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;  break;
2549b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE; break;
2559b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80; break;
2569b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128; break;
2579b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
2589b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;  break;
2599b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA; break;
2609b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX; break;
2619b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::IntegerTyID:
2629b044ec938fd56355012851890c63974c8042c9fStephen Hines      // INTEGER: [width]
2639b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::TYPE_CODE_INTEGER;
2649b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
2659b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
2669b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::PointerTyID: {
2679b044ec938fd56355012851890c63974c8042c9fStephen Hines      PointerType *PTy = cast<PointerType>(T);
2689b044ec938fd56355012851890c63974c8042c9fStephen Hines      // POINTER: [pointee type, address space]
2699b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::TYPE_CODE_POINTER;
2709b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
2719b044ec938fd56355012851890c63974c8042c9fStephen Hines      unsigned AddressSpace = PTy->getAddressSpace();
2729b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(AddressSpace);
2739b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
2749b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
2759b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
2769b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::FunctionTyID: {
2779b044ec938fd56355012851890c63974c8042c9fStephen Hines      FunctionType *FT = cast<FunctionType>(T);
2789b044ec938fd56355012851890c63974c8042c9fStephen Hines      // FUNCTION: [isvararg, attrid, retty, paramty x N]
2799b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::TYPE_CODE_FUNCTION_OLD;
2809b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(FT->isVarArg());
2819b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(0);  // FIXME: DEAD: remove in llvm 3.0
2829b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
2839b044ec938fd56355012851890c63974c8042c9fStephen Hines      for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
2849b044ec938fd56355012851890c63974c8042c9fStephen Hines        TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
2859b044ec938fd56355012851890c63974c8042c9fStephen Hines      AbbrevToUse = FunctionAbbrev;
2869b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
2879b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
2889b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::StructTyID: {
2899b044ec938fd56355012851890c63974c8042c9fStephen Hines      StructType *ST = cast<StructType>(T);
2909b044ec938fd56355012851890c63974c8042c9fStephen Hines      // STRUCT: [ispacked, eltty x N]
2919b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(ST->isPacked());
2929b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Output all of the element types.
2939b044ec938fd56355012851890c63974c8042c9fStephen Hines      for (StructType::element_iterator I = ST->element_begin(),
2949b044ec938fd56355012851890c63974c8042c9fStephen Hines           E = ST->element_end(); I != E; ++I)
2959b044ec938fd56355012851890c63974c8042c9fStephen Hines        TypeVals.push_back(VE.getTypeID(*I));
296c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
2979b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (ST->isLiteral()) {
2989b044ec938fd56355012851890c63974c8042c9fStephen Hines        Code = bitc::TYPE_CODE_STRUCT_ANON;
2999b044ec938fd56355012851890c63974c8042c9fStephen Hines        AbbrevToUse = StructAnonAbbrev;
3009b044ec938fd56355012851890c63974c8042c9fStephen Hines      } else {
3019b044ec938fd56355012851890c63974c8042c9fStephen Hines        if (ST->isOpaque()) {
3029b044ec938fd56355012851890c63974c8042c9fStephen Hines          Code = bitc::TYPE_CODE_OPAQUE;
3039b044ec938fd56355012851890c63974c8042c9fStephen Hines        } else {
3049b044ec938fd56355012851890c63974c8042c9fStephen Hines          Code = bitc::TYPE_CODE_STRUCT_NAMED;
3059b044ec938fd56355012851890c63974c8042c9fStephen Hines          AbbrevToUse = StructNamedAbbrev;
3069b044ec938fd56355012851890c63974c8042c9fStephen Hines        }
3079b044ec938fd56355012851890c63974c8042c9fStephen Hines
3089b044ec938fd56355012851890c63974c8042c9fStephen Hines        // Emit the name if it is present.
3099b044ec938fd56355012851890c63974c8042c9fStephen Hines        if (!ST->getName().empty())
3109b044ec938fd56355012851890c63974c8042c9fStephen Hines          WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
3119b044ec938fd56355012851890c63974c8042c9fStephen Hines                            StructNameAbbrev, Stream);
3129b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
3139b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
3149b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
3159b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::ArrayTyID: {
3169b044ec938fd56355012851890c63974c8042c9fStephen Hines      ArrayType *AT = cast<ArrayType>(T);
3179b044ec938fd56355012851890c63974c8042c9fStephen Hines      // ARRAY: [numelts, eltty]
3189b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::TYPE_CODE_ARRAY;
3199b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(AT->getNumElements());
3209b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(VE.getTypeID(AT->getElementType()));
3219b044ec938fd56355012851890c63974c8042c9fStephen Hines      AbbrevToUse = ArrayAbbrev;
3229b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
3239b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
3249b044ec938fd56355012851890c63974c8042c9fStephen Hines    case Type::VectorTyID: {
3259b044ec938fd56355012851890c63974c8042c9fStephen Hines      VectorType *VT = cast<VectorType>(T);
3269b044ec938fd56355012851890c63974c8042c9fStephen Hines      // VECTOR [numelts, eltty]
3279b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::TYPE_CODE_VECTOR;
3289b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(VT->getNumElements());
3299b044ec938fd56355012851890c63974c8042c9fStephen Hines      TypeVals.push_back(VE.getTypeID(VT->getElementType()));
3309b044ec938fd56355012851890c63974c8042c9fStephen Hines      break;
3319b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
3329b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
3339b044ec938fd56355012851890c63974c8042c9fStephen Hines
3349b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Emit the finished record.
3359b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
3369b044ec938fd56355012851890c63974c8042c9fStephen Hines    TypeVals.clear();
3379b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
3389b044ec938fd56355012851890c63974c8042c9fStephen Hines
3399b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.ExitBlock();
3409b044ec938fd56355012851890c63974c8042c9fStephen Hines}
3419b044ec938fd56355012851890c63974c8042c9fStephen Hines
342c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic unsigned getEncodedLinkage(const GlobalValue &GV) {
343c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  switch (GV.getLinkage()) {
344c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::ExternalLinkage:
345c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 0;
346c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::WeakAnyLinkage:
347c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 1;
348c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::AppendingLinkage:
349c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 2;
350c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::InternalLinkage:
351c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 3;
352c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::LinkOnceAnyLinkage:
353c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 4;
354c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::ExternalWeakLinkage:
355c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 7;
356c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::CommonLinkage:
357c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 8;
358c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::PrivateLinkage:
359c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 9;
360c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::WeakODRLinkage:
361c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 10;
362c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::LinkOnceODRLinkage:
363c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 11;
364c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case GlobalValue::AvailableExternallyLinkage:
365c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return 12;
3669b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
367376dc03c6bb42050ddd8a56d1a3e3c6f5d6f6e1dStephen Hines  llvm_unreachable("Invalid linkage");
3689b044ec938fd56355012851890c63974c8042c9fStephen Hines}
3699b044ec938fd56355012851890c63974c8042c9fStephen Hines
370c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic unsigned getEncodedVisibility(const GlobalValue &GV) {
371c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  switch (GV.getVisibility()) {
3729b044ec938fd56355012851890c63974c8042c9fStephen Hines  case GlobalValue::DefaultVisibility:   return 0;
3739b044ec938fd56355012851890c63974c8042c9fStephen Hines  case GlobalValue::HiddenVisibility:    return 1;
3749b044ec938fd56355012851890c63974c8042c9fStephen Hines  case GlobalValue::ProtectedVisibility: return 2;
3759b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
376c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  llvm_unreachable("Invalid visibility");
3779b044ec938fd56355012851890c63974c8042c9fStephen Hines}
3789b044ec938fd56355012851890c63974c8042c9fStephen Hines
3799b044ec938fd56355012851890c63974c8042c9fStephen Hines// Emit top-level description of module, including target triple, inline asm,
3809b044ec938fd56355012851890c63974c8042c9fStephen Hines// descriptors for global variables, and function prototype info.
3819937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hinesstatic void WriteModuleInfo(const Module *M,
3829937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                            const llvm_2_9_func::ValueEnumerator &VE,
3839b044ec938fd56355012851890c63974c8042c9fStephen Hines                            BitstreamWriter &Stream) {
3849b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit various pieces of data attached to a module.
3859b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (!M->getTargetTriple().empty())
3869b044ec938fd56355012851890c63974c8042c9fStephen Hines    WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
3879b044ec938fd56355012851890c63974c8042c9fStephen Hines                      0/*TODO*/, Stream);
388c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const std::string &DL = M->getDataLayoutStr();
389c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (!DL.empty())
390c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
3919b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (!M->getModuleInlineAsm().empty())
3929b044ec938fd56355012851890c63974c8042c9fStephen Hines    WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
3939b044ec938fd56355012851890c63974c8042c9fStephen Hines                      0/*TODO*/, Stream);
3949b044ec938fd56355012851890c63974c8042c9fStephen Hines
3959b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit information about sections and GC, computing how many there are. Also
3969b044ec938fd56355012851890c63974c8042c9fStephen Hines  // compute the maximum alignment value.
3979b044ec938fd56355012851890c63974c8042c9fStephen Hines  std::map<std::string, unsigned> SectionMap;
3989b044ec938fd56355012851890c63974c8042c9fStephen Hines  std::map<std::string, unsigned> GCMap;
3999b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned MaxAlignment = 0;
4009b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned MaxGlobalType = 0;
401c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalValue &GV : M->globals()) {
402c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
403c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
404c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (GV.hasSection()) {
405c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      // Give section names unique ID's.
406c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = SectionMap[GV.getSection()];
407c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      if (!Entry) {
408c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
409c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                          0/*TODO*/, Stream);
410c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        Entry = SectionMap.size();
411c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      }
412c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
4139b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
414c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Function &F : *M) {
415c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MaxAlignment = std::max(MaxAlignment, F.getAlignment());
416c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (F.hasSection()) {
4179b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Give section names unique ID's.
418c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = SectionMap[F.getSection()];
4199b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (!Entry) {
420c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
4219b044ec938fd56355012851890c63974c8042c9fStephen Hines                          0/*TODO*/, Stream);
4229b044ec938fd56355012851890c63974c8042c9fStephen Hines        Entry = SectionMap.size();
4239b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
4249b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
425c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (F.hasGC()) {
4269b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Same for GC names.
427c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      unsigned &Entry = GCMap[F.getGC()];
4289b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (!Entry) {
429c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
4309b044ec938fd56355012851890c63974c8042c9fStephen Hines                          0/*TODO*/, Stream);
4319b044ec938fd56355012851890c63974c8042c9fStephen Hines        Entry = GCMap.size();
4329b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
4339b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
4349b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
4359b044ec938fd56355012851890c63974c8042c9fStephen Hines
4369b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit abbrev for globals, now that we know # sections and max alignment.
4379b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned SimpleGVarAbbrev = 0;
4389b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (!M->global_empty()) {
4399b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Add an abbrev for common globals with no visibility or thread localness.
4409b044ec938fd56355012851890c63974c8042c9fStephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
4419b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
4429b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4439b044ec938fd56355012851890c63974c8042c9fStephen Hines                              Log2_32_Ceil(MaxGlobalType+1)));
4449b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
4459b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
4469b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));      // Linkage.
4479b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (MaxAlignment == 0)                                      // Alignment.
4489b044ec938fd56355012851890c63974c8042c9fStephen Hines      Abbv->Add(BitCodeAbbrevOp(0));
4499b044ec938fd56355012851890c63974c8042c9fStephen Hines    else {
4509b044ec938fd56355012851890c63974c8042c9fStephen Hines      unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
4519b044ec938fd56355012851890c63974c8042c9fStephen Hines      Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4529b044ec938fd56355012851890c63974c8042c9fStephen Hines                               Log2_32_Ceil(MaxEncAlignment+1)));
4539b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
4549b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (SectionMap.empty())                                    // Section.
4559b044ec938fd56355012851890c63974c8042c9fStephen Hines      Abbv->Add(BitCodeAbbrevOp(0));
4569b044ec938fd56355012851890c63974c8042c9fStephen Hines    else
4579b044ec938fd56355012851890c63974c8042c9fStephen Hines      Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
4589b044ec938fd56355012851890c63974c8042c9fStephen Hines                               Log2_32_Ceil(SectionMap.size()+1)));
4599b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Don't bother emitting vis + thread local.
4609b044ec938fd56355012851890c63974c8042c9fStephen Hines    SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
4619b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
4629b044ec938fd56355012851890c63974c8042c9fStephen Hines
4639b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit the global variable information.
4649b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<unsigned, 64> Vals;
465c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalVariable &GV : M->globals()) {
4669b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned AbbrevToUse = 0;
4679b044ec938fd56355012851890c63974c8042c9fStephen Hines
4689b044ec938fd56355012851890c63974c8042c9fStephen Hines    // GLOBALVAR: [type, isconst, initid,
4699b044ec938fd56355012851890c63974c8042c9fStephen Hines    //             linkage, alignment, section, visibility, threadlocal,
4709b044ec938fd56355012851890c63974c8042c9fStephen Hines    //             unnamed_addr]
471c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(GV.getType()));
472c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.isConstant());
473c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.isDeclaration() ? 0 :
474c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                   (VE.getValueID(GV.getInitializer()) + 1));
4759b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.push_back(getEncodedLinkage(GV));
476c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(Log2_32(GV.getAlignment())+1);
477c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
478c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (GV.isThreadLocal() ||
479c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        GV.getVisibility() != GlobalValue::DefaultVisibility ||
480c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        GV.hasUnnamedAddr()) {
4819b044ec938fd56355012851890c63974c8042c9fStephen Hines      Vals.push_back(getEncodedVisibility(GV));
482c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Vals.push_back(GV.isThreadLocal());
483c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Vals.push_back(GV.hasUnnamedAddr());
4849b044ec938fd56355012851890c63974c8042c9fStephen Hines    } else {
4859b044ec938fd56355012851890c63974c8042c9fStephen Hines      AbbrevToUse = SimpleGVarAbbrev;
4869b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
4879b044ec938fd56355012851890c63974c8042c9fStephen Hines
4889b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
4899b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.clear();
4909b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
4919b044ec938fd56355012851890c63974c8042c9fStephen Hines
4929b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit the function proto information.
493c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Function &F : *M) {
4949b044ec938fd56355012851890c63974c8042c9fStephen Hines    // FUNCTION:  [type, callingconv, isproto, paramattr,
4959b044ec938fd56355012851890c63974c8042c9fStephen Hines    //             linkage, alignment, section, visibility, gc, unnamed_addr]
496c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(F.getType()));
497c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.getCallingConv());
498c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.isDeclaration());
4999b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.push_back(getEncodedLinkage(F));
500c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getAttributeID(F.getAttributes()));
501c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(Log2_32(F.getAlignment())+1);
502c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
5039b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.push_back(getEncodedVisibility(F));
504c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
505c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(F.hasUnnamedAddr());
5069b044ec938fd56355012851890c63974c8042c9fStephen Hines
5079b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned AbbrevToUse = 0;
5089b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
5099b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.clear();
5109b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
5119b044ec938fd56355012851890c63974c8042c9fStephen Hines
5129b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Emit the alias information.
513c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const GlobalAlias &A : M->aliases()) {
514c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getTypeID(A.getType()));
515c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(VE.getValueID(A.getAliasee()));
516c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(getEncodedLinkage(A));
517c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Vals.push_back(getEncodedVisibility(A));
5189b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned AbbrevToUse = 0;
5198f093e05e28046b6fc74175b66a06152f72e0c66Pirama Arumuga Nainar    Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
5209b044ec938fd56355012851890c63974c8042c9fStephen Hines    Vals.clear();
5219b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
5229b044ec938fd56355012851890c63974c8042c9fStephen Hines}
5239b044ec938fd56355012851890c63974c8042c9fStephen Hines
5249b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic uint64_t GetOptimizationFlags(const Value *V) {
5259b044ec938fd56355012851890c63974c8042c9fStephen Hines  uint64_t Flags = 0;
5269b044ec938fd56355012851890c63974c8042c9fStephen Hines
527c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
5289b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (OBO->hasNoSignedWrap())
5299b044ec938fd56355012851890c63974c8042c9fStephen Hines      Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
5309b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (OBO->hasNoUnsignedWrap())
5319b044ec938fd56355012851890c63974c8042c9fStephen Hines      Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
532c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
5339b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (PEO->isExact())
5349b044ec938fd56355012851890c63974c8042c9fStephen Hines      Flags |= 1 << bitc::PEO_EXACT;
5359b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
5369b044ec938fd56355012851890c63974c8042c9fStephen Hines
5379b044ec938fd56355012851890c63974c8042c9fStephen Hines  return Flags;
5389b044ec938fd56355012851890c63974c8042c9fStephen Hines}
5399b044ec938fd56355012851890c63974c8042c9fStephen Hines
540c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteValueAsMetadata(const ValueAsMetadata *MD,
541c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 const llvm_2_9_func::ValueEnumerator &VE,
542c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 BitstreamWriter &Stream,
543c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                 SmallVectorImpl<uint64_t> &Record) {
544c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  // Mimic an MDNode with a value as one operand.
545c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Value *V = MD->getValue();
546c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getTypeID(V->getType()));
547c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getValueID(V));
548c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(bitc::METADATA_OLD_NODE, Record, 0);
549c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.clear();
550c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}
551c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
552c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteMDTuple(const MDTuple *N, const llvm_2_9_func::ValueEnumerator &VE,
553c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                         BitstreamWriter &Stream,
554c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                         SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
5559b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
556c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Metadata *MD = N->getOperand(i);
557c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    assert(!(MD && isa<LocalAsMetadata>(MD)) &&
558c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines           "Unexpected function-local metadata");
559c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (!MD) {
560c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      // TODO(srhines): I don't believe this case can exist for RS.
561c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(llvm::Type::getVoidTy(N->getContext())));
5629b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.push_back(0);
563c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    } else if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
564c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(MDC->getType()));
565c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getValueID(MDC->getValue()));
566c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    } else {
567c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getTypeID(
568c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines          llvm::Type::getMetadataTy(N->getContext())));
569c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getMetadataID(MD));
5709b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
5719b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
572c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(bitc::METADATA_OLD_NODE, Record, Abbrev);
5739b044ec938fd56355012851890c63974c8042c9fStephen Hines  Record.clear();
5749b044ec938fd56355012851890c63974c8042c9fStephen Hines}
5759b044ec938fd56355012851890c63974c8042c9fStephen Hines
576c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines/*static void WriteMDLocation(const MDLocation *N, const llvm_2_9_func::ValueEnumerator &VE,
577c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            BitstreamWriter &Stream,
578c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            SmallVectorImpl<uint64_t> &Record,
579c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                            unsigned Abbrev) {
580c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->isDistinct());
581c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->getLine());
582c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(N->getColumn());
583c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getMetadataID(N->getScope()));
584c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
585c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
586c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
587c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Record.clear();
588c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}
589c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
590c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hinesstatic void WriteGenericDebugNode(const GenericDebugNode *,
591c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                  const llvm_2_9_func::ValueEnumerator &, BitstreamWriter &,
592c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines                                  SmallVectorImpl<uint64_t> &, unsigned) {
593c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  llvm_unreachable("unimplemented");
594c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines}*/
595c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
5969b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteModuleMetadata(const Module *M,
5979937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                const llvm_2_9_func::ValueEnumerator &VE,
5989b044ec938fd56355012851890c63974c8042c9fStephen Hines                                BitstreamWriter &Stream) {
599c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const auto &MDs = VE.getMDs();
600c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (MDs.empty() && M->named_metadata_empty())
601c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    return;
602c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
603c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  // RenderScript files *ALWAYS* have metadata!
604c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
605c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
6069b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned MDSAbbrev = 0;
607c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (VE.hasMDString()) {
608c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_STRING.
609c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
610c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
611c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
612c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
613c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MDSAbbrev = Stream.EmitAbbrev(Abbv);
614c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6159b044ec938fd56355012851890c63974c8042c9fStephen Hines
616c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned MDLocationAbbrev = 0;
6171906a00dce8e32fe3bb8a957e333ebbbee0888e3Pirama Arumuga Nainar  if (VE.hasDILocation()) {
618c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // TODO(srhines): Should be unreachable for RenderScript.
619c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_LOCATION.
620c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    //
621c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Assume the column is usually under 128, and always output the inlined-at
622c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // location (it's never more expensive than building an array size 1).
623c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
624c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
625c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
626c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
627c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
628c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
629c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
630c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    MDLocationAbbrev = Stream.EmitAbbrev(Abbv);
631c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6329b044ec938fd56355012851890c63974c8042c9fStephen Hines
633c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned NameAbbrev = 0;
634c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  if (!M->named_metadata_empty()) {
635c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Abbrev for METADATA_NAME.
636c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
637c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
638c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
639c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
640c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    NameAbbrev = Stream.EmitAbbrev(Abbv);
641c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
6429b044ec938fd56355012851890c63974c8042c9fStephen Hines
643c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  unsigned MDTupleAbbrev = 0;
644c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  //unsigned GenericDebugNodeAbbrev = 0;
645c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  SmallVector<uint64_t, 64> Record;
646c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const Metadata *MD : MDs) {
647c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (const MDNode *N = dyn_cast<MDNode>(MD)) {
648c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      switch (N->getMetadataID()) {
649c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      default:
650c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        llvm_unreachable("Invalid MDNode subclass");
651c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)
652c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#define HANDLE_MDNODE_LEAF(CLASS)                                              \
653c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  case Metadata::CLASS##Kind:                                                  \
654c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev);           \
655c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    continue;
656c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines#include "llvm/IR/Metadata.def"
657c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      }
6589b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
659c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
660c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      WriteValueAsMetadata(MDC, VE, Stream, Record);
661c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      continue;
662c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
663c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    const MDString *MDS = cast<MDString>(MD);
664c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Code: [strchar x N]
665c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.append(MDS->bytes_begin(), MDS->bytes_end());
666c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines
667c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    // Emit the finished record.
668c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
669c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.clear();
6709b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
6719b044ec938fd56355012851890c63974c8042c9fStephen Hines
6729b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Write named metadata.
673c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (const NamedMDNode &NMD : M->named_metadata()) {
6749b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Write name.
675c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    StringRef Str = NMD.getName();
676c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Record.append(Str.bytes_begin(), Str.bytes_end());
677c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
6789b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.clear();
6799b044ec938fd56355012851890c63974c8042c9fStephen Hines
6809b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Write named metadata operands.
681c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    for (const MDNode *N : NMD.operands())
682c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Record.push_back(VE.getMetadataID(N));
6839b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
6849b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.clear();
6859b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
6869b044ec938fd56355012851890c63974c8042c9fStephen Hines
687c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Stream.ExitBlock();
6889b044ec938fd56355012851890c63974c8042c9fStephen Hines}
6899b044ec938fd56355012851890c63974c8042c9fStephen Hines
6909b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteFunctionLocalMetadata(const Function &F,
6919937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                       const llvm_2_9_func::ValueEnumerator &VE,
6929b044ec938fd56355012851890c63974c8042c9fStephen Hines                                       BitstreamWriter &Stream) {
6939b044ec938fd56355012851890c63974c8042c9fStephen Hines  bool StartedMetadataBlock = false;
6949b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> Record;
695c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  const SmallVectorImpl<const LocalAsMetadata *> &MDs =
696c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      VE.getFunctionLocalMDs();
697c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
698c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    assert(MDs[i] && "Expected valid function-local metadata");
699c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    if (!StartedMetadataBlock) {
700c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
701c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines      StartedMetadataBlock = true;
702c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    }
703c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines    WriteValueAsMetadata(MDs[i], VE, Stream, Record);
704c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  }
70523c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7069b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (StartedMetadataBlock)
7079b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.ExitBlock();
7089b044ec938fd56355012851890c63974c8042c9fStephen Hines}
7099b044ec938fd56355012851890c63974c8042c9fStephen Hines
7109b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteMetadataAttachment(const Function &F,
7119937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                                    const llvm_2_9_func::ValueEnumerator &VE,
7129b044ec938fd56355012851890c63974c8042c9fStephen Hines                                    BitstreamWriter &Stream) {
7139b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
7149b044ec938fd56355012851890c63974c8042c9fStephen Hines
7159b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> Record;
7169b044ec938fd56355012851890c63974c8042c9fStephen Hines
7179b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Write metadata attachments
7189b044ec938fd56355012851890c63974c8042c9fStephen Hines  // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
719c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
72023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7219b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
7229b044ec938fd56355012851890c63974c8042c9fStephen Hines    for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
7239b044ec938fd56355012851890c63974c8042c9fStephen Hines         I != E; ++I) {
7249b044ec938fd56355012851890c63974c8042c9fStephen Hines      MDs.clear();
7259b044ec938fd56355012851890c63974c8042c9fStephen Hines      I->getAllMetadataOtherThanDebugLoc(MDs);
72623c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7279b044ec938fd56355012851890c63974c8042c9fStephen Hines      // If no metadata, ignore instruction.
7289b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (MDs.empty()) continue;
7299b044ec938fd56355012851890c63974c8042c9fStephen Hines
7308f093e05e28046b6fc74175b66a06152f72e0c66Pirama Arumuga Nainar      Record.push_back(VE.getInstructionID(&*I));
73123c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7329b044ec938fd56355012851890c63974c8042c9fStephen Hines      for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
7339b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(MDs[i].first);
734c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines        Record.push_back(VE.getMetadataID(MDs[i].second));
7359b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
7369b044ec938fd56355012851890c63974c8042c9fStephen Hines      Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
7379b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.clear();
7389b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
7399b044ec938fd56355012851890c63974c8042c9fStephen Hines
7409b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.ExitBlock();
7419b044ec938fd56355012851890c63974c8042c9fStephen Hines}
7429b044ec938fd56355012851890c63974c8042c9fStephen Hines
7439b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
7449b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> Record;
7459b044ec938fd56355012851890c63974c8042c9fStephen Hines
7469b044ec938fd56355012851890c63974c8042c9fStephen Hines  // Write metadata kinds
7479b044ec938fd56355012851890c63974c8042c9fStephen Hines  // METADATA_KIND - [n x [id, name]]
7489b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<StringRef, 4> Names;
7499b044ec938fd56355012851890c63974c8042c9fStephen Hines  M->getMDKindNames(Names);
75023c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7519b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (Names.empty()) return;
7529b044ec938fd56355012851890c63974c8042c9fStephen Hines
7539b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
75423c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7559b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
7569b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.push_back(MDKindID);
7579b044ec938fd56355012851890c63974c8042c9fStephen Hines    StringRef KName = Names[MDKindID];
7589b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.append(KName.begin(), KName.end());
75923c4358f12bd9d0ba7166eceebd683db95a41b3fStephen Hines
7609b044ec938fd56355012851890c63974c8042c9fStephen Hines    Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
7619b044ec938fd56355012851890c63974c8042c9fStephen Hines    Record.clear();
7629b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
7639b044ec938fd56355012851890c63974c8042c9fStephen Hines
7649b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.ExitBlock();
7659b044ec938fd56355012851890c63974c8042c9fStephen Hines}
7669b044ec938fd56355012851890c63974c8042c9fStephen Hines
7679b044ec938fd56355012851890c63974c8042c9fStephen Hinesstatic void WriteConstants(unsigned FirstVal, unsigned LastVal,
7689937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines                           const llvm_2_9_func::ValueEnumerator &VE,
7699b044ec938fd56355012851890c63974c8042c9fStephen Hines                           BitstreamWriter &Stream, bool isGlobal) {
7709b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (FirstVal == LastVal) return;
7719b044ec938fd56355012851890c63974c8042c9fStephen Hines
7729b044ec938fd56355012851890c63974c8042c9fStephen Hines  Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
7739b044ec938fd56355012851890c63974c8042c9fStephen Hines
7749b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned AggregateAbbrev = 0;
7759b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned String8Abbrev = 0;
7769b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned CString7Abbrev = 0;
7779b044ec938fd56355012851890c63974c8042c9fStephen Hines  unsigned CString6Abbrev = 0;
7789b044ec938fd56355012851890c63974c8042c9fStephen Hines  // If this is a constant pool for the module, emit module-specific abbrevs.
7799b044ec938fd56355012851890c63974c8042c9fStephen Hines  if (isGlobal) {
7809b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Abbrev for CST_CODE_AGGREGATE.
7819b044ec938fd56355012851890c63974c8042c9fStephen Hines    BitCodeAbbrev *Abbv = new BitCodeAbbrev();
7829b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
7839b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7849b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
7859b044ec938fd56355012851890c63974c8042c9fStephen Hines    AggregateAbbrev = Stream.EmitAbbrev(Abbv);
7869b044ec938fd56355012851890c63974c8042c9fStephen Hines
7879b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Abbrev for CST_CODE_STRING.
7889b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv = new BitCodeAbbrev();
7899b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
7909b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7919b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
7929b044ec938fd56355012851890c63974c8042c9fStephen Hines    String8Abbrev = Stream.EmitAbbrev(Abbv);
7939b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Abbrev for CST_CODE_CSTRING.
7949b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv = new BitCodeAbbrev();
7959b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
7969b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
7979b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
7989b044ec938fd56355012851890c63974c8042c9fStephen Hines    CString7Abbrev = Stream.EmitAbbrev(Abbv);
7999b044ec938fd56355012851890c63974c8042c9fStephen Hines    // Abbrev for CST_CODE_CSTRING.
8009b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv = new BitCodeAbbrev();
8019b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
8029b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
8039b044ec938fd56355012851890c63974c8042c9fStephen Hines    Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
8049b044ec938fd56355012851890c63974c8042c9fStephen Hines    CString6Abbrev = Stream.EmitAbbrev(Abbv);
8059b044ec938fd56355012851890c63974c8042c9fStephen Hines  }
8069b044ec938fd56355012851890c63974c8042c9fStephen Hines
8079b044ec938fd56355012851890c63974c8042c9fStephen Hines  SmallVector<uint64_t, 64> Record;
8089b044ec938fd56355012851890c63974c8042c9fStephen Hines
8099937d116e09feb32d46a4c76eca1be6afcd3bed5Stephen Hines  const llvm_2_9_func::ValueEnumerator::ValueList &Vals = VE.getValues();
810c706907a8041faaa882f9bd87f1d1c1669023a62Stephen Hines  Type *LastTy = nullptr;
8119b044ec938fd56355012851890c63974c8042c9fStephen Hines  for (unsigned i = FirstVal; i != LastVal; ++i) {
8129b044ec938fd56355012851890c63974c8042c9fStephen Hines    const Value *V = Vals[i].first;
8139b044ec938fd56355012851890c63974c8042c9fStephen Hines    // If we need to switch types, do so now.
8149b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (V->getType() != LastTy) {
8159b044ec938fd56355012851890c63974c8042c9fStephen Hines      LastTy = V->getType();
8169b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.push_back(VE.getTypeID(LastTy));
8179b044ec938fd56355012851890c63974c8042c9fStephen Hines      Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
8189b044ec938fd56355012851890c63974c8042c9fStephen Hines                        CONSTANTS_SETTYPE_ABBREV);
8199b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.clear();
8209b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
8219b044ec938fd56355012851890c63974c8042c9fStephen Hines
8229b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
8239b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.push_back(unsigned(IA->hasSideEffects()) |
8249b044ec938fd56355012851890c63974c8042c9fStephen Hines                       unsigned(IA->isAlignStack()) << 1);
8259b044ec938fd56355012851890c63974c8042c9fStephen Hines
8269b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Add the asm string.
8279b044ec938fd56355012851890c63974c8042c9fStephen Hines      const std::string &AsmStr = IA->getAsmString();
8289b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.push_back(AsmStr.size());
8299b044ec938fd56355012851890c63974c8042c9fStephen Hines      for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
8309b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(AsmStr[i]);
8319b044ec938fd56355012851890c63974c8042c9fStephen Hines
8329b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Add the constraint string.
8339b044ec938fd56355012851890c63974c8042c9fStephen Hines      const std::string &ConstraintStr = IA->getConstraintString();
8349b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.push_back(ConstraintStr.size());
8359b044ec938fd56355012851890c63974c8042c9fStephen Hines      for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
8369b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(ConstraintStr[i]);
8379b044ec938fd56355012851890c63974c8042c9fStephen Hines      Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
8389b044ec938fd56355012851890c63974c8042c9fStephen Hines      Record.clear();
8399b044ec938fd56355012851890c63974c8042c9fStephen Hines      continue;
8409b044ec938fd56355012851890c63974c8042c9fStephen Hines    }
8419b044ec938fd56355012851890c63974c8042c9fStephen Hines    const Constant *C = cast<Constant>(V);
8429b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned Code = -1U;
8439b044ec938fd56355012851890c63974c8042c9fStephen Hines    unsigned AbbrevToUse = 0;
8449b044ec938fd56355012851890c63974c8042c9fStephen Hines    if (C->isNullValue()) {
8459b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::CST_CODE_NULL;
8469b044ec938fd56355012851890c63974c8042c9fStephen Hines    } else if (isa<UndefValue>(C)) {
8479b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::CST_CODE_UNDEF;
8489b044ec938fd56355012851890c63974c8042c9fStephen Hines    } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
8499b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (IV->getBitWidth() <= 64) {
8509b044ec938fd56355012851890c63974c8042c9fStephen Hines        uint64_t V = IV->getSExtValue();
8519b044ec938fd56355012851890c63974c8042c9fStephen Hines        if ((int64_t)V >= 0)
8529b044ec938fd56355012851890c63974c8042c9fStephen Hines          Record.push_back(V << 1);
8539b044ec938fd56355012851890c63974c8042c9fStephen Hines        else
8549b044ec938fd56355012851890c63974c8042c9fStephen Hines          Record.push_back((-V << 1) | 1);
8559b044ec938fd56355012851890c63974c8042c9fStephen Hines        Code = bitc::CST_CODE_INTEGER;
8569b044ec938fd56355012851890c63974c8042c9fStephen Hines        AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
8579b044ec938fd56355012851890c63974c8042c9fStephen Hines      } else {                             // Wide integers, > 64 bits in size.
8589b044ec938fd56355012851890c63974c8042c9fStephen Hines        // We have an arbitrary precision integer value to write whose
8599b044ec938fd56355012851890c63974c8042c9fStephen Hines        // bit width is > 64. However, in canonical unsigned integer
8609b044ec938fd56355012851890c63974c8042c9fStephen Hines        // format it is likely that the high bits are going to be zero.
8619b044ec938fd56355012851890c63974c8042c9fStephen Hines        // So, we only write the number of active words.
8629b044ec938fd56355012851890c63974c8042c9fStephen Hines        unsigned NWords = IV->getValue().getActiveWords();
8639b044ec938fd56355012851890c63974c8042c9fStephen Hines        const uint64_t *RawWords = IV->getValue().getRawData();
8649b044ec938fd56355012851890c63974c8042c9fStephen Hines        for (unsigned i = 0; i != NWords; ++i) {
8659b044ec938fd56355012851890c63974c8042c9fStephen Hines          int64_t V = RawWords[i];
8669b044ec938fd56355012851890c63974c8042c9fStephen Hines          if (V >= 0)
8679b044ec938fd56355012851890c63974c8042c9fStephen Hines            Record.push_back(V << 1);
8689b044ec938fd56355012851890c63974c8042c9fStephen Hines          else
8699b044ec938fd56355012851890c63974c8042c9fStephen Hines            Record.push_back((-V << 1) | 1);
8709b044ec938fd56355012851890c63974c8042c9fStephen Hines        }
8719b044ec938fd56355012851890c63974c8042c9fStephen Hines        Code = bitc::CST_CODE_WIDE_INTEGER;
8729b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
8739b044ec938fd56355012851890c63974c8042c9fStephen Hines    } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
8749b044ec938fd56355012851890c63974c8042c9fStephen Hines      Code = bitc::CST_CODE_FLOAT;
8759b044ec938fd56355012851890c63974c8042c9fStephen Hines      Type *Ty = CFP->getType();
8769b044ec938fd56355012851890c63974c8042c9fStephen Hines      if (Ty->isFloatTy() || Ty->isDoubleTy()) {
8779b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
8789b044ec938fd56355012851890c63974c8042c9fStephen Hines      } else if (Ty->isX86_FP80Ty()) {
8799b044ec938fd56355012851890c63974c8042c9fStephen Hines        // api needed to prevent premature destruction
8809b044ec938fd56355012851890c63974c8042c9fStephen Hines        // bits are not in the same order as a normal i80 APInt, compensate.
8819b044ec938fd56355012851890c63974c8042c9fStephen Hines        APInt api = CFP->getValueAPF().bitcastToAPInt();
8829b044ec938fd56355012851890c63974c8042c9fStephen Hines        const uint64_t *p = api.getRawData();
8839b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back((p[1] << 48) | (p[0] >> 16));
8849b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(p[0] & 0xffffLL);
8859b044ec938fd56355012851890c63974c8042c9fStephen Hines      } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
8869b044ec938fd56355012851890c63974c8042c9fStephen Hines        APInt api = CFP->getValueAPF().bitcastToAPInt();
8879b044ec938fd56355012851890c63974c8042c9fStephen Hines        const uint64_t *p = api.getRawData();
8889b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(p[0]);
8899b044ec938fd56355012851890c63974c8042c9fStephen Hines        Record.push_back(p[1]);
8909b044ec938fd56355012851890c63974c8042c9fStephen Hines      } else {
8919b044ec938fd56355012851890c63974c8042c9fStephen Hines        assert (0 && "Unknown FP type!");
8929b044ec938fd56355012851890c63974c8042c9fStephen Hines      }
8930444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines    } else if (isa<ConstantDataSequential>(C) &&
8940444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines               cast<ConstantDataSequential>(C)->isString()) {
8950444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
8969b044ec938fd56355012851890c63974c8042c9fStephen Hines      // Emit constant strings specially.
8970444de0c0e7cfc8d8f8fed6f64cd97812bdd6a41Stephen Hines      unsigned NumElts = Str->getNumElements();
8989b044ec938fd56355012851890c6397