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 "ValueEnumerator.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/Triple.h" 17 #include "llvm/Analysis/BlockFrequencyInfo.h" 18 #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 19 #include "llvm/Analysis/BranchProbabilityInfo.h" 20 #include "llvm/Analysis/LoopInfo.h" 21 #include "llvm/Bitcode/BitstreamWriter.h" 22 #include "llvm/Bitcode/LLVMBitCodes.h" 23 #include "llvm/Bitcode/ReaderWriter.h" 24 #include "llvm/IR/CallSite.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/DebugInfoMetadata.h" 27 #include "llvm/IR/DerivedTypes.h" 28 #include "llvm/IR/Dominators.h" 29 #include "llvm/IR/InlineAsm.h" 30 #include "llvm/IR/Instructions.h" 31 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/IR/Module.h" 34 #include "llvm/IR/Operator.h" 35 #include "llvm/IR/UseListOrder.h" 36 #include "llvm/IR/ValueSymbolTable.h" 37 #include "llvm/ProfileData/ProfileCommon.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/ErrorHandling.h" 40 #include "llvm/Support/MathExtras.h" 41 #include "llvm/Support/Program.h" 42 #include "llvm/Support/raw_ostream.h" 43 #include <cctype> 44 #include <map> 45 using namespace llvm; 46 47 /// These are manifest constants used by the bitcode writer. They do not need to 48 /// be kept in sync with the reader, but need to be consistent within this file. 49 enum { 50 // VALUE_SYMTAB_BLOCK abbrev id's. 51 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 52 VST_ENTRY_7_ABBREV, 53 VST_ENTRY_6_ABBREV, 54 VST_BBENTRY_6_ABBREV, 55 56 // CONSTANTS_BLOCK abbrev id's. 57 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 58 CONSTANTS_INTEGER_ABBREV, 59 CONSTANTS_CE_CAST_Abbrev, 60 CONSTANTS_NULL_Abbrev, 61 62 // FUNCTION_BLOCK abbrev id's. 63 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 64 FUNCTION_INST_BINOP_ABBREV, 65 FUNCTION_INST_BINOP_FLAGS_ABBREV, 66 FUNCTION_INST_CAST_ABBREV, 67 FUNCTION_INST_RET_VOID_ABBREV, 68 FUNCTION_INST_RET_VAL_ABBREV, 69 FUNCTION_INST_UNREACHABLE_ABBREV, 70 FUNCTION_INST_GEP_ABBREV, 71 }; 72 73 static unsigned GetEncodedCastOpcode(unsigned Opcode) { 74 switch (Opcode) { 75 default: llvm_unreachable("Unknown cast instruction!"); 76 case Instruction::Trunc : return bitc::CAST_TRUNC; 77 case Instruction::ZExt : return bitc::CAST_ZEXT; 78 case Instruction::SExt : return bitc::CAST_SEXT; 79 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 80 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 81 case Instruction::UIToFP : return bitc::CAST_UITOFP; 82 case Instruction::SIToFP : return bitc::CAST_SITOFP; 83 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 84 case Instruction::FPExt : return bitc::CAST_FPEXT; 85 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 86 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 87 case Instruction::BitCast : return bitc::CAST_BITCAST; 88 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST; 89 } 90 } 91 92 static unsigned GetEncodedBinaryOpcode(unsigned Opcode) { 93 switch (Opcode) { 94 default: llvm_unreachable("Unknown binary instruction!"); 95 case Instruction::Add: 96 case Instruction::FAdd: return bitc::BINOP_ADD; 97 case Instruction::Sub: 98 case Instruction::FSub: return bitc::BINOP_SUB; 99 case Instruction::Mul: 100 case Instruction::FMul: return bitc::BINOP_MUL; 101 case Instruction::UDiv: return bitc::BINOP_UDIV; 102 case Instruction::FDiv: 103 case Instruction::SDiv: return bitc::BINOP_SDIV; 104 case Instruction::URem: return bitc::BINOP_UREM; 105 case Instruction::FRem: 106 case Instruction::SRem: return bitc::BINOP_SREM; 107 case Instruction::Shl: return bitc::BINOP_SHL; 108 case Instruction::LShr: return bitc::BINOP_LSHR; 109 case Instruction::AShr: return bitc::BINOP_ASHR; 110 case Instruction::And: return bitc::BINOP_AND; 111 case Instruction::Or: return bitc::BINOP_OR; 112 case Instruction::Xor: return bitc::BINOP_XOR; 113 } 114 } 115 116 static unsigned GetEncodedRMWOperation(AtomicRMWInst::BinOp Op) { 117 switch (Op) { 118 default: llvm_unreachable("Unknown RMW operation!"); 119 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; 120 case AtomicRMWInst::Add: return bitc::RMW_ADD; 121 case AtomicRMWInst::Sub: return bitc::RMW_SUB; 122 case AtomicRMWInst::And: return bitc::RMW_AND; 123 case AtomicRMWInst::Nand: return bitc::RMW_NAND; 124 case AtomicRMWInst::Or: return bitc::RMW_OR; 125 case AtomicRMWInst::Xor: return bitc::RMW_XOR; 126 case AtomicRMWInst::Max: return bitc::RMW_MAX; 127 case AtomicRMWInst::Min: return bitc::RMW_MIN; 128 case AtomicRMWInst::UMax: return bitc::RMW_UMAX; 129 case AtomicRMWInst::UMin: return bitc::RMW_UMIN; 130 } 131 } 132 133 static unsigned GetEncodedOrdering(AtomicOrdering Ordering) { 134 switch (Ordering) { 135 case NotAtomic: return bitc::ORDERING_NOTATOMIC; 136 case Unordered: return bitc::ORDERING_UNORDERED; 137 case Monotonic: return bitc::ORDERING_MONOTONIC; 138 case Acquire: return bitc::ORDERING_ACQUIRE; 139 case Release: return bitc::ORDERING_RELEASE; 140 case AcquireRelease: return bitc::ORDERING_ACQREL; 141 case SequentiallyConsistent: return bitc::ORDERING_SEQCST; 142 } 143 llvm_unreachable("Invalid ordering"); 144 } 145 146 static unsigned GetEncodedSynchScope(SynchronizationScope SynchScope) { 147 switch (SynchScope) { 148 case SingleThread: return bitc::SYNCHSCOPE_SINGLETHREAD; 149 case CrossThread: return bitc::SYNCHSCOPE_CROSSTHREAD; 150 } 151 llvm_unreachable("Invalid synch scope"); 152 } 153 154 static void WriteStringRecord(unsigned Code, StringRef Str, 155 unsigned AbbrevToUse, BitstreamWriter &Stream) { 156 SmallVector<unsigned, 64> Vals; 157 158 // Code: [strchar x N] 159 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 160 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(Str[i])) 161 AbbrevToUse = 0; 162 Vals.push_back(Str[i]); 163 } 164 165 // Emit the finished record. 166 Stream.EmitRecord(Code, Vals, AbbrevToUse); 167 } 168 169 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) { 170 switch (Kind) { 171 case Attribute::Alignment: 172 return bitc::ATTR_KIND_ALIGNMENT; 173 case Attribute::AlwaysInline: 174 return bitc::ATTR_KIND_ALWAYS_INLINE; 175 case Attribute::ArgMemOnly: 176 return bitc::ATTR_KIND_ARGMEMONLY; 177 case Attribute::Builtin: 178 return bitc::ATTR_KIND_BUILTIN; 179 case Attribute::ByVal: 180 return bitc::ATTR_KIND_BY_VAL; 181 case Attribute::Convergent: 182 return bitc::ATTR_KIND_CONVERGENT; 183 case Attribute::InAlloca: 184 return bitc::ATTR_KIND_IN_ALLOCA; 185 case Attribute::Cold: 186 return bitc::ATTR_KIND_COLD; 187 case Attribute::InaccessibleMemOnly: 188 return bitc::ATTR_KIND_INACCESSIBLEMEM_ONLY; 189 case Attribute::InaccessibleMemOrArgMemOnly: 190 return bitc::ATTR_KIND_INACCESSIBLEMEM_OR_ARGMEMONLY; 191 case Attribute::InlineHint: 192 return bitc::ATTR_KIND_INLINE_HINT; 193 case Attribute::InReg: 194 return bitc::ATTR_KIND_IN_REG; 195 case Attribute::JumpTable: 196 return bitc::ATTR_KIND_JUMP_TABLE; 197 case Attribute::MinSize: 198 return bitc::ATTR_KIND_MIN_SIZE; 199 case Attribute::Naked: 200 return bitc::ATTR_KIND_NAKED; 201 case Attribute::Nest: 202 return bitc::ATTR_KIND_NEST; 203 case Attribute::NoAlias: 204 return bitc::ATTR_KIND_NO_ALIAS; 205 case Attribute::NoBuiltin: 206 return bitc::ATTR_KIND_NO_BUILTIN; 207 case Attribute::NoCapture: 208 return bitc::ATTR_KIND_NO_CAPTURE; 209 case Attribute::NoDuplicate: 210 return bitc::ATTR_KIND_NO_DUPLICATE; 211 case Attribute::NoImplicitFloat: 212 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT; 213 case Attribute::NoInline: 214 return bitc::ATTR_KIND_NO_INLINE; 215 case Attribute::NoRecurse: 216 return bitc::ATTR_KIND_NO_RECURSE; 217 case Attribute::NonLazyBind: 218 return bitc::ATTR_KIND_NON_LAZY_BIND; 219 case Attribute::NonNull: 220 return bitc::ATTR_KIND_NON_NULL; 221 case Attribute::Dereferenceable: 222 return bitc::ATTR_KIND_DEREFERENCEABLE; 223 case Attribute::DereferenceableOrNull: 224 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL; 225 case Attribute::NoRedZone: 226 return bitc::ATTR_KIND_NO_RED_ZONE; 227 case Attribute::NoReturn: 228 return bitc::ATTR_KIND_NO_RETURN; 229 case Attribute::NoUnwind: 230 return bitc::ATTR_KIND_NO_UNWIND; 231 case Attribute::OptimizeForSize: 232 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE; 233 case Attribute::OptimizeNone: 234 return bitc::ATTR_KIND_OPTIMIZE_NONE; 235 case Attribute::ReadNone: 236 return bitc::ATTR_KIND_READ_NONE; 237 case Attribute::ReadOnly: 238 return bitc::ATTR_KIND_READ_ONLY; 239 case Attribute::Returned: 240 return bitc::ATTR_KIND_RETURNED; 241 case Attribute::ReturnsTwice: 242 return bitc::ATTR_KIND_RETURNS_TWICE; 243 case Attribute::SExt: 244 return bitc::ATTR_KIND_S_EXT; 245 case Attribute::StackAlignment: 246 return bitc::ATTR_KIND_STACK_ALIGNMENT; 247 case Attribute::StackProtect: 248 return bitc::ATTR_KIND_STACK_PROTECT; 249 case Attribute::StackProtectReq: 250 return bitc::ATTR_KIND_STACK_PROTECT_REQ; 251 case Attribute::StackProtectStrong: 252 return bitc::ATTR_KIND_STACK_PROTECT_STRONG; 253 case Attribute::SafeStack: 254 return bitc::ATTR_KIND_SAFESTACK; 255 case Attribute::StructRet: 256 return bitc::ATTR_KIND_STRUCT_RET; 257 case Attribute::SanitizeAddress: 258 return bitc::ATTR_KIND_SANITIZE_ADDRESS; 259 case Attribute::SanitizeThread: 260 return bitc::ATTR_KIND_SANITIZE_THREAD; 261 case Attribute::SanitizeMemory: 262 return bitc::ATTR_KIND_SANITIZE_MEMORY; 263 case Attribute::UWTable: 264 return bitc::ATTR_KIND_UW_TABLE; 265 case Attribute::ZExt: 266 return bitc::ATTR_KIND_Z_EXT; 267 case Attribute::EndAttrKinds: 268 llvm_unreachable("Can not encode end-attribute kinds marker."); 269 case Attribute::None: 270 llvm_unreachable("Can not encode none-attribute."); 271 } 272 273 llvm_unreachable("Trying to encode unknown attribute"); 274 } 275 276 static void WriteAttributeGroupTable(const ValueEnumerator &VE, 277 BitstreamWriter &Stream) { 278 const std::vector<AttributeSet> &AttrGrps = VE.getAttributeGroups(); 279 if (AttrGrps.empty()) return; 280 281 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3); 282 283 SmallVector<uint64_t, 64> Record; 284 for (unsigned i = 0, e = AttrGrps.size(); i != e; ++i) { 285 AttributeSet AS = AttrGrps[i]; 286 for (unsigned i = 0, e = AS.getNumSlots(); i != e; ++i) { 287 AttributeSet A = AS.getSlotAttributes(i); 288 289 Record.push_back(VE.getAttributeGroupID(A)); 290 Record.push_back(AS.getSlotIndex(i)); 291 292 for (AttributeSet::iterator I = AS.begin(0), E = AS.end(0); 293 I != E; ++I) { 294 Attribute Attr = *I; 295 if (Attr.isEnumAttribute()) { 296 Record.push_back(0); 297 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 298 } else if (Attr.isIntAttribute()) { 299 Record.push_back(1); 300 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 301 Record.push_back(Attr.getValueAsInt()); 302 } else { 303 StringRef Kind = Attr.getKindAsString(); 304 StringRef Val = Attr.getValueAsString(); 305 306 Record.push_back(Val.empty() ? 3 : 4); 307 Record.append(Kind.begin(), Kind.end()); 308 Record.push_back(0); 309 if (!Val.empty()) { 310 Record.append(Val.begin(), Val.end()); 311 Record.push_back(0); 312 } 313 } 314 } 315 316 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record); 317 Record.clear(); 318 } 319 } 320 321 Stream.ExitBlock(); 322 } 323 324 static void WriteAttributeTable(const ValueEnumerator &VE, 325 BitstreamWriter &Stream) { 326 const std::vector<AttributeSet> &Attrs = VE.getAttributes(); 327 if (Attrs.empty()) return; 328 329 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 330 331 SmallVector<uint64_t, 64> Record; 332 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) { 333 const AttributeSet &A = Attrs[i]; 334 for (unsigned i = 0, e = A.getNumSlots(); i != e; ++i) 335 Record.push_back(VE.getAttributeGroupID(A.getSlotAttributes(i))); 336 337 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 338 Record.clear(); 339 } 340 341 Stream.ExitBlock(); 342 } 343 344 /// WriteTypeTable - Write out the type table for a module. 345 static void WriteTypeTable(const ValueEnumerator &VE, BitstreamWriter &Stream) { 346 const ValueEnumerator::TypeList &TypeList = VE.getTypes(); 347 348 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 349 SmallVector<uint64_t, 64> TypeVals; 350 351 uint64_t NumBits = VE.computeBitsRequiredForTypeIndicies(); 352 353 // Abbrev for TYPE_CODE_POINTER. 354 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 355 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_POINTER)); 356 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 357 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 358 unsigned PtrAbbrev = Stream.EmitAbbrev(Abbv); 359 360 // Abbrev for TYPE_CODE_FUNCTION. 361 Abbv = new BitCodeAbbrev(); 362 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 363 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 365 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 366 367 unsigned FunctionAbbrev = Stream.EmitAbbrev(Abbv); 368 369 // Abbrev for TYPE_CODE_STRUCT_ANON. 370 Abbv = new BitCodeAbbrev(); 371 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 372 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 373 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 374 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 375 376 unsigned StructAnonAbbrev = Stream.EmitAbbrev(Abbv); 377 378 // Abbrev for TYPE_CODE_STRUCT_NAME. 379 Abbv = new BitCodeAbbrev(); 380 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 381 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 382 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 383 unsigned StructNameAbbrev = Stream.EmitAbbrev(Abbv); 384 385 // Abbrev for TYPE_CODE_STRUCT_NAMED. 386 Abbv = new BitCodeAbbrev(); 387 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 388 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 389 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 390 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 391 392 unsigned StructNamedAbbrev = Stream.EmitAbbrev(Abbv); 393 394 // Abbrev for TYPE_CODE_ARRAY. 395 Abbv = new BitCodeAbbrev(); 396 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 397 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 398 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 399 400 unsigned ArrayAbbrev = Stream.EmitAbbrev(Abbv); 401 402 // Emit an entry count so the reader can reserve space. 403 TypeVals.push_back(TypeList.size()); 404 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 405 TypeVals.clear(); 406 407 // Loop over all of the types, emitting each in turn. 408 for (unsigned i = 0, e = TypeList.size(); i != e; ++i) { 409 Type *T = TypeList[i]; 410 int AbbrevToUse = 0; 411 unsigned Code = 0; 412 413 switch (T->getTypeID()) { 414 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 415 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; 416 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 417 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 418 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 419 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 420 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 421 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 422 case Type::MetadataTyID: Code = bitc::TYPE_CODE_METADATA; break; 423 case Type::X86_MMXTyID: Code = bitc::TYPE_CODE_X86_MMX; break; 424 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break; 425 case Type::IntegerTyID: 426 // INTEGER: [width] 427 Code = bitc::TYPE_CODE_INTEGER; 428 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 429 break; 430 case Type::PointerTyID: { 431 PointerType *PTy = cast<PointerType>(T); 432 // POINTER: [pointee type, address space] 433 Code = bitc::TYPE_CODE_POINTER; 434 TypeVals.push_back(VE.getTypeID(PTy->getElementType())); 435 unsigned AddressSpace = PTy->getAddressSpace(); 436 TypeVals.push_back(AddressSpace); 437 if (AddressSpace == 0) AbbrevToUse = PtrAbbrev; 438 break; 439 } 440 case Type::FunctionTyID: { 441 FunctionType *FT = cast<FunctionType>(T); 442 // FUNCTION: [isvararg, retty, paramty x N] 443 Code = bitc::TYPE_CODE_FUNCTION; 444 TypeVals.push_back(FT->isVarArg()); 445 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 446 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 447 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 448 AbbrevToUse = FunctionAbbrev; 449 break; 450 } 451 case Type::StructTyID: { 452 StructType *ST = cast<StructType>(T); 453 // STRUCT: [ispacked, eltty x N] 454 TypeVals.push_back(ST->isPacked()); 455 // Output all of the element types. 456 for (StructType::element_iterator I = ST->element_begin(), 457 E = ST->element_end(); I != E; ++I) 458 TypeVals.push_back(VE.getTypeID(*I)); 459 460 if (ST->isLiteral()) { 461 Code = bitc::TYPE_CODE_STRUCT_ANON; 462 AbbrevToUse = StructAnonAbbrev; 463 } else { 464 if (ST->isOpaque()) { 465 Code = bitc::TYPE_CODE_OPAQUE; 466 } else { 467 Code = bitc::TYPE_CODE_STRUCT_NAMED; 468 AbbrevToUse = StructNamedAbbrev; 469 } 470 471 // Emit the name if it is present. 472 if (!ST->getName().empty()) 473 WriteStringRecord(bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 474 StructNameAbbrev, Stream); 475 } 476 break; 477 } 478 case Type::ArrayTyID: { 479 ArrayType *AT = cast<ArrayType>(T); 480 // ARRAY: [numelts, eltty] 481 Code = bitc::TYPE_CODE_ARRAY; 482 TypeVals.push_back(AT->getNumElements()); 483 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 484 AbbrevToUse = ArrayAbbrev; 485 break; 486 } 487 case Type::VectorTyID: { 488 VectorType *VT = cast<VectorType>(T); 489 // VECTOR [numelts, eltty] 490 Code = bitc::TYPE_CODE_VECTOR; 491 TypeVals.push_back(VT->getNumElements()); 492 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 493 break; 494 } 495 } 496 497 // Emit the finished record. 498 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 499 TypeVals.clear(); 500 } 501 502 Stream.ExitBlock(); 503 } 504 505 static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) { 506 switch (Linkage) { 507 case GlobalValue::ExternalLinkage: 508 return 0; 509 case GlobalValue::WeakAnyLinkage: 510 return 16; 511 case GlobalValue::AppendingLinkage: 512 return 2; 513 case GlobalValue::InternalLinkage: 514 return 3; 515 case GlobalValue::LinkOnceAnyLinkage: 516 return 18; 517 case GlobalValue::ExternalWeakLinkage: 518 return 7; 519 case GlobalValue::CommonLinkage: 520 return 8; 521 case GlobalValue::PrivateLinkage: 522 return 9; 523 case GlobalValue::WeakODRLinkage: 524 return 17; 525 case GlobalValue::LinkOnceODRLinkage: 526 return 19; 527 case GlobalValue::AvailableExternallyLinkage: 528 return 12; 529 } 530 llvm_unreachable("Invalid linkage"); 531 } 532 533 static unsigned getEncodedLinkage(const GlobalValue &GV) { 534 return getEncodedLinkage(GV.getLinkage()); 535 } 536 537 static unsigned getEncodedVisibility(const GlobalValue &GV) { 538 switch (GV.getVisibility()) { 539 case GlobalValue::DefaultVisibility: return 0; 540 case GlobalValue::HiddenVisibility: return 1; 541 case GlobalValue::ProtectedVisibility: return 2; 542 } 543 llvm_unreachable("Invalid visibility"); 544 } 545 546 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) { 547 switch (GV.getDLLStorageClass()) { 548 case GlobalValue::DefaultStorageClass: return 0; 549 case GlobalValue::DLLImportStorageClass: return 1; 550 case GlobalValue::DLLExportStorageClass: return 2; 551 } 552 llvm_unreachable("Invalid DLL storage class"); 553 } 554 555 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) { 556 switch (GV.getThreadLocalMode()) { 557 case GlobalVariable::NotThreadLocal: return 0; 558 case GlobalVariable::GeneralDynamicTLSModel: return 1; 559 case GlobalVariable::LocalDynamicTLSModel: return 2; 560 case GlobalVariable::InitialExecTLSModel: return 3; 561 case GlobalVariable::LocalExecTLSModel: return 4; 562 } 563 llvm_unreachable("Invalid TLS model"); 564 } 565 566 static unsigned getEncodedComdatSelectionKind(const Comdat &C) { 567 switch (C.getSelectionKind()) { 568 case Comdat::Any: 569 return bitc::COMDAT_SELECTION_KIND_ANY; 570 case Comdat::ExactMatch: 571 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH; 572 case Comdat::Largest: 573 return bitc::COMDAT_SELECTION_KIND_LARGEST; 574 case Comdat::NoDuplicates: 575 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES; 576 case Comdat::SameSize: 577 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE; 578 } 579 llvm_unreachable("Invalid selection kind"); 580 } 581 582 static void writeComdats(const ValueEnumerator &VE, BitstreamWriter &Stream) { 583 SmallVector<unsigned, 64> Vals; 584 for (const Comdat *C : VE.getComdats()) { 585 // COMDAT: [selection_kind, name] 586 Vals.push_back(getEncodedComdatSelectionKind(*C)); 587 size_t Size = C->getName().size(); 588 assert(isUInt<32>(Size)); 589 Vals.push_back(Size); 590 for (char Chr : C->getName()) 591 Vals.push_back((unsigned char)Chr); 592 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0); 593 Vals.clear(); 594 } 595 } 596 597 /// Write a record that will eventually hold the word offset of the 598 /// module-level VST. For now the offset is 0, which will be backpatched 599 /// after the real VST is written. Returns the bit offset to backpatch. 600 static uint64_t WriteValueSymbolTableForwardDecl(BitstreamWriter &Stream) { 601 // Write a placeholder value in for the offset of the real VST, 602 // which is written after the function blocks so that it can include 603 // the offset of each function. The placeholder offset will be 604 // updated when the real VST is written. 605 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 606 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET)); 607 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to 608 // hold the real VST offset. Must use fixed instead of VBR as we don't 609 // know how many VBR chunks to reserve ahead of time. 610 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 611 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(Abbv); 612 613 // Emit the placeholder 614 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0}; 615 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals); 616 617 // Compute and return the bit offset to the placeholder, which will be 618 // patched when the real VST is written. We can simply subtract the 32-bit 619 // fixed size from the current bit number to get the location to backpatch. 620 return Stream.GetCurrentBitNo() - 32; 621 } 622 623 enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 }; 624 625 /// Determine the encoding to use for the given string name and length. 626 static StringEncoding getStringEncoding(const char *Str, unsigned StrLen) { 627 bool isChar6 = true; 628 for (const char *C = Str, *E = C + StrLen; C != E; ++C) { 629 if (isChar6) 630 isChar6 = BitCodeAbbrevOp::isChar6(*C); 631 if ((unsigned char)*C & 128) 632 // don't bother scanning the rest. 633 return SE_Fixed8; 634 } 635 if (isChar6) 636 return SE_Char6; 637 else 638 return SE_Fixed7; 639 } 640 641 /// Emit top-level description of module, including target triple, inline asm, 642 /// descriptors for global variables, and function prototype info. 643 /// Returns the bit offset to backpatch with the location of the real VST. 644 static uint64_t WriteModuleInfo(const Module *M, const ValueEnumerator &VE, 645 BitstreamWriter &Stream) { 646 // Emit various pieces of data attached to a module. 647 if (!M->getTargetTriple().empty()) 648 WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 649 0/*TODO*/, Stream); 650 const std::string &DL = M->getDataLayoutStr(); 651 if (!DL.empty()) 652 WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream); 653 if (!M->getModuleInlineAsm().empty()) 654 WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 655 0/*TODO*/, Stream); 656 657 // Emit information about sections and GC, computing how many there are. Also 658 // compute the maximum alignment value. 659 std::map<std::string, unsigned> SectionMap; 660 std::map<std::string, unsigned> GCMap; 661 unsigned MaxAlignment = 0; 662 unsigned MaxGlobalType = 0; 663 for (const GlobalValue &GV : M->globals()) { 664 MaxAlignment = std::max(MaxAlignment, GV.getAlignment()); 665 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType())); 666 if (GV.hasSection()) { 667 // Give section names unique ID's. 668 unsigned &Entry = SectionMap[GV.getSection()]; 669 if (!Entry) { 670 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, GV.getSection(), 671 0/*TODO*/, Stream); 672 Entry = SectionMap.size(); 673 } 674 } 675 } 676 for (const Function &F : *M) { 677 MaxAlignment = std::max(MaxAlignment, F.getAlignment()); 678 if (F.hasSection()) { 679 // Give section names unique ID's. 680 unsigned &Entry = SectionMap[F.getSection()]; 681 if (!Entry) { 682 WriteStringRecord(bitc::MODULE_CODE_SECTIONNAME, F.getSection(), 683 0/*TODO*/, Stream); 684 Entry = SectionMap.size(); 685 } 686 } 687 if (F.hasGC()) { 688 // Same for GC names. 689 unsigned &Entry = GCMap[F.getGC()]; 690 if (!Entry) { 691 WriteStringRecord(bitc::MODULE_CODE_GCNAME, F.getGC(), 692 0/*TODO*/, Stream); 693 Entry = GCMap.size(); 694 } 695 } 696 } 697 698 // Emit abbrev for globals, now that we know # sections and max alignment. 699 unsigned SimpleGVarAbbrev = 0; 700 if (!M->global_empty()) { 701 // Add an abbrev for common globals with no visibility or thread localness. 702 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 703 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 704 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 705 Log2_32_Ceil(MaxGlobalType+1))); 706 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2 707 //| explicitType << 1 708 //| constant 709 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 710 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage. 711 if (MaxAlignment == 0) // Alignment. 712 Abbv->Add(BitCodeAbbrevOp(0)); 713 else { 714 unsigned MaxEncAlignment = Log2_32(MaxAlignment)+1; 715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 716 Log2_32_Ceil(MaxEncAlignment+1))); 717 } 718 if (SectionMap.empty()) // Section. 719 Abbv->Add(BitCodeAbbrevOp(0)); 720 else 721 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 722 Log2_32_Ceil(SectionMap.size()+1))); 723 // Don't bother emitting vis + thread local. 724 SimpleGVarAbbrev = Stream.EmitAbbrev(Abbv); 725 } 726 727 // Emit the global variable information. 728 SmallVector<unsigned, 64> Vals; 729 for (const GlobalVariable &GV : M->globals()) { 730 unsigned AbbrevToUse = 0; 731 732 // GLOBALVAR: [type, isconst, initid, 733 // linkage, alignment, section, visibility, threadlocal, 734 // unnamed_addr, externally_initialized, dllstorageclass, 735 // comdat] 736 Vals.push_back(VE.getTypeID(GV.getValueType())); 737 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant()); 738 Vals.push_back(GV.isDeclaration() ? 0 : 739 (VE.getValueID(GV.getInitializer()) + 1)); 740 Vals.push_back(getEncodedLinkage(GV)); 741 Vals.push_back(Log2_32(GV.getAlignment())+1); 742 Vals.push_back(GV.hasSection() ? SectionMap[GV.getSection()] : 0); 743 if (GV.isThreadLocal() || 744 GV.getVisibility() != GlobalValue::DefaultVisibility || 745 GV.hasUnnamedAddr() || GV.isExternallyInitialized() || 746 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass || 747 GV.hasComdat()) { 748 Vals.push_back(getEncodedVisibility(GV)); 749 Vals.push_back(getEncodedThreadLocalMode(GV)); 750 Vals.push_back(GV.hasUnnamedAddr()); 751 Vals.push_back(GV.isExternallyInitialized()); 752 Vals.push_back(getEncodedDLLStorageClass(GV)); 753 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0); 754 } else { 755 AbbrevToUse = SimpleGVarAbbrev; 756 } 757 758 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 759 Vals.clear(); 760 } 761 762 // Emit the function proto information. 763 for (const Function &F : *M) { 764 // FUNCTION: [type, callingconv, isproto, linkage, paramattrs, alignment, 765 // section, visibility, gc, unnamed_addr, prologuedata, 766 // dllstorageclass, comdat, prefixdata, personalityfn] 767 Vals.push_back(VE.getTypeID(F.getFunctionType())); 768 Vals.push_back(F.getCallingConv()); 769 Vals.push_back(F.isDeclaration()); 770 Vals.push_back(getEncodedLinkage(F)); 771 Vals.push_back(VE.getAttributeID(F.getAttributes())); 772 Vals.push_back(Log2_32(F.getAlignment())+1); 773 Vals.push_back(F.hasSection() ? SectionMap[F.getSection()] : 0); 774 Vals.push_back(getEncodedVisibility(F)); 775 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0); 776 Vals.push_back(F.hasUnnamedAddr()); 777 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) 778 : 0); 779 Vals.push_back(getEncodedDLLStorageClass(F)); 780 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0); 781 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1) 782 : 0); 783 Vals.push_back( 784 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0); 785 786 unsigned AbbrevToUse = 0; 787 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 788 Vals.clear(); 789 } 790 791 // Emit the alias information. 792 for (const GlobalAlias &A : M->aliases()) { 793 // ALIAS: [alias type, aliasee val#, linkage, visibility] 794 Vals.push_back(VE.getTypeID(A.getValueType())); 795 Vals.push_back(A.getType()->getAddressSpace()); 796 Vals.push_back(VE.getValueID(A.getAliasee())); 797 Vals.push_back(getEncodedLinkage(A)); 798 Vals.push_back(getEncodedVisibility(A)); 799 Vals.push_back(getEncodedDLLStorageClass(A)); 800 Vals.push_back(getEncodedThreadLocalMode(A)); 801 Vals.push_back(A.hasUnnamedAddr()); 802 unsigned AbbrevToUse = 0; 803 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 804 Vals.clear(); 805 } 806 807 // Write a record indicating the number of module-level metadata IDs 808 // This is needed because the ids of metadata are assigned implicitly 809 // based on their ordering in the bitcode, with the function-level 810 // metadata ids starting after the module-level metadata ids. For 811 // function importing where we lazy load the metadata as a postpass, 812 // we want to avoid parsing the module-level metadata before parsing 813 // the imported functions. 814 { 815 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 816 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_METADATA_VALUES)); 817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 818 unsigned MDValsAbbrev = Stream.EmitAbbrev(Abbv); 819 Vals.push_back(VE.numMDs()); 820 Stream.EmitRecord(bitc::MODULE_CODE_METADATA_VALUES, Vals, MDValsAbbrev); 821 Vals.clear(); 822 } 823 824 // Emit the module's source file name. 825 { 826 StringEncoding Bits = getStringEncoding(M->getSourceFileName().data(), 827 M->getSourceFileName().size()); 828 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8); 829 if (Bits == SE_Char6) 830 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6); 831 else if (Bits == SE_Fixed7) 832 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7); 833 834 // MODULE_CODE_SOURCE_FILENAME: [namechar x N] 835 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 836 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME)); 837 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 838 Abbv->Add(AbbrevOpToUse); 839 unsigned FilenameAbbrev = Stream.EmitAbbrev(Abbv); 840 841 for (const auto P : M->getSourceFileName()) 842 Vals.push_back((unsigned char)P); 843 844 // Emit the finished record. 845 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev); 846 Vals.clear(); 847 } 848 849 // If we have a VST, write the VSTOFFSET record placeholder and return 850 // its offset. 851 if (M->getValueSymbolTable().empty()) 852 return 0; 853 return WriteValueSymbolTableForwardDecl(Stream); 854 } 855 856 static uint64_t GetOptimizationFlags(const Value *V) { 857 uint64_t Flags = 0; 858 859 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) { 860 if (OBO->hasNoSignedWrap()) 861 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 862 if (OBO->hasNoUnsignedWrap()) 863 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 864 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) { 865 if (PEO->isExact()) 866 Flags |= 1 << bitc::PEO_EXACT; 867 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) { 868 if (FPMO->hasUnsafeAlgebra()) 869 Flags |= FastMathFlags::UnsafeAlgebra; 870 if (FPMO->hasNoNaNs()) 871 Flags |= FastMathFlags::NoNaNs; 872 if (FPMO->hasNoInfs()) 873 Flags |= FastMathFlags::NoInfs; 874 if (FPMO->hasNoSignedZeros()) 875 Flags |= FastMathFlags::NoSignedZeros; 876 if (FPMO->hasAllowReciprocal()) 877 Flags |= FastMathFlags::AllowReciprocal; 878 } 879 880 return Flags; 881 } 882 883 static void WriteValueAsMetadata(const ValueAsMetadata *MD, 884 const ValueEnumerator &VE, 885 BitstreamWriter &Stream, 886 SmallVectorImpl<uint64_t> &Record) { 887 // Mimic an MDNode with a value as one operand. 888 Value *V = MD->getValue(); 889 Record.push_back(VE.getTypeID(V->getType())); 890 Record.push_back(VE.getValueID(V)); 891 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0); 892 Record.clear(); 893 } 894 895 static void WriteMDTuple(const MDTuple *N, const ValueEnumerator &VE, 896 BitstreamWriter &Stream, 897 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { 898 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 899 Metadata *MD = N->getOperand(i); 900 assert(!(MD && isa<LocalAsMetadata>(MD)) && 901 "Unexpected function-local metadata"); 902 Record.push_back(VE.getMetadataOrNullID(MD)); 903 } 904 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE 905 : bitc::METADATA_NODE, 906 Record, Abbrev); 907 Record.clear(); 908 } 909 910 static void WriteDILocation(const DILocation *N, const ValueEnumerator &VE, 911 BitstreamWriter &Stream, 912 SmallVectorImpl<uint64_t> &Record, 913 unsigned Abbrev) { 914 Record.push_back(N->isDistinct()); 915 Record.push_back(N->getLine()); 916 Record.push_back(N->getColumn()); 917 Record.push_back(VE.getMetadataID(N->getScope())); 918 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt())); 919 920 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev); 921 Record.clear(); 922 } 923 924 static void WriteGenericDINode(const GenericDINode *N, 925 const ValueEnumerator &VE, 926 BitstreamWriter &Stream, 927 SmallVectorImpl<uint64_t> &Record, 928 unsigned Abbrev) { 929 Record.push_back(N->isDistinct()); 930 Record.push_back(N->getTag()); 931 Record.push_back(0); // Per-tag version field; unused for now. 932 933 for (auto &I : N->operands()) 934 Record.push_back(VE.getMetadataOrNullID(I)); 935 936 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev); 937 Record.clear(); 938 } 939 940 static uint64_t rotateSign(int64_t I) { 941 uint64_t U = I; 942 return I < 0 ? ~(U << 1) : U << 1; 943 } 944 945 static void WriteDISubrange(const DISubrange *N, const ValueEnumerator &, 946 BitstreamWriter &Stream, 947 SmallVectorImpl<uint64_t> &Record, 948 unsigned Abbrev) { 949 Record.push_back(N->isDistinct()); 950 Record.push_back(N->getCount()); 951 Record.push_back(rotateSign(N->getLowerBound())); 952 953 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev); 954 Record.clear(); 955 } 956 957 static void WriteDIEnumerator(const DIEnumerator *N, const ValueEnumerator &VE, 958 BitstreamWriter &Stream, 959 SmallVectorImpl<uint64_t> &Record, 960 unsigned Abbrev) { 961 Record.push_back(N->isDistinct()); 962 Record.push_back(rotateSign(N->getValue())); 963 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 964 965 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev); 966 Record.clear(); 967 } 968 969 static void WriteDIBasicType(const DIBasicType *N, const ValueEnumerator &VE, 970 BitstreamWriter &Stream, 971 SmallVectorImpl<uint64_t> &Record, 972 unsigned Abbrev) { 973 Record.push_back(N->isDistinct()); 974 Record.push_back(N->getTag()); 975 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 976 Record.push_back(N->getSizeInBits()); 977 Record.push_back(N->getAlignInBits()); 978 Record.push_back(N->getEncoding()); 979 980 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev); 981 Record.clear(); 982 } 983 984 static void WriteDIDerivedType(const DIDerivedType *N, 985 const ValueEnumerator &VE, 986 BitstreamWriter &Stream, 987 SmallVectorImpl<uint64_t> &Record, 988 unsigned Abbrev) { 989 Record.push_back(N->isDistinct()); 990 Record.push_back(N->getTag()); 991 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 992 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 993 Record.push_back(N->getLine()); 994 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 995 Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); 996 Record.push_back(N->getSizeInBits()); 997 Record.push_back(N->getAlignInBits()); 998 Record.push_back(N->getOffsetInBits()); 999 Record.push_back(N->getFlags()); 1000 Record.push_back(VE.getMetadataOrNullID(N->getExtraData())); 1001 1002 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev); 1003 Record.clear(); 1004 } 1005 1006 static void WriteDICompositeType(const DICompositeType *N, 1007 const ValueEnumerator &VE, 1008 BitstreamWriter &Stream, 1009 SmallVectorImpl<uint64_t> &Record, 1010 unsigned Abbrev) { 1011 Record.push_back(N->isDistinct()); 1012 Record.push_back(N->getTag()); 1013 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1014 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1015 Record.push_back(N->getLine()); 1016 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1017 Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); 1018 Record.push_back(N->getSizeInBits()); 1019 Record.push_back(N->getAlignInBits()); 1020 Record.push_back(N->getOffsetInBits()); 1021 Record.push_back(N->getFlags()); 1022 Record.push_back(VE.getMetadataOrNullID(N->getElements().get())); 1023 Record.push_back(N->getRuntimeLang()); 1024 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder())); 1025 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get())); 1026 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier())); 1027 1028 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev); 1029 Record.clear(); 1030 } 1031 1032 static void WriteDISubroutineType(const DISubroutineType *N, 1033 const ValueEnumerator &VE, 1034 BitstreamWriter &Stream, 1035 SmallVectorImpl<uint64_t> &Record, 1036 unsigned Abbrev) { 1037 Record.push_back(N->isDistinct()); 1038 Record.push_back(N->getFlags()); 1039 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get())); 1040 1041 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev); 1042 Record.clear(); 1043 } 1044 1045 static void WriteDIFile(const DIFile *N, const ValueEnumerator &VE, 1046 BitstreamWriter &Stream, 1047 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { 1048 Record.push_back(N->isDistinct()); 1049 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename())); 1050 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory())); 1051 1052 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev); 1053 Record.clear(); 1054 } 1055 1056 static void WriteDICompileUnit(const DICompileUnit *N, 1057 const ValueEnumerator &VE, 1058 BitstreamWriter &Stream, 1059 SmallVectorImpl<uint64_t> &Record, 1060 unsigned Abbrev) { 1061 assert(N->isDistinct() && "Expected distinct compile units"); 1062 Record.push_back(/* IsDistinct */ true); 1063 Record.push_back(N->getSourceLanguage()); 1064 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1065 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer())); 1066 Record.push_back(N->isOptimized()); 1067 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags())); 1068 Record.push_back(N->getRuntimeVersion()); 1069 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename())); 1070 Record.push_back(N->getEmissionKind()); 1071 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get())); 1072 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get())); 1073 Record.push_back(VE.getMetadataOrNullID(N->getSubprograms().get())); 1074 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get())); 1075 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get())); 1076 Record.push_back(N->getDWOId()); 1077 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get())); 1078 1079 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev); 1080 Record.clear(); 1081 } 1082 1083 static void WriteDISubprogram(const DISubprogram *N, const ValueEnumerator &VE, 1084 BitstreamWriter &Stream, 1085 SmallVectorImpl<uint64_t> &Record, 1086 unsigned Abbrev) { 1087 Record.push_back(N->isDistinct()); 1088 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1089 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1090 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); 1091 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1092 Record.push_back(N->getLine()); 1093 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1094 Record.push_back(N->isLocalToUnit()); 1095 Record.push_back(N->isDefinition()); 1096 Record.push_back(N->getScopeLine()); 1097 Record.push_back(VE.getMetadataOrNullID(N->getContainingType())); 1098 Record.push_back(N->getVirtuality()); 1099 Record.push_back(N->getVirtualIndex()); 1100 Record.push_back(N->getFlags()); 1101 Record.push_back(N->isOptimized()); 1102 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get())); 1103 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration())); 1104 Record.push_back(VE.getMetadataOrNullID(N->getVariables().get())); 1105 1106 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev); 1107 Record.clear(); 1108 } 1109 1110 static void WriteDILexicalBlock(const DILexicalBlock *N, 1111 const ValueEnumerator &VE, 1112 BitstreamWriter &Stream, 1113 SmallVectorImpl<uint64_t> &Record, 1114 unsigned Abbrev) { 1115 Record.push_back(N->isDistinct()); 1116 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1117 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1118 Record.push_back(N->getLine()); 1119 Record.push_back(N->getColumn()); 1120 1121 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev); 1122 Record.clear(); 1123 } 1124 1125 static void WriteDILexicalBlockFile(const DILexicalBlockFile *N, 1126 const ValueEnumerator &VE, 1127 BitstreamWriter &Stream, 1128 SmallVectorImpl<uint64_t> &Record, 1129 unsigned Abbrev) { 1130 Record.push_back(N->isDistinct()); 1131 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1132 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1133 Record.push_back(N->getDiscriminator()); 1134 1135 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev); 1136 Record.clear(); 1137 } 1138 1139 static void WriteDINamespace(const DINamespace *N, const ValueEnumerator &VE, 1140 BitstreamWriter &Stream, 1141 SmallVectorImpl<uint64_t> &Record, 1142 unsigned Abbrev) { 1143 Record.push_back(N->isDistinct()); 1144 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1145 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1146 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1147 Record.push_back(N->getLine()); 1148 1149 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev); 1150 Record.clear(); 1151 } 1152 1153 static void WriteDIMacro(const DIMacro *N, const ValueEnumerator &VE, 1154 BitstreamWriter &Stream, 1155 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { 1156 Record.push_back(N->isDistinct()); 1157 Record.push_back(N->getMacinfoType()); 1158 Record.push_back(N->getLine()); 1159 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1160 Record.push_back(VE.getMetadataOrNullID(N->getRawValue())); 1161 1162 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev); 1163 Record.clear(); 1164 } 1165 1166 static void WriteDIMacroFile(const DIMacroFile *N, const ValueEnumerator &VE, 1167 BitstreamWriter &Stream, 1168 SmallVectorImpl<uint64_t> &Record, 1169 unsigned Abbrev) { 1170 Record.push_back(N->isDistinct()); 1171 Record.push_back(N->getMacinfoType()); 1172 Record.push_back(N->getLine()); 1173 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1174 Record.push_back(VE.getMetadataOrNullID(N->getElements().get())); 1175 1176 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev); 1177 Record.clear(); 1178 } 1179 1180 static void WriteDIModule(const DIModule *N, const ValueEnumerator &VE, 1181 BitstreamWriter &Stream, 1182 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev) { 1183 Record.push_back(N->isDistinct()); 1184 for (auto &I : N->operands()) 1185 Record.push_back(VE.getMetadataOrNullID(I)); 1186 1187 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev); 1188 Record.clear(); 1189 } 1190 1191 static void WriteDITemplateTypeParameter(const DITemplateTypeParameter *N, 1192 const ValueEnumerator &VE, 1193 BitstreamWriter &Stream, 1194 SmallVectorImpl<uint64_t> &Record, 1195 unsigned Abbrev) { 1196 Record.push_back(N->isDistinct()); 1197 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1198 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1199 1200 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev); 1201 Record.clear(); 1202 } 1203 1204 static void WriteDITemplateValueParameter(const DITemplateValueParameter *N, 1205 const ValueEnumerator &VE, 1206 BitstreamWriter &Stream, 1207 SmallVectorImpl<uint64_t> &Record, 1208 unsigned Abbrev) { 1209 Record.push_back(N->isDistinct()); 1210 Record.push_back(N->getTag()); 1211 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1212 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1213 Record.push_back(VE.getMetadataOrNullID(N->getValue())); 1214 1215 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev); 1216 Record.clear(); 1217 } 1218 1219 static void WriteDIGlobalVariable(const DIGlobalVariable *N, 1220 const ValueEnumerator &VE, 1221 BitstreamWriter &Stream, 1222 SmallVectorImpl<uint64_t> &Record, 1223 unsigned Abbrev) { 1224 Record.push_back(N->isDistinct()); 1225 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1226 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1227 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); 1228 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1229 Record.push_back(N->getLine()); 1230 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1231 Record.push_back(N->isLocalToUnit()); 1232 Record.push_back(N->isDefinition()); 1233 Record.push_back(VE.getMetadataOrNullID(N->getRawVariable())); 1234 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration())); 1235 1236 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev); 1237 Record.clear(); 1238 } 1239 1240 static void WriteDILocalVariable(const DILocalVariable *N, 1241 const ValueEnumerator &VE, 1242 BitstreamWriter &Stream, 1243 SmallVectorImpl<uint64_t> &Record, 1244 unsigned Abbrev) { 1245 Record.push_back(N->isDistinct()); 1246 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1247 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1248 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1249 Record.push_back(N->getLine()); 1250 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1251 Record.push_back(N->getArg()); 1252 Record.push_back(N->getFlags()); 1253 1254 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev); 1255 Record.clear(); 1256 } 1257 1258 static void WriteDIExpression(const DIExpression *N, const ValueEnumerator &, 1259 BitstreamWriter &Stream, 1260 SmallVectorImpl<uint64_t> &Record, 1261 unsigned Abbrev) { 1262 Record.reserve(N->getElements().size() + 1); 1263 1264 Record.push_back(N->isDistinct()); 1265 Record.append(N->elements_begin(), N->elements_end()); 1266 1267 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev); 1268 Record.clear(); 1269 } 1270 1271 static void WriteDIObjCProperty(const DIObjCProperty *N, 1272 const ValueEnumerator &VE, 1273 BitstreamWriter &Stream, 1274 SmallVectorImpl<uint64_t> &Record, 1275 unsigned Abbrev) { 1276 Record.push_back(N->isDistinct()); 1277 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1278 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1279 Record.push_back(N->getLine()); 1280 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName())); 1281 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName())); 1282 Record.push_back(N->getAttributes()); 1283 Record.push_back(VE.getMetadataOrNullID(N->getType())); 1284 1285 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev); 1286 Record.clear(); 1287 } 1288 1289 static void WriteDIImportedEntity(const DIImportedEntity *N, 1290 const ValueEnumerator &VE, 1291 BitstreamWriter &Stream, 1292 SmallVectorImpl<uint64_t> &Record, 1293 unsigned Abbrev) { 1294 Record.push_back(N->isDistinct()); 1295 Record.push_back(N->getTag()); 1296 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1297 Record.push_back(VE.getMetadataOrNullID(N->getEntity())); 1298 Record.push_back(N->getLine()); 1299 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1300 1301 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev); 1302 Record.clear(); 1303 } 1304 1305 static void WriteModuleMetadata(const Module *M, 1306 const ValueEnumerator &VE, 1307 BitstreamWriter &Stream) { 1308 const auto &MDs = VE.getMDs(); 1309 if (MDs.empty() && M->named_metadata_empty()) 1310 return; 1311 1312 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 1313 1314 unsigned MDSAbbrev = 0; 1315 if (VE.hasMDString()) { 1316 // Abbrev for METADATA_STRING. 1317 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1318 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); 1319 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1320 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1321 MDSAbbrev = Stream.EmitAbbrev(Abbv); 1322 } 1323 1324 // Initialize MDNode abbreviations. 1325 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0; 1326 #include "llvm/IR/Metadata.def" 1327 1328 if (VE.hasDILocation()) { 1329 // Abbrev for METADATA_LOCATION. 1330 // 1331 // Assume the column is usually under 128, and always output the inlined-at 1332 // location (it's never more expensive than building an array size 1). 1333 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1334 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION)); 1335 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 1336 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1337 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1338 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1339 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1340 DILocationAbbrev = Stream.EmitAbbrev(Abbv); 1341 } 1342 1343 if (VE.hasGenericDINode()) { 1344 // Abbrev for METADATA_GENERIC_DEBUG. 1345 // 1346 // Assume the column is usually under 128, and always output the inlined-at 1347 // location (it's never more expensive than building an array size 1). 1348 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1349 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG)); 1350 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 1351 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1352 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 1353 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1354 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1355 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1356 GenericDINodeAbbrev = Stream.EmitAbbrev(Abbv); 1357 } 1358 1359 unsigned NameAbbrev = 0; 1360 if (!M->named_metadata_empty()) { 1361 // Abbrev for METADATA_NAME. 1362 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1363 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME)); 1364 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1365 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1366 NameAbbrev = Stream.EmitAbbrev(Abbv); 1367 } 1368 1369 SmallVector<uint64_t, 64> Record; 1370 for (const Metadata *MD : MDs) { 1371 if (const MDNode *N = dyn_cast<MDNode>(MD)) { 1372 assert(N->isResolved() && "Expected forward references to be resolved"); 1373 1374 switch (N->getMetadataID()) { 1375 default: 1376 llvm_unreachable("Invalid MDNode subclass"); 1377 #define HANDLE_MDNODE_LEAF(CLASS) \ 1378 case Metadata::CLASS##Kind: \ 1379 Write##CLASS(cast<CLASS>(N), VE, Stream, Record, CLASS##Abbrev); \ 1380 continue; 1381 #include "llvm/IR/Metadata.def" 1382 } 1383 } 1384 if (const auto *MDC = dyn_cast<ConstantAsMetadata>(MD)) { 1385 WriteValueAsMetadata(MDC, VE, Stream, Record); 1386 continue; 1387 } 1388 const MDString *MDS = cast<MDString>(MD); 1389 // Code: [strchar x N] 1390 Record.append(MDS->bytes_begin(), MDS->bytes_end()); 1391 1392 // Emit the finished record. 1393 Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); 1394 Record.clear(); 1395 } 1396 1397 // Write named metadata. 1398 for (const NamedMDNode &NMD : M->named_metadata()) { 1399 // Write name. 1400 StringRef Str = NMD.getName(); 1401 Record.append(Str.bytes_begin(), Str.bytes_end()); 1402 Stream.EmitRecord(bitc::METADATA_NAME, Record, NameAbbrev); 1403 Record.clear(); 1404 1405 // Write named metadata operands. 1406 for (const MDNode *N : NMD.operands()) 1407 Record.push_back(VE.getMetadataID(N)); 1408 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 1409 Record.clear(); 1410 } 1411 1412 Stream.ExitBlock(); 1413 } 1414 1415 static void WriteFunctionLocalMetadata(const Function &F, 1416 const ValueEnumerator &VE, 1417 BitstreamWriter &Stream) { 1418 bool StartedMetadataBlock = false; 1419 SmallVector<uint64_t, 64> Record; 1420 const SmallVectorImpl<const LocalAsMetadata *> &MDs = 1421 VE.getFunctionLocalMDs(); 1422 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 1423 assert(MDs[i] && "Expected valid function-local metadata"); 1424 if (!StartedMetadataBlock) { 1425 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 1426 StartedMetadataBlock = true; 1427 } 1428 WriteValueAsMetadata(MDs[i], VE, Stream, Record); 1429 } 1430 1431 if (StartedMetadataBlock) 1432 Stream.ExitBlock(); 1433 } 1434 1435 static void WriteMetadataAttachment(const Function &F, 1436 const ValueEnumerator &VE, 1437 BitstreamWriter &Stream) { 1438 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 1439 1440 SmallVector<uint64_t, 64> Record; 1441 1442 // Write metadata attachments 1443 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 1444 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 1445 F.getAllMetadata(MDs); 1446 if (!MDs.empty()) { 1447 for (const auto &I : MDs) { 1448 Record.push_back(I.first); 1449 Record.push_back(VE.getMetadataID(I.second)); 1450 } 1451 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 1452 Record.clear(); 1453 } 1454 1455 for (const BasicBlock &BB : F) 1456 for (const Instruction &I : BB) { 1457 MDs.clear(); 1458 I.getAllMetadataOtherThanDebugLoc(MDs); 1459 1460 // If no metadata, ignore instruction. 1461 if (MDs.empty()) continue; 1462 1463 Record.push_back(VE.getInstructionID(&I)); 1464 1465 for (unsigned i = 0, e = MDs.size(); i != e; ++i) { 1466 Record.push_back(MDs[i].first); 1467 Record.push_back(VE.getMetadataID(MDs[i].second)); 1468 } 1469 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 1470 Record.clear(); 1471 } 1472 1473 Stream.ExitBlock(); 1474 } 1475 1476 static void WriteModuleMetadataStore(const Module *M, BitstreamWriter &Stream) { 1477 SmallVector<uint64_t, 64> Record; 1478 1479 // Write metadata kinds 1480 // METADATA_KIND - [n x [id, name]] 1481 SmallVector<StringRef, 8> Names; 1482 M->getMDKindNames(Names); 1483 1484 if (Names.empty()) return; 1485 1486 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3); 1487 1488 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 1489 Record.push_back(MDKindID); 1490 StringRef KName = Names[MDKindID]; 1491 Record.append(KName.begin(), KName.end()); 1492 1493 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 1494 Record.clear(); 1495 } 1496 1497 Stream.ExitBlock(); 1498 } 1499 1500 static void WriteOperandBundleTags(const Module *M, BitstreamWriter &Stream) { 1501 // Write metadata kinds 1502 // 1503 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG 1504 // 1505 // OPERAND_BUNDLE_TAG - [strchr x N] 1506 1507 SmallVector<StringRef, 8> Tags; 1508 M->getOperandBundleTags(Tags); 1509 1510 if (Tags.empty()) 1511 return; 1512 1513 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3); 1514 1515 SmallVector<uint64_t, 64> Record; 1516 1517 for (auto Tag : Tags) { 1518 Record.append(Tag.begin(), Tag.end()); 1519 1520 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0); 1521 Record.clear(); 1522 } 1523 1524 Stream.ExitBlock(); 1525 } 1526 1527 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 1528 if ((int64_t)V >= 0) 1529 Vals.push_back(V << 1); 1530 else 1531 Vals.push_back((-V << 1) | 1); 1532 } 1533 1534 static void WriteConstants(unsigned FirstVal, unsigned LastVal, 1535 const ValueEnumerator &VE, 1536 BitstreamWriter &Stream, bool isGlobal) { 1537 if (FirstVal == LastVal) return; 1538 1539 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 1540 1541 unsigned AggregateAbbrev = 0; 1542 unsigned String8Abbrev = 0; 1543 unsigned CString7Abbrev = 0; 1544 unsigned CString6Abbrev = 0; 1545 // If this is a constant pool for the module, emit module-specific abbrevs. 1546 if (isGlobal) { 1547 // Abbrev for CST_CODE_AGGREGATE. 1548 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 1549 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 1550 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1551 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 1552 AggregateAbbrev = Stream.EmitAbbrev(Abbv); 1553 1554 // Abbrev for CST_CODE_STRING. 1555 Abbv = new BitCodeAbbrev(); 1556 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 1557 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1558 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 1559 String8Abbrev = Stream.EmitAbbrev(Abbv); 1560 // Abbrev for CST_CODE_CSTRING. 1561 Abbv = new BitCodeAbbrev(); 1562 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 1563 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1564 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 1565 CString7Abbrev = Stream.EmitAbbrev(Abbv); 1566 // Abbrev for CST_CODE_CSTRING. 1567 Abbv = new BitCodeAbbrev(); 1568 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 1569 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1570 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1571 CString6Abbrev = Stream.EmitAbbrev(Abbv); 1572 } 1573 1574 SmallVector<uint64_t, 64> Record; 1575 1576 const ValueEnumerator::ValueList &Vals = VE.getValues(); 1577 Type *LastTy = nullptr; 1578 for (unsigned i = FirstVal; i != LastVal; ++i) { 1579 const Value *V = Vals[i].first; 1580 // If we need to switch types, do so now. 1581 if (V->getType() != LastTy) { 1582 LastTy = V->getType(); 1583 Record.push_back(VE.getTypeID(LastTy)); 1584 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 1585 CONSTANTS_SETTYPE_ABBREV); 1586 Record.clear(); 1587 } 1588 1589 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 1590 Record.push_back(unsigned(IA->hasSideEffects()) | 1591 unsigned(IA->isAlignStack()) << 1 | 1592 unsigned(IA->getDialect()&1) << 2); 1593 1594 // Add the asm string. 1595 const std::string &AsmStr = IA->getAsmString(); 1596 Record.push_back(AsmStr.size()); 1597 Record.append(AsmStr.begin(), AsmStr.end()); 1598 1599 // Add the constraint string. 1600 const std::string &ConstraintStr = IA->getConstraintString(); 1601 Record.push_back(ConstraintStr.size()); 1602 Record.append(ConstraintStr.begin(), ConstraintStr.end()); 1603 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 1604 Record.clear(); 1605 continue; 1606 } 1607 const Constant *C = cast<Constant>(V); 1608 unsigned Code = -1U; 1609 unsigned AbbrevToUse = 0; 1610 if (C->isNullValue()) { 1611 Code = bitc::CST_CODE_NULL; 1612 } else if (isa<UndefValue>(C)) { 1613 Code = bitc::CST_CODE_UNDEF; 1614 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 1615 if (IV->getBitWidth() <= 64) { 1616 uint64_t V = IV->getSExtValue(); 1617 emitSignedInt64(Record, V); 1618 Code = bitc::CST_CODE_INTEGER; 1619 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 1620 } else { // Wide integers, > 64 bits in size. 1621 // We have an arbitrary precision integer value to write whose 1622 // bit width is > 64. However, in canonical unsigned integer 1623 // format it is likely that the high bits are going to be zero. 1624 // So, we only write the number of active words. 1625 unsigned NWords = IV->getValue().getActiveWords(); 1626 const uint64_t *RawWords = IV->getValue().getRawData(); 1627 for (unsigned i = 0; i != NWords; ++i) { 1628 emitSignedInt64(Record, RawWords[i]); 1629 } 1630 Code = bitc::CST_CODE_WIDE_INTEGER; 1631 } 1632 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 1633 Code = bitc::CST_CODE_FLOAT; 1634 Type *Ty = CFP->getType(); 1635 if (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy()) { 1636 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 1637 } else if (Ty->isX86_FP80Ty()) { 1638 // api needed to prevent premature destruction 1639 // bits are not in the same order as a normal i80 APInt, compensate. 1640 APInt api = CFP->getValueAPF().bitcastToAPInt(); 1641 const uint64_t *p = api.getRawData(); 1642 Record.push_back((p[1] << 48) | (p[0] >> 16)); 1643 Record.push_back(p[0] & 0xffffLL); 1644 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 1645 APInt api = CFP->getValueAPF().bitcastToAPInt(); 1646 const uint64_t *p = api.getRawData(); 1647 Record.push_back(p[0]); 1648 Record.push_back(p[1]); 1649 } else { 1650 assert (0 && "Unknown FP type!"); 1651 } 1652 } else if (isa<ConstantDataSequential>(C) && 1653 cast<ConstantDataSequential>(C)->isString()) { 1654 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 1655 // Emit constant strings specially. 1656 unsigned NumElts = Str->getNumElements(); 1657 // If this is a null-terminated string, use the denser CSTRING encoding. 1658 if (Str->isCString()) { 1659 Code = bitc::CST_CODE_CSTRING; 1660 --NumElts; // Don't encode the null, which isn't allowed by char6. 1661 } else { 1662 Code = bitc::CST_CODE_STRING; 1663 AbbrevToUse = String8Abbrev; 1664 } 1665 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 1666 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 1667 for (unsigned i = 0; i != NumElts; ++i) { 1668 unsigned char V = Str->getElementAsInteger(i); 1669 Record.push_back(V); 1670 isCStr7 &= (V & 128) == 0; 1671 if (isCStrChar6) 1672 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 1673 } 1674 1675 if (isCStrChar6) 1676 AbbrevToUse = CString6Abbrev; 1677 else if (isCStr7) 1678 AbbrevToUse = CString7Abbrev; 1679 } else if (const ConstantDataSequential *CDS = 1680 dyn_cast<ConstantDataSequential>(C)) { 1681 Code = bitc::CST_CODE_DATA; 1682 Type *EltTy = CDS->getType()->getElementType(); 1683 if (isa<IntegerType>(EltTy)) { 1684 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 1685 Record.push_back(CDS->getElementAsInteger(i)); 1686 } else { 1687 for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) 1688 Record.push_back( 1689 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue()); 1690 } 1691 } else if (isa<ConstantArray>(C) || isa<ConstantStruct>(C) || 1692 isa<ConstantVector>(C)) { 1693 Code = bitc::CST_CODE_AGGREGATE; 1694 for (const Value *Op : C->operands()) 1695 Record.push_back(VE.getValueID(Op)); 1696 AbbrevToUse = AggregateAbbrev; 1697 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 1698 switch (CE->getOpcode()) { 1699 default: 1700 if (Instruction::isCast(CE->getOpcode())) { 1701 Code = bitc::CST_CODE_CE_CAST; 1702 Record.push_back(GetEncodedCastOpcode(CE->getOpcode())); 1703 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1704 Record.push_back(VE.getValueID(C->getOperand(0))); 1705 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 1706 } else { 1707 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 1708 Code = bitc::CST_CODE_CE_BINOP; 1709 Record.push_back(GetEncodedBinaryOpcode(CE->getOpcode())); 1710 Record.push_back(VE.getValueID(C->getOperand(0))); 1711 Record.push_back(VE.getValueID(C->getOperand(1))); 1712 uint64_t Flags = GetOptimizationFlags(CE); 1713 if (Flags != 0) 1714 Record.push_back(Flags); 1715 } 1716 break; 1717 case Instruction::GetElementPtr: { 1718 Code = bitc::CST_CODE_CE_GEP; 1719 const auto *GO = cast<GEPOperator>(C); 1720 if (GO->isInBounds()) 1721 Code = bitc::CST_CODE_CE_INBOUNDS_GEP; 1722 Record.push_back(VE.getTypeID(GO->getSourceElementType())); 1723 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 1724 Record.push_back(VE.getTypeID(C->getOperand(i)->getType())); 1725 Record.push_back(VE.getValueID(C->getOperand(i))); 1726 } 1727 break; 1728 } 1729 case Instruction::Select: 1730 Code = bitc::CST_CODE_CE_SELECT; 1731 Record.push_back(VE.getValueID(C->getOperand(0))); 1732 Record.push_back(VE.getValueID(C->getOperand(1))); 1733 Record.push_back(VE.getValueID(C->getOperand(2))); 1734 break; 1735 case Instruction::ExtractElement: 1736 Code = bitc::CST_CODE_CE_EXTRACTELT; 1737 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1738 Record.push_back(VE.getValueID(C->getOperand(0))); 1739 Record.push_back(VE.getTypeID(C->getOperand(1)->getType())); 1740 Record.push_back(VE.getValueID(C->getOperand(1))); 1741 break; 1742 case Instruction::InsertElement: 1743 Code = bitc::CST_CODE_CE_INSERTELT; 1744 Record.push_back(VE.getValueID(C->getOperand(0))); 1745 Record.push_back(VE.getValueID(C->getOperand(1))); 1746 Record.push_back(VE.getTypeID(C->getOperand(2)->getType())); 1747 Record.push_back(VE.getValueID(C->getOperand(2))); 1748 break; 1749 case Instruction::ShuffleVector: 1750 // If the return type and argument types are the same, this is a 1751 // standard shufflevector instruction. If the types are different, 1752 // then the shuffle is widening or truncating the input vectors, and 1753 // the argument type must also be encoded. 1754 if (C->getType() == C->getOperand(0)->getType()) { 1755 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 1756 } else { 1757 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 1758 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1759 } 1760 Record.push_back(VE.getValueID(C->getOperand(0))); 1761 Record.push_back(VE.getValueID(C->getOperand(1))); 1762 Record.push_back(VE.getValueID(C->getOperand(2))); 1763 break; 1764 case Instruction::ICmp: 1765 case Instruction::FCmp: 1766 Code = bitc::CST_CODE_CE_CMP; 1767 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 1768 Record.push_back(VE.getValueID(C->getOperand(0))); 1769 Record.push_back(VE.getValueID(C->getOperand(1))); 1770 Record.push_back(CE->getPredicate()); 1771 break; 1772 } 1773 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 1774 Code = bitc::CST_CODE_BLOCKADDRESS; 1775 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 1776 Record.push_back(VE.getValueID(BA->getFunction())); 1777 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 1778 } else { 1779 #ifndef NDEBUG 1780 C->dump(); 1781 #endif 1782 llvm_unreachable("Unknown constant!"); 1783 } 1784 Stream.EmitRecord(Code, Record, AbbrevToUse); 1785 Record.clear(); 1786 } 1787 1788 Stream.ExitBlock(); 1789 } 1790 1791 static void WriteModuleConstants(const ValueEnumerator &VE, 1792 BitstreamWriter &Stream) { 1793 const ValueEnumerator::ValueList &Vals = VE.getValues(); 1794 1795 // Find the first constant to emit, which is the first non-globalvalue value. 1796 // We know globalvalues have been emitted by WriteModuleInfo. 1797 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 1798 if (!isa<GlobalValue>(Vals[i].first)) { 1799 WriteConstants(i, Vals.size(), VE, Stream, true); 1800 return; 1801 } 1802 } 1803 } 1804 1805 /// PushValueAndType - The file has to encode both the value and type id for 1806 /// many values, because we need to know what type to create for forward 1807 /// references. However, most operands are not forward references, so this type 1808 /// field is not needed. 1809 /// 1810 /// This function adds V's value ID to Vals. If the value ID is higher than the 1811 /// instruction ID, then it is a forward reference, and it also includes the 1812 /// type ID. The value ID that is written is encoded relative to the InstID. 1813 static bool PushValueAndType(const Value *V, unsigned InstID, 1814 SmallVectorImpl<unsigned> &Vals, 1815 ValueEnumerator &VE) { 1816 unsigned ValID = VE.getValueID(V); 1817 // Make encoding relative to the InstID. 1818 Vals.push_back(InstID - ValID); 1819 if (ValID >= InstID) { 1820 Vals.push_back(VE.getTypeID(V->getType())); 1821 return true; 1822 } 1823 return false; 1824 } 1825 1826 static void WriteOperandBundles(BitstreamWriter &Stream, ImmutableCallSite CS, 1827 unsigned InstID, ValueEnumerator &VE) { 1828 SmallVector<unsigned, 64> Record; 1829 LLVMContext &C = CS.getInstruction()->getContext(); 1830 1831 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) { 1832 const auto &Bundle = CS.getOperandBundleAt(i); 1833 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName())); 1834 1835 for (auto &Input : Bundle.Inputs) 1836 PushValueAndType(Input, InstID, Record, VE); 1837 1838 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record); 1839 Record.clear(); 1840 } 1841 } 1842 1843 /// pushValue - Like PushValueAndType, but where the type of the value is 1844 /// omitted (perhaps it was already encoded in an earlier operand). 1845 static void pushValue(const Value *V, unsigned InstID, 1846 SmallVectorImpl<unsigned> &Vals, 1847 ValueEnumerator &VE) { 1848 unsigned ValID = VE.getValueID(V); 1849 Vals.push_back(InstID - ValID); 1850 } 1851 1852 static void pushValueSigned(const Value *V, unsigned InstID, 1853 SmallVectorImpl<uint64_t> &Vals, 1854 ValueEnumerator &VE) { 1855 unsigned ValID = VE.getValueID(V); 1856 int64_t diff = ((int32_t)InstID - (int32_t)ValID); 1857 emitSignedInt64(Vals, diff); 1858 } 1859 1860 /// WriteInstruction - Emit an instruction to the specified stream. 1861 static void WriteInstruction(const Instruction &I, unsigned InstID, 1862 ValueEnumerator &VE, BitstreamWriter &Stream, 1863 SmallVectorImpl<unsigned> &Vals) { 1864 unsigned Code = 0; 1865 unsigned AbbrevToUse = 0; 1866 VE.setInstructionID(&I); 1867 switch (I.getOpcode()) { 1868 default: 1869 if (Instruction::isCast(I.getOpcode())) { 1870 Code = bitc::FUNC_CODE_INST_CAST; 1871 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1872 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 1873 Vals.push_back(VE.getTypeID(I.getType())); 1874 Vals.push_back(GetEncodedCastOpcode(I.getOpcode())); 1875 } else { 1876 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 1877 Code = bitc::FUNC_CODE_INST_BINOP; 1878 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1879 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 1880 pushValue(I.getOperand(1), InstID, Vals, VE); 1881 Vals.push_back(GetEncodedBinaryOpcode(I.getOpcode())); 1882 uint64_t Flags = GetOptimizationFlags(&I); 1883 if (Flags != 0) { 1884 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 1885 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 1886 Vals.push_back(Flags); 1887 } 1888 } 1889 break; 1890 1891 case Instruction::GetElementPtr: { 1892 Code = bitc::FUNC_CODE_INST_GEP; 1893 AbbrevToUse = FUNCTION_INST_GEP_ABBREV; 1894 auto &GEPInst = cast<GetElementPtrInst>(I); 1895 Vals.push_back(GEPInst.isInBounds()); 1896 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType())); 1897 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 1898 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1899 break; 1900 } 1901 case Instruction::ExtractValue: { 1902 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 1903 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1904 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 1905 Vals.append(EVI->idx_begin(), EVI->idx_end()); 1906 break; 1907 } 1908 case Instruction::InsertValue: { 1909 Code = bitc::FUNC_CODE_INST_INSERTVAL; 1910 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1911 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1912 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 1913 Vals.append(IVI->idx_begin(), IVI->idx_end()); 1914 break; 1915 } 1916 case Instruction::Select: 1917 Code = bitc::FUNC_CODE_INST_VSELECT; 1918 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1919 pushValue(I.getOperand(2), InstID, Vals, VE); 1920 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1921 break; 1922 case Instruction::ExtractElement: 1923 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 1924 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1925 PushValueAndType(I.getOperand(1), InstID, Vals, VE); 1926 break; 1927 case Instruction::InsertElement: 1928 Code = bitc::FUNC_CODE_INST_INSERTELT; 1929 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1930 pushValue(I.getOperand(1), InstID, Vals, VE); 1931 PushValueAndType(I.getOperand(2), InstID, Vals, VE); 1932 break; 1933 case Instruction::ShuffleVector: 1934 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 1935 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1936 pushValue(I.getOperand(1), InstID, Vals, VE); 1937 pushValue(I.getOperand(2), InstID, Vals, VE); 1938 break; 1939 case Instruction::ICmp: 1940 case Instruction::FCmp: { 1941 // compare returning Int1Ty or vector of Int1Ty 1942 Code = bitc::FUNC_CODE_INST_CMP2; 1943 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 1944 pushValue(I.getOperand(1), InstID, Vals, VE); 1945 Vals.push_back(cast<CmpInst>(I).getPredicate()); 1946 uint64_t Flags = GetOptimizationFlags(&I); 1947 if (Flags != 0) 1948 Vals.push_back(Flags); 1949 break; 1950 } 1951 1952 case Instruction::Ret: 1953 { 1954 Code = bitc::FUNC_CODE_INST_RET; 1955 unsigned NumOperands = I.getNumOperands(); 1956 if (NumOperands == 0) 1957 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 1958 else if (NumOperands == 1) { 1959 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) 1960 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 1961 } else { 1962 for (unsigned i = 0, e = NumOperands; i != e; ++i) 1963 PushValueAndType(I.getOperand(i), InstID, Vals, VE); 1964 } 1965 } 1966 break; 1967 case Instruction::Br: 1968 { 1969 Code = bitc::FUNC_CODE_INST_BR; 1970 const BranchInst &II = cast<BranchInst>(I); 1971 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 1972 if (II.isConditional()) { 1973 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 1974 pushValue(II.getCondition(), InstID, Vals, VE); 1975 } 1976 } 1977 break; 1978 case Instruction::Switch: 1979 { 1980 Code = bitc::FUNC_CODE_INST_SWITCH; 1981 const SwitchInst &SI = cast<SwitchInst>(I); 1982 Vals.push_back(VE.getTypeID(SI.getCondition()->getType())); 1983 pushValue(SI.getCondition(), InstID, Vals, VE); 1984 Vals.push_back(VE.getValueID(SI.getDefaultDest())); 1985 for (SwitchInst::ConstCaseIt Case : SI.cases()) { 1986 Vals.push_back(VE.getValueID(Case.getCaseValue())); 1987 Vals.push_back(VE.getValueID(Case.getCaseSuccessor())); 1988 } 1989 } 1990 break; 1991 case Instruction::IndirectBr: 1992 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 1993 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 1994 // Encode the address operand as relative, but not the basic blocks. 1995 pushValue(I.getOperand(0), InstID, Vals, VE); 1996 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) 1997 Vals.push_back(VE.getValueID(I.getOperand(i))); 1998 break; 1999 2000 case Instruction::Invoke: { 2001 const InvokeInst *II = cast<InvokeInst>(&I); 2002 const Value *Callee = II->getCalledValue(); 2003 FunctionType *FTy = II->getFunctionType(); 2004 2005 if (II->hasOperandBundles()) 2006 WriteOperandBundles(Stream, II, InstID, VE); 2007 2008 Code = bitc::FUNC_CODE_INST_INVOKE; 2009 2010 Vals.push_back(VE.getAttributeID(II->getAttributes())); 2011 Vals.push_back(II->getCallingConv() | 1 << 13); 2012 Vals.push_back(VE.getValueID(II->getNormalDest())); 2013 Vals.push_back(VE.getValueID(II->getUnwindDest())); 2014 Vals.push_back(VE.getTypeID(FTy)); 2015 PushValueAndType(Callee, InstID, Vals, VE); 2016 2017 // Emit value #'s for the fixed parameters. 2018 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 2019 pushValue(I.getOperand(i), InstID, Vals, VE); // fixed param. 2020 2021 // Emit type/value pairs for varargs params. 2022 if (FTy->isVarArg()) { 2023 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-3; 2024 i != e; ++i) 2025 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // vararg 2026 } 2027 break; 2028 } 2029 case Instruction::Resume: 2030 Code = bitc::FUNC_CODE_INST_RESUME; 2031 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 2032 break; 2033 case Instruction::CleanupRet: { 2034 Code = bitc::FUNC_CODE_INST_CLEANUPRET; 2035 const auto &CRI = cast<CleanupReturnInst>(I); 2036 pushValue(CRI.getCleanupPad(), InstID, Vals, VE); 2037 if (CRI.hasUnwindDest()) 2038 Vals.push_back(VE.getValueID(CRI.getUnwindDest())); 2039 break; 2040 } 2041 case Instruction::CatchRet: { 2042 Code = bitc::FUNC_CODE_INST_CATCHRET; 2043 const auto &CRI = cast<CatchReturnInst>(I); 2044 pushValue(CRI.getCatchPad(), InstID, Vals, VE); 2045 Vals.push_back(VE.getValueID(CRI.getSuccessor())); 2046 break; 2047 } 2048 case Instruction::CleanupPad: 2049 case Instruction::CatchPad: { 2050 const auto &FuncletPad = cast<FuncletPadInst>(I); 2051 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD 2052 : bitc::FUNC_CODE_INST_CLEANUPPAD; 2053 pushValue(FuncletPad.getParentPad(), InstID, Vals, VE); 2054 2055 unsigned NumArgOperands = FuncletPad.getNumArgOperands(); 2056 Vals.push_back(NumArgOperands); 2057 for (unsigned Op = 0; Op != NumArgOperands; ++Op) 2058 PushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals, VE); 2059 break; 2060 } 2061 case Instruction::CatchSwitch: { 2062 Code = bitc::FUNC_CODE_INST_CATCHSWITCH; 2063 const auto &CatchSwitch = cast<CatchSwitchInst>(I); 2064 2065 pushValue(CatchSwitch.getParentPad(), InstID, Vals, VE); 2066 2067 unsigned NumHandlers = CatchSwitch.getNumHandlers(); 2068 Vals.push_back(NumHandlers); 2069 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers()) 2070 Vals.push_back(VE.getValueID(CatchPadBB)); 2071 2072 if (CatchSwitch.hasUnwindDest()) 2073 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest())); 2074 break; 2075 } 2076 case Instruction::Unreachable: 2077 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 2078 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 2079 break; 2080 2081 case Instruction::PHI: { 2082 const PHINode &PN = cast<PHINode>(I); 2083 Code = bitc::FUNC_CODE_INST_PHI; 2084 // With the newer instruction encoding, forward references could give 2085 // negative valued IDs. This is most common for PHIs, so we use 2086 // signed VBRs. 2087 SmallVector<uint64_t, 128> Vals64; 2088 Vals64.push_back(VE.getTypeID(PN.getType())); 2089 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 2090 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64, VE); 2091 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i))); 2092 } 2093 // Emit a Vals64 vector and exit. 2094 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 2095 Vals64.clear(); 2096 return; 2097 } 2098 2099 case Instruction::LandingPad: { 2100 const LandingPadInst &LP = cast<LandingPadInst>(I); 2101 Code = bitc::FUNC_CODE_INST_LANDINGPAD; 2102 Vals.push_back(VE.getTypeID(LP.getType())); 2103 Vals.push_back(LP.isCleanup()); 2104 Vals.push_back(LP.getNumClauses()); 2105 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { 2106 if (LP.isCatch(I)) 2107 Vals.push_back(LandingPadInst::Catch); 2108 else 2109 Vals.push_back(LandingPadInst::Filter); 2110 PushValueAndType(LP.getClause(I), InstID, Vals, VE); 2111 } 2112 break; 2113 } 2114 2115 case Instruction::Alloca: { 2116 Code = bitc::FUNC_CODE_INST_ALLOCA; 2117 const AllocaInst &AI = cast<AllocaInst>(I); 2118 Vals.push_back(VE.getTypeID(AI.getAllocatedType())); 2119 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 2120 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 2121 unsigned AlignRecord = Log2_32(AI.getAlignment()) + 1; 2122 assert(Log2_32(Value::MaximumAlignment) + 1 < 1 << 5 && 2123 "not enough bits for maximum alignment"); 2124 assert(AlignRecord < 1 << 5 && "alignment greater than 1 << 64"); 2125 AlignRecord |= AI.isUsedWithInAlloca() << 5; 2126 AlignRecord |= 1 << 6; 2127 // Reserve bit 7 for SwiftError flag. 2128 // AlignRecord |= AI.isSwiftError() << 7; 2129 Vals.push_back(AlignRecord); 2130 break; 2131 } 2132 2133 case Instruction::Load: 2134 if (cast<LoadInst>(I).isAtomic()) { 2135 Code = bitc::FUNC_CODE_INST_LOADATOMIC; 2136 PushValueAndType(I.getOperand(0), InstID, Vals, VE); 2137 } else { 2138 Code = bitc::FUNC_CODE_INST_LOAD; 2139 if (!PushValueAndType(I.getOperand(0), InstID, Vals, VE)) // ptr 2140 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 2141 } 2142 Vals.push_back(VE.getTypeID(I.getType())); 2143 Vals.push_back(Log2_32(cast<LoadInst>(I).getAlignment())+1); 2144 Vals.push_back(cast<LoadInst>(I).isVolatile()); 2145 if (cast<LoadInst>(I).isAtomic()) { 2146 Vals.push_back(GetEncodedOrdering(cast<LoadInst>(I).getOrdering())); 2147 Vals.push_back(GetEncodedSynchScope(cast<LoadInst>(I).getSynchScope())); 2148 } 2149 break; 2150 case Instruction::Store: 2151 if (cast<StoreInst>(I).isAtomic()) 2152 Code = bitc::FUNC_CODE_INST_STOREATOMIC; 2153 else 2154 Code = bitc::FUNC_CODE_INST_STORE; 2155 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // ptrty + ptr 2156 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // valty + val 2157 Vals.push_back(Log2_32(cast<StoreInst>(I).getAlignment())+1); 2158 Vals.push_back(cast<StoreInst>(I).isVolatile()); 2159 if (cast<StoreInst>(I).isAtomic()) { 2160 Vals.push_back(GetEncodedOrdering(cast<StoreInst>(I).getOrdering())); 2161 Vals.push_back(GetEncodedSynchScope(cast<StoreInst>(I).getSynchScope())); 2162 } 2163 break; 2164 case Instruction::AtomicCmpXchg: 2165 Code = bitc::FUNC_CODE_INST_CMPXCHG; 2166 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 2167 PushValueAndType(I.getOperand(1), InstID, Vals, VE); // cmp. 2168 pushValue(I.getOperand(2), InstID, Vals, VE); // newval. 2169 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); 2170 Vals.push_back(GetEncodedOrdering( 2171 cast<AtomicCmpXchgInst>(I).getSuccessOrdering())); 2172 Vals.push_back(GetEncodedSynchScope( 2173 cast<AtomicCmpXchgInst>(I).getSynchScope())); 2174 Vals.push_back(GetEncodedOrdering( 2175 cast<AtomicCmpXchgInst>(I).getFailureOrdering())); 2176 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak()); 2177 break; 2178 case Instruction::AtomicRMW: 2179 Code = bitc::FUNC_CODE_INST_ATOMICRMW; 2180 PushValueAndType(I.getOperand(0), InstID, Vals, VE); // ptrty + ptr 2181 pushValue(I.getOperand(1), InstID, Vals, VE); // val. 2182 Vals.push_back(GetEncodedRMWOperation( 2183 cast<AtomicRMWInst>(I).getOperation())); 2184 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); 2185 Vals.push_back(GetEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); 2186 Vals.push_back(GetEncodedSynchScope( 2187 cast<AtomicRMWInst>(I).getSynchScope())); 2188 break; 2189 case Instruction::Fence: 2190 Code = bitc::FUNC_CODE_INST_FENCE; 2191 Vals.push_back(GetEncodedOrdering(cast<FenceInst>(I).getOrdering())); 2192 Vals.push_back(GetEncodedSynchScope(cast<FenceInst>(I).getSynchScope())); 2193 break; 2194 case Instruction::Call: { 2195 const CallInst &CI = cast<CallInst>(I); 2196 FunctionType *FTy = CI.getFunctionType(); 2197 2198 if (CI.hasOperandBundles()) 2199 WriteOperandBundles(Stream, &CI, InstID, VE); 2200 2201 Code = bitc::FUNC_CODE_INST_CALL; 2202 2203 Vals.push_back(VE.getAttributeID(CI.getAttributes())); 2204 2205 unsigned Flags = GetOptimizationFlags(&I); 2206 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV | 2207 unsigned(CI.isTailCall()) << bitc::CALL_TAIL | 2208 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL | 2209 1 << bitc::CALL_EXPLICIT_TYPE | 2210 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL | 2211 unsigned(Flags != 0) << bitc::CALL_FMF); 2212 if (Flags != 0) 2213 Vals.push_back(Flags); 2214 2215 Vals.push_back(VE.getTypeID(FTy)); 2216 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee 2217 2218 // Emit value #'s for the fixed parameters. 2219 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) { 2220 // Check for labels (can happen with asm labels). 2221 if (FTy->getParamType(i)->isLabelTy()) 2222 Vals.push_back(VE.getValueID(CI.getArgOperand(i))); 2223 else 2224 pushValue(CI.getArgOperand(i), InstID, Vals, VE); // fixed param. 2225 } 2226 2227 // Emit type/value pairs for varargs params. 2228 if (FTy->isVarArg()) { 2229 for (unsigned i = FTy->getNumParams(), e = CI.getNumArgOperands(); 2230 i != e; ++i) 2231 PushValueAndType(CI.getArgOperand(i), InstID, Vals, VE); // varargs 2232 } 2233 break; 2234 } 2235 case Instruction::VAArg: 2236 Code = bitc::FUNC_CODE_INST_VAARG; 2237 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 2238 pushValue(I.getOperand(0), InstID, Vals, VE); // valist. 2239 Vals.push_back(VE.getTypeID(I.getType())); // restype. 2240 break; 2241 } 2242 2243 Stream.EmitRecord(Code, Vals, AbbrevToUse); 2244 Vals.clear(); 2245 } 2246 2247 /// Emit names for globals/functions etc. The VSTOffsetPlaceholder, 2248 /// BitcodeStartBit and FunctionIndex are only passed for the module-level 2249 /// VST, where we are including a function bitcode index and need to 2250 /// backpatch the VST forward declaration record. 2251 static void WriteValueSymbolTable( 2252 const ValueSymbolTable &VST, const ValueEnumerator &VE, 2253 BitstreamWriter &Stream, uint64_t VSTOffsetPlaceholder = 0, 2254 uint64_t BitcodeStartBit = 0, 2255 DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> 2256 *FunctionIndex = nullptr) { 2257 if (VST.empty()) { 2258 // WriteValueSymbolTableForwardDecl should have returned early as 2259 // well. Ensure this handling remains in sync by asserting that 2260 // the placeholder offset is not set. 2261 assert(VSTOffsetPlaceholder == 0); 2262 return; 2263 } 2264 2265 if (VSTOffsetPlaceholder > 0) { 2266 // Get the offset of the VST we are writing, and backpatch it into 2267 // the VST forward declaration record. 2268 uint64_t VSTOffset = Stream.GetCurrentBitNo(); 2269 // The BitcodeStartBit was the stream offset of the actual bitcode 2270 // (e.g. excluding any initial darwin header). 2271 VSTOffset -= BitcodeStartBit; 2272 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned"); 2273 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32); 2274 } 2275 2276 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 2277 2278 // For the module-level VST, add abbrev Ids for the VST_CODE_FNENTRY 2279 // records, which are not used in the per-function VSTs. 2280 unsigned FnEntry8BitAbbrev; 2281 unsigned FnEntry7BitAbbrev; 2282 unsigned FnEntry6BitAbbrev; 2283 if (VSTOffsetPlaceholder > 0) { 2284 // 8-bit fixed-width VST_CODE_FNENTRY function strings. 2285 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2286 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY)); 2287 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 2288 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset 2289 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2290 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 2291 FnEntry8BitAbbrev = Stream.EmitAbbrev(Abbv); 2292 2293 // 7-bit fixed width VST_CODE_FNENTRY function strings. 2294 Abbv = new BitCodeAbbrev(); 2295 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY)); 2296 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 2297 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset 2298 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2299 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 2300 FnEntry7BitAbbrev = Stream.EmitAbbrev(Abbv); 2301 2302 // 6-bit char6 VST_CODE_FNENTRY function strings. 2303 Abbv = new BitCodeAbbrev(); 2304 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY)); 2305 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 2306 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset 2307 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2308 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 2309 FnEntry6BitAbbrev = Stream.EmitAbbrev(Abbv); 2310 } 2311 2312 // FIXME: Set up the abbrev, we know how many values there are! 2313 // FIXME: We know if the type names can use 7-bit ascii. 2314 SmallVector<unsigned, 64> NameVals; 2315 2316 for (const ValueName &Name : VST) { 2317 // Figure out the encoding to use for the name. 2318 StringEncoding Bits = 2319 getStringEncoding(Name.getKeyData(), Name.getKeyLength()); 2320 2321 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 2322 NameVals.push_back(VE.getValueID(Name.getValue())); 2323 2324 Function *F = dyn_cast<Function>(Name.getValue()); 2325 if (!F) { 2326 // If value is an alias, need to get the aliased base object to 2327 // see if it is a function. 2328 auto *GA = dyn_cast<GlobalAlias>(Name.getValue()); 2329 if (GA && GA->getBaseObject()) 2330 F = dyn_cast<Function>(GA->getBaseObject()); 2331 } 2332 2333 // VST_CODE_ENTRY: [valueid, namechar x N] 2334 // VST_CODE_FNENTRY: [valueid, funcoffset, namechar x N] 2335 // VST_CODE_BBENTRY: [bbid, namechar x N] 2336 unsigned Code; 2337 if (isa<BasicBlock>(Name.getValue())) { 2338 Code = bitc::VST_CODE_BBENTRY; 2339 if (Bits == SE_Char6) 2340 AbbrevToUse = VST_BBENTRY_6_ABBREV; 2341 } else if (F && !F->isDeclaration()) { 2342 // Must be the module-level VST, where we pass in the Index and 2343 // have a VSTOffsetPlaceholder. The function-level VST should not 2344 // contain any Function symbols. 2345 assert(FunctionIndex); 2346 assert(VSTOffsetPlaceholder > 0); 2347 2348 // Save the word offset of the function (from the start of the 2349 // actual bitcode written to the stream). 2350 assert(FunctionIndex->count(F) == 1); 2351 uint64_t BitcodeIndex = 2352 (*FunctionIndex)[F]->bitcodeIndex() - BitcodeStartBit; 2353 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned"); 2354 NameVals.push_back(BitcodeIndex / 32); 2355 2356 Code = bitc::VST_CODE_FNENTRY; 2357 AbbrevToUse = FnEntry8BitAbbrev; 2358 if (Bits == SE_Char6) 2359 AbbrevToUse = FnEntry6BitAbbrev; 2360 else if (Bits == SE_Fixed7) 2361 AbbrevToUse = FnEntry7BitAbbrev; 2362 } else { 2363 Code = bitc::VST_CODE_ENTRY; 2364 if (Bits == SE_Char6) 2365 AbbrevToUse = VST_ENTRY_6_ABBREV; 2366 else if (Bits == SE_Fixed7) 2367 AbbrevToUse = VST_ENTRY_7_ABBREV; 2368 } 2369 2370 for (const auto P : Name.getKey()) 2371 NameVals.push_back((unsigned char)P); 2372 2373 // Emit the finished record. 2374 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 2375 NameVals.clear(); 2376 } 2377 Stream.ExitBlock(); 2378 } 2379 2380 /// Emit function names and summary offsets for the combined index 2381 /// used by ThinLTO. 2382 static void 2383 WriteCombinedValueSymbolTable(const FunctionInfoIndex &Index, 2384 BitstreamWriter &Stream, 2385 std::map<uint64_t, unsigned> &GUIDToValueIdMap, 2386 uint64_t VSTOffsetPlaceholder) { 2387 assert(VSTOffsetPlaceholder > 0 && "Expected non-zero VSTOffsetPlaceholder"); 2388 // Get the offset of the VST we are writing, and backpatch it into 2389 // the VST forward declaration record. 2390 uint64_t VSTOffset = Stream.GetCurrentBitNo(); 2391 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned"); 2392 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32); 2393 2394 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 2395 2396 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2397 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_GVDEFENTRY)); 2398 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 2399 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // sumoffset 2400 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // guid 2401 unsigned DefEntryAbbrev = Stream.EmitAbbrev(Abbv); 2402 2403 Abbv = new BitCodeAbbrev(); 2404 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_COMBINED_ENTRY)); 2405 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 2406 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // refguid 2407 unsigned EntryAbbrev = Stream.EmitAbbrev(Abbv); 2408 2409 SmallVector<uint64_t, 64> NameVals; 2410 2411 for (const auto &FII : Index) { 2412 uint64_t FuncGUID = FII.first; 2413 const auto &VMI = GUIDToValueIdMap.find(FuncGUID); 2414 assert(VMI != GUIDToValueIdMap.end()); 2415 2416 for (const auto &FI : FII.second) { 2417 // VST_CODE_COMBINED_GVDEFENTRY: [valueid, sumoffset, guid] 2418 NameVals.push_back(VMI->second); 2419 NameVals.push_back(FI->bitcodeIndex()); 2420 NameVals.push_back(FuncGUID); 2421 2422 // Emit the finished record. 2423 Stream.EmitRecord(bitc::VST_CODE_COMBINED_GVDEFENTRY, NameVals, 2424 DefEntryAbbrev); 2425 NameVals.clear(); 2426 } 2427 GUIDToValueIdMap.erase(VMI); 2428 } 2429 for (const auto &GVI : GUIDToValueIdMap) { 2430 // VST_CODE_COMBINED_ENTRY: [valueid, refguid] 2431 NameVals.push_back(GVI.second); 2432 NameVals.push_back(GVI.first); 2433 2434 // Emit the finished record. 2435 Stream.EmitRecord(bitc::VST_CODE_COMBINED_ENTRY, NameVals, EntryAbbrev); 2436 NameVals.clear(); 2437 } 2438 Stream.ExitBlock(); 2439 } 2440 2441 static void WriteUseList(ValueEnumerator &VE, UseListOrder &&Order, 2442 BitstreamWriter &Stream) { 2443 assert(Order.Shuffle.size() >= 2 && "Shuffle too small"); 2444 unsigned Code; 2445 if (isa<BasicBlock>(Order.V)) 2446 Code = bitc::USELIST_CODE_BB; 2447 else 2448 Code = bitc::USELIST_CODE_DEFAULT; 2449 2450 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end()); 2451 Record.push_back(VE.getValueID(Order.V)); 2452 Stream.EmitRecord(Code, Record); 2453 } 2454 2455 static void WriteUseListBlock(const Function *F, ValueEnumerator &VE, 2456 BitstreamWriter &Stream) { 2457 assert(VE.shouldPreserveUseListOrder() && 2458 "Expected to be preserving use-list order"); 2459 2460 auto hasMore = [&]() { 2461 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F; 2462 }; 2463 if (!hasMore()) 2464 // Nothing to do. 2465 return; 2466 2467 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3); 2468 while (hasMore()) { 2469 WriteUseList(VE, std::move(VE.UseListOrders.back()), Stream); 2470 VE.UseListOrders.pop_back(); 2471 } 2472 Stream.ExitBlock(); 2473 } 2474 2475 // Walk through the operands of a given User via worklist iteration and populate 2476 // the set of GlobalValue references encountered. Invoked either on an 2477 // Instruction or a GlobalVariable (which walks its initializer). 2478 static void findRefEdges(const User *CurUser, const ValueEnumerator &VE, 2479 DenseSet<unsigned> &RefEdges, 2480 SmallPtrSet<const User *, 8> &Visited) { 2481 SmallVector<const User *, 32> Worklist; 2482 Worklist.push_back(CurUser); 2483 2484 while (!Worklist.empty()) { 2485 const User *U = Worklist.pop_back_val(); 2486 2487 if (!Visited.insert(U).second) 2488 continue; 2489 2490 ImmutableCallSite CS(U); 2491 2492 for (const auto &OI : U->operands()) { 2493 const User *Operand = dyn_cast<User>(OI); 2494 if (!Operand) 2495 continue; 2496 if (isa<BlockAddress>(Operand)) 2497 continue; 2498 if (isa<GlobalValue>(Operand)) { 2499 // We have a reference to a global value. This should be added to 2500 // the reference set unless it is a callee. Callees are handled 2501 // specially by WriteFunction and are added to a separate list. 2502 if (!(CS && CS.isCallee(&OI))) 2503 RefEdges.insert(VE.getValueID(Operand)); 2504 continue; 2505 } 2506 Worklist.push_back(Operand); 2507 } 2508 } 2509 } 2510 2511 /// Emit a function body to the module stream. 2512 static void WriteFunction( 2513 const Function &F, const Module *M, ValueEnumerator &VE, 2514 BitstreamWriter &Stream, 2515 DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> &FunctionIndex, 2516 bool EmitSummaryIndex) { 2517 // Save the bitcode index of the start of this function block for recording 2518 // in the VST. 2519 uint64_t BitcodeIndex = Stream.GetCurrentBitNo(); 2520 2521 bool HasProfileData = F.getEntryCount().hasValue(); 2522 std::unique_ptr<BlockFrequencyInfo> BFI; 2523 if (EmitSummaryIndex && HasProfileData) { 2524 Function &Func = const_cast<Function &>(F); 2525 LoopInfo LI{DominatorTree(Func)}; 2526 BranchProbabilityInfo BPI{Func, LI}; 2527 BFI = llvm::make_unique<BlockFrequencyInfo>(Func, BPI, LI); 2528 } 2529 2530 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 4); 2531 VE.incorporateFunction(F); 2532 2533 SmallVector<unsigned, 64> Vals; 2534 2535 // Emit the number of basic blocks, so the reader can create them ahead of 2536 // time. 2537 Vals.push_back(VE.getBasicBlocks().size()); 2538 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 2539 Vals.clear(); 2540 2541 // If there are function-local constants, emit them now. 2542 unsigned CstStart, CstEnd; 2543 VE.getFunctionConstantRange(CstStart, CstEnd); 2544 WriteConstants(CstStart, CstEnd, VE, Stream, false); 2545 2546 // If there is function-local metadata, emit it now. 2547 WriteFunctionLocalMetadata(F, VE, Stream); 2548 2549 // Keep a running idea of what the instruction ID is. 2550 unsigned InstID = CstEnd; 2551 2552 bool NeedsMetadataAttachment = F.hasMetadata(); 2553 2554 DILocation *LastDL = nullptr; 2555 unsigned NumInsts = 0; 2556 // Map from callee ValueId to profile count. Used to accumulate profile 2557 // counts for all static calls to a given callee. 2558 DenseMap<unsigned, CalleeInfo> CallGraphEdges; 2559 DenseSet<unsigned> RefEdges; 2560 2561 SmallPtrSet<const User *, 8> Visited; 2562 // Finally, emit all the instructions, in order. 2563 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 2564 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 2565 I != E; ++I) { 2566 WriteInstruction(*I, InstID, VE, Stream, Vals); 2567 2568 if (!isa<DbgInfoIntrinsic>(I)) 2569 ++NumInsts; 2570 2571 if (!I->getType()->isVoidTy()) 2572 ++InstID; 2573 2574 if (EmitSummaryIndex) { 2575 if (auto CS = ImmutableCallSite(&*I)) { 2576 auto *CalledFunction = CS.getCalledFunction(); 2577 if (CalledFunction && CalledFunction->hasName() && 2578 !CalledFunction->isIntrinsic()) { 2579 uint64_t ScaledCount = 0; 2580 if (HasProfileData) 2581 ScaledCount = getBlockProfileCount( 2582 BFI->getBlockFreq(&(*BB)).getFrequency(), BFI->getEntryFreq(), 2583 F.getEntryCount().getValue()); 2584 unsigned CalleeId = VE.getValueID( 2585 M->getValueSymbolTable().lookup(CalledFunction->getName())); 2586 CallGraphEdges[CalleeId] += ScaledCount; 2587 } 2588 } 2589 findRefEdges(&*I, VE, RefEdges, Visited); 2590 } 2591 2592 // If the instruction has metadata, write a metadata attachment later. 2593 NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc(); 2594 2595 // If the instruction has a debug location, emit it. 2596 DILocation *DL = I->getDebugLoc(); 2597 if (!DL) 2598 continue; 2599 2600 if (DL == LastDL) { 2601 // Just repeat the same debug loc as last time. 2602 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 2603 continue; 2604 } 2605 2606 Vals.push_back(DL->getLine()); 2607 Vals.push_back(DL->getColumn()); 2608 Vals.push_back(VE.getMetadataOrNullID(DL->getScope())); 2609 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt())); 2610 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals); 2611 Vals.clear(); 2612 2613 LastDL = DL; 2614 } 2615 2616 std::unique_ptr<FunctionSummary> FuncSummary; 2617 if (EmitSummaryIndex) { 2618 FuncSummary = llvm::make_unique<FunctionSummary>(F.getLinkage(), NumInsts); 2619 FuncSummary->addCallGraphEdges(CallGraphEdges); 2620 FuncSummary->addRefEdges(RefEdges); 2621 } 2622 FunctionIndex[&F] = 2623 llvm::make_unique<GlobalValueInfo>(BitcodeIndex, std::move(FuncSummary)); 2624 2625 // Emit names for all the instructions etc. 2626 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); 2627 2628 if (NeedsMetadataAttachment) 2629 WriteMetadataAttachment(F, VE, Stream); 2630 if (VE.shouldPreserveUseListOrder()) 2631 WriteUseListBlock(&F, VE, Stream); 2632 VE.purgeFunction(); 2633 Stream.ExitBlock(); 2634 } 2635 2636 // Emit blockinfo, which defines the standard abbreviations etc. 2637 static void WriteBlockInfo(const ValueEnumerator &VE, BitstreamWriter &Stream) { 2638 // We only want to emit block info records for blocks that have multiple 2639 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 2640 // Other blocks can define their abbrevs inline. 2641 Stream.EnterBlockInfoBlock(2); 2642 2643 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings. 2644 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2645 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 2646 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2647 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2648 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 2649 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 2650 Abbv) != VST_ENTRY_8_ABBREV) 2651 llvm_unreachable("Unexpected abbrev ordering!"); 2652 } 2653 2654 { // 7-bit fixed width VST_CODE_ENTRY strings. 2655 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2656 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 2657 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2658 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2659 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 2660 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 2661 Abbv) != VST_ENTRY_7_ABBREV) 2662 llvm_unreachable("Unexpected abbrev ordering!"); 2663 } 2664 { // 6-bit char6 VST_CODE_ENTRY strings. 2665 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2666 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 2667 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2668 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2669 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 2670 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 2671 Abbv) != VST_ENTRY_6_ABBREV) 2672 llvm_unreachable("Unexpected abbrev ordering!"); 2673 } 2674 { // 6-bit char6 VST_CODE_BBENTRY strings. 2675 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2676 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 2677 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2678 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2679 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 2680 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, 2681 Abbv) != VST_BBENTRY_6_ABBREV) 2682 llvm_unreachable("Unexpected abbrev ordering!"); 2683 } 2684 2685 2686 2687 { // SETTYPE abbrev for CONSTANTS_BLOCK. 2688 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2689 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 2690 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2691 VE.computeBitsRequiredForTypeIndicies())); 2692 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 2693 Abbv) != CONSTANTS_SETTYPE_ABBREV) 2694 llvm_unreachable("Unexpected abbrev ordering!"); 2695 } 2696 2697 { // INTEGER abbrev for CONSTANTS_BLOCK. 2698 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2699 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 2700 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2701 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 2702 Abbv) != CONSTANTS_INTEGER_ABBREV) 2703 llvm_unreachable("Unexpected abbrev ordering!"); 2704 } 2705 2706 { // CE_CAST abbrev for CONSTANTS_BLOCK. 2707 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2708 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 2709 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 2710 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 2711 VE.computeBitsRequiredForTypeIndicies())); 2712 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 2713 2714 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 2715 Abbv) != CONSTANTS_CE_CAST_Abbrev) 2716 llvm_unreachable("Unexpected abbrev ordering!"); 2717 } 2718 { // NULL abbrev for CONSTANTS_BLOCK. 2719 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2720 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 2721 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, 2722 Abbv) != CONSTANTS_NULL_Abbrev) 2723 llvm_unreachable("Unexpected abbrev ordering!"); 2724 } 2725 2726 // FIXME: This should only use space for first class types! 2727 2728 { // INST_LOAD abbrev for FUNCTION_BLOCK. 2729 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2730 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 2731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Ptr 2732 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 2733 VE.computeBitsRequiredForTypeIndicies())); 2734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 2735 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 2736 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2737 Abbv) != FUNCTION_INST_LOAD_ABBREV) 2738 llvm_unreachable("Unexpected abbrev ordering!"); 2739 } 2740 { // INST_BINOP abbrev for FUNCTION_BLOCK. 2741 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2742 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 2743 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 2744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 2745 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 2746 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2747 Abbv) != FUNCTION_INST_BINOP_ABBREV) 2748 llvm_unreachable("Unexpected abbrev ordering!"); 2749 } 2750 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 2751 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2752 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 2753 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHS 2754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // RHS 2755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 2756 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // flags 2757 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2758 Abbv) != FUNCTION_INST_BINOP_FLAGS_ABBREV) 2759 llvm_unreachable("Unexpected abbrev ordering!"); 2760 } 2761 { // INST_CAST abbrev for FUNCTION_BLOCK. 2762 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2763 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 2764 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpVal 2765 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 2766 VE.computeBitsRequiredForTypeIndicies())); 2767 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 2768 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2769 Abbv) != FUNCTION_INST_CAST_ABBREV) 2770 llvm_unreachable("Unexpected abbrev ordering!"); 2771 } 2772 2773 { // INST_RET abbrev for FUNCTION_BLOCK. 2774 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2775 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 2776 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2777 Abbv) != FUNCTION_INST_RET_VOID_ABBREV) 2778 llvm_unreachable("Unexpected abbrev ordering!"); 2779 } 2780 { // INST_RET abbrev for FUNCTION_BLOCK. 2781 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2782 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 2783 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ValID 2784 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2785 Abbv) != FUNCTION_INST_RET_VAL_ABBREV) 2786 llvm_unreachable("Unexpected abbrev ordering!"); 2787 } 2788 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 2789 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2790 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 2791 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, 2792 Abbv) != FUNCTION_INST_UNREACHABLE_ABBREV) 2793 llvm_unreachable("Unexpected abbrev ordering!"); 2794 } 2795 { 2796 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2797 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP)); 2798 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 2799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // dest ty 2800 Log2_32_Ceil(VE.getTypes().size() + 1))); 2801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2802 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 2803 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 2804 FUNCTION_INST_GEP_ABBREV) 2805 llvm_unreachable("Unexpected abbrev ordering!"); 2806 } 2807 2808 Stream.ExitBlock(); 2809 } 2810 2811 /// Write the module path strings, currently only used when generating 2812 /// a combined index file. 2813 static void WriteModStrings(const FunctionInfoIndex &I, 2814 BitstreamWriter &Stream) { 2815 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3); 2816 2817 // TODO: See which abbrev sizes we actually need to emit 2818 2819 // 8-bit fixed-width MST_ENTRY strings. 2820 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2821 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 2822 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2823 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2824 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 2825 unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv); 2826 2827 // 7-bit fixed width MST_ENTRY strings. 2828 Abbv = new BitCodeAbbrev(); 2829 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 2830 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2831 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2832 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 2833 unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv); 2834 2835 // 6-bit char6 MST_ENTRY strings. 2836 Abbv = new BitCodeAbbrev(); 2837 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 2838 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2839 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2840 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 2841 unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv); 2842 2843 SmallVector<unsigned, 64> NameVals; 2844 for (const StringMapEntry<uint64_t> &MPSE : I.modPathStringEntries()) { 2845 StringEncoding Bits = 2846 getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size()); 2847 unsigned AbbrevToUse = Abbrev8Bit; 2848 if (Bits == SE_Char6) 2849 AbbrevToUse = Abbrev6Bit; 2850 else if (Bits == SE_Fixed7) 2851 AbbrevToUse = Abbrev7Bit; 2852 2853 NameVals.push_back(MPSE.getValue()); 2854 2855 for (const auto P : MPSE.getKey()) 2856 NameVals.push_back((unsigned char)P); 2857 2858 // Emit the finished record. 2859 Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse); 2860 NameVals.clear(); 2861 } 2862 Stream.ExitBlock(); 2863 } 2864 2865 // Helper to emit a single function summary record. 2866 static void WritePerModuleFunctionSummaryRecord( 2867 SmallVector<uint64_t, 64> &NameVals, FunctionSummary *FS, unsigned ValueID, 2868 unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev, 2869 BitstreamWriter &Stream, const Function &F) { 2870 assert(FS); 2871 NameVals.push_back(ValueID); 2872 NameVals.push_back(getEncodedLinkage(FS->linkage())); 2873 NameVals.push_back(FS->instCount()); 2874 NameVals.push_back(FS->refs().size()); 2875 2876 for (auto &RI : FS->refs()) 2877 NameVals.push_back(RI); 2878 2879 bool HasProfileData = F.getEntryCount().hasValue(); 2880 for (auto &ECI : FS->edges()) { 2881 NameVals.push_back(ECI.first); 2882 assert(ECI.second.CallsiteCount > 0 && "Expected at least one callsite"); 2883 NameVals.push_back(ECI.second.CallsiteCount); 2884 if (HasProfileData) 2885 NameVals.push_back(ECI.second.ProfileCount); 2886 } 2887 2888 unsigned FSAbbrev = (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev); 2889 unsigned Code = 2890 (HasProfileData ? bitc::FS_PERMODULE_PROFILE : bitc::FS_PERMODULE); 2891 2892 // Emit the finished record. 2893 Stream.EmitRecord(Code, NameVals, FSAbbrev); 2894 NameVals.clear(); 2895 } 2896 2897 // Collect the global value references in the given variable's initializer, 2898 // and emit them in a summary record. 2899 static void WriteModuleLevelReferences(const GlobalVariable &V, 2900 const ValueEnumerator &VE, 2901 SmallVector<uint64_t, 64> &NameVals, 2902 unsigned FSModRefsAbbrev, 2903 BitstreamWriter &Stream) { 2904 DenseSet<unsigned> RefEdges; 2905 SmallPtrSet<const User *, 8> Visited; 2906 findRefEdges(&V, VE, RefEdges, Visited); 2907 unsigned RefCount = RefEdges.size(); 2908 if (RefCount) { 2909 NameVals.push_back(VE.getValueID(&V)); 2910 NameVals.push_back(getEncodedLinkage(V.getLinkage())); 2911 for (auto RefId : RefEdges) { 2912 NameVals.push_back(RefId); 2913 } 2914 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals, 2915 FSModRefsAbbrev); 2916 NameVals.clear(); 2917 } 2918 } 2919 2920 /// Emit the per-module summary section alongside the rest of 2921 /// the module's bitcode. 2922 static void WritePerModuleGlobalValueSummary( 2923 DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> &FunctionIndex, 2924 const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) { 2925 if (M->empty()) 2926 return; 2927 2928 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3); 2929 2930 // Abbrev for FS_PERMODULE. 2931 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 2932 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE)); 2933 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 2934 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 2935 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 2936 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 2937 // numrefs x valueid, n x (valueid, callsitecount) 2938 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2939 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2940 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv); 2941 2942 // Abbrev for FS_PERMODULE_PROFILE. 2943 Abbv = new BitCodeAbbrev(); 2944 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE)); 2945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 2946 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 2947 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 2948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 2949 // numrefs x valueid, n x (valueid, callsitecount, profilecount) 2950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2952 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv); 2953 2954 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS. 2955 Abbv = new BitCodeAbbrev(); 2956 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS)); 2957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 2958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 2959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids 2960 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 2961 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv); 2962 2963 SmallVector<uint64_t, 64> NameVals; 2964 // Iterate over the list of functions instead of the FunctionIndex map to 2965 // ensure the ordering is stable. 2966 for (const Function &F : *M) { 2967 if (F.isDeclaration()) 2968 continue; 2969 // Skip anonymous functions. We will emit a function summary for 2970 // any aliases below. 2971 if (!F.hasName()) 2972 continue; 2973 2974 assert(FunctionIndex.count(&F) == 1); 2975 2976 WritePerModuleFunctionSummaryRecord( 2977 NameVals, dyn_cast<FunctionSummary>(FunctionIndex[&F]->summary()), 2978 VE.getValueID(M->getValueSymbolTable().lookup(F.getName())), 2979 FSCallsAbbrev, FSCallsProfileAbbrev, Stream, F); 2980 } 2981 2982 for (const GlobalAlias &A : M->aliases()) { 2983 if (!A.getBaseObject()) 2984 continue; 2985 const Function *F = dyn_cast<Function>(A.getBaseObject()); 2986 if (!F || F->isDeclaration()) 2987 continue; 2988 2989 assert(FunctionIndex.count(F) == 1); 2990 FunctionSummary *FS = 2991 dyn_cast<FunctionSummary>(FunctionIndex[F]->summary()); 2992 // Add the alias to the reference list of aliasee function. 2993 FS->addRefEdge( 2994 VE.getValueID(M->getValueSymbolTable().lookup(A.getName()))); 2995 WritePerModuleFunctionSummaryRecord( 2996 NameVals, FS, 2997 VE.getValueID(M->getValueSymbolTable().lookup(A.getName())), 2998 FSCallsAbbrev, FSCallsProfileAbbrev, Stream, *F); 2999 } 3000 3001 // Capture references from GlobalVariable initializers, which are outside 3002 // of a function scope. 3003 for (const GlobalVariable &G : M->globals()) 3004 WriteModuleLevelReferences(G, VE, NameVals, FSModRefsAbbrev, Stream); 3005 for (const GlobalAlias &A : M->aliases()) { 3006 const auto *GV = dyn_cast<GlobalVariable>(A.getBaseObject()); 3007 if (GV) 3008 WriteModuleLevelReferences(*GV, VE, NameVals, FSModRefsAbbrev, Stream); 3009 } 3010 3011 Stream.ExitBlock(); 3012 } 3013 3014 /// Emit the combined function summary section into the combined index 3015 /// file. 3016 static void WriteCombinedGlobalValueSummary( 3017 const FunctionInfoIndex &I, BitstreamWriter &Stream, 3018 std::map<uint64_t, unsigned> &GUIDToValueIdMap, unsigned GlobalValueId) { 3019 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3); 3020 3021 // Abbrev for FS_COMBINED. 3022 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 3023 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED)); 3024 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 3025 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 3026 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 3027 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 3028 // numrefs x valueid, n x (valueid, callsitecount) 3029 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3030 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3031 unsigned FSCallsAbbrev = Stream.EmitAbbrev(Abbv); 3032 3033 // Abbrev for FS_COMBINED_PROFILE. 3034 Abbv = new BitCodeAbbrev(); 3035 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE)); 3036 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 3037 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 3038 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 3039 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 3040 // numrefs x valueid, n x (valueid, callsitecount, profilecount) 3041 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3042 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3043 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(Abbv); 3044 3045 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS. 3046 Abbv = new BitCodeAbbrev(); 3047 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS)); 3048 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 3049 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // linkage 3050 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids 3051 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3052 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(Abbv); 3053 3054 SmallVector<uint64_t, 64> NameVals; 3055 for (const auto &FII : I) { 3056 for (auto &FI : FII.second) { 3057 GlobalValueSummary *S = FI->summary(); 3058 assert(S); 3059 3060 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) { 3061 assert(!VS->refs().empty() && "Expected at least one ref edge"); 3062 NameVals.push_back(I.getModuleId(VS->modulePath())); 3063 NameVals.push_back(getEncodedLinkage(VS->linkage())); 3064 for (auto &RI : VS->refs()) { 3065 const auto &VMI = GUIDToValueIdMap.find(RI); 3066 unsigned RefId; 3067 // If this GUID doesn't have an entry, assign one. 3068 if (VMI == GUIDToValueIdMap.end()) { 3069 GUIDToValueIdMap[RI] = ++GlobalValueId; 3070 RefId = GlobalValueId; 3071 } else { 3072 RefId = VMI->second; 3073 } 3074 NameVals.push_back(RefId); 3075 } 3076 3077 // Record the starting offset of this summary entry for use 3078 // in the VST entry. Add the current code size since the 3079 // reader will invoke readRecord after the abbrev id read. 3080 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + 3081 Stream.GetAbbrevIDWidth()); 3082 3083 // Emit the finished record. 3084 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals, 3085 FSModRefsAbbrev); 3086 NameVals.clear(); 3087 continue; 3088 } 3089 3090 auto *FS = dyn_cast<FunctionSummary>(S); 3091 assert(FS); 3092 NameVals.push_back(I.getModuleId(FS->modulePath())); 3093 NameVals.push_back(getEncodedLinkage(FS->linkage())); 3094 NameVals.push_back(FS->instCount()); 3095 NameVals.push_back(FS->refs().size()); 3096 3097 for (auto &RI : FS->refs()) { 3098 const auto &VMI = GUIDToValueIdMap.find(RI); 3099 unsigned RefId; 3100 // If this GUID doesn't have an entry, assign one. 3101 if (VMI == GUIDToValueIdMap.end()) { 3102 GUIDToValueIdMap[RI] = ++GlobalValueId; 3103 RefId = GlobalValueId; 3104 } else { 3105 RefId = VMI->second; 3106 } 3107 NameVals.push_back(RefId); 3108 } 3109 3110 bool HasProfileData = false; 3111 for (auto &EI : FS->edges()) { 3112 HasProfileData |= EI.second.ProfileCount != 0; 3113 if (HasProfileData) 3114 break; 3115 } 3116 3117 for (auto &EI : FS->edges()) { 3118 const auto &VMI = GUIDToValueIdMap.find(EI.first); 3119 // If this GUID doesn't have an entry, it doesn't have a function 3120 // summary and we don't need to record any calls to it. 3121 if (VMI == GUIDToValueIdMap.end()) 3122 continue; 3123 NameVals.push_back(VMI->second); 3124 assert(EI.second.CallsiteCount > 0 && "Expected at least one callsite"); 3125 NameVals.push_back(EI.second.CallsiteCount); 3126 if (HasProfileData) 3127 NameVals.push_back(EI.second.ProfileCount); 3128 } 3129 3130 // Record the starting offset of this summary entry for use 3131 // in the VST entry. Add the current code size since the 3132 // reader will invoke readRecord after the abbrev id read. 3133 FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth()); 3134 3135 unsigned FSAbbrev = 3136 (HasProfileData ? FSCallsProfileAbbrev : FSCallsAbbrev); 3137 unsigned Code = 3138 (HasProfileData ? bitc::FS_COMBINED_PROFILE : bitc::FS_COMBINED); 3139 3140 // Emit the finished record. 3141 Stream.EmitRecord(Code, NameVals, FSAbbrev); 3142 NameVals.clear(); 3143 } 3144 } 3145 3146 Stream.ExitBlock(); 3147 } 3148 3149 // Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the 3150 // current llvm version, and a record for the epoch number. 3151 static void WriteIdentificationBlock(const Module *M, BitstreamWriter &Stream) { 3152 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5); 3153 3154 // Write the "user readable" string identifying the bitcode producer 3155 BitCodeAbbrev *Abbv = new BitCodeAbbrev(); 3156 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING)); 3157 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3158 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 3159 auto StringAbbrev = Stream.EmitAbbrev(Abbv); 3160 WriteStringRecord(bitc::IDENTIFICATION_CODE_STRING, 3161 "LLVM" LLVM_VERSION_STRING, StringAbbrev, Stream); 3162 3163 // Write the epoch version 3164 Abbv = new BitCodeAbbrev(); 3165 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH)); 3166 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 3167 auto EpochAbbrev = Stream.EmitAbbrev(Abbv); 3168 SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH}; 3169 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev); 3170 Stream.ExitBlock(); 3171 } 3172 3173 /// WriteModule - Emit the specified module to the bitstream. 3174 static void WriteModule(const Module *M, BitstreamWriter &Stream, 3175 bool ShouldPreserveUseListOrder, 3176 uint64_t BitcodeStartBit, bool EmitSummaryIndex) { 3177 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 3178 3179 SmallVector<unsigned, 1> Vals; 3180 unsigned CurVersion = 1; 3181 Vals.push_back(CurVersion); 3182 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 3183 3184 // Analyze the module, enumerating globals, functions, etc. 3185 ValueEnumerator VE(*M, ShouldPreserveUseListOrder); 3186 3187 // Emit blockinfo, which defines the standard abbreviations etc. 3188 WriteBlockInfo(VE, Stream); 3189 3190 // Emit information about attribute groups. 3191 WriteAttributeGroupTable(VE, Stream); 3192 3193 // Emit information about parameter attributes. 3194 WriteAttributeTable(VE, Stream); 3195 3196 // Emit information describing all of the types in the module. 3197 WriteTypeTable(VE, Stream); 3198 3199 writeComdats(VE, Stream); 3200 3201 // Emit top-level description of module, including target triple, inline asm, 3202 // descriptors for global variables, and function prototype info. 3203 uint64_t VSTOffsetPlaceholder = WriteModuleInfo(M, VE, Stream); 3204 3205 // Emit constants. 3206 WriteModuleConstants(VE, Stream); 3207 3208 // Emit metadata. 3209 WriteModuleMetadata(M, VE, Stream); 3210 3211 // Emit metadata. 3212 WriteModuleMetadataStore(M, Stream); 3213 3214 // Emit module-level use-lists. 3215 if (VE.shouldPreserveUseListOrder()) 3216 WriteUseListBlock(nullptr, VE, Stream); 3217 3218 WriteOperandBundleTags(M, Stream); 3219 3220 // Emit function bodies. 3221 DenseMap<const Function *, std::unique_ptr<GlobalValueInfo>> FunctionIndex; 3222 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) 3223 if (!F->isDeclaration()) 3224 WriteFunction(*F, M, VE, Stream, FunctionIndex, EmitSummaryIndex); 3225 3226 // Need to write after the above call to WriteFunction which populates 3227 // the summary information in the index. 3228 if (EmitSummaryIndex) 3229 WritePerModuleGlobalValueSummary(FunctionIndex, M, VE, Stream); 3230 3231 WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream, 3232 VSTOffsetPlaceholder, BitcodeStartBit, &FunctionIndex); 3233 3234 Stream.ExitBlock(); 3235 } 3236 3237 /// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a 3238 /// header and trailer to make it compatible with the system archiver. To do 3239 /// this we emit the following header, and then emit a trailer that pads the 3240 /// file out to be a multiple of 16 bytes. 3241 /// 3242 /// struct bc_header { 3243 /// uint32_t Magic; // 0x0B17C0DE 3244 /// uint32_t Version; // Version, currently always 0. 3245 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 3246 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 3247 /// uint32_t CPUType; // CPU specifier. 3248 /// ... potentially more later ... 3249 /// }; 3250 3251 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, 3252 uint32_t &Position) { 3253 support::endian::write32le(&Buffer[Position], Value); 3254 Position += 4; 3255 } 3256 3257 static void EmitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, 3258 const Triple &TT) { 3259 unsigned CPUType = ~0U; 3260 3261 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 3262 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 3263 // number from /usr/include/mach/machine.h. It is ok to reproduce the 3264 // specific constants here because they are implicitly part of the Darwin ABI. 3265 enum { 3266 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 3267 DARWIN_CPU_TYPE_X86 = 7, 3268 DARWIN_CPU_TYPE_ARM = 12, 3269 DARWIN_CPU_TYPE_POWERPC = 18 3270 }; 3271 3272 Triple::ArchType Arch = TT.getArch(); 3273 if (Arch == Triple::x86_64) 3274 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 3275 else if (Arch == Triple::x86) 3276 CPUType = DARWIN_CPU_TYPE_X86; 3277 else if (Arch == Triple::ppc) 3278 CPUType = DARWIN_CPU_TYPE_POWERPC; 3279 else if (Arch == Triple::ppc64) 3280 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 3281 else if (Arch == Triple::arm || Arch == Triple::thumb) 3282 CPUType = DARWIN_CPU_TYPE_ARM; 3283 3284 // Traditional Bitcode starts after header. 3285 assert(Buffer.size() >= BWH_HeaderSize && 3286 "Expected header size to be reserved"); 3287 unsigned BCOffset = BWH_HeaderSize; 3288 unsigned BCSize = Buffer.size() - BWH_HeaderSize; 3289 3290 // Write the magic and version. 3291 unsigned Position = 0; 3292 WriteInt32ToBuffer(0x0B17C0DE , Buffer, Position); 3293 WriteInt32ToBuffer(0 , Buffer, Position); // Version. 3294 WriteInt32ToBuffer(BCOffset , Buffer, Position); 3295 WriteInt32ToBuffer(BCSize , Buffer, Position); 3296 WriteInt32ToBuffer(CPUType , Buffer, Position); 3297 3298 // If the file is not a multiple of 16 bytes, insert dummy padding. 3299 while (Buffer.size() & 15) 3300 Buffer.push_back(0); 3301 } 3302 3303 /// Helper to write the header common to all bitcode files. 3304 static void WriteBitcodeHeader(BitstreamWriter &Stream) { 3305 // Emit the file header. 3306 Stream.Emit((unsigned)'B', 8); 3307 Stream.Emit((unsigned)'C', 8); 3308 Stream.Emit(0x0, 4); 3309 Stream.Emit(0xC, 4); 3310 Stream.Emit(0xE, 4); 3311 Stream.Emit(0xD, 4); 3312 } 3313 3314 /// WriteBitcodeToFile - Write the specified module to the specified output 3315 /// stream. 3316 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out, 3317 bool ShouldPreserveUseListOrder, 3318 bool EmitSummaryIndex) { 3319 SmallVector<char, 0> Buffer; 3320 Buffer.reserve(256*1024); 3321 3322 // If this is darwin or another generic macho target, reserve space for the 3323 // header. 3324 Triple TT(M->getTargetTriple()); 3325 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) 3326 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0); 3327 3328 // Emit the module into the buffer. 3329 { 3330 BitstreamWriter Stream(Buffer); 3331 // Save the start bit of the actual bitcode, in case there is space 3332 // saved at the start for the darwin header above. The reader stream 3333 // will start at the bitcode, and we need the offset of the VST 3334 // to line up. 3335 uint64_t BitcodeStartBit = Stream.GetCurrentBitNo(); 3336 3337 // Emit the file header. 3338 WriteBitcodeHeader(Stream); 3339 3340 WriteIdentificationBlock(M, Stream); 3341 3342 // Emit the module. 3343 WriteModule(M, Stream, ShouldPreserveUseListOrder, BitcodeStartBit, 3344 EmitSummaryIndex); 3345 } 3346 3347 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) 3348 EmitDarwinBCHeaderAndTrailer(Buffer, TT); 3349 3350 // Write the generated bitstream to "Out". 3351 Out.write((char*)&Buffer.front(), Buffer.size()); 3352 } 3353 3354 // Write the specified module summary index to the given raw output stream, 3355 // where it will be written in a new bitcode block. This is used when 3356 // writing the combined index file for ThinLTO. 3357 void llvm::WriteIndexToFile(const FunctionInfoIndex &Index, raw_ostream &Out) { 3358 SmallVector<char, 0> Buffer; 3359 Buffer.reserve(256 * 1024); 3360 3361 BitstreamWriter Stream(Buffer); 3362 3363 // Emit the bitcode header. 3364 WriteBitcodeHeader(Stream); 3365 3366 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 3367 3368 SmallVector<unsigned, 1> Vals; 3369 unsigned CurVersion = 1; 3370 Vals.push_back(CurVersion); 3371 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, Vals); 3372 3373 // If we have a VST, write the VSTOFFSET record placeholder and record 3374 // its offset. 3375 uint64_t VSTOffsetPlaceholder = WriteValueSymbolTableForwardDecl(Stream); 3376 3377 // Write the module paths in the combined index. 3378 WriteModStrings(Index, Stream); 3379 3380 // Assign unique value ids to all functions in the index for use 3381 // in writing out the call graph edges. Save the mapping from GUID 3382 // to the new global value id to use when writing those edges, which 3383 // are currently saved in the index in terms of GUID. 3384 std::map<uint64_t, unsigned> GUIDToValueIdMap; 3385 unsigned GlobalValueId = 0; 3386 for (auto &II : Index) 3387 GUIDToValueIdMap[II.first] = ++GlobalValueId; 3388 3389 // Write the summary combined index records. 3390 WriteCombinedGlobalValueSummary(Index, Stream, GUIDToValueIdMap, 3391 GlobalValueId); 3392 3393 // Need a special VST writer for the combined index (we don't have a 3394 // real VST and real values when this is invoked). 3395 WriteCombinedValueSymbolTable(Index, Stream, GUIDToValueIdMap, 3396 VSTOffsetPlaceholder); 3397 3398 Stream.ExitBlock(); 3399 3400 Out.write((char *)&Buffer.front(), Buffer.size()); 3401 } 3402