Home | History | Annotate | Download | only in BitWriter_3_2
      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 "ReaderWriter_3_2.h"
     15 #include "legacy_bitcode.h"
     16 #include "ValueEnumerator.h"
     17 #include "llvm/ADT/Triple.h"
     18 #include "llvm/Bitcode/BitstreamWriter.h"
     19 #include "llvm/Bitcode/LLVMBitCodes.h"
     20 #include "llvm/IR/Constants.h"
     21 #include "llvm/IR/DebugInfoMetadata.h"
     22 #include "llvm/IR/DerivedTypes.h"
     23 #include "llvm/IR/InlineAsm.h"
     24 #include "llvm/IR/Instructions.h"
     25 #include "llvm/IR/Module.h"
     26 #include "llvm/IR/Operator.h"
     27 #include "llvm/IR/UseListOrder.h"
     28 #include "llvm/IR/ValueSymbolTable.h"
     29 #include "llvm/Support/CommandLine.h"
     30 #include "llvm/Support/ErrorHandling.h"
     31 #include "llvm/Support/MathExtras.h"
     32 #include "llvm/Support/Program.h"
     33 #include "llvm/Support/raw_ostream.h"
     34 #include <cctype>
     35 #include <map>
     36 using namespace llvm;
     37 
     38 /// These are manifest constants used by the bitcode writer. They do not need to
     39 /// be kept in sync with the reader, but need to be consistent within this file.
     40 enum {
     41   // VALUE_SYMTAB_BLOCK abbrev id's.
     42   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     43   VST_ENTRY_7_ABBREV,
     44   VST_ENTRY_6_ABBREV,
     45   VST_BBENTRY_6_ABBREV,
     46 
     47   // CONSTANTS_BLOCK abbrev id's.
     48   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     49   CONSTANTS_INTEGER_ABBREV,
     50   CONSTANTS_CE_CAST_Abbrev,
     51   CONSTANTS_NULL_Abbrev,
     52 
     53   // FUNCTION_BLOCK abbrev id's.
     54   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
     55   FUNCTION_INST_BINOP_ABBREV,
     56   FUNCTION_INST_BINOP_FLAGS_ABBREV,
     57   FUNCTION_INST_CAST_ABBREV,
     58   FUNCTION_INST_RET_VOID_ABBREV,
     59   FUNCTION_INST_RET_VAL_ABBREV,
     60   FUNCTION_INST_UNREACHABLE_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   }
     79 }
     80 
     81 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
     82   switch (Opcode) {
     83   default: llvm_unreachable("Unknown binary instruction!");
     84   case Instruction::Add:
     85   case Instruction::FAdd: return bitc::BINOP_ADD;
     86   case Instruction::Sub:
     87   case Instruction::FSub: return bitc::BINOP_SUB;
     88   case Instruction::Mul:
     89   case Instruction::FMul: return bitc::BINOP_MUL;
     90   case Instruction::UDiv: return bitc::BINOP_UDIV;
     91   case Instruction::FDiv:
     92   case Instruction::SDiv: return bitc::BINOP_SDIV;
     93   case Instruction::URem: return bitc::BINOP_UREM;
     94   case Instruction::FRem:
     95   case Instruction::SRem: return bitc::BINOP_SREM;
     96   case Instruction::Shl:  return bitc::BINOP_SHL;
     97   case Instruction::LShr: return bitc::BINOP_LSHR;
     98   case Instruction::AShr: return bitc::BINOP_ASHR;
     99   case Instruction::And:  return bitc::BINOP_AND;
    100   case Instruction::Or:   return bitc::BINOP_OR;
    101   case Instruction::Xor:  return bitc::BINOP_XOR;
    102   }
    103 }
    104 
    105 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
    106   switch (Op) {
    107   default: llvm_unreachable("Unknown RMW operation!");
    108   case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
    109   case AtomicRMWInst::Add: return bitc::RMW_ADD;
    110   case AtomicRMWInst::Sub: return bitc::RMW_SUB;
    111   case AtomicRMWInst::And: return bitc::RMW_AND;
    112   case AtomicRMWInst::Nand: return bitc::RMW_NAND;
    113   case AtomicRMWInst::Or: return bitc::RMW_OR;
    114   case AtomicRMWInst::Xor: return bitc::RMW_XOR;
    115   case AtomicRMWInst::Max: return bitc::RMW_MAX;
    116   case AtomicRMWInst::Min: return bitc::RMW_MIN;
    117   case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
    118   case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
    119   }
    120 }
    121 
    122 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
    123   switch (Ordering) {
    124   case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC;
    125   case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED;
    126   case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC;
    127   case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE;
    128   case AtomicOrdering::Release: return bitc::ORDERING_RELEASE;
    129   case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL;
    130   case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST;
    131   }
    132   llvm_unreachable("Invalid ordering");
    133 }
    134 
    135 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
    136   switch (SynchScope) {
    137   case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
    138   case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
    139   }
    140   llvm_unreachable("Invalid synch scope");
    141 }
    142 
    143 static void WriteStringRecord(unsigned Code, StringRef Str,
    144                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
    145   SmallVector<unsigned, 64> Vals;
    146 
    147   // Code: [strchar x N]
    148   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
    149     if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
    150       AbbrevToUse = 0;
    151     Vals.push_back(Str[i]);
    152   }
    153 
    154   // Emit the finished record.
    155   Stream.EmitRecord(Code, Vals, AbbrevToUse);
    156 }
    157 
    158 // Emit information about parameter attributes.
    159 static void WriteAttributeTable(const llvm_3_2::ValueEnumerator &VE,
    160                                 BitstreamWriter &Stream) {
    161   const std::vector<AttributeSet> &Attrs = VE.getAttributes();
    162   if (Attrs.empty()) return;
    163 
    164   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
    165 
    166   SmallVector<uint64_t, 64> Record;
    167   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
    168     const AttributeSet &A = Attrs[i];
    169     for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
    170       Record.push_back(A.getSlotIndex(i));
    171       Record.push_back(encodeLLVMAttributesForBitcode(A, A.getSlotIndex(i)));
    172     }
    173 
    174     // This needs to use the 3.2 entry type
    175     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY_OLD, Record);
    176     Record.clear();
    177   }
    178 
    179   Stream.ExitBlock();
    180 }
    181 
    182 /// WriteTypeTable - Write out the type table for a module.
    183 static void WriteTypeTable(const llvm_3_2::ValueEnumerator &VE,
    184                            BitstreamWriter &Stream) {
    185   const llvm_3_2::ValueEnumerator::TypeList &TypeList = VE.getTypes();
    186 
    187   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
    188   SmallVector<uint64_t, 64> TypeVals;
    189 
    190   uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
    191 
    192   // Abbrev for TYPE_CODE_POINTER.
    193   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    194   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
    195   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    196   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
    197   unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
    198 
    199   // Abbrev for TYPE_CODE_FUNCTION.
    200   Abbv = new BitCodeAbbrev();
    201   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
    202   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
    203   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    204   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    205 
    206   unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
    207 
    208   // Abbrev for TYPE_CODE_STRUCT_ANON.
    209   Abbv = new BitCodeAbbrev();
    210   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
    211   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
    212   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    213   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    214 
    215   unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
    216 
    217   // Abbrev for TYPE_CODE_STRUCT_NAME.
    218   Abbv = new BitCodeAbbrev();
    219   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
    220   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    221   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    222   unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
    223 
    224   // Abbrev for TYPE_CODE_STRUCT_NAMED.
    225   Abbv = new BitCodeAbbrev();
    226   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
    227   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
    228   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    229   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    230 
    231   unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
    232 
    233   // Abbrev for TYPE_CODE_ARRAY.
    234   Abbv = new BitCodeAbbrev();
    235   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
    236   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
    237   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
    238 
    239   unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
    240 
    241   // Emit an entry count so the reader can reserve space.
    242   TypeVals.push_back(TypeList.size());
    243   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
    244   TypeVals.clear();
    245 
    246   // Loop over all of the types, emitting each in turn.
    247   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
    248     Type *T = TypeList[i];
    249     int AbbrevToUse = 0;
    250     unsigned Code = 0;
    251 
    252     switch (T->getTypeID()) {
    253     default: llvm_unreachable("Unknown type!");
    254     case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;      break;
    255     case Type::HalfTyID:      Code = bitc::TYPE_CODE_HALF;      break;
    256     case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;     break;
    257     case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE;    break;
    258     case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80;  break;
    259     case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128;     break;
    260     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
    261     case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;     break;
    262     case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA;  break;
    263     case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX;   break;
    264     case Type::IntegerTyID:
    265       // INTEGER: [width]
    266       Code = bitc::TYPE_CODE_INTEGER;
    267       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
    268       break;
    269     case Type::PointerTyID: {
    270       PointerType *PTy = cast<PointerType>(T);
    271       // POINTER: [pointee type, address space]
    272       Code = bitc::TYPE_CODE_POINTER;
    273       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
    274       unsigned AddressSpace = PTy->getAddressSpace();
    275       TypeVals.push_back(AddressSpace);
    276       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
    277       break;
    278     }
    279     case Type::FunctionTyID: {
    280       FunctionType *FT = cast<FunctionType>(T);
    281       // FUNCTION: [isvararg, retty, paramty x N]
    282       Code = bitc::TYPE_CODE_FUNCTION;
    283       TypeVals.push_back(FT->isVarArg());
    284       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
    285       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
    286         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
    287       AbbrevToUse = FunctionAbbrev;
    288       break;
    289     }
    290     case Type::StructTyID: {
    291       StructType *ST = cast<StructType>(T);
    292       // STRUCT: [ispacked, eltty x N]
    293       TypeVals.push_back(ST->isPacked());
    294       // Output all of the element types.
    295       for (StructType::element_iterator I = ST->element_begin(),
    296            E = ST->element_end(); I != E; ++I)
    297         TypeVals.push_back(VE.getTypeID(*I));
    298 
    299       if (ST->isLiteral()) {
    300         Code = bitc::TYPE_CODE_STRUCT_ANON;
    301         AbbrevToUse = StructAnonAbbrev;
    302       } else {
    303         if (ST->isOpaque()) {
    304           Code = bitc::TYPE_CODE_OPAQUE;
    305         } else {
    306           Code = bitc::TYPE_CODE_STRUCT_NAMED;
    307           AbbrevToUse = StructNamedAbbrev;
    308         }
    309 
    310         // Emit the name if it is present.
    311         if (!ST->getName().empty())
    312           WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
    313                             StructNameAbbrev, Stream);
    314       }
    315       break;
    316     }
    317     case Type::ArrayTyID: {
    318       ArrayType *AT = cast<ArrayType>(T);
    319       // ARRAY: [numelts, eltty]
    320       Code = bitc::TYPE_CODE_ARRAY;
    321       TypeVals.push_back(AT->getNumElements());
    322       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
    323       AbbrevToUse = ArrayAbbrev;
    324       break;
    325     }
    326     case Type::VectorTyID: {
    327       VectorType *VT = cast<VectorType>(T);
    328       // VECTOR [numelts, eltty]
    329       Code = bitc::TYPE_CODE_VECTOR;
    330       TypeVals.push_back(VT->getNumElements());
    331       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
    332       break;
    333     }
    334     }
    335 
    336     // Emit the finished record.
    337     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
    338     TypeVals.clear();
    339   }
    340 
    341   Stream.ExitBlock();
    342 }
    343 
    344 static unsigned getEncodedLinkage(const GlobalValue &GV) {
    345   switch (GV.getLinkage()) {
    346   case GlobalValue::ExternalLinkage:
    347     return 0;
    348   case GlobalValue::WeakAnyLinkage:
    349     return 1;
    350   case GlobalValue::AppendingLinkage:
    351     return 2;
    352   case GlobalValue::InternalLinkage:
    353     return 3;
    354   case GlobalValue::LinkOnceAnyLinkage:
    355     return 4;
    356   case GlobalValue::ExternalWeakLinkage:
    357     return 7;
    358   case GlobalValue::CommonLinkage:
    359     return 8;
    360   case GlobalValue::PrivateLinkage:
    361     return 9;
    362   case GlobalValue::WeakODRLinkage:
    363     return 10;
    364   case GlobalValue::LinkOnceODRLinkage:
    365     return 11;
    366   case GlobalValue::AvailableExternallyLinkage:
    367     return 12;
    368   }
    369   llvm_unreachable("Invalid linkage");
    370 }
    371 
    372 static unsigned getEncodedVisibility(const GlobalValue &GV) {
    373   switch (GV.getVisibility()) {
    374   case GlobalValue::DefaultVisibility:   return 0;
    375   case GlobalValue::HiddenVisibility:    return 1;
    376   case GlobalValue::ProtectedVisibility: return 2;
    377   }
    378   llvm_unreachable("Invalid visibility");
    379 }
    380 
    381 static unsigned getEncodedThreadLocalMode(const GlobalVariable &GV) {
    382   switch (GV.getThreadLocalMode()) {
    383     case GlobalVariable::NotThreadLocal:         return 0;
    384     case GlobalVariable::GeneralDynamicTLSModel: return 1;
    385     case GlobalVariable::LocalDynamicTLSModel:   return 2;
    386     case GlobalVariable::InitialExecTLSModel:    return 3;
    387     case GlobalVariable::LocalExecTLSModel:      return 4;
    388   }
    389   llvm_unreachable("Invalid TLS model");
    390 }
    391 
    392 // Emit top-level description of module, including target triple, inline asm,
    393 // descriptors for global variables, and function prototype info.
    394 static void WriteModuleInfo(const Module *M,
    395                             const llvm_3_2::ValueEnumerator &VE,
    396                             BitstreamWriter &Stream) {
    397   // Emit various pieces of data attached to a module.
    398   if (!M->getTargetTriple().empty())
    399     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
    400                       0/*TODO*/, Stream);
    401   const std::string &DL = M->getDataLayoutStr();
    402   if (!DL.empty())
    403     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
    404   if (!M->getModuleInlineAsm().empty())
    405     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
    406                       0/*TODO*/, Stream);
    407 
    408   // Emit information about sections and GC, computing how many there are. Also
    409   // compute the maximum alignment value.
    410   std::map<std::string, unsigned> SectionMap;
    411   std::map<std::string, unsigned> GCMap;
    412   unsigned MaxAlignment = 0;
    413   unsigned MaxGlobalType = 0;
    414   for (const GlobalValue &GV : M->globals()) {
    415     MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
    416     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
    417     if (GV.hasSection()) {
    418       // Give section names unique ID's.
    419       unsigned &Entry = SectionMap[GV.getSection()];
    420       if (!Entry) {
    421         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
    422                           0/*TODO*/, Stream);
    423         Entry = SectionMap.size();
    424       }
    425     }
    426   }
    427   for (const Function &F : *M) {
    428     MaxAlignment = std::max(MaxAlignment, F.getAlignment());
    429     if (F.hasSection()) {
    430       // Give section names unique ID's.
    431       unsigned &Entry = SectionMap[F.getSection()];
    432       if (!Entry) {
    433         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
    434                           0/*TODO*/, Stream);
    435         Entry = SectionMap.size();
    436       }
    437     }
    438     if (F.hasGC()) {
    439       // Same for GC names.
    440       unsigned &Entry = GCMap[F.getGC()];
    441       if (!Entry) {
    442         WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
    443                           0/*TODO*/, Stream);
    444         Entry = GCMap.size();
    445       }
    446     }
    447   }
    448 
    449   // Emit abbrev for globals, now that we know # sections and max alignment.
    450   unsigned SimpleGVarAbbrev = 0;
    451   if (!M->global_empty()) {
    452     // Add an abbrev for common globals with no visibility or thread localness.
    453     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    454     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
    455     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    456                               Log2_32_Ceil(MaxGlobalType+1)));
    457     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
    458     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
    459     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));      // Linkage.
    460     if (MaxAlignment == 0)                                      // Alignment.
    461       Abbv->Add(BitCodeAbbrevOp(0));
    462     else {
    463       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
    464       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    465                                Log2_32_Ceil(MaxEncAlignment+1)));
    466     }
    467     if (SectionMap.empty())                                    // Section.
    468       Abbv->Add(BitCodeAbbrevOp(0));
    469     else
    470       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
    471                                Log2_32_Ceil(SectionMap.size()+1)));
    472     // Don't bother emitting vis + thread local.
    473     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
    474   }
    475 
    476   // Emit the global variable information.
    477   SmallVector<unsigned, 64> Vals;
    478   for (const GlobalVariable &GV : M->globals()) {
    479     unsigned AbbrevToUse = 0;
    480 
    481     // GLOBALVAR: [type, isconst, initid,
    482     //             linkage, alignment, section, visibility, threadlocal,
    483     //             unnamed_addr]
    484     Vals.push_back(VE.getTypeID(GV.getType()));
    485     Vals.push_back(GV.isConstant());
    486     Vals.push_back(GV.isDeclaration() ? 0 :
    487                    (VE.getValueID(GV.getInitializer()) + 1));
    488     Vals.push_back(getEncodedLinkage(GV));
    489     Vals.push_back(Log2_32(GV.getAlignment())+1);
    490     Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
    491     if (GV.isThreadLocal() ||
    492         GV.getVisibility() != GlobalValue::DefaultVisibility ||
    493         GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None ||
    494         GV.isExternallyInitialized()) {
    495       Vals.push_back(getEncodedVisibility(GV));
    496       Vals.push_back(getEncodedThreadLocalMode(GV));
    497       Vals.push_back(GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
    498       Vals.push_back(GV.isExternallyInitialized());
    499     } else {
    500       AbbrevToUse = SimpleGVarAbbrev;
    501     }
    502 
    503     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
    504     Vals.clear();
    505   }
    506 
    507   // Emit the function proto information.
    508   for (const Function &F : *M) {
    509     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
    510     //             section, visibility, gc, unnamed_addr]
    511     Vals.push_back(VE.getTypeID(F.getType()));
    512     Vals.push_back(F.getCallingConv());
    513     Vals.push_back(F.isDeclaration());
    514     Vals.push_back(getEncodedLinkage(F));
    515     Vals.push_back(VE.getAttributeID(F.getAttributes()));
    516     Vals.push_back(Log2_32(F.getAlignment())+1);
    517     Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
    518     Vals.push_back(getEncodedVisibility(F));
    519     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
    520     Vals.push_back(F.getUnnamedAddr() != GlobalValue::UnnamedAddr::None);
    521 
    522     unsigned AbbrevToUse = 0;
    523     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
    524     Vals.clear();
    525   }
    526 
    527   // Emit the alias information.
    528   for (const GlobalAlias &A : M->aliases()) {
    529     // ALIAS: [alias type, aliasee val#, linkage, visibility]
    530     Vals.push_back(VE.getTypeID(A.getType()));
    531     Vals.push_back(VE.getValueID(A.getAliasee()));
    532     Vals.push_back(getEncodedLinkage(A));
    533     Vals.push_back(getEncodedVisibility(A));
    534     unsigned AbbrevToUse = 0;
    535     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS_OLD, Vals, AbbrevToUse);
    536     Vals.clear();
    537   }
    538 }
    539 
    540 static uint64_t GetOptimizationFlags(const Value *V) {
    541   uint64_t Flags = 0;
    542 
    543   if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
    544     if (OBO->hasNoSignedWrap())
    545       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
    546     if (OBO->hasNoUnsignedWrap())
    547       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
    548   } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
    549     if (PEO->isExact())
    550       Flags |= 1 << bitc::PEO_EXACT;
    551   } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
    552     // FIXME(srhines): We don't handle fast math in llvm-rs-cc today.
    553     if (false) {
    554       if (FPMO->hasUnsafeAlgebra())
    555         Flags |= FastMathFlags::UnsafeAlgebra;
    556       if (FPMO->hasNoNaNs())
    557         Flags |= FastMathFlags::NoNaNs;
    558       if (FPMO->hasNoInfs())
    559         Flags |= FastMathFlags::NoInfs;
    560       if (FPMO->hasNoSignedZeros())
    561         Flags |= FastMathFlags::NoSignedZeros;
    562       if (FPMO->hasAllowReciprocal())
    563         Flags |= FastMathFlags::AllowReciprocal;
    564     }
    565   }
    566 
    567   return Flags;
    568 }
    569 
    570 static void WriteValueAsMetadata(const ValueAsMetadata *MD,
    571                                  const llvm_3_2::ValueEnumerator &VE,
    572                                  BitstreamWriter &Stream,
    573                                  SmallVectorImpl<uint64_t> &Record) {
    574   // Mimic an MDNode with a value as one operand.
    575   Value *V = MD->getValue();
    576   Record.push_back(VE.getTypeID(V->getType()));
    577   Record.push_back(VE.getValueID(V));
    578   Stream.EmitRecord(bitc::METADATA_OLD_NODE, Record, 0);
    579   Record.clear();
    580 }
    581 
    582 static void WriteMDTuple(const MDTuple *N, const llvm_3_2::ValueEnumerator &VE,
    583                          BitstreamWriter &Stream,
    584                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
    585   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
    586     Metadata *MD = N->getOperand(i);
    587     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
    588            "Unexpected function-local metadata");
    589     if (!MD) {
    590       // TODO(srhines): I don't believe this case can exist for RS.
    591       Record.push_back(VE.getTypeID(llvm::Type::getVoidTy(N->getContext())));
    592       Record.push_back(0);
    593     } else if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
    594       Record.push_back(VE.getTypeID(MDC->getType()));
    595       Record.push_back(VE.getValueID(MDC->getValue()));
    596     } else {
    597       Record.push_back(VE.getTypeID(
    598           llvm::Type::getMetadataTy(N->getContext())));
    599       Record.push_back(VE.getMetadataID(MD));
    600     }
    601   }
    602   Stream.EmitRecord(bitc::METADATA_OLD_NODE, Record, Abbrev);
    603   Record.clear();
    604 }
    605 
    606 /*static void WriteMDLocation(const MDLocation *N, const llvm_3_2::ValueEnumerator &VE,
    607                             BitstreamWriter &Stream,
    608                             SmallVectorImpl<uint64_t> &Record,
    609                             unsigned Abbrev) {
    610   Record.push_back(N->isDistinct());
    611   Record.push_back(N->getLine());
    612   Record.push_back(N->getColumn());
    613   Record.push_back(VE.getMetadataID(N->getScope()));
    614   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
    615 
    616   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
    617   Record.clear();
    618 }
    619 
    620 static void WriteGenericDebugNode(const GenericDebugNode *,
    621                                   const llvm_3_2::ValueEnumerator &, BitstreamWriter &,
    622                                   SmallVectorImpl<uint64_t> &, unsigned) {
    623   llvm_unreachable("unimplemented");
    624 }*/
    625 
    626 static void WriteModuleMetadata(const Module *M,
    627                                 const llvm_3_2::ValueEnumerator &VE,
    628                                 BitstreamWriter &Stream) {
    629   const auto &MDs = VE.getMDs();
    630   if (MDs.empty() && M->named_metadata_empty())
    631     return;
    632 
    633   // RenderScript files *ALWAYS* have metadata!
    634   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
    635 
    636   unsigned MDSAbbrev = 0;
    637   if (VE.hasMDString()) {
    638     // Abbrev for METADATA_STRING.
    639     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    640     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING_OLD));
    641     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    642     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    643     MDSAbbrev = Stream.EmitAbbrev(Abbv);
    644   }
    645 
    646   unsigned MDLocationAbbrev = 0;
    647   if (VE.hasDILocation()) {
    648     // TODO(srhines): Should be unreachable for RenderScript.
    649     // Abbrev for METADATA_LOCATION.
    650     //
    651     // Assume the column is usually under 128, and always output the inlined-at
    652     // location (it's never more expensive than building an array size 1).
    653     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    654     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
    655     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
    656     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    657     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
    658     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    659     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
    660     MDLocationAbbrev = Stream.EmitAbbrev(Abbv);
    661   }
    662 
    663   unsigned NameAbbrev = 0;
    664   if (!M->named_metadata_empty()) {
    665     // Abbrev for METADATA_NAME.
    666     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    667     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
    668     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    669     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    670     NameAbbrev = Stream.EmitAbbrev(Abbv);
    671   }
    672 
    673   unsigned MDTupleAbbrev = 0;
    674   //unsigned GenericDebugNodeAbbrev = 0;
    675   SmallVector<uint64_t, 64> Record;
    676   for (const Metadata *MD : MDs) {
    677     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
    678       switch (N->getMetadataID()) {
    679       default:
    680         llvm_unreachable("Invalid MDNode subclass");
    681 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)
    682 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
    683   case Metadata::CLASS##Kind:                                                  \
    684     Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev);           \
    685     continue;
    686 #include "llvm/IR/Metadata.def"
    687       }
    688     }
    689     if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
    690       WriteValueAsMetadata(MDC, VE, Stream, Record);
    691       continue;
    692     }
    693     const MDString *MDS = cast<MDString>(MD);
    694     // Code: [strchar x N]
    695     Record.append(MDS->bytes_begin(), MDS->bytes_end());
    696 
    697     // Emit the finished record.
    698     Stream.EmitRecord(bitc::METADATA_STRING_OLD, Record, MDSAbbrev);
    699     Record.clear();
    700   }
    701 
    702   // Write named metadata.
    703   for (const NamedMDNode &NMD : M->named_metadata()) {
    704     // Write name.
    705     StringRef Str = NMD.getName();
    706     Record.append(Str.bytes_begin(), Str.bytes_end());
    707     Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
    708     Record.clear();
    709 
    710     // Write named metadata operands.
    711     for (const MDNode *N : NMD.operands())
    712       Record.push_back(VE.getMetadataID(N));
    713     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
    714     Record.clear();
    715   }
    716 
    717   Stream.ExitBlock();
    718 }
    719 
    720 static void WriteFunctionLocalMetadata(const Function &F,
    721                                        const llvm_3_2::ValueEnumerator &VE,
    722                                        BitstreamWriter &Stream) {
    723   bool StartedMetadataBlock = false;
    724   SmallVector<uint64_t, 64> Record;
    725   const SmallVectorImpl<const LocalAsMetadata *> &MDs =
    726       VE.getFunctionLocalMDs();
    727   for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
    728     assert(MDs[i] && "Expected valid function-local metadata");
    729     if (!StartedMetadataBlock) {
    730       Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
    731       StartedMetadataBlock = true;
    732     }
    733     WriteValueAsMetadata(MDs[i], VE, Stream, Record);
    734   }
    735 
    736   if (StartedMetadataBlock)
    737     Stream.ExitBlock();
    738 }
    739 
    740 static void WriteMetadataAttachment(const Function &F,
    741                                     const llvm_3_2::ValueEnumerator &VE,
    742                                     BitstreamWriter &Stream) {
    743   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
    744 
    745   SmallVector<uint64_t, 64> Record;
    746 
    747   // Write metadata attachments
    748   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
    749   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    750 
    751   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
    752     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
    753          I != E; ++I) {
    754       MDs.clear();
    755       I->getAllMetadataOtherThanDebugLoc(MDs);
    756 
    757       // If no metadata, ignore instruction.
    758       if (MDs.empty()) continue;
    759 
    760       Record.push_back(VE.getInstructionID(&*I));
    761 
    762       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
    763         Record.push_back(MDs[i].first);
    764         Record.push_back(VE.getMetadataID(MDs[i].second));
    765       }
    766       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
    767       Record.clear();
    768     }
    769 
    770   Stream.ExitBlock();
    771 }
    772 
    773 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
    774   SmallVector<uint64_t, 64> Record;
    775 
    776   // Write metadata kinds
    777   // METADATA_KIND - [n x [id, name]]
    778   SmallVector<StringRef, 4> Names;
    779   M->getMDKindNames(Names);
    780 
    781   if (Names.empty()) return;
    782 
    783   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
    784 
    785   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
    786     Record.push_back(MDKindID);
    787     StringRef KName = Names[MDKindID];
    788     Record.append(KName.begin(), KName.end());
    789 
    790     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
    791     Record.clear();
    792   }
    793 
    794   Stream.ExitBlock();
    795 }
    796 
    797 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
    798   if ((int64_t)V >= 0)
    799     Vals.push_back(V << 1);
    800   else
    801     Vals.push_back((-V << 1) | 1);
    802 }
    803 
    804 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
    805                            const llvm_3_2::ValueEnumerator &VE,
    806                            BitstreamWriter &Stream, bool isGlobal) {
    807   if (FirstVal == LastVal) return;
    808 
    809   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
    810 
    811   unsigned AggregateAbbrev = 0;
    812   unsigned String8Abbrev = 0;
    813   unsigned CString7Abbrev = 0;
    814   unsigned CString6Abbrev = 0;
    815   // If this is a constant pool for the module, emit module-specific abbrevs.
    816   if (isGlobal) {
    817     // Abbrev for CST_CODE_AGGREGATE.
    818     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
    819     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
    820     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    821     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
    822     AggregateAbbrev = Stream.EmitAbbrev(Abbv);
    823 
    824     // Abbrev for CST_CODE_STRING.
    825     Abbv = new BitCodeAbbrev();
    826     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
    827     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    828     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
    829     String8Abbrev = Stream.EmitAbbrev(Abbv);
    830     // Abbrev for CST_CODE_CSTRING.
    831     Abbv = new BitCodeAbbrev();
    832     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
    833     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    834     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
    835     CString7Abbrev = Stream.EmitAbbrev(Abbv);
    836     // Abbrev for CST_CODE_CSTRING.
    837     Abbv = new BitCodeAbbrev();
    838     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
    839     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
    840     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
    841     CString6Abbrev = Stream.EmitAbbrev(Abbv);
    842   }
    843 
    844   SmallVector<uint64_t, 64> Record;
    845 
    846   const llvm_3_2::ValueEnumerator::ValueList &Vals = VE.getValues();
    847   Type *LastTy = nullptr;
    848   for (unsigned i = FirstVal; i != LastVal; ++i) {
    849     const Value *V = Vals[i].first;
    850     // If we need to switch types, do so now.
    851     if (V->getType() != LastTy) {
    852       LastTy = V->getType();
    853       Record.push_back(VE.getTypeID(LastTy));
    854       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
    855                         CONSTANTS_SETTYPE_ABBREV);
    856       Record.clear();
    857     }
    858 
    859     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
    860       Record.push_back(unsigned(IA->hasSideEffects()) |
    861                        unsigned(IA->isAlignStack()) << 1 |
    862                        unsigned(IA->getDialect()&1) << 2);
    863 
    864       // Add the asm string.
    865       const std::string &AsmStr = IA->getAsmString();
    866       Record.push_back(AsmStr.size());
    867       for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
    868         Record.push_back(AsmStr[i]);
    869 
    870       // Add the constraint string.
    871       const std::string &ConstraintStr = IA->getConstraintString();
    872       Record.push_back(ConstraintStr.size());
    873       for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
    874         Record.push_back(ConstraintStr[i]);
    875       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
    876       Record.clear();
    877       continue;
    878     }
    879     const Constant *C = cast<Constant>(V);
    880     unsigned Code = -1U;
    881     unsigned AbbrevToUse = 0;
    882     if (C->isNullValue()) {
    883       Code = bitc::CST_CODE_NULL;
    884     } else if (isa<UndefValue>(C)) {
    885       Code = bitc::CST_CODE_UNDEF;
    886     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
    887       if (IV->getBitWidth() <= 64) {
    888         uint64_t V = IV->getSExtValue();
    889         emitSignedInt64(Record, V);
    890         Code = bitc::CST_CODE_INTEGER;
    891         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
    892       } else {                             // Wide integers, > 64 bits in size.
    893         // We have an arbitrary precision integer value to write whose
    894         // bit width is > 64. However, in canonical unsigned integer
    895         // format it is likely that the high bits are going to be zero.
    896         // So, we only write the number of active words.
    897         unsigned NWords = IV->getValue().getActiveWords();
    898         const uint64_t *RawWords = IV->getValue().getRawData();
    899         for (unsigned i = 0; i != NWords; ++i) {
    900           emitSignedInt64(Record, RawWords[i]);
    901         }
    902         Code = bitc::CST_CODE_WIDE_INTEGER;
    903       }
    904     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
    905       Code = bitc::CST_CODE_FLOAT;
    906       Type *Ty = CFP->getType();
    907       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
    908         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
    909       } else if (Ty->isX86_FP80Ty()) {
    910         // api needed to prevent premature destruction
    911         // bits are not in the same order as a normal i80 APInt, compensate.
    912         APInt api = CFP->getValueAPF().bitcastToAPInt();
    913         const uint64_t *p = api.getRawData();
    914         Record.push_back((p[1] << 48) | (p[0] >> 16));
    915         Record.push_back(p[0] & 0xffffLL);
    916       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
    917         APInt api = CFP->getValueAPF().bitcastToAPInt();
    918         const uint64_t *p = api.getRawData();
    919         Record.push_back(p[0]);
    920         Record.push_back(p[1]);
    921       } else {
    922         assert (0 && "Unknown FP type!");
    923       }
    924     } else if (isa<ConstantDataSequential>(C) &&
    925                cast<ConstantDataSequential>(C)->isString()) {
    926       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
    927       // Emit constant strings specially.
    928       unsigned NumElts = Str->getNumElements();
    929       // If this is a null-terminated string, use the denser CSTRING encoding.
    930       if (Str->isCString()) {
    931         Code = bitc::CST_CODE_CSTRING;
    932         --NumElts;  // Don't encode the null, which isn't allowed by char6.
    933       } else {
    934         Code = bitc::CST_CODE_STRING;
    935         AbbrevToUse = String8Abbrev;
    936       }
    937       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
    938       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
    939       for (unsigned i = 0; i != NumElts; ++i) {
    940         unsigned char V = Str->getElementAsInteger(i);
    941         Record.push_back(V);
    942         isCStr7 &= (V & 128) == 0;
    943         if (isCStrChar6)
    944           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
    945       }
    946 
    947       if (isCStrChar6)
    948         AbbrevToUse = CString6Abbrev;
    949       else if (isCStr7)
    950         AbbrevToUse = CString7Abbrev;
    951     } else if (const ConstantDataSequential *CDS =
    952                   dyn_cast<ConstantDataSequential>(C)) {
    953       Code = bitc::CST_CODE_DATA;
    954       Type *EltTy = CDS->getType()->getElementType();
    955       if (isa<IntegerType>(EltTy)) {
    956         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
    957           Record.push_back(CDS->getElementAsInteger(i));
    958       } else {
    959         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
    960           Record.push_back(
    961               CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
    962       }
    963     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
    964                isa<ConstantVector>(C)) {
    965       Code = bitc::CST_CODE_AGGREGATE;
    966       for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
    967         Record.push_back(VE.getValueID(C->getOperand(i)));
    968       AbbrevToUse = AggregateAbbrev;
    969     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
    970       switch (CE->getOpcode()) {
    971       default:
    972         if (Instruction::isCast(CE->getOpcode())) {
    973           Code = bitc::CST_CODE_CE_CAST;
    974           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
    975           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
    976           Record.push_back(VE.getValueID(C->getOperand(0)));
    977           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
    978         } else {
    979           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
    980           Code = bitc::CST_CODE_CE_BINOP;
    981           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
    982           Record.push_back(VE.getValueID(C->getOperand(0)));
    983           Record.push_back(VE.getValueID(C->getOperand(1)));
    984           uint64_t Flags = GetOptimizationFlags(CE);
    985           if (Flags != 0)
    986             Record.push_back(Flags);
    987         }
    988         break;
    989       case Instruction::GetElementPtr:
    990         Code = bitc::CST_CODE_CE_GEP;
    991         if (cast<GEPOperator>(C)->isInBounds())
    992           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
    993         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
    994           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
    995           Record.push_back(VE.getValueID(C->getOperand(i)));
    996         }
    997         break;
    998       case Instruction::Select:
    999         Code = bitc::CST_CODE_CE_SELECT;
   1000         Record.push_back(VE.getValueID(C->getOperand(0)));
   1001         Record.push_back(VE.getValueID(C->getOperand(1)));
   1002         Record.push_back(VE.getValueID(C->getOperand(2)));
   1003         break;
   1004       case Instruction::ExtractElement:
   1005         Code = bitc::CST_CODE_CE_EXTRACTELT;
   1006         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1007         Record.push_back(VE.getValueID(C->getOperand(0)));
   1008         Record.push_back(VE.getValueID(C->getOperand(1)));
   1009         break;
   1010       case Instruction::InsertElement:
   1011         Code = bitc::CST_CODE_CE_INSERTELT;
   1012         Record.push_back(VE.getValueID(C->getOperand(0)));
   1013         Record.push_back(VE.getValueID(C->getOperand(1)));
   1014         Record.push_back(VE.getValueID(C->getOperand(2)));
   1015         break;
   1016       case Instruction::ShuffleVector:
   1017         // If the return type and argument types are the same, this is a
   1018         // standard shufflevector instruction.  If the types are different,
   1019         // then the shuffle is widening or truncating the input vectors, and
   1020         // the argument type must also be encoded.
   1021         if (C->getType() == C->getOperand(0)->getType()) {
   1022           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
   1023         } else {
   1024           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
   1025           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1026         }
   1027         Record.push_back(VE.getValueID(C->getOperand(0)));
   1028         Record.push_back(VE.getValueID(C->getOperand(1)));
   1029         Record.push_back(VE.getValueID(C->getOperand(2)));
   1030         break;
   1031       case Instruction::ICmp:
   1032       case Instruction::FCmp:
   1033         Code = bitc::CST_CODE_CE_CMP;
   1034         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
   1035         Record.push_back(VE.getValueID(C->getOperand(0)));
   1036         Record.push_back(VE.getValueID(C->getOperand(1)));
   1037         Record.push_back(CE->getPredicate());
   1038         break;
   1039       }
   1040     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
   1041       Code = bitc::CST_CODE_BLOCKADDRESS;
   1042       Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
   1043       Record.push_back(VE.getValueID(BA->getFunction()));
   1044       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
   1045     } else {
   1046 #ifndef NDEBUG
   1047       C->dump();
   1048 #endif
   1049       llvm_unreachable("Unknown constant!");
   1050     }
   1051     Stream.EmitRecord(Code, Record, AbbrevToUse);
   1052     Record.clear();
   1053   }
   1054 
   1055   Stream.ExitBlock();
   1056 }
   1057 
   1058 static void WriteModuleConstants(const llvm_3_2::ValueEnumerator &VE,
   1059                                  BitstreamWriter &Stream) {
   1060   const llvm_3_2::ValueEnumerator::ValueList &Vals = VE.getValues();
   1061 
   1062   // Find the first constant to emit, which is the first non-globalvalue value.
   1063   // We know globalvalues have been emitted by WriteModuleInfo.
   1064   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
   1065     if (!isa<GlobalValue>(Vals[i].first)) {
   1066       WriteConstants(i, Vals.size(), VE, Stream, true);
   1067       return;
   1068     }
   1069   }
   1070 }
   1071 
   1072 /// PushValueAndType - The file has to encode both the value and type id for
   1073 /// many values, because we need to know what type to create for forward
   1074 /// references.  However, most operands are not forward references, so this type
   1075 /// field is not needed.
   1076 ///
   1077 /// This function adds V's value ID to Vals.  If the value ID is higher than the
   1078 /// instruction ID, then it is a forward reference, and it also includes the
   1079 /// type ID.
   1080 static bool PushValueAndType(const Value *V, unsigned InstID,
   1081                              SmallVector<unsigned, 64> &Vals,
   1082                              llvm_3_2::ValueEnumerator &VE) {
   1083   unsigned ValID = VE.getValueID(V);
   1084   Vals.push_back(ValID);
   1085   if (ValID >= InstID) {
   1086     Vals.push_back(VE.getTypeID(V->getType()));
   1087     return true;
   1088   }
   1089   return false;
   1090 }
   1091 
   1092 /// WriteInstruction - Emit an instruction to the specified stream.
   1093 static void WriteInstruction(const Instruction &I, unsigned InstID,
   1094                              llvm_3_2::ValueEnumerator &VE,
   1095                              BitstreamWriter &Stream,
   1096                              SmallVector<unsigned, 64> &Vals) {
   1097   unsigned Code = 0;
   1098   unsigned AbbrevToUse = 0;
   1099   VE.setInstructionID(&I);
   1100   switch (I.getOpcode()) {
   1101   default:
   1102     if (Instruction::isCast(I.getOpcode())) {
   1103       Code = bitc::FUNC_CODE_INST_CAST;
   1104       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1105         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
   1106       Vals.push_back(VE.getTypeID(I.getType()));
   1107       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
   1108     } else {
   1109       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
   1110       Code = bitc::FUNC_CODE_INST_BINOP;
   1111       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1112         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
   1113       Vals.push_back(VE.getValueID(I.getOperand(1)));
   1114       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
   1115       uint64_t Flags = GetOptimizationFlags(&I);
   1116       if (Flags != 0) {
   1117         if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
   1118           AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
   1119         Vals.push_back(Flags);
   1120       }
   1121     }
   1122     break;
   1123 
   1124   case Instruction::GetElementPtr:
   1125     Code = bitc::FUNC_CODE_INST_GEP_OLD;
   1126     if (cast<GEPOperator>(&I)->isInBounds())
   1127       Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP_OLD;
   1128     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
   1129       PushValueAndType(I.getOperand(i), InstID, Vals, VE);
   1130     break;
   1131   case Instruction::ExtractValue: {
   1132     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
   1133     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1134     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
   1135     for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
   1136       Vals.push_back(*i);
   1137     break;
   1138   }
   1139   case Instruction::InsertValue: {
   1140     Code = bitc::FUNC_CODE_INST_INSERTVAL;
   1141     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1142     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
   1143     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
   1144     for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
   1145       Vals.push_back(*i);
   1146     break;
   1147   }
   1148   case Instruction::Select:
   1149     Code = bitc::FUNC_CODE_INST_VSELECT;
   1150     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
   1151     Vals.push_back(VE.getValueID(I.getOperand(2)));
   1152     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1153     break;
   1154   case Instruction::ExtractElement:
   1155     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
   1156     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1157     Vals.push_back(VE.getValueID(I.getOperand(1)));
   1158     break;
   1159   case Instruction::InsertElement:
   1160     Code = bitc::FUNC_CODE_INST_INSERTELT;
   1161     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1162     Vals.push_back(VE.getValueID(I.getOperand(1)));
   1163     Vals.push_back(VE.getValueID(I.getOperand(2)));
   1164     break;
   1165   case Instruction::ShuffleVector:
   1166     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
   1167     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1168     Vals.push_back(VE.getValueID(I.getOperand(1)));
   1169     Vals.push_back(VE.getValueID(I.getOperand(2)));
   1170     break;
   1171   case Instruction::ICmp:
   1172   case Instruction::FCmp:
   1173     // compare returning Int1Ty or vector of Int1Ty
   1174     Code = bitc::FUNC_CODE_INST_CMP2;
   1175     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1176     Vals.push_back(VE.getValueID(I.getOperand(1)));
   1177     Vals.push_back(cast<CmpInst>(I).getPredicate());
   1178     break;
   1179 
   1180   case Instruction::Ret:
   1181     {
   1182       Code = bitc::FUNC_CODE_INST_RET;
   1183       unsigned NumOperands = I.getNumOperands();
   1184       if (NumOperands == 0)
   1185         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
   1186       else if (NumOperands == 1) {
   1187         if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
   1188           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
   1189       } else {
   1190         for (unsigned i = 0, e = NumOperands; i != e; ++i)
   1191           PushValueAndType(I.getOperand(i), InstID, Vals, VE);
   1192       }
   1193     }
   1194     break;
   1195   case Instruction::Br:
   1196     {
   1197       Code = bitc::FUNC_CODE_INST_BR;
   1198       const BranchInst &II = cast<BranchInst>(I);
   1199       Vals.push_back(VE.getValueID(II.getSuccessor(0)));
   1200       if (II.isConditional()) {
   1201         Vals.push_back(VE.getValueID(II.getSuccessor(1)));
   1202         Vals.push_back(VE.getValueID(II.getCondition()));
   1203       }
   1204     }
   1205     break;
   1206   case Instruction::Switch:
   1207     {
   1208       Code = bitc::FUNC_CODE_INST_SWITCH;
   1209       const SwitchInst &SI = cast<SwitchInst>(I);
   1210       Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
   1211       Vals.push_back(VE.getValueID(SI.getCondition()));
   1212       Vals.push_back(VE.getValueID(SI.getDefaultDest()));
   1213       for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
   1214            i != e; ++i) {
   1215         Vals.push_back(VE.getValueID(i.getCaseValue()));
   1216         Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
   1217       }
   1218     }
   1219     break;
   1220   case Instruction::IndirectBr:
   1221     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
   1222     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
   1223     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
   1224       Vals.push_back(VE.getValueID(I.getOperand(i)));
   1225     break;
   1226 
   1227   case Instruction::Invoke: {
   1228     const InvokeInst *II = cast<InvokeInst>(&I);
   1229     const Value *Callee(II->getCalledValue());
   1230     PointerType *PTy = cast<PointerType>(Callee->getType());
   1231     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
   1232     Code = bitc::FUNC_CODE_INST_INVOKE;
   1233 
   1234     Vals.push_back(VE.getAttributeID(II->getAttributes()));
   1235     Vals.push_back(II->getCallingConv());
   1236     Vals.push_back(VE.getValueID(II->getNormalDest()));
   1237     Vals.push_back(VE.getValueID(II->getUnwindDest()));
   1238     PushValueAndType(Callee, InstID, Vals, VE);
   1239 
   1240     // Emit value #'s for the fixed parameters.
   1241     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
   1242       Vals.push_back(VE.getValueID(I.getOperand(i)));  // fixed param.
   1243 
   1244     // Emit type/value pairs for varargs params.
   1245     if (FTy->isVarArg()) {
   1246       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
   1247            i != e; ++i)
   1248         PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
   1249     }
   1250     break;
   1251   }
   1252   case Instruction::Resume:
   1253     Code = bitc::FUNC_CODE_INST_RESUME;
   1254     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1255     break;
   1256   case Instruction::Unreachable:
   1257     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
   1258     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
   1259     break;
   1260 
   1261   case Instruction::PHI: {
   1262     const PHINode &PN = cast<PHINode>(I);
   1263     Code = bitc::FUNC_CODE_INST_PHI;
   1264     Vals.push_back(VE.getTypeID(PN.getType()));
   1265     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
   1266       Vals.push_back(VE.getValueID(PN.getIncomingValue(i)));
   1267       Vals.push_back(VE.getValueID(PN.getIncomingBlock(i)));
   1268     }
   1269     break;
   1270   }
   1271 
   1272   case Instruction::LandingPad: {
   1273     const LandingPadInst &LP = cast<LandingPadInst>(I);
   1274     Code = bitc::FUNC_CODE_INST_LANDINGPAD_OLD;
   1275     Vals.push_back(VE.getTypeID(LP.getType()));
   1276     // TODO (rebase): is this fix enough?
   1277     // PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
   1278     Vals.push_back(LP.isCleanup());
   1279     Vals.push_back(LP.getNumClauses());
   1280     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
   1281       if (LP.isCatch(I))
   1282         Vals.push_back(LandingPadInst::Catch);
   1283       else
   1284         Vals.push_back(LandingPadInst::Filter);
   1285       PushValueAndType(LP.getClause(I), InstID, Vals, VE);
   1286     }
   1287     break;
   1288   }
   1289 
   1290   case Instruction::Alloca: {
   1291     Code = bitc::FUNC_CODE_INST_ALLOCA;
   1292     Vals.push_back(VE.getTypeID(I.getType()));
   1293     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
   1294     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
   1295     Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
   1296     break;
   1297   }
   1298 
   1299   case Instruction::Load:
   1300     if (cast<LoadInst>(I).isAtomic()) {
   1301       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
   1302       PushValueAndType(I.getOperand(0), InstID, Vals, VE);
   1303     } else {
   1304       Code = bitc::FUNC_CODE_INST_LOAD;
   1305       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))  // ptr
   1306         AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
   1307     }
   1308     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
   1309     Vals.push_back(cast<LoadInst>(I).isVolatile());
   1310     if (cast<LoadInst>(I).isAtomic()) {
   1311       Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
   1312       Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
   1313     }
   1314     break;
   1315   case Instruction::Store:
   1316     if (cast<StoreInst>(I).isAtomic())
   1317       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
   1318     else
   1319       Code = bitc::FUNC_CODE_INST_STORE_OLD;
   1320     PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
   1321     Vals.push_back(VE.getValueID(I.getOperand(0)));       // val.
   1322     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
   1323     Vals.push_back(cast<StoreInst>(I).isVolatile());
   1324     if (cast<StoreInst>(I).isAtomic()) {
   1325       Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
   1326       Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
   1327     }
   1328     break;
   1329   case Instruction::AtomicCmpXchg:
   1330     Code = bitc::FUNC_CODE_INST_CMPXCHG;
   1331     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
   1332     Vals.push_back(VE.getValueID(I.getOperand(1)));       // cmp.
   1333     Vals.push_back(VE.getValueID(I.getOperand(2)));       // newval.
   1334     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
   1335     Vals.push_back(GetEncodedOrdering(
   1336                      cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
   1337     Vals.push_back(GetEncodedSynchScope(
   1338                      cast<AtomicCmpXchgInst>(I).getSynchScope()));
   1339     break;
   1340   case Instruction::AtomicRMW:
   1341     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
   1342     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
   1343     Vals.push_back(VE.getValueID(I.getOperand(1)));       // val.
   1344     Vals.push_back(GetEncodedRMWOperation(
   1345                      cast<AtomicRMWInst>(I).getOperation()));
   1346     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
   1347     Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
   1348     Vals.push_back(GetEncodedSynchScope(
   1349                      cast<AtomicRMWInst>(I).getSynchScope()));
   1350     break;
   1351   case Instruction::Fence:
   1352     Code = bitc::FUNC_CODE_INST_FENCE;
   1353     Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
   1354     Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
   1355     break;
   1356   case Instruction::Call: {
   1357     const CallInst &CI = cast<CallInst>(I);
   1358     PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
   1359     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
   1360 
   1361     Code = bitc::FUNC_CODE_INST_CALL;
   1362 
   1363     Vals.push_back(VE.getAttributeID(CI.getAttributes()));
   1364     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
   1365     PushValueAndType(CI.getCalledValue(), InstID, Vals, VE);  // Callee
   1366 
   1367     // Emit value #'s for the fixed parameters.
   1368     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
   1369       Vals.push_back(VE.getValueID(CI.getArgOperand(i)));  // fixed param.
   1370 
   1371     // Emit type/value pairs for varargs params.
   1372     if (FTy->isVarArg()) {
   1373       for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
   1374            i != e; ++i)
   1375         PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE);  // varargs
   1376     }
   1377     break;
   1378   }
   1379   case Instruction::VAArg:
   1380     Code = bitc::FUNC_CODE_INST_VAARG;
   1381     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
   1382     Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
   1383     Vals.push_back(VE.getTypeID(I.getType())); // restype.
   1384     break;
   1385   }
   1386 
   1387   Stream.EmitRecord(Code, Vals, AbbrevToUse);
   1388   Vals.clear();
   1389 }
   1390 
   1391 // Emit names for globals/functions etc.
   1392 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
   1393                                   const llvm_3_2::ValueEnumerator &VE,
   1394                                   BitstreamWriter &Stream) {
   1395   if (VST.empty()) return;
   1396   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
   1397 
   1398   // FIXME: Set up the abbrev, we know how many values there are!
   1399   // FIXME: We know if the type names can use 7-bit ascii.
   1400   SmallVector<unsigned, 64> NameVals;
   1401 
   1402   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
   1403        SI != SE; ++SI) {
   1404 
   1405     const ValueName &Name = *SI;
   1406 
   1407     // Figure out the encoding to use for the name.
   1408     bool is7Bit = true;
   1409     bool isChar6 = true;
   1410     for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
   1411          C != E; ++C) {
   1412       if (isChar6)
   1413         isChar6 = BitCodeAbbrevOp::isChar6(*C);
   1414       if ((unsigned char)*C & 128) {
   1415         is7Bit = false;
   1416         break;  // don't bother scanning the rest.
   1417       }
   1418     }
   1419 
   1420     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
   1421 
   1422     // VST_ENTRY:   [valueid, namechar x N]
   1423     // VST_BBENTRY: [bbid, namechar x N]
   1424     unsigned Code;
   1425     if (isa<BasicBlock>(SI->getValue())) {
   1426       Code = bitc::VST_CODE_BBENTRY;
   1427       if (isChar6)
   1428         AbbrevToUse = VST_BBENTRY_6_ABBREV;
   1429     } else {
   1430       Code = bitc::VST_CODE_ENTRY;
   1431       if (isChar6)
   1432         AbbrevToUse = VST_ENTRY_6_ABBREV;
   1433       else if (is7Bit)
   1434         AbbrevToUse = VST_ENTRY_7_ABBREV;
   1435     }
   1436 
   1437     NameVals.push_back(VE.getValueID(SI->getValue()));
   1438     for (const char *P = Name.getKeyData(),
   1439          *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
   1440       NameVals.push_back((unsigned char)*P);
   1441 
   1442     // Emit the finished record.
   1443     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
   1444     NameVals.clear();
   1445   }
   1446   Stream.ExitBlock();
   1447 }
   1448 
   1449 static void WriteUseList(llvm_3_2::ValueEnumerator &VE, UseListOrder &&Order,
   1450                          BitstreamWriter &Stream) {
   1451   assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
   1452   unsigned Code;
   1453   if (isa<BasicBlock>(Order.V))
   1454     Code = bitc::USELIST_CODE_BB;
   1455   else
   1456     Code = bitc::USELIST_CODE_DEFAULT;
   1457 
   1458   SmallVector<uint64_t, 64> Record;
   1459   for (unsigned I : Order.Shuffle)
   1460     Record.push_back(I);
   1461   Record.push_back(VE.getValueID(Order.V));
   1462   Stream.EmitRecord(Code, Record);
   1463 }
   1464 
   1465 static void WriteUseListBlock(const Function *F, llvm_3_2::ValueEnumerator &VE,
   1466                               BitstreamWriter &Stream) {
   1467   auto hasMore = [&]() {
   1468     return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
   1469   };
   1470   if (!hasMore())
   1471     // Nothing to do.
   1472     return;
   1473 
   1474   Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
   1475   while (hasMore()) {
   1476     WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream);
   1477     VE.UseListOrders.pop_back();
   1478   }
   1479   Stream.ExitBlock();
   1480 }
   1481 
   1482 /// WriteFunction - Emit a function body to the module stream.
   1483 static void WriteFunction(const Function &F, llvm_3_2::ValueEnumerator &VE,
   1484                           BitstreamWriter &Stream) {
   1485   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
   1486   VE.incorporateFunction(F);
   1487 
   1488   SmallVector<unsigned, 64> Vals;
   1489 
   1490   // Emit the number of basic blocks, so the reader can create them ahead of
   1491   // time.
   1492   Vals.push_back(VE.getBasicBlocks().size());
   1493   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
   1494   Vals.clear();
   1495 
   1496   // If there are function-local constants, emit them now.
   1497   unsigned CstStart, CstEnd;
   1498   VE.getFunctionConstantRange(CstStart, CstEnd);
   1499   WriteConstants(CstStart, CstEnd, VE, Stream, false);
   1500 
   1501   // If there is function-local metadata, emit it now.
   1502   WriteFunctionLocalMetadata(F, VE, Stream);
   1503 
   1504   // Keep a running idea of what the instruction ID is.
   1505   unsigned InstID = CstEnd;
   1506 
   1507   bool NeedsMetadataAttachment = false;
   1508 
   1509   DILocation *LastDL = nullptr;
   1510 
   1511   // Finally, emit all the instructions, in order.
   1512   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
   1513     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
   1514          I != E; ++I) {
   1515       WriteInstruction(*I, InstID, VE, Stream, Vals);
   1516 
   1517       if (!I->getType()->isVoidTy())
   1518         ++InstID;
   1519 
   1520       // If the instruction has metadata, write a metadata attachment later.
   1521       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
   1522 
   1523       // If the instruction has a debug location, emit it.
   1524       DILocation *DL = I->getDebugLoc();
   1525       if (!DL)
   1526         continue;
   1527 
   1528       if (DL == LastDL) {
   1529         // Just repeat the same debug loc as last time.
   1530         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
   1531         continue;
   1532       }
   1533 
   1534       Vals.push_back(DL->getLine());
   1535       Vals.push_back(DL->getColumn());
   1536       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
   1537       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
   1538       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
   1539       Vals.clear();
   1540 
   1541       // Fixme(pirama): The following line is missing from upstream
   1542       // https://llvm.org/bugs/show_bug.cgi?id=23436
   1543       LastDL = DL;
   1544     }
   1545 
   1546   // Emit names for all the instructions etc.
   1547   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
   1548 
   1549   if (NeedsMetadataAttachment)
   1550     WriteMetadataAttachment(F, VE, Stream);
   1551   if (false)
   1552     WriteUseListBlock(&F, VE, Stream);
   1553   VE.purgeFunction();
   1554   Stream.ExitBlock();
   1555 }
   1556 
   1557 // Emit blockinfo, which defines the standard abbreviations etc.
   1558 static void WriteBlockInfo(const llvm_3_2::ValueEnumerator &VE,
   1559                            BitstreamWriter &Stream) {
   1560   // We only want to emit block info records for blocks that have multiple
   1561   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.  Other
   1562   // blocks can defined their abbrevs inline.
   1563   Stream.EnterBlockInfoBlock(2);
   1564 
   1565   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
   1566     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1567     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
   1568     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1569     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1570     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
   1571     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   1572                                    Abbv) != VST_ENTRY_8_ABBREV)
   1573       llvm_unreachable("Unexpected abbrev ordering!");
   1574   }
   1575 
   1576   { // 7-bit fixed width VST_ENTRY strings.
   1577     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1578     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
   1579     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1580     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1581     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
   1582     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   1583                                    Abbv) != VST_ENTRY_7_ABBREV)
   1584       llvm_unreachable("Unexpected abbrev ordering!");
   1585   }
   1586   { // 6-bit char6 VST_ENTRY strings.
   1587     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1588     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
   1589     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1590     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1591     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
   1592     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   1593                                    Abbv) != VST_ENTRY_6_ABBREV)
   1594       llvm_unreachable("Unexpected abbrev ordering!");
   1595   }
   1596   { // 6-bit char6 VST_BBENTRY strings.
   1597     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1598     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
   1599     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1600     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1601     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
   1602     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
   1603                                    Abbv) != VST_BBENTRY_6_ABBREV)
   1604       llvm_unreachable("Unexpected abbrev ordering!");
   1605   }
   1606 
   1607 
   1608 
   1609   { // SETTYPE abbrev for CONSTANTS_BLOCK.
   1610     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1611     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
   1612     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
   1613                               Log2_32_Ceil(VE.getTypes().size()+1)));
   1614     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   1615                                    Abbv) != CONSTANTS_SETTYPE_ABBREV)
   1616       llvm_unreachable("Unexpected abbrev ordering!");
   1617   }
   1618 
   1619   { // INTEGER abbrev for CONSTANTS_BLOCK.
   1620     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1621     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
   1622     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
   1623     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   1624                                    Abbv) != CONSTANTS_INTEGER_ABBREV)
   1625       llvm_unreachable("Unexpected abbrev ordering!");
   1626   }
   1627 
   1628   { // CE_CAST abbrev for CONSTANTS_BLOCK.
   1629     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1630     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
   1631     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
   1632     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
   1633                               Log2_32_Ceil(VE.getTypes().size()+1)));
   1634     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
   1635 
   1636     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   1637                                    Abbv) != CONSTANTS_CE_CAST_Abbrev)
   1638       llvm_unreachable("Unexpected abbrev ordering!");
   1639   }
   1640   { // NULL abbrev for CONSTANTS_BLOCK.
   1641     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1642     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
   1643     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
   1644                                    Abbv) != CONSTANTS_NULL_Abbrev)
   1645       llvm_unreachable("Unexpected abbrev ordering!");
   1646   }
   1647 
   1648   // FIXME: This should only use space for first class types!
   1649 
   1650   { // INST_LOAD abbrev for FUNCTION_BLOCK.
   1651     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1652     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
   1653     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
   1654     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
   1655     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
   1656     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1657                                    Abbv) != FUNCTION_INST_LOAD_ABBREV)
   1658       llvm_unreachable("Unexpected abbrev ordering!");
   1659   }
   1660   { // INST_BINOP abbrev for FUNCTION_BLOCK.
   1661     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1662     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
   1663     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
   1664     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
   1665     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
   1666     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1667                                    Abbv) != FUNCTION_INST_BINOP_ABBREV)
   1668       llvm_unreachable("Unexpected abbrev ordering!");
   1669   }
   1670   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
   1671     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1672     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
   1673     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
   1674     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
   1675     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
   1676     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
   1677     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1678                                    Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
   1679       llvm_unreachable("Unexpected abbrev ordering!");
   1680   }
   1681   { // INST_CAST abbrev for FUNCTION_BLOCK.
   1682     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1683     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
   1684     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
   1685     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
   1686                               Log2_32_Ceil(VE.getTypes().size()+1)));
   1687     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
   1688     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1689                                    Abbv) != FUNCTION_INST_CAST_ABBREV)
   1690       llvm_unreachable("Unexpected abbrev ordering!");
   1691   }
   1692 
   1693   { // INST_RET abbrev for FUNCTION_BLOCK.
   1694     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1695     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
   1696     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1697                                    Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
   1698       llvm_unreachable("Unexpected abbrev ordering!");
   1699   }
   1700   { // INST_RET abbrev for FUNCTION_BLOCK.
   1701     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1702     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
   1703     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
   1704     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1705                                    Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
   1706       llvm_unreachable("Unexpected abbrev ordering!");
   1707   }
   1708   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
   1709     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
   1710     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
   1711     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
   1712                                    Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
   1713       llvm_unreachable("Unexpected abbrev ordering!");
   1714   }
   1715 
   1716   Stream.ExitBlock();
   1717 }
   1718 
   1719 /// WriteModule - Emit the specified module to the bitstream.
   1720 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
   1721   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
   1722 
   1723   SmallVector<unsigned, 1> Vals;
   1724   // TODO(srhines): RenderScript is always version 0 for now.
   1725   unsigned CurVersion = 0;
   1726   if (CurVersion) {
   1727     Vals.push_back(CurVersion);
   1728     Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
   1729   }
   1730 
   1731   // Analyze the module, enumerating globals, functions, etc.
   1732   llvm_3_2::ValueEnumerator VE(*M);
   1733 
   1734   // Emit blockinfo, which defines the standard abbreviations etc.
   1735   WriteBlockInfo(VE, Stream);
   1736 
   1737   // Emit information about parameter attributes.
   1738   WriteAttributeTable(VE, Stream);
   1739 
   1740   // Emit information describing all of the types in the module.
   1741   WriteTypeTable(VE, Stream);
   1742 
   1743   // Emit top-level description of module, including target triple, inline asm,
   1744   // descriptors for global variables, and function prototype info.
   1745   WriteModuleInfo(M, VE, Stream);
   1746 
   1747   // Emit constants.
   1748   WriteModuleConstants(VE, Stream);
   1749 
   1750   // Emit metadata.
   1751   WriteModuleMetadata(M, VE, Stream);
   1752 
   1753   // Emit metadata.
   1754   WriteModuleMetadataStore(M, Stream);
   1755 
   1756   // Emit names for globals/functions etc.
   1757   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
   1758 
   1759   // Emit module-level use-lists.
   1760   if (false)
   1761     WriteUseListBlock(nullptr, VE, Stream);
   1762 
   1763   // Emit function bodies.
   1764   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
   1765     if (!F->isDeclaration())
   1766       WriteFunction(*F, VE, Stream);
   1767 
   1768   Stream.ExitBlock();
   1769 }
   1770 
   1771 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
   1772 /// header and trailer to make it compatible with the system archiver.  To do
   1773 /// this we emit the following header, and then emit a trailer that pads the
   1774 /// file out to be a multiple of 16 bytes.
   1775 ///
   1776 /// struct bc_header {
   1777 ///   uint32_t Magic;         // 0x0B17C0DE
   1778 ///   uint32_t Version;       // Version, currently always 0.
   1779 ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
   1780 ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
   1781 ///   uint32_t CPUType;       // CPU specifier.
   1782 ///   ... potentially more later ...
   1783 /// };
   1784 enum {
   1785   DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
   1786   DarwinBCHeaderSize = 5*4
   1787 };
   1788 
   1789 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
   1790                                uint32_t &Position) {
   1791   Buffer[Position + 0] = (unsigned char) (Value >>  0);
   1792   Buffer[Position + 1] = (unsigned char) (Value >>  8);
   1793   Buffer[Position + 2] = (unsigned char) (Value >> 16);
   1794   Buffer[Position + 3] = (unsigned char) (Value >> 24);
   1795   Position += 4;
   1796 }
   1797 
   1798 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
   1799                                          const Triple &TT) {
   1800   unsigned CPUType = ~0U;
   1801 
   1802   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
   1803   // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
   1804   // number from /usr/include/mach/machine.h.  It is ok to reproduce the
   1805   // specific constants here because they are implicitly part of the Darwin ABI.
   1806   enum {
   1807     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
   1808     DARWIN_CPU_TYPE_X86        = 7,
   1809     DARWIN_CPU_TYPE_ARM        = 12,
   1810     DARWIN_CPU_TYPE_POWERPC    = 18
   1811   };
   1812 
   1813   Triple::ArchType Arch = TT.getArch();
   1814   if (Arch == Triple::x86_64)
   1815     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
   1816   else if (Arch == Triple::x86)
   1817     CPUType = DARWIN_CPU_TYPE_X86;
   1818   else if (Arch == Triple::ppc)
   1819     CPUType = DARWIN_CPU_TYPE_POWERPC;
   1820   else if (Arch == Triple::ppc64)
   1821     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
   1822   else if (Arch == Triple::arm || Arch == Triple::thumb)
   1823     CPUType = DARWIN_CPU_TYPE_ARM;
   1824 
   1825   // Traditional Bitcode starts after header.
   1826   assert(Buffer.size() >= DarwinBCHeaderSize &&
   1827          "Expected header size to be reserved");
   1828   unsigned BCOffset = DarwinBCHeaderSize;
   1829   unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
   1830 
   1831   // Write the magic and version.
   1832   unsigned Position = 0;
   1833   WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
   1834   WriteInt32ToBuffer(0          , Buffer, Position); // Version.
   1835   WriteInt32ToBuffer(BCOffset   , Buffer, Position);
   1836   WriteInt32ToBuffer(BCSize     , Buffer, Position);
   1837   WriteInt32ToBuffer(CPUType    , Buffer, Position);
   1838 
   1839   // If the file is not a multiple of 16 bytes, insert dummy padding.
   1840   while (Buffer.size() & 15)
   1841     Buffer.push_back(0);
   1842 }
   1843 
   1844 /// WriteBitcodeToFile - Write the specified module to the specified output
   1845 /// stream.
   1846 void llvm_3_2::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
   1847   SmallVector<char, 0> Buffer;
   1848   Buffer.reserve(256*1024);
   1849 
   1850   // If this is darwin or another generic macho target, reserve space for the
   1851   // header.
   1852   Triple TT(M->getTargetTriple());
   1853   if (TT.isOSDarwin())
   1854     Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
   1855 
   1856   // Emit the module into the buffer.
   1857   {
   1858     BitstreamWriter Stream(Buffer);
   1859 
   1860     // Emit the file header.
   1861     Stream.Emit((unsigned)'B', 8);
   1862     Stream.Emit((unsigned)'C', 8);
   1863     Stream.Emit(0x0, 4);
   1864     Stream.Emit(0xC, 4);
   1865     Stream.Emit(0xE, 4);
   1866     Stream.Emit(0xD, 4);
   1867 
   1868     // Emit the module.
   1869     WriteModule(M, Stream);
   1870   }
   1871 
   1872   if (TT.isOSDarwin())
   1873     EmitDarwinBCHeaderAndTrailer(Buffer, TT);
   1874 
   1875   // Write the generated bitstream to "Out".
   1876   Out.write((char*)&Buffer.front(), Buffer.size());
   1877 }
   1878