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