1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "ValueEnumerator.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Bitcode/BitstreamWriter.h"
18 #include "llvm/Bitcode/LLVMBitCodes.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/InlineAsm.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/ValueSymbolTable.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MathExtras.h"
29 #include "llvm/Support/Program.h"
30 #include "llvm/Support/raw_ostream.h"
36 EnablePreserveUseListOrdering("enable-bc-uselist-preserve",
37 cl::desc("Turn on experimental support for "
38 "use-list order preservation."),
39 cl::init(false), cl::Hidden);
41 /// These are manifest constants used by the bitcode writer. They do not need to
42 /// be kept in sync with the reader, but need to be consistent within this file.
44 // VALUE_SYMTAB_BLOCK abbrev id's.
45 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
50 // CONSTANTS_BLOCK abbrev id's.
51 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
52 CONSTANTS_INTEGER_ABBREV,
53 CONSTANTS_CE_CAST_Abbrev,
54 CONSTANTS_NULL_Abbrev,
56 // FUNCTION_BLOCK abbrev id's.
57 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
58 FUNCTION_INST_BINOP_ABBREV,
59 FUNCTION_INST_BINOP_FLAGS_ABBREV,
60 FUNCTION_INST_CAST_ABBREV,
61 FUNCTION_INST_RET_VOID_ABBREV,
62 FUNCTION_INST_RET_VAL_ABBREV,
63 FUNCTION_INST_UNREACHABLE_ABBREV
66 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
68 default: llvm_unreachable("Unknown cast instruction!");
69 case Instruction::Trunc : return bitc::CAST_TRUNC;
70 case Instruction::ZExt : return bitc::CAST_ZEXT;
71 case Instruction::SExt : return bitc::CAST_SEXT;
72 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
73 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
74 case Instruction::UIToFP : return bitc::CAST_UITOFP;
75 case Instruction::SIToFP : return bitc::CAST_SITOFP;
76 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
77 case Instruction::FPExt : return bitc::CAST_FPEXT;
78 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
79 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
80 case Instruction::BitCast : return bitc::CAST_BITCAST;
81 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST;
85 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
87 default: llvm_unreachable("Unknown binary instruction!");
88 case Instruction::Add:
89 case Instruction::FAdd: return bitc::BINOP_ADD;
90 case Instruction::Sub:
91 case Instruction::FSub: return bitc::BINOP_SUB;
92 case Instruction::Mul:
93 case Instruction::FMul: return bitc::BINOP_MUL;
94 case Instruction::UDiv: return bitc::BINOP_UDIV;
95 case Instruction::FDiv:
96 case Instruction::SDiv: return bitc::BINOP_SDIV;
97 case Instruction::URem: return bitc::BINOP_UREM;
98 case Instruction::FRem:
99 case Instruction::SRem: return bitc::BINOP_SREM;
100 case Instruction::Shl: return bitc::BINOP_SHL;
101 case Instruction::LShr: return bitc::BINOP_LSHR;
102 case Instruction::AShr: return bitc::BINOP_ASHR;
103 case Instruction::And: return bitc::BINOP_AND;
104 case Instruction::Or: return bitc::BINOP_OR;
105 case Instruction::Xor: return bitc::BINOP_XOR;
109 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
111 default: llvm_unreachable("Unknown RMW operation!");
112 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
113 case AtomicRMWInst::Add: return bitc::RMW_ADD;
114 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
115 case AtomicRMWInst::And: return bitc::RMW_AND;
116 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
117 case AtomicRMWInst::Or: return bitc::RMW_OR;
118 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
119 case AtomicRMWInst::Max: return bitc::RMW_MAX;
120 case AtomicRMWInst::Min: return bitc::RMW_MIN;
121 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
122 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
126 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
128 case NotAtomic: return bitc::ORDERING_NOTATOMIC;
129 case Unordered: return bitc::ORDERING_UNORDERED;
130 case Monotonic: return bitc::ORDERING_MONOTONIC;
131 case Acquire: return bitc::ORDERING_ACQUIRE;
132 case Release: return bitc::ORDERING_RELEASE;
133 case AcquireRelease: return bitc::ORDERING_ACQREL;
134 case SequentiallyConsistent: return bitc::ORDERING_SEQCST;
136 llvm_unreachable("Invalid ordering");
139 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
140 switch (SynchScope) {
141 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
142 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
144 llvm_unreachable("Invalid synch scope");
147 static void WriteStringRecord(unsigned Code, StringRef Str,
148 unsigned AbbrevToUse, BitstreamWriter &Stream) {
149 SmallVector<unsigned, 64> Vals;
151 // Code: [strchar x N]
152 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
153 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
155 Vals.push_back(Str[i]);
158 // Emit the finished record.
159 Stream.EmitRecord(Code, Vals, AbbrevToUse);
162 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
164 case Attribute::Alignment:
165 return bitc::ATTR_KIND_ALIGNMENT;
166 case Attribute::AlwaysInline:
167 return bitc::ATTR_KIND_ALWAYS_INLINE;
168 case Attribute::Builtin:
169 return bitc::ATTR_KIND_BUILTIN;
170 case Attribute::ByVal:
171 return bitc::ATTR_KIND_BY_VAL;
172 case Attribute::InAlloca:
173 return bitc::ATTR_KIND_IN_ALLOCA;
174 case Attribute::Cold:
175 return bitc::ATTR_KIND_COLD;
176 case Attribute::InlineHint:
177 return bitc::ATTR_KIND_INLINE_HINT;
178 case Attribute::InReg:
179 return bitc::ATTR_KIND_IN_REG;
180 case Attribute::JumpTable:
181 return bitc::ATTR_KIND_JUMP_TABLE;
182 case Attribute::MinSize:
183 return bitc::ATTR_KIND_MIN_SIZE;
184 case Attribute::Naked:
185 return bitc::ATTR_KIND_NAKED;
186 case Attribute::Nest:
187 return bitc::ATTR_KIND_NEST;
188 case Attribute::NoAlias:
189 return bitc::ATTR_KIND_NO_ALIAS;
190 case Attribute::NoBuiltin:
191 return bitc::ATTR_KIND_NO_BUILTIN;
192 case Attribute::NoCapture:
193 return bitc::ATTR_KIND_NO_CAPTURE;
194 case Attribute::NoDuplicate:
195 return bitc::ATTR_KIND_NO_DUPLICATE;
196 case Attribute::NoImplicitFloat:
197 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT;
198 case Attribute::NoInline:
199 return bitc::ATTR_KIND_NO_INLINE;
200 case Attribute::NonLazyBind:
201 return bitc::ATTR_KIND_NON_LAZY_BIND;
202 case Attribute::NonNull:
203 return bitc::ATTR_KIND_NON_NULL;
204 case Attribute::Dereferenceable:
205 return bitc::ATTR_KIND_DEREFERENCEABLE;
206 case Attribute::NoRedZone:
207 return bitc::ATTR_KIND_NO_RED_ZONE;
208 case Attribute::NoReturn:
209 return bitc::ATTR_KIND_NO_RETURN;
210 case Attribute::NoUnwind:
211 return bitc::ATTR_KIND_NO_UNWIND;
212 case Attribute::OptimizeForSize:
213 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
214 case Attribute::OptimizeNone:
215 return bitc::ATTR_KIND_OPTIMIZE_NONE;
216 case Attribute::ReadNone:
217 return bitc::ATTR_KIND_READ_NONE;
218 case Attribute::ReadOnly:
219 return bitc::ATTR_KIND_READ_ONLY;
220 case Attribute::Returned:
221 return bitc::ATTR_KIND_RETURNED;
222 case Attribute::ReturnsTwice:
223 return bitc::ATTR_KIND_RETURNS_TWICE;
224 case Attribute::SExt:
225 return bitc::ATTR_KIND_S_EXT;
226 case Attribute::StackAlignment:
227 return bitc::ATTR_KIND_STACK_ALIGNMENT;
228 case Attribute::StackProtect:
229 return bitc::ATTR_KIND_STACK_PROTECT;
230 case Attribute::StackProtectReq:
231 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
232 case Attribute::StackProtectStrong:
233 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
234 case Attribute::StructRet:
235 return bitc::ATTR_KIND_STRUCT_RET;
236 case Attribute::SanitizeAddress:
237 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
238 case Attribute::SanitizeThread:
239 return bitc::ATTR_KIND_SANITIZE_THREAD;
240 case Attribute::SanitizeMemory:
241 return bitc::ATTR_KIND_SANITIZE_MEMORY;
242 case Attribute::UWTable:
243 return bitc::ATTR_KIND_UW_TABLE;
244 case Attribute::ZExt:
245 return bitc::ATTR_KIND_Z_EXT;
246 case Attribute::EndAttrKinds:
247 llvm_unreachable("Can not encode end-attribute kinds marker.");
248 case Attribute::None:
249 llvm_unreachable("Can not encode none-attribute.");
252 llvm_unreachable("Trying to encode unknown attribute");
255 static void WriteAttributeGroupTable(const ValueEnumerator &VE,
256 BitstreamWriter &Stream) {
257 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
258 if (AttrGrps.empty()) return;
260 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
262 SmallVector<uint64_t, 64> Record;
263 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
264 AttributeSet AS = AttrGrps[i];
265 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
266 AttributeSet A = AS.getSlotAttributes(i);
268 Record.push_back(VE.getAttributeGroupID(A));
269 Record.push_back(AS.getSlotIndex(i));
271 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
274 if (Attr.isEnumAttribute()) {
276 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
277 } else if (Attr.isIntAttribute()) {
279 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
280 Record.push_back(Attr.getValueAsInt());
282 StringRef Kind = Attr.getKindAsString();
283 StringRef Val = Attr.getValueAsString();
285 Record.push_back(Val.empty() ? 3 : 4);
286 Record.append(Kind.begin(), Kind.end());
289 Record.append(Val.begin(), Val.end());
295 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
303 static void WriteAttributeTable(const ValueEnumerator &VE,
304 BitstreamWriter &Stream) {
305 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
306 if (Attrs.empty()) return;
308 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
310 SmallVector<uint64_t, 64> Record;
311 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
312 const AttributeSet &A = Attrs[i];
313 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
314 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
316 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
323 /// WriteTypeTable - Write out the type table for a module.
324 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
325 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
327 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
328 SmallVector<uint64_t, 64> TypeVals;
330 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
332 // Abbrev for TYPE_CODE_POINTER.
333 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
334 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
335 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
336 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
337 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
339 // Abbrev for TYPE_CODE_FUNCTION.
340 Abbv = new BitCodeAbbrev();
341 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
342 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
343 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
344 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
346 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
348 // Abbrev for TYPE_CODE_STRUCT_ANON.
349 Abbv = new BitCodeAbbrev();
350 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
355 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
357 // Abbrev for TYPE_CODE_STRUCT_NAME.
358 Abbv = new BitCodeAbbrev();
359 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
360 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
361 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
362 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
364 // Abbrev for TYPE_CODE_STRUCT_NAMED.
365 Abbv = new BitCodeAbbrev();
366 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
367 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
368 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
371 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
373 // Abbrev for TYPE_CODE_ARRAY.
374 Abbv = new BitCodeAbbrev();
375 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
376 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
377 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
379 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
381 // Emit an entry count so the reader can reserve space.
382 TypeVals.push_back(TypeList.size());
383 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
386 // Loop over all of the types, emitting each in turn.
387 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
388 Type *T = TypeList[i];
392 switch (T->getTypeID()) {
393 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
394 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
395 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
396 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
397 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
398 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
399 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
400 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
401 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
402 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
403 case Type::IntegerTyID:
405 Code = bitc::TYPE_CODE_INTEGER;
406 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
408 case Type::PointerTyID: {
409 PointerType *PTy = cast<PointerType>(T);
410 // POINTER: [pointee type, address space]
411 Code = bitc::TYPE_CODE_POINTER;
412 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
413 unsigned AddressSpace = PTy->getAddressSpace();
414 TypeVals.push_back(AddressSpace);
415 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
418 case Type::FunctionTyID: {
419 FunctionType *FT = cast<FunctionType>(T);
420 // FUNCTION: [isvararg, retty, paramty x N]
421 Code = bitc::TYPE_CODE_FUNCTION;
422 TypeVals.push_back(FT->isVarArg());
423 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
424 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
425 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
426 AbbrevToUse = FunctionAbbrev;
429 case Type::StructTyID: {
430 StructType *ST = cast<StructType>(T);
431 // STRUCT: [ispacked, eltty x N]
432 TypeVals.push_back(ST->isPacked());
433 // Output all of the element types.
434 for (StructType::element_iterator I = ST->element_begin(),
435 E = ST->element_end(); I != E; ++I)
436 TypeVals.push_back(VE.getTypeID(*I));
438 if (ST->isLiteral()) {
439 Code = bitc::TYPE_CODE_STRUCT_ANON;
440 AbbrevToUse = StructAnonAbbrev;
442 if (ST->isOpaque()) {
443 Code = bitc::TYPE_CODE_OPAQUE;
445 Code = bitc::TYPE_CODE_STRUCT_NAMED;
446 AbbrevToUse = StructNamedAbbrev;
449 // Emit the name if it is present.
450 if (!ST->getName().empty())
451 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
452 StructNameAbbrev, Stream);
456 case Type::ArrayTyID: {
457 ArrayType *AT = cast<ArrayType>(T);
458 // ARRAY: [numelts, eltty]
459 Code = bitc::TYPE_CODE_ARRAY;
460 TypeVals.push_back(AT->getNumElements());
461 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
462 AbbrevToUse = ArrayAbbrev;
465 case Type::VectorTyID: {
466 VectorType *VT = cast<VectorType>(T);
467 // VECTOR [numelts, eltty]
468 Code = bitc::TYPE_CODE_VECTOR;
469 TypeVals.push_back(VT->getNumElements());
470 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
475 // Emit the finished record.
476 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
483 static unsigned getEncodedLinkage(const GlobalValue &GV) {
484 switch (GV.getLinkage()) {
485 case GlobalValue::ExternalLinkage: return 0;
486 case GlobalValue::WeakAnyLinkage: return 1;
487 case GlobalValue::AppendingLinkage: return 2;
488 case GlobalValue::InternalLinkage: return 3;
489 case GlobalValue::LinkOnceAnyLinkage: return 4;
490 case GlobalValue::ExternalWeakLinkage: return 7;
491 case GlobalValue::CommonLinkage: return 8;
492 case GlobalValue::PrivateLinkage: return 9;
493 case GlobalValue::WeakODRLinkage: return 10;
494 case GlobalValue::LinkOnceODRLinkage: return 11;
495 case GlobalValue::AvailableExternallyLinkage: return 12;
497 llvm_unreachable("Invalid linkage");
500 static unsigned getEncodedVisibility(const GlobalValue &GV) {
501 switch (GV.getVisibility()) {
502 case GlobalValue::DefaultVisibility: return 0;
503 case GlobalValue::HiddenVisibility: return 1;
504 case GlobalValue::ProtectedVisibility: return 2;
506 llvm_unreachable("Invalid visibility");
509 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
510 switch (GV.getDLLStorageClass()) {
511 case GlobalValue::DefaultStorageClass: return 0;
512 case GlobalValue::DLLImportStorageClass: return 1;
513 case GlobalValue::DLLExportStorageClass: return 2;
515 llvm_unreachable("Invalid DLL storage class");
518 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
519 switch (GV.getThreadLocalMode()) {
520 case GlobalVariable::NotThreadLocal: return 0;
521 case GlobalVariable::GeneralDynamicTLSModel: return 1;
522 case GlobalVariable::LocalDynamicTLSModel: return 2;
523 case GlobalVariable::InitialExecTLSModel: return 3;
524 case GlobalVariable::LocalExecTLSModel: return 4;
526 llvm_unreachable("Invalid TLS model");
529 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
530 switch (C.getSelectionKind()) {
532 return bitc::COMDAT_SELECTION_KIND_ANY;
533 case Comdat::ExactMatch:
534 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
535 case Comdat::Largest:
536 return bitc::COMDAT_SELECTION_KIND_LARGEST;
537 case Comdat::NoDuplicates:
538 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
539 case Comdat::SameSize:
540 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
542 llvm_unreachable("Invalid selection kind");
545 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
546 SmallVector<uint8_t, 64> Vals;
547 for (const Comdat *C : VE.getComdats()) {
548 // COMDAT: [selection_kind, name]
549 Vals.push_back(getEncodedComdatSelectionKind(*C));
550 Vals.push_back(C->getName().size());
551 for (char Chr : C->getName())
552 Vals.push_back((unsigned char)Chr);
553 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
558 // Emit top-level description of module, including target triple, inline asm,
559 // descriptors for global variables, and function prototype info.
560 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
561 BitstreamWriter &Stream) {
562 // Emit various pieces of data attached to a module.
563 if (!M->getTargetTriple().empty())
564 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
566 const std::string &DL = M->getDataLayoutStr();
568 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
569 if (!M->getModuleInlineAsm().empty())
570 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
573 // Emit information about sections and GC, computing how many there are. Also
574 // compute the maximum alignment value.
575 std::map<std::string, unsigned> SectionMap;
576 std::map<std::string, unsigned> GCMap;
577 unsigned MaxAlignment = 0;
578 unsigned MaxGlobalType = 0;
579 for (const GlobalValue &GV : M->globals()) {
580 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
581 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
582 if (GV.hasSection()) {
583 // Give section names unique ID's.
584 unsigned &Entry = SectionMap[GV.getSection()];
586 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
588 Entry = SectionMap.size();
592 for (const Function &F : *M) {
593 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
594 if (F.hasSection()) {
595 // Give section names unique ID's.
596 unsigned &Entry = SectionMap[F.getSection()];
598 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
600 Entry = SectionMap.size();
604 // Same for GC names.
605 unsigned &Entry = GCMap[F.getGC()];
607 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
609 Entry = GCMap.size();
614 // Emit abbrev for globals, now that we know # sections and max alignment.
615 unsigned SimpleGVarAbbrev = 0;
616 if (!M->global_empty()) {
617 // Add an abbrev for common globals with no visibility or thread localness.
618 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
619 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
620 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
621 Log2_32_Ceil(MaxGlobalType+1)));
622 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
623 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
624 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
625 if (MaxAlignment == 0) // Alignment.
626 Abbv->Add(BitCodeAbbrevOp(0));
628 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
629 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
630 Log2_32_Ceil(MaxEncAlignment+1)));
632 if (SectionMap.empty()) // Section.
633 Abbv->Add(BitCodeAbbrevOp(0));
635 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
636 Log2_32_Ceil(SectionMap.size()+1)));
637 // Don't bother emitting vis + thread local.
638 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
641 // Emit the global variable information.
642 SmallVector<unsigned, 64> Vals;
643 for (const GlobalVariable &GV : M->globals()) {
644 unsigned AbbrevToUse = 0;
646 // GLOBALVAR: [type, isconst, initid,
647 // linkage, alignment, section, visibility, threadlocal,
648 // unnamed_addr, externally_initialized, dllstorageclass]
649 Vals.push_back(VE.getTypeID(GV.getType()));
650 Vals.push_back(GV.isConstant());
651 Vals.push_back(GV.isDeclaration() ? 0 :
652 (VE.getValueID(GV.getInitializer()) + 1));
653 Vals.push_back(getEncodedLinkage(GV));
654 Vals.push_back(Log2_32(GV.getAlignment())+1);
655 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
656 if (GV.isThreadLocal() ||
657 GV.getVisibility() != GlobalValue::DefaultVisibility ||
658 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
659 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
661 Vals.push_back(getEncodedVisibility(GV));
662 Vals.push_back(getEncodedThreadLocalMode(GV));
663 Vals.push_back(GV.hasUnnamedAddr());
664 Vals.push_back(GV.isExternallyInitialized());
665 Vals.push_back(getEncodedDLLStorageClass(GV));
666 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
668 AbbrevToUse = SimpleGVarAbbrev;
671 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
675 // Emit the function proto information.
676 for (const Function &F : *M) {
677 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
678 // section, visibility, gc, unnamed_addr, prefix]
679 Vals.push_back(VE.getTypeID(F.getType()));
680 Vals.push_back(F.getCallingConv());
681 Vals.push_back(F.isDeclaration());
682 Vals.push_back(getEncodedLinkage(F));
683 Vals.push_back(VE.getAttributeID(F.getAttributes()));
684 Vals.push_back(Log2_32(F.getAlignment())+1);
685 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
686 Vals.push_back(getEncodedVisibility(F));
687 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
688 Vals.push_back(F.hasUnnamedAddr());
689 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
691 Vals.push_back(getEncodedDLLStorageClass(F));
692 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
694 unsigned AbbrevToUse = 0;
695 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
699 // Emit the alias information.
700 for (const GlobalAlias &A : M->aliases()) {
701 // ALIAS: [alias type, aliasee val#, linkage, visibility]
702 Vals.push_back(VE.getTypeID(A.getType()));
703 Vals.push_back(VE.getValueID(A.getAliasee()));
704 Vals.push_back(getEncodedLinkage(A));
705 Vals.push_back(getEncodedVisibility(A));
706 Vals.push_back(getEncodedDLLStorageClass(A));
707 Vals.push_back(getEncodedThreadLocalMode(A));
708 Vals.push_back(A.hasUnnamedAddr());
709 unsigned AbbrevToUse = 0;
710 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
715 static uint64_t GetOptimizationFlags(const Value *V) {
718 if (const OverflowingBinaryOperator *OBO =
719 dyn_cast<OverflowingBinaryOperator>(V)) {
720 if (OBO->hasNoSignedWrap())
721 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
722 if (OBO->hasNoUnsignedWrap())
723 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
724 } else if (const PossiblyExactOperator *PEO =
725 dyn_cast<PossiblyExactOperator>(V)) {
727 Flags |= 1 << bitc::PEO_EXACT;
728 } else if (const FPMathOperator *FPMO =
729 dyn_cast<const FPMathOperator>(V)) {
730 if (FPMO->hasUnsafeAlgebra())
731 Flags |= FastMathFlags::UnsafeAlgebra;
732 if (FPMO->hasNoNaNs())
733 Flags |= FastMathFlags::NoNaNs;
734 if (FPMO->hasNoInfs())
735 Flags |= FastMathFlags::NoInfs;
736 if (FPMO->hasNoSignedZeros())
737 Flags |= FastMathFlags::NoSignedZeros;
738 if (FPMO->hasAllowReciprocal())
739 Flags |= FastMathFlags::AllowReciprocal;
745 static void WriteMDNode(const MDNode *N,
746 const ValueEnumerator &VE,
747 BitstreamWriter &Stream,
748 SmallVectorImpl<uint64_t> &Record) {
749 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
750 if (N->getOperand(i)) {
751 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
752 Record.push_back(VE.getValueID(N->getOperand(i)));
754 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
758 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
760 Stream.EmitRecord(MDCode, Record, 0);
764 static void WriteModuleMetadata(const Module *M,
765 const ValueEnumerator &VE,
766 BitstreamWriter &Stream) {
767 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
768 bool StartedMetadataBlock = false;
769 unsigned MDSAbbrev = 0;
770 SmallVector<uint64_t, 64> Record;
771 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
773 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
774 if (!N->isFunctionLocal() || !N->getFunction()) {
775 if (!StartedMetadataBlock) {
776 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
777 StartedMetadataBlock = true;
779 WriteMDNode(N, VE, Stream, Record);
781 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
782 if (!StartedMetadataBlock) {
783 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
785 // Abbrev for METADATA_STRING.
786 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
787 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
790 MDSAbbrev = Stream.EmitAbbrev(Abbv);
791 StartedMetadataBlock = true;
794 // Code: [strchar x N]
795 Record.append(MDS->begin(), MDS->end());
797 // Emit the finished record.
798 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
803 // Write named metadata.
804 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
805 E = M->named_metadata_end(); I != E; ++I) {
806 const NamedMDNode *NMD = I;
807 if (!StartedMetadataBlock) {
808 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
809 StartedMetadataBlock = true;
813 StringRef Str = NMD->getName();
814 for (unsigned i = 0, e = Str.size(); i != e; ++i)
815 Record.push_back(Str[i]);
816 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
819 // Write named metadata operands.
820 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
821 Record.push_back(VE.getValueID(NMD->getOperand(i)));
822 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
826 if (StartedMetadataBlock)
830 static void WriteFunctionLocalMetadata(const Function &F,
831 const ValueEnumerator &VE,
832 BitstreamWriter &Stream) {
833 bool StartedMetadataBlock = false;
834 SmallVector<uint64_t, 64> Record;
835 const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues();
836 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
837 if (const MDNode *N = Vals[i])
838 if (N->isFunctionLocal() && N->getFunction() == &F) {
839 if (!StartedMetadataBlock) {
840 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
841 StartedMetadataBlock = true;
843 WriteMDNode(N, VE, Stream, Record);
846 if (StartedMetadataBlock)
850 static void WriteMetadataAttachment(const Function &F,
851 const ValueEnumerator &VE,
852 BitstreamWriter &Stream) {
853 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
855 SmallVector<uint64_t, 64> Record;
857 // Write metadata attachments
858 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
859 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
861 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
862 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
865 I->getAllMetadataOtherThanDebugLoc(MDs);
867 // If no metadata, ignore instruction.
868 if (MDs.empty()) continue;
870 Record.push_back(VE.getInstructionID(I));
872 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
873 Record.push_back(MDs[i].first);
874 Record.push_back(VE.getValueID(MDs[i].second));
876 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
883 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
884 SmallVector<uint64_t, 64> Record;
886 // Write metadata kinds
887 // METADATA_KIND - [n x [id, name]]
888 SmallVector<StringRef, 8> Names;
889 M->getMDKindNames(Names);
891 if (Names.empty()) return;
893 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
895 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
896 Record.push_back(MDKindID);
897 StringRef KName = Names[MDKindID];
898 Record.append(KName.begin(), KName.end());
900 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
907 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
909 Vals.push_back(V << 1);
911 Vals.push_back((-V << 1) | 1);
914 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
915 const ValueEnumerator &VE,
916 BitstreamWriter &Stream, bool isGlobal) {
917 if (FirstVal == LastVal) return;
919 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
921 unsigned AggregateAbbrev = 0;
922 unsigned String8Abbrev = 0;
923 unsigned CString7Abbrev = 0;
924 unsigned CString6Abbrev = 0;
925 // If this is a constant pool for the module, emit module-specific abbrevs.
927 // Abbrev for CST_CODE_AGGREGATE.
928 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
929 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
930 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
931 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
932 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
934 // Abbrev for CST_CODE_STRING.
935 Abbv = new BitCodeAbbrev();
936 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
937 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
938 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
939 String8Abbrev = Stream.EmitAbbrev(Abbv);
940 // Abbrev for CST_CODE_CSTRING.
941 Abbv = new BitCodeAbbrev();
942 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
943 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
944 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
945 CString7Abbrev = Stream.EmitAbbrev(Abbv);
946 // Abbrev for CST_CODE_CSTRING.
947 Abbv = new BitCodeAbbrev();
948 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
951 CString6Abbrev = Stream.EmitAbbrev(Abbv);
954 SmallVector<uint64_t, 64> Record;
956 const ValueEnumerator::ValueList &Vals = VE.getValues();
957 Type *LastTy = nullptr;
958 for (unsigned i = FirstVal; i != LastVal; ++i) {
959 const Value *V = Vals[i].first;
960 // If we need to switch types, do so now.
961 if (V->getType() != LastTy) {
962 LastTy = V->getType();
963 Record.push_back(VE.getTypeID(LastTy));
964 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
965 CONSTANTS_SETTYPE_ABBREV);
969 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
970 Record.push_back(unsigned(IA->hasSideEffects()) |
971 unsigned(IA->isAlignStack()) << 1 |
972 unsigned(IA->getDialect()&1) << 2);
974 // Add the asm string.
975 const std::string &AsmStr = IA->getAsmString();
976 Record.push_back(AsmStr.size());
977 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
978 Record.push_back(AsmStr[i]);
980 // Add the constraint string.
981 const std::string &ConstraintStr = IA->getConstraintString();
982 Record.push_back(ConstraintStr.size());
983 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
984 Record.push_back(ConstraintStr[i]);
985 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
989 const Constant *C = cast<Constant>(V);
991 unsigned AbbrevToUse = 0;
992 if (C->isNullValue()) {
993 Code = bitc::CST_CODE_NULL;
994 } else if (isa<UndefValue>(C)) {
995 Code = bitc::CST_CODE_UNDEF;
996 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
997 if (IV->getBitWidth() <= 64) {
998 uint64_t V = IV->getSExtValue();
999 emitSignedInt64(Record, V);
1000 Code = bitc::CST_CODE_INTEGER;
1001 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1002 } else { // Wide integers, > 64 bits in size.
1003 // We have an arbitrary precision integer value to write whose
1004 // bit width is > 64. However, in canonical unsigned integer
1005 // format it is likely that the high bits are going to be zero.
1006 // So, we only write the number of active words.
1007 unsigned NWords = IV->getValue().getActiveWords();
1008 const uint64_t *RawWords = IV->getValue().getRawData();
1009 for (unsigned i = 0; i != NWords; ++i) {
1010 emitSignedInt64(Record, RawWords[i]);
1012 Code = bitc::CST_CODE_WIDE_INTEGER;
1014 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1015 Code = bitc::CST_CODE_FLOAT;
1016 Type *Ty = CFP->getType();
1017 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
1018 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
1019 } else if (Ty->isX86_FP80Ty()) {
1020 // api needed to prevent premature destruction
1021 // bits are not in the same order as a normal i80 APInt, compensate.
1022 APInt api = CFP->getValueAPF().bitcastToAPInt();
1023 const uint64_t *p = api.getRawData();
1024 Record.push_back((p[1] << 48) | (p[0] >> 16));
1025 Record.push_back(p[0] & 0xffffLL);
1026 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
1027 APInt api = CFP->getValueAPF().bitcastToAPInt();
1028 const uint64_t *p = api.getRawData();
1029 Record.push_back(p[0]);
1030 Record.push_back(p[1]);
1032 assert (0 && "Unknown FP type!");
1034 } else if (isa<ConstantDataSequential>(C) &&
1035 cast<ConstantDataSequential>(C)->isString()) {
1036 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1037 // Emit constant strings specially.
1038 unsigned NumElts = Str->getNumElements();
1039 // If this is a null-terminated string, use the denser CSTRING encoding.
1040 if (Str->isCString()) {
1041 Code = bitc::CST_CODE_CSTRING;
1042 --NumElts; // Don't encode the null, which isn't allowed by char6.
1044 Code = bitc::CST_CODE_STRING;
1045 AbbrevToUse = String8Abbrev;
1047 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1048 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1049 for (unsigned i = 0; i != NumElts; ++i) {
1050 unsigned char V = Str->getElementAsInteger(i);
1051 Record.push_back(V);
1052 isCStr7 &= (V & 128) == 0;
1054 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1058 AbbrevToUse = CString6Abbrev;
1060 AbbrevToUse = CString7Abbrev;
1061 } else if (const ConstantDataSequential *CDS =
1062 dyn_cast<ConstantDataSequential>(C)) {
1063 Code = bitc::CST_CODE_DATA;
1064 Type *EltTy = CDS->getType()->getElementType();
1065 if (isa<IntegerType>(EltTy)) {
1066 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1067 Record.push_back(CDS->getElementAsInteger(i));
1068 } else if (EltTy->isFloatTy()) {
1069 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1070 union { float F; uint32_t I; };
1071 F = CDS->getElementAsFloat(i);
1072 Record.push_back(I);
1075 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
1076 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1077 union { double F; uint64_t I; };
1078 F = CDS->getElementAsDouble(i);
1079 Record.push_back(I);
1082 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1083 isa<ConstantVector>(C)) {
1084 Code = bitc::CST_CODE_AGGREGATE;
1085 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
1086 Record.push_back(VE.getValueID(C->getOperand(i)));
1087 AbbrevToUse = AggregateAbbrev;
1088 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1089 switch (CE->getOpcode()) {
1091 if (Instruction::isCast(CE->getOpcode())) {
1092 Code = bitc::CST_CODE_CE_CAST;
1093 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
1094 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1095 Record.push_back(VE.getValueID(C->getOperand(0)));
1096 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
1098 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1099 Code = bitc::CST_CODE_CE_BINOP;
1100 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
1101 Record.push_back(VE.getValueID(C->getOperand(0)));
1102 Record.push_back(VE.getValueID(C->getOperand(1)));
1103 uint64_t Flags = GetOptimizationFlags(CE);
1105 Record.push_back(Flags);
1108 case Instruction::GetElementPtr:
1109 Code = bitc::CST_CODE_CE_GEP;
1110 if (cast<GEPOperator>(C)->isInBounds())
1111 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
1112 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1113 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1114 Record.push_back(VE.getValueID(C->getOperand(i)));
1117 case Instruction::Select:
1118 Code = bitc::CST_CODE_CE_SELECT;
1119 Record.push_back(VE.getValueID(C->getOperand(0)));
1120 Record.push_back(VE.getValueID(C->getOperand(1)));
1121 Record.push_back(VE.getValueID(C->getOperand(2)));
1123 case Instruction::ExtractElement:
1124 Code = bitc::CST_CODE_CE_EXTRACTELT;
1125 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1126 Record.push_back(VE.getValueID(C->getOperand(0)));
1127 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
1128 Record.push_back(VE.getValueID(C->getOperand(1)));
1130 case Instruction::InsertElement:
1131 Code = bitc::CST_CODE_CE_INSERTELT;
1132 Record.push_back(VE.getValueID(C->getOperand(0)));
1133 Record.push_back(VE.getValueID(C->getOperand(1)));
1134 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
1135 Record.push_back(VE.getValueID(C->getOperand(2)));
1137 case Instruction::ShuffleVector:
1138 // If the return type and argument types are the same, this is a
1139 // standard shufflevector instruction. If the types are different,
1140 // then the shuffle is widening or truncating the input vectors, and
1141 // the argument type must also be encoded.
1142 if (C->getType() == C->getOperand(0)->getType()) {
1143 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1145 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1146 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1148 Record.push_back(VE.getValueID(C->getOperand(0)));
1149 Record.push_back(VE.getValueID(C->getOperand(1)));
1150 Record.push_back(VE.getValueID(C->getOperand(2)));
1152 case Instruction::ICmp:
1153 case Instruction::FCmp:
1154 Code = bitc::CST_CODE_CE_CMP;
1155 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1156 Record.push_back(VE.getValueID(C->getOperand(0)));
1157 Record.push_back(VE.getValueID(C->getOperand(1)));
1158 Record.push_back(CE->getPredicate());
1161 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1162 Code = bitc::CST_CODE_BLOCKADDRESS;
1163 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1164 Record.push_back(VE.getValueID(BA->getFunction()));
1165 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1170 llvm_unreachable("Unknown constant!");
1172 Stream.EmitRecord(Code, Record, AbbrevToUse);
1179 static void WriteModuleConstants(const ValueEnumerator &VE,
1180 BitstreamWriter &Stream) {
1181 const ValueEnumerator::ValueList &Vals = VE.getValues();
1183 // Find the first constant to emit, which is the first non-globalvalue value.
1184 // We know globalvalues have been emitted by WriteModuleInfo.
1185 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1186 if (!isa<GlobalValue>(Vals[i].first)) {
1187 WriteConstants(i, Vals.size(), VE, Stream, true);
1193 /// PushValueAndType - The file has to encode both the value and type id for
1194 /// many values, because we need to know what type to create for forward
1195 /// references. However, most operands are not forward references, so this type
1196 /// field is not needed.
1198 /// This function adds V's value ID to Vals. If the value ID is higher than the
1199 /// instruction ID, then it is a forward reference, and it also includes the
1200 /// type ID. The value ID that is written is encoded relative to the InstID.
1201 static bool PushValueAndType(const Value *V, unsigned InstID,
1202 SmallVectorImpl<unsigned> &Vals,
1203 ValueEnumerator &VE) {
1204 unsigned ValID = VE.getValueID(V);
1205 // Make encoding relative to the InstID.
1206 Vals.push_back(InstID - ValID);
1207 if (ValID >= InstID) {
1208 Vals.push_back(VE.getTypeID(V->getType()));
1214 /// pushValue - Like PushValueAndType, but where the type of the value is
1215 /// omitted (perhaps it was already encoded in an earlier operand).
1216 static void pushValue(const Value *V, unsigned InstID,
1217 SmallVectorImpl<unsigned> &Vals,
1218 ValueEnumerator &VE) {
1219 unsigned ValID = VE.getValueID(V);
1220 Vals.push_back(InstID - ValID);
1223 static void pushValueSigned(const Value *V, unsigned InstID,
1224 SmallVectorImpl<uint64_t> &Vals,
1225 ValueEnumerator &VE) {
1226 unsigned ValID = VE.getValueID(V);
1227 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
1228 emitSignedInt64(Vals, diff);
1231 /// WriteInstruction - Emit an instruction to the specified stream.
1232 static void WriteInstruction(const Instruction &I, unsigned InstID,
1233 ValueEnumerator &VE, BitstreamWriter &Stream,
1234 SmallVectorImpl<unsigned> &Vals) {
1236 unsigned AbbrevToUse = 0;
1237 VE.setInstructionID(&I);
1238 switch (I.getOpcode()) {
1240 if (Instruction::isCast(I.getOpcode())) {
1241 Code = bitc::FUNC_CODE_INST_CAST;
1242 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1243 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1244 Vals.push_back(VE.getTypeID(I.getType()));
1245 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1247 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1248 Code = bitc::FUNC_CODE_INST_BINOP;
1249 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1250 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1251 pushValue(I.getOperand(1), InstID, Vals, VE);
1252 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1253 uint64_t Flags = GetOptimizationFlags(&I);
1255 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1256 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1257 Vals.push_back(Flags);
1262 case Instruction::GetElementPtr:
1263 Code = bitc::FUNC_CODE_INST_GEP;
1264 if (cast<GEPOperator>(&I)->isInBounds())
1265 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
1266 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1267 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1269 case Instruction::ExtractValue: {
1270 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
1271 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1272 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1273 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1277 case Instruction::InsertValue: {
1278 Code = bitc::FUNC_CODE_INST_INSERTVAL;
1279 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1280 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1281 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1282 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1286 case Instruction::Select:
1287 Code = bitc::FUNC_CODE_INST_VSELECT;
1288 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1289 pushValue(I.getOperand(2), InstID, Vals, VE);
1290 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1292 case Instruction::ExtractElement:
1293 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
1294 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1295 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1297 case Instruction::InsertElement:
1298 Code = bitc::FUNC_CODE_INST_INSERTELT;
1299 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1300 pushValue(I.getOperand(1), InstID, Vals, VE);
1301 PushValueAndType(I.getOperand(2), InstID, Vals, VE);
1303 case Instruction::ShuffleVector:
1304 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
1305 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1306 pushValue(I.getOperand(1), InstID, Vals, VE);
1307 pushValue(I.getOperand(2), InstID, Vals, VE);
1309 case Instruction::ICmp:
1310 case Instruction::FCmp:
1311 // compare returning Int1Ty or vector of Int1Ty
1312 Code = bitc::FUNC_CODE_INST_CMP2;
1313 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1314 pushValue(I.getOperand(1), InstID, Vals, VE);
1315 Vals.push_back(cast<CmpInst>(I).getPredicate());
1318 case Instruction::Ret:
1320 Code = bitc::FUNC_CODE_INST_RET;
1321 unsigned NumOperands = I.getNumOperands();
1322 if (NumOperands == 0)
1323 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1324 else if (NumOperands == 1) {
1325 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1326 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1328 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1329 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1333 case Instruction::Br:
1335 Code = bitc::FUNC_CODE_INST_BR;
1336 const BranchInst &II = cast<BranchInst>(I);
1337 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1338 if (II.isConditional()) {
1339 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1340 pushValue(II.getCondition(), InstID, Vals, VE);
1344 case Instruction::Switch:
1346 Code = bitc::FUNC_CODE_INST_SWITCH;
1347 const SwitchInst &SI = cast<SwitchInst>(I);
1348 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
1349 pushValue(SI.getCondition(), InstID, Vals, VE);
1350 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1351 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
1353 Vals.push_back(VE.getValueID(i.getCaseValue()));
1354 Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
1358 case Instruction::IndirectBr:
1359 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
1360 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1361 // Encode the address operand as relative, but not the basic blocks.
1362 pushValue(I.getOperand(0), InstID, Vals, VE);
1363 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1364 Vals.push_back(VE.getValueID(I.getOperand(i)));
1367 case Instruction::Invoke: {
1368 const InvokeInst *II = cast<InvokeInst>(&I);
1369 const Value *Callee(II->getCalledValue());
1370 PointerType *PTy = cast<PointerType>(Callee->getType());
1371 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1372 Code = bitc::FUNC_CODE_INST_INVOKE;
1374 Vals.push_back(VE.getAttributeID(II->getAttributes()));
1375 Vals.push_back(II->getCallingConv());
1376 Vals.push_back(VE.getValueID(II->getNormalDest()));
1377 Vals.push_back(VE.getValueID(II->getUnwindDest()));
1378 PushValueAndType(Callee, InstID, Vals, VE);
1380 // Emit value #'s for the fixed parameters.
1381 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1382 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
1384 // Emit type/value pairs for varargs params.
1385 if (FTy->isVarArg()) {
1386 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1388 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1392 case Instruction::Resume:
1393 Code = bitc::FUNC_CODE_INST_RESUME;
1394 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1396 case Instruction::Unreachable:
1397 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
1398 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
1401 case Instruction::PHI: {
1402 const PHINode &PN = cast<PHINode>(I);
1403 Code = bitc::FUNC_CODE_INST_PHI;
1404 // With the newer instruction encoding, forward references could give
1405 // negative valued IDs. This is most common for PHIs, so we use
1407 SmallVector<uint64_t, 128> Vals64;
1408 Vals64.push_back(VE.getTypeID(PN.getType()));
1409 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1410 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
1411 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
1413 // Emit a Vals64 vector and exit.
1414 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
1419 case Instruction::LandingPad: {
1420 const LandingPadInst &LP = cast<LandingPadInst>(I);
1421 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
1422 Vals.push_back(VE.getTypeID(LP.getType()));
1423 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1424 Vals.push_back(LP.isCleanup());
1425 Vals.push_back(LP.getNumClauses());
1426 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1428 Vals.push_back(LandingPadInst::Catch);
1430 Vals.push_back(LandingPadInst::Filter);
1431 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1436 case Instruction::Alloca: {
1437 Code = bitc::FUNC_CODE_INST_ALLOCA;
1438 Vals.push_back(VE.getTypeID(I.getType()));
1439 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1440 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1441 const AllocaInst &AI = cast<AllocaInst>(I);
1442 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
1443 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
1444 "not enough bits for maximum alignment");
1445 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
1446 AlignRecord |= AI.isUsedWithInAlloca() << 5;
1447 Vals.push_back(AlignRecord);
1451 case Instruction::Load:
1452 if (cast<LoadInst>(I).isAtomic()) {
1453 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
1454 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1456 Code = bitc::FUNC_CODE_INST_LOAD;
1457 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1458 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1460 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1461 Vals.push_back(cast<LoadInst>(I).isVolatile());
1462 if (cast<LoadInst>(I).isAtomic()) {
1463 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
1464 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
1467 case Instruction::Store:
1468 if (cast<StoreInst>(I).isAtomic())
1469 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
1471 Code = bitc::FUNC_CODE_INST_STORE;
1472 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1473 pushValue(I.getOperand(0), InstID, Vals, VE); // val.
1474 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1475 Vals.push_back(cast<StoreInst>(I).isVolatile());
1476 if (cast<StoreInst>(I).isAtomic()) {
1477 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
1478 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
1481 case Instruction::AtomicCmpXchg:
1482 Code = bitc::FUNC_CODE_INST_CMPXCHG;
1483 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1484 pushValue(I.getOperand(1), InstID, Vals, VE); // cmp.
1485 pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
1486 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
1487 Vals.push_back(GetEncodedOrdering(
1488 cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
1489 Vals.push_back(GetEncodedSynchScope(
1490 cast<AtomicCmpXchgInst>(I).getSynchScope()));
1491 Vals.push_back(GetEncodedOrdering(
1492 cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
1493 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
1495 case Instruction::AtomicRMW:
1496 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
1497 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1498 pushValue(I.getOperand(1), InstID, Vals, VE); // val.
1499 Vals.push_back(GetEncodedRMWOperation(
1500 cast<AtomicRMWInst>(I).getOperation()));
1501 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
1502 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
1503 Vals.push_back(GetEncodedSynchScope(
1504 cast<AtomicRMWInst>(I).getSynchScope()));
1506 case Instruction::Fence:
1507 Code = bitc::FUNC_CODE_INST_FENCE;
1508 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
1509 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
1511 case Instruction::Call: {
1512 const CallInst &CI = cast<CallInst>(I);
1513 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1514 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1516 Code = bitc::FUNC_CODE_INST_CALL;
1518 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1519 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
1520 unsigned(CI.isMustTailCall()) << 14);
1521 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
1523 // Emit value #'s for the fixed parameters.
1524 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1525 // Check for labels (can happen with asm labels).
1526 if (FTy->getParamType(i)->isLabelTy())
1527 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
1529 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
1532 // Emit type/value pairs for varargs params.
1533 if (FTy->isVarArg()) {
1534 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
1536 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
1540 case Instruction::VAArg:
1541 Code = bitc::FUNC_CODE_INST_VAARG;
1542 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1543 pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
1544 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1548 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1552 // Emit names for globals/functions etc.
1553 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1554 const ValueEnumerator &VE,
1555 BitstreamWriter &Stream) {
1556 if (VST.empty()) return;
1557 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
1559 // FIXME: Set up the abbrev, we know how many values there are!
1560 // FIXME: We know if the type names can use 7-bit ascii.
1561 SmallVector<unsigned, 64> NameVals;
1563 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1566 const ValueName &Name = *SI;
1568 // Figure out the encoding to use for the name.
1570 bool isChar6 = true;
1571 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1574 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1575 if ((unsigned char)*C & 128) {
1577 break; // don't bother scanning the rest.
1581 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
1583 // VST_ENTRY: [valueid, namechar x N]
1584 // VST_BBENTRY: [bbid, namechar x N]
1586 if (isa<BasicBlock>(SI->getValue())) {
1587 Code = bitc::VST_CODE_BBENTRY;
1589 AbbrevToUse = VST_BBENTRY_6_ABBREV;
1591 Code = bitc::VST_CODE_ENTRY;
1593 AbbrevToUse = VST_ENTRY_6_ABBREV;
1595 AbbrevToUse = VST_ENTRY_7_ABBREV;
1598 NameVals.push_back(VE.getValueID(SI->getValue()));
1599 for (const char *P = Name.getKeyData(),
1600 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
1601 NameVals.push_back((unsigned char)*P);
1603 // Emit the finished record.
1604 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1610 /// WriteFunction - Emit a function body to the module stream.
1611 static void WriteFunction(const Function &F, ValueEnumerator &VE,
1612 BitstreamWriter &Stream) {
1613 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
1614 VE.incorporateFunction(F);
1616 SmallVector<unsigned, 64> Vals;
1618 // Emit the number of basic blocks, so the reader can create them ahead of
1620 Vals.push_back(VE.getBasicBlocks().size());
1621 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1624 // If there are function-local constants, emit them now.
1625 unsigned CstStart, CstEnd;
1626 VE.getFunctionConstantRange(CstStart, CstEnd);
1627 WriteConstants(CstStart, CstEnd, VE, Stream, false);
1629 // If there is function-local metadata, emit it now.
1630 WriteFunctionLocalMetadata(F, VE, Stream);
1632 // Keep a running idea of what the instruction ID is.
1633 unsigned InstID = CstEnd;
1635 bool NeedsMetadataAttachment = false;
1639 // Finally, emit all the instructions, in order.
1640 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1641 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1643 WriteInstruction(*I, InstID, VE, Stream, Vals);
1645 if (!I->getType()->isVoidTy())
1648 // If the instruction has metadata, write a metadata attachment later.
1649 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1651 // If the instruction has a debug location, emit it.
1652 DebugLoc DL = I->getDebugLoc();
1653 if (DL.isUnknown()) {
1655 } else if (DL == LastDL) {
1656 // Just repeat the same debug loc as last time.
1657 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1660 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1662 Vals.push_back(DL.getLine());
1663 Vals.push_back(DL.getCol());
1664 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1665 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1666 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
1673 // Emit names for all the instructions etc.
1674 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1676 if (NeedsMetadataAttachment)
1677 WriteMetadataAttachment(F, VE, Stream);
1682 // Emit blockinfo, which defines the standard abbreviations etc.
1683 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1684 // We only want to emit block info records for blocks that have multiple
1685 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
1686 // Other blocks can define their abbrevs inline.
1687 Stream.EnterBlockInfoBlock(2);
1689 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1690 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1691 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1692 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1693 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1694 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1695 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1696 Abbv) != VST_ENTRY_8_ABBREV)
1697 llvm_unreachable("Unexpected abbrev ordering!");
1700 { // 7-bit fixed width VST_ENTRY strings.
1701 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1702 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1703 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1704 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1706 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1707 Abbv) != VST_ENTRY_7_ABBREV)
1708 llvm_unreachable("Unexpected abbrev ordering!");
1710 { // 6-bit char6 VST_ENTRY strings.
1711 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1712 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1713 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1716 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1717 Abbv) != VST_ENTRY_6_ABBREV)
1718 llvm_unreachable("Unexpected abbrev ordering!");
1720 { // 6-bit char6 VST_BBENTRY strings.
1721 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1722 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1723 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1724 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1725 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1726 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1727 Abbv) != VST_BBENTRY_6_ABBREV)
1728 llvm_unreachable("Unexpected abbrev ordering!");
1733 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1734 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1735 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1736 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1737 Log2_32_Ceil(VE.getTypes().size()+1)));
1738 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1739 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1740 llvm_unreachable("Unexpected abbrev ordering!");
1743 { // INTEGER abbrev for CONSTANTS_BLOCK.
1744 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1745 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1746 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1747 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1748 Abbv) != CONSTANTS_INTEGER_ABBREV)
1749 llvm_unreachable("Unexpected abbrev ordering!");
1752 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1753 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1754 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1756 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1757 Log2_32_Ceil(VE.getTypes().size()+1)));
1758 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1760 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1761 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1762 llvm_unreachable("Unexpected abbrev ordering!");
1764 { // NULL abbrev for CONSTANTS_BLOCK.
1765 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1766 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1767 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1768 Abbv) != CONSTANTS_NULL_Abbrev)
1769 llvm_unreachable("Unexpected abbrev ordering!");
1772 // FIXME: This should only use space for first class types!
1774 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1775 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1776 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1778 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1780 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1781 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1782 llvm_unreachable("Unexpected abbrev ordering!");
1784 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1785 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1786 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1788 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1790 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1791 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1792 llvm_unreachable("Unexpected abbrev ordering!");
1794 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1795 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1796 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1801 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1802 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1803 llvm_unreachable("Unexpected abbrev ordering!");
1805 { // INST_CAST abbrev for FUNCTION_BLOCK.
1806 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1807 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1808 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1810 Log2_32_Ceil(VE.getTypes().size()+1)));
1811 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1812 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1813 Abbv) != FUNCTION_INST_CAST_ABBREV)
1814 llvm_unreachable("Unexpected abbrev ordering!");
1817 { // INST_RET abbrev for FUNCTION_BLOCK.
1818 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1819 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1820 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1821 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1822 llvm_unreachable("Unexpected abbrev ordering!");
1824 { // INST_RET abbrev for FUNCTION_BLOCK.
1825 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1826 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1827 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1828 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1829 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1830 llvm_unreachable("Unexpected abbrev ordering!");
1832 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1833 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1834 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1835 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1836 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1837 llvm_unreachable("Unexpected abbrev ordering!");
1843 // Sort the Users based on the order in which the reader parses the bitcode
1845 static bool bitcodereader_order(const User *lhs, const User *rhs) {
1850 static void WriteUseList(const Value *V, const ValueEnumerator &VE,
1851 BitstreamWriter &Stream) {
1853 // One or zero uses can't get out of order.
1854 if (V->use_empty() || V->hasNUses(1))
1857 // Make a copy of the in-memory use-list for sorting.
1858 SmallVector<const User*, 8> UserList(V->user_begin(), V->user_end());
1860 // Sort the copy based on the order read by the BitcodeReader.
1861 std::sort(UserList.begin(), UserList.end(), bitcodereader_order);
1863 // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the
1864 // sorted list (i.e., the expected BitcodeReader in-memory use-list).
1866 // TODO: Emit the USELIST_CODE_ENTRYs.
1869 static void WriteFunctionUseList(const Function *F, ValueEnumerator &VE,
1870 BitstreamWriter &Stream) {
1871 VE.incorporateFunction(*F);
1873 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1875 WriteUseList(AI, VE, Stream);
1876 for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE;
1878 WriteUseList(BB, VE, Stream);
1879 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
1881 WriteUseList(II, VE, Stream);
1882 for (User::const_op_iterator OI = II->op_begin(), E = II->op_end();
1884 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
1885 isa<InlineAsm>(*OI))
1886 WriteUseList(*OI, VE, Stream);
1894 static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE,
1895 BitstreamWriter &Stream) {
1896 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
1898 // XXX: this modifies the module, but in a way that should never change the
1899 // behavior of any pass or codegen in LLVM. The problem is that GVs may
1900 // contain entries in the use_list that do not exist in the Module and are
1901 // not stored in the .bc file.
1902 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1904 I->removeDeadConstantUsers();
1906 // Write the global variables.
1907 for (Module::const_global_iterator GI = M->global_begin(),
1908 GE = M->global_end(); GI != GE; ++GI) {
1909 WriteUseList(GI, VE, Stream);
1911 // Write the global variable initializers.
1912 if (GI->hasInitializer())
1913 WriteUseList(GI->getInitializer(), VE, Stream);
1916 // Write the functions.
1917 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
1918 WriteUseList(FI, VE, Stream);
1919 if (!FI->isDeclaration())
1920 WriteFunctionUseList(FI, VE, Stream);
1921 if (FI->hasPrefixData())
1922 WriteUseList(FI->getPrefixData(), VE, Stream);
1925 // Write the aliases.
1926 for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end();
1928 WriteUseList(AI, VE, Stream);
1929 WriteUseList(AI->getAliasee(), VE, Stream);
1935 /// WriteModule - Emit the specified module to the bitstream.
1936 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1937 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1939 SmallVector<unsigned, 1> Vals;
1940 unsigned CurVersion = 1;
1941 Vals.push_back(CurVersion);
1942 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1944 // Analyze the module, enumerating globals, functions, etc.
1945 ValueEnumerator VE(M);
1947 // Emit blockinfo, which defines the standard abbreviations etc.
1948 WriteBlockInfo(VE, Stream);
1950 // Emit information about attribute groups.
1951 WriteAttributeGroupTable(VE, Stream);
1953 // Emit information about parameter attributes.
1954 WriteAttributeTable(VE, Stream);
1956 // Emit information describing all of the types in the module.
1957 WriteTypeTable(VE, Stream);
1959 writeComdats(VE, Stream);
1961 // Emit top-level description of module, including target triple, inline asm,
1962 // descriptors for global variables, and function prototype info.
1963 WriteModuleInfo(M, VE, Stream);
1966 WriteModuleConstants(VE, Stream);
1969 WriteModuleMetadata(M, VE, Stream);
1972 WriteModuleMetadataStore(M, Stream);
1974 // Emit names for globals/functions etc.
1975 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1978 if (EnablePreserveUseListOrdering)
1979 WriteModuleUseLists(M, VE, Stream);
1981 // Emit function bodies.
1982 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1983 if (!F->isDeclaration())
1984 WriteFunction(*F, VE, Stream);
1989 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1990 /// header and trailer to make it compatible with the system archiver. To do
1991 /// this we emit the following header, and then emit a trailer that pads the
1992 /// file out to be a multiple of 16 bytes.
1994 /// struct bc_header {
1995 /// uint32_t Magic; // 0x0B17C0DE
1996 /// uint32_t Version; // Version, currently always 0.
1997 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1998 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
1999 /// uint32_t CPUType; // CPU specifier.
2000 /// ... potentially more later ...
2003 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
2004 DarwinBCHeaderSize = 5*4
2007 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
2008 uint32_t &Position) {
2009 Buffer[Position + 0] = (unsigned char) (Value >> 0);
2010 Buffer[Position + 1] = (unsigned char) (Value >> 8);
2011 Buffer[Position + 2] = (unsigned char) (Value >> 16);
2012 Buffer[Position + 3] = (unsigned char) (Value >> 24);
2016 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
2018 unsigned CPUType = ~0U;
2020 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
2021 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
2022 // number from /usr/include/mach/machine.h. It is ok to reproduce the
2023 // specific constants here because they are implicitly part of the Darwin ABI.
2025 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
2026 DARWIN_CPU_TYPE_X86 = 7,
2027 DARWIN_CPU_TYPE_ARM = 12,
2028 DARWIN_CPU_TYPE_POWERPC = 18
2031 Triple::ArchType Arch = TT.getArch();
2032 if (Arch == Triple::x86_64)
2033 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
2034 else if (Arch == Triple::x86)
2035 CPUType = DARWIN_CPU_TYPE_X86;
2036 else if (Arch == Triple::ppc)
2037 CPUType = DARWIN_CPU_TYPE_POWERPC;
2038 else if (Arch == Triple::ppc64)
2039 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
2040 else if (Arch == Triple::arm || Arch == Triple::thumb)
2041 CPUType = DARWIN_CPU_TYPE_ARM;
2043 // Traditional Bitcode starts after header.
2044 assert(Buffer.size() >= DarwinBCHeaderSize &&
2045 "Expected header size to be reserved");
2046 unsigned BCOffset = DarwinBCHeaderSize;
2047 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
2049 // Write the magic and version.
2050 unsigned Position = 0;
2051 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
2052 WriteInt32ToBuffer(0 , Buffer, Position); // Version.
2053 WriteInt32ToBuffer(BCOffset , Buffer, Position);
2054 WriteInt32ToBuffer(BCSize , Buffer, Position);
2055 WriteInt32ToBuffer(CPUType , Buffer, Position);
2057 // If the file is not a multiple of 16 bytes, insert dummy padding.
2058 while (Buffer.size() & 15)
2059 Buffer.push_back(0);
2062 /// WriteBitcodeToFile - Write the specified module to the specified output
2064 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
2065 SmallVector<char, 0> Buffer;
2066 Buffer.reserve(256*1024);
2068 // If this is darwin or another generic macho target, reserve space for the
2070 Triple TT(M->getTargetTriple());
2071 if (TT.isOSDarwin())
2072 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
2074 // Emit the module into the buffer.
2076 BitstreamWriter Stream(Buffer);
2078 // Emit the file header.
2079 Stream.Emit((unsigned)'B', 8);
2080 Stream.Emit((unsigned)'C', 8);
2081 Stream.Emit(0x0, 4);
2082 Stream.Emit(0xC, 4);
2083 Stream.Emit(0xE, 4);
2084 Stream.Emit(0xD, 4);
2087 WriteModule(M, Stream);
2090 if (TT.isOSDarwin())
2091 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
2093 // Write the generated bitstream to "Out".
2094 Out.write((char*)&Buffer.front(), Buffer.size());