blob: bc218b3ca6a2e2a47cbca65472168784e33c3ecd [file] [log] [blame]
Chris Lattner1a9df8e2007-04-29 05:31:57 +00001//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===//
Chris Lattnerfd57cec2007-04-22 06:24:45 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Bitcode writer implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Bitcode/ReaderWriter.h"
15#include "llvm/Bitcode/BitstreamWriter.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000016#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "ValueEnumerator.h"
Chris Lattner2edd22b2007-04-24 00:16:04 +000018#include "llvm/Constants.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/DerivedTypes.h"
Chris Lattner2bce93a2007-05-06 01:58:20 +000020#include "llvm/InlineAsm.h"
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +000021#include "llvm/Instructions.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000022#include "llvm/Module.h"
Dan Gohman1224c382009-07-20 21:19:07 +000023#include "llvm/Operator.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000024#include "llvm/TypeSymbolTable.h"
Chris Lattnerb992be12007-04-23 20:35:01 +000025#include "llvm/ValueSymbolTable.h"
Evan Chenge889c692011-06-14 01:51:33 +000026#include "llvm/ADT/Triple.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbard1ce3b42008-10-22 17:39:14 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Program.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000031#include <cctype>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000032using namespace llvm;
33
Chris Lattner59698302007-05-04 20:52:02 +000034/// These are manifest constants used by the bitcode writer. They do not need to
35/// be kept in sync with the reader, but need to be consistent within this file.
36enum {
37 CurVersion = 0,
Daniel Dunbara279bc32009-09-20 02:20:51 +000038
Chris Lattner59698302007-05-04 20:52:02 +000039 // VALUE_SYMTAB_BLOCK abbrev id's.
40 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000041 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000042 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000043 VST_BBENTRY_6_ABBREV,
Daniel Dunbara279bc32009-09-20 02:20:51 +000044
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000045 // CONSTANTS_BLOCK abbrev id's.
46 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
47 CONSTANTS_INTEGER_ABBREV,
48 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000049 CONSTANTS_NULL_Abbrev,
Daniel Dunbara279bc32009-09-20 02:20:51 +000050
Chris Lattner440168b2007-05-05 07:44:49 +000051 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000052 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000053 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman1224c382009-07-20 21:19:07 +000054 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000055 FUNCTION_INST_CAST_ABBREV,
Chris Lattner94687ac2007-05-06 01:28:01 +000056 FUNCTION_INST_RET_VOID_ABBREV,
57 FUNCTION_INST_RET_VAL_ABBREV,
58 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000059};
60
Chris Lattnerfd57cec2007-04-22 06:24:45 +000061
Chris Lattnerf581c3b2007-04-24 07:07:11 +000062static unsigned GetEncodedCastOpcode(unsigned Opcode) {
63 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000064 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattnerf581c3b2007-04-24 07:07:11 +000065 case Instruction::Trunc : return bitc::CAST_TRUNC;
66 case Instruction::ZExt : return bitc::CAST_ZEXT;
67 case Instruction::SExt : return bitc::CAST_SEXT;
68 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
69 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
70 case Instruction::UIToFP : return bitc::CAST_UITOFP;
71 case Instruction::SIToFP : return bitc::CAST_SITOFP;
72 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
73 case Instruction::FPExt : return bitc::CAST_FPEXT;
74 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
75 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
76 case Instruction::BitCast : return bitc::CAST_BITCAST;
77 }
78}
79
80static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
81 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000082 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +000083 case Instruction::Add:
84 case Instruction::FAdd: return bitc::BINOP_ADD;
85 case Instruction::Sub:
86 case Instruction::FSub: return bitc::BINOP_SUB;
87 case Instruction::Mul:
88 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattnerf581c3b2007-04-24 07:07:11 +000089 case Instruction::UDiv: return bitc::BINOP_UDIV;
90 case Instruction::FDiv:
91 case Instruction::SDiv: return bitc::BINOP_SDIV;
92 case Instruction::URem: return bitc::BINOP_UREM;
93 case Instruction::FRem:
94 case Instruction::SRem: return bitc::BINOP_SREM;
95 case Instruction::Shl: return bitc::BINOP_SHL;
96 case Instruction::LShr: return bitc::BINOP_LSHR;
97 case Instruction::AShr: return bitc::BINOP_ASHR;
98 case Instruction::And: return bitc::BINOP_AND;
99 case Instruction::Or: return bitc::BINOP_OR;
100 case Instruction::Xor: return bitc::BINOP_XOR;
101 }
102}
103
Daniel Dunbara279bc32009-09-20 02:20:51 +0000104static void WriteStringRecord(unsigned Code, const std::string &Str,
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000105 unsigned AbbrevToUse, BitstreamWriter &Stream) {
106 SmallVector<unsigned, 64> Vals;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000107
Chris Lattner15e6d172007-05-04 19:11:41 +0000108 // Code: [strchar x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000109 for (unsigned i = 0, e = Str.size(); i != e; ++i)
110 Vals.push_back(Str[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000111
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000112 // Emit the finished record.
113 Stream.EmitRecord(Code, Vals, AbbrevToUse);
114}
115
Chris Lattner62bbeea2007-05-04 00:44:52 +0000116// Emit information about parameter attributes.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000117static void WriteAttributeTable(const ValueEnumerator &VE,
Chris Lattner62bbeea2007-05-04 00:44:52 +0000118 BitstreamWriter &Stream) {
Devang Patel05988662008-09-25 21:00:45 +0000119 const std::vector<AttrListPtr> &Attrs = VE.getAttributes();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000120 if (Attrs.empty()) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000121
Chris Lattnerf0a65312007-05-04 02:59:04 +0000122 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
123
124 SmallVector<uint64_t, 64> Record;
125 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000126 const AttrListPtr &A = Attrs[i];
Chris Lattner58d74912008-03-12 17:45:29 +0000127 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000128 const AttributeWithIndex &PAWI = A.getSlot(i);
Chris Lattner58d74912008-03-12 17:45:29 +0000129 Record.push_back(PAWI.Index);
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000130
131 // FIXME: remove in LLVM 3.0
132 // Store the alignment in the bitcode as a 16-bit raw value instead of a
133 // 5-bit log2 encoded value. Shift the bits above the alignment up by
134 // 11 bits.
135 uint64_t FauxAttr = PAWI.Attrs & 0xffff;
136 if (PAWI.Attrs & Attribute::Alignment)
137 FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16);
138 FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11;
139
140 Record.push_back(FauxAttr);
Chris Lattnerf0a65312007-05-04 02:59:04 +0000141 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000142
Chris Lattnerf0a65312007-05-04 02:59:04 +0000143 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
144 Record.clear();
145 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000146
Chris Lattnerf0a65312007-05-04 02:59:04 +0000147 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000148}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000149
150/// WriteTypeTable - Write out the type table for a module.
151static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
152 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000153
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000154 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID, 4 /*count from # abbrevs */);
155 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000156
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000157 // Abbrev for TYPE_CODE_POINTER.
158 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
159 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
160 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
161 Log2_32_Ceil(VE.getTypes().size()+1)));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000162 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000163 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000164
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000165 // Abbrev for TYPE_CODE_FUNCTION.
166 Abbv = new BitCodeAbbrev();
167 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
168 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnera1afde72007-11-27 17:48:06 +0000169 Abbv->Add(BitCodeAbbrevOp(0)); // FIXME: DEAD value, remove in LLVM 3.0
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
171 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
172 Log2_32_Ceil(VE.getTypes().size()+1)));
173 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000174
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000175 // Abbrev for TYPE_CODE_STRUCT.
176 Abbv = new BitCodeAbbrev();
177 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT));
178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
181 Log2_32_Ceil(VE.getTypes().size()+1)));
182 unsigned StructAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000183
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000184 // Abbrev for TYPE_CODE_ARRAY.
185 Abbv = new BitCodeAbbrev();
186 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
189 Log2_32_Ceil(VE.getTypes().size()+1)));
190 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000192 // Emit an entry count so the reader can reserve space.
193 TypeVals.push_back(TypeList.size());
194 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
195 TypeVals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000196
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000197 // Loop over all of the types, emitting each in turn.
198 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000199 const Type *T = TypeList[i];
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000200 int AbbrevToUse = 0;
201 unsigned Code = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000202
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000203 switch (T->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000204 default: llvm_unreachable("Unknown type!");
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000205 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
206 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
207 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000208 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
209 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
210 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000211 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
212 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000213 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
Dale Johannesenbb811a22010-09-10 20:55:01 +0000214 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000215 case Type::IntegerTyID:
216 // INTEGER: [width]
217 Code = bitc::TYPE_CODE_INTEGER;
218 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
219 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000220 case Type::PointerTyID: {
Christopher Lambfe63fb92007-12-11 08:59:05 +0000221 const PointerType *PTy = cast<PointerType>(T);
Christopher Lambd49e18d2007-12-12 08:44:39 +0000222 // POINTER: [pointee type, address space]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000223 Code = bitc::TYPE_CODE_POINTER;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000224 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000225 unsigned AddressSpace = PTy->getAddressSpace();
226 TypeVals.push_back(AddressSpace);
227 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000228 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000229 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000230 case Type::FunctionTyID: {
231 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnera1afde72007-11-27 17:48:06 +0000232 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000233 Code = bitc::TYPE_CODE_FUNCTION;
234 TypeVals.push_back(FT->isVarArg());
Chris Lattnera1afde72007-11-27 17:48:06 +0000235 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000236 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000237 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
238 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000239 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000240 break;
241 }
242 case Type::StructTyID: {
243 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000244 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000245 Code = bitc::TYPE_CODE_STRUCT;
246 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000247 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000248 for (StructType::element_iterator I = ST->element_begin(),
249 E = ST->element_end(); I != E; ++I)
250 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000251 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000252 break;
253 }
254 case Type::ArrayTyID: {
255 const ArrayType *AT = cast<ArrayType>(T);
256 // ARRAY: [numelts, eltty]
257 Code = bitc::TYPE_CODE_ARRAY;
258 TypeVals.push_back(AT->getNumElements());
259 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000260 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000261 break;
262 }
263 case Type::VectorTyID: {
264 const VectorType *VT = cast<VectorType>(T);
265 // VECTOR [numelts, eltty]
266 Code = bitc::TYPE_CODE_VECTOR;
267 TypeVals.push_back(VT->getNumElements());
268 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
269 break;
270 }
271 }
272
273 // Emit the finished record.
274 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
275 TypeVals.clear();
276 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000277
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000278 Stream.ExitBlock();
279}
280
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000281static unsigned getEncodedLinkage(const GlobalValue *GV) {
282 switch (GV->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000283 default: llvm_unreachable("Invalid linkage!");
Bill Wendling55ae5152010-08-20 22:05:50 +0000284 case GlobalValue::ExternalLinkage: return 0;
285 case GlobalValue::WeakAnyLinkage: return 1;
286 case GlobalValue::AppendingLinkage: return 2;
287 case GlobalValue::InternalLinkage: return 3;
288 case GlobalValue::LinkOnceAnyLinkage: return 4;
289 case GlobalValue::DLLImportLinkage: return 5;
290 case GlobalValue::DLLExportLinkage: return 6;
291 case GlobalValue::ExternalWeakLinkage: return 7;
292 case GlobalValue::CommonLinkage: return 8;
293 case GlobalValue::PrivateLinkage: return 9;
294 case GlobalValue::WeakODRLinkage: return 10;
295 case GlobalValue::LinkOnceODRLinkage: return 11;
296 case GlobalValue::AvailableExternallyLinkage: return 12;
297 case GlobalValue::LinkerPrivateLinkage: return 13;
298 case GlobalValue::LinkerPrivateWeakLinkage: return 14;
299 case GlobalValue::LinkerPrivateWeakDefAutoLinkage: return 15;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300 }
301}
302
303static unsigned getEncodedVisibility(const GlobalValue *GV) {
304 switch (GV->getVisibility()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000305 default: llvm_unreachable("Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000306 case GlobalValue::DefaultVisibility: return 0;
307 case GlobalValue::HiddenVisibility: return 1;
308 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000309 }
310}
311
312// Emit top-level description of module, including target triple, inline asm,
313// descriptors for global variables, and function prototype info.
314static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
315 BitstreamWriter &Stream) {
316 // Emit the list of dependent libraries for the Module.
317 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
318 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
319
320 // Emit various pieces of data attached to a module.
321 if (!M->getTargetTriple().empty())
322 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
323 0/*TODO*/, Stream);
324 if (!M->getDataLayout().empty())
325 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
326 0/*TODO*/, Stream);
327 if (!M->getModuleInlineAsm().empty())
328 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
329 0/*TODO*/, Stream);
330
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000331 // Emit information about sections and GC, computing how many there are. Also
332 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000333 std::map<std::string, unsigned> SectionMap;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000334 std::map<std::string, unsigned> GCMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000335 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000336 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000337 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
338 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000339 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000340 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000341
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000342 if (!GV->hasSection()) continue;
343 // Give section names unique ID's.
344 unsigned &Entry = SectionMap[GV->getSection()];
345 if (Entry != 0) continue;
346 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
347 0/*TODO*/, Stream);
348 Entry = SectionMap.size();
349 }
350 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000351 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000352 if (F->hasSection()) {
353 // Give section names unique ID's.
354 unsigned &Entry = SectionMap[F->getSection()];
355 if (!Entry) {
356 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
357 0/*TODO*/, Stream);
358 Entry = SectionMap.size();
359 }
360 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000361 if (F->hasGC()) {
362 // Same for GC names.
363 unsigned &Entry = GCMap[F->getGC()];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000364 if (!Entry) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000365 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000366 0/*TODO*/, Stream);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000367 Entry = GCMap.size();
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000368 }
369 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000370 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000371
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000372 // Emit abbrev for globals, now that we know # sections and max alignment.
373 unsigned SimpleGVarAbbrev = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000374 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000375 // Add an abbrev for common globals with no visibility or thread localness.
376 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
377 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000378 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000379 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000380 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Dale Johannesen66676462008-05-15 20:49:28 +0000382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000383 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000384 Abbv->Add(BitCodeAbbrevOp(0));
385 else {
386 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000387 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000388 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000389 }
390 if (SectionMap.empty()) // Section.
391 Abbv->Add(BitCodeAbbrevOp(0));
392 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000393 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000394 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000395 // Don't bother emitting vis + thread local.
396 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
397 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000398
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000399 // Emit the global variable information.
400 SmallVector<unsigned, 64> Vals;
401 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
402 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000403 unsigned AbbrevToUse = 0;
404
Daniel Dunbara279bc32009-09-20 02:20:51 +0000405 // GLOBALVAR: [type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +0000406 // linkage, alignment, section, visibility, threadlocal,
407 // unnamed_addr]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000408 Vals.push_back(VE.getTypeID(GV->getType()));
409 Vals.push_back(GV->isConstant());
410 Vals.push_back(GV->isDeclaration() ? 0 :
411 (VE.getValueID(GV->getInitializer()) + 1));
412 Vals.push_back(getEncodedLinkage(GV));
413 Vals.push_back(Log2_32(GV->getAlignment())+1);
414 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000415 if (GV->isThreadLocal() ||
Rafael Espindolabea46262011-01-08 16:42:36 +0000416 GV->getVisibility() != GlobalValue::DefaultVisibility ||
417 GV->hasUnnamedAddr()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000418 Vals.push_back(getEncodedVisibility(GV));
419 Vals.push_back(GV->isThreadLocal());
Rafael Espindolabea46262011-01-08 16:42:36 +0000420 Vals.push_back(GV->hasUnnamedAddr());
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000421 } else {
422 AbbrevToUse = SimpleGVarAbbrev;
423 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000424
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000425 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
426 Vals.clear();
427 }
428
429 // Emit the function proto information.
430 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000431 // FUNCTION: [type, callingconv, isproto, paramattr,
Rafael Espindolabea46262011-01-08 16:42:36 +0000432 // linkage, alignment, section, visibility, gc, unnamed_addr]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000433 Vals.push_back(VE.getTypeID(F->getType()));
434 Vals.push_back(F->getCallingConv());
435 Vals.push_back(F->isDeclaration());
436 Vals.push_back(getEncodedLinkage(F));
Devang Patel05988662008-09-25 21:00:45 +0000437 Vals.push_back(VE.getAttributeID(F->getAttributes()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000438 Vals.push_back(Log2_32(F->getAlignment())+1);
439 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
440 Vals.push_back(getEncodedVisibility(F));
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000441 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
Rafael Espindolabea46262011-01-08 16:42:36 +0000442 Vals.push_back(F->hasUnnamedAddr());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000443
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000444 unsigned AbbrevToUse = 0;
445 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
446 Vals.clear();
447 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000448
Chris Lattner07d98b42007-04-26 02:46:40 +0000449 // Emit the alias information.
450 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
451 AI != E; ++AI) {
452 Vals.push_back(VE.getTypeID(AI->getType()));
453 Vals.push_back(VE.getValueID(AI->getAliasee()));
454 Vals.push_back(getEncodedLinkage(AI));
Anton Korobeynikovf8342b92008-03-11 21:40:17 +0000455 Vals.push_back(getEncodedVisibility(AI));
Chris Lattner07d98b42007-04-26 02:46:40 +0000456 unsigned AbbrevToUse = 0;
457 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
458 Vals.clear();
459 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000460}
461
Dan Gohman1224c382009-07-20 21:19:07 +0000462static uint64_t GetOptimizationFlags(const Value *V) {
463 uint64_t Flags = 0;
464
465 if (const OverflowingBinaryOperator *OBO =
466 dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000467 if (OBO->hasNoSignedWrap())
468 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
469 if (OBO->hasNoUnsignedWrap())
470 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Chris Lattner35bda892011-02-06 21:44:57 +0000471 } else if (const PossiblyExactOperator *PEO =
472 dyn_cast<PossiblyExactOperator>(V)) {
473 if (PEO->isExact())
474 Flags |= 1 << bitc::PEO_EXACT;
Dan Gohman1224c382009-07-20 21:19:07 +0000475 }
476
477 return Flags;
478}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000479
Devang Patel912538b2009-08-04 05:01:35 +0000480static void WriteMDNode(const MDNode *N,
Devang Patel985fea22009-07-29 18:15:02 +0000481 const ValueEnumerator &VE,
Devang Patel912538b2009-08-04 05:01:35 +0000482 BitstreamWriter &Stream,
483 SmallVector<uint64_t, 64> &Record) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000484 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
485 if (N->getOperand(i)) {
486 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
487 Record.push_back(VE.getValueID(N->getOperand(i)));
Devang Patel912538b2009-08-04 05:01:35 +0000488 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +0000489 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
Devang Patel912538b2009-08-04 05:01:35 +0000490 Record.push_back(0);
491 }
492 }
Dan Gohman70c2fc02010-09-09 23:12:39 +0000493 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE2 :
494 bitc::METADATA_NODE2;
Victor Hernandez24e64df2010-01-10 07:14:18 +0000495 Stream.EmitRecord(MDCode, Record, 0);
Devang Patel912538b2009-08-04 05:01:35 +0000496 Record.clear();
497}
Devang Patel104cf9e2009-07-23 01:07:34 +0000498
Dan Gohman17aa92c2010-07-21 23:38:33 +0000499static void WriteModuleMetadata(const Module *M,
500 const ValueEnumerator &VE,
Devang Patel912538b2009-08-04 05:01:35 +0000501 BitstreamWriter &Stream) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000502 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
Devang Patel912538b2009-08-04 05:01:35 +0000503 bool StartedMetadataBlock = false;
504 unsigned MDSAbbrev = 0;
505 SmallVector<uint64_t, 64> Record;
506 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000507
Devang Patel912538b2009-08-04 05:01:35 +0000508 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
Victor Hernandez2c482f42010-01-29 21:19:19 +0000509 if (!N->isFunctionLocal() || !N->getFunction()) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000510 if (!StartedMetadataBlock) {
511 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
512 StartedMetadataBlock = true;
513 }
514 WriteMDNode(N, VE, Stream, Record);
Devang Patel912538b2009-08-04 05:01:35 +0000515 }
Devang Patel912538b2009-08-04 05:01:35 +0000516 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
517 if (!StartedMetadataBlock) {
518 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000519
Devang Patel70c9d172009-07-22 21:10:50 +0000520 // Abbrev for METADATA_STRING.
521 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
522 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
Devang Patele54abc92009-07-22 17:43:22 +0000523 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Devang Patel70c9d172009-07-22 21:10:50 +0000524 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
525 MDSAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel912538b2009-08-04 05:01:35 +0000526 StartedMetadataBlock = true;
Devang Patel985fea22009-07-29 18:15:02 +0000527 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000528
Devang Patel985fea22009-07-29 18:15:02 +0000529 // Code: [strchar x N]
Chris Lattner12f031d2009-10-19 05:51:03 +0000530 Record.append(MDS->begin(), MDS->end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000531
Devang Patel985fea22009-07-29 18:15:02 +0000532 // Emit the finished record.
Devang Patel104cf9e2009-07-23 01:07:34 +0000533 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
534 Record.clear();
Devang Patel912538b2009-08-04 05:01:35 +0000535 }
536 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000537
Dan Gohman17aa92c2010-07-21 23:38:33 +0000538 // Write named metadata.
539 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
540 E = M->named_metadata_end(); I != E; ++I) {
541 const NamedMDNode *NMD = I;
542 if (!StartedMetadataBlock) {
543 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
544 StartedMetadataBlock = true;
545 }
546
547 // Write name.
548 StringRef Str = NMD->getName();
549 for (unsigned i = 0, e = Str.size(); i != e; ++i)
550 Record.push_back(Str[i]);
551 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
552 Record.clear();
553
554 // Write named metadata operands.
555 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
556 Record.push_back(VE.getValueID(NMD->getOperand(i)));
Dan Gohman70c2fc02010-09-09 23:12:39 +0000557 Stream.EmitRecord(bitc::METADATA_NAMED_NODE2, Record, 0);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000558 Record.clear();
559 }
560
Devang Patel912538b2009-08-04 05:01:35 +0000561 if (StartedMetadataBlock)
Devang Patele8e02132009-09-18 19:26:43 +0000562 Stream.ExitBlock();
563}
564
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000565static void WriteFunctionLocalMetadata(const Function &F,
566 const ValueEnumerator &VE,
Victor Hernandez0c316412010-01-13 19:37:33 +0000567 BitstreamWriter &Stream) {
568 bool StartedMetadataBlock = false;
569 SmallVector<uint64_t, 64> Record;
Devang Patel62098692010-06-02 23:05:04 +0000570 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000571 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Devang Patel62098692010-06-02 23:05:04 +0000572 if (const MDNode *N = Vals[i])
Victor Hernandez2c482f42010-01-29 21:19:19 +0000573 if (N->isFunctionLocal() && N->getFunction() == &F) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000574 if (!StartedMetadataBlock) {
575 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
576 StartedMetadataBlock = true;
577 }
578 WriteMDNode(N, VE, Stream, Record);
Victor Hernandez0c316412010-01-13 19:37:33 +0000579 }
Devang Patel62098692010-06-02 23:05:04 +0000580
Victor Hernandez0c316412010-01-13 19:37:33 +0000581 if (StartedMetadataBlock)
582 Stream.ExitBlock();
583}
584
Devang Patele8e02132009-09-18 19:26:43 +0000585static void WriteMetadataAttachment(const Function &F,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000586 const ValueEnumerator &VE,
587 BitstreamWriter &Stream) {
Chris Lattnera6245242010-04-03 02:17:50 +0000588 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
589
Devang Patele8e02132009-09-18 19:26:43 +0000590 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000591
Devang Patele8e02132009-09-18 19:26:43 +0000592 // Write metadata attachments
Dan Gohman70c2fc02010-09-09 23:12:39 +0000593 // METADATA_ATTACHMENT2 - [m x [value, [n x [id, mdnode]]]
Chris Lattner3990b122009-12-28 23:41:32 +0000594 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
595
Devang Patele8e02132009-09-18 19:26:43 +0000596 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
597 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
598 I != E; ++I) {
Devang Patelf61b2372009-10-22 18:55:16 +0000599 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +0000600 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +0000601
602 // If no metadata, ignore instruction.
603 if (MDs.empty()) continue;
604
605 Record.push_back(VE.getInstructionID(I));
606
607 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
608 Record.push_back(MDs[i].first);
609 Record.push_back(VE.getValueID(MDs[i].second));
Devang Patele8e02132009-09-18 19:26:43 +0000610 }
Dan Gohman70c2fc02010-09-09 23:12:39 +0000611 Stream.EmitRecord(bitc::METADATA_ATTACHMENT2, Record, 0);
Chris Lattner3990b122009-12-28 23:41:32 +0000612 Record.clear();
Devang Patele8e02132009-09-18 19:26:43 +0000613 }
614
Chris Lattnera6245242010-04-03 02:17:50 +0000615 Stream.ExitBlock();
Devang Patele8e02132009-09-18 19:26:43 +0000616}
617
Chris Lattner7d05c462009-12-28 20:10:43 +0000618static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
Devang Patele8e02132009-09-18 19:26:43 +0000619 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000620
Devang Patele8e02132009-09-18 19:26:43 +0000621 // Write metadata kinds
622 // METADATA_KIND - [n x [id, name]]
Chris Lattner7d05c462009-12-28 20:10:43 +0000623 SmallVector<StringRef, 4> Names;
Chris Lattner08113472009-12-29 09:01:33 +0000624 M->getMDKindNames(Names);
Chris Lattner7d05c462009-12-28 20:10:43 +0000625
Dan Gohman19538d12010-07-20 21:42:28 +0000626 if (Names.empty()) return;
Chris Lattner7d05c462009-12-28 20:10:43 +0000627
628 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
629
Dan Gohman19538d12010-07-20 21:42:28 +0000630 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattner7d05c462009-12-28 20:10:43 +0000631 Record.push_back(MDKindID);
632 StringRef KName = Names[MDKindID];
633 Record.append(KName.begin(), KName.end());
634
Devang Patele8e02132009-09-18 19:26:43 +0000635 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
636 Record.clear();
637 }
638
Chris Lattner7d05c462009-12-28 20:10:43 +0000639 Stream.ExitBlock();
Devang Patel912538b2009-08-04 05:01:35 +0000640}
641
642static void WriteConstants(unsigned FirstVal, unsigned LastVal,
643 const ValueEnumerator &VE,
644 BitstreamWriter &Stream, bool isGlobal) {
645 if (FirstVal == LastVal) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000646
Devang Patel912538b2009-08-04 05:01:35 +0000647 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
648
649 unsigned AggregateAbbrev = 0;
650 unsigned String8Abbrev = 0;
651 unsigned CString7Abbrev = 0;
652 unsigned CString6Abbrev = 0;
653 // If this is a constant pool for the module, emit module-specific abbrevs.
654 if (isGlobal) {
655 // Abbrev for CST_CODE_AGGREGATE.
656 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
657 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
658 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
659 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
660 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
661
662 // Abbrev for CST_CODE_STRING.
663 Abbv = new BitCodeAbbrev();
664 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
665 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
666 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
667 String8Abbrev = Stream.EmitAbbrev(Abbv);
668 // Abbrev for CST_CODE_CSTRING.
669 Abbv = new BitCodeAbbrev();
670 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
671 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
672 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
673 CString7Abbrev = Stream.EmitAbbrev(Abbv);
674 // Abbrev for CST_CODE_CSTRING.
675 Abbv = new BitCodeAbbrev();
676 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
677 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
678 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
679 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000680 }
681
Devang Patel912538b2009-08-04 05:01:35 +0000682 SmallVector<uint64_t, 64> Record;
683
684 const ValueEnumerator::ValueList &Vals = VE.getValues();
685 const Type *LastTy = 0;
686 for (unsigned i = FirstVal; i != LastVal; ++i) {
687 const Value *V = Vals[i].first;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000688 // If we need to switch types, do so now.
689 if (V->getType() != LastTy) {
690 LastTy = V->getType();
691 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000692 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
693 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000694 Record.clear();
695 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000696
Chris Lattner2edd22b2007-04-24 00:16:04 +0000697 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesen43602982009-10-13 20:46:56 +0000698 Record.push_back(unsigned(IA->hasSideEffects()) |
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +0000699 unsigned(IA->isAlignStack()) << 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000700
Chris Lattner2bce93a2007-05-06 01:58:20 +0000701 // Add the asm string.
702 const std::string &AsmStr = IA->getAsmString();
703 Record.push_back(AsmStr.size());
704 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
705 Record.push_back(AsmStr[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000706
Chris Lattner2bce93a2007-05-06 01:58:20 +0000707 // Add the constraint string.
708 const std::string &ConstraintStr = IA->getConstraintString();
709 Record.push_back(ConstraintStr.size());
710 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
711 Record.push_back(ConstraintStr[i]);
712 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
713 Record.clear();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000714 continue;
715 }
716 const Constant *C = cast<Constant>(V);
717 unsigned Code = -1U;
718 unsigned AbbrevToUse = 0;
719 if (C->isNullValue()) {
720 Code = bitc::CST_CODE_NULL;
721 } else if (isa<UndefValue>(C)) {
722 Code = bitc::CST_CODE_UNDEF;
723 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
724 if (IV->getBitWidth() <= 64) {
Chris Lattner2359e512010-08-03 16:57:03 +0000725 uint64_t V = IV->getSExtValue();
726 if ((int64_t)V >= 0)
Chris Lattner2edd22b2007-04-24 00:16:04 +0000727 Record.push_back(V << 1);
728 else
729 Record.push_back((-V << 1) | 1);
730 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000731 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000732 } else { // Wide integers, > 64 bits in size.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000733 // We have an arbitrary precision integer value to write whose
734 // bit width is > 64. However, in canonical unsigned integer
Chris Lattner2edd22b2007-04-24 00:16:04 +0000735 // format it is likely that the high bits are going to be zero.
736 // So, we only write the number of active words.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000737 unsigned NWords = IV->getValue().getActiveWords();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000738 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000739 for (unsigned i = 0; i != NWords; ++i) {
740 int64_t V = RawWords[i];
741 if (V >= 0)
742 Record.push_back(V << 1);
743 else
744 Record.push_back((-V << 1) | 1);
745 }
746 Code = bitc::CST_CODE_WIDE_INTEGER;
747 }
748 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
749 Code = bitc::CST_CODE_FLOAT;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000750 const Type *Ty = CFP->getType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000751 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000752 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000753 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen693717f2007-09-26 23:20:33 +0000754 // api needed to prevent premature destruction
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000755 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen7111b022008-10-09 18:53:47 +0000756 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000757 const uint64_t *p = api.getRawData();
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000758 Record.push_back((p[1] << 48) | (p[0] >> 16));
759 Record.push_back(p[0] & 0xffffLL);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000760 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000761 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000762 const uint64_t *p = api.getRawData();
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000763 Record.push_back(p[0]);
764 Record.push_back(p[1]);
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000765 } else {
766 assert (0 && "Unknown FP type!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000767 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000768 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000769 const ConstantArray *CA = cast<ConstantArray>(C);
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000770 // Emit constant strings specially.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000771 unsigned NumOps = CA->getNumOperands();
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000772 // If this is a null-terminated string, use the denser CSTRING encoding.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000773 if (CA->getOperand(NumOps-1)->isNullValue()) {
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000774 Code = bitc::CST_CODE_CSTRING;
775 --NumOps; // Don't encode the null, which isn't allowed by char6.
776 } else {
777 Code = bitc::CST_CODE_STRING;
778 AbbrevToUse = String8Abbrev;
779 }
780 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
781 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
782 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000783 unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue();
Chris Lattner817f08a2007-05-06 00:42:18 +0000784 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000785 isCStr7 &= (V & 128) == 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000786 if (isCStrChar6)
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000787 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000788 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000789
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000790 if (isCStrChar6)
791 AbbrevToUse = CString6Abbrev;
792 else if (isCStr7)
793 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000794 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
Chris Lattner93b122d2010-03-16 21:25:55 +0000795 isa<ConstantVector>(V)) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000796 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000797 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
798 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000799 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000800 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000801 switch (CE->getOpcode()) {
802 default:
803 if (Instruction::isCast(CE->getOpcode())) {
804 Code = bitc::CST_CODE_CE_CAST;
805 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
806 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
807 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000808 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000809 } else {
810 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
811 Code = bitc::CST_CODE_CE_BINOP;
812 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
813 Record.push_back(VE.getValueID(C->getOperand(0)));
814 Record.push_back(VE.getValueID(C->getOperand(1)));
Dan Gohman1224c382009-07-20 21:19:07 +0000815 uint64_t Flags = GetOptimizationFlags(CE);
816 if (Flags != 0)
817 Record.push_back(Flags);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000818 }
819 break;
820 case Instruction::GetElementPtr:
821 Code = bitc::CST_CODE_CE_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +0000822 if (cast<GEPOperator>(C)->isInBounds())
823 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000824 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
825 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
826 Record.push_back(VE.getValueID(C->getOperand(i)));
827 }
828 break;
829 case Instruction::Select:
830 Code = bitc::CST_CODE_CE_SELECT;
831 Record.push_back(VE.getValueID(C->getOperand(0)));
832 Record.push_back(VE.getValueID(C->getOperand(1)));
833 Record.push_back(VE.getValueID(C->getOperand(2)));
834 break;
835 case Instruction::ExtractElement:
836 Code = bitc::CST_CODE_CE_EXTRACTELT;
837 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
838 Record.push_back(VE.getValueID(C->getOperand(0)));
839 Record.push_back(VE.getValueID(C->getOperand(1)));
840 break;
841 case Instruction::InsertElement:
842 Code = bitc::CST_CODE_CE_INSERTELT;
843 Record.push_back(VE.getValueID(C->getOperand(0)));
844 Record.push_back(VE.getValueID(C->getOperand(1)));
845 Record.push_back(VE.getValueID(C->getOperand(2)));
846 break;
847 case Instruction::ShuffleVector:
Nate Begeman0f123cf2009-02-12 21:28:33 +0000848 // If the return type and argument types are the same, this is a
849 // standard shufflevector instruction. If the types are different,
850 // then the shuffle is widening or truncating the input vectors, and
851 // the argument type must also be encoded.
852 if (C->getType() == C->getOperand(0)->getType()) {
853 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
854 } else {
855 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
856 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
857 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000858 Record.push_back(VE.getValueID(C->getOperand(0)));
859 Record.push_back(VE.getValueID(C->getOperand(1)));
860 Record.push_back(VE.getValueID(C->getOperand(2)));
861 break;
862 case Instruction::ICmp:
863 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000864 Code = bitc::CST_CODE_CE_CMP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000865 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
866 Record.push_back(VE.getValueID(C->getOperand(0)));
867 Record.push_back(VE.getValueID(C->getOperand(1)));
868 Record.push_back(CE->getPredicate());
869 break;
870 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000871 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattner837e04a2009-10-28 05:24:40 +0000872 Code = bitc::CST_CODE_BLOCKADDRESS;
873 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
874 Record.push_back(VE.getValueID(BA->getFunction()));
875 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner2edd22b2007-04-24 00:16:04 +0000876 } else {
Dan Gohman29d27162010-07-21 21:18:37 +0000877#ifndef NDEBUG
878 C->dump();
879#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000880 llvm_unreachable("Unknown constant!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000881 }
882 Stream.EmitRecord(Code, Record, AbbrevToUse);
883 Record.clear();
884 }
885
886 Stream.ExitBlock();
887}
888
889static void WriteModuleConstants(const ValueEnumerator &VE,
890 BitstreamWriter &Stream) {
891 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000892
Chris Lattner2edd22b2007-04-24 00:16:04 +0000893 // Find the first constant to emit, which is the first non-globalvalue value.
894 // We know globalvalues have been emitted by WriteModuleInfo.
895 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
896 if (!isa<GlobalValue>(Vals[i].first)) {
Devang Patel912538b2009-08-04 05:01:35 +0000897 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000898 return;
899 }
900 }
901}
Chris Lattnerb992be12007-04-23 20:35:01 +0000902
Chris Lattner7337ab92007-05-06 00:00:00 +0000903/// PushValueAndType - The file has to encode both the value and type id for
904/// many values, because we need to know what type to create for forward
905/// references. However, most operands are not forward references, so this type
906/// field is not needed.
907///
908/// This function adds V's value ID to Vals. If the value ID is higher than the
909/// instruction ID, then it is a forward reference, and it also includes the
910/// type ID.
Gabor Greif75a46eb2009-01-16 18:40:27 +0000911static bool PushValueAndType(const Value *V, unsigned InstID,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000912 SmallVector<unsigned, 64> &Vals,
Chris Lattner7337ab92007-05-06 00:00:00 +0000913 ValueEnumerator &VE) {
914 unsigned ValID = VE.getValueID(V);
915 Vals.push_back(ValID);
916 if (ValID >= InstID) {
917 Vals.push_back(VE.getTypeID(V->getType()));
918 return true;
919 }
920 return false;
921}
922
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000923/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +0000924static void WriteInstruction(const Instruction &I, unsigned InstID,
925 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000926 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000927 unsigned Code = 0;
928 unsigned AbbrevToUse = 0;
Devang Patele8e02132009-09-18 19:26:43 +0000929 VE.setInstructionID(&I);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000930 switch (I.getOpcode()) {
931 default:
932 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000933 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000934 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
935 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000936 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000937 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000938 } else {
939 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000940 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000941 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
942 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000943 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000944 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Dan Gohman1224c382009-07-20 21:19:07 +0000945 uint64_t Flags = GetOptimizationFlags(&I);
946 if (Flags != 0) {
947 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
948 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
949 Vals.push_back(Flags);
950 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000951 }
952 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000953
954 case Instruction::GetElementPtr:
955 Code = bitc::FUNC_CODE_INST_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +0000956 if (cast<GEPOperator>(&I)->isInBounds())
957 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000958 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
959 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000960 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000961 case Instruction::ExtractValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000962 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000963 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
964 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
965 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
966 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000967 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000968 }
969 case Instruction::InsertValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000970 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000971 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
972 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
973 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
974 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
975 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000976 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000977 }
Chris Lattnerd309c752007-05-01 02:13:26 +0000978 case Instruction::Select:
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +0000979 Code = bitc::FUNC_CODE_INST_VSELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000980 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000981 Vals.push_back(VE.getValueID(I.getOperand(2)));
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +0000982 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000983 break;
984 case Instruction::ExtractElement:
985 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000986 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000987 Vals.push_back(VE.getValueID(I.getOperand(1)));
988 break;
989 case Instruction::InsertElement:
990 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000991 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000992 Vals.push_back(VE.getValueID(I.getOperand(1)));
993 Vals.push_back(VE.getValueID(I.getOperand(2)));
994 break;
995 case Instruction::ShuffleVector:
996 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +0000997 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000998 Vals.push_back(VE.getValueID(I.getOperand(1)));
999 Vals.push_back(VE.getValueID(I.getOperand(2)));
1000 break;
1001 case Instruction::ICmp:
1002 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001003 // compare returning Int1Ty or vector of Int1Ty
1004 Code = bitc::FUNC_CODE_INST_CMP2;
Chris Lattner7337ab92007-05-06 00:00:00 +00001005 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001006 Vals.push_back(VE.getValueID(I.getOperand(1)));
1007 Vals.push_back(cast<CmpInst>(I).getPredicate());
1008 break;
1009
Daniel Dunbara279bc32009-09-20 02:20:51 +00001010 case Instruction::Ret:
Devang Pateld9d99ff2008-02-26 01:29:32 +00001011 {
1012 Code = bitc::FUNC_CODE_INST_RET;
1013 unsigned NumOperands = I.getNumOperands();
Devang Pateld9d99ff2008-02-26 01:29:32 +00001014 if (NumOperands == 0)
1015 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1016 else if (NumOperands == 1) {
1017 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1018 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1019 } else {
1020 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1021 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1022 }
1023 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001024 break;
1025 case Instruction::Br:
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001026 {
1027 Code = bitc::FUNC_CODE_INST_BR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001028 BranchInst &II = cast<BranchInst>(I);
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001029 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1030 if (II.isConditional()) {
1031 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1032 Vals.push_back(VE.getValueID(II.getCondition()));
1033 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001034 }
1035 break;
1036 case Instruction::Switch:
1037 Code = bitc::FUNC_CODE_INST_SWITCH;
1038 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001039 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1040 Vals.push_back(VE.getValueID(I.getOperand(i)));
1041 break;
Chris Lattnerab21db72009-10-28 00:19:10 +00001042 case Instruction::IndirectBr:
1043 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001044 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1045 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1046 Vals.push_back(VE.getValueID(I.getOperand(i)));
1047 break;
1048
Chris Lattner60ce9b52007-05-01 07:03:37 +00001049 case Instruction::Invoke: {
Gabor Greif75a46eb2009-01-16 18:40:27 +00001050 const InvokeInst *II = cast<InvokeInst>(&I);
1051 const Value *Callee(II->getCalledValue());
1052 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattnera9bb7132007-05-08 05:38:01 +00001053 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnerd309c752007-05-01 02:13:26 +00001054 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001055
Devang Patel05988662008-09-25 21:00:45 +00001056 Vals.push_back(VE.getAttributeID(II->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +00001057 Vals.push_back(II->getCallingConv());
Gabor Greif8fe53b72009-01-07 22:39:29 +00001058 Vals.push_back(VE.getValueID(II->getNormalDest()));
1059 Vals.push_back(VE.getValueID(II->getUnwindDest()));
Gabor Greif75a46eb2009-01-16 18:40:27 +00001060 PushValueAndType(Callee, InstID, Vals, VE);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001061
Chris Lattnerd309c752007-05-01 02:13:26 +00001062 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001063 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Gabor Greifc9f75002010-03-24 13:21:49 +00001064 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param.
Chris Lattnerd309c752007-05-01 02:13:26 +00001065
1066 // Emit type/value pairs for varargs params.
1067 if (FTy->isVarArg()) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001068 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattner7337ab92007-05-06 00:00:00 +00001069 i != e; ++i)
1070 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +00001071 }
1072 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001073 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001074 case Instruction::Unwind:
1075 Code = bitc::FUNC_CODE_INST_UNWIND;
1076 break;
1077 case Instruction::Unreachable:
1078 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +00001079 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001080 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001081
Chris Lattnerd309c752007-05-01 02:13:26 +00001082 case Instruction::PHI:
1083 Code = bitc::FUNC_CODE_INST_PHI;
1084 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001085 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1086 Vals.push_back(VE.getValueID(I.getOperand(i)));
1087 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001088
Chris Lattnerd309c752007-05-01 02:13:26 +00001089 case Instruction::Alloca:
1090 Code = bitc::FUNC_CODE_INST_ALLOCA;
1091 Vals.push_back(VE.getTypeID(I.getType()));
Dan Gohmana37dd3e2010-05-28 01:38:28 +00001092 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001093 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1094 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1095 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001096
Chris Lattnerd309c752007-05-01 02:13:26 +00001097 case Instruction::Load:
1098 Code = bitc::FUNC_CODE_INST_LOAD;
Chris Lattner7337ab92007-05-06 00:00:00 +00001099 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1100 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001101
Chris Lattnerd309c752007-05-01 02:13:26 +00001102 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1103 Vals.push_back(cast<LoadInst>(I).isVolatile());
1104 break;
1105 case Instruction::Store:
Christopher Lambfe63fb92007-12-11 08:59:05 +00001106 Code = bitc::FUNC_CODE_INST_STORE2;
1107 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1108 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
Chris Lattnerd309c752007-05-01 02:13:26 +00001109 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1110 Vals.push_back(cast<StoreInst>(I).isVolatile());
1111 break;
1112 case Instruction::Call: {
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001113 const CallInst &CI = cast<CallInst>(I);
1114 const PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
Chris Lattnera9bb7132007-05-08 05:38:01 +00001115 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1116
Dan Gohman70c2fc02010-09-09 23:12:39 +00001117 Code = bitc::FUNC_CODE_INST_CALL2;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001118
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001119 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1120 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1121 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
Daniel Dunbara279bc32009-09-20 02:20:51 +00001122
Chris Lattnerd309c752007-05-01 02:13:26 +00001123 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001124 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001125 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001126
Chris Lattner60ce9b52007-05-01 07:03:37 +00001127 // Emit type/value pairs for varargs params.
1128 if (FTy->isVarArg()) {
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001129 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +00001130 i != e; ++i)
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001131 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +00001132 }
1133 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001134 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001135 case Instruction::VAArg:
1136 Code = bitc::FUNC_CODE_INST_VAARG;
1137 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1138 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
1139 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1140 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001141 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001142
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001143 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1144 Vals.clear();
1145}
1146
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001147// Emit names for globals/functions etc.
1148static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1149 const ValueEnumerator &VE,
1150 BitstreamWriter &Stream) {
1151 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +00001152 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +00001153
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001154 // FIXME: Set up the abbrev, we know how many values there are!
1155 // FIXME: We know if the type names can use 7-bit ascii.
1156 SmallVector<unsigned, 64> NameVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001157
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001158 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1159 SI != SE; ++SI) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001160
Chris Lattner59698302007-05-04 20:52:02 +00001161 const ValueName &Name = *SI;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001162
Chris Lattner59698302007-05-04 20:52:02 +00001163 // Figure out the encoding to use for the name.
1164 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +00001165 bool isChar6 = true;
1166 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1167 C != E; ++C) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001168 if (isChar6)
Chris Lattnerff294a42007-05-05 01:26:50 +00001169 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1170 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +00001171 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +00001172 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +00001173 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001174 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001175
Chris Lattnerfd1ae952007-05-04 21:31:13 +00001176 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001177
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001178 // VST_ENTRY: [valueid, namechar x N]
1179 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +00001180 unsigned Code;
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001181 if (isa<BasicBlock>(SI->getValue())) {
1182 Code = bitc::VST_CODE_BBENTRY;
1183 if (isChar6)
1184 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001185 } else {
1186 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +00001187 if (isChar6)
1188 AbbrevToUse = VST_ENTRY_6_ABBREV;
1189 else if (is7Bit)
1190 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001191 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001192
Chris Lattnere825ed52007-05-03 22:18:21 +00001193 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +00001194 for (const char *P = Name.getKeyData(),
1195 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001196 NameVals.push_back((unsigned char)*P);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001197
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001198 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +00001199 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001200 NameVals.clear();
1201 }
1202 Stream.ExitBlock();
1203}
1204
Chris Lattner8d35c792007-04-26 03:50:57 +00001205/// WriteFunction - Emit a function body to the module stream.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001206static void WriteFunction(const Function &F, ValueEnumerator &VE,
Chris Lattner198f34a2007-04-26 03:27:58 +00001207 BitstreamWriter &Stream) {
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001208 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a0c04d2011-06-03 17:02:19 +00001209 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001210
1211 SmallVector<unsigned, 64> Vals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001212
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001213 // Emit the number of basic blocks, so the reader can create them ahead of
1214 // time.
1215 Vals.push_back(VE.getBasicBlocks().size());
1216 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1217 Vals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001218
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001219 // If there are function-local constants, emit them now.
1220 unsigned CstStart, CstEnd;
1221 VE.getFunctionConstantRange(CstStart, CstEnd);
Devang Patel912538b2009-08-04 05:01:35 +00001222 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001223
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001224 // If there is function-local metadata, emit it now.
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +00001225 WriteFunctionLocalMetadata(F, VE, Stream);
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001226
Daniel Dunbara279bc32009-09-20 02:20:51 +00001227 // Keep a running idea of what the instruction ID is.
Chris Lattner7337ab92007-05-06 00:00:00 +00001228 unsigned InstID = CstEnd;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001229
Chris Lattnera6245242010-04-03 02:17:50 +00001230 bool NeedsMetadataAttachment = false;
1231
1232 DebugLoc LastDL;
1233
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001234 // Finally, emit all the instructions, in order.
Nick Lewycky280a6e62008-04-25 16:53:59 +00001235 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +00001236 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1237 I != E; ++I) {
1238 WriteInstruction(*I, InstID, VE, Stream, Vals);
Chris Lattnera6245242010-04-03 02:17:50 +00001239
Chris Lattner3990b122009-12-28 23:41:32 +00001240 if (!I->getType()->isVoidTy())
Chris Lattner7337ab92007-05-06 00:00:00 +00001241 ++InstID;
Chris Lattnera6245242010-04-03 02:17:50 +00001242
1243 // If the instruction has metadata, write a metadata attachment later.
1244 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1245
1246 // If the instruction has a debug location, emit it.
1247 DebugLoc DL = I->getDebugLoc();
1248 if (DL.isUnknown()) {
1249 // nothing todo.
1250 } else if (DL == LastDL) {
1251 // Just repeat the same debug loc as last time.
1252 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1253 } else {
1254 MDNode *Scope, *IA;
1255 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1256
1257 Vals.push_back(DL.getLine());
1258 Vals.push_back(DL.getCol());
1259 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1260 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
Dan Gohman70c2fc02010-09-09 23:12:39 +00001261 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC2, Vals);
Chris Lattnera6245242010-04-03 02:17:50 +00001262 Vals.clear();
1263
1264 LastDL = DL;
1265 }
Chris Lattner7337ab92007-05-06 00:00:00 +00001266 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001267
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001268 // Emit names for all the instructions etc.
1269 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001270
Chris Lattnera6245242010-04-03 02:17:50 +00001271 if (NeedsMetadataAttachment)
1272 WriteMetadataAttachment(F, VE, Stream);
Chad Rosier6a0c04d2011-06-03 17:02:19 +00001273 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001274 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +00001275}
1276
1277/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1278static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1279 const ValueEnumerator &VE,
1280 BitstreamWriter &Stream) {
1281 if (TST.empty()) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001282
Chris Lattner198f34a2007-04-26 03:27:58 +00001283 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001284
Chris Lattner7a263ea2007-05-05 00:47:19 +00001285 // 7-bit fixed width VST_CODE_ENTRY strings.
1286 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1287 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1288 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1289 Log2_32_Ceil(VE.getTypes().size()+1)));
1290 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1291 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1292 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001293
Chris Lattner198f34a2007-04-26 03:27:58 +00001294 SmallVector<unsigned, 64> NameVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001295
1296 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattner198f34a2007-04-26 03:27:58 +00001297 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +00001298 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +00001299 NameVals.push_back(VE.getTypeID(TI->second));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001300
Chris Lattner198f34a2007-04-26 03:27:58 +00001301 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +00001302 bool is7Bit = true;
1303 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1304 NameVals.push_back((unsigned char)Str[i]);
1305 if (Str[i] & 128)
1306 is7Bit = false;
1307 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001308
Chris Lattner198f34a2007-04-26 03:27:58 +00001309 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +00001310 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +00001311 NameVals.clear();
1312 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001313
Chris Lattner198f34a2007-04-26 03:27:58 +00001314 Stream.ExitBlock();
1315}
1316
Chris Lattner07faafc2007-05-04 18:26:27 +00001317// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001318static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +00001319 // We only want to emit block info records for blocks that have multiple
1320 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1321 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +00001322 Stream.EnterBlockInfoBlock(2);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001323
Chris Lattnere17b6582007-05-05 00:17:00 +00001324 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1325 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1326 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1328 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1329 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001330 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnere17b6582007-05-05 00:17:00 +00001331 Abbv) != VST_ENTRY_8_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001332 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001333 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001334
Chris Lattnere17b6582007-05-05 00:17:00 +00001335 { // 7-bit fixed width VST_ENTRY strings.
1336 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1337 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1340 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1341 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1342 Abbv) != VST_ENTRY_7_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001343 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001344 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001345 { // 6-bit char6 VST_ENTRY strings.
1346 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1347 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1349 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1351 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1352 Abbv) != VST_ENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001353 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerff294a42007-05-05 01:26:50 +00001354 }
1355 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001356 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1357 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1359 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001360 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001361 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001362 Abbv) != VST_BBENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001363 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001364 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001365
1366
Daniel Dunbara279bc32009-09-20 02:20:51 +00001367
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001368 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1369 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1370 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1371 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1372 Log2_32_Ceil(VE.getTypes().size()+1)));
1373 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1374 Abbv) != CONSTANTS_SETTYPE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001375 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001376 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001377
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001378 { // INTEGER abbrev for CONSTANTS_BLOCK.
1379 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1380 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1382 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1383 Abbv) != CONSTANTS_INTEGER_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001384 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001385 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001386
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001387 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1388 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1389 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1390 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1391 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1392 Log2_32_Ceil(VE.getTypes().size()+1)));
1393 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1394
1395 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1396 Abbv) != CONSTANTS_CE_CAST_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001397 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001398 }
1399 { // NULL abbrev for CONSTANTS_BLOCK.
1400 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1401 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1402 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1403 Abbv) != CONSTANTS_NULL_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001404 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001405 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001406
Chris Lattner440168b2007-05-05 07:44:49 +00001407 // FIXME: This should only use space for first class types!
Daniel Dunbara279bc32009-09-20 02:20:51 +00001408
Chris Lattner440168b2007-05-05 07:44:49 +00001409 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1410 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1411 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1413 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1414 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1415 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1416 Abbv) != FUNCTION_INST_LOAD_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001417 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner440168b2007-05-05 07:44:49 +00001418 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001419 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1420 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1421 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1422 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1423 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1424 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1425 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1426 Abbv) != FUNCTION_INST_BINOP_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001427 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001428 }
Dan Gohman1224c382009-07-20 21:19:07 +00001429 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1430 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1431 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1434 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1435 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1436 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1437 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1438 llvm_unreachable("Unexpected abbrev ordering!");
1439 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001440 { // INST_CAST abbrev for FUNCTION_BLOCK.
1441 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1442 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1443 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1445 Log2_32_Ceil(VE.getTypes().size()+1)));
1446 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1447 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1448 Abbv) != FUNCTION_INST_CAST_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001449 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001450 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001451
Chris Lattner94687ac2007-05-06 01:28:01 +00001452 { // INST_RET abbrev for FUNCTION_BLOCK.
1453 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1454 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1455 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1456 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001457 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001458 }
1459 { // INST_RET abbrev for FUNCTION_BLOCK.
1460 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1461 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1462 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1463 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1464 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001465 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001466 }
1467 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1468 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1469 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1470 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1471 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001472 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001473 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001474
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001475 Stream.ExitBlock();
1476}
1477
1478
1479/// WriteModule - Emit the specified module to the bitstream.
1480static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1481 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001482
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001483 // Emit the version number if it is non-zero.
1484 if (CurVersion) {
1485 SmallVector<unsigned, 1> Vals;
1486 Vals.push_back(CurVersion);
1487 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1488 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001489
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001490 // Analyze the module, enumerating globals, functions, etc.
1491 ValueEnumerator VE(M);
1492
1493 // Emit blockinfo, which defines the standard abbreviations etc.
1494 WriteBlockInfo(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001495
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001496 // Emit information about parameter attributes.
Devang Patel05988662008-09-25 21:00:45 +00001497 WriteAttributeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001498
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001499 // Emit information describing all of the types in the module.
1500 WriteTypeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001501
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001502 // Emit top-level description of module, including target triple, inline asm,
1503 // descriptors for global variables, and function prototype info.
1504 WriteModuleInfo(M, VE, Stream);
Devang Patele54abc92009-07-22 17:43:22 +00001505
Devang Patel104cf9e2009-07-23 01:07:34 +00001506 // Emit constants.
1507 WriteModuleConstants(VE, Stream);
1508
Devang Patel912538b2009-08-04 05:01:35 +00001509 // Emit metadata.
Dan Gohman17aa92c2010-07-21 23:38:33 +00001510 WriteModuleMetadata(M, VE, Stream);
Devang Patel912538b2009-08-04 05:01:35 +00001511
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001512 // Emit function bodies.
Chad Rosier3d26f232011-06-03 05:09:12 +00001513 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1514 if (!F->isDeclaration())
1515 WriteFunction(*F, VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001516
1517 // Emit metadata.
Chris Lattner7d05c462009-12-28 20:10:43 +00001518 WriteModuleMetadataStore(M, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001519
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001520 // Emit the type symbol table information.
1521 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001522
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001523 // Emit names for globals/functions etc.
1524 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001525
Chris Lattner07faafc2007-05-04 18:26:27 +00001526 Stream.ExitBlock();
1527}
1528
Chris Lattner6fa6a322008-07-09 05:14:23 +00001529/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1530/// header and trailer to make it compatible with the system archiver. To do
1531/// this we emit the following header, and then emit a trailer that pads the
1532/// file out to be a multiple of 16 bytes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001533///
Chris Lattner6fa6a322008-07-09 05:14:23 +00001534/// struct bc_header {
1535/// uint32_t Magic; // 0x0B17C0DE
1536/// uint32_t Version; // Version, currently always 0.
1537/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1538/// uint32_t BitcodeSize; // Size of traditional bitcode file.
1539/// uint32_t CPUType; // CPU specifier.
1540/// ... potentially more later ...
1541/// };
1542enum {
1543 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1544 DarwinBCHeaderSize = 5*4
1545};
1546
Evan Chenge889c692011-06-14 01:51:33 +00001547static void EmitDarwinBCHeader(BitstreamWriter &Stream, const Triple &TT) {
Chris Lattner6fa6a322008-07-09 05:14:23 +00001548 unsigned CPUType = ~0U;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001549
Evan Cheng0f8868b2010-02-12 20:13:44 +00001550 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng3922a9b2010-02-12 20:39:35 +00001551 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
1552 // number from /usr/include/mach/machine.h. It is ok to reproduce the
1553 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattner6fa6a322008-07-09 05:14:23 +00001554 enum {
1555 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1556 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng0f8868b2010-02-12 20:13:44 +00001557 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattner6fa6a322008-07-09 05:14:23 +00001558 DARWIN_CPU_TYPE_POWERPC = 18
1559 };
Daniel Dunbara279bc32009-09-20 02:20:51 +00001560
Evan Chenge889c692011-06-14 01:51:33 +00001561 Triple::ArchType Arch = TT.getArch();
1562 if (Arch == Triple::x86_64)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001563 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chenge889c692011-06-14 01:51:33 +00001564 else if (Arch == Triple::x86)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001565 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chenge889c692011-06-14 01:51:33 +00001566 else if (Arch == Triple::ppc)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001567 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chenge889c692011-06-14 01:51:33 +00001568 else if (Arch == Triple::ppc64)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001569 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chenge889c692011-06-14 01:51:33 +00001570 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng0f8868b2010-02-12 20:13:44 +00001571 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001572
Chris Lattner6fa6a322008-07-09 05:14:23 +00001573 // Traditional Bitcode starts after header.
1574 unsigned BCOffset = DarwinBCHeaderSize;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001575
Chris Lattner6fa6a322008-07-09 05:14:23 +00001576 Stream.Emit(0x0B17C0DE, 32);
1577 Stream.Emit(0 , 32); // Version.
1578 Stream.Emit(BCOffset , 32);
1579 Stream.Emit(0 , 32); // Filled in later.
1580 Stream.Emit(CPUType , 32);
1581}
1582
1583/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1584/// finalize the header.
1585static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1586 // Update the size field in the header.
1587 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001588
Chris Lattner6fa6a322008-07-09 05:14:23 +00001589 // If the file is not a multiple of 16 bytes, insert dummy padding.
1590 while (BufferSize & 15) {
1591 Stream.Emit(0, 8);
1592 ++BufferSize;
1593 }
1594}
1595
Chris Lattner07faafc2007-05-04 18:26:27 +00001596
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001597/// WriteBitcodeToFile - Write the specified module to the specified output
1598/// stream.
Daniel Dunbard1ce3b42008-10-22 17:39:14 +00001599void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001600 std::vector<unsigned char> Buffer;
1601 BitstreamWriter Stream(Buffer);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001602
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001603 Buffer.reserve(256*1024);
Chris Lattner38e77212008-12-19 18:37:59 +00001604
1605 WriteBitcodeToStream( M, Stream );
Daniel Dunbara279bc32009-09-20 02:20:51 +00001606
Chris Lattner38e77212008-12-19 18:37:59 +00001607 // Write the generated bitstream to "Out".
1608 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattner38e77212008-12-19 18:37:59 +00001609}
1610
1611/// WriteBitcodeToStream - Write the specified module to the specified output
1612/// stream.
1613void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
Chris Lattner695b2812010-11-29 23:29:54 +00001614 // If this is darwin or another generic macho target, emit a file header and
1615 // trailer if needed.
Evan Chenge889c692011-06-14 01:51:33 +00001616 Triple TT(M->getTargetTriple());
1617 if (TT.isOSDarwin())
1618 EmitDarwinBCHeader(Stream, TT);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001619
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001620 // Emit the file header.
1621 Stream.Emit((unsigned)'B', 8);
1622 Stream.Emit((unsigned)'C', 8);
1623 Stream.Emit(0x0, 4);
1624 Stream.Emit(0xC, 4);
1625 Stream.Emit(0xE, 4);
1626 Stream.Emit(0xD, 4);
1627
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001628 // Emit the module.
1629 WriteModule(M, Stream);
Chris Lattner6fa6a322008-07-09 05:14:23 +00001630
Evan Chenge889c692011-06-14 01:51:33 +00001631 if (TT.isOSDarwin())
Chris Lattner38e77212008-12-19 18:37:59 +00001632 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size());
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001633}