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