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