Home | History | Annotate | Download | only in Writer
      1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Bitcode writer implementation.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Bitcode/ReaderWriter.h"
     15 #include "ValueEnumerator.h"
     16 #include "llvm/ADT/Triple.h"
     17 #include "llvm/Bitcode/BitstreamWriter.h"
     18 #include "llvm/Bitcode/LLVMBitCodes.h"
     19 #include "llvm/IR/Constants.h"
     20 #include "llvm/IR/DebugInfoMetadata.h"
     21 #include "llvm/IR/DerivedTypes.h"
     22 #include "llvm/IR/InlineAsm.h"
     23 #include "llvm/IR/Instructions.h"
     24 #include "llvm/IR/Module.h"
     25 #include "llvm/IR/Operator.h"
     26 #include "llvm/IR/UseListOrder.h"
     27 #include "llvm/IR/ValueSymbolTable.h"
     28 #include "llvm/Support/CommandLine.h"
     29 #include "llvm/Support/ErrorHandling.h"
     30 #include "llvm/Support/MathExtras.h"
     31 #include "llvm/Support/Program.h"
     32 #include "llvm/Support/raw_ostream.h"
     33 #include <cctype>
     34 #include <map>
     35 using namespace llvm;
     36 
     37 /// These are manifest constants used by the bitcode writer. They do not need to
     38 /// be kept in sync with the reader, but need to be consistent within this file.
     39 enum {
     40   // VALUE_SYMTAB_BLOCK abbrev id's.
     41   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     42   VST_ENTRY_7_ABBREV,
     43   VST_ENTRY_6_ABBREV,
     44   VST_BBENTRY_6_ABBREV,
     45 
     46   // CONSTANTS_BLOCK abbrev id's.
     47   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     48   CONSTANTS_INTEGER_ABBREV,
     49   CONSTANTS_CE_CAST_Abbrev,
     50   CONSTANTS_NULL_Abbrev,
     51 
     52   // FUNCTION_BLOCK abbrev id's.
     53   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     54   FUNCTION_INST_BINOP_ABBREV,
     55   FUNCTION_INST_BINOP_FLAGS_ABBREV,
     56   FUNCTION_INST_CAST_ABBREV,
     57   FUNCTION_INST_RET_VOID_ABBREV,
     58   FUNCTION_INST_RET_VAL_ABBREV,
     59   FUNCTION_INST_UNREACHABLE_ABBREV,
     60   FUNCTION_INST_GEP_ABBREV,
     61 };
     62 
     63 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
     64   switch (Opcode) {
     65   default: llvm_unreachable("Unknown cast instruction!");
     66   case Instruction::Trunc   : return bitc::CAST_TRUNC;
     67   case Instruction::ZExt    : return bitc::CAST_ZEXT;
     68   case Instruction::SExt    : return bitc::CAST_SEXT;
     69   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
     70   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
     71   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
     72   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
     73   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
     74   case Instruction::FPExt   : return bitc::CAST_FPEXT;
     75   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
     76   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
     77   case Instruction::BitCast : return bitc::CAST_BITCAST;
     78   case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
     79   }
     80 }
     81 
     82 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
     83   switch (Opcode) {
     84   default: llvm_unreachable("Unknown binary instruction!");
     85   case Instruction::Add:
     86   case Instruction::FAdd: return bitc::BINOP_ADD;
     87   case Instruction::Sub:
     88   case Instruction::FSub: return bitc::BINOP_SUB;
     89   case Instruction::Mul:
     90   case Instruction::FMul: return bitc::BINOP_MUL;
     91   case Instruction::UDiv: return bitc::BINOP_UDIV;
     92   case Instruction::FDiv:
     93   case Instruction::SDiv: return bitc::BINOP_SDIV;
     94   case Instruction::URem: return bitc::BINOP_UREM;
     95   case Instruction::FRem:
     96   case Instruction::SRem: return bitc::BINOP_SREM;
     97   case Instruction::Shl:  return bitc::BINOP_SHL;
     98   case Instruction::LShr: return bitc::BINOP_LSHR;
     99   case Instruction::AShr: return bitc::BINOP_ASHR;
    100   case Instruction::And:  return bitc::BINOP_AND;
    101   case Instruction::Or:   return bitc::BINOP_OR;
    102   case Instruction::Xor:  return bitc::BINOP_XOR;
    103   }
    104 }
    105 
    106 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
    107   switch (Op) {
    108   default: llvm_unreachable("Unknown RMW operation!");
    109   case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
    110   case AtomicRMWInst::Add: return bitc::RMW_ADD;
    111   case AtomicRMWInst::Sub: return bitc::RMW_SUB;
    112   case AtomicRMWInst::And: return bitc::RMW_AND;
    113   case AtomicRMWInst::Nand: return bitc::RMW_NAND;
    114   case AtomicRMWInst::Or: return bitc::RMW_OR;
    115   case AtomicRMWInst::Xor: return bitc::RMW_XOR;
    116   case AtomicRMWInst::Max: return bitc::RMW_MAX;
    117   case AtomicRMWInst::Min: return bitc::RMW_MIN;
    118   case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
    119   case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
    120   }
    121 }
    122 
    123 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
    124   switch (Ordering) {
    125   case NotAtomic: return bitc::ORDERING_NOTATOMIC;
    126   case Unordered: return bitc::ORDERING_UNORDERED;
    127   case Monotonic: return bitc::ORDERING_MONOTONIC;
    128   case Acquire: return bitc::ORDERING_ACQUIRE;
    129   case Release: return bitc::ORDERING_RELEASE;
    130   case AcquireRelease: return bitc::ORDERING_ACQREL;
    131   case SequentiallyConsistent: return bitc::ORDERING_SEQCST;
    132   }
    133   llvm_unreachable("Invalid ordering");
    134 }
    135 
    136 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
    137   switch (SynchScope) {
    138   case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
    139   case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
    140   }
    141   llvm_unreachable("Invalid synch scope");
    142 }
    143 
    144 static void WriteStringRecord(unsigned Code, StringRef Str,
    145                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
    146   SmallVector<unsigned, 64> Vals;
    147 
    148   // Code: [strchar x N]
    149   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
    150     if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
    151       AbbrevToUse = 0;
    152     Vals.push_back(Str[i]);
    153   }
    154 
    155   // Emit the finished record.
    156   Stream.EmitRecord(Code, Vals, AbbrevToUse);
    157 }
    158 
    159 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
    160   switch (Kind) {
    161   case Attribute::Alignment:
    162     return bitc::ATTR_KIND_ALIGNMENT;
    163   case Attribute::AlwaysInline:
    164     return bitc::ATTR_KIND_ALWAYS_INLINE;
    165   case Attribute::Builtin:
    166     return bitc::ATTR_KIND_BUILTIN;
    167   case Attribute::ByVal:
    168     return bitc::ATTR_KIND_BY_VAL;
    169   case Attribute::InAlloca:
    170     return bitc::ATTR_KIND_IN_ALLOCA;
    171   case Attribute::Cold:
    172     return bitc::ATTR_KIND_COLD;
    173   case Attribute::InlineHint:
    174     return bitc::ATTR_KIND_INLINE_HINT;
    175   case Attribute::InReg:
    176     return bitc::ATTR_KIND_IN_REG;
    177   case Attribute::JumpTable:
    178     return bitc::ATTR_KIND_JUMP_TABLE;
    179   case Attribute::MinSize:
    180     return bitc::ATTR_KIND_MIN_SIZE;
    181   case Attribute::Naked:
    182     return bitc::ATTR_KIND_NAKED;
    183   case Attribute::Nest:
    184     return bitc::ATTR_KIND_NEST;
    185   case Attribute::NoAlias:
    186     return bitc::ATTR_KIND_NO_ALIAS;
    187   case Attribute::NoBuiltin:
    188     return bitc::ATTR_KIND_NO_BUILTIN;
    189   case Attribute::NoCapture:
    190     return bitc::ATTR_KIND_NO_CAPTURE;
    191   case Attribute::NoDuplicate:
    192     return bitc::ATTR_KIND_NO_DUPLICATE;
    193   case Attribute::NoImplicitFloat:
    194     return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
    195   case Attribute::NoInline:
    196     return bitc::ATTR_KIND_NO_INLINE;
    197   case Attribute::NonLazyBind:
    198     return bitc::ATTR_KIND_NON_LAZY_BIND;
    199   case Attribute::NonNull:
    200     return bitc::ATTR_KIND_NON_NULL;
    201   case Attribute::Dereferenceable:
    202     return bitc::ATTR_KIND_DEREFERENCEABLE;
    203   case Attribute::DereferenceableOrNull:
    204     return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
    205   case Attribute::NoRedZone:
    206     return bitc::ATTR_KIND_NO_RED_ZONE;
    207   case Attribute::NoReturn:
    208     return bitc::ATTR_KIND_NO_RETURN;
    209   case Attribute::NoUnwind:
    210     return bitc::ATTR_KIND_NO_UNWIND;
    211   case Attribute::OptimizeForSize:
    212     return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
    213   case Attribute::OptimizeNone:
    214     return bitc::ATTR_KIND_OPTIMIZE_NONE;
    215   case Attribute::ReadNone:
    216     return bitc::ATTR_KIND_READ_NONE;
    217   case Attribute::ReadOnly:
    218     return bitc::ATTR_KIND_READ_ONLY;
    219   case Attribute::Returned:
    220     return bitc::ATTR_KIND_RETURNED;
    221   case Attribute::ReturnsTwice:
    222     return bitc::ATTR_KIND_RETURNS_TWICE;
    223   case Attribute::SExt:
    224     return bitc::ATTR_KIND_S_EXT;
    225   case Attribute::StackAlignment:
    226     return bitc::ATTR_KIND_STACK_ALIGNMENT;
    227   case Attribute::StackProtect:
    228     return bitc::ATTR_KIND_STACK_PROTECT;
    229   case Attribute::StackProtectReq:
    230     return bitc::ATTR_KIND_STACK_PROTECT_REQ;
    231   case Attribute::StackProtectStrong:
    232     return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
    233   case Attribute::StructRet:
    234     return bitc::ATTR_KIND_STRUCT_RET;
    235   case Attribute::SanitizeAddress:
    236     return bitc::ATTR_KIND_SANITIZE_ADDRESS;
    237   case Attribute::SanitizeThread:
    238     return bitc::ATTR_KIND_SANITIZE_THREAD;
    239   case Attribute::SanitizeMemory:
    240     return bitc::ATTR_KIND_SANITIZE_MEMORY;
    241   case Attribute::UWTable:
    242     return bitc::ATTR_KIND_UW_TABLE;
    243   case Attribute::ZExt:
    244     return bitc::ATTR_KIND_Z_EXT;
    245   case Attribute::EndAttrKinds:
    246     llvm_unreachable("Can not encode end-attribute kinds marker.");
    247   case Attribute::None:
    248     llvm_unreachable("Can not encode none-attribute.");
    249   }
    250 
    251   llvm_unreachable("Trying to encode unknown attribute");
    252 }
    253 
    254 static void WriteAttributeGroupTable(const ValueEnumerator &VE,
    255                                      BitstreamWriter &Stream) {
    256   const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
    257   if (AttrGrps.empty()) return;
    258 
    259   Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
    260 
    261   SmallVector<uint64_t, 64> Record;
    262   for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
    263     AttributeSet AS = AttrGrps[i];
    264     for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
    265       AttributeSet A = AS.getSlotAttributes(i);
    266 
    267       Record.push_back(VE.getAttributeGroupID(A));
    268       Record.push_back(AS.getSlotIndex(i));
    269 
    270       for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
    271            I != E; ++I) {
    272         Attribute Attr = *I;
    273         if (Attr.isEnumAttribute()) {
    274           Record.push_back(0);
    275           Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
    276         } else if (Attr.isIntAttribute()) {
    277           Record.push_back(1);
    278           Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
    279           Record.push_back(Attr.getValueAsInt());
    280         } else {
    281           StringRef Kind = Attr.getKindAsString();
    282           StringRef Val = Attr.getValueAsString();
    283 
    284           Record.push_back(Val.empty() ? 3 : 4);
    285           Record.append(Kind.begin(), Kind.end());
    286           Record.push_back(0);
    287           if (!Val.empty()) {
    288             Record.append(Val.begin(), Val.end());
    289             Record.push_back(0);
    290           }
    291         }
    292       }
    293 
    294       Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
    295       Record.clear();
    296     }
    297   }
    298 
    299   Stream.ExitBlock();
    300 }
    301 
    302 static void WriteAttributeTable(const ValueEnumerator &VE,
    303                                 BitstreamWriter &Stream) {
    304   const std::vector<AttributeSet> &Attrs = VE.getAttributes();
    305   if (Attrs.empty()) return;
    306 
    307   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
    308 
    309   SmallVector<uint64_t, 64> Record;
    310   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
    311     const AttributeSet &A = Attrs[i];
    312     for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
    313       Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
    314 
    315     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
    316     Record.clear();
    317   }
    318 
    319   Stream.ExitBlock();
    320 }
    321 
    322 /// WriteTypeTable - Write out the type table for a module.
    323 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
    324   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
    325 
    326   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
    327   SmallVector<uint64_t, 64> TypeVals;
    328 
    329   uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
    330 
    331   // Abbrev for TYPE_CODE_POINTER.
    332   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    333   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
    334   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    335   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
    336   unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
    337 
    338   // Abbrev for TYPE_CODE_FUNCTION.
    339   Abbv = new BitCodeAbbrev();
    340   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
    341   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
    342   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    343   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    344 
    345   unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
    346 
    347   // Abbrev for TYPE_CODE_STRUCT_ANON.
    348   Abbv = new BitCodeAbbrev();
    349   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
    350   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
    351   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    352   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    353 
    354   unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
    355 
    356   // Abbrev for TYPE_CODE_STRUCT_NAME.
    357   Abbv = new BitCodeAbbrev();
    358   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
    359   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    360   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    361   unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
    362 
    363   // Abbrev for TYPE_CODE_STRUCT_NAMED.
    364   Abbv = new BitCodeAbbrev();
    365   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
    366   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
    367   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    368   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    369 
    370   unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
    371 
    372   // Abbrev for TYPE_CODE_ARRAY.
    373   Abbv = new BitCodeAbbrev();
    374   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
    375   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
    376   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    377 
    378   unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
    379 
    380   // Emit an entry count so the reader can reserve space.
    381   TypeVals.push_back(TypeList.size());
    382   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
    383   TypeVals.clear();
    384 
    385   // Loop over all of the types, emitting each in turn.
    386   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
    387     Type *T = TypeList[i];
    388     int AbbrevToUse = 0;
    389     unsigned Code = 0;
    390 
    391     switch (T->getTypeID()) {
    392     case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;      break;
    393     case Type::HalfTyID:      Code = bitc::TYPE_CODE_HALF;      break;
    394     case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;     break;
    395     case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE;    break;
    396     case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80;  break;
    397     case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128;     break;
    398     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
    399     case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;     break;
    400     case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA;  break;
    401     case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX;   break;
    402     case Type::IntegerTyID:
    403       // INTEGER: [width]
    404       Code = bitc::TYPE_CODE_INTEGER;
    405       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
    406       break;
    407     case Type::PointerTyID: {
    408       PointerType *PTy = cast<PointerType>(T);
    409       // POINTER: [pointee type, address space]
    410       Code = bitc::TYPE_CODE_POINTER;
    411       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
    412       unsigned AddressSpace = PTy->getAddressSpace();
    413       TypeVals.push_back(AddressSpace);
    414       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
    415       break;
    416     }
    417     case Type::FunctionTyID: {
    418       FunctionType *FT = cast<FunctionType>(T);
    419       // FUNCTION: [isvararg, retty, paramty x N]
    420       Code = bitc::TYPE_CODE_FUNCTION;
    421       TypeVals.push_back(FT->isVarArg());
    422       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
    423       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
    424         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
    425       AbbrevToUse = FunctionAbbrev;
    426       break;
    427     }
    428     case Type::StructTyID: {
    429       StructType *ST = cast<StructType>(T);
    430       // STRUCT: [ispacked, eltty x N]
    431       TypeVals.push_back(ST->isPacked());
    432       // Output all of the element types.
    433       for (StructType::element_iterator I = ST->element_begin(),
    434            E = ST->element_end(); I != E; ++I)
    435         TypeVals.push_back(VE.getTypeID(*I));
    436 
    437       if (ST->isLiteral()) {
    438         Code = bitc::TYPE_CODE_STRUCT_ANON;
    439         AbbrevToUse = StructAnonAbbrev;
    440       } else {
    441         if (ST->isOpaque()) {
    442           Code = bitc::TYPE_CODE_OPAQUE;
    443         } else {
    444           Code = bitc::TYPE_CODE_STRUCT_NAMED;
    445           AbbrevToUse = StructNamedAbbrev;
    446         }
    447 
    448         // Emit the name if it is present.
    449         if (!ST->getName().empty())
    450           WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
    451                             StructNameAbbrev, Stream);
    452       }
    453       break;
    454     }
    455     case Type::ArrayTyID: {
    456       ArrayType *AT = cast<ArrayType>(T);
    457       // ARRAY: [numelts, eltty]
    458       Code = bitc::TYPE_CODE_ARRAY;
    459       TypeVals.push_back(AT->getNumElements());
    460       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
    461       AbbrevToUse = ArrayAbbrev;
    462       break;
    463     }
    464     case Type::VectorTyID: {
    465       VectorType *VT = cast<VectorType>(T);
    466       // VECTOR [numelts, eltty]
    467       Code = bitc::TYPE_CODE_VECTOR;
    468       TypeVals.push_back(VT->getNumElements());
    469       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
    470       break;
    471     }
    472     }
    473 
    474     // Emit the finished record.
    475     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
    476     TypeVals.clear();
    477   }
    478 
    479   Stream.ExitBlock();
    480 }
    481 
    482 static unsigned getEncodedLinkage(const GlobalValue &GV) {
    483   switch (GV.getLinkage()) {
    484   case GlobalValue::ExternalLinkage:
    485     return 0;
    486   case GlobalValue::WeakAnyLinkage:
    487     return 16;
    488   case GlobalValue::AppendingLinkage:
    489     return 2;
    490   case GlobalValue::InternalLinkage:
    491     return 3;
    492   case GlobalValue::LinkOnceAnyLinkage:
    493     return 18;
    494   case GlobalValue::ExternalWeakLinkage:
    495     return 7;
    496   case GlobalValue::CommonLinkage:
    497     return 8;
    498   case GlobalValue::PrivateLinkage:
    499     return 9;
    500   case GlobalValue::WeakODRLinkage:
    501     return 17;
    502   case GlobalValue::LinkOnceODRLinkage:
    503     return 19;
    504   case GlobalValue::AvailableExternallyLinkage:
    505     return 12;
    506   }
    507   llvm_unreachable("Invalid linkage");
    508 }
    509 
    510 static unsigned getEncodedVisibility(const GlobalValue &GV) {
    511   switch (GV.getVisibility()) {
    512   case GlobalValue::DefaultVisibility:   return 0;
    513   case GlobalValue::HiddenVisibility:    return 1;
    514   case GlobalValue::ProtectedVisibility: return 2;
    515   }
    516   llvm_unreachable("Invalid visibility");
    517 }
    518 
    519 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
    520   switch (GV.getDLLStorageClass()) {
    521   case GlobalValue::DefaultStorageClass:   return 0;
    522   case GlobalValue::DLLImportStorageClass: return 1;
    523   case GlobalValue::DLLExportStorageClass: return 2;
    524   }
    525   llvm_unreachable("Invalid DLL storage class");
    526 }
    527 
    528 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
    529   switch (GV.getThreadLocalMode()) {
    530     case GlobalVariable::NotThreadLocal:         return 0;
    531     case GlobalVariable::GeneralDynamicTLSModel: return 1;
    532     case GlobalVariable::LocalDynamicTLSModel:   return 2;
    533     case GlobalVariable::InitialExecTLSModel:    return 3;
    534     case GlobalVariable::LocalExecTLSModel:      return 4;
    535   }
    536   llvm_unreachable("Invalid TLS model");
    537 }
    538 
    539 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
    540   switch (C.getSelectionKind()) {
    541   case Comdat::Any:
    542     return bitc::COMDAT_SELECTION_KIND_ANY;
    543   case Comdat::ExactMatch:
    544     return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
    545   case Comdat::Largest:
    546     return bitc::COMDAT_SELECTION_KIND_LARGEST;
    547   case Comdat::NoDuplicates:
    548     return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
    549   case Comdat::SameSize:
    550     return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
    551   }
    552   llvm_unreachable("Invalid selection kind");
    553 }
    554 
    555 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
    556   SmallVector<uint16_t, 64> Vals;
    557   for (const Comdat *C : VE.getComdats()) {
    558     // COMDAT: [selection_kind, name]
    559     Vals.push_back(getEncodedComdatSelectionKind(*C));
    560     size_t Size = C->getName().size();
    561     assert(isUInt<16>(Size));
    562     Vals.push_back(Size);
    563     for (char Chr : C->getName())
    564       Vals.push_back((unsigned char)Chr);
    565     Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
    566     Vals.clear();
    567   }
    568 }
    569 
    570 // Emit top-level description of module, including target triple, inline asm,
    571 // descriptors for global variables, and function prototype info.
    572 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
    573                             BitstreamWriter &Stream) {
    574   // Emit various pieces of data attached to a module.
    575   if (!M->getTargetTriple().empty())
    576     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
    577                       0/*TODO*/, Stream);
    578   const std::string &DL = M->getDataLayoutStr();
    579   if (!DL.empty())
    580     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
    581   if (!M->getModuleInlineAsm().empty())
    582     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
    583                       0/*TODO*/, Stream);
    584 
    585   // Emit information about sections and GC, computing how many there are. Also
    586   // compute the maximum alignment value.
    587   std::map<std::string, unsigned> SectionMap;
    588   std::map<std::string, unsigned> GCMap;
    589   unsigned MaxAlignment = 0;
    590   unsigned MaxGlobalType = 0;
    591   for (const GlobalValue &GV : M->globals()) {
    592     MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
    593     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
    594     if (GV.hasSection()) {
    595       // Give section names unique ID's.
    596       unsigned &Entry = SectionMap[GV.getSection()];
    597       if (!Entry) {
    598         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
    599                           0/*TODO*/, Stream);
    600         Entry = SectionMap.size();
    601       }
    602     }
    603   }
    604   for (const Function &F : *M) {
    605     MaxAlignment = std::max(MaxAlignment, F.getAlignment());
    606     if (F.hasSection()) {
    607       // Give section names unique ID's.
    608       unsigned &Entry = SectionMap[F.getSection()];
    609       if (!Entry) {
    610         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
    611                           0/*TODO*/, Stream);
    612         Entry = SectionMap.size();
    613       }
    614     }
    615     if (F.hasGC()) {
    616       // Same for GC names.
    617       unsigned &Entry = GCMap[F.getGC()];
    618       if (!Entry) {
    619         WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
    620                           0/*TODO*/, Stream);
    621         Entry = GCMap.size();
    622       }
    623     }
    624   }
    625 
    626   // Emit abbrev for globals, now that we know # sections and max alignment.
    627   unsigned SimpleGVarAbbrev = 0;
    628   if (!M->global_empty()) {
    629     // Add an abbrev for common globals with no visibility or thread localness.
    630     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    631     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
    632     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    633                               Log2_32_Ceil(MaxGlobalType+1)));
    634     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
    635     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
    636     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));      // Linkage.
    637     if (MaxAlignment == 0)                                      // Alignment.
    638       Abbv->Add(BitCodeAbbrevOp(0));
    639     else {
    640       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
    641       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    642                                Log2_32_Ceil(MaxEncAlignment+1)));
    643     }
    644     if (SectionMap.empty())                                    // Section.
    645       Abbv->Add(BitCodeAbbrevOp(0));
    646     else
    647       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    648                                Log2_32_Ceil(SectionMap.size()+1)));
    649     // Don't bother emitting vis + thread local.
    650     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
    651   }
    652 
    653   // Emit the global variable information.
    654   SmallVector<unsigned, 64> Vals;
    655   for (const GlobalVariable &GV : M->globals()) {
    656     unsigned AbbrevToUse = 0;
    657 
    658     // GLOBALVAR: [type, isconst, initid,
    659     //             linkage, alignment, section, visibility, threadlocal,
    660     //             unnamed_addr, externally_initialized, dllstorageclass,
    661     //             comdat]
    662     Vals.push_back(VE.getTypeID(GV.getType()));
    663     Vals.push_back(GV.isConstant());
    664     Vals.push_back(GV.isDeclaration() ? 0 :
    665                    (VE.getValueID(GV.getInitializer()) + 1));
    666     Vals.push_back(getEncodedLinkage(GV));
    667     Vals.push_back(Log2_32(GV.getAlignment())+1);
    668     Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
    669     if (GV.isThreadLocal() ||
    670         GV.getVisibility() != GlobalValue::DefaultVisibility ||
    671         GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
    672         GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
    673         GV.hasComdat()) {
    674       Vals.push_back(getEncodedVisibility(GV));
    675       Vals.push_back(getEncodedThreadLocalMode(GV));
    676       Vals.push_back(GV.hasUnnamedAddr());
    677       Vals.push_back(GV.isExternallyInitialized());
    678       Vals.push_back(getEncodedDLLStorageClass(GV));
    679       Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
    680     } else {
    681       AbbrevToUse = SimpleGVarAbbrev;
    682     }
    683 
    684     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
    685     Vals.clear();
    686   }
    687 
    688   // Emit the function proto information.
    689   for (const Function &F : *M) {
    690     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
    691     //             section, visibility, gc, unnamed_addr, prologuedata,
    692     //             dllstorageclass, comdat, prefixdata]
    693     Vals.push_back(VE.getTypeID(F.getType()));
    694     Vals.push_back(F.getCallingConv());
    695     Vals.push_back(F.isDeclaration());
    696     Vals.push_back(getEncodedLinkage(F));
    697     Vals.push_back(VE.getAttributeID(F.getAttributes()));
    698     Vals.push_back(Log2_32(F.getAlignment())+1);
    699     Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
    700     Vals.push_back(getEncodedVisibility(F));
    701     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
    702     Vals.push_back(F.hasUnnamedAddr());
    703     Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
    704                                        : 0);
    705     Vals.push_back(getEncodedDLLStorageClass(F));
    706     Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
    707     Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
    708                                      : 0);
    709 
    710     unsigned AbbrevToUse = 0;
    711     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
    712     Vals.clear();
    713   }
    714 
    715   // Emit the alias information.
    716   for (const GlobalAlias &A : M->aliases()) {
    717     // ALIAS: [alias type, aliasee val#, linkage, visibility]
    718     Vals.push_back(VE.getTypeID(A.getType()));
    719     Vals.push_back(VE.getValueID(A.getAliasee()));
    720     Vals.push_back(getEncodedLinkage(A));
    721     Vals.push_back(getEncodedVisibility(A));
    722     Vals.push_back(getEncodedDLLStorageClass(A));
    723     Vals.push_back(getEncodedThreadLocalMode(A));
    724     Vals.push_back(A.hasUnnamedAddr());
    725     unsigned AbbrevToUse = 0;
    726     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
    727     Vals.clear();
    728   }
    729 }
    730 
    731 static uint64_t GetOptimizationFlags(const Value *V) {
    732   uint64_t Flags = 0;
    733 
    734   if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
    735     if (OBO->hasNoSignedWrap())
    736       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
    737     if (OBO->hasNoUnsignedWrap())
    738       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
    739   } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
    740     if (PEO->isExact())
    741       Flags |= 1 << bitc::PEO_EXACT;
    742   } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
    743     if (FPMO->hasUnsafeAlgebra())
    744       Flags |= FastMathFlags::UnsafeAlgebra;
    745     if (FPMO->hasNoNaNs())
    746       Flags |= FastMathFlags::NoNaNs;
    747     if (FPMO->hasNoInfs())
    748       Flags |= FastMathFlags::NoInfs;
    749     if (FPMO->hasNoSignedZeros())
    750       Flags |= FastMathFlags::NoSignedZeros;
    751     if (FPMO->hasAllowReciprocal())
    752       Flags |= FastMathFlags::AllowReciprocal;
    753   }
    754 
    755   return Flags;
    756 }
    757 
    758 static void WriteValueAsMetadata(const ValueAsMetadata *MD,
    759                                  const ValueEnumerator &VE,
    760                                  BitstreamWriter &Stream,
    761                                  SmallVectorImpl<uint64_t> &Record) {
    762   // Mimic an MDNode with a value as one operand.
    763   Value *V = MD->getValue();
    764   Record.push_back(VE.getTypeID(V->getType()));
    765   Record.push_back(VE.getValueID(V));
    766   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
    767   Record.clear();
    768 }
    769 
    770 static void WriteMDTuple(const MDTuple *N, const ValueEnumerator &VE,
    771                          BitstreamWriter &Stream,
    772                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
    773   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
    774     Metadata *MD = N->getOperand(i);
    775     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
    776            "Unexpected function-local metadata");
    777     Record.push_back(VE.getMetadataOrNullID(MD));
    778   }
    779   Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
    780                                     : bitc::METADATA_NODE,
    781                     Record, Abbrev);
    782   Record.clear();
    783 }
    784 
    785 static void WriteMDLocation(const MDLocation *N, const ValueEnumerator &VE,
    786                             BitstreamWriter &Stream,
    787                             SmallVectorImpl<uint64_t> &Record,
    788                             unsigned Abbrev) {
    789   Record.push_back(N->isDistinct());
    790   Record.push_back(N->getLine());
    791   Record.push_back(N->getColumn());
    792   Record.push_back(VE.getMetadataID(N->getScope()));
    793   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
    794 
    795   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
    796   Record.clear();
    797 }
    798 
    799 static void WriteGenericDebugNode(const GenericDebugNode *N,
    800                                   const ValueEnumerator &VE,
    801                                   BitstreamWriter &Stream,
    802                                   SmallVectorImpl<uint64_t> &Record,
    803                                   unsigned Abbrev) {
    804   Record.push_back(N->isDistinct());
    805   Record.push_back(N->getTag());
    806   Record.push_back(0); // Per-tag version field; unused for now.
    807 
    808   for (auto &I : N->operands())
    809     Record.push_back(VE.getMetadataOrNullID(I));
    810 
    811   Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
    812   Record.clear();
    813 }
    814 
    815 static uint64_t rotateSign(int64_t I) {
    816   uint64_t U = I;
    817   return I < 0 ? ~(U << 1) : U << 1;
    818 }
    819 
    820 static void WriteMDSubrange(const MDSubrange *N, const ValueEnumerator &,
    821                             BitstreamWriter &Stream,
    822                             SmallVectorImpl<uint64_t> &Record,
    823                             unsigned Abbrev) {
    824   Record.push_back(N->isDistinct());
    825   Record.push_back(N->getCount());
    826   Record.push_back(rotateSign(N->getLowerBound()));
    827 
    828   Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
    829   Record.clear();
    830 }
    831 
    832 static void WriteMDEnumerator(const MDEnumerator *N, const ValueEnumerator &VE,
    833                               BitstreamWriter &Stream,
    834                               SmallVectorImpl<uint64_t> &Record,
    835                               unsigned Abbrev) {
    836   Record.push_back(N->isDistinct());
    837   Record.push_back(rotateSign(N->getValue()));
    838   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    839 
    840   Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
    841   Record.clear();
    842 }
    843 
    844 static void WriteMDBasicType(const MDBasicType *N, const ValueEnumerator &VE,
    845                              BitstreamWriter &Stream,
    846                              SmallVectorImpl<uint64_t> &Record,
    847                              unsigned Abbrev) {
    848   Record.push_back(N->isDistinct());
    849   Record.push_back(N->getTag());
    850   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    851   Record.push_back(N->getSizeInBits());
    852   Record.push_back(N->getAlignInBits());
    853   Record.push_back(N->getEncoding());
    854 
    855   Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
    856   Record.clear();
    857 }
    858 
    859 static void WriteMDDerivedType(const MDDerivedType *N,
    860                                const ValueEnumerator &VE,
    861                                BitstreamWriter &Stream,
    862                                SmallVectorImpl<uint64_t> &Record,
    863                                unsigned Abbrev) {
    864   Record.push_back(N->isDistinct());
    865   Record.push_back(N->getTag());
    866   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    867   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    868   Record.push_back(N->getLine());
    869   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    870   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
    871   Record.push_back(N->getSizeInBits());
    872   Record.push_back(N->getAlignInBits());
    873   Record.push_back(N->getOffsetInBits());
    874   Record.push_back(N->getFlags());
    875   Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
    876 
    877   Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
    878   Record.clear();
    879 }
    880 
    881 static void WriteMDCompositeType(const MDCompositeType *N,
    882                                  const ValueEnumerator &VE,
    883                                  BitstreamWriter &Stream,
    884                                  SmallVectorImpl<uint64_t> &Record,
    885                                  unsigned Abbrev) {
    886   Record.push_back(N->isDistinct());
    887   Record.push_back(N->getTag());
    888   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    889   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    890   Record.push_back(N->getLine());
    891   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    892   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
    893   Record.push_back(N->getSizeInBits());
    894   Record.push_back(N->getAlignInBits());
    895   Record.push_back(N->getOffsetInBits());
    896   Record.push_back(N->getFlags());
    897   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
    898   Record.push_back(N->getRuntimeLang());
    899   Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
    900   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
    901   Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
    902 
    903   Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
    904   Record.clear();
    905 }
    906 
    907 static void WriteMDSubroutineType(const MDSubroutineType *N,
    908                                   const ValueEnumerator &VE,
    909                                   BitstreamWriter &Stream,
    910                                   SmallVectorImpl<uint64_t> &Record,
    911                                   unsigned Abbrev) {
    912   Record.push_back(N->isDistinct());
    913   Record.push_back(N->getFlags());
    914   Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
    915 
    916   Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
    917   Record.clear();
    918 }
    919 
    920 static void WriteMDFile(const MDFile *N, const ValueEnumerator &VE,
    921                         BitstreamWriter &Stream,
    922                         SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
    923   Record.push_back(N->isDistinct());
    924   Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
    925   Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
    926 
    927   Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
    928   Record.clear();
    929 }
    930 
    931 static void WriteMDCompileUnit(const MDCompileUnit *N,
    932                                const ValueEnumerator &VE,
    933                                BitstreamWriter &Stream,
    934                                SmallVectorImpl<uint64_t> &Record,
    935                                unsigned Abbrev) {
    936   Record.push_back(N->isDistinct());
    937   Record.push_back(N->getSourceLanguage());
    938   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    939   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
    940   Record.push_back(N->isOptimized());
    941   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
    942   Record.push_back(N->getRuntimeVersion());
    943   Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
    944   Record.push_back(N->getEmissionKind());
    945   Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
    946   Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
    947   Record.push_back(VE.getMetadataOrNullID(N->getSubprograms().get()));
    948   Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
    949   Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
    950 
    951   Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
    952   Record.clear();
    953 }
    954 
    955 static void WriteMDSubprogram(const MDSubprogram *N,
    956                                const ValueEnumerator &VE,
    957                                BitstreamWriter &Stream,
    958                                SmallVectorImpl<uint64_t> &Record,
    959                                unsigned Abbrev) {
    960   Record.push_back(N->isDistinct());
    961   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    962   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
    963   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
    964   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    965   Record.push_back(N->getLine());
    966   Record.push_back(VE.getMetadataOrNullID(N->getType()));
    967   Record.push_back(N->isLocalToUnit());
    968   Record.push_back(N->isDefinition());
    969   Record.push_back(N->getScopeLine());
    970   Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
    971   Record.push_back(N->getVirtuality());
    972   Record.push_back(N->getVirtualIndex());
    973   Record.push_back(N->getFlags());
    974   Record.push_back(N->isOptimized());
    975   Record.push_back(VE.getMetadataOrNullID(N->getRawFunction()));
    976   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
    977   Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
    978   Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
    979 
    980   Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
    981   Record.clear();
    982 }
    983 
    984 static void WriteMDLexicalBlock(const MDLexicalBlock *N,
    985                                const ValueEnumerator &VE,
    986                                BitstreamWriter &Stream,
    987                                SmallVectorImpl<uint64_t> &Record,
    988                                unsigned Abbrev) {
    989   Record.push_back(N->isDistinct());
    990   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
    991   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
    992   Record.push_back(N->getLine());
    993   Record.push_back(N->getColumn());
    994 
    995   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
    996   Record.clear();
    997 }
    998 
    999 static void WriteMDLexicalBlockFile(const MDLexicalBlockFile *N,
   1000                                     const ValueEnumerator &VE,
   1001                                     BitstreamWriter &Stream,
   1002                                     SmallVectorImpl<uint64_t> &Record,
   1003                                     unsigned Abbrev) {
   1004   Record.push_back(N->isDistinct());
   1005   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
   1006   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   1007   Record.push_back(N->getDiscriminator());
   1008 
   1009   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
   1010   Record.clear();
   1011 }
   1012 
   1013 static void WriteMDNamespace(const MDNamespace *N, const ValueEnumerator &VE,
   1014                              BitstreamWriter &Stream,
   1015                              SmallVectorImpl<uint64_t> &Record,
   1016                              unsigned Abbrev) {
   1017   Record.push_back(N->isDistinct());
   1018   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
   1019   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   1020   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1021   Record.push_back(N->getLine());
   1022 
   1023   Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
   1024   Record.clear();
   1025 }
   1026 
   1027 static void WriteMDTemplateTypeParameter(const MDTemplateTypeParameter *N,
   1028                                          const ValueEnumerator &VE,
   1029                                          BitstreamWriter &Stream,
   1030                                          SmallVectorImpl<uint64_t> &Record,
   1031                                          unsigned Abbrev) {
   1032   Record.push_back(N->isDistinct());
   1033   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1034   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   1035 
   1036   Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
   1037   Record.clear();
   1038 }
   1039 
   1040 static void WriteMDTemplateValueParameter(const MDTemplateValueParameter *N,
   1041                                           const ValueEnumerator &VE,
   1042                                           BitstreamWriter &Stream,
   1043                                           SmallVectorImpl<uint64_t> &Record,
   1044                                           unsigned Abbrev) {
   1045   Record.push_back(N->isDistinct());
   1046   Record.push_back(N->getTag());
   1047   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1048   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   1049   Record.push_back(VE.getMetadataOrNullID(N->getValue()));
   1050 
   1051   Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
   1052   Record.clear();
   1053 }
   1054 
   1055 static void WriteMDGlobalVariable(const MDGlobalVariable *N,
   1056                                   const ValueEnumerator &VE,
   1057                                   BitstreamWriter &Stream,
   1058                                   SmallVectorImpl<uint64_t> &Record,
   1059                                   unsigned Abbrev) {
   1060   Record.push_back(N->isDistinct());
   1061   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
   1062   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1063   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
   1064   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   1065   Record.push_back(N->getLine());
   1066   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   1067   Record.push_back(N->isLocalToUnit());
   1068   Record.push_back(N->isDefinition());
   1069   Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
   1070   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
   1071 
   1072   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
   1073   Record.clear();
   1074 }
   1075 
   1076 static void WriteMDLocalVariable(const MDLocalVariable *N,
   1077                                  const ValueEnumerator &VE,
   1078                                  BitstreamWriter &Stream,
   1079                                  SmallVectorImpl<uint64_t> &Record,
   1080                                  unsigned Abbrev) {
   1081   Record.push_back(N->isDistinct());
   1082   Record.push_back(N->getTag());
   1083   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
   1084   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1085   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   1086   Record.push_back(N->getLine());
   1087   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   1088   Record.push_back(N->getArg());
   1089   Record.push_back(N->getFlags());
   1090 
   1091   Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
   1092   Record.clear();
   1093 }
   1094 
   1095 static void WriteMDExpression(const MDExpression *N, const ValueEnumerator &,
   1096                               BitstreamWriter &Stream,
   1097                               SmallVectorImpl<uint64_t> &Record,
   1098                               unsigned Abbrev) {
   1099   Record.reserve(N->getElements().size() + 1);
   1100 
   1101   Record.push_back(N->isDistinct());
   1102   Record.append(N->elements_begin(), N->elements_end());
   1103 
   1104   Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
   1105   Record.clear();
   1106 }
   1107 
   1108 static void WriteMDObjCProperty(const MDObjCProperty *N,
   1109                                  const ValueEnumerator &VE,
   1110                                  BitstreamWriter &Stream,
   1111                                  SmallVectorImpl<uint64_t> &Record,
   1112                                  unsigned Abbrev) {
   1113   Record.push_back(N->isDistinct());
   1114   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1115   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
   1116   Record.push_back(N->getLine());
   1117   Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
   1118   Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
   1119   Record.push_back(N->getAttributes());
   1120   Record.push_back(VE.getMetadataOrNullID(N->getType()));
   1121 
   1122   Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
   1123   Record.clear();
   1124 }
   1125 
   1126 static void WriteMDImportedEntity(const MDImportedEntity *N,
   1127                                   const ValueEnumerator &VE,
   1128                                   BitstreamWriter &Stream,
   1129                                   SmallVectorImpl<uint64_t> &Record,
   1130                                   unsigned Abbrev) {
   1131   Record.push_back(N->isDistinct());
   1132   Record.push_back(N->getTag());
   1133   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
   1134   Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
   1135   Record.push_back(N->getLine());
   1136   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
   1137 
   1138   Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
   1139   Record.clear();
   1140 }
   1141 
   1142 static void WriteModuleMetadata(const Module *M,
   1143                                 const ValueEnumerator &VE,
   1144                                 BitstreamWriter &Stream) {
   1145   const auto &MDs = VE.getMDs();
   1146   if (MDs.empty() && M->named_metadata_empty())
   1147     return;
   1148 
   1149   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
   1150 
   1151   unsigned MDSAbbrev = 0;
   1152   if (VE.hasMDString()) {
   1153     // Abbrev for METADATA_STRING.
   1154     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1155     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
   1156     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1157     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
   1158     MDSAbbrev = Stream.EmitAbbrev(Abbv);
   1159   }
   1160 
   1161   // Initialize MDNode abbreviations.
   1162 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
   1163 #include "llvm/IR/Metadata.def"
   1164 
   1165   if (VE.hasMDLocation()) {
   1166     // Abbrev for METADATA_LOCATION.
   1167     //
   1168     // Assume the column is usually under 128, and always output the inlined-at
   1169     // location (it's never more expensive than building an array size 1).
   1170     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1171     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
   1172     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
   1173     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1174     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1175     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1176     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1177     MDLocationAbbrev = Stream.EmitAbbrev(Abbv);
   1178   }
   1179 
   1180   if (VE.hasGenericDebugNode()) {
   1181     // Abbrev for METADATA_GENERIC_DEBUG.
   1182     //
   1183     // Assume the column is usually under 128, and always output the inlined-at
   1184     // location (it's never more expensive than building an array size 1).
   1185     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1186     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
   1187     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
   1188     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1189     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
   1190     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1191     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1192     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1193     GenericDebugNodeAbbrev = Stream.EmitAbbrev(Abbv);
   1194   }
   1195 
   1196   unsigned NameAbbrev = 0;
   1197   if (!M->named_metadata_empty()) {
   1198     // Abbrev for METADATA_NAME.
   1199     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1200     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
   1201     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1202     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
   1203     NameAbbrev = Stream.EmitAbbrev(Abbv);
   1204   }
   1205 
   1206   SmallVector<uint64_t, 64> Record;
   1207   for (const Metadata *MD : MDs) {
   1208     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
   1209       assert(N->isResolved() && "Expected forward references to be resolved");
   1210 
   1211       switch (N->getMetadataID()) {
   1212       default:
   1213         llvm_unreachable("Invalid MDNode subclass");
   1214 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
   1215   case Metadata::CLASS##Kind:                                                  \
   1216     Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev);           \
   1217     continue;
   1218 #include "llvm/IR/Metadata.def"
   1219       }
   1220     }
   1221     if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
   1222       WriteValueAsMetadata(MDC, VE, Stream, Record);
   1223       continue;
   1224     }
   1225     const MDString *MDS = cast<MDString>(MD);
   1226     // Code: [strchar x N]
   1227     Record.append(MDS->bytes_begin(), MDS->bytes_end());
   1228 
   1229     // Emit the finished record.
   1230     Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
   1231     Record.clear();
   1232   }
   1233 
   1234   // Write named metadata.
   1235   for (const NamedMDNode &NMD : M->named_metadata()) {
   1236     // Write name.
   1237     StringRef Str = NMD.getName();
   1238     Record.append(Str.bytes_begin(), Str.bytes_end());
   1239     Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
   1240     Record.clear();
   1241 
   1242     // Write named metadata operands.
   1243     for (const MDNode *N : NMD.operands())
   1244       Record.push_back(VE.getMetadataID(N));
   1245     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
   1246     Record.clear();
   1247   }
   1248 
   1249   Stream.ExitBlock();
   1250 }
   1251 
   1252 static void WriteFunctionLocalMetadata(const Function &F,
   1253                                        const ValueEnumerator &VE,
   1254                                        BitstreamWriter &Stream) {
   1255   bool StartedMetadataBlock = false;
   1256   SmallVector<uint64_t, 64> Record;
   1257   const SmallVectorImpl<const LocalAsMetadata *> &MDs =
   1258       VE.getFunctionLocalMDs();
   1259   for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
   1260     assert(MDs[i] && "Expected valid function-local metadata");
   1261     if (!StartedMetadataBlock) {
   1262       Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
   1263       StartedMetadataBlock = true;
   1264     }
   1265     WriteValueAsMetadata(MDs[i], VE, Stream, Record);
   1266   }
   1267 
   1268   if (StartedMetadataBlock)
   1269     Stream.ExitBlock();
   1270 }
   1271 
   1272 static void WriteMetadataAttachment(const Function &F,
   1273                                     const ValueEnumerator &VE,
   1274                                     BitstreamWriter &Stream) {
   1275   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
   1276 
   1277   SmallVector<uint64_t, 64> Record;
   1278 
   1279   // Write metadata attachments
   1280   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
   1281   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
   1282 
   1283   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
   1284     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
   1285          I != E; ++I) {
   1286       MDs.clear();
   1287       I->getAllMetadataOtherThanDebugLoc(MDs);
   1288 
   1289       // If no metadata, ignore instruction.
   1290       if (MDs.empty()) continue;
   1291 
   1292       Record.push_back(VE.getInstructionID(I));
   1293 
   1294       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
   1295         Record.push_back(MDs[i].first);
   1296         Record.push_back(VE.getMetadataID(MDs[i].second));
   1297       }
   1298       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
   1299       Record.clear();
   1300     }
   1301 
   1302   Stream.ExitBlock();
   1303 }
   1304 
   1305 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
   1306   SmallVector<uint64_t, 64> Record;
   1307 
   1308   // Write metadata kinds
   1309   // METADATA_KIND - [n x [id, name]]
   1310   SmallVector<StringRef, 8> Names;
   1311   M->getMDKindNames(Names);
   1312 
   1313   if (Names.empty()) return;
   1314 
   1315   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
   1316 
   1317   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
   1318     Record.push_back(MDKindID);
   1319     StringRef KName = Names[MDKindID];
   1320     Record.append(KName.begin(), KName.end());
   1321 
   1322     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
   1323     Record.clear();
   1324   }
   1325 
   1326   Stream.ExitBlock();
   1327 }
   1328 
   1329 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
   1330   if ((int64_t)V >= 0)
   1331     Vals.push_back(V << 1);
   1332   else
   1333     Vals.push_back((-V << 1) | 1);
   1334 }
   1335 
   1336 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
   1337                            const ValueEnumerator &VE,
   1338                            BitstreamWriter &Stream, bool isGlobal) {
   1339   if (FirstVal == LastVal) return;
   1340 
   1341   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
   1342 
   1343   unsigned AggregateAbbrev = 0;
   1344   unsigned String8Abbrev = 0;
   1345   unsigned CString7Abbrev = 0;
   1346   unsigned CString6Abbrev = 0;
   1347   // If this is a constant pool for the module, emit module-specific abbrevs.
   1348   if (isGlobal) {
   1349     // Abbrev for CST_CODE_AGGREGATE.
   1350     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1351     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
   1352     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1353     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
   1354     AggregateAbbrev = Stream.EmitAbbrev(Abbv);
   1355 
   1356     // Abbrev for CST_CODE_STRING.
   1357     Abbv = new BitCodeAbbrev();
   1358     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
   1359     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1360     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
   1361     String8Abbrev = Stream.EmitAbbrev(Abbv);
   1362     // Abbrev for CST_CODE_CSTRING.
   1363     Abbv = new BitCodeAbbrev();
   1364     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
   1365     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1366     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
   1367     CString7Abbrev = Stream.EmitAbbrev(Abbv);
   1368     // Abbrev for CST_CODE_CSTRING.
   1369     Abbv = new BitCodeAbbrev();
   1370     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
   1371     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1372     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
   1373     CString6Abbrev = Stream.EmitAbbrev(Abbv);
   1374   }
   1375 
   1376   SmallVector<uint64_t, 64> Record;
   1377 
   1378   const ValueEnumerator::ValueList &Vals = VE.getValues();
   1379   Type *LastTy = nullptr;
   1380   for (unsigned i = FirstVal; i != LastVal; ++i) {
   1381     const Value *V = Vals[i].first;
   1382     // If we need to switch types, do so now.
   1383     if (V->getType() != LastTy) {
   1384       LastTy = V->getType();
   1385       Record.push_back(VE.getTypeID(LastTy));
   1386       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
   1387                         CONSTANTS_SETTYPE_ABBREV);
   1388       Record.clear();
   1389     }
   1390 
   1391     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
   1392       Record.push_back(unsigned(IA->hasSideEffects()) |
   1393                        unsigned(IA->isAlignStack()) << 1 |
   1394                        unsigned(IA->getDialect()&1) << 2);
   1395 
   1396       // Add the asm string.
   1397       const std::string &AsmStr = IA->getAsmString();
   1398       Record.push_back(AsmStr.size());
   1399       Record.append(AsmStr.begin(), AsmStr.end());
   1400 
   1401       // Add the constraint string.
   1402       const std::string &ConstraintStr = IA->getConstraintString();
   1403       Record.push_back(ConstraintStr.size());
   1404       Record.append(ConstraintStr.begin(), ConstraintStr.end());
   1405       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
   1406       Record.clear();
   1407       continue;
   1408     }
   1409     const Constant *C = cast<Constant>(V);
   1410     unsigned Code = -1U;
   1411     unsigned AbbrevToUse = 0;
   1412     if (C->isNullValue()) {
   1413       Code = bitc::CST_CODE_NULL;
   1414     } else if (isa<UndefValue>(C)) {
   1415       Code = bitc::CST_CODE_UNDEF;
   1416     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
   1417       if (IV->getBitWidth() <= 64) {
   1418         uint64_t V = IV->getSExtValue();
   1419         emitSignedInt64(Record, V);
   1420         Code = bitc::CST_CODE_INTEGER;
   1421         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
   1422       } else {                             // Wide integers, > 64 bits in size.
   1423         // We have an arbitrary precision integer value to write whose
   1424         // bit width is > 64. However, in canonical unsigned integer
   1425         // format it is likely that the high bits are going to be zero.
   1426         // So, we only write the number of active words.
   1427         unsigned NWords = IV->getValue().getActiveWords();
   1428         const uint64_t *RawWords = IV->getValue().getRawData();
   1429         for (unsigned i = 0; i != NWords; ++i) {
   1430           emitSignedInt64(Record, RawWords[i]);
   1431         }
   1432         Code = bitc::CST_CODE_WIDE_INTEGER;
   1433       }
   1434     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
   1435       Code = bitc::CST_CODE_FLOAT;
   1436       Type *Ty = CFP->getType();
   1437       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
   1438         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
   1439       } else if (Ty->isX86_FP80Ty()) {
   1440         // api needed to prevent premature destruction
   1441         // bits are not in the same order as a normal i80 APInt, compensate.
   1442         APInt api = CFP->getValueAPF().bitcastToAPInt();
   1443         const uint64_t *p = api.getRawData();
   1444         Record.push_back((p[1] << 48) | (p[0] >> 16));
   1445         Record.push_back(p[0] & 0xffffLL);
   1446       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
   1447         APInt api = CFP->getValueAPF().bitcastToAPInt();
   1448         const uint64_t *p = api.getRawData();
   1449         Record.push_back(p[0]);
   1450         Record.push_back(p[1]);
   1451       } else {
   1452         assert (0 && "Unknown FP type!");
   1453       }
   1454     } else if (isa<ConstantDataSequential>(C) &&
   1455                cast<ConstantDataSequential>(C)->isString()) {
   1456       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
   1457       // Emit constant strings specially.
   1458       unsigned NumElts = Str->getNumElements();
   1459       // If this is a null-terminated string, use the denser CSTRING encoding.
   1460       if (Str->isCString()) {
   1461         Code = bitc::CST_CODE_CSTRING;
   1462         --NumElts;  // Don't encode the null, which isn't allowed by char6.
   1463       } else {
   1464         Code = bitc::CST_CODE_STRING;
   1465         AbbrevToUse = String8Abbrev;
   1466       }
   1467       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
   1468       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
   1469       for (unsigned i = 0; i != NumElts; ++i) {
   1470         unsigned char V = Str->getElementAsInteger(i);
   1471         Record.push_back(V);
   1472         isCStr7 &= (V & 128) == 0;
   1473         if (isCStrChar6)
   1474           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
   1475       }
   1476 
   1477       if (isCStrChar6)
   1478         AbbrevToUse = CString6Abbrev;
   1479       else if (isCStr7)
   1480         AbbrevToUse = CString7Abbrev;
   1481     } else if (const ConstantDataSequential *CDS =
   1482                   dyn_cast<ConstantDataSequential>(C)) {
   1483       Code = bitc::CST_CODE_DATA;
   1484       Type *EltTy = CDS->getType()->getElementType();
   1485       if (isa<IntegerType>(EltTy)) {
   1486         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
   1487           Record.push_back(CDS->getElementAsInteger(i));
   1488       } else if (EltTy->isFloatTy()) {
   1489         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
   1490           union { float F; uint32_t I; };
   1491           F = CDS->getElementAsFloat(i);
   1492           Record.push_back(I);
   1493         }
   1494       } else {
   1495         assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
   1496         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
   1497           union { double F; uint64_t I; };
   1498           F = CDS->getElementAsDouble(i);
   1499           Record.push_back(I);
   1500         }
   1501       }
   1502     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
   1503                isa<ConstantVector>(C)) {
   1504       Code = bitc::CST_CODE_AGGREGATE;
   1505       for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
   1506         Record.push_back(VE.getValueID(C->getOperand(i)));
   1507       AbbrevToUse = AggregateAbbrev;
   1508     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
   1509       switch (CE->getOpcode()) {
   1510       default:
   1511         if (Instruction::isCast(CE->getOpcode())) {
   1512           Code = bitc::CST_CODE_CE_CAST;
   1513           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
   1514           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1515           Record.push_back(VE.getValueID(C->getOperand(0)));
   1516           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
   1517         } else {
   1518           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
   1519           Code = bitc::CST_CODE_CE_BINOP;
   1520           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
   1521           Record.push_back(VE.getValueID(C->getOperand(0)));
   1522           Record.push_back(VE.getValueID(C->getOperand(1)));
   1523           uint64_t Flags = GetOptimizationFlags(CE);
   1524           if (Flags != 0)
   1525             Record.push_back(Flags);
   1526         }
   1527         break;
   1528       case Instruction::GetElementPtr: {
   1529         Code = bitc::CST_CODE_CE_GEP;
   1530         const auto *GO = cast<GEPOperator>(C);
   1531         if (GO->isInBounds())
   1532           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
   1533         Record.push_back(VE.getTypeID(GO->getSourceElementType()));
   1534         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
   1535           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
   1536           Record.push_back(VE.getValueID(C->getOperand(i)));
   1537         }
   1538         break;
   1539       }
   1540       case Instruction::Select:
   1541         Code = bitc::CST_CODE_CE_SELECT;
   1542         Record.push_back(VE.getValueID(C->getOperand(0)));
   1543         Record.push_back(VE.getValueID(C->getOperand(1)));
   1544         Record.push_back(VE.getValueID(C->getOperand(2)));
   1545         break;
   1546       case Instruction::ExtractElement:
   1547         Code = bitc::CST_CODE_CE_EXTRACTELT;
   1548         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1549         Record.push_back(VE.getValueID(C->getOperand(0)));
   1550         Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
   1551         Record.push_back(VE.getValueID(C->getOperand(1)));
   1552         break;
   1553       case Instruction::InsertElement:
   1554         Code = bitc::CST_CODE_CE_INSERTELT;
   1555         Record.push_back(VE.getValueID(C->getOperand(0)));
   1556         Record.push_back(VE.getValueID(C->getOperand(1)));
   1557         Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
   1558         Record.push_back(VE.getValueID(C->getOperand(2)));
   1559         break;
   1560       case Instruction::ShuffleVector:
   1561         // If the return type and argument types are the same, this is a
   1562         // standard shufflevector instruction.  If the types are different,
   1563         // then the shuffle is widening or truncating the input vectors, and
   1564         // the argument type must also be encoded.
   1565         if (C->getType() == C->getOperand(0)->getType()) {
   1566           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
   1567         } else {
   1568           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
   1569           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1570         }
   1571         Record.push_back(VE.getValueID(C->getOperand(0)));
   1572         Record.push_back(VE.getValueID(C->getOperand(1)));
   1573         Record.push_back(VE.getValueID(C->getOperand(2)));
   1574         break;
   1575       case Instruction::ICmp:
   1576       case Instruction::FCmp:
   1577         Code = bitc::CST_CODE_CE_CMP;
   1578         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1579         Record.push_back(VE.getValueID(C->getOperand(0)));
   1580         Record.push_back(VE.getValueID(C->getOperand(1)));
   1581         Record.push_back(CE->getPredicate());
   1582         break;
   1583       }
   1584     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
   1585       Code = bitc::CST_CODE_BLOCKADDRESS;
   1586       Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
   1587       Record.push_back(VE.getValueID(BA->getFunction()));
   1588       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
   1589     } else {
   1590 #ifndef NDEBUG
   1591       C->dump();
   1592 #endif
   1593       llvm_unreachable("Unknown constant!");
   1594     }
   1595     Stream.EmitRecord(Code, Record, AbbrevToUse);
   1596     Record.clear();
   1597   }
   1598 
   1599   Stream.ExitBlock();
   1600 }
   1601 
   1602 static void WriteModuleConstants(const ValueEnumerator &VE,
   1603                                  BitstreamWriter &Stream) {
   1604   const ValueEnumerator::ValueList &Vals = VE.getValues();
   1605 
   1606   // Find the first constant to emit, which is the first non-globalvalue value.
   1607   // We know globalvalues have been emitted by WriteModuleInfo.
   1608   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
   1609     if (!isa<GlobalValue>(Vals[i].first)) {
   1610       WriteConstants(i, Vals.size(), VE, Stream, true);
   1611       return;
   1612     }
   1613   }
   1614 }
   1615 
   1616 /// PushValueAndType - The file has to encode both the value and type id for
   1617 /// many values, because we need to know what type to create for forward
   1618 /// references.  However, most operands are not forward references, so this type
   1619 /// field is not needed.
   1620 ///
   1621 /// This function adds V's value ID to Vals.  If the value ID is higher than the
   1622 /// instruction ID, then it is a forward reference, and it also includes the
   1623 /// type ID.  The value ID that is written is encoded relative to the InstID.
   1624 static bool PushValueAndType(const Value *V, unsigned InstID,
   1625                              SmallVectorImpl<unsigned> &Vals,
   1626                              ValueEnumerator &VE) {
   1627   unsigned ValID = VE.getValueID(V);
   1628   // Make encoding relative to the InstID.
   1629   Vals.push_back(InstID - ValID);
   1630   if (ValID >= InstID) {
   1631     Vals.push_back(VE.getTypeID(V->getType()));
   1632     return true;
   1633   }
   1634   return false;
   1635 }
   1636 
   1637 /// pushValue - Like PushValueAndType, but where the type of the value is
   1638 /// omitted (perhaps it was already encoded in an earlier operand).
   1639 static void pushValue(const Value *V, unsigned InstID,
   1640                       SmallVectorImpl<unsigned> &Vals,
   1641                       ValueEnumerator &VE) {
   1642   unsigned ValID = VE.getValueID(V);
   1643   Vals.push_back(InstID - ValID);
   1644 }
   1645 
   1646 static void pushValueSigned(const Value *V, unsigned InstID,
   1647                             SmallVectorImpl<uint64_t> &Vals,
   1648                             ValueEnumerator &VE) {
   1649   unsigned ValID = VE.getValueID(V);
   1650   int64_t diff = ((int32_t)InstID - (int32_t)ValID);
   1651   emitSignedInt64(Vals, diff);
   1652 }
   1653 
   1654 /// WriteInstruction - Emit an instruction to the specified stream.
   1655 static void WriteInstruction(const Instruction &I, unsigned InstID,
   1656                              ValueEnumerator &VE, BitstreamWriter &Stream,
   1657                              SmallVectorImpl<unsigned> &Vals) {
   1658   unsigned Code = 0;
   1659   unsigned AbbrevToUse = 0;
   1660   VE.setInstructionID(&I);
   1661   switch (I.getOpcode()) {
   1662   default:
   1663     if (Instruction::isCast(I.getOpcode())) {
   1664       Code = bitc::FUNC_CODE_INST_CAST;
   1665       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1666         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
   1667       Vals.push_back(VE.getTypeID(I.getType()));
   1668       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
   1669     } else {
   1670       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
   1671       Code = bitc::FUNC_CODE_INST_BINOP;
   1672       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1673         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
   1674       pushValue(I.getOperand(1), InstID, Vals, VE);
   1675       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
   1676       uint64_t Flags = GetOptimizationFlags(&I);
   1677       if (Flags != 0) {
   1678         if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
   1679           AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
   1680         Vals.push_back(Flags);
   1681       }
   1682     }
   1683     break;
   1684 
   1685   case Instruction::GetElementPtr: {
   1686     Code = bitc::FUNC_CODE_INST_GEP;
   1687     AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
   1688     auto &GEPInst = cast<GetElementPtrInst>(I);
   1689     Vals.push_back(GEPInst.isInBounds());
   1690     Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
   1691     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
   1692       PushValueAndType(I.getOperand(i), InstID, Vals, VE);
   1693     break;
   1694   }
   1695   case Instruction::ExtractValue: {
   1696     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
   1697     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1698     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
   1699     Vals.append(EVI->idx_begin(), EVI->idx_end());
   1700     break;
   1701   }
   1702   case Instruction::InsertValue: {
   1703     Code = bitc::FUNC_CODE_INST_INSERTVAL;
   1704     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1705     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
   1706     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
   1707     Vals.append(IVI->idx_begin(), IVI->idx_end());
   1708     break;
   1709   }
   1710   case Instruction::Select:
   1711     Code = bitc::FUNC_CODE_INST_VSELECT;
   1712     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
   1713     pushValue(I.getOperand(2), InstID, Vals, VE);
   1714     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1715     break;
   1716   case Instruction::ExtractElement:
   1717     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
   1718     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1719     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
   1720     break;
   1721   case Instruction::InsertElement:
   1722     Code = bitc::FUNC_CODE_INST_INSERTELT;
   1723     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1724     pushValue(I.getOperand(1), InstID, Vals, VE);
   1725     PushValueAndType(I.getOperand(2), InstID, Vals, VE);
   1726     break;
   1727   case Instruction::ShuffleVector:
   1728     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
   1729     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1730     pushValue(I.getOperand(1), InstID, Vals, VE);
   1731     pushValue(I.getOperand(2), InstID, Vals, VE);
   1732     break;
   1733   case Instruction::ICmp:
   1734   case Instruction::FCmp:
   1735     // compare returning Int1Ty or vector of Int1Ty
   1736     Code = bitc::FUNC_CODE_INST_CMP2;
   1737     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1738     pushValue(I.getOperand(1), InstID, Vals, VE);
   1739     Vals.push_back(cast<CmpInst>(I).getPredicate());
   1740     break;
   1741 
   1742   case Instruction::Ret:
   1743     {
   1744       Code = bitc::FUNC_CODE_INST_RET;
   1745       unsigned NumOperands = I.getNumOperands();
   1746       if (NumOperands == 0)
   1747         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
   1748       else if (NumOperands == 1) {
   1749         if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1750           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
   1751       } else {
   1752         for (unsigned i = 0, e = NumOperands; i != e; ++i)
   1753           PushValueAndType(I.getOperand(i), InstID, Vals, VE);
   1754       }
   1755     }
   1756     break;
   1757   case Instruction::Br:
   1758     {
   1759       Code = bitc::FUNC_CODE_INST_BR;
   1760       const BranchInst &II = cast<BranchInst>(I);
   1761       Vals.push_back(VE.getValueID(II.getSuccessor(0)));
   1762       if (II.isConditional()) {
   1763         Vals.push_back(VE.getValueID(II.getSuccessor(1)));
   1764         pushValue(II.getCondition(), InstID, Vals, VE);
   1765       }
   1766     }
   1767     break;
   1768   case Instruction::Switch:
   1769     {
   1770       Code = bitc::FUNC_CODE_INST_SWITCH;
   1771       const SwitchInst &SI = cast<SwitchInst>(I);
   1772       Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
   1773       pushValue(SI.getCondition(), InstID, Vals, VE);
   1774       Vals.push_back(VE.getValueID(SI.getDefaultDest()));
   1775       for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
   1776            i != e; ++i) {
   1777         Vals.push_back(VE.getValueID(i.getCaseValue()));
   1778         Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
   1779       }
   1780     }
   1781     break;
   1782   case Instruction::IndirectBr:
   1783     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
   1784     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
   1785     // Encode the address operand as relative, but not the basic blocks.
   1786     pushValue(I.getOperand(0), InstID, Vals, VE);
   1787     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
   1788       Vals.push_back(VE.getValueID(I.getOperand(i)));
   1789     break;
   1790 
   1791   case Instruction::Invoke: {
   1792     const InvokeInst *II = cast<InvokeInst>(&I);
   1793     const Value *Callee(II->getCalledValue());
   1794     PointerType *PTy = cast<PointerType>(Callee->getType());
   1795     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
   1796     Code = bitc::FUNC_CODE_INST_INVOKE;
   1797 
   1798     Vals.push_back(VE.getAttributeID(II->getAttributes()));
   1799     Vals.push_back(II->getCallingConv());
   1800     Vals.push_back(VE.getValueID(II->getNormalDest()));
   1801     Vals.push_back(VE.getValueID(II->getUnwindDest()));
   1802     PushValueAndType(Callee, InstID, Vals, VE);
   1803 
   1804     // Emit value #'s for the fixed parameters.
   1805     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
   1806       pushValue(I.getOperand(i), InstID, Vals, VE);  // fixed param.
   1807 
   1808     // Emit type/value pairs for varargs params.
   1809     if (FTy->isVarArg()) {
   1810       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
   1811            i != e; ++i)
   1812         PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
   1813     }
   1814     break;
   1815   }
   1816   case Instruction::Resume:
   1817     Code = bitc::FUNC_CODE_INST_RESUME;
   1818     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1819     break;
   1820   case Instruction::Unreachable:
   1821     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
   1822     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
   1823     break;
   1824 
   1825   case Instruction::PHI: {
   1826     const PHINode &PN = cast<PHINode>(I);
   1827     Code = bitc::FUNC_CODE_INST_PHI;
   1828     // With the newer instruction encoding, forward references could give
   1829     // negative valued IDs.  This is most common for PHIs, so we use
   1830     // signed VBRs.
   1831     SmallVector<uint64_t, 128> Vals64;
   1832     Vals64.push_back(VE.getTypeID(PN.getType()));
   1833     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
   1834       pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
   1835       Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
   1836     }
   1837     // Emit a Vals64 vector and exit.
   1838     Stream.EmitRecord(Code, Vals64, AbbrevToUse);
   1839     Vals64.clear();
   1840     return;
   1841   }
   1842 
   1843   case Instruction::LandingPad: {
   1844     const LandingPadInst &LP = cast<LandingPadInst>(I);
   1845     Code = bitc::FUNC_CODE_INST_LANDINGPAD;
   1846     Vals.push_back(VE.getTypeID(LP.getType()));
   1847     PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
   1848     Vals.push_back(LP.isCleanup());
   1849     Vals.push_back(LP.getNumClauses());
   1850     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
   1851       if (LP.isCatch(I))
   1852         Vals.push_back(LandingPadInst::Catch);
   1853       else
   1854         Vals.push_back(LandingPadInst::Filter);
   1855       PushValueAndType(LP.getClause(I), InstID, Vals, VE);
   1856     }
   1857     break;
   1858   }
   1859 
   1860   case Instruction::Alloca: {
   1861     Code = bitc::FUNC_CODE_INST_ALLOCA;
   1862     Vals.push_back(VE.getTypeID(I.getType()));
   1863     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
   1864     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
   1865     const AllocaInst &AI = cast<AllocaInst>(I);
   1866     unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
   1867     assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
   1868            "not enough bits for maximum alignment");
   1869     assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
   1870     AlignRecord |= AI.isUsedWithInAlloca() << 5;
   1871     Vals.push_back(AlignRecord);
   1872     break;
   1873   }
   1874 
   1875   case Instruction::Load:
   1876     if (cast<LoadInst>(I).isAtomic()) {
   1877       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
   1878       PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1879     } else {
   1880       Code = bitc::FUNC_CODE_INST_LOAD;
   1881       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))  // ptr
   1882         AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
   1883     }
   1884     Vals.push_back(VE.getTypeID(I.getType()));
   1885     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
   1886     Vals.push_back(cast<LoadInst>(I).isVolatile());
   1887     if (cast<LoadInst>(I).isAtomic()) {
   1888       Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
   1889       Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
   1890     }
   1891     break;
   1892   case Instruction::Store:
   1893     if (cast<StoreInst>(I).isAtomic())
   1894       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
   1895     else
   1896       Code = bitc::FUNC_CODE_INST_STORE;
   1897     PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
   1898     pushValue(I.getOperand(0), InstID, Vals, VE);         // val.
   1899     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
   1900     Vals.push_back(cast<StoreInst>(I).isVolatile());
   1901     if (cast<StoreInst>(I).isAtomic()) {
   1902       Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
   1903       Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
   1904     }
   1905     break;
   1906   case Instruction::AtomicCmpXchg:
   1907     Code = bitc::FUNC_CODE_INST_CMPXCHG;
   1908     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
   1909     pushValue(I.getOperand(1), InstID, Vals, VE);         // cmp.
   1910     pushValue(I.getOperand(2), InstID, Vals, VE);         // newval.
   1911     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
   1912     Vals.push_back(GetEncodedOrdering(
   1913                      cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
   1914     Vals.push_back(GetEncodedSynchScope(
   1915                      cast<AtomicCmpXchgInst>(I).getSynchScope()));
   1916     Vals.push_back(GetEncodedOrdering(
   1917                      cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
   1918     Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
   1919     break;
   1920   case Instruction::AtomicRMW:
   1921     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
   1922     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
   1923     pushValue(I.getOperand(1), InstID, Vals, VE);         // val.
   1924     Vals.push_back(GetEncodedRMWOperation(
   1925                      cast<AtomicRMWInst>(I).getOperation()));
   1926     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
   1927     Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
   1928     Vals.push_back(GetEncodedSynchScope(
   1929                      cast<AtomicRMWInst>(I).getSynchScope()));
   1930     break;
   1931   case Instruction::Fence:
   1932     Code = bitc::FUNC_CODE_INST_FENCE;
   1933     Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
   1934     Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
   1935     break;
   1936   case Instruction::Call: {
   1937     const CallInst &CI = cast<CallInst>(I);
   1938     PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
   1939     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
   1940 
   1941     Code = bitc::FUNC_CODE_INST_CALL;
   1942 
   1943     Vals.push_back(VE.getAttributeID(CI.getAttributes()));
   1944     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
   1945                    unsigned(CI.isMustTailCall()) << 14);
   1946     PushValueAndType(CI.getCalledValue(), InstID, Vals, VE);  // Callee
   1947 
   1948     // Emit value #'s for the fixed parameters.
   1949     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
   1950       // Check for labels (can happen with asm labels).
   1951       if (FTy->getParamType(i)->isLabelTy())
   1952         Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
   1953       else
   1954         pushValue(CI.getArgOperand(i), InstID, Vals, VE);  // fixed param.
   1955     }
   1956 
   1957     // Emit type/value pairs for varargs params.
   1958     if (FTy->isVarArg()) {
   1959       for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
   1960            i != e; ++i)
   1961         PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE);  // varargs
   1962     }
   1963     break;
   1964   }
   1965   case Instruction::VAArg:
   1966     Code = bitc::FUNC_CODE_INST_VAARG;
   1967     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
   1968     pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
   1969     Vals.push_back(VE.getTypeID(I.getType())); // restype.
   1970     break;
   1971   }
   1972 
   1973   Stream.EmitRecord(Code, Vals, AbbrevToUse);
   1974   Vals.clear();
   1975 }
   1976 
   1977 // Emit names for globals/functions etc.
   1978 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
   1979                                   const ValueEnumerator &VE,
   1980                                   BitstreamWriter &Stream) {
   1981   if (VST.empty()) return;
   1982   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
   1983 
   1984   // FIXME: Set up the abbrev, we know how many values there are!
   1985   // FIXME: We know if the type names can use 7-bit ascii.
   1986   SmallVector<unsigned, 64> NameVals;
   1987 
   1988   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
   1989        SI != SE; ++SI) {
   1990 
   1991     const ValueName &Name = *SI;
   1992 
   1993     // Figure out the encoding to use for the name.
   1994     bool is7Bit = true;
   1995     bool isChar6 = true;
   1996     for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
   1997          C != E; ++C) {
   1998       if (isChar6)
   1999         isChar6 = BitCodeAbbrevOp::isChar6(*C);
   2000       if ((unsigned char)*C & 128) {
   2001         is7Bit = false;
   2002         break;  // don't bother scanning the rest.
   2003       }
   2004     }
   2005 
   2006     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
   2007 
   2008     // VST_ENTRY:   [valueid, namechar x N]
   2009     // VST_BBENTRY: [bbid, namechar x N]
   2010     unsigned Code;
   2011     if (isa<BasicBlock>(SI->getValue())) {
   2012       Code = bitc::VST_CODE_BBENTRY;
   2013       if (isChar6)
   2014         AbbrevToUse = VST_BBENTRY_6_ABBREV;
   2015     } else {
   2016       Code = bitc::VST_CODE_ENTRY;
   2017       if (isChar6)
   2018         AbbrevToUse = VST_ENTRY_6_ABBREV;
   2019       else if (is7Bit)
   2020         AbbrevToUse = VST_ENTRY_7_ABBREV;
   2021     }
   2022 
   2023     NameVals.push_back(VE.getValueID(SI->getValue()));
   2024     for (const char *P = Name.getKeyData(),
   2025          *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
   2026       NameVals.push_back((unsigned char)*P);
   2027 
   2028     // Emit the finished record.
   2029     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
   2030     NameVals.clear();
   2031   }
   2032   Stream.ExitBlock();
   2033 }
   2034 
   2035 static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order,
   2036                          BitstreamWriter &Stream) {
   2037   assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
   2038   unsigned Code;
   2039   if (isa<BasicBlock>(Order.V))
   2040     Code = bitc::USELIST_CODE_BB;
   2041   else
   2042     Code = bitc::USELIST_CODE_DEFAULT;
   2043 
   2044   SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
   2045   Record.push_back(VE.getValueID(Order.V));
   2046   Stream.EmitRecord(Code, Record);
   2047 }
   2048 
   2049 static void WriteUseListBlock(const Function *F, ValueEnumerator &VE,
   2050                               BitstreamWriter &Stream) {
   2051   assert(VE.shouldPreserveUseListOrder() &&
   2052          "Expected to be preserving use-list order");
   2053 
   2054   auto hasMore = [&]() {
   2055     return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
   2056   };
   2057   if (!hasMore())
   2058     // Nothing to do.
   2059     return;
   2060 
   2061   Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
   2062   while (hasMore()) {
   2063     WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream);
   2064     VE.UseListOrders.pop_back();
   2065   }
   2066   Stream.ExitBlock();
   2067 }
   2068 
   2069 /// WriteFunction - Emit a function body to the module stream.
   2070 static void WriteFunction(const Function &F, ValueEnumerator &VE,
   2071                           BitstreamWriter &Stream) {
   2072   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
   2073   VE.incorporateFunction(F);
   2074 
   2075   SmallVector<unsigned, 64> Vals;
   2076 
   2077   // Emit the number of basic blocks, so the reader can create them ahead of
   2078   // time.
   2079   Vals.push_back(VE.getBasicBlocks().size());
   2080   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
   2081   Vals.clear();
   2082 
   2083   // If there are function-local constants, emit them now.
   2084   unsigned CstStart, CstEnd;
   2085   VE.getFunctionConstantRange(CstStart, CstEnd);
   2086   WriteConstants(CstStart, CstEnd, VE, Stream, false);
   2087 
   2088   // If there is function-local metadata, emit it now.
   2089   WriteFunctionLocalMetadata(F, VE, Stream);
   2090 
   2091   // Keep a running idea of what the instruction ID is.
   2092   unsigned InstID = CstEnd;
   2093 
   2094   bool NeedsMetadataAttachment = false;
   2095 
   2096   MDLocation *LastDL = nullptr;
   2097 
   2098   // Finally, emit all the instructions, in order.
   2099   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
   2100     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
   2101          I != E; ++I) {
   2102       WriteInstruction(*I, InstID, VE, Stream, Vals);
   2103 
   2104       if (!I->getType()->isVoidTy())
   2105         ++InstID;
   2106 
   2107       // If the instruction has metadata, write a metadata attachment later.
   2108       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
   2109 
   2110       // If the instruction has a debug location, emit it.
   2111       MDLocation *DL = I->getDebugLoc();
   2112       if (!DL)
   2113         continue;
   2114 
   2115       if (DL == LastDL) {
   2116         // Just repeat the same debug loc as last time.
   2117         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
   2118         continue;
   2119       }
   2120 
   2121       Vals.push_back(DL->getLine());
   2122       Vals.push_back(DL->getColumn());
   2123       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
   2124       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
   2125       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
   2126       Vals.clear();
   2127     }
   2128 
   2129   // Emit names for all the instructions etc.
   2130   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
   2131 
   2132   if (NeedsMetadataAttachment)
   2133     WriteMetadataAttachment(F, VE, Stream);
   2134   if (VE.shouldPreserveUseListOrder())
   2135     WriteUseListBlock(&F, VE, Stream);
   2136   VE.purgeFunction();
   2137   Stream.ExitBlock();
   2138 }
   2139 
   2140 // Emit blockinfo, which defines the standard abbreviations etc.
   2141 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
   2142   // We only want to emit block info records for blocks that have multiple
   2143   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
   2144   // Other blocks can define their abbrevs inline.
   2145   Stream.EnterBlockInfoBlock(2);
   2146 
   2147   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
   2148     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2149     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
   2150     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   2151     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   2152     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
   2153     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   2154                                    Abbv) != VST_ENTRY_8_ABBREV)
   2155       llvm_unreachable("Unexpected abbrev ordering!");
   2156   }
   2157 
   2158   { // 7-bit fixed width VST_ENTRY strings.
   2159     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2160     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
   2161     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   2162     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   2163     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
   2164     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   2165                                    Abbv) != VST_ENTRY_7_ABBREV)
   2166       llvm_unreachable("Unexpected abbrev ordering!");
   2167   }
   2168   { // 6-bit char6 VST_ENTRY strings.
   2169     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2170     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
   2171     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   2172     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   2173     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
   2174     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   2175                                    Abbv) != VST_ENTRY_6_ABBREV)
   2176       llvm_unreachable("Unexpected abbrev ordering!");
   2177   }
   2178   { // 6-bit char6 VST_BBENTRY strings.
   2179     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2180     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
   2181     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   2182     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   2183     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
   2184     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   2185                                    Abbv) != VST_BBENTRY_6_ABBREV)
   2186       llvm_unreachable("Unexpected abbrev ordering!");
   2187   }
   2188 
   2189 
   2190 
   2191   { // SETTYPE abbrev for CONSTANTS_BLOCK.
   2192     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2193     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
   2194     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
   2195                               VE.computeBitsRequiredForTypeIndicies()));
   2196     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   2197                                    Abbv) != CONSTANTS_SETTYPE_ABBREV)
   2198       llvm_unreachable("Unexpected abbrev ordering!");
   2199   }
   2200 
   2201   { // INTEGER abbrev for CONSTANTS_BLOCK.
   2202     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2203     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
   2204     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   2205     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   2206                                    Abbv) != CONSTANTS_INTEGER_ABBREV)
   2207       llvm_unreachable("Unexpected abbrev ordering!");
   2208   }
   2209 
   2210   { // CE_CAST abbrev for CONSTANTS_BLOCK.
   2211     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2212     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
   2213     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
   2214     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
   2215                               VE.computeBitsRequiredForTypeIndicies()));
   2216     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
   2217 
   2218     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   2219                                    Abbv) != CONSTANTS_CE_CAST_Abbrev)
   2220       llvm_unreachable("Unexpected abbrev ordering!");
   2221   }
   2222   { // NULL abbrev for CONSTANTS_BLOCK.
   2223     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2224     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
   2225     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   2226                                    Abbv) != CONSTANTS_NULL_Abbrev)
   2227       llvm_unreachable("Unexpected abbrev ordering!");
   2228   }
   2229 
   2230   // FIXME: This should only use space for first class types!
   2231 
   2232   { // INST_LOAD abbrev for FUNCTION_BLOCK.
   2233     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2234     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
   2235     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
   2236     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
   2237                               VE.computeBitsRequiredForTypeIndicies()));
   2238     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
   2239     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
   2240     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2241                                    Abbv) != FUNCTION_INST_LOAD_ABBREV)
   2242       llvm_unreachable("Unexpected abbrev ordering!");
   2243   }
   2244   { // INST_BINOP abbrev for FUNCTION_BLOCK.
   2245     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2246     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
   2247     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
   2248     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
   2249     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
   2250     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2251                                    Abbv) != FUNCTION_INST_BINOP_ABBREV)
   2252       llvm_unreachable("Unexpected abbrev ordering!");
   2253   }
   2254   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
   2255     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2256     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
   2257     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
   2258     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
   2259     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
   2260     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
   2261     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2262                                    Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
   2263       llvm_unreachable("Unexpected abbrev ordering!");
   2264   }
   2265   { // INST_CAST abbrev for FUNCTION_BLOCK.
   2266     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2267     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
   2268     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
   2269     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
   2270                               VE.computeBitsRequiredForTypeIndicies()));
   2271     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
   2272     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2273                                    Abbv) != FUNCTION_INST_CAST_ABBREV)
   2274       llvm_unreachable("Unexpected abbrev ordering!");
   2275   }
   2276 
   2277   { // INST_RET abbrev for FUNCTION_BLOCK.
   2278     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2279     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
   2280     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2281                                    Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
   2282       llvm_unreachable("Unexpected abbrev ordering!");
   2283   }
   2284   { // INST_RET abbrev for FUNCTION_BLOCK.
   2285     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2286     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
   2287     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
   2288     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2289                                    Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
   2290       llvm_unreachable("Unexpected abbrev ordering!");
   2291   }
   2292   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
   2293     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2294     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
   2295     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   2296                                    Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
   2297       llvm_unreachable("Unexpected abbrev ordering!");
   2298   }
   2299   {
   2300     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   2301     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
   2302     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
   2303     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
   2304                               Log2_32_Ceil(VE.getTypes().size() + 1)));
   2305     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   2306     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   2307     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
   2308         FUNCTION_INST_GEP_ABBREV)
   2309       llvm_unreachable("Unexpected abbrev ordering!");
   2310   }
   2311 
   2312   Stream.ExitBlock();
   2313 }
   2314 
   2315 /// WriteModule - Emit the specified module to the bitstream.
   2316 static void WriteModule(const Module *M, BitstreamWriter &Stream,
   2317                         bool ShouldPreserveUseListOrder) {
   2318   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
   2319 
   2320   SmallVector<unsigned, 1> Vals;
   2321   unsigned CurVersion = 1;
   2322   Vals.push_back(CurVersion);
   2323   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
   2324 
   2325   // Analyze the module, enumerating globals, functions, etc.
   2326   ValueEnumerator VE(*M, ShouldPreserveUseListOrder);
   2327 
   2328   // Emit blockinfo, which defines the standard abbreviations etc.
   2329   WriteBlockInfo(VE, Stream);
   2330 
   2331   // Emit information about attribute groups.
   2332   WriteAttributeGroupTable(VE, Stream);
   2333 
   2334   // Emit information about parameter attributes.
   2335   WriteAttributeTable(VE, Stream);
   2336 
   2337   // Emit information describing all of the types in the module.
   2338   WriteTypeTable(VE, Stream);
   2339 
   2340   writeComdats(VE, Stream);
   2341 
   2342   // Emit top-level description of module, including target triple, inline asm,
   2343   // descriptors for global variables, and function prototype info.
   2344   WriteModuleInfo(M, VE, Stream);
   2345 
   2346   // Emit constants.
   2347   WriteModuleConstants(VE, Stream);
   2348 
   2349   // Emit metadata.
   2350   WriteModuleMetadata(M, VE, Stream);
   2351 
   2352   // Emit metadata.
   2353   WriteModuleMetadataStore(M, Stream);
   2354 
   2355   // Emit names for globals/functions etc.
   2356   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
   2357 
   2358   // Emit module-level use-lists.
   2359   if (VE.shouldPreserveUseListOrder())
   2360     WriteUseListBlock(nullptr, VE, Stream);
   2361 
   2362   // Emit function bodies.
   2363   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
   2364     if (!F->isDeclaration())
   2365       WriteFunction(*F, VE, Stream);
   2366 
   2367   Stream.ExitBlock();
   2368 }
   2369 
   2370 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
   2371 /// header and trailer to make it compatible with the system archiver.  To do
   2372 /// this we emit the following header, and then emit a trailer that pads the
   2373 /// file out to be a multiple of 16 bytes.
   2374 ///
   2375 /// struct bc_header {
   2376 ///   uint32_t Magic;         // 0x0B17C0DE
   2377 ///   uint32_t Version;       // Version, currently always 0.
   2378 ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
   2379 ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
   2380 ///   uint32_t CPUType;       // CPU specifier.
   2381 ///   ... potentially more later ...
   2382 /// };
   2383 enum {
   2384   DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
   2385   DarwinBCHeaderSize = 5*4
   2386 };
   2387 
   2388 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
   2389                                uint32_t &Position) {
   2390   Buffer[Position + 0] = (unsigned char) (Value >>  0);
   2391   Buffer[Position + 1] = (unsigned char) (Value >>  8);
   2392   Buffer[Position + 2] = (unsigned char) (Value >> 16);
   2393   Buffer[Position + 3] = (unsigned char) (Value >> 24);
   2394   Position += 4;
   2395 }
   2396 
   2397 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
   2398                                          const Triple &TT) {
   2399   unsigned CPUType = ~0U;
   2400 
   2401   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
   2402   // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
   2403   // number from /usr/include/mach/machine.h.  It is ok to reproduce the
   2404   // specific constants here because they are implicitly part of the Darwin ABI.
   2405   enum {
   2406     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
   2407     DARWIN_CPU_TYPE_X86        = 7,
   2408     DARWIN_CPU_TYPE_ARM        = 12,
   2409     DARWIN_CPU_TYPE_POWERPC    = 18
   2410   };
   2411 
   2412   Triple::ArchType Arch = TT.getArch();
   2413   if (Arch == Triple::x86_64)
   2414     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
   2415   else if (Arch == Triple::x86)
   2416     CPUType = DARWIN_CPU_TYPE_X86;
   2417   else if (Arch == Triple::ppc)
   2418     CPUType = DARWIN_CPU_TYPE_POWERPC;
   2419   else if (Arch == Triple::ppc64)
   2420     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
   2421   else if (Arch == Triple::arm || Arch == Triple::thumb)
   2422     CPUType = DARWIN_CPU_TYPE_ARM;
   2423 
   2424   // Traditional Bitcode starts after header.
   2425   assert(Buffer.size() >= DarwinBCHeaderSize &&
   2426          "Expected header size to be reserved");
   2427   unsigned BCOffset = DarwinBCHeaderSize;
   2428   unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
   2429 
   2430   // Write the magic and version.
   2431   unsigned Position = 0;
   2432   WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
   2433   WriteInt32ToBuffer(0          , Buffer, Position); // Version.
   2434   WriteInt32ToBuffer(BCOffset   , Buffer, Position);
   2435   WriteInt32ToBuffer(BCSize     , Buffer, Position);
   2436   WriteInt32ToBuffer(CPUType    , Buffer, Position);
   2437 
   2438   // If the file is not a multiple of 16 bytes, insert dummy padding.
   2439   while (Buffer.size() & 15)
   2440     Buffer.push_back(0);
   2441 }
   2442 
   2443 /// WriteBitcodeToFile - Write the specified module to the specified output
   2444 /// stream.
   2445 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
   2446                               bool ShouldPreserveUseListOrder) {
   2447   SmallVector<char, 0> Buffer;
   2448   Buffer.reserve(256*1024);
   2449 
   2450   // If this is darwin or another generic macho target, reserve space for the
   2451   // header.
   2452   Triple TT(M->getTargetTriple());
   2453   if (TT.isOSDarwin())
   2454     Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
   2455 
   2456   // Emit the module into the buffer.
   2457   {
   2458     BitstreamWriter Stream(Buffer);
   2459 
   2460     // Emit the file header.
   2461     Stream.Emit((unsigned)'B', 8);
   2462     Stream.Emit((unsigned)'C', 8);
   2463     Stream.Emit(0x0, 4);
   2464     Stream.Emit(0xC, 4);
   2465     Stream.Emit(0xE, 4);
   2466     Stream.Emit(0xD, 4);
   2467 
   2468     // Emit the module.
   2469     WriteModule(M, Stream, ShouldPreserveUseListOrder);
   2470   }
   2471 
   2472   if (TT.isOSDarwin())
   2473     EmitDarwinBCHeaderAndTrailer(Buffer, TT);
   2474 
   2475   // Write the generated bitstream to "Out".
   2476   Out.write((char*)&Buffer.front(), Buffer.size());
   2477 }
   2478