blob: 2c3604664a7a0de99c58a75ad8de3b7f6b10f07e [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 Lattnerb992be12007-04-23 20:35:01 +000024#include "llvm/ValueSymbolTable.h"
Evan Chenge889c692011-06-14 01:51:33 +000025#include "llvm/ADT/Triple.h"
Chad Rosiercbbb0962011-12-07 21:44:12 +000026#include "llvm/Support/CommandLine.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000028#include "llvm/Support/MathExtras.h"
Daniel Dunbard1ce3b42008-10-22 17:39:14 +000029#include "llvm/Support/raw_ostream.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000030#include "llvm/Support/Program.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000031#include <cctype>
Chris Lattner1afcace2011-07-09 17:41:24 +000032#include <map>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000033using namespace llvm;
34
Chad Rosiercbbb0962011-12-07 21:44:12 +000035static cl::opt<bool>
36EnablePreserveUseListOrdering("enable-bc-uselist-preserve",
37 cl::desc("Turn on experimental support for "
38 "use-list order preservation."),
39 cl::init(false), cl::Hidden);
40
Chris Lattner59698302007-05-04 20:52:02 +000041/// These are manifest constants used by the bitcode writer. They do not need to
42/// be kept in sync with the reader, but need to be consistent within this file.
43enum {
44 CurVersion = 0,
Daniel Dunbara279bc32009-09-20 02:20:51 +000045
Chris Lattner59698302007-05-04 20:52:02 +000046 // VALUE_SYMTAB_BLOCK abbrev id's.
47 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattner2453f712007-05-04 20:58:35 +000048 VST_ENTRY_7_ABBREV,
Chris Lattnerff294a42007-05-05 01:26:50 +000049 VST_ENTRY_6_ABBREV,
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000050 VST_BBENTRY_6_ABBREV,
Daniel Dunbara279bc32009-09-20 02:20:51 +000051
Chris Lattnera0f1ecc2007-05-05 07:36:14 +000052 // CONSTANTS_BLOCK abbrev id's.
53 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
54 CONSTANTS_INTEGER_ABBREV,
55 CONSTANTS_CE_CAST_Abbrev,
Chris Lattner440168b2007-05-05 07:44:49 +000056 CONSTANTS_NULL_Abbrev,
Daniel Dunbara279bc32009-09-20 02:20:51 +000057
Chris Lattner440168b2007-05-05 07:44:49 +000058 // FUNCTION_BLOCK abbrev id's.
Chris Lattner94687ac2007-05-06 01:28:01 +000059 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000060 FUNCTION_INST_BINOP_ABBREV,
Dan Gohman1224c382009-07-20 21:19:07 +000061 FUNCTION_INST_BINOP_FLAGS_ABBREV,
Chris Lattnerf9f2e832007-05-06 02:38:57 +000062 FUNCTION_INST_CAST_ABBREV,
Chris Lattner94687ac2007-05-06 01:28:01 +000063 FUNCTION_INST_RET_VOID_ABBREV,
64 FUNCTION_INST_RET_VAL_ABBREV,
65 FUNCTION_INST_UNREACHABLE_ABBREV
Chris Lattner59698302007-05-04 20:52:02 +000066};
67
Chris Lattnerf581c3b2007-04-24 07:07:11 +000068static unsigned GetEncodedCastOpcode(unsigned Opcode) {
69 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000070 default: llvm_unreachable("Unknown cast instruction!");
Chris Lattnerf581c3b2007-04-24 07:07:11 +000071 case Instruction::Trunc : return bitc::CAST_TRUNC;
72 case Instruction::ZExt : return bitc::CAST_ZEXT;
73 case Instruction::SExt : return bitc::CAST_SEXT;
74 case Instruction::FPToUI : return bitc::CAST_FPTOUI;
75 case Instruction::FPToSI : return bitc::CAST_FPTOSI;
76 case Instruction::UIToFP : return bitc::CAST_UITOFP;
77 case Instruction::SIToFP : return bitc::CAST_SITOFP;
78 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC;
79 case Instruction::FPExt : return bitc::CAST_FPEXT;
80 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT;
81 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR;
82 case Instruction::BitCast : return bitc::CAST_BITCAST;
83 }
84}
85
86static unsigned GetEncodedBinaryOpcode(unsigned Opcode) {
87 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +000088 default: llvm_unreachable("Unknown binary instruction!");
Dan Gohmanae3a0be2009-06-04 22:49:04 +000089 case Instruction::Add:
90 case Instruction::FAdd: return bitc::BINOP_ADD;
91 case Instruction::Sub:
92 case Instruction::FSub: return bitc::BINOP_SUB;
93 case Instruction::Mul:
94 case Instruction::FMul: return bitc::BINOP_MUL;
Chris Lattnerf581c3b2007-04-24 07:07:11 +000095 case Instruction::UDiv: return bitc::BINOP_UDIV;
96 case Instruction::FDiv:
97 case Instruction::SDiv: return bitc::BINOP_SDIV;
98 case Instruction::URem: return bitc::BINOP_UREM;
99 case Instruction::FRem:
100 case Instruction::SRem: return bitc::BINOP_SREM;
101 case Instruction::Shl: return bitc::BINOP_SHL;
102 case Instruction::LShr: return bitc::BINOP_LSHR;
103 case Instruction::AShr: return bitc::BINOP_ASHR;
104 case Instruction::And: return bitc::BINOP_AND;
105 case Instruction::Or: return bitc::BINOP_OR;
106 case Instruction::Xor: return bitc::BINOP_XOR;
107 }
108}
109
Eli Friedmanff030482011-07-28 21:48:00 +0000110static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) {
111 switch (Op) {
112 default: llvm_unreachable("Unknown RMW operation!");
113 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG;
114 case AtomicRMWInst::Add: return bitc::RMW_ADD;
115 case AtomicRMWInst::Sub: return bitc::RMW_SUB;
116 case AtomicRMWInst::And: return bitc::RMW_AND;
117 case AtomicRMWInst::Nand: return bitc::RMW_NAND;
118 case AtomicRMWInst::Or: return bitc::RMW_OR;
119 case AtomicRMWInst::Xor: return bitc::RMW_XOR;
120 case AtomicRMWInst::Max: return bitc::RMW_MAX;
121 case AtomicRMWInst::Min: return bitc::RMW_MIN;
122 case AtomicRMWInst::UMax: return bitc::RMW_UMAX;
123 case AtomicRMWInst::UMin: return bitc::RMW_UMIN;
124 }
125}
126
Eli Friedman47f35132011-07-25 23:16:38 +0000127static unsigned GetEncodedOrdering(AtomicOrdering Ordering) {
128 switch (Ordering) {
129 default: llvm_unreachable("Unknown atomic ordering");
130 case NotAtomic: return bitc::ORDERING_NOTATOMIC;
131 case Unordered: return bitc::ORDERING_UNORDERED;
132 case Monotonic: return bitc::ORDERING_MONOTONIC;
133 case Acquire: return bitc::ORDERING_ACQUIRE;
134 case Release: return bitc::ORDERING_RELEASE;
135 case AcquireRelease: return bitc::ORDERING_ACQREL;
136 case SequentiallyConsistent: return bitc::ORDERING_SEQCST;
137 }
138}
139
140static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) {
141 switch (SynchScope) {
142 default: llvm_unreachable("Unknown synchronization scope");
143 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD;
144 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD;
145 }
146}
147
Chris Lattner1afcace2011-07-09 17:41:24 +0000148static void WriteStringRecord(unsigned Code, StringRef Str,
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000149 unsigned AbbrevToUse, BitstreamWriter &Stream) {
150 SmallVector<unsigned, 64> Vals;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000151
Chris Lattner15e6d172007-05-04 19:11:41 +0000152 // Code: [strchar x N]
Chris Lattner1afcace2011-07-09 17:41:24 +0000153 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
154 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i]))
155 AbbrevToUse = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000156 Vals.push_back(Str[i]);
Chris Lattner1afcace2011-07-09 17:41:24 +0000157 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000158
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000159 // Emit the finished record.
160 Stream.EmitRecord(Code, Vals, AbbrevToUse);
161}
162
Chris Lattner62bbeea2007-05-04 00:44:52 +0000163// Emit information about parameter attributes.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000164static void WriteAttributeTable(const ValueEnumerator &VE,
Chris Lattner62bbeea2007-05-04 00:44:52 +0000165 BitstreamWriter &Stream) {
Devang Patel05988662008-09-25 21:00:45 +0000166 const std::vector<AttrListPtr> &Attrs = VE.getAttributes();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000167 if (Attrs.empty()) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000168
Chris Lattnerf0a65312007-05-04 02:59:04 +0000169 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3);
170
171 SmallVector<uint64_t, 64> Record;
172 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000173 const AttrListPtr &A = Attrs[i];
Chris Lattner58d74912008-03-12 17:45:29 +0000174 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000175 const AttributeWithIndex &PAWI = A.getSlot(i);
Chris Lattner58d74912008-03-12 17:45:29 +0000176 Record.push_back(PAWI.Index);
Nick Lewycky73ddd4f2008-12-19 09:38:31 +0000177
178 // FIXME: remove in LLVM 3.0
179 // Store the alignment in the bitcode as a 16-bit raw value instead of a
180 // 5-bit log2 encoded value. Shift the bits above the alignment up by
181 // 11 bits.
182 uint64_t FauxAttr = PAWI.Attrs & 0xffff;
183 if (PAWI.Attrs & Attribute::Alignment)
184 FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16);
185 FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11;
186
187 Record.push_back(FauxAttr);
Chris Lattnerf0a65312007-05-04 02:59:04 +0000188 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000189
Chris Lattnerf0a65312007-05-04 02:59:04 +0000190 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record);
191 Record.clear();
192 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000193
Chris Lattnerf0a65312007-05-04 02:59:04 +0000194 Stream.ExitBlock();
Chris Lattner62bbeea2007-05-04 00:44:52 +0000195}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000196
197/// WriteTypeTable - Write out the type table for a module.
198static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) {
199 const ValueEnumerator::TypeList &TypeList = VE.getTypes();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000200
Chris Lattner1afcace2011-07-09 17:41:24 +0000201 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000202 SmallVector<uint64_t, 64> TypeVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000203
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000204 // Abbrev for TYPE_CODE_POINTER.
205 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
206 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER));
207 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
208 Log2_32_Ceil(VE.getTypes().size()+1)));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000209 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000210 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000211
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000212 // Abbrev for TYPE_CODE_FUNCTION.
213 Abbv = new BitCodeAbbrev();
214 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION));
215 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000216 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
217 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
218 Log2_32_Ceil(VE.getTypes().size()+1)));
219 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000220
Chris Lattner1afcace2011-07-09 17:41:24 +0000221 // Abbrev for TYPE_CODE_STRUCT_ANON.
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000222 Abbv = new BitCodeAbbrev();
Chris Lattner1afcace2011-07-09 17:41:24 +0000223 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000224 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
225 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
226 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
227 Log2_32_Ceil(VE.getTypes().size()+1)));
Chris Lattner1afcace2011-07-09 17:41:24 +0000228 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000229
Chris Lattner1afcace2011-07-09 17:41:24 +0000230 // Abbrev for TYPE_CODE_STRUCT_NAME.
231 Abbv = new BitCodeAbbrev();
232 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME));
233 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
234 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
235 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv);
236
237 // Abbrev for TYPE_CODE_STRUCT_NAMED.
238 Abbv = new BitCodeAbbrev();
239 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED));
240 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked
241 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
242 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
243 Log2_32_Ceil(VE.getTypes().size()+1)));
244 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv);
Chris Lattner1afcace2011-07-09 17:41:24 +0000245
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000246 // Abbrev for TYPE_CODE_ARRAY.
247 Abbv = new BitCodeAbbrev();
248 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY));
249 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size
250 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
251 Log2_32_Ceil(VE.getTypes().size()+1)));
252 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000253
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000254 // Emit an entry count so the reader can reserve space.
255 TypeVals.push_back(TypeList.size());
256 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals);
257 TypeVals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000258
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000259 // Loop over all of the types, emitting each in turn.
260 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000261 Type *T = TypeList[i];
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000262 int AbbrevToUse = 0;
263 unsigned Code = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000264
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000265 switch (T->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000266 default: llvm_unreachable("Unknown type!");
Chris Lattner1afcace2011-07-09 17:41:24 +0000267 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break;
268 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break;
269 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break;
270 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break;
271 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break;
Dale Johannesen320fc8a2007-08-03 01:03:46 +0000272 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break;
Chris Lattner1afcace2011-07-09 17:41:24 +0000273 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break;
274 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break;
275 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000276 case Type::IntegerTyID:
277 // INTEGER: [width]
278 Code = bitc::TYPE_CODE_INTEGER;
279 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth());
280 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000281 case Type::PointerTyID: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000282 PointerType *PTy = cast<PointerType>(T);
Christopher Lambd49e18d2007-12-12 08:44:39 +0000283 // POINTER: [pointee type, address space]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000284 Code = bitc::TYPE_CODE_POINTER;
Christopher Lambfe63fb92007-12-11 08:59:05 +0000285 TypeVals.push_back(VE.getTypeID(PTy->getElementType()));
Christopher Lambd49e18d2007-12-12 08:44:39 +0000286 unsigned AddressSpace = PTy->getAddressSpace();
287 TypeVals.push_back(AddressSpace);
288 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000289 break;
Duncan Sands216b74c2007-12-11 12:20:47 +0000290 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000291 case Type::FunctionTyID: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000292 FunctionType *FT = cast<FunctionType>(T);
Chad Rosiercde54642011-11-03 00:14:01 +0000293 // FUNCTION: [isvararg, retty, paramty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000294 Code = bitc::TYPE_CODE_FUNCTION;
295 TypeVals.push_back(FT->isVarArg());
296 TypeVals.push_back(VE.getTypeID(FT->getReturnType()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000297 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i)
298 TypeVals.push_back(VE.getTypeID(FT->getParamType(i)));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000299 AbbrevToUse = FunctionAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300 break;
301 }
302 case Type::StructTyID: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000303 StructType *ST = cast<StructType>(T);
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000304 // STRUCT: [ispacked, eltty x N]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000305 TypeVals.push_back(ST->isPacked());
Chris Lattner15e6d172007-05-04 19:11:41 +0000306 // Output all of the element types.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000307 for (StructType::element_iterator I = ST->element_begin(),
308 E = ST->element_end(); I != E; ++I)
309 TypeVals.push_back(VE.getTypeID(*I));
Chris Lattner1afcace2011-07-09 17:41:24 +0000310
Chris Lattner3ebb6492011-08-12 18:06:37 +0000311 if (ST->isLiteral()) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000312 Code = bitc::TYPE_CODE_STRUCT_ANON;
313 AbbrevToUse = StructAnonAbbrev;
314 } else {
315 if (ST->isOpaque()) {
316 Code = bitc::TYPE_CODE_OPAQUE;
317 } else {
318 Code = bitc::TYPE_CODE_STRUCT_NAMED;
319 AbbrevToUse = StructNamedAbbrev;
320 }
321
322 // Emit the name if it is present.
323 if (!ST->getName().empty())
324 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(),
325 StructNameAbbrev, Stream);
326 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000327 break;
328 }
329 case Type::ArrayTyID: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000330 ArrayType *AT = cast<ArrayType>(T);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000331 // ARRAY: [numelts, eltty]
332 Code = bitc::TYPE_CODE_ARRAY;
333 TypeVals.push_back(AT->getNumElements());
334 TypeVals.push_back(VE.getTypeID(AT->getElementType()));
Chris Lattnerd092e0e2007-05-05 06:30:12 +0000335 AbbrevToUse = ArrayAbbrev;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000336 break;
337 }
338 case Type::VectorTyID: {
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000339 VectorType *VT = cast<VectorType>(T);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000340 // VECTOR [numelts, eltty]
341 Code = bitc::TYPE_CODE_VECTOR;
342 TypeVals.push_back(VT->getNumElements());
343 TypeVals.push_back(VE.getTypeID(VT->getElementType()));
344 break;
345 }
346 }
347
348 // Emit the finished record.
349 Stream.EmitRecord(Code, TypeVals, AbbrevToUse);
350 TypeVals.clear();
351 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000352
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000353 Stream.ExitBlock();
354}
355
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000356static unsigned getEncodedLinkage(const GlobalValue *GV) {
357 switch (GV->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000358 default: llvm_unreachable("Invalid linkage!");
Bill Wendling55ae5152010-08-20 22:05:50 +0000359 case GlobalValue::ExternalLinkage: return 0;
360 case GlobalValue::WeakAnyLinkage: return 1;
361 case GlobalValue::AppendingLinkage: return 2;
362 case GlobalValue::InternalLinkage: return 3;
363 case GlobalValue::LinkOnceAnyLinkage: return 4;
364 case GlobalValue::DLLImportLinkage: return 5;
365 case GlobalValue::DLLExportLinkage: return 6;
366 case GlobalValue::ExternalWeakLinkage: return 7;
367 case GlobalValue::CommonLinkage: return 8;
368 case GlobalValue::PrivateLinkage: return 9;
369 case GlobalValue::WeakODRLinkage: return 10;
370 case GlobalValue::LinkOnceODRLinkage: return 11;
371 case GlobalValue::AvailableExternallyLinkage: return 12;
372 case GlobalValue::LinkerPrivateLinkage: return 13;
373 case GlobalValue::LinkerPrivateWeakLinkage: return 14;
374 case GlobalValue::LinkerPrivateWeakDefAutoLinkage: return 15;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000375 }
376}
377
378static unsigned getEncodedVisibility(const GlobalValue *GV) {
379 switch (GV->getVisibility()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000380 default: llvm_unreachable("Invalid visibility!");
Anton Korobeynikov9cd3ccf2007-04-29 20:56:48 +0000381 case GlobalValue::DefaultVisibility: return 0;
382 case GlobalValue::HiddenVisibility: return 1;
383 case GlobalValue::ProtectedVisibility: return 2;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000384 }
385}
386
387// Emit top-level description of module, including target triple, inline asm,
388// descriptors for global variables, and function prototype info.
389static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
390 BitstreamWriter &Stream) {
391 // Emit the list of dependent libraries for the Module.
392 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I)
393 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream);
394
395 // Emit various pieces of data attached to a module.
396 if (!M->getTargetTriple().empty())
397 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(),
398 0/*TODO*/, Stream);
399 if (!M->getDataLayout().empty())
400 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(),
401 0/*TODO*/, Stream);
402 if (!M->getModuleInlineAsm().empty())
403 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(),
404 0/*TODO*/, Stream);
405
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000406 // Emit information about sections and GC, computing how many there are. Also
407 // compute the maximum alignment value.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000408 std::map<std::string, unsigned> SectionMap;
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000409 std::map<std::string, unsigned> GCMap;
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000410 unsigned MaxAlignment = 0;
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000411 unsigned MaxGlobalType = 0;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000412 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
413 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000414 MaxAlignment = std::max(MaxAlignment, GV->getAlignment());
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000415 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType()));
Chad Rosier08712212011-08-12 16:45:18 +0000416 if (GV->hasSection()) {
417 // Give section names unique ID's.
418 unsigned &Entry = SectionMap[GV->getSection()];
419 if (!Entry) {
420 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(),
421 0/*TODO*/, Stream);
422 Entry = SectionMap.size();
423 }
424 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000425 }
426 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000427 MaxAlignment = std::max(MaxAlignment, F->getAlignment());
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000428 if (F->hasSection()) {
429 // Give section names unique ID's.
430 unsigned &Entry = SectionMap[F->getSection()];
431 if (!Entry) {
432 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(),
433 0/*TODO*/, Stream);
434 Entry = SectionMap.size();
435 }
436 }
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000437 if (F->hasGC()) {
438 // Same for GC names.
439 unsigned &Entry = GCMap[F->getGC()];
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000440 if (!Entry) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000441 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(),
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000442 0/*TODO*/, Stream);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000443 Entry = GCMap.size();
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000444 }
445 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000446 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000447
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000448 // Emit abbrev for globals, now that we know # sections and max alignment.
449 unsigned SimpleGVarAbbrev = 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000450 if (!M->global_empty()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000451 // Add an abbrev for common globals with no visibility or thread localness.
452 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
453 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000454 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000455 Log2_32_Ceil(MaxGlobalType+1)));
Chris Lattner2e7899d2007-05-04 20:34:50 +0000456 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000457 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer.
Dale Johannesen66676462008-05-15 20:49:28 +0000458 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage.
Chris Lattner2e7899d2007-05-04 20:34:50 +0000459 if (MaxAlignment == 0) // Alignment.
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000460 Abbv->Add(BitCodeAbbrevOp(0));
461 else {
462 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1;
Chris Lattner2e7899d2007-05-04 20:34:50 +0000463 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattnerd127c1b2007-04-23 18:58:34 +0000464 Log2_32_Ceil(MaxEncAlignment+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000465 }
466 if (SectionMap.empty()) // Section.
467 Abbv->Add(BitCodeAbbrevOp(0));
468 else
Chris Lattner2e7899d2007-05-04 20:34:50 +0000469 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
Chris Lattner631a8ed2007-04-24 03:29:47 +0000470 Log2_32_Ceil(SectionMap.size()+1)));
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000471 // Don't bother emitting vis + thread local.
472 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv);
473 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000474
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000475 // Emit the global variable information.
476 SmallVector<unsigned, 64> Vals;
477 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end();
478 GV != E; ++GV) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000479 unsigned AbbrevToUse = 0;
480
Daniel Dunbara279bc32009-09-20 02:20:51 +0000481 // GLOBALVAR: [type, isconst, initid,
Rafael Espindolabea46262011-01-08 16:42:36 +0000482 // linkage, alignment, section, visibility, threadlocal,
483 // unnamed_addr]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000484 Vals.push_back(VE.getTypeID(GV->getType()));
485 Vals.push_back(GV->isConstant());
486 Vals.push_back(GV->isDeclaration() ? 0 :
487 (VE.getValueID(GV->getInitializer()) + 1));
488 Vals.push_back(getEncodedLinkage(GV));
489 Vals.push_back(Log2_32(GV->getAlignment())+1);
490 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000491 if (GV->isThreadLocal() ||
Rafael Espindolabea46262011-01-08 16:42:36 +0000492 GV->getVisibility() != GlobalValue::DefaultVisibility ||
493 GV->hasUnnamedAddr()) {
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000494 Vals.push_back(getEncodedVisibility(GV));
495 Vals.push_back(GV->isThreadLocal());
Rafael Espindolabea46262011-01-08 16:42:36 +0000496 Vals.push_back(GV->hasUnnamedAddr());
Chris Lattner36d5e7d2007-04-23 16:04:05 +0000497 } else {
498 AbbrevToUse = SimpleGVarAbbrev;
499 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000500
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000501 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse);
502 Vals.clear();
503 }
504
505 // Emit the function proto information.
506 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Chad Rosier0c89f7f2011-12-07 23:57:55 +0000507 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment,
508 // section, visibility, gc, unnamed_addr]
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000509 Vals.push_back(VE.getTypeID(F->getType()));
510 Vals.push_back(F->getCallingConv());
511 Vals.push_back(F->isDeclaration());
512 Vals.push_back(getEncodedLinkage(F));
Devang Patel05988662008-09-25 21:00:45 +0000513 Vals.push_back(VE.getAttributeID(F->getAttributes()));
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000514 Vals.push_back(Log2_32(F->getAlignment())+1);
515 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0);
516 Vals.push_back(getEncodedVisibility(F));
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000517 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0);
Rafael Espindolabea46262011-01-08 16:42:36 +0000518 Vals.push_back(F->hasUnnamedAddr());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000519
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000520 unsigned AbbrevToUse = 0;
521 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
522 Vals.clear();
523 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000524
Chris Lattner07d98b42007-04-26 02:46:40 +0000525 // Emit the alias information.
526 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end();
527 AI != E; ++AI) {
Chad Rosier14d622d2011-12-08 00:11:31 +0000528 // ALIAS: [alias type, aliasee val#, linkage, visibility]
Chris Lattner07d98b42007-04-26 02:46:40 +0000529 Vals.push_back(VE.getTypeID(AI->getType()));
530 Vals.push_back(VE.getValueID(AI->getAliasee()));
531 Vals.push_back(getEncodedLinkage(AI));
Anton Korobeynikovf8342b92008-03-11 21:40:17 +0000532 Vals.push_back(getEncodedVisibility(AI));
Chris Lattner07d98b42007-04-26 02:46:40 +0000533 unsigned AbbrevToUse = 0;
534 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
535 Vals.clear();
536 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000537}
538
Dan Gohman1224c382009-07-20 21:19:07 +0000539static uint64_t GetOptimizationFlags(const Value *V) {
540 uint64_t Flags = 0;
541
542 if (const OverflowingBinaryOperator *OBO =
543 dyn_cast<OverflowingBinaryOperator>(V)) {
Dan Gohman5078f842009-08-20 17:11:38 +0000544 if (OBO->hasNoSignedWrap())
545 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
546 if (OBO->hasNoUnsignedWrap())
547 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
Chris Lattner35bda892011-02-06 21:44:57 +0000548 } else if (const PossiblyExactOperator *PEO =
549 dyn_cast<PossiblyExactOperator>(V)) {
550 if (PEO->isExact())
551 Flags |= 1 << bitc::PEO_EXACT;
Dan Gohman1224c382009-07-20 21:19:07 +0000552 }
553
554 return Flags;
555}
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000556
Devang Patel912538b2009-08-04 05:01:35 +0000557static void WriteMDNode(const MDNode *N,
Devang Patel985fea22009-07-29 18:15:02 +0000558 const ValueEnumerator &VE,
Devang Patel912538b2009-08-04 05:01:35 +0000559 BitstreamWriter &Stream,
560 SmallVector<uint64_t, 64> &Record) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000561 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
562 if (N->getOperand(i)) {
563 Record.push_back(VE.getTypeID(N->getOperand(i)->getType()));
564 Record.push_back(VE.getValueID(N->getOperand(i)));
Devang Patel912538b2009-08-04 05:01:35 +0000565 } else {
Owen Anderson1d0be152009-08-13 21:58:54 +0000566 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext())));
Devang Patel912538b2009-08-04 05:01:35 +0000567 Record.push_back(0);
568 }
569 }
Chris Lattner799a58a552011-06-17 17:56:00 +0000570 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE :
571 bitc::METADATA_NODE;
Victor Hernandez24e64df2010-01-10 07:14:18 +0000572 Stream.EmitRecord(MDCode, Record, 0);
Devang Patel912538b2009-08-04 05:01:35 +0000573 Record.clear();
574}
Devang Patel104cf9e2009-07-23 01:07:34 +0000575
Dan Gohman17aa92c2010-07-21 23:38:33 +0000576static void WriteModuleMetadata(const Module *M,
577 const ValueEnumerator &VE,
Devang Patel912538b2009-08-04 05:01:35 +0000578 BitstreamWriter &Stream) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000579 const ValueEnumerator::ValueList &Vals = VE.getMDValues();
Devang Patel912538b2009-08-04 05:01:35 +0000580 bool StartedMetadataBlock = false;
581 unsigned MDSAbbrev = 0;
582 SmallVector<uint64_t, 64> Record;
583 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000584
Devang Patel912538b2009-08-04 05:01:35 +0000585 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) {
Victor Hernandez2c482f42010-01-29 21:19:19 +0000586 if (!N->isFunctionLocal() || !N->getFunction()) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000587 if (!StartedMetadataBlock) {
588 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
589 StartedMetadataBlock = true;
590 }
591 WriteMDNode(N, VE, Stream, Record);
Devang Patel912538b2009-08-04 05:01:35 +0000592 }
Devang Patel912538b2009-08-04 05:01:35 +0000593 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) {
594 if (!StartedMetadataBlock) {
595 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000596
Devang Patel70c9d172009-07-22 21:10:50 +0000597 // Abbrev for METADATA_STRING.
598 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
599 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
Devang Patele54abc92009-07-22 17:43:22 +0000600 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Devang Patel70c9d172009-07-22 21:10:50 +0000601 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
602 MDSAbbrev = Stream.EmitAbbrev(Abbv);
Devang Patel912538b2009-08-04 05:01:35 +0000603 StartedMetadataBlock = true;
Devang Patel985fea22009-07-29 18:15:02 +0000604 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000605
Devang Patel985fea22009-07-29 18:15:02 +0000606 // Code: [strchar x N]
Chris Lattner12f031d2009-10-19 05:51:03 +0000607 Record.append(MDS->begin(), MDS->end());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000608
Devang Patel985fea22009-07-29 18:15:02 +0000609 // Emit the finished record.
Devang Patel104cf9e2009-07-23 01:07:34 +0000610 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
611 Record.clear();
Devang Patel912538b2009-08-04 05:01:35 +0000612 }
613 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000614
Dan Gohman17aa92c2010-07-21 23:38:33 +0000615 // Write named metadata.
616 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
617 E = M->named_metadata_end(); I != E; ++I) {
618 const NamedMDNode *NMD = I;
619 if (!StartedMetadataBlock) {
620 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
621 StartedMetadataBlock = true;
622 }
623
624 // Write name.
625 StringRef Str = NMD->getName();
626 for (unsigned i = 0, e = Str.size(); i != e; ++i)
627 Record.push_back(Str[i]);
628 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
629 Record.clear();
630
631 // Write named metadata operands.
632 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
633 Record.push_back(VE.getValueID(NMD->getOperand(i)));
Chris Lattner799a58a552011-06-17 17:56:00 +0000634 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Dan Gohman17aa92c2010-07-21 23:38:33 +0000635 Record.clear();
636 }
637
Devang Patel912538b2009-08-04 05:01:35 +0000638 if (StartedMetadataBlock)
Devang Patele8e02132009-09-18 19:26:43 +0000639 Stream.ExitBlock();
640}
641
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000642static void WriteFunctionLocalMetadata(const Function &F,
643 const ValueEnumerator &VE,
Victor Hernandez0c316412010-01-13 19:37:33 +0000644 BitstreamWriter &Stream) {
645 bool StartedMetadataBlock = false;
646 SmallVector<uint64_t, 64> Record;
Devang Patel62098692010-06-02 23:05:04 +0000647 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues();
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +0000648 for (unsigned i = 0, e = Vals.size(); i != e; ++i)
Devang Patel62098692010-06-02 23:05:04 +0000649 if (const MDNode *N = Vals[i])
Victor Hernandez2c482f42010-01-29 21:19:19 +0000650 if (N->isFunctionLocal() && N->getFunction() == &F) {
Victor Hernandez0c316412010-01-13 19:37:33 +0000651 if (!StartedMetadataBlock) {
652 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
653 StartedMetadataBlock = true;
654 }
655 WriteMDNode(N, VE, Stream, Record);
Victor Hernandez0c316412010-01-13 19:37:33 +0000656 }
Devang Patel62098692010-06-02 23:05:04 +0000657
Victor Hernandez0c316412010-01-13 19:37:33 +0000658 if (StartedMetadataBlock)
659 Stream.ExitBlock();
660}
661
Devang Patele8e02132009-09-18 19:26:43 +0000662static void WriteMetadataAttachment(const Function &F,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000663 const ValueEnumerator &VE,
664 BitstreamWriter &Stream) {
Chris Lattnera6245242010-04-03 02:17:50 +0000665 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
666
Devang Patele8e02132009-09-18 19:26:43 +0000667 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000668
Devang Patele8e02132009-09-18 19:26:43 +0000669 // Write metadata attachments
Chris Lattner799a58a552011-06-17 17:56:00 +0000670 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
Chris Lattner3990b122009-12-28 23:41:32 +0000671 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
672
Devang Patele8e02132009-09-18 19:26:43 +0000673 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
674 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
675 I != E; ++I) {
Devang Patelf61b2372009-10-22 18:55:16 +0000676 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +0000677 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +0000678
679 // If no metadata, ignore instruction.
680 if (MDs.empty()) continue;
681
682 Record.push_back(VE.getInstructionID(I));
683
684 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
685 Record.push_back(MDs[i].first);
686 Record.push_back(VE.getValueID(MDs[i].second));
Devang Patele8e02132009-09-18 19:26:43 +0000687 }
Chris Lattner799a58a552011-06-17 17:56:00 +0000688 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
Chris Lattner3990b122009-12-28 23:41:32 +0000689 Record.clear();
Devang Patele8e02132009-09-18 19:26:43 +0000690 }
691
Chris Lattnera6245242010-04-03 02:17:50 +0000692 Stream.ExitBlock();
Devang Patele8e02132009-09-18 19:26:43 +0000693}
694
Chris Lattner7d05c462009-12-28 20:10:43 +0000695static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) {
Devang Patele8e02132009-09-18 19:26:43 +0000696 SmallVector<uint64_t, 64> Record;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000697
Devang Patele8e02132009-09-18 19:26:43 +0000698 // Write metadata kinds
699 // METADATA_KIND - [n x [id, name]]
Chris Lattner7d05c462009-12-28 20:10:43 +0000700 SmallVector<StringRef, 4> Names;
Chris Lattner08113472009-12-29 09:01:33 +0000701 M->getMDKindNames(Names);
Chris Lattner7d05c462009-12-28 20:10:43 +0000702
Dan Gohman19538d12010-07-20 21:42:28 +0000703 if (Names.empty()) return;
Chris Lattner7d05c462009-12-28 20:10:43 +0000704
705 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
706
Dan Gohman19538d12010-07-20 21:42:28 +0000707 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) {
Chris Lattner7d05c462009-12-28 20:10:43 +0000708 Record.push_back(MDKindID);
709 StringRef KName = Names[MDKindID];
710 Record.append(KName.begin(), KName.end());
711
Devang Patele8e02132009-09-18 19:26:43 +0000712 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0);
713 Record.clear();
714 }
715
Chris Lattner7d05c462009-12-28 20:10:43 +0000716 Stream.ExitBlock();
Devang Patel912538b2009-08-04 05:01:35 +0000717}
718
719static void WriteConstants(unsigned FirstVal, unsigned LastVal,
720 const ValueEnumerator &VE,
721 BitstreamWriter &Stream, bool isGlobal) {
722 if (FirstVal == LastVal) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000723
Devang Patel912538b2009-08-04 05:01:35 +0000724 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
725
726 unsigned AggregateAbbrev = 0;
727 unsigned String8Abbrev = 0;
728 unsigned CString7Abbrev = 0;
729 unsigned CString6Abbrev = 0;
730 // If this is a constant pool for the module, emit module-specific abbrevs.
731 if (isGlobal) {
732 // Abbrev for CST_CODE_AGGREGATE.
733 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
734 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
735 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
736 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
737 AggregateAbbrev = Stream.EmitAbbrev(Abbv);
738
739 // Abbrev for CST_CODE_STRING.
740 Abbv = new BitCodeAbbrev();
741 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
742 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
744 String8Abbrev = Stream.EmitAbbrev(Abbv);
745 // Abbrev for CST_CODE_CSTRING.
746 Abbv = new BitCodeAbbrev();
747 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
748 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
749 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
750 CString7Abbrev = Stream.EmitAbbrev(Abbv);
751 // Abbrev for CST_CODE_CSTRING.
752 Abbv = new BitCodeAbbrev();
753 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
756 CString6Abbrev = Stream.EmitAbbrev(Abbv);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000757 }
758
Devang Patel912538b2009-08-04 05:01:35 +0000759 SmallVector<uint64_t, 64> Record;
760
761 const ValueEnumerator::ValueList &Vals = VE.getValues();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000762 Type *LastTy = 0;
Devang Patel912538b2009-08-04 05:01:35 +0000763 for (unsigned i = FirstVal; i != LastVal; ++i) {
764 const Value *V = Vals[i].first;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000765 // If we need to switch types, do so now.
766 if (V->getType() != LastTy) {
767 LastTy = V->getType();
768 Record.push_back(VE.getTypeID(LastTy));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000769 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record,
770 CONSTANTS_SETTYPE_ABBREV);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000771 Record.clear();
772 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000773
Chris Lattner2edd22b2007-04-24 00:16:04 +0000774 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Dale Johannesen43602982009-10-13 20:46:56 +0000775 Record.push_back(unsigned(IA->hasSideEffects()) |
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +0000776 unsigned(IA->isAlignStack()) << 1);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000777
Chris Lattner2bce93a2007-05-06 01:58:20 +0000778 // Add the asm string.
779 const std::string &AsmStr = IA->getAsmString();
780 Record.push_back(AsmStr.size());
781 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i)
782 Record.push_back(AsmStr[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000783
Chris Lattner2bce93a2007-05-06 01:58:20 +0000784 // Add the constraint string.
785 const std::string &ConstraintStr = IA->getConstraintString();
786 Record.push_back(ConstraintStr.size());
787 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i)
788 Record.push_back(ConstraintStr[i]);
789 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record);
790 Record.clear();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000791 continue;
792 }
793 const Constant *C = cast<Constant>(V);
794 unsigned Code = -1U;
795 unsigned AbbrevToUse = 0;
796 if (C->isNullValue()) {
797 Code = bitc::CST_CODE_NULL;
798 } else if (isa<UndefValue>(C)) {
799 Code = bitc::CST_CODE_UNDEF;
800 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) {
801 if (IV->getBitWidth() <= 64) {
Chris Lattner2359e512010-08-03 16:57:03 +0000802 uint64_t V = IV->getSExtValue();
803 if ((int64_t)V >= 0)
Chris Lattner2edd22b2007-04-24 00:16:04 +0000804 Record.push_back(V << 1);
805 else
806 Record.push_back((-V << 1) | 1);
807 Code = bitc::CST_CODE_INTEGER;
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000808 AbbrevToUse = CONSTANTS_INTEGER_ABBREV;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000809 } else { // Wide integers, > 64 bits in size.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000810 // We have an arbitrary precision integer value to write whose
811 // bit width is > 64. However, in canonical unsigned integer
Chris Lattner2edd22b2007-04-24 00:16:04 +0000812 // format it is likely that the high bits are going to be zero.
813 // So, we only write the number of active words.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000814 unsigned NWords = IV->getValue().getActiveWords();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000815 const uint64_t *RawWords = IV->getValue().getRawData();
Chris Lattner2edd22b2007-04-24 00:16:04 +0000816 for (unsigned i = 0; i != NWords; ++i) {
817 int64_t V = RawWords[i];
818 if (V >= 0)
819 Record.push_back(V << 1);
820 else
821 Record.push_back((-V << 1) | 1);
822 }
823 Code = bitc::CST_CODE_WIDE_INTEGER;
824 }
825 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
826 Code = bitc::CST_CODE_FLOAT;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000827 Type *Ty = CFP->getType();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000828 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000829 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000830 } else if (Ty->isX86_FP80Ty()) {
Dale Johannesen693717f2007-09-26 23:20:33 +0000831 // api needed to prevent premature destruction
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000832 // bits are not in the same order as a normal i80 APInt, compensate.
Dale Johannesen7111b022008-10-09 18:53:47 +0000833 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000834 const uint64_t *p = api.getRawData();
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000835 Record.push_back((p[1] << 48) | (p[0] >> 16));
836 Record.push_back(p[0] & 0xffffLL);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000837 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) {
Dale Johannesen7111b022008-10-09 18:53:47 +0000838 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesen693717f2007-09-26 23:20:33 +0000839 const uint64_t *p = api.getRawData();
Dale Johannesen3f6eb742007-09-11 18:32:33 +0000840 Record.push_back(p[0]);
841 Record.push_back(p[1]);
Dale Johannesenebbc95d2007-08-09 22:51:36 +0000842 } else {
843 assert (0 && "Unknown FP type!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000844 }
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000845 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000846 const ConstantArray *CA = cast<ConstantArray>(C);
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000847 // Emit constant strings specially.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000848 unsigned NumOps = CA->getNumOperands();
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000849 // If this is a null-terminated string, use the denser CSTRING encoding.
Chris Lattner0eeb9132009-10-28 05:14:34 +0000850 if (CA->getOperand(NumOps-1)->isNullValue()) {
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000851 Code = bitc::CST_CODE_CSTRING;
852 --NumOps; // Don't encode the null, which isn't allowed by char6.
853 } else {
854 Code = bitc::CST_CODE_STRING;
855 AbbrevToUse = String8Abbrev;
856 }
857 bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
858 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
859 for (unsigned i = 0; i != NumOps; ++i) {
Chris Lattner0eeb9132009-10-28 05:14:34 +0000860 unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue();
Chris Lattner817f08a2007-05-06 00:42:18 +0000861 Record.push_back(V);
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000862 isCStr7 &= (V & 128) == 0;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000863 if (isCStrChar6)
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000864 isCStrChar6 = BitCodeAbbrevOp::isChar6(V);
Chris Lattner817f08a2007-05-06 00:42:18 +0000865 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000866
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000867 if (isCStrChar6)
868 AbbrevToUse = CString6Abbrev;
869 else if (isCStr7)
870 AbbrevToUse = CString7Abbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000871 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) ||
Chris Lattner93b122d2010-03-16 21:25:55 +0000872 isa<ConstantVector>(V)) {
Chris Lattner2edd22b2007-04-24 00:16:04 +0000873 Code = bitc::CST_CODE_AGGREGATE;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000874 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
875 Record.push_back(VE.getValueID(C->getOperand(i)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000876 AbbrevToUse = AggregateAbbrev;
Chris Lattner2edd22b2007-04-24 00:16:04 +0000877 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000878 switch (CE->getOpcode()) {
879 default:
880 if (Instruction::isCast(CE->getOpcode())) {
881 Code = bitc::CST_CODE_CE_CAST;
882 Record.push_back(GetEncodedCastOpcode(CE->getOpcode()));
883 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
884 Record.push_back(VE.getValueID(C->getOperand(0)));
Chris Lattnera0f1ecc2007-05-05 07:36:14 +0000885 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000886 } else {
887 assert(CE->getNumOperands() == 2 && "Unknown constant expr!");
888 Code = bitc::CST_CODE_CE_BINOP;
889 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode()));
890 Record.push_back(VE.getValueID(C->getOperand(0)));
891 Record.push_back(VE.getValueID(C->getOperand(1)));
Dan Gohman1224c382009-07-20 21:19:07 +0000892 uint64_t Flags = GetOptimizationFlags(CE);
893 if (Flags != 0)
894 Record.push_back(Flags);
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000895 }
896 break;
897 case Instruction::GetElementPtr:
898 Code = bitc::CST_CODE_CE_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +0000899 if (cast<GEPOperator>(C)->isInBounds())
900 Code = bitc::CST_CODE_CE_INBOUNDS_GEP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000901 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) {
902 Record.push_back(VE.getTypeID(C->getOperand(i)->getType()));
903 Record.push_back(VE.getValueID(C->getOperand(i)));
904 }
905 break;
906 case Instruction::Select:
907 Code = bitc::CST_CODE_CE_SELECT;
908 Record.push_back(VE.getValueID(C->getOperand(0)));
909 Record.push_back(VE.getValueID(C->getOperand(1)));
910 Record.push_back(VE.getValueID(C->getOperand(2)));
911 break;
912 case Instruction::ExtractElement:
913 Code = bitc::CST_CODE_CE_EXTRACTELT;
914 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
915 Record.push_back(VE.getValueID(C->getOperand(0)));
916 Record.push_back(VE.getValueID(C->getOperand(1)));
917 break;
918 case Instruction::InsertElement:
919 Code = bitc::CST_CODE_CE_INSERTELT;
920 Record.push_back(VE.getValueID(C->getOperand(0)));
921 Record.push_back(VE.getValueID(C->getOperand(1)));
922 Record.push_back(VE.getValueID(C->getOperand(2)));
923 break;
924 case Instruction::ShuffleVector:
Nate Begeman0f123cf2009-02-12 21:28:33 +0000925 // If the return type and argument types are the same, this is a
926 // standard shufflevector instruction. If the types are different,
927 // then the shuffle is widening or truncating the input vectors, and
928 // the argument type must also be encoded.
929 if (C->getType() == C->getOperand(0)->getType()) {
930 Code = bitc::CST_CODE_CE_SHUFFLEVEC;
931 } else {
932 Code = bitc::CST_CODE_CE_SHUFVEC_EX;
933 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
934 }
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000935 Record.push_back(VE.getValueID(C->getOperand(0)));
936 Record.push_back(VE.getValueID(C->getOperand(1)));
937 Record.push_back(VE.getValueID(C->getOperand(2)));
938 break;
939 case Instruction::ICmp:
940 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +0000941 Code = bitc::CST_CODE_CE_CMP;
Chris Lattnerf581c3b2007-04-24 07:07:11 +0000942 Record.push_back(VE.getTypeID(C->getOperand(0)->getType()));
943 Record.push_back(VE.getValueID(C->getOperand(0)));
944 Record.push_back(VE.getValueID(C->getOperand(1)));
945 Record.push_back(CE->getPredicate());
946 break;
947 }
Chris Lattner837e04a2009-10-28 05:24:40 +0000948 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
Chris Lattner837e04a2009-10-28 05:24:40 +0000949 Code = bitc::CST_CODE_BLOCKADDRESS;
950 Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
951 Record.push_back(VE.getValueID(BA->getFunction()));
952 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock()));
Chris Lattner2edd22b2007-04-24 00:16:04 +0000953 } else {
Dan Gohman29d27162010-07-21 21:18:37 +0000954#ifndef NDEBUG
955 C->dump();
956#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000957 llvm_unreachable("Unknown constant!");
Chris Lattner2edd22b2007-04-24 00:16:04 +0000958 }
959 Stream.EmitRecord(Code, Record, AbbrevToUse);
960 Record.clear();
961 }
962
963 Stream.ExitBlock();
964}
965
966static void WriteModuleConstants(const ValueEnumerator &VE,
967 BitstreamWriter &Stream) {
968 const ValueEnumerator::ValueList &Vals = VE.getValues();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000969
Chris Lattner2edd22b2007-04-24 00:16:04 +0000970 // Find the first constant to emit, which is the first non-globalvalue value.
971 // We know globalvalues have been emitted by WriteModuleInfo.
972 for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
973 if (!isa<GlobalValue>(Vals[i].first)) {
Devang Patel912538b2009-08-04 05:01:35 +0000974 WriteConstants(i, Vals.size(), VE, Stream, true);
Chris Lattner2edd22b2007-04-24 00:16:04 +0000975 return;
976 }
977 }
978}
Chris Lattnerb992be12007-04-23 20:35:01 +0000979
Chris Lattner7337ab92007-05-06 00:00:00 +0000980/// PushValueAndType - The file has to encode both the value and type id for
981/// many values, because we need to know what type to create for forward
982/// references. However, most operands are not forward references, so this type
983/// field is not needed.
984///
985/// This function adds V's value ID to Vals. If the value ID is higher than the
986/// instruction ID, then it is a forward reference, and it also includes the
987/// type ID.
Gabor Greif75a46eb2009-01-16 18:40:27 +0000988static bool PushValueAndType(const Value *V, unsigned InstID,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000989 SmallVector<unsigned, 64> &Vals,
Chris Lattner7337ab92007-05-06 00:00:00 +0000990 ValueEnumerator &VE) {
991 unsigned ValID = VE.getValueID(V);
992 Vals.push_back(ValID);
993 if (ValID >= InstID) {
994 Vals.push_back(VE.getTypeID(V->getType()));
995 return true;
996 }
997 return false;
998}
999
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001000/// WriteInstruction - Emit an instruction to the specified stream.
Chris Lattner7337ab92007-05-06 00:00:00 +00001001static void WriteInstruction(const Instruction &I, unsigned InstID,
1002 ValueEnumerator &VE, BitstreamWriter &Stream,
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001003 SmallVector<unsigned, 64> &Vals) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001004 unsigned Code = 0;
1005 unsigned AbbrevToUse = 0;
Devang Patele8e02132009-09-18 19:26:43 +00001006 VE.setInstructionID(&I);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001007 switch (I.getOpcode()) {
1008 default:
1009 if (Instruction::isCast(I.getOpcode())) {
Chris Lattnerd309c752007-05-01 02:13:26 +00001010 Code = bitc::FUNC_CODE_INST_CAST;
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001011 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1012 AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001013 Vals.push_back(VE.getTypeID(I.getType()));
Chris Lattnerabfbf852007-05-06 00:21:25 +00001014 Vals.push_back(GetEncodedCastOpcode(I.getOpcode()));
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001015 } else {
1016 assert(isa<BinaryOperator>(I) && "Unknown instruction!");
Chris Lattnerf6398752007-05-02 04:26:36 +00001017 Code = bitc::FUNC_CODE_INST_BINOP;
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001018 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1019 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001020 Vals.push_back(VE.getValueID(I.getOperand(1)));
Chris Lattnerabfbf852007-05-06 00:21:25 +00001021 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode()));
Dan Gohman1224c382009-07-20 21:19:07 +00001022 uint64_t Flags = GetOptimizationFlags(&I);
1023 if (Flags != 0) {
1024 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV)
1025 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV;
1026 Vals.push_back(Flags);
1027 }
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001028 }
1029 break;
Chris Lattnerd309c752007-05-01 02:13:26 +00001030
1031 case Instruction::GetElementPtr:
1032 Code = bitc::FUNC_CODE_INST_GEP;
Dan Gohmandd8004d2009-07-27 21:53:46 +00001033 if (cast<GEPOperator>(&I)->isInBounds())
1034 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP;
Chris Lattner7337ab92007-05-06 00:00:00 +00001035 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1036 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001037 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001038 case Instruction::ExtractValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +00001039 Code = bitc::FUNC_CODE_INST_EXTRACTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001040 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1041 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
1042 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
1043 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +00001044 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001045 }
1046 case Instruction::InsertValue: {
Dan Gohmane4977cf2008-05-23 01:55:30 +00001047 Code = bitc::FUNC_CODE_INST_INSERTVAL;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001048 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1049 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
1050 const InsertValueInst *IVI = cast<InsertValueInst>(&I);
1051 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
1052 Vals.push_back(*i);
Dan Gohmane4977cf2008-05-23 01:55:30 +00001053 break;
Dan Gohman0aab28b2008-05-31 19:11:15 +00001054 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001055 case Instruction::Select:
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00001056 Code = bitc::FUNC_CODE_INST_VSELECT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001057 PushValueAndType(I.getOperand(1), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001058 Vals.push_back(VE.getValueID(I.getOperand(2)));
Dan Gohmanfb2bbbe2008-09-16 01:01:33 +00001059 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001060 break;
1061 case Instruction::ExtractElement:
1062 Code = bitc::FUNC_CODE_INST_EXTRACTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001063 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001064 Vals.push_back(VE.getValueID(I.getOperand(1)));
1065 break;
1066 case Instruction::InsertElement:
1067 Code = bitc::FUNC_CODE_INST_INSERTELT;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001068 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001069 Vals.push_back(VE.getValueID(I.getOperand(1)));
1070 Vals.push_back(VE.getValueID(I.getOperand(2)));
1071 break;
1072 case Instruction::ShuffleVector:
1073 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC;
Chris Lattnerabfbf852007-05-06 00:21:25 +00001074 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001075 Vals.push_back(VE.getValueID(I.getOperand(1)));
1076 Vals.push_back(VE.getValueID(I.getOperand(2)));
1077 break;
1078 case Instruction::ICmp:
1079 case Instruction::FCmp:
Nick Lewycky7f6aa2b2009-07-08 03:04:38 +00001080 // compare returning Int1Ty or vector of Int1Ty
1081 Code = bitc::FUNC_CODE_INST_CMP2;
Chris Lattner7337ab92007-05-06 00:00:00 +00001082 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
Chris Lattnerd309c752007-05-01 02:13:26 +00001083 Vals.push_back(VE.getValueID(I.getOperand(1)));
1084 Vals.push_back(cast<CmpInst>(I).getPredicate());
1085 break;
1086
Daniel Dunbara279bc32009-09-20 02:20:51 +00001087 case Instruction::Ret:
Devang Pateld9d99ff2008-02-26 01:29:32 +00001088 {
1089 Code = bitc::FUNC_CODE_INST_RET;
1090 unsigned NumOperands = I.getNumOperands();
Devang Pateld9d99ff2008-02-26 01:29:32 +00001091 if (NumOperands == 0)
1092 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV;
1093 else if (NumOperands == 1) {
1094 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE))
1095 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV;
1096 } else {
1097 for (unsigned i = 0, e = NumOperands; i != e; ++i)
1098 PushValueAndType(I.getOperand(i), InstID, Vals, VE);
1099 }
1100 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001101 break;
1102 case Instruction::Br:
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001103 {
1104 Code = bitc::FUNC_CODE_INST_BR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001105 BranchInst &II = cast<BranchInst>(I);
Gabor Greifae6ab1e2009-01-30 18:27:21 +00001106 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
1107 if (II.isConditional()) {
1108 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
1109 Vals.push_back(VE.getValueID(II.getCondition()));
1110 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001111 }
1112 break;
1113 case Instruction::Switch:
1114 Code = bitc::FUNC_CODE_INST_SWITCH;
1115 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001116 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1117 Vals.push_back(VE.getValueID(I.getOperand(i)));
1118 break;
Chris Lattnerab21db72009-10-28 00:19:10 +00001119 case Instruction::IndirectBr:
1120 Code = bitc::FUNC_CODE_INST_INDIRECTBR;
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001121 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
1122 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
1123 Vals.push_back(VE.getValueID(I.getOperand(i)));
1124 break;
1125
Chris Lattner60ce9b52007-05-01 07:03:37 +00001126 case Instruction::Invoke: {
Gabor Greif75a46eb2009-01-16 18:40:27 +00001127 const InvokeInst *II = cast<InvokeInst>(&I);
1128 const Value *Callee(II->getCalledValue());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001129 PointerType *PTy = cast<PointerType>(Callee->getType());
1130 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnerd309c752007-05-01 02:13:26 +00001131 Code = bitc::FUNC_CODE_INST_INVOKE;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001132
Devang Patel05988662008-09-25 21:00:45 +00001133 Vals.push_back(VE.getAttributeID(II->getAttributes()));
Duncan Sandsdc024672007-11-27 13:23:08 +00001134 Vals.push_back(II->getCallingConv());
Gabor Greif8fe53b72009-01-07 22:39:29 +00001135 Vals.push_back(VE.getValueID(II->getNormalDest()));
1136 Vals.push_back(VE.getValueID(II->getUnwindDest()));
Gabor Greif75a46eb2009-01-16 18:40:27 +00001137 PushValueAndType(Callee, InstID, Vals, VE);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001138
Chris Lattnerd309c752007-05-01 02:13:26 +00001139 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001140 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Gabor Greifc9f75002010-03-24 13:21:49 +00001141 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param.
Chris Lattnerd309c752007-05-01 02:13:26 +00001142
1143 // Emit type/value pairs for varargs params.
1144 if (FTy->isVarArg()) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001145 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3;
Chris Lattner7337ab92007-05-06 00:00:00 +00001146 i != e; ++i)
1147 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg
Chris Lattnerd309c752007-05-01 02:13:26 +00001148 }
1149 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001150 }
Bill Wendlingdccc03b2011-07-31 06:30:59 +00001151 case Instruction::Resume:
1152 Code = bitc::FUNC_CODE_INST_RESUME;
1153 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1154 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001155 case Instruction::Unwind:
1156 Code = bitc::FUNC_CODE_INST_UNWIND;
1157 break;
1158 case Instruction::Unreachable:
1159 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
Chris Lattner94687ac2007-05-06 01:28:01 +00001160 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001161 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001162
Jay Foadc1371202011-06-20 14:18:48 +00001163 case Instruction::PHI: {
1164 const PHINode &PN = cast<PHINode>(I);
Chris Lattnerd309c752007-05-01 02:13:26 +00001165 Code = bitc::FUNC_CODE_INST_PHI;
Jay Foadc1371202011-06-20 14:18:48 +00001166 Vals.push_back(VE.getTypeID(PN.getType()));
1167 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1168 Vals.push_back(VE.getValueID(PN.getIncomingValue(i)));
1169 Vals.push_back(VE.getValueID(PN.getIncomingBlock(i)));
1170 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001171 break;
Jay Foadc1371202011-06-20 14:18:48 +00001172 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001173
Bill Wendlinge6e88262011-08-12 20:24:12 +00001174 case Instruction::LandingPad: {
1175 const LandingPadInst &LP = cast<LandingPadInst>(I);
1176 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
1177 Vals.push_back(VE.getTypeID(LP.getType()));
1178 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1179 Vals.push_back(LP.isCleanup());
1180 Vals.push_back(LP.getNumClauses());
1181 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1182 if (LP.isCatch(I))
1183 Vals.push_back(LandingPadInst::Catch);
1184 else
1185 Vals.push_back(LandingPadInst::Filter);
1186 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1187 }
1188 break;
1189 }
1190
Chris Lattnerd309c752007-05-01 02:13:26 +00001191 case Instruction::Alloca:
1192 Code = bitc::FUNC_CODE_INST_ALLOCA;
1193 Vals.push_back(VE.getTypeID(I.getType()));
Dan Gohmana37dd3e2010-05-28 01:38:28 +00001194 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
Chris Lattnerd309c752007-05-01 02:13:26 +00001195 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1196 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1);
1197 break;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001198
Chris Lattnerd309c752007-05-01 02:13:26 +00001199 case Instruction::Load:
Eli Friedman21006d42011-08-09 23:02:53 +00001200 if (cast<LoadInst>(I).isAtomic()) {
1201 Code = bitc::FUNC_CODE_INST_LOADATOMIC;
1202 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
1203 } else {
1204 Code = bitc::FUNC_CODE_INST_LOAD;
1205 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr
1206 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV;
1207 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001208 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1);
1209 Vals.push_back(cast<LoadInst>(I).isVolatile());
Eli Friedman21006d42011-08-09 23:02:53 +00001210 if (cast<LoadInst>(I).isAtomic()) {
1211 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering()));
1212 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope()));
1213 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001214 break;
1215 case Instruction::Store:
Eli Friedman21006d42011-08-09 23:02:53 +00001216 if (cast<StoreInst>(I).isAtomic())
1217 Code = bitc::FUNC_CODE_INST_STOREATOMIC;
1218 else
1219 Code = bitc::FUNC_CODE_INST_STORE;
Christopher Lambfe63fb92007-12-11 08:59:05 +00001220 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr
1221 Vals.push_back(VE.getValueID(I.getOperand(0))); // val.
Chris Lattnerd309c752007-05-01 02:13:26 +00001222 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1);
1223 Vals.push_back(cast<StoreInst>(I).isVolatile());
Eli Friedman21006d42011-08-09 23:02:53 +00001224 if (cast<StoreInst>(I).isAtomic()) {
1225 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering()));
1226 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope()));
1227 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001228 break;
Eli Friedmanff030482011-07-28 21:48:00 +00001229 case Instruction::AtomicCmpXchg:
1230 Code = bitc::FUNC_CODE_INST_CMPXCHG;
1231 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1232 Vals.push_back(VE.getValueID(I.getOperand(1))); // cmp.
1233 Vals.push_back(VE.getValueID(I.getOperand(2))); // newval.
1234 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile());
1235 Vals.push_back(GetEncodedOrdering(
1236 cast<AtomicCmpXchgInst>(I).getOrdering()));
1237 Vals.push_back(GetEncodedSynchScope(
1238 cast<AtomicCmpXchgInst>(I).getSynchScope()));
1239 break;
1240 case Instruction::AtomicRMW:
1241 Code = bitc::FUNC_CODE_INST_ATOMICRMW;
1242 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr
1243 Vals.push_back(VE.getValueID(I.getOperand(1))); // val.
1244 Vals.push_back(GetEncodedRMWOperation(
1245 cast<AtomicRMWInst>(I).getOperation()));
1246 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile());
1247 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering()));
1248 Vals.push_back(GetEncodedSynchScope(
1249 cast<AtomicRMWInst>(I).getSynchScope()));
1250 break;
Eli Friedman47f35132011-07-25 23:16:38 +00001251 case Instruction::Fence:
1252 Code = bitc::FUNC_CODE_INST_FENCE;
1253 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering()));
1254 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope()));
1255 break;
Chris Lattnerd309c752007-05-01 02:13:26 +00001256 case Instruction::Call: {
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001257 const CallInst &CI = cast<CallInst>(I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001258 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType());
1259 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
Chris Lattnera9bb7132007-05-08 05:38:01 +00001260
Chris Lattner4f6bab92011-06-17 18:17:37 +00001261 Code = bitc::FUNC_CODE_INST_CALL;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001262
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001263 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1264 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1265 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
Daniel Dunbara279bc32009-09-20 02:20:51 +00001266
Chris Lattnerd309c752007-05-01 02:13:26 +00001267 // Emit value #'s for the fixed parameters.
Chris Lattnerd309c752007-05-01 02:13:26 +00001268 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001269 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001270
Chris Lattner60ce9b52007-05-01 07:03:37 +00001271 // Emit type/value pairs for varargs params.
1272 if (FTy->isVarArg()) {
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001273 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands();
Chris Lattner7337ab92007-05-06 00:00:00 +00001274 i != e; ++i)
Gabor Greiffc91e7d2010-06-26 09:35:09 +00001275 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs
Chris Lattnerd309c752007-05-01 02:13:26 +00001276 }
1277 break;
Chris Lattner60ce9b52007-05-01 07:03:37 +00001278 }
Chris Lattnerd309c752007-05-01 02:13:26 +00001279 case Instruction::VAArg:
1280 Code = bitc::FUNC_CODE_INST_VAARG;
1281 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty
1282 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist.
1283 Vals.push_back(VE.getTypeID(I.getType())); // restype.
1284 break;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001285 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001286
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001287 Stream.EmitRecord(Code, Vals, AbbrevToUse);
1288 Vals.clear();
1289}
1290
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001291// Emit names for globals/functions etc.
1292static void WriteValueSymbolTable(const ValueSymbolTable &VST,
1293 const ValueEnumerator &VE,
1294 BitstreamWriter &Stream) {
1295 if (VST.empty()) return;
Chris Lattnerff294a42007-05-05 01:26:50 +00001296 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4);
Chris Lattner2e7899d2007-05-04 20:34:50 +00001297
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001298 // FIXME: Set up the abbrev, we know how many values there are!
1299 // FIXME: We know if the type names can use 7-bit ascii.
1300 SmallVector<unsigned, 64> NameVals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001301
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001302 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end();
1303 SI != SE; ++SI) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001304
Chris Lattner59698302007-05-04 20:52:02 +00001305 const ValueName &Name = *SI;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001306
Chris Lattner59698302007-05-04 20:52:02 +00001307 // Figure out the encoding to use for the name.
1308 bool is7Bit = true;
Chris Lattnerff294a42007-05-05 01:26:50 +00001309 bool isChar6 = true;
1310 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength();
1311 C != E; ++C) {
Daniel Dunbara279bc32009-09-20 02:20:51 +00001312 if (isChar6)
Chris Lattnerff294a42007-05-05 01:26:50 +00001313 isChar6 = BitCodeAbbrevOp::isChar6(*C);
1314 if ((unsigned char)*C & 128) {
Chris Lattner59698302007-05-04 20:52:02 +00001315 is7Bit = false;
Chris Lattnerff294a42007-05-05 01:26:50 +00001316 break; // don't bother scanning the rest.
Chris Lattner59698302007-05-04 20:52:02 +00001317 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001318 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001319
Chris Lattnerfd1ae952007-05-04 21:31:13 +00001320 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001321
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001322 // VST_ENTRY: [valueid, namechar x N]
1323 // VST_BBENTRY: [bbid, namechar x N]
Chris Lattnere825ed52007-05-03 22:18:21 +00001324 unsigned Code;
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001325 if (isa<BasicBlock>(SI->getValue())) {
1326 Code = bitc::VST_CODE_BBENTRY;
1327 if (isChar6)
1328 AbbrevToUse = VST_BBENTRY_6_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001329 } else {
1330 Code = bitc::VST_CODE_ENTRY;
Chris Lattnerff294a42007-05-05 01:26:50 +00001331 if (isChar6)
1332 AbbrevToUse = VST_ENTRY_6_ABBREV;
1333 else if (is7Bit)
1334 AbbrevToUse = VST_ENTRY_7_ABBREV;
Chris Lattnere825ed52007-05-03 22:18:21 +00001335 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001336
Chris Lattnere825ed52007-05-03 22:18:21 +00001337 NameVals.push_back(VE.getValueID(SI->getValue()));
Chris Lattner59698302007-05-04 20:52:02 +00001338 for (const char *P = Name.getKeyData(),
1339 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P)
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001340 NameVals.push_back((unsigned char)*P);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001341
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001342 // Emit the finished record.
Chris Lattnere825ed52007-05-03 22:18:21 +00001343 Stream.EmitRecord(Code, NameVals, AbbrevToUse);
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001344 NameVals.clear();
1345 }
1346 Stream.ExitBlock();
1347}
1348
Chris Lattner8d35c792007-04-26 03:50:57 +00001349/// WriteFunction - Emit a function body to the module stream.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001350static void WriteFunction(const Function &F, ValueEnumerator &VE,
Chris Lattner198f34a2007-04-26 03:27:58 +00001351 BitstreamWriter &Stream) {
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001352 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4);
Chad Rosier6a0c04d2011-06-03 17:02:19 +00001353 VE.incorporateFunction(F);
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001354
1355 SmallVector<unsigned, 64> Vals;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001356
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001357 // Emit the number of basic blocks, so the reader can create them ahead of
1358 // time.
1359 Vals.push_back(VE.getBasicBlocks().size());
1360 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals);
1361 Vals.clear();
Daniel Dunbara279bc32009-09-20 02:20:51 +00001362
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001363 // If there are function-local constants, emit them now.
1364 unsigned CstStart, CstEnd;
1365 VE.getFunctionConstantRange(CstStart, CstEnd);
Devang Patel912538b2009-08-04 05:01:35 +00001366 WriteConstants(CstStart, CstEnd, VE, Stream, false);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001367
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001368 // If there is function-local metadata, emit it now.
Victor Hernandez3cd7c3d2010-01-14 19:38:44 +00001369 WriteFunctionLocalMetadata(F, VE, Stream);
Victor Hernandez8ea5d4c2010-01-14 01:50:08 +00001370
Daniel Dunbara279bc32009-09-20 02:20:51 +00001371 // Keep a running idea of what the instruction ID is.
Chris Lattner7337ab92007-05-06 00:00:00 +00001372 unsigned InstID = CstEnd;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001373
Chris Lattnera6245242010-04-03 02:17:50 +00001374 bool NeedsMetadataAttachment = false;
1375
1376 DebugLoc LastDL;
1377
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001378 // Finally, emit all the instructions, in order.
Nick Lewycky280a6e62008-04-25 16:53:59 +00001379 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
Chris Lattner7337ab92007-05-06 00:00:00 +00001380 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
1381 I != E; ++I) {
1382 WriteInstruction(*I, InstID, VE, Stream, Vals);
Chris Lattnera6245242010-04-03 02:17:50 +00001383
Chris Lattner3990b122009-12-28 23:41:32 +00001384 if (!I->getType()->isVoidTy())
Chris Lattner7337ab92007-05-06 00:00:00 +00001385 ++InstID;
Chris Lattnera6245242010-04-03 02:17:50 +00001386
1387 // If the instruction has metadata, write a metadata attachment later.
1388 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
1389
1390 // If the instruction has a debug location, emit it.
1391 DebugLoc DL = I->getDebugLoc();
1392 if (DL.isUnknown()) {
1393 // nothing todo.
1394 } else if (DL == LastDL) {
1395 // Just repeat the same debug loc as last time.
1396 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals);
1397 } else {
1398 MDNode *Scope, *IA;
1399 DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
1400
1401 Vals.push_back(DL.getLine());
1402 Vals.push_back(DL.getCol());
1403 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0);
1404 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0);
Chris Lattner4f6bab92011-06-17 18:17:37 +00001405 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
Chris Lattnera6245242010-04-03 02:17:50 +00001406 Vals.clear();
1407
1408 LastDL = DL;
1409 }
Chris Lattner7337ab92007-05-06 00:00:00 +00001410 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001411
Chris Lattnerbe1f9932007-05-01 02:14:57 +00001412 // Emit names for all the instructions etc.
1413 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001414
Chris Lattnera6245242010-04-03 02:17:50 +00001415 if (NeedsMetadataAttachment)
1416 WriteMetadataAttachment(F, VE, Stream);
Chad Rosier6a0c04d2011-06-03 17:02:19 +00001417 VE.purgeFunction();
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +00001418 Stream.ExitBlock();
Chris Lattner198f34a2007-04-26 03:27:58 +00001419}
1420
Chris Lattner07faafc2007-05-04 18:26:27 +00001421// Emit blockinfo, which defines the standard abbreviations etc.
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001422static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) {
Chris Lattner07faafc2007-05-04 18:26:27 +00001423 // We only want to emit block info records for blocks that have multiple
1424 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other
1425 // blocks can defined their abbrevs inline.
Chris Lattnere17b6582007-05-05 00:17:00 +00001426 Stream.EnterBlockInfoBlock(2);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001427
Chris Lattnere17b6582007-05-05 00:17:00 +00001428 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings.
1429 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1430 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));
1431 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
Daniel Dunbara279bc32009-09-20 02:20:51 +00001434 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnere17b6582007-05-05 00:17:00 +00001435 Abbv) != VST_ENTRY_8_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001436 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001437 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001438
Chris Lattnere17b6582007-05-05 00:17:00 +00001439 { // 7-bit fixed width VST_ENTRY strings.
1440 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1441 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1442 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1443 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
1445 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1446 Abbv) != VST_ENTRY_7_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001447 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001448 }
Chris Lattnerff294a42007-05-05 01:26:50 +00001449 { // 6-bit char6 VST_ENTRY strings.
1450 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1451 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY));
1452 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1453 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1454 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
1455 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
1456 Abbv) != VST_ENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001457 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerff294a42007-05-05 01:26:50 +00001458 }
1459 { // 6-bit char6 VST_BBENTRY strings.
Chris Lattnere17b6582007-05-05 00:17:00 +00001460 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1461 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY));
1462 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1463 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Chris Lattnerff294a42007-05-05 01:26:50 +00001464 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
Chris Lattnere17b6582007-05-05 00:17:00 +00001465 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID,
Chris Lattnerff294a42007-05-05 01:26:50 +00001466 Abbv) != VST_BBENTRY_6_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001467 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnere17b6582007-05-05 00:17:00 +00001468 }
Bill Wendling5d7a5a42011-04-10 23:18:04 +00001469
1470
Daniel Dunbara279bc32009-09-20 02:20:51 +00001471
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001472 { // SETTYPE abbrev for CONSTANTS_BLOCK.
1473 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1474 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE));
1475 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,
1476 Log2_32_Ceil(VE.getTypes().size()+1)));
1477 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1478 Abbv) != CONSTANTS_SETTYPE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001479 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001480 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001481
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001482 { // INTEGER abbrev for CONSTANTS_BLOCK.
1483 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1484 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER));
1485 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
1486 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1487 Abbv) != CONSTANTS_INTEGER_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001488 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001489 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001490
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001491 { // CE_CAST abbrev for CONSTANTS_BLOCK.
1492 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1493 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST));
1494 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc
1495 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid
1496 Log2_32_Ceil(VE.getTypes().size()+1)));
1497 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id
1498
1499 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1500 Abbv) != CONSTANTS_CE_CAST_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001501 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001502 }
1503 { // NULL abbrev for CONSTANTS_BLOCK.
1504 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1505 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL));
1506 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID,
1507 Abbv) != CONSTANTS_NULL_Abbrev)
Torok Edwinc23197a2009-07-14 16:55:14 +00001508 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001509 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001510
Chris Lattner440168b2007-05-05 07:44:49 +00001511 // FIXME: This should only use space for first class types!
Daniel Dunbara279bc32009-09-20 02:20:51 +00001512
Chris Lattner440168b2007-05-05 07:44:49 +00001513 { // INST_LOAD abbrev for FUNCTION_BLOCK.
1514 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1515 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD));
Chris Lattner440168b2007-05-05 07:44:49 +00001516 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr
1517 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align
1518 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile
1519 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1520 Abbv) != FUNCTION_INST_LOAD_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001521 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner440168b2007-05-05 07:44:49 +00001522 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001523 { // INST_BINOP abbrev for FUNCTION_BLOCK.
1524 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1525 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1526 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1529 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1530 Abbv) != FUNCTION_INST_BINOP_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001531 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001532 }
Dan Gohman1224c382009-07-20 21:19:07 +00001533 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK.
1534 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1535 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP));
1536 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS
1537 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS
1538 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1539 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags
1540 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1541 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV)
1542 llvm_unreachable("Unexpected abbrev ordering!");
1543 }
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001544 { // INST_CAST abbrev for FUNCTION_BLOCK.
1545 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1546 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST));
1547 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal
1548 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty
1549 Log2_32_Ceil(VE.getTypes().size()+1)));
1550 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc
1551 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1552 Abbv) != FUNCTION_INST_CAST_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001553 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattnerf9f2e832007-05-06 02:38:57 +00001554 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001555
Chris Lattner94687ac2007-05-06 01:28:01 +00001556 { // INST_RET abbrev for FUNCTION_BLOCK.
1557 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1558 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1559 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1560 Abbv) != FUNCTION_INST_RET_VOID_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001561 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001562 }
1563 { // INST_RET abbrev for FUNCTION_BLOCK.
1564 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1565 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET));
1566 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID
1567 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1568 Abbv) != FUNCTION_INST_RET_VAL_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001569 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001570 }
1571 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK.
1572 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
1573 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE));
1574 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID,
1575 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV)
Torok Edwinc23197a2009-07-14 16:55:14 +00001576 llvm_unreachable("Unexpected abbrev ordering!");
Chris Lattner94687ac2007-05-06 01:28:01 +00001577 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001578
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001579 Stream.ExitBlock();
1580}
1581
Chad Rosier32c59812011-12-07 22:49:05 +00001582// Sort the Users based on the order in which the reader parses the bitcode
1583// file.
1584static bool bitcodereader_order(const User *lhs, const User *rhs) {
1585 // TODO: Implement.
1586 return true;
1587}
1588
1589static void WriteUseList(const Value *V, const ValueEnumerator &VE,
1590 BitstreamWriter &Stream) {
1591
1592 // One or zero uses can't get out of order.
1593 if (V->use_empty() || V->hasNUses(1))
1594 return;
1595
1596 // Make a copy of the in-memory use-list for sorting.
1597 unsigned UseListSize = std::distance(V->use_begin(), V->use_end());
1598 SmallVector<const User*, 8> UseList;
1599 UseList.reserve(UseListSize);
1600 for (Value::const_use_iterator I = V->use_begin(), E = V->use_end();
1601 I != E; ++I) {
1602 const User *U = *I;
1603 UseList.push_back(U);
1604 }
1605
1606 // Sort the copy based on the order read by the BitcodeReader.
1607 std::sort(UseList.begin(), UseList.end(), bitcodereader_order);
1608
1609 // TODO: Generate a diff between the BitcodeWriter in-memory use-list and the
1610 // sorted list (i.e., the expected BitcodeReader in-memory use-list).
1611
1612 // TODO: Emit the USELIST_CODE_ENTRYs.
1613}
1614
1615static void WriteFunctionUseList(const Function *F, ValueEnumerator &VE,
1616 BitstreamWriter &Stream) {
1617 VE.incorporateFunction(*F);
1618
1619 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
1620 AI != AE; ++AI)
1621 WriteUseList(AI, VE, Stream);
1622 for (Function::const_iterator BB = F->begin(), FE = F->end(); BB != FE;
1623 ++BB) {
1624 WriteUseList(BB, VE, Stream);
1625 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
1626 ++II) {
1627 WriteUseList(II, VE, Stream);
1628 for (User::const_op_iterator OI = II->op_begin(), E = II->op_end();
1629 OI != E; ++OI) {
1630 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
1631 isa<InlineAsm>(*OI))
1632 WriteUseList(*OI, VE, Stream);
1633 }
1634 }
1635 }
1636 VE.purgeFunction();
1637}
1638
Chad Rosiercbbb0962011-12-07 21:44:12 +00001639// Emit use-lists.
1640static void WriteModuleUseLists(const Module *M, ValueEnumerator &VE,
1641 BitstreamWriter &Stream) {
1642 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3);
1643
Chad Rosier32c59812011-12-07 22:49:05 +00001644 // XXX: this modifies the module, but in a way that should never change the
1645 // behavior of any pass or codegen in LLVM. The problem is that GVs may
1646 // contain entries in the use_list that do not exist in the Module and are
1647 // not stored in the .bc file.
1648 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
1649 I != E; ++I)
1650 I->removeDeadConstantUsers();
1651
1652 // Write the global variables.
1653 for (Module::const_global_iterator GI = M->global_begin(),
1654 GE = M->global_end(); GI != GE; ++GI) {
1655 WriteUseList(GI, VE, Stream);
Chad Rosiercbbb0962011-12-07 21:44:12 +00001656
Chad Rosier32c59812011-12-07 22:49:05 +00001657 // Write the global variable initializers.
1658 if (GI->hasInitializer())
1659 WriteUseList(GI->getInitializer(), VE, Stream);
1660 }
1661
1662 // Write the functions.
1663 for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
1664 WriteUseList(FI, VE, Stream);
1665 if (!FI->isDeclaration())
1666 WriteFunctionUseList(FI, VE, Stream);
1667 }
1668
1669 // Write the aliases.
1670 for (Module::const_alias_iterator AI = M->alias_begin(), AE = M->alias_end();
1671 AI != AE; ++AI) {
1672 WriteUseList(AI, VE, Stream);
1673 WriteUseList(AI->getAliasee(), VE, Stream);
1674 }
Chad Rosiercbbb0962011-12-07 21:44:12 +00001675
1676 Stream.ExitBlock();
1677}
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001678
1679/// WriteModule - Emit the specified module to the bitstream.
1680static void WriteModule(const Module *M, BitstreamWriter &Stream) {
1681 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001682
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001683 // Emit the version number if it is non-zero.
1684 if (CurVersion) {
1685 SmallVector<unsigned, 1> Vals;
1686 Vals.push_back(CurVersion);
1687 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals);
1688 }
Daniel Dunbara279bc32009-09-20 02:20:51 +00001689
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001690 // Analyze the module, enumerating globals, functions, etc.
1691 ValueEnumerator VE(M);
1692
1693 // Emit blockinfo, which defines the standard abbreviations etc.
1694 WriteBlockInfo(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001695
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001696 // Emit information about parameter attributes.
Devang Patel05988662008-09-25 21:00:45 +00001697 WriteAttributeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001698
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001699 // Emit information describing all of the types in the module.
1700 WriteTypeTable(VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001701
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001702 // Emit top-level description of module, including target triple, inline asm,
1703 // descriptors for global variables, and function prototype info.
1704 WriteModuleInfo(M, VE, Stream);
Devang Patele54abc92009-07-22 17:43:22 +00001705
Devang Patel104cf9e2009-07-23 01:07:34 +00001706 // Emit constants.
1707 WriteModuleConstants(VE, Stream);
1708
Devang Patel912538b2009-08-04 05:01:35 +00001709 // Emit metadata.
Dan Gohman17aa92c2010-07-21 23:38:33 +00001710 WriteModuleMetadata(M, VE, Stream);
Devang Patel912538b2009-08-04 05:01:35 +00001711
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001712 // Emit function bodies.
Chad Rosier3d26f232011-06-03 05:09:12 +00001713 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F)
1714 if (!F->isDeclaration())
1715 WriteFunction(*F, VE, Stream);
Devang Patele8e02132009-09-18 19:26:43 +00001716
1717 // Emit metadata.
Chris Lattner7d05c462009-12-28 20:10:43 +00001718 WriteModuleMetadataStore(M, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001719
Chris Lattnera0f1ecc2007-05-05 07:36:14 +00001720 // Emit names for globals/functions etc.
1721 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001722
Chad Rosiercbbb0962011-12-07 21:44:12 +00001723 // Emit use-lists.
1724 if (EnablePreserveUseListOrdering)
1725 WriteModuleUseLists(M, VE, Stream);
1726
Chris Lattner07faafc2007-05-04 18:26:27 +00001727 Stream.ExitBlock();
1728}
1729
Chris Lattner6fa6a322008-07-09 05:14:23 +00001730/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
1731/// header and trailer to make it compatible with the system archiver. To do
1732/// this we emit the following header, and then emit a trailer that pads the
1733/// file out to be a multiple of 16 bytes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00001734///
Chris Lattner6fa6a322008-07-09 05:14:23 +00001735/// struct bc_header {
1736/// uint32_t Magic; // 0x0B17C0DE
1737/// uint32_t Version; // Version, currently always 0.
1738/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
1739/// uint32_t BitcodeSize; // Size of traditional bitcode file.
1740/// uint32_t CPUType; // CPU specifier.
1741/// ... potentially more later ...
1742/// };
1743enum {
1744 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
1745 DarwinBCHeaderSize = 5*4
1746};
1747
Evan Chenge889c692011-06-14 01:51:33 +00001748static void EmitDarwinBCHeader(BitstreamWriter &Stream, const Triple &TT) {
Chris Lattner6fa6a322008-07-09 05:14:23 +00001749 unsigned CPUType = ~0U;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001750
Evan Cheng0f8868b2010-02-12 20:13:44 +00001751 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*,
Evan Cheng3922a9b2010-02-12 20:39:35 +00001752 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic
1753 // number from /usr/include/mach/machine.h. It is ok to reproduce the
1754 // specific constants here because they are implicitly part of the Darwin ABI.
Chris Lattner6fa6a322008-07-09 05:14:23 +00001755 enum {
1756 DARWIN_CPU_ARCH_ABI64 = 0x01000000,
1757 DARWIN_CPU_TYPE_X86 = 7,
Evan Cheng0f8868b2010-02-12 20:13:44 +00001758 DARWIN_CPU_TYPE_ARM = 12,
Chris Lattner6fa6a322008-07-09 05:14:23 +00001759 DARWIN_CPU_TYPE_POWERPC = 18
1760 };
Daniel Dunbara279bc32009-09-20 02:20:51 +00001761
Evan Chenge889c692011-06-14 01:51:33 +00001762 Triple::ArchType Arch = TT.getArch();
1763 if (Arch == Triple::x86_64)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001764 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
Evan Chenge889c692011-06-14 01:51:33 +00001765 else if (Arch == Triple::x86)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001766 CPUType = DARWIN_CPU_TYPE_X86;
Evan Chenge889c692011-06-14 01:51:33 +00001767 else if (Arch == Triple::ppc)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001768 CPUType = DARWIN_CPU_TYPE_POWERPC;
Evan Chenge889c692011-06-14 01:51:33 +00001769 else if (Arch == Triple::ppc64)
Chris Lattner6fa6a322008-07-09 05:14:23 +00001770 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
Evan Chenge889c692011-06-14 01:51:33 +00001771 else if (Arch == Triple::arm || Arch == Triple::thumb)
Evan Cheng0f8868b2010-02-12 20:13:44 +00001772 CPUType = DARWIN_CPU_TYPE_ARM;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001773
Chris Lattner6fa6a322008-07-09 05:14:23 +00001774 // Traditional Bitcode starts after header.
1775 unsigned BCOffset = DarwinBCHeaderSize;
Daniel Dunbara279bc32009-09-20 02:20:51 +00001776
Chris Lattner6fa6a322008-07-09 05:14:23 +00001777 Stream.Emit(0x0B17C0DE, 32);
1778 Stream.Emit(0 , 32); // Version.
1779 Stream.Emit(BCOffset , 32);
1780 Stream.Emit(0 , 32); // Filled in later.
1781 Stream.Emit(CPUType , 32);
1782}
1783
1784/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
1785/// finalize the header.
1786static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
1787 // Update the size field in the header.
1788 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001789
Chris Lattner6fa6a322008-07-09 05:14:23 +00001790 // If the file is not a multiple of 16 bytes, insert dummy padding.
1791 while (BufferSize & 15) {
1792 Stream.Emit(0, 8);
1793 ++BufferSize;
1794 }
1795}
1796
Chris Lattner07faafc2007-05-04 18:26:27 +00001797
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001798/// WriteBitcodeToFile - Write the specified module to the specified output
1799/// stream.
Daniel Dunbard1ce3b42008-10-22 17:39:14 +00001800void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001801 std::vector<unsigned char> Buffer;
1802 BitstreamWriter Stream(Buffer);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001803
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001804 Buffer.reserve(256*1024);
Chris Lattner38e77212008-12-19 18:37:59 +00001805
1806 WriteBitcodeToStream( M, Stream );
Daniel Dunbara279bc32009-09-20 02:20:51 +00001807
Chris Lattner38e77212008-12-19 18:37:59 +00001808 // Write the generated bitstream to "Out".
1809 Out.write((char*)&Buffer.front(), Buffer.size());
Chris Lattner38e77212008-12-19 18:37:59 +00001810}
1811
1812/// WriteBitcodeToStream - Write the specified module to the specified output
1813/// stream.
1814void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) {
Chris Lattner695b2812010-11-29 23:29:54 +00001815 // If this is darwin or another generic macho target, emit a file header and
1816 // trailer if needed.
Evan Chenge889c692011-06-14 01:51:33 +00001817 Triple TT(M->getTargetTriple());
1818 if (TT.isOSDarwin())
1819 EmitDarwinBCHeader(Stream, TT);
Daniel Dunbara279bc32009-09-20 02:20:51 +00001820
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001821 // Emit the file header.
1822 Stream.Emit((unsigned)'B', 8);
1823 Stream.Emit((unsigned)'C', 8);
1824 Stream.Emit(0x0, 4);
1825 Stream.Emit(0xC, 4);
1826 Stream.Emit(0xE, 4);
1827 Stream.Emit(0xD, 4);
1828
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001829 // Emit the module.
1830 WriteModule(M, Stream);
Chris Lattner6fa6a322008-07-09 05:14:23 +00001831
Evan Chenge889c692011-06-14 01:51:33 +00001832 if (TT.isOSDarwin())
Chris Lattner38e77212008-12-19 18:37:59 +00001833 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size());
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001834}