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