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