Add InaccessibleMemOnly and inaccessibleMemOrArgMemOnly attributes
[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 "ValueEnumerator.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Bitcode/BitstreamWriter.h"
19 #include "llvm/Bitcode/LLVMBitCodes.h"
20 #include "llvm/IR/CallSite.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DebugInfoMetadata.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/InlineAsm.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/UseListOrder.h"
31 #include "llvm/IR/ValueSymbolTable.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/Support/Program.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <cctype>
38 #include <map>
39 using namespace llvm;
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   // VALUE_SYMTAB_BLOCK abbrev id's.
45   VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
46   VST_ENTRY_7_ABBREV,
47   VST_ENTRY_6_ABBREV,
48   VST_BBENTRY_6_ABBREV,
49
50   // CONSTANTS_BLOCK abbrev id's.
51   CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
52   CONSTANTS_INTEGER_ABBREV,
53   CONSTANTS_CE_CAST_Abbrev,
54   CONSTANTS_NULL_Abbrev,
55
56   // FUNCTION_BLOCK abbrev id's.
57   FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
58   FUNCTION_INST_BINOP_ABBREV,
59   FUNCTION_INST_BINOP_FLAGS_ABBREV,
60   FUNCTION_INST_CAST_ABBREV,
61   FUNCTION_INST_RET_VOID_ABBREV,
62   FUNCTION_INST_RET_VAL_ABBREV,
63   FUNCTION_INST_UNREACHABLE_ABBREV,
64   FUNCTION_INST_GEP_ABBREV,
65 };
66
67 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
68   switch (Opcode) {
69   default: llvm_unreachable("Unknown cast instruction!");
70   case Instruction::Trunc   : return bitc::CAST_TRUNC;
71   case Instruction::ZExt    : return bitc::CAST_ZEXT;
72   case Instruction::SExt    : return bitc::CAST_SEXT;
73   case Instruction::FPToUI  : return bitc::CAST_FPTOUI;
74   case Instruction::FPToSI  : return bitc::CAST_FPTOSI;
75   case Instruction::UIToFP  : return bitc::CAST_UITOFP;
76   case Instruction::SIToFP  : return bitc::CAST_SITOFP;
77   case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
78   case Instruction::FPExt   : return bitc::CAST_FPEXT;
79   case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
80   case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
81   case Instruction::BitCast : return bitc::CAST_BITCAST;
82   case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
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 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
164   switch (Kind) {
165   case Attribute::Alignment:
166     return bitc::ATTR_KIND_ALIGNMENT;
167   case Attribute::AlwaysInline:
168     return bitc::ATTR_KIND_ALWAYS_INLINE;
169   case Attribute::ArgMemOnly:
170     return bitc::ATTR_KIND_ARGMEMONLY;
171   case Attribute::Builtin:
172     return bitc::ATTR_KIND_BUILTIN;
173   case Attribute::ByVal:
174     return bitc::ATTR_KIND_BY_VAL;
175   case Attribute::Convergent:
176     return bitc::ATTR_KIND_CONVERGENT;
177   case Attribute::InAlloca:
178     return bitc::ATTR_KIND_IN_ALLOCA;
179   case Attribute::Cold:
180     return bitc::ATTR_KIND_COLD;
181   case Attribute::InaccessibleMemOnly:
182     return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY;
183   case Attribute::InaccessibleMemOrArgMemOnly:
184     return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY;
185   case Attribute::InlineHint:
186     return bitc::ATTR_KIND_INLINE_HINT;
187   case Attribute::InReg:
188     return bitc::ATTR_KIND_IN_REG;
189   case Attribute::JumpTable:
190     return bitc::ATTR_KIND_JUMP_TABLE;
191   case Attribute::MinSize:
192     return bitc::ATTR_KIND_MIN_SIZE;
193   case Attribute::Naked:
194     return bitc::ATTR_KIND_NAKED;
195   case Attribute::Nest:
196     return bitc::ATTR_KIND_NEST;
197   case Attribute::NoAlias:
198     return bitc::ATTR_KIND_NO_ALIAS;
199   case Attribute::NoBuiltin:
200     return bitc::ATTR_KIND_NO_BUILTIN;
201   case Attribute::NoCapture:
202     return bitc::ATTR_KIND_NO_CAPTURE;
203   case Attribute::NoDuplicate:
204     return bitc::ATTR_KIND_NO_DUPLICATE;
205   case Attribute::NoImplicitFloat:
206     return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
207   case Attribute::NoInline:
208     return bitc::ATTR_KIND_NO_INLINE;
209   case Attribute::NoRecurse:
210     return bitc::ATTR_KIND_NO_RECURSE;
211   case Attribute::NonLazyBind:
212     return bitc::ATTR_KIND_NON_LAZY_BIND;
213   case Attribute::NonNull:
214     return bitc::ATTR_KIND_NON_NULL;
215   case Attribute::Dereferenceable:
216     return bitc::ATTR_KIND_DEREFERENCEABLE;
217   case Attribute::DereferenceableOrNull:
218     return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
219   case Attribute::NoRedZone:
220     return bitc::ATTR_KIND_NO_RED_ZONE;
221   case Attribute::NoReturn:
222     return bitc::ATTR_KIND_NO_RETURN;
223   case Attribute::NoUnwind:
224     return bitc::ATTR_KIND_NO_UNWIND;
225   case Attribute::OptimizeForSize:
226     return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
227   case Attribute::OptimizeNone:
228     return bitc::ATTR_KIND_OPTIMIZE_NONE;
229   case Attribute::ReadNone:
230     return bitc::ATTR_KIND_READ_NONE;
231   case Attribute::ReadOnly:
232     return bitc::ATTR_KIND_READ_ONLY;
233   case Attribute::Returned:
234     return bitc::ATTR_KIND_RETURNED;
235   case Attribute::ReturnsTwice:
236     return bitc::ATTR_KIND_RETURNS_TWICE;
237   case Attribute::SExt:
238     return bitc::ATTR_KIND_S_EXT;
239   case Attribute::StackAlignment:
240     return bitc::ATTR_KIND_STACK_ALIGNMENT;
241   case Attribute::StackProtect:
242     return bitc::ATTR_KIND_STACK_PROTECT;
243   case Attribute::StackProtectReq:
244     return bitc::ATTR_KIND_STACK_PROTECT_REQ;
245   case Attribute::StackProtectStrong:
246     return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
247   case Attribute::SafeStack:
248     return bitc::ATTR_KIND_SAFESTACK;
249   case Attribute::StructRet:
250     return bitc::ATTR_KIND_STRUCT_RET;
251   case Attribute::SanitizeAddress:
252     return bitc::ATTR_KIND_SANITIZE_ADDRESS;
253   case Attribute::SanitizeThread:
254     return bitc::ATTR_KIND_SANITIZE_THREAD;
255   case Attribute::SanitizeMemory:
256     return bitc::ATTR_KIND_SANITIZE_MEMORY;
257   case Attribute::UWTable:
258     return bitc::ATTR_KIND_UW_TABLE;
259   case Attribute::ZExt:
260     return bitc::ATTR_KIND_Z_EXT;
261   case Attribute::EndAttrKinds:
262     llvm_unreachable("Can not encode end-attribute kinds marker.");
263   case Attribute::None:
264     llvm_unreachable("Can not encode none-attribute.");
265   }
266
267   llvm_unreachable("Trying to encode unknown attribute");
268 }
269
270 static void WriteAttributeGroupTable(const ValueEnumerator &VE,
271                                      BitstreamWriter &Stream) {
272   const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
273   if (AttrGrps.empty()) return;
274
275   Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
276
277   SmallVector<uint64_t, 64> Record;
278   for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
279     AttributeSet AS = AttrGrps[i];
280     for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
281       AttributeSet A = AS.getSlotAttributes(i);
282
283       Record.push_back(VE.getAttributeGroupID(A));
284       Record.push_back(AS.getSlotIndex(i));
285
286       for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
287            I != E; ++I) {
288         Attribute Attr = *I;
289         if (Attr.isEnumAttribute()) {
290           Record.push_back(0);
291           Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
292         } else if (Attr.isIntAttribute()) {
293           Record.push_back(1);
294           Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
295           Record.push_back(Attr.getValueAsInt());
296         } else {
297           StringRef Kind = Attr.getKindAsString();
298           StringRef Val = Attr.getValueAsString();
299
300           Record.push_back(Val.empty() ? 3 : 4);
301           Record.append(Kind.begin(), Kind.end());
302           Record.push_back(0);
303           if (!Val.empty()) {
304             Record.append(Val.begin(), Val.end());
305             Record.push_back(0);
306           }
307         }
308       }
309
310       Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
311       Record.clear();
312     }
313   }
314
315   Stream.ExitBlock();
316 }
317
318 static void WriteAttributeTable(const ValueEnumerator &VE,
319                                 BitstreamWriter &Stream) {
320   const std::vector<AttributeSet> &Attrs = VE.getAttributes();
321   if (Attrs.empty()) return;
322
323   Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
324
325   SmallVector<uint64_t, 64> Record;
326   for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
327     const AttributeSet &A = Attrs[i];
328     for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
329       Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
330
331     Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
332     Record.clear();
333   }
334
335   Stream.ExitBlock();
336 }
337
338 /// WriteTypeTable - Write out the type table for a module.
339 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
340   const ValueEnumerator::TypeList &TypeList = VE.getTypes();
341
342   Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
343   SmallVector<uint64_t, 64> TypeVals;
344
345   uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
346
347   // Abbrev for TYPE_CODE_POINTER.
348   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
349   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
350   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
351   Abbv->Add(BitCodeAbbrevOp(0));  // Addrspace = 0
352   unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
353
354   // Abbrev for TYPE_CODE_FUNCTION.
355   Abbv = new BitCodeAbbrev();
356   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
357   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // isvararg
358   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
359   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
360
361   unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
362
363   // Abbrev for TYPE_CODE_STRUCT_ANON.
364   Abbv = new BitCodeAbbrev();
365   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
366   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
367   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
368   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
369
370   unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
371
372   // Abbrev for TYPE_CODE_STRUCT_NAME.
373   Abbv = new BitCodeAbbrev();
374   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
375   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
376   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
377   unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
378
379   // Abbrev for TYPE_CODE_STRUCT_NAMED.
380   Abbv = new BitCodeAbbrev();
381   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
382   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));  // ispacked
383   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
384   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
385
386   unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
387
388   // Abbrev for TYPE_CODE_ARRAY.
389   Abbv = new BitCodeAbbrev();
390   Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
391   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // size
392   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
393
394   unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
395
396   // Emit an entry count so the reader can reserve space.
397   TypeVals.push_back(TypeList.size());
398   Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
399   TypeVals.clear();
400
401   // Loop over all of the types, emitting each in turn.
402   for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
403     Type *T = TypeList[i];
404     int AbbrevToUse = 0;
405     unsigned Code = 0;
406
407     switch (T->getTypeID()) {
408     case Type::VoidTyID:      Code = bitc::TYPE_CODE_VOID;      break;
409     case Type::HalfTyID:      Code = bitc::TYPE_CODE_HALF;      break;
410     case Type::FloatTyID:     Code = bitc::TYPE_CODE_FLOAT;     break;
411     case Type::DoubleTyID:    Code = bitc::TYPE_CODE_DOUBLE;    break;
412     case Type::X86_FP80TyID:  Code = bitc::TYPE_CODE_X86_FP80;  break;
413     case Type::FP128TyID:     Code = bitc::TYPE_CODE_FP128;     break;
414     case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
415     case Type::LabelTyID:     Code = bitc::TYPE_CODE_LABEL;     break;
416     case Type::MetadataTyID:  Code = bitc::TYPE_CODE_METADATA;  break;
417     case Type::X86_MMXTyID:   Code = bitc::TYPE_CODE_X86_MMX;   break;
418     case Type::TokenTyID:     Code = bitc::TYPE_CODE_TOKEN;     break;
419     case Type::IntegerTyID:
420       // INTEGER: [width]
421       Code = bitc::TYPE_CODE_INTEGER;
422       TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
423       break;
424     case Type::PointerTyID: {
425       PointerType *PTy = cast<PointerType>(T);
426       // POINTER: [pointee type, address space]
427       Code = bitc::TYPE_CODE_POINTER;
428       TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
429       unsigned AddressSpace = PTy->getAddressSpace();
430       TypeVals.push_back(AddressSpace);
431       if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
432       break;
433     }
434     case Type::FunctionTyID: {
435       FunctionType *FT = cast<FunctionType>(T);
436       // FUNCTION: [isvararg, retty, paramty x N]
437       Code = bitc::TYPE_CODE_FUNCTION;
438       TypeVals.push_back(FT->isVarArg());
439       TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
440       for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
441         TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
442       AbbrevToUse = FunctionAbbrev;
443       break;
444     }
445     case Type::StructTyID: {
446       StructType *ST = cast<StructType>(T);
447       // STRUCT: [ispacked, eltty x N]
448       TypeVals.push_back(ST->isPacked());
449       // Output all of the element types.
450       for (StructType::element_iterator I = ST->element_begin(),
451            E = ST->element_end(); I != E; ++I)
452         TypeVals.push_back(VE.getTypeID(*I));
453
454       if (ST->isLiteral()) {
455         Code = bitc::TYPE_CODE_STRUCT_ANON;
456         AbbrevToUse = StructAnonAbbrev;
457       } else {
458         if (ST->isOpaque()) {
459           Code = bitc::TYPE_CODE_OPAQUE;
460         } else {
461           Code = bitc::TYPE_CODE_STRUCT_NAMED;
462           AbbrevToUse = StructNamedAbbrev;
463         }
464
465         // Emit the name if it is present.
466         if (!ST->getName().empty())
467           WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
468                             StructNameAbbrev, Stream);
469       }
470       break;
471     }
472     case Type::ArrayTyID: {
473       ArrayType *AT = cast<ArrayType>(T);
474       // ARRAY: [numelts, eltty]
475       Code = bitc::TYPE_CODE_ARRAY;
476       TypeVals.push_back(AT->getNumElements());
477       TypeVals.push_back(VE.getTypeID(AT->getElementType()));
478       AbbrevToUse = ArrayAbbrev;
479       break;
480     }
481     case Type::VectorTyID: {
482       VectorType *VT = cast<VectorType>(T);
483       // VECTOR [numelts, eltty]
484       Code = bitc::TYPE_CODE_VECTOR;
485       TypeVals.push_back(VT->getNumElements());
486       TypeVals.push_back(VE.getTypeID(VT->getElementType()));
487       break;
488     }
489     }
490
491     // Emit the finished record.
492     Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
493     TypeVals.clear();
494   }
495
496   Stream.ExitBlock();
497 }
498
499 static unsigned getEncodedLinkage(const GlobalValue &GV) {
500   switch (GV.getLinkage()) {
501   case GlobalValue::ExternalLinkage:
502     return 0;
503   case GlobalValue::WeakAnyLinkage:
504     return 16;
505   case GlobalValue::AppendingLinkage:
506     return 2;
507   case GlobalValue::InternalLinkage:
508     return 3;
509   case GlobalValue::LinkOnceAnyLinkage:
510     return 18;
511   case GlobalValue::ExternalWeakLinkage:
512     return 7;
513   case GlobalValue::CommonLinkage:
514     return 8;
515   case GlobalValue::PrivateLinkage:
516     return 9;
517   case GlobalValue::WeakODRLinkage:
518     return 17;
519   case GlobalValue::LinkOnceODRLinkage:
520     return 19;
521   case GlobalValue::AvailableExternallyLinkage:
522     return 12;
523   }
524   llvm_unreachable("Invalid linkage");
525 }
526
527 static unsigned getEncodedVisibility(const GlobalValue &GV) {
528   switch (GV.getVisibility()) {
529   case GlobalValue::DefaultVisibility:   return 0;
530   case GlobalValue::HiddenVisibility:    return 1;
531   case GlobalValue::ProtectedVisibility: return 2;
532   }
533   llvm_unreachable("Invalid visibility");
534 }
535
536 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
537   switch (GV.getDLLStorageClass()) {
538   case GlobalValue::DefaultStorageClass:   return 0;
539   case GlobalValue::DLLImportStorageClass: return 1;
540   case GlobalValue::DLLExportStorageClass: return 2;
541   }
542   llvm_unreachable("Invalid DLL storage class");
543 }
544
545 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
546   switch (GV.getThreadLocalMode()) {
547     case GlobalVariable::NotThreadLocal:         return 0;
548     case GlobalVariable::GeneralDynamicTLSModel: return 1;
549     case GlobalVariable::LocalDynamicTLSModel:   return 2;
550     case GlobalVariable::InitialExecTLSModel:    return 3;
551     case GlobalVariable::LocalExecTLSModel:      return 4;
552   }
553   llvm_unreachable("Invalid TLS model");
554 }
555
556 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
557   switch (C.getSelectionKind()) {
558   case Comdat::Any:
559     return bitc::COMDAT_SELECTION_KIND_ANY;
560   case Comdat::ExactMatch:
561     return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
562   case Comdat::Largest:
563     return bitc::COMDAT_SELECTION_KIND_LARGEST;
564   case Comdat::NoDuplicates:
565     return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
566   case Comdat::SameSize:
567     return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
568   }
569   llvm_unreachable("Invalid selection kind");
570 }
571
572 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
573   SmallVector<uint16_t, 64> Vals;
574   for (const Comdat *C : VE.getComdats()) {
575     // COMDAT: [selection_kind, name]
576     Vals.push_back(getEncodedComdatSelectionKind(*C));
577     size_t Size = C->getName().size();
578     assert(isUInt<16>(Size));
579     Vals.push_back(Size);
580     for (char Chr : C->getName())
581       Vals.push_back((unsigned char)Chr);
582     Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
583     Vals.clear();
584   }
585 }
586
587 /// Write a record that will eventually hold the word offset of the
588 /// module-level VST. For now the offset is 0, which will be backpatched
589 /// after the real VST is written. Returns the bit offset to backpatch.
590 static uint64_t WriteValueSymbolTableForwardDecl(const ValueSymbolTable &VST,
591                                                  BitstreamWriter &Stream) {
592   if (VST.empty())
593     return 0;
594
595   // Write a placeholder value in for the offset of the real VST,
596   // which is written after the function blocks so that it can include
597   // the offset of each function. The placeholder offset will be
598   // updated when the real VST is written.
599   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
600   Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
601   // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
602   // hold the real VST offset. Must use fixed instead of VBR as we don't
603   // know how many VBR chunks to reserve ahead of time.
604   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
605   unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
606
607   // Emit the placeholder
608   uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
609   Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
610
611   // Compute and return the bit offset to the placeholder, which will be
612   // patched when the real VST is written. We can simply subtract the 32-bit
613   // fixed size from the current bit number to get the location to backpatch.
614   return Stream.GetCurrentBitNo() - 32;
615 }
616
617 /// Emit top-level description of module, including target triple, inline asm,
618 /// descriptors for global variables, and function prototype info.
619 /// Returns the bit offset to backpatch with the location of the real VST.
620 static uint64_t WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
621                                 BitstreamWriter &Stream) {
622   // Emit various pieces of data attached to a module.
623   if (!M->getTargetTriple().empty())
624     WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
625                       0/*TODO*/, Stream);
626   const std::string &DL = M->getDataLayoutStr();
627   if (!DL.empty())
628     WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
629   if (!M->getModuleInlineAsm().empty())
630     WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
631                       0/*TODO*/, Stream);
632
633   // Emit information about sections and GC, computing how many there are. Also
634   // compute the maximum alignment value.
635   std::map<std::string, unsigned> SectionMap;
636   std::map<std::string, unsigned> GCMap;
637   unsigned MaxAlignment = 0;
638   unsigned MaxGlobalType = 0;
639   for (const GlobalValue &GV : M->globals()) {
640     MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
641     MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
642     if (GV.hasSection()) {
643       // Give section names unique ID's.
644       unsigned &Entry = SectionMap[GV.getSection()];
645       if (!Entry) {
646         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
647                           0/*TODO*/, Stream);
648         Entry = SectionMap.size();
649       }
650     }
651   }
652   for (const Function &F : *M) {
653     MaxAlignment = std::max(MaxAlignment, F.getAlignment());
654     if (F.hasSection()) {
655       // Give section names unique ID's.
656       unsigned &Entry = SectionMap[F.getSection()];
657       if (!Entry) {
658         WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
659                           0/*TODO*/, Stream);
660         Entry = SectionMap.size();
661       }
662     }
663     if (F.hasGC()) {
664       // Same for GC names.
665       unsigned &Entry = GCMap[F.getGC()];
666       if (!Entry) {
667         WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
668                           0/*TODO*/, Stream);
669         Entry = GCMap.size();
670       }
671     }
672   }
673
674   // Emit abbrev for globals, now that we know # sections and max alignment.
675   unsigned SimpleGVarAbbrev = 0;
676   if (!M->global_empty()) {
677     // Add an abbrev for common globals with no visibility or thread localness.
678     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
679     Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
680     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
681                               Log2_32_Ceil(MaxGlobalType+1)));
682     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddrSpace << 2
683                                                            //| explicitType << 1
684                                                            //| constant
685     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Initializer.
686     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
687     if (MaxAlignment == 0)                                 // Alignment.
688       Abbv->Add(BitCodeAbbrevOp(0));
689     else {
690       unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
691       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
692                                Log2_32_Ceil(MaxEncAlignment+1)));
693     }
694     if (SectionMap.empty())                                    // Section.
695       Abbv->Add(BitCodeAbbrevOp(0));
696     else
697       Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
698                                Log2_32_Ceil(SectionMap.size()+1)));
699     // Don't bother emitting vis + thread local.
700     SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
701   }
702
703   // Emit the global variable information.
704   SmallVector<unsigned, 64> Vals;
705   for (const GlobalVariable &GV : M->globals()) {
706     unsigned AbbrevToUse = 0;
707
708     // GLOBALVAR: [type, isconst, initid,
709     //             linkage, alignment, section, visibility, threadlocal,
710     //             unnamed_addr, externally_initialized, dllstorageclass,
711     //             comdat]
712     Vals.push_back(VE.getTypeID(GV.getValueType()));
713     Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
714     Vals.push_back(GV.isDeclaration() ? 0 :
715                    (VE.getValueID(GV.getInitializer()) + 1));
716     Vals.push_back(getEncodedLinkage(GV));
717     Vals.push_back(Log2_32(GV.getAlignment())+1);
718     Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
719     if (GV.isThreadLocal() ||
720         GV.getVisibility() != GlobalValue::DefaultVisibility ||
721         GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
722         GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
723         GV.hasComdat()) {
724       Vals.push_back(getEncodedVisibility(GV));
725       Vals.push_back(getEncodedThreadLocalMode(GV));
726       Vals.push_back(GV.hasUnnamedAddr());
727       Vals.push_back(GV.isExternallyInitialized());
728       Vals.push_back(getEncodedDLLStorageClass(GV));
729       Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
730     } else {
731       AbbrevToUse = SimpleGVarAbbrev;
732     }
733
734     Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
735     Vals.clear();
736   }
737
738   // Emit the function proto information.
739   for (const Function &F : *M) {
740     // FUNCTION:  [type, callingconv, isproto, linkage, paramattrs, alignment,
741     //             section, visibility, gc, unnamed_addr, prologuedata,
742     //             dllstorageclass, comdat, prefixdata, personalityfn]
743     Vals.push_back(VE.getTypeID(F.getFunctionType()));
744     Vals.push_back(F.getCallingConv());
745     Vals.push_back(F.isDeclaration());
746     Vals.push_back(getEncodedLinkage(F));
747     Vals.push_back(VE.getAttributeID(F.getAttributes()));
748     Vals.push_back(Log2_32(F.getAlignment())+1);
749     Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
750     Vals.push_back(getEncodedVisibility(F));
751     Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
752     Vals.push_back(F.hasUnnamedAddr());
753     Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
754                                        : 0);
755     Vals.push_back(getEncodedDLLStorageClass(F));
756     Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
757     Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
758                                      : 0);
759     Vals.push_back(
760         F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
761
762     unsigned AbbrevToUse = 0;
763     Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
764     Vals.clear();
765   }
766
767   // Emit the alias information.
768   for (const GlobalAlias &A : M->aliases()) {
769     // ALIAS: [alias type, aliasee val#, linkage, visibility]
770     Vals.push_back(VE.getTypeID(A.getValueType()));
771     Vals.push_back(A.getType()->getAddressSpace());
772     Vals.push_back(VE.getValueID(A.getAliasee()));
773     Vals.push_back(getEncodedLinkage(A));
774     Vals.push_back(getEncodedVisibility(A));
775     Vals.push_back(getEncodedDLLStorageClass(A));
776     Vals.push_back(getEncodedThreadLocalMode(A));
777     Vals.push_back(A.hasUnnamedAddr());
778     unsigned AbbrevToUse = 0;
779     Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
780     Vals.clear();
781   }
782
783   // Write a record indicating the number of module-level metadata IDs
784   // This is needed because the ids of metadata are assigned implicitly
785   // based on their ordering in the bitcode, with the function-level
786   // metadata ids starting after the module-level metadata ids. For
787   // function importing where we lazy load the metadata as a postpass,
788   // we want to avoid parsing the module-level metadata before parsing
789   // the imported functions.
790   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
791   Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_METADATA_VALUES));
792   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
793   unsigned MDValsAbbrev = Stream.EmitAbbrev(Abbv);
794   Vals.push_back(VE.numMDs());
795   Stream.EmitRecord(bitc::MODULE_CODE_METADATA_VALUES, Vals, MDValsAbbrev);
796   Vals.clear();
797
798   uint64_t VSTOffsetPlaceholder =
799       WriteValueSymbolTableForwardDecl(M->getValueSymbolTable(), Stream);
800   return VSTOffsetPlaceholder;
801 }
802
803 static uint64_t GetOptimizationFlags(const Value *V) {
804   uint64_t Flags = 0;
805
806   if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
807     if (OBO->hasNoSignedWrap())
808       Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
809     if (OBO->hasNoUnsignedWrap())
810       Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
811   } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
812     if (PEO->isExact())
813       Flags |= 1 << bitc::PEO_EXACT;
814   } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
815     if (FPMO->hasUnsafeAlgebra())
816       Flags |= FastMathFlags::UnsafeAlgebra;
817     if (FPMO->hasNoNaNs())
818       Flags |= FastMathFlags::NoNaNs;
819     if (FPMO->hasNoInfs())
820       Flags |= FastMathFlags::NoInfs;
821     if (FPMO->hasNoSignedZeros())
822       Flags |= FastMathFlags::NoSignedZeros;
823     if (FPMO->hasAllowReciprocal())
824       Flags |= FastMathFlags::AllowReciprocal;
825   }
826
827   return Flags;
828 }
829
830 static void WriteValueAsMetadata(const ValueAsMetadata *MD,
831                                  const ValueEnumerator &VE,
832                                  BitstreamWriter &Stream,
833                                  SmallVectorImpl<uint64_t> &Record) {
834   // Mimic an MDNode with a value as one operand.
835   Value *V = MD->getValue();
836   Record.push_back(VE.getTypeID(V->getType()));
837   Record.push_back(VE.getValueID(V));
838   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
839   Record.clear();
840 }
841
842 static void WriteMDTuple(const MDTuple *N, const ValueEnumerator &VE,
843                          BitstreamWriter &Stream,
844                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
845   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
846     Metadata *MD = N->getOperand(i);
847     assert(!(MD && isa<LocalAsMetadata>(MD)) &&
848            "Unexpected function-local metadata");
849     Record.push_back(VE.getMetadataOrNullID(MD));
850   }
851   Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
852                                     : bitc::METADATA_NODE,
853                     Record, Abbrev);
854   Record.clear();
855 }
856
857 static void WriteDILocation(const DILocation *N, const ValueEnumerator &VE,
858                             BitstreamWriter &Stream,
859                             SmallVectorImpl<uint64_t> &Record,
860                             unsigned Abbrev) {
861   Record.push_back(N->isDistinct());
862   Record.push_back(N->getLine());
863   Record.push_back(N->getColumn());
864   Record.push_back(VE.getMetadataID(N->getScope()));
865   Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
866
867   Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
868   Record.clear();
869 }
870
871 static void WriteGenericDINode(const GenericDINode *N,
872                                const ValueEnumerator &VE,
873                                BitstreamWriter &Stream,
874                                SmallVectorImpl<uint64_t> &Record,
875                                unsigned Abbrev) {
876   Record.push_back(N->isDistinct());
877   Record.push_back(N->getTag());
878   Record.push_back(0); // Per-tag version field; unused for now.
879
880   for (auto &I : N->operands())
881     Record.push_back(VE.getMetadataOrNullID(I));
882
883   Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
884   Record.clear();
885 }
886
887 static uint64_t rotateSign(int64_t I) {
888   uint64_t U = I;
889   return I < 0 ? ~(U << 1) : U << 1;
890 }
891
892 static void WriteDISubrange(const DISubrange *N, const ValueEnumerator &,
893                             BitstreamWriter &Stream,
894                             SmallVectorImpl<uint64_t> &Record,
895                             unsigned Abbrev) {
896   Record.push_back(N->isDistinct());
897   Record.push_back(N->getCount());
898   Record.push_back(rotateSign(N->getLowerBound()));
899
900   Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
901   Record.clear();
902 }
903
904 static void WriteDIEnumerator(const DIEnumerator *N, const ValueEnumerator &VE,
905                               BitstreamWriter &Stream,
906                               SmallVectorImpl<uint64_t> &Record,
907                               unsigned Abbrev) {
908   Record.push_back(N->isDistinct());
909   Record.push_back(rotateSign(N->getValue()));
910   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
911
912   Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
913   Record.clear();
914 }
915
916 static void WriteDIBasicType(const DIBasicType *N, const ValueEnumerator &VE,
917                              BitstreamWriter &Stream,
918                              SmallVectorImpl<uint64_t> &Record,
919                              unsigned Abbrev) {
920   Record.push_back(N->isDistinct());
921   Record.push_back(N->getTag());
922   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
923   Record.push_back(N->getSizeInBits());
924   Record.push_back(N->getAlignInBits());
925   Record.push_back(N->getEncoding());
926
927   Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
928   Record.clear();
929 }
930
931 static void WriteDIDerivedType(const DIDerivedType *N,
932                                const ValueEnumerator &VE,
933                                BitstreamWriter &Stream,
934                                SmallVectorImpl<uint64_t> &Record,
935                                unsigned Abbrev) {
936   Record.push_back(N->isDistinct());
937   Record.push_back(N->getTag());
938   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
939   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
940   Record.push_back(N->getLine());
941   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
942   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
943   Record.push_back(N->getSizeInBits());
944   Record.push_back(N->getAlignInBits());
945   Record.push_back(N->getOffsetInBits());
946   Record.push_back(N->getFlags());
947   Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
948
949   Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
950   Record.clear();
951 }
952
953 static void WriteDICompositeType(const DICompositeType *N,
954                                  const ValueEnumerator &VE,
955                                  BitstreamWriter &Stream,
956                                  SmallVectorImpl<uint64_t> &Record,
957                                  unsigned Abbrev) {
958   Record.push_back(N->isDistinct());
959   Record.push_back(N->getTag());
960   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
961   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
962   Record.push_back(N->getLine());
963   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
964   Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
965   Record.push_back(N->getSizeInBits());
966   Record.push_back(N->getAlignInBits());
967   Record.push_back(N->getOffsetInBits());
968   Record.push_back(N->getFlags());
969   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
970   Record.push_back(N->getRuntimeLang());
971   Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
972   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
973   Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
974
975   Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
976   Record.clear();
977 }
978
979 static void WriteDISubroutineType(const DISubroutineType *N,
980                                   const ValueEnumerator &VE,
981                                   BitstreamWriter &Stream,
982                                   SmallVectorImpl<uint64_t> &Record,
983                                   unsigned Abbrev) {
984   Record.push_back(N->isDistinct());
985   Record.push_back(N->getFlags());
986   Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
987
988   Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
989   Record.clear();
990 }
991
992 static void WriteDIFile(const DIFile *N, const ValueEnumerator &VE,
993                         BitstreamWriter &Stream,
994                         SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
995   Record.push_back(N->isDistinct());
996   Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
997   Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
998
999   Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
1000   Record.clear();
1001 }
1002
1003 static void WriteDICompileUnit(const DICompileUnit *N,
1004                                const ValueEnumerator &VE,
1005                                BitstreamWriter &Stream,
1006                                SmallVectorImpl<uint64_t> &Record,
1007                                unsigned Abbrev) {
1008   assert(N->isDistinct() && "Expected distinct compile units");
1009   Record.push_back(/* IsDistinct */ true);
1010   Record.push_back(N->getSourceLanguage());
1011   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1012   Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
1013   Record.push_back(N->isOptimized());
1014   Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
1015   Record.push_back(N->getRuntimeVersion());
1016   Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
1017   Record.push_back(N->getEmissionKind());
1018   Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1019   Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1020   Record.push_back(VE.getMetadataOrNullID(N->getSubprograms().get()));
1021   Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1022   Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1023   Record.push_back(N->getDWOId());
1024   Record.push_back(VE.getMetadataOrNullID(N->getMacros().get()));
1025
1026   Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1027   Record.clear();
1028 }
1029
1030 static void WriteDISubprogram(const DISubprogram *N, const ValueEnumerator &VE,
1031                               BitstreamWriter &Stream,
1032                               SmallVectorImpl<uint64_t> &Record,
1033                               unsigned Abbrev) {
1034   Record.push_back(N->isDistinct());
1035   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1036   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1037   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1038   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1039   Record.push_back(N->getLine());
1040   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1041   Record.push_back(N->isLocalToUnit());
1042   Record.push_back(N->isDefinition());
1043   Record.push_back(N->getScopeLine());
1044   Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1045   Record.push_back(N->getVirtuality());
1046   Record.push_back(N->getVirtualIndex());
1047   Record.push_back(N->getFlags());
1048   Record.push_back(N->isOptimized());
1049   Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1050   Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1051   Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
1052
1053   Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1054   Record.clear();
1055 }
1056
1057 static void WriteDILexicalBlock(const DILexicalBlock *N,
1058                                 const ValueEnumerator &VE,
1059                                 BitstreamWriter &Stream,
1060                                 SmallVectorImpl<uint64_t> &Record,
1061                                 unsigned Abbrev) {
1062   Record.push_back(N->isDistinct());
1063   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1064   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1065   Record.push_back(N->getLine());
1066   Record.push_back(N->getColumn());
1067
1068   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1069   Record.clear();
1070 }
1071
1072 static void WriteDILexicalBlockFile(const DILexicalBlockFile *N,
1073                                     const ValueEnumerator &VE,
1074                                     BitstreamWriter &Stream,
1075                                     SmallVectorImpl<uint64_t> &Record,
1076                                     unsigned Abbrev) {
1077   Record.push_back(N->isDistinct());
1078   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1079   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1080   Record.push_back(N->getDiscriminator());
1081
1082   Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1083   Record.clear();
1084 }
1085
1086 static void WriteDINamespace(const DINamespace *N, const ValueEnumerator &VE,
1087                              BitstreamWriter &Stream,
1088                              SmallVectorImpl<uint64_t> &Record,
1089                              unsigned Abbrev) {
1090   Record.push_back(N->isDistinct());
1091   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1092   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1093   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1094   Record.push_back(N->getLine());
1095
1096   Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1097   Record.clear();
1098 }
1099
1100 static void WriteDIMacro(const DIMacro *N, const ValueEnumerator &VE,
1101                          BitstreamWriter &Stream,
1102                          SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
1103   Record.push_back(N->isDistinct());
1104   Record.push_back(N->getMacinfoType());
1105   Record.push_back(N->getLine());
1106   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1107   Record.push_back(VE.getMetadataOrNullID(N->getRawValue()));
1108
1109   Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev);
1110   Record.clear();
1111 }
1112
1113 static void WriteDIMacroFile(const DIMacroFile *N, const ValueEnumerator &VE,
1114                              BitstreamWriter &Stream,
1115                              SmallVectorImpl<uint64_t> &Record,
1116                              unsigned Abbrev) {
1117   Record.push_back(N->isDistinct());
1118   Record.push_back(N->getMacinfoType());
1119   Record.push_back(N->getLine());
1120   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1121   Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
1122
1123   Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev);
1124   Record.clear();
1125 }
1126
1127 static void WriteDIModule(const DIModule *N, const ValueEnumerator &VE,
1128                           BitstreamWriter &Stream,
1129                           SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
1130   Record.push_back(N->isDistinct());
1131   for (auto &I : N->operands())
1132     Record.push_back(VE.getMetadataOrNullID(I));
1133
1134   Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1135   Record.clear();
1136 }
1137
1138 static void WriteDITemplateTypeParameter(const DITemplateTypeParameter *N,
1139                                          const ValueEnumerator &VE,
1140                                          BitstreamWriter &Stream,
1141                                          SmallVectorImpl<uint64_t> &Record,
1142                                          unsigned Abbrev) {
1143   Record.push_back(N->isDistinct());
1144   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1145   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1146
1147   Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1148   Record.clear();
1149 }
1150
1151 static void WriteDITemplateValueParameter(const DITemplateValueParameter *N,
1152                                           const ValueEnumerator &VE,
1153                                           BitstreamWriter &Stream,
1154                                           SmallVectorImpl<uint64_t> &Record,
1155                                           unsigned Abbrev) {
1156   Record.push_back(N->isDistinct());
1157   Record.push_back(N->getTag());
1158   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1159   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1160   Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1161
1162   Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1163   Record.clear();
1164 }
1165
1166 static void WriteDIGlobalVariable(const DIGlobalVariable *N,
1167                                   const ValueEnumerator &VE,
1168                                   BitstreamWriter &Stream,
1169                                   SmallVectorImpl<uint64_t> &Record,
1170                                   unsigned Abbrev) {
1171   Record.push_back(N->isDistinct());
1172   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1173   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1174   Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1175   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1176   Record.push_back(N->getLine());
1177   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1178   Record.push_back(N->isLocalToUnit());
1179   Record.push_back(N->isDefinition());
1180   Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
1181   Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1182
1183   Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1184   Record.clear();
1185 }
1186
1187 static void WriteDILocalVariable(const DILocalVariable *N,
1188                                  const ValueEnumerator &VE,
1189                                  BitstreamWriter &Stream,
1190                                  SmallVectorImpl<uint64_t> &Record,
1191                                  unsigned Abbrev) {
1192   Record.push_back(N->isDistinct());
1193   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1194   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1195   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1196   Record.push_back(N->getLine());
1197   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1198   Record.push_back(N->getArg());
1199   Record.push_back(N->getFlags());
1200
1201   Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1202   Record.clear();
1203 }
1204
1205 static void WriteDIExpression(const DIExpression *N, const ValueEnumerator &,
1206                               BitstreamWriter &Stream,
1207                               SmallVectorImpl<uint64_t> &Record,
1208                               unsigned Abbrev) {
1209   Record.reserve(N->getElements().size() + 1);
1210
1211   Record.push_back(N->isDistinct());
1212   Record.append(N->elements_begin(), N->elements_end());
1213
1214   Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1215   Record.clear();
1216 }
1217
1218 static void WriteDIObjCProperty(const DIObjCProperty *N,
1219                                 const ValueEnumerator &VE,
1220                                 BitstreamWriter &Stream,
1221                                 SmallVectorImpl<uint64_t> &Record,
1222                                 unsigned Abbrev) {
1223   Record.push_back(N->isDistinct());
1224   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1225   Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1226   Record.push_back(N->getLine());
1227   Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1228   Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1229   Record.push_back(N->getAttributes());
1230   Record.push_back(VE.getMetadataOrNullID(N->getType()));
1231
1232   Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
1233   Record.clear();
1234 }
1235
1236 static void WriteDIImportedEntity(const DIImportedEntity *N,
1237                                   const ValueEnumerator &VE,
1238                                   BitstreamWriter &Stream,
1239                                   SmallVectorImpl<uint64_t> &Record,
1240                                   unsigned Abbrev) {
1241   Record.push_back(N->isDistinct());
1242   Record.push_back(N->getTag());
1243   Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1244   Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1245   Record.push_back(N->getLine());
1246   Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1247
1248   Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1249   Record.clear();
1250 }
1251
1252 static void WriteModuleMetadata(const Module *M,
1253                                 const ValueEnumerator &VE,
1254                                 BitstreamWriter &Stream) {
1255   const auto &MDs = VE.getMDs();
1256   if (MDs.empty() && M->named_metadata_empty())
1257     return;
1258
1259   Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1260
1261   unsigned MDSAbbrev = 0;
1262   if (VE.hasMDString()) {
1263     // Abbrev for METADATA_STRING.
1264     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1265     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
1266     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1267     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1268     MDSAbbrev = Stream.EmitAbbrev(Abbv);
1269   }
1270
1271   // Initialize MDNode abbreviations.
1272 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1273 #include "llvm/IR/Metadata.def"
1274
1275   if (VE.hasDILocation()) {
1276     // Abbrev for METADATA_LOCATION.
1277     //
1278     // Assume the column is usually under 128, and always output the inlined-at
1279     // location (it's never more expensive than building an array size 1).
1280     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1281     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1282     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1283     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1284     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1285     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1286     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1287     DILocationAbbrev = Stream.EmitAbbrev(Abbv);
1288   }
1289
1290   if (VE.hasGenericDINode()) {
1291     // Abbrev for METADATA_GENERIC_DEBUG.
1292     //
1293     // Assume the column is usually under 128, and always output the inlined-at
1294     // location (it's never more expensive than building an array size 1).
1295     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1296     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1297     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1298     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1299     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1300     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1301     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1302     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1303     GenericDINodeAbbrev = Stream.EmitAbbrev(Abbv);
1304   }
1305
1306   unsigned NameAbbrev = 0;
1307   if (!M->named_metadata_empty()) {
1308     // Abbrev for METADATA_NAME.
1309     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1310     Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1311     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1312     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1313     NameAbbrev = Stream.EmitAbbrev(Abbv);
1314   }
1315
1316   SmallVector<uint64_t, 64> Record;
1317   for (const Metadata *MD : MDs) {
1318     if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1319       assert(N->isResolved() && "Expected forward references to be resolved");
1320
1321       switch (N->getMetadataID()) {
1322       default:
1323         llvm_unreachable("Invalid MDNode subclass");
1324 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
1325   case Metadata::CLASS##Kind:                                                  \
1326     Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev);           \
1327     continue;
1328 #include "llvm/IR/Metadata.def"
1329       }
1330     }
1331     if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
1332       WriteValueAsMetadata(MDC, VE, Stream, Record);
1333       continue;
1334     }
1335     const MDString *MDS = cast<MDString>(MD);
1336     // Code: [strchar x N]
1337     Record.append(MDS->bytes_begin(), MDS->bytes_end());
1338
1339     // Emit the finished record.
1340     Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
1341     Record.clear();
1342   }
1343
1344   // Write named metadata.
1345   for (const NamedMDNode &NMD : M->named_metadata()) {
1346     // Write name.
1347     StringRef Str = NMD.getName();
1348     Record.append(Str.bytes_begin(), Str.bytes_end());
1349     Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1350     Record.clear();
1351
1352     // Write named metadata operands.
1353     for (const MDNode *N : NMD.operands())
1354       Record.push_back(VE.getMetadataID(N));
1355     Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1356     Record.clear();
1357   }
1358
1359   Stream.ExitBlock();
1360 }
1361
1362 static void WriteFunctionLocalMetadata(const Function &F,
1363                                        const ValueEnumerator &VE,
1364                                        BitstreamWriter &Stream) {
1365   bool StartedMetadataBlock = false;
1366   SmallVector<uint64_t, 64> Record;
1367   const SmallVectorImpl<const LocalAsMetadata *> &MDs =
1368       VE.getFunctionLocalMDs();
1369   for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1370     assert(MDs[i] && "Expected valid function-local metadata");
1371     if (!StartedMetadataBlock) {
1372       Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1373       StartedMetadataBlock = true;
1374     }
1375     WriteValueAsMetadata(MDs[i], VE, Stream, Record);
1376   }
1377
1378   if (StartedMetadataBlock)
1379     Stream.ExitBlock();
1380 }
1381
1382 static void WriteMetadataAttachment(const Function &F,
1383                                     const ValueEnumerator &VE,
1384                                     BitstreamWriter &Stream) {
1385   Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1386
1387   SmallVector<uint64_t, 64> Record;
1388
1389   // Write metadata attachments
1390   // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1391   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1392   F.getAllMetadata(MDs);
1393   if (!MDs.empty()) {
1394     for (const auto &I : MDs) {
1395       Record.push_back(I.first);
1396       Record.push_back(VE.getMetadataID(I.second));
1397     }
1398     Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1399     Record.clear();
1400   }
1401
1402   for (const BasicBlock &BB : F)
1403     for (const Instruction &I : BB) {
1404       MDs.clear();
1405       I.getAllMetadataOtherThanDebugLoc(MDs);
1406
1407       // If no metadata, ignore instruction.
1408       if (MDs.empty()) continue;
1409
1410       Record.push_back(VE.getInstructionID(&I));
1411
1412       for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1413         Record.push_back(MDs[i].first);
1414         Record.push_back(VE.getMetadataID(MDs[i].second));
1415       }
1416       Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1417       Record.clear();
1418     }
1419
1420   Stream.ExitBlock();
1421 }
1422
1423 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
1424   SmallVector<uint64_t, 64> Record;
1425
1426   // Write metadata kinds
1427   // METADATA_KIND - [n x [id, name]]
1428   SmallVector<StringRef, 8> Names;
1429   M->getMDKindNames(Names);
1430
1431   if (Names.empty()) return;
1432
1433   Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
1434
1435   for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1436     Record.push_back(MDKindID);
1437     StringRef KName = Names[MDKindID];
1438     Record.append(KName.begin(), KName.end());
1439
1440     Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1441     Record.clear();
1442   }
1443
1444   Stream.ExitBlock();
1445 }
1446
1447 static void WriteOperandBundleTags(const Module *M, BitstreamWriter &Stream) {
1448   // Write metadata kinds
1449   //
1450   // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1451   //
1452   // OPERAND_BUNDLE_TAG - [strchr x N]
1453
1454   SmallVector<StringRef, 8> Tags;
1455   M->getOperandBundleTags(Tags);
1456
1457   if (Tags.empty())
1458     return;
1459
1460   Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
1461
1462   SmallVector<uint64_t, 64> Record;
1463
1464   for (auto Tag : Tags) {
1465     Record.append(Tag.begin(), Tag.end());
1466
1467     Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
1468     Record.clear();
1469   }
1470
1471   Stream.ExitBlock();
1472 }
1473
1474 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1475   if ((int64_t)V >= 0)
1476     Vals.push_back(V << 1);
1477   else
1478     Vals.push_back((-V << 1) | 1);
1479 }
1480
1481 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
1482                            const ValueEnumerator &VE,
1483                            BitstreamWriter &Stream, bool isGlobal) {
1484   if (FirstVal == LastVal) return;
1485
1486   Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1487
1488   unsigned AggregateAbbrev = 0;
1489   unsigned String8Abbrev = 0;
1490   unsigned CString7Abbrev = 0;
1491   unsigned CString6Abbrev = 0;
1492   // If this is a constant pool for the module, emit module-specific abbrevs.
1493   if (isGlobal) {
1494     // Abbrev for CST_CODE_AGGREGATE.
1495     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1496     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1497     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1498     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
1499     AggregateAbbrev = Stream.EmitAbbrev(Abbv);
1500
1501     // Abbrev for CST_CODE_STRING.
1502     Abbv = new BitCodeAbbrev();
1503     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1504     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1505     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1506     String8Abbrev = Stream.EmitAbbrev(Abbv);
1507     // Abbrev for CST_CODE_CSTRING.
1508     Abbv = new BitCodeAbbrev();
1509     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1510     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1511     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1512     CString7Abbrev = Stream.EmitAbbrev(Abbv);
1513     // Abbrev for CST_CODE_CSTRING.
1514     Abbv = new BitCodeAbbrev();
1515     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1516     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1517     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1518     CString6Abbrev = Stream.EmitAbbrev(Abbv);
1519   }
1520
1521   SmallVector<uint64_t, 64> Record;
1522
1523   const ValueEnumerator::ValueList &Vals = VE.getValues();
1524   Type *LastTy = nullptr;
1525   for (unsigned i = FirstVal; i != LastVal; ++i) {
1526     const Value *V = Vals[i].first;
1527     // If we need to switch types, do so now.
1528     if (V->getType() != LastTy) {
1529       LastTy = V->getType();
1530       Record.push_back(VE.getTypeID(LastTy));
1531       Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1532                         CONSTANTS_SETTYPE_ABBREV);
1533       Record.clear();
1534     }
1535
1536     if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1537       Record.push_back(unsigned(IA->hasSideEffects()) |
1538                        unsigned(IA->isAlignStack()) << 1 |
1539                        unsigned(IA->getDialect()&1) << 2);
1540
1541       // Add the asm string.
1542       const std::string &AsmStr = IA->getAsmString();
1543       Record.push_back(AsmStr.size());
1544       Record.append(AsmStr.begin(), AsmStr.end());
1545
1546       // Add the constraint string.
1547       const std::string &ConstraintStr = IA->getConstraintString();
1548       Record.push_back(ConstraintStr.size());
1549       Record.append(ConstraintStr.begin(), ConstraintStr.end());
1550       Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
1551       Record.clear();
1552       continue;
1553     }
1554     const Constant *C = cast<Constant>(V);
1555     unsigned Code = -1U;
1556     unsigned AbbrevToUse = 0;
1557     if (C->isNullValue()) {
1558       Code = bitc::CST_CODE_NULL;
1559     } else if (isa<UndefValue>(C)) {
1560       Code = bitc::CST_CODE_UNDEF;
1561     } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
1562       if (IV->getBitWidth() <= 64) {
1563         uint64_t V = IV->getSExtValue();
1564         emitSignedInt64(Record, V);
1565         Code = bitc::CST_CODE_INTEGER;
1566         AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1567       } else {                             // Wide integers, > 64 bits in size.
1568         // We have an arbitrary precision integer value to write whose
1569         // bit width is > 64. However, in canonical unsigned integer
1570         // format it is likely that the high bits are going to be zero.
1571         // So, we only write the number of active words.
1572         unsigned NWords = IV->getValue().getActiveWords();
1573         const uint64_t *RawWords = IV->getValue().getRawData();
1574         for (unsigned i = 0; i != NWords; ++i) {
1575           emitSignedInt64(Record, RawWords[i]);
1576         }
1577         Code = bitc::CST_CODE_WIDE_INTEGER;
1578       }
1579     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1580       Code = bitc::CST_CODE_FLOAT;
1581       Type *Ty = CFP->getType();
1582       if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
1583         Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
1584       } else if (Ty->isX86_FP80Ty()) {
1585         // api needed to prevent premature destruction
1586         // bits are not in the same order as a normal i80 APInt, compensate.
1587         APInt api = CFP->getValueAPF().bitcastToAPInt();
1588         const uint64_t *p = api.getRawData();
1589         Record.push_back((p[1] << 48) | (p[0] >> 16));
1590         Record.push_back(p[0] & 0xffffLL);
1591       } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
1592         APInt api = CFP->getValueAPF().bitcastToAPInt();
1593         const uint64_t *p = api.getRawData();
1594         Record.push_back(p[0]);
1595         Record.push_back(p[1]);
1596       } else {
1597         assert (0 && "Unknown FP type!");
1598       }
1599     } else if (isa<ConstantDataSequential>(C) &&
1600                cast<ConstantDataSequential>(C)->isString()) {
1601       const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1602       // Emit constant strings specially.
1603       unsigned NumElts = Str->getNumElements();
1604       // If this is a null-terminated string, use the denser CSTRING encoding.
1605       if (Str->isCString()) {
1606         Code = bitc::CST_CODE_CSTRING;
1607         --NumElts;  // Don't encode the null, which isn't allowed by char6.
1608       } else {
1609         Code = bitc::CST_CODE_STRING;
1610         AbbrevToUse = String8Abbrev;
1611       }
1612       bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1613       bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1614       for (unsigned i = 0; i != NumElts; ++i) {
1615         unsigned char V = Str->getElementAsInteger(i);
1616         Record.push_back(V);
1617         isCStr7 &= (V & 128) == 0;
1618         if (isCStrChar6)
1619           isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1620       }
1621
1622       if (isCStrChar6)
1623         AbbrevToUse = CString6Abbrev;
1624       else if (isCStr7)
1625         AbbrevToUse = CString7Abbrev;
1626     } else if (const ConstantDataSequential *CDS =
1627                   dyn_cast<ConstantDataSequential>(C)) {
1628       Code = bitc::CST_CODE_DATA;
1629       Type *EltTy = CDS->getType()->getElementType();
1630       if (isa<IntegerType>(EltTy)) {
1631         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1632           Record.push_back(CDS->getElementAsInteger(i));
1633       } else if (EltTy->isFloatTy()) {
1634         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1635           union { float F; uint32_t I; };
1636           F = CDS->getElementAsFloat(i);
1637           Record.push_back(I);
1638         }
1639       } else {
1640         assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
1641         for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1642           union { double F; uint64_t I; };
1643           F = CDS->getElementAsDouble(i);
1644           Record.push_back(I);
1645         }
1646       }
1647     } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1648                isa<ConstantVector>(C)) {
1649       Code = bitc::CST_CODE_AGGREGATE;
1650       for (const Value *Op : C->operands())
1651         Record.push_back(VE.getValueID(Op));
1652       AbbrevToUse = AggregateAbbrev;
1653     } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1654       switch (CE->getOpcode()) {
1655       default:
1656         if (Instruction::isCast(CE->getOpcode())) {
1657           Code = bitc::CST_CODE_CE_CAST;
1658           Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
1659           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1660           Record.push_back(VE.getValueID(C->getOperand(0)));
1661           AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
1662         } else {
1663           assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1664           Code = bitc::CST_CODE_CE_BINOP;
1665           Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
1666           Record.push_back(VE.getValueID(C->getOperand(0)));
1667           Record.push_back(VE.getValueID(C->getOperand(1)));
1668           uint64_t Flags = GetOptimizationFlags(CE);
1669           if (Flags != 0)
1670             Record.push_back(Flags);
1671         }
1672         break;
1673       case Instruction::GetElementPtr: {
1674         Code = bitc::CST_CODE_CE_GEP;
1675         const auto *GO = cast<GEPOperator>(C);
1676         if (GO->isInBounds())
1677           Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
1678         Record.push_back(VE.getTypeID(GO->getSourceElementType()));
1679         for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1680           Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1681           Record.push_back(VE.getValueID(C->getOperand(i)));
1682         }
1683         break;
1684       }
1685       case Instruction::Select:
1686         Code = bitc::CST_CODE_CE_SELECT;
1687         Record.push_back(VE.getValueID(C->getOperand(0)));
1688         Record.push_back(VE.getValueID(C->getOperand(1)));
1689         Record.push_back(VE.getValueID(C->getOperand(2)));
1690         break;
1691       case Instruction::ExtractElement:
1692         Code = bitc::CST_CODE_CE_EXTRACTELT;
1693         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1694         Record.push_back(VE.getValueID(C->getOperand(0)));
1695         Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
1696         Record.push_back(VE.getValueID(C->getOperand(1)));
1697         break;
1698       case Instruction::InsertElement:
1699         Code = bitc::CST_CODE_CE_INSERTELT;
1700         Record.push_back(VE.getValueID(C->getOperand(0)));
1701         Record.push_back(VE.getValueID(C->getOperand(1)));
1702         Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
1703         Record.push_back(VE.getValueID(C->getOperand(2)));
1704         break;
1705       case Instruction::ShuffleVector:
1706         // If the return type and argument types are the same, this is a
1707         // standard shufflevector instruction.  If the types are different,
1708         // then the shuffle is widening or truncating the input vectors, and
1709         // the argument type must also be encoded.
1710         if (C->getType() == C->getOperand(0)->getType()) {
1711           Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1712         } else {
1713           Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1714           Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1715         }
1716         Record.push_back(VE.getValueID(C->getOperand(0)));
1717         Record.push_back(VE.getValueID(C->getOperand(1)));
1718         Record.push_back(VE.getValueID(C->getOperand(2)));
1719         break;
1720       case Instruction::ICmp:
1721       case Instruction::FCmp:
1722         Code = bitc::CST_CODE_CE_CMP;
1723         Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1724         Record.push_back(VE.getValueID(C->getOperand(0)));
1725         Record.push_back(VE.getValueID(C->getOperand(1)));
1726         Record.push_back(CE->getPredicate());
1727         break;
1728       }
1729     } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1730       Code = bitc::CST_CODE_BLOCKADDRESS;
1731       Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1732       Record.push_back(VE.getValueID(BA->getFunction()));
1733       Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1734     } else {
1735 #ifndef NDEBUG
1736       C->dump();
1737 #endif
1738       llvm_unreachable("Unknown constant!");
1739     }
1740     Stream.EmitRecord(Code, Record, AbbrevToUse);
1741     Record.clear();
1742   }
1743
1744   Stream.ExitBlock();
1745 }
1746
1747 static void WriteModuleConstants(const ValueEnumerator &VE,
1748                                  BitstreamWriter &Stream) {
1749   const ValueEnumerator::ValueList &Vals = VE.getValues();
1750
1751   // Find the first constant to emit, which is the first non-globalvalue value.
1752   // We know globalvalues have been emitted by WriteModuleInfo.
1753   for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1754     if (!isa<GlobalValue>(Vals[i].first)) {
1755       WriteConstants(i, Vals.size(), VE, Stream, true);
1756       return;
1757     }
1758   }
1759 }
1760
1761 /// PushValueAndType - The file has to encode both the value and type id for
1762 /// many values, because we need to know what type to create for forward
1763 /// references.  However, most operands are not forward references, so this type
1764 /// field is not needed.
1765 ///
1766 /// This function adds V's value ID to Vals.  If the value ID is higher than the
1767 /// instruction ID, then it is a forward reference, and it also includes the
1768 /// type ID.  The value ID that is written is encoded relative to the InstID.
1769 static bool PushValueAndType(const Value *V, unsigned InstID,
1770                              SmallVectorImpl<unsigned> &Vals,
1771                              ValueEnumerator &VE) {
1772   unsigned ValID = VE.getValueID(V);
1773   // Make encoding relative to the InstID.
1774   Vals.push_back(InstID - ValID);
1775   if (ValID >= InstID) {
1776     Vals.push_back(VE.getTypeID(V->getType()));
1777     return true;
1778   }
1779   return false;
1780 }
1781
1782 static void WriteOperandBundles(BitstreamWriter &Stream, ImmutableCallSite CS,
1783                                 unsigned InstID, ValueEnumerator &VE) {
1784   SmallVector<unsigned, 64> Record;
1785   LLVMContext &C = CS.getInstruction()->getContext();
1786
1787   for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
1788     const auto &Bundle = CS.getOperandBundleAt(i);
1789     Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
1790
1791     for (auto &Input : Bundle.Inputs)
1792       PushValueAndType(Input, InstID, Record, VE);
1793
1794     Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
1795     Record.clear();
1796   }
1797 }
1798
1799 /// pushValue - Like PushValueAndType, but where the type of the value is
1800 /// omitted (perhaps it was already encoded in an earlier operand).
1801 static void pushValue(const Value *V, unsigned InstID,
1802                       SmallVectorImpl<unsigned> &Vals,
1803                       ValueEnumerator &VE) {
1804   unsigned ValID = VE.getValueID(V);
1805   Vals.push_back(InstID - ValID);
1806 }
1807
1808 static void pushValueSigned(const Value *V, unsigned InstID,
1809                             SmallVectorImpl<uint64_t> &Vals,
1810                             ValueEnumerator &VE) {
1811   unsigned ValID = VE.getValueID(V);
1812   int64_t diff = ((int32_t)InstID - (int32_t)ValID);
1813   emitSignedInt64(Vals, diff);
1814 }
1815
1816 /// WriteInstruction - Emit an instruction to the specified stream.
1817 static void WriteInstruction(const Instruction &I, unsigned InstID,
1818                              ValueEnumerator &VE, BitstreamWriter &Stream,
1819                              SmallVectorImpl<unsigned> &Vals) {
1820   unsigned Code = 0;
1821   unsigned AbbrevToUse = 0;
1822   VE.setInstructionID(&I);
1823   switch (I.getOpcode()) {
1824   default:
1825     if (Instruction::isCast(I.getOpcode())) {
1826       Code = bitc::FUNC_CODE_INST_CAST;
1827       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1828         AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1829       Vals.push_back(VE.getTypeID(I.getType()));
1830       Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1831     } else {
1832       assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1833       Code = bitc::FUNC_CODE_INST_BINOP;
1834       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1835         AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1836       pushValue(I.getOperand(1), InstID, Vals, VE);
1837       Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1838       uint64_t Flags = GetOptimizationFlags(&I);
1839       if (Flags != 0) {
1840         if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1841           AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1842         Vals.push_back(Flags);
1843       }
1844     }
1845     break;
1846
1847   case Instruction::GetElementPtr: {
1848     Code = bitc::FUNC_CODE_INST_GEP;
1849     AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
1850     auto &GEPInst = cast<GetElementPtrInst>(I);
1851     Vals.push_back(GEPInst.isInBounds());
1852     Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
1853     for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1854       PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1855     break;
1856   }
1857   case Instruction::ExtractValue: {
1858     Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
1859     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1860     const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1861     Vals.append(EVI->idx_begin(), EVI->idx_end());
1862     break;
1863   }
1864   case Instruction::InsertValue: {
1865     Code = bitc::FUNC_CODE_INST_INSERTVAL;
1866     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1867     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1868     const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1869     Vals.append(IVI->idx_begin(), IVI->idx_end());
1870     break;
1871   }
1872   case Instruction::Select:
1873     Code = bitc::FUNC_CODE_INST_VSELECT;
1874     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1875     pushValue(I.getOperand(2), InstID, Vals, VE);
1876     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1877     break;
1878   case Instruction::ExtractElement:
1879     Code = bitc::FUNC_CODE_INST_EXTRACTELT;
1880     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1881     PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1882     break;
1883   case Instruction::InsertElement:
1884     Code = bitc::FUNC_CODE_INST_INSERTELT;
1885     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1886     pushValue(I.getOperand(1), InstID, Vals, VE);
1887     PushValueAndType(I.getOperand(2), InstID, Vals, VE);
1888     break;
1889   case Instruction::ShuffleVector:
1890     Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
1891     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1892     pushValue(I.getOperand(1), InstID, Vals, VE);
1893     pushValue(I.getOperand(2), InstID, Vals, VE);
1894     break;
1895   case Instruction::ICmp:
1896   case Instruction::FCmp: {
1897     // compare returning Int1Ty or vector of Int1Ty
1898     Code = bitc::FUNC_CODE_INST_CMP2;
1899     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1900     pushValue(I.getOperand(1), InstID, Vals, VE);
1901     Vals.push_back(cast<CmpInst>(I).getPredicate());
1902     uint64_t Flags = GetOptimizationFlags(&I);
1903     if (Flags != 0)
1904       Vals.push_back(Flags);
1905     break;
1906   }
1907
1908   case Instruction::Ret:
1909     {
1910       Code = bitc::FUNC_CODE_INST_RET;
1911       unsigned NumOperands = I.getNumOperands();
1912       if (NumOperands == 0)
1913         AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1914       else if (NumOperands == 1) {
1915         if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1916           AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1917       } else {
1918         for (unsigned i = 0, e = NumOperands; i != e; ++i)
1919           PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1920       }
1921     }
1922     break;
1923   case Instruction::Br:
1924     {
1925       Code = bitc::FUNC_CODE_INST_BR;
1926       const BranchInst &II = cast<BranchInst>(I);
1927       Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1928       if (II.isConditional()) {
1929         Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1930         pushValue(II.getCondition(), InstID, Vals, VE);
1931       }
1932     }
1933     break;
1934   case Instruction::Switch:
1935     {
1936       Code = bitc::FUNC_CODE_INST_SWITCH;
1937       const SwitchInst &SI = cast<SwitchInst>(I);
1938       Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
1939       pushValue(SI.getCondition(), InstID, Vals, VE);
1940       Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1941       for (SwitchInst::ConstCaseIt Case : SI.cases()) {
1942         Vals.push_back(VE.getValueID(Case.getCaseValue()));
1943         Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
1944       }
1945     }
1946     break;
1947   case Instruction::IndirectBr:
1948     Code = bitc::FUNC_CODE_INST_INDIRECTBR;
1949     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1950     // Encode the address operand as relative, but not the basic blocks.
1951     pushValue(I.getOperand(0), InstID, Vals, VE);
1952     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1953       Vals.push_back(VE.getValueID(I.getOperand(i)));
1954     break;
1955
1956   case Instruction::Invoke: {
1957     const InvokeInst *II = cast<InvokeInst>(&I);
1958     const Value *Callee = II->getCalledValue();
1959     FunctionType *FTy = II->getFunctionType();
1960
1961     if (II->hasOperandBundles())
1962       WriteOperandBundles(Stream, II, InstID, VE);
1963
1964     Code = bitc::FUNC_CODE_INST_INVOKE;
1965
1966     Vals.push_back(VE.getAttributeID(II->getAttributes()));
1967     Vals.push_back(II->getCallingConv() | 1 << 13);
1968     Vals.push_back(VE.getValueID(II->getNormalDest()));
1969     Vals.push_back(VE.getValueID(II->getUnwindDest()));
1970     Vals.push_back(VE.getTypeID(FTy));
1971     PushValueAndType(Callee, InstID, Vals, VE);
1972
1973     // Emit value #'s for the fixed parameters.
1974     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1975       pushValue(I.getOperand(i), InstID, Vals, VE);  // fixed param.
1976
1977     // Emit type/value pairs for varargs params.
1978     if (FTy->isVarArg()) {
1979       for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1980            i != e; ++i)
1981         PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1982     }
1983     break;
1984   }
1985   case Instruction::Resume:
1986     Code = bitc::FUNC_CODE_INST_RESUME;
1987     PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1988     break;
1989   case Instruction::CleanupRet: {
1990     Code = bitc::FUNC_CODE_INST_CLEANUPRET;
1991     const auto &CRI = cast<CleanupReturnInst>(I);
1992     pushValue(CRI.getCleanupPad(), InstID, Vals, VE);
1993     if (CRI.hasUnwindDest())
1994       Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
1995     break;
1996   }
1997   case Instruction::CatchRet: {
1998     Code = bitc::FUNC_CODE_INST_CATCHRET;
1999     const auto &CRI = cast<CatchReturnInst>(I);
2000     pushValue(CRI.getCatchPad(), InstID, Vals, VE);
2001     Vals.push_back(VE.getValueID(CRI.getSuccessor()));
2002     break;
2003   }
2004   case Instruction::CleanupPad:
2005   case Instruction::CatchPad: {
2006     const auto &FuncletPad = cast<FuncletPadInst>(I);
2007     Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD
2008                                          : bitc::FUNC_CODE_INST_CLEANUPPAD;
2009     pushValue(FuncletPad.getParentPad(), InstID, Vals, VE);
2010
2011     unsigned NumArgOperands = FuncletPad.getNumArgOperands();
2012     Vals.push_back(NumArgOperands);
2013     for (unsigned Op = 0; Op != NumArgOperands; ++Op)
2014       PushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals, VE);
2015     break;
2016   }
2017   case Instruction::CatchSwitch: {
2018     Code = bitc::FUNC_CODE_INST_CATCHSWITCH;
2019     const auto &CatchSwitch = cast<CatchSwitchInst>(I);
2020
2021     pushValue(CatchSwitch.getParentPad(), InstID, Vals, VE);
2022
2023     unsigned NumHandlers = CatchSwitch.getNumHandlers();
2024     Vals.push_back(NumHandlers);
2025     for (const BasicBlock *CatchPadBB : CatchSwitch.handlers())
2026       Vals.push_back(VE.getValueID(CatchPadBB));
2027
2028     if (CatchSwitch.hasUnwindDest())
2029       Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
2030     break;
2031   }
2032   case Instruction::Unreachable:
2033     Code = bitc::FUNC_CODE_INST_UNREACHABLE;
2034     AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
2035     break;
2036
2037   case Instruction::PHI: {
2038     const PHINode &PN = cast<PHINode>(I);
2039     Code = bitc::FUNC_CODE_INST_PHI;
2040     // With the newer instruction encoding, forward references could give
2041     // negative valued IDs.  This is most common for PHIs, so we use
2042     // signed VBRs.
2043     SmallVector<uint64_t, 128> Vals64;
2044     Vals64.push_back(VE.getTypeID(PN.getType()));
2045     for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2046       pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
2047       Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2048     }
2049     // Emit a Vals64 vector and exit.
2050     Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2051     Vals64.clear();
2052     return;
2053   }
2054
2055   case Instruction::LandingPad: {
2056     const LandingPadInst &LP = cast<LandingPadInst>(I);
2057     Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2058     Vals.push_back(VE.getTypeID(LP.getType()));
2059     Vals.push_back(LP.isCleanup());
2060     Vals.push_back(LP.getNumClauses());
2061     for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2062       if (LP.isCatch(I))
2063         Vals.push_back(LandingPadInst::Catch);
2064       else
2065         Vals.push_back(LandingPadInst::Filter);
2066       PushValueAndType(LP.getClause(I), InstID, Vals, VE);
2067     }
2068     break;
2069   }
2070
2071   case Instruction::Alloca: {
2072     Code = bitc::FUNC_CODE_INST_ALLOCA;
2073     const AllocaInst &AI = cast<AllocaInst>(I);
2074     Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
2075     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
2076     Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2077     unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2078     assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2079            "not enough bits for maximum alignment");
2080     assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2081     AlignRecord |= AI.isUsedWithInAlloca() << 5;
2082     AlignRecord |= 1 << 6;
2083     // Reserve bit 7 for SwiftError flag.
2084     // AlignRecord |= AI.isSwiftError() << 7;
2085     Vals.push_back(AlignRecord);
2086     break;
2087   }
2088
2089   case Instruction::Load:
2090     if (cast<LoadInst>(I).isAtomic()) {
2091       Code = bitc::FUNC_CODE_INST_LOADATOMIC;
2092       PushValueAndType(I.getOperand(0), InstID, Vals, VE);
2093     } else {
2094       Code = bitc::FUNC_CODE_INST_LOAD;
2095       if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))  // ptr
2096         AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2097     }
2098     Vals.push_back(VE.getTypeID(I.getType()));
2099     Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2100     Vals.push_back(cast<LoadInst>(I).isVolatile());
2101     if (cast<LoadInst>(I).isAtomic()) {
2102       Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2103       Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
2104     }
2105     break;
2106   case Instruction::Store:
2107     if (cast<StoreInst>(I).isAtomic())
2108       Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2109     else
2110       Code = bitc::FUNC_CODE_INST_STORE;
2111     PushValueAndType(I.getOperand(1), InstID, Vals, VE);  // ptrty + ptr
2112     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // valty + val
2113     Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2114     Vals.push_back(cast<StoreInst>(I).isVolatile());
2115     if (cast<StoreInst>(I).isAtomic()) {
2116       Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2117       Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
2118     }
2119     break;
2120   case Instruction::AtomicCmpXchg:
2121     Code = bitc::FUNC_CODE_INST_CMPXCHG;
2122     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
2123     PushValueAndType(I.getOperand(1), InstID, Vals, VE);         // cmp.
2124     pushValue(I.getOperand(2), InstID, Vals, VE);         // newval.
2125     Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2126     Vals.push_back(GetEncodedOrdering(
2127                      cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2128     Vals.push_back(GetEncodedSynchScope(
2129                      cast<AtomicCmpXchgInst>(I).getSynchScope()));
2130     Vals.push_back(GetEncodedOrdering(
2131                      cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2132     Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2133     break;
2134   case Instruction::AtomicRMW:
2135     Code = bitc::FUNC_CODE_INST_ATOMICRMW;
2136     PushValueAndType(I.getOperand(0), InstID, Vals, VE);  // ptrty + ptr
2137     pushValue(I.getOperand(1), InstID, Vals, VE);         // val.
2138     Vals.push_back(GetEncodedRMWOperation(
2139                      cast<AtomicRMWInst>(I).getOperation()));
2140     Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2141     Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2142     Vals.push_back(GetEncodedSynchScope(
2143                      cast<AtomicRMWInst>(I).getSynchScope()));
2144     break;
2145   case Instruction::Fence:
2146     Code = bitc::FUNC_CODE_INST_FENCE;
2147     Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2148     Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
2149     break;
2150   case Instruction::Call: {
2151     const CallInst &CI = cast<CallInst>(I);
2152     FunctionType *FTy = CI.getFunctionType();
2153
2154     if (CI.hasOperandBundles())
2155       WriteOperandBundles(Stream, &CI, InstID, VE);
2156
2157     Code = bitc::FUNC_CODE_INST_CALL;
2158
2159     Vals.push_back(VE.getAttributeID(CI.getAttributes()));
2160
2161     unsigned Flags = GetOptimizationFlags(&I);
2162     Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2163                    unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2164                    unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2165                    1 << bitc::CALL_EXPLICIT_TYPE |
2166                    unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL |
2167                    unsigned(Flags != 0) << bitc::CALL_FMF);
2168     if (Flags != 0)
2169       Vals.push_back(Flags);
2170
2171     Vals.push_back(VE.getTypeID(FTy));
2172     PushValueAndType(CI.getCalledValue(), InstID, Vals, VE);  // Callee
2173
2174     // Emit value #'s for the fixed parameters.
2175     for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2176       // Check for labels (can happen with asm labels).
2177       if (FTy->getParamType(i)->isLabelTy())
2178         Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2179       else
2180         pushValue(CI.getArgOperand(i), InstID, Vals, VE);  // fixed param.
2181     }
2182
2183     // Emit type/value pairs for varargs params.
2184     if (FTy->isVarArg()) {
2185       for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
2186            i != e; ++i)
2187         PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE);  // varargs
2188     }
2189     break;
2190   }
2191   case Instruction::VAArg:
2192     Code = bitc::FUNC_CODE_INST_VAARG;
2193     Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));   // valistty
2194     pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
2195     Vals.push_back(VE.getTypeID(I.getType())); // restype.
2196     break;
2197   }
2198
2199   Stream.EmitRecord(Code, Vals, AbbrevToUse);
2200   Vals.clear();
2201 }
2202
2203 enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
2204
2205 /// Determine the encoding to use for the given string name and length.
2206 static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
2207   bool isChar6 = true;
2208   for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
2209     if (isChar6)
2210       isChar6 = BitCodeAbbrevOp::isChar6(*C);
2211     if ((unsigned char)*C & 128)
2212       // don't bother scanning the rest.
2213       return SE_Fixed8;
2214   }
2215   if (isChar6)
2216     return SE_Char6;
2217   else
2218     return SE_Fixed7;
2219 }
2220
2221 /// Emit names for globals/functions etc. The VSTOffsetPlaceholder,
2222 /// BitcodeStartBit and FunctionIndex are only passed for the module-level
2223 /// VST, where we are including a function bitcode index and need to
2224 /// backpatch the VST forward declaration record.
2225 static void WriteValueSymbolTable(
2226     const ValueSymbolTable &VST, const ValueEnumerator &VE,
2227     BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0,
2228     uint64_t BitcodeStartBit = 0,
2229     DenseMap<const Function *, std::unique_ptr<FunctionInfo>> *FunctionIndex =
2230         nullptr) {
2231   if (VST.empty()) {
2232     // WriteValueSymbolTableForwardDecl should have returned early as
2233     // well. Ensure this handling remains in sync by asserting that
2234     // the placeholder offset is not set.
2235     assert(VSTOffsetPlaceholder == 0);
2236     return;
2237   }
2238
2239   if (VSTOffsetPlaceholder > 0) {
2240     // Get the offset of the VST we are writing, and backpatch it into
2241     // the VST forward declaration record.
2242     uint64_t VSTOffset = Stream.GetCurrentBitNo();
2243     // The BitcodeStartBit was the stream offset of the actual bitcode
2244     // (e.g. excluding any initial darwin header).
2245     VSTOffset -= BitcodeStartBit;
2246     assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2247     Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
2248   }
2249
2250   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2251
2252   // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2253   // records, which are not used in the per-function VSTs.
2254   unsigned FnEntry8BitAbbrev;
2255   unsigned FnEntry7BitAbbrev;
2256   unsigned FnEntry6BitAbbrev;
2257   if (VSTOffsetPlaceholder > 0) {
2258     // 8-bit fixed-width VST_FNENTRY function strings.
2259     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2260     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2261     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2262     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2263     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2264     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2265     FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
2266
2267     // 7-bit fixed width VST_FNENTRY function strings.
2268     Abbv = new BitCodeAbbrev();
2269     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2270     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2271     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2272     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2273     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2274     FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
2275
2276     // 6-bit char6 VST_FNENTRY function strings.
2277     Abbv = new BitCodeAbbrev();
2278     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2279     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2280     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2281     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2282     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2283     FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
2284   }
2285
2286   // FIXME: Set up the abbrev, we know how many values there are!
2287   // FIXME: We know if the type names can use 7-bit ascii.
2288   SmallVector<unsigned, 64> NameVals;
2289
2290   for (const ValueName &Name : VST) {
2291     // Figure out the encoding to use for the name.
2292     StringEncoding Bits =
2293         getStringEncoding(Name.getKeyData(), Name.getKeyLength());
2294
2295     unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2296     NameVals.push_back(VE.getValueID(Name.getValue()));
2297
2298     Function *F = dyn_cast<Function>(Name.getValue());
2299     if (!F) {
2300       // If value is an alias, need to get the aliased base object to
2301       // see if it is a function.
2302       auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2303       if (GA && GA->getBaseObject())
2304         F = dyn_cast<Function>(GA->getBaseObject());
2305     }
2306
2307     // VST_ENTRY:   [valueid, namechar x N]
2308     // VST_FNENTRY: [valueid, funcoffset, namechar x N]
2309     // VST_BBENTRY: [bbid, namechar x N]
2310     unsigned Code;
2311     if (isa<BasicBlock>(Name.getValue())) {
2312       Code = bitc::VST_CODE_BBENTRY;
2313       if (Bits == SE_Char6)
2314         AbbrevToUse = VST_BBENTRY_6_ABBREV;
2315     } else if (F && !F->isDeclaration()) {
2316       // Must be the module-level VST, where we pass in the Index and
2317       // have a VSTOffsetPlaceholder. The function-level VST should not
2318       // contain any Function symbols.
2319       assert(FunctionIndex);
2320       assert(VSTOffsetPlaceholder > 0);
2321
2322       // Save the word offset of the function (from the start of the
2323       // actual bitcode written to the stream).
2324       assert(FunctionIndex->count(F) == 1);
2325       uint64_t BitcodeIndex =
2326           (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit;
2327       assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2328       NameVals.push_back(BitcodeIndex / 32);
2329
2330       Code = bitc::VST_CODE_FNENTRY;
2331       AbbrevToUse = FnEntry8BitAbbrev;
2332       if (Bits == SE_Char6)
2333         AbbrevToUse = FnEntry6BitAbbrev;
2334       else if (Bits == SE_Fixed7)
2335         AbbrevToUse = FnEntry7BitAbbrev;
2336     } else {
2337       Code = bitc::VST_CODE_ENTRY;
2338       if (Bits == SE_Char6)
2339         AbbrevToUse = VST_ENTRY_6_ABBREV;
2340       else if (Bits == SE_Fixed7)
2341         AbbrevToUse = VST_ENTRY_7_ABBREV;
2342     }
2343
2344     for (const auto P : Name.getKey())
2345       NameVals.push_back((unsigned char)P);
2346
2347     // Emit the finished record.
2348     Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2349     NameVals.clear();
2350   }
2351   Stream.ExitBlock();
2352 }
2353
2354 /// Emit function names and summary offsets for the combined index
2355 /// used by ThinLTO.
2356 static void WriteCombinedValueSymbolTable(const FunctionInfoIndex &Index,
2357                                           BitstreamWriter &Stream) {
2358   Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2359
2360   // 8-bit fixed-width VST_COMBINED_FNENTRY function strings.
2361   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2362   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2363   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2364   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2365   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2366   unsigned FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
2367
2368   // 7-bit fixed width VST_COMBINED_FNENTRY function strings.
2369   Abbv = new BitCodeAbbrev();
2370   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2371   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2372   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2373   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2374   unsigned FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
2375
2376   // 6-bit char6 VST_COMBINED_FNENTRY function strings.
2377   Abbv = new BitCodeAbbrev();
2378   Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2379   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2380   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2381   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2382   unsigned FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
2383
2384   // FIXME: We know if the type names can use 7-bit ascii.
2385   SmallVector<unsigned, 64> NameVals;
2386
2387   for (const auto &FII : Index) {
2388     for (const auto &FI : FII.getValue()) {
2389       NameVals.push_back(FI->bitcodeIndex());
2390
2391       StringRef FuncName = FII.first();
2392
2393       // Figure out the encoding to use for the name.
2394       StringEncoding Bits = getStringEncoding(FuncName.data(), FuncName.size());
2395
2396       // VST_COMBINED_FNENTRY: [funcsumoffset, namechar x N]
2397       unsigned AbbrevToUse = FnEntry8BitAbbrev;
2398       if (Bits == SE_Char6)
2399         AbbrevToUse = FnEntry6BitAbbrev;
2400       else if (Bits == SE_Fixed7)
2401         AbbrevToUse = FnEntry7BitAbbrev;
2402
2403       for (const auto P : FuncName)
2404         NameVals.push_back((unsigned char)P);
2405
2406       // Emit the finished record.
2407       Stream.EmitRecord(bitc::VST_CODE_COMBINED_FNENTRY, NameVals, AbbrevToUse);
2408       NameVals.clear();
2409     }
2410   }
2411   Stream.ExitBlock();
2412 }
2413
2414 static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order,
2415                          BitstreamWriter &Stream) {
2416   assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2417   unsigned Code;
2418   if (isa<BasicBlock>(Order.V))
2419     Code = bitc::USELIST_CODE_BB;
2420   else
2421     Code = bitc::USELIST_CODE_DEFAULT;
2422
2423   SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
2424   Record.push_back(VE.getValueID(Order.V));
2425   Stream.EmitRecord(Code, Record);
2426 }
2427
2428 static void WriteUseListBlock(const Function *F, ValueEnumerator &VE,
2429                               BitstreamWriter &Stream) {
2430   assert(VE.shouldPreserveUseListOrder() &&
2431          "Expected to be preserving use-list order");
2432
2433   auto hasMore = [&]() {
2434     return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2435   };
2436   if (!hasMore())
2437     // Nothing to do.
2438     return;
2439
2440   Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
2441   while (hasMore()) {
2442     WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream);
2443     VE.UseListOrders.pop_back();
2444   }
2445   Stream.ExitBlock();
2446 }
2447
2448 /// \brief Save information for the given function into the function index.
2449 ///
2450 /// At a minimum this saves the bitcode index of the function record that
2451 /// was just written. However, if we are emitting function summary information,
2452 /// for example for ThinLTO, then a \a FunctionSummary object is created
2453 /// to hold the provided summary information.
2454 static void SaveFunctionInfo(
2455     const Function &F,
2456     DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2457     unsigned NumInsts, uint64_t BitcodeIndex, bool EmitFunctionSummary) {
2458   std::unique_ptr<FunctionSummary> FuncSummary;
2459   if (EmitFunctionSummary) {
2460     FuncSummary = llvm::make_unique<FunctionSummary>(NumInsts);
2461     FuncSummary->setLocalFunction(F.hasLocalLinkage());
2462   }
2463   FunctionIndex[&F] =
2464       llvm::make_unique<FunctionInfo>(BitcodeIndex, std::move(FuncSummary));
2465 }
2466
2467 /// Emit a function body to the module stream.
2468 static void WriteFunction(
2469     const Function &F, ValueEnumerator &VE, BitstreamWriter &Stream,
2470     DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2471     bool EmitFunctionSummary) {
2472   // Save the bitcode index of the start of this function block for recording
2473   // in the VST.
2474   uint64_t BitcodeIndex = Stream.GetCurrentBitNo();
2475
2476   Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2477   VE.incorporateFunction(F);
2478
2479   SmallVector<unsigned, 64> Vals;
2480
2481   // Emit the number of basic blocks, so the reader can create them ahead of
2482   // time.
2483   Vals.push_back(VE.getBasicBlocks().size());
2484   Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2485   Vals.clear();
2486
2487   // If there are function-local constants, emit them now.
2488   unsigned CstStart, CstEnd;
2489   VE.getFunctionConstantRange(CstStart, CstEnd);
2490   WriteConstants(CstStart, CstEnd, VE, Stream, false);
2491
2492   // If there is function-local metadata, emit it now.
2493   WriteFunctionLocalMetadata(F, VE, Stream);
2494
2495   // Keep a running idea of what the instruction ID is.
2496   unsigned InstID = CstEnd;
2497
2498   bool NeedsMetadataAttachment = F.hasMetadata();
2499
2500   DILocation *LastDL = nullptr;
2501   unsigned NumInsts = 0;
2502
2503   // Finally, emit all the instructions, in order.
2504   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2505     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2506          I != E; ++I) {
2507       WriteInstruction(*I, InstID, VE, Stream, Vals);
2508
2509       if (!isa<DbgInfoIntrinsic>(I))
2510         ++NumInsts;
2511
2512       if (!I->getType()->isVoidTy())
2513         ++InstID;
2514
2515       // If the instruction has metadata, write a metadata attachment later.
2516       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
2517
2518       // If the instruction has a debug location, emit it.
2519       DILocation *DL = I->getDebugLoc();
2520       if (!DL)
2521         continue;
2522
2523       if (DL == LastDL) {
2524         // Just repeat the same debug loc as last time.
2525         Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2526         continue;
2527       }
2528
2529       Vals.push_back(DL->getLine());
2530       Vals.push_back(DL->getColumn());
2531       Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2532       Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2533       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2534       Vals.clear();
2535
2536       LastDL = DL;
2537     }
2538
2539   // Emit names for all the instructions etc.
2540   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
2541
2542   if (NeedsMetadataAttachment)
2543     WriteMetadataAttachment(F, VE, Stream);
2544   if (VE.shouldPreserveUseListOrder())
2545     WriteUseListBlock(&F, VE, Stream);
2546   VE.purgeFunction();
2547   Stream.ExitBlock();
2548
2549   SaveFunctionInfo(F, FunctionIndex, NumInsts, BitcodeIndex,
2550                    EmitFunctionSummary);
2551 }
2552
2553 // Emit blockinfo, which defines the standard abbreviations etc.
2554 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
2555   // We only want to emit block info records for blocks that have multiple
2556   // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2557   // Other blocks can define their abbrevs inline.
2558   Stream.EnterBlockInfoBlock(2);
2559
2560   { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2561     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2562     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2563     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2564     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2565     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2566     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2567                                    Abbv) != VST_ENTRY_8_ABBREV)
2568       llvm_unreachable("Unexpected abbrev ordering!");
2569   }
2570
2571   { // 7-bit fixed width VST_ENTRY strings.
2572     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2573     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2574     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2575     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2576     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2577     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2578                                    Abbv) != VST_ENTRY_7_ABBREV)
2579       llvm_unreachable("Unexpected abbrev ordering!");
2580   }
2581   { // 6-bit char6 VST_ENTRY strings.
2582     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2583     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2584     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2585     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2586     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2587     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2588                                    Abbv) != VST_ENTRY_6_ABBREV)
2589       llvm_unreachable("Unexpected abbrev ordering!");
2590   }
2591   { // 6-bit char6 VST_BBENTRY strings.
2592     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2593     Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2594     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2595     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2596     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2597     if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2598                                    Abbv) != VST_BBENTRY_6_ABBREV)
2599       llvm_unreachable("Unexpected abbrev ordering!");
2600   }
2601
2602
2603
2604   { // SETTYPE abbrev for CONSTANTS_BLOCK.
2605     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2606     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2607     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2608                               VE.computeBitsRequiredForTypeIndicies()));
2609     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2610                                    Abbv) != CONSTANTS_SETTYPE_ABBREV)
2611       llvm_unreachable("Unexpected abbrev ordering!");
2612   }
2613
2614   { // INTEGER abbrev for CONSTANTS_BLOCK.
2615     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2616     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2617     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2618     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2619                                    Abbv) != CONSTANTS_INTEGER_ABBREV)
2620       llvm_unreachable("Unexpected abbrev ordering!");
2621   }
2622
2623   { // CE_CAST abbrev for CONSTANTS_BLOCK.
2624     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2625     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2626     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // cast opc
2627     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // typeid
2628                               VE.computeBitsRequiredForTypeIndicies()));
2629     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));    // value id
2630
2631     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2632                                    Abbv) != CONSTANTS_CE_CAST_Abbrev)
2633       llvm_unreachable("Unexpected abbrev ordering!");
2634   }
2635   { // NULL abbrev for CONSTANTS_BLOCK.
2636     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2637     Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2638     if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2639                                    Abbv) != CONSTANTS_NULL_Abbrev)
2640       llvm_unreachable("Unexpected abbrev ordering!");
2641   }
2642
2643   // FIXME: This should only use space for first class types!
2644
2645   { // INST_LOAD abbrev for FUNCTION_BLOCK.
2646     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2647     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2648     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2649     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,    // dest ty
2650                               VE.computeBitsRequiredForTypeIndicies()));
2651     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2652     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2653     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2654                                    Abbv) != FUNCTION_INST_LOAD_ABBREV)
2655       llvm_unreachable("Unexpected abbrev ordering!");
2656   }
2657   { // INST_BINOP abbrev for FUNCTION_BLOCK.
2658     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2659     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2660     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2661     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2662     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2663     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2664                                    Abbv) != FUNCTION_INST_BINOP_ABBREV)
2665       llvm_unreachable("Unexpected abbrev ordering!");
2666   }
2667   { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2668     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2669     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2670     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2671     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2672     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2673     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2674     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2675                                    Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
2676       llvm_unreachable("Unexpected abbrev ordering!");
2677   }
2678   { // INST_CAST abbrev for FUNCTION_BLOCK.
2679     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2680     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2681     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // OpVal
2682     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,       // dest ty
2683                               VE.computeBitsRequiredForTypeIndicies()));
2684     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4));  // opc
2685     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2686                                    Abbv) != FUNCTION_INST_CAST_ABBREV)
2687       llvm_unreachable("Unexpected abbrev ordering!");
2688   }
2689
2690   { // INST_RET abbrev for FUNCTION_BLOCK.
2691     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2692     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2693     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2694                                    Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
2695       llvm_unreachable("Unexpected abbrev ordering!");
2696   }
2697   { // INST_RET abbrev for FUNCTION_BLOCK.
2698     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2699     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2700     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2701     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2702                                    Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
2703       llvm_unreachable("Unexpected abbrev ordering!");
2704   }
2705   { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2706     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2707     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2708     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2709                                    Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
2710       llvm_unreachable("Unexpected abbrev ordering!");
2711   }
2712   {
2713     BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2714     Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2715     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2716     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2717                               Log2_32_Ceil(VE.getTypes().size() + 1)));
2718     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2719     Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2720     if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
2721         FUNCTION_INST_GEP_ABBREV)
2722       llvm_unreachable("Unexpected abbrev ordering!");
2723   }
2724
2725   Stream.ExitBlock();
2726 }
2727
2728 /// Write the module path strings, currently only used when generating
2729 /// a combined index file.
2730 static void WriteModStrings(const FunctionInfoIndex &I,
2731                             BitstreamWriter &Stream) {
2732   Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
2733
2734   // TODO: See which abbrev sizes we actually need to emit
2735
2736   // 8-bit fixed-width MST_ENTRY strings.
2737   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2738   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2739   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2740   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2741   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2742   unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
2743
2744   // 7-bit fixed width MST_ENTRY strings.
2745   Abbv = new BitCodeAbbrev();
2746   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2747   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2748   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2749   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2750   unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
2751
2752   // 6-bit char6 MST_ENTRY strings.
2753   Abbv = new BitCodeAbbrev();
2754   Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2755   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2756   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2757   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2758   unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
2759
2760   SmallVector<unsigned, 64> NameVals;
2761   for (const StringMapEntry<uint64_t> &MPSE : I.modPathStringEntries()) {
2762     StringEncoding Bits =
2763         getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2764     unsigned AbbrevToUse = Abbrev8Bit;
2765     if (Bits == SE_Char6)
2766       AbbrevToUse = Abbrev6Bit;
2767     else if (Bits == SE_Fixed7)
2768       AbbrevToUse = Abbrev7Bit;
2769
2770     NameVals.push_back(MPSE.getValue());
2771
2772     for (const auto P : MPSE.getKey())
2773       NameVals.push_back((unsigned char)P);
2774
2775     // Emit the finished record.
2776     Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
2777     NameVals.clear();
2778   }
2779   Stream.ExitBlock();
2780 }
2781
2782 // Helper to emit a single function summary record.
2783 static void WritePerModuleFunctionSummaryRecord(
2784     SmallVector<unsigned, 64> &NameVals, FunctionSummary *FS, unsigned ValueID,
2785     unsigned FSAbbrev, BitstreamWriter &Stream) {
2786   assert(FS);
2787   NameVals.push_back(ValueID);
2788   NameVals.push_back(FS->isLocalFunction());
2789   NameVals.push_back(FS->instCount());
2790
2791   // Emit the finished record.
2792   Stream.EmitRecord(bitc::FS_CODE_PERMODULE_ENTRY, NameVals, FSAbbrev);
2793   NameVals.clear();
2794 }
2795
2796 /// Emit the per-module function summary section alongside the rest of
2797 /// the module's bitcode.
2798 static void WritePerModuleFunctionSummary(
2799     DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2800     const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
2801   Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2802
2803   // Abbrev for FS_CODE_PERMODULE_ENTRY.
2804   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2805   Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_PERMODULE_ENTRY));
2806   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // valueid
2807   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // islocal
2808   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));   // instcount
2809   unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2810
2811   SmallVector<unsigned, 64> NameVals;
2812   for (auto &I : FunctionIndex) {
2813     // Skip anonymous functions. We will emit a function summary for
2814     // any aliases below.
2815     if (!I.first->hasName())
2816       continue;
2817
2818     WritePerModuleFunctionSummaryRecord(
2819         NameVals, I.second->functionSummary(),
2820         VE.getValueID(M->getValueSymbolTable().lookup(I.first->getName())),
2821         FSAbbrev, Stream);
2822   }
2823
2824   for (const GlobalAlias &A : M->aliases()) {
2825     if (!A.getBaseObject())
2826       continue;
2827     const Function *F = dyn_cast<Function>(A.getBaseObject());
2828     if (!F || F->isDeclaration())
2829       continue;
2830
2831     assert(FunctionIndex.count(F) == 1);
2832     WritePerModuleFunctionSummaryRecord(
2833         NameVals, FunctionIndex[F]->functionSummary(),
2834         VE.getValueID(M->getValueSymbolTable().lookup(A.getName())), FSAbbrev,
2835         Stream);
2836   }
2837
2838   Stream.ExitBlock();
2839 }
2840
2841 /// Emit the combined function summary section into the combined index
2842 /// file.
2843 static void WriteCombinedFunctionSummary(const FunctionInfoIndex &I,
2844                                          BitstreamWriter &Stream) {
2845   Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2846
2847   // Abbrev for FS_CODE_COMBINED_ENTRY.
2848   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2849   Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_COMBINED_ENTRY));
2850   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
2851   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
2852   unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2853
2854   SmallVector<unsigned, 64> NameVals;
2855   for (const auto &FII : I) {
2856     for (auto &FI : FII.getValue()) {
2857       FunctionSummary *FS = FI->functionSummary();
2858       assert(FS);
2859
2860       NameVals.push_back(I.getModuleId(FS->modulePath()));
2861       NameVals.push_back(FS->instCount());
2862
2863       // Record the starting offset of this summary entry for use
2864       // in the VST entry. Add the current code size since the
2865       // reader will invoke readRecord after the abbrev id read.
2866       FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
2867
2868       // Emit the finished record.
2869       Stream.EmitRecord(bitc::FS_CODE_COMBINED_ENTRY, NameVals, FSAbbrev);
2870       NameVals.clear();
2871     }
2872   }
2873
2874   Stream.ExitBlock();
2875 }
2876
2877 // Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
2878 // current llvm version, and a record for the epoch number.
2879 static void WriteIdentificationBlock(const Module *M, BitstreamWriter &Stream) {
2880   Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
2881
2882   // Write the "user readable" string identifying the bitcode producer
2883   BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2884   Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
2885   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2886   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2887   auto StringAbbrev = Stream.EmitAbbrev(Abbv);
2888   WriteStringRecord(bitc::IDENTIFICATION_CODE_STRING,
2889                     "LLVM" LLVM_VERSION_STRING, StringAbbrev, Stream);
2890
2891   // Write the epoch version
2892   Abbv = new BitCodeAbbrev();
2893   Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
2894   Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2895   auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
2896   SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
2897   Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
2898   Stream.ExitBlock();
2899 }
2900
2901 /// WriteModule - Emit the specified module to the bitstream.
2902 static void WriteModule(const Module *M, BitstreamWriter &Stream,
2903                         bool ShouldPreserveUseListOrder,
2904                         uint64_t BitcodeStartBit, bool EmitFunctionSummary) {
2905   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2906
2907   SmallVector<unsigned, 1> Vals;
2908   unsigned CurVersion = 1;
2909   Vals.push_back(CurVersion);
2910   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
2911
2912   // Analyze the module, enumerating globals, functions, etc.
2913   ValueEnumerator VE(*M, ShouldPreserveUseListOrder);
2914
2915   // Emit blockinfo, which defines the standard abbreviations etc.
2916   WriteBlockInfo(VE, Stream);
2917
2918   // Emit information about attribute groups.
2919   WriteAttributeGroupTable(VE, Stream);
2920
2921   // Emit information about parameter attributes.
2922   WriteAttributeTable(VE, Stream);
2923
2924   // Emit information describing all of the types in the module.
2925   WriteTypeTable(VE, Stream);
2926
2927   writeComdats(VE, Stream);
2928
2929   // Emit top-level description of module, including target triple, inline asm,
2930   // descriptors for global variables, and function prototype info.
2931   uint64_t VSTOffsetPlaceholder = WriteModuleInfo(M, VE, Stream);
2932
2933   // Emit constants.
2934   WriteModuleConstants(VE, Stream);
2935
2936   // Emit metadata.
2937   WriteModuleMetadata(M, VE, Stream);
2938
2939   // Emit metadata.
2940   WriteModuleMetadataStore(M, Stream);
2941
2942   // Emit module-level use-lists.
2943   if (VE.shouldPreserveUseListOrder())
2944     WriteUseListBlock(nullptr, VE, Stream);
2945
2946   WriteOperandBundleTags(M, Stream);
2947
2948   // Emit function bodies.
2949   DenseMap<const Function *, std::unique_ptr<FunctionInfo>> FunctionIndex;
2950   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
2951     if (!F->isDeclaration())
2952       WriteFunction(*F, VE, Stream, FunctionIndex, EmitFunctionSummary);
2953
2954   // Need to write after the above call to WriteFunction which populates
2955   // the summary information in the index.
2956   if (EmitFunctionSummary)
2957     WritePerModuleFunctionSummary(FunctionIndex, M, VE, Stream);
2958
2959   WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
2960                         VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex);
2961
2962   Stream.ExitBlock();
2963 }
2964
2965 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
2966 /// header and trailer to make it compatible with the system archiver.  To do
2967 /// this we emit the following header, and then emit a trailer that pads the
2968 /// file out to be a multiple of 16 bytes.
2969 ///
2970 /// struct bc_header {
2971 ///   uint32_t Magic;         // 0x0B17C0DE
2972 ///   uint32_t Version;       // Version, currently always 0.
2973 ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
2974 ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
2975 ///   uint32_t CPUType;       // CPU specifier.
2976 ///   ... potentially more later ...
2977 /// };
2978 enum {
2979   DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
2980   DarwinBCHeaderSize = 5*4
2981 };
2982
2983 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
2984                                uint32_t &Position) {
2985   support::endian::write32le(&Buffer[Position], Value);
2986   Position += 4;
2987 }
2988
2989 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
2990                                          const Triple &TT) {
2991   unsigned CPUType = ~0U;
2992
2993   // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
2994   // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
2995   // number from /usr/include/mach/machine.h.  It is ok to reproduce the
2996   // specific constants here because they are implicitly part of the Darwin ABI.
2997   enum {
2998     DARWIN_CPU_ARCH_ABI64      = 0x01000000,
2999     DARWIN_CPU_TYPE_X86        = 7,
3000     DARWIN_CPU_TYPE_ARM        = 12,
3001     DARWIN_CPU_TYPE_POWERPC    = 18
3002   };
3003
3004   Triple::ArchType Arch = TT.getArch();
3005   if (Arch == Triple::x86_64)
3006     CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
3007   else if (Arch == Triple::x86)
3008     CPUType = DARWIN_CPU_TYPE_X86;
3009   else if (Arch == Triple::ppc)
3010     CPUType = DARWIN_CPU_TYPE_POWERPC;
3011   else if (Arch == Triple::ppc64)
3012     CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
3013   else if (Arch == Triple::arm || Arch == Triple::thumb)
3014     CPUType = DARWIN_CPU_TYPE_ARM;
3015
3016   // Traditional Bitcode starts after header.
3017   assert(Buffer.size() >= DarwinBCHeaderSize &&
3018          "Expected header size to be reserved");
3019   unsigned BCOffset = DarwinBCHeaderSize;
3020   unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
3021
3022   // Write the magic and version.
3023   unsigned Position = 0;
3024   WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
3025   WriteInt32ToBuffer(0          , Buffer, Position); // Version.
3026   WriteInt32ToBuffer(BCOffset   , Buffer, Position);
3027   WriteInt32ToBuffer(BCSize     , Buffer, Position);
3028   WriteInt32ToBuffer(CPUType    , Buffer, Position);
3029
3030   // If the file is not a multiple of 16 bytes, insert dummy padding.
3031   while (Buffer.size() & 15)
3032     Buffer.push_back(0);
3033 }
3034
3035 /// Helper to write the header common to all bitcode files.
3036 static void WriteBitcodeHeader(BitstreamWriter &Stream) {
3037   // Emit the file header.
3038   Stream.Emit((unsigned)'B', 8);
3039   Stream.Emit((unsigned)'C', 8);
3040   Stream.Emit(0x0, 4);
3041   Stream.Emit(0xC, 4);
3042   Stream.Emit(0xE, 4);
3043   Stream.Emit(0xD, 4);
3044 }
3045
3046 /// WriteBitcodeToFile - Write the specified module to the specified output
3047 /// stream.
3048 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
3049                               bool ShouldPreserveUseListOrder,
3050                               bool EmitFunctionSummary) {
3051   SmallVector<char, 0> Buffer;
3052   Buffer.reserve(256*1024);
3053
3054   // If this is darwin or another generic macho target, reserve space for the
3055   // header.
3056   Triple TT(M->getTargetTriple());
3057   if (TT.isOSDarwin())
3058     Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
3059
3060   // Emit the module into the buffer.
3061   {
3062     BitstreamWriter Stream(Buffer);
3063     // Save the start bit of the actual bitcode, in case there is space
3064     // saved at the start for the darwin header above. The reader stream
3065     // will start at the bitcode, and we need the offset of the VST
3066     // to line up.
3067     uint64_t BitcodeStartBit = Stream.GetCurrentBitNo();
3068
3069     // Emit the file header.
3070     WriteBitcodeHeader(Stream);
3071
3072     WriteIdentificationBlock(M, Stream);
3073
3074     // Emit the module.
3075     WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
3076                 EmitFunctionSummary);
3077   }
3078
3079   if (TT.isOSDarwin())
3080     EmitDarwinBCHeaderAndTrailer(Buffer, TT);
3081
3082   // Write the generated bitstream to "Out".
3083   Out.write((char*)&Buffer.front(), Buffer.size());
3084 }
3085
3086 // Write the specified function summary index to the given raw output stream,
3087 // where it will be written in a new bitcode block. This is used when
3088 // writing the combined index file for ThinLTO.
3089 void llvm::WriteFunctionSummaryToFile(const FunctionInfoIndex &Index,
3090                                       raw_ostream &Out) {
3091   SmallVector<char, 0> Buffer;
3092   Buffer.reserve(256 * 1024);
3093
3094   BitstreamWriter Stream(Buffer);
3095
3096   // Emit the bitcode header.
3097   WriteBitcodeHeader(Stream);
3098
3099   Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3100
3101   SmallVector<unsigned, 1> Vals;
3102   unsigned CurVersion = 1;
3103   Vals.push_back(CurVersion);
3104   Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
3105
3106   // Write the module paths in the combined index.
3107   WriteModStrings(Index, Stream);
3108
3109   // Write the function summary combined index records.
3110   WriteCombinedFunctionSummary(Index, Stream);
3111
3112   // Need a special VST writer for the combined index (we don't have a
3113   // real VST and real values when this is invoked).
3114   WriteCombinedValueSymbolTable(Index, Stream);
3115
3116   Stream.ExitBlock();
3117
3118   Out.write((char *)&Buffer.front(), Buffer.size());
3119 }