1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
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"
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.
44 // VALUE_SYMTAB_BLOCK abbrev id's.
45 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
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,
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,
67 static unsigned GetEncodedCastOpcode(unsigned 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;
86 static unsigned GetEncodedBinaryOpcode(unsigned 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;
110 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp 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;
127 static unsigned GetEncodedOrdering(AtomicOrdering 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;
137 llvm_unreachable("Invalid ordering");
140 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
141 switch (SynchScope) {
142 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
143 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
145 llvm_unreachable("Invalid synch scope");
148 static void WriteStringRecord(unsigned Code, StringRef Str,
149 unsigned AbbrevToUse, BitstreamWriter &Stream) {
150 SmallVector<unsigned, 64> Vals;
152 // Code: [strchar x N]
153 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
154 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
156 Vals.push_back(Str[i]);
159 // Emit the finished record.
160 Stream.EmitRecord(Code, Vals, AbbrevToUse);
163 static uint64_t getAttrKindEncoding(Attribute::AttrKind 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::InlineHint:
182 return bitc::ATTR_KIND_INLINE_HINT;
183 case Attribute::InReg:
184 return bitc::ATTR_KIND_IN_REG;
185 case Attribute::JumpTable:
186 return bitc::ATTR_KIND_JUMP_TABLE;
187 case Attribute::MinSize:
188 return bitc::ATTR_KIND_MIN_SIZE;
189 case Attribute::Naked:
190 return bitc::ATTR_KIND_NAKED;
191 case Attribute::Nest:
192 return bitc::ATTR_KIND_NEST;
193 case Attribute::NoAlias:
194 return bitc::ATTR_KIND_NO_ALIAS;
195 case Attribute::NoBuiltin:
196 return bitc::ATTR_KIND_NO_BUILTIN;
197 case Attribute::NoCapture:
198 return bitc::ATTR_KIND_NO_CAPTURE;
199 case Attribute::NoDuplicate:
200 return bitc::ATTR_KIND_NO_DUPLICATE;
201 case Attribute::NoImplicitFloat:
202 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
203 case Attribute::NoInline:
204 return bitc::ATTR_KIND_NO_INLINE;
205 case Attribute::NoRecurse:
206 return bitc::ATTR_KIND_NO_RECURSE;
207 case Attribute::NonLazyBind:
208 return bitc::ATTR_KIND_NON_LAZY_BIND;
209 case Attribute::NonNull:
210 return bitc::ATTR_KIND_NON_NULL;
211 case Attribute::Dereferenceable:
212 return bitc::ATTR_KIND_DEREFERENCEABLE;
213 case Attribute::DereferenceableOrNull:
214 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL;
215 case Attribute::NoRedZone:
216 return bitc::ATTR_KIND_NO_RED_ZONE;
217 case Attribute::NoReturn:
218 return bitc::ATTR_KIND_NO_RETURN;
219 case Attribute::NoUnwind:
220 return bitc::ATTR_KIND_NO_UNWIND;
221 case Attribute::OptimizeForSize:
222 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
223 case Attribute::OptimizeNone:
224 return bitc::ATTR_KIND_OPTIMIZE_NONE;
225 case Attribute::ReadNone:
226 return bitc::ATTR_KIND_READ_NONE;
227 case Attribute::ReadOnly:
228 return bitc::ATTR_KIND_READ_ONLY;
229 case Attribute::Returned:
230 return bitc::ATTR_KIND_RETURNED;
231 case Attribute::ReturnsTwice:
232 return bitc::ATTR_KIND_RETURNS_TWICE;
233 case Attribute::SExt:
234 return bitc::ATTR_KIND_S_EXT;
235 case Attribute::StackAlignment:
236 return bitc::ATTR_KIND_STACK_ALIGNMENT;
237 case Attribute::StackProtect:
238 return bitc::ATTR_KIND_STACK_PROTECT;
239 case Attribute::StackProtectReq:
240 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
241 case Attribute::StackProtectStrong:
242 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
243 case Attribute::SafeStack:
244 return bitc::ATTR_KIND_SAFESTACK;
245 case Attribute::StructRet:
246 return bitc::ATTR_KIND_STRUCT_RET;
247 case Attribute::SanitizeAddress:
248 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
249 case Attribute::SanitizeThread:
250 return bitc::ATTR_KIND_SANITIZE_THREAD;
251 case Attribute::SanitizeMemory:
252 return bitc::ATTR_KIND_SANITIZE_MEMORY;
253 case Attribute::UWTable:
254 return bitc::ATTR_KIND_UW_TABLE;
255 case Attribute::ZExt:
256 return bitc::ATTR_KIND_Z_EXT;
257 case Attribute::EndAttrKinds:
258 llvm_unreachable("Can not encode end-attribute kinds marker.");
259 case Attribute::None:
260 llvm_unreachable("Can not encode none-attribute.");
263 llvm_unreachable("Trying to encode unknown attribute");
266 static void WriteAttributeGroupTable(const ValueEnumerator &VE,
267 BitstreamWriter &Stream) {
268 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
269 if (AttrGrps.empty()) return;
271 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
273 SmallVector<uint64_t, 64> Record;
274 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
275 AttributeSet AS = AttrGrps[i];
276 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
277 AttributeSet A = AS.getSlotAttributes(i);
279 Record.push_back(VE.getAttributeGroupID(A));
280 Record.push_back(AS.getSlotIndex(i));
282 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
285 if (Attr.isEnumAttribute()) {
287 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
288 } else if (Attr.isIntAttribute()) {
290 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
291 Record.push_back(Attr.getValueAsInt());
293 StringRef Kind = Attr.getKindAsString();
294 StringRef Val = Attr.getValueAsString();
296 Record.push_back(Val.empty() ? 3 : 4);
297 Record.append(Kind.begin(), Kind.end());
300 Record.append(Val.begin(), Val.end());
306 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
314 static void WriteAttributeTable(const ValueEnumerator &VE,
315 BitstreamWriter &Stream) {
316 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
317 if (Attrs.empty()) return;
319 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
321 SmallVector<uint64_t, 64> Record;
322 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
323 const AttributeSet &A = Attrs[i];
324 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
325 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
327 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
334 /// WriteTypeTable - Write out the type table for a module.
335 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
336 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
338 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
339 SmallVector<uint64_t, 64> TypeVals;
341 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies();
343 // Abbrev for TYPE_CODE_POINTER.
344 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
345 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
347 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
348 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
350 // Abbrev for TYPE_CODE_FUNCTION.
351 Abbv = new BitCodeAbbrev();
352 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
354 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
355 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
357 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
359 // Abbrev for TYPE_CODE_STRUCT_ANON.
360 Abbv = new BitCodeAbbrev();
361 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
362 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
366 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
368 // Abbrev for TYPE_CODE_STRUCT_NAME.
369 Abbv = new BitCodeAbbrev();
370 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
373 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
375 // Abbrev for TYPE_CODE_STRUCT_NAMED.
376 Abbv = new BitCodeAbbrev();
377 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
378 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
379 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
380 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
382 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
384 // Abbrev for TYPE_CODE_ARRAY.
385 Abbv = new BitCodeAbbrev();
386 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
390 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
392 // Emit an entry count so the reader can reserve space.
393 TypeVals.push_back(TypeList.size());
394 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
397 // Loop over all of the types, emitting each in turn.
398 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
399 Type *T = TypeList[i];
403 switch (T->getTypeID()) {
404 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
405 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
406 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
407 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
408 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
409 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
410 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
411 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
412 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
413 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
414 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break;
415 case Type::IntegerTyID:
417 Code = bitc::TYPE_CODE_INTEGER;
418 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
420 case Type::PointerTyID: {
421 PointerType *PTy = cast<PointerType>(T);
422 // POINTER: [pointee type, address space]
423 Code = bitc::TYPE_CODE_POINTER;
424 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
425 unsigned AddressSpace = PTy->getAddressSpace();
426 TypeVals.push_back(AddressSpace);
427 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
430 case Type::FunctionTyID: {
431 FunctionType *FT = cast<FunctionType>(T);
432 // FUNCTION: [isvararg, retty, paramty x N]
433 Code = bitc::TYPE_CODE_FUNCTION;
434 TypeVals.push_back(FT->isVarArg());
435 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
436 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
437 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
438 AbbrevToUse = FunctionAbbrev;
441 case Type::StructTyID: {
442 StructType *ST = cast<StructType>(T);
443 // STRUCT: [ispacked, eltty x N]
444 TypeVals.push_back(ST->isPacked());
445 // Output all of the element types.
446 for (StructType::element_iterator I = ST->element_begin(),
447 E = ST->element_end(); I != E; ++I)
448 TypeVals.push_back(VE.getTypeID(*I));
450 if (ST->isLiteral()) {
451 Code = bitc::TYPE_CODE_STRUCT_ANON;
452 AbbrevToUse = StructAnonAbbrev;
454 if (ST->isOpaque()) {
455 Code = bitc::TYPE_CODE_OPAQUE;
457 Code = bitc::TYPE_CODE_STRUCT_NAMED;
458 AbbrevToUse = StructNamedAbbrev;
461 // Emit the name if it is present.
462 if (!ST->getName().empty())
463 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
464 StructNameAbbrev, Stream);
468 case Type::ArrayTyID: {
469 ArrayType *AT = cast<ArrayType>(T);
470 // ARRAY: [numelts, eltty]
471 Code = bitc::TYPE_CODE_ARRAY;
472 TypeVals.push_back(AT->getNumElements());
473 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
474 AbbrevToUse = ArrayAbbrev;
477 case Type::VectorTyID: {
478 VectorType *VT = cast<VectorType>(T);
479 // VECTOR [numelts, eltty]
480 Code = bitc::TYPE_CODE_VECTOR;
481 TypeVals.push_back(VT->getNumElements());
482 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
487 // Emit the finished record.
488 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
495 static unsigned getEncodedLinkage(const GlobalValue &GV) {
496 switch (GV.getLinkage()) {
497 case GlobalValue::ExternalLinkage:
499 case GlobalValue::WeakAnyLinkage:
501 case GlobalValue::AppendingLinkage:
503 case GlobalValue::InternalLinkage:
505 case GlobalValue::LinkOnceAnyLinkage:
507 case GlobalValue::ExternalWeakLinkage:
509 case GlobalValue::CommonLinkage:
511 case GlobalValue::PrivateLinkage:
513 case GlobalValue::WeakODRLinkage:
515 case GlobalValue::LinkOnceODRLinkage:
517 case GlobalValue::AvailableExternallyLinkage:
520 llvm_unreachable("Invalid linkage");
523 static unsigned getEncodedVisibility(const GlobalValue &GV) {
524 switch (GV.getVisibility()) {
525 case GlobalValue::DefaultVisibility: return 0;
526 case GlobalValue::HiddenVisibility: return 1;
527 case GlobalValue::ProtectedVisibility: return 2;
529 llvm_unreachable("Invalid visibility");
532 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
533 switch (GV.getDLLStorageClass()) {
534 case GlobalValue::DefaultStorageClass: return 0;
535 case GlobalValue::DLLImportStorageClass: return 1;
536 case GlobalValue::DLLExportStorageClass: return 2;
538 llvm_unreachable("Invalid DLL storage class");
541 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
542 switch (GV.getThreadLocalMode()) {
543 case GlobalVariable::NotThreadLocal: return 0;
544 case GlobalVariable::GeneralDynamicTLSModel: return 1;
545 case GlobalVariable::LocalDynamicTLSModel: return 2;
546 case GlobalVariable::InitialExecTLSModel: return 3;
547 case GlobalVariable::LocalExecTLSModel: return 4;
549 llvm_unreachable("Invalid TLS model");
552 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
553 switch (C.getSelectionKind()) {
555 return bitc::COMDAT_SELECTION_KIND_ANY;
556 case Comdat::ExactMatch:
557 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
558 case Comdat::Largest:
559 return bitc::COMDAT_SELECTION_KIND_LARGEST;
560 case Comdat::NoDuplicates:
561 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
562 case Comdat::SameSize:
563 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
565 llvm_unreachable("Invalid selection kind");
568 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
569 SmallVector<uint16_t, 64> Vals;
570 for (const Comdat *C : VE.getComdats()) {
571 // COMDAT: [selection_kind, name]
572 Vals.push_back(getEncodedComdatSelectionKind(*C));
573 size_t Size = C->getName().size();
574 assert(isUInt<16>(Size));
575 Vals.push_back(Size);
576 for (char Chr : C->getName())
577 Vals.push_back((unsigned char)Chr);
578 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
583 /// Write a record that will eventually hold the word offset of the
584 /// module-level VST. For now the offset is 0, which will be backpatched
585 /// after the real VST is written. Returns the bit offset to backpatch.
586 static uint64_t WriteValueSymbolTableForwardDecl(const ValueSymbolTable &VST,
587 BitstreamWriter &Stream) {
591 // Write a placeholder value in for the offset of the real VST,
592 // which is written after the function blocks so that it can include
593 // the offset of each function. The placeholder offset will be
594 // updated when the real VST is written.
595 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
596 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET));
597 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to
598 // hold the real VST offset. Must use fixed instead of VBR as we don't
599 // know how many VBR chunks to reserve ahead of time.
600 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
601 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv);
603 // Emit the placeholder
604 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0};
605 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals);
607 // Compute and return the bit offset to the placeholder, which will be
608 // patched when the real VST is written. We can simply subtract the 32-bit
609 // fixed size from the current bit number to get the location to backpatch.
610 return Stream.GetCurrentBitNo() - 32;
613 /// Emit top-level description of module, including target triple, inline asm,
614 /// descriptors for global variables, and function prototype info.
615 /// Returns the bit offset to backpatch with the location of the real VST.
616 static uint64_t WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
617 BitstreamWriter &Stream) {
618 // Emit various pieces of data attached to a module.
619 if (!M->getTargetTriple().empty())
620 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
622 const std::string &DL = M->getDataLayoutStr();
624 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
625 if (!M->getModuleInlineAsm().empty())
626 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
629 // Emit information about sections and GC, computing how many there are. Also
630 // compute the maximum alignment value.
631 std::map<std::string, unsigned> SectionMap;
632 std::map<std::string, unsigned> GCMap;
633 unsigned MaxAlignment = 0;
634 unsigned MaxGlobalType = 0;
635 for (const GlobalValue &GV : M->globals()) {
636 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
637 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType()));
638 if (GV.hasSection()) {
639 // Give section names unique ID's.
640 unsigned &Entry = SectionMap[GV.getSection()];
642 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
644 Entry = SectionMap.size();
648 for (const Function &F : *M) {
649 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
650 if (F.hasSection()) {
651 // Give section names unique ID's.
652 unsigned &Entry = SectionMap[F.getSection()];
654 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
656 Entry = SectionMap.size();
660 // Same for GC names.
661 unsigned &Entry = GCMap[F.getGC()];
663 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
665 Entry = GCMap.size();
670 // Emit abbrev for globals, now that we know # sections and max alignment.
671 unsigned SimpleGVarAbbrev = 0;
672 if (!M->global_empty()) {
673 // Add an abbrev for common globals with no visibility or thread localness.
674 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
675 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
676 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
677 Log2_32_Ceil(MaxGlobalType+1)));
678 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2
679 //| explicitType << 1
681 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
682 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage.
683 if (MaxAlignment == 0) // Alignment.
684 Abbv->Add(BitCodeAbbrevOp(0));
686 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
687 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
688 Log2_32_Ceil(MaxEncAlignment+1)));
690 if (SectionMap.empty()) // Section.
691 Abbv->Add(BitCodeAbbrevOp(0));
693 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
694 Log2_32_Ceil(SectionMap.size()+1)));
695 // Don't bother emitting vis + thread local.
696 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
699 // Emit the global variable information.
700 SmallVector<unsigned, 64> Vals;
701 for (const GlobalVariable &GV : M->globals()) {
702 unsigned AbbrevToUse = 0;
704 // GLOBALVAR: [type, isconst, initid,
705 // linkage, alignment, section, visibility, threadlocal,
706 // unnamed_addr, externally_initialized, dllstorageclass,
708 Vals.push_back(VE.getTypeID(GV.getValueType()));
709 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant());
710 Vals.push_back(GV.isDeclaration() ? 0 :
711 (VE.getValueID(GV.getInitializer()) + 1));
712 Vals.push_back(getEncodedLinkage(GV));
713 Vals.push_back(Log2_32(GV.getAlignment())+1);
714 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
715 if (GV.isThreadLocal() ||
716 GV.getVisibility() != GlobalValue::DefaultVisibility ||
717 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
718 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
720 Vals.push_back(getEncodedVisibility(GV));
721 Vals.push_back(getEncodedThreadLocalMode(GV));
722 Vals.push_back(GV.hasUnnamedAddr());
723 Vals.push_back(GV.isExternallyInitialized());
724 Vals.push_back(getEncodedDLLStorageClass(GV));
725 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
727 AbbrevToUse = SimpleGVarAbbrev;
730 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
734 // Emit the function proto information.
735 for (const Function &F : *M) {
736 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
737 // section, visibility, gc, unnamed_addr, prologuedata,
738 // dllstorageclass, comdat, prefixdata, personalityfn]
739 Vals.push_back(VE.getTypeID(F.getFunctionType()));
740 Vals.push_back(F.getCallingConv());
741 Vals.push_back(F.isDeclaration());
742 Vals.push_back(getEncodedLinkage(F));
743 Vals.push_back(VE.getAttributeID(F.getAttributes()));
744 Vals.push_back(Log2_32(F.getAlignment())+1);
745 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
746 Vals.push_back(getEncodedVisibility(F));
747 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
748 Vals.push_back(F.hasUnnamedAddr());
749 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1)
751 Vals.push_back(getEncodedDLLStorageClass(F));
752 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
753 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
756 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0);
758 unsigned AbbrevToUse = 0;
759 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
763 // Emit the alias information.
764 for (const GlobalAlias &A : M->aliases()) {
765 // ALIAS: [alias type, aliasee val#, linkage, visibility]
766 Vals.push_back(VE.getTypeID(A.getValueType()));
767 Vals.push_back(A.getType()->getAddressSpace());
768 Vals.push_back(VE.getValueID(A.getAliasee()));
769 Vals.push_back(getEncodedLinkage(A));
770 Vals.push_back(getEncodedVisibility(A));
771 Vals.push_back(getEncodedDLLStorageClass(A));
772 Vals.push_back(getEncodedThreadLocalMode(A));
773 Vals.push_back(A.hasUnnamedAddr());
774 unsigned AbbrevToUse = 0;
775 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
779 uint64_t VSTOffsetPlaceholder =
780 WriteValueSymbolTableForwardDecl(M->getValueSymbolTable(), Stream);
781 return VSTOffsetPlaceholder;
784 static uint64_t GetOptimizationFlags(const Value *V) {
787 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) {
788 if (OBO->hasNoSignedWrap())
789 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
790 if (OBO->hasNoUnsignedWrap())
791 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
792 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) {
794 Flags |= 1 << bitc::PEO_EXACT;
795 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) {
796 if (FPMO->hasUnsafeAlgebra())
797 Flags |= FastMathFlags::UnsafeAlgebra;
798 if (FPMO->hasNoNaNs())
799 Flags |= FastMathFlags::NoNaNs;
800 if (FPMO->hasNoInfs())
801 Flags |= FastMathFlags::NoInfs;
802 if (FPMO->hasNoSignedZeros())
803 Flags |= FastMathFlags::NoSignedZeros;
804 if (FPMO->hasAllowReciprocal())
805 Flags |= FastMathFlags::AllowReciprocal;
811 static void WriteValueAsMetadata(const ValueAsMetadata *MD,
812 const ValueEnumerator &VE,
813 BitstreamWriter &Stream,
814 SmallVectorImpl<uint64_t> &Record) {
815 // Mimic an MDNode with a value as one operand.
816 Value *V = MD->getValue();
817 Record.push_back(VE.getTypeID(V->getType()));
818 Record.push_back(VE.getValueID(V));
819 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
823 static void WriteMDTuple(const MDTuple *N, const ValueEnumerator &VE,
824 BitstreamWriter &Stream,
825 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
826 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
827 Metadata *MD = N->getOperand(i);
828 assert(!(MD && isa<LocalAsMetadata>(MD)) &&
829 "Unexpected function-local metadata");
830 Record.push_back(VE.getMetadataOrNullID(MD));
832 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE
833 : bitc::METADATA_NODE,
838 static void WriteDILocation(const DILocation *N, const ValueEnumerator &VE,
839 BitstreamWriter &Stream,
840 SmallVectorImpl<uint64_t> &Record,
842 Record.push_back(N->isDistinct());
843 Record.push_back(N->getLine());
844 Record.push_back(N->getColumn());
845 Record.push_back(VE.getMetadataID(N->getScope()));
846 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt()));
848 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev);
852 static void WriteGenericDINode(const GenericDINode *N,
853 const ValueEnumerator &VE,
854 BitstreamWriter &Stream,
855 SmallVectorImpl<uint64_t> &Record,
857 Record.push_back(N->isDistinct());
858 Record.push_back(N->getTag());
859 Record.push_back(0); // Per-tag version field; unused for now.
861 for (auto &I : N->operands())
862 Record.push_back(VE.getMetadataOrNullID(I));
864 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev);
868 static uint64_t rotateSign(int64_t I) {
870 return I < 0 ? ~(U << 1) : U << 1;
873 static void WriteDISubrange(const DISubrange *N, const ValueEnumerator &,
874 BitstreamWriter &Stream,
875 SmallVectorImpl<uint64_t> &Record,
877 Record.push_back(N->isDistinct());
878 Record.push_back(N->getCount());
879 Record.push_back(rotateSign(N->getLowerBound()));
881 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev);
885 static void WriteDIEnumerator(const DIEnumerator *N, const ValueEnumerator &VE,
886 BitstreamWriter &Stream,
887 SmallVectorImpl<uint64_t> &Record,
889 Record.push_back(N->isDistinct());
890 Record.push_back(rotateSign(N->getValue()));
891 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
893 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev);
897 static void WriteDIBasicType(const DIBasicType *N, const ValueEnumerator &VE,
898 BitstreamWriter &Stream,
899 SmallVectorImpl<uint64_t> &Record,
901 Record.push_back(N->isDistinct());
902 Record.push_back(N->getTag());
903 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
904 Record.push_back(N->getSizeInBits());
905 Record.push_back(N->getAlignInBits());
906 Record.push_back(N->getEncoding());
908 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev);
912 static void WriteDIDerivedType(const DIDerivedType *N,
913 const ValueEnumerator &VE,
914 BitstreamWriter &Stream,
915 SmallVectorImpl<uint64_t> &Record,
917 Record.push_back(N->isDistinct());
918 Record.push_back(N->getTag());
919 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
920 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
921 Record.push_back(N->getLine());
922 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
923 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
924 Record.push_back(N->getSizeInBits());
925 Record.push_back(N->getAlignInBits());
926 Record.push_back(N->getOffsetInBits());
927 Record.push_back(N->getFlags());
928 Record.push_back(VE.getMetadataOrNullID(N->getExtraData()));
930 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev);
934 static void WriteDICompositeType(const DICompositeType *N,
935 const ValueEnumerator &VE,
936 BitstreamWriter &Stream,
937 SmallVectorImpl<uint64_t> &Record,
939 Record.push_back(N->isDistinct());
940 Record.push_back(N->getTag());
941 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
942 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
943 Record.push_back(N->getLine());
944 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
945 Record.push_back(VE.getMetadataOrNullID(N->getBaseType()));
946 Record.push_back(N->getSizeInBits());
947 Record.push_back(N->getAlignInBits());
948 Record.push_back(N->getOffsetInBits());
949 Record.push_back(N->getFlags());
950 Record.push_back(VE.getMetadataOrNullID(N->getElements().get()));
951 Record.push_back(N->getRuntimeLang());
952 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder()));
953 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
954 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier()));
956 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev);
960 static void WriteDISubroutineType(const DISubroutineType *N,
961 const ValueEnumerator &VE,
962 BitstreamWriter &Stream,
963 SmallVectorImpl<uint64_t> &Record,
965 Record.push_back(N->isDistinct());
966 Record.push_back(N->getFlags());
967 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get()));
969 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev);
973 static void WriteDIFile(const DIFile *N, const ValueEnumerator &VE,
974 BitstreamWriter &Stream,
975 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
976 Record.push_back(N->isDistinct());
977 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename()));
978 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory()));
980 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev);
984 static void WriteDICompileUnit(const DICompileUnit *N,
985 const ValueEnumerator &VE,
986 BitstreamWriter &Stream,
987 SmallVectorImpl<uint64_t> &Record,
989 assert(N->isDistinct() && "Expected distinct compile units");
990 Record.push_back(/* IsDistinct */ true);
991 Record.push_back(N->getSourceLanguage());
992 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
993 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer()));
994 Record.push_back(N->isOptimized());
995 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags()));
996 Record.push_back(N->getRuntimeVersion());
997 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename()));
998 Record.push_back(N->getEmissionKind());
999 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get()));
1000 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get()));
1001 Record.push_back(VE.getMetadataOrNullID(N->getSubprograms().get()));
1002 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get()));
1003 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get()));
1004 Record.push_back(N->getDWOId());
1006 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev);
1010 static void WriteDISubprogram(const DISubprogram *N, const ValueEnumerator &VE,
1011 BitstreamWriter &Stream,
1012 SmallVectorImpl<uint64_t> &Record,
1014 Record.push_back(N->isDistinct());
1015 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1016 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1017 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1018 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1019 Record.push_back(N->getLine());
1020 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1021 Record.push_back(N->isLocalToUnit());
1022 Record.push_back(N->isDefinition());
1023 Record.push_back(N->getScopeLine());
1024 Record.push_back(VE.getMetadataOrNullID(N->getContainingType()));
1025 Record.push_back(N->getVirtuality());
1026 Record.push_back(N->getVirtualIndex());
1027 Record.push_back(N->getFlags());
1028 Record.push_back(N->isOptimized());
1029 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get()));
1030 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration()));
1031 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get()));
1033 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev);
1037 static void WriteDILexicalBlock(const DILexicalBlock *N,
1038 const ValueEnumerator &VE,
1039 BitstreamWriter &Stream,
1040 SmallVectorImpl<uint64_t> &Record,
1042 Record.push_back(N->isDistinct());
1043 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1044 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1045 Record.push_back(N->getLine());
1046 Record.push_back(N->getColumn());
1048 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev);
1052 static void WriteDILexicalBlockFile(const DILexicalBlockFile *N,
1053 const ValueEnumerator &VE,
1054 BitstreamWriter &Stream,
1055 SmallVectorImpl<uint64_t> &Record,
1057 Record.push_back(N->isDistinct());
1058 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1059 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1060 Record.push_back(N->getDiscriminator());
1062 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev);
1066 static void WriteDINamespace(const DINamespace *N, const ValueEnumerator &VE,
1067 BitstreamWriter &Stream,
1068 SmallVectorImpl<uint64_t> &Record,
1070 Record.push_back(N->isDistinct());
1071 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1072 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1073 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1074 Record.push_back(N->getLine());
1076 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev);
1080 static void WriteDIModule(const DIModule *N, const ValueEnumerator &VE,
1081 BitstreamWriter &Stream,
1082 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) {
1083 Record.push_back(N->isDistinct());
1084 for (auto &I : N->operands())
1085 Record.push_back(VE.getMetadataOrNullID(I));
1087 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev);
1091 static void WriteDITemplateTypeParameter(const DITemplateTypeParameter *N,
1092 const ValueEnumerator &VE,
1093 BitstreamWriter &Stream,
1094 SmallVectorImpl<uint64_t> &Record,
1096 Record.push_back(N->isDistinct());
1097 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1098 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1100 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev);
1104 static void WriteDITemplateValueParameter(const DITemplateValueParameter *N,
1105 const ValueEnumerator &VE,
1106 BitstreamWriter &Stream,
1107 SmallVectorImpl<uint64_t> &Record,
1109 Record.push_back(N->isDistinct());
1110 Record.push_back(N->getTag());
1111 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1112 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1113 Record.push_back(VE.getMetadataOrNullID(N->getValue()));
1115 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev);
1119 static void WriteDIGlobalVariable(const DIGlobalVariable *N,
1120 const ValueEnumerator &VE,
1121 BitstreamWriter &Stream,
1122 SmallVectorImpl<uint64_t> &Record,
1124 Record.push_back(N->isDistinct());
1125 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1126 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1127 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName()));
1128 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1129 Record.push_back(N->getLine());
1130 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1131 Record.push_back(N->isLocalToUnit());
1132 Record.push_back(N->isDefinition());
1133 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable()));
1134 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration()));
1136 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev);
1140 static void WriteDILocalVariable(const DILocalVariable *N,
1141 const ValueEnumerator &VE,
1142 BitstreamWriter &Stream,
1143 SmallVectorImpl<uint64_t> &Record,
1145 Record.push_back(N->isDistinct());
1146 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1147 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1148 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1149 Record.push_back(N->getLine());
1150 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1151 Record.push_back(N->getArg());
1152 Record.push_back(N->getFlags());
1154 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev);
1158 static void WriteDIExpression(const DIExpression *N, const ValueEnumerator &,
1159 BitstreamWriter &Stream,
1160 SmallVectorImpl<uint64_t> &Record,
1162 Record.reserve(N->getElements().size() + 1);
1164 Record.push_back(N->isDistinct());
1165 Record.append(N->elements_begin(), N->elements_end());
1167 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev);
1171 static void WriteDIObjCProperty(const DIObjCProperty *N,
1172 const ValueEnumerator &VE,
1173 BitstreamWriter &Stream,
1174 SmallVectorImpl<uint64_t> &Record,
1176 Record.push_back(N->isDistinct());
1177 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1178 Record.push_back(VE.getMetadataOrNullID(N->getFile()));
1179 Record.push_back(N->getLine());
1180 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName()));
1181 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName()));
1182 Record.push_back(N->getAttributes());
1183 Record.push_back(VE.getMetadataOrNullID(N->getType()));
1185 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev);
1189 static void WriteDIImportedEntity(const DIImportedEntity *N,
1190 const ValueEnumerator &VE,
1191 BitstreamWriter &Stream,
1192 SmallVectorImpl<uint64_t> &Record,
1194 Record.push_back(N->isDistinct());
1195 Record.push_back(N->getTag());
1196 Record.push_back(VE.getMetadataOrNullID(N->getScope()));
1197 Record.push_back(VE.getMetadataOrNullID(N->getEntity()));
1198 Record.push_back(N->getLine());
1199 Record.push_back(VE.getMetadataOrNullID(N->getRawName()));
1201 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev);
1205 static void WriteModuleMetadata(const Module *M,
1206 const ValueEnumerator &VE,
1207 BitstreamWriter &Stream) {
1208 const auto &MDs = VE.getMDs();
1209 if (MDs.empty() && M->named_metadata_empty())
1212 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1214 unsigned MDSAbbrev = 0;
1215 if (VE.hasMDString()) {
1216 // Abbrev for METADATA_STRING.
1217 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1218 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
1219 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1220 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1221 MDSAbbrev = Stream.EmitAbbrev(Abbv);
1224 // Initialize MDNode abbreviations.
1225 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0;
1226 #include "llvm/IR/Metadata.def"
1228 if (VE.hasDILocation()) {
1229 // Abbrev for METADATA_LOCATION.
1231 // Assume the column is usually under 128, and always output the inlined-at
1232 // location (it's never more expensive than building an array size 1).
1233 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1234 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION));
1235 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1237 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1238 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1240 DILocationAbbrev = Stream.EmitAbbrev(Abbv);
1243 if (VE.hasGenericDINode()) {
1244 // Abbrev for METADATA_GENERIC_DEBUG.
1246 // Assume the column is usually under 128, and always output the inlined-at
1247 // location (it's never more expensive than building an array size 1).
1248 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1249 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG));
1250 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1251 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1252 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
1253 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1254 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1255 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1256 GenericDINodeAbbrev = Stream.EmitAbbrev(Abbv);
1259 unsigned NameAbbrev = 0;
1260 if (!M->named_metadata_empty()) {
1261 // Abbrev for METADATA_NAME.
1262 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1263 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME));
1264 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1265 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1266 NameAbbrev = Stream.EmitAbbrev(Abbv);
1269 SmallVector<uint64_t, 64> Record;
1270 for (const Metadata *MD : MDs) {
1271 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
1272 assert(N->isResolved() && "Expected forward references to be resolved");
1274 switch (N->getMetadataID()) {
1276 llvm_unreachable("Invalid MDNode subclass");
1277 #define HANDLE_MDNODE_LEAF(CLASS) \
1278 case Metadata::CLASS##Kind: \
1279 Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev); \
1281 #include "llvm/IR/Metadata.def"
1284 if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) {
1285 WriteValueAsMetadata(MDC, VE, Stream, Record);
1288 const MDString *MDS = cast<MDString>(MD);
1289 // Code: [strchar x N]
1290 Record.append(MDS->bytes_begin(), MDS->bytes_end());
1292 // Emit the finished record.
1293 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
1297 // Write named metadata.
1298 for (const NamedMDNode &NMD : M->named_metadata()) {
1300 StringRef Str = NMD.getName();
1301 Record.append(Str.bytes_begin(), Str.bytes_end());
1302 Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev);
1305 // Write named metadata operands.
1306 for (const MDNode *N : NMD.operands())
1307 Record.push_back(VE.getMetadataID(N));
1308 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
1315 static void WriteFunctionLocalMetadata(const Function &F,
1316 const ValueEnumerator &VE,
1317 BitstreamWriter &Stream) {
1318 bool StartedMetadataBlock = false;
1319 SmallVector<uint64_t, 64> Record;
1320 const SmallVectorImpl<const LocalAsMetadata *> &MDs =
1321 VE.getFunctionLocalMDs();
1322 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1323 assert(MDs[i] && "Expected valid function-local metadata");
1324 if (!StartedMetadataBlock) {
1325 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
1326 StartedMetadataBlock = true;
1328 WriteValueAsMetadata(MDs[i], VE, Stream, Record);
1331 if (StartedMetadataBlock)
1335 static void WriteMetadataAttachment(const Function &F,
1336 const ValueEnumerator &VE,
1337 BitstreamWriter &Stream) {
1338 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
1340 SmallVector<uint64_t, 64> Record;
1342 // Write metadata attachments
1343 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
1344 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1345 F.getAllMetadata(MDs);
1347 for (const auto &I : MDs) {
1348 Record.push_back(I.first);
1349 Record.push_back(VE.getMetadataID(I.second));
1351 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1355 for (const BasicBlock &BB : F)
1356 for (const Instruction &I : BB) {
1358 I.getAllMetadataOtherThanDebugLoc(MDs);
1360 // If no metadata, ignore instruction.
1361 if (MDs.empty()) continue;
1363 Record.push_back(VE.getInstructionID(&I));
1365 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
1366 Record.push_back(MDs[i].first);
1367 Record.push_back(VE.getMetadataID(MDs[i].second));
1369 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
1376 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
1377 SmallVector<uint64_t, 64> Record;
1379 // Write metadata kinds
1380 // METADATA_KIND - [n x [id, name]]
1381 SmallVector<StringRef, 8> Names;
1382 M->getMDKindNames(Names);
1384 if (Names.empty()) return;
1386 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3);
1388 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
1389 Record.push_back(MDKindID);
1390 StringRef KName = Names[MDKindID];
1391 Record.append(KName.begin(), KName.end());
1393 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
1400 static void WriteOperandBundleTags(const Module *M, BitstreamWriter &Stream) {
1401 // Write metadata kinds
1403 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG
1405 // OPERAND_BUNDLE_TAG - [strchr x N]
1407 SmallVector<StringRef, 8> Tags;
1408 M->getOperandBundleTags(Tags);
1413 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3);
1415 SmallVector<uint64_t, 64> Record;
1417 for (auto Tag : Tags) {
1418 Record.append(Tag.begin(), Tag.end());
1420 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0);
1427 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
1428 if ((int64_t)V >= 0)
1429 Vals.push_back(V << 1);
1431 Vals.push_back((-V << 1) | 1);
1434 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
1435 const ValueEnumerator &VE,
1436 BitstreamWriter &Stream, bool isGlobal) {
1437 if (FirstVal == LastVal) return;
1439 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
1441 unsigned AggregateAbbrev = 0;
1442 unsigned String8Abbrev = 0;
1443 unsigned CString7Abbrev = 0;
1444 unsigned CString6Abbrev = 0;
1445 // If this is a constant pool for the module, emit module-specific abbrevs.
1447 // Abbrev for CST_CODE_AGGREGATE.
1448 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1449 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
1450 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1451 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
1452 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
1454 // Abbrev for CST_CODE_STRING.
1455 Abbv = new BitCodeAbbrev();
1456 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
1457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1458 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1459 String8Abbrev = Stream.EmitAbbrev(Abbv);
1460 // Abbrev for CST_CODE_CSTRING.
1461 Abbv = new BitCodeAbbrev();
1462 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1463 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1464 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1465 CString7Abbrev = Stream.EmitAbbrev(Abbv);
1466 // Abbrev for CST_CODE_CSTRING.
1467 Abbv = new BitCodeAbbrev();
1468 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
1469 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1470 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1471 CString6Abbrev = Stream.EmitAbbrev(Abbv);
1474 SmallVector<uint64_t, 64> Record;
1476 const ValueEnumerator::ValueList &Vals = VE.getValues();
1477 Type *LastTy = nullptr;
1478 for (unsigned i = FirstVal; i != LastVal; ++i) {
1479 const Value *V = Vals[i].first;
1480 // If we need to switch types, do so now.
1481 if (V->getType() != LastTy) {
1482 LastTy = V->getType();
1483 Record.push_back(VE.getTypeID(LastTy));
1484 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
1485 CONSTANTS_SETTYPE_ABBREV);
1489 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
1490 Record.push_back(unsigned(IA->hasSideEffects()) |
1491 unsigned(IA->isAlignStack()) << 1 |
1492 unsigned(IA->getDialect()&1) << 2);
1494 // Add the asm string.
1495 const std::string &AsmStr = IA->getAsmString();
1496 Record.push_back(AsmStr.size());
1497 Record.append(AsmStr.begin(), AsmStr.end());
1499 // Add the constraint string.
1500 const std::string &ConstraintStr = IA->getConstraintString();
1501 Record.push_back(ConstraintStr.size());
1502 Record.append(ConstraintStr.begin(), ConstraintStr.end());
1503 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
1507 const Constant *C = cast<Constant>(V);
1508 unsigned Code = -1U;
1509 unsigned AbbrevToUse = 0;
1510 if (C->isNullValue()) {
1511 Code = bitc::CST_CODE_NULL;
1512 } else if (isa<UndefValue>(C)) {
1513 Code = bitc::CST_CODE_UNDEF;
1514 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
1515 if (IV->getBitWidth() <= 64) {
1516 uint64_t V = IV->getSExtValue();
1517 emitSignedInt64(Record, V);
1518 Code = bitc::CST_CODE_INTEGER;
1519 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1520 } else { // Wide integers, > 64 bits in size.
1521 // We have an arbitrary precision integer value to write whose
1522 // bit width is > 64. However, in canonical unsigned integer
1523 // format it is likely that the high bits are going to be zero.
1524 // So, we only write the number of active words.
1525 unsigned NWords = IV->getValue().getActiveWords();
1526 const uint64_t *RawWords = IV->getValue().getRawData();
1527 for (unsigned i = 0; i != NWords; ++i) {
1528 emitSignedInt64(Record, RawWords[i]);
1530 Code = bitc::CST_CODE_WIDE_INTEGER;
1532 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1533 Code = bitc::CST_CODE_FLOAT;
1534 Type *Ty = CFP->getType();
1535 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
1536 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
1537 } else if (Ty->isX86_FP80Ty()) {
1538 // api needed to prevent premature destruction
1539 // bits are not in the same order as a normal i80 APInt, compensate.
1540 APInt api = CFP->getValueAPF().bitcastToAPInt();
1541 const uint64_t *p = api.getRawData();
1542 Record.push_back((p[1] << 48) | (p[0] >> 16));
1543 Record.push_back(p[0] & 0xffffLL);
1544 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
1545 APInt api = CFP->getValueAPF().bitcastToAPInt();
1546 const uint64_t *p = api.getRawData();
1547 Record.push_back(p[0]);
1548 Record.push_back(p[1]);
1550 assert (0 && "Unknown FP type!");
1552 } else if (isa<ConstantDataSequential>(C) &&
1553 cast<ConstantDataSequential>(C)->isString()) {
1554 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1555 // Emit constant strings specially.
1556 unsigned NumElts = Str->getNumElements();
1557 // If this is a null-terminated string, use the denser CSTRING encoding.
1558 if (Str->isCString()) {
1559 Code = bitc::CST_CODE_CSTRING;
1560 --NumElts; // Don't encode the null, which isn't allowed by char6.
1562 Code = bitc::CST_CODE_STRING;
1563 AbbrevToUse = String8Abbrev;
1565 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1566 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1567 for (unsigned i = 0; i != NumElts; ++i) {
1568 unsigned char V = Str->getElementAsInteger(i);
1569 Record.push_back(V);
1570 isCStr7 &= (V & 128) == 0;
1572 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1576 AbbrevToUse = CString6Abbrev;
1578 AbbrevToUse = CString7Abbrev;
1579 } else if (const ConstantDataSequential *CDS =
1580 dyn_cast<ConstantDataSequential>(C)) {
1581 Code = bitc::CST_CODE_DATA;
1582 Type *EltTy = CDS->getType()->getElementType();
1583 if (isa<IntegerType>(EltTy)) {
1584 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1585 Record.push_back(CDS->getElementAsInteger(i));
1586 } else if (EltTy->isFloatTy()) {
1587 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1588 union { float F; uint32_t I; };
1589 F = CDS->getElementAsFloat(i);
1590 Record.push_back(I);
1593 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
1594 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1595 union { double F; uint64_t I; };
1596 F = CDS->getElementAsDouble(i);
1597 Record.push_back(I);
1600 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1601 isa<ConstantVector>(C)) {
1602 Code = bitc::CST_CODE_AGGREGATE;
1603 for (const Value *Op : C->operands())
1604 Record.push_back(VE.getValueID(Op));
1605 AbbrevToUse = AggregateAbbrev;
1606 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1607 switch (CE->getOpcode()) {
1609 if (Instruction::isCast(CE->getOpcode())) {
1610 Code = bitc::CST_CODE_CE_CAST;
1611 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
1612 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1613 Record.push_back(VE.getValueID(C->getOperand(0)));
1614 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
1616 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1617 Code = bitc::CST_CODE_CE_BINOP;
1618 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
1619 Record.push_back(VE.getValueID(C->getOperand(0)));
1620 Record.push_back(VE.getValueID(C->getOperand(1)));
1621 uint64_t Flags = GetOptimizationFlags(CE);
1623 Record.push_back(Flags);
1626 case Instruction::GetElementPtr: {
1627 Code = bitc::CST_CODE_CE_GEP;
1628 const auto *GO = cast<GEPOperator>(C);
1629 if (GO->isInBounds())
1630 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
1631 Record.push_back(VE.getTypeID(GO->getSourceElementType()));
1632 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1633 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1634 Record.push_back(VE.getValueID(C->getOperand(i)));
1638 case Instruction::Select:
1639 Code = bitc::CST_CODE_CE_SELECT;
1640 Record.push_back(VE.getValueID(C->getOperand(0)));
1641 Record.push_back(VE.getValueID(C->getOperand(1)));
1642 Record.push_back(VE.getValueID(C->getOperand(2)));
1644 case Instruction::ExtractElement:
1645 Code = bitc::CST_CODE_CE_EXTRACTELT;
1646 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1647 Record.push_back(VE.getValueID(C->getOperand(0)));
1648 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
1649 Record.push_back(VE.getValueID(C->getOperand(1)));
1651 case Instruction::InsertElement:
1652 Code = bitc::CST_CODE_CE_INSERTELT;
1653 Record.push_back(VE.getValueID(C->getOperand(0)));
1654 Record.push_back(VE.getValueID(C->getOperand(1)));
1655 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
1656 Record.push_back(VE.getValueID(C->getOperand(2)));
1658 case Instruction::ShuffleVector:
1659 // If the return type and argument types are the same, this is a
1660 // standard shufflevector instruction. If the types are different,
1661 // then the shuffle is widening or truncating the input vectors, and
1662 // the argument type must also be encoded.
1663 if (C->getType() == C->getOperand(0)->getType()) {
1664 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1666 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1667 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1669 Record.push_back(VE.getValueID(C->getOperand(0)));
1670 Record.push_back(VE.getValueID(C->getOperand(1)));
1671 Record.push_back(VE.getValueID(C->getOperand(2)));
1673 case Instruction::ICmp:
1674 case Instruction::FCmp:
1675 Code = bitc::CST_CODE_CE_CMP;
1676 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1677 Record.push_back(VE.getValueID(C->getOperand(0)));
1678 Record.push_back(VE.getValueID(C->getOperand(1)));
1679 Record.push_back(CE->getPredicate());
1682 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1683 Code = bitc::CST_CODE_BLOCKADDRESS;
1684 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1685 Record.push_back(VE.getValueID(BA->getFunction()));
1686 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1691 llvm_unreachable("Unknown constant!");
1693 Stream.EmitRecord(Code, Record, AbbrevToUse);
1700 static void WriteModuleConstants(const ValueEnumerator &VE,
1701 BitstreamWriter &Stream) {
1702 const ValueEnumerator::ValueList &Vals = VE.getValues();
1704 // Find the first constant to emit, which is the first non-globalvalue value.
1705 // We know globalvalues have been emitted by WriteModuleInfo.
1706 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1707 if (!isa<GlobalValue>(Vals[i].first)) {
1708 WriteConstants(i, Vals.size(), VE, Stream, true);
1714 /// PushValueAndType - The file has to encode both the value and type id for
1715 /// many values, because we need to know what type to create for forward
1716 /// references. However, most operands are not forward references, so this type
1717 /// field is not needed.
1719 /// This function adds V's value ID to Vals. If the value ID is higher than the
1720 /// instruction ID, then it is a forward reference, and it also includes the
1721 /// type ID. The value ID that is written is encoded relative to the InstID.
1722 static bool PushValueAndType(const Value *V, unsigned InstID,
1723 SmallVectorImpl<unsigned> &Vals,
1724 ValueEnumerator &VE) {
1725 unsigned ValID = VE.getValueID(V);
1726 // Make encoding relative to the InstID.
1727 Vals.push_back(InstID - ValID);
1728 if (ValID >= InstID) {
1729 Vals.push_back(VE.getTypeID(V->getType()));
1735 static void WriteOperandBundles(BitstreamWriter &Stream, ImmutableCallSite CS,
1736 unsigned InstID, ValueEnumerator &VE) {
1737 SmallVector<unsigned, 64> Record;
1738 LLVMContext &C = CS.getInstruction()->getContext();
1740 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
1741 const auto &Bundle = CS.getOperandBundleAt(i);
1742 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
1744 for (auto &Input : Bundle.Inputs)
1745 PushValueAndType(Input, InstID, Record, VE);
1747 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
1752 /// pushValue - Like PushValueAndType, but where the type of the value is
1753 /// omitted (perhaps it was already encoded in an earlier operand).
1754 static void pushValue(const Value *V, unsigned InstID,
1755 SmallVectorImpl<unsigned> &Vals,
1756 ValueEnumerator &VE) {
1757 unsigned ValID = VE.getValueID(V);
1758 Vals.push_back(InstID - ValID);
1761 static void pushValueSigned(const Value *V, unsigned InstID,
1762 SmallVectorImpl<uint64_t> &Vals,
1763 ValueEnumerator &VE) {
1764 unsigned ValID = VE.getValueID(V);
1765 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
1766 emitSignedInt64(Vals, diff);
1769 /// WriteInstruction - Emit an instruction to the specified stream.
1770 static void WriteInstruction(const Instruction &I, unsigned InstID,
1771 ValueEnumerator &VE, BitstreamWriter &Stream,
1772 SmallVectorImpl<unsigned> &Vals) {
1774 unsigned AbbrevToUse = 0;
1775 VE.setInstructionID(&I);
1776 switch (I.getOpcode()) {
1778 if (Instruction::isCast(I.getOpcode())) {
1779 Code = bitc::FUNC_CODE_INST_CAST;
1780 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1781 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1782 Vals.push_back(VE.getTypeID(I.getType()));
1783 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1785 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1786 Code = bitc::FUNC_CODE_INST_BINOP;
1787 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1788 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1789 pushValue(I.getOperand(1), InstID, Vals, VE);
1790 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1791 uint64_t Flags = GetOptimizationFlags(&I);
1793 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1794 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1795 Vals.push_back(Flags);
1800 case Instruction::GetElementPtr: {
1801 Code = bitc::FUNC_CODE_INST_GEP;
1802 AbbrevToUse = FUNCTION_INST_GEP_ABBREV;
1803 auto &GEPInst = cast<GetElementPtrInst>(I);
1804 Vals.push_back(GEPInst.isInBounds());
1805 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType()));
1806 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1807 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1810 case Instruction::ExtractValue: {
1811 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
1812 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1813 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1814 Vals.append(EVI->idx_begin(), EVI->idx_end());
1817 case Instruction::InsertValue: {
1818 Code = bitc::FUNC_CODE_INST_INSERTVAL;
1819 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1820 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1821 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1822 Vals.append(IVI->idx_begin(), IVI->idx_end());
1825 case Instruction::Select:
1826 Code = bitc::FUNC_CODE_INST_VSELECT;
1827 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1828 pushValue(I.getOperand(2), InstID, Vals, VE);
1829 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1831 case Instruction::ExtractElement:
1832 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
1833 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1834 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1836 case Instruction::InsertElement:
1837 Code = bitc::FUNC_CODE_INST_INSERTELT;
1838 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1839 pushValue(I.getOperand(1), InstID, Vals, VE);
1840 PushValueAndType(I.getOperand(2), InstID, Vals, VE);
1842 case Instruction::ShuffleVector:
1843 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
1844 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1845 pushValue(I.getOperand(1), InstID, Vals, VE);
1846 pushValue(I.getOperand(2), InstID, Vals, VE);
1848 case Instruction::ICmp:
1849 case Instruction::FCmp: {
1850 // compare returning Int1Ty or vector of Int1Ty
1851 Code = bitc::FUNC_CODE_INST_CMP2;
1852 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1853 pushValue(I.getOperand(1), InstID, Vals, VE);
1854 Vals.push_back(cast<CmpInst>(I).getPredicate());
1855 uint64_t Flags = GetOptimizationFlags(&I);
1857 Vals.push_back(Flags);
1861 case Instruction::Ret:
1863 Code = bitc::FUNC_CODE_INST_RET;
1864 unsigned NumOperands = I.getNumOperands();
1865 if (NumOperands == 0)
1866 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1867 else if (NumOperands == 1) {
1868 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1869 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1871 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1872 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1876 case Instruction::Br:
1878 Code = bitc::FUNC_CODE_INST_BR;
1879 const BranchInst &II = cast<BranchInst>(I);
1880 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1881 if (II.isConditional()) {
1882 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1883 pushValue(II.getCondition(), InstID, Vals, VE);
1887 case Instruction::Switch:
1889 Code = bitc::FUNC_CODE_INST_SWITCH;
1890 const SwitchInst &SI = cast<SwitchInst>(I);
1891 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
1892 pushValue(SI.getCondition(), InstID, Vals, VE);
1893 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1894 for (SwitchInst::ConstCaseIt Case : SI.cases()) {
1895 Vals.push_back(VE.getValueID(Case.getCaseValue()));
1896 Vals.push_back(VE.getValueID(Case.getCaseSuccessor()));
1900 case Instruction::IndirectBr:
1901 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
1902 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1903 // Encode the address operand as relative, but not the basic blocks.
1904 pushValue(I.getOperand(0), InstID, Vals, VE);
1905 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1906 Vals.push_back(VE.getValueID(I.getOperand(i)));
1909 case Instruction::Invoke: {
1910 const InvokeInst *II = cast<InvokeInst>(&I);
1911 const Value *Callee = II->getCalledValue();
1912 FunctionType *FTy = II->getFunctionType();
1914 if (II->hasOperandBundles())
1915 WriteOperandBundles(Stream, II, InstID, VE);
1917 Code = bitc::FUNC_CODE_INST_INVOKE;
1919 Vals.push_back(VE.getAttributeID(II->getAttributes()));
1920 Vals.push_back(II->getCallingConv() | 1 << 13);
1921 Vals.push_back(VE.getValueID(II->getNormalDest()));
1922 Vals.push_back(VE.getValueID(II->getUnwindDest()));
1923 Vals.push_back(VE.getTypeID(FTy));
1924 PushValueAndType(Callee, InstID, Vals, VE);
1926 // Emit value #'s for the fixed parameters.
1927 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1928 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
1930 // Emit type/value pairs for varargs params.
1931 if (FTy->isVarArg()) {
1932 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1934 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1938 case Instruction::Resume:
1939 Code = bitc::FUNC_CODE_INST_RESUME;
1940 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1942 case Instruction::CleanupRet: {
1943 Code = bitc::FUNC_CODE_INST_CLEANUPRET;
1944 const auto &CRI = cast<CleanupReturnInst>(I);
1945 pushValue(CRI.getCleanupPad(), InstID, Vals, VE);
1946 if (CRI.hasUnwindDest())
1947 Vals.push_back(VE.getValueID(CRI.getUnwindDest()));
1950 case Instruction::CatchRet: {
1951 Code = bitc::FUNC_CODE_INST_CATCHRET;
1952 const auto &CRI = cast<CatchReturnInst>(I);
1953 pushValue(CRI.getCatchPad(), InstID, Vals, VE);
1954 Vals.push_back(VE.getValueID(CRI.getSuccessor()));
1957 case Instruction::CatchPad: {
1958 Code = bitc::FUNC_CODE_INST_CATCHPAD;
1959 const auto &CPI = cast<CatchPadInst>(I);
1960 Vals.push_back(VE.getValueID(CPI.getNormalDest()));
1961 Vals.push_back(VE.getValueID(CPI.getUnwindDest()));
1962 unsigned NumArgOperands = CPI.getNumArgOperands();
1963 Vals.push_back(NumArgOperands);
1964 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
1965 PushValueAndType(CPI.getArgOperand(Op), InstID, Vals, VE);
1968 case Instruction::TerminatePad: {
1969 Code = bitc::FUNC_CODE_INST_TERMINATEPAD;
1970 const auto &TPI = cast<TerminatePadInst>(I);
1971 Vals.push_back(TPI.hasUnwindDest());
1972 if (TPI.hasUnwindDest())
1973 Vals.push_back(VE.getValueID(TPI.getUnwindDest()));
1974 unsigned NumArgOperands = TPI.getNumArgOperands();
1975 Vals.push_back(NumArgOperands);
1976 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
1977 PushValueAndType(TPI.getArgOperand(Op), InstID, Vals, VE);
1980 case Instruction::CleanupPad: {
1981 Code = bitc::FUNC_CODE_INST_CLEANUPPAD;
1982 const auto &CPI = cast<CleanupPadInst>(I);
1983 unsigned NumOperands = CPI.getNumOperands();
1984 Vals.push_back(NumOperands);
1985 for (unsigned Op = 0; Op != NumOperands; ++Op)
1986 PushValueAndType(CPI.getOperand(Op), InstID, Vals, VE);
1989 case Instruction::CatchEndPad: {
1990 Code = bitc::FUNC_CODE_INST_CATCHENDPAD;
1991 const auto &CEPI = cast<CatchEndPadInst>(I);
1992 if (CEPI.hasUnwindDest())
1993 Vals.push_back(VE.getValueID(CEPI.getUnwindDest()));
1996 case Instruction::CleanupEndPad: {
1997 Code = bitc::FUNC_CODE_INST_CLEANUPENDPAD;
1998 const auto &CEPI = cast<CleanupEndPadInst>(I);
1999 pushValue(CEPI.getCleanupPad(), InstID, Vals, VE);
2000 if (CEPI.hasUnwindDest())
2001 Vals.push_back(VE.getValueID(CEPI.getUnwindDest()));
2004 case Instruction::Unreachable:
2005 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
2006 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
2009 case Instruction::PHI: {
2010 const PHINode &PN = cast<PHINode>(I);
2011 Code = bitc::FUNC_CODE_INST_PHI;
2012 // With the newer instruction encoding, forward references could give
2013 // negative valued IDs. This is most common for PHIs, so we use
2015 SmallVector<uint64_t, 128> Vals64;
2016 Vals64.push_back(VE.getTypeID(PN.getType()));
2017 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2018 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
2019 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
2021 // Emit a Vals64 vector and exit.
2022 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
2027 case Instruction::LandingPad: {
2028 const LandingPadInst &LP = cast<LandingPadInst>(I);
2029 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
2030 Vals.push_back(VE.getTypeID(LP.getType()));
2031 Vals.push_back(LP.isCleanup());
2032 Vals.push_back(LP.getNumClauses());
2033 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
2035 Vals.push_back(LandingPadInst::Catch);
2037 Vals.push_back(LandingPadInst::Filter);
2038 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
2043 case Instruction::Alloca: {
2044 Code = bitc::FUNC_CODE_INST_ALLOCA;
2045 const AllocaInst &AI = cast<AllocaInst>(I);
2046 Vals.push_back(VE.getTypeID(AI.getAllocatedType()));
2047 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
2048 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
2049 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
2050 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
2051 "not enough bits for maximum alignment");
2052 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
2053 AlignRecord |= AI.isUsedWithInAlloca() << 5;
2054 AlignRecord |= 1 << 6;
2055 // Reserve bit 7 for SwiftError flag.
2056 // AlignRecord |= AI.isSwiftError() << 7;
2057 Vals.push_back(AlignRecord);
2061 case Instruction::Load:
2062 if (cast<LoadInst>(I).isAtomic()) {
2063 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
2064 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
2066 Code = bitc::FUNC_CODE_INST_LOAD;
2067 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
2068 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
2070 Vals.push_back(VE.getTypeID(I.getType()));
2071 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
2072 Vals.push_back(cast<LoadInst>(I).isVolatile());
2073 if (cast<LoadInst>(I).isAtomic()) {
2074 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
2075 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
2078 case Instruction::Store:
2079 if (cast<StoreInst>(I).isAtomic())
2080 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
2082 Code = bitc::FUNC_CODE_INST_STORE;
2083 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
2084 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // valty + val
2085 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
2086 Vals.push_back(cast<StoreInst>(I).isVolatile());
2087 if (cast<StoreInst>(I).isAtomic()) {
2088 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
2089 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
2092 case Instruction::AtomicCmpXchg:
2093 Code = bitc::FUNC_CODE_INST_CMPXCHG;
2094 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
2095 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // cmp.
2096 pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
2097 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
2098 Vals.push_back(GetEncodedOrdering(
2099 cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
2100 Vals.push_back(GetEncodedSynchScope(
2101 cast<AtomicCmpXchgInst>(I).getSynchScope()));
2102 Vals.push_back(GetEncodedOrdering(
2103 cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
2104 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
2106 case Instruction::AtomicRMW:
2107 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
2108 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
2109 pushValue(I.getOperand(1), InstID, Vals, VE); // val.
2110 Vals.push_back(GetEncodedRMWOperation(
2111 cast<AtomicRMWInst>(I).getOperation()));
2112 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
2113 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
2114 Vals.push_back(GetEncodedSynchScope(
2115 cast<AtomicRMWInst>(I).getSynchScope()));
2117 case Instruction::Fence:
2118 Code = bitc::FUNC_CODE_INST_FENCE;
2119 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
2120 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
2122 case Instruction::Call: {
2123 const CallInst &CI = cast<CallInst>(I);
2124 FunctionType *FTy = CI.getFunctionType();
2126 if (CI.hasOperandBundles())
2127 WriteOperandBundles(Stream, &CI, InstID, VE);
2129 Code = bitc::FUNC_CODE_INST_CALL;
2131 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
2132 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV |
2133 unsigned(CI.isTailCall()) << bitc::CALL_TAIL |
2134 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL |
2135 1 << bitc::CALL_EXPLICIT_TYPE |
2136 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL);
2137 Vals.push_back(VE.getTypeID(FTy));
2138 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
2140 // Emit value #'s for the fixed parameters.
2141 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2142 // Check for labels (can happen with asm labels).
2143 if (FTy->getParamType(i)->isLabelTy())
2144 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
2146 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
2149 // Emit type/value pairs for varargs params.
2150 if (FTy->isVarArg()) {
2151 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
2153 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
2157 case Instruction::VAArg:
2158 Code = bitc::FUNC_CODE_INST_VAARG;
2159 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
2160 pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
2161 Vals.push_back(VE.getTypeID(I.getType())); // restype.
2165 Stream.EmitRecord(Code, Vals, AbbrevToUse);
2169 enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 };
2171 /// Determine the encoding to use for the given string name and length.
2172 static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) {
2173 bool isChar6 = true;
2174 for (const char *C = Str, *E = C + StrLen; C != E; ++C) {
2176 isChar6 = BitCodeAbbrevOp::isChar6(*C);
2177 if ((unsigned char)*C & 128)
2178 // don't bother scanning the rest.
2187 /// Emit names for globals/functions etc. The VSTOffsetPlaceholder,
2188 /// BitcodeStartBit and FunctionIndex are only passed for the module-level
2189 /// VST, where we are including a function bitcode index and need to
2190 /// backpatch the VST forward declaration record.
2191 static void WriteValueSymbolTable(
2192 const ValueSymbolTable &VST, const ValueEnumerator &VE,
2193 BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0,
2194 uint64_t BitcodeStartBit = 0,
2195 DenseMap<const Function *, std::unique_ptr<FunctionInfo>> *FunctionIndex =
2198 // WriteValueSymbolTableForwardDecl should have returned early as
2199 // well. Ensure this handling remains in sync by asserting that
2200 // the placeholder offset is not set.
2201 assert(VSTOffsetPlaceholder == 0);
2205 if (VSTOffsetPlaceholder > 0) {
2206 // Get the offset of the VST we are writing, and backpatch it into
2207 // the VST forward declaration record.
2208 uint64_t VSTOffset = Stream.GetCurrentBitNo();
2209 // The BitcodeStartBit was the stream offset of the actual bitcode
2210 // (e.g. excluding any initial darwin header).
2211 VSTOffset -= BitcodeStartBit;
2212 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned");
2213 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32);
2216 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2218 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY
2219 // records, which are not used in the per-function VSTs.
2220 unsigned FnEntry8BitAbbrev;
2221 unsigned FnEntry7BitAbbrev;
2222 unsigned FnEntry6BitAbbrev;
2223 if (VSTOffsetPlaceholder > 0) {
2224 // 8-bit fixed-width VST_FNENTRY function strings.
2225 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2226 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2227 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2228 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2229 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2230 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2231 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
2233 // 7-bit fixed width VST_FNENTRY function strings.
2234 Abbv = new BitCodeAbbrev();
2235 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2236 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2237 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2238 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2239 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2240 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
2242 // 6-bit char6 VST_FNENTRY function strings.
2243 Abbv = new BitCodeAbbrev();
2244 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY));
2245 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2246 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2247 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2248 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2249 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
2252 // FIXME: Set up the abbrev, we know how many values there are!
2253 // FIXME: We know if the type names can use 7-bit ascii.
2254 SmallVector<unsigned, 64> NameVals;
2256 for (const ValueName &Name : VST) {
2257 // Figure out the encoding to use for the name.
2258 StringEncoding Bits =
2259 getStringEncoding(Name.getKeyData(), Name.getKeyLength());
2261 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
2262 NameVals.push_back(VE.getValueID(Name.getValue()));
2264 Function *F = dyn_cast<Function>(Name.getValue());
2266 // If value is an alias, need to get the aliased base object to
2267 // see if it is a function.
2268 auto *GA = dyn_cast<GlobalAlias>(Name.getValue());
2269 if (GA && GA->getBaseObject())
2270 F = dyn_cast<Function>(GA->getBaseObject());
2273 // VST_ENTRY: [valueid, namechar x N]
2274 // VST_FNENTRY: [valueid, funcoffset, namechar x N]
2275 // VST_BBENTRY: [bbid, namechar x N]
2277 if (isa<BasicBlock>(Name.getValue())) {
2278 Code = bitc::VST_CODE_BBENTRY;
2279 if (Bits == SE_Char6)
2280 AbbrevToUse = VST_BBENTRY_6_ABBREV;
2281 } else if (F && !F->isDeclaration()) {
2282 // Must be the module-level VST, where we pass in the Index and
2283 // have a VSTOffsetPlaceholder. The function-level VST should not
2284 // contain any Function symbols.
2285 assert(FunctionIndex);
2286 assert(VSTOffsetPlaceholder > 0);
2288 // Save the word offset of the function (from the start of the
2289 // actual bitcode written to the stream).
2290 assert(FunctionIndex->count(F) == 1);
2291 uint64_t BitcodeIndex =
2292 (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit;
2293 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned");
2294 NameVals.push_back(BitcodeIndex / 32);
2296 Code = bitc::VST_CODE_FNENTRY;
2297 AbbrevToUse = FnEntry8BitAbbrev;
2298 if (Bits == SE_Char6)
2299 AbbrevToUse = FnEntry6BitAbbrev;
2300 else if (Bits == SE_Fixed7)
2301 AbbrevToUse = FnEntry7BitAbbrev;
2303 Code = bitc::VST_CODE_ENTRY;
2304 if (Bits == SE_Char6)
2305 AbbrevToUse = VST_ENTRY_6_ABBREV;
2306 else if (Bits == SE_Fixed7)
2307 AbbrevToUse = VST_ENTRY_7_ABBREV;
2310 for (const auto P : Name.getKey())
2311 NameVals.push_back((unsigned char)P);
2313 // Emit the finished record.
2314 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
2320 /// Emit function names and summary offsets for the combined index
2321 /// used by ThinLTO.
2322 static void WriteCombinedValueSymbolTable(const FunctionInfoIndex &Index,
2323 BitstreamWriter &Stream) {
2324 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
2326 // 8-bit fixed-width VST_COMBINED_FNENTRY function strings.
2327 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2328 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2329 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2330 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2331 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2332 unsigned FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv);
2334 // 7-bit fixed width VST_COMBINED_FNENTRY function strings.
2335 Abbv = new BitCodeAbbrev();
2336 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2340 unsigned FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv);
2342 // 6-bit char6 VST_COMBINED_FNENTRY function strings.
2343 Abbv = new BitCodeAbbrev();
2344 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_FNENTRY));
2345 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset
2346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2348 unsigned FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv);
2350 // FIXME: We know if the type names can use 7-bit ascii.
2351 SmallVector<unsigned, 64> NameVals;
2353 for (const auto &FII : Index) {
2354 for (const auto &FI : FII.getValue()) {
2355 NameVals.push_back(FI->bitcodeIndex());
2357 StringRef FuncName = FII.first();
2359 // Figure out the encoding to use for the name.
2360 StringEncoding Bits = getStringEncoding(FuncName.data(), FuncName.size());
2362 // VST_COMBINED_FNENTRY: [funcsumoffset, namechar x N]
2363 unsigned AbbrevToUse = FnEntry8BitAbbrev;
2364 if (Bits == SE_Char6)
2365 AbbrevToUse = FnEntry6BitAbbrev;
2366 else if (Bits == SE_Fixed7)
2367 AbbrevToUse = FnEntry7BitAbbrev;
2369 for (const auto P : FuncName)
2370 NameVals.push_back((unsigned char)P);
2372 // Emit the finished record.
2373 Stream.EmitRecord(bitc::VST_CODE_COMBINED_FNENTRY, NameVals, AbbrevToUse);
2380 static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order,
2381 BitstreamWriter &Stream) {
2382 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
2384 if (isa<BasicBlock>(Order.V))
2385 Code = bitc::USELIST_CODE_BB;
2387 Code = bitc::USELIST_CODE_DEFAULT;
2389 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end());
2390 Record.push_back(VE.getValueID(Order.V));
2391 Stream.EmitRecord(Code, Record);
2394 static void WriteUseListBlock(const Function *F, ValueEnumerator &VE,
2395 BitstreamWriter &Stream) {
2396 assert(VE.shouldPreserveUseListOrder() &&
2397 "Expected to be preserving use-list order");
2399 auto hasMore = [&]() {
2400 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F;
2406 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
2408 WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream);
2409 VE.UseListOrders.pop_back();
2414 /// \brief Save information for the given function into the function index.
2416 /// At a minimum this saves the bitcode index of the function record that
2417 /// was just written. However, if we are emitting function summary information,
2418 /// for example for ThinLTO, then a \a FunctionSummary object is created
2419 /// to hold the provided summary information.
2420 static void SaveFunctionInfo(
2422 DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2423 unsigned NumInsts, uint64_t BitcodeIndex, bool EmitFunctionSummary) {
2424 std::unique_ptr<FunctionSummary> FuncSummary;
2425 if (EmitFunctionSummary) {
2426 FuncSummary = llvm::make_unique<FunctionSummary>(NumInsts);
2427 FuncSummary->setLocalFunction(F.hasLocalLinkage());
2430 llvm::make_unique<FunctionInfo>(BitcodeIndex, std::move(FuncSummary));
2433 /// Emit a function body to the module stream.
2434 static void WriteFunction(
2435 const Function &F, ValueEnumerator &VE, BitstreamWriter &Stream,
2436 DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2437 bool EmitFunctionSummary) {
2438 // Save the bitcode index of the start of this function block for recording
2440 uint64_t BitcodeIndex = Stream.GetCurrentBitNo();
2442 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
2443 VE.incorporateFunction(F);
2445 SmallVector<unsigned, 64> Vals;
2447 // Emit the number of basic blocks, so the reader can create them ahead of
2449 Vals.push_back(VE.getBasicBlocks().size());
2450 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
2453 // If there are function-local constants, emit them now.
2454 unsigned CstStart, CstEnd;
2455 VE.getFunctionConstantRange(CstStart, CstEnd);
2456 WriteConstants(CstStart, CstEnd, VE, Stream, false);
2458 // If there is function-local metadata, emit it now.
2459 WriteFunctionLocalMetadata(F, VE, Stream);
2461 // Keep a running idea of what the instruction ID is.
2462 unsigned InstID = CstEnd;
2464 bool NeedsMetadataAttachment = F.hasMetadata();
2466 DILocation *LastDL = nullptr;
2467 unsigned NumInsts = 0;
2469 // Finally, emit all the instructions, in order.
2470 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
2471 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
2473 WriteInstruction(*I, InstID, VE, Stream, Vals);
2475 if (!isa<DbgInfoIntrinsic>(I))
2478 if (!I->getType()->isVoidTy())
2481 // If the instruction has metadata, write a metadata attachment later.
2482 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
2484 // If the instruction has a debug location, emit it.
2485 DILocation *DL = I->getDebugLoc();
2490 // Just repeat the same debug loc as last time.
2491 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
2495 Vals.push_back(DL->getLine());
2496 Vals.push_back(DL->getColumn());
2497 Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
2498 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
2499 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
2505 // Emit names for all the instructions etc.
2506 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
2508 if (NeedsMetadataAttachment)
2509 WriteMetadataAttachment(F, VE, Stream);
2510 if (VE.shouldPreserveUseListOrder())
2511 WriteUseListBlock(&F, VE, Stream);
2515 SaveFunctionInfo(F, FunctionIndex, NumInsts, BitcodeIndex,
2516 EmitFunctionSummary);
2519 // Emit blockinfo, which defines the standard abbreviations etc.
2520 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
2521 // We only want to emit block info records for blocks that have multiple
2522 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
2523 // Other blocks can define their abbrevs inline.
2524 Stream.EnterBlockInfoBlock(2);
2526 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
2527 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
2529 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2530 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2531 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2532 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2533 Abbv) != VST_ENTRY_8_ABBREV)
2534 llvm_unreachable("Unexpected abbrev ordering!");
2537 { // 7-bit fixed width VST_ENTRY strings.
2538 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2539 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2540 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2541 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2542 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2543 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2544 Abbv) != VST_ENTRY_7_ABBREV)
2545 llvm_unreachable("Unexpected abbrev ordering!");
2547 { // 6-bit char6 VST_ENTRY strings.
2548 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2549 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
2550 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2551 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2552 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2553 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2554 Abbv) != VST_ENTRY_6_ABBREV)
2555 llvm_unreachable("Unexpected abbrev ordering!");
2557 { // 6-bit char6 VST_BBENTRY strings.
2558 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2559 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
2560 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2561 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2562 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2563 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
2564 Abbv) != VST_BBENTRY_6_ABBREV)
2565 llvm_unreachable("Unexpected abbrev ordering!");
2570 { // SETTYPE abbrev for CONSTANTS_BLOCK.
2571 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2572 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
2573 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
2574 VE.computeBitsRequiredForTypeIndicies()));
2575 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2576 Abbv) != CONSTANTS_SETTYPE_ABBREV)
2577 llvm_unreachable("Unexpected abbrev ordering!");
2580 { // INTEGER abbrev for CONSTANTS_BLOCK.
2581 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2582 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
2583 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2584 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2585 Abbv) != CONSTANTS_INTEGER_ABBREV)
2586 llvm_unreachable("Unexpected abbrev ordering!");
2589 { // CE_CAST abbrev for CONSTANTS_BLOCK.
2590 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2591 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
2592 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
2593 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
2594 VE.computeBitsRequiredForTypeIndicies()));
2595 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
2597 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2598 Abbv) != CONSTANTS_CE_CAST_Abbrev)
2599 llvm_unreachable("Unexpected abbrev ordering!");
2601 { // NULL abbrev for CONSTANTS_BLOCK.
2602 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2603 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
2604 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
2605 Abbv) != CONSTANTS_NULL_Abbrev)
2606 llvm_unreachable("Unexpected abbrev ordering!");
2609 // FIXME: This should only use space for first class types!
2611 { // INST_LOAD abbrev for FUNCTION_BLOCK.
2612 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2613 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
2614 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
2615 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2616 VE.computeBitsRequiredForTypeIndicies()));
2617 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
2618 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
2619 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2620 Abbv) != FUNCTION_INST_LOAD_ABBREV)
2621 llvm_unreachable("Unexpected abbrev ordering!");
2623 { // INST_BINOP abbrev for FUNCTION_BLOCK.
2624 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2625 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2626 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2627 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2628 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2629 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2630 Abbv) != FUNCTION_INST_BINOP_ABBREV)
2631 llvm_unreachable("Unexpected abbrev ordering!");
2633 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
2634 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2635 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
2636 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
2637 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
2638 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2639 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
2640 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2641 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
2642 llvm_unreachable("Unexpected abbrev ordering!");
2644 { // INST_CAST abbrev for FUNCTION_BLOCK.
2645 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2646 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
2647 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
2648 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2649 VE.computeBitsRequiredForTypeIndicies()));
2650 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
2651 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2652 Abbv) != FUNCTION_INST_CAST_ABBREV)
2653 llvm_unreachable("Unexpected abbrev ordering!");
2656 { // INST_RET abbrev for FUNCTION_BLOCK.
2657 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2658 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2659 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2660 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
2661 llvm_unreachable("Unexpected abbrev ordering!");
2663 { // INST_RET abbrev for FUNCTION_BLOCK.
2664 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2665 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
2666 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
2667 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2668 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
2669 llvm_unreachable("Unexpected abbrev ordering!");
2671 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
2672 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2673 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
2674 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
2675 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
2676 llvm_unreachable("Unexpected abbrev ordering!");
2679 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2680 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP));
2681 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2682 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
2683 Log2_32_Ceil(VE.getTypes().size() + 1)));
2684 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2685 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2686 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) !=
2687 FUNCTION_INST_GEP_ABBREV)
2688 llvm_unreachable("Unexpected abbrev ordering!");
2694 /// Write the module path strings, currently only used when generating
2695 /// a combined index file.
2696 static void WriteModStrings(const FunctionInfoIndex &I,
2697 BitstreamWriter &Stream) {
2698 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
2700 // TODO: See which abbrev sizes we actually need to emit
2702 // 8-bit fixed-width MST_ENTRY strings.
2703 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2704 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2707 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
2708 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
2710 // 7-bit fixed width MST_ENTRY strings.
2711 Abbv = new BitCodeAbbrev();
2712 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2713 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
2716 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
2718 // 6-bit char6 MST_ENTRY strings.
2719 Abbv = new BitCodeAbbrev();
2720 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
2721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
2722 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2723 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2724 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
2726 SmallVector<unsigned, 64> NameVals;
2727 for (const StringMapEntry<uint64_t> &MPSE : I.modPathStringEntries()) {
2728 StringEncoding Bits =
2729 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
2730 unsigned AbbrevToUse = Abbrev8Bit;
2731 if (Bits == SE_Char6)
2732 AbbrevToUse = Abbrev6Bit;
2733 else if (Bits == SE_Fixed7)
2734 AbbrevToUse = Abbrev7Bit;
2736 NameVals.push_back(MPSE.getValue());
2738 for (const auto P : MPSE.getKey())
2739 NameVals.push_back((unsigned char)P);
2741 // Emit the finished record.
2742 Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
2748 // Helper to emit a single function summary record.
2749 static void WritePerModuleFunctionSummaryRecord(
2750 SmallVector<unsigned, 64> &NameVals, FunctionSummary *FS, unsigned ValueID,
2751 unsigned FSAbbrev, BitstreamWriter &Stream) {
2753 NameVals.push_back(ValueID);
2754 NameVals.push_back(FS->isLocalFunction());
2755 NameVals.push_back(FS->instCount());
2757 // Emit the finished record.
2758 Stream.EmitRecord(bitc::FS_CODE_PERMODULE_ENTRY, NameVals, FSAbbrev);
2762 /// Emit the per-module function summary section alongside the rest of
2763 /// the module's bitcode.
2764 static void WritePerModuleFunctionSummary(
2765 DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
2766 const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
2767 Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2769 // Abbrev for FS_CODE_PERMODULE_ENTRY.
2770 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2771 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_PERMODULE_ENTRY));
2772 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
2773 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // islocal
2774 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
2775 unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2777 SmallVector<unsigned, 64> NameVals;
2778 for (auto &I : FunctionIndex) {
2779 // Skip anonymous functions. We will emit a function summary for
2780 // any aliases below.
2781 if (!I.first->hasName())
2784 WritePerModuleFunctionSummaryRecord(
2785 NameVals, I.second->functionSummary(),
2786 VE.getValueID(M->getValueSymbolTable().lookup(I.first->getName())),
2790 for (const GlobalAlias &A : M->aliases()) {
2791 if (!A.getBaseObject())
2793 const Function *F = dyn_cast<Function>(A.getBaseObject());
2794 if (!F || F->isDeclaration())
2797 assert(FunctionIndex.count(F) == 1);
2798 WritePerModuleFunctionSummaryRecord(
2799 NameVals, FunctionIndex[F]->functionSummary(),
2800 VE.getValueID(M->getValueSymbolTable().lookup(A.getName())), FSAbbrev,
2807 /// Emit the combined function summary section into the combined index
2809 static void WriteCombinedFunctionSummary(const FunctionInfoIndex &I,
2810 BitstreamWriter &Stream) {
2811 Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
2813 // Abbrev for FS_CODE_COMBINED_ENTRY.
2814 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2815 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_COMBINED_ENTRY));
2816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
2817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
2818 unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
2820 SmallVector<unsigned, 64> NameVals;
2821 for (const auto &FII : I) {
2822 for (auto &FI : FII.getValue()) {
2823 FunctionSummary *FS = FI->functionSummary();
2826 NameVals.push_back(I.getModuleId(FS->modulePath()));
2827 NameVals.push_back(FS->instCount());
2829 // Record the starting offset of this summary entry for use
2830 // in the VST entry. Add the current code size since the
2831 // reader will invoke readRecord after the abbrev id read.
2832 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
2834 // Emit the finished record.
2835 Stream.EmitRecord(bitc::FS_CODE_COMBINED_ENTRY, NameVals, FSAbbrev);
2843 // Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
2844 // current llvm version, and a record for the epoch number.
2845 static void WriteIdentificationBlock(const Module *M, BitstreamWriter &Stream) {
2846 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
2848 // Write the "user readable" string identifying the bitcode producer
2849 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
2850 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
2851 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2852 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
2853 auto StringAbbrev = Stream.EmitAbbrev(Abbv);
2854 WriteStringRecord(bitc::IDENTIFICATION_CODE_STRING,
2855 "LLVM" LLVM_VERSION_STRING, StringAbbrev, Stream);
2857 // Write the epoch version
2858 Abbv = new BitCodeAbbrev();
2859 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
2860 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2861 auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
2862 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
2863 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
2867 /// WriteModule - Emit the specified module to the bitstream.
2868 static void WriteModule(const Module *M, BitstreamWriter &Stream,
2869 bool ShouldPreserveUseListOrder,
2870 uint64_t BitcodeStartBit, bool EmitFunctionSummary) {
2871 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
2873 SmallVector<unsigned, 1> Vals;
2874 unsigned CurVersion = 1;
2875 Vals.push_back(CurVersion);
2876 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
2878 // Analyze the module, enumerating globals, functions, etc.
2879 ValueEnumerator VE(*M, ShouldPreserveUseListOrder);
2881 // Emit blockinfo, which defines the standard abbreviations etc.
2882 WriteBlockInfo(VE, Stream);
2884 // Emit information about attribute groups.
2885 WriteAttributeGroupTable(VE, Stream);
2887 // Emit information about parameter attributes.
2888 WriteAttributeTable(VE, Stream);
2890 // Emit information describing all of the types in the module.
2891 WriteTypeTable(VE, Stream);
2893 writeComdats(VE, Stream);
2895 // Emit top-level description of module, including target triple, inline asm,
2896 // descriptors for global variables, and function prototype info.
2897 uint64_t VSTOffsetPlaceholder = WriteModuleInfo(M, VE, Stream);
2900 WriteModuleConstants(VE, Stream);
2903 WriteModuleMetadata(M, VE, Stream);
2906 WriteModuleMetadataStore(M, Stream);
2908 // Emit module-level use-lists.
2909 if (VE.shouldPreserveUseListOrder())
2910 WriteUseListBlock(nullptr, VE, Stream);
2912 WriteOperandBundleTags(M, Stream);
2914 // Emit function bodies.
2915 DenseMap<const Function *, std::unique_ptr<FunctionInfo>> FunctionIndex;
2916 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
2917 if (!F->isDeclaration())
2918 WriteFunction(*F, VE, Stream, FunctionIndex, EmitFunctionSummary);
2920 // Need to write after the above call to WriteFunction which populates
2921 // the summary information in the index.
2922 if (EmitFunctionSummary)
2923 WritePerModuleFunctionSummary(FunctionIndex, M, VE, Stream);
2925 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream,
2926 VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex);
2931 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
2932 /// header and trailer to make it compatible with the system archiver. To do
2933 /// this we emit the following header, and then emit a trailer that pads the
2934 /// file out to be a multiple of 16 bytes.
2936 /// struct bc_header {
2937 /// uint32_t Magic; // 0x0B17C0DE
2938 /// uint32_t Version; // Version, currently always 0.
2939 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
2940 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
2941 /// uint32_t CPUType; // CPU specifier.
2942 /// ... potentially more later ...
2945 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
2946 DarwinBCHeaderSize = 5*4
2949 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
2950 uint32_t &Position) {
2951 support::endian::write32le(&Buffer[Position], Value);
2955 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
2957 unsigned CPUType = ~0U;
2959 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
2960 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
2961 // number from /usr/include/mach/machine.h. It is ok to reproduce the
2962 // specific constants here because they are implicitly part of the Darwin ABI.
2964 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
2965 DARWIN_CPU_TYPE_X86 = 7,
2966 DARWIN_CPU_TYPE_ARM = 12,
2967 DARWIN_CPU_TYPE_POWERPC = 18
2970 Triple::ArchType Arch = TT.getArch();
2971 if (Arch == Triple::x86_64)
2972 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
2973 else if (Arch == Triple::x86)
2974 CPUType = DARWIN_CPU_TYPE_X86;
2975 else if (Arch == Triple::ppc)
2976 CPUType = DARWIN_CPU_TYPE_POWERPC;
2977 else if (Arch == Triple::ppc64)
2978 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
2979 else if (Arch == Triple::arm || Arch == Triple::thumb)
2980 CPUType = DARWIN_CPU_TYPE_ARM;
2982 // Traditional Bitcode starts after header.
2983 assert(Buffer.size() >= DarwinBCHeaderSize &&
2984 "Expected header size to be reserved");
2985 unsigned BCOffset = DarwinBCHeaderSize;
2986 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
2988 // Write the magic and version.
2989 unsigned Position = 0;
2990 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
2991 WriteInt32ToBuffer(0 , Buffer, Position); // Version.
2992 WriteInt32ToBuffer(BCOffset , Buffer, Position);
2993 WriteInt32ToBuffer(BCSize , Buffer, Position);
2994 WriteInt32ToBuffer(CPUType , Buffer, Position);
2996 // If the file is not a multiple of 16 bytes, insert dummy padding.
2997 while (Buffer.size() & 15)
2998 Buffer.push_back(0);
3001 /// Helper to write the header common to all bitcode files.
3002 static void WriteBitcodeHeader(BitstreamWriter &Stream) {
3003 // Emit the file header.
3004 Stream.Emit((unsigned)'B', 8);
3005 Stream.Emit((unsigned)'C', 8);
3006 Stream.Emit(0x0, 4);
3007 Stream.Emit(0xC, 4);
3008 Stream.Emit(0xE, 4);
3009 Stream.Emit(0xD, 4);
3012 /// WriteBitcodeToFile - Write the specified module to the specified output
3014 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out,
3015 bool ShouldPreserveUseListOrder,
3016 bool EmitFunctionSummary) {
3017 SmallVector<char, 0> Buffer;
3018 Buffer.reserve(256*1024);
3020 // If this is darwin or another generic macho target, reserve space for the
3022 Triple TT(M->getTargetTriple());
3023 if (TT.isOSDarwin())
3024 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
3026 // Emit the module into the buffer.
3028 BitstreamWriter Stream(Buffer);
3029 // Save the start bit of the actual bitcode, in case there is space
3030 // saved at the start for the darwin header above. The reader stream
3031 // will start at the bitcode, and we need the offset of the VST
3033 uint64_t BitcodeStartBit = Stream.GetCurrentBitNo();
3035 // Emit the file header.
3036 WriteBitcodeHeader(Stream);
3038 WriteIdentificationBlock(M, Stream);
3041 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit,
3042 EmitFunctionSummary);
3045 if (TT.isOSDarwin())
3046 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
3048 // Write the generated bitstream to "Out".
3049 Out.write((char*)&Buffer.front(), Buffer.size());
3052 // Write the specified function summary index to the given raw output stream,
3053 // where it will be written in a new bitcode block. This is used when
3054 // writing the combined index file for ThinLTO.
3055 void llvm::WriteFunctionSummaryToFile(const FunctionInfoIndex &Index,
3057 SmallVector<char, 0> Buffer;
3058 Buffer.reserve(256 * 1024);
3060 BitstreamWriter Stream(Buffer);
3062 // Emit the bitcode header.
3063 WriteBitcodeHeader(Stream);
3065 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
3067 SmallVector<unsigned, 1> Vals;
3068 unsigned CurVersion = 1;
3069 Vals.push_back(CurVersion);
3070 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
3072 // Write the module paths in the combined index.
3073 WriteModStrings(Index, Stream);
3075 // Write the function summary combined index records.
3076 WriteCombinedFunctionSummary(Index, Stream);
3078 // Need a special VST writer for the combined index (we don't have a
3079 // real VST and real values when this is invoked).
3080 WriteCombinedValueSymbolTable(Index, Stream);
3084 Out.write((char *)&Buffer.front(), Buffer.size());