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