blob: 9bda6dca3d5e9757e786c050c1477c7d6321ead7 [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000027#include "llvm/Support/MathExtras.h"
Daniel Dunbard1ce3b42008-10-22 17:39:14 +000028#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovb0a882f2008-06-06 07:24:01 +000029#include "llvm/System/Program.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000030using namespace llvm;
31
Chris Lattner59698302007-05-04 20:52:02 +000032/// These are manifest constants used by the bitcode writer. They do not need to
33/// be kept in sync with the reader, but need to be consistent within this file.
34enum {
35 CurVersion = 0,
Daniel Dunbara279bc32009-09-20 02:20:51 +000036
Chris Lattner59698302007-05-04 20:52:02 +000037 // VALUE_SYMTAB_BLOCK abbrev id's.
38 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000039 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000040 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000041 VST_BBENTRY_6_ABBREV,
Daniel Dunbara279bc32009-09-20 02:20:51 +000042
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000043 // CONSTANTS_BLOCK abbrev id's.
44 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
45 CONSTANTS_INTEGER_ABBREV,
46 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000047 CONSTANTS_NULL_Abbrev,
Daniel Dunbara279bc32009-09-20 02:20:51 +000048
Chris Lattner440168b2007-05-05 07:44:49 +000049 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000050 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000051 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman1224c382009-07-20 21:19:07 +000052 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000053 FUNCTION_INST_CAST_ABBREV,
Chris Lattner94687ac2007-05-06 01:28:01 +000054 FUNCTION_INST_RET_VOID_ABBREV,
55 FUNCTION_INST_RET_VAL_ABBREV,
56 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000057};
58
Chris Lattnerfd57cec2007-04-22 06:24:45 +000059
Chris Lattnerf581c3b2007-04-24 07:07:11 +000060static unsigned GetEncodedCastOpcode(unsigned Opcode) {
61 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000062 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattnerf581c3b2007-04-24 07:07:11 +000063 case Instruction::Trunc : return bitc::CAST_TRUNC;
64 case Instruction::ZExt : return bitc::CAST_ZEXT;
65 case Instruction::SExt : return bitc::CAST_SEXT;
66 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
67 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
68 case Instruction::UIToFP : return bitc::CAST_UITOFP;
69 case Instruction::SIToFP : return bitc::CAST_SITOFP;
70 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
71 case Instruction::FPExt : return bitc::CAST_FPEXT;
72 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
73 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
74 case Instruction::BitCast : return bitc::CAST_BITCAST;
75 }
76}
77
78static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
79 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000080 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +000081 case Instruction::Add:
82 case Instruction::FAdd: return bitc::BINOP_ADD;
83 case Instruction::Sub:
84 case Instruction::FSub: return bitc::BINOP_SUB;
85 case Instruction::Mul:
86 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattnerf581c3b2007-04-24 07:07:11 +000087 case Instruction::UDiv: return bitc::BINOP_UDIV;
88 case Instruction::FDiv:
89 case Instruction::SDiv: return bitc::BINOP_SDIV;
90 case Instruction::URem: return bitc::BINOP_UREM;
91 case Instruction::FRem:
92 case Instruction::SRem: return bitc::BINOP_SREM;
93 case Instruction::Shl: return bitc::BINOP_SHL;
94 case Instruction::LShr: return bitc::BINOP_LSHR;
95 case Instruction::AShr: return bitc::BINOP_ASHR;
96 case Instruction::And: return bitc::BINOP_AND;
97 case Instruction::Or: return bitc::BINOP_OR;
98 case Instruction::Xor: return bitc::BINOP_XOR;
99 }
100}
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 Lattnerfdfeb692010-02-12 20:49:41 +0000184 // Abbrev for TYPE_CODE_UNION.
185 Abbv = new BitCodeAbbrev();
186 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_UNION));
187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
189 Log2_32_Ceil(VE.getTypes().size()+1)));
190 unsigned UnionAbbrev = Stream.EmitAbbrev(Abbv);
191
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000192 // Abbrev for TYPE_CODE_ARRAY.
193 Abbv = new BitCodeAbbrev();
194 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
197 Log2_32_Ceil(VE.getTypes().size()+1)));
198 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000199
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000200 // Emit an entry count so the reader can reserve space.
201 TypeVals.push_back(TypeList.size());
202 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
203 TypeVals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000204
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000205 // Loop over all of the types, emitting each in turn.
206 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
207 const Type *T = TypeList[i].first;
208 int AbbrevToUse = 0;
209 unsigned Code = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000210
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000211 switch (T->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000212 default: llvm_unreachable("Unknown type!");
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000213 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
214 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
215 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000216 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
217 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
218 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000219 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
220 case Type::OpaqueTyID: Code = bitc::TYPE_CODE_OPAQUE; break;
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000221 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000222 case Type::IntegerTyID:
223 // INTEGER: [width]
224 Code = bitc::TYPE_CODE_INTEGER;
225 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
226 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000227 case Type::PointerTyID: {
Christopher Lambfe63fb92007-12-11 08:59:05 +0000228 const PointerType *PTy = cast<PointerType>(T);
Christopher Lambd49e18d2007-12-12 08:44:39 +0000229 // POINTER: [pointee type, address space]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000230 Code = bitc::TYPE_CODE_POINTER;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000231 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000232 unsigned AddressSpace = PTy->getAddressSpace();
233 TypeVals.push_back(AddressSpace);
234 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000235 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000236 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000237 case Type::FunctionTyID: {
238 const FunctionType *FT = cast<FunctionType>(T);
Chris Lattnera1afde72007-11-27 17:48:06 +0000239 // FUNCTION: [isvararg, attrid, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000240 Code = bitc::TYPE_CODE_FUNCTION;
241 TypeVals.push_back(FT->isVarArg());
Chris Lattnera1afde72007-11-27 17:48:06 +0000242 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000243 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000244 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
245 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000246 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000247 break;
248 }
249 case Type::StructTyID: {
250 const StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000251 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000252 Code = bitc::TYPE_CODE_STRUCT;
253 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000254 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000255 for (StructType::element_iterator I = ST->element_begin(),
256 E = ST->element_end(); I != E; ++I)
257 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000258 AbbrevToUse = StructAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000259 break;
260 }
Chris Lattnerfdfeb692010-02-12 20:49:41 +0000261 case Type::UnionTyID: {
262 const UnionType *UT = cast<UnionType>(T);
263 // UNION: [eltty x N]
264 Code = bitc::TYPE_CODE_UNION;
265 // Output all of the element types.
266 for (UnionType::element_iterator I = UT->element_begin(),
267 E = UT->element_end(); I != E; ++I)
268 TypeVals.push_back(VE.getTypeID(*I));
269 AbbrevToUse = UnionAbbrev;
270 break;
271 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000272 case Type::ArrayTyID: {
273 const ArrayType *AT = cast<ArrayType>(T);
274 // ARRAY: [numelts, eltty]
275 Code = bitc::TYPE_CODE_ARRAY;
276 TypeVals.push_back(AT->getNumElements());
277 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000278 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000279 break;
280 }
281 case Type::VectorTyID: {
282 const VectorType *VT = cast<VectorType>(T);
283 // VECTOR [numelts, eltty]
284 Code = bitc::TYPE_CODE_VECTOR;
285 TypeVals.push_back(VT->getNumElements());
286 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
287 break;
288 }
289 }
290
291 // Emit the finished record.
292 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
293 TypeVals.clear();
294 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000295
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000296 Stream.ExitBlock();
297}
298
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000299static unsigned getEncodedLinkage(const GlobalValue *GV) {
300 switch (GV->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000301 default: llvm_unreachable("Invalid linkage!");
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000302 case GlobalValue::ExternalLinkage: return 0;
303 case GlobalValue::WeakAnyLinkage: return 1;
304 case GlobalValue::AppendingLinkage: return 2;
305 case GlobalValue::InternalLinkage: return 3;
306 case GlobalValue::LinkOnceAnyLinkage: return 4;
307 case GlobalValue::DLLImportLinkage: return 5;
308 case GlobalValue::DLLExportLinkage: return 6;
309 case GlobalValue::ExternalWeakLinkage: return 7;
310 case GlobalValue::CommonLinkage: return 8;
311 case GlobalValue::PrivateLinkage: return 9;
312 case GlobalValue::WeakODRLinkage: return 10;
313 case GlobalValue::LinkOnceODRLinkage: return 11;
314 case GlobalValue::AvailableExternallyLinkage: return 12;
315 case GlobalValue::LinkerPrivateLinkage: return 13;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000316 }
317}
318
319static unsigned getEncodedVisibility(const GlobalValue *GV) {
320 switch (GV->getVisibility()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000321 default: llvm_unreachable("Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000322 case GlobalValue::DefaultVisibility: return 0;
323 case GlobalValue::HiddenVisibility: return 1;
324 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000325 }
326}
327
328// Emit top-level description of module, including target triple, inline asm,
329// descriptors for global variables, and function prototype info.
330static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
331 BitstreamWriter &Stream) {
332 // Emit the list of dependent libraries for the Module.
333 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
334 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
335
336 // Emit various pieces of data attached to a module.
337 if (!M->getTargetTriple().empty())
338 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
339 0/*TODO*/, Stream);
340 if (!M->getDataLayout().empty())
341 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
342 0/*TODO*/, Stream);
343 if (!M->getModuleInlineAsm().empty())
344 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
345 0/*TODO*/, Stream);
346
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000347 // Emit information about sections and GC, computing how many there are. Also
348 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000349 std::map<std::string, unsigned> SectionMap;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000350 std::map<std::string, unsigned> GCMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000351 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000352 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000353 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
354 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000355 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000356 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000357
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000358 if (!GV->hasSection()) continue;
359 // Give section names unique ID's.
360 unsigned &Entry = SectionMap[GV->getSection()];
361 if (Entry != 0) continue;
362 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
363 0/*TODO*/, Stream);
364 Entry = SectionMap.size();
365 }
366 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000367 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000368 if (F->hasSection()) {
369 // Give section names unique ID's.
370 unsigned &Entry = SectionMap[F->getSection()];
371 if (!Entry) {
372 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
373 0/*TODO*/, Stream);
374 Entry = SectionMap.size();
375 }
376 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000377 if (F->hasGC()) {
378 // Same for GC names.
379 unsigned &Entry = GCMap[F->getGC()];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000380 if (!Entry) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000381 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000382 0/*TODO*/, Stream);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000383 Entry = GCMap.size();
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000384 }
385 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000386 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000387
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000388 // Emit abbrev for globals, now that we know # sections and max alignment.
389 unsigned SimpleGVarAbbrev = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000390 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000391 // Add an abbrev for common globals with no visibility or thread localness.
392 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
393 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000394 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000395 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000396 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000397 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Dale Johannesen66676462008-05-15 20:49:28 +0000398 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000399 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000400 Abbv->Add(BitCodeAbbrevOp(0));
401 else {
402 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000403 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000404 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000405 }
406 if (SectionMap.empty()) // Section.
407 Abbv->Add(BitCodeAbbrevOp(0));
408 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000409 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000410 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000411 // Don't bother emitting vis + thread local.
412 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
413 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000414
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000415 // Emit the global variable information.
416 SmallVector<unsigned, 64> Vals;
417 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
418 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000419 unsigned AbbrevToUse = 0;
420
Daniel Dunbara279bc32009-09-20 02:20:51 +0000421 // GLOBALVAR: [type, isconst, initid,
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000422 // linkage, alignment, section, visibility, threadlocal]
423 Vals.push_back(VE.getTypeID(GV->getType()));
424 Vals.push_back(GV->isConstant());
425 Vals.push_back(GV->isDeclaration() ? 0 :
426 (VE.getValueID(GV->getInitializer()) + 1));
427 Vals.push_back(getEncodedLinkage(GV));
428 Vals.push_back(Log2_32(GV->getAlignment())+1);
429 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000430 if (GV->isThreadLocal() ||
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000431 GV->getVisibility() != GlobalValue::DefaultVisibility) {
432 Vals.push_back(getEncodedVisibility(GV));
433 Vals.push_back(GV->isThreadLocal());
434 } else {
435 AbbrevToUse = SimpleGVarAbbrev;
436 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000437
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000438 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
439 Vals.clear();
440 }
441
442 // Emit the function proto information.
443 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Duncan Sandsdc024672007-11-27 13:23:08 +0000444 // FUNCTION: [type, callingconv, isproto, paramattr,
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000445 // linkage, alignment, section, visibility, gc]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000446 Vals.push_back(VE.getTypeID(F->getType()));
447 Vals.push_back(F->getCallingConv());
448 Vals.push_back(F->isDeclaration());
449 Vals.push_back(getEncodedLinkage(F));
Devang Patel05988662008-09-25 21:00:45 +0000450 Vals.push_back(VE.getAttributeID(F->getAttributes()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000451 Vals.push_back(Log2_32(F->getAlignment())+1);
452 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
453 Vals.push_back(getEncodedVisibility(F));
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000454 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000455
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000456 unsigned AbbrevToUse = 0;
457 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
458 Vals.clear();
459 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000460
461
Chris Lattner07d98b42007-04-26 02:46:40 +0000462 // Emit the alias information.
463 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
464 AI != E; ++AI) {
465 Vals.push_back(VE.getTypeID(AI->getType()));
466 Vals.push_back(VE.getValueID(AI->getAliasee()));
467 Vals.push_back(getEncodedLinkage(AI));
Anton Korobeynikovf8342b92008-03-11 21:40:17 +0000468 Vals.push_back(getEncodedVisibility(AI));
Chris Lattner07d98b42007-04-26 02:46:40 +0000469 unsigned AbbrevToUse = 0;
470 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
471 Vals.clear();
472 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000473}
474
Dan Gohman1224c382009-07-20 21:19:07 +0000475static uint64_t GetOptimizationFlags(const Value *V) {
476 uint64_t Flags = 0;
477
478 if (const OverflowingBinaryOperator *OBO =
479 dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000480 if (OBO->hasNoSignedWrap())
481 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
482 if (OBO->hasNoUnsignedWrap())
483 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Dan Gohman1224c382009-07-20 21:19:07 +0000484 } else if (const SDivOperator *Div = dyn_cast<SDivOperator>(V)) {
485 if (Div->isExact())
486 Flags |= 1 << bitc::SDIV_EXACT;
487 }
488
489 return Flags;
490}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000491
Devang Patel912538b2009-08-04 05:01:35 +0000492static void WriteMDNode(const MDNode *N,
Devang Patel985fea22009-07-29 18:15:02 +0000493 const ValueEnumerator &VE,
Devang Patel912538b2009-08-04 05:01:35 +0000494 BitstreamWriter &Stream,
495 SmallVector<uint64_t, 64> &Record) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000496 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
497 if (N->getOperand(i)) {
498 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
499 Record.push_back(VE.getValueID(N->getOperand(i)));
Devang Patel912538b2009-08-04 05:01:35 +0000500 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +0000501 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
Devang Patel912538b2009-08-04 05:01:35 +0000502 Record.push_back(0);
503 }
504 }
Victor Hernandez24e64df2010-01-10 07:14:18 +0000505 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
506 bitc::METADATA_NODE;
507 Stream.EmitRecord(MDCode, Record, 0);
Devang Patel912538b2009-08-04 05:01:35 +0000508 Record.clear();
509}
Devang Patel104cf9e2009-07-23 01:07:34 +0000510
Devang Patel912538b2009-08-04 05:01:35 +0000511static void WriteModuleMetadata(const ValueEnumerator &VE,
512 BitstreamWriter &Stream) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000513 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
Devang Patel912538b2009-08-04 05:01:35 +0000514 bool StartedMetadataBlock = false;
515 unsigned MDSAbbrev = 0;
516 SmallVector<uint64_t, 64> Record;
517 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000518
Devang Patel912538b2009-08-04 05:01:35 +0000519 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
Victor Hernandez2c482f42010-01-29 21:19:19 +0000520 if (!N->isFunctionLocal() || !N->getFunction()) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000521 if (!StartedMetadataBlock) {
522 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
523 StartedMetadataBlock = true;
524 }
525 WriteMDNode(N, VE, Stream, Record);
Devang Patel912538b2009-08-04 05:01:35 +0000526 }
Devang Patel912538b2009-08-04 05:01:35 +0000527 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
528 if (!StartedMetadataBlock) {
529 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000530
Devang Patel70c9d172009-07-22 21:10:50 +0000531 // Abbrev for METADATA_STRING.
532 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
533 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
Devang Patele54abc92009-07-22 17:43:22 +0000534 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Devang Patel70c9d172009-07-22 21:10:50 +0000535 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
536 MDSAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel912538b2009-08-04 05:01:35 +0000537 StartedMetadataBlock = true;
Devang Patel985fea22009-07-29 18:15:02 +0000538 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000539
Devang Patel985fea22009-07-29 18:15:02 +0000540 // Code: [strchar x N]
Chris Lattner12f031d2009-10-19 05:51:03 +0000541 Record.append(MDS->begin(), MDS->end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000542
Devang Patel985fea22009-07-29 18:15:02 +0000543 // Emit the finished record.
Devang Patel104cf9e2009-07-23 01:07:34 +0000544 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
545 Record.clear();
Devang Patel912538b2009-08-04 05:01:35 +0000546 } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
547 if (!StartedMetadataBlock) {
548 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
549 StartedMetadataBlock = true;
Devang Patelaa993142009-07-29 22:34:41 +0000550 }
Devang Patel912538b2009-08-04 05:01:35 +0000551
Devang Patelaa993142009-07-29 22:34:41 +0000552 // Write name.
Devang Patel0386f012010-01-07 19:39:36 +0000553 StringRef Str = NMD->getName();
554 for (unsigned i = 0, e = Str.size(); i != e; ++i)
555 Record.push_back(Str[i]);
Devang Patelb3f7c612009-07-30 23:06:35 +0000556 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
Devang Patelaa993142009-07-29 22:34:41 +0000557 Record.clear();
Devang Patel912538b2009-08-04 05:01:35 +0000558
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000559 // Write named metadata operands.
560 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
561 if (NMD->getOperand(i))
562 Record.push_back(VE.getValueID(NMD->getOperand(i)));
Daniel Dunbara279bc32009-09-20 02:20:51 +0000563 else
Devang Patel69d02e02010-01-05 21:47:32 +0000564 Record.push_back(~0U);
Devang Patelaa993142009-07-29 22:34:41 +0000565 }
566 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
567 Record.clear();
Devang Patel912538b2009-08-04 05:01:35 +0000568 }
569 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000570
Devang Patel912538b2009-08-04 05:01:35 +0000571 if (StartedMetadataBlock)
Devang Patele8e02132009-09-18 19:26:43 +0000572 Stream.ExitBlock();
573}
574
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000575static void WriteFunctionLocalMetadata(const Function &F,
576 const ValueEnumerator &VE,
Victor Hernandez0c316412010-01-13 19:37:33 +0000577 BitstreamWriter &Stream) {
578 bool StartedMetadataBlock = false;
579 SmallVector<uint64_t, 64> Record;
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000580 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
581
582 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
583 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first))
Victor Hernandez2c482f42010-01-29 21:19:19 +0000584 if (N->isFunctionLocal() && N->getFunction() == &F) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000585 if (!StartedMetadataBlock) {
586 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
587 StartedMetadataBlock = true;
588 }
589 WriteMDNode(N, VE, Stream, Record);
Victor Hernandez0c316412010-01-13 19:37:33 +0000590 }
Victor Hernandez0c316412010-01-13 19:37:33 +0000591
592 if (StartedMetadataBlock)
593 Stream.ExitBlock();
594}
595
Devang Patele8e02132009-09-18 19:26:43 +0000596static void WriteMetadataAttachment(const Function &F,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000597 const ValueEnumerator &VE,
598 BitstreamWriter &Stream) {
Chris Lattnera6245242010-04-03 02:17:50 +0000599 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
600
Devang Patele8e02132009-09-18 19:26:43 +0000601 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000602
Devang Patele8e02132009-09-18 19:26:43 +0000603 // Write metadata attachments
604 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Chris Lattner3990b122009-12-28 23:41:32 +0000605 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
606
Devang Patele8e02132009-09-18 19:26:43 +0000607 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
608 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
609 I != E; ++I) {
Devang Patelf61b2372009-10-22 18:55:16 +0000610 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +0000611 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +0000612
613 // If no metadata, ignore instruction.
614 if (MDs.empty()) continue;
615
616 Record.push_back(VE.getInstructionID(I));
617
618 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
619 Record.push_back(MDs[i].first);
620 Record.push_back(VE.getValueID(MDs[i].second));
Devang Patele8e02132009-09-18 19:26:43 +0000621 }
Chris Lattner3990b122009-12-28 23:41:32 +0000622 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
623 Record.clear();
Devang Patele8e02132009-09-18 19:26:43 +0000624 }
625
Chris Lattnera6245242010-04-03 02:17:50 +0000626 Stream.ExitBlock();
Devang Patele8e02132009-09-18 19:26:43 +0000627}
628
Chris Lattner7d05c462009-12-28 20:10:43 +0000629static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
Devang Patele8e02132009-09-18 19:26:43 +0000630 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000631
Devang Patele8e02132009-09-18 19:26:43 +0000632 // Write metadata kinds
633 // METADATA_KIND - [n x [id, name]]
Chris Lattner7d05c462009-12-28 20:10:43 +0000634 SmallVector<StringRef, 4> Names;
Chris Lattner08113472009-12-29 09:01:33 +0000635 M->getMDKindNames(Names);
Chris Lattner7d05c462009-12-28 20:10:43 +0000636
637 assert(Names[0] == "" && "MDKind #0 is invalid");
638 if (Names.size() == 1) return;
639
640 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
641
642 for (unsigned MDKindID = 1, e = Names.size(); MDKindID != e; ++MDKindID) {
643 Record.push_back(MDKindID);
644 StringRef KName = Names[MDKindID];
645 Record.append(KName.begin(), KName.end());
646
Devang Patele8e02132009-09-18 19:26:43 +0000647 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
648 Record.clear();
649 }
650
Chris Lattner7d05c462009-12-28 20:10:43 +0000651 Stream.ExitBlock();
Devang Patel912538b2009-08-04 05:01:35 +0000652}
653
654static void WriteConstants(unsigned FirstVal, unsigned LastVal,
655 const ValueEnumerator &VE,
656 BitstreamWriter &Stream, bool isGlobal) {
657 if (FirstVal == LastVal) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000658
Devang Patel912538b2009-08-04 05:01:35 +0000659 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
660
661 unsigned AggregateAbbrev = 0;
662 unsigned String8Abbrev = 0;
663 unsigned CString7Abbrev = 0;
664 unsigned CString6Abbrev = 0;
665 // If this is a constant pool for the module, emit module-specific abbrevs.
666 if (isGlobal) {
667 // Abbrev for CST_CODE_AGGREGATE.
668 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
669 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
670 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
671 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
672 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
673
674 // Abbrev for CST_CODE_STRING.
675 Abbv = new BitCodeAbbrev();
676 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
677 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
678 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
679 String8Abbrev = Stream.EmitAbbrev(Abbv);
680 // Abbrev for CST_CODE_CSTRING.
681 Abbv = new BitCodeAbbrev();
682 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
683 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
684 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
685 CString7Abbrev = Stream.EmitAbbrev(Abbv);
686 // Abbrev for CST_CODE_CSTRING.
687 Abbv = new BitCodeAbbrev();
688 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
689 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
690 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
691 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000692 }
693
Devang Patel912538b2009-08-04 05:01:35 +0000694 SmallVector<uint64_t, 64> Record;
695
696 const ValueEnumerator::ValueList &Vals = VE.getValues();
697 const Type *LastTy = 0;
698 for (unsigned i = FirstVal; i != LastVal; ++i) {
699 const Value *V = Vals[i].first;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000700 // If we need to switch types, do so now.
701 if (V->getType() != LastTy) {
702 LastTy = V->getType();
703 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000704 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
705 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000706 Record.clear();
707 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000708
Chris Lattner2edd22b2007-04-24 00:16:04 +0000709 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesen43602982009-10-13 20:46:56 +0000710 Record.push_back(unsigned(IA->hasSideEffects()) |
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +0000711 unsigned(IA->isAlignStack()) << 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000712
Chris Lattner2bce93a2007-05-06 01:58:20 +0000713 // Add the asm string.
714 const std::string &AsmStr = IA->getAsmString();
715 Record.push_back(AsmStr.size());
716 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
717 Record.push_back(AsmStr[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000718
Chris Lattner2bce93a2007-05-06 01:58:20 +0000719 // Add the constraint string.
720 const std::string &ConstraintStr = IA->getConstraintString();
721 Record.push_back(ConstraintStr.size());
722 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
723 Record.push_back(ConstraintStr[i]);
724 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
725 Record.clear();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000726 continue;
727 }
728 const Constant *C = cast<Constant>(V);
729 unsigned Code = -1U;
730 unsigned AbbrevToUse = 0;
731 if (C->isNullValue()) {
732 Code = bitc::CST_CODE_NULL;
733 } else if (isa<UndefValue>(C)) {
734 Code = bitc::CST_CODE_UNDEF;
735 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
736 if (IV->getBitWidth() <= 64) {
737 int64_t V = IV->getSExtValue();
738 if (V >= 0)
739 Record.push_back(V << 1);
740 else
741 Record.push_back((-V << 1) | 1);
742 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000743 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000744 } else { // Wide integers, > 64 bits in size.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000745 // We have an arbitrary precision integer value to write whose
746 // bit width is > 64. However, in canonical unsigned integer
Chris Lattner2edd22b2007-04-24 00:16:04 +0000747 // format it is likely that the high bits are going to be zero.
748 // So, we only write the number of active words.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000749 unsigned NWords = IV->getValue().getActiveWords();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000750 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000751 for (unsigned i = 0; i != NWords; ++i) {
752 int64_t V = RawWords[i];
753 if (V >= 0)
754 Record.push_back(V << 1);
755 else
756 Record.push_back((-V << 1) | 1);
757 }
758 Code = bitc::CST_CODE_WIDE_INTEGER;
759 }
760 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
761 Code = bitc::CST_CODE_FLOAT;
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000762 const Type *Ty = CFP->getType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000763 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000764 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000765 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen693717f2007-09-26 23:20:33 +0000766 // api needed to prevent premature destruction
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000767 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen7111b022008-10-09 18:53:47 +0000768 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000769 const uint64_t *p = api.getRawData();
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000770 Record.push_back((p[1] << 48) | (p[0] >> 16));
771 Record.push_back(p[0] & 0xffffLL);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000772 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000773 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000774 const uint64_t *p = api.getRawData();
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000775 Record.push_back(p[0]);
776 Record.push_back(p[1]);
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000777 } else {
778 assert (0 && "Unknown FP type!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000779 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000780 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000781 const ConstantArray *CA = cast<ConstantArray>(C);
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000782 // Emit constant strings specially.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000783 unsigned NumOps = CA->getNumOperands();
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000784 // If this is a null-terminated string, use the denser CSTRING encoding.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000785 if (CA->getOperand(NumOps-1)->isNullValue()) {
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000786 Code = bitc::CST_CODE_CSTRING;
787 --NumOps; // Don't encode the null, which isn't allowed by char6.
788 } else {
789 Code = bitc::CST_CODE_STRING;
790 AbbrevToUse = String8Abbrev;
791 }
792 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
793 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
794 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000795 unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue();
Chris Lattner817f08a2007-05-06 00:42:18 +0000796 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000797 isCStr7 &= (V & 128) == 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000798 if (isCStrChar6)
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000799 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000800 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000801
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000802 if (isCStrChar6)
803 AbbrevToUse = CString6Abbrev;
804 else if (isCStr7)
805 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000806 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
Chris Lattner93b122d2010-03-16 21:25:55 +0000807 isa<ConstantVector>(V)) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000808 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000809 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
810 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000811 AbbrevToUse = AggregateAbbrev;
Chris Lattner93b122d2010-03-16 21:25:55 +0000812 } else if (isa<ConstantUnion>(C)) {
813 Code = bitc::CST_CODE_AGGREGATE;
814
815 // Unions only have one entry but we must send type along with it.
816 const Type *EntryKind = C->getOperand(0)->getType();
817
818 const UnionType *UnTy = cast<UnionType>(C->getType());
819 int UnionIndex = UnTy->getElementTypeIndex(EntryKind);
820 assert(UnionIndex != -1 && "Constant union contains invalid entry");
821
822 Record.push_back(UnionIndex);
823 Record.push_back(VE.getValueID(C->getOperand(0)));
824
825 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000826 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000827 switch (CE->getOpcode()) {
828 default:
829 if (Instruction::isCast(CE->getOpcode())) {
830 Code = bitc::CST_CODE_CE_CAST;
831 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
832 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
833 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000834 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000835 } else {
836 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
837 Code = bitc::CST_CODE_CE_BINOP;
838 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
839 Record.push_back(VE.getValueID(C->getOperand(0)));
840 Record.push_back(VE.getValueID(C->getOperand(1)));
Dan Gohman1224c382009-07-20 21:19:07 +0000841 uint64_t Flags = GetOptimizationFlags(CE);
842 if (Flags != 0)
843 Record.push_back(Flags);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000844 }
845 break;
846 case Instruction::GetElementPtr:
847 Code = bitc::CST_CODE_CE_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +0000848 if (cast<GEPOperator>(C)->isInBounds())
849 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000850 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
851 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
852 Record.push_back(VE.getValueID(C->getOperand(i)));
853 }
854 break;
855 case Instruction::Select:
856 Code = bitc::CST_CODE_CE_SELECT;
857 Record.push_back(VE.getValueID(C->getOperand(0)));
858 Record.push_back(VE.getValueID(C->getOperand(1)));
859 Record.push_back(VE.getValueID(C->getOperand(2)));
860 break;
861 case Instruction::ExtractElement:
862 Code = bitc::CST_CODE_CE_EXTRACTELT;
863 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
864 Record.push_back(VE.getValueID(C->getOperand(0)));
865 Record.push_back(VE.getValueID(C->getOperand(1)));
866 break;
867 case Instruction::InsertElement:
868 Code = bitc::CST_CODE_CE_INSERTELT;
869 Record.push_back(VE.getValueID(C->getOperand(0)));
870 Record.push_back(VE.getValueID(C->getOperand(1)));
871 Record.push_back(VE.getValueID(C->getOperand(2)));
872 break;
873 case Instruction::ShuffleVector:
Nate Begeman0f123cf2009-02-12 21:28:33 +0000874 // If the return type and argument types are the same, this is a
875 // standard shufflevector instruction. If the types are different,
876 // then the shuffle is widening or truncating the input vectors, and
877 // the argument type must also be encoded.
878 if (C->getType() == C->getOperand(0)->getType()) {
879 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
880 } else {
881 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
882 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
883 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000884 Record.push_back(VE.getValueID(C->getOperand(0)));
885 Record.push_back(VE.getValueID(C->getOperand(1)));
886 Record.push_back(VE.getValueID(C->getOperand(2)));
887 break;
888 case Instruction::ICmp:
889 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000890 Code = bitc::CST_CODE_CE_CMP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000891 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
892 Record.push_back(VE.getValueID(C->getOperand(0)));
893 Record.push_back(VE.getValueID(C->getOperand(1)));
894 Record.push_back(CE->getPredicate());
895 break;
896 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000897 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattnercdfc9402009-11-01 01:27:45 +0000898 assert(BA->getFunction() == BA->getBasicBlock()->getParent() &&
Chris Lattner837e04a2009-10-28 05:24:40 +0000899 "Malformed blockaddress");
900 Code = bitc::CST_CODE_BLOCKADDRESS;
901 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
902 Record.push_back(VE.getValueID(BA->getFunction()));
903 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner2edd22b2007-04-24 00:16:04 +0000904 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000905 llvm_unreachable("Unknown constant!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000906 }
907 Stream.EmitRecord(Code, Record, AbbrevToUse);
908 Record.clear();
909 }
910
911 Stream.ExitBlock();
912}
913
914static void WriteModuleConstants(const ValueEnumerator &VE,
915 BitstreamWriter &Stream) {
916 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000917
Chris Lattner2edd22b2007-04-24 00:16:04 +0000918 // Find the first constant to emit, which is the first non-globalvalue value.
919 // We know globalvalues have been emitted by WriteModuleInfo.
920 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
921 if (!isa<GlobalValue>(Vals[i].first)) {
Devang Patel912538b2009-08-04 05:01:35 +0000922 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000923 return;
924 }
925 }
926}
Chris Lattnerb992be12007-04-23 20:35:01 +0000927
Chris Lattner7337ab92007-05-06 00:00:00 +0000928/// PushValueAndType - The file has to encode both the value and type id for
929/// many values, because we need to know what type to create for forward
930/// references. However, most operands are not forward references, so this type
931/// field is not needed.
932///
933/// This function adds V's value ID to Vals. If the value ID is higher than the
934/// instruction ID, then it is a forward reference, and it also includes the
935/// type ID.
Gabor Greif75a46eb2009-01-16 18:40:27 +0000936static bool PushValueAndType(const Value *V, unsigned InstID,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000937 SmallVector<unsigned, 64> &Vals,
Chris Lattner7337ab92007-05-06 00:00:00 +0000938 ValueEnumerator &VE) {
939 unsigned ValID = VE.getValueID(V);
940 Vals.push_back(ValID);
941 if (ValID >= InstID) {
942 Vals.push_back(VE.getTypeID(V->getType()));
943 return true;
944 }
945 return false;
946}
947
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000948/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +0000949static void WriteInstruction(const Instruction &I, unsigned InstID,
950 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000951 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000952 unsigned Code = 0;
953 unsigned AbbrevToUse = 0;
Devang Patele8e02132009-09-18 19:26:43 +0000954 VE.setInstructionID(&I);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000955 switch (I.getOpcode()) {
956 default:
957 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +0000958 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000959 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
960 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000961 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000962 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000963 } else {
964 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +0000965 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerf9f2e832007-05-06 02:38:57 +0000966 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
967 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000968 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +0000969 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Dan Gohman1224c382009-07-20 21:19:07 +0000970 uint64_t Flags = GetOptimizationFlags(&I);
971 if (Flags != 0) {
972 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
973 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
974 Vals.push_back(Flags);
975 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000976 }
977 break;
Chris Lattnerd309c752007-05-01 02:13:26 +0000978
979 case Instruction::GetElementPtr:
980 Code = bitc::FUNC_CODE_INST_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +0000981 if (cast<GEPOperator>(&I)->isInBounds())
982 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +0000983 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
984 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +0000985 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000986 case Instruction::ExtractValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000987 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000988 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
989 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
990 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
991 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000992 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000993 }
994 case Instruction::InsertValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +0000995 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +0000996 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
997 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
998 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
999 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1000 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +00001001 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001002 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001003 case Instruction::Select:
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00001004 Code = bitc::FUNC_CODE_INST_VSELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001005 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001006 Vals.push_back(VE.getValueID(I.getOperand(2)));
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00001007 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001008 break;
1009 case Instruction::ExtractElement:
1010 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001011 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001012 Vals.push_back(VE.getValueID(I.getOperand(1)));
1013 break;
1014 case Instruction::InsertElement:
1015 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001016 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001017 Vals.push_back(VE.getValueID(I.getOperand(1)));
1018 Vals.push_back(VE.getValueID(I.getOperand(2)));
1019 break;
1020 case Instruction::ShuffleVector:
1021 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001022 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001023 Vals.push_back(VE.getValueID(I.getOperand(1)));
1024 Vals.push_back(VE.getValueID(I.getOperand(2)));
1025 break;
1026 case Instruction::ICmp:
1027 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001028 // compare returning Int1Ty or vector of Int1Ty
1029 Code = bitc::FUNC_CODE_INST_CMP2;
Chris Lattner7337ab92007-05-06 00:00:00 +00001030 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001031 Vals.push_back(VE.getValueID(I.getOperand(1)));
1032 Vals.push_back(cast<CmpInst>(I).getPredicate());
1033 break;
1034
Daniel Dunbara279bc32009-09-20 02:20:51 +00001035 case Instruction::Ret:
Devang Pateld9d99ff2008-02-26 01:29:32 +00001036 {
1037 Code = bitc::FUNC_CODE_INST_RET;
1038 unsigned NumOperands = I.getNumOperands();
Devang Pateld9d99ff2008-02-26 01:29:32 +00001039 if (NumOperands == 0)
1040 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1041 else if (NumOperands == 1) {
1042 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1043 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1044 } else {
1045 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1046 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1047 }
1048 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001049 break;
1050 case Instruction::Br:
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001051 {
1052 Code = bitc::FUNC_CODE_INST_BR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001053 BranchInst &II = cast<BranchInst>(I);
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001054 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1055 if (II.isConditional()) {
1056 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1057 Vals.push_back(VE.getValueID(II.getCondition()));
1058 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001059 }
1060 break;
1061 case Instruction::Switch:
1062 Code = bitc::FUNC_CODE_INST_SWITCH;
1063 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001064 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1065 Vals.push_back(VE.getValueID(I.getOperand(i)));
1066 break;
Chris Lattnerab21db72009-10-28 00:19:10 +00001067 case Instruction::IndirectBr:
1068 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001069 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1070 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1071 Vals.push_back(VE.getValueID(I.getOperand(i)));
1072 break;
1073
Chris Lattner60ce9b52007-05-01 07:03:37 +00001074 case Instruction::Invoke: {
Gabor Greif75a46eb2009-01-16 18:40:27 +00001075 const InvokeInst *II = cast<InvokeInst>(&I);
1076 const Value *Callee(II->getCalledValue());
1077 const PointerType *PTy = cast<PointerType>(Callee->getType());
Chris Lattnera9bb7132007-05-08 05:38:01 +00001078 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnerd309c752007-05-01 02:13:26 +00001079 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001080
Devang Patel05988662008-09-25 21:00:45 +00001081 Vals.push_back(VE.getAttributeID(II->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +00001082 Vals.push_back(II->getCallingConv());
Gabor Greif8fe53b72009-01-07 22:39:29 +00001083 Vals.push_back(VE.getValueID(II->getNormalDest()));
1084 Vals.push_back(VE.getValueID(II->getUnwindDest()));
Gabor Greif75a46eb2009-01-16 18:40:27 +00001085 PushValueAndType(Callee, InstID, Vals, VE);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001086
Chris Lattnerd309c752007-05-01 02:13:26 +00001087 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001088 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Gabor Greifc9f75002010-03-24 13:21:49 +00001089 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param.
Chris Lattnerd309c752007-05-01 02:13:26 +00001090
1091 // Emit type/value pairs for varargs params.
1092 if (FTy->isVarArg()) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001093 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattner7337ab92007-05-06 00:00:00 +00001094 i != e; ++i)
1095 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +00001096 }
1097 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001098 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001099 case Instruction::Unwind:
1100 Code = bitc::FUNC_CODE_INST_UNWIND;
1101 break;
1102 case Instruction::Unreachable:
1103 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +00001104 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001105 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001106
Chris Lattnerd309c752007-05-01 02:13:26 +00001107 case Instruction::PHI:
1108 Code = bitc::FUNC_CODE_INST_PHI;
1109 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001110 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1111 Vals.push_back(VE.getValueID(I.getOperand(i)));
1112 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001113
Chris Lattnerd309c752007-05-01 02:13:26 +00001114 case Instruction::Alloca:
1115 Code = bitc::FUNC_CODE_INST_ALLOCA;
1116 Vals.push_back(VE.getTypeID(I.getType()));
1117 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1118 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1119 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001120
Chris Lattnerd309c752007-05-01 02:13:26 +00001121 case Instruction::Load:
1122 Code = bitc::FUNC_CODE_INST_LOAD;
Chris Lattner7337ab92007-05-06 00:00:00 +00001123 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1124 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001125
Chris Lattnerd309c752007-05-01 02:13:26 +00001126 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1127 Vals.push_back(cast<LoadInst>(I).isVolatile());
1128 break;
1129 case Instruction::Store:
Christopher Lambfe63fb92007-12-11 08:59:05 +00001130 Code = bitc::FUNC_CODE_INST_STORE2;
1131 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1132 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
Chris Lattnerd309c752007-05-01 02:13:26 +00001133 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1134 Vals.push_back(cast<StoreInst>(I).isVolatile());
1135 break;
1136 case Instruction::Call: {
Chris Lattnera9bb7132007-05-08 05:38:01 +00001137 const PointerType *PTy = cast<PointerType>(I.getOperand(0)->getType());
1138 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1139
Chris Lattnerd309c752007-05-01 02:13:26 +00001140 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001141
Duncan Sandsdc024672007-11-27 13:23:08 +00001142 const CallInst *CI = cast<CallInst>(&I);
Devang Patel05988662008-09-25 21:00:45 +00001143 Vals.push_back(VE.getAttributeID(CI->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +00001144 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
1145 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee
Daniel Dunbara279bc32009-09-20 02:20:51 +00001146
Chris Lattnerd309c752007-05-01 02:13:26 +00001147 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001148 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1149 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001150
Chris Lattner60ce9b52007-05-01 07:03:37 +00001151 // Emit type/value pairs for varargs params.
1152 if (FTy->isVarArg()) {
1153 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
Chris Lattner60ce9b52007-05-01 07:03:37 +00001154 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +00001155 i != e; ++i)
1156 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +00001157 }
1158 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001159 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001160 case Instruction::VAArg:
1161 Code = bitc::FUNC_CODE_INST_VAARG;
1162 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1163 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
1164 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1165 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001166 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001167
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001168 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1169 Vals.clear();
1170}
1171
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001172// Emit names for globals/functions etc.
1173static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1174 const ValueEnumerator &VE,
1175 BitstreamWriter &Stream) {
1176 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +00001177 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +00001178
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001179 // FIXME: Set up the abbrev, we know how many values there are!
1180 // FIXME: We know if the type names can use 7-bit ascii.
1181 SmallVector<unsigned, 64> NameVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001182
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001183 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1184 SI != SE; ++SI) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001185
Chris Lattner59698302007-05-04 20:52:02 +00001186 const ValueName &Name = *SI;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001187
Chris Lattner59698302007-05-04 20:52:02 +00001188 // Figure out the encoding to use for the name.
1189 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +00001190 bool isChar6 = true;
1191 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1192 C != E; ++C) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001193 if (isChar6)
Chris Lattnerff294a42007-05-05 01:26:50 +00001194 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1195 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +00001196 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +00001197 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +00001198 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001199 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001200
Chris Lattnerfd1ae952007-05-04 21:31:13 +00001201 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001202
Chris Lattnerfd1ae952007-05-04 21:31:13 +00001203 // VST_ENTRY: [valueid, namechar x N]
1204 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +00001205 unsigned Code;
Chris Lattner4f95d2b2009-08-04 23:07:12 +00001206 if (isa<BasicBlock>(SI->getValue())) {
Chris Lattnere825ed52007-05-03 22:18:21 +00001207 Code = bitc::VST_CODE_BBENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +00001208 if (isChar6)
1209 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001210 } else {
1211 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +00001212 if (isChar6)
1213 AbbrevToUse = VST_ENTRY_6_ABBREV;
1214 else if (is7Bit)
1215 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001216 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001217
Chris Lattnere825ed52007-05-03 22:18:21 +00001218 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +00001219 for (const char *P = Name.getKeyData(),
1220 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001221 NameVals.push_back((unsigned char)*P);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001222
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001223 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +00001224 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001225 NameVals.clear();
1226 }
1227 Stream.ExitBlock();
1228}
1229
Chris Lattner8d35c792007-04-26 03:50:57 +00001230/// WriteFunction - Emit a function body to the module stream.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001231static void WriteFunction(const Function &F, ValueEnumerator &VE,
Chris Lattner198f34a2007-04-26 03:27:58 +00001232 BitstreamWriter &Stream) {
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001233 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chris Lattner8d35c792007-04-26 03:50:57 +00001234 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001235
1236 SmallVector<unsigned, 64> Vals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001237
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001238 // Emit the number of basic blocks, so the reader can create them ahead of
1239 // time.
1240 Vals.push_back(VE.getBasicBlocks().size());
1241 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1242 Vals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001243
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001244 // If there are function-local constants, emit them now.
1245 unsigned CstStart, CstEnd;
1246 VE.getFunctionConstantRange(CstStart, CstEnd);
Devang Patel912538b2009-08-04 05:01:35 +00001247 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001248
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001249 // If there is function-local metadata, emit it now.
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +00001250 WriteFunctionLocalMetadata(F, VE, Stream);
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001251
Daniel Dunbara279bc32009-09-20 02:20:51 +00001252 // Keep a running idea of what the instruction ID is.
Chris Lattner7337ab92007-05-06 00:00:00 +00001253 unsigned InstID = CstEnd;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001254
Chris Lattnera6245242010-04-03 02:17:50 +00001255 bool NeedsMetadataAttachment = false;
1256
1257 DebugLoc LastDL;
1258
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001259 // Finally, emit all the instructions, in order.
Nick Lewycky280a6e62008-04-25 16:53:59 +00001260 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +00001261 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1262 I != E; ++I) {
1263 WriteInstruction(*I, InstID, VE, Stream, Vals);
Chris Lattnera6245242010-04-03 02:17:50 +00001264
Chris Lattner3990b122009-12-28 23:41:32 +00001265 if (!I->getType()->isVoidTy())
Chris Lattner7337ab92007-05-06 00:00:00 +00001266 ++InstID;
Chris Lattnera6245242010-04-03 02:17:50 +00001267
1268 // If the instruction has metadata, write a metadata attachment later.
1269 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1270
1271 // If the instruction has a debug location, emit it.
1272 DebugLoc DL = I->getDebugLoc();
1273 if (DL.isUnknown()) {
1274 // nothing todo.
1275 } else if (DL == LastDL) {
1276 // Just repeat the same debug loc as last time.
1277 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1278 } else {
1279 MDNode *Scope, *IA;
1280 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1281
1282 Vals.push_back(DL.getLine());
1283 Vals.push_back(DL.getCol());
1284 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1285 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
1286 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
1287 Vals.clear();
1288
1289 LastDL = DL;
1290 }
Chris Lattner7337ab92007-05-06 00:00:00 +00001291 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001292
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001293 // Emit names for all the instructions etc.
1294 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001295
Chris Lattnera6245242010-04-03 02:17:50 +00001296 if (NeedsMetadataAttachment)
1297 WriteMetadataAttachment(F, VE, Stream);
Chris Lattner8d35c792007-04-26 03:50:57 +00001298 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001299 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +00001300}
1301
1302/// WriteTypeSymbolTable - Emit a block for the specified type symtab.
1303static void WriteTypeSymbolTable(const TypeSymbolTable &TST,
1304 const ValueEnumerator &VE,
1305 BitstreamWriter &Stream) {
1306 if (TST.empty()) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001307
Chris Lattner198f34a2007-04-26 03:27:58 +00001308 Stream.EnterSubblock(bitc::TYPE_SYMTAB_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001309
Chris Lattner7a263ea2007-05-05 00:47:19 +00001310 // 7-bit fixed width VST_CODE_ENTRY strings.
1311 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1312 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1313 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1314 Log2_32_Ceil(VE.getTypes().size()+1)));
1315 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1316 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1317 unsigned V7Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001318
Chris Lattner198f34a2007-04-26 03:27:58 +00001319 SmallVector<unsigned, 64> NameVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001320
1321 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattner198f34a2007-04-26 03:27:58 +00001322 TI != TE; ++TI) {
Chris Lattner7a263ea2007-05-05 00:47:19 +00001323 // TST_ENTRY: [typeid, namechar x N]
Chris Lattner198f34a2007-04-26 03:27:58 +00001324 NameVals.push_back(VE.getTypeID(TI->second));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001325
Chris Lattner198f34a2007-04-26 03:27:58 +00001326 const std::string &Str = TI->first;
Chris Lattner7a263ea2007-05-05 00:47:19 +00001327 bool is7Bit = true;
1328 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
1329 NameVals.push_back((unsigned char)Str[i]);
1330 if (Str[i] & 128)
1331 is7Bit = false;
1332 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001333
Chris Lattner198f34a2007-04-26 03:27:58 +00001334 // Emit the finished record.
Chris Lattner7a263ea2007-05-05 00:47:19 +00001335 Stream.EmitRecord(bitc::VST_CODE_ENTRY, NameVals, is7Bit ? V7Abbrev : 0);
Chris Lattner198f34a2007-04-26 03:27:58 +00001336 NameVals.clear();
1337 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001338
Chris Lattner198f34a2007-04-26 03:27:58 +00001339 Stream.ExitBlock();
1340}
1341
Chris Lattner07faafc2007-05-04 18:26:27 +00001342// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001343static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +00001344 // We only want to emit block info records for blocks that have multiple
1345 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1346 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +00001347 Stream.EnterBlockInfoBlock(2);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001348
Chris Lattnere17b6582007-05-05 00:17:00 +00001349 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1350 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1354 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001355 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnere17b6582007-05-05 00:17:00 +00001356 Abbv) != VST_ENTRY_8_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001357 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001358 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001359
Chris Lattnere17b6582007-05-05 00:17:00 +00001360 { // 7-bit fixed width VST_ENTRY strings.
1361 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1362 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1365 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1366 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1367 Abbv) != VST_ENTRY_7_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001368 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001369 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001370 { // 6-bit char6 VST_ENTRY strings.
1371 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1372 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1375 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1376 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1377 Abbv) != VST_ENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001378 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerff294a42007-05-05 01:26:50 +00001379 }
1380 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001381 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1382 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1383 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1384 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001385 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001386 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001387 Abbv) != VST_BBENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001388 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001389 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001390
1391
1392
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001393 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1394 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1395 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1396 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1397 Log2_32_Ceil(VE.getTypes().size()+1)));
1398 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1399 Abbv) != CONSTANTS_SETTYPE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001400 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001401 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001402
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001403 { // INTEGER abbrev for CONSTANTS_BLOCK.
1404 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1405 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1406 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1407 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1408 Abbv) != CONSTANTS_INTEGER_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001409 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001410 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001411
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001412 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1413 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1414 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1415 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1416 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1417 Log2_32_Ceil(VE.getTypes().size()+1)));
1418 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1419
1420 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1421 Abbv) != CONSTANTS_CE_CAST_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001422 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001423 }
1424 { // NULL abbrev for CONSTANTS_BLOCK.
1425 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1426 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1427 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1428 Abbv) != CONSTANTS_NULL_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001429 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001430 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001431
Chris Lattner440168b2007-05-05 07:44:49 +00001432 // FIXME: This should only use space for first class types!
Daniel Dunbara279bc32009-09-20 02:20:51 +00001433
Chris Lattner440168b2007-05-05 07:44:49 +00001434 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1435 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1436 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001437 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1438 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1439 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1440 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1441 Abbv) != FUNCTION_INST_LOAD_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001442 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner440168b2007-05-05 07:44:49 +00001443 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001444 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1445 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1446 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1447 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1448 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1449 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1450 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1451 Abbv) != FUNCTION_INST_BINOP_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001452 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001453 }
Dan Gohman1224c382009-07-20 21:19:07 +00001454 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1455 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1456 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1458 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1459 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1460 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1461 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1462 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1463 llvm_unreachable("Unexpected abbrev ordering!");
1464 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001465 { // INST_CAST abbrev for FUNCTION_BLOCK.
1466 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1467 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1468 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1469 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1470 Log2_32_Ceil(VE.getTypes().size()+1)));
1471 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1472 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1473 Abbv) != FUNCTION_INST_CAST_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001474 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001475 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001476
Chris Lattner94687ac2007-05-06 01:28:01 +00001477 { // INST_RET abbrev for FUNCTION_BLOCK.
1478 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1479 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1480 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1481 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001482 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001483 }
1484 { // INST_RET abbrev for FUNCTION_BLOCK.
1485 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1486 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1487 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1488 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1489 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001490 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001491 }
1492 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1493 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1494 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1495 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1496 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001497 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001498 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001499
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001500 Stream.ExitBlock();
1501}
1502
1503
1504/// WriteModule - Emit the specified module to the bitstream.
1505static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1506 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001507
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001508 // Emit the version number if it is non-zero.
1509 if (CurVersion) {
1510 SmallVector<unsigned, 1> Vals;
1511 Vals.push_back(CurVersion);
1512 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1513 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001514
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001515 // Analyze the module, enumerating globals, functions, etc.
1516 ValueEnumerator VE(M);
1517
1518 // Emit blockinfo, which defines the standard abbreviations etc.
1519 WriteBlockInfo(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001520
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001521 // Emit information about parameter attributes.
Devang Patel05988662008-09-25 21:00:45 +00001522 WriteAttributeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001523
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001524 // Emit information describing all of the types in the module.
1525 WriteTypeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001526
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001527 // Emit top-level description of module, including target triple, inline asm,
1528 // descriptors for global variables, and function prototype info.
1529 WriteModuleInfo(M, VE, Stream);
Devang Patele54abc92009-07-22 17:43:22 +00001530
Devang Patel104cf9e2009-07-23 01:07:34 +00001531 // Emit constants.
1532 WriteModuleConstants(VE, Stream);
1533
Devang Patel912538b2009-08-04 05:01:35 +00001534 // Emit metadata.
1535 WriteModuleMetadata(VE, Stream);
1536
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001537 // Emit function bodies.
1538 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
1539 if (!I->isDeclaration())
1540 WriteFunction(*I, VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001541
1542 // Emit metadata.
Chris Lattner7d05c462009-12-28 20:10:43 +00001543 WriteModuleMetadataStore(M, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001544
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001545 // Emit the type symbol table information.
1546 WriteTypeSymbolTable(M->getTypeSymbolTable(), VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001547
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001548 // Emit names for globals/functions etc.
1549 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001550
Chris Lattner07faafc2007-05-04 18:26:27 +00001551 Stream.ExitBlock();
1552}
1553
Chris Lattner6fa6a322008-07-09 05:14:23 +00001554/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1555/// header and trailer to make it compatible with the system archiver. To do
1556/// this we emit the following header, and then emit a trailer that pads the
1557/// file out to be a multiple of 16 bytes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001558///
Chris Lattner6fa6a322008-07-09 05:14:23 +00001559/// struct bc_header {
1560/// uint32_t Magic; // 0x0B17C0DE
1561/// uint32_t Version; // Version, currently always 0.
1562/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1563/// uint32_t BitcodeSize; // Size of traditional bitcode file.
1564/// uint32_t CPUType; // CPU specifier.
1565/// ... potentially more later ...
1566/// };
1567enum {
1568 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1569 DarwinBCHeaderSize = 5*4
1570};
1571
Evan Cheng0f8868b2010-02-12 20:13:44 +00001572/// isARMTriplet - Return true if the triplet looks like:
Evan Cheng3922a9b2010-02-12 20:39:35 +00001573/// arm-*, thumb-*, armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*.
Evan Cheng0f8868b2010-02-12 20:13:44 +00001574static bool isARMTriplet(const std::string &TT) {
1575 size_t Pos = 0;
1576 size_t Size = TT.size();
1577 if (Size >= 6 &&
1578 TT[0] == 't' && TT[1] == 'h' && TT[2] == 'u' &&
1579 TT[3] == 'm' && TT[4] == 'b')
1580 Pos = 5;
1581 else if (Size >= 4 && TT[0] == 'a' && TT[1] == 'r' && TT[2] == 'm')
1582 Pos = 3;
1583 else
1584 return false;
1585
1586 if (TT[Pos] == '-')
1587 return true;
Evan Cheng3922a9b2010-02-12 20:39:35 +00001588 else if (TT[Pos] == 'v') {
1589 if (Size >= Pos+4 &&
1590 TT[Pos+1] == '6' && TT[Pos+2] == 't' && TT[Pos+3] == '2')
1591 return true;
1592 else if (Size >= Pos+4 &&
1593 TT[Pos+1] == '5' && TT[Pos+2] == 't' && TT[Pos+3] == 'e')
1594 return true;
1595 } else
Evan Cheng0f8868b2010-02-12 20:13:44 +00001596 return false;
1597 while (++Pos < Size && TT[Pos] != '-') {
1598 if (!isdigit(TT[Pos]))
1599 return false;
1600 }
1601 return true;
1602}
1603
Chris Lattner6fa6a322008-07-09 05:14:23 +00001604static void EmitDarwinBCHeader(BitstreamWriter &Stream,
1605 const std::string &TT) {
1606 unsigned CPUType = ~0U;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001607
Evan Cheng0f8868b2010-02-12 20:13:44 +00001608 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng3922a9b2010-02-12 20:39:35 +00001609 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
1610 // number from /usr/include/mach/machine.h. It is ok to reproduce the
1611 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattner6fa6a322008-07-09 05:14:23 +00001612 enum {
1613 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1614 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng0f8868b2010-02-12 20:13:44 +00001615 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattner6fa6a322008-07-09 05:14:23 +00001616 DARWIN_CPU_TYPE_POWERPC = 18
1617 };
Daniel Dunbara279bc32009-09-20 02:20:51 +00001618
Chris Lattner6fa6a322008-07-09 05:14:23 +00001619 if (TT.find("x86_64-") == 0)
1620 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
1621 else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
1622 TT[4] == '-' && TT[1] - '3' < 6)
1623 CPUType = DARWIN_CPU_TYPE_X86;
1624 else if (TT.find("powerpc-") == 0)
1625 CPUType = DARWIN_CPU_TYPE_POWERPC;
1626 else if (TT.find("powerpc64-") == 0)
1627 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Cheng0f8868b2010-02-12 20:13:44 +00001628 else if (isARMTriplet(TT))
1629 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001630
Chris Lattner6fa6a322008-07-09 05:14:23 +00001631 // Traditional Bitcode starts after header.
1632 unsigned BCOffset = DarwinBCHeaderSize;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001633
Chris Lattner6fa6a322008-07-09 05:14:23 +00001634 Stream.Emit(0x0B17C0DE, 32);
1635 Stream.Emit(0 , 32); // Version.
1636 Stream.Emit(BCOffset , 32);
1637 Stream.Emit(0 , 32); // Filled in later.
1638 Stream.Emit(CPUType , 32);
1639}
1640
1641/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1642/// finalize the header.
1643static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1644 // Update the size field in the header.
1645 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001646
Chris Lattner6fa6a322008-07-09 05:14:23 +00001647 // If the file is not a multiple of 16 bytes, insert dummy padding.
1648 while (BufferSize & 15) {
1649 Stream.Emit(0, 8);
1650 ++BufferSize;
1651 }
1652}
1653
Chris Lattner07faafc2007-05-04 18:26:27 +00001654
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001655/// WriteBitcodeToFile - Write the specified module to the specified output
1656/// stream.
Daniel Dunbard1ce3b42008-10-22 17:39:14 +00001657void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001658 std::vector<unsigned char> Buffer;
1659 BitstreamWriter Stream(Buffer);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001660
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001661 Buffer.reserve(256*1024);
Chris Lattner38e77212008-12-19 18:37:59 +00001662
1663 WriteBitcodeToStream( M, Stream );
Daniel Dunbara279bc32009-09-20 02:20:51 +00001664
Chris Lattner38e77212008-12-19 18:37:59 +00001665 // If writing to stdout, set binary mode.
1666 if (&llvm::outs() == &Out)
1667 sys::Program::ChangeStdoutToBinary();
1668
1669 // Write the generated bitstream to "Out".
1670 Out.write((char*)&Buffer.front(), Buffer.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +00001671
Chris Lattner38e77212008-12-19 18:37:59 +00001672 // Make sure it hits disk now.
1673 Out.flush();
1674}
1675
1676/// WriteBitcodeToStream - Write the specified module to the specified output
1677/// stream.
1678void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
Chris Lattner6fa6a322008-07-09 05:14:23 +00001679 // If this is darwin, emit a file header and trailer if needed.
1680 bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
1681 if (isDarwin)
1682 EmitDarwinBCHeader(Stream, M->getTargetTriple());
Daniel Dunbara279bc32009-09-20 02:20:51 +00001683
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001684 // Emit the file header.
1685 Stream.Emit((unsigned)'B', 8);
1686 Stream.Emit((unsigned)'C', 8);
1687 Stream.Emit(0x0, 4);
1688 Stream.Emit(0xC, 4);
1689 Stream.Emit(0xE, 4);
1690 Stream.Emit(0xD, 4);
1691
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001692 // Emit the module.
1693 WriteModule(M, Stream);
Chris Lattner6fa6a322008-07-09 05:14:23 +00001694
1695 if (isDarwin)
Chris Lattner38e77212008-12-19 18:37:59 +00001696 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size());
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001697}