BitcodeWriter.cpp revision 225736
1//===--- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// Bitcode writer implementation. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/Bitcode/ReaderWriter.h" 15#include "llvm/Bitcode/BitstreamWriter.h" 16#include "llvm/Bitcode/LLVMBitCodes.h" 17#include "ValueEnumerator.h" 18#include "llvm/Constants.h" 19#include "llvm/DerivedTypes.h" 20#include "llvm/InlineAsm.h" 21#include "llvm/Instructions.h" 22#include "llvm/Module.h" 23#include "llvm/Operator.h" 24#include "llvm/ValueSymbolTable.h" 25#include "llvm/ADT/Triple.h" 26#include "llvm/Support/ErrorHandling.h" 27#include "llvm/Support/MathExtras.h" 28#include "llvm/Support/raw_ostream.h" 29#include "llvm/Support/Program.h" 30#include <cctype> 31#include <map> 32using namespace llvm; 33 34/// These are manifest constants used by the bitcode writer. They do not need to 35/// be kept in sync with the reader, but need to be consistent within this file. 36enum { 37 CurVersion = 0, 38 39 // VALUE_SYMTAB_BLOCK abbrev id's. 40 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 41 VST_ENTRY_7_ABBREV, 42 VST_ENTRY_6_ABBREV, 43 VST_BBENTRY_6_ABBREV, 44 45 // CONSTANTS_BLOCK abbrev id's. 46 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 47 CONSTANTS_INTEGER_ABBREV, 48 CONSTANTS_CE_CAST_Abbrev, 49 CONSTANTS_NULL_Abbrev, 50 51 // FUNCTION_BLOCK abbrev id's. 52 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 53 FUNCTION_INST_BINOP_ABBREV, 54 FUNCTION_INST_BINOP_FLAGS_ABBREV, 55 FUNCTION_INST_CAST_ABBREV, 56 FUNCTION_INST_RET_VOID_ABBREV, 57 FUNCTION_INST_RET_VAL_ABBREV, 58 FUNCTION_INST_UNREACHABLE_ABBREV 59}; 60 61 62static unsigned GetEncodedCastOpcode(unsigned Opcode) { 63 switch (Opcode) { 64 default: llvm_unreachable("Unknown cast instruction!"); 65 case Instruction::Trunc : return bitc::CAST_TRUNC; 66 case Instruction::ZExt : return bitc::CAST_ZEXT; 67 case Instruction::SExt : return bitc::CAST_SEXT; 68 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 69 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 70 case Instruction::UIToFP : return bitc::CAST_UITOFP; 71 case Instruction::SIToFP : return bitc::CAST_SITOFP; 72 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 73 case Instruction::FPExt : return bitc::CAST_FPEXT; 74 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 75 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 76 case Instruction::BitCast : return bitc::CAST_BITCAST; 77 } 78} 79 80static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 81 switch (Opcode) { 82 default: llvm_unreachable("Unknown binary instruction!"); 83 case Instruction::Add: 84 case Instruction::FAdd: return bitc::BINOP_ADD; 85 case Instruction::Sub: 86 case Instruction::FSub: return bitc::BINOP_SUB; 87 case Instruction::Mul: 88 case Instruction::FMul: return bitc::BINOP_MUL; 89 case Instruction::UDiv: return bitc::BINOP_UDIV; 90 case Instruction::FDiv: 91 case Instruction::SDiv: return bitc::BINOP_SDIV; 92 case Instruction::URem: return bitc::BINOP_UREM; 93 case Instruction::FRem: 94 case Instruction::SRem: return bitc::BINOP_SREM; 95 case Instruction::Shl: return bitc::BINOP_SHL; 96 case Instruction::LShr: return bitc::BINOP_LSHR; 97 case Instruction::AShr: return bitc::BINOP_ASHR; 98 case Instruction::And: return bitc::BINOP_AND; 99 case Instruction::Or: return bitc::BINOP_OR; 100 case Instruction::Xor: return bitc::BINOP_XOR; 101 } 102} 103 104static void WriteStringRecord(unsigned Code, StringRef Str, 105 unsigned AbbrevToUse, BitstreamWriter &Stream) { 106 SmallVector<unsigned, 64> Vals; 107 108 // Code: [strchar x N] 109 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 110 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) 111 AbbrevToUse = 0; 112 Vals.push_back(Str[i]); 113 } 114 115 // Emit the finished record. 116 Stream.EmitRecord(Code, Vals, AbbrevToUse); 117} 118 119// Emit information about parameter attributes. 120static void WriteAttributeTable(const ValueEnumerator &VE, 121 BitstreamWriter &Stream) { 122 const std::vector<AttrListPtr> &Attrs = VE.getAttributes(); 123 if (Attrs.empty()) return; 124 125 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 126 127 SmallVector<uint64_t, 64> Record; 128 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 129 const AttrListPtr &A = Attrs[i]; 130 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) { 131 const AttributeWithIndex &PAWI = A.getSlot(i); 132 Record.push_back(PAWI.Index); 133 134 // FIXME: remove in LLVM 3.0 135 // Store the alignment in the bitcode as a 16-bit raw value instead of a 136 // 5-bit log2 encoded value. Shift the bits above the alignment up by 137 // 11 bits. 138 uint64_t FauxAttr = PAWI.Attrs & 0xffff; 139 if (PAWI.Attrs & Attribute::Alignment) 140 FauxAttr |= (1ull<<16)<<(((PAWI.Attrs & Attribute::Alignment)-1) >> 16); 141 FauxAttr |= (PAWI.Attrs & (0x3FFull << 21)) << 11; 142 143 Record.push_back(FauxAttr); 144 } 145 146 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 147 Record.clear(); 148 } 149 150 Stream.ExitBlock(); 151} 152 153/// WriteTypeTable - Write out the type table for a module. 154static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { 155 const ValueEnumerator::TypeList &TypeList = VE.getTypes(); 156 157 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 158 SmallVector<uint64_t, 64> TypeVals; 159 160 // Abbrev for TYPE_CODE_POINTER. 161 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 162 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 163 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 164 Log2_32_Ceil(VE.getTypes().size()+1))); 165 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 166 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 167 168 // Abbrev for TYPE_CODE_FUNCTION. 169 Abbv = new BitCodeAbbrev(); 170 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 171 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 172 Abbv->Add(BitCodeAbbrevOp(0)); // FIXME: DEAD value, remove in LLVM 3.0 173 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 174 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 175 Log2_32_Ceil(VE.getTypes().size()+1))); 176 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 177 178 // Abbrev for TYPE_CODE_STRUCT_ANON. 179 Abbv = new BitCodeAbbrev(); 180 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 181 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 182 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 183 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 184 Log2_32_Ceil(VE.getTypes().size()+1))); 185 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 186 187 // Abbrev for TYPE_CODE_STRUCT_NAME. 188 Abbv = new BitCodeAbbrev(); 189 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 190 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 191 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 192 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 193 194 // Abbrev for TYPE_CODE_STRUCT_NAMED. 195 Abbv = new BitCodeAbbrev(); 196 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 197 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 198 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 199 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 200 Log2_32_Ceil(VE.getTypes().size()+1))); 201 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 202 203 204 // Abbrev for TYPE_CODE_ARRAY. 205 Abbv = new BitCodeAbbrev(); 206 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 207 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 208 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 209 Log2_32_Ceil(VE.getTypes().size()+1))); 210 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 211 212 // Emit an entry count so the reader can reserve space. 213 TypeVals.push_back(TypeList.size()); 214 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 215 TypeVals.clear(); 216 217 // Loop over all of the types, emitting each in turn. 218 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 219 const Type *T = TypeList[i]; 220 int AbbrevToUse = 0; 221 unsigned Code = 0; 222 223 switch (T->getTypeID()) { 224 default: llvm_unreachable("Unknown type!"); 225 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 226 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 227 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 228 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 229 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 230 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 231 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 232 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 233 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; 234 case Type::IntegerTyID: 235 // INTEGER: [width] 236 Code = bitc::TYPE_CODE_INTEGER; 237 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 238 break; 239 case Type::PointerTyID: { 240 const PointerType *PTy = cast<PointerType>(T); 241 // POINTER: [pointee type, address space] 242 Code = bitc::TYPE_CODE_POINTER; 243 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 244 unsigned AddressSpace = PTy->getAddressSpace(); 245 TypeVals.push_back(AddressSpace); 246 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 247 break; 248 } 249 case Type::FunctionTyID: { 250 const FunctionType *FT = cast<FunctionType>(T); 251 // FUNCTION: [isvararg, attrid, retty, paramty x N] 252 Code = bitc::TYPE_CODE_FUNCTION; 253 TypeVals.push_back(FT->isVarArg()); 254 TypeVals.push_back(0); // FIXME: DEAD: remove in llvm 3.0 255 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 256 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 257 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 258 AbbrevToUse = FunctionAbbrev; 259 break; 260 } 261 case Type::StructTyID: { 262 const StructType *ST = cast<StructType>(T); 263 // STRUCT: [ispacked, eltty x N] 264 TypeVals.push_back(ST->isPacked()); 265 // Output all of the element types. 266 for (StructType::element_iterator I = ST->element_begin(), 267 E = ST->element_end(); I != E; ++I) 268 TypeVals.push_back(VE.getTypeID(*I)); 269 270 if (ST->isAnonymous()) { 271 Code = bitc::TYPE_CODE_STRUCT_ANON; 272 AbbrevToUse = StructAnonAbbrev; 273 } else { 274 if (ST->isOpaque()) { 275 Code = bitc::TYPE_CODE_OPAQUE; 276 } else { 277 Code = bitc::TYPE_CODE_STRUCT_NAMED; 278 AbbrevToUse = StructNamedAbbrev; 279 } 280 281 // Emit the name if it is present. 282 if (!ST->getName().empty()) 283 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 284 StructNameAbbrev, Stream); 285 } 286 break; 287 } 288 case Type::ArrayTyID: { 289 const ArrayType *AT = cast<ArrayType>(T); 290 // ARRAY: [numelts, eltty] 291 Code = bitc::TYPE_CODE_ARRAY; 292 TypeVals.push_back(AT->getNumElements()); 293 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 294 AbbrevToUse = ArrayAbbrev; 295 break; 296 } 297 case Type::VectorTyID: { 298 const VectorType *VT = cast<VectorType>(T); 299 // VECTOR [numelts, eltty] 300 Code = bitc::TYPE_CODE_VECTOR; 301 TypeVals.push_back(VT->getNumElements()); 302 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 303 break; 304 } 305 } 306 307 // Emit the finished record. 308 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 309 TypeVals.clear(); 310 } 311 312 Stream.ExitBlock(); 313} 314 315static unsigned getEncodedLinkage(const GlobalValue *GV) { 316 switch (GV->getLinkage()) { 317 default: llvm_unreachable("Invalid linkage!"); 318 case GlobalValue::ExternalLinkage: return 0; 319 case GlobalValue::WeakAnyLinkage: return 1; 320 case GlobalValue::AppendingLinkage: return 2; 321 case GlobalValue::InternalLinkage: return 3; 322 case GlobalValue::LinkOnceAnyLinkage: return 4; 323 case GlobalValue::DLLImportLinkage: return 5; 324 case GlobalValue::DLLExportLinkage: return 6; 325 case GlobalValue::ExternalWeakLinkage: return 7; 326 case GlobalValue::CommonLinkage: return 8; 327 case GlobalValue::PrivateLinkage: return 9; 328 case GlobalValue::WeakODRLinkage: return 10; 329 case GlobalValue::LinkOnceODRLinkage: return 11; 330 case GlobalValue::AvailableExternallyLinkage: return 12; 331 case GlobalValue::LinkerPrivateLinkage: return 13; 332 case GlobalValue::LinkerPrivateWeakLinkage: return 14; 333 case GlobalValue::LinkerPrivateWeakDefAutoLinkage: return 15; 334 } 335} 336 337static unsigned getEncodedVisibility(const GlobalValue *GV) { 338 switch (GV->getVisibility()) { 339 default: llvm_unreachable("Invalid visibility!"); 340 case GlobalValue::DefaultVisibility: return 0; 341 case GlobalValue::HiddenVisibility: return 1; 342 case GlobalValue::ProtectedVisibility: return 2; 343 } 344} 345 346// Emit top-level description of module, including target triple, inline asm, 347// descriptors for global variables, and function prototype info. 348static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, 349 BitstreamWriter &Stream) { 350 // Emit the list of dependent libraries for the Module. 351 for (Module::lib_iterator I = M->lib_begin(), E = M->lib_end(); I != E; ++I) 352 WriteStringRecord(bitc::MODULE_CODE_DEPLIB, *I, 0/*TODO*/, Stream); 353 354 // Emit various pieces of data attached to a module. 355 if (!M->getTargetTriple().empty()) 356 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 357 0/*TODO*/, Stream); 358 if (!M->getDataLayout().empty()) 359 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), 360 0/*TODO*/, Stream); 361 if (!M->getModuleInlineAsm().empty()) 362 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 363 0/*TODO*/, Stream); 364 365 // Emit information about sections and GC, computing how many there are. Also 366 // compute the maximum alignment value. 367 std::map<std::string, unsigned> SectionMap; 368 std::map<std::string, unsigned> GCMap; 369 unsigned MaxAlignment = 0; 370 unsigned MaxGlobalType = 0; 371 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 372 GV != E; ++GV) { 373 MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); 374 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); 375 376 if (!GV->hasSection()) continue; 377 // Give section names unique ID's. 378 unsigned &Entry = SectionMap[GV->getSection()]; 379 if (Entry != 0) continue; 380 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), 381 0/*TODO*/, Stream); 382 Entry = SectionMap.size(); 383 } 384 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 385 MaxAlignment = std::max(MaxAlignment, F->getAlignment()); 386 if (F->hasSection()) { 387 // Give section names unique ID's. 388 unsigned &Entry = SectionMap[F->getSection()]; 389 if (!Entry) { 390 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), 391 0/*TODO*/, Stream); 392 Entry = SectionMap.size(); 393 } 394 } 395 if (F->hasGC()) { 396 // Same for GC names. 397 unsigned &Entry = GCMap[F->getGC()]; 398 if (!Entry) { 399 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), 400 0/*TODO*/, Stream); 401 Entry = GCMap.size(); 402 } 403 } 404 } 405 406 // Emit abbrev for globals, now that we know # sections and max alignment. 407 unsigned SimpleGVarAbbrev = 0; 408 if (!M->global_empty()) { 409 // Add an abbrev for common globals with no visibility or thread localness. 410 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 411 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 413 Log2_32_Ceil(MaxGlobalType+1))); 414 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 415 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 416 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 417 if (MaxAlignment == 0) // Alignment. 418 Abbv->Add(BitCodeAbbrevOp(0)); 419 else { 420 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 421 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 422 Log2_32_Ceil(MaxEncAlignment+1))); 423 } 424 if (SectionMap.empty()) // Section. 425 Abbv->Add(BitCodeAbbrevOp(0)); 426 else 427 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 428 Log2_32_Ceil(SectionMap.size()+1))); 429 // Don't bother emitting vis + thread local. 430 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 431 } 432 433 // Emit the global variable information. 434 SmallVector<unsigned, 64> Vals; 435 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 436 GV != E; ++GV) { 437 unsigned AbbrevToUse = 0; 438 439 // GLOBALVAR: [type, isconst, initid, 440 // linkage, alignment, section, visibility, threadlocal, 441 // unnamed_addr] 442 Vals.push_back(VE.getTypeID(GV->getType())); 443 Vals.push_back(GV->isConstant()); 444 Vals.push_back(GV->isDeclaration() ? 0 : 445 (VE.getValueID(GV->getInitializer()) + 1)); 446 Vals.push_back(getEncodedLinkage(GV)); 447 Vals.push_back(Log2_32(GV->getAlignment())+1); 448 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 449 if (GV->isThreadLocal() || 450 GV->getVisibility() != GlobalValue::DefaultVisibility || 451 GV->hasUnnamedAddr()) { 452 Vals.push_back(getEncodedVisibility(GV)); 453 Vals.push_back(GV->isThreadLocal()); 454 Vals.push_back(GV->hasUnnamedAddr()); 455 } else { 456 AbbrevToUse = SimpleGVarAbbrev; 457 } 458 459 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 460 Vals.clear(); 461 } 462 463 // Emit the function proto information. 464 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 465 // FUNCTION: [type, callingconv, isproto, paramattr, 466 // linkage, alignment, section, visibility, gc, unnamed_addr] 467 Vals.push_back(VE.getTypeID(F->getType())); 468 Vals.push_back(F->getCallingConv()); 469 Vals.push_back(F->isDeclaration()); 470 Vals.push_back(getEncodedLinkage(F)); 471 Vals.push_back(VE.getAttributeID(F->getAttributes())); 472 Vals.push_back(Log2_32(F->getAlignment())+1); 473 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); 474 Vals.push_back(getEncodedVisibility(F)); 475 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); 476 Vals.push_back(F->hasUnnamedAddr()); 477 478 unsigned AbbrevToUse = 0; 479 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 480 Vals.clear(); 481 } 482 483 // Emit the alias information. 484 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); 485 AI != E; ++AI) { 486 Vals.push_back(VE.getTypeID(AI->getType())); 487 Vals.push_back(VE.getValueID(AI->getAliasee())); 488 Vals.push_back(getEncodedLinkage(AI)); 489 Vals.push_back(getEncodedVisibility(AI)); 490 unsigned AbbrevToUse = 0; 491 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 492 Vals.clear(); 493 } 494} 495 496static uint64_t GetOptimizationFlags(const Value *V) { 497 uint64_t Flags = 0; 498 499 if (const OverflowingBinaryOperator *OBO = 500 dyn_cast<OverflowingBinaryOperator>(V)) { 501 if (OBO->hasNoSignedWrap()) 502 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 503 if (OBO->hasNoUnsignedWrap()) 504 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 505 } else if (const PossiblyExactOperator *PEO = 506 dyn_cast<PossiblyExactOperator>(V)) { 507 if (PEO->isExact()) 508 Flags |= 1 << bitc::PEO_EXACT; 509 } 510 511 return Flags; 512} 513 514static void WriteMDNode(const MDNode *N, 515 const ValueEnumerator &VE, 516 BitstreamWriter &Stream, 517 SmallVector<uint64_t, 64> &Record) { 518 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 519 if (N->getOperand(i)) { 520 Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); 521 Record.push_back(VE.getValueID(N->getOperand(i))); 522 } else { 523 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); 524 Record.push_back(0); 525 } 526 } 527 unsigned MDCode = N->isFunctionLocal() ? bitc::METADATA_FN_NODE : 528 bitc::METADATA_NODE; 529 Stream.EmitRecord(MDCode, Record, 0); 530 Record.clear(); 531} 532 533static void WriteModuleMetadata(const Module *M, 534 const ValueEnumerator &VE, 535 BitstreamWriter &Stream) { 536 const ValueEnumerator::ValueList &Vals = VE.getMDValues(); 537 bool StartedMetadataBlock = false; 538 unsigned MDSAbbrev = 0; 539 SmallVector<uint64_t, 64> Record; 540 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 541 542 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 543 if (!N->isFunctionLocal() || !N->getFunction()) { 544 if (!StartedMetadataBlock) { 545 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 546 StartedMetadataBlock = true; 547 } 548 WriteMDNode(N, VE, Stream, Record); 549 } 550 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 551 if (!StartedMetadataBlock) { 552 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 553 554 // Abbrev for METADATA_STRING. 555 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 556 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 557 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 558 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 559 MDSAbbrev = Stream.EmitAbbrev(Abbv); 560 StartedMetadataBlock = true; 561 } 562 563 // Code: [strchar x N] 564 Record.append(MDS->begin(), MDS->end()); 565 566 // Emit the finished record. 567 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 568 Record.clear(); 569 } 570 } 571 572 // Write named metadata. 573 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 574 E = M->named_metadata_end(); I != E; ++I) { 575 const NamedMDNode *NMD = I; 576 if (!StartedMetadataBlock) { 577 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 578 StartedMetadataBlock = true; 579 } 580 581 // Write name. 582 StringRef Str = NMD->getName(); 583 for (unsigned i = 0, e = Str.size(); i != e; ++i) 584 Record.push_back(Str[i]); 585 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); 586 Record.clear(); 587 588 // Write named metadata operands. 589 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 590 Record.push_back(VE.getValueID(NMD->getOperand(i))); 591 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 592 Record.clear(); 593 } 594 595 if (StartedMetadataBlock) 596 Stream.ExitBlock(); 597} 598 599static void WriteFunctionLocalMetadata(const Function &F, 600 const ValueEnumerator &VE, 601 BitstreamWriter &Stream) { 602 bool StartedMetadataBlock = false; 603 SmallVector<uint64_t, 64> Record; 604 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 605 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 606 if (const MDNode *N = Vals[i]) 607 if (N->isFunctionLocal() && N->getFunction() == &F) { 608 if (!StartedMetadataBlock) { 609 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 610 StartedMetadataBlock = true; 611 } 612 WriteMDNode(N, VE, Stream, Record); 613 } 614 615 if (StartedMetadataBlock) 616 Stream.ExitBlock(); 617} 618 619static void WriteMetadataAttachment(const Function &F, 620 const ValueEnumerator &VE, 621 BitstreamWriter &Stream) { 622 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 623 624 SmallVector<uint64_t, 64> Record; 625 626 // Write metadata attachments 627 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 628 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 629 630 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 631 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 632 I != E; ++I) { 633 MDs.clear(); 634 I->getAllMetadataOtherThanDebugLoc(MDs); 635 636 // If no metadata, ignore instruction. 637 if (MDs.empty()) continue; 638 639 Record.push_back(VE.getInstructionID(I)); 640 641 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 642 Record.push_back(MDs[i].first); 643 Record.push_back(VE.getValueID(MDs[i].second)); 644 } 645 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 646 Record.clear(); 647 } 648 649 Stream.ExitBlock(); 650} 651 652static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { 653 SmallVector<uint64_t, 64> Record; 654 655 // Write metadata kinds 656 // METADATA_KIND - [n x [id, name]] 657 SmallVector<StringRef, 4> Names; 658 M->getMDKindNames(Names); 659 660 if (Names.empty()) return; 661 662 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 663 664 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 665 Record.push_back(MDKindID); 666 StringRef KName = Names[MDKindID]; 667 Record.append(KName.begin(), KName.end()); 668 669 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 670 Record.clear(); 671 } 672 673 Stream.ExitBlock(); 674} 675 676static void WriteConstants(unsigned FirstVal, unsigned LastVal, 677 const ValueEnumerator &VE, 678 BitstreamWriter &Stream, bool isGlobal) { 679 if (FirstVal == LastVal) return; 680 681 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 682 683 unsigned AggregateAbbrev = 0; 684 unsigned String8Abbrev = 0; 685 unsigned CString7Abbrev = 0; 686 unsigned CString6Abbrev = 0; 687 // If this is a constant pool for the module, emit module-specific abbrevs. 688 if (isGlobal) { 689 // Abbrev for CST_CODE_AGGREGATE. 690 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 691 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 692 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 693 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 694 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 695 696 // Abbrev for CST_CODE_STRING. 697 Abbv = new BitCodeAbbrev(); 698 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 699 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 700 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 701 String8Abbrev = Stream.EmitAbbrev(Abbv); 702 // Abbrev for CST_CODE_CSTRING. 703 Abbv = new BitCodeAbbrev(); 704 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 705 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 707 CString7Abbrev = Stream.EmitAbbrev(Abbv); 708 // Abbrev for CST_CODE_CSTRING. 709 Abbv = new BitCodeAbbrev(); 710 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 711 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 712 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 713 CString6Abbrev = Stream.EmitAbbrev(Abbv); 714 } 715 716 SmallVector<uint64_t, 64> Record; 717 718 const ValueEnumerator::ValueList &Vals = VE.getValues(); 719 const Type *LastTy = 0; 720 for (unsigned i = FirstVal; i != LastVal; ++i) { 721 const Value *V = Vals[i].first; 722 // If we need to switch types, do so now. 723 if (V->getType() != LastTy) { 724 LastTy = V->getType(); 725 Record.push_back(VE.getTypeID(LastTy)); 726 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 727 CONSTANTS_SETTYPE_ABBREV); 728 Record.clear(); 729 } 730 731 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 732 Record.push_back(unsigned(IA->hasSideEffects()) | 733 unsigned(IA->isAlignStack()) << 1); 734 735 // Add the asm string. 736 const std::string &AsmStr = IA->getAsmString(); 737 Record.push_back(AsmStr.size()); 738 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 739 Record.push_back(AsmStr[i]); 740 741 // Add the constraint string. 742 const std::string &ConstraintStr = IA->getConstraintString(); 743 Record.push_back(ConstraintStr.size()); 744 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 745 Record.push_back(ConstraintStr[i]); 746 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 747 Record.clear(); 748 continue; 749 } 750 const Constant *C = cast<Constant>(V); 751 unsigned Code = -1U; 752 unsigned AbbrevToUse = 0; 753 if (C->isNullValue()) { 754 Code = bitc::CST_CODE_NULL; 755 } else if (isa<UndefValue>(C)) { 756 Code = bitc::CST_CODE_UNDEF; 757 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 758 if (IV->getBitWidth() <= 64) { 759 uint64_t V = IV->getSExtValue(); 760 if ((int64_t)V >= 0) 761 Record.push_back(V << 1); 762 else 763 Record.push_back((-V << 1) | 1); 764 Code = bitc::CST_CODE_INTEGER; 765 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 766 } else { // Wide integers, > 64 bits in size. 767 // We have an arbitrary precision integer value to write whose 768 // bit width is > 64. However, in canonical unsigned integer 769 // format it is likely that the high bits are going to be zero. 770 // So, we only write the number of active words. 771 unsigned NWords = IV->getValue().getActiveWords(); 772 const uint64_t *RawWords = IV->getValue().getRawData(); 773 for (unsigned i = 0; i != NWords; ++i) { 774 int64_t V = RawWords[i]; 775 if (V >= 0) 776 Record.push_back(V << 1); 777 else 778 Record.push_back((-V << 1) | 1); 779 } 780 Code = bitc::CST_CODE_WIDE_INTEGER; 781 } 782 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 783 Code = bitc::CST_CODE_FLOAT; 784 const Type *Ty = CFP->getType(); 785 if (Ty->isFloatTy() || Ty->isDoubleTy()) { 786 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 787 } else if (Ty->isX86_FP80Ty()) { 788 // api needed to prevent premature destruction 789 // bits are not in the same order as a normal i80 APInt, compensate. 790 APInt api = CFP->getValueAPF().bitcastToAPInt(); 791 const uint64_t *p = api.getRawData(); 792 Record.push_back((p[1] << 48) | (p[0] >> 16)); 793 Record.push_back(p[0] & 0xffffLL); 794 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 795 APInt api = CFP->getValueAPF().bitcastToAPInt(); 796 const uint64_t *p = api.getRawData(); 797 Record.push_back(p[0]); 798 Record.push_back(p[1]); 799 } else { 800 assert (0 && "Unknown FP type!"); 801 } 802 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) { 803 const ConstantArray *CA = cast<ConstantArray>(C); 804 // Emit constant strings specially. 805 unsigned NumOps = CA->getNumOperands(); 806 // If this is a null-terminated string, use the denser CSTRING encoding. 807 if (CA->getOperand(NumOps-1)->isNullValue()) { 808 Code = bitc::CST_CODE_CSTRING; 809 --NumOps; // Don't encode the null, which isn't allowed by char6. 810 } else { 811 Code = bitc::CST_CODE_STRING; 812 AbbrevToUse = String8Abbrev; 813 } 814 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 815 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 816 for (unsigned i = 0; i != NumOps; ++i) { 817 unsigned char V = cast<ConstantInt>(CA->getOperand(i))->getZExtValue(); 818 Record.push_back(V); 819 isCStr7 &= (V & 128) == 0; 820 if (isCStrChar6) 821 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 822 } 823 824 if (isCStrChar6) 825 AbbrevToUse = CString6Abbrev; 826 else if (isCStr7) 827 AbbrevToUse = CString7Abbrev; 828 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(V) || 829 isa<ConstantVector>(V)) { 830 Code = bitc::CST_CODE_AGGREGATE; 831 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 832 Record.push_back(VE.getValueID(C->getOperand(i))); 833 AbbrevToUse = AggregateAbbrev; 834 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 835 switch (CE->getOpcode()) { 836 default: 837 if (Instruction::isCast(CE->getOpcode())) { 838 Code = bitc::CST_CODE_CE_CAST; 839 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 840 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 841 Record.push_back(VE.getValueID(C->getOperand(0))); 842 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 843 } else { 844 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 845 Code = bitc::CST_CODE_CE_BINOP; 846 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 847 Record.push_back(VE.getValueID(C->getOperand(0))); 848 Record.push_back(VE.getValueID(C->getOperand(1))); 849 uint64_t Flags = GetOptimizationFlags(CE); 850 if (Flags != 0) 851 Record.push_back(Flags); 852 } 853 break; 854 case Instruction::GetElementPtr: 855 Code = bitc::CST_CODE_CE_GEP; 856 if (cast<GEPOperator>(C)->isInBounds()) 857 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 858 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 859 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 860 Record.push_back(VE.getValueID(C->getOperand(i))); 861 } 862 break; 863 case Instruction::Select: 864 Code = bitc::CST_CODE_CE_SELECT; 865 Record.push_back(VE.getValueID(C->getOperand(0))); 866 Record.push_back(VE.getValueID(C->getOperand(1))); 867 Record.push_back(VE.getValueID(C->getOperand(2))); 868 break; 869 case Instruction::ExtractElement: 870 Code = bitc::CST_CODE_CE_EXTRACTELT; 871 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 872 Record.push_back(VE.getValueID(C->getOperand(0))); 873 Record.push_back(VE.getValueID(C->getOperand(1))); 874 break; 875 case Instruction::InsertElement: 876 Code = bitc::CST_CODE_CE_INSERTELT; 877 Record.push_back(VE.getValueID(C->getOperand(0))); 878 Record.push_back(VE.getValueID(C->getOperand(1))); 879 Record.push_back(VE.getValueID(C->getOperand(2))); 880 break; 881 case Instruction::ShuffleVector: 882 // If the return type and argument types are the same, this is a 883 // standard shufflevector instruction. If the types are different, 884 // then the shuffle is widening or truncating the input vectors, and 885 // the argument type must also be encoded. 886 if (C->getType() == C->getOperand(0)->getType()) { 887 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 888 } else { 889 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 890 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 891 } 892 Record.push_back(VE.getValueID(C->getOperand(0))); 893 Record.push_back(VE.getValueID(C->getOperand(1))); 894 Record.push_back(VE.getValueID(C->getOperand(2))); 895 break; 896 case Instruction::ICmp: 897 case Instruction::FCmp: 898 Code = bitc::CST_CODE_CE_CMP; 899 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 900 Record.push_back(VE.getValueID(C->getOperand(0))); 901 Record.push_back(VE.getValueID(C->getOperand(1))); 902 Record.push_back(CE->getPredicate()); 903 break; 904 } 905 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 906 Code = bitc::CST_CODE_BLOCKADDRESS; 907 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 908 Record.push_back(VE.getValueID(BA->getFunction())); 909 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 910 } else { 911#ifndef NDEBUG 912 C->dump(); 913#endif 914 llvm_unreachable("Unknown constant!"); 915 } 916 Stream.EmitRecord(Code, Record, AbbrevToUse); 917 Record.clear(); 918 } 919 920 Stream.ExitBlock(); 921} 922 923static void WriteModuleConstants(const ValueEnumerator &VE, 924 BitstreamWriter &Stream) { 925 const ValueEnumerator::ValueList &Vals = VE.getValues(); 926 927 // Find the first constant to emit, which is the first non-globalvalue value. 928 // We know globalvalues have been emitted by WriteModuleInfo. 929 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 930 if (!isa<GlobalValue>(Vals[i].first)) { 931 WriteConstants(i, Vals.size(), VE, Stream, true); 932 return; 933 } 934 } 935} 936 937/// PushValueAndType - The file has to encode both the value and type id for 938/// many values, because we need to know what type to create for forward 939/// references. However, most operands are not forward references, so this type 940/// field is not needed. 941/// 942/// This function adds V's value ID to Vals. If the value ID is higher than the 943/// instruction ID, then it is a forward reference, and it also includes the 944/// type ID. 945static bool PushValueAndType(const Value *V, unsigned InstID, 946 SmallVector<unsigned, 64> &Vals, 947 ValueEnumerator &VE) { 948 unsigned ValID = VE.getValueID(V); 949 Vals.push_back(ValID); 950 if (ValID >= InstID) { 951 Vals.push_back(VE.getTypeID(V->getType())); 952 return true; 953 } 954 return false; 955} 956 957/// WriteInstruction - Emit an instruction to the specified stream. 958static void WriteInstruction(const Instruction &I, unsigned InstID, 959 ValueEnumerator &VE, BitstreamWriter &Stream, 960 SmallVector<unsigned, 64> &Vals) { 961 unsigned Code = 0; 962 unsigned AbbrevToUse = 0; 963 VE.setInstructionID(&I); 964 switch (I.getOpcode()) { 965 default: 966 if (Instruction::isCast(I.getOpcode())) { 967 Code = bitc::FUNC_CODE_INST_CAST; 968 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 969 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 970 Vals.push_back(VE.getTypeID(I.getType())); 971 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 972 } else { 973 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 974 Code = bitc::FUNC_CODE_INST_BINOP; 975 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 976 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 977 Vals.push_back(VE.getValueID(I.getOperand(1))); 978 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 979 uint64_t Flags = GetOptimizationFlags(&I); 980 if (Flags != 0) { 981 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 982 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 983 Vals.push_back(Flags); 984 } 985 } 986 break; 987 988 case Instruction::GetElementPtr: 989 Code = bitc::FUNC_CODE_INST_GEP; 990 if (cast<GEPOperator>(&I)->isInBounds()) 991 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; 992 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 993 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 994 break; 995 case Instruction::ExtractValue: { 996 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 997 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 998 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 999 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 1000 Vals.push_back(*i); 1001 break; 1002 } 1003 case Instruction::InsertValue: { 1004 Code = bitc::FUNC_CODE_INST_INSERTVAL; 1005 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1006 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1007 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 1008 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 1009 Vals.push_back(*i); 1010 break; 1011 } 1012 case Instruction::Select: 1013 Code = bitc::FUNC_CODE_INST_VSELECT; 1014 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1015 Vals.push_back(VE.getValueID(I.getOperand(2))); 1016 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1017 break; 1018 case Instruction::ExtractElement: 1019 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 1020 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1021 Vals.push_back(VE.getValueID(I.getOperand(1))); 1022 break; 1023 case Instruction::InsertElement: 1024 Code = bitc::FUNC_CODE_INST_INSERTELT; 1025 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1026 Vals.push_back(VE.getValueID(I.getOperand(1))); 1027 Vals.push_back(VE.getValueID(I.getOperand(2))); 1028 break; 1029 case Instruction::ShuffleVector: 1030 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 1031 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1032 Vals.push_back(VE.getValueID(I.getOperand(1))); 1033 Vals.push_back(VE.getValueID(I.getOperand(2))); 1034 break; 1035 case Instruction::ICmp: 1036 case Instruction::FCmp: 1037 // compare returning Int1Ty or vector of Int1Ty 1038 Code = bitc::FUNC_CODE_INST_CMP2; 1039 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1040 Vals.push_back(VE.getValueID(I.getOperand(1))); 1041 Vals.push_back(cast<CmpInst>(I).getPredicate()); 1042 break; 1043 1044 case Instruction::Ret: 1045 { 1046 Code = bitc::FUNC_CODE_INST_RET; 1047 unsigned NumOperands = I.getNumOperands(); 1048 if (NumOperands == 0) 1049 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 1050 else if (NumOperands == 1) { 1051 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1052 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 1053 } else { 1054 for (unsigned i = 0, e = NumOperands; i != e; ++i) 1055 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1056 } 1057 } 1058 break; 1059 case Instruction::Br: 1060 { 1061 Code = bitc::FUNC_CODE_INST_BR; 1062 BranchInst &II = cast<BranchInst>(I); 1063 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 1064 if (II.isConditional()) { 1065 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 1066 Vals.push_back(VE.getValueID(II.getCondition())); 1067 } 1068 } 1069 break; 1070 case Instruction::Switch: 1071 Code = bitc::FUNC_CODE_INST_SWITCH; 1072 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1073 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1074 Vals.push_back(VE.getValueID(I.getOperand(i))); 1075 break; 1076 case Instruction::IndirectBr: 1077 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 1078 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1079 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1080 Vals.push_back(VE.getValueID(I.getOperand(i))); 1081 break; 1082 1083 case Instruction::Invoke: { 1084 const InvokeInst *II = cast<InvokeInst>(&I); 1085 const Value *Callee(II->getCalledValue()); 1086 const PointerType *PTy = cast<PointerType>(Callee->getType()); 1087 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1088 Code = bitc::FUNC_CODE_INST_INVOKE; 1089 1090 Vals.push_back(VE.getAttributeID(II->getAttributes())); 1091 Vals.push_back(II->getCallingConv()); 1092 Vals.push_back(VE.getValueID(II->getNormalDest())); 1093 Vals.push_back(VE.getValueID(II->getUnwindDest())); 1094 PushValueAndType(Callee, InstID, Vals, VE); 1095 1096 // Emit value #'s for the fixed parameters. 1097 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1098 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param. 1099 1100 // Emit type/value pairs for varargs params. 1101 if (FTy->isVarArg()) { 1102 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 1103 i != e; ++i) 1104 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 1105 } 1106 break; 1107 } 1108 case Instruction::Unwind: 1109 Code = bitc::FUNC_CODE_INST_UNWIND; 1110 break; 1111 case Instruction::Unreachable: 1112 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 1113 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 1114 break; 1115 1116 case Instruction::PHI: { 1117 const PHINode &PN = cast<PHINode>(I); 1118 Code = bitc::FUNC_CODE_INST_PHI; 1119 Vals.push_back(VE.getTypeID(PN.getType())); 1120 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 1121 Vals.push_back(VE.getValueID(PN.getIncomingValue(i))); 1122 Vals.push_back(VE.getValueID(PN.getIncomingBlock(i))); 1123 } 1124 break; 1125 } 1126 1127 case Instruction::Alloca: 1128 Code = bitc::FUNC_CODE_INST_ALLOCA; 1129 Vals.push_back(VE.getTypeID(I.getType())); 1130 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1131 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 1132 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 1133 break; 1134 1135 case Instruction::Load: 1136 Code = bitc::FUNC_CODE_INST_LOAD; 1137 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 1138 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 1139 1140 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 1141 Vals.push_back(cast<LoadInst>(I).isVolatile()); 1142 break; 1143 case Instruction::Store: 1144 Code = bitc::FUNC_CODE_INST_STORE; 1145 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 1146 Vals.push_back(VE.getValueID(I.getOperand(0))); // val. 1147 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 1148 Vals.push_back(cast<StoreInst>(I).isVolatile()); 1149 break; 1150 case Instruction::Call: { 1151 const CallInst &CI = cast<CallInst>(I); 1152 const PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); 1153 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1154 1155 Code = bitc::FUNC_CODE_INST_CALL; 1156 1157 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 1158 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); 1159 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 1160 1161 // Emit value #'s for the fixed parameters. 1162 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1163 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param. 1164 1165 // Emit type/value pairs for varargs params. 1166 if (FTy->isVarArg()) { 1167 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 1168 i != e; ++i) 1169 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 1170 } 1171 break; 1172 } 1173 case Instruction::VAArg: 1174 Code = bitc::FUNC_CODE_INST_VAARG; 1175 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 1176 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 1177 Vals.push_back(VE.getTypeID(I.getType())); // restype. 1178 break; 1179 } 1180 1181 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1182 Vals.clear(); 1183} 1184 1185// Emit names for globals/functions etc. 1186static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1187 const ValueEnumerator &VE, 1188 BitstreamWriter &Stream) { 1189 if (VST.empty()) return; 1190 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 1191 1192 // FIXME: Set up the abbrev, we know how many values there are! 1193 // FIXME: We know if the type names can use 7-bit ascii. 1194 SmallVector<unsigned, 64> NameVals; 1195 1196 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1197 SI != SE; ++SI) { 1198 1199 const ValueName &Name = *SI; 1200 1201 // Figure out the encoding to use for the name. 1202 bool is7Bit = true; 1203 bool isChar6 = true; 1204 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 1205 C != E; ++C) { 1206 if (isChar6) 1207 isChar6 = BitCodeAbbrevOp::isChar6(*C); 1208 if ((unsigned char)*C & 128) { 1209 is7Bit = false; 1210 break; // don't bother scanning the rest. 1211 } 1212 } 1213 1214 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 1215 1216 // VST_ENTRY: [valueid, namechar x N] 1217 // VST_BBENTRY: [bbid, namechar x N] 1218 unsigned Code; 1219 if (isa<BasicBlock>(SI->getValue())) { 1220 Code = bitc::VST_CODE_BBENTRY; 1221 if (isChar6) 1222 AbbrevToUse = VST_BBENTRY_6_ABBREV; 1223 } else { 1224 Code = bitc::VST_CODE_ENTRY; 1225 if (isChar6) 1226 AbbrevToUse = VST_ENTRY_6_ABBREV; 1227 else if (is7Bit) 1228 AbbrevToUse = VST_ENTRY_7_ABBREV; 1229 } 1230 1231 NameVals.push_back(VE.getValueID(SI->getValue())); 1232 for (const char *P = Name.getKeyData(), 1233 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 1234 NameVals.push_back((unsigned char)*P); 1235 1236 // Emit the finished record. 1237 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1238 NameVals.clear(); 1239 } 1240 Stream.ExitBlock(); 1241} 1242 1243/// WriteFunction - Emit a function body to the module stream. 1244static void WriteFunction(const Function &F, ValueEnumerator &VE, 1245 BitstreamWriter &Stream) { 1246 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 1247 VE.incorporateFunction(F); 1248 1249 SmallVector<unsigned, 64> Vals; 1250 1251 // Emit the number of basic blocks, so the reader can create them ahead of 1252 // time. 1253 Vals.push_back(VE.getBasicBlocks().size()); 1254 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1255 Vals.clear(); 1256 1257 // If there are function-local constants, emit them now. 1258 unsigned CstStart, CstEnd; 1259 VE.getFunctionConstantRange(CstStart, CstEnd); 1260 WriteConstants(CstStart, CstEnd, VE, Stream, false); 1261 1262 // If there is function-local metadata, emit it now. 1263 WriteFunctionLocalMetadata(F, VE, Stream); 1264 1265 // Keep a running idea of what the instruction ID is. 1266 unsigned InstID = CstEnd; 1267 1268 bool NeedsMetadataAttachment = false; 1269 1270 DebugLoc LastDL; 1271 1272 // Finally, emit all the instructions, in order. 1273 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 1274 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 1275 I != E; ++I) { 1276 WriteInstruction(*I, InstID, VE, Stream, Vals); 1277 1278 if (!I->getType()->isVoidTy()) 1279 ++InstID; 1280 1281 // If the instruction has metadata, write a metadata attachment later. 1282 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 1283 1284 // If the instruction has a debug location, emit it. 1285 DebugLoc DL = I->getDebugLoc(); 1286 if (DL.isUnknown()) { 1287 // nothing todo. 1288 } else if (DL == LastDL) { 1289 // Just repeat the same debug loc as last time. 1290 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 1291 } else { 1292 MDNode *Scope, *IA; 1293 DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); 1294 1295 Vals.push_back(DL.getLine()); 1296 Vals.push_back(DL.getCol()); 1297 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); 1298 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); 1299 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); 1300 Vals.clear(); 1301 1302 LastDL = DL; 1303 } 1304 } 1305 1306 // Emit names for all the instructions etc. 1307 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 1308 1309 if (NeedsMetadataAttachment) 1310 WriteMetadataAttachment(F, VE, Stream); 1311 VE.purgeFunction(); 1312 Stream.ExitBlock(); 1313} 1314 1315// Emit blockinfo, which defines the standard abbreviations etc. 1316static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 1317 // We only want to emit block info records for blocks that have multiple 1318 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 1319 // blocks can defined their abbrevs inline. 1320 Stream.EnterBlockInfoBlock(2); 1321 1322 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1323 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1324 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 1325 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1326 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1327 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1328 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1329 Abbv) != VST_ENTRY_8_ABBREV) 1330 llvm_unreachable("Unexpected abbrev ordering!"); 1331 } 1332 1333 { // 7-bit fixed width VST_ENTRY strings. 1334 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1335 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1339 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1340 Abbv) != VST_ENTRY_7_ABBREV) 1341 llvm_unreachable("Unexpected abbrev ordering!"); 1342 } 1343 { // 6-bit char6 VST_ENTRY strings. 1344 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1345 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1346 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1347 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1348 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1349 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1350 Abbv) != VST_ENTRY_6_ABBREV) 1351 llvm_unreachable("Unexpected abbrev ordering!"); 1352 } 1353 { // 6-bit char6 VST_BBENTRY strings. 1354 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1355 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 1356 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1357 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1358 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1359 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1360 Abbv) != VST_BBENTRY_6_ABBREV) 1361 llvm_unreachable("Unexpected abbrev ordering!"); 1362 } 1363 1364 1365 1366 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1367 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1368 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 1369 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1370 Log2_32_Ceil(VE.getTypes().size()+1))); 1371 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1372 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1373 llvm_unreachable("Unexpected abbrev ordering!"); 1374 } 1375 1376 { // INTEGER abbrev for CONSTANTS_BLOCK. 1377 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1378 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 1379 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1380 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1381 Abbv) != CONSTANTS_INTEGER_ABBREV) 1382 llvm_unreachable("Unexpected abbrev ordering!"); 1383 } 1384 1385 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1386 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1387 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 1388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 1389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 1390 Log2_32_Ceil(VE.getTypes().size()+1))); 1391 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 1392 1393 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1394 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1395 llvm_unreachable("Unexpected abbrev ordering!"); 1396 } 1397 { // NULL abbrev for CONSTANTS_BLOCK. 1398 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1399 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1400 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1401 Abbv) != CONSTANTS_NULL_Abbrev) 1402 llvm_unreachable("Unexpected abbrev ordering!"); 1403 } 1404 1405 // FIXME: This should only use space for first class types! 1406 1407 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1408 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1409 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1410 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1411 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1413 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1414 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1415 llvm_unreachable("Unexpected abbrev ordering!"); 1416 } 1417 { // INST_BINOP abbrev for FUNCTION_BLOCK. 1418 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1419 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1420 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1421 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1422 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1423 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1424 Abbv) != FUNCTION_INST_BINOP_ABBREV) 1425 llvm_unreachable("Unexpected abbrev ordering!"); 1426 } 1427 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 1428 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1429 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1430 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1431 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 1434 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1435 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1436 llvm_unreachable("Unexpected abbrev ordering!"); 1437 } 1438 { // INST_CAST abbrev for FUNCTION_BLOCK. 1439 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1440 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 1441 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 1442 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 1443 Log2_32_Ceil(VE.getTypes().size()+1))); 1444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1445 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1446 Abbv) != FUNCTION_INST_CAST_ABBREV) 1447 llvm_unreachable("Unexpected abbrev ordering!"); 1448 } 1449 1450 { // INST_RET abbrev for FUNCTION_BLOCK. 1451 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1452 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1453 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1454 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1455 llvm_unreachable("Unexpected abbrev ordering!"); 1456 } 1457 { // INST_RET abbrev for FUNCTION_BLOCK. 1458 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1459 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1460 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 1461 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1462 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1463 llvm_unreachable("Unexpected abbrev ordering!"); 1464 } 1465 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1466 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1467 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 1468 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1469 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1470 llvm_unreachable("Unexpected abbrev ordering!"); 1471 } 1472 1473 Stream.ExitBlock(); 1474} 1475 1476 1477/// WriteModule - Emit the specified module to the bitstream. 1478static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1479 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1480 1481 // Emit the version number if it is non-zero. 1482 if (CurVersion) { 1483 SmallVector<unsigned, 1> Vals; 1484 Vals.push_back(CurVersion); 1485 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1486 } 1487 1488 // Analyze the module, enumerating globals, functions, etc. 1489 ValueEnumerator VE(M); 1490 1491 // Emit blockinfo, which defines the standard abbreviations etc. 1492 WriteBlockInfo(VE, Stream); 1493 1494 // Emit information about parameter attributes. 1495 WriteAttributeTable(VE, Stream); 1496 1497 // Emit information describing all of the types in the module. 1498 WriteTypeTable(VE, Stream); 1499 1500 // Emit top-level description of module, including target triple, inline asm, 1501 // descriptors for global variables, and function prototype info. 1502 WriteModuleInfo(M, VE, Stream); 1503 1504 // Emit constants. 1505 WriteModuleConstants(VE, Stream); 1506 1507 // Emit metadata. 1508 WriteModuleMetadata(M, VE, Stream); 1509 1510 // Emit function bodies. 1511 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 1512 if (!F->isDeclaration()) 1513 WriteFunction(*F, VE, Stream); 1514 1515 // Emit metadata. 1516 WriteModuleMetadataStore(M, Stream); 1517 1518 // Emit names for globals/functions etc. 1519 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1520 1521 Stream.ExitBlock(); 1522} 1523 1524/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1525/// header and trailer to make it compatible with the system archiver. To do 1526/// this we emit the following header, and then emit a trailer that pads the 1527/// file out to be a multiple of 16 bytes. 1528/// 1529/// struct bc_header { 1530/// uint32_t Magic; // 0x0B17C0DE 1531/// uint32_t Version; // Version, currently always 0. 1532/// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 1533/// uint32_t BitcodeSize; // Size of traditional bitcode file. 1534/// uint32_t CPUType; // CPU specifier. 1535/// ... potentially more later ... 1536/// }; 1537enum { 1538 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. 1539 DarwinBCHeaderSize = 5*4 1540}; 1541 1542static void EmitDarwinBCHeader(BitstreamWriter &Stream, const Triple &TT) { 1543 unsigned CPUType = ~0U; 1544 1545 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 1546 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 1547 // number from /usr/include/mach/machine.h. It is ok to reproduce the 1548 // specific constants here because they are implicitly part of the Darwin ABI. 1549 enum { 1550 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 1551 DARWIN_CPU_TYPE_X86 = 7, 1552 DARWIN_CPU_TYPE_ARM = 12, 1553 DARWIN_CPU_TYPE_POWERPC = 18 1554 }; 1555 1556 Triple::ArchType Arch = TT.getArch(); 1557 if (Arch == Triple::x86_64) 1558 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 1559 else if (Arch == Triple::x86) 1560 CPUType = DARWIN_CPU_TYPE_X86; 1561 else if (Arch == Triple::ppc) 1562 CPUType = DARWIN_CPU_TYPE_POWERPC; 1563 else if (Arch == Triple::ppc64) 1564 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 1565 else if (Arch == Triple::arm || Arch == Triple::thumb) 1566 CPUType = DARWIN_CPU_TYPE_ARM; 1567 1568 // Traditional Bitcode starts after header. 1569 unsigned BCOffset = DarwinBCHeaderSize; 1570 1571 Stream.Emit(0x0B17C0DE, 32); 1572 Stream.Emit(0 , 32); // Version. 1573 Stream.Emit(BCOffset , 32); 1574 Stream.Emit(0 , 32); // Filled in later. 1575 Stream.Emit(CPUType , 32); 1576} 1577 1578/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and 1579/// finalize the header. 1580static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) { 1581 // Update the size field in the header. 1582 Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize); 1583 1584 // If the file is not a multiple of 16 bytes, insert dummy padding. 1585 while (BufferSize & 15) { 1586 Stream.Emit(0, 8); 1587 ++BufferSize; 1588 } 1589} 1590 1591 1592/// WriteBitcodeToFile - Write the specified module to the specified output 1593/// stream. 1594void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { 1595 std::vector<unsigned char> Buffer; 1596 BitstreamWriter Stream(Buffer); 1597 1598 Buffer.reserve(256*1024); 1599 1600 WriteBitcodeToStream( M, Stream ); 1601 1602 // Write the generated bitstream to "Out". 1603 Out.write((char*)&Buffer.front(), Buffer.size()); 1604} 1605 1606/// WriteBitcodeToStream - Write the specified module to the specified output 1607/// stream. 1608void llvm::WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream) { 1609 // If this is darwin or another generic macho target, emit a file header and 1610 // trailer if needed. 1611 Triple TT(M->getTargetTriple()); 1612 if (TT.isOSDarwin()) 1613 EmitDarwinBCHeader(Stream, TT); 1614 1615 // Emit the file header. 1616 Stream.Emit((unsigned)'B', 8); 1617 Stream.Emit((unsigned)'C', 8); 1618 Stream.Emit(0x0, 4); 1619 Stream.Emit(0xC, 4); 1620 Stream.Emit(0xE, 4); 1621 Stream.Emit(0xD, 4); 1622 1623 // Emit the module. 1624 WriteModule(M, Stream); 1625 1626 if (TT.isOSDarwin()) 1627 EmitDarwinBCTrailer(Stream, Stream.getBuffer().size()); 1628} 1629