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