1 //===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Bitcode writer implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/Bitcode/BitstreamWriter.h"
16 #include "llvm/Bitcode/LLVMBitCodes.h"
17 #include "ValueEnumerator.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/ParameterAttributes.h"
23 #include "llvm/TypeSymbolTable.h"
24 #include "llvm/ValueSymbolTable.h"
25 #include "llvm/Support/MathExtras.h"
28 /// These are manifest constants used by the bitcode writer. They do not need to
29 /// be kept in sync with the reader, but need to be consistent within this file.
33 // VALUE_SYMTAB_BLOCK abbrev id's.
34 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
41 static unsigned GetEncodedCastOpcode(unsigned Opcode) {
43 default: assert(0 && "Unknown cast instruction!");
44 case Instruction::Trunc : return bitc::CAST_TRUNC;
45 case Instruction::ZExt : return bitc::CAST_ZEXT;
46 case Instruction::SExt : return bitc::CAST_SEXT;
47 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
48 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
49 case Instruction::UIToFP : return bitc::CAST_UITOFP;
50 case Instruction::SIToFP : return bitc::CAST_SITOFP;
51 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
52 case Instruction::FPExt : return bitc::CAST_FPEXT;
53 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
54 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
55 case Instruction::BitCast : return bitc::CAST_BITCAST;
59 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
61 default: assert(0 && "Unknown binary instruction!");
62 case Instruction::Add: return bitc::BINOP_ADD;
63 case Instruction::Sub: return bitc::BINOP_SUB;
64 case Instruction::Mul: return bitc::BINOP_MUL;
65 case Instruction::UDiv: return bitc::BINOP_UDIV;
66 case Instruction::FDiv:
67 case Instruction::SDiv: return bitc::BINOP_SDIV;
68 case Instruction::URem: return bitc::BINOP_UREM;
69 case Instruction::FRem:
70 case Instruction::SRem: return bitc::BINOP_SREM;
71 case Instruction::Shl: return bitc::BINOP_SHL;
72 case Instruction::LShr: return bitc::BINOP_LSHR;
73 case Instruction::AShr: return bitc::BINOP_ASHR;
74 case Instruction::And: return bitc::BINOP_AND;
75 case Instruction::Or: return bitc::BINOP_OR;
76 case Instruction::Xor: return bitc::BINOP_XOR;
82 static void WriteStringRecord(unsigned Code, const std::string &Str,
83 unsigned AbbrevToUse, BitstreamWriter &Stream) {
84 SmallVector<unsigned, 64> Vals;
86 // Code: [strchar x N]
87 for (unsigned i = 0, e = Str.size(); i != e; ++i)
88 Vals.push_back(Str[i]);
90 // Emit the finished record.
91 Stream.EmitRecord(Code, Vals, AbbrevToUse);
94 // Emit information about parameter attributes.
95 static void WriteParamAttrTable(const ValueEnumerator &VE,
96 BitstreamWriter &Stream) {
97 const std::vector<const ParamAttrsList*> &Attrs = VE.getParamAttrs();
98 if (Attrs.empty()) return;
100 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
102 SmallVector<uint64_t, 64> Record;
103 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
104 const ParamAttrsList *A = Attrs[i];
105 for (unsigned op = 0, e = A->size(); op != e; ++op) {
106 Record.push_back(A->getParamIndex(op));
107 Record.push_back(A->getParamAttrsAtIndex(op));
110 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
117 /// WriteTypeTable - Write out the type table for a module.
118 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
119 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
121 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
122 SmallVector<uint64_t, 64> TypeVals;
124 // FIXME: Set up abbrevs now that we know the width of the type fields, etc.
126 // Emit an entry count so the reader can reserve space.
127 TypeVals.push_back(TypeList.size());
128 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
131 // Loop over all of the types, emitting each in turn.
132 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
133 const Type *T = TypeList[i].first;
137 switch (T->getTypeID()) {
138 case Type::PackedStructTyID: // FIXME: Delete Type::PackedStructTyID.
139 default: assert(0 && "Unknown type!");
140 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
141 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
142 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
143 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
144 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
145 case Type::IntegerTyID:
147 Code = bitc::TYPE_CODE_INTEGER;
148 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
150 case Type::PointerTyID:
151 // POINTER: [pointee type]
152 Code = bitc::TYPE_CODE_POINTER;
153 TypeVals.push_back(VE.getTypeID(cast<PointerType>(T)->getElementType()));
156 case Type::FunctionTyID: {
157 const FunctionType *FT = cast<FunctionType>(T);
158 // FUNCTION: [isvararg, attrid, #pararms, paramty x N]
159 Code = bitc::TYPE_CODE_FUNCTION;
160 TypeVals.push_back(FT->isVarArg());
161 TypeVals.push_back(VE.getParamAttrID(FT->getParamAttrs()));
162 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
163 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
164 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
167 case Type::StructTyID: {
168 const StructType *ST = cast<StructType>(T);
169 // STRUCT: [ispacked, #elts, eltty x N]
170 Code = bitc::TYPE_CODE_STRUCT;
171 TypeVals.push_back(ST->isPacked());
172 // Output all of the element types.
173 for (StructType::element_iterator I = ST->element_begin(),
174 E = ST->element_end(); I != E; ++I)
175 TypeVals.push_back(VE.getTypeID(*I));
178 case Type::ArrayTyID: {
179 const ArrayType *AT = cast<ArrayType>(T);
180 // ARRAY: [numelts, eltty]
181 Code = bitc::TYPE_CODE_ARRAY;
182 TypeVals.push_back(AT->getNumElements());
183 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
186 case Type::VectorTyID: {
187 const VectorType *VT = cast<VectorType>(T);
188 // VECTOR [numelts, eltty]
189 Code = bitc::TYPE_CODE_VECTOR;
190 TypeVals.push_back(VT->getNumElements());
191 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
196 // Emit the finished record.
197 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
204 static unsigned getEncodedLinkage(const GlobalValue *GV) {
205 switch (GV->getLinkage()) {
206 default: assert(0 && "Invalid linkage!");
207 case GlobalValue::ExternalLinkage: return 0;
208 case GlobalValue::WeakLinkage: return 1;
209 case GlobalValue::AppendingLinkage: return 2;
210 case GlobalValue::InternalLinkage: return 3;
211 case GlobalValue::LinkOnceLinkage: return 4;
212 case GlobalValue::DLLImportLinkage: return 5;
213 case GlobalValue::DLLExportLinkage: return 6;
214 case GlobalValue::ExternalWeakLinkage: return 7;
218 static unsigned getEncodedVisibility(const GlobalValue *GV) {
219 switch (GV->getVisibility()) {
220 default: assert(0 && "Invalid visibility!");
221 case GlobalValue::DefaultVisibility: return 0;
222 case GlobalValue::HiddenVisibility: return 1;
223 case GlobalValue::ProtectedVisibility: return 2;
227 // Emit top-level description of module, including target triple, inline asm,
228 // descriptors for global variables, and function prototype info.
229 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
230 BitstreamWriter &Stream) {
231 // Emit the list of dependent libraries for the Module.
232 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
233 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
235 // Emit various pieces of data attached to a module.
236 if (!M->getTargetTriple().empty())
237 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
239 if (!M->getDataLayout().empty())
240 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
242 if (!M->getModuleInlineAsm().empty())
243 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
246 // Emit information about sections, computing how many there are. Also
247 // compute the maximum alignment value.
248 std::map<std::string, unsigned> SectionMap;
249 unsigned MaxAlignment = 0;
250 unsigned MaxGlobalType = 0;
251 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
253 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
254 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
256 if (!GV->hasSection()) continue;
257 // Give section names unique ID's.
258 unsigned &Entry = SectionMap[GV->getSection()];
259 if (Entry != 0) continue;
260 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
262 Entry = SectionMap.size();
264 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
265 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
266 if (!F->hasSection()) continue;
267 // Give section names unique ID's.
268 unsigned &Entry = SectionMap[F->getSection()];
269 if (Entry != 0) continue;
270 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
272 Entry = SectionMap.size();
275 // Emit abbrev for globals, now that we know # sections and max alignment.
276 unsigned SimpleGVarAbbrev = 0;
277 if (!M->global_empty()) {
278 // Add an abbrev for common globals with no visibility or thread localness.
279 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
280 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
281 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
282 Log2_32_Ceil(MaxGlobalType+1)));
283 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
284 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
285 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage.
286 if (MaxAlignment == 0) // Alignment.
287 Abbv->Add(BitCodeAbbrevOp(0));
289 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
290 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
291 Log2_32_Ceil(MaxEncAlignment+1)));
293 if (SectionMap.empty()) // Section.
294 Abbv->Add(BitCodeAbbrevOp(0));
296 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
297 Log2_32_Ceil(SectionMap.size()+1)));
298 // Don't bother emitting vis + thread local.
299 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
302 // Emit the global variable information.
303 SmallVector<unsigned, 64> Vals;
304 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
306 unsigned AbbrevToUse = 0;
308 // GLOBALVAR: [type, isconst, initid,
309 // linkage, alignment, section, visibility, threadlocal]
310 Vals.push_back(VE.getTypeID(GV->getType()));
311 Vals.push_back(GV->isConstant());
312 Vals.push_back(GV->isDeclaration() ? 0 :
313 (VE.getValueID(GV->getInitializer()) + 1));
314 Vals.push_back(getEncodedLinkage(GV));
315 Vals.push_back(Log2_32(GV->getAlignment())+1);
316 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
317 if (GV->isThreadLocal() ||
318 GV->getVisibility() != GlobalValue::DefaultVisibility) {
319 Vals.push_back(getEncodedVisibility(GV));
320 Vals.push_back(GV->isThreadLocal());
322 AbbrevToUse = SimpleGVarAbbrev;
325 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
329 // Emit the function proto information.
330 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
331 // FUNCTION: [type, callingconv, isproto, linkage, alignment, section,
333 Vals.push_back(VE.getTypeID(F->getType()));
334 Vals.push_back(F->getCallingConv());
335 Vals.push_back(F->isDeclaration());
336 Vals.push_back(getEncodedLinkage(F));
337 Vals.push_back(Log2_32(F->getAlignment())+1);
338 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
339 Vals.push_back(getEncodedVisibility(F));
341 unsigned AbbrevToUse = 0;
342 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
347 // Emit the alias information.
348 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
350 Vals.push_back(VE.getTypeID(AI->getType()));
351 Vals.push_back(VE.getValueID(AI->getAliasee()));
352 Vals.push_back(getEncodedLinkage(AI));
353 unsigned AbbrevToUse = 0;
354 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
360 static void WriteConstants(unsigned FirstVal, unsigned LastVal,
361 const ValueEnumerator &VE,
362 BitstreamWriter &Stream) {
363 if (FirstVal == LastVal) return;
365 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 2);
367 // FIXME: Install and use abbrevs to reduce size. Install them globally so
368 // they don't need to be reemitted for each function body.
370 SmallVector<uint64_t, 64> Record;
372 const ValueEnumerator::ValueList &Vals = VE.getValues();
373 const Type *LastTy = 0;
374 for (unsigned i = FirstVal; i != LastVal; ++i) {
375 const Value *V = Vals[i].first;
376 // If we need to switch types, do so now.
377 if (V->getType() != LastTy) {
378 LastTy = V->getType();
379 Record.push_back(VE.getTypeID(LastTy));
380 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record);
384 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
385 assert(0 && IA && "FIXME: Inline asm writing unimp!");
388 const Constant *C = cast<Constant>(V);
390 unsigned AbbrevToUse = 0;
391 if (C->isNullValue()) {
392 Code = bitc::CST_CODE_NULL;
393 } else if (isa<UndefValue>(C)) {
394 Code = bitc::CST_CODE_UNDEF;
395 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
396 if (IV->getBitWidth() <= 64) {
397 int64_t V = IV->getSExtValue();
399 Record.push_back(V << 1);
401 Record.push_back((-V << 1) | 1);
402 Code = bitc::CST_CODE_INTEGER;
403 } else { // Wide integers, > 64 bits in size.
404 // We have an arbitrary precision integer value to write whose
405 // bit width is > 64. However, in canonical unsigned integer
406 // format it is likely that the high bits are going to be zero.
407 // So, we only write the number of active words.
408 unsigned NWords = IV->getValue().getActiveWords();
409 const uint64_t *RawWords = IV->getValue().getRawData();
410 for (unsigned i = 0; i != NWords; ++i) {
411 int64_t V = RawWords[i];
413 Record.push_back(V << 1);
415 Record.push_back((-V << 1) | 1);
417 Code = bitc::CST_CODE_WIDE_INTEGER;
419 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
420 Code = bitc::CST_CODE_FLOAT;
421 if (CFP->getType() == Type::FloatTy) {
422 Record.push_back(FloatToBits((float)CFP->getValue()));
424 assert (CFP->getType() == Type::DoubleTy && "Unknown FP type!");
425 Record.push_back(DoubleToBits((double)CFP->getValue()));
427 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
428 isa<ConstantVector>(V)) {
429 Code = bitc::CST_CODE_AGGREGATE;
430 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
431 Record.push_back(VE.getValueID(C->getOperand(i)));
432 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
433 switch (CE->getOpcode()) {
435 if (Instruction::isCast(CE->getOpcode())) {
436 Code = bitc::CST_CODE_CE_CAST;
437 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
438 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
439 Record.push_back(VE.getValueID(C->getOperand(0)));
441 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
442 Code = bitc::CST_CODE_CE_BINOP;
443 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
444 Record.push_back(VE.getValueID(C->getOperand(0)));
445 Record.push_back(VE.getValueID(C->getOperand(1)));
448 case Instruction::GetElementPtr:
449 Code = bitc::CST_CODE_CE_GEP;
450 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
451 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
452 Record.push_back(VE.getValueID(C->getOperand(i)));
455 case Instruction::Select:
456 Code = bitc::CST_CODE_CE_SELECT;
457 Record.push_back(VE.getValueID(C->getOperand(0)));
458 Record.push_back(VE.getValueID(C->getOperand(1)));
459 Record.push_back(VE.getValueID(C->getOperand(2)));
461 case Instruction::ExtractElement:
462 Code = bitc::CST_CODE_CE_EXTRACTELT;
463 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
464 Record.push_back(VE.getValueID(C->getOperand(0)));
465 Record.push_back(VE.getValueID(C->getOperand(1)));
467 case Instruction::InsertElement:
468 Code = bitc::CST_CODE_CE_INSERTELT;
469 Record.push_back(VE.getValueID(C->getOperand(0)));
470 Record.push_back(VE.getValueID(C->getOperand(1)));
471 Record.push_back(VE.getValueID(C->getOperand(2)));
473 case Instruction::ShuffleVector:
474 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
475 Record.push_back(VE.getValueID(C->getOperand(0)));
476 Record.push_back(VE.getValueID(C->getOperand(1)));
477 Record.push_back(VE.getValueID(C->getOperand(2)));
479 case Instruction::ICmp:
480 case Instruction::FCmp:
481 Code = bitc::CST_CODE_CE_CMP;
482 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
483 Record.push_back(VE.getValueID(C->getOperand(0)));
484 Record.push_back(VE.getValueID(C->getOperand(1)));
485 Record.push_back(CE->getPredicate());
489 assert(0 && "Unknown constant!");
491 Stream.EmitRecord(Code, Record, AbbrevToUse);
498 static void WriteModuleConstants(const ValueEnumerator &VE,
499 BitstreamWriter &Stream) {
500 const ValueEnumerator::ValueList &Vals = VE.getValues();
502 // Find the first constant to emit, which is the first non-globalvalue value.
503 // We know globalvalues have been emitted by WriteModuleInfo.
504 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
505 if (!isa<GlobalValue>(Vals[i].first)) {
506 WriteConstants(i, Vals.size(), VE, Stream);
512 /// WriteInstruction - Emit an instruction to the specified stream.
513 static void WriteInstruction(const Instruction &I, ValueEnumerator &VE,
514 BitstreamWriter &Stream,
515 SmallVector<unsigned, 64> &Vals) {
517 unsigned AbbrevToUse = 0;
518 switch (I.getOpcode()) {
520 if (Instruction::isCast(I.getOpcode())) {
521 Code = bitc::FUNC_CODE_INST_CAST;
522 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
523 Vals.push_back(VE.getTypeID(I.getType()));
524 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
525 Vals.push_back(VE.getValueID(I.getOperand(0)));
527 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
528 Code = bitc::FUNC_CODE_INST_BINOP;
529 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
530 Vals.push_back(VE.getTypeID(I.getType()));
531 Vals.push_back(VE.getValueID(I.getOperand(0)));
532 Vals.push_back(VE.getValueID(I.getOperand(1)));
536 case Instruction::GetElementPtr:
537 Code = bitc::FUNC_CODE_INST_GEP;
538 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
539 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
540 Vals.push_back(VE.getValueID(I.getOperand(i)));
543 case Instruction::Select:
544 Code = bitc::FUNC_CODE_INST_SELECT;
545 Vals.push_back(VE.getTypeID(I.getType()));
546 Vals.push_back(VE.getValueID(I.getOperand(0)));
547 Vals.push_back(VE.getValueID(I.getOperand(1)));
548 Vals.push_back(VE.getValueID(I.getOperand(2)));
550 case Instruction::ExtractElement:
551 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
552 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
553 Vals.push_back(VE.getValueID(I.getOperand(0)));
554 Vals.push_back(VE.getValueID(I.getOperand(1)));
556 case Instruction::InsertElement:
557 Code = bitc::FUNC_CODE_INST_INSERTELT;
558 Vals.push_back(VE.getTypeID(I.getType()));
559 Vals.push_back(VE.getValueID(I.getOperand(0)));
560 Vals.push_back(VE.getValueID(I.getOperand(1)));
561 Vals.push_back(VE.getValueID(I.getOperand(2)));
563 case Instruction::ShuffleVector:
564 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
565 Vals.push_back(VE.getTypeID(I.getType()));
566 Vals.push_back(VE.getValueID(I.getOperand(0)));
567 Vals.push_back(VE.getValueID(I.getOperand(1)));
568 Vals.push_back(VE.getValueID(I.getOperand(2)));
570 case Instruction::ICmp:
571 case Instruction::FCmp:
572 Code = bitc::FUNC_CODE_INST_CMP;
573 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
574 Vals.push_back(VE.getValueID(I.getOperand(0)));
575 Vals.push_back(VE.getValueID(I.getOperand(1)));
576 Vals.push_back(cast<CmpInst>(I).getPredicate());
579 case Instruction::Ret:
580 Code = bitc::FUNC_CODE_INST_RET;
581 if (I.getNumOperands()) {
582 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
583 Vals.push_back(VE.getValueID(I.getOperand(0)));
586 case Instruction::Br:
587 Code = bitc::FUNC_CODE_INST_BR;
588 Vals.push_back(VE.getValueID(I.getOperand(0)));
589 if (cast<BranchInst>(I).isConditional()) {
590 Vals.push_back(VE.getValueID(I.getOperand(1)));
591 Vals.push_back(VE.getValueID(I.getOperand(2)));
594 case Instruction::Switch:
595 Code = bitc::FUNC_CODE_INST_SWITCH;
596 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
597 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
598 Vals.push_back(VE.getValueID(I.getOperand(i)));
600 case Instruction::Invoke: {
601 Code = bitc::FUNC_CODE_INST_INVOKE;
602 Vals.push_back(cast<InvokeInst>(I).getCallingConv());
603 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
604 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
605 Vals.push_back(VE.getValueID(I.getOperand(1))); // normal
606 Vals.push_back(VE.getValueID(I.getOperand(2))); // unwind
608 // Emit value #'s for the fixed parameters.
609 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
610 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
611 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
612 Vals.push_back(VE.getValueID(I.getOperand(i+3))); // fixed param.
614 // Emit type/value pairs for varargs params.
615 if (FTy->isVarArg()) {
616 unsigned NumVarargs = I.getNumOperands()-3-FTy->getNumParams();
617 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
619 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
620 Vals.push_back(VE.getValueID(I.getOperand(i)));
625 case Instruction::Unwind:
626 Code = bitc::FUNC_CODE_INST_UNWIND;
628 case Instruction::Unreachable:
629 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
632 case Instruction::PHI:
633 Code = bitc::FUNC_CODE_INST_PHI;
634 Vals.push_back(VE.getTypeID(I.getType()));
635 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
636 Vals.push_back(VE.getValueID(I.getOperand(i)));
639 case Instruction::Malloc:
640 Code = bitc::FUNC_CODE_INST_MALLOC;
641 Vals.push_back(VE.getTypeID(I.getType()));
642 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
643 Vals.push_back(Log2_32(cast<MallocInst>(I).getAlignment())+1);
646 case Instruction::Free:
647 Code = bitc::FUNC_CODE_INST_FREE;
648 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
649 Vals.push_back(VE.getValueID(I.getOperand(0)));
652 case Instruction::Alloca:
653 Code = bitc::FUNC_CODE_INST_ALLOCA;
654 Vals.push_back(VE.getTypeID(I.getType()));
655 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
656 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
659 case Instruction::Load:
660 Code = bitc::FUNC_CODE_INST_LOAD;
661 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
662 Vals.push_back(VE.getValueID(I.getOperand(0))); // ptr.
663 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
664 Vals.push_back(cast<LoadInst>(I).isVolatile());
666 case Instruction::Store:
667 Code = bitc::FUNC_CODE_INST_STORE;
668 Vals.push_back(VE.getTypeID(I.getOperand(1)->getType())); // Pointer
669 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
670 Vals.push_back(VE.getValueID(I.getOperand(1))); // ptr.
671 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
672 Vals.push_back(cast<StoreInst>(I).isVolatile());
674 case Instruction::Call: {
675 Code = bitc::FUNC_CODE_INST_CALL;
676 Vals.push_back((cast<CallInst>(I).getCallingConv() << 1) |
677 cast<CallInst>(I).isTailCall());
678 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
679 Vals.push_back(VE.getValueID(I.getOperand(0))); // callee
681 // Emit value #'s for the fixed parameters.
682 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
683 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
684 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
685 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
687 // Emit type/value pairs for varargs params.
688 if (FTy->isVarArg()) {
689 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
690 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
692 Vals.push_back(VE.getTypeID(I.getOperand(i)->getType()));
693 Vals.push_back(VE.getValueID(I.getOperand(i)));
698 case Instruction::VAArg:
699 Code = bitc::FUNC_CODE_INST_VAARG;
700 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
701 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
702 Vals.push_back(VE.getTypeID(I.getType())); // restype.
706 Stream.EmitRecord(Code, Vals, AbbrevToUse);
710 // Emit names for globals/functions etc.
711 static void WriteValueSymbolTable(const ValueSymbolTable &VST,
712 const ValueEnumerator &VE,
713 BitstreamWriter &Stream) {
714 if (VST.empty()) return;
715 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
717 // FIXME: Set up the abbrev, we know how many values there are!
718 // FIXME: We know if the type names can use 7-bit ascii.
719 SmallVector<unsigned, 64> NameVals;
721 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
724 const ValueName &Name = *SI;
726 // Figure out the encoding to use for the name.
729 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
732 isChar6 = BitCodeAbbrevOp::isChar6(*C);
733 if ((unsigned char)*C & 128) {
735 break; // don't bother scanning the rest.
739 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
741 // VST_ENTRY: [valueid, namechar x N]
742 // VST_BBENTRY: [bbid, namechar x N]
744 if (isa<BasicBlock>(SI->getValue())) {
745 Code = bitc::VST_CODE_BBENTRY;
747 AbbrevToUse = VST_BBENTRY_6_ABBREV;
749 Code = bitc::VST_CODE_ENTRY;
751 AbbrevToUse = VST_ENTRY_6_ABBREV;
753 AbbrevToUse = VST_ENTRY_7_ABBREV;
756 NameVals.push_back(VE.getValueID(SI->getValue()));
757 for (const char *P = Name.getKeyData(),
758 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
759 NameVals.push_back((unsigned char)*P);
761 // Emit the finished record.
762 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
768 /// WriteFunction - Emit a function body to the module stream.
769 static void WriteFunction(const Function &F, ValueEnumerator &VE,
770 BitstreamWriter &Stream) {
771 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 3);
772 VE.incorporateFunction(F);
774 SmallVector<unsigned, 64> Vals;
776 // Emit the number of basic blocks, so the reader can create them ahead of
778 Vals.push_back(VE.getBasicBlocks().size());
779 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
782 // FIXME: Function attributes?
784 // If there are function-local constants, emit them now.
785 unsigned CstStart, CstEnd;
786 VE.getFunctionConstantRange(CstStart, CstEnd);
787 WriteConstants(CstStart, CstEnd, VE, Stream);
789 // Finally, emit all the instructions, in order.
790 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
791 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
792 WriteInstruction(*I, VE, Stream, Vals);
794 // Emit names for all the instructions etc.
795 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
801 /// WriteTypeSymbolTable - Emit a block for the specified type symtab.
802 static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
803 const ValueEnumerator &VE,
804 BitstreamWriter &Stream) {
805 if (TST.empty()) return;
807 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
809 // 7-bit fixed width VST_CODE_ENTRY strings.
810 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
811 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
812 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
813 Log2_32_Ceil(VE.getTypes().size()+1)));
814 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
816 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
818 SmallVector<unsigned, 64> NameVals;
820 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
822 // TST_ENTRY: [typeid, namechar x N]
823 NameVals.push_back(VE.getTypeID(TI->second));
825 const std::string &Str = TI->first;
827 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
828 NameVals.push_back((unsigned char)Str[i]);
833 // Emit the finished record.
834 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
842 /// WriteModule - Emit the specified module to the bitstream.
843 static void WriteModule(const Module *M, BitstreamWriter &Stream) {
844 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
846 // Emit the version number if it is non-zero.
848 SmallVector<unsigned, 1> Vals;
849 Vals.push_back(CurVersion);
850 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
853 // Analyze the module, enumerating globals, functions, etc.
854 ValueEnumerator VE(M);
856 // Emit information about parameter attributes.
857 WriteParamAttrTable(VE, Stream);
859 // Emit information describing all of the types in the module.
860 WriteTypeTable(VE, Stream);
862 // Emit top-level description of module, including target triple, inline asm,
863 // descriptors for global variables, and function prototype info.
864 WriteModuleInfo(M, VE, Stream);
867 WriteModuleConstants(VE, Stream);
869 // If we have any aggregate values in the value table, purge them - these can
870 // only be used to initialize global variables. Doing so makes the value
871 // namespace smaller for code in functions.
872 int NumNonAggregates = VE.PurgeAggregateValues();
873 if (NumNonAggregates != -1) {
874 SmallVector<unsigned, 1> Vals;
875 Vals.push_back(NumNonAggregates);
876 Stream.EmitRecord(bitc::MODULE_CODE_PURGEVALS, Vals);
879 // Emit function bodies.
880 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
881 if (!I->isDeclaration())
882 WriteFunction(*I, VE, Stream);
884 // Emit the type symbol table information.
885 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
887 // Emit names for globals/functions etc.
888 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
893 // Emit blockinfo, which defines the standard abbreviations etc.
894 static void WriteBlockInfo(BitstreamWriter &Stream) {
895 // We only want to emit block info records for blocks that have multiple
896 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
897 // blocks can defined their abbrevs inline.
898 Stream.EnterBlockInfoBlock(2);
900 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
901 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
902 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
903 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
904 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
905 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
906 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
907 Abbv) != VST_ENTRY_8_ABBREV)
908 assert(0 && "Unexpected abbrev ordering!");
911 { // 7-bit fixed width VST_ENTRY strings.
912 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
913 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
914 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
915 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
916 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
917 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
918 Abbv) != VST_ENTRY_7_ABBREV)
919 assert(0 && "Unexpected abbrev ordering!");
921 { // 6-bit char6 VST_ENTRY strings.
922 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
923 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
924 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
927 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
928 Abbv) != VST_ENTRY_6_ABBREV)
929 assert(0 && "Unexpected abbrev ordering!");
931 { // 6-bit char6 VST_BBENTRY strings.
932 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
933 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
934 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
935 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
937 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
938 Abbv) != VST_BBENTRY_6_ABBREV)
939 assert(0 && "Unexpected abbrev ordering!");
946 /// WriteBitcodeToFile - Write the specified module to the specified output
948 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
949 std::vector<unsigned char> Buffer;
950 BitstreamWriter Stream(Buffer);
952 Buffer.reserve(256*1024);
954 // Emit the file header.
955 Stream.Emit((unsigned)'B', 8);
956 Stream.Emit((unsigned)'C', 8);
962 // Emit blockinfo, which defines the standard abbreviations etc.
963 WriteBlockInfo(Stream);
966 WriteModule(M, Stream);
968 // Write the generated bitstream to "Out".
969 Out.write((char*)&Buffer.front(), Buffer.size());