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