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