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::ExternalWeakLinkage: return 7; 398 case GlobalValue::CommonLinkage: return 8; 399 case GlobalValue::PrivateLinkage: return 9; 400 case GlobalValue::WeakODRLinkage: return 10; 401 case GlobalValue::LinkOnceODRLinkage: return 11; 402 case GlobalValue::AvailableExternallyLinkage: return 12; 403 } 404 llvm_unreachable("Invalid linkage"); 405} 406 407static unsigned getEncodedVisibility(const GlobalValue *GV) { 408 switch (GV->getVisibility()) { 409 default: llvm_unreachable("Invalid visibility!"); 410 case GlobalValue::DefaultVisibility: return 0; 411 case GlobalValue::HiddenVisibility: return 1; 412 case GlobalValue::ProtectedVisibility: return 2; 413 } 414} 415 416// Emit top-level description of module, including target triple, inline asm, 417// descriptors for global variables, and function prototype info. 418static void WriteModuleInfo(const Module *M, 419 const llvm_2_9::ValueEnumerator &VE, 420 BitstreamWriter &Stream) { 421 // Emit various pieces of data attached to a module. 422 if (!M->getTargetTriple().empty()) 423 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 424 0/*TODO*/, Stream); 425 if (M->getDataLayout() != nullptr) 426 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout()->getStringRepresentation(), 427 0/*TODO*/, Stream); 428 if (!M->getModuleInlineAsm().empty()) 429 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 430 0/*TODO*/, Stream); 431 432 // Emit information about sections and GC, computing how many there are. Also 433 // compute the maximum alignment value. 434 std::map<std::string, unsigned> SectionMap; 435 std::map<std::string, unsigned> GCMap; 436 unsigned MaxAlignment = 0; 437 unsigned MaxGlobalType = 0; 438 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 439 GV != E; ++GV) { 440 MaxAlignment = std::max(MaxAlignment, GV->getAlignment()); 441 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV->getType())); 442 443 if (!GV->hasSection()) continue; 444 // Give section names unique ID's. 445 unsigned &Entry = SectionMap[GV->getSection()]; 446 if (Entry != 0) continue; 447 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV->getSection(), 448 0/*TODO*/, Stream); 449 Entry = SectionMap.size(); 450 } 451 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 452 MaxAlignment = std::max(MaxAlignment, F->getAlignment()); 453 if (F->hasSection()) { 454 // Give section names unique ID's. 455 unsigned &Entry = SectionMap[F->getSection()]; 456 if (!Entry) { 457 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F->getSection(), 458 0/*TODO*/, Stream); 459 Entry = SectionMap.size(); 460 } 461 } 462 if (F->hasGC()) { 463 // Same for GC names. 464 unsigned &Entry = GCMap[F->getGC()]; 465 if (!Entry) { 466 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F->getGC(), 467 0/*TODO*/, Stream); 468 Entry = GCMap.size(); 469 } 470 } 471 } 472 473 // Emit abbrev for globals, now that we know # sections and max alignment. 474 unsigned SimpleGVarAbbrev = 0; 475 if (!M->global_empty()) { 476 // Add an abbrev for common globals with no visibility or thread localness. 477 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 478 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 479 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 480 Log2_32_Ceil(MaxGlobalType+1))); 481 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Constant. 482 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // Linkage. 484 if (MaxAlignment == 0) // Alignment. 485 Abbv->Add(BitCodeAbbrevOp(0)); 486 else { 487 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 488 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 489 Log2_32_Ceil(MaxEncAlignment+1))); 490 } 491 if (SectionMap.empty()) // Section. 492 Abbv->Add(BitCodeAbbrevOp(0)); 493 else 494 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 495 Log2_32_Ceil(SectionMap.size()+1))); 496 // Don't bother emitting vis + thread local. 497 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 498 } 499 500 // Emit the global variable information. 501 SmallVector<unsigned, 64> Vals; 502 for (Module::const_global_iterator GV = M->global_begin(),E = M->global_end(); 503 GV != E; ++GV) { 504 unsigned AbbrevToUse = 0; 505 506 // GLOBALVAR: [type, isconst, initid, 507 // linkage, alignment, section, visibility, threadlocal, 508 // unnamed_addr] 509 Vals.push_back(VE.getTypeID(GV->getType())); 510 Vals.push_back(GV->isConstant()); 511 Vals.push_back(GV->isDeclaration() ? 0 : 512 (VE.getValueID(GV->getInitializer()) + 1)); 513 Vals.push_back(getEncodedLinkage(GV)); 514 Vals.push_back(Log2_32(GV->getAlignment())+1); 515 Vals.push_back(GV->hasSection() ? SectionMap[GV->getSection()] : 0); 516 if (GV->isThreadLocal() || 517 GV->getVisibility() != GlobalValue::DefaultVisibility || 518 GV->hasUnnamedAddr()) { 519 Vals.push_back(getEncodedVisibility(GV)); 520 Vals.push_back(GV->isThreadLocal()); 521 Vals.push_back(GV->hasUnnamedAddr()); 522 } else { 523 AbbrevToUse = SimpleGVarAbbrev; 524 } 525 526 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 527 Vals.clear(); 528 } 529 530 // Emit the function proto information. 531 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) { 532 // FUNCTION: [type, callingconv, isproto, paramattr, 533 // linkage, alignment, section, visibility, gc, unnamed_addr] 534 Vals.push_back(VE.getTypeID(F->getType())); 535 Vals.push_back(F->getCallingConv()); 536 Vals.push_back(F->isDeclaration()); 537 Vals.push_back(getEncodedLinkage(F)); 538 Vals.push_back(VE.getAttributeID(F->getAttributes())); 539 Vals.push_back(Log2_32(F->getAlignment())+1); 540 Vals.push_back(F->hasSection() ? SectionMap[F->getSection()] : 0); 541 Vals.push_back(getEncodedVisibility(F)); 542 Vals.push_back(F->hasGC() ? GCMap[F->getGC()] : 0); 543 Vals.push_back(F->hasUnnamedAddr()); 544 545 unsigned AbbrevToUse = 0; 546 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 547 Vals.clear(); 548 } 549 550 // Emit the alias information. 551 for (Module::const_alias_iterator AI = M->alias_begin(), E = M->alias_end(); 552 AI != E; ++AI) { 553 Vals.push_back(VE.getTypeID(AI->getType())); 554 Vals.push_back(VE.getValueID(AI->getAliasee())); 555 Vals.push_back(getEncodedLinkage(AI)); 556 Vals.push_back(getEncodedVisibility(AI)); 557 unsigned AbbrevToUse = 0; 558 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 559 Vals.clear(); 560 } 561} 562 563static uint64_t GetOptimizationFlags(const Value *V) { 564 uint64_t Flags = 0; 565 566 if (const OverflowingBinaryOperator *OBO = 567 dyn_cast<OverflowingBinaryOperator>(V)) { 568 if (OBO->hasNoSignedWrap()) 569 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 570 if (OBO->hasNoUnsignedWrap()) 571 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 572 } else if (const PossiblyExactOperator *PEO = 573 dyn_cast<PossiblyExactOperator>(V)) { 574 if (PEO->isExact()) 575 Flags |= 1 << bitc::PEO_EXACT; 576 } 577 578 return Flags; 579} 580 581static void WriteMDNode(const MDNode *N, 582 const llvm_2_9::ValueEnumerator &VE, 583 BitstreamWriter &Stream, 584 SmallVector<uint64_t, 64> &Record) { 585 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 586 if (N->getOperand(i)) { 587 Record.push_back(VE.getTypeID(N->getOperand(i)->getType())); 588 Record.push_back(VE.getValueID(N->getOperand(i))); 589 } else { 590 Record.push_back(VE.getTypeID(Type::getVoidTy(N->getContext()))); 591 Record.push_back(0); 592 } 593 } 594 unsigned MDCode = N->isFunctionLocal() ? METADATA_FN_NODE_2_7 : 595 METADATA_NODE_2_7; 596 Stream.EmitRecord(MDCode, Record, 0); 597 Record.clear(); 598} 599 600static void WriteModuleMetadata(const Module *M, 601 const llvm_2_9::ValueEnumerator &VE, 602 BitstreamWriter &Stream) { 603 const llvm_2_9::ValueEnumerator::ValueList &Vals = VE.getMDValues(); 604 bool StartedMetadataBlock = false; 605 unsigned MDSAbbrev = 0; 606 SmallVector<uint64_t, 64> Record; 607 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 608 609 if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { 610 if (!N->isFunctionLocal() || !N->getFunction()) { 611 if (!StartedMetadataBlock) { 612 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 613 StartedMetadataBlock = true; 614 } 615 WriteMDNode(N, VE, Stream, Record); 616 } 617 } else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { 618 if (!StartedMetadataBlock) { 619 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 620 621 // Abbrev for METADATA_STRING. 622 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 623 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 624 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 625 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 626 MDSAbbrev = Stream.EmitAbbrev(Abbv); 627 StartedMetadataBlock = true; 628 } 629 630 // Code: [strchar x N] 631 Record.append(MDS->begin(), MDS->end()); 632 633 // Emit the finished record. 634 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 635 Record.clear(); 636 } 637 } 638 639 // Write named metadata. 640 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), 641 E = M->named_metadata_end(); I != E; ++I) { 642 const NamedMDNode *NMD = I; 643 if (!StartedMetadataBlock) { 644 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 645 StartedMetadataBlock = true; 646 } 647 648 // Write name. 649 StringRef Str = NMD->getName(); 650 for (unsigned i = 0, e = Str.size(); i != e; ++i) 651 Record.push_back(Str[i]); 652 Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); 653 Record.clear(); 654 655 // Write named metadata operands. 656 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) 657 Record.push_back(VE.getValueID(NMD->getOperand(i))); 658 Stream.EmitRecord(METADATA_NAMED_NODE_2_7, Record, 0); 659 Record.clear(); 660 } 661 662 if (StartedMetadataBlock) 663 Stream.ExitBlock(); 664} 665 666static void WriteFunctionLocalMetadata(const Function &F, 667 const llvm_2_9::ValueEnumerator &VE, 668 BitstreamWriter &Stream) { 669 bool StartedMetadataBlock = false; 670 SmallVector<uint64_t, 64> Record; 671 const SmallVector<const MDNode *, 8> &Vals = VE.getFunctionLocalMDValues(); 672 for (unsigned i = 0, e = Vals.size(); i != e; ++i) 673 if (const MDNode *N = Vals[i]) 674 if (N->isFunctionLocal() && N->getFunction() == &F) { 675 if (!StartedMetadataBlock) { 676 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 677 StartedMetadataBlock = true; 678 } 679 WriteMDNode(N, VE, Stream, Record); 680 } 681 682 if (StartedMetadataBlock) 683 Stream.ExitBlock(); 684} 685 686static void WriteMetadataAttachment(const Function &F, 687 const llvm_2_9::ValueEnumerator &VE, 688 BitstreamWriter &Stream) { 689 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 690 691 SmallVector<uint64_t, 64> Record; 692 693 // Write metadata attachments 694 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 695 SmallVector<std::pair<unsigned, MDNode*>, 4> MDs; 696 697 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 698 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 699 I != E; ++I) { 700 MDs.clear(); 701 I->getAllMetadataOtherThanDebugLoc(MDs); 702 703 // If no metadata, ignore instruction. 704 if (MDs.empty()) continue; 705 706 Record.push_back(VE.getInstructionID(I)); 707 708 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 709 Record.push_back(MDs[i].first); 710 Record.push_back(VE.getValueID(MDs[i].second)); 711 } 712 Stream.EmitRecord(METADATA_ATTACHMENT_2_7, Record, 0); 713 Record.clear(); 714 } 715 716 Stream.ExitBlock(); 717} 718 719static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { 720 SmallVector<uint64_t, 64> Record; 721 722 // Write metadata kinds 723 // METADATA_KIND - [n x [id, name]] 724 SmallVector<StringRef, 4> Names; 725 M->getMDKindNames(Names); 726 727 if (Names.empty()) return; 728 729 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 730 731 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 732 Record.push_back(MDKindID); 733 StringRef KName = Names[MDKindID]; 734 Record.append(KName.begin(), KName.end()); 735 736 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 737 Record.clear(); 738 } 739 740 Stream.ExitBlock(); 741} 742 743static void WriteConstants(unsigned FirstVal, unsigned LastVal, 744 const llvm_2_9::ValueEnumerator &VE, 745 BitstreamWriter &Stream, bool isGlobal) { 746 if (FirstVal == LastVal) return; 747 748 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 749 750 unsigned AggregateAbbrev = 0; 751 unsigned String8Abbrev = 0; 752 unsigned CString7Abbrev = 0; 753 unsigned CString6Abbrev = 0; 754 // If this is a constant pool for the module, emit module-specific abbrevs. 755 if (isGlobal) { 756 // Abbrev for CST_CODE_AGGREGATE. 757 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 758 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 759 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 760 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 761 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 762 763 // Abbrev for CST_CODE_STRING. 764 Abbv = new BitCodeAbbrev(); 765 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 766 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 767 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 768 String8Abbrev = Stream.EmitAbbrev(Abbv); 769 // Abbrev for CST_CODE_CSTRING. 770 Abbv = new BitCodeAbbrev(); 771 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 772 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 773 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 774 CString7Abbrev = Stream.EmitAbbrev(Abbv); 775 // Abbrev for CST_CODE_CSTRING. 776 Abbv = new BitCodeAbbrev(); 777 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 778 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 779 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 780 CString6Abbrev = Stream.EmitAbbrev(Abbv); 781 } 782 783 SmallVector<uint64_t, 64> Record; 784 785 const llvm_2_9::ValueEnumerator::ValueList &Vals = VE.getValues(); 786 Type *LastTy = 0; 787 for (unsigned i = FirstVal; i != LastVal; ++i) { 788 const Value *V = Vals[i].first; 789 // If we need to switch types, do so now. 790 if (V->getType() != LastTy) { 791 LastTy = V->getType(); 792 Record.push_back(VE.getTypeID(LastTy)); 793 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 794 CONSTANTS_SETTYPE_ABBREV); 795 Record.clear(); 796 } 797 798 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 799 Record.push_back(unsigned(IA->hasSideEffects()) | 800 unsigned(IA->isAlignStack()) << 1); 801 802 // Add the asm string. 803 const std::string &AsmStr = IA->getAsmString(); 804 Record.push_back(AsmStr.size()); 805 for (unsigned i = 0, e = AsmStr.size(); i != e; ++i) 806 Record.push_back(AsmStr[i]); 807 808 // Add the constraint string. 809 const std::string &ConstraintStr = IA->getConstraintString(); 810 Record.push_back(ConstraintStr.size()); 811 for (unsigned i = 0, e = ConstraintStr.size(); i != e; ++i) 812 Record.push_back(ConstraintStr[i]); 813 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 814 Record.clear(); 815 continue; 816 } 817 const Constant *C = cast<Constant>(V); 818 unsigned Code = -1U; 819 unsigned AbbrevToUse = 0; 820 if (C->isNullValue()) { 821 Code = bitc::CST_CODE_NULL; 822 } else if (isa<UndefValue>(C)) { 823 Code = bitc::CST_CODE_UNDEF; 824 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 825 if (IV->getBitWidth() <= 64) { 826 uint64_t V = IV->getSExtValue(); 827 if ((int64_t)V >= 0) 828 Record.push_back(V << 1); 829 else 830 Record.push_back((-V << 1) | 1); 831 Code = bitc::CST_CODE_INTEGER; 832 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 833 } else { // Wide integers, > 64 bits in size. 834 // We have an arbitrary precision integer value to write whose 835 // bit width is > 64. However, in canonical unsigned integer 836 // format it is likely that the high bits are going to be zero. 837 // So, we only write the number of active words. 838 unsigned NWords = IV->getValue().getActiveWords(); 839 const uint64_t *RawWords = IV->getValue().getRawData(); 840 for (unsigned i = 0; i != NWords; ++i) { 841 int64_t V = RawWords[i]; 842 if (V >= 0) 843 Record.push_back(V << 1); 844 else 845 Record.push_back((-V << 1) | 1); 846 } 847 Code = bitc::CST_CODE_WIDE_INTEGER; 848 } 849 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 850 Code = bitc::CST_CODE_FLOAT; 851 Type *Ty = CFP->getType(); 852 if (Ty->isFloatTy() || Ty->isDoubleTy()) { 853 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 854 } else if (Ty->isX86_FP80Ty()) { 855 // api needed to prevent premature destruction 856 // bits are not in the same order as a normal i80 APInt, compensate. 857 APInt api = CFP->getValueAPF().bitcastToAPInt(); 858 const uint64_t *p = api.getRawData(); 859 Record.push_back((p[1] << 48) | (p[0] >> 16)); 860 Record.push_back(p[0] & 0xffffLL); 861 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 862 APInt api = CFP->getValueAPF().bitcastToAPInt(); 863 const uint64_t *p = api.getRawData(); 864 Record.push_back(p[0]); 865 Record.push_back(p[1]); 866 } else { 867 assert (0 && "Unknown FP type!"); 868 } 869 } else if (isa<ConstantDataSequential>(C) && 870 cast<ConstantDataSequential>(C)->isString()) { 871 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 872 // Emit constant strings specially. 873 unsigned NumElts = Str->getNumElements(); 874 // If this is a null-terminated string, use the denser CSTRING encoding. 875 if (Str->isCString()) { 876 Code = bitc::CST_CODE_CSTRING; 877 --NumElts; // Don't encode the null, which isn't allowed by char6. 878 } else { 879 Code = bitc::CST_CODE_STRING; 880 AbbrevToUse = String8Abbrev; 881 } 882 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 883 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 884 for (unsigned i = 0; i != NumElts; ++i) { 885 unsigned char V = Str->getElementAsInteger(i); 886 Record.push_back(V); 887 isCStr7 &= (V & 128) == 0; 888 if (isCStrChar6) 889 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 890 } 891 892 if (isCStrChar6) 893 AbbrevToUse = CString6Abbrev; 894 else if (isCStr7) 895 AbbrevToUse = CString7Abbrev; 896 } else if (const ConstantDataSequential *CDS = 897 dyn_cast<ConstantDataSequential>(C)) { 898 // We must replace ConstantDataSequential's representation with the 899 // legacy ConstantArray/ConstantVector/ConstantStruct version. 900 // ValueEnumerator is similarly modified to mark the appropriate 901 // Constants as used (so they are emitted). 902 Code = bitc::CST_CODE_AGGREGATE; 903 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 904 Record.push_back(VE.getValueID(CDS->getElementAsConstant(i))); 905 AbbrevToUse = AggregateAbbrev; 906 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 907 isa<ConstantVector>(C)) { 908 Code = bitc::CST_CODE_AGGREGATE; 909 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) 910 Record.push_back(VE.getValueID(C->getOperand(i))); 911 AbbrevToUse = AggregateAbbrev; 912 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 913 switch (CE->getOpcode()) { 914 default: 915 if (Instruction::isCast(CE->getOpcode())) { 916 Code = bitc::CST_CODE_CE_CAST; 917 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 918 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 919 Record.push_back(VE.getValueID(C->getOperand(0))); 920 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 921 } else { 922 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 923 Code = bitc::CST_CODE_CE_BINOP; 924 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 925 Record.push_back(VE.getValueID(C->getOperand(0))); 926 Record.push_back(VE.getValueID(C->getOperand(1))); 927 uint64_t Flags = GetOptimizationFlags(CE); 928 if (Flags != 0) 929 Record.push_back(Flags); 930 } 931 break; 932 case Instruction::GetElementPtr: 933 Code = bitc::CST_CODE_CE_GEP; 934 if (cast<GEPOperator>(C)->isInBounds()) 935 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 936 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 937 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 938 Record.push_back(VE.getValueID(C->getOperand(i))); 939 } 940 break; 941 case Instruction::Select: 942 Code = bitc::CST_CODE_CE_SELECT; 943 Record.push_back(VE.getValueID(C->getOperand(0))); 944 Record.push_back(VE.getValueID(C->getOperand(1))); 945 Record.push_back(VE.getValueID(C->getOperand(2))); 946 break; 947 case Instruction::ExtractElement: 948 Code = bitc::CST_CODE_CE_EXTRACTELT; 949 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 950 Record.push_back(VE.getValueID(C->getOperand(0))); 951 Record.push_back(VE.getValueID(C->getOperand(1))); 952 break; 953 case Instruction::InsertElement: 954 Code = bitc::CST_CODE_CE_INSERTELT; 955 Record.push_back(VE.getValueID(C->getOperand(0))); 956 Record.push_back(VE.getValueID(C->getOperand(1))); 957 Record.push_back(VE.getValueID(C->getOperand(2))); 958 break; 959 case Instruction::ShuffleVector: 960 // If the return type and argument types are the same, this is a 961 // standard shufflevector instruction. If the types are different, 962 // then the shuffle is widening or truncating the input vectors, and 963 // the argument type must also be encoded. 964 if (C->getType() == C->getOperand(0)->getType()) { 965 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 966 } else { 967 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 968 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 969 } 970 Record.push_back(VE.getValueID(C->getOperand(0))); 971 Record.push_back(VE.getValueID(C->getOperand(1))); 972 Record.push_back(VE.getValueID(C->getOperand(2))); 973 break; 974 case Instruction::ICmp: 975 case Instruction::FCmp: 976 Code = bitc::CST_CODE_CE_CMP; 977 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 978 Record.push_back(VE.getValueID(C->getOperand(0))); 979 Record.push_back(VE.getValueID(C->getOperand(1))); 980 Record.push_back(CE->getPredicate()); 981 break; 982 } 983 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 984 Code = bitc::CST_CODE_BLOCKADDRESS; 985 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 986 Record.push_back(VE.getValueID(BA->getFunction())); 987 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 988 } else { 989#ifndef NDEBUG 990 C->dump(); 991#endif 992 llvm_unreachable("Unknown constant!"); 993 } 994 Stream.EmitRecord(Code, Record, AbbrevToUse); 995 Record.clear(); 996 } 997 998 Stream.ExitBlock(); 999} 1000 1001static void WriteModuleConstants(const llvm_2_9::ValueEnumerator &VE, 1002 BitstreamWriter &Stream) { 1003 const llvm_2_9::ValueEnumerator::ValueList &Vals = VE.getValues(); 1004 1005 // Find the first constant to emit, which is the first non-globalvalue value. 1006 // We know globalvalues have been emitted by WriteModuleInfo. 1007 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1008 if (!isa<GlobalValue>(Vals[i].first)) { 1009 WriteConstants(i, Vals.size(), VE, Stream, true); 1010 return; 1011 } 1012 } 1013} 1014 1015/// PushValueAndType - The file has to encode both the value and type id for 1016/// many values, because we need to know what type to create for forward 1017/// references. However, most operands are not forward references, so this type 1018/// field is not needed. 1019/// 1020/// This function adds V's value ID to Vals. If the value ID is higher than the 1021/// instruction ID, then it is a forward reference, and it also includes the 1022/// type ID. 1023static bool PushValueAndType(const Value *V, unsigned InstID, 1024 SmallVector<unsigned, 64> &Vals, 1025 llvm_2_9::ValueEnumerator &VE) { 1026 unsigned ValID = VE.getValueID(V); 1027 Vals.push_back(ValID); 1028 if (ValID >= InstID) { 1029 Vals.push_back(VE.getTypeID(V->getType())); 1030 return true; 1031 } 1032 return false; 1033} 1034 1035/// WriteInstruction - Emit an instruction to the specified stream. 1036static void WriteInstruction(const Instruction &I, unsigned InstID, 1037 llvm_2_9::ValueEnumerator &VE, 1038 BitstreamWriter &Stream, 1039 SmallVector<unsigned, 64> &Vals) { 1040 unsigned Code = 0; 1041 unsigned AbbrevToUse = 0; 1042 VE.setInstructionID(&I); 1043 switch (I.getOpcode()) { 1044 default: 1045 if (Instruction::isCast(I.getOpcode())) { 1046 Code = bitc::FUNC_CODE_INST_CAST; 1047 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1048 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 1049 Vals.push_back(VE.getTypeID(I.getType())); 1050 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 1051 } else { 1052 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 1053 Code = bitc::FUNC_CODE_INST_BINOP; 1054 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1055 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 1056 Vals.push_back(VE.getValueID(I.getOperand(1))); 1057 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 1058 uint64_t Flags = GetOptimizationFlags(&I); 1059 if (Flags != 0) { 1060 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 1061 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 1062 Vals.push_back(Flags); 1063 } 1064 } 1065 break; 1066 1067 case Instruction::GetElementPtr: 1068 Code = bitc::FUNC_CODE_INST_GEP; 1069 if (cast<GEPOperator>(&I)->isInBounds()) 1070 Code = bitc::FUNC_CODE_INST_INBOUNDS_GEP; 1071 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1072 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1073 break; 1074 case Instruction::ExtractValue: { 1075 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 1076 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1077 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 1078 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i) 1079 Vals.push_back(*i); 1080 break; 1081 } 1082 case Instruction::InsertValue: { 1083 Code = bitc::FUNC_CODE_INST_INSERTVAL; 1084 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1085 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1086 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 1087 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i) 1088 Vals.push_back(*i); 1089 break; 1090 } 1091 case Instruction::Select: 1092 Code = bitc::FUNC_CODE_INST_VSELECT; 1093 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1094 Vals.push_back(VE.getValueID(I.getOperand(2))); 1095 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1096 break; 1097 case Instruction::ExtractElement: 1098 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 1099 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1100 Vals.push_back(VE.getValueID(I.getOperand(1))); 1101 break; 1102 case Instruction::InsertElement: 1103 Code = bitc::FUNC_CODE_INST_INSERTELT; 1104 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1105 Vals.push_back(VE.getValueID(I.getOperand(1))); 1106 Vals.push_back(VE.getValueID(I.getOperand(2))); 1107 break; 1108 case Instruction::ShuffleVector: 1109 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 1110 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1111 Vals.push_back(VE.getValueID(I.getOperand(1))); 1112 Vals.push_back(VE.getValueID(I.getOperand(2))); 1113 break; 1114 case Instruction::ICmp: 1115 case Instruction::FCmp: 1116 // compare returning Int1Ty or vector of Int1Ty 1117 Code = bitc::FUNC_CODE_INST_CMP2; 1118 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1119 Vals.push_back(VE.getValueID(I.getOperand(1))); 1120 Vals.push_back(cast<CmpInst>(I).getPredicate()); 1121 break; 1122 1123 case Instruction::Ret: 1124 { 1125 Code = bitc::FUNC_CODE_INST_RET; 1126 unsigned NumOperands = I.getNumOperands(); 1127 if (NumOperands == 0) 1128 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 1129 else if (NumOperands == 1) { 1130 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1131 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 1132 } else { 1133 for (unsigned i = 0, e = NumOperands; i != e; ++i) 1134 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1135 } 1136 } 1137 break; 1138 case Instruction::Br: 1139 { 1140 Code = bitc::FUNC_CODE_INST_BR; 1141 const BranchInst &II = cast<BranchInst>(I); 1142 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 1143 if (II.isConditional()) { 1144 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 1145 Vals.push_back(VE.getValueID(II.getCondition())); 1146 } 1147 } 1148 break; 1149 case Instruction::Switch: 1150 { 1151 Code = bitc::FUNC_CODE_INST_SWITCH; 1152 const SwitchInst &SI = cast<SwitchInst>(I); 1153 1154 Vals.push_back(VE.getTypeID(SI.getCondition()->getType())); 1155 Vals.push_back(VE.getValueID(SI.getCondition())); 1156 Vals.push_back(VE.getValueID(SI.getDefaultDest())); 1157 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end(); 1158 i != e; ++i) { 1159 Vals.push_back(VE.getValueID(i.getCaseValue())); 1160 Vals.push_back(VE.getValueID(i.getCaseSuccessor())); 1161 } 1162 } 1163 break; 1164 case Instruction::IndirectBr: 1165 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 1166 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1167 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1168 Vals.push_back(VE.getValueID(I.getOperand(i))); 1169 break; 1170 1171 case Instruction::Invoke: { 1172 const InvokeInst *II = cast<InvokeInst>(&I); 1173 const Value *Callee(II->getCalledValue()); 1174 PointerType *PTy = cast<PointerType>(Callee->getType()); 1175 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1176 Code = bitc::FUNC_CODE_INST_INVOKE; 1177 1178 Vals.push_back(VE.getAttributeID(II->getAttributes())); 1179 Vals.push_back(II->getCallingConv()); 1180 Vals.push_back(VE.getValueID(II->getNormalDest())); 1181 Vals.push_back(VE.getValueID(II->getUnwindDest())); 1182 PushValueAndType(Callee, InstID, Vals, VE); 1183 1184 // Emit value #'s for the fixed parameters. 1185 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1186 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param. 1187 1188 // Emit type/value pairs for varargs params. 1189 if (FTy->isVarArg()) { 1190 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 1191 i != e; ++i) 1192 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 1193 } 1194 break; 1195 } 1196 case Instruction::Unreachable: 1197 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 1198 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 1199 break; 1200 1201 case Instruction::PHI: { 1202 const PHINode &PN = cast<PHINode>(I); 1203 Code = bitc::FUNC_CODE_INST_PHI; 1204 Vals.push_back(VE.getTypeID(PN.getType())); 1205 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 1206 Vals.push_back(VE.getValueID(PN.getIncomingValue(i))); 1207 Vals.push_back(VE.getValueID(PN.getIncomingBlock(i))); 1208 } 1209 break; 1210 } 1211 1212 case Instruction::Alloca: 1213 Code = bitc::FUNC_CODE_INST_ALLOCA; 1214 Vals.push_back(VE.getTypeID(I.getType())); 1215 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1216 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 1217 Vals.push_back(Log2_32(cast<AllocaInst>(I).getAlignment())+1); 1218 break; 1219 1220 case Instruction::Load: 1221 Code = bitc::FUNC_CODE_INST_LOAD; 1222 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 1223 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 1224 1225 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 1226 Vals.push_back(cast<LoadInst>(I).isVolatile()); 1227 break; 1228 case Instruction::Store: 1229 Code = bitc::FUNC_CODE_INST_STORE; 1230 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 1231 Vals.push_back(VE.getValueID(I.getOperand(0))); // val. 1232 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 1233 Vals.push_back(cast<StoreInst>(I).isVolatile()); 1234 break; 1235 case Instruction::Call: { 1236 const CallInst &CI = cast<CallInst>(I); 1237 PointerType *PTy = cast<PointerType>(CI.getCalledValue()->getType()); 1238 FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1239 1240 Code = FUNC_CODE_INST_CALL_2_7; 1241 1242 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 1243 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall())); 1244 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 1245 1246 // Emit value #'s for the fixed parameters. 1247 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 1248 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); // fixed param. 1249 1250 // Emit type/value pairs for varargs params. 1251 if (FTy->isVarArg()) { 1252 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 1253 i != e; ++i) 1254 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 1255 } 1256 break; 1257 } 1258 case Instruction::VAArg: 1259 Code = bitc::FUNC_CODE_INST_VAARG; 1260 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 1261 Vals.push_back(VE.getValueID(I.getOperand(0))); // valist. 1262 Vals.push_back(VE.getTypeID(I.getType())); // restype. 1263 break; 1264 } 1265 1266 Stream.EmitRecord(Code, Vals, AbbrevToUse); 1267 Vals.clear(); 1268} 1269 1270// Emit names for globals/functions etc. 1271static void WriteValueSymbolTable(const ValueSymbolTable &VST, 1272 const llvm_2_9::ValueEnumerator &VE, 1273 BitstreamWriter &Stream) { 1274 if (VST.empty()) return; 1275 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 1276 1277 // FIXME: Set up the abbrev, we know how many values there are! 1278 // FIXME: We know if the type names can use 7-bit ascii. 1279 SmallVector<unsigned, 64> NameVals; 1280 1281 for (ValueSymbolTable::const_iterator SI = VST.begin(), SE = VST.end(); 1282 SI != SE; ++SI) { 1283 1284 const ValueName &Name = *SI; 1285 1286 // Figure out the encoding to use for the name. 1287 bool is7Bit = true; 1288 bool isChar6 = true; 1289 for (const char *C = Name.getKeyData(), *E = C+Name.getKeyLength(); 1290 C != E; ++C) { 1291 if (isChar6) 1292 isChar6 = BitCodeAbbrevOp::isChar6(*C); 1293 if ((unsigned char)*C & 128) { 1294 is7Bit = false; 1295 break; // don't bother scanning the rest. 1296 } 1297 } 1298 1299 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 1300 1301 // VST_ENTRY: [valueid, namechar x N] 1302 // VST_BBENTRY: [bbid, namechar x N] 1303 unsigned Code; 1304 if (isa<BasicBlock>(SI->getValue())) { 1305 Code = bitc::VST_CODE_BBENTRY; 1306 if (isChar6) 1307 AbbrevToUse = VST_BBENTRY_6_ABBREV; 1308 } else { 1309 Code = bitc::VST_CODE_ENTRY; 1310 if (isChar6) 1311 AbbrevToUse = VST_ENTRY_6_ABBREV; 1312 else if (is7Bit) 1313 AbbrevToUse = VST_ENTRY_7_ABBREV; 1314 } 1315 1316 NameVals.push_back(VE.getValueID(SI->getValue())); 1317 for (const char *P = Name.getKeyData(), 1318 *E = Name.getKeyData()+Name.getKeyLength(); P != E; ++P) 1319 NameVals.push_back((unsigned char)*P); 1320 1321 // Emit the finished record. 1322 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 1323 NameVals.clear(); 1324 } 1325 Stream.ExitBlock(); 1326} 1327 1328/// WriteFunction - Emit a function body to the module stream. 1329static void WriteFunction(const Function &F, llvm_2_9::ValueEnumerator &VE, 1330 BitstreamWriter &Stream) { 1331 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 1332 VE.incorporateFunction(F); 1333 1334 SmallVector<unsigned, 64> Vals; 1335 1336 // Emit the number of basic blocks, so the reader can create them ahead of 1337 // time. 1338 Vals.push_back(VE.getBasicBlocks().size()); 1339 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 1340 Vals.clear(); 1341 1342 // If there are function-local constants, emit them now. 1343 unsigned CstStart, CstEnd; 1344 VE.getFunctionConstantRange(CstStart, CstEnd); 1345 WriteConstants(CstStart, CstEnd, VE, Stream, false); 1346 1347 // If there is function-local metadata, emit it now. 1348 WriteFunctionLocalMetadata(F, VE, Stream); 1349 1350 // Keep a running idea of what the instruction ID is. 1351 unsigned InstID = CstEnd; 1352 1353 bool NeedsMetadataAttachment = false; 1354 1355 DebugLoc LastDL; 1356 1357 // Finally, emit all the instructions, in order. 1358 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 1359 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 1360 I != E; ++I) { 1361 WriteInstruction(*I, InstID, VE, Stream, Vals); 1362 1363 if (!I->getType()->isVoidTy()) 1364 ++InstID; 1365 1366 // If the instruction has metadata, write a metadata attachment later. 1367 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 1368 1369 // If the instruction has a debug location, emit it. 1370 DebugLoc DL = I->getDebugLoc(); 1371 if (DL.isUnknown()) { 1372 // nothing todo. 1373 } else if (DL == LastDL) { 1374 // Just repeat the same debug loc as last time. 1375 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 1376 } else { 1377 MDNode *Scope, *IA; 1378 DL.getScopeAndInlinedAt(Scope, IA, I->getContext()); 1379 1380 Vals.push_back(DL.getLine()); 1381 Vals.push_back(DL.getCol()); 1382 Vals.push_back(Scope ? VE.getValueID(Scope)+1 : 0); 1383 Vals.push_back(IA ? VE.getValueID(IA)+1 : 0); 1384 Stream.EmitRecord(FUNC_CODE_DEBUG_LOC_2_7, Vals); 1385 Vals.clear(); 1386 1387 LastDL = DL; 1388 } 1389 } 1390 1391 // Emit names for all the instructions etc. 1392 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 1393 1394 if (NeedsMetadataAttachment) 1395 WriteMetadataAttachment(F, VE, Stream); 1396 VE.purgeFunction(); 1397 Stream.ExitBlock(); 1398} 1399 1400// Emit blockinfo, which defines the standard abbreviations etc. 1401static void WriteBlockInfo(const llvm_2_9::ValueEnumerator &VE, 1402 BitstreamWriter &Stream) { 1403 // We only want to emit block info records for blocks that have multiple 1404 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. Other 1405 // blocks can defined their abbrevs inline. 1406 Stream.EnterBlockInfoBlock(2); 1407 1408 { // 8-bit fixed-width VST_ENTRY/VST_BBENTRY strings. 1409 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1410 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 1411 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1412 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1413 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1414 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1415 Abbv) != VST_ENTRY_8_ABBREV) 1416 llvm_unreachable("Unexpected abbrev ordering!"); 1417 } 1418 1419 { // 7-bit fixed width VST_ENTRY strings. 1420 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1421 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1422 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1423 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1424 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1425 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1426 Abbv) != VST_ENTRY_7_ABBREV) 1427 llvm_unreachable("Unexpected abbrev ordering!"); 1428 } 1429 { // 6-bit char6 VST_ENTRY strings. 1430 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1431 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 1432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1434 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1435 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1436 Abbv) != VST_ENTRY_6_ABBREV) 1437 llvm_unreachable("Unexpected abbrev ordering!"); 1438 } 1439 { // 6-bit char6 VST_BBENTRY strings. 1440 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1441 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 1442 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1443 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1444 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1445 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 1446 Abbv) != VST_BBENTRY_6_ABBREV) 1447 llvm_unreachable("Unexpected abbrev ordering!"); 1448 } 1449 1450 1451 1452 { // SETTYPE abbrev for CONSTANTS_BLOCK. 1453 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1454 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 1455 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1456 Log2_32_Ceil(VE.getTypes().size()+1))); 1457 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1458 Abbv) != CONSTANTS_SETTYPE_ABBREV) 1459 llvm_unreachable("Unexpected abbrev ordering!"); 1460 } 1461 1462 { // INTEGER abbrev for CONSTANTS_BLOCK. 1463 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1464 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 1465 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1466 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1467 Abbv) != CONSTANTS_INTEGER_ABBREV) 1468 llvm_unreachable("Unexpected abbrev ordering!"); 1469 } 1470 1471 { // CE_CAST abbrev for CONSTANTS_BLOCK. 1472 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1473 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 1474 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 1475 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 1476 Log2_32_Ceil(VE.getTypes().size()+1))); 1477 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 1478 1479 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1480 Abbv) != CONSTANTS_CE_CAST_Abbrev) 1481 llvm_unreachable("Unexpected abbrev ordering!"); 1482 } 1483 { // NULL abbrev for CONSTANTS_BLOCK. 1484 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1485 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 1486 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 1487 Abbv) != CONSTANTS_NULL_Abbrev) 1488 llvm_unreachable("Unexpected abbrev ordering!"); 1489 } 1490 1491 // FIXME: This should only use space for first class types! 1492 1493 { // INST_LOAD abbrev for FUNCTION_BLOCK. 1494 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1495 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 1496 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 1497 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 1498 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 1499 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1500 Abbv) != FUNCTION_INST_LOAD_ABBREV) 1501 llvm_unreachable("Unexpected abbrev ordering!"); 1502 } 1503 { // INST_BINOP abbrev for FUNCTION_BLOCK. 1504 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1505 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1506 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1507 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1508 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1509 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1510 Abbv) != FUNCTION_INST_BINOP_ABBREV) 1511 llvm_unreachable("Unexpected abbrev ordering!"); 1512 } 1513 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 1514 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1515 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 1516 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 1517 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 1518 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1519 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 1520 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1521 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 1522 llvm_unreachable("Unexpected abbrev ordering!"); 1523 } 1524 { // INST_CAST abbrev for FUNCTION_BLOCK. 1525 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1526 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 1527 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 1528 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 1529 Log2_32_Ceil(VE.getTypes().size()+1))); 1530 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 1531 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1532 Abbv) != FUNCTION_INST_CAST_ABBREV) 1533 llvm_unreachable("Unexpected abbrev ordering!"); 1534 } 1535 1536 { // INST_RET abbrev for FUNCTION_BLOCK. 1537 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1538 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1539 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1540 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 1541 llvm_unreachable("Unexpected abbrev ordering!"); 1542 } 1543 { // INST_RET abbrev for FUNCTION_BLOCK. 1544 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1545 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 1546 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 1547 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1548 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 1549 llvm_unreachable("Unexpected abbrev ordering!"); 1550 } 1551 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 1552 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1553 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 1554 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 1555 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 1556 llvm_unreachable("Unexpected abbrev ordering!"); 1557 } 1558 1559 Stream.ExitBlock(); 1560} 1561 1562 1563/// WriteModule - Emit the specified module to the bitstream. 1564static void WriteModule(const Module *M, BitstreamWriter &Stream) { 1565 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 1566 1567 // Emit the version number if it is non-zero. 1568 if (CurVersion) { 1569 SmallVector<unsigned, 1> Vals; 1570 Vals.push_back(CurVersion); 1571 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 1572 } 1573 1574 // Analyze the module, enumerating globals, functions, etc. 1575 llvm_2_9::ValueEnumerator VE(M); 1576 1577 // Emit blockinfo, which defines the standard abbreviations etc. 1578 WriteBlockInfo(VE, Stream); 1579 1580 // Emit information about parameter attributes. 1581 WriteAttributeTable(VE, Stream); 1582 1583 // Emit information describing all of the types in the module. 1584 WriteTypeTable(VE, Stream); 1585 1586 // Emit top-level description of module, including target triple, inline asm, 1587 // descriptors for global variables, and function prototype info. 1588 WriteModuleInfo(M, VE, Stream); 1589 1590 // Emit constants. 1591 WriteModuleConstants(VE, Stream); 1592 1593 // Emit metadata. 1594 WriteModuleMetadata(M, VE, Stream); 1595 1596 // Emit function bodies. 1597 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 1598 if (!F->isDeclaration()) 1599 WriteFunction(*F, VE, Stream); 1600 1601 // Emit metadata. 1602 WriteModuleMetadataStore(M, Stream); 1603 1604 // Emit names for globals/functions etc. 1605 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); 1606 1607 Stream.ExitBlock(); 1608} 1609 1610/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 1611/// header and trailer to make it compatible with the system archiver. To do 1612/// this we emit the following header, and then emit a trailer that pads the 1613/// file out to be a multiple of 16 bytes. 1614/// 1615/// struct bc_header { 1616/// uint32_t Magic; // 0x0B17C0DE 1617/// uint32_t Version; // Version, currently always 0. 1618/// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 1619/// uint32_t BitcodeSize; // Size of traditional bitcode file. 1620/// uint32_t CPUType; // CPU specifier. 1621/// ... potentially more later ... 1622/// }; 1623enum { 1624 DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size. 1625 DarwinBCHeaderSize = 5*4 1626}; 1627 1628static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, 1629 uint32_t &Position) { 1630 Buffer[Position + 0] = (unsigned char) (Value >> 0); 1631 Buffer[Position + 1] = (unsigned char) (Value >> 8); 1632 Buffer[Position + 2] = (unsigned char) (Value >> 16); 1633 Buffer[Position + 3] = (unsigned char) (Value >> 24); 1634 Position += 4; 1635} 1636 1637static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, 1638 const Triple &TT) { 1639 unsigned CPUType = ~0U; 1640 1641 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 1642 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 1643 // number from /usr/include/mach/machine.h. It is ok to reproduce the 1644 // specific constants here because they are implicitly part of the Darwin ABI. 1645 enum { 1646 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 1647 DARWIN_CPU_TYPE_X86 = 7, 1648 DARWIN_CPU_TYPE_ARM = 12, 1649 DARWIN_CPU_TYPE_POWERPC = 18 1650 }; 1651 1652 Triple::ArchType Arch = TT.getArch(); 1653 if (Arch == Triple::x86_64) 1654 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 1655 else if (Arch == Triple::x86) 1656 CPUType = DARWIN_CPU_TYPE_X86; 1657 else if (Arch == Triple::ppc) 1658 CPUType = DARWIN_CPU_TYPE_POWERPC; 1659 else if (Arch == Triple::ppc64) 1660 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 1661 else if (Arch == Triple::arm || Arch == Triple::thumb) 1662 CPUType = DARWIN_CPU_TYPE_ARM; 1663 1664 // Traditional Bitcode starts after header. 1665 assert(Buffer.size() >= DarwinBCHeaderSize && 1666 "Expected header size to be reserved"); 1667 unsigned BCOffset = DarwinBCHeaderSize; 1668 unsigned BCSize = Buffer.size()-DarwinBCHeaderSize; 1669 1670 // Write the magic and version. 1671 unsigned Position = 0; 1672 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position); 1673 WriteInt32ToBuffer(0 , Buffer, Position); // Version. 1674 WriteInt32ToBuffer(BCOffset , Buffer, Position); 1675 WriteInt32ToBuffer(BCSize , Buffer, Position); 1676 WriteInt32ToBuffer(CPUType , Buffer, Position); 1677 1678 // If the file is not a multiple of 16 bytes, insert dummy padding. 1679 while (Buffer.size() & 15) 1680 Buffer.push_back(0); 1681} 1682 1683/// WriteBitcodeToFile - Write the specified module to the specified output 1684/// stream. 1685void llvm_2_9::WriteBitcodeToFile(const Module *M, raw_ostream &Out) { 1686 SmallVector<char, 1024> Buffer; 1687 Buffer.reserve(256*1024); 1688 1689 // If this is darwin or another generic macho target, reserve space for the 1690 // header. 1691 Triple TT(M->getTargetTriple()); 1692 if (TT.isOSDarwin()) 1693 Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0); 1694 1695 // Emit the module into the buffer. 1696 { 1697 BitstreamWriter Stream(Buffer); 1698 1699 // Emit the file header. 1700 Stream.Emit((unsigned)'B', 8); 1701 Stream.Emit((unsigned)'C', 8); 1702 Stream.Emit(0x0, 4); 1703 Stream.Emit(0xC, 4); 1704 Stream.Emit(0xE, 4); 1705 Stream.Emit(0xD, 4); 1706 1707 // Emit the module. 1708 WriteModule(M, Stream); 1709 } 1710 1711 if (TT.isOSDarwin()) 1712 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 1713 1714 // Write the generated bitstream to "Out". 1715 Out.write((char*)&Buffer.front(), Buffer.size()); 1716} 1717