Add X86 MMX type to bitcode and Type.
[oota-llvm.git] / lib / Bitcode / Writer / BitcodeWriter.cpp
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 "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/InlineAsm.h"
21 #include "llvm/Instructions.h"
22 #include "llvm/Module.h"
23 #include "llvm/Operator.h"
24 #include "llvm/TypeSymbolTable.h"
25 #include "llvm/ValueSymbolTable.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/Program.h"
30 using namespace llvm;
31
32 /// These are manifest constants used by the bitcode writer. They do not need to
33 /// be kept in sync with the reader, but need to be consistent within this file.
34 enum {
35   CurVersion = 0,
36
37   // VALUE_SYMTAB_BLOCK abbrev id's.
38   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
39   VST_ENTRY_7_ABBREV,
40   VST_ENTRY_6_ABBREV,
41   VST_BBENTRY_6_ABBREV,
42
43   // CONSTANTS_BLOCK abbrev id's.
44   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
45   CONSTANTS_INTEGER_ABBREV,
46   CONSTANTS_CE_CAST_Abbrev,
47   CONSTANTS_NULL_Abbrev,
48
49   // FUNCTION_BLOCK abbrev id's.
50   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
51   FUNCTION_INST_BINOP_ABBREV,
52   FUNCTION_INST_BINOP_FLAGS_ABBREV,
53   FUNCTION_INST_CAST_ABBREV,
54   FUNCTION_INST_RET_VOID_ABBREV,
55   FUNCTION_INST_RET_VAL_ABBREV,
56   FUNCTION_INST_UNREACHABLE_ABBREV
57 };
58
59
60 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
61   switch (Opcode) {
62   default: llvm_unreachable("Unknown cast instruction!");
63   case Instruction::Trunc   : return bitc::CAST_TRUNC;
64   case Instruction::ZExt    : return bitc::CAST_ZEXT;
65   case Instruction::SExt    : return bitc::CAST_SEXT;
66   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
67   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
68   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
69   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
70   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
71   case Instruction::FPExt   : return bitc::CAST_FPEXT;
72   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
73   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
74   case Instruction::BitCast : return bitc::CAST_BITCAST;
75   }
76 }
77
78 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
79   switch (Opcode) {
80   default: llvm_unreachable("Unknown binary instruction!");
81   case Instruction::Add:
82   case Instruction::FAdd: return bitc::BINOP_ADD;
83   case Instruction::Sub:
84   case Instruction::FSub: return bitc::BINOP_SUB;
85   case Instruction::Mul:
86   case Instruction::FMul: return bitc::BINOP_MUL;
87   case Instruction::UDiv: return bitc::BINOP_UDIV;
88   case Instruction::FDiv:
89   case Instruction::SDiv: return bitc::BINOP_SDIV;
90   case Instruction::URem: return bitc::BINOP_UREM;
91   case Instruction::FRem:
92   case Instruction::SRem: return bitc::BINOP_SREM;
93   case Instruction::Shl:  return bitc::BINOP_SHL;
94   case Instruction::LShr: return bitc::BINOP_LSHR;
95   case Instruction::AShr: return bitc::BINOP_ASHR;
96   case Instruction::And:  return bitc::BINOP_AND;
97   case Instruction::Or:   return bitc::BINOP_OR;
98   case Instruction::Xor:  return bitc::BINOP_XOR;
99   }
100 }
101
102
103
104 static void WriteStringRecord(unsigned Code, const std::string &Str,
105                               unsigned AbbrevToUse, BitstreamWriter &Stream) {
106   SmallVector<unsigned, 64> Vals;
107
108   // Code: [strchar x N]
109   for (unsigned i = 0, e = Str.size(); i != e; ++i)
110     Vals.push_back(Str[i]);
111
112   // Emit the finished record.
113   Stream.EmitRecord(Code, Vals, AbbrevToUse);
114 }
115
116 // Emit information about parameter attributes.
117 static void WriteAttributeTable(const ValueEnumerator &VE,
118                                 BitstreamWriter &Stream) {
119   const std::vector<AttrListPtr> &Attrs = VE.getAttributes();
120   if (Attrs.empty()) return;
121
122   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
123
124   SmallVector<uint64_t, 64> Record;
125   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
126     const AttrListPtr &A = Attrs[i];
127     for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
128       const AttributeWithIndex &PAWI = A.getSlot(i);
129       Record.push_back(PAWI.Index);
130
131       // FIXME: remove in LLVM 3.0
132       // Store the alignment in the bitcode as a 16-bit raw value instead of a
133       // 5-bit log2 encoded value. Shift the bits above the alignment up by
134       // 11 bits.
135       uint64_t FauxAttr = PAWI.Attrs & 0xffff;
136       if (PAWI.Attrs & Attribute::Alignment)
137         FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16);
138       FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11;
139
140       Record.push_back(FauxAttr);
141     }
142
143     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
144     Record.clear();
145   }
146
147   Stream.ExitBlock();
148 }
149
150 /// WriteTypeTable - Write out the type table for a module.
151 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
152   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
153
154   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
155   SmallVector<uint64_t, 64> TypeVals;
156
157   // Abbrev for TYPE_CODE_POINTER.
158   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
159   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
160   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
161                             Log2_32_Ceil(VE.getTypes().size()+1)));
162   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
163   unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
164
165   // Abbrev for TYPE_CODE_FUNCTION.
166   Abbv = new BitCodeAbbrev();
167   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
168   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
169   Abbv->Add(BitCodeAbbrevOp(0));  // FIXME: DEAD value, remove in LLVM 3.0
170   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
171   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
172                             Log2_32_Ceil(VE.getTypes().size()+1)));
173   unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
174
175   // Abbrev for TYPE_CODE_STRUCT.
176   Abbv = new BitCodeAbbrev();
177   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
178   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
179   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
180   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
181                             Log2_32_Ceil(VE.getTypes().size()+1)));
182   unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
183
184   // Abbrev for TYPE_CODE_ARRAY.
185   Abbv = new BitCodeAbbrev();
186   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
187   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
188   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
189                             Log2_32_Ceil(VE.getTypes().size()+1)));
190   unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
191
192   // Emit an entry count so the reader can reserve space.
193   TypeVals.push_back(TypeList.size());
194   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
195   TypeVals.clear();
196
197   // Loop over all of the types, emitting each in turn.
198   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
199     const Type *T = TypeList[i].first;
200     int AbbrevToUse = 0;
201     unsigned Code = 0;
202
203     switch (T->getTypeID()) {
204     default: llvm_unreachable("Unknown type!");
205     case Type::VoidTyID:   Code = bitc::TYPE_CODE_VOID;   break;
206     case Type::FloatTyID:  Code = bitc::TYPE_CODE_FLOAT;  break;
207     case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
208     case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
209     case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
210     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
211     case Type::LabelTyID:  Code = bitc::TYPE_CODE_LABEL;  break;
212     case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
213     case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
214     case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
215     case Type::IntegerTyID:
216       // INTEGER: [width]
217       Code = bitc::TYPE_CODE_INTEGER;
218       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
219       break;
220     case Type::PointerTyID: {
221       const PointerType *PTy = cast<PointerType>(T);
222       // POINTER: [pointee type, address space]
223       Code = bitc::TYPE_CODE_POINTER;
224       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
225       unsigned AddressSpace = PTy->getAddressSpace();
226       TypeVals.push_back(AddressSpace);
227       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
228       break;
229     }
230     case Type::FunctionTyID: {
231       const FunctionType *FT = cast<FunctionType>(T);
232       // FUNCTION: [isvararg, attrid, retty, paramty x N]
233       Code = bitc::TYPE_CODE_FUNCTION;
234       TypeVals.push_back(FT->isVarArg());
235       TypeVals.push_back(0);  // FIXME: DEAD: remove in llvm 3.0
236       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
237       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
238         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
239       AbbrevToUse = FunctionAbbrev;
240       break;
241     }
242     case Type::StructTyID: {
243       const StructType *ST = cast<StructType>(T);
244       // STRUCT: [ispacked, eltty x N]
245       Code = bitc::TYPE_CODE_STRUCT;
246       TypeVals.push_back(ST->isPacked());
247       // Output all of the element types.
248       for (StructType::element_iterator I = ST->element_begin(),
249            E = ST->element_end(); I != E; ++I)
250         TypeVals.push_back(VE.getTypeID(*I));
251       AbbrevToUse = StructAbbrev;
252       break;
253     }
254     case Type::ArrayTyID: {
255       const ArrayType *AT = cast<ArrayType>(T);
256       // ARRAY: [numelts, eltty]
257       Code = bitc::TYPE_CODE_ARRAY;
258       TypeVals.push_back(AT->getNumElements());
259       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
260       AbbrevToUse = ArrayAbbrev;
261       break;
262     }
263     case Type::VectorTyID: {
264       const VectorType *VT = cast<VectorType>(T);
265       // VECTOR [numelts, eltty]
266       Code = bitc::TYPE_CODE_VECTOR;
267       TypeVals.push_back(VT->getNumElements());
268       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
269       break;
270     }
271     }
272
273     // Emit the finished record.
274     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
275     TypeVals.clear();
276   }
277
278   Stream.ExitBlock();
279 }
280
281 static unsigned getEncodedLinkage(const GlobalValue *GV) {
282   switch (GV->getLinkage()) {
283   default: llvm_unreachable("Invalid linkage!");
284   case GlobalValue::ExternalLinkage:                 return 0;
285   case GlobalValue::WeakAnyLinkage:                  return 1;
286   case GlobalValue::AppendingLinkage:                return 2;
287   case GlobalValue::InternalLinkage:                 return 3;
288   case GlobalValue::LinkOnceAnyLinkage:              return 4;
289   case GlobalValue::DLLImportLinkage:                return 5;
290   case GlobalValue::DLLExportLinkage:                return 6;
291   case GlobalValue::ExternalWeakLinkage:             return 7;
292   case GlobalValue::CommonLinkage:                   return 8;
293   case GlobalValue::PrivateLinkage:                  return 9;
294   case GlobalValue::WeakODRLinkage:                  return 10;
295   case GlobalValue::LinkOnceODRLinkage:              return 11;
296   case GlobalValue::AvailableExternallyLinkage:      return 12;
297   case GlobalValue::LinkerPrivateLinkage:            return 13;
298   case GlobalValue::LinkerPrivateWeakLinkage:        return 14;
299   case GlobalValue::LinkerPrivateWeakDefAutoLinkage: return 15;
300   }
301 }
302
303 static unsigned getEncodedVisibility(const GlobalValue *GV) {
304   switch (GV->getVisibility()) {
305   default: llvm_unreachable("Invalid visibility!");
306   case GlobalValue::DefaultVisibility:   return 0;
307   case GlobalValue::HiddenVisibility:    return 1;
308   case GlobalValue::ProtectedVisibility: return 2;
309   }
310 }
311
312 // Emit top-level description of module, including target triple, inline asm,
313 // descriptors for global variables, and function prototype info.
314 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
315                             BitstreamWriter &Stream) {
316   // Emit the list of dependent libraries for the Module.
317   for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
318     WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
319
320   // Emit various pieces of data attached to a module.
321   if (!M->getTargetTriple().empty())
322     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
323                       0/*TODO*/, Stream);
324   if (!M->getDataLayout().empty())
325     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
326                       0/*TODO*/, Stream);
327   if (!M->getModuleInlineAsm().empty())
328     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
329                       0/*TODO*/, Stream);
330
331   // Emit information about sections and GC, computing how many there are. Also
332   // compute the maximum alignment value.
333   std::map<std::string, unsigned> SectionMap;
334   std::map<std::string, unsigned> GCMap;
335   unsigned MaxAlignment = 0;
336   unsigned MaxGlobalType = 0;
337   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
338        GV != E; ++GV) {
339     MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
340     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
341
342     if (!GV->hasSection()) continue;
343     // Give section names unique ID's.
344     unsigned &Entry = SectionMap[GV->getSection()];
345     if (Entry != 0) continue;
346     WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
347                       0/*TODO*/, Stream);
348     Entry = SectionMap.size();
349   }
350   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
351     MaxAlignment = std::max(MaxAlignment, F->getAlignment());
352     if (F->hasSection()) {
353       // Give section names unique ID's.
354       unsigned &Entry = SectionMap[F->getSection()];
355       if (!Entry) {
356         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
357                           0/*TODO*/, Stream);
358         Entry = SectionMap.size();
359       }
360     }
361     if (F->hasGC()) {
362       // Same for GC names.
363       unsigned &Entry = GCMap[F->getGC()];
364       if (!Entry) {
365         WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
366                           0/*TODO*/, Stream);
367         Entry = GCMap.size();
368       }
369     }
370   }
371
372   // Emit abbrev for globals, now that we know # sections and max alignment.
373   unsigned SimpleGVarAbbrev = 0;
374   if (!M->global_empty()) {
375     // Add an abbrev for common globals with no visibility or thread localness.
376     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
377     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
378     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
379                               Log2_32_Ceil(MaxGlobalType+1)));
380     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));      // Constant.
381     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));        // Initializer.
382     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));      // Linkage.
383     if (MaxAlignment == 0)                                      // Alignment.
384       Abbv->Add(BitCodeAbbrevOp(0));
385     else {
386       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
387       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
388                                Log2_32_Ceil(MaxEncAlignment+1)));
389     }
390     if (SectionMap.empty())                                    // Section.
391       Abbv->Add(BitCodeAbbrevOp(0));
392     else
393       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
394                                Log2_32_Ceil(SectionMap.size()+1)));
395     // Don't bother emitting vis + thread local.
396     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
397   }
398
399   // Emit the global variable information.
400   SmallVector<unsigned, 64> Vals;
401   for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
402        GV != E; ++GV) {
403     unsigned AbbrevToUse = 0;
404
405     // GLOBALVAR: [type, isconst, initid,
406     //             linkage, alignment, section, visibility, threadlocal]
407     Vals.push_back(VE.getTypeID(GV->getType()));
408     Vals.push_back(GV->isConstant());
409     Vals.push_back(GV->isDeclaration() ? 0 :
410                    (VE.getValueID(GV->getInitializer()) + 1));
411     Vals.push_back(getEncodedLinkage(GV));
412     Vals.push_back(Log2_32(GV->getAlignment())+1);
413     Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
414     if (GV->isThreadLocal() ||
415         GV->getVisibility() != GlobalValue::DefaultVisibility) {
416       Vals.push_back(getEncodedVisibility(GV));
417       Vals.push_back(GV->isThreadLocal());
418     } else {
419       AbbrevToUse = SimpleGVarAbbrev;
420     }
421
422     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
423     Vals.clear();
424   }
425
426   // Emit the function proto information.
427   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
428     // FUNCTION:  [type, callingconv, isproto, paramattr,
429     //             linkage, alignment, section, visibility, gc]
430     Vals.push_back(VE.getTypeID(F->getType()));
431     Vals.push_back(F->getCallingConv());
432     Vals.push_back(F->isDeclaration());
433     Vals.push_back(getEncodedLinkage(F));
434     Vals.push_back(VE.getAttributeID(F->getAttributes()));
435     Vals.push_back(Log2_32(F->getAlignment())+1);
436     Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
437     Vals.push_back(getEncodedVisibility(F));
438     Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
439
440     unsigned AbbrevToUse = 0;
441     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
442     Vals.clear();
443   }
444
445
446   // Emit the alias information.
447   for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
448        AI != E; ++AI) {
449     Vals.push_back(VE.getTypeID(AI->getType()));
450     Vals.push_back(VE.getValueID(AI->getAliasee()));
451     Vals.push_back(getEncodedLinkage(AI));
452     Vals.push_back(getEncodedVisibility(AI));
453     unsigned AbbrevToUse = 0;
454     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
455     Vals.clear();
456   }
457 }
458
459 static uint64_t GetOptimizationFlags(const Value *V) {
460   uint64_t Flags = 0;
461
462   if (const OverflowingBinaryOperator *OBO =
463         dyn_cast<OverflowingBinaryOperator>(V)) {
464     if (OBO->hasNoSignedWrap())
465       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
466     if (OBO->hasNoUnsignedWrap())
467       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
468   } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(V)) {
469     if (Div->isExact())
470       Flags |= 1 << bitc::SDIV_EXACT;
471   }
472
473   return Flags;
474 }
475
476 static void WriteMDNode(const MDNode *N,
477                         const ValueEnumerator &VE,
478                         BitstreamWriter &Stream,
479                         SmallVector<uint64_t, 64> &Record) {
480   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
481     if (N->getOperand(i)) {
482       Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
483       Record.push_back(VE.getValueID(N->getOperand(i)));
484     } else {
485       Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
486       Record.push_back(0);
487     }
488   }
489   unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE2 :
490                                            bitc::METADATA_NODE2;
491   Stream.EmitRecord(MDCode, Record, 0);
492   Record.clear();
493 }
494
495 static void WriteModuleMetadata(const Module *M,
496                                 const ValueEnumerator &VE,
497                                 BitstreamWriter &Stream) {
498   const ValueEnumerator::ValueList &Vals = VE.getMDValues();
499   bool StartedMetadataBlock = false;
500   unsigned MDSAbbrev = 0;
501   SmallVector<uint64_t, 64> Record;
502   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
503
504     if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
505       if (!N->isFunctionLocal() || !N->getFunction()) {
506         if (!StartedMetadataBlock) {
507           Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
508           StartedMetadataBlock = true;
509         }
510         WriteMDNode(N, VE, Stream, Record);
511       }
512     } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
513       if (!StartedMetadataBlock)  {
514         Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
515
516         // Abbrev for METADATA_STRING.
517         BitCodeAbbrev *Abbv = new BitCodeAbbrev();
518         Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
519         Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
520         Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
521         MDSAbbrev = Stream.EmitAbbrev(Abbv);
522         StartedMetadataBlock = true;
523       }
524
525       // Code: [strchar x N]
526       Record.append(MDS->begin(), MDS->end());
527
528       // Emit the finished record.
529       Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
530       Record.clear();
531     }
532   }
533
534   // Write named metadata.
535   for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
536        E = M->named_metadata_end(); I != E; ++I) {
537     const NamedMDNode *NMD = I;
538     if (!StartedMetadataBlock)  {
539       Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
540       StartedMetadataBlock = true;
541     }
542
543     // Write name.
544     StringRef Str = NMD->getName();
545     for (unsigned i = 0, e = Str.size(); i != e; ++i)
546       Record.push_back(Str[i]);
547     Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
548     Record.clear();
549
550     // Write named metadata operands.
551     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
552       Record.push_back(VE.getValueID(NMD->getOperand(i)));
553     Stream.EmitRecord(bitc::METADATA_NAMED_NODE2, Record, 0);
554     Record.clear();
555   }
556
557   if (StartedMetadataBlock)
558     Stream.ExitBlock();
559 }
560
561 static void WriteFunctionLocalMetadata(const Function &F,
562                                        const ValueEnumerator &VE,
563                                        BitstreamWriter &Stream) {
564   bool StartedMetadataBlock = false;
565   SmallVector<uint64_t, 64> Record;
566   const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
567   for (unsigned i = 0, e = Vals.size(); i != e; ++i)
568     if (const MDNode *N = Vals[i])
569       if (N->isFunctionLocal() && N->getFunction() == &F) {
570         if (!StartedMetadataBlock) {
571           Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
572           StartedMetadataBlock = true;
573         }
574         WriteMDNode(N, VE, Stream, Record);
575       }
576       
577   if (StartedMetadataBlock)
578     Stream.ExitBlock();
579 }
580
581 static void WriteMetadataAttachment(const Function &F,
582                                     const ValueEnumerator &VE,
583                                     BitstreamWriter &Stream) {
584   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
585
586   SmallVector<uint64_t, 64> Record;
587
588   // Write metadata attachments
589   // METADATA_ATTACHMENT2 - [m x [value, [n x [id, mdnode]]]
590   SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
591   
592   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
593     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
594          I != E; ++I) {
595       MDs.clear();
596       I->getAllMetadataOtherThanDebugLoc(MDs);
597       
598       // If no metadata, ignore instruction.
599       if (MDs.empty()) continue;
600
601       Record.push_back(VE.getInstructionID(I));
602       
603       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
604         Record.push_back(MDs[i].first);
605         Record.push_back(VE.getValueID(MDs[i].second));
606       }
607       Stream.EmitRecord(bitc::METADATA_ATTACHMENT2, Record, 0);
608       Record.clear();
609     }
610
611   Stream.ExitBlock();
612 }
613
614 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
615   SmallVector<uint64_t, 64> Record;
616
617   // Write metadata kinds
618   // METADATA_KIND - [n x [id, name]]
619   SmallVector<StringRef, 4> Names;
620   M->getMDKindNames(Names);
621   
622   if (Names.empty()) return;
623
624   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
625   
626   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
627     Record.push_back(MDKindID);
628     StringRef KName = Names[MDKindID];
629     Record.append(KName.begin(), KName.end());
630     
631     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
632     Record.clear();
633   }
634
635   Stream.ExitBlock();
636 }
637
638 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
639                            const ValueEnumerator &VE,
640                            BitstreamWriter &Stream, bool isGlobal) {
641   if (FirstVal == LastVal) return;
642
643   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
644
645   unsigned AggregateAbbrev = 0;
646   unsigned String8Abbrev = 0;
647   unsigned CString7Abbrev = 0;
648   unsigned CString6Abbrev = 0;
649   // If this is a constant pool for the module, emit module-specific abbrevs.
650   if (isGlobal) {
651     // Abbrev for CST_CODE_AGGREGATE.
652     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
653     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
654     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
655     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
656     AggregateAbbrev = Stream.EmitAbbrev(Abbv);
657
658     // Abbrev for CST_CODE_STRING.
659     Abbv = new BitCodeAbbrev();
660     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
661     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
662     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
663     String8Abbrev = Stream.EmitAbbrev(Abbv);
664     // Abbrev for CST_CODE_CSTRING.
665     Abbv = new BitCodeAbbrev();
666     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
667     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
668     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
669     CString7Abbrev = Stream.EmitAbbrev(Abbv);
670     // Abbrev for CST_CODE_CSTRING.
671     Abbv = new BitCodeAbbrev();
672     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
673     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
674     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
675     CString6Abbrev = Stream.EmitAbbrev(Abbv);
676   }
677
678   SmallVector<uint64_t, 64> Record;
679
680   const ValueEnumerator::ValueList &Vals = VE.getValues();
681   const Type *LastTy = 0;
682   for (unsigned i = FirstVal; i != LastVal; ++i) {
683     const Value *V = Vals[i].first;
684     // If we need to switch types, do so now.
685     if (V->getType() != LastTy) {
686       LastTy = V->getType();
687       Record.push_back(VE.getTypeID(LastTy));
688       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
689                         CONSTANTS_SETTYPE_ABBREV);
690       Record.clear();
691     }
692
693     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
694       Record.push_back(unsigned(IA->hasSideEffects()) |
695                        unsigned(IA->isAlignStack()) << 1);
696
697       // Add the asm string.
698       const std::string &AsmStr = IA->getAsmString();
699       Record.push_back(AsmStr.size());
700       for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
701         Record.push_back(AsmStr[i]);
702
703       // Add the constraint string.
704       const std::string &ConstraintStr = IA->getConstraintString();
705       Record.push_back(ConstraintStr.size());
706       for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
707         Record.push_back(ConstraintStr[i]);
708       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
709       Record.clear();
710       continue;
711     }
712     const Constant *C = cast<Constant>(V);
713     unsigned Code = -1U;
714     unsigned AbbrevToUse = 0;
715     if (C->isNullValue()) {
716       Code = bitc::CST_CODE_NULL;
717     } else if (isa<UndefValue>(C)) {
718       Code = bitc::CST_CODE_UNDEF;
719     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
720       if (IV->getBitWidth() <= 64) {
721         uint64_t V = IV->getSExtValue();
722         if ((int64_t)V >= 0)
723           Record.push_back(V << 1);
724         else
725           Record.push_back((-V << 1) | 1);
726         Code = bitc::CST_CODE_INTEGER;
727         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
728       } else {                             // Wide integers, > 64 bits in size.
729         // We have an arbitrary precision integer value to write whose
730         // bit width is > 64. However, in canonical unsigned integer
731         // format it is likely that the high bits are going to be zero.
732         // So, we only write the number of active words.
733         unsigned NWords = IV->getValue().getActiveWords();
734         const uint64_t *RawWords = IV->getValue().getRawData();
735         for (unsigned i = 0; i != NWords; ++i) {
736           int64_t V = RawWords[i];
737           if (V >= 0)
738             Record.push_back(V << 1);
739           else
740             Record.push_back((-V << 1) | 1);
741         }
742         Code = bitc::CST_CODE_WIDE_INTEGER;
743       }
744     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
745       Code = bitc::CST_CODE_FLOAT;
746       const Type *Ty = CFP->getType();
747       if (Ty->isFloatTy() || Ty->isDoubleTy()) {
748         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
749       } else if (Ty->isX86_FP80Ty()) {
750         // api needed to prevent premature destruction
751         // bits are not in the same order as a normal i80 APInt, compensate.
752         APInt api = CFP->getValueAPF().bitcastToAPInt();
753         const uint64_t *p = api.getRawData();
754         Record.push_back((p[1] << 48) | (p[0] >> 16));
755         Record.push_back(p[0] & 0xffffLL);
756       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
757         APInt api = CFP->getValueAPF().bitcastToAPInt();
758         const uint64_t *p = api.getRawData();
759         Record.push_back(p[0]);
760         Record.push_back(p[1]);
761       } else {
762         assert (0 && "Unknown FP type!");
763       }
764     } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
765       const ConstantArray *CA = cast<ConstantArray>(C);
766       // Emit constant strings specially.
767       unsigned NumOps = CA->getNumOperands();
768       // If this is a null-terminated string, use the denser CSTRING encoding.
769       if (CA->getOperand(NumOps-1)->isNullValue()) {
770         Code = bitc::CST_CODE_CSTRING;
771         --NumOps;  // Don't encode the null, which isn't allowed by char6.
772       } else {
773         Code = bitc::CST_CODE_STRING;
774         AbbrevToUse = String8Abbrev;
775       }
776       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
777       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
778       for (unsigned i = 0; i != NumOps; ++i) {
779         unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue();
780         Record.push_back(V);
781         isCStr7 &= (V & 128) == 0;
782         if (isCStrChar6)
783           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
784       }
785
786       if (isCStrChar6)
787         AbbrevToUse = CString6Abbrev;
788       else if (isCStr7)
789         AbbrevToUse = CString7Abbrev;
790     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
791                isa<ConstantVector>(V)) {
792       Code = bitc::CST_CODE_AGGREGATE;
793       for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
794         Record.push_back(VE.getValueID(C->getOperand(i)));
795       AbbrevToUse = AggregateAbbrev;
796     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
797       switch (CE->getOpcode()) {
798       default:
799         if (Instruction::isCast(CE->getOpcode())) {
800           Code = bitc::CST_CODE_CE_CAST;
801           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
802           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
803           Record.push_back(VE.getValueID(C->getOperand(0)));
804           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
805         } else {
806           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
807           Code = bitc::CST_CODE_CE_BINOP;
808           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
809           Record.push_back(VE.getValueID(C->getOperand(0)));
810           Record.push_back(VE.getValueID(C->getOperand(1)));
811           uint64_t Flags = GetOptimizationFlags(CE);
812           if (Flags != 0)
813             Record.push_back(Flags);
814         }
815         break;
816       case Instruction::GetElementPtr:
817         Code = bitc::CST_CODE_CE_GEP;
818         if (cast<GEPOperator>(C)->isInBounds())
819           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
820         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
821           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
822           Record.push_back(VE.getValueID(C->getOperand(i)));
823         }
824         break;
825       case Instruction::Select:
826         Code = bitc::CST_CODE_CE_SELECT;
827         Record.push_back(VE.getValueID(C->getOperand(0)));
828         Record.push_back(VE.getValueID(C->getOperand(1)));
829         Record.push_back(VE.getValueID(C->getOperand(2)));
830         break;
831       case Instruction::ExtractElement:
832         Code = bitc::CST_CODE_CE_EXTRACTELT;
833         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
834         Record.push_back(VE.getValueID(C->getOperand(0)));
835         Record.push_back(VE.getValueID(C->getOperand(1)));
836         break;
837       case Instruction::InsertElement:
838         Code = bitc::CST_CODE_CE_INSERTELT;
839         Record.push_back(VE.getValueID(C->getOperand(0)));
840         Record.push_back(VE.getValueID(C->getOperand(1)));
841         Record.push_back(VE.getValueID(C->getOperand(2)));
842         break;
843       case Instruction::ShuffleVector:
844         // If the return type and argument types are the same, this is a
845         // standard shufflevector instruction.  If the types are different,
846         // then the shuffle is widening or truncating the input vectors, and
847         // the argument type must also be encoded.
848         if (C->getType() == C->getOperand(0)->getType()) {
849           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
850         } else {
851           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
852           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
853         }
854         Record.push_back(VE.getValueID(C->getOperand(0)));
855         Record.push_back(VE.getValueID(C->getOperand(1)));
856         Record.push_back(VE.getValueID(C->getOperand(2)));
857         break;
858       case Instruction::ICmp:
859       case Instruction::FCmp:
860         Code = bitc::CST_CODE_CE_CMP;
861         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
862         Record.push_back(VE.getValueID(C->getOperand(0)));
863         Record.push_back(VE.getValueID(C->getOperand(1)));
864         Record.push_back(CE->getPredicate());
865         break;
866       }
867     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
868       assert(BA->getFunction() == BA->getBasicBlock()->getParent() &&
869              "Malformed blockaddress");
870       Code = bitc::CST_CODE_BLOCKADDRESS;
871       Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
872       Record.push_back(VE.getValueID(BA->getFunction()));
873       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
874     } else {
875 #ifndef NDEBUG
876       C->dump();
877 #endif
878       llvm_unreachable("Unknown constant!");
879     }
880     Stream.EmitRecord(Code, Record, AbbrevToUse);
881     Record.clear();
882   }
883
884   Stream.ExitBlock();
885 }
886
887 static void WriteModuleConstants(const ValueEnumerator &VE,
888                                  BitstreamWriter &Stream) {
889   const ValueEnumerator::ValueList &Vals = VE.getValues();
890
891   // Find the first constant to emit, which is the first non-globalvalue value.
892   // We know globalvalues have been emitted by WriteModuleInfo.
893   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
894     if (!isa<GlobalValue>(Vals[i].first)) {
895       WriteConstants(i, Vals.size(), VE, Stream, true);
896       return;
897     }
898   }
899 }
900
901 /// PushValueAndType - The file has to encode both the value and type id for
902 /// many values, because we need to know what type to create for forward
903 /// references.  However, most operands are not forward references, so this type
904 /// field is not needed.
905 ///
906 /// This function adds V's value ID to Vals.  If the value ID is higher than the
907 /// instruction ID, then it is a forward reference, and it also includes the
908 /// type ID.
909 static bool PushValueAndType(const Value *V, unsigned InstID,
910                              SmallVector<unsigned, 64> &Vals,
911                              ValueEnumerator &VE) {
912   unsigned ValID = VE.getValueID(V);
913   Vals.push_back(ValID);
914   if (ValID >= InstID) {
915     Vals.push_back(VE.getTypeID(V->getType()));
916     return true;
917   }
918   return false;
919 }
920
921 /// WriteInstruction - Emit an instruction to the specified stream.
922 static void WriteInstruction(const Instruction &I, unsigned InstID,
923                              ValueEnumerator &VE, BitstreamWriter &Stream,
924                              SmallVector<unsigned, 64> &Vals) {
925   unsigned Code = 0;
926   unsigned AbbrevToUse = 0;
927   VE.setInstructionID(&I);
928   switch (I.getOpcode()) {
929   default:
930     if (Instruction::isCast(I.getOpcode())) {
931       Code = bitc::FUNC_CODE_INST_CAST;
932       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
933         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
934       Vals.push_back(VE.getTypeID(I.getType()));
935       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
936     } else {
937       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
938       Code = bitc::FUNC_CODE_INST_BINOP;
939       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
940         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
941       Vals.push_back(VE.getValueID(I.getOperand(1)));
942       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
943       uint64_t Flags = GetOptimizationFlags(&I);
944       if (Flags != 0) {
945         if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
946           AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
947         Vals.push_back(Flags);
948       }
949     }
950     break;
951
952   case Instruction::GetElementPtr:
953     Code = bitc::FUNC_CODE_INST_GEP;
954     if (cast<GEPOperator>(&I)->isInBounds())
955       Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
956     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
957       PushValueAndType(I.getOperand(i), InstID, Vals, VE);
958     break;
959   case Instruction::ExtractValue: {
960     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
961     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
962     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
963     for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
964       Vals.push_back(*i);
965     break;
966   }
967   case Instruction::InsertValue: {
968     Code = bitc::FUNC_CODE_INST_INSERTVAL;
969     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
970     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
971     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
972     for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
973       Vals.push_back(*i);
974     break;
975   }
976   case Instruction::Select:
977     Code = bitc::FUNC_CODE_INST_VSELECT;
978     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
979     Vals.push_back(VE.getValueID(I.getOperand(2)));
980     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
981     break;
982   case Instruction::ExtractElement:
983     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
984     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
985     Vals.push_back(VE.getValueID(I.getOperand(1)));
986     break;
987   case Instruction::InsertElement:
988     Code = bitc::FUNC_CODE_INST_INSERTELT;
989     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
990     Vals.push_back(VE.getValueID(I.getOperand(1)));
991     Vals.push_back(VE.getValueID(I.getOperand(2)));
992     break;
993   case Instruction::ShuffleVector:
994     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
995     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
996     Vals.push_back(VE.getValueID(I.getOperand(1)));
997     Vals.push_back(VE.getValueID(I.getOperand(2)));
998     break;
999   case Instruction::ICmp:
1000   case Instruction::FCmp:
1001     // compare returning Int1Ty or vector of Int1Ty
1002     Code = bitc::FUNC_CODE_INST_CMP2;
1003     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1004     Vals.push_back(VE.getValueID(I.getOperand(1)));
1005     Vals.push_back(cast<CmpInst>(I).getPredicate());
1006     break;
1007
1008   case Instruction::Ret:
1009     {
1010       Code = bitc::FUNC_CODE_INST_RET;
1011       unsigned NumOperands = I.getNumOperands();
1012       if (NumOperands == 0)
1013         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1014       else if (NumOperands == 1) {
1015         if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1016           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1017       } else {
1018         for (unsigned i = 0, e = NumOperands; i != e; ++i)
1019           PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1020       }
1021     }
1022     break;
1023   case Instruction::Br:
1024     {
1025       Code = bitc::FUNC_CODE_INST_BR;
1026       BranchInst &II = cast<BranchInst>(I);
1027       Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1028       if (II.isConditional()) {
1029         Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1030         Vals.push_back(VE.getValueID(II.getCondition()));
1031       }
1032     }
1033     break;
1034   case Instruction::Switch:
1035     Code = bitc::FUNC_CODE_INST_SWITCH;
1036     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1037     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1038       Vals.push_back(VE.getValueID(I.getOperand(i)));
1039     break;
1040   case Instruction::IndirectBr:
1041     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
1042     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1043     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1044       Vals.push_back(VE.getValueID(I.getOperand(i)));
1045     break;
1046       
1047   case Instruction::Invoke: {
1048     const InvokeInst *II = cast<InvokeInst>(&I);
1049     const Value *Callee(II->getCalledValue());
1050     const PointerType *PTy = cast<PointerType>(Callee->getType());
1051     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1052     Code = bitc::FUNC_CODE_INST_INVOKE;
1053
1054     Vals.push_back(VE.getAttributeID(II->getAttributes()));
1055     Vals.push_back(II->getCallingConv());
1056     Vals.push_back(VE.getValueID(II->getNormalDest()));
1057     Vals.push_back(VE.getValueID(II->getUnwindDest()));
1058     PushValueAndType(Callee, InstID, Vals, VE);
1059
1060     // Emit value #'s for the fixed parameters.
1061     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1062       Vals.push_back(VE.getValueID(I.getOperand(i)));  // fixed param.
1063
1064     // Emit type/value pairs for varargs params.
1065     if (FTy->isVarArg()) {
1066       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1067            i != e; ++i)
1068         PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1069     }
1070     break;
1071   }
1072   case Instruction::Unwind:
1073     Code = bitc::FUNC_CODE_INST_UNWIND;
1074     break;
1075   case Instruction::Unreachable:
1076     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
1077     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
1078     break;
1079
1080   case Instruction::PHI:
1081     Code = bitc::FUNC_CODE_INST_PHI;
1082     Vals.push_back(VE.getTypeID(I.getType()));
1083     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1084       Vals.push_back(VE.getValueID(I.getOperand(i)));
1085     break;
1086
1087   case Instruction::Alloca:
1088     Code = bitc::FUNC_CODE_INST_ALLOCA;
1089     Vals.push_back(VE.getTypeID(I.getType()));
1090     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1091     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1092     Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1093     break;
1094
1095   case Instruction::Load:
1096     Code = bitc::FUNC_CODE_INST_LOAD;
1097     if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))  // ptr
1098       AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1099
1100     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1101     Vals.push_back(cast<LoadInst>(I).isVolatile());
1102     break;
1103   case Instruction::Store:
1104     Code = bitc::FUNC_CODE_INST_STORE2;
1105     PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
1106     Vals.push_back(VE.getValueID(I.getOperand(0)));       // val.
1107     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1108     Vals.push_back(cast<StoreInst>(I).isVolatile());
1109     break;
1110   case Instruction::Call: {
1111     const CallInst &CI = cast<CallInst>(I);
1112     const PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1113     const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1114
1115     Code = bitc::FUNC_CODE_INST_CALL2;
1116
1117     Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1118     Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1119     PushValueAndType(CI.getCalledValue(), InstID, Vals, VE);  // Callee
1120
1121     // Emit value #'s for the fixed parameters.
1122     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1123       Vals.push_back(VE.getValueID(CI.getArgOperand(i)));  // fixed param.
1124
1125     // Emit type/value pairs for varargs params.
1126     if (FTy->isVarArg()) {
1127       for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
1128            i != e; ++i)
1129         PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE);  // varargs
1130     }
1131     break;
1132   }
1133   case Instruction::VAArg:
1134     Code = bitc::FUNC_CODE_INST_VAARG;
1135     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
1136     Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
1137     Vals.push_back(VE.getTypeID(I.getType())); // restype.
1138     break;
1139   }
1140
1141   Stream.EmitRecord(Code, Vals, AbbrevToUse);
1142   Vals.clear();
1143 }
1144
1145 // Emit names for globals/functions etc.
1146 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1147                                   const ValueEnumerator &VE,
1148                                   BitstreamWriter &Stream) {
1149   if (VST.empty()) return;
1150   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
1151
1152   // FIXME: Set up the abbrev, we know how many values there are!
1153   // FIXME: We know if the type names can use 7-bit ascii.
1154   SmallVector<unsigned, 64> NameVals;
1155
1156   for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1157        SI != SE; ++SI) {
1158
1159     const ValueName &Name = *SI;
1160
1161     // Figure out the encoding to use for the name.
1162     bool is7Bit = true;
1163     bool isChar6 = true;
1164     for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1165          C != E; ++C) {
1166       if (isChar6)
1167         isChar6 = BitCodeAbbrevOp::isChar6(*C);
1168       if ((unsigned char)*C & 128) {
1169         is7Bit = false;
1170         break;  // don't bother scanning the rest.
1171       }
1172     }
1173
1174     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
1175
1176     // VST_ENTRY:   [valueid, namechar x N]
1177     // VST_BBENTRY: [bbid, namechar x N]
1178     unsigned Code;
1179     if (isa<BasicBlock>(SI->getValue())) {
1180       Code = bitc::VST_CODE_BBENTRY;
1181       if (isChar6)
1182         AbbrevToUse = VST_BBENTRY_6_ABBREV;
1183     } else {
1184       Code = bitc::VST_CODE_ENTRY;
1185       if (isChar6)
1186         AbbrevToUse = VST_ENTRY_6_ABBREV;
1187       else if (is7Bit)
1188         AbbrevToUse = VST_ENTRY_7_ABBREV;
1189     }
1190
1191     NameVals.push_back(VE.getValueID(SI->getValue()));
1192     for (const char *P = Name.getKeyData(),
1193          *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
1194       NameVals.push_back((unsigned char)*P);
1195
1196     // Emit the finished record.
1197     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1198     NameVals.clear();
1199   }
1200   Stream.ExitBlock();
1201 }
1202
1203 /// WriteFunction - Emit a function body to the module stream.
1204 static void WriteFunction(const Function &F, ValueEnumerator &VE,
1205                           BitstreamWriter &Stream) {
1206   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
1207   VE.incorporateFunction(F);
1208
1209   SmallVector<unsigned, 64> Vals;
1210
1211   // Emit the number of basic blocks, so the reader can create them ahead of
1212   // time.
1213   Vals.push_back(VE.getBasicBlocks().size());
1214   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1215   Vals.clear();
1216
1217   // If there are function-local constants, emit them now.
1218   unsigned CstStart, CstEnd;
1219   VE.getFunctionConstantRange(CstStart, CstEnd);
1220   WriteConstants(CstStart, CstEnd, VE, Stream, false);
1221
1222   // If there is function-local metadata, emit it now.
1223   WriteFunctionLocalMetadata(F, VE, Stream);
1224
1225   // Keep a running idea of what the instruction ID is.
1226   unsigned InstID = CstEnd;
1227
1228   bool NeedsMetadataAttachment = false;
1229   
1230   DebugLoc LastDL;
1231   
1232   // Finally, emit all the instructions, in order.
1233   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1234     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1235          I != E; ++I) {
1236       WriteInstruction(*I, InstID, VE, Stream, Vals);
1237       
1238       if (!I->getType()->isVoidTy())
1239         ++InstID;
1240       
1241       // If the instruction has metadata, write a metadata attachment later.
1242       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1243       
1244       // If the instruction has a debug location, emit it.
1245       DebugLoc DL = I->getDebugLoc();
1246       if (DL.isUnknown()) {
1247         // nothing todo.
1248       } else if (DL == LastDL) {
1249         // Just repeat the same debug loc as last time.
1250         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1251       } else {
1252         MDNode *Scope, *IA;
1253         DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1254         
1255         Vals.push_back(DL.getLine());
1256         Vals.push_back(DL.getCol());
1257         Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1258         Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1259         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC2, Vals);
1260         Vals.clear();
1261         
1262         LastDL = DL;
1263       }
1264     }
1265
1266   // Emit names for all the instructions etc.
1267   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1268
1269   if (NeedsMetadataAttachment)
1270     WriteMetadataAttachment(F, VE, Stream);
1271   VE.purgeFunction();
1272   Stream.ExitBlock();
1273 }
1274
1275 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1276 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1277                                  const ValueEnumerator &VE,
1278                                  BitstreamWriter &Stream) {
1279   if (TST.empty()) return;
1280
1281   Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
1282
1283   // 7-bit fixed width VST_CODE_ENTRY strings.
1284   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1285   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1286   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1287                             Log2_32_Ceil(VE.getTypes().size()+1)));
1288   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1289   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1290   unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
1291
1292   SmallVector<unsigned, 64> NameVals;
1293
1294   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
1295        TI != TE; ++TI) {
1296     // TST_ENTRY: [typeid, namechar x N]
1297     NameVals.push_back(VE.getTypeID(TI->second));
1298
1299     const std::string &Str = TI->first;
1300     bool is7Bit = true;
1301     for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1302       NameVals.push_back((unsigned char)Str[i]);
1303       if (Str[i] & 128)
1304         is7Bit = false;
1305     }
1306
1307     // Emit the finished record.
1308     Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
1309     NameVals.clear();
1310   }
1311
1312   Stream.ExitBlock();
1313 }
1314
1315 // Emit blockinfo, which defines the standard abbreviations etc.
1316 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1317   // We only want to emit block info records for blocks that have multiple
1318   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.  Other
1319   // blocks can defined their abbrevs inline.
1320   Stream.EnterBlockInfoBlock(2);
1321
1322   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1323     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1324     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1325     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1326     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1327     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1328     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1329                                    Abbv) != VST_ENTRY_8_ABBREV)
1330       llvm_unreachable("Unexpected abbrev ordering!");
1331   }
1332
1333   { // 7-bit fixed width VST_ENTRY strings.
1334     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1335     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1336     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1337     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1338     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1339     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1340                                    Abbv) != VST_ENTRY_7_ABBREV)
1341       llvm_unreachable("Unexpected abbrev ordering!");
1342   }
1343   { // 6-bit char6 VST_ENTRY strings.
1344     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1345     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1346     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1347     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1348     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1349     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1350                                    Abbv) != VST_ENTRY_6_ABBREV)
1351       llvm_unreachable("Unexpected abbrev ordering!");
1352   }
1353   { // 6-bit char6 VST_BBENTRY strings.
1354     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1355     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1356     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1357     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1358     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1359     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1360                                    Abbv) != VST_BBENTRY_6_ABBREV)
1361       llvm_unreachable("Unexpected abbrev ordering!");
1362   }
1363
1364
1365
1366   { // SETTYPE abbrev for CONSTANTS_BLOCK.
1367     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1368     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1369     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1370                               Log2_32_Ceil(VE.getTypes().size()+1)));
1371     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1372                                    Abbv) != CONSTANTS_SETTYPE_ABBREV)
1373       llvm_unreachable("Unexpected abbrev ordering!");
1374   }
1375
1376   { // INTEGER abbrev for CONSTANTS_BLOCK.
1377     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1378     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1379     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1380     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1381                                    Abbv) != CONSTANTS_INTEGER_ABBREV)
1382       llvm_unreachable("Unexpected abbrev ordering!");
1383   }
1384
1385   { // CE_CAST abbrev for CONSTANTS_BLOCK.
1386     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1387     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1388     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
1389     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
1390                               Log2_32_Ceil(VE.getTypes().size()+1)));
1391     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
1392
1393     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1394                                    Abbv) != CONSTANTS_CE_CAST_Abbrev)
1395       llvm_unreachable("Unexpected abbrev ordering!");
1396   }
1397   { // NULL abbrev for CONSTANTS_BLOCK.
1398     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1399     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1400     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1401                                    Abbv) != CONSTANTS_NULL_Abbrev)
1402       llvm_unreachable("Unexpected abbrev ordering!");
1403   }
1404
1405   // FIXME: This should only use space for first class types!
1406
1407   { // INST_LOAD abbrev for FUNCTION_BLOCK.
1408     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1409     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1410     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1411     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1412     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1413     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1414                                    Abbv) != FUNCTION_INST_LOAD_ABBREV)
1415       llvm_unreachable("Unexpected abbrev ordering!");
1416   }
1417   { // INST_BINOP abbrev for FUNCTION_BLOCK.
1418     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1419     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1420     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1421     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1422     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1423     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1424                                    Abbv) != FUNCTION_INST_BINOP_ABBREV)
1425       llvm_unreachable("Unexpected abbrev ordering!");
1426   }
1427   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1428     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1429     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1430     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1431     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1432     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1433     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1434     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1435                                    Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1436       llvm_unreachable("Unexpected abbrev ordering!");
1437   }
1438   { // INST_CAST abbrev for FUNCTION_BLOCK.
1439     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1440     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1441     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
1442     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
1443                               Log2_32_Ceil(VE.getTypes().size()+1)));
1444     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
1445     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1446                                    Abbv) != FUNCTION_INST_CAST_ABBREV)
1447       llvm_unreachable("Unexpected abbrev ordering!");
1448   }
1449
1450   { // INST_RET abbrev for FUNCTION_BLOCK.
1451     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1452     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1453     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1454                                    Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1455       llvm_unreachable("Unexpected abbrev ordering!");
1456   }
1457   { // INST_RET abbrev for FUNCTION_BLOCK.
1458     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1459     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1460     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1461     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1462                                    Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1463       llvm_unreachable("Unexpected abbrev ordering!");
1464   }
1465   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1466     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1467     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1468     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1469                                    Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1470       llvm_unreachable("Unexpected abbrev ordering!");
1471   }
1472
1473   Stream.ExitBlock();
1474 }
1475
1476
1477 /// WriteModule - Emit the specified module to the bitstream.
1478 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1479   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1480
1481   // Emit the version number if it is non-zero.
1482   if (CurVersion) {
1483     SmallVector<unsigned, 1> Vals;
1484     Vals.push_back(CurVersion);
1485     Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1486   }
1487
1488   // Analyze the module, enumerating globals, functions, etc.
1489   ValueEnumerator VE(M);
1490
1491   // Emit blockinfo, which defines the standard abbreviations etc.
1492   WriteBlockInfo(VE, Stream);
1493
1494   // Emit information about parameter attributes.
1495   WriteAttributeTable(VE, Stream);
1496
1497   // Emit information describing all of the types in the module.
1498   WriteTypeTable(VE, Stream);
1499
1500   // Emit top-level description of module, including target triple, inline asm,
1501   // descriptors for global variables, and function prototype info.
1502   WriteModuleInfo(M, VE, Stream);
1503
1504   // Emit constants.
1505   WriteModuleConstants(VE, Stream);
1506
1507   // Emit metadata.
1508   WriteModuleMetadata(M, VE, Stream);
1509
1510   // Emit function bodies.
1511   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1512     if (!I->isDeclaration())
1513       WriteFunction(*I, VE, Stream);
1514
1515   // Emit metadata.
1516   WriteModuleMetadataStore(M, Stream);
1517
1518   // Emit the type symbol table information.
1519   WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
1520
1521   // Emit names for globals/functions etc.
1522   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1523
1524   Stream.ExitBlock();
1525 }
1526
1527 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1528 /// header and trailer to make it compatible with the system archiver.  To do
1529 /// this we emit the following header, and then emit a trailer that pads the
1530 /// file out to be a multiple of 16 bytes.
1531 ///
1532 /// struct bc_header {
1533 ///   uint32_t Magic;         // 0x0B17C0DE
1534 ///   uint32_t Version;       // Version, currently always 0.
1535 ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1536 ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
1537 ///   uint32_t CPUType;       // CPU specifier.
1538 ///   ... potentially more later ...
1539 /// };
1540 enum {
1541   DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1542   DarwinBCHeaderSize = 5*4
1543 };
1544
1545 /// isARMTriplet - Return true if the triplet looks like:
1546 /// arm-*, thumb-*, armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*.
1547 static bool isARMTriplet(const std::string &TT) {
1548   size_t Pos = 0;
1549   size_t Size = TT.size();
1550   if (Size >= 6 &&
1551       TT[0] == 't' && TT[1] == 'h' && TT[2] == 'u' &&
1552       TT[3] == 'm' && TT[4] == 'b')
1553     Pos = 5;
1554   else if (Size >= 4 && TT[0] == 'a' && TT[1] == 'r' && TT[2] == 'm')
1555     Pos = 3;
1556   else
1557     return false;
1558
1559   if (TT[Pos] == '-')
1560     return true;
1561   else if (TT[Pos] == 'v') {
1562     if (Size >= Pos+4 &&
1563         TT[Pos+1] == '6' && TT[Pos+2] == 't' && TT[Pos+3] == '2')
1564       return true;
1565     else if (Size >= Pos+4 &&
1566              TT[Pos+1] == '5' && TT[Pos+2] == 't' && TT[Pos+3] == 'e')
1567       return true;
1568   } else
1569     return false;
1570   while (++Pos < Size && TT[Pos] != '-') {
1571     if (!isdigit(TT[Pos]))
1572       return false;
1573   }
1574   return true;
1575 }
1576
1577 static void EmitDarwinBCHeader(BitstreamWriter &Stream,
1578                                const std::string &TT) {
1579   unsigned CPUType = ~0U;
1580
1581   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
1582   // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
1583   // number from /usr/include/mach/machine.h.  It is ok to reproduce the
1584   // specific constants here because they are implicitly part of the Darwin ABI.
1585   enum {
1586     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
1587     DARWIN_CPU_TYPE_X86        = 7,
1588     DARWIN_CPU_TYPE_ARM        = 12,
1589     DARWIN_CPU_TYPE_POWERPC    = 18
1590   };
1591
1592   if (TT.find("x86_64-") == 0)
1593     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1594   else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
1595            TT[4] == '-' && TT[1] - '3' < 6)
1596     CPUType = DARWIN_CPU_TYPE_X86;
1597   else if (TT.find("powerpc-") == 0)
1598     CPUType = DARWIN_CPU_TYPE_POWERPC;
1599   else if (TT.find("powerpc64-") == 0)
1600     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
1601   else if (isARMTriplet(TT))
1602     CPUType = DARWIN_CPU_TYPE_ARM;
1603
1604   // Traditional Bitcode starts after header.
1605   unsigned BCOffset = DarwinBCHeaderSize;
1606
1607   Stream.Emit(0x0B17C0DE, 32);
1608   Stream.Emit(0         , 32);  // Version.
1609   Stream.Emit(BCOffset  , 32);
1610   Stream.Emit(0         , 32);  // Filled in later.
1611   Stream.Emit(CPUType   , 32);
1612 }
1613
1614 /// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1615 /// finalize the header.
1616 static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1617   // Update the size field in the header.
1618   Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
1619
1620   // If the file is not a multiple of 16 bytes, insert dummy padding.
1621   while (BufferSize & 15) {
1622     Stream.Emit(0, 8);
1623     ++BufferSize;
1624   }
1625 }
1626
1627
1628 /// WriteBitcodeToFile - Write the specified module to the specified output
1629 /// stream.
1630 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
1631   std::vector<unsigned char> Buffer;
1632   BitstreamWriter Stream(Buffer);
1633
1634   Buffer.reserve(256*1024);
1635
1636   WriteBitcodeToStream( M, Stream );
1637
1638   // Write the generated bitstream to "Out".
1639   Out.write((char*)&Buffer.front(), Buffer.size());
1640 }
1641
1642 /// WriteBitcodeToStream - Write the specified module to the specified output
1643 /// stream.
1644 void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
1645   // If this is darwin, emit a file header and trailer if needed.
1646   bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
1647   if (isDarwin)
1648     EmitDarwinBCHeader(Stream, M->getTargetTriple());
1649
1650   // Emit the file header.
1651   Stream.Emit((unsigned)'B', 8);
1652   Stream.Emit((unsigned)'C', 8);
1653   Stream.Emit(0x0, 4);
1654   Stream.Emit(0xC, 4);
1655   Stream.Emit(0xE, 4);
1656   Stream.Emit(0xD, 4);
1657
1658   // Emit the module.
1659   WriteModule(M, Stream);
1660
1661   if (isDarwin)
1662     EmitDarwinBCTrailer(Stream, Stream.getBuffer().size());
1663 }