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::NoRedZone:
205 return bitc::ATTR_KIND_NO_RED_ZONE;
206 case Attribute::NoReturn:
207 return bitc::ATTR_KIND_NO_RETURN;
208 case Attribute::NoUnwind:
209 return bitc::ATTR_KIND_NO_UNWIND;
210 case Attribute::OptimizeForSize:
211 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE;
212 case Attribute::OptimizeNone:
213 return bitc::ATTR_KIND_OPTIMIZE_NONE;
214 case Attribute::ReadNone:
215 return bitc::ATTR_KIND_READ_NONE;
216 case Attribute::ReadOnly:
217 return bitc::ATTR_KIND_READ_ONLY;
218 case Attribute::Returned:
219 return bitc::ATTR_KIND_RETURNED;
220 case Attribute::ReturnsTwice:
221 return bitc::ATTR_KIND_RETURNS_TWICE;
222 case Attribute::SExt:
223 return bitc::ATTR_KIND_S_EXT;
224 case Attribute::StackAlignment:
225 return bitc::ATTR_KIND_STACK_ALIGNMENT;
226 case Attribute::StackProtect:
227 return bitc::ATTR_KIND_STACK_PROTECT;
228 case Attribute::StackProtectReq:
229 return bitc::ATTR_KIND_STACK_PROTECT_REQ;
230 case Attribute::StackProtectStrong:
231 return bitc::ATTR_KIND_STACK_PROTECT_STRONG;
232 case Attribute::StructRet:
233 return bitc::ATTR_KIND_STRUCT_RET;
234 case Attribute::SanitizeAddress:
235 return bitc::ATTR_KIND_SANITIZE_ADDRESS;
236 case Attribute::SanitizeThread:
237 return bitc::ATTR_KIND_SANITIZE_THREAD;
238 case Attribute::SanitizeMemory:
239 return bitc::ATTR_KIND_SANITIZE_MEMORY;
240 case Attribute::UWTable:
241 return bitc::ATTR_KIND_UW_TABLE;
242 case Attribute::ZExt:
243 return bitc::ATTR_KIND_Z_EXT;
244 case Attribute::EndAttrKinds:
245 llvm_unreachable("Can not encode end-attribute kinds marker.");
246 case Attribute::None:
247 llvm_unreachable("Can not encode none-attribute.");
250 llvm_unreachable("Trying to encode unknown attribute");
253 static void WriteAttributeGroupTable(const ValueEnumerator &VE,
254 BitstreamWriter &Stream) {
255 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups();
256 if (AttrGrps.empty()) return;
258 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3);
260 SmallVector<uint64_t, 64> Record;
261 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) {
262 AttributeSet AS = AttrGrps[i];
263 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) {
264 AttributeSet A = AS.getSlotAttributes(i);
266 Record.push_back(VE.getAttributeGroupID(A));
267 Record.push_back(AS.getSlotIndex(i));
269 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0);
272 if (Attr.isEnumAttribute()) {
274 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
275 } else if (Attr.isIntAttribute()) {
277 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum()));
278 Record.push_back(Attr.getValueAsInt());
280 StringRef Kind = Attr.getKindAsString();
281 StringRef Val = Attr.getValueAsString();
283 Record.push_back(Val.empty() ? 3 : 4);
284 Record.append(Kind.begin(), Kind.end());
287 Record.append(Val.begin(), Val.end());
293 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record);
301 static void WriteAttributeTable(const ValueEnumerator &VE,
302 BitstreamWriter &Stream) {
303 const std::vector<AttributeSet> &Attrs = VE.getAttributes();
304 if (Attrs.empty()) return;
306 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
308 SmallVector<uint64_t, 64> Record;
309 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
310 const AttributeSet &A = Attrs[i];
311 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i)
312 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i)));
314 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
321 /// WriteTypeTable - Write out the type table for a module.
322 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
323 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
325 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
326 SmallVector<uint64_t, 64> TypeVals;
328 uint64_t NumBits = Log2_32_Ceil(VE.getTypes().size()+1);
330 // Abbrev for TYPE_CODE_POINTER.
331 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
332 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
333 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
334 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
335 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
337 // Abbrev for TYPE_CODE_FUNCTION.
338 Abbv = new BitCodeAbbrev();
339 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
341 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
342 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
344 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
346 // Abbrev for TYPE_CODE_STRUCT_ANON.
347 Abbv = new BitCodeAbbrev();
348 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
353 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
355 // Abbrev for TYPE_CODE_STRUCT_NAME.
356 Abbv = new BitCodeAbbrev();
357 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
360 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
362 // Abbrev for TYPE_CODE_STRUCT_NAMED.
363 Abbv = new BitCodeAbbrev();
364 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
365 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
366 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
367 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
369 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
371 // Abbrev for TYPE_CODE_ARRAY.
372 Abbv = new BitCodeAbbrev();
373 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits));
377 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
379 // Emit an entry count so the reader can reserve space.
380 TypeVals.push_back(TypeList.size());
381 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
384 // Loop over all of the types, emitting each in turn.
385 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
386 Type *T = TypeList[i];
390 switch (T->getTypeID()) {
391 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
392 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break;
393 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
394 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
395 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
396 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
397 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
398 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
399 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
400 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
401 case Type::IntegerTyID:
403 Code = bitc::TYPE_CODE_INTEGER;
404 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
406 case Type::PointerTyID: {
407 PointerType *PTy = cast<PointerType>(T);
408 // POINTER: [pointee type, address space]
409 Code = bitc::TYPE_CODE_POINTER;
410 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
411 unsigned AddressSpace = PTy->getAddressSpace();
412 TypeVals.push_back(AddressSpace);
413 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
416 case Type::FunctionTyID: {
417 FunctionType *FT = cast<FunctionType>(T);
418 // FUNCTION: [isvararg, retty, paramty x N]
419 Code = bitc::TYPE_CODE_FUNCTION;
420 TypeVals.push_back(FT->isVarArg());
421 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
422 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
423 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
424 AbbrevToUse = FunctionAbbrev;
427 case Type::StructTyID: {
428 StructType *ST = cast<StructType>(T);
429 // STRUCT: [ispacked, eltty x N]
430 TypeVals.push_back(ST->isPacked());
431 // Output all of the element types.
432 for (StructType::element_iterator I = ST->element_begin(),
433 E = ST->element_end(); I != E; ++I)
434 TypeVals.push_back(VE.getTypeID(*I));
436 if (ST->isLiteral()) {
437 Code = bitc::TYPE_CODE_STRUCT_ANON;
438 AbbrevToUse = StructAnonAbbrev;
440 if (ST->isOpaque()) {
441 Code = bitc::TYPE_CODE_OPAQUE;
443 Code = bitc::TYPE_CODE_STRUCT_NAMED;
444 AbbrevToUse = StructNamedAbbrev;
447 // Emit the name if it is present.
448 if (!ST->getName().empty())
449 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
450 StructNameAbbrev, Stream);
454 case Type::ArrayTyID: {
455 ArrayType *AT = cast<ArrayType>(T);
456 // ARRAY: [numelts, eltty]
457 Code = bitc::TYPE_CODE_ARRAY;
458 TypeVals.push_back(AT->getNumElements());
459 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
460 AbbrevToUse = ArrayAbbrev;
463 case Type::VectorTyID: {
464 VectorType *VT = cast<VectorType>(T);
465 // VECTOR [numelts, eltty]
466 Code = bitc::TYPE_CODE_VECTOR;
467 TypeVals.push_back(VT->getNumElements());
468 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
473 // Emit the finished record.
474 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
481 static unsigned getEncodedLinkage(const GlobalValue &GV) {
482 switch (GV.getLinkage()) {
483 case GlobalValue::ExternalLinkage: return 0;
484 case GlobalValue::WeakAnyLinkage: return 1;
485 case GlobalValue::AppendingLinkage: return 2;
486 case GlobalValue::InternalLinkage: return 3;
487 case GlobalValue::LinkOnceAnyLinkage: return 4;
488 case GlobalValue::ExternalWeakLinkage: return 7;
489 case GlobalValue::CommonLinkage: return 8;
490 case GlobalValue::PrivateLinkage: return 9;
491 case GlobalValue::WeakODRLinkage: return 10;
492 case GlobalValue::LinkOnceODRLinkage: return 11;
493 case GlobalValue::AvailableExternallyLinkage: return 12;
495 llvm_unreachable("Invalid linkage");
498 static unsigned getEncodedVisibility(const GlobalValue &GV) {
499 switch (GV.getVisibility()) {
500 case GlobalValue::DefaultVisibility: return 0;
501 case GlobalValue::HiddenVisibility: return 1;
502 case GlobalValue::ProtectedVisibility: return 2;
504 llvm_unreachable("Invalid visibility");
507 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) {
508 switch (GV.getDLLStorageClass()) {
509 case GlobalValue::DefaultStorageClass: return 0;
510 case GlobalValue::DLLImportStorageClass: return 1;
511 case GlobalValue::DLLExportStorageClass: return 2;
513 llvm_unreachable("Invalid DLL storage class");
516 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) {
517 switch (GV.getThreadLocalMode()) {
518 case GlobalVariable::NotThreadLocal: return 0;
519 case GlobalVariable::GeneralDynamicTLSModel: return 1;
520 case GlobalVariable::LocalDynamicTLSModel: return 2;
521 case GlobalVariable::InitialExecTLSModel: return 3;
522 case GlobalVariable::LocalExecTLSModel: return 4;
524 llvm_unreachable("Invalid TLS model");
527 static unsigned getEncodedComdatSelectionKind(const Comdat &C) {
528 switch (C.getSelectionKind()) {
530 return bitc::COMDAT_SELECTION_KIND_ANY;
531 case Comdat::ExactMatch:
532 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH;
533 case Comdat::Largest:
534 return bitc::COMDAT_SELECTION_KIND_LARGEST;
535 case Comdat::NoDuplicates:
536 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES;
537 case Comdat::SameSize:
538 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE;
540 llvm_unreachable("Invalid selection kind");
543 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) {
544 SmallVector<uint8_t, 64> Vals;
545 for (const Comdat *C : VE.getComdats()) {
546 // COMDAT: [selection_kind, name]
547 Vals.push_back(getEncodedComdatSelectionKind(*C));
548 Vals.push_back(C->getName().size());
549 for (char Chr : C->getName())
550 Vals.push_back((unsigned char)Chr);
551 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0);
556 // Emit top-level description of module, including target triple, inline asm,
557 // descriptors for global variables, and function prototype info.
558 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
559 BitstreamWriter &Stream) {
560 // Emit various pieces of data attached to a module.
561 if (!M->getTargetTriple().empty())
562 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
564 const std::string &DL = M->getDataLayoutStr();
566 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream);
567 if (!M->getModuleInlineAsm().empty())
568 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
571 // Emit information about sections and GC, computing how many there are. Also
572 // compute the maximum alignment value.
573 std::map<std::string, unsigned> SectionMap;
574 std::map<std::string, unsigned> GCMap;
575 unsigned MaxAlignment = 0;
576 unsigned MaxGlobalType = 0;
577 for (const GlobalValue &GV : M->globals()) {
578 MaxAlignment = std::max(MaxAlignment, GV.getAlignment());
579 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getType()));
580 if (GV.hasSection()) {
581 // Give section names unique ID's.
582 unsigned &Entry = SectionMap[GV.getSection()];
584 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(),
586 Entry = SectionMap.size();
590 for (const Function &F : *M) {
591 MaxAlignment = std::max(MaxAlignment, F.getAlignment());
592 if (F.hasSection()) {
593 // Give section names unique ID's.
594 unsigned &Entry = SectionMap[F.getSection()];
596 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(),
598 Entry = SectionMap.size();
602 // Same for GC names.
603 unsigned &Entry = GCMap[F.getGC()];
605 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(),
607 Entry = GCMap.size();
612 // Emit abbrev for globals, now that we know # sections and max alignment.
613 unsigned SimpleGVarAbbrev = 0;
614 if (!M->global_empty()) {
615 // Add an abbrev for common globals with no visibility or thread localness.
616 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
617 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
618 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
619 Log2_32_Ceil(MaxGlobalType+1)));
620 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
621 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
622 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
623 if (MaxAlignment == 0) // Alignment.
624 Abbv->Add(BitCodeAbbrevOp(0));
626 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
627 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
628 Log2_32_Ceil(MaxEncAlignment+1)));
630 if (SectionMap.empty()) // Section.
631 Abbv->Add(BitCodeAbbrevOp(0));
633 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
634 Log2_32_Ceil(SectionMap.size()+1)));
635 // Don't bother emitting vis + thread local.
636 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
639 // Emit the global variable information.
640 SmallVector<unsigned, 64> Vals;
641 for (const GlobalVariable &GV : M->globals()) {
642 unsigned AbbrevToUse = 0;
644 // GLOBALVAR: [type, isconst, initid,
645 // linkage, alignment, section, visibility, threadlocal,
646 // unnamed_addr, externally_initialized, dllstorageclass]
647 Vals.push_back(VE.getTypeID(GV.getType()));
648 Vals.push_back(GV.isConstant());
649 Vals.push_back(GV.isDeclaration() ? 0 :
650 (VE.getValueID(GV.getInitializer()) + 1));
651 Vals.push_back(getEncodedLinkage(GV));
652 Vals.push_back(Log2_32(GV.getAlignment())+1);
653 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0);
654 if (GV.isThreadLocal() ||
655 GV.getVisibility() != GlobalValue::DefaultVisibility ||
656 GV.hasUnnamedAddr() || GV.isExternallyInitialized() ||
657 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass ||
659 Vals.push_back(getEncodedVisibility(GV));
660 Vals.push_back(getEncodedThreadLocalMode(GV));
661 Vals.push_back(GV.hasUnnamedAddr());
662 Vals.push_back(GV.isExternallyInitialized());
663 Vals.push_back(getEncodedDLLStorageClass(GV));
664 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0);
666 AbbrevToUse = SimpleGVarAbbrev;
669 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
673 // Emit the function proto information.
674 for (const Function &F : *M) {
675 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
676 // section, visibility, gc, unnamed_addr, prefix]
677 Vals.push_back(VE.getTypeID(F.getType()));
678 Vals.push_back(F.getCallingConv());
679 Vals.push_back(F.isDeclaration());
680 Vals.push_back(getEncodedLinkage(F));
681 Vals.push_back(VE.getAttributeID(F.getAttributes()));
682 Vals.push_back(Log2_32(F.getAlignment())+1);
683 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0);
684 Vals.push_back(getEncodedVisibility(F));
685 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0);
686 Vals.push_back(F.hasUnnamedAddr());
687 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1)
689 Vals.push_back(getEncodedDLLStorageClass(F));
690 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0);
692 unsigned AbbrevToUse = 0;
693 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
697 // Emit the alias information.
698 for (const GlobalAlias &A : M->aliases()) {
699 // ALIAS: [alias type, aliasee val#, linkage, visibility]
700 Vals.push_back(VE.getTypeID(A.getType()));
701 Vals.push_back(VE.getValueID(A.getAliasee()));
702 Vals.push_back(getEncodedLinkage(A));
703 Vals.push_back(getEncodedVisibility(A));
704 Vals.push_back(getEncodedDLLStorageClass(A));
705 Vals.push_back(getEncodedThreadLocalMode(A));
706 Vals.push_back(A.hasUnnamedAddr());
707 unsigned AbbrevToUse = 0;
708 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
713 static uint64_t GetOptimizationFlags(const Value *V) {
716 if (const OverflowingBinaryOperator *OBO =
717 dyn_cast<OverflowingBinaryOperator>(V)) {
718 if (OBO->hasNoSignedWrap())
719 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
720 if (OBO->hasNoUnsignedWrap())
721 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
722 } else if (const PossiblyExactOperator *PEO =
723 dyn_cast<PossiblyExactOperator>(V)) {
725 Flags |= 1 << bitc::PEO_EXACT;
726 } else if (const FPMathOperator *FPMO =
727 dyn_cast<const FPMathOperator>(V)) {
728 if (FPMO->hasUnsafeAlgebra())
729 Flags |= FastMathFlags::UnsafeAlgebra;
730 if (FPMO->hasNoNaNs())
731 Flags |= FastMathFlags::NoNaNs;
732 if (FPMO->hasNoInfs())
733 Flags |= FastMathFlags::NoInfs;
734 if (FPMO->hasNoSignedZeros())
735 Flags |= FastMathFlags::NoSignedZeros;
736 if (FPMO->hasAllowReciprocal())
737 Flags |= FastMathFlags::AllowReciprocal;
743 static void WriteMDNode(const MDNode *N,
744 const ValueEnumerator &VE,
745 BitstreamWriter &Stream,
746 SmallVectorImpl<uint64_t> &Record) {
747 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
748 if (N->getOperand(i)) {
749 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
750 Record.push_back(VE.getValueID(N->getOperand(i)));
752 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
756 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
758 Stream.EmitRecord(MDCode, Record, 0);
762 static void WriteModuleMetadata(const Module *M,
763 const ValueEnumerator &VE,
764 BitstreamWriter &Stream) {
765 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
766 bool StartedMetadataBlock = false;
767 unsigned MDSAbbrev = 0;
768 SmallVector<uint64_t, 64> Record;
769 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
771 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
772 if (!N->isFunctionLocal() || !N->getFunction()) {
773 if (!StartedMetadataBlock) {
774 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
775 StartedMetadataBlock = true;
777 WriteMDNode(N, VE, Stream, Record);
779 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
780 if (!StartedMetadataBlock) {
781 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
783 // Abbrev for METADATA_STRING.
784 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
785 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
788 MDSAbbrev = Stream.EmitAbbrev(Abbv);
789 StartedMetadataBlock = true;
792 // Code: [strchar x N]
793 Record.append(MDS->begin(), MDS->end());
795 // Emit the finished record.
796 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
801 // Write named metadata.
802 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
803 E = M->named_metadata_end(); I != E; ++I) {
804 const NamedMDNode *NMD = I;
805 if (!StartedMetadataBlock) {
806 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
807 StartedMetadataBlock = true;
811 StringRef Str = NMD->getName();
812 for (unsigned i = 0, e = Str.size(); i != e; ++i)
813 Record.push_back(Str[i]);
814 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
817 // Write named metadata operands.
818 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
819 Record.push_back(VE.getValueID(NMD->getOperand(i)));
820 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
824 if (StartedMetadataBlock)
828 static void WriteFunctionLocalMetadata(const Function &F,
829 const ValueEnumerator &VE,
830 BitstreamWriter &Stream) {
831 bool StartedMetadataBlock = false;
832 SmallVector<uint64_t, 64> Record;
833 const SmallVectorImpl<const MDNode *> &Vals = VE.getFunctionLocalMDValues();
834 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
835 if (const MDNode *N = Vals[i])
836 if (N->isFunctionLocal() && N->getFunction() == &F) {
837 if (!StartedMetadataBlock) {
838 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
839 StartedMetadataBlock = true;
841 WriteMDNode(N, VE, Stream, Record);
844 if (StartedMetadataBlock)
848 static void WriteMetadataAttachment(const Function &F,
849 const ValueEnumerator &VE,
850 BitstreamWriter &Stream) {
851 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
853 SmallVector<uint64_t, 64> Record;
855 // Write metadata attachments
856 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
857 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
859 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
860 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
863 I->getAllMetadataOtherThanDebugLoc(MDs);
865 // If no metadata, ignore instruction.
866 if (MDs.empty()) continue;
868 Record.push_back(VE.getInstructionID(I));
870 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
871 Record.push_back(MDs[i].first);
872 Record.push_back(VE.getValueID(MDs[i].second));
874 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
881 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
882 SmallVector<uint64_t, 64> Record;
884 // Write metadata kinds
885 // METADATA_KIND - [n x [id, name]]
886 SmallVector<StringRef, 8> Names;
887 M->getMDKindNames(Names);
889 if (Names.empty()) return;
891 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
893 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
894 Record.push_back(MDKindID);
895 StringRef KName = Names[MDKindID];
896 Record.append(KName.begin(), KName.end());
898 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
905 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) {
907 Vals.push_back(V << 1);
909 Vals.push_back((-V << 1) | 1);
912 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
913 const ValueEnumerator &VE,
914 BitstreamWriter &Stream, bool isGlobal) {
915 if (FirstVal == LastVal) return;
917 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
919 unsigned AggregateAbbrev = 0;
920 unsigned String8Abbrev = 0;
921 unsigned CString7Abbrev = 0;
922 unsigned CString6Abbrev = 0;
923 // If this is a constant pool for the module, emit module-specific abbrevs.
925 // Abbrev for CST_CODE_AGGREGATE.
926 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
927 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
928 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
929 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
930 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
932 // Abbrev for CST_CODE_STRING.
933 Abbv = new BitCodeAbbrev();
934 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
935 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
937 String8Abbrev = Stream.EmitAbbrev(Abbv);
938 // Abbrev for CST_CODE_CSTRING.
939 Abbv = new BitCodeAbbrev();
940 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
941 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
942 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
943 CString7Abbrev = Stream.EmitAbbrev(Abbv);
944 // Abbrev for CST_CODE_CSTRING.
945 Abbv = new BitCodeAbbrev();
946 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
947 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
949 CString6Abbrev = Stream.EmitAbbrev(Abbv);
952 SmallVector<uint64_t, 64> Record;
954 const ValueEnumerator::ValueList &Vals = VE.getValues();
955 Type *LastTy = nullptr;
956 for (unsigned i = FirstVal; i != LastVal; ++i) {
957 const Value *V = Vals[i].first;
958 // If we need to switch types, do so now.
959 if (V->getType() != LastTy) {
960 LastTy = V->getType();
961 Record.push_back(VE.getTypeID(LastTy));
962 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
963 CONSTANTS_SETTYPE_ABBREV);
967 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
968 Record.push_back(unsigned(IA->hasSideEffects()) |
969 unsigned(IA->isAlignStack()) << 1 |
970 unsigned(IA->getDialect()&1) << 2);
972 // Add the asm string.
973 const std::string &AsmStr = IA->getAsmString();
974 Record.push_back(AsmStr.size());
975 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
976 Record.push_back(AsmStr[i]);
978 // Add the constraint string.
979 const std::string &ConstraintStr = IA->getConstraintString();
980 Record.push_back(ConstraintStr.size());
981 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
982 Record.push_back(ConstraintStr[i]);
983 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
987 const Constant *C = cast<Constant>(V);
989 unsigned AbbrevToUse = 0;
990 if (C->isNullValue()) {
991 Code = bitc::CST_CODE_NULL;
992 } else if (isa<UndefValue>(C)) {
993 Code = bitc::CST_CODE_UNDEF;
994 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
995 if (IV->getBitWidth() <= 64) {
996 uint64_t V = IV->getSExtValue();
997 emitSignedInt64(Record, V);
998 Code = bitc::CST_CODE_INTEGER;
999 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
1000 } else { // Wide integers, > 64 bits in size.
1001 // We have an arbitrary precision integer value to write whose
1002 // bit width is > 64. However, in canonical unsigned integer
1003 // format it is likely that the high bits are going to be zero.
1004 // So, we only write the number of active words.
1005 unsigned NWords = IV->getValue().getActiveWords();
1006 const uint64_t *RawWords = IV->getValue().getRawData();
1007 for (unsigned i = 0; i != NWords; ++i) {
1008 emitSignedInt64(Record, RawWords[i]);
1010 Code = bitc::CST_CODE_WIDE_INTEGER;
1012 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
1013 Code = bitc::CST_CODE_FLOAT;
1014 Type *Ty = CFP->getType();
1015 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) {
1016 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
1017 } else if (Ty->isX86_FP80Ty()) {
1018 // api needed to prevent premature destruction
1019 // bits are not in the same order as a normal i80 APInt, compensate.
1020 APInt api = CFP->getValueAPF().bitcastToAPInt();
1021 const uint64_t *p = api.getRawData();
1022 Record.push_back((p[1] << 48) | (p[0] >> 16));
1023 Record.push_back(p[0] & 0xffffLL);
1024 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
1025 APInt api = CFP->getValueAPF().bitcastToAPInt();
1026 const uint64_t *p = api.getRawData();
1027 Record.push_back(p[0]);
1028 Record.push_back(p[1]);
1030 assert (0 && "Unknown FP type!");
1032 } else if (isa<ConstantDataSequential>(C) &&
1033 cast<ConstantDataSequential>(C)->isString()) {
1034 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
1035 // Emit constant strings specially.
1036 unsigned NumElts = Str->getNumElements();
1037 // If this is a null-terminated string, use the denser CSTRING encoding.
1038 if (Str->isCString()) {
1039 Code = bitc::CST_CODE_CSTRING;
1040 --NumElts; // Don't encode the null, which isn't allowed by char6.
1042 Code = bitc::CST_CODE_STRING;
1043 AbbrevToUse = String8Abbrev;
1045 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
1046 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
1047 for (unsigned i = 0; i != NumElts; ++i) {
1048 unsigned char V = Str->getElementAsInteger(i);
1049 Record.push_back(V);
1050 isCStr7 &= (V & 128) == 0;
1052 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
1056 AbbrevToUse = CString6Abbrev;
1058 AbbrevToUse = CString7Abbrev;
1059 } else if (const ConstantDataSequential *CDS =
1060 dyn_cast<ConstantDataSequential>(C)) {
1061 Code = bitc::CST_CODE_DATA;
1062 Type *EltTy = CDS->getType()->getElementType();
1063 if (isa<IntegerType>(EltTy)) {
1064 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
1065 Record.push_back(CDS->getElementAsInteger(i));
1066 } else if (EltTy->isFloatTy()) {
1067 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1068 union { float F; uint32_t I; };
1069 F = CDS->getElementAsFloat(i);
1070 Record.push_back(I);
1073 assert(EltTy->isDoubleTy() && "Unknown ConstantData element type");
1074 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1075 union { double F; uint64_t I; };
1076 F = CDS->getElementAsDouble(i);
1077 Record.push_back(I);
1080 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) ||
1081 isa<ConstantVector>(C)) {
1082 Code = bitc::CST_CODE_AGGREGATE;
1083 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
1084 Record.push_back(VE.getValueID(C->getOperand(i)));
1085 AbbrevToUse = AggregateAbbrev;
1086 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
1087 switch (CE->getOpcode()) {
1089 if (Instruction::isCast(CE->getOpcode())) {
1090 Code = bitc::CST_CODE_CE_CAST;
1091 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
1092 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1093 Record.push_back(VE.getValueID(C->getOperand(0)));
1094 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
1096 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
1097 Code = bitc::CST_CODE_CE_BINOP;
1098 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
1099 Record.push_back(VE.getValueID(C->getOperand(0)));
1100 Record.push_back(VE.getValueID(C->getOperand(1)));
1101 uint64_t Flags = GetOptimizationFlags(CE);
1103 Record.push_back(Flags);
1106 case Instruction::GetElementPtr:
1107 Code = bitc::CST_CODE_CE_GEP;
1108 if (cast<GEPOperator>(C)->isInBounds())
1109 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
1110 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
1111 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
1112 Record.push_back(VE.getValueID(C->getOperand(i)));
1115 case Instruction::Select:
1116 Code = bitc::CST_CODE_CE_SELECT;
1117 Record.push_back(VE.getValueID(C->getOperand(0)));
1118 Record.push_back(VE.getValueID(C->getOperand(1)));
1119 Record.push_back(VE.getValueID(C->getOperand(2)));
1121 case Instruction::ExtractElement:
1122 Code = bitc::CST_CODE_CE_EXTRACTELT;
1123 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1124 Record.push_back(VE.getValueID(C->getOperand(0)));
1125 Record.push_back(VE.getTypeID(C->getOperand(1)->getType()));
1126 Record.push_back(VE.getValueID(C->getOperand(1)));
1128 case Instruction::InsertElement:
1129 Code = bitc::CST_CODE_CE_INSERTELT;
1130 Record.push_back(VE.getValueID(C->getOperand(0)));
1131 Record.push_back(VE.getValueID(C->getOperand(1)));
1132 Record.push_back(VE.getTypeID(C->getOperand(2)->getType()));
1133 Record.push_back(VE.getValueID(C->getOperand(2)));
1135 case Instruction::ShuffleVector:
1136 // If the return type and argument types are the same, this is a
1137 // standard shufflevector instruction. If the types are different,
1138 // then the shuffle is widening or truncating the input vectors, and
1139 // the argument type must also be encoded.
1140 if (C->getType() == C->getOperand(0)->getType()) {
1141 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
1143 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
1144 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1146 Record.push_back(VE.getValueID(C->getOperand(0)));
1147 Record.push_back(VE.getValueID(C->getOperand(1)));
1148 Record.push_back(VE.getValueID(C->getOperand(2)));
1150 case Instruction::ICmp:
1151 case Instruction::FCmp:
1152 Code = bitc::CST_CODE_CE_CMP;
1153 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
1154 Record.push_back(VE.getValueID(C->getOperand(0)));
1155 Record.push_back(VE.getValueID(C->getOperand(1)));
1156 Record.push_back(CE->getPredicate());
1159 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
1160 Code = bitc::CST_CODE_BLOCKADDRESS;
1161 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
1162 Record.push_back(VE.getValueID(BA->getFunction()));
1163 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
1168 llvm_unreachable("Unknown constant!");
1170 Stream.EmitRecord(Code, Record, AbbrevToUse);
1177 static void WriteModuleConstants(const ValueEnumerator &VE,
1178 BitstreamWriter &Stream) {
1179 const ValueEnumerator::ValueList &Vals = VE.getValues();
1181 // Find the first constant to emit, which is the first non-globalvalue value.
1182 // We know globalvalues have been emitted by WriteModuleInfo.
1183 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
1184 if (!isa<GlobalValue>(Vals[i].first)) {
1185 WriteConstants(i, Vals.size(), VE, Stream, true);
1191 /// PushValueAndType - The file has to encode both the value and type id for
1192 /// many values, because we need to know what type to create for forward
1193 /// references. However, most operands are not forward references, so this type
1194 /// field is not needed.
1196 /// This function adds V's value ID to Vals. If the value ID is higher than the
1197 /// instruction ID, then it is a forward reference, and it also includes the
1198 /// type ID. The value ID that is written is encoded relative to the InstID.
1199 static bool PushValueAndType(const Value *V, unsigned InstID,
1200 SmallVectorImpl<unsigned> &Vals,
1201 ValueEnumerator &VE) {
1202 unsigned ValID = VE.getValueID(V);
1203 // Make encoding relative to the InstID.
1204 Vals.push_back(InstID - ValID);
1205 if (ValID >= InstID) {
1206 Vals.push_back(VE.getTypeID(V->getType()));
1212 /// pushValue - Like PushValueAndType, but where the type of the value is
1213 /// omitted (perhaps it was already encoded in an earlier operand).
1214 static void pushValue(const Value *V, unsigned InstID,
1215 SmallVectorImpl<unsigned> &Vals,
1216 ValueEnumerator &VE) {
1217 unsigned ValID = VE.getValueID(V);
1218 Vals.push_back(InstID - ValID);
1221 static void pushValueSigned(const Value *V, unsigned InstID,
1222 SmallVectorImpl<uint64_t> &Vals,
1223 ValueEnumerator &VE) {
1224 unsigned ValID = VE.getValueID(V);
1225 int64_t diff = ((int32_t)InstID - (int32_t)ValID);
1226 emitSignedInt64(Vals, diff);
1229 /// WriteInstruction - Emit an instruction to the specified stream.
1230 static void WriteInstruction(const Instruction &I, unsigned InstID,
1231 ValueEnumerator &VE, BitstreamWriter &Stream,
1232 SmallVectorImpl<unsigned> &Vals) {
1234 unsigned AbbrevToUse = 0;
1235 VE.setInstructionID(&I);
1236 switch (I.getOpcode()) {
1238 if (Instruction::isCast(I.getOpcode())) {
1239 Code = bitc::FUNC_CODE_INST_CAST;
1240 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1241 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
1242 Vals.push_back(VE.getTypeID(I.getType()));
1243 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
1245 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
1246 Code = bitc::FUNC_CODE_INST_BINOP;
1247 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1248 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
1249 pushValue(I.getOperand(1), InstID, Vals, VE);
1250 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
1251 uint64_t Flags = GetOptimizationFlags(&I);
1253 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1254 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1255 Vals.push_back(Flags);
1260 case Instruction::GetElementPtr:
1261 Code = bitc::FUNC_CODE_INST_GEP;
1262 if (cast<GEPOperator>(&I)->isInBounds())
1263 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
1264 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1265 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1267 case Instruction::ExtractValue: {
1268 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
1269 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1270 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1271 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1275 case Instruction::InsertValue: {
1276 Code = bitc::FUNC_CODE_INST_INSERTVAL;
1277 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1278 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1279 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1280 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1284 case Instruction::Select:
1285 Code = bitc::FUNC_CODE_INST_VSELECT;
1286 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1287 pushValue(I.getOperand(2), InstID, Vals, VE);
1288 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1290 case Instruction::ExtractElement:
1291 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
1292 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1293 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1295 case Instruction::InsertElement:
1296 Code = bitc::FUNC_CODE_INST_INSERTELT;
1297 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1298 pushValue(I.getOperand(1), InstID, Vals, VE);
1299 PushValueAndType(I.getOperand(2), InstID, Vals, VE);
1301 case Instruction::ShuffleVector:
1302 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
1303 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1304 pushValue(I.getOperand(1), InstID, Vals, VE);
1305 pushValue(I.getOperand(2), InstID, Vals, VE);
1307 case Instruction::ICmp:
1308 case Instruction::FCmp:
1309 // compare returning Int1Ty or vector of Int1Ty
1310 Code = bitc::FUNC_CODE_INST_CMP2;
1311 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1312 pushValue(I.getOperand(1), InstID, Vals, VE);
1313 Vals.push_back(cast<CmpInst>(I).getPredicate());
1316 case Instruction::Ret:
1318 Code = bitc::FUNC_CODE_INST_RET;
1319 unsigned NumOperands = I.getNumOperands();
1320 if (NumOperands == 0)
1321 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1322 else if (NumOperands == 1) {
1323 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1324 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1326 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1327 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1331 case Instruction::Br:
1333 Code = bitc::FUNC_CODE_INST_BR;
1334 const BranchInst &II = cast<BranchInst>(I);
1335 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1336 if (II.isConditional()) {
1337 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1338 pushValue(II.getCondition(), InstID, Vals, VE);
1342 case Instruction::Switch:
1344 Code = bitc::FUNC_CODE_INST_SWITCH;
1345 const SwitchInst &SI = cast<SwitchInst>(I);
1346 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
1347 pushValue(SI.getCondition(), InstID, Vals, VE);
1348 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1349 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
1351 Vals.push_back(VE.getValueID(i.getCaseValue()));
1352 Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
1356 case Instruction::IndirectBr:
1357 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
1358 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1359 // Encode the address operand as relative, but not the basic blocks.
1360 pushValue(I.getOperand(0), InstID, Vals, VE);
1361 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i)
1362 Vals.push_back(VE.getValueID(I.getOperand(i)));
1365 case Instruction::Invoke: {
1366 const InvokeInst *II = cast<InvokeInst>(&I);
1367 const Value *Callee(II->getCalledValue());
1368 PointerType *PTy = cast<PointerType>(Callee->getType());
1369 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1370 Code = bitc::FUNC_CODE_INST_INVOKE;
1372 Vals.push_back(VE.getAttributeID(II->getAttributes()));
1373 Vals.push_back(II->getCallingConv());
1374 Vals.push_back(VE.getValueID(II->getNormalDest()));
1375 Vals.push_back(VE.getValueID(II->getUnwindDest()));
1376 PushValueAndType(Callee, InstID, Vals, VE);
1378 // Emit value #'s for the fixed parameters.
1379 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1380 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param.
1382 // Emit type/value pairs for varargs params.
1383 if (FTy->isVarArg()) {
1384 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
1386 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
1390 case Instruction::Resume:
1391 Code = bitc::FUNC_CODE_INST_RESUME;
1392 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1394 case Instruction::Unreachable:
1395 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
1396 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
1399 case Instruction::PHI: {
1400 const PHINode &PN = cast<PHINode>(I);
1401 Code = bitc::FUNC_CODE_INST_PHI;
1402 // With the newer instruction encoding, forward references could give
1403 // negative valued IDs. This is most common for PHIs, so we use
1405 SmallVector<uint64_t, 128> Vals64;
1406 Vals64.push_back(VE.getTypeID(PN.getType()));
1407 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1408 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE);
1409 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i)));
1411 // Emit a Vals64 vector and exit.
1412 Stream.EmitRecord(Code, Vals64, AbbrevToUse);
1417 case Instruction::LandingPad: {
1418 const LandingPadInst &LP = cast<LandingPadInst>(I);
1419 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
1420 Vals.push_back(VE.getTypeID(LP.getType()));
1421 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1422 Vals.push_back(LP.isCleanup());
1423 Vals.push_back(LP.getNumClauses());
1424 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1426 Vals.push_back(LandingPadInst::Catch);
1428 Vals.push_back(LandingPadInst::Filter);
1429 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1434 case Instruction::Alloca: {
1435 Code = bitc::FUNC_CODE_INST_ALLOCA;
1436 Vals.push_back(VE.getTypeID(I.getType()));
1437 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1438 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1439 const AllocaInst &AI = cast<AllocaInst>(I);
1440 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1;
1441 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 &&
1442 "not enough bits for maximum alignment");
1443 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64");
1444 AlignRecord |= AI.isUsedWithInAlloca() << 5;
1445 Vals.push_back(AlignRecord);
1449 case Instruction::Load:
1450 if (cast<LoadInst>(I).isAtomic()) {
1451 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
1452 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1454 Code = bitc::FUNC_CODE_INST_LOAD;
1455 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1456 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1458 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1459 Vals.push_back(cast<LoadInst>(I).isVolatile());
1460 if (cast<LoadInst>(I).isAtomic()) {
1461 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
1462 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
1465 case Instruction::Store:
1466 if (cast<StoreInst>(I).isAtomic())
1467 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
1469 Code = bitc::FUNC_CODE_INST_STORE;
1470 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1471 pushValue(I.getOperand(0), InstID, Vals, VE); // val.
1472 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1473 Vals.push_back(cast<StoreInst>(I).isVolatile());
1474 if (cast<StoreInst>(I).isAtomic()) {
1475 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
1476 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
1479 case Instruction::AtomicCmpXchg:
1480 Code = bitc::FUNC_CODE_INST_CMPXCHG;
1481 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1482 pushValue(I.getOperand(1), InstID, Vals, VE); // cmp.
1483 pushValue(I.getOperand(2), InstID, Vals, VE); // newval.
1484 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
1485 Vals.push_back(GetEncodedOrdering(
1486 cast<AtomicCmpXchgInst>(I).getSuccessOrdering()));
1487 Vals.push_back(GetEncodedSynchScope(
1488 cast<AtomicCmpXchgInst>(I).getSynchScope()));
1489 Vals.push_back(GetEncodedOrdering(
1490 cast<AtomicCmpXchgInst>(I).getFailureOrdering()));
1491 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak());
1493 case Instruction::AtomicRMW:
1494 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
1495 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1496 pushValue(I.getOperand(1), InstID, Vals, VE); // val.
1497 Vals.push_back(GetEncodedRMWOperation(
1498 cast<AtomicRMWInst>(I).getOperation()));
1499 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
1500 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
1501 Vals.push_back(GetEncodedSynchScope(
1502 cast<AtomicRMWInst>(I).getSynchScope()));
1504 case Instruction::Fence:
1505 Code = bitc::FUNC_CODE_INST_FENCE;
1506 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
1507 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
1509 case Instruction::Call: {
1510 const CallInst &CI = cast<CallInst>(I);
1511 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1512 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1514 Code = bitc::FUNC_CODE_INST_CALL;
1516 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1517 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()) |
1518 unsigned(CI.isMustTailCall()) << 14);
1519 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
1521 // Emit value #'s for the fixed parameters.
1522 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
1523 // Check for labels (can happen with asm labels).
1524 if (FTy->getParamType(i)->isLabelTy())
1525 Vals.push_back(VE.getValueID(CI.getArgOperand(i)));
1527 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param.
1530 // Emit type/value pairs for varargs params.
1531 if (FTy->isVarArg()) {
1532 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
1534 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
1538 case Instruction::VAArg:
1539 Code = bitc::FUNC_CODE_INST_VAARG;
1540 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1541 pushValue(I.getOperand(0), InstID, Vals, VE); // valist.
1542 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1546 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1550 // Emit names for globals/functions etc.
1551 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1552 const ValueEnumerator &VE,
1553 BitstreamWriter &Stream) {
1554 if (VST.empty()) return;
1555 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
1557 // FIXME: Set up the abbrev, we know how many values there are!
1558 // FIXME: We know if the type names can use 7-bit ascii.
1559 SmallVector<unsigned, 64> NameVals;
1561 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1564 const ValueName &Name = *SI;
1566 // Figure out the encoding to use for the name.
1568 bool isChar6 = true;
1569 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1572 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1573 if ((unsigned char)*C & 128) {
1575 break; // don't bother scanning the rest.
1579 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
1581 // VST_ENTRY: [valueid, namechar x N]
1582 // VST_BBENTRY: [bbid, namechar x N]
1584 if (isa<BasicBlock>(SI->getValue())) {
1585 Code = bitc::VST_CODE_BBENTRY;
1587 AbbrevToUse = VST_BBENTRY_6_ABBREV;
1589 Code = bitc::VST_CODE_ENTRY;
1591 AbbrevToUse = VST_ENTRY_6_ABBREV;
1593 AbbrevToUse = VST_ENTRY_7_ABBREV;
1596 NameVals.push_back(VE.getValueID(SI->getValue()));
1597 for (const char *P = Name.getKeyData(),
1598 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
1599 NameVals.push_back((unsigned char)*P);
1601 // Emit the finished record.
1602 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
1608 /// WriteFunction - Emit a function body to the module stream.
1609 static void WriteFunction(const Function &F, ValueEnumerator &VE,
1610 BitstreamWriter &Stream) {
1611 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
1612 VE.incorporateFunction(F);
1614 SmallVector<unsigned, 64> Vals;
1616 // Emit the number of basic blocks, so the reader can create them ahead of
1618 Vals.push_back(VE.getBasicBlocks().size());
1619 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1622 // If there are function-local constants, emit them now.
1623 unsigned CstStart, CstEnd;
1624 VE.getFunctionConstantRange(CstStart, CstEnd);
1625 WriteConstants(CstStart, CstEnd, VE, Stream, false);
1627 // If there is function-local metadata, emit it now.
1628 WriteFunctionLocalMetadata(F, VE, Stream);
1630 // Keep a running idea of what the instruction ID is.
1631 unsigned InstID = CstEnd;
1633 bool NeedsMetadataAttachment = false;
1637 // Finally, emit all the instructions, in order.
1638 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1639 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1641 WriteInstruction(*I, InstID, VE, Stream, Vals);
1643 if (!I->getType()->isVoidTy())
1646 // If the instruction has metadata, write a metadata attachment later.
1647 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1649 // If the instruction has a debug location, emit it.
1650 DebugLoc DL = I->getDebugLoc();
1651 if (DL.isUnknown()) {
1653 } else if (DL == LastDL) {
1654 // Just repeat the same debug loc as last time.
1655 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1658 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1660 Vals.push_back(DL.getLine());
1661 Vals.push_back(DL.getCol());
1662 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1663 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1664 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
1671 // Emit names for all the instructions etc.
1672 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
1674 if (NeedsMetadataAttachment)
1675 WriteMetadataAttachment(F, VE, Stream);
1680 // Emit blockinfo, which defines the standard abbreviations etc.
1681 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
1682 // We only want to emit block info records for blocks that have multiple
1683 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK.
1684 // Other blocks can define their abbrevs inline.
1685 Stream.EnterBlockInfoBlock(2);
1687 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1688 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1689 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1690 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1691 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1692 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
1693 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1694 Abbv) != VST_ENTRY_8_ABBREV)
1695 llvm_unreachable("Unexpected abbrev ordering!");
1698 { // 7-bit fixed width VST_ENTRY strings.
1699 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1700 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1701 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1702 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1703 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1704 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1705 Abbv) != VST_ENTRY_7_ABBREV)
1706 llvm_unreachable("Unexpected abbrev ordering!");
1708 { // 6-bit char6 VST_ENTRY strings.
1709 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1710 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1711 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1712 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1713 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1714 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1715 Abbv) != VST_ENTRY_6_ABBREV)
1716 llvm_unreachable("Unexpected abbrev ordering!");
1718 { // 6-bit char6 VST_BBENTRY strings.
1719 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1720 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1722 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1723 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1724 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1725 Abbv) != VST_BBENTRY_6_ABBREV)
1726 llvm_unreachable("Unexpected abbrev ordering!");
1731 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1732 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1733 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1735 Log2_32_Ceil(VE.getTypes().size()+1)));
1736 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1737 Abbv) != CONSTANTS_SETTYPE_ABBREV)
1738 llvm_unreachable("Unexpected abbrev ordering!");
1741 { // INTEGER abbrev for CONSTANTS_BLOCK.
1742 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1743 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1745 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1746 Abbv) != CONSTANTS_INTEGER_ABBREV)
1747 llvm_unreachable("Unexpected abbrev ordering!");
1750 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1751 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1752 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1753 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1755 Log2_32_Ceil(VE.getTypes().size()+1)));
1756 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1758 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1759 Abbv) != CONSTANTS_CE_CAST_Abbrev)
1760 llvm_unreachable("Unexpected abbrev ordering!");
1762 { // NULL abbrev for CONSTANTS_BLOCK.
1763 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1764 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1765 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1766 Abbv) != CONSTANTS_NULL_Abbrev)
1767 llvm_unreachable("Unexpected abbrev ordering!");
1770 // FIXME: This should only use space for first class types!
1772 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1773 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1774 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
1775 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1777 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1778 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1779 Abbv) != FUNCTION_INST_LOAD_ABBREV)
1780 llvm_unreachable("Unexpected abbrev ordering!");
1782 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1783 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1784 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1785 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1786 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1788 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1789 Abbv) != FUNCTION_INST_BINOP_ABBREV)
1790 llvm_unreachable("Unexpected abbrev ordering!");
1792 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1793 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1794 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1797 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1799 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1800 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1801 llvm_unreachable("Unexpected abbrev ordering!");
1803 { // INST_CAST abbrev for FUNCTION_BLOCK.
1804 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1805 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1806 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1807 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1808 Log2_32_Ceil(VE.getTypes().size()+1)));
1809 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1810 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1811 Abbv) != FUNCTION_INST_CAST_ABBREV)
1812 llvm_unreachable("Unexpected abbrev ordering!");
1815 { // INST_RET abbrev for FUNCTION_BLOCK.
1816 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1817 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1818 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1819 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
1820 llvm_unreachable("Unexpected abbrev ordering!");
1822 { // INST_RET abbrev for FUNCTION_BLOCK.
1823 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1824 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1825 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1826 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1827 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
1828 llvm_unreachable("Unexpected abbrev ordering!");
1830 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1831 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1832 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1833 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1834 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
1835 llvm_unreachable("Unexpected abbrev ordering!");
1841 // Sort the Users based on the order in which the reader parses the bitcode
1843 static bool bitcodereader_order(const User *lhs, const User *rhs) {
1848 static void WriteUseList(const Value *V, const ValueEnumerator &VE,
1849 BitstreamWriter &Stream) {
1851 // One or zero uses can't get out of order.
1852 if (V->use_empty() || V->hasNUses(1))
1855 // Make a copy of the in-memory use-list for sorting.
1856 SmallVector<const User*, 8> UserList(V->user_begin(), V->user_end());
1858 // Sort the copy based on the order read by the BitcodeReader.
1859 std::sort(UserList.begin(), UserList.end(), bitcodereader_order);
1861 // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the
1862 // sorted list (i.e., the expected BitcodeReader in-memory use-list).
1864 // TODO: Emit the USELIST_CODE_ENTRYs.
1867 static void WriteFunctionUseList(const Function *F, ValueEnumerator &VE,
1868 BitstreamWriter &Stream) {
1869 VE.incorporateFunction(*F);
1871 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1873 WriteUseList(AI, VE, Stream);
1874 for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE;
1876 WriteUseList(BB, VE, Stream);
1877 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
1879 WriteUseList(II, VE, Stream);
1880 for (User::const_op_iterator OI = II->op_begin(), E = II->op_end();
1882 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
1883 isa<InlineAsm>(*OI))
1884 WriteUseList(*OI, VE, Stream);
1892 static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE,
1893 BitstreamWriter &Stream) {
1894 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
1896 // XXX: this modifies the module, but in a way that should never change the
1897 // behavior of any pass or codegen in LLVM. The problem is that GVs may
1898 // contain entries in the use_list that do not exist in the Module and are
1899 // not stored in the .bc file.
1900 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1902 I->removeDeadConstantUsers();
1904 // Write the global variables.
1905 for (Module::const_global_iterator GI = M->global_begin(),
1906 GE = M->global_end(); GI != GE; ++GI) {
1907 WriteUseList(GI, VE, Stream);
1909 // Write the global variable initializers.
1910 if (GI->hasInitializer())
1911 WriteUseList(GI->getInitializer(), VE, Stream);
1914 // Write the functions.
1915 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
1916 WriteUseList(FI, VE, Stream);
1917 if (!FI->isDeclaration())
1918 WriteFunctionUseList(FI, VE, Stream);
1919 if (FI->hasPrefixData())
1920 WriteUseList(FI->getPrefixData(), VE, Stream);
1923 // Write the aliases.
1924 for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end();
1926 WriteUseList(AI, VE, Stream);
1927 WriteUseList(AI->getAliasee(), VE, Stream);
1933 /// WriteModule - Emit the specified module to the bitstream.
1934 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1935 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
1937 SmallVector<unsigned, 1> Vals;
1938 unsigned CurVersion = 1;
1939 Vals.push_back(CurVersion);
1940 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1942 // Analyze the module, enumerating globals, functions, etc.
1943 ValueEnumerator VE(M);
1945 // Emit blockinfo, which defines the standard abbreviations etc.
1946 WriteBlockInfo(VE, Stream);
1948 // Emit information about attribute groups.
1949 WriteAttributeGroupTable(VE, Stream);
1951 // Emit information about parameter attributes.
1952 WriteAttributeTable(VE, Stream);
1954 // Emit information describing all of the types in the module.
1955 WriteTypeTable(VE, Stream);
1957 writeComdats(VE, Stream);
1959 // Emit top-level description of module, including target triple, inline asm,
1960 // descriptors for global variables, and function prototype info.
1961 WriteModuleInfo(M, VE, Stream);
1964 WriteModuleConstants(VE, Stream);
1967 WriteModuleMetadata(M, VE, Stream);
1970 WriteModuleMetadataStore(M, Stream);
1972 // Emit names for globals/functions etc.
1973 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
1976 if (EnablePreserveUseListOrdering)
1977 WriteModuleUseLists(M, VE, Stream);
1979 // Emit function bodies.
1980 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1981 if (!F->isDeclaration())
1982 WriteFunction(*F, VE, Stream);
1987 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1988 /// header and trailer to make it compatible with the system archiver. To do
1989 /// this we emit the following header, and then emit a trailer that pads the
1990 /// file out to be a multiple of 16 bytes.
1992 /// struct bc_header {
1993 /// uint32_t Magic; // 0x0B17C0DE
1994 /// uint32_t Version; // Version, currently always 0.
1995 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1996 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
1997 /// uint32_t CPUType; // CPU specifier.
1998 /// ... potentially more later ...
2001 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
2002 DarwinBCHeaderSize = 5*4
2005 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
2006 uint32_t &Position) {
2007 Buffer[Position + 0] = (unsigned char) (Value >> 0);
2008 Buffer[Position + 1] = (unsigned char) (Value >> 8);
2009 Buffer[Position + 2] = (unsigned char) (Value >> 16);
2010 Buffer[Position + 3] = (unsigned char) (Value >> 24);
2014 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
2016 unsigned CPUType = ~0U;
2018 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
2019 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
2020 // number from /usr/include/mach/machine.h. It is ok to reproduce the
2021 // specific constants here because they are implicitly part of the Darwin ABI.
2023 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
2024 DARWIN_CPU_TYPE_X86 = 7,
2025 DARWIN_CPU_TYPE_ARM = 12,
2026 DARWIN_CPU_TYPE_POWERPC = 18
2029 Triple::ArchType Arch = TT.getArch();
2030 if (Arch == Triple::x86_64)
2031 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
2032 else if (Arch == Triple::x86)
2033 CPUType = DARWIN_CPU_TYPE_X86;
2034 else if (Arch == Triple::ppc)
2035 CPUType = DARWIN_CPU_TYPE_POWERPC;
2036 else if (Arch == Triple::ppc64)
2037 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
2038 else if (Arch == Triple::arm || Arch == Triple::thumb)
2039 CPUType = DARWIN_CPU_TYPE_ARM;
2041 // Traditional Bitcode starts after header.
2042 assert(Buffer.size() >= DarwinBCHeaderSize &&
2043 "Expected header size to be reserved");
2044 unsigned BCOffset = DarwinBCHeaderSize;
2045 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
2047 // Write the magic and version.
2048 unsigned Position = 0;
2049 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position);
2050 WriteInt32ToBuffer(0 , Buffer, Position); // Version.
2051 WriteInt32ToBuffer(BCOffset , Buffer, Position);
2052 WriteInt32ToBuffer(BCSize , Buffer, Position);
2053 WriteInt32ToBuffer(CPUType , Buffer, Position);
2055 // If the file is not a multiple of 16 bytes, insert dummy padding.
2056 while (Buffer.size() & 15)
2057 Buffer.push_back(0);
2060 /// WriteBitcodeToFile - Write the specified module to the specified output
2062 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
2063 SmallVector<char, 0> Buffer;
2064 Buffer.reserve(256*1024);
2066 // If this is darwin or another generic macho target, reserve space for the
2068 Triple TT(M->getTargetTriple());
2069 if (TT.isOSDarwin())
2070 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
2072 // Emit the module into the buffer.
2074 BitstreamWriter Stream(Buffer);
2076 // Emit the file header.
2077 Stream.Emit((unsigned)'B', 8);
2078 Stream.Emit((unsigned)'C', 8);
2079 Stream.Emit(0x0, 4);
2080 Stream.Emit(0xC, 4);
2081 Stream.Emit(0xE, 4);
2082 Stream.Emit(0xD, 4);
2085 WriteModule(M, Stream);
2088 if (TT.isOSDarwin())
2089 EmitDarwinBCHeaderAndTrailer(Buffer, TT);
2091 // Write the generated bitstream to "Out".
2092 Out.write((char*)&Buffer.front(), Buffer.size());