1 //===- Bitcode/Writer/BitcodeWriter.cpp - Bitcode Writer ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Bitcode writer implementation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Bitcode/BitcodeWriter.h" 14 #include "ValueEnumerator.h" 15 #include "llvm/ADT/APFloat.h" 16 #include "llvm/ADT/APInt.h" 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SetVector.h" 21 #include "llvm/ADT/SmallPtrSet.h" 22 #include "llvm/ADT/SmallString.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringMap.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/Analysis/MemoryProfileInfo.h" 27 #include "llvm/BinaryFormat/Dwarf.h" 28 #include "llvm/Bitcode/BitcodeCommon.h" 29 #include "llvm/Bitcode/BitcodeReader.h" 30 #include "llvm/Bitcode/LLVMBitCodes.h" 31 #include "llvm/Bitstream/BitCodes.h" 32 #include "llvm/Bitstream/BitstreamWriter.h" 33 #include "llvm/Config/llvm-config.h" 34 #include "llvm/IR/Attributes.h" 35 #include "llvm/IR/BasicBlock.h" 36 #include "llvm/IR/Comdat.h" 37 #include "llvm/IR/Constant.h" 38 #include "llvm/IR/ConstantRangeList.h" 39 #include "llvm/IR/Constants.h" 40 #include "llvm/IR/DebugInfoMetadata.h" 41 #include "llvm/IR/DebugLoc.h" 42 #include "llvm/IR/DerivedTypes.h" 43 #include "llvm/IR/Function.h" 44 #include "llvm/IR/GlobalAlias.h" 45 #include "llvm/IR/GlobalIFunc.h" 46 #include "llvm/IR/GlobalObject.h" 47 #include "llvm/IR/GlobalValue.h" 48 #include "llvm/IR/GlobalVariable.h" 49 #include "llvm/IR/InlineAsm.h" 50 #include "llvm/IR/InstrTypes.h" 51 #include "llvm/IR/Instruction.h" 52 #include "llvm/IR/Instructions.h" 53 #include "llvm/IR/LLVMContext.h" 54 #include "llvm/IR/Metadata.h" 55 #include "llvm/IR/Module.h" 56 #include "llvm/IR/ModuleSummaryIndex.h" 57 #include "llvm/IR/Operator.h" 58 #include "llvm/IR/Type.h" 59 #include "llvm/IR/UseListOrder.h" 60 #include "llvm/IR/Value.h" 61 #include "llvm/IR/ValueSymbolTable.h" 62 #include "llvm/MC/StringTableBuilder.h" 63 #include "llvm/MC/TargetRegistry.h" 64 #include "llvm/Object/IRSymtab.h" 65 #include "llvm/ProfileData/MemProf.h" 66 #include "llvm/ProfileData/MemProfRadixTree.h" 67 #include "llvm/Support/AtomicOrdering.h" 68 #include "llvm/Support/Casting.h" 69 #include "llvm/Support/CommandLine.h" 70 #include "llvm/Support/Compiler.h" 71 #include "llvm/Support/Endian.h" 72 #include "llvm/Support/Error.h" 73 #include "llvm/Support/ErrorHandling.h" 74 #include "llvm/Support/MathExtras.h" 75 #include "llvm/Support/SHA1.h" 76 #include "llvm/Support/raw_ostream.h" 77 #include "llvm/TargetParser/Triple.h" 78 #include <algorithm> 79 #include <cassert> 80 #include <cstddef> 81 #include <cstdint> 82 #include <iterator> 83 #include <map> 84 #include <memory> 85 #include <optional> 86 #include <string> 87 #include <utility> 88 #include <vector> 89 90 using namespace llvm; 91 using namespace llvm::memprof; 92 93 static cl::opt<unsigned> 94 IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25), 95 cl::desc("Number of metadatas above which we emit an index " 96 "to enable lazy-loading")); 97 static cl::opt<uint32_t> FlushThreshold( 98 "bitcode-flush-threshold", cl::Hidden, cl::init(512), 99 cl::desc("The threshold (unit M) for flushing LLVM bitcode.")); 100 101 static cl::opt<bool> WriteRelBFToSummary( 102 "write-relbf-to-summary", cl::Hidden, cl::init(false), 103 cl::desc("Write relative block frequency to function summary ")); 104 105 // Since we only use the context information in the memprof summary records in 106 // the LTO backends to do assertion checking, save time and space by only 107 // serializing the context for non-NDEBUG builds. 108 // TODO: Currently this controls writing context of the allocation info records, 109 // which are larger and more expensive, but we should do this for the callsite 110 // records as well. 111 // FIXME: Convert to a const once this has undergone more sigificant testing. 112 static cl::opt<bool> 113 CombinedIndexMemProfContext("combined-index-memprof-context", cl::Hidden, 114 #ifdef NDEBUG 115 cl::init(false), 116 #else 117 cl::init(true), 118 #endif 119 cl::desc("")); 120 121 namespace llvm { 122 extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold; 123 } 124 125 namespace { 126 127 /// These are manifest constants used by the bitcode writer. They do not need to 128 /// be kept in sync with the reader, but need to be consistent within this file. 129 enum { 130 // VALUE_SYMTAB_BLOCK abbrev id's. 131 VST_ENTRY_8_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 132 VST_ENTRY_7_ABBREV, 133 VST_ENTRY_6_ABBREV, 134 VST_BBENTRY_6_ABBREV, 135 136 // CONSTANTS_BLOCK abbrev id's. 137 CONSTANTS_SETTYPE_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 138 CONSTANTS_INTEGER_ABBREV, 139 CONSTANTS_CE_CAST_Abbrev, 140 CONSTANTS_NULL_Abbrev, 141 142 // FUNCTION_BLOCK abbrev id's. 143 FUNCTION_INST_LOAD_ABBREV = bitc::FIRST_APPLICATION_ABBREV, 144 FUNCTION_INST_STORE_ABBREV, 145 FUNCTION_INST_UNOP_ABBREV, 146 FUNCTION_INST_UNOP_FLAGS_ABBREV, 147 FUNCTION_INST_BINOP_ABBREV, 148 FUNCTION_INST_BINOP_FLAGS_ABBREV, 149 FUNCTION_INST_CAST_ABBREV, 150 FUNCTION_INST_CAST_FLAGS_ABBREV, 151 FUNCTION_INST_RET_VOID_ABBREV, 152 FUNCTION_INST_RET_VAL_ABBREV, 153 FUNCTION_INST_BR_UNCOND_ABBREV, 154 FUNCTION_INST_BR_COND_ABBREV, 155 FUNCTION_INST_UNREACHABLE_ABBREV, 156 FUNCTION_INST_GEP_ABBREV, 157 FUNCTION_INST_CMP_ABBREV, 158 FUNCTION_INST_CMP_FLAGS_ABBREV, 159 FUNCTION_DEBUG_RECORD_VALUE_ABBREV, 160 FUNCTION_DEBUG_LOC_ABBREV, 161 }; 162 163 /// Abstract class to manage the bitcode writing, subclassed for each bitcode 164 /// file type. 165 class BitcodeWriterBase { 166 protected: 167 /// The stream created and owned by the client. 168 BitstreamWriter &Stream; 169 170 StringTableBuilder &StrtabBuilder; 171 172 public: 173 /// Constructs a BitcodeWriterBase object that writes to the provided 174 /// \p Stream. 175 BitcodeWriterBase(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder) 176 : Stream(Stream), StrtabBuilder(StrtabBuilder) {} 177 178 protected: 179 void writeModuleVersion(); 180 }; 181 182 void BitcodeWriterBase::writeModuleVersion() { 183 // VERSION: [version#] 184 Stream.EmitRecord(bitc::MODULE_CODE_VERSION, ArrayRef<uint64_t>{2}); 185 } 186 187 /// Base class to manage the module bitcode writing, currently subclassed for 188 /// ModuleBitcodeWriter and ThinLinkBitcodeWriter. 189 class ModuleBitcodeWriterBase : public BitcodeWriterBase { 190 protected: 191 /// The Module to write to bitcode. 192 const Module &M; 193 194 /// Enumerates ids for all values in the module. 195 ValueEnumerator VE; 196 197 /// Optional per-module index to write for ThinLTO. 198 const ModuleSummaryIndex *Index; 199 200 /// Map that holds the correspondence between GUIDs in the summary index, 201 /// that came from indirect call profiles, and a value id generated by this 202 /// class to use in the VST and summary block records. 203 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap; 204 205 /// Tracks the last value id recorded in the GUIDToValueMap. 206 unsigned GlobalValueId; 207 208 /// Saves the offset of the VSTOffset record that must eventually be 209 /// backpatched with the offset of the actual VST. 210 uint64_t VSTOffsetPlaceholder = 0; 211 212 public: 213 /// Constructs a ModuleBitcodeWriterBase object for the given Module, 214 /// writing to the provided \p Buffer. 215 ModuleBitcodeWriterBase(const Module &M, StringTableBuilder &StrtabBuilder, 216 BitstreamWriter &Stream, 217 bool ShouldPreserveUseListOrder, 218 const ModuleSummaryIndex *Index) 219 : BitcodeWriterBase(Stream, StrtabBuilder), M(M), 220 VE(M, ShouldPreserveUseListOrder), Index(Index) { 221 // Assign ValueIds to any callee values in the index that came from 222 // indirect call profiles and were recorded as a GUID not a Value* 223 // (which would have been assigned an ID by the ValueEnumerator). 224 // The starting ValueId is just after the number of values in the 225 // ValueEnumerator, so that they can be emitted in the VST. 226 GlobalValueId = VE.getValues().size(); 227 if (!Index) 228 return; 229 for (const auto &GUIDSummaryLists : *Index) 230 // Examine all summaries for this GUID. 231 for (auto &Summary : GUIDSummaryLists.second.SummaryList) 232 if (auto FS = dyn_cast<FunctionSummary>(Summary.get())) { 233 // For each call in the function summary, see if the call 234 // is to a GUID (which means it is for an indirect call, 235 // otherwise we would have a Value for it). If so, synthesize 236 // a value id. 237 for (auto &CallEdge : FS->calls()) 238 if (!CallEdge.first.haveGVs() || !CallEdge.first.getValue()) 239 assignValueId(CallEdge.first.getGUID()); 240 241 // For each referenced variables in the function summary, see if the 242 // variable is represented by a GUID (as opposed to a symbol to 243 // declarations or definitions in the module). If so, synthesize a 244 // value id. 245 for (auto &RefEdge : FS->refs()) 246 if (!RefEdge.haveGVs() || !RefEdge.getValue()) 247 assignValueId(RefEdge.getGUID()); 248 } 249 } 250 251 protected: 252 void writePerModuleGlobalValueSummary(); 253 254 private: 255 void writePerModuleFunctionSummaryRecord( 256 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary, 257 unsigned ValueID, unsigned FSCallsAbbrev, unsigned FSCallsProfileAbbrev, 258 unsigned CallsiteAbbrev, unsigned AllocAbbrev, unsigned ContextIdAbbvId, 259 const Function &F, DenseMap<CallStackId, LinearCallStackId> &CallStackPos, 260 CallStackId &CallStackCount); 261 void writeModuleLevelReferences(const GlobalVariable &V, 262 SmallVector<uint64_t, 64> &NameVals, 263 unsigned FSModRefsAbbrev, 264 unsigned FSModVTableRefsAbbrev); 265 266 void assignValueId(GlobalValue::GUID ValGUID) { 267 GUIDToValueIdMap[ValGUID] = ++GlobalValueId; 268 } 269 270 unsigned getValueId(GlobalValue::GUID ValGUID) { 271 const auto &VMI = GUIDToValueIdMap.find(ValGUID); 272 // Expect that any GUID value had a value Id assigned by an 273 // earlier call to assignValueId. 274 assert(VMI != GUIDToValueIdMap.end() && 275 "GUID does not have assigned value Id"); 276 return VMI->second; 277 } 278 279 // Helper to get the valueId for the type of value recorded in VI. 280 unsigned getValueId(ValueInfo VI) { 281 if (!VI.haveGVs() || !VI.getValue()) 282 return getValueId(VI.getGUID()); 283 return VE.getValueID(VI.getValue()); 284 } 285 286 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; } 287 }; 288 289 /// Class to manage the bitcode writing for a module. 290 class ModuleBitcodeWriter : public ModuleBitcodeWriterBase { 291 /// True if a module hash record should be written. 292 bool GenerateHash; 293 294 /// If non-null, when GenerateHash is true, the resulting hash is written 295 /// into ModHash. 296 ModuleHash *ModHash; 297 298 SHA1 Hasher; 299 300 /// The start bit of the identification block. 301 uint64_t BitcodeStartBit; 302 303 public: 304 /// Constructs a ModuleBitcodeWriter object for the given Module, 305 /// writing to the provided \p Buffer. 306 ModuleBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder, 307 BitstreamWriter &Stream, bool ShouldPreserveUseListOrder, 308 const ModuleSummaryIndex *Index, bool GenerateHash, 309 ModuleHash *ModHash = nullptr) 310 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream, 311 ShouldPreserveUseListOrder, Index), 312 GenerateHash(GenerateHash), ModHash(ModHash), 313 BitcodeStartBit(Stream.GetCurrentBitNo()) {} 314 315 /// Emit the current module to the bitstream. 316 void write(); 317 318 private: 319 uint64_t bitcodeStartBit() { return BitcodeStartBit; } 320 321 size_t addToStrtab(StringRef Str); 322 323 void writeAttributeGroupTable(); 324 void writeAttributeTable(); 325 void writeTypeTable(); 326 void writeComdats(); 327 void writeValueSymbolTableForwardDecl(); 328 void writeModuleInfo(); 329 void writeValueAsMetadata(const ValueAsMetadata *MD, 330 SmallVectorImpl<uint64_t> &Record); 331 void writeMDTuple(const MDTuple *N, SmallVectorImpl<uint64_t> &Record, 332 unsigned Abbrev); 333 unsigned createDILocationAbbrev(); 334 void writeDILocation(const DILocation *N, SmallVectorImpl<uint64_t> &Record, 335 unsigned &Abbrev); 336 unsigned createGenericDINodeAbbrev(); 337 void writeGenericDINode(const GenericDINode *N, 338 SmallVectorImpl<uint64_t> &Record, unsigned &Abbrev); 339 void writeDISubrange(const DISubrange *N, SmallVectorImpl<uint64_t> &Record, 340 unsigned Abbrev); 341 void writeDIGenericSubrange(const DIGenericSubrange *N, 342 SmallVectorImpl<uint64_t> &Record, 343 unsigned Abbrev); 344 void writeDIEnumerator(const DIEnumerator *N, 345 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 346 void writeDIBasicType(const DIBasicType *N, SmallVectorImpl<uint64_t> &Record, 347 unsigned Abbrev); 348 void writeDIFixedPointType(const DIFixedPointType *N, 349 SmallVectorImpl<uint64_t> &Record, 350 unsigned Abbrev); 351 void writeDIStringType(const DIStringType *N, 352 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 353 void writeDIDerivedType(const DIDerivedType *N, 354 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 355 void writeDISubrangeType(const DISubrangeType *N, 356 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 357 void writeDICompositeType(const DICompositeType *N, 358 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 359 void writeDISubroutineType(const DISubroutineType *N, 360 SmallVectorImpl<uint64_t> &Record, 361 unsigned Abbrev); 362 void writeDIFile(const DIFile *N, SmallVectorImpl<uint64_t> &Record, 363 unsigned Abbrev); 364 void writeDICompileUnit(const DICompileUnit *N, 365 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 366 void writeDISubprogram(const DISubprogram *N, 367 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 368 void writeDILexicalBlock(const DILexicalBlock *N, 369 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 370 void writeDILexicalBlockFile(const DILexicalBlockFile *N, 371 SmallVectorImpl<uint64_t> &Record, 372 unsigned Abbrev); 373 void writeDICommonBlock(const DICommonBlock *N, 374 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 375 void writeDINamespace(const DINamespace *N, SmallVectorImpl<uint64_t> &Record, 376 unsigned Abbrev); 377 void writeDIMacro(const DIMacro *N, SmallVectorImpl<uint64_t> &Record, 378 unsigned Abbrev); 379 void writeDIMacroFile(const DIMacroFile *N, SmallVectorImpl<uint64_t> &Record, 380 unsigned Abbrev); 381 void writeDIArgList(const DIArgList *N, SmallVectorImpl<uint64_t> &Record); 382 void writeDIModule(const DIModule *N, SmallVectorImpl<uint64_t> &Record, 383 unsigned Abbrev); 384 void writeDIAssignID(const DIAssignID *N, SmallVectorImpl<uint64_t> &Record, 385 unsigned Abbrev); 386 void writeDITemplateTypeParameter(const DITemplateTypeParameter *N, 387 SmallVectorImpl<uint64_t> &Record, 388 unsigned Abbrev); 389 void writeDITemplateValueParameter(const DITemplateValueParameter *N, 390 SmallVectorImpl<uint64_t> &Record, 391 unsigned Abbrev); 392 void writeDIGlobalVariable(const DIGlobalVariable *N, 393 SmallVectorImpl<uint64_t> &Record, 394 unsigned Abbrev); 395 void writeDILocalVariable(const DILocalVariable *N, 396 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 397 void writeDILabel(const DILabel *N, 398 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 399 void writeDIExpression(const DIExpression *N, 400 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 401 void writeDIGlobalVariableExpression(const DIGlobalVariableExpression *N, 402 SmallVectorImpl<uint64_t> &Record, 403 unsigned Abbrev); 404 void writeDIObjCProperty(const DIObjCProperty *N, 405 SmallVectorImpl<uint64_t> &Record, unsigned Abbrev); 406 void writeDIImportedEntity(const DIImportedEntity *N, 407 SmallVectorImpl<uint64_t> &Record, 408 unsigned Abbrev); 409 unsigned createNamedMetadataAbbrev(); 410 void writeNamedMetadata(SmallVectorImpl<uint64_t> &Record); 411 unsigned createMetadataStringsAbbrev(); 412 void writeMetadataStrings(ArrayRef<const Metadata *> Strings, 413 SmallVectorImpl<uint64_t> &Record); 414 void writeMetadataRecords(ArrayRef<const Metadata *> MDs, 415 SmallVectorImpl<uint64_t> &Record, 416 std::vector<unsigned> *MDAbbrevs = nullptr, 417 std::vector<uint64_t> *IndexPos = nullptr); 418 void writeModuleMetadata(); 419 void writeFunctionMetadata(const Function &F); 420 void writeFunctionMetadataAttachment(const Function &F); 421 void pushGlobalMetadataAttachment(SmallVectorImpl<uint64_t> &Record, 422 const GlobalObject &GO); 423 void writeModuleMetadataKinds(); 424 void writeOperandBundleTags(); 425 void writeSyncScopeNames(); 426 void writeConstants(unsigned FirstVal, unsigned LastVal, bool isGlobal); 427 void writeModuleConstants(); 428 bool pushValueAndType(const Value *V, unsigned InstID, 429 SmallVectorImpl<unsigned> &Vals); 430 bool pushValueOrMetadata(const Value *V, unsigned InstID, 431 SmallVectorImpl<unsigned> &Vals); 432 void writeOperandBundles(const CallBase &CB, unsigned InstID); 433 void pushValue(const Value *V, unsigned InstID, 434 SmallVectorImpl<unsigned> &Vals); 435 void pushValueSigned(const Value *V, unsigned InstID, 436 SmallVectorImpl<uint64_t> &Vals); 437 void writeInstruction(const Instruction &I, unsigned InstID, 438 SmallVectorImpl<unsigned> &Vals); 439 void writeFunctionLevelValueSymbolTable(const ValueSymbolTable &VST); 440 void writeGlobalValueSymbolTable( 441 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex); 442 void writeUseList(UseListOrder &&Order); 443 void writeUseListBlock(const Function *F); 444 void 445 writeFunction(const Function &F, 446 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex); 447 void writeBlockInfo(); 448 void writeModuleHash(StringRef View); 449 450 unsigned getEncodedSyncScopeID(SyncScope::ID SSID) { 451 return unsigned(SSID); 452 } 453 454 unsigned getEncodedAlign(MaybeAlign Alignment) { return encode(Alignment); } 455 }; 456 457 /// Class to manage the bitcode writing for a combined index. 458 class IndexBitcodeWriter : public BitcodeWriterBase { 459 /// The combined index to write to bitcode. 460 const ModuleSummaryIndex &Index; 461 462 /// When writing combined summaries, provides the set of global value 463 /// summaries for which the value (function, function alias, etc) should be 464 /// imported as a declaration. 465 const GVSummaryPtrSet *DecSummaries = nullptr; 466 467 /// When writing a subset of the index for distributed backends, client 468 /// provides a map of modules to the corresponding GUIDs/summaries to write. 469 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex; 470 471 /// Map that holds the correspondence between the GUID used in the combined 472 /// index and a value id generated by this class to use in references. 473 std::map<GlobalValue::GUID, unsigned> GUIDToValueIdMap; 474 475 // The stack ids used by this index, which will be a subset of those in 476 // the full index in the case of distributed indexes. 477 std::vector<uint64_t> StackIds; 478 479 // Keep a map of the stack id indices used by records being written for this 480 // index to the index of the corresponding stack id in the above StackIds 481 // vector. Ensures we write each referenced stack id once. 482 DenseMap<unsigned, unsigned> StackIdIndicesToIndex; 483 484 /// Tracks the last value id recorded in the GUIDToValueMap. 485 unsigned GlobalValueId = 0; 486 487 /// Tracks the assignment of module paths in the module path string table to 488 /// an id assigned for use in summary references to the module path. 489 DenseMap<StringRef, uint64_t> ModuleIdMap; 490 491 public: 492 /// Constructs a IndexBitcodeWriter object for the given combined index, 493 /// writing to the provided \p Buffer. When writing a subset of the index 494 /// for a distributed backend, provide a \p ModuleToSummariesForIndex map. 495 /// If provided, \p DecSummaries specifies the set of summaries for which 496 /// the corresponding functions or aliased functions should be imported as a 497 /// declaration (but not definition) for each module. 498 IndexBitcodeWriter( 499 BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder, 500 const ModuleSummaryIndex &Index, 501 const GVSummaryPtrSet *DecSummaries = nullptr, 502 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex = nullptr) 503 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index), 504 DecSummaries(DecSummaries), 505 ModuleToSummariesForIndex(ModuleToSummariesForIndex) { 506 507 // See if the StackIdIndex was already added to the StackId map and 508 // vector. If not, record it. 509 auto RecordStackIdReference = [&](unsigned StackIdIndex) { 510 // If the StackIdIndex is not yet in the map, the below insert ensures 511 // that it will point to the new StackIds vector entry we push to just 512 // below. 513 auto Inserted = 514 StackIdIndicesToIndex.insert({StackIdIndex, StackIds.size()}); 515 if (Inserted.second) 516 StackIds.push_back(Index.getStackIdAtIndex(StackIdIndex)); 517 }; 518 519 // Assign unique value ids to all summaries to be written, for use 520 // in writing out the call graph edges. Save the mapping from GUID 521 // to the new global value id to use when writing those edges, which 522 // are currently saved in the index in terms of GUID. 523 forEachSummary([&](GVInfo I, bool IsAliasee) { 524 GUIDToValueIdMap[I.first] = ++GlobalValueId; 525 // If this is invoked for an aliasee, we want to record the above mapping, 526 // but not the information needed for its summary entry (if the aliasee is 527 // to be imported, we will invoke this separately with IsAliasee=false). 528 if (IsAliasee) 529 return; 530 auto *FS = dyn_cast<FunctionSummary>(I.second); 531 if (!FS) 532 return; 533 // Record all stack id indices actually used in the summary entries being 534 // written, so that we can compact them in the case of distributed ThinLTO 535 // indexes. 536 for (auto &CI : FS->callsites()) { 537 // If the stack id list is empty, this callsite info was synthesized for 538 // a missing tail call frame. Ensure that the callee's GUID gets a value 539 // id. Normally we only generate these for defined summaries, which in 540 // the case of distributed ThinLTO is only the functions already defined 541 // in the module or that we want to import. We don't bother to include 542 // all the callee symbols as they aren't normally needed in the backend. 543 // However, for the synthesized callsite infos we do need the callee 544 // GUID in the backend so that we can correlate the identified callee 545 // with this callsite info (which for non-tail calls is done by the 546 // ordering of the callsite infos and verified via stack ids). 547 if (CI.StackIdIndices.empty()) { 548 GUIDToValueIdMap[CI.Callee.getGUID()] = ++GlobalValueId; 549 continue; 550 } 551 for (auto Idx : CI.StackIdIndices) 552 RecordStackIdReference(Idx); 553 } 554 if (CombinedIndexMemProfContext) { 555 for (auto &AI : FS->allocs()) 556 for (auto &MIB : AI.MIBs) 557 for (auto Idx : MIB.StackIdIndices) 558 RecordStackIdReference(Idx); 559 } 560 }); 561 } 562 563 /// The below iterator returns the GUID and associated summary. 564 using GVInfo = std::pair<GlobalValue::GUID, GlobalValueSummary *>; 565 566 /// Calls the callback for each value GUID and summary to be written to 567 /// bitcode. This hides the details of whether they are being pulled from the 568 /// entire index or just those in a provided ModuleToSummariesForIndex map. 569 template<typename Functor> 570 void forEachSummary(Functor Callback) { 571 if (ModuleToSummariesForIndex) { 572 for (auto &M : *ModuleToSummariesForIndex) 573 for (auto &Summary : M.second) { 574 Callback(Summary, false); 575 // Ensure aliasee is handled, e.g. for assigning a valueId, 576 // even if we are not importing the aliasee directly (the 577 // imported alias will contain a copy of aliasee). 578 if (auto *AS = dyn_cast<AliasSummary>(Summary.getSecond())) 579 Callback({AS->getAliaseeGUID(), &AS->getAliasee()}, true); 580 } 581 } else { 582 for (auto &Summaries : Index) 583 for (auto &Summary : Summaries.second.SummaryList) 584 Callback({Summaries.first, Summary.get()}, false); 585 } 586 } 587 588 /// Calls the callback for each entry in the modulePaths StringMap that 589 /// should be written to the module path string table. This hides the details 590 /// of whether they are being pulled from the entire index or just those in a 591 /// provided ModuleToSummariesForIndex map. 592 template <typename Functor> void forEachModule(Functor Callback) { 593 if (ModuleToSummariesForIndex) { 594 for (const auto &M : *ModuleToSummariesForIndex) { 595 const auto &MPI = Index.modulePaths().find(M.first); 596 if (MPI == Index.modulePaths().end()) { 597 // This should only happen if the bitcode file was empty, in which 598 // case we shouldn't be importing (the ModuleToSummariesForIndex 599 // would only include the module we are writing and index for). 600 assert(ModuleToSummariesForIndex->size() == 1); 601 continue; 602 } 603 Callback(*MPI); 604 } 605 } else { 606 // Since StringMap iteration order isn't guaranteed, order by path string 607 // first. 608 // FIXME: Make this a vector of StringMapEntry instead to avoid the later 609 // map lookup. 610 std::vector<StringRef> ModulePaths; 611 for (auto &[ModPath, _] : Index.modulePaths()) 612 ModulePaths.push_back(ModPath); 613 llvm::sort(ModulePaths.begin(), ModulePaths.end()); 614 for (auto &ModPath : ModulePaths) 615 Callback(*Index.modulePaths().find(ModPath)); 616 } 617 } 618 619 /// Main entry point for writing a combined index to bitcode. 620 void write(); 621 622 private: 623 void writeModStrings(); 624 void writeCombinedGlobalValueSummary(); 625 626 std::optional<unsigned> getValueId(GlobalValue::GUID ValGUID) { 627 auto VMI = GUIDToValueIdMap.find(ValGUID); 628 if (VMI == GUIDToValueIdMap.end()) 629 return std::nullopt; 630 return VMI->second; 631 } 632 633 std::map<GlobalValue::GUID, unsigned> &valueIds() { return GUIDToValueIdMap; } 634 }; 635 636 } // end anonymous namespace 637 638 static unsigned getEncodedCastOpcode(unsigned Opcode) { 639 switch (Opcode) { 640 default: llvm_unreachable("Unknown cast instruction!"); 641 case Instruction::Trunc : return bitc::CAST_TRUNC; 642 case Instruction::ZExt : return bitc::CAST_ZEXT; 643 case Instruction::SExt : return bitc::CAST_SEXT; 644 case Instruction::FPToUI : return bitc::CAST_FPTOUI; 645 case Instruction::FPToSI : return bitc::CAST_FPTOSI; 646 case Instruction::UIToFP : return bitc::CAST_UITOFP; 647 case Instruction::SIToFP : return bitc::CAST_SITOFP; 648 case Instruction::FPTrunc : return bitc::CAST_FPTRUNC; 649 case Instruction::FPExt : return bitc::CAST_FPEXT; 650 case Instruction::PtrToInt: return bitc::CAST_PTRTOINT; 651 case Instruction::IntToPtr: return bitc::CAST_INTTOPTR; 652 case Instruction::BitCast : return bitc::CAST_BITCAST; 653 case Instruction::AddrSpaceCast: return bitc::CAST_ADDRSPACECAST; 654 } 655 } 656 657 static unsigned getEncodedUnaryOpcode(unsigned Opcode) { 658 switch (Opcode) { 659 default: llvm_unreachable("Unknown binary instruction!"); 660 case Instruction::FNeg: return bitc::UNOP_FNEG; 661 } 662 } 663 664 static unsigned getEncodedBinaryOpcode(unsigned Opcode) { 665 switch (Opcode) { 666 default: llvm_unreachable("Unknown binary instruction!"); 667 case Instruction::Add: 668 case Instruction::FAdd: return bitc::BINOP_ADD; 669 case Instruction::Sub: 670 case Instruction::FSub: return bitc::BINOP_SUB; 671 case Instruction::Mul: 672 case Instruction::FMul: return bitc::BINOP_MUL; 673 case Instruction::UDiv: return bitc::BINOP_UDIV; 674 case Instruction::FDiv: 675 case Instruction::SDiv: return bitc::BINOP_SDIV; 676 case Instruction::URem: return bitc::BINOP_UREM; 677 case Instruction::FRem: 678 case Instruction::SRem: return bitc::BINOP_SREM; 679 case Instruction::Shl: return bitc::BINOP_SHL; 680 case Instruction::LShr: return bitc::BINOP_LSHR; 681 case Instruction::AShr: return bitc::BINOP_ASHR; 682 case Instruction::And: return bitc::BINOP_AND; 683 case Instruction::Or: return bitc::BINOP_OR; 684 case Instruction::Xor: return bitc::BINOP_XOR; 685 } 686 } 687 688 static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) { 689 switch (Op) { 690 default: llvm_unreachable("Unknown RMW operation!"); 691 case AtomicRMWInst::Xchg: return bitc::RMW_XCHG; 692 case AtomicRMWInst::Add: return bitc::RMW_ADD; 693 case AtomicRMWInst::Sub: return bitc::RMW_SUB; 694 case AtomicRMWInst::And: return bitc::RMW_AND; 695 case AtomicRMWInst::Nand: return bitc::RMW_NAND; 696 case AtomicRMWInst::Or: return bitc::RMW_OR; 697 case AtomicRMWInst::Xor: return bitc::RMW_XOR; 698 case AtomicRMWInst::Max: return bitc::RMW_MAX; 699 case AtomicRMWInst::Min: return bitc::RMW_MIN; 700 case AtomicRMWInst::UMax: return bitc::RMW_UMAX; 701 case AtomicRMWInst::UMin: return bitc::RMW_UMIN; 702 case AtomicRMWInst::FAdd: return bitc::RMW_FADD; 703 case AtomicRMWInst::FSub: return bitc::RMW_FSUB; 704 case AtomicRMWInst::FMax: return bitc::RMW_FMAX; 705 case AtomicRMWInst::FMin: return bitc::RMW_FMIN; 706 case AtomicRMWInst::FMaximum: 707 return bitc::RMW_FMAXIMUM; 708 case AtomicRMWInst::FMinimum: 709 return bitc::RMW_FMINIMUM; 710 case AtomicRMWInst::UIncWrap: 711 return bitc::RMW_UINC_WRAP; 712 case AtomicRMWInst::UDecWrap: 713 return bitc::RMW_UDEC_WRAP; 714 case AtomicRMWInst::USubCond: 715 return bitc::RMW_USUB_COND; 716 case AtomicRMWInst::USubSat: 717 return bitc::RMW_USUB_SAT; 718 } 719 } 720 721 static unsigned getEncodedOrdering(AtomicOrdering Ordering) { 722 switch (Ordering) { 723 case AtomicOrdering::NotAtomic: return bitc::ORDERING_NOTATOMIC; 724 case AtomicOrdering::Unordered: return bitc::ORDERING_UNORDERED; 725 case AtomicOrdering::Monotonic: return bitc::ORDERING_MONOTONIC; 726 case AtomicOrdering::Acquire: return bitc::ORDERING_ACQUIRE; 727 case AtomicOrdering::Release: return bitc::ORDERING_RELEASE; 728 case AtomicOrdering::AcquireRelease: return bitc::ORDERING_ACQREL; 729 case AtomicOrdering::SequentiallyConsistent: return bitc::ORDERING_SEQCST; 730 } 731 llvm_unreachable("Invalid ordering"); 732 } 733 734 static void writeStringRecord(BitstreamWriter &Stream, unsigned Code, 735 StringRef Str, unsigned AbbrevToUse) { 736 SmallVector<unsigned, 64> Vals; 737 738 // Code: [strchar x N] 739 for (char C : Str) { 740 if (AbbrevToUse && !BitCodeAbbrevOp::isChar6(C)) 741 AbbrevToUse = 0; 742 Vals.push_back(C); 743 } 744 745 // Emit the finished record. 746 Stream.EmitRecord(Code, Vals, AbbrevToUse); 747 } 748 749 static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) { 750 switch (Kind) { 751 case Attribute::Alignment: 752 return bitc::ATTR_KIND_ALIGNMENT; 753 case Attribute::AllocAlign: 754 return bitc::ATTR_KIND_ALLOC_ALIGN; 755 case Attribute::AllocSize: 756 return bitc::ATTR_KIND_ALLOC_SIZE; 757 case Attribute::AlwaysInline: 758 return bitc::ATTR_KIND_ALWAYS_INLINE; 759 case Attribute::Builtin: 760 return bitc::ATTR_KIND_BUILTIN; 761 case Attribute::ByVal: 762 return bitc::ATTR_KIND_BY_VAL; 763 case Attribute::Convergent: 764 return bitc::ATTR_KIND_CONVERGENT; 765 case Attribute::InAlloca: 766 return bitc::ATTR_KIND_IN_ALLOCA; 767 case Attribute::Cold: 768 return bitc::ATTR_KIND_COLD; 769 case Attribute::DisableSanitizerInstrumentation: 770 return bitc::ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION; 771 case Attribute::FnRetThunkExtern: 772 return bitc::ATTR_KIND_FNRETTHUNK_EXTERN; 773 case Attribute::Hot: 774 return bitc::ATTR_KIND_HOT; 775 case Attribute::ElementType: 776 return bitc::ATTR_KIND_ELEMENTTYPE; 777 case Attribute::HybridPatchable: 778 return bitc::ATTR_KIND_HYBRID_PATCHABLE; 779 case Attribute::InlineHint: 780 return bitc::ATTR_KIND_INLINE_HINT; 781 case Attribute::InReg: 782 return bitc::ATTR_KIND_IN_REG; 783 case Attribute::JumpTable: 784 return bitc::ATTR_KIND_JUMP_TABLE; 785 case Attribute::MinSize: 786 return bitc::ATTR_KIND_MIN_SIZE; 787 case Attribute::AllocatedPointer: 788 return bitc::ATTR_KIND_ALLOCATED_POINTER; 789 case Attribute::AllocKind: 790 return bitc::ATTR_KIND_ALLOC_KIND; 791 case Attribute::Memory: 792 return bitc::ATTR_KIND_MEMORY; 793 case Attribute::NoFPClass: 794 return bitc::ATTR_KIND_NOFPCLASS; 795 case Attribute::Naked: 796 return bitc::ATTR_KIND_NAKED; 797 case Attribute::Nest: 798 return bitc::ATTR_KIND_NEST; 799 case Attribute::NoAlias: 800 return bitc::ATTR_KIND_NO_ALIAS; 801 case Attribute::NoBuiltin: 802 return bitc::ATTR_KIND_NO_BUILTIN; 803 case Attribute::NoCallback: 804 return bitc::ATTR_KIND_NO_CALLBACK; 805 case Attribute::NoDivergenceSource: 806 return bitc::ATTR_KIND_NO_DIVERGENCE_SOURCE; 807 case Attribute::NoDuplicate: 808 return bitc::ATTR_KIND_NO_DUPLICATE; 809 case Attribute::NoFree: 810 return bitc::ATTR_KIND_NOFREE; 811 case Attribute::NoImplicitFloat: 812 return bitc::ATTR_KIND_NO_IMPLICIT_FLOAT; 813 case Attribute::NoInline: 814 return bitc::ATTR_KIND_NO_INLINE; 815 case Attribute::NoRecurse: 816 return bitc::ATTR_KIND_NO_RECURSE; 817 case Attribute::NoMerge: 818 return bitc::ATTR_KIND_NO_MERGE; 819 case Attribute::NonLazyBind: 820 return bitc::ATTR_KIND_NON_LAZY_BIND; 821 case Attribute::NonNull: 822 return bitc::ATTR_KIND_NON_NULL; 823 case Attribute::Dereferenceable: 824 return bitc::ATTR_KIND_DEREFERENCEABLE; 825 case Attribute::DereferenceableOrNull: 826 return bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL; 827 case Attribute::NoRedZone: 828 return bitc::ATTR_KIND_NO_RED_ZONE; 829 case Attribute::NoReturn: 830 return bitc::ATTR_KIND_NO_RETURN; 831 case Attribute::NoSync: 832 return bitc::ATTR_KIND_NOSYNC; 833 case Attribute::NoCfCheck: 834 return bitc::ATTR_KIND_NOCF_CHECK; 835 case Attribute::NoProfile: 836 return bitc::ATTR_KIND_NO_PROFILE; 837 case Attribute::SkipProfile: 838 return bitc::ATTR_KIND_SKIP_PROFILE; 839 case Attribute::NoUnwind: 840 return bitc::ATTR_KIND_NO_UNWIND; 841 case Attribute::NoSanitizeBounds: 842 return bitc::ATTR_KIND_NO_SANITIZE_BOUNDS; 843 case Attribute::NoSanitizeCoverage: 844 return bitc::ATTR_KIND_NO_SANITIZE_COVERAGE; 845 case Attribute::NullPointerIsValid: 846 return bitc::ATTR_KIND_NULL_POINTER_IS_VALID; 847 case Attribute::OptimizeForDebugging: 848 return bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING; 849 case Attribute::OptForFuzzing: 850 return bitc::ATTR_KIND_OPT_FOR_FUZZING; 851 case Attribute::OptimizeForSize: 852 return bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE; 853 case Attribute::OptimizeNone: 854 return bitc::ATTR_KIND_OPTIMIZE_NONE; 855 case Attribute::ReadNone: 856 return bitc::ATTR_KIND_READ_NONE; 857 case Attribute::ReadOnly: 858 return bitc::ATTR_KIND_READ_ONLY; 859 case Attribute::Returned: 860 return bitc::ATTR_KIND_RETURNED; 861 case Attribute::ReturnsTwice: 862 return bitc::ATTR_KIND_RETURNS_TWICE; 863 case Attribute::SExt: 864 return bitc::ATTR_KIND_S_EXT; 865 case Attribute::Speculatable: 866 return bitc::ATTR_KIND_SPECULATABLE; 867 case Attribute::StackAlignment: 868 return bitc::ATTR_KIND_STACK_ALIGNMENT; 869 case Attribute::StackProtect: 870 return bitc::ATTR_KIND_STACK_PROTECT; 871 case Attribute::StackProtectReq: 872 return bitc::ATTR_KIND_STACK_PROTECT_REQ; 873 case Attribute::StackProtectStrong: 874 return bitc::ATTR_KIND_STACK_PROTECT_STRONG; 875 case Attribute::SafeStack: 876 return bitc::ATTR_KIND_SAFESTACK; 877 case Attribute::ShadowCallStack: 878 return bitc::ATTR_KIND_SHADOWCALLSTACK; 879 case Attribute::StrictFP: 880 return bitc::ATTR_KIND_STRICT_FP; 881 case Attribute::StructRet: 882 return bitc::ATTR_KIND_STRUCT_RET; 883 case Attribute::SanitizeAddress: 884 return bitc::ATTR_KIND_SANITIZE_ADDRESS; 885 case Attribute::SanitizeHWAddress: 886 return bitc::ATTR_KIND_SANITIZE_HWADDRESS; 887 case Attribute::SanitizeThread: 888 return bitc::ATTR_KIND_SANITIZE_THREAD; 889 case Attribute::SanitizeType: 890 return bitc::ATTR_KIND_SANITIZE_TYPE; 891 case Attribute::SanitizeMemory: 892 return bitc::ATTR_KIND_SANITIZE_MEMORY; 893 case Attribute::SanitizeNumericalStability: 894 return bitc::ATTR_KIND_SANITIZE_NUMERICAL_STABILITY; 895 case Attribute::SanitizeRealtime: 896 return bitc::ATTR_KIND_SANITIZE_REALTIME; 897 case Attribute::SanitizeRealtimeBlocking: 898 return bitc::ATTR_KIND_SANITIZE_REALTIME_BLOCKING; 899 case Attribute::SpeculativeLoadHardening: 900 return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING; 901 case Attribute::SwiftError: 902 return bitc::ATTR_KIND_SWIFT_ERROR; 903 case Attribute::SwiftSelf: 904 return bitc::ATTR_KIND_SWIFT_SELF; 905 case Attribute::SwiftAsync: 906 return bitc::ATTR_KIND_SWIFT_ASYNC; 907 case Attribute::UWTable: 908 return bitc::ATTR_KIND_UW_TABLE; 909 case Attribute::VScaleRange: 910 return bitc::ATTR_KIND_VSCALE_RANGE; 911 case Attribute::WillReturn: 912 return bitc::ATTR_KIND_WILLRETURN; 913 case Attribute::WriteOnly: 914 return bitc::ATTR_KIND_WRITEONLY; 915 case Attribute::ZExt: 916 return bitc::ATTR_KIND_Z_EXT; 917 case Attribute::ImmArg: 918 return bitc::ATTR_KIND_IMMARG; 919 case Attribute::SanitizeMemTag: 920 return bitc::ATTR_KIND_SANITIZE_MEMTAG; 921 case Attribute::Preallocated: 922 return bitc::ATTR_KIND_PREALLOCATED; 923 case Attribute::NoUndef: 924 return bitc::ATTR_KIND_NOUNDEF; 925 case Attribute::ByRef: 926 return bitc::ATTR_KIND_BYREF; 927 case Attribute::MustProgress: 928 return bitc::ATTR_KIND_MUSTPROGRESS; 929 case Attribute::PresplitCoroutine: 930 return bitc::ATTR_KIND_PRESPLIT_COROUTINE; 931 case Attribute::Writable: 932 return bitc::ATTR_KIND_WRITABLE; 933 case Attribute::CoroDestroyOnlyWhenComplete: 934 return bitc::ATTR_KIND_CORO_ONLY_DESTROY_WHEN_COMPLETE; 935 case Attribute::CoroElideSafe: 936 return bitc::ATTR_KIND_CORO_ELIDE_SAFE; 937 case Attribute::DeadOnUnwind: 938 return bitc::ATTR_KIND_DEAD_ON_UNWIND; 939 case Attribute::Range: 940 return bitc::ATTR_KIND_RANGE; 941 case Attribute::Initializes: 942 return bitc::ATTR_KIND_INITIALIZES; 943 case Attribute::NoExt: 944 return bitc::ATTR_KIND_NO_EXT; 945 case Attribute::Captures: 946 return bitc::ATTR_KIND_CAPTURES; 947 case Attribute::DeadOnReturn: 948 return bitc::ATTR_KIND_DEAD_ON_RETURN; 949 case Attribute::EndAttrKinds: 950 llvm_unreachable("Can not encode end-attribute kinds marker."); 951 case Attribute::None: 952 llvm_unreachable("Can not encode none-attribute."); 953 case Attribute::EmptyKey: 954 case Attribute::TombstoneKey: 955 llvm_unreachable("Trying to encode EmptyKey/TombstoneKey"); 956 } 957 958 llvm_unreachable("Trying to encode unknown attribute"); 959 } 960 961 static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { 962 if ((int64_t)V >= 0) 963 Vals.push_back(V << 1); 964 else 965 Vals.push_back((-V << 1) | 1); 966 } 967 968 static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) { 969 // We have an arbitrary precision integer value to write whose 970 // bit width is > 64. However, in canonical unsigned integer 971 // format it is likely that the high bits are going to be zero. 972 // So, we only write the number of active words. 973 unsigned NumWords = A.getActiveWords(); 974 const uint64_t *RawData = A.getRawData(); 975 for (unsigned i = 0; i < NumWords; i++) 976 emitSignedInt64(Vals, RawData[i]); 977 } 978 979 static void emitConstantRange(SmallVectorImpl<uint64_t> &Record, 980 const ConstantRange &CR, bool EmitBitWidth) { 981 unsigned BitWidth = CR.getBitWidth(); 982 if (EmitBitWidth) 983 Record.push_back(BitWidth); 984 if (BitWidth > 64) { 985 Record.push_back(CR.getLower().getActiveWords() | 986 (uint64_t(CR.getUpper().getActiveWords()) << 32)); 987 emitWideAPInt(Record, CR.getLower()); 988 emitWideAPInt(Record, CR.getUpper()); 989 } else { 990 emitSignedInt64(Record, CR.getLower().getSExtValue()); 991 emitSignedInt64(Record, CR.getUpper().getSExtValue()); 992 } 993 } 994 995 void ModuleBitcodeWriter::writeAttributeGroupTable() { 996 const std::vector<ValueEnumerator::IndexAndAttrSet> &AttrGrps = 997 VE.getAttributeGroups(); 998 if (AttrGrps.empty()) return; 999 1000 Stream.EnterSubblock(bitc::PARAMATTR_GROUP_BLOCK_ID, 3); 1001 1002 SmallVector<uint64_t, 64> Record; 1003 for (ValueEnumerator::IndexAndAttrSet Pair : AttrGrps) { 1004 unsigned AttrListIndex = Pair.first; 1005 AttributeSet AS = Pair.second; 1006 Record.push_back(VE.getAttributeGroupID(Pair)); 1007 Record.push_back(AttrListIndex); 1008 1009 for (Attribute Attr : AS) { 1010 if (Attr.isEnumAttribute()) { 1011 Record.push_back(0); 1012 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 1013 } else if (Attr.isIntAttribute()) { 1014 Record.push_back(1); 1015 Attribute::AttrKind Kind = Attr.getKindAsEnum(); 1016 Record.push_back(getAttrKindEncoding(Kind)); 1017 if (Kind == Attribute::Memory) { 1018 // Version field for upgrading old memory effects. 1019 const uint64_t Version = 1; 1020 Record.push_back((Version << 56) | Attr.getValueAsInt()); 1021 } else { 1022 Record.push_back(Attr.getValueAsInt()); 1023 } 1024 } else if (Attr.isStringAttribute()) { 1025 StringRef Kind = Attr.getKindAsString(); 1026 StringRef Val = Attr.getValueAsString(); 1027 1028 Record.push_back(Val.empty() ? 3 : 4); 1029 Record.append(Kind.begin(), Kind.end()); 1030 Record.push_back(0); 1031 if (!Val.empty()) { 1032 Record.append(Val.begin(), Val.end()); 1033 Record.push_back(0); 1034 } 1035 } else if (Attr.isTypeAttribute()) { 1036 Type *Ty = Attr.getValueAsType(); 1037 Record.push_back(Ty ? 6 : 5); 1038 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 1039 if (Ty) 1040 Record.push_back(VE.getTypeID(Attr.getValueAsType())); 1041 } else if (Attr.isConstantRangeAttribute()) { 1042 Record.push_back(7); 1043 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 1044 emitConstantRange(Record, Attr.getValueAsConstantRange(), 1045 /*EmitBitWidth=*/true); 1046 } else { 1047 assert(Attr.isConstantRangeListAttribute()); 1048 Record.push_back(8); 1049 Record.push_back(getAttrKindEncoding(Attr.getKindAsEnum())); 1050 ArrayRef<ConstantRange> Val = Attr.getValueAsConstantRangeList(); 1051 Record.push_back(Val.size()); 1052 Record.push_back(Val[0].getBitWidth()); 1053 for (auto &CR : Val) 1054 emitConstantRange(Record, CR, /*EmitBitWidth=*/false); 1055 } 1056 } 1057 1058 Stream.EmitRecord(bitc::PARAMATTR_GRP_CODE_ENTRY, Record); 1059 Record.clear(); 1060 } 1061 1062 Stream.ExitBlock(); 1063 } 1064 1065 void ModuleBitcodeWriter::writeAttributeTable() { 1066 const std::vector<AttributeList> &Attrs = VE.getAttributeLists(); 1067 if (Attrs.empty()) return; 1068 1069 Stream.EnterSubblock(bitc::PARAMATTR_BLOCK_ID, 3); 1070 1071 SmallVector<uint64_t, 64> Record; 1072 for (const AttributeList &AL : Attrs) { 1073 for (unsigned i : AL.indexes()) { 1074 AttributeSet AS = AL.getAttributes(i); 1075 if (AS.hasAttributes()) 1076 Record.push_back(VE.getAttributeGroupID({i, AS})); 1077 } 1078 1079 Stream.EmitRecord(bitc::PARAMATTR_CODE_ENTRY, Record); 1080 Record.clear(); 1081 } 1082 1083 Stream.ExitBlock(); 1084 } 1085 1086 /// WriteTypeTable - Write out the type table for a module. 1087 void ModuleBitcodeWriter::writeTypeTable() { 1088 const ValueEnumerator::TypeList &TypeList = VE.getTypes(); 1089 1090 Stream.EnterSubblock(bitc::TYPE_BLOCK_ID_NEW, 4 /*count from # abbrevs */); 1091 SmallVector<uint64_t, 64> TypeVals; 1092 1093 uint64_t NumBits = VE.computeBitsRequiredForTypeIndices(); 1094 1095 // Abbrev for TYPE_CODE_OPAQUE_POINTER. 1096 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1097 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_OPAQUE_POINTER)); 1098 Abbv->Add(BitCodeAbbrevOp(0)); // Addrspace = 0 1099 unsigned OpaquePtrAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1100 1101 // Abbrev for TYPE_CODE_FUNCTION. 1102 Abbv = std::make_shared<BitCodeAbbrev>(); 1103 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_FUNCTION)); 1104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isvararg 1105 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1106 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 1107 unsigned FunctionAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1108 1109 // Abbrev for TYPE_CODE_STRUCT_ANON. 1110 Abbv = std::make_shared<BitCodeAbbrev>(); 1111 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_ANON)); 1112 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 1113 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1114 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 1115 unsigned StructAnonAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1116 1117 // Abbrev for TYPE_CODE_STRUCT_NAME. 1118 Abbv = std::make_shared<BitCodeAbbrev>(); 1119 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAME)); 1120 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1121 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 1122 unsigned StructNameAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1123 1124 // Abbrev for TYPE_CODE_STRUCT_NAMED. 1125 Abbv = std::make_shared<BitCodeAbbrev>(); 1126 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_STRUCT_NAMED)); 1127 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ispacked 1128 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1129 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 1130 unsigned StructNamedAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1131 1132 // Abbrev for TYPE_CODE_ARRAY. 1133 Abbv = std::make_shared<BitCodeAbbrev>(); 1134 Abbv->Add(BitCodeAbbrevOp(bitc::TYPE_CODE_ARRAY)); 1135 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // size 1136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, NumBits)); 1137 unsigned ArrayAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1138 1139 // Emit an entry count so the reader can reserve space. 1140 TypeVals.push_back(TypeList.size()); 1141 Stream.EmitRecord(bitc::TYPE_CODE_NUMENTRY, TypeVals); 1142 TypeVals.clear(); 1143 1144 // Loop over all of the types, emitting each in turn. 1145 for (Type *T : TypeList) { 1146 int AbbrevToUse = 0; 1147 unsigned Code = 0; 1148 1149 switch (T->getTypeID()) { 1150 case Type::VoidTyID: Code = bitc::TYPE_CODE_VOID; break; 1151 case Type::HalfTyID: Code = bitc::TYPE_CODE_HALF; break; 1152 case Type::BFloatTyID: Code = bitc::TYPE_CODE_BFLOAT; break; 1153 case Type::FloatTyID: Code = bitc::TYPE_CODE_FLOAT; break; 1154 case Type::DoubleTyID: Code = bitc::TYPE_CODE_DOUBLE; break; 1155 case Type::X86_FP80TyID: Code = bitc::TYPE_CODE_X86_FP80; break; 1156 case Type::FP128TyID: Code = bitc::TYPE_CODE_FP128; break; 1157 case Type::PPC_FP128TyID: Code = bitc::TYPE_CODE_PPC_FP128; break; 1158 case Type::LabelTyID: Code = bitc::TYPE_CODE_LABEL; break; 1159 case Type::MetadataTyID: 1160 Code = bitc::TYPE_CODE_METADATA; 1161 break; 1162 case Type::X86_AMXTyID: Code = bitc::TYPE_CODE_X86_AMX; break; 1163 case Type::TokenTyID: Code = bitc::TYPE_CODE_TOKEN; break; 1164 case Type::IntegerTyID: 1165 // INTEGER: [width] 1166 Code = bitc::TYPE_CODE_INTEGER; 1167 TypeVals.push_back(cast<IntegerType>(T)->getBitWidth()); 1168 break; 1169 case Type::PointerTyID: { 1170 PointerType *PTy = cast<PointerType>(T); 1171 unsigned AddressSpace = PTy->getAddressSpace(); 1172 // OPAQUE_POINTER: [address space] 1173 Code = bitc::TYPE_CODE_OPAQUE_POINTER; 1174 TypeVals.push_back(AddressSpace); 1175 if (AddressSpace == 0) 1176 AbbrevToUse = OpaquePtrAbbrev; 1177 break; 1178 } 1179 case Type::FunctionTyID: { 1180 FunctionType *FT = cast<FunctionType>(T); 1181 // FUNCTION: [isvararg, retty, paramty x N] 1182 Code = bitc::TYPE_CODE_FUNCTION; 1183 TypeVals.push_back(FT->isVarArg()); 1184 TypeVals.push_back(VE.getTypeID(FT->getReturnType())); 1185 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) 1186 TypeVals.push_back(VE.getTypeID(FT->getParamType(i))); 1187 AbbrevToUse = FunctionAbbrev; 1188 break; 1189 } 1190 case Type::StructTyID: { 1191 StructType *ST = cast<StructType>(T); 1192 // STRUCT: [ispacked, eltty x N] 1193 TypeVals.push_back(ST->isPacked()); 1194 // Output all of the element types. 1195 for (Type *ET : ST->elements()) 1196 TypeVals.push_back(VE.getTypeID(ET)); 1197 1198 if (ST->isLiteral()) { 1199 Code = bitc::TYPE_CODE_STRUCT_ANON; 1200 AbbrevToUse = StructAnonAbbrev; 1201 } else { 1202 if (ST->isOpaque()) { 1203 Code = bitc::TYPE_CODE_OPAQUE; 1204 } else { 1205 Code = bitc::TYPE_CODE_STRUCT_NAMED; 1206 AbbrevToUse = StructNamedAbbrev; 1207 } 1208 1209 // Emit the name if it is present. 1210 if (!ST->getName().empty()) 1211 writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, ST->getName(), 1212 StructNameAbbrev); 1213 } 1214 break; 1215 } 1216 case Type::ArrayTyID: { 1217 ArrayType *AT = cast<ArrayType>(T); 1218 // ARRAY: [numelts, eltty] 1219 Code = bitc::TYPE_CODE_ARRAY; 1220 TypeVals.push_back(AT->getNumElements()); 1221 TypeVals.push_back(VE.getTypeID(AT->getElementType())); 1222 AbbrevToUse = ArrayAbbrev; 1223 break; 1224 } 1225 case Type::FixedVectorTyID: 1226 case Type::ScalableVectorTyID: { 1227 VectorType *VT = cast<VectorType>(T); 1228 // VECTOR [numelts, eltty] or 1229 // [numelts, eltty, scalable] 1230 Code = bitc::TYPE_CODE_VECTOR; 1231 TypeVals.push_back(VT->getElementCount().getKnownMinValue()); 1232 TypeVals.push_back(VE.getTypeID(VT->getElementType())); 1233 if (isa<ScalableVectorType>(VT)) 1234 TypeVals.push_back(true); 1235 break; 1236 } 1237 case Type::TargetExtTyID: { 1238 TargetExtType *TET = cast<TargetExtType>(T); 1239 Code = bitc::TYPE_CODE_TARGET_TYPE; 1240 writeStringRecord(Stream, bitc::TYPE_CODE_STRUCT_NAME, TET->getName(), 1241 StructNameAbbrev); 1242 TypeVals.push_back(TET->getNumTypeParameters()); 1243 for (Type *InnerTy : TET->type_params()) 1244 TypeVals.push_back(VE.getTypeID(InnerTy)); 1245 llvm::append_range(TypeVals, TET->int_params()); 1246 break; 1247 } 1248 case Type::TypedPointerTyID: 1249 llvm_unreachable("Typed pointers cannot be added to IR modules"); 1250 } 1251 1252 // Emit the finished record. 1253 Stream.EmitRecord(Code, TypeVals, AbbrevToUse); 1254 TypeVals.clear(); 1255 } 1256 1257 Stream.ExitBlock(); 1258 } 1259 1260 static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) { 1261 switch (Linkage) { 1262 case GlobalValue::ExternalLinkage: 1263 return 0; 1264 case GlobalValue::WeakAnyLinkage: 1265 return 16; 1266 case GlobalValue::AppendingLinkage: 1267 return 2; 1268 case GlobalValue::InternalLinkage: 1269 return 3; 1270 case GlobalValue::LinkOnceAnyLinkage: 1271 return 18; 1272 case GlobalValue::ExternalWeakLinkage: 1273 return 7; 1274 case GlobalValue::CommonLinkage: 1275 return 8; 1276 case GlobalValue::PrivateLinkage: 1277 return 9; 1278 case GlobalValue::WeakODRLinkage: 1279 return 17; 1280 case GlobalValue::LinkOnceODRLinkage: 1281 return 19; 1282 case GlobalValue::AvailableExternallyLinkage: 1283 return 12; 1284 } 1285 llvm_unreachable("Invalid linkage"); 1286 } 1287 1288 static unsigned getEncodedLinkage(const GlobalValue &GV) { 1289 return getEncodedLinkage(GV.getLinkage()); 1290 } 1291 1292 static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) { 1293 uint64_t RawFlags = 0; 1294 RawFlags |= Flags.ReadNone; 1295 RawFlags |= (Flags.ReadOnly << 1); 1296 RawFlags |= (Flags.NoRecurse << 2); 1297 RawFlags |= (Flags.ReturnDoesNotAlias << 3); 1298 RawFlags |= (Flags.NoInline << 4); 1299 RawFlags |= (Flags.AlwaysInline << 5); 1300 RawFlags |= (Flags.NoUnwind << 6); 1301 RawFlags |= (Flags.MayThrow << 7); 1302 RawFlags |= (Flags.HasUnknownCall << 8); 1303 RawFlags |= (Flags.MustBeUnreachable << 9); 1304 return RawFlags; 1305 } 1306 1307 // Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags 1308 // in BitcodeReader.cpp. 1309 static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags, 1310 bool ImportAsDecl = false) { 1311 uint64_t RawFlags = 0; 1312 1313 RawFlags |= Flags.NotEligibleToImport; // bool 1314 RawFlags |= (Flags.Live << 1); 1315 RawFlags |= (Flags.DSOLocal << 2); 1316 RawFlags |= (Flags.CanAutoHide << 3); 1317 1318 // Linkage don't need to be remapped at that time for the summary. Any future 1319 // change to the getEncodedLinkage() function will need to be taken into 1320 // account here as well. 1321 RawFlags = (RawFlags << 4) | Flags.Linkage; // 4 bits 1322 1323 RawFlags |= (Flags.Visibility << 8); // 2 bits 1324 1325 unsigned ImportType = Flags.ImportType | ImportAsDecl; 1326 RawFlags |= (ImportType << 10); // 1 bit 1327 1328 return RawFlags; 1329 } 1330 1331 static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags) { 1332 uint64_t RawFlags = Flags.MaybeReadOnly | (Flags.MaybeWriteOnly << 1) | 1333 (Flags.Constant << 2) | Flags.VCallVisibility << 3; 1334 return RawFlags; 1335 } 1336 1337 static uint64_t getEncodedHotnessCallEdgeInfo(const CalleeInfo &CI) { 1338 uint64_t RawFlags = 0; 1339 1340 RawFlags |= CI.Hotness; // 3 bits 1341 RawFlags |= (CI.HasTailCall << 3); // 1 bit 1342 1343 return RawFlags; 1344 } 1345 1346 static uint64_t getEncodedRelBFCallEdgeInfo(const CalleeInfo &CI) { 1347 uint64_t RawFlags = 0; 1348 1349 RawFlags |= CI.RelBlockFreq; // CalleeInfo::RelBlockFreqBits bits 1350 RawFlags |= (CI.HasTailCall << CalleeInfo::RelBlockFreqBits); // 1 bit 1351 1352 return RawFlags; 1353 } 1354 1355 static unsigned getEncodedVisibility(const GlobalValue &GV) { 1356 switch (GV.getVisibility()) { 1357 case GlobalValue::DefaultVisibility: return 0; 1358 case GlobalValue::HiddenVisibility: return 1; 1359 case GlobalValue::ProtectedVisibility: return 2; 1360 } 1361 llvm_unreachable("Invalid visibility"); 1362 } 1363 1364 static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) { 1365 switch (GV.getDLLStorageClass()) { 1366 case GlobalValue::DefaultStorageClass: return 0; 1367 case GlobalValue::DLLImportStorageClass: return 1; 1368 case GlobalValue::DLLExportStorageClass: return 2; 1369 } 1370 llvm_unreachable("Invalid DLL storage class"); 1371 } 1372 1373 static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) { 1374 switch (GV.getThreadLocalMode()) { 1375 case GlobalVariable::NotThreadLocal: return 0; 1376 case GlobalVariable::GeneralDynamicTLSModel: return 1; 1377 case GlobalVariable::LocalDynamicTLSModel: return 2; 1378 case GlobalVariable::InitialExecTLSModel: return 3; 1379 case GlobalVariable::LocalExecTLSModel: return 4; 1380 } 1381 llvm_unreachable("Invalid TLS model"); 1382 } 1383 1384 static unsigned getEncodedComdatSelectionKind(const Comdat &C) { 1385 switch (C.getSelectionKind()) { 1386 case Comdat::Any: 1387 return bitc::COMDAT_SELECTION_KIND_ANY; 1388 case Comdat::ExactMatch: 1389 return bitc::COMDAT_SELECTION_KIND_EXACT_MATCH; 1390 case Comdat::Largest: 1391 return bitc::COMDAT_SELECTION_KIND_LARGEST; 1392 case Comdat::NoDeduplicate: 1393 return bitc::COMDAT_SELECTION_KIND_NO_DUPLICATES; 1394 case Comdat::SameSize: 1395 return bitc::COMDAT_SELECTION_KIND_SAME_SIZE; 1396 } 1397 llvm_unreachable("Invalid selection kind"); 1398 } 1399 1400 static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) { 1401 switch (GV.getUnnamedAddr()) { 1402 case GlobalValue::UnnamedAddr::None: return 0; 1403 case GlobalValue::UnnamedAddr::Local: return 2; 1404 case GlobalValue::UnnamedAddr::Global: return 1; 1405 } 1406 llvm_unreachable("Invalid unnamed_addr"); 1407 } 1408 1409 size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) { 1410 if (GenerateHash) 1411 Hasher.update(Str); 1412 return StrtabBuilder.add(Str); 1413 } 1414 1415 void ModuleBitcodeWriter::writeComdats() { 1416 SmallVector<unsigned, 64> Vals; 1417 for (const Comdat *C : VE.getComdats()) { 1418 // COMDAT: [strtab offset, strtab size, selection_kind] 1419 Vals.push_back(addToStrtab(C->getName())); 1420 Vals.push_back(C->getName().size()); 1421 Vals.push_back(getEncodedComdatSelectionKind(*C)); 1422 Stream.EmitRecord(bitc::MODULE_CODE_COMDAT, Vals, /*AbbrevToUse=*/0); 1423 Vals.clear(); 1424 } 1425 } 1426 1427 /// Write a record that will eventually hold the word offset of the 1428 /// module-level VST. For now the offset is 0, which will be backpatched 1429 /// after the real VST is written. Saves the bit offset to backpatch. 1430 void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() { 1431 // Write a placeholder value in for the offset of the real VST, 1432 // which is written after the function blocks so that it can include 1433 // the offset of each function. The placeholder offset will be 1434 // updated when the real VST is written. 1435 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1436 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_VSTOFFSET)); 1437 // Blocks are 32-bit aligned, so we can use a 32-bit word offset to 1438 // hold the real VST offset. Must use fixed instead of VBR as we don't 1439 // know how many VBR chunks to reserve ahead of time. 1440 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 1441 unsigned VSTOffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1442 1443 // Emit the placeholder 1444 uint64_t Vals[] = {bitc::MODULE_CODE_VSTOFFSET, 0}; 1445 Stream.EmitRecordWithAbbrev(VSTOffsetAbbrev, Vals); 1446 1447 // Compute and save the bit offset to the placeholder, which will be 1448 // patched when the real VST is written. We can simply subtract the 32-bit 1449 // fixed size from the current bit number to get the location to backpatch. 1450 VSTOffsetPlaceholder = Stream.GetCurrentBitNo() - 32; 1451 } 1452 1453 enum StringEncoding { SE_Char6, SE_Fixed7, SE_Fixed8 }; 1454 1455 /// Determine the encoding to use for the given string name and length. 1456 static StringEncoding getStringEncoding(StringRef Str) { 1457 bool isChar6 = true; 1458 for (char C : Str) { 1459 if (isChar6) 1460 isChar6 = BitCodeAbbrevOp::isChar6(C); 1461 if ((unsigned char)C & 128) 1462 // don't bother scanning the rest. 1463 return SE_Fixed8; 1464 } 1465 if (isChar6) 1466 return SE_Char6; 1467 return SE_Fixed7; 1468 } 1469 1470 static_assert(sizeof(GlobalValue::SanitizerMetadata) <= sizeof(unsigned), 1471 "Sanitizer Metadata is too large for naive serialization."); 1472 static unsigned 1473 serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta) { 1474 return Meta.NoAddress | (Meta.NoHWAddress << 1) | 1475 (Meta.Memtag << 2) | (Meta.IsDynInit << 3); 1476 } 1477 1478 /// Emit top-level description of module, including target triple, inline asm, 1479 /// descriptors for global variables, and function prototype info. 1480 /// Returns the bit offset to backpatch with the location of the real VST. 1481 void ModuleBitcodeWriter::writeModuleInfo() { 1482 // Emit various pieces of data attached to a module. 1483 if (!M.getTargetTriple().empty()) 1484 writeStringRecord(Stream, bitc::MODULE_CODE_TRIPLE, 1485 M.getTargetTriple().str(), 0 /*TODO*/); 1486 const std::string &DL = M.getDataLayoutStr(); 1487 if (!DL.empty()) 1488 writeStringRecord(Stream, bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/); 1489 if (!M.getModuleInlineAsm().empty()) 1490 writeStringRecord(Stream, bitc::MODULE_CODE_ASM, M.getModuleInlineAsm(), 1491 0 /*TODO*/); 1492 1493 // Emit information about sections and GC, computing how many there are. Also 1494 // compute the maximum alignment value. 1495 std::map<std::string, unsigned> SectionMap; 1496 std::map<std::string, unsigned> GCMap; 1497 MaybeAlign MaxAlignment; 1498 unsigned MaxGlobalType = 0; 1499 const auto UpdateMaxAlignment = [&MaxAlignment](const MaybeAlign A) { 1500 if (A) 1501 MaxAlignment = !MaxAlignment ? *A : std::max(*MaxAlignment, *A); 1502 }; 1503 for (const GlobalVariable &GV : M.globals()) { 1504 UpdateMaxAlignment(GV.getAlign()); 1505 MaxGlobalType = std::max(MaxGlobalType, VE.getTypeID(GV.getValueType())); 1506 if (GV.hasSection()) { 1507 // Give section names unique ID's. 1508 unsigned &Entry = SectionMap[std::string(GV.getSection())]; 1509 if (!Entry) { 1510 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, GV.getSection(), 1511 0 /*TODO*/); 1512 Entry = SectionMap.size(); 1513 } 1514 } 1515 } 1516 for (const Function &F : M) { 1517 UpdateMaxAlignment(F.getAlign()); 1518 if (F.hasSection()) { 1519 // Give section names unique ID's. 1520 unsigned &Entry = SectionMap[std::string(F.getSection())]; 1521 if (!Entry) { 1522 writeStringRecord(Stream, bitc::MODULE_CODE_SECTIONNAME, F.getSection(), 1523 0 /*TODO*/); 1524 Entry = SectionMap.size(); 1525 } 1526 } 1527 if (F.hasGC()) { 1528 // Same for GC names. 1529 unsigned &Entry = GCMap[F.getGC()]; 1530 if (!Entry) { 1531 writeStringRecord(Stream, bitc::MODULE_CODE_GCNAME, F.getGC(), 1532 0 /*TODO*/); 1533 Entry = GCMap.size(); 1534 } 1535 } 1536 } 1537 1538 // Emit abbrev for globals, now that we know # sections and max alignment. 1539 unsigned SimpleGVarAbbrev = 0; 1540 if (!M.global_empty()) { 1541 // Add an abbrev for common globals with no visibility or thread localness. 1542 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1543 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_GLOBALVAR)); 1544 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1545 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 1546 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1547 Log2_32_Ceil(MaxGlobalType+1))); 1548 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddrSpace << 2 1549 //| explicitType << 1 1550 //| constant 1551 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Initializer. 1552 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); // Linkage. 1553 if (!MaxAlignment) // Alignment. 1554 Abbv->Add(BitCodeAbbrevOp(0)); 1555 else { 1556 unsigned MaxEncAlignment = getEncodedAlign(MaxAlignment); 1557 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1558 Log2_32_Ceil(MaxEncAlignment+1))); 1559 } 1560 if (SectionMap.empty()) // Section. 1561 Abbv->Add(BitCodeAbbrevOp(0)); 1562 else 1563 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1564 Log2_32_Ceil(SectionMap.size()+1))); 1565 // Don't bother emitting vis + thread local. 1566 SimpleGVarAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1567 } 1568 1569 SmallVector<unsigned, 64> Vals; 1570 // Emit the module's source file name. 1571 { 1572 StringEncoding Bits = getStringEncoding(M.getSourceFileName()); 1573 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8); 1574 if (Bits == SE_Char6) 1575 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6); 1576 else if (Bits == SE_Fixed7) 1577 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7); 1578 1579 // MODULE_CODE_SOURCE_FILENAME: [namechar x N] 1580 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1581 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME)); 1582 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1583 Abbv->Add(AbbrevOpToUse); 1584 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 1585 1586 for (const auto P : M.getSourceFileName()) 1587 Vals.push_back((unsigned char)P); 1588 1589 // Emit the finished record. 1590 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev); 1591 Vals.clear(); 1592 } 1593 1594 // Emit the global variable information. 1595 for (const GlobalVariable &GV : M.globals()) { 1596 unsigned AbbrevToUse = 0; 1597 1598 // GLOBALVAR: [strtab offset, strtab size, type, isconst, initid, 1599 // linkage, alignment, section, visibility, threadlocal, 1600 // unnamed_addr, externally_initialized, dllstorageclass, 1601 // comdat, attributes, DSO_Local, GlobalSanitizer, code_model] 1602 Vals.push_back(addToStrtab(GV.getName())); 1603 Vals.push_back(GV.getName().size()); 1604 Vals.push_back(VE.getTypeID(GV.getValueType())); 1605 Vals.push_back(GV.getType()->getAddressSpace() << 2 | 2 | GV.isConstant()); 1606 Vals.push_back(GV.isDeclaration() ? 0 : 1607 (VE.getValueID(GV.getInitializer()) + 1)); 1608 Vals.push_back(getEncodedLinkage(GV)); 1609 Vals.push_back(getEncodedAlign(GV.getAlign())); 1610 Vals.push_back(GV.hasSection() ? SectionMap[std::string(GV.getSection())] 1611 : 0); 1612 if (GV.isThreadLocal() || 1613 GV.getVisibility() != GlobalValue::DefaultVisibility || 1614 GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None || 1615 GV.isExternallyInitialized() || 1616 GV.getDLLStorageClass() != GlobalValue::DefaultStorageClass || 1617 GV.hasComdat() || GV.hasAttributes() || GV.isDSOLocal() || 1618 GV.hasPartition() || GV.hasSanitizerMetadata() || GV.getCodeModel()) { 1619 Vals.push_back(getEncodedVisibility(GV)); 1620 Vals.push_back(getEncodedThreadLocalMode(GV)); 1621 Vals.push_back(getEncodedUnnamedAddr(GV)); 1622 Vals.push_back(GV.isExternallyInitialized()); 1623 Vals.push_back(getEncodedDLLStorageClass(GV)); 1624 Vals.push_back(GV.hasComdat() ? VE.getComdatID(GV.getComdat()) : 0); 1625 1626 auto AL = GV.getAttributesAsList(AttributeList::FunctionIndex); 1627 Vals.push_back(VE.getAttributeListID(AL)); 1628 1629 Vals.push_back(GV.isDSOLocal()); 1630 Vals.push_back(addToStrtab(GV.getPartition())); 1631 Vals.push_back(GV.getPartition().size()); 1632 1633 Vals.push_back((GV.hasSanitizerMetadata() ? serializeSanitizerMetadata( 1634 GV.getSanitizerMetadata()) 1635 : 0)); 1636 Vals.push_back(GV.getCodeModelRaw()); 1637 } else { 1638 AbbrevToUse = SimpleGVarAbbrev; 1639 } 1640 1641 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals, AbbrevToUse); 1642 Vals.clear(); 1643 } 1644 1645 // Emit the function proto information. 1646 for (const Function &F : M) { 1647 // FUNCTION: [strtab offset, strtab size, type, callingconv, isproto, 1648 // linkage, paramattrs, alignment, section, visibility, gc, 1649 // unnamed_addr, prologuedata, dllstorageclass, comdat, 1650 // prefixdata, personalityfn, DSO_Local, addrspace] 1651 Vals.push_back(addToStrtab(F.getName())); 1652 Vals.push_back(F.getName().size()); 1653 Vals.push_back(VE.getTypeID(F.getFunctionType())); 1654 Vals.push_back(F.getCallingConv()); 1655 Vals.push_back(F.isDeclaration()); 1656 Vals.push_back(getEncodedLinkage(F)); 1657 Vals.push_back(VE.getAttributeListID(F.getAttributes())); 1658 Vals.push_back(getEncodedAlign(F.getAlign())); 1659 Vals.push_back(F.hasSection() ? SectionMap[std::string(F.getSection())] 1660 : 0); 1661 Vals.push_back(getEncodedVisibility(F)); 1662 Vals.push_back(F.hasGC() ? GCMap[F.getGC()] : 0); 1663 Vals.push_back(getEncodedUnnamedAddr(F)); 1664 Vals.push_back(F.hasPrologueData() ? (VE.getValueID(F.getPrologueData()) + 1) 1665 : 0); 1666 Vals.push_back(getEncodedDLLStorageClass(F)); 1667 Vals.push_back(F.hasComdat() ? VE.getComdatID(F.getComdat()) : 0); 1668 Vals.push_back(F.hasPrefixData() ? (VE.getValueID(F.getPrefixData()) + 1) 1669 : 0); 1670 Vals.push_back( 1671 F.hasPersonalityFn() ? (VE.getValueID(F.getPersonalityFn()) + 1) : 0); 1672 1673 Vals.push_back(F.isDSOLocal()); 1674 Vals.push_back(F.getAddressSpace()); 1675 Vals.push_back(addToStrtab(F.getPartition())); 1676 Vals.push_back(F.getPartition().size()); 1677 1678 unsigned AbbrevToUse = 0; 1679 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse); 1680 Vals.clear(); 1681 } 1682 1683 // Emit the alias information. 1684 for (const GlobalAlias &A : M.aliases()) { 1685 // ALIAS: [strtab offset, strtab size, alias type, aliasee val#, linkage, 1686 // visibility, dllstorageclass, threadlocal, unnamed_addr, 1687 // DSO_Local] 1688 Vals.push_back(addToStrtab(A.getName())); 1689 Vals.push_back(A.getName().size()); 1690 Vals.push_back(VE.getTypeID(A.getValueType())); 1691 Vals.push_back(A.getType()->getAddressSpace()); 1692 Vals.push_back(VE.getValueID(A.getAliasee())); 1693 Vals.push_back(getEncodedLinkage(A)); 1694 Vals.push_back(getEncodedVisibility(A)); 1695 Vals.push_back(getEncodedDLLStorageClass(A)); 1696 Vals.push_back(getEncodedThreadLocalMode(A)); 1697 Vals.push_back(getEncodedUnnamedAddr(A)); 1698 Vals.push_back(A.isDSOLocal()); 1699 Vals.push_back(addToStrtab(A.getPartition())); 1700 Vals.push_back(A.getPartition().size()); 1701 1702 unsigned AbbrevToUse = 0; 1703 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse); 1704 Vals.clear(); 1705 } 1706 1707 // Emit the ifunc information. 1708 for (const GlobalIFunc &I : M.ifuncs()) { 1709 // IFUNC: [strtab offset, strtab size, ifunc type, address space, resolver 1710 // val#, linkage, visibility, DSO_Local] 1711 Vals.push_back(addToStrtab(I.getName())); 1712 Vals.push_back(I.getName().size()); 1713 Vals.push_back(VE.getTypeID(I.getValueType())); 1714 Vals.push_back(I.getType()->getAddressSpace()); 1715 Vals.push_back(VE.getValueID(I.getResolver())); 1716 Vals.push_back(getEncodedLinkage(I)); 1717 Vals.push_back(getEncodedVisibility(I)); 1718 Vals.push_back(I.isDSOLocal()); 1719 Vals.push_back(addToStrtab(I.getPartition())); 1720 Vals.push_back(I.getPartition().size()); 1721 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals); 1722 Vals.clear(); 1723 } 1724 1725 writeValueSymbolTableForwardDecl(); 1726 } 1727 1728 static uint64_t getOptimizationFlags(const Value *V) { 1729 uint64_t Flags = 0; 1730 1731 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(V)) { 1732 if (OBO->hasNoSignedWrap()) 1733 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP; 1734 if (OBO->hasNoUnsignedWrap()) 1735 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP; 1736 } else if (const auto *PEO = dyn_cast<PossiblyExactOperator>(V)) { 1737 if (PEO->isExact()) 1738 Flags |= 1 << bitc::PEO_EXACT; 1739 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) { 1740 if (PDI->isDisjoint()) 1741 Flags |= 1 << bitc::PDI_DISJOINT; 1742 } else if (const auto *FPMO = dyn_cast<FPMathOperator>(V)) { 1743 if (FPMO->hasAllowReassoc()) 1744 Flags |= bitc::AllowReassoc; 1745 if (FPMO->hasNoNaNs()) 1746 Flags |= bitc::NoNaNs; 1747 if (FPMO->hasNoInfs()) 1748 Flags |= bitc::NoInfs; 1749 if (FPMO->hasNoSignedZeros()) 1750 Flags |= bitc::NoSignedZeros; 1751 if (FPMO->hasAllowReciprocal()) 1752 Flags |= bitc::AllowReciprocal; 1753 if (FPMO->hasAllowContract()) 1754 Flags |= bitc::AllowContract; 1755 if (FPMO->hasApproxFunc()) 1756 Flags |= bitc::ApproxFunc; 1757 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(V)) { 1758 if (NNI->hasNonNeg()) 1759 Flags |= 1 << bitc::PNNI_NON_NEG; 1760 } else if (const auto *TI = dyn_cast<TruncInst>(V)) { 1761 if (TI->hasNoSignedWrap()) 1762 Flags |= 1 << bitc::TIO_NO_SIGNED_WRAP; 1763 if (TI->hasNoUnsignedWrap()) 1764 Flags |= 1 << bitc::TIO_NO_UNSIGNED_WRAP; 1765 } else if (const auto *GEP = dyn_cast<GEPOperator>(V)) { 1766 if (GEP->isInBounds()) 1767 Flags |= 1 << bitc::GEP_INBOUNDS; 1768 if (GEP->hasNoUnsignedSignedWrap()) 1769 Flags |= 1 << bitc::GEP_NUSW; 1770 if (GEP->hasNoUnsignedWrap()) 1771 Flags |= 1 << bitc::GEP_NUW; 1772 } else if (const auto *ICmp = dyn_cast<ICmpInst>(V)) { 1773 if (ICmp->hasSameSign()) 1774 Flags |= 1 << bitc::ICMP_SAME_SIGN; 1775 } 1776 1777 return Flags; 1778 } 1779 1780 void ModuleBitcodeWriter::writeValueAsMetadata( 1781 const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) { 1782 // Mimic an MDNode with a value as one operand. 1783 Value *V = MD->getValue(); 1784 Record.push_back(VE.getTypeID(V->getType())); 1785 Record.push_back(VE.getValueID(V)); 1786 Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0); 1787 Record.clear(); 1788 } 1789 1790 void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N, 1791 SmallVectorImpl<uint64_t> &Record, 1792 unsigned Abbrev) { 1793 for (const MDOperand &MDO : N->operands()) { 1794 Metadata *MD = MDO; 1795 assert(!(MD && isa<LocalAsMetadata>(MD)) && 1796 "Unexpected function-local metadata"); 1797 Record.push_back(VE.getMetadataOrNullID(MD)); 1798 } 1799 Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE 1800 : bitc::METADATA_NODE, 1801 Record, Abbrev); 1802 Record.clear(); 1803 } 1804 1805 unsigned ModuleBitcodeWriter::createDILocationAbbrev() { 1806 // Assume the column is usually under 128, and always output the inlined-at 1807 // location (it's never more expensive than building an array size 1). 1808 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1809 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_LOCATION)); 1810 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isDistinct 1811 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // line 1812 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // column 1813 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // scope 1814 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // inlinedAt 1815 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImplicitCode 1816 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // atomGroup 1817 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // atomRank 1818 return Stream.EmitAbbrev(std::move(Abbv)); 1819 } 1820 1821 void ModuleBitcodeWriter::writeDILocation(const DILocation *N, 1822 SmallVectorImpl<uint64_t> &Record, 1823 unsigned &Abbrev) { 1824 if (!Abbrev) 1825 Abbrev = createDILocationAbbrev(); 1826 1827 Record.push_back(N->isDistinct()); 1828 Record.push_back(N->getLine()); 1829 Record.push_back(N->getColumn()); 1830 Record.push_back(VE.getMetadataID(N->getScope())); 1831 Record.push_back(VE.getMetadataOrNullID(N->getInlinedAt())); 1832 Record.push_back(N->isImplicitCode()); 1833 Record.push_back(N->getAtomGroup()); 1834 Record.push_back(N->getAtomRank()); 1835 Stream.EmitRecord(bitc::METADATA_LOCATION, Record, Abbrev); 1836 Record.clear(); 1837 } 1838 1839 unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() { 1840 // Assume the column is usually under 128, and always output the inlined-at 1841 // location (it's never more expensive than building an array size 1). 1842 auto Abbv = std::make_shared<BitCodeAbbrev>(); 1843 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_GENERIC_DEBUG)); 1844 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 1845 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1846 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 1847 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1848 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 1849 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 1850 return Stream.EmitAbbrev(std::move(Abbv)); 1851 } 1852 1853 void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N, 1854 SmallVectorImpl<uint64_t> &Record, 1855 unsigned &Abbrev) { 1856 if (!Abbrev) 1857 Abbrev = createGenericDINodeAbbrev(); 1858 1859 Record.push_back(N->isDistinct()); 1860 Record.push_back(N->getTag()); 1861 Record.push_back(0); // Per-tag version field; unused for now. 1862 1863 for (auto &I : N->operands()) 1864 Record.push_back(VE.getMetadataOrNullID(I)); 1865 1866 Stream.EmitRecord(bitc::METADATA_GENERIC_DEBUG, Record, Abbrev); 1867 Record.clear(); 1868 } 1869 1870 void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N, 1871 SmallVectorImpl<uint64_t> &Record, 1872 unsigned Abbrev) { 1873 const uint64_t Version = 2 << 1; 1874 Record.push_back((uint64_t)N->isDistinct() | Version); 1875 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode())); 1876 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound())); 1877 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound())); 1878 Record.push_back(VE.getMetadataOrNullID(N->getRawStride())); 1879 1880 Stream.EmitRecord(bitc::METADATA_SUBRANGE, Record, Abbrev); 1881 Record.clear(); 1882 } 1883 1884 void ModuleBitcodeWriter::writeDIGenericSubrange( 1885 const DIGenericSubrange *N, SmallVectorImpl<uint64_t> &Record, 1886 unsigned Abbrev) { 1887 Record.push_back((uint64_t)N->isDistinct()); 1888 Record.push_back(VE.getMetadataOrNullID(N->getRawCountNode())); 1889 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound())); 1890 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound())); 1891 Record.push_back(VE.getMetadataOrNullID(N->getRawStride())); 1892 1893 Stream.EmitRecord(bitc::METADATA_GENERIC_SUBRANGE, Record, Abbrev); 1894 Record.clear(); 1895 } 1896 1897 void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N, 1898 SmallVectorImpl<uint64_t> &Record, 1899 unsigned Abbrev) { 1900 const uint64_t IsBigInt = 1 << 2; 1901 Record.push_back(IsBigInt | (N->isUnsigned() << 1) | N->isDistinct()); 1902 Record.push_back(N->getValue().getBitWidth()); 1903 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1904 emitWideAPInt(Record, N->getValue()); 1905 1906 Stream.EmitRecord(bitc::METADATA_ENUMERATOR, Record, Abbrev); 1907 Record.clear(); 1908 } 1909 1910 void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N, 1911 SmallVectorImpl<uint64_t> &Record, 1912 unsigned Abbrev) { 1913 const unsigned SizeIsMetadata = 0x2; 1914 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct()); 1915 Record.push_back(N->getTag()); 1916 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1917 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 1918 Record.push_back(N->getAlignInBits()); 1919 Record.push_back(N->getEncoding()); 1920 Record.push_back(N->getFlags()); 1921 Record.push_back(N->getNumExtraInhabitants()); 1922 1923 Stream.EmitRecord(bitc::METADATA_BASIC_TYPE, Record, Abbrev); 1924 Record.clear(); 1925 } 1926 1927 void ModuleBitcodeWriter::writeDIFixedPointType( 1928 const DIFixedPointType *N, SmallVectorImpl<uint64_t> &Record, 1929 unsigned Abbrev) { 1930 const unsigned SizeIsMetadata = 0x2; 1931 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct()); 1932 Record.push_back(N->getTag()); 1933 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1934 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 1935 Record.push_back(N->getAlignInBits()); 1936 Record.push_back(N->getEncoding()); 1937 Record.push_back(N->getFlags()); 1938 Record.push_back(N->getKind()); 1939 Record.push_back(N->getFactorRaw()); 1940 1941 auto WriteWideInt = [&](const APInt &Value) { 1942 // Write an encoded word that holds the number of active words and 1943 // the number of bits. 1944 uint64_t NumWords = Value.getActiveWords(); 1945 uint64_t Encoded = (NumWords << 32) | Value.getBitWidth(); 1946 Record.push_back(Encoded); 1947 emitWideAPInt(Record, Value); 1948 }; 1949 1950 WriteWideInt(N->getNumeratorRaw()); 1951 WriteWideInt(N->getDenominatorRaw()); 1952 1953 Stream.EmitRecord(bitc::METADATA_FIXED_POINT_TYPE, Record, Abbrev); 1954 Record.clear(); 1955 } 1956 1957 void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N, 1958 SmallVectorImpl<uint64_t> &Record, 1959 unsigned Abbrev) { 1960 const unsigned SizeIsMetadata = 0x2; 1961 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct()); 1962 Record.push_back(N->getTag()); 1963 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1964 Record.push_back(VE.getMetadataOrNullID(N->getStringLength())); 1965 Record.push_back(VE.getMetadataOrNullID(N->getStringLengthExp())); 1966 Record.push_back(VE.getMetadataOrNullID(N->getStringLocationExp())); 1967 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 1968 Record.push_back(N->getAlignInBits()); 1969 Record.push_back(N->getEncoding()); 1970 1971 Stream.EmitRecord(bitc::METADATA_STRING_TYPE, Record, Abbrev); 1972 Record.clear(); 1973 } 1974 1975 void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N, 1976 SmallVectorImpl<uint64_t> &Record, 1977 unsigned Abbrev) { 1978 const unsigned SizeIsMetadata = 0x2; 1979 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct()); 1980 Record.push_back(N->getTag()); 1981 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 1982 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 1983 Record.push_back(N->getLine()); 1984 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 1985 Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); 1986 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 1987 Record.push_back(N->getAlignInBits()); 1988 Record.push_back(VE.getMetadataOrNullID(N->getRawOffsetInBits())); 1989 Record.push_back(N->getFlags()); 1990 Record.push_back(VE.getMetadataOrNullID(N->getExtraData())); 1991 1992 // DWARF address space is encoded as N->getDWARFAddressSpace() + 1. 0 means 1993 // that there is no DWARF address space associated with DIDerivedType. 1994 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace()) 1995 Record.push_back(*DWARFAddressSpace + 1); 1996 else 1997 Record.push_back(0); 1998 1999 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get())); 2000 2001 if (auto PtrAuthData = N->getPtrAuthData()) 2002 Record.push_back(PtrAuthData->RawData); 2003 else 2004 Record.push_back(0); 2005 2006 Stream.EmitRecord(bitc::METADATA_DERIVED_TYPE, Record, Abbrev); 2007 Record.clear(); 2008 } 2009 2010 void ModuleBitcodeWriter::writeDISubrangeType(const DISubrangeType *N, 2011 SmallVectorImpl<uint64_t> &Record, 2012 unsigned Abbrev) { 2013 const unsigned SizeIsMetadata = 0x2; 2014 Record.push_back(SizeIsMetadata | (unsigned)N->isDistinct()); 2015 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2016 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2017 Record.push_back(N->getLine()); 2018 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2019 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 2020 Record.push_back(N->getAlignInBits()); 2021 Record.push_back(N->getFlags()); 2022 Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); 2023 Record.push_back(VE.getMetadataOrNullID(N->getRawLowerBound())); 2024 Record.push_back(VE.getMetadataOrNullID(N->getRawUpperBound())); 2025 Record.push_back(VE.getMetadataOrNullID(N->getRawStride())); 2026 Record.push_back(VE.getMetadataOrNullID(N->getRawBias())); 2027 2028 Stream.EmitRecord(bitc::METADATA_SUBRANGE_TYPE, Record, Abbrev); 2029 Record.clear(); 2030 } 2031 2032 void ModuleBitcodeWriter::writeDICompositeType( 2033 const DICompositeType *N, SmallVectorImpl<uint64_t> &Record, 2034 unsigned Abbrev) { 2035 const unsigned IsNotUsedInOldTypeRef = 0x2; 2036 const unsigned SizeIsMetadata = 0x4; 2037 Record.push_back(SizeIsMetadata | IsNotUsedInOldTypeRef | 2038 (unsigned)N->isDistinct()); 2039 Record.push_back(N->getTag()); 2040 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2041 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2042 Record.push_back(N->getLine()); 2043 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2044 Record.push_back(VE.getMetadataOrNullID(N->getBaseType())); 2045 Record.push_back(VE.getMetadataOrNullID(N->getRawSizeInBits())); 2046 Record.push_back(N->getAlignInBits()); 2047 Record.push_back(VE.getMetadataOrNullID(N->getRawOffsetInBits())); 2048 Record.push_back(N->getFlags()); 2049 Record.push_back(VE.getMetadataOrNullID(N->getElements().get())); 2050 Record.push_back(N->getRuntimeLang()); 2051 Record.push_back(VE.getMetadataOrNullID(N->getVTableHolder())); 2052 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get())); 2053 Record.push_back(VE.getMetadataOrNullID(N->getRawIdentifier())); 2054 Record.push_back(VE.getMetadataOrNullID(N->getDiscriminator())); 2055 Record.push_back(VE.getMetadataOrNullID(N->getRawDataLocation())); 2056 Record.push_back(VE.getMetadataOrNullID(N->getRawAssociated())); 2057 Record.push_back(VE.getMetadataOrNullID(N->getRawAllocated())); 2058 Record.push_back(VE.getMetadataOrNullID(N->getRawRank())); 2059 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get())); 2060 Record.push_back(N->getNumExtraInhabitants()); 2061 Record.push_back(VE.getMetadataOrNullID(N->getRawSpecification())); 2062 Record.push_back( 2063 N->getEnumKind().value_or(dwarf::DW_APPLE_ENUM_KIND_invalid)); 2064 Record.push_back(VE.getMetadataOrNullID(N->getRawBitStride())); 2065 2066 Stream.EmitRecord(bitc::METADATA_COMPOSITE_TYPE, Record, Abbrev); 2067 Record.clear(); 2068 } 2069 2070 void ModuleBitcodeWriter::writeDISubroutineType( 2071 const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record, 2072 unsigned Abbrev) { 2073 const unsigned HasNoOldTypeRefs = 0x2; 2074 Record.push_back(HasNoOldTypeRefs | (unsigned)N->isDistinct()); 2075 Record.push_back(N->getFlags()); 2076 Record.push_back(VE.getMetadataOrNullID(N->getTypeArray().get())); 2077 Record.push_back(N->getCC()); 2078 2079 Stream.EmitRecord(bitc::METADATA_SUBROUTINE_TYPE, Record, Abbrev); 2080 Record.clear(); 2081 } 2082 2083 void ModuleBitcodeWriter::writeDIFile(const DIFile *N, 2084 SmallVectorImpl<uint64_t> &Record, 2085 unsigned Abbrev) { 2086 Record.push_back(N->isDistinct()); 2087 Record.push_back(VE.getMetadataOrNullID(N->getRawFilename())); 2088 Record.push_back(VE.getMetadataOrNullID(N->getRawDirectory())); 2089 if (N->getRawChecksum()) { 2090 Record.push_back(N->getRawChecksum()->Kind); 2091 Record.push_back(VE.getMetadataOrNullID(N->getRawChecksum()->Value)); 2092 } else { 2093 // Maintain backwards compatibility with the old internal representation of 2094 // CSK_None in ChecksumKind by writing nulls here when Checksum is None. 2095 Record.push_back(0); 2096 Record.push_back(VE.getMetadataOrNullID(nullptr)); 2097 } 2098 auto Source = N->getRawSource(); 2099 if (Source) 2100 Record.push_back(VE.getMetadataOrNullID(Source)); 2101 2102 Stream.EmitRecord(bitc::METADATA_FILE, Record, Abbrev); 2103 Record.clear(); 2104 } 2105 2106 void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N, 2107 SmallVectorImpl<uint64_t> &Record, 2108 unsigned Abbrev) { 2109 assert(N->isDistinct() && "Expected distinct compile units"); 2110 Record.push_back(/* IsDistinct */ true); 2111 Record.push_back(N->getSourceLanguage()); 2112 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2113 Record.push_back(VE.getMetadataOrNullID(N->getRawProducer())); 2114 Record.push_back(N->isOptimized()); 2115 Record.push_back(VE.getMetadataOrNullID(N->getRawFlags())); 2116 Record.push_back(N->getRuntimeVersion()); 2117 Record.push_back(VE.getMetadataOrNullID(N->getRawSplitDebugFilename())); 2118 Record.push_back(N->getEmissionKind()); 2119 Record.push_back(VE.getMetadataOrNullID(N->getEnumTypes().get())); 2120 Record.push_back(VE.getMetadataOrNullID(N->getRetainedTypes().get())); 2121 Record.push_back(/* subprograms */ 0); 2122 Record.push_back(VE.getMetadataOrNullID(N->getGlobalVariables().get())); 2123 Record.push_back(VE.getMetadataOrNullID(N->getImportedEntities().get())); 2124 Record.push_back(N->getDWOId()); 2125 Record.push_back(VE.getMetadataOrNullID(N->getMacros().get())); 2126 Record.push_back(N->getSplitDebugInlining()); 2127 Record.push_back(N->getDebugInfoForProfiling()); 2128 Record.push_back((unsigned)N->getNameTableKind()); 2129 Record.push_back(N->getRangesBaseAddress()); 2130 Record.push_back(VE.getMetadataOrNullID(N->getRawSysRoot())); 2131 Record.push_back(VE.getMetadataOrNullID(N->getRawSDK())); 2132 2133 Stream.EmitRecord(bitc::METADATA_COMPILE_UNIT, Record, Abbrev); 2134 Record.clear(); 2135 } 2136 2137 void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N, 2138 SmallVectorImpl<uint64_t> &Record, 2139 unsigned Abbrev) { 2140 const uint64_t HasUnitFlag = 1 << 1; 2141 const uint64_t HasSPFlagsFlag = 1 << 2; 2142 Record.push_back(uint64_t(N->isDistinct()) | HasUnitFlag | HasSPFlagsFlag); 2143 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2144 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2145 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); 2146 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2147 Record.push_back(N->getLine()); 2148 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2149 Record.push_back(N->getScopeLine()); 2150 Record.push_back(VE.getMetadataOrNullID(N->getContainingType())); 2151 Record.push_back(N->getSPFlags()); 2152 Record.push_back(N->getVirtualIndex()); 2153 Record.push_back(N->getFlags()); 2154 Record.push_back(VE.getMetadataOrNullID(N->getRawUnit())); 2155 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams().get())); 2156 Record.push_back(VE.getMetadataOrNullID(N->getDeclaration())); 2157 Record.push_back(VE.getMetadataOrNullID(N->getRetainedNodes().get())); 2158 Record.push_back(N->getThisAdjustment()); 2159 Record.push_back(VE.getMetadataOrNullID(N->getThrownTypes().get())); 2160 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get())); 2161 Record.push_back(VE.getMetadataOrNullID(N->getRawTargetFuncName())); 2162 Record.push_back(N->getKeyInstructionsEnabled()); 2163 2164 Stream.EmitRecord(bitc::METADATA_SUBPROGRAM, Record, Abbrev); 2165 Record.clear(); 2166 } 2167 2168 void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N, 2169 SmallVectorImpl<uint64_t> &Record, 2170 unsigned Abbrev) { 2171 Record.push_back(N->isDistinct()); 2172 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2173 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2174 Record.push_back(N->getLine()); 2175 Record.push_back(N->getColumn()); 2176 2177 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK, Record, Abbrev); 2178 Record.clear(); 2179 } 2180 2181 void ModuleBitcodeWriter::writeDILexicalBlockFile( 2182 const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record, 2183 unsigned Abbrev) { 2184 Record.push_back(N->isDistinct()); 2185 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2186 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2187 Record.push_back(N->getDiscriminator()); 2188 2189 Stream.EmitRecord(bitc::METADATA_LEXICAL_BLOCK_FILE, Record, Abbrev); 2190 Record.clear(); 2191 } 2192 2193 void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N, 2194 SmallVectorImpl<uint64_t> &Record, 2195 unsigned Abbrev) { 2196 Record.push_back(N->isDistinct()); 2197 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2198 Record.push_back(VE.getMetadataOrNullID(N->getDecl())); 2199 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2200 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2201 Record.push_back(N->getLineNo()); 2202 2203 Stream.EmitRecord(bitc::METADATA_COMMON_BLOCK, Record, Abbrev); 2204 Record.clear(); 2205 } 2206 2207 void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N, 2208 SmallVectorImpl<uint64_t> &Record, 2209 unsigned Abbrev) { 2210 Record.push_back(N->isDistinct() | N->getExportSymbols() << 1); 2211 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2212 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2213 2214 Stream.EmitRecord(bitc::METADATA_NAMESPACE, Record, Abbrev); 2215 Record.clear(); 2216 } 2217 2218 void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N, 2219 SmallVectorImpl<uint64_t> &Record, 2220 unsigned Abbrev) { 2221 Record.push_back(N->isDistinct()); 2222 Record.push_back(N->getMacinfoType()); 2223 Record.push_back(N->getLine()); 2224 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2225 Record.push_back(VE.getMetadataOrNullID(N->getRawValue())); 2226 2227 Stream.EmitRecord(bitc::METADATA_MACRO, Record, Abbrev); 2228 Record.clear(); 2229 } 2230 2231 void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N, 2232 SmallVectorImpl<uint64_t> &Record, 2233 unsigned Abbrev) { 2234 Record.push_back(N->isDistinct()); 2235 Record.push_back(N->getMacinfoType()); 2236 Record.push_back(N->getLine()); 2237 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2238 Record.push_back(VE.getMetadataOrNullID(N->getElements().get())); 2239 2240 Stream.EmitRecord(bitc::METADATA_MACRO_FILE, Record, Abbrev); 2241 Record.clear(); 2242 } 2243 2244 void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N, 2245 SmallVectorImpl<uint64_t> &Record) { 2246 Record.reserve(N->getArgs().size()); 2247 for (ValueAsMetadata *MD : N->getArgs()) 2248 Record.push_back(VE.getMetadataID(MD)); 2249 2250 Stream.EmitRecord(bitc::METADATA_ARG_LIST, Record); 2251 Record.clear(); 2252 } 2253 2254 void ModuleBitcodeWriter::writeDIModule(const DIModule *N, 2255 SmallVectorImpl<uint64_t> &Record, 2256 unsigned Abbrev) { 2257 Record.push_back(N->isDistinct()); 2258 for (auto &I : N->operands()) 2259 Record.push_back(VE.getMetadataOrNullID(I)); 2260 Record.push_back(N->getLineNo()); 2261 Record.push_back(N->getIsDecl()); 2262 2263 Stream.EmitRecord(bitc::METADATA_MODULE, Record, Abbrev); 2264 Record.clear(); 2265 } 2266 2267 void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N, 2268 SmallVectorImpl<uint64_t> &Record, 2269 unsigned Abbrev) { 2270 // There are no arguments for this metadata type. 2271 Record.push_back(N->isDistinct()); 2272 Stream.EmitRecord(bitc::METADATA_ASSIGN_ID, Record, Abbrev); 2273 Record.clear(); 2274 } 2275 2276 void ModuleBitcodeWriter::writeDITemplateTypeParameter( 2277 const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record, 2278 unsigned Abbrev) { 2279 Record.push_back(N->isDistinct()); 2280 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2281 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2282 Record.push_back(N->isDefault()); 2283 2284 Stream.EmitRecord(bitc::METADATA_TEMPLATE_TYPE, Record, Abbrev); 2285 Record.clear(); 2286 } 2287 2288 void ModuleBitcodeWriter::writeDITemplateValueParameter( 2289 const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record, 2290 unsigned Abbrev) { 2291 Record.push_back(N->isDistinct()); 2292 Record.push_back(N->getTag()); 2293 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2294 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2295 Record.push_back(N->isDefault()); 2296 Record.push_back(VE.getMetadataOrNullID(N->getValue())); 2297 2298 Stream.EmitRecord(bitc::METADATA_TEMPLATE_VALUE, Record, Abbrev); 2299 Record.clear(); 2300 } 2301 2302 void ModuleBitcodeWriter::writeDIGlobalVariable( 2303 const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record, 2304 unsigned Abbrev) { 2305 const uint64_t Version = 2 << 1; 2306 Record.push_back((uint64_t)N->isDistinct() | Version); 2307 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2308 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2309 Record.push_back(VE.getMetadataOrNullID(N->getRawLinkageName())); 2310 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2311 Record.push_back(N->getLine()); 2312 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2313 Record.push_back(N->isLocalToUnit()); 2314 Record.push_back(N->isDefinition()); 2315 Record.push_back(VE.getMetadataOrNullID(N->getStaticDataMemberDeclaration())); 2316 Record.push_back(VE.getMetadataOrNullID(N->getTemplateParams())); 2317 Record.push_back(N->getAlignInBits()); 2318 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get())); 2319 2320 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR, Record, Abbrev); 2321 Record.clear(); 2322 } 2323 2324 void ModuleBitcodeWriter::writeDILocalVariable( 2325 const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record, 2326 unsigned Abbrev) { 2327 // In order to support all possible bitcode formats in BitcodeReader we need 2328 // to distinguish the following cases: 2329 // 1) Record has no artificial tag (Record[1]), 2330 // has no obsolete inlinedAt field (Record[9]). 2331 // In this case Record size will be 8, HasAlignment flag is false. 2332 // 2) Record has artificial tag (Record[1]), 2333 // has no obsolete inlignedAt field (Record[9]). 2334 // In this case Record size will be 9, HasAlignment flag is false. 2335 // 3) Record has both artificial tag (Record[1]) and 2336 // obsolete inlignedAt field (Record[9]). 2337 // In this case Record size will be 10, HasAlignment flag is false. 2338 // 4) Record has neither artificial tag, nor inlignedAt field, but 2339 // HasAlignment flag is true and Record[8] contains alignment value. 2340 const uint64_t HasAlignmentFlag = 1 << 1; 2341 Record.push_back((uint64_t)N->isDistinct() | HasAlignmentFlag); 2342 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2343 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2344 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2345 Record.push_back(N->getLine()); 2346 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2347 Record.push_back(N->getArg()); 2348 Record.push_back(N->getFlags()); 2349 Record.push_back(N->getAlignInBits()); 2350 Record.push_back(VE.getMetadataOrNullID(N->getAnnotations().get())); 2351 2352 Stream.EmitRecord(bitc::METADATA_LOCAL_VAR, Record, Abbrev); 2353 Record.clear(); 2354 } 2355 2356 void ModuleBitcodeWriter::writeDILabel( 2357 const DILabel *N, SmallVectorImpl<uint64_t> &Record, 2358 unsigned Abbrev) { 2359 uint64_t IsArtificialFlag = uint64_t(N->isArtificial()) << 1; 2360 Record.push_back((uint64_t)N->isDistinct() | IsArtificialFlag); 2361 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2362 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2363 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2364 Record.push_back(N->getLine()); 2365 Record.push_back(N->getColumn()); 2366 Record.push_back(N->getCoroSuspendIdx().has_value() 2367 ? (uint64_t)N->getCoroSuspendIdx().value() 2368 : std::numeric_limits<uint64_t>::max()); 2369 2370 Stream.EmitRecord(bitc::METADATA_LABEL, Record, Abbrev); 2371 Record.clear(); 2372 } 2373 2374 void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N, 2375 SmallVectorImpl<uint64_t> &Record, 2376 unsigned Abbrev) { 2377 Record.reserve(N->getElements().size() + 1); 2378 const uint64_t Version = 3 << 1; 2379 Record.push_back((uint64_t)N->isDistinct() | Version); 2380 Record.append(N->elements_begin(), N->elements_end()); 2381 2382 Stream.EmitRecord(bitc::METADATA_EXPRESSION, Record, Abbrev); 2383 Record.clear(); 2384 } 2385 2386 void ModuleBitcodeWriter::writeDIGlobalVariableExpression( 2387 const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record, 2388 unsigned Abbrev) { 2389 Record.push_back(N->isDistinct()); 2390 Record.push_back(VE.getMetadataOrNullID(N->getVariable())); 2391 Record.push_back(VE.getMetadataOrNullID(N->getExpression())); 2392 2393 Stream.EmitRecord(bitc::METADATA_GLOBAL_VAR_EXPR, Record, Abbrev); 2394 Record.clear(); 2395 } 2396 2397 void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N, 2398 SmallVectorImpl<uint64_t> &Record, 2399 unsigned Abbrev) { 2400 Record.push_back(N->isDistinct()); 2401 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2402 Record.push_back(VE.getMetadataOrNullID(N->getFile())); 2403 Record.push_back(N->getLine()); 2404 Record.push_back(VE.getMetadataOrNullID(N->getRawSetterName())); 2405 Record.push_back(VE.getMetadataOrNullID(N->getRawGetterName())); 2406 Record.push_back(N->getAttributes()); 2407 Record.push_back(VE.getMetadataOrNullID(N->getType())); 2408 2409 Stream.EmitRecord(bitc::METADATA_OBJC_PROPERTY, Record, Abbrev); 2410 Record.clear(); 2411 } 2412 2413 void ModuleBitcodeWriter::writeDIImportedEntity( 2414 const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record, 2415 unsigned Abbrev) { 2416 Record.push_back(N->isDistinct()); 2417 Record.push_back(N->getTag()); 2418 Record.push_back(VE.getMetadataOrNullID(N->getScope())); 2419 Record.push_back(VE.getMetadataOrNullID(N->getEntity())); 2420 Record.push_back(N->getLine()); 2421 Record.push_back(VE.getMetadataOrNullID(N->getRawName())); 2422 Record.push_back(VE.getMetadataOrNullID(N->getRawFile())); 2423 Record.push_back(VE.getMetadataOrNullID(N->getElements().get())); 2424 2425 Stream.EmitRecord(bitc::METADATA_IMPORTED_ENTITY, Record, Abbrev); 2426 Record.clear(); 2427 } 2428 2429 unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() { 2430 auto Abbv = std::make_shared<BitCodeAbbrev>(); 2431 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_NAME)); 2432 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2433 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 2434 return Stream.EmitAbbrev(std::move(Abbv)); 2435 } 2436 2437 void ModuleBitcodeWriter::writeNamedMetadata( 2438 SmallVectorImpl<uint64_t> &Record) { 2439 if (M.named_metadata_empty()) 2440 return; 2441 2442 unsigned Abbrev = createNamedMetadataAbbrev(); 2443 for (const NamedMDNode &NMD : M.named_metadata()) { 2444 // Write name. 2445 StringRef Str = NMD.getName(); 2446 Record.append(Str.bytes_begin(), Str.bytes_end()); 2447 Stream.EmitRecord(bitc::METADATA_NAME, Record, Abbrev); 2448 Record.clear(); 2449 2450 // Write named metadata operands. 2451 for (const MDNode *N : NMD.operands()) 2452 Record.push_back(VE.getMetadataID(N)); 2453 Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); 2454 Record.clear(); 2455 } 2456 } 2457 2458 unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() { 2459 auto Abbv = std::make_shared<BitCodeAbbrev>(); 2460 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRINGS)); 2461 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of strings 2462 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // offset to chars 2463 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 2464 return Stream.EmitAbbrev(std::move(Abbv)); 2465 } 2466 2467 /// Write out a record for MDString. 2468 /// 2469 /// All the metadata strings in a metadata block are emitted in a single 2470 /// record. The sizes and strings themselves are shoved into a blob. 2471 void ModuleBitcodeWriter::writeMetadataStrings( 2472 ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) { 2473 if (Strings.empty()) 2474 return; 2475 2476 // Start the record with the number of strings. 2477 Record.push_back(bitc::METADATA_STRINGS); 2478 Record.push_back(Strings.size()); 2479 2480 // Emit the sizes of the strings in the blob. 2481 SmallString<256> Blob; 2482 { 2483 BitstreamWriter W(Blob); 2484 for (const Metadata *MD : Strings) 2485 W.EmitVBR(cast<MDString>(MD)->getLength(), 6); 2486 W.FlushToWord(); 2487 } 2488 2489 // Add the offset to the strings to the record. 2490 Record.push_back(Blob.size()); 2491 2492 // Add the strings to the blob. 2493 for (const Metadata *MD : Strings) 2494 Blob.append(cast<MDString>(MD)->getString()); 2495 2496 // Emit the final record. 2497 Stream.EmitRecordWithBlob(createMetadataStringsAbbrev(), Record, Blob); 2498 Record.clear(); 2499 } 2500 2501 // Generates an enum to use as an index in the Abbrev array of Metadata record. 2502 enum MetadataAbbrev : unsigned { 2503 #define HANDLE_MDNODE_LEAF(CLASS) CLASS##AbbrevID, 2504 #include "llvm/IR/Metadata.def" 2505 LastPlusOne 2506 }; 2507 2508 void ModuleBitcodeWriter::writeMetadataRecords( 2509 ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record, 2510 std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) { 2511 if (MDs.empty()) 2512 return; 2513 2514 // Initialize MDNode abbreviations. 2515 #define HANDLE_MDNODE_LEAF(CLASS) unsigned CLASS##Abbrev = 0; 2516 #include "llvm/IR/Metadata.def" 2517 2518 for (const Metadata *MD : MDs) { 2519 if (IndexPos) 2520 IndexPos->push_back(Stream.GetCurrentBitNo()); 2521 if (const MDNode *N = dyn_cast<MDNode>(MD)) { 2522 assert(N->isResolved() && "Expected forward references to be resolved"); 2523 2524 switch (N->getMetadataID()) { 2525 default: 2526 llvm_unreachable("Invalid MDNode subclass"); 2527 #define HANDLE_MDNODE_LEAF(CLASS) \ 2528 case Metadata::CLASS##Kind: \ 2529 if (MDAbbrevs) \ 2530 write##CLASS(cast<CLASS>(N), Record, \ 2531 (*MDAbbrevs)[MetadataAbbrev::CLASS##AbbrevID]); \ 2532 else \ 2533 write##CLASS(cast<CLASS>(N), Record, CLASS##Abbrev); \ 2534 continue; 2535 #include "llvm/IR/Metadata.def" 2536 } 2537 } 2538 if (auto *AL = dyn_cast<DIArgList>(MD)) { 2539 writeDIArgList(AL, Record); 2540 continue; 2541 } 2542 writeValueAsMetadata(cast<ValueAsMetadata>(MD), Record); 2543 } 2544 } 2545 2546 void ModuleBitcodeWriter::writeModuleMetadata() { 2547 if (!VE.hasMDs() && M.named_metadata_empty()) 2548 return; 2549 2550 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 4); 2551 SmallVector<uint64_t, 64> Record; 2552 2553 // Emit all abbrevs upfront, so that the reader can jump in the middle of the 2554 // block and load any metadata. 2555 std::vector<unsigned> MDAbbrevs; 2556 2557 MDAbbrevs.resize(MetadataAbbrev::LastPlusOne); 2558 MDAbbrevs[MetadataAbbrev::DILocationAbbrevID] = createDILocationAbbrev(); 2559 MDAbbrevs[MetadataAbbrev::GenericDINodeAbbrevID] = 2560 createGenericDINodeAbbrev(); 2561 2562 auto Abbv = std::make_shared<BitCodeAbbrev>(); 2563 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX_OFFSET)); 2564 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2565 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 2566 unsigned OffsetAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 2567 2568 Abbv = std::make_shared<BitCodeAbbrev>(); 2569 Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_INDEX)); 2570 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2571 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 2572 unsigned IndexAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 2573 2574 // Emit MDStrings together upfront. 2575 writeMetadataStrings(VE.getMDStrings(), Record); 2576 2577 // We only emit an index for the metadata record if we have more than a given 2578 // (naive) threshold of metadatas, otherwise it is not worth it. 2579 if (VE.getNonMDStrings().size() > IndexThreshold) { 2580 // Write a placeholder value in for the offset of the metadata index, 2581 // which is written after the records, so that it can include 2582 // the offset of each entry. The placeholder offset will be 2583 // updated after all records are emitted. 2584 uint64_t Vals[] = {0, 0}; 2585 Stream.EmitRecord(bitc::METADATA_INDEX_OFFSET, Vals, OffsetAbbrev); 2586 } 2587 2588 // Compute and save the bit offset to the current position, which will be 2589 // patched when we emit the index later. We can simply subtract the 64-bit 2590 // fixed size from the current bit number to get the location to backpatch. 2591 uint64_t IndexOffsetRecordBitPos = Stream.GetCurrentBitNo(); 2592 2593 // This index will contain the bitpos for each individual record. 2594 std::vector<uint64_t> IndexPos; 2595 IndexPos.reserve(VE.getNonMDStrings().size()); 2596 2597 // Write all the records 2598 writeMetadataRecords(VE.getNonMDStrings(), Record, &MDAbbrevs, &IndexPos); 2599 2600 if (VE.getNonMDStrings().size() > IndexThreshold) { 2601 // Now that we have emitted all the records we will emit the index. But 2602 // first 2603 // backpatch the forward reference so that the reader can skip the records 2604 // efficiently. 2605 Stream.BackpatchWord64(IndexOffsetRecordBitPos - 64, 2606 Stream.GetCurrentBitNo() - IndexOffsetRecordBitPos); 2607 2608 // Delta encode the index. 2609 uint64_t PreviousValue = IndexOffsetRecordBitPos; 2610 for (auto &Elt : IndexPos) { 2611 auto EltDelta = Elt - PreviousValue; 2612 PreviousValue = Elt; 2613 Elt = EltDelta; 2614 } 2615 // Emit the index record. 2616 Stream.EmitRecord(bitc::METADATA_INDEX, IndexPos, IndexAbbrev); 2617 IndexPos.clear(); 2618 } 2619 2620 // Write the named metadata now. 2621 writeNamedMetadata(Record); 2622 2623 auto AddDeclAttachedMetadata = [&](const GlobalObject &GO) { 2624 SmallVector<uint64_t, 4> Record; 2625 Record.push_back(VE.getValueID(&GO)); 2626 pushGlobalMetadataAttachment(Record, GO); 2627 Stream.EmitRecord(bitc::METADATA_GLOBAL_DECL_ATTACHMENT, Record); 2628 }; 2629 for (const Function &F : M) 2630 if (F.isDeclaration() && F.hasMetadata()) 2631 AddDeclAttachedMetadata(F); 2632 // FIXME: Only store metadata for declarations here, and move data for global 2633 // variable definitions to a separate block (PR28134). 2634 for (const GlobalVariable &GV : M.globals()) 2635 if (GV.hasMetadata()) 2636 AddDeclAttachedMetadata(GV); 2637 2638 Stream.ExitBlock(); 2639 } 2640 2641 void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) { 2642 if (!VE.hasMDs()) 2643 return; 2644 2645 Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); 2646 SmallVector<uint64_t, 64> Record; 2647 writeMetadataStrings(VE.getMDStrings(), Record); 2648 writeMetadataRecords(VE.getNonMDStrings(), Record); 2649 Stream.ExitBlock(); 2650 } 2651 2652 void ModuleBitcodeWriter::pushGlobalMetadataAttachment( 2653 SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) { 2654 // [n x [id, mdnode]] 2655 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 2656 GO.getAllMetadata(MDs); 2657 for (const auto &I : MDs) { 2658 Record.push_back(I.first); 2659 Record.push_back(VE.getMetadataID(I.second)); 2660 } 2661 } 2662 2663 void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) { 2664 Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3); 2665 2666 SmallVector<uint64_t, 64> Record; 2667 2668 if (F.hasMetadata()) { 2669 pushGlobalMetadataAttachment(Record, F); 2670 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 2671 Record.clear(); 2672 } 2673 2674 // Write metadata attachments 2675 // METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]] 2676 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 2677 for (const BasicBlock &BB : F) 2678 for (const Instruction &I : BB) { 2679 MDs.clear(); 2680 I.getAllMetadataOtherThanDebugLoc(MDs); 2681 2682 // If no metadata, ignore instruction. 2683 if (MDs.empty()) continue; 2684 2685 Record.push_back(VE.getInstructionID(&I)); 2686 2687 for (const auto &[ID, MD] : MDs) { 2688 Record.push_back(ID); 2689 Record.push_back(VE.getMetadataID(MD)); 2690 } 2691 Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0); 2692 Record.clear(); 2693 } 2694 2695 Stream.ExitBlock(); 2696 } 2697 2698 void ModuleBitcodeWriter::writeModuleMetadataKinds() { 2699 SmallVector<uint64_t, 64> Record; 2700 2701 // Write metadata kinds 2702 // METADATA_KIND - [n x [id, name]] 2703 SmallVector<StringRef, 8> Names; 2704 M.getMDKindNames(Names); 2705 2706 if (Names.empty()) return; 2707 2708 Stream.EnterSubblock(bitc::METADATA_KIND_BLOCK_ID, 3); 2709 2710 for (unsigned MDKindID = 0, e = Names.size(); MDKindID != e; ++MDKindID) { 2711 Record.push_back(MDKindID); 2712 StringRef KName = Names[MDKindID]; 2713 Record.append(KName.begin(), KName.end()); 2714 2715 Stream.EmitRecord(bitc::METADATA_KIND, Record, 0); 2716 Record.clear(); 2717 } 2718 2719 Stream.ExitBlock(); 2720 } 2721 2722 void ModuleBitcodeWriter::writeOperandBundleTags() { 2723 // Write metadata kinds 2724 // 2725 // OPERAND_BUNDLE_TAGS_BLOCK_ID : N x OPERAND_BUNDLE_TAG 2726 // 2727 // OPERAND_BUNDLE_TAG - [strchr x N] 2728 2729 SmallVector<StringRef, 8> Tags; 2730 M.getOperandBundleTags(Tags); 2731 2732 if (Tags.empty()) 2733 return; 2734 2735 Stream.EnterSubblock(bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID, 3); 2736 2737 SmallVector<uint64_t, 64> Record; 2738 2739 for (auto Tag : Tags) { 2740 Record.append(Tag.begin(), Tag.end()); 2741 2742 Stream.EmitRecord(bitc::OPERAND_BUNDLE_TAG, Record, 0); 2743 Record.clear(); 2744 } 2745 2746 Stream.ExitBlock(); 2747 } 2748 2749 void ModuleBitcodeWriter::writeSyncScopeNames() { 2750 SmallVector<StringRef, 8> SSNs; 2751 M.getContext().getSyncScopeNames(SSNs); 2752 if (SSNs.empty()) 2753 return; 2754 2755 Stream.EnterSubblock(bitc::SYNC_SCOPE_NAMES_BLOCK_ID, 2); 2756 2757 SmallVector<uint64_t, 64> Record; 2758 for (auto SSN : SSNs) { 2759 Record.append(SSN.begin(), SSN.end()); 2760 Stream.EmitRecord(bitc::SYNC_SCOPE_NAME, Record, 0); 2761 Record.clear(); 2762 } 2763 2764 Stream.ExitBlock(); 2765 } 2766 2767 void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal, 2768 bool isGlobal) { 2769 if (FirstVal == LastVal) return; 2770 2771 Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4); 2772 2773 unsigned AggregateAbbrev = 0; 2774 unsigned String8Abbrev = 0; 2775 unsigned CString7Abbrev = 0; 2776 unsigned CString6Abbrev = 0; 2777 // If this is a constant pool for the module, emit module-specific abbrevs. 2778 if (isGlobal) { 2779 // Abbrev for CST_CODE_AGGREGATE. 2780 auto Abbv = std::make_shared<BitCodeAbbrev>(); 2781 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); 2782 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2783 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1))); 2784 AggregateAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 2785 2786 // Abbrev for CST_CODE_STRING. 2787 Abbv = std::make_shared<BitCodeAbbrev>(); 2788 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); 2789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2790 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 2791 String8Abbrev = Stream.EmitAbbrev(std::move(Abbv)); 2792 // Abbrev for CST_CODE_CSTRING. 2793 Abbv = std::make_shared<BitCodeAbbrev>(); 2794 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 2795 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2796 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 2797 CString7Abbrev = Stream.EmitAbbrev(std::move(Abbv)); 2798 // Abbrev for CST_CODE_CSTRING. 2799 Abbv = std::make_shared<BitCodeAbbrev>(); 2800 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING)); 2801 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 2802 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 2803 CString6Abbrev = Stream.EmitAbbrev(std::move(Abbv)); 2804 } 2805 2806 SmallVector<uint64_t, 64> Record; 2807 2808 const ValueEnumerator::ValueList &Vals = VE.getValues(); 2809 Type *LastTy = nullptr; 2810 for (unsigned i = FirstVal; i != LastVal; ++i) { 2811 const Value *V = Vals[i].first; 2812 // If we need to switch types, do so now. 2813 if (V->getType() != LastTy) { 2814 LastTy = V->getType(); 2815 Record.push_back(VE.getTypeID(LastTy)); 2816 Stream.EmitRecord(bitc::CST_CODE_SETTYPE, Record, 2817 CONSTANTS_SETTYPE_ABBREV); 2818 Record.clear(); 2819 } 2820 2821 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 2822 Record.push_back(VE.getTypeID(IA->getFunctionType())); 2823 Record.push_back( 2824 unsigned(IA->hasSideEffects()) | unsigned(IA->isAlignStack()) << 1 | 2825 unsigned(IA->getDialect() & 1) << 2 | unsigned(IA->canThrow()) << 3); 2826 2827 // Add the asm string. 2828 StringRef AsmStr = IA->getAsmString(); 2829 Record.push_back(AsmStr.size()); 2830 Record.append(AsmStr.begin(), AsmStr.end()); 2831 2832 // Add the constraint string. 2833 StringRef ConstraintStr = IA->getConstraintString(); 2834 Record.push_back(ConstraintStr.size()); 2835 Record.append(ConstraintStr.begin(), ConstraintStr.end()); 2836 Stream.EmitRecord(bitc::CST_CODE_INLINEASM, Record); 2837 Record.clear(); 2838 continue; 2839 } 2840 const Constant *C = cast<Constant>(V); 2841 unsigned Code = -1U; 2842 unsigned AbbrevToUse = 0; 2843 if (C->isNullValue()) { 2844 Code = bitc::CST_CODE_NULL; 2845 } else if (isa<PoisonValue>(C)) { 2846 Code = bitc::CST_CODE_POISON; 2847 } else if (isa<UndefValue>(C)) { 2848 Code = bitc::CST_CODE_UNDEF; 2849 } else if (const ConstantInt *IV = dyn_cast<ConstantInt>(C)) { 2850 if (IV->getBitWidth() <= 64) { 2851 uint64_t V = IV->getSExtValue(); 2852 emitSignedInt64(Record, V); 2853 Code = bitc::CST_CODE_INTEGER; 2854 AbbrevToUse = CONSTANTS_INTEGER_ABBREV; 2855 } else { // Wide integers, > 64 bits in size. 2856 emitWideAPInt(Record, IV->getValue()); 2857 Code = bitc::CST_CODE_WIDE_INTEGER; 2858 } 2859 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 2860 Code = bitc::CST_CODE_FLOAT; 2861 Type *Ty = CFP->getType()->getScalarType(); 2862 if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() || 2863 Ty->isDoubleTy()) { 2864 Record.push_back(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 2865 } else if (Ty->isX86_FP80Ty()) { 2866 // api needed to prevent premature destruction 2867 // bits are not in the same order as a normal i80 APInt, compensate. 2868 APInt api = CFP->getValueAPF().bitcastToAPInt(); 2869 const uint64_t *p = api.getRawData(); 2870 Record.push_back((p[1] << 48) | (p[0] >> 16)); 2871 Record.push_back(p[0] & 0xffffLL); 2872 } else if (Ty->isFP128Ty() || Ty->isPPC_FP128Ty()) { 2873 APInt api = CFP->getValueAPF().bitcastToAPInt(); 2874 const uint64_t *p = api.getRawData(); 2875 Record.push_back(p[0]); 2876 Record.push_back(p[1]); 2877 } else { 2878 assert(0 && "Unknown FP type!"); 2879 } 2880 } else if (isa<ConstantDataSequential>(C) && 2881 cast<ConstantDataSequential>(C)->isString()) { 2882 const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); 2883 // Emit constant strings specially. 2884 uint64_t NumElts = Str->getNumElements(); 2885 // If this is a null-terminated string, use the denser CSTRING encoding. 2886 if (Str->isCString()) { 2887 Code = bitc::CST_CODE_CSTRING; 2888 --NumElts; // Don't encode the null, which isn't allowed by char6. 2889 } else { 2890 Code = bitc::CST_CODE_STRING; 2891 AbbrevToUse = String8Abbrev; 2892 } 2893 bool isCStr7 = Code == bitc::CST_CODE_CSTRING; 2894 bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; 2895 for (uint64_t i = 0; i != NumElts; ++i) { 2896 unsigned char V = Str->getElementAsInteger(i); 2897 Record.push_back(V); 2898 isCStr7 &= (V & 128) == 0; 2899 if (isCStrChar6) 2900 isCStrChar6 = BitCodeAbbrevOp::isChar6(V); 2901 } 2902 2903 if (isCStrChar6) 2904 AbbrevToUse = CString6Abbrev; 2905 else if (isCStr7) 2906 AbbrevToUse = CString7Abbrev; 2907 } else if (const ConstantDataSequential *CDS = 2908 dyn_cast<ConstantDataSequential>(C)) { 2909 Code = bitc::CST_CODE_DATA; 2910 Type *EltTy = CDS->getElementType(); 2911 if (isa<IntegerType>(EltTy)) { 2912 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) 2913 Record.push_back(CDS->getElementAsInteger(i)); 2914 } else { 2915 for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) 2916 Record.push_back( 2917 CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue()); 2918 } 2919 } else if (isa<ConstantAggregate>(C)) { 2920 Code = bitc::CST_CODE_AGGREGATE; 2921 for (const Value *Op : C->operands()) 2922 Record.push_back(VE.getValueID(Op)); 2923 AbbrevToUse = AggregateAbbrev; 2924 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 2925 switch (CE->getOpcode()) { 2926 default: 2927 if (Instruction::isCast(CE->getOpcode())) { 2928 Code = bitc::CST_CODE_CE_CAST; 2929 Record.push_back(getEncodedCastOpcode(CE->getOpcode())); 2930 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 2931 Record.push_back(VE.getValueID(C->getOperand(0))); 2932 AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; 2933 } else { 2934 assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); 2935 Code = bitc::CST_CODE_CE_BINOP; 2936 Record.push_back(getEncodedBinaryOpcode(CE->getOpcode())); 2937 Record.push_back(VE.getValueID(C->getOperand(0))); 2938 Record.push_back(VE.getValueID(C->getOperand(1))); 2939 uint64_t Flags = getOptimizationFlags(CE); 2940 if (Flags != 0) 2941 Record.push_back(Flags); 2942 } 2943 break; 2944 case Instruction::FNeg: { 2945 assert(CE->getNumOperands() == 1 && "Unknown constant expr!"); 2946 Code = bitc::CST_CODE_CE_UNOP; 2947 Record.push_back(getEncodedUnaryOpcode(CE->getOpcode())); 2948 Record.push_back(VE.getValueID(C->getOperand(0))); 2949 uint64_t Flags = getOptimizationFlags(CE); 2950 if (Flags != 0) 2951 Record.push_back(Flags); 2952 break; 2953 } 2954 case Instruction::GetElementPtr: { 2955 Code = bitc::CST_CODE_CE_GEP; 2956 const auto *GO = cast<GEPOperator>(C); 2957 Record.push_back(VE.getTypeID(GO->getSourceElementType())); 2958 Record.push_back(getOptimizationFlags(GO)); 2959 if (std::optional<ConstantRange> Range = GO->getInRange()) { 2960 Code = bitc::CST_CODE_CE_GEP_WITH_INRANGE; 2961 emitConstantRange(Record, *Range, /*EmitBitWidth=*/true); 2962 } 2963 for (const Value *Op : CE->operands()) { 2964 Record.push_back(VE.getTypeID(Op->getType())); 2965 Record.push_back(VE.getValueID(Op)); 2966 } 2967 break; 2968 } 2969 case Instruction::ExtractElement: 2970 Code = bitc::CST_CODE_CE_EXTRACTELT; 2971 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 2972 Record.push_back(VE.getValueID(C->getOperand(0))); 2973 Record.push_back(VE.getTypeID(C->getOperand(1)->getType())); 2974 Record.push_back(VE.getValueID(C->getOperand(1))); 2975 break; 2976 case Instruction::InsertElement: 2977 Code = bitc::CST_CODE_CE_INSERTELT; 2978 Record.push_back(VE.getValueID(C->getOperand(0))); 2979 Record.push_back(VE.getValueID(C->getOperand(1))); 2980 Record.push_back(VE.getTypeID(C->getOperand(2)->getType())); 2981 Record.push_back(VE.getValueID(C->getOperand(2))); 2982 break; 2983 case Instruction::ShuffleVector: 2984 // If the return type and argument types are the same, this is a 2985 // standard shufflevector instruction. If the types are different, 2986 // then the shuffle is widening or truncating the input vectors, and 2987 // the argument type must also be encoded. 2988 if (C->getType() == C->getOperand(0)->getType()) { 2989 Code = bitc::CST_CODE_CE_SHUFFLEVEC; 2990 } else { 2991 Code = bitc::CST_CODE_CE_SHUFVEC_EX; 2992 Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); 2993 } 2994 Record.push_back(VE.getValueID(C->getOperand(0))); 2995 Record.push_back(VE.getValueID(C->getOperand(1))); 2996 Record.push_back(VE.getValueID(CE->getShuffleMaskForBitcode())); 2997 break; 2998 } 2999 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) { 3000 Code = bitc::CST_CODE_BLOCKADDRESS; 3001 Record.push_back(VE.getTypeID(BA->getFunction()->getType())); 3002 Record.push_back(VE.getValueID(BA->getFunction())); 3003 Record.push_back(VE.getGlobalBasicBlockID(BA->getBasicBlock())); 3004 } else if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(C)) { 3005 Code = bitc::CST_CODE_DSO_LOCAL_EQUIVALENT; 3006 Record.push_back(VE.getTypeID(Equiv->getGlobalValue()->getType())); 3007 Record.push_back(VE.getValueID(Equiv->getGlobalValue())); 3008 } else if (const auto *NC = dyn_cast<NoCFIValue>(C)) { 3009 Code = bitc::CST_CODE_NO_CFI_VALUE; 3010 Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType())); 3011 Record.push_back(VE.getValueID(NC->getGlobalValue())); 3012 } else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) { 3013 Code = bitc::CST_CODE_PTRAUTH; 3014 Record.push_back(VE.getValueID(CPA->getPointer())); 3015 Record.push_back(VE.getValueID(CPA->getKey())); 3016 Record.push_back(VE.getValueID(CPA->getDiscriminator())); 3017 Record.push_back(VE.getValueID(CPA->getAddrDiscriminator())); 3018 } else { 3019 #ifndef NDEBUG 3020 C->dump(); 3021 #endif 3022 llvm_unreachable("Unknown constant!"); 3023 } 3024 Stream.EmitRecord(Code, Record, AbbrevToUse); 3025 Record.clear(); 3026 } 3027 3028 Stream.ExitBlock(); 3029 } 3030 3031 void ModuleBitcodeWriter::writeModuleConstants() { 3032 const ValueEnumerator::ValueList &Vals = VE.getValues(); 3033 3034 // Find the first constant to emit, which is the first non-globalvalue value. 3035 // We know globalvalues have been emitted by WriteModuleInfo. 3036 for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 3037 if (!isa<GlobalValue>(Vals[i].first)) { 3038 writeConstants(i, Vals.size(), true); 3039 return; 3040 } 3041 } 3042 } 3043 3044 /// pushValueAndType - The file has to encode both the value and type id for 3045 /// many values, because we need to know what type to create for forward 3046 /// references. However, most operands are not forward references, so this type 3047 /// field is not needed. 3048 /// 3049 /// This function adds V's value ID to Vals. If the value ID is higher than the 3050 /// instruction ID, then it is a forward reference, and it also includes the 3051 /// type ID. The value ID that is written is encoded relative to the InstID. 3052 bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID, 3053 SmallVectorImpl<unsigned> &Vals) { 3054 unsigned ValID = VE.getValueID(V); 3055 // Make encoding relative to the InstID. 3056 Vals.push_back(InstID - ValID); 3057 if (ValID >= InstID) { 3058 Vals.push_back(VE.getTypeID(V->getType())); 3059 return true; 3060 } 3061 return false; 3062 } 3063 3064 bool ModuleBitcodeWriter::pushValueOrMetadata(const Value *V, unsigned InstID, 3065 SmallVectorImpl<unsigned> &Vals) { 3066 bool IsMetadata = V->getType()->isMetadataTy(); 3067 if (IsMetadata) { 3068 Vals.push_back(bitc::OB_METADATA); 3069 Metadata *MD = cast<MetadataAsValue>(V)->getMetadata(); 3070 unsigned ValID = VE.getMetadataID(MD); 3071 Vals.push_back(InstID - ValID); 3072 return false; 3073 } 3074 return pushValueAndType(V, InstID, Vals); 3075 } 3076 3077 void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS, 3078 unsigned InstID) { 3079 SmallVector<unsigned, 64> Record; 3080 LLVMContext &C = CS.getContext(); 3081 3082 for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) { 3083 const auto &Bundle = CS.getOperandBundleAt(i); 3084 Record.push_back(C.getOperandBundleTagID(Bundle.getTagName())); 3085 3086 for (auto &Input : Bundle.Inputs) 3087 pushValueOrMetadata(Input, InstID, Record); 3088 3089 Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record); 3090 Record.clear(); 3091 } 3092 } 3093 3094 /// pushValue - Like pushValueAndType, but where the type of the value is 3095 /// omitted (perhaps it was already encoded in an earlier operand). 3096 void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID, 3097 SmallVectorImpl<unsigned> &Vals) { 3098 unsigned ValID = VE.getValueID(V); 3099 Vals.push_back(InstID - ValID); 3100 } 3101 3102 void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID, 3103 SmallVectorImpl<uint64_t> &Vals) { 3104 unsigned ValID = VE.getValueID(V); 3105 int64_t diff = ((int32_t)InstID - (int32_t)ValID); 3106 emitSignedInt64(Vals, diff); 3107 } 3108 3109 /// WriteInstruction - Emit an instruction to the specified stream. 3110 void ModuleBitcodeWriter::writeInstruction(const Instruction &I, 3111 unsigned InstID, 3112 SmallVectorImpl<unsigned> &Vals) { 3113 unsigned Code = 0; 3114 unsigned AbbrevToUse = 0; 3115 VE.setInstructionID(&I); 3116 switch (I.getOpcode()) { 3117 default: 3118 if (Instruction::isCast(I.getOpcode())) { 3119 Code = bitc::FUNC_CODE_INST_CAST; 3120 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) 3121 AbbrevToUse = FUNCTION_INST_CAST_ABBREV; 3122 Vals.push_back(VE.getTypeID(I.getType())); 3123 Vals.push_back(getEncodedCastOpcode(I.getOpcode())); 3124 uint64_t Flags = getOptimizationFlags(&I); 3125 if (Flags != 0) { 3126 if (AbbrevToUse == FUNCTION_INST_CAST_ABBREV) 3127 AbbrevToUse = FUNCTION_INST_CAST_FLAGS_ABBREV; 3128 Vals.push_back(Flags); 3129 } 3130 } else { 3131 assert(isa<BinaryOperator>(I) && "Unknown instruction!"); 3132 Code = bitc::FUNC_CODE_INST_BINOP; 3133 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) 3134 AbbrevToUse = FUNCTION_INST_BINOP_ABBREV; 3135 pushValue(I.getOperand(1), InstID, Vals); 3136 Vals.push_back(getEncodedBinaryOpcode(I.getOpcode())); 3137 uint64_t Flags = getOptimizationFlags(&I); 3138 if (Flags != 0) { 3139 if (AbbrevToUse == FUNCTION_INST_BINOP_ABBREV) 3140 AbbrevToUse = FUNCTION_INST_BINOP_FLAGS_ABBREV; 3141 Vals.push_back(Flags); 3142 } 3143 } 3144 break; 3145 case Instruction::FNeg: { 3146 Code = bitc::FUNC_CODE_INST_UNOP; 3147 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) 3148 AbbrevToUse = FUNCTION_INST_UNOP_ABBREV; 3149 Vals.push_back(getEncodedUnaryOpcode(I.getOpcode())); 3150 uint64_t Flags = getOptimizationFlags(&I); 3151 if (Flags != 0) { 3152 if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV) 3153 AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV; 3154 Vals.push_back(Flags); 3155 } 3156 break; 3157 } 3158 case Instruction::GetElementPtr: { 3159 Code = bitc::FUNC_CODE_INST_GEP; 3160 AbbrevToUse = FUNCTION_INST_GEP_ABBREV; 3161 auto &GEPInst = cast<GetElementPtrInst>(I); 3162 Vals.push_back(getOptimizationFlags(&I)); 3163 Vals.push_back(VE.getTypeID(GEPInst.getSourceElementType())); 3164 for (const Value *Op : I.operands()) 3165 pushValueAndType(Op, InstID, Vals); 3166 break; 3167 } 3168 case Instruction::ExtractValue: { 3169 Code = bitc::FUNC_CODE_INST_EXTRACTVAL; 3170 pushValueAndType(I.getOperand(0), InstID, Vals); 3171 const ExtractValueInst *EVI = cast<ExtractValueInst>(&I); 3172 Vals.append(EVI->idx_begin(), EVI->idx_end()); 3173 break; 3174 } 3175 case Instruction::InsertValue: { 3176 Code = bitc::FUNC_CODE_INST_INSERTVAL; 3177 pushValueAndType(I.getOperand(0), InstID, Vals); 3178 pushValueAndType(I.getOperand(1), InstID, Vals); 3179 const InsertValueInst *IVI = cast<InsertValueInst>(&I); 3180 Vals.append(IVI->idx_begin(), IVI->idx_end()); 3181 break; 3182 } 3183 case Instruction::Select: { 3184 Code = bitc::FUNC_CODE_INST_VSELECT; 3185 pushValueAndType(I.getOperand(1), InstID, Vals); 3186 pushValue(I.getOperand(2), InstID, Vals); 3187 pushValueAndType(I.getOperand(0), InstID, Vals); 3188 uint64_t Flags = getOptimizationFlags(&I); 3189 if (Flags != 0) 3190 Vals.push_back(Flags); 3191 break; 3192 } 3193 case Instruction::ExtractElement: 3194 Code = bitc::FUNC_CODE_INST_EXTRACTELT; 3195 pushValueAndType(I.getOperand(0), InstID, Vals); 3196 pushValueAndType(I.getOperand(1), InstID, Vals); 3197 break; 3198 case Instruction::InsertElement: 3199 Code = bitc::FUNC_CODE_INST_INSERTELT; 3200 pushValueAndType(I.getOperand(0), InstID, Vals); 3201 pushValue(I.getOperand(1), InstID, Vals); 3202 pushValueAndType(I.getOperand(2), InstID, Vals); 3203 break; 3204 case Instruction::ShuffleVector: 3205 Code = bitc::FUNC_CODE_INST_SHUFFLEVEC; 3206 pushValueAndType(I.getOperand(0), InstID, Vals); 3207 pushValue(I.getOperand(1), InstID, Vals); 3208 pushValue(cast<ShuffleVectorInst>(I).getShuffleMaskForBitcode(), InstID, 3209 Vals); 3210 break; 3211 case Instruction::ICmp: 3212 case Instruction::FCmp: { 3213 // compare returning Int1Ty or vector of Int1Ty 3214 Code = bitc::FUNC_CODE_INST_CMP2; 3215 AbbrevToUse = FUNCTION_INST_CMP_ABBREV; 3216 if (pushValueAndType(I.getOperand(0), InstID, Vals)) 3217 AbbrevToUse = 0; 3218 pushValue(I.getOperand(1), InstID, Vals); 3219 Vals.push_back(cast<CmpInst>(I).getPredicate()); 3220 uint64_t Flags = getOptimizationFlags(&I); 3221 if (Flags != 0) { 3222 Vals.push_back(Flags); 3223 if (AbbrevToUse) 3224 AbbrevToUse = FUNCTION_INST_CMP_FLAGS_ABBREV; 3225 } 3226 break; 3227 } 3228 3229 case Instruction::Ret: 3230 { 3231 Code = bitc::FUNC_CODE_INST_RET; 3232 unsigned NumOperands = I.getNumOperands(); 3233 if (NumOperands == 0) 3234 AbbrevToUse = FUNCTION_INST_RET_VOID_ABBREV; 3235 else if (NumOperands == 1) { 3236 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) 3237 AbbrevToUse = FUNCTION_INST_RET_VAL_ABBREV; 3238 } else { 3239 for (const Value *Op : I.operands()) 3240 pushValueAndType(Op, InstID, Vals); 3241 } 3242 } 3243 break; 3244 case Instruction::Br: 3245 { 3246 Code = bitc::FUNC_CODE_INST_BR; 3247 AbbrevToUse = FUNCTION_INST_BR_UNCOND_ABBREV; 3248 const BranchInst &II = cast<BranchInst>(I); 3249 Vals.push_back(VE.getValueID(II.getSuccessor(0))); 3250 if (II.isConditional()) { 3251 Vals.push_back(VE.getValueID(II.getSuccessor(1))); 3252 pushValue(II.getCondition(), InstID, Vals); 3253 AbbrevToUse = FUNCTION_INST_BR_COND_ABBREV; 3254 } 3255 } 3256 break; 3257 case Instruction::Switch: 3258 { 3259 Code = bitc::FUNC_CODE_INST_SWITCH; 3260 const SwitchInst &SI = cast<SwitchInst>(I); 3261 Vals.push_back(VE.getTypeID(SI.getCondition()->getType())); 3262 pushValue(SI.getCondition(), InstID, Vals); 3263 Vals.push_back(VE.getValueID(SI.getDefaultDest())); 3264 for (auto Case : SI.cases()) { 3265 Vals.push_back(VE.getValueID(Case.getCaseValue())); 3266 Vals.push_back(VE.getValueID(Case.getCaseSuccessor())); 3267 } 3268 } 3269 break; 3270 case Instruction::IndirectBr: 3271 Code = bitc::FUNC_CODE_INST_INDIRECTBR; 3272 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 3273 // Encode the address operand as relative, but not the basic blocks. 3274 pushValue(I.getOperand(0), InstID, Vals); 3275 for (const Value *Op : drop_begin(I.operands())) 3276 Vals.push_back(VE.getValueID(Op)); 3277 break; 3278 3279 case Instruction::Invoke: { 3280 const InvokeInst *II = cast<InvokeInst>(&I); 3281 const Value *Callee = II->getCalledOperand(); 3282 FunctionType *FTy = II->getFunctionType(); 3283 3284 if (II->hasOperandBundles()) 3285 writeOperandBundles(*II, InstID); 3286 3287 Code = bitc::FUNC_CODE_INST_INVOKE; 3288 3289 Vals.push_back(VE.getAttributeListID(II->getAttributes())); 3290 Vals.push_back(II->getCallingConv() | 1 << 13); 3291 Vals.push_back(VE.getValueID(II->getNormalDest())); 3292 Vals.push_back(VE.getValueID(II->getUnwindDest())); 3293 Vals.push_back(VE.getTypeID(FTy)); 3294 pushValueAndType(Callee, InstID, Vals); 3295 3296 // Emit value #'s for the fixed parameters. 3297 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 3298 pushValue(I.getOperand(i), InstID, Vals); // fixed param. 3299 3300 // Emit type/value pairs for varargs params. 3301 if (FTy->isVarArg()) { 3302 for (unsigned i = FTy->getNumParams(), e = II->arg_size(); i != e; ++i) 3303 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg 3304 } 3305 break; 3306 } 3307 case Instruction::Resume: 3308 Code = bitc::FUNC_CODE_INST_RESUME; 3309 pushValueAndType(I.getOperand(0), InstID, Vals); 3310 break; 3311 case Instruction::CleanupRet: { 3312 Code = bitc::FUNC_CODE_INST_CLEANUPRET; 3313 const auto &CRI = cast<CleanupReturnInst>(I); 3314 pushValue(CRI.getCleanupPad(), InstID, Vals); 3315 if (CRI.hasUnwindDest()) 3316 Vals.push_back(VE.getValueID(CRI.getUnwindDest())); 3317 break; 3318 } 3319 case Instruction::CatchRet: { 3320 Code = bitc::FUNC_CODE_INST_CATCHRET; 3321 const auto &CRI = cast<CatchReturnInst>(I); 3322 pushValue(CRI.getCatchPad(), InstID, Vals); 3323 Vals.push_back(VE.getValueID(CRI.getSuccessor())); 3324 break; 3325 } 3326 case Instruction::CleanupPad: 3327 case Instruction::CatchPad: { 3328 const auto &FuncletPad = cast<FuncletPadInst>(I); 3329 Code = isa<CatchPadInst>(FuncletPad) ? bitc::FUNC_CODE_INST_CATCHPAD 3330 : bitc::FUNC_CODE_INST_CLEANUPPAD; 3331 pushValue(FuncletPad.getParentPad(), InstID, Vals); 3332 3333 unsigned NumArgOperands = FuncletPad.arg_size(); 3334 Vals.push_back(NumArgOperands); 3335 for (unsigned Op = 0; Op != NumArgOperands; ++Op) 3336 pushValueAndType(FuncletPad.getArgOperand(Op), InstID, Vals); 3337 break; 3338 } 3339 case Instruction::CatchSwitch: { 3340 Code = bitc::FUNC_CODE_INST_CATCHSWITCH; 3341 const auto &CatchSwitch = cast<CatchSwitchInst>(I); 3342 3343 pushValue(CatchSwitch.getParentPad(), InstID, Vals); 3344 3345 unsigned NumHandlers = CatchSwitch.getNumHandlers(); 3346 Vals.push_back(NumHandlers); 3347 for (const BasicBlock *CatchPadBB : CatchSwitch.handlers()) 3348 Vals.push_back(VE.getValueID(CatchPadBB)); 3349 3350 if (CatchSwitch.hasUnwindDest()) 3351 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest())); 3352 break; 3353 } 3354 case Instruction::CallBr: { 3355 const CallBrInst *CBI = cast<CallBrInst>(&I); 3356 const Value *Callee = CBI->getCalledOperand(); 3357 FunctionType *FTy = CBI->getFunctionType(); 3358 3359 if (CBI->hasOperandBundles()) 3360 writeOperandBundles(*CBI, InstID); 3361 3362 Code = bitc::FUNC_CODE_INST_CALLBR; 3363 3364 Vals.push_back(VE.getAttributeListID(CBI->getAttributes())); 3365 3366 Vals.push_back(CBI->getCallingConv() << bitc::CALL_CCONV | 3367 1 << bitc::CALL_EXPLICIT_TYPE); 3368 3369 Vals.push_back(VE.getValueID(CBI->getDefaultDest())); 3370 Vals.push_back(CBI->getNumIndirectDests()); 3371 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) 3372 Vals.push_back(VE.getValueID(CBI->getIndirectDest(i))); 3373 3374 Vals.push_back(VE.getTypeID(FTy)); 3375 pushValueAndType(Callee, InstID, Vals); 3376 3377 // Emit value #'s for the fixed parameters. 3378 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 3379 pushValue(I.getOperand(i), InstID, Vals); // fixed param. 3380 3381 // Emit type/value pairs for varargs params. 3382 if (FTy->isVarArg()) { 3383 for (unsigned i = FTy->getNumParams(), e = CBI->arg_size(); i != e; ++i) 3384 pushValueAndType(I.getOperand(i), InstID, Vals); // vararg 3385 } 3386 break; 3387 } 3388 case Instruction::Unreachable: 3389 Code = bitc::FUNC_CODE_INST_UNREACHABLE; 3390 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV; 3391 break; 3392 3393 case Instruction::PHI: { 3394 const PHINode &PN = cast<PHINode>(I); 3395 Code = bitc::FUNC_CODE_INST_PHI; 3396 // With the newer instruction encoding, forward references could give 3397 // negative valued IDs. This is most common for PHIs, so we use 3398 // signed VBRs. 3399 SmallVector<uint64_t, 128> Vals64; 3400 Vals64.push_back(VE.getTypeID(PN.getType())); 3401 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 3402 pushValueSigned(PN.getIncomingValue(i), InstID, Vals64); 3403 Vals64.push_back(VE.getValueID(PN.getIncomingBlock(i))); 3404 } 3405 3406 uint64_t Flags = getOptimizationFlags(&I); 3407 if (Flags != 0) 3408 Vals64.push_back(Flags); 3409 3410 // Emit a Vals64 vector and exit. 3411 Stream.EmitRecord(Code, Vals64, AbbrevToUse); 3412 Vals64.clear(); 3413 return; 3414 } 3415 3416 case Instruction::LandingPad: { 3417 const LandingPadInst &LP = cast<LandingPadInst>(I); 3418 Code = bitc::FUNC_CODE_INST_LANDINGPAD; 3419 Vals.push_back(VE.getTypeID(LP.getType())); 3420 Vals.push_back(LP.isCleanup()); 3421 Vals.push_back(LP.getNumClauses()); 3422 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) { 3423 if (LP.isCatch(I)) 3424 Vals.push_back(LandingPadInst::Catch); 3425 else 3426 Vals.push_back(LandingPadInst::Filter); 3427 pushValueAndType(LP.getClause(I), InstID, Vals); 3428 } 3429 break; 3430 } 3431 3432 case Instruction::Alloca: { 3433 Code = bitc::FUNC_CODE_INST_ALLOCA; 3434 const AllocaInst &AI = cast<AllocaInst>(I); 3435 Vals.push_back(VE.getTypeID(AI.getAllocatedType())); 3436 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); 3437 Vals.push_back(VE.getValueID(I.getOperand(0))); // size. 3438 using APV = AllocaPackedValues; 3439 unsigned Record = 0; 3440 unsigned EncodedAlign = getEncodedAlign(AI.getAlign()); 3441 Bitfield::set<APV::AlignLower>( 3442 Record, EncodedAlign & ((1 << APV::AlignLower::Bits) - 1)); 3443 Bitfield::set<APV::AlignUpper>(Record, 3444 EncodedAlign >> APV::AlignLower::Bits); 3445 Bitfield::set<APV::UsedWithInAlloca>(Record, AI.isUsedWithInAlloca()); 3446 Bitfield::set<APV::ExplicitType>(Record, true); 3447 Bitfield::set<APV::SwiftError>(Record, AI.isSwiftError()); 3448 Vals.push_back(Record); 3449 3450 unsigned AS = AI.getAddressSpace(); 3451 if (AS != M.getDataLayout().getAllocaAddrSpace()) 3452 Vals.push_back(AS); 3453 break; 3454 } 3455 3456 case Instruction::Load: 3457 if (cast<LoadInst>(I).isAtomic()) { 3458 Code = bitc::FUNC_CODE_INST_LOADATOMIC; 3459 pushValueAndType(I.getOperand(0), InstID, Vals); 3460 } else { 3461 Code = bitc::FUNC_CODE_INST_LOAD; 3462 if (!pushValueAndType(I.getOperand(0), InstID, Vals)) // ptr 3463 AbbrevToUse = FUNCTION_INST_LOAD_ABBREV; 3464 } 3465 Vals.push_back(VE.getTypeID(I.getType())); 3466 Vals.push_back(getEncodedAlign(cast<LoadInst>(I).getAlign())); 3467 Vals.push_back(cast<LoadInst>(I).isVolatile()); 3468 if (cast<LoadInst>(I).isAtomic()) { 3469 Vals.push_back(getEncodedOrdering(cast<LoadInst>(I).getOrdering())); 3470 Vals.push_back(getEncodedSyncScopeID(cast<LoadInst>(I).getSyncScopeID())); 3471 } 3472 break; 3473 case Instruction::Store: 3474 if (cast<StoreInst>(I).isAtomic()) { 3475 Code = bitc::FUNC_CODE_INST_STOREATOMIC; 3476 } else { 3477 Code = bitc::FUNC_CODE_INST_STORE; 3478 AbbrevToUse = FUNCTION_INST_STORE_ABBREV; 3479 } 3480 if (pushValueAndType(I.getOperand(1), InstID, Vals)) // ptrty + ptr 3481 AbbrevToUse = 0; 3482 if (pushValueAndType(I.getOperand(0), InstID, Vals)) // valty + val 3483 AbbrevToUse = 0; 3484 Vals.push_back(getEncodedAlign(cast<StoreInst>(I).getAlign())); 3485 Vals.push_back(cast<StoreInst>(I).isVolatile()); 3486 if (cast<StoreInst>(I).isAtomic()) { 3487 Vals.push_back(getEncodedOrdering(cast<StoreInst>(I).getOrdering())); 3488 Vals.push_back( 3489 getEncodedSyncScopeID(cast<StoreInst>(I).getSyncScopeID())); 3490 } 3491 break; 3492 case Instruction::AtomicCmpXchg: 3493 Code = bitc::FUNC_CODE_INST_CMPXCHG; 3494 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr 3495 pushValueAndType(I.getOperand(1), InstID, Vals); // cmp. 3496 pushValue(I.getOperand(2), InstID, Vals); // newval. 3497 Vals.push_back(cast<AtomicCmpXchgInst>(I).isVolatile()); 3498 Vals.push_back( 3499 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getSuccessOrdering())); 3500 Vals.push_back( 3501 getEncodedSyncScopeID(cast<AtomicCmpXchgInst>(I).getSyncScopeID())); 3502 Vals.push_back( 3503 getEncodedOrdering(cast<AtomicCmpXchgInst>(I).getFailureOrdering())); 3504 Vals.push_back(cast<AtomicCmpXchgInst>(I).isWeak()); 3505 Vals.push_back(getEncodedAlign(cast<AtomicCmpXchgInst>(I).getAlign())); 3506 break; 3507 case Instruction::AtomicRMW: 3508 Code = bitc::FUNC_CODE_INST_ATOMICRMW; 3509 pushValueAndType(I.getOperand(0), InstID, Vals); // ptrty + ptr 3510 pushValueAndType(I.getOperand(1), InstID, Vals); // valty + val 3511 Vals.push_back( 3512 getEncodedRMWOperation(cast<AtomicRMWInst>(I).getOperation())); 3513 Vals.push_back(cast<AtomicRMWInst>(I).isVolatile()); 3514 Vals.push_back(getEncodedOrdering(cast<AtomicRMWInst>(I).getOrdering())); 3515 Vals.push_back( 3516 getEncodedSyncScopeID(cast<AtomicRMWInst>(I).getSyncScopeID())); 3517 Vals.push_back(getEncodedAlign(cast<AtomicRMWInst>(I).getAlign())); 3518 break; 3519 case Instruction::Fence: 3520 Code = bitc::FUNC_CODE_INST_FENCE; 3521 Vals.push_back(getEncodedOrdering(cast<FenceInst>(I).getOrdering())); 3522 Vals.push_back(getEncodedSyncScopeID(cast<FenceInst>(I).getSyncScopeID())); 3523 break; 3524 case Instruction::Call: { 3525 const CallInst &CI = cast<CallInst>(I); 3526 FunctionType *FTy = CI.getFunctionType(); 3527 3528 if (CI.hasOperandBundles()) 3529 writeOperandBundles(CI, InstID); 3530 3531 Code = bitc::FUNC_CODE_INST_CALL; 3532 3533 Vals.push_back(VE.getAttributeListID(CI.getAttributes())); 3534 3535 unsigned Flags = getOptimizationFlags(&I); 3536 Vals.push_back(CI.getCallingConv() << bitc::CALL_CCONV | 3537 unsigned(CI.isTailCall()) << bitc::CALL_TAIL | 3538 unsigned(CI.isMustTailCall()) << bitc::CALL_MUSTTAIL | 3539 1 << bitc::CALL_EXPLICIT_TYPE | 3540 unsigned(CI.isNoTailCall()) << bitc::CALL_NOTAIL | 3541 unsigned(Flags != 0) << bitc::CALL_FMF); 3542 if (Flags != 0) 3543 Vals.push_back(Flags); 3544 3545 Vals.push_back(VE.getTypeID(FTy)); 3546 pushValueAndType(CI.getCalledOperand(), InstID, Vals); // Callee 3547 3548 // Emit value #'s for the fixed parameters. 3549 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) 3550 pushValue(CI.getArgOperand(i), InstID, Vals); // fixed param. 3551 3552 // Emit type/value pairs for varargs params. 3553 if (FTy->isVarArg()) { 3554 for (unsigned i = FTy->getNumParams(), e = CI.arg_size(); i != e; ++i) 3555 pushValueAndType(CI.getArgOperand(i), InstID, Vals); // varargs 3556 } 3557 break; 3558 } 3559 case Instruction::VAArg: 3560 Code = bitc::FUNC_CODE_INST_VAARG; 3561 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType())); // valistty 3562 pushValue(I.getOperand(0), InstID, Vals); // valist. 3563 Vals.push_back(VE.getTypeID(I.getType())); // restype. 3564 break; 3565 case Instruction::Freeze: 3566 Code = bitc::FUNC_CODE_INST_FREEZE; 3567 pushValueAndType(I.getOperand(0), InstID, Vals); 3568 break; 3569 } 3570 3571 Stream.EmitRecord(Code, Vals, AbbrevToUse); 3572 Vals.clear(); 3573 } 3574 3575 /// Write a GlobalValue VST to the module. The purpose of this data structure is 3576 /// to allow clients to efficiently find the function body. 3577 void ModuleBitcodeWriter::writeGlobalValueSymbolTable( 3578 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) { 3579 // Get the offset of the VST we are writing, and backpatch it into 3580 // the VST forward declaration record. 3581 uint64_t VSTOffset = Stream.GetCurrentBitNo(); 3582 // The BitcodeStartBit was the stream offset of the identification block. 3583 VSTOffset -= bitcodeStartBit(); 3584 assert((VSTOffset & 31) == 0 && "VST block not 32-bit aligned"); 3585 // Note that we add 1 here because the offset is relative to one word 3586 // before the start of the identification block, which was historically 3587 // always the start of the regular bitcode header. 3588 Stream.BackpatchWord(VSTOffsetPlaceholder, VSTOffset / 32 + 1); 3589 3590 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 3591 3592 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3593 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_FNENTRY)); 3594 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 3595 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // funcoffset 3596 unsigned FnEntryAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 3597 3598 for (const Function &F : M) { 3599 uint64_t Record[2]; 3600 3601 if (F.isDeclaration()) 3602 continue; 3603 3604 Record[0] = VE.getValueID(&F); 3605 3606 // Save the word offset of the function (from the start of the 3607 // actual bitcode written to the stream). 3608 uint64_t BitcodeIndex = FunctionToBitcodeIndex[&F] - bitcodeStartBit(); 3609 assert((BitcodeIndex & 31) == 0 && "function block not 32-bit aligned"); 3610 // Note that we add 1 here because the offset is relative to one word 3611 // before the start of the identification block, which was historically 3612 // always the start of the regular bitcode header. 3613 Record[1] = BitcodeIndex / 32 + 1; 3614 3615 Stream.EmitRecord(bitc::VST_CODE_FNENTRY, Record, FnEntryAbbrev); 3616 } 3617 3618 Stream.ExitBlock(); 3619 } 3620 3621 /// Emit names for arguments, instructions and basic blocks in a function. 3622 void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable( 3623 const ValueSymbolTable &VST) { 3624 if (VST.empty()) 3625 return; 3626 3627 Stream.EnterSubblock(bitc::VALUE_SYMTAB_BLOCK_ID, 4); 3628 3629 // FIXME: Set up the abbrev, we know how many values there are! 3630 // FIXME: We know if the type names can use 7-bit ascii. 3631 SmallVector<uint64_t, 64> NameVals; 3632 3633 for (const ValueName &Name : VST) { 3634 // Figure out the encoding to use for the name. 3635 StringEncoding Bits = getStringEncoding(Name.getKey()); 3636 3637 unsigned AbbrevToUse = VST_ENTRY_8_ABBREV; 3638 NameVals.push_back(VE.getValueID(Name.getValue())); 3639 3640 // VST_CODE_ENTRY: [valueid, namechar x N] 3641 // VST_CODE_BBENTRY: [bbid, namechar x N] 3642 unsigned Code; 3643 if (isa<BasicBlock>(Name.getValue())) { 3644 Code = bitc::VST_CODE_BBENTRY; 3645 if (Bits == SE_Char6) 3646 AbbrevToUse = VST_BBENTRY_6_ABBREV; 3647 } else { 3648 Code = bitc::VST_CODE_ENTRY; 3649 if (Bits == SE_Char6) 3650 AbbrevToUse = VST_ENTRY_6_ABBREV; 3651 else if (Bits == SE_Fixed7) 3652 AbbrevToUse = VST_ENTRY_7_ABBREV; 3653 } 3654 3655 for (const auto P : Name.getKey()) 3656 NameVals.push_back((unsigned char)P); 3657 3658 // Emit the finished record. 3659 Stream.EmitRecord(Code, NameVals, AbbrevToUse); 3660 NameVals.clear(); 3661 } 3662 3663 Stream.ExitBlock(); 3664 } 3665 3666 void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) { 3667 assert(Order.Shuffle.size() >= 2 && "Shuffle too small"); 3668 unsigned Code; 3669 if (isa<BasicBlock>(Order.V)) 3670 Code = bitc::USELIST_CODE_BB; 3671 else 3672 Code = bitc::USELIST_CODE_DEFAULT; 3673 3674 SmallVector<uint64_t, 64> Record(Order.Shuffle.begin(), Order.Shuffle.end()); 3675 Record.push_back(VE.getValueID(Order.V)); 3676 Stream.EmitRecord(Code, Record); 3677 } 3678 3679 void ModuleBitcodeWriter::writeUseListBlock(const Function *F) { 3680 assert(VE.shouldPreserveUseListOrder() && 3681 "Expected to be preserving use-list order"); 3682 3683 auto hasMore = [&]() { 3684 return !VE.UseListOrders.empty() && VE.UseListOrders.back().F == F; 3685 }; 3686 if (!hasMore()) 3687 // Nothing to do. 3688 return; 3689 3690 Stream.EnterSubblock(bitc::USELIST_BLOCK_ID, 3); 3691 while (hasMore()) { 3692 writeUseList(std::move(VE.UseListOrders.back())); 3693 VE.UseListOrders.pop_back(); 3694 } 3695 Stream.ExitBlock(); 3696 } 3697 3698 /// Emit a function body to the module stream. 3699 void ModuleBitcodeWriter::writeFunction( 3700 const Function &F, 3701 DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) { 3702 // Save the bitcode index of the start of this function block for recording 3703 // in the VST. 3704 FunctionToBitcodeIndex[&F] = Stream.GetCurrentBitNo(); 3705 3706 Stream.EnterSubblock(bitc::FUNCTION_BLOCK_ID, 5); 3707 VE.incorporateFunction(F); 3708 3709 SmallVector<unsigned, 64> Vals; 3710 3711 // Emit the number of basic blocks, so the reader can create them ahead of 3712 // time. 3713 Vals.push_back(VE.getBasicBlocks().size()); 3714 Stream.EmitRecord(bitc::FUNC_CODE_DECLAREBLOCKS, Vals); 3715 Vals.clear(); 3716 3717 // If there are function-local constants, emit them now. 3718 unsigned CstStart, CstEnd; 3719 VE.getFunctionConstantRange(CstStart, CstEnd); 3720 writeConstants(CstStart, CstEnd, false); 3721 3722 // If there is function-local metadata, emit it now. 3723 writeFunctionMetadata(F); 3724 3725 // Keep a running idea of what the instruction ID is. 3726 unsigned InstID = CstEnd; 3727 3728 bool NeedsMetadataAttachment = F.hasMetadata(); 3729 3730 DILocation *LastDL = nullptr; 3731 SmallSetVector<Function *, 4> BlockAddressUsers; 3732 3733 // Finally, emit all the instructions, in order. 3734 for (const BasicBlock &BB : F) { 3735 for (const Instruction &I : BB) { 3736 writeInstruction(I, InstID, Vals); 3737 3738 if (!I.getType()->isVoidTy()) 3739 ++InstID; 3740 3741 // If the instruction has metadata, write a metadata attachment later. 3742 NeedsMetadataAttachment |= I.hasMetadataOtherThanDebugLoc(); 3743 3744 // If the instruction has a debug location, emit it. 3745 if (DILocation *DL = I.getDebugLoc()) { 3746 if (DL == LastDL) { 3747 // Just repeat the same debug loc as last time. 3748 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC_AGAIN, Vals); 3749 } else { 3750 Vals.push_back(DL->getLine()); 3751 Vals.push_back(DL->getColumn()); 3752 Vals.push_back(VE.getMetadataOrNullID(DL->getScope())); 3753 Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt())); 3754 Vals.push_back(DL->isImplicitCode()); 3755 Vals.push_back(DL->getAtomGroup()); 3756 Vals.push_back(DL->getAtomRank()); 3757 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals, 3758 FUNCTION_DEBUG_LOC_ABBREV); 3759 Vals.clear(); 3760 LastDL = DL; 3761 } 3762 } 3763 3764 // If the instruction has DbgRecords attached to it, emit them. Note that 3765 // they come after the instruction so that it's easy to attach them again 3766 // when reading the bitcode, even though conceptually the debug locations 3767 // start "before" the instruction. 3768 if (I.hasDbgRecords()) { 3769 /// Try to push the value only (unwrapped), otherwise push the 3770 /// metadata wrapped value. Returns true if the value was pushed 3771 /// without the ValueAsMetadata wrapper. 3772 auto PushValueOrMetadata = [&Vals, InstID, 3773 this](Metadata *RawLocation) { 3774 assert(RawLocation && 3775 "RawLocation unexpectedly null in DbgVariableRecord"); 3776 if (ValueAsMetadata *VAM = dyn_cast<ValueAsMetadata>(RawLocation)) { 3777 SmallVector<unsigned, 2> ValAndType; 3778 // If the value is a fwd-ref the type is also pushed. We don't 3779 // want the type, so fwd-refs are kept wrapped (pushValueAndType 3780 // returns false if the value is pushed without type). 3781 if (!pushValueAndType(VAM->getValue(), InstID, ValAndType)) { 3782 Vals.push_back(ValAndType[0]); 3783 return true; 3784 } 3785 } 3786 // The metadata is a DIArgList, or ValueAsMetadata wrapping a 3787 // fwd-ref. Push the metadata ID. 3788 Vals.push_back(VE.getMetadataID(RawLocation)); 3789 return false; 3790 }; 3791 3792 // Write out non-instruction debug information attached to this 3793 // instruction. Write it after the instruction so that it's easy to 3794 // re-attach to the instruction reading the records in. 3795 for (DbgRecord &DR : I.DebugMarker->getDbgRecordRange()) { 3796 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) { 3797 Vals.push_back(VE.getMetadataID(&*DLR->getDebugLoc())); 3798 Vals.push_back(VE.getMetadataID(DLR->getLabel())); 3799 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_LABEL, Vals); 3800 Vals.clear(); 3801 continue; 3802 } 3803 3804 // First 3 fields are common to all kinds: 3805 // DILocation, DILocalVariable, DIExpression 3806 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE) 3807 // ..., LocationMetadata 3808 // dbg_value (FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE - abbrev'd) 3809 // ..., Value 3810 // dbg_declare (FUNC_CODE_DEBUG_RECORD_DECLARE) 3811 // ..., LocationMetadata 3812 // dbg_assign (FUNC_CODE_DEBUG_RECORD_ASSIGN) 3813 // ..., LocationMetadata, DIAssignID, DIExpression, LocationMetadata 3814 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR); 3815 Vals.push_back(VE.getMetadataID(&*DVR.getDebugLoc())); 3816 Vals.push_back(VE.getMetadataID(DVR.getVariable())); 3817 Vals.push_back(VE.getMetadataID(DVR.getExpression())); 3818 if (DVR.isDbgValue()) { 3819 if (PushValueOrMetadata(DVR.getRawLocation())) 3820 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE, Vals, 3821 FUNCTION_DEBUG_RECORD_VALUE_ABBREV); 3822 else 3823 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_VALUE, Vals); 3824 } else if (DVR.isDbgDeclare()) { 3825 Vals.push_back(VE.getMetadataID(DVR.getRawLocation())); 3826 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_DECLARE, Vals); 3827 } else { 3828 assert(DVR.isDbgAssign() && "Unexpected DbgRecord kind"); 3829 Vals.push_back(VE.getMetadataID(DVR.getRawLocation())); 3830 Vals.push_back(VE.getMetadataID(DVR.getAssignID())); 3831 Vals.push_back(VE.getMetadataID(DVR.getAddressExpression())); 3832 Vals.push_back(VE.getMetadataID(DVR.getRawAddress())); 3833 Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_RECORD_ASSIGN, Vals); 3834 } 3835 Vals.clear(); 3836 } 3837 } 3838 } 3839 3840 if (BlockAddress *BA = BlockAddress::lookup(&BB)) { 3841 SmallVector<Value *> Worklist{BA}; 3842 SmallPtrSet<Value *, 8> Visited{BA}; 3843 while (!Worklist.empty()) { 3844 Value *V = Worklist.pop_back_val(); 3845 for (User *U : V->users()) { 3846 if (auto *I = dyn_cast<Instruction>(U)) { 3847 Function *P = I->getFunction(); 3848 if (P != &F) 3849 BlockAddressUsers.insert(P); 3850 } else if (isa<Constant>(U) && !isa<GlobalValue>(U) && 3851 Visited.insert(U).second) 3852 Worklist.push_back(U); 3853 } 3854 } 3855 } 3856 } 3857 3858 if (!BlockAddressUsers.empty()) { 3859 Vals.resize(BlockAddressUsers.size()); 3860 for (auto I : llvm::enumerate(BlockAddressUsers)) 3861 Vals[I.index()] = VE.getValueID(I.value()); 3862 Stream.EmitRecord(bitc::FUNC_CODE_BLOCKADDR_USERS, Vals); 3863 Vals.clear(); 3864 } 3865 3866 // Emit names for all the instructions etc. 3867 if (auto *Symtab = F.getValueSymbolTable()) 3868 writeFunctionLevelValueSymbolTable(*Symtab); 3869 3870 if (NeedsMetadataAttachment) 3871 writeFunctionMetadataAttachment(F); 3872 if (VE.shouldPreserveUseListOrder()) 3873 writeUseListBlock(&F); 3874 VE.purgeFunction(); 3875 Stream.ExitBlock(); 3876 } 3877 3878 // Emit blockinfo, which defines the standard abbreviations etc. 3879 void ModuleBitcodeWriter::writeBlockInfo() { 3880 // We only want to emit block info records for blocks that have multiple 3881 // instances: CONSTANTS_BLOCK, FUNCTION_BLOCK and VALUE_SYMTAB_BLOCK. 3882 // Other blocks can define their abbrevs inline. 3883 Stream.EnterBlockInfoBlock(); 3884 3885 // Encode type indices using fixed size based on number of types. 3886 BitCodeAbbrevOp TypeAbbrevOp(BitCodeAbbrevOp::Fixed, 3887 VE.computeBitsRequiredForTypeIndices()); 3888 // Encode value indices as 6-bit VBR. 3889 BitCodeAbbrevOp ValAbbrevOp(BitCodeAbbrevOp::VBR, 6); 3890 3891 { // 8-bit fixed-width VST_CODE_ENTRY/VST_CODE_BBENTRY strings. 3892 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3893 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); 3894 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3895 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3896 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 3897 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) != 3898 VST_ENTRY_8_ABBREV) 3899 llvm_unreachable("Unexpected abbrev ordering!"); 3900 } 3901 3902 { // 7-bit fixed width VST_CODE_ENTRY strings. 3903 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3904 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 3905 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3906 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3907 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 3908 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) != 3909 VST_ENTRY_7_ABBREV) 3910 llvm_unreachable("Unexpected abbrev ordering!"); 3911 } 3912 { // 6-bit char6 VST_CODE_ENTRY strings. 3913 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3914 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_ENTRY)); 3915 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3916 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3917 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 3918 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) != 3919 VST_ENTRY_6_ABBREV) 3920 llvm_unreachable("Unexpected abbrev ordering!"); 3921 } 3922 { // 6-bit char6 VST_CODE_BBENTRY strings. 3923 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3924 Abbv->Add(BitCodeAbbrevOp(bitc::VST_CODE_BBENTRY)); 3925 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3926 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 3927 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 3928 if (Stream.EmitBlockInfoAbbrev(bitc::VALUE_SYMTAB_BLOCK_ID, Abbv) != 3929 VST_BBENTRY_6_ABBREV) 3930 llvm_unreachable("Unexpected abbrev ordering!"); 3931 } 3932 3933 { // SETTYPE abbrev for CONSTANTS_BLOCK. 3934 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3935 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_SETTYPE)); 3936 Abbv->Add(TypeAbbrevOp); 3937 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) != 3938 CONSTANTS_SETTYPE_ABBREV) 3939 llvm_unreachable("Unexpected abbrev ordering!"); 3940 } 3941 3942 { // INTEGER abbrev for CONSTANTS_BLOCK. 3943 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3944 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_INTEGER)); 3945 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 3946 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) != 3947 CONSTANTS_INTEGER_ABBREV) 3948 llvm_unreachable("Unexpected abbrev ordering!"); 3949 } 3950 3951 { // CE_CAST abbrev for CONSTANTS_BLOCK. 3952 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3953 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CE_CAST)); 3954 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // cast opc 3955 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, // typeid 3956 VE.computeBitsRequiredForTypeIndices())); 3957 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // value id 3958 3959 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) != 3960 CONSTANTS_CE_CAST_Abbrev) 3961 llvm_unreachable("Unexpected abbrev ordering!"); 3962 } 3963 { // NULL abbrev for CONSTANTS_BLOCK. 3964 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3965 Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_NULL)); 3966 if (Stream.EmitBlockInfoAbbrev(bitc::CONSTANTS_BLOCK_ID, Abbv) != 3967 CONSTANTS_NULL_Abbrev) 3968 llvm_unreachable("Unexpected abbrev ordering!"); 3969 } 3970 3971 // FIXME: This should only use space for first class types! 3972 3973 { // INST_LOAD abbrev for FUNCTION_BLOCK. 3974 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3975 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_LOAD)); 3976 Abbv->Add(ValAbbrevOp); // Ptr 3977 Abbv->Add(TypeAbbrevOp); // dest ty 3978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // Align 3979 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 3980 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 3981 FUNCTION_INST_LOAD_ABBREV) 3982 llvm_unreachable("Unexpected abbrev ordering!"); 3983 } 3984 { 3985 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3986 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_STORE)); 3987 Abbv->Add(ValAbbrevOp); // op1 3988 Abbv->Add(ValAbbrevOp); // op0 3989 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // align 3990 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // volatile 3991 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 3992 FUNCTION_INST_STORE_ABBREV) 3993 llvm_unreachable("Unexpected abbrev ordering!"); 3994 } 3995 { // INST_UNOP abbrev for FUNCTION_BLOCK. 3996 auto Abbv = std::make_shared<BitCodeAbbrev>(); 3997 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP)); 3998 Abbv->Add(ValAbbrevOp); // LHS 3999 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4000 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4001 FUNCTION_INST_UNOP_ABBREV) 4002 llvm_unreachable("Unexpected abbrev ordering!"); 4003 } 4004 { // INST_UNOP_FLAGS abbrev for FUNCTION_BLOCK. 4005 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4006 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNOP)); 4007 Abbv->Add(ValAbbrevOp); // LHS 4008 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4009 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags 4010 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4011 FUNCTION_INST_UNOP_FLAGS_ABBREV) 4012 llvm_unreachable("Unexpected abbrev ordering!"); 4013 } 4014 { // INST_BINOP abbrev for FUNCTION_BLOCK. 4015 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4016 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 4017 Abbv->Add(ValAbbrevOp); // LHS 4018 Abbv->Add(ValAbbrevOp); // RHS 4019 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4020 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4021 FUNCTION_INST_BINOP_ABBREV) 4022 llvm_unreachable("Unexpected abbrev ordering!"); 4023 } 4024 { // INST_BINOP_FLAGS abbrev for FUNCTION_BLOCK. 4025 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4026 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BINOP)); 4027 Abbv->Add(ValAbbrevOp); // LHS 4028 Abbv->Add(ValAbbrevOp); // RHS 4029 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4030 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags 4031 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4032 FUNCTION_INST_BINOP_FLAGS_ABBREV) 4033 llvm_unreachable("Unexpected abbrev ordering!"); 4034 } 4035 { // INST_CAST abbrev for FUNCTION_BLOCK. 4036 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4037 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 4038 Abbv->Add(ValAbbrevOp); // OpVal 4039 Abbv->Add(TypeAbbrevOp); // dest ty 4040 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4041 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4042 FUNCTION_INST_CAST_ABBREV) 4043 llvm_unreachable("Unexpected abbrev ordering!"); 4044 } 4045 { // INST_CAST_FLAGS abbrev for FUNCTION_BLOCK. 4046 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4047 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CAST)); 4048 Abbv->Add(ValAbbrevOp); // OpVal 4049 Abbv->Add(TypeAbbrevOp); // dest ty 4050 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // opc 4051 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags 4052 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4053 FUNCTION_INST_CAST_FLAGS_ABBREV) 4054 llvm_unreachable("Unexpected abbrev ordering!"); 4055 } 4056 4057 { // INST_RET abbrev for FUNCTION_BLOCK. 4058 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4059 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 4060 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4061 FUNCTION_INST_RET_VOID_ABBREV) 4062 llvm_unreachable("Unexpected abbrev ordering!"); 4063 } 4064 { // INST_RET abbrev for FUNCTION_BLOCK. 4065 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4066 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_RET)); 4067 Abbv->Add(ValAbbrevOp); 4068 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4069 FUNCTION_INST_RET_VAL_ABBREV) 4070 llvm_unreachable("Unexpected abbrev ordering!"); 4071 } 4072 { 4073 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4074 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR)); 4075 // TODO: Use different abbrev for absolute value reference (succ0)? 4076 Abbv->Add(ValAbbrevOp); // succ0 4077 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4078 FUNCTION_INST_BR_UNCOND_ABBREV) 4079 llvm_unreachable("Unexpected abbrev ordering!"); 4080 } 4081 { 4082 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4083 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_BR)); 4084 // TODO: Use different abbrev for absolute value references (succ0, succ1)? 4085 Abbv->Add(ValAbbrevOp); // succ0 4086 Abbv->Add(ValAbbrevOp); // succ1 4087 Abbv->Add(ValAbbrevOp); // cond 4088 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4089 FUNCTION_INST_BR_COND_ABBREV) 4090 llvm_unreachable("Unexpected abbrev ordering!"); 4091 } 4092 { // INST_UNREACHABLE abbrev for FUNCTION_BLOCK. 4093 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4094 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_UNREACHABLE)); 4095 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4096 FUNCTION_INST_UNREACHABLE_ABBREV) 4097 llvm_unreachable("Unexpected abbrev ordering!"); 4098 } 4099 { 4100 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4101 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_GEP)); 4102 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // flags 4103 Abbv->Add(TypeAbbrevOp); // dest ty 4104 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4105 Abbv->Add(ValAbbrevOp); 4106 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4107 FUNCTION_INST_GEP_ABBREV) 4108 llvm_unreachable("Unexpected abbrev ordering!"); 4109 } 4110 { 4111 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4112 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2)); 4113 Abbv->Add(ValAbbrevOp); // op0 4114 Abbv->Add(ValAbbrevOp); // op1 4115 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred 4116 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4117 FUNCTION_INST_CMP_ABBREV) 4118 llvm_unreachable("Unexpected abbrev ordering!"); 4119 } 4120 { 4121 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4122 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_INST_CMP2)); 4123 Abbv->Add(ValAbbrevOp); // op0 4124 Abbv->Add(ValAbbrevOp); // op1 4125 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // pred 4126 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // flags 4127 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4128 FUNCTION_INST_CMP_FLAGS_ABBREV) 4129 llvm_unreachable("Unexpected abbrev ordering!"); 4130 } 4131 { 4132 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4133 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_RECORD_VALUE_SIMPLE)); 4134 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // dbgloc 4135 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // var 4136 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // expr 4137 Abbv->Add(ValAbbrevOp); // val 4138 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4139 FUNCTION_DEBUG_RECORD_VALUE_ABBREV) 4140 llvm_unreachable("Unexpected abbrev ordering! 1"); 4141 } 4142 { 4143 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4144 Abbv->Add(BitCodeAbbrevOp(bitc::FUNC_CODE_DEBUG_LOC)); 4145 // NOTE: No IsDistinct field for FUNC_CODE_DEBUG_LOC. 4146 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 4147 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4148 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 4149 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 4150 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); 4151 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Atom group. 4152 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3)); // Atom rank. 4153 if (Stream.EmitBlockInfoAbbrev(bitc::FUNCTION_BLOCK_ID, Abbv) != 4154 FUNCTION_DEBUG_LOC_ABBREV) 4155 llvm_unreachable("Unexpected abbrev ordering!"); 4156 } 4157 Stream.ExitBlock(); 4158 } 4159 4160 /// Write the module path strings, currently only used when generating 4161 /// a combined index file. 4162 void IndexBitcodeWriter::writeModStrings() { 4163 Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3); 4164 4165 // TODO: See which abbrev sizes we actually need to emit 4166 4167 // 8-bit fixed-width MST_ENTRY strings. 4168 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4169 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 4170 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4171 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4172 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); 4173 unsigned Abbrev8Bit = Stream.EmitAbbrev(std::move(Abbv)); 4174 4175 // 7-bit fixed width MST_ENTRY strings. 4176 Abbv = std::make_shared<BitCodeAbbrev>(); 4177 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 4178 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4179 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4180 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); 4181 unsigned Abbrev7Bit = Stream.EmitAbbrev(std::move(Abbv)); 4182 4183 // 6-bit char6 MST_ENTRY strings. 4184 Abbv = std::make_shared<BitCodeAbbrev>(); 4185 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY)); 4186 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4187 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4188 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 4189 unsigned Abbrev6Bit = Stream.EmitAbbrev(std::move(Abbv)); 4190 4191 // Module Hash, 160 bits SHA1. Optionally, emitted after each MST_CODE_ENTRY. 4192 Abbv = std::make_shared<BitCodeAbbrev>(); 4193 Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_HASH)); 4194 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4195 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4196 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4197 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4198 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4199 unsigned AbbrevHash = Stream.EmitAbbrev(std::move(Abbv)); 4200 4201 SmallVector<unsigned, 64> Vals; 4202 forEachModule([&](const StringMapEntry<ModuleHash> &MPSE) { 4203 StringRef Key = MPSE.getKey(); 4204 const auto &Hash = MPSE.getValue(); 4205 StringEncoding Bits = getStringEncoding(Key); 4206 unsigned AbbrevToUse = Abbrev8Bit; 4207 if (Bits == SE_Char6) 4208 AbbrevToUse = Abbrev6Bit; 4209 else if (Bits == SE_Fixed7) 4210 AbbrevToUse = Abbrev7Bit; 4211 4212 auto ModuleId = ModuleIdMap.size(); 4213 ModuleIdMap[Key] = ModuleId; 4214 Vals.push_back(ModuleId); 4215 Vals.append(Key.begin(), Key.end()); 4216 4217 // Emit the finished record. 4218 Stream.EmitRecord(bitc::MST_CODE_ENTRY, Vals, AbbrevToUse); 4219 4220 // Emit an optional hash for the module now 4221 if (llvm::any_of(Hash, [](uint32_t H) { return H; })) { 4222 Vals.assign(Hash.begin(), Hash.end()); 4223 // Emit the hash record. 4224 Stream.EmitRecord(bitc::MST_CODE_HASH, Vals, AbbrevHash); 4225 } 4226 4227 Vals.clear(); 4228 }); 4229 Stream.ExitBlock(); 4230 } 4231 4232 /// Write the function type metadata related records that need to appear before 4233 /// a function summary entry (whether per-module or combined). 4234 template <typename Fn> 4235 static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream, 4236 FunctionSummary *FS, 4237 Fn GetValueID) { 4238 if (!FS->type_tests().empty()) 4239 Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests()); 4240 4241 SmallVector<uint64_t, 64> Record; 4242 4243 auto WriteVFuncIdVec = [&](uint64_t Ty, 4244 ArrayRef<FunctionSummary::VFuncId> VFs) { 4245 if (VFs.empty()) 4246 return; 4247 Record.clear(); 4248 for (auto &VF : VFs) { 4249 Record.push_back(VF.GUID); 4250 Record.push_back(VF.Offset); 4251 } 4252 Stream.EmitRecord(Ty, Record); 4253 }; 4254 4255 WriteVFuncIdVec(bitc::FS_TYPE_TEST_ASSUME_VCALLS, 4256 FS->type_test_assume_vcalls()); 4257 WriteVFuncIdVec(bitc::FS_TYPE_CHECKED_LOAD_VCALLS, 4258 FS->type_checked_load_vcalls()); 4259 4260 auto WriteConstVCallVec = [&](uint64_t Ty, 4261 ArrayRef<FunctionSummary::ConstVCall> VCs) { 4262 for (auto &VC : VCs) { 4263 Record.clear(); 4264 Record.push_back(VC.VFunc.GUID); 4265 Record.push_back(VC.VFunc.Offset); 4266 llvm::append_range(Record, VC.Args); 4267 Stream.EmitRecord(Ty, Record); 4268 } 4269 }; 4270 4271 WriteConstVCallVec(bitc::FS_TYPE_TEST_ASSUME_CONST_VCALL, 4272 FS->type_test_assume_const_vcalls()); 4273 WriteConstVCallVec(bitc::FS_TYPE_CHECKED_LOAD_CONST_VCALL, 4274 FS->type_checked_load_const_vcalls()); 4275 4276 auto WriteRange = [&](ConstantRange Range) { 4277 Range = Range.sextOrTrunc(FunctionSummary::ParamAccess::RangeWidth); 4278 assert(Range.getLower().getNumWords() == 1); 4279 assert(Range.getUpper().getNumWords() == 1); 4280 emitSignedInt64(Record, *Range.getLower().getRawData()); 4281 emitSignedInt64(Record, *Range.getUpper().getRawData()); 4282 }; 4283 4284 if (!FS->paramAccesses().empty()) { 4285 Record.clear(); 4286 for (auto &Arg : FS->paramAccesses()) { 4287 size_t UndoSize = Record.size(); 4288 Record.push_back(Arg.ParamNo); 4289 WriteRange(Arg.Use); 4290 Record.push_back(Arg.Calls.size()); 4291 for (auto &Call : Arg.Calls) { 4292 Record.push_back(Call.ParamNo); 4293 std::optional<unsigned> ValueID = GetValueID(Call.Callee); 4294 if (!ValueID) { 4295 // If ValueID is unknown we can't drop just this call, we must drop 4296 // entire parameter. 4297 Record.resize(UndoSize); 4298 break; 4299 } 4300 Record.push_back(*ValueID); 4301 WriteRange(Call.Offsets); 4302 } 4303 } 4304 if (!Record.empty()) 4305 Stream.EmitRecord(bitc::FS_PARAM_ACCESS, Record); 4306 } 4307 } 4308 4309 /// Collect type IDs from type tests used by function. 4310 static void 4311 getReferencedTypeIds(FunctionSummary *FS, 4312 std::set<GlobalValue::GUID> &ReferencedTypeIds) { 4313 if (!FS->type_tests().empty()) 4314 for (auto &TT : FS->type_tests()) 4315 ReferencedTypeIds.insert(TT); 4316 4317 auto GetReferencedTypesFromVFuncIdVec = 4318 [&](ArrayRef<FunctionSummary::VFuncId> VFs) { 4319 for (auto &VF : VFs) 4320 ReferencedTypeIds.insert(VF.GUID); 4321 }; 4322 4323 GetReferencedTypesFromVFuncIdVec(FS->type_test_assume_vcalls()); 4324 GetReferencedTypesFromVFuncIdVec(FS->type_checked_load_vcalls()); 4325 4326 auto GetReferencedTypesFromConstVCallVec = 4327 [&](ArrayRef<FunctionSummary::ConstVCall> VCs) { 4328 for (auto &VC : VCs) 4329 ReferencedTypeIds.insert(VC.VFunc.GUID); 4330 }; 4331 4332 GetReferencedTypesFromConstVCallVec(FS->type_test_assume_const_vcalls()); 4333 GetReferencedTypesFromConstVCallVec(FS->type_checked_load_const_vcalls()); 4334 } 4335 4336 static void writeWholeProgramDevirtResolutionByArg( 4337 SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args, 4338 const WholeProgramDevirtResolution::ByArg &ByArg) { 4339 NameVals.push_back(args.size()); 4340 llvm::append_range(NameVals, args); 4341 4342 NameVals.push_back(ByArg.TheKind); 4343 NameVals.push_back(ByArg.Info); 4344 NameVals.push_back(ByArg.Byte); 4345 NameVals.push_back(ByArg.Bit); 4346 } 4347 4348 static void writeWholeProgramDevirtResolution( 4349 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder, 4350 uint64_t Id, const WholeProgramDevirtResolution &Wpd) { 4351 NameVals.push_back(Id); 4352 4353 NameVals.push_back(Wpd.TheKind); 4354 NameVals.push_back(StrtabBuilder.add(Wpd.SingleImplName)); 4355 NameVals.push_back(Wpd.SingleImplName.size()); 4356 4357 NameVals.push_back(Wpd.ResByArg.size()); 4358 for (auto &A : Wpd.ResByArg) 4359 writeWholeProgramDevirtResolutionByArg(NameVals, A.first, A.second); 4360 } 4361 4362 static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals, 4363 StringTableBuilder &StrtabBuilder, 4364 StringRef Id, 4365 const TypeIdSummary &Summary) { 4366 NameVals.push_back(StrtabBuilder.add(Id)); 4367 NameVals.push_back(Id.size()); 4368 4369 NameVals.push_back(Summary.TTRes.TheKind); 4370 NameVals.push_back(Summary.TTRes.SizeM1BitWidth); 4371 NameVals.push_back(Summary.TTRes.AlignLog2); 4372 NameVals.push_back(Summary.TTRes.SizeM1); 4373 NameVals.push_back(Summary.TTRes.BitMask); 4374 NameVals.push_back(Summary.TTRes.InlineBits); 4375 4376 for (auto &W : Summary.WPDRes) 4377 writeWholeProgramDevirtResolution(NameVals, StrtabBuilder, W.first, 4378 W.second); 4379 } 4380 4381 static void writeTypeIdCompatibleVtableSummaryRecord( 4382 SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder, 4383 StringRef Id, const TypeIdCompatibleVtableInfo &Summary, 4384 ValueEnumerator &VE) { 4385 NameVals.push_back(StrtabBuilder.add(Id)); 4386 NameVals.push_back(Id.size()); 4387 4388 for (auto &P : Summary) { 4389 NameVals.push_back(P.AddressPointOffset); 4390 NameVals.push_back(VE.getValueID(P.VTableVI.getValue())); 4391 } 4392 } 4393 4394 // Adds the allocation contexts to the CallStacks map. We simply use the 4395 // size at the time the context was added as the CallStackId. This works because 4396 // when we look up the call stacks later on we process the function summaries 4397 // and their allocation records in the same exact order. 4398 static void collectMemProfCallStacks( 4399 FunctionSummary *FS, std::function<LinearFrameId(unsigned)> GetStackIndex, 4400 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> &CallStacks) { 4401 // The interfaces in ProfileData/MemProf.h use a type alias for a stack frame 4402 // id offset into the index of the full stack frames. The ModuleSummaryIndex 4403 // currently uses unsigned. Make sure these stay in sync. 4404 static_assert(std::is_same_v<LinearFrameId, unsigned>); 4405 for (auto &AI : FS->allocs()) { 4406 for (auto &MIB : AI.MIBs) { 4407 SmallVector<unsigned> StackIdIndices; 4408 StackIdIndices.reserve(MIB.StackIdIndices.size()); 4409 for (auto Id : MIB.StackIdIndices) 4410 StackIdIndices.push_back(GetStackIndex(Id)); 4411 // The CallStackId is the size at the time this context was inserted. 4412 CallStacks.insert({CallStacks.size(), StackIdIndices}); 4413 } 4414 } 4415 } 4416 4417 // Build the radix tree from the accumulated CallStacks, write out the resulting 4418 // linearized radix tree array, and return the map of call stack positions into 4419 // this array for use when writing the allocation records. The returned map is 4420 // indexed by a CallStackId which in this case is implicitly determined by the 4421 // order of function summaries and their allocation infos being written. 4422 static DenseMap<CallStackId, LinearCallStackId> writeMemoryProfileRadixTree( 4423 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> &&CallStacks, 4424 BitstreamWriter &Stream, unsigned RadixAbbrev) { 4425 assert(!CallStacks.empty()); 4426 DenseMap<unsigned, FrameStat> FrameHistogram = 4427 computeFrameHistogram<LinearFrameId>(CallStacks); 4428 CallStackRadixTreeBuilder<LinearFrameId> Builder; 4429 // We don't need a MemProfFrameIndexes map as we have already converted the 4430 // full stack id hash to a linear offset into the StackIds array. 4431 Builder.build(std::move(CallStacks), /*MemProfFrameIndexes=*/nullptr, 4432 FrameHistogram); 4433 Stream.EmitRecord(bitc::FS_CONTEXT_RADIX_TREE_ARRAY, Builder.getRadixArray(), 4434 RadixAbbrev); 4435 return Builder.takeCallStackPos(); 4436 } 4437 4438 static void writeFunctionHeapProfileRecords( 4439 BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev, 4440 unsigned AllocAbbrev, unsigned ContextIdAbbvId, bool PerModule, 4441 std::function<unsigned(const ValueInfo &VI)> GetValueID, 4442 std::function<unsigned(unsigned)> GetStackIndex, 4443 bool WriteContextSizeInfoIndex, 4444 DenseMap<CallStackId, LinearCallStackId> &CallStackPos, 4445 CallStackId &CallStackCount) { 4446 SmallVector<uint64_t> Record; 4447 4448 for (auto &CI : FS->callsites()) { 4449 Record.clear(); 4450 // Per module callsite clones should always have a single entry of 4451 // value 0. 4452 assert(!PerModule || (CI.Clones.size() == 1 && CI.Clones[0] == 0)); 4453 Record.push_back(GetValueID(CI.Callee)); 4454 if (!PerModule) { 4455 Record.push_back(CI.StackIdIndices.size()); 4456 Record.push_back(CI.Clones.size()); 4457 } 4458 for (auto Id : CI.StackIdIndices) 4459 Record.push_back(GetStackIndex(Id)); 4460 if (!PerModule) 4461 llvm::append_range(Record, CI.Clones); 4462 Stream.EmitRecord(PerModule ? bitc::FS_PERMODULE_CALLSITE_INFO 4463 : bitc::FS_COMBINED_CALLSITE_INFO, 4464 Record, CallsiteAbbrev); 4465 } 4466 4467 for (auto &AI : FS->allocs()) { 4468 Record.clear(); 4469 // Per module alloc versions should always have a single entry of 4470 // value 0. 4471 assert(!PerModule || (AI.Versions.size() == 1 && AI.Versions[0] == 0)); 4472 Record.push_back(AI.MIBs.size()); 4473 if (!PerModule) 4474 Record.push_back(AI.Versions.size()); 4475 for (auto &MIB : AI.MIBs) { 4476 Record.push_back((uint8_t)MIB.AllocType); 4477 // The per-module summary always needs to include the alloc context, as we 4478 // use it during the thin link. For the combined index it is optional (see 4479 // comments where CombinedIndexMemProfContext is defined). 4480 if (PerModule || CombinedIndexMemProfContext) { 4481 // Record the index into the radix tree array for this context. 4482 assert(CallStackCount <= CallStackPos.size()); 4483 Record.push_back(CallStackPos[CallStackCount++]); 4484 } 4485 } 4486 if (!PerModule) 4487 llvm::append_range(Record, AI.Versions); 4488 assert(AI.ContextSizeInfos.empty() || 4489 AI.ContextSizeInfos.size() == AI.MIBs.size()); 4490 // Optionally emit the context size information if it exists. 4491 if (WriteContextSizeInfoIndex && !AI.ContextSizeInfos.empty()) { 4492 // The abbreviation id for the context ids record should have been created 4493 // if we are emitting the per-module index, which is where we write this 4494 // info. 4495 assert(ContextIdAbbvId); 4496 SmallVector<uint32_t> ContextIds; 4497 // At least one context id per ContextSizeInfos entry (MIB), broken into 2 4498 // halves. 4499 ContextIds.reserve(AI.ContextSizeInfos.size() * 2); 4500 for (auto &Infos : AI.ContextSizeInfos) { 4501 Record.push_back(Infos.size()); 4502 for (auto [FullStackId, TotalSize] : Infos) { 4503 // The context ids are emitted separately as a fixed width array, 4504 // which is more efficient than a VBR given that these hashes are 4505 // typically close to 64-bits. The max fixed width entry is 32 bits so 4506 // it is split into 2. 4507 ContextIds.push_back(static_cast<uint32_t>(FullStackId >> 32)); 4508 ContextIds.push_back(static_cast<uint32_t>(FullStackId)); 4509 Record.push_back(TotalSize); 4510 } 4511 } 4512 // The context ids are expected by the reader to immediately precede the 4513 // associated alloc info record. 4514 Stream.EmitRecord(bitc::FS_ALLOC_CONTEXT_IDS, ContextIds, 4515 ContextIdAbbvId); 4516 } 4517 Stream.EmitRecord(PerModule 4518 ? bitc::FS_PERMODULE_ALLOC_INFO 4519 : (CombinedIndexMemProfContext 4520 ? bitc::FS_COMBINED_ALLOC_INFO 4521 : bitc::FS_COMBINED_ALLOC_INFO_NO_CONTEXT), 4522 Record, AllocAbbrev); 4523 } 4524 } 4525 4526 // Helper to emit a single function summary record. 4527 void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord( 4528 SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary, 4529 unsigned ValueID, unsigned FSCallsRelBFAbbrev, 4530 unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev, 4531 unsigned AllocAbbrev, unsigned ContextIdAbbvId, const Function &F, 4532 DenseMap<CallStackId, LinearCallStackId> &CallStackPos, 4533 CallStackId &CallStackCount) { 4534 NameVals.push_back(ValueID); 4535 4536 FunctionSummary *FS = cast<FunctionSummary>(Summary); 4537 4538 writeFunctionTypeMetadataRecords( 4539 Stream, FS, [&](const ValueInfo &VI) -> std::optional<unsigned> { 4540 return {VE.getValueID(VI.getValue())}; 4541 }); 4542 4543 writeFunctionHeapProfileRecords( 4544 Stream, FS, CallsiteAbbrev, AllocAbbrev, ContextIdAbbvId, 4545 /*PerModule*/ true, 4546 /*GetValueId*/ [&](const ValueInfo &VI) { return getValueId(VI); }, 4547 /*GetStackIndex*/ [&](unsigned I) { return I; }, 4548 /*WriteContextSizeInfoIndex*/ true, CallStackPos, CallStackCount); 4549 4550 auto SpecialRefCnts = FS->specialRefCounts(); 4551 NameVals.push_back(getEncodedGVSummaryFlags(FS->flags())); 4552 NameVals.push_back(FS->instCount()); 4553 NameVals.push_back(getEncodedFFlags(FS->fflags())); 4554 NameVals.push_back(FS->refs().size()); 4555 NameVals.push_back(SpecialRefCnts.first); // rorefcnt 4556 NameVals.push_back(SpecialRefCnts.second); // worefcnt 4557 4558 for (auto &RI : FS->refs()) 4559 NameVals.push_back(getValueId(RI)); 4560 4561 const bool UseRelBFRecord = 4562 WriteRelBFToSummary && !F.hasProfileData() && 4563 ForceSummaryEdgesCold == FunctionSummary::FSHT_None; 4564 for (auto &ECI : FS->calls()) { 4565 NameVals.push_back(getValueId(ECI.first)); 4566 if (UseRelBFRecord) 4567 NameVals.push_back(getEncodedRelBFCallEdgeInfo(ECI.second)); 4568 else 4569 NameVals.push_back(getEncodedHotnessCallEdgeInfo(ECI.second)); 4570 } 4571 4572 unsigned FSAbbrev = 4573 (UseRelBFRecord ? FSCallsRelBFAbbrev : FSCallsProfileAbbrev); 4574 unsigned Code = 4575 (UseRelBFRecord ? bitc::FS_PERMODULE_RELBF : bitc::FS_PERMODULE_PROFILE); 4576 4577 // Emit the finished record. 4578 Stream.EmitRecord(Code, NameVals, FSAbbrev); 4579 NameVals.clear(); 4580 } 4581 4582 // Collect the global value references in the given variable's initializer, 4583 // and emit them in a summary record. 4584 void ModuleBitcodeWriterBase::writeModuleLevelReferences( 4585 const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals, 4586 unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) { 4587 auto VI = Index->getValueInfo(V.getGUID()); 4588 if (!VI || VI.getSummaryList().empty()) { 4589 // Only declarations should not have a summary (a declaration might however 4590 // have a summary if the def was in module level asm). 4591 assert(V.isDeclaration()); 4592 return; 4593 } 4594 auto *Summary = VI.getSummaryList()[0].get(); 4595 NameVals.push_back(VE.getValueID(&V)); 4596 GlobalVarSummary *VS = cast<GlobalVarSummary>(Summary); 4597 NameVals.push_back(getEncodedGVSummaryFlags(VS->flags())); 4598 NameVals.push_back(getEncodedGVarFlags(VS->varflags())); 4599 4600 auto VTableFuncs = VS->vTableFuncs(); 4601 if (!VTableFuncs.empty()) 4602 NameVals.push_back(VS->refs().size()); 4603 4604 unsigned SizeBeforeRefs = NameVals.size(); 4605 for (auto &RI : VS->refs()) 4606 NameVals.push_back(VE.getValueID(RI.getValue())); 4607 // Sort the refs for determinism output, the vector returned by FS->refs() has 4608 // been initialized from a DenseSet. 4609 llvm::sort(drop_begin(NameVals, SizeBeforeRefs)); 4610 4611 if (VTableFuncs.empty()) 4612 Stream.EmitRecord(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS, NameVals, 4613 FSModRefsAbbrev); 4614 else { 4615 // VTableFuncs pairs should already be sorted by offset. 4616 for (auto &P : VTableFuncs) { 4617 NameVals.push_back(VE.getValueID(P.FuncVI.getValue())); 4618 NameVals.push_back(P.VTableOffset); 4619 } 4620 4621 Stream.EmitRecord(bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS, NameVals, 4622 FSModVTableRefsAbbrev); 4623 } 4624 NameVals.clear(); 4625 } 4626 4627 /// Emit the per-module summary section alongside the rest of 4628 /// the module's bitcode. 4629 void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { 4630 // By default we compile with ThinLTO if the module has a summary, but the 4631 // client can request full LTO with a module flag. 4632 bool IsThinLTO = true; 4633 if (auto *MD = 4634 mdconst::extract_or_null<ConstantInt>(M.getModuleFlag("ThinLTO"))) 4635 IsThinLTO = MD->getZExtValue(); 4636 Stream.EnterSubblock(IsThinLTO ? bitc::GLOBALVAL_SUMMARY_BLOCK_ID 4637 : bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID, 4638 4); 4639 4640 Stream.EmitRecord( 4641 bitc::FS_VERSION, 4642 ArrayRef<uint64_t>{ModuleSummaryIndex::BitcodeSummaryVersion}); 4643 4644 // Write the index flags. 4645 uint64_t Flags = 0; 4646 // Bits 1-3 are set only in the combined index, skip them. 4647 if (Index->enableSplitLTOUnit()) 4648 Flags |= 0x8; 4649 if (Index->hasUnifiedLTO()) 4650 Flags |= 0x200; 4651 4652 Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Flags}); 4653 4654 if (Index->begin() == Index->end()) { 4655 Stream.ExitBlock(); 4656 return; 4657 } 4658 4659 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4660 Abbv->Add(BitCodeAbbrevOp(bitc::FS_VALUE_GUID)); 4661 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 4662 // GUIDS often use up most of 64-bits, so encode as two Fixed 32. 4663 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4664 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4665 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4666 4667 for (const auto &GVI : valueIds()) { 4668 Stream.EmitRecord(bitc::FS_VALUE_GUID, 4669 ArrayRef<uint32_t>{GVI.second, 4670 static_cast<uint32_t>(GVI.first >> 32), 4671 static_cast<uint32_t>(GVI.first)}, 4672 ValueGuidAbbrev); 4673 } 4674 4675 if (!Index->stackIds().empty()) { 4676 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>(); 4677 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS)); 4678 // numids x stackid 4679 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4680 // The stack ids are hashes that are close to 64 bits in size, so emitting 4681 // as a pair of 32-bit fixed-width values is more efficient than a VBR. 4682 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4683 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv)); 4684 SmallVector<uint32_t> Vals; 4685 Vals.reserve(Index->stackIds().size() * 2); 4686 for (auto Id : Index->stackIds()) { 4687 Vals.push_back(static_cast<uint32_t>(Id >> 32)); 4688 Vals.push_back(static_cast<uint32_t>(Id)); 4689 } 4690 Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId); 4691 } 4692 4693 unsigned ContextIdAbbvId = 0; 4694 if (metadataMayIncludeContextSizeInfo()) { 4695 // n x context id 4696 auto ContextIdAbbv = std::make_shared<BitCodeAbbrev>(); 4697 ContextIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_ALLOC_CONTEXT_IDS)); 4698 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4699 // The context ids are hashes that are close to 64 bits in size, so emitting 4700 // as a pair of 32-bit fixed-width values is more efficient than a VBR if we 4701 // are emitting them for all MIBs. Otherwise we use VBR to better compress 0 4702 // values that are expected to more frequently occur in an alloc's memprof 4703 // summary. 4704 if (metadataIncludesAllContextSizeInfo()) 4705 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4706 else 4707 ContextIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4708 ContextIdAbbvId = Stream.EmitAbbrev(std::move(ContextIdAbbv)); 4709 } 4710 4711 // Abbrev for FS_PERMODULE_PROFILE. 4712 Abbv = std::make_shared<BitCodeAbbrev>(); 4713 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_PROFILE)); 4714 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4715 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // flags 4716 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 4717 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags 4718 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 4719 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt 4720 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt 4721 // numrefs x valueid, n x (valueid, hotness+tailcall flags) 4722 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4723 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4724 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4725 4726 // Abbrev for FS_PERMODULE_RELBF. 4727 Abbv = std::make_shared<BitCodeAbbrev>(); 4728 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_RELBF)); 4729 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4730 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4731 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 4732 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags 4733 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 4734 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt 4735 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt 4736 // numrefs x valueid, n x (valueid, rel_block_freq+tailcall]) 4737 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4738 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4739 unsigned FSCallsRelBFAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4740 4741 // Abbrev for FS_PERMODULE_GLOBALVAR_INIT_REFS. 4742 Abbv = std::make_shared<BitCodeAbbrev>(); 4743 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_GLOBALVAR_INIT_REFS)); 4744 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4745 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4746 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids 4747 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4748 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4749 4750 // Abbrev for FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS. 4751 Abbv = std::make_shared<BitCodeAbbrev>(); 4752 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_VTABLE_GLOBALVAR_INIT_REFS)); 4753 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4754 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4755 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 4756 // numrefs x valueid, n x (valueid , offset) 4757 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4758 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4759 unsigned FSModVTableRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4760 4761 // Abbrev for FS_ALIAS. 4762 Abbv = std::make_shared<BitCodeAbbrev>(); 4763 Abbv->Add(BitCodeAbbrevOp(bitc::FS_ALIAS)); 4764 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4765 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4766 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4767 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4768 4769 // Abbrev for FS_TYPE_ID_METADATA 4770 Abbv = std::make_shared<BitCodeAbbrev>(); 4771 Abbv->Add(BitCodeAbbrevOp(bitc::FS_TYPE_ID_METADATA)); 4772 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid strtab index 4773 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // typeid length 4774 // n x (valueid , offset) 4775 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4776 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4777 unsigned TypeIdCompatibleVtableAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4778 4779 Abbv = std::make_shared<BitCodeAbbrev>(); 4780 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_CALLSITE_INFO)); 4781 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4782 // n x stackidindex 4783 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4784 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4785 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4786 4787 Abbv = std::make_shared<BitCodeAbbrev>(); 4788 Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE_ALLOC_INFO)); 4789 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib 4790 // n x (alloc type, context radix tree index) 4791 // optional: nummib x (numcontext x total size) 4792 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4793 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4794 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4795 4796 Abbv = std::make_shared<BitCodeAbbrev>(); 4797 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CONTEXT_RADIX_TREE_ARRAY)); 4798 // n x entry 4799 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4800 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4801 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4802 4803 // First walk through all the functions and collect the allocation contexts in 4804 // their associated summaries, for use in constructing a radix tree of 4805 // contexts. Note that we need to do this in the same order as the functions 4806 // are processed further below since the call stack positions in the resulting 4807 // radix tree array are identified based on this order. 4808 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> CallStacks; 4809 for (const Function &F : M) { 4810 // Summary emission does not support anonymous functions, they have to be 4811 // renamed using the anonymous function renaming pass. 4812 if (!F.hasName()) 4813 report_fatal_error("Unexpected anonymous function when writing summary"); 4814 4815 ValueInfo VI = Index->getValueInfo(F.getGUID()); 4816 if (!VI || VI.getSummaryList().empty()) { 4817 // Only declarations should not have a summary (a declaration might 4818 // however have a summary if the def was in module level asm). 4819 assert(F.isDeclaration()); 4820 continue; 4821 } 4822 auto *Summary = VI.getSummaryList()[0].get(); 4823 FunctionSummary *FS = cast<FunctionSummary>(Summary); 4824 collectMemProfCallStacks( 4825 FS, /*GetStackIndex*/ [](unsigned I) { return I; }, CallStacks); 4826 } 4827 // Finalize the radix tree, write it out, and get the map of positions in the 4828 // linearized tree array. 4829 DenseMap<CallStackId, LinearCallStackId> CallStackPos; 4830 if (!CallStacks.empty()) { 4831 CallStackPos = 4832 writeMemoryProfileRadixTree(std::move(CallStacks), Stream, RadixAbbrev); 4833 } 4834 4835 // Keep track of the current index into the CallStackPos map. 4836 CallStackId CallStackCount = 0; 4837 4838 SmallVector<uint64_t, 64> NameVals; 4839 // Iterate over the list of functions instead of the Index to 4840 // ensure the ordering is stable. 4841 for (const Function &F : M) { 4842 // Summary emission does not support anonymous functions, they have to 4843 // renamed using the anonymous function renaming pass. 4844 if (!F.hasName()) 4845 report_fatal_error("Unexpected anonymous function when writing summary"); 4846 4847 ValueInfo VI = Index->getValueInfo(F.getGUID()); 4848 if (!VI || VI.getSummaryList().empty()) { 4849 // Only declarations should not have a summary (a declaration might 4850 // however have a summary if the def was in module level asm). 4851 assert(F.isDeclaration()); 4852 continue; 4853 } 4854 auto *Summary = VI.getSummaryList()[0].get(); 4855 writePerModuleFunctionSummaryRecord( 4856 NameVals, Summary, VE.getValueID(&F), FSCallsRelBFAbbrev, 4857 FSCallsProfileAbbrev, CallsiteAbbrev, AllocAbbrev, ContextIdAbbvId, F, 4858 CallStackPos, CallStackCount); 4859 } 4860 4861 // Capture references from GlobalVariable initializers, which are outside 4862 // of a function scope. 4863 for (const GlobalVariable &G : M.globals()) 4864 writeModuleLevelReferences(G, NameVals, FSModRefsAbbrev, 4865 FSModVTableRefsAbbrev); 4866 4867 for (const GlobalAlias &A : M.aliases()) { 4868 auto *Aliasee = A.getAliaseeObject(); 4869 // Skip ifunc and nameless functions which don't have an entry in the 4870 // summary. 4871 if (!Aliasee->hasName() || isa<GlobalIFunc>(Aliasee)) 4872 continue; 4873 auto AliasId = VE.getValueID(&A); 4874 auto AliaseeId = VE.getValueID(Aliasee); 4875 NameVals.push_back(AliasId); 4876 auto *Summary = Index->getGlobalValueSummary(A); 4877 AliasSummary *AS = cast<AliasSummary>(Summary); 4878 NameVals.push_back(getEncodedGVSummaryFlags(AS->flags())); 4879 NameVals.push_back(AliaseeId); 4880 Stream.EmitRecord(bitc::FS_ALIAS, NameVals, FSAliasAbbrev); 4881 NameVals.clear(); 4882 } 4883 4884 for (auto &S : Index->typeIdCompatibleVtableMap()) { 4885 writeTypeIdCompatibleVtableSummaryRecord(NameVals, StrtabBuilder, S.first, 4886 S.second, VE); 4887 Stream.EmitRecord(bitc::FS_TYPE_ID_METADATA, NameVals, 4888 TypeIdCompatibleVtableAbbrev); 4889 NameVals.clear(); 4890 } 4891 4892 if (Index->getBlockCount()) 4893 Stream.EmitRecord(bitc::FS_BLOCK_COUNT, 4894 ArrayRef<uint64_t>{Index->getBlockCount()}); 4895 4896 Stream.ExitBlock(); 4897 } 4898 4899 /// Emit the combined summary section into the combined index file. 4900 void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { 4901 Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 4); 4902 Stream.EmitRecord( 4903 bitc::FS_VERSION, 4904 ArrayRef<uint64_t>{ModuleSummaryIndex::BitcodeSummaryVersion}); 4905 4906 // Write the index flags. 4907 Stream.EmitRecord(bitc::FS_FLAGS, ArrayRef<uint64_t>{Index.getFlags()}); 4908 4909 auto Abbv = std::make_shared<BitCodeAbbrev>(); 4910 Abbv->Add(BitCodeAbbrevOp(bitc::FS_VALUE_GUID)); 4911 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 4912 // GUIDS often use up most of 64-bits, so encode as two Fixed 32. 4913 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4914 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4915 unsigned ValueGuidAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4916 4917 for (const auto &GVI : valueIds()) { 4918 Stream.EmitRecord(bitc::FS_VALUE_GUID, 4919 ArrayRef<uint32_t>{GVI.second, 4920 static_cast<uint32_t>(GVI.first >> 32), 4921 static_cast<uint32_t>(GVI.first)}, 4922 ValueGuidAbbrev); 4923 } 4924 4925 // Write the stack ids used by this index, which will be a subset of those in 4926 // the full index in the case of distributed indexes. 4927 if (!StackIds.empty()) { 4928 auto StackIdAbbv = std::make_shared<BitCodeAbbrev>(); 4929 StackIdAbbv->Add(BitCodeAbbrevOp(bitc::FS_STACK_IDS)); 4930 // numids x stackid 4931 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4932 // The stack ids are hashes that are close to 64 bits in size, so emitting 4933 // as a pair of 32-bit fixed-width values is more efficient than a VBR. 4934 StackIdAbbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 4935 unsigned StackIdAbbvId = Stream.EmitAbbrev(std::move(StackIdAbbv)); 4936 SmallVector<uint32_t> Vals; 4937 Vals.reserve(StackIds.size() * 2); 4938 for (auto Id : StackIds) { 4939 Vals.push_back(static_cast<uint32_t>(Id >> 32)); 4940 Vals.push_back(static_cast<uint32_t>(Id)); 4941 } 4942 Stream.EmitRecord(bitc::FS_STACK_IDS, Vals, StackIdAbbvId); 4943 } 4944 4945 // Abbrev for FS_COMBINED_PROFILE. 4946 Abbv = std::make_shared<BitCodeAbbrev>(); 4947 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_PROFILE)); 4948 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4949 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 4950 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4951 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount 4952 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // fflags 4953 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // entrycount 4954 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numrefs 4955 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // rorefcnt 4956 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // worefcnt 4957 // numrefs x valueid, n x (valueid, hotness+tailcall flags) 4958 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4959 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4960 unsigned FSCallsProfileAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4961 4962 // Abbrev for FS_COMBINED_GLOBALVAR_INIT_REFS. 4963 Abbv = std::make_shared<BitCodeAbbrev>(); 4964 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS)); 4965 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4966 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 4967 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4968 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); // valueids 4969 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4970 unsigned FSModRefsAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4971 4972 // Abbrev for FS_COMBINED_ALIAS. 4973 Abbv = std::make_shared<BitCodeAbbrev>(); 4974 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_ALIAS)); 4975 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4976 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid 4977 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // flags 4978 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4979 unsigned FSAliasAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4980 4981 Abbv = std::make_shared<BitCodeAbbrev>(); 4982 Abbv->Add(BitCodeAbbrevOp(bitc::FS_COMBINED_CALLSITE_INFO)); 4983 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid 4984 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numstackindices 4985 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver 4986 // numstackindices x stackidindex, numver x version 4987 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 4988 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 4989 unsigned CallsiteAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 4990 4991 Abbv = std::make_shared<BitCodeAbbrev>(); 4992 Abbv->Add(BitCodeAbbrevOp(CombinedIndexMemProfContext 4993 ? bitc::FS_COMBINED_ALLOC_INFO 4994 : bitc::FS_COMBINED_ALLOC_INFO_NO_CONTEXT)); 4995 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // nummib 4996 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 4)); // numver 4997 // nummib x (alloc type, context radix tree index), 4998 // numver x version 4999 // optional: nummib x total size 5000 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 5001 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 5002 unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 5003 5004 auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool { 5005 if (DecSummaries == nullptr) 5006 return false; 5007 return DecSummaries->count(GVS); 5008 }; 5009 5010 // The aliases are emitted as a post-pass, and will point to the value 5011 // id of the aliasee. Save them in a vector for post-processing. 5012 SmallVector<AliasSummary *, 64> Aliases; 5013 5014 // Save the value id for each summary for alias emission. 5015 DenseMap<const GlobalValueSummary *, unsigned> SummaryToValueIdMap; 5016 5017 SmallVector<uint64_t, 64> NameVals; 5018 5019 // Set that will be populated during call to writeFunctionTypeMetadataRecords 5020 // with the type ids referenced by this index file. 5021 std::set<GlobalValue::GUID> ReferencedTypeIds; 5022 5023 // For local linkage, we also emit the original name separately 5024 // immediately after the record. 5025 auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) { 5026 // We don't need to emit the original name if we are writing the index for 5027 // distributed backends (in which case ModuleToSummariesForIndex is 5028 // non-null). The original name is only needed during the thin link, since 5029 // for SamplePGO the indirect call targets for local functions have 5030 // have the original name annotated in profile. 5031 // Continue to emit it when writing out the entire combined index, which is 5032 // used in testing the thin link via llvm-lto. 5033 if (ModuleToSummariesForIndex || !GlobalValue::isLocalLinkage(S.linkage())) 5034 return; 5035 NameVals.push_back(S.getOriginalName()); 5036 Stream.EmitRecord(bitc::FS_COMBINED_ORIGINAL_NAME, NameVals); 5037 NameVals.clear(); 5038 }; 5039 5040 DenseMap<CallStackId, LinearCallStackId> CallStackPos; 5041 if (CombinedIndexMemProfContext) { 5042 Abbv = std::make_shared<BitCodeAbbrev>(); 5043 Abbv->Add(BitCodeAbbrevOp(bitc::FS_CONTEXT_RADIX_TREE_ARRAY)); 5044 // n x entry 5045 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 5046 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); 5047 unsigned RadixAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 5048 5049 // First walk through all the functions and collect the allocation contexts 5050 // in their associated summaries, for use in constructing a radix tree of 5051 // contexts. Note that we need to do this in the same order as the functions 5052 // are processed further below since the call stack positions in the 5053 // resulting radix tree array are identified based on this order. 5054 MapVector<CallStackId, llvm::SmallVector<LinearFrameId>> CallStacks; 5055 forEachSummary([&](GVInfo I, bool IsAliasee) { 5056 // Don't collect this when invoked for an aliasee, as it is not needed for 5057 // the alias summary. If the aliasee is to be imported, we will invoke 5058 // this separately with IsAliasee=false. 5059 if (IsAliasee) 5060 return; 5061 GlobalValueSummary *S = I.second; 5062 assert(S); 5063 auto *FS = dyn_cast<FunctionSummary>(S); 5064 if (!FS) 5065 return; 5066 collectMemProfCallStacks( 5067 FS, 5068 /*GetStackIndex*/ 5069 [&](unsigned I) { 5070 // Get the corresponding index into the list of StackIds actually 5071 // being written for this combined index (which may be a subset in 5072 // the case of distributed indexes). 5073 assert(StackIdIndicesToIndex.contains(I)); 5074 return StackIdIndicesToIndex[I]; 5075 }, 5076 CallStacks); 5077 }); 5078 // Finalize the radix tree, write it out, and get the map of positions in 5079 // the linearized tree array. 5080 if (!CallStacks.empty()) { 5081 CallStackPos = writeMemoryProfileRadixTree(std::move(CallStacks), Stream, 5082 RadixAbbrev); 5083 } 5084 } 5085 5086 // Keep track of the current index into the CallStackPos map. Not used if 5087 // CombinedIndexMemProfContext is false. 5088 CallStackId CallStackCount = 0; 5089 5090 DenseSet<GlobalValue::GUID> DefOrUseGUIDs; 5091 forEachSummary([&](GVInfo I, bool IsAliasee) { 5092 GlobalValueSummary *S = I.second; 5093 assert(S); 5094 DefOrUseGUIDs.insert(I.first); 5095 for (const ValueInfo &VI : S->refs()) 5096 DefOrUseGUIDs.insert(VI.getGUID()); 5097 5098 auto ValueId = getValueId(I.first); 5099 assert(ValueId); 5100 SummaryToValueIdMap[S] = *ValueId; 5101 5102 // If this is invoked for an aliasee, we want to record the above 5103 // mapping, but then not emit a summary entry (if the aliasee is 5104 // to be imported, we will invoke this separately with IsAliasee=false). 5105 if (IsAliasee) 5106 return; 5107 5108 if (auto *AS = dyn_cast<AliasSummary>(S)) { 5109 // Will process aliases as a post-pass because the reader wants all 5110 // global to be loaded first. 5111 Aliases.push_back(AS); 5112 return; 5113 } 5114 5115 if (auto *VS = dyn_cast<GlobalVarSummary>(S)) { 5116 NameVals.push_back(*ValueId); 5117 assert(ModuleIdMap.count(VS->modulePath())); 5118 NameVals.push_back(ModuleIdMap[VS->modulePath()]); 5119 NameVals.push_back( 5120 getEncodedGVSummaryFlags(VS->flags(), shouldImportValueAsDecl(VS))); 5121 NameVals.push_back(getEncodedGVarFlags(VS->varflags())); 5122 for (auto &RI : VS->refs()) { 5123 auto RefValueId = getValueId(RI.getGUID()); 5124 if (!RefValueId) 5125 continue; 5126 NameVals.push_back(*RefValueId); 5127 } 5128 5129 // Emit the finished record. 5130 Stream.EmitRecord(bitc::FS_COMBINED_GLOBALVAR_INIT_REFS, NameVals, 5131 FSModRefsAbbrev); 5132 NameVals.clear(); 5133 MaybeEmitOriginalName(*S); 5134 return; 5135 } 5136 5137 auto GetValueId = [&](const ValueInfo &VI) -> std::optional<unsigned> { 5138 if (!VI) 5139 return std::nullopt; 5140 return getValueId(VI.getGUID()); 5141 }; 5142 5143 auto *FS = cast<FunctionSummary>(S); 5144 writeFunctionTypeMetadataRecords(Stream, FS, GetValueId); 5145 getReferencedTypeIds(FS, ReferencedTypeIds); 5146 5147 writeFunctionHeapProfileRecords( 5148 Stream, FS, CallsiteAbbrev, AllocAbbrev, /*ContextIdAbbvId*/ 0, 5149 /*PerModule*/ false, 5150 /*GetValueId*/ 5151 [&](const ValueInfo &VI) -> unsigned { 5152 std::optional<unsigned> ValueID = GetValueId(VI); 5153 // This can happen in shared index files for distributed ThinLTO if 5154 // the callee function summary is not included. Record 0 which we 5155 // will have to deal with conservatively when doing any kind of 5156 // validation in the ThinLTO backends. 5157 if (!ValueID) 5158 return 0; 5159 return *ValueID; 5160 }, 5161 /*GetStackIndex*/ 5162 [&](unsigned I) { 5163 // Get the corresponding index into the list of StackIds actually 5164 // being written for this combined index (which may be a subset in 5165 // the case of distributed indexes). 5166 assert(StackIdIndicesToIndex.contains(I)); 5167 return StackIdIndicesToIndex[I]; 5168 }, 5169 /*WriteContextSizeInfoIndex*/ false, CallStackPos, CallStackCount); 5170 5171 NameVals.push_back(*ValueId); 5172 assert(ModuleIdMap.count(FS->modulePath())); 5173 NameVals.push_back(ModuleIdMap[FS->modulePath()]); 5174 NameVals.push_back( 5175 getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS))); 5176 NameVals.push_back(FS->instCount()); 5177 NameVals.push_back(getEncodedFFlags(FS->fflags())); 5178 // TODO: Stop writing entry count and bump bitcode version. 5179 NameVals.push_back(0 /* EntryCount */); 5180 5181 // Fill in below 5182 NameVals.push_back(0); // numrefs 5183 NameVals.push_back(0); // rorefcnt 5184 NameVals.push_back(0); // worefcnt 5185 5186 unsigned Count = 0, RORefCnt = 0, WORefCnt = 0; 5187 for (auto &RI : FS->refs()) { 5188 auto RefValueId = getValueId(RI.getGUID()); 5189 if (!RefValueId) 5190 continue; 5191 NameVals.push_back(*RefValueId); 5192 if (RI.isReadOnly()) 5193 RORefCnt++; 5194 else if (RI.isWriteOnly()) 5195 WORefCnt++; 5196 Count++; 5197 } 5198 NameVals[6] = Count; 5199 NameVals[7] = RORefCnt; 5200 NameVals[8] = WORefCnt; 5201 5202 for (auto &EI : FS->calls()) { 5203 // If this GUID doesn't have a value id, it doesn't have a function 5204 // summary and we don't need to record any calls to it. 5205 std::optional<unsigned> CallValueId = GetValueId(EI.first); 5206 if (!CallValueId) 5207 continue; 5208 NameVals.push_back(*CallValueId); 5209 NameVals.push_back(getEncodedHotnessCallEdgeInfo(EI.second)); 5210 } 5211 5212 // Emit the finished record. 5213 Stream.EmitRecord(bitc::FS_COMBINED_PROFILE, NameVals, 5214 FSCallsProfileAbbrev); 5215 NameVals.clear(); 5216 MaybeEmitOriginalName(*S); 5217 }); 5218 5219 for (auto *AS : Aliases) { 5220 auto AliasValueId = SummaryToValueIdMap[AS]; 5221 assert(AliasValueId); 5222 NameVals.push_back(AliasValueId); 5223 assert(ModuleIdMap.count(AS->modulePath())); 5224 NameVals.push_back(ModuleIdMap[AS->modulePath()]); 5225 NameVals.push_back( 5226 getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS))); 5227 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()]; 5228 assert(AliaseeValueId); 5229 NameVals.push_back(AliaseeValueId); 5230 5231 // Emit the finished record. 5232 Stream.EmitRecord(bitc::FS_COMBINED_ALIAS, NameVals, FSAliasAbbrev); 5233 NameVals.clear(); 5234 MaybeEmitOriginalName(*AS); 5235 5236 if (auto *FS = dyn_cast<FunctionSummary>(&AS->getAliasee())) 5237 getReferencedTypeIds(FS, ReferencedTypeIds); 5238 } 5239 5240 SmallVector<StringRef, 4> Functions; 5241 auto EmitCfiFunctions = [&](const CfiFunctionIndex &CfiIndex, 5242 bitc::GlobalValueSummarySymtabCodes Code) { 5243 if (CfiIndex.empty()) 5244 return; 5245 for (GlobalValue::GUID GUID : DefOrUseGUIDs) { 5246 auto Defs = CfiIndex.forGuid(GUID); 5247 llvm::append_range(Functions, Defs); 5248 } 5249 if (Functions.empty()) 5250 return; 5251 llvm::sort(Functions); 5252 for (const auto &S : Functions) { 5253 NameVals.push_back(StrtabBuilder.add(S)); 5254 NameVals.push_back(S.size()); 5255 } 5256 Stream.EmitRecord(Code, NameVals); 5257 NameVals.clear(); 5258 Functions.clear(); 5259 }; 5260 5261 EmitCfiFunctions(Index.cfiFunctionDefs(), bitc::FS_CFI_FUNCTION_DEFS); 5262 EmitCfiFunctions(Index.cfiFunctionDecls(), bitc::FS_CFI_FUNCTION_DECLS); 5263 5264 // Walk the GUIDs that were referenced, and write the 5265 // corresponding type id records. 5266 for (auto &T : ReferencedTypeIds) { 5267 auto TidIter = Index.typeIds().equal_range(T); 5268 for (const auto &[GUID, TypeIdPair] : make_range(TidIter)) { 5269 writeTypeIdSummaryRecord(NameVals, StrtabBuilder, TypeIdPair.first, 5270 TypeIdPair.second); 5271 Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals); 5272 NameVals.clear(); 5273 } 5274 } 5275 5276 if (Index.getBlockCount()) 5277 Stream.EmitRecord(bitc::FS_BLOCK_COUNT, 5278 ArrayRef<uint64_t>{Index.getBlockCount()}); 5279 5280 Stream.ExitBlock(); 5281 } 5282 5283 /// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the 5284 /// current llvm version, and a record for the epoch number. 5285 static void writeIdentificationBlock(BitstreamWriter &Stream) { 5286 Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5); 5287 5288 // Write the "user readable" string identifying the bitcode producer 5289 auto Abbv = std::make_shared<BitCodeAbbrev>(); 5290 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING)); 5291 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 5292 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6)); 5293 auto StringAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 5294 writeStringRecord(Stream, bitc::IDENTIFICATION_CODE_STRING, 5295 "LLVM" LLVM_VERSION_STRING, StringAbbrev); 5296 5297 // Write the epoch version 5298 Abbv = std::make_shared<BitCodeAbbrev>(); 5299 Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH)); 5300 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); 5301 auto EpochAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 5302 constexpr std::array<unsigned, 1> Vals = {{bitc::BITCODE_CURRENT_EPOCH}}; 5303 Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev); 5304 Stream.ExitBlock(); 5305 } 5306 5307 void ModuleBitcodeWriter::writeModuleHash(StringRef View) { 5308 // Emit the module's hash. 5309 // MODULE_CODE_HASH: [5*i32] 5310 if (GenerateHash) { 5311 uint32_t Vals[5]; 5312 Hasher.update(ArrayRef<uint8_t>( 5313 reinterpret_cast<const uint8_t *>(View.data()), View.size())); 5314 std::array<uint8_t, 20> Hash = Hasher.result(); 5315 for (int Pos = 0; Pos < 20; Pos += 4) { 5316 Vals[Pos / 4] = support::endian::read32be(Hash.data() + Pos); 5317 } 5318 5319 // Emit the finished record. 5320 Stream.EmitRecord(bitc::MODULE_CODE_HASH, Vals); 5321 5322 if (ModHash) 5323 // Save the written hash value. 5324 llvm::copy(Vals, std::begin(*ModHash)); 5325 } 5326 } 5327 5328 void ModuleBitcodeWriter::write() { 5329 writeIdentificationBlock(Stream); 5330 5331 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 5332 // We will want to write the module hash at this point. Block any flushing so 5333 // we can have access to the whole underlying data later. 5334 Stream.markAndBlockFlushing(); 5335 5336 writeModuleVersion(); 5337 5338 // Emit blockinfo, which defines the standard abbreviations etc. 5339 writeBlockInfo(); 5340 5341 // Emit information describing all of the types in the module. 5342 writeTypeTable(); 5343 5344 // Emit information about attribute groups. 5345 writeAttributeGroupTable(); 5346 5347 // Emit information about parameter attributes. 5348 writeAttributeTable(); 5349 5350 writeComdats(); 5351 5352 // Emit top-level description of module, including target triple, inline asm, 5353 // descriptors for global variables, and function prototype info. 5354 writeModuleInfo(); 5355 5356 // Emit constants. 5357 writeModuleConstants(); 5358 5359 // Emit metadata kind names. 5360 writeModuleMetadataKinds(); 5361 5362 // Emit metadata. 5363 writeModuleMetadata(); 5364 5365 // Emit module-level use-lists. 5366 if (VE.shouldPreserveUseListOrder()) 5367 writeUseListBlock(nullptr); 5368 5369 writeOperandBundleTags(); 5370 writeSyncScopeNames(); 5371 5372 // Emit function bodies. 5373 DenseMap<const Function *, uint64_t> FunctionToBitcodeIndex; 5374 for (const Function &F : M) 5375 if (!F.isDeclaration()) 5376 writeFunction(F, FunctionToBitcodeIndex); 5377 5378 // Need to write after the above call to WriteFunction which populates 5379 // the summary information in the index. 5380 if (Index) 5381 writePerModuleGlobalValueSummary(); 5382 5383 writeGlobalValueSymbolTable(FunctionToBitcodeIndex); 5384 5385 writeModuleHash(Stream.getMarkedBufferAndResumeFlushing()); 5386 5387 Stream.ExitBlock(); 5388 } 5389 5390 static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer, 5391 uint32_t &Position) { 5392 support::endian::write32le(&Buffer[Position], Value); 5393 Position += 4; 5394 } 5395 5396 /// If generating a bc file on darwin, we have to emit a 5397 /// header and trailer to make it compatible with the system archiver. To do 5398 /// this we emit the following header, and then emit a trailer that pads the 5399 /// file out to be a multiple of 16 bytes. 5400 /// 5401 /// struct bc_header { 5402 /// uint32_t Magic; // 0x0B17C0DE 5403 /// uint32_t Version; // Version, currently always 0. 5404 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file. 5405 /// uint32_t BitcodeSize; // Size of traditional bitcode file. 5406 /// uint32_t CPUType; // CPU specifier. 5407 /// ... potentially more later ... 5408 /// }; 5409 static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer, 5410 const Triple &TT) { 5411 unsigned CPUType = ~0U; 5412 5413 // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*, arm-*, thumb-*, 5414 // armv[0-9]-*, thumbv[0-9]-*, armv5te-*, or armv6t2-*. The CPUType is a magic 5415 // number from /usr/include/mach/machine.h. It is ok to reproduce the 5416 // specific constants here because they are implicitly part of the Darwin ABI. 5417 enum { 5418 DARWIN_CPU_ARCH_ABI64 = 0x01000000, 5419 DARWIN_CPU_TYPE_X86 = 7, 5420 DARWIN_CPU_TYPE_ARM = 12, 5421 DARWIN_CPU_TYPE_POWERPC = 18 5422 }; 5423 5424 Triple::ArchType Arch = TT.getArch(); 5425 if (Arch == Triple::x86_64) 5426 CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64; 5427 else if (Arch == Triple::x86) 5428 CPUType = DARWIN_CPU_TYPE_X86; 5429 else if (Arch == Triple::ppc) 5430 CPUType = DARWIN_CPU_TYPE_POWERPC; 5431 else if (Arch == Triple::ppc64) 5432 CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64; 5433 else if (Arch == Triple::arm || Arch == Triple::thumb) 5434 CPUType = DARWIN_CPU_TYPE_ARM; 5435 5436 // Traditional Bitcode starts after header. 5437 assert(Buffer.size() >= BWH_HeaderSize && 5438 "Expected header size to be reserved"); 5439 unsigned BCOffset = BWH_HeaderSize; 5440 unsigned BCSize = Buffer.size() - BWH_HeaderSize; 5441 5442 // Write the magic and version. 5443 unsigned Position = 0; 5444 writeInt32ToBuffer(0x0B17C0DE, Buffer, Position); 5445 writeInt32ToBuffer(0, Buffer, Position); // Version. 5446 writeInt32ToBuffer(BCOffset, Buffer, Position); 5447 writeInt32ToBuffer(BCSize, Buffer, Position); 5448 writeInt32ToBuffer(CPUType, Buffer, Position); 5449 5450 // If the file is not a multiple of 16 bytes, insert dummy padding. 5451 while (Buffer.size() & 15) 5452 Buffer.push_back(0); 5453 } 5454 5455 /// Helper to write the header common to all bitcode files. 5456 static void writeBitcodeHeader(BitstreamWriter &Stream) { 5457 // Emit the file header. 5458 Stream.Emit((unsigned)'B', 8); 5459 Stream.Emit((unsigned)'C', 8); 5460 Stream.Emit(0x0, 4); 5461 Stream.Emit(0xC, 4); 5462 Stream.Emit(0xE, 4); 5463 Stream.Emit(0xD, 4); 5464 } 5465 5466 BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer) 5467 : Stream(new BitstreamWriter(Buffer)) { 5468 writeBitcodeHeader(*Stream); 5469 } 5470 5471 BitcodeWriter::BitcodeWriter(raw_ostream &FS) 5472 : Stream(new BitstreamWriter(FS, FlushThreshold)) { 5473 writeBitcodeHeader(*Stream); 5474 } 5475 5476 BitcodeWriter::~BitcodeWriter() { assert(WroteStrtab); } 5477 5478 void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) { 5479 Stream->EnterSubblock(Block, 3); 5480 5481 auto Abbv = std::make_shared<BitCodeAbbrev>(); 5482 Abbv->Add(BitCodeAbbrevOp(Record)); 5483 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 5484 auto AbbrevNo = Stream->EmitAbbrev(std::move(Abbv)); 5485 5486 Stream->EmitRecordWithBlob(AbbrevNo, ArrayRef<uint64_t>{Record}, Blob); 5487 5488 Stream->ExitBlock(); 5489 } 5490 5491 void BitcodeWriter::writeSymtab() { 5492 assert(!WroteStrtab && !WroteSymtab); 5493 5494 // If any module has module-level inline asm, we will require a registered asm 5495 // parser for the target so that we can create an accurate symbol table for 5496 // the module. 5497 for (Module *M : Mods) { 5498 if (M->getModuleInlineAsm().empty()) 5499 continue; 5500 5501 std::string Err; 5502 const Triple TT(M->getTargetTriple()); 5503 const Target *T = TargetRegistry::lookupTarget(TT, Err); 5504 if (!T || !T->hasMCAsmParser()) 5505 return; 5506 } 5507 5508 WroteSymtab = true; 5509 SmallVector<char, 0> Symtab; 5510 // The irsymtab::build function may be unable to create a symbol table if the 5511 // module is malformed (e.g. it contains an invalid alias). Writing a symbol 5512 // table is not required for correctness, but we still want to be able to 5513 // write malformed modules to bitcode files, so swallow the error. 5514 if (Error E = irsymtab::build(Mods, Symtab, StrtabBuilder, Alloc)) { 5515 consumeError(std::move(E)); 5516 return; 5517 } 5518 5519 writeBlob(bitc::SYMTAB_BLOCK_ID, bitc::SYMTAB_BLOB, 5520 {Symtab.data(), Symtab.size()}); 5521 } 5522 5523 void BitcodeWriter::writeStrtab() { 5524 assert(!WroteStrtab); 5525 5526 std::vector<char> Strtab; 5527 StrtabBuilder.finalizeInOrder(); 5528 Strtab.resize(StrtabBuilder.getSize()); 5529 StrtabBuilder.write((uint8_t *)Strtab.data()); 5530 5531 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, 5532 {Strtab.data(), Strtab.size()}); 5533 5534 WroteStrtab = true; 5535 } 5536 5537 void BitcodeWriter::copyStrtab(StringRef Strtab) { 5538 writeBlob(bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB, Strtab); 5539 WroteStrtab = true; 5540 } 5541 5542 void BitcodeWriter::writeModule(const Module &M, 5543 bool ShouldPreserveUseListOrder, 5544 const ModuleSummaryIndex *Index, 5545 bool GenerateHash, ModuleHash *ModHash) { 5546 assert(!WroteStrtab); 5547 5548 // The Mods vector is used by irsymtab::build, which requires non-const 5549 // Modules in case it needs to materialize metadata. But the bitcode writer 5550 // requires that the module is materialized, so we can cast to non-const here, 5551 // after checking that it is in fact materialized. 5552 assert(M.isMaterialized()); 5553 Mods.push_back(const_cast<Module *>(&M)); 5554 5555 ModuleBitcodeWriter ModuleWriter(M, StrtabBuilder, *Stream, 5556 ShouldPreserveUseListOrder, Index, 5557 GenerateHash, ModHash); 5558 ModuleWriter.write(); 5559 } 5560 5561 void BitcodeWriter::writeIndex( 5562 const ModuleSummaryIndex *Index, 5563 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex, 5564 const GVSummaryPtrSet *DecSummaries) { 5565 IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries, 5566 ModuleToSummariesForIndex); 5567 IndexWriter.write(); 5568 } 5569 5570 /// Write the specified module to the specified output stream. 5571 void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out, 5572 bool ShouldPreserveUseListOrder, 5573 const ModuleSummaryIndex *Index, 5574 bool GenerateHash, ModuleHash *ModHash) { 5575 auto Write = [&](BitcodeWriter &Writer) { 5576 Writer.writeModule(M, ShouldPreserveUseListOrder, Index, GenerateHash, 5577 ModHash); 5578 Writer.writeSymtab(); 5579 Writer.writeStrtab(); 5580 }; 5581 Triple TT(M.getTargetTriple()); 5582 if (TT.isOSDarwin() || TT.isOSBinFormatMachO()) { 5583 // If this is darwin or another generic macho target, reserve space for the 5584 // header. Note that the header is computed *after* the output is known, so 5585 // we currently explicitly use a buffer, write to it, and then subsequently 5586 // flush to Out. 5587 SmallVector<char, 0> Buffer; 5588 Buffer.reserve(256 * 1024); 5589 Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0); 5590 BitcodeWriter Writer(Buffer); 5591 Write(Writer); 5592 emitDarwinBCHeaderAndTrailer(Buffer, TT); 5593 Out.write(Buffer.data(), Buffer.size()); 5594 } else { 5595 BitcodeWriter Writer(Out); 5596 Write(Writer); 5597 } 5598 } 5599 5600 void IndexBitcodeWriter::write() { 5601 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 5602 5603 writeModuleVersion(); 5604 5605 // Write the module paths in the combined index. 5606 writeModStrings(); 5607 5608 // Write the summary combined index records. 5609 writeCombinedGlobalValueSummary(); 5610 5611 Stream.ExitBlock(); 5612 } 5613 5614 // Write the specified module summary index to the given raw output stream, 5615 // where it will be written in a new bitcode block. This is used when 5616 // writing the combined index file for ThinLTO. When writing a subset of the 5617 // index for a distributed backend, provide a \p ModuleToSummariesForIndex map. 5618 void llvm::writeIndexToFile( 5619 const ModuleSummaryIndex &Index, raw_ostream &Out, 5620 const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex, 5621 const GVSummaryPtrSet *DecSummaries) { 5622 SmallVector<char, 0> Buffer; 5623 Buffer.reserve(256 * 1024); 5624 5625 BitcodeWriter Writer(Buffer); 5626 Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries); 5627 Writer.writeStrtab(); 5628 5629 Out.write((char *)&Buffer.front(), Buffer.size()); 5630 } 5631 5632 namespace { 5633 5634 /// Class to manage the bitcode writing for a thin link bitcode file. 5635 class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase { 5636 /// ModHash is for use in ThinLTO incremental build, generated while writing 5637 /// the module bitcode file. 5638 const ModuleHash *ModHash; 5639 5640 public: 5641 ThinLinkBitcodeWriter(const Module &M, StringTableBuilder &StrtabBuilder, 5642 BitstreamWriter &Stream, 5643 const ModuleSummaryIndex &Index, 5644 const ModuleHash &ModHash) 5645 : ModuleBitcodeWriterBase(M, StrtabBuilder, Stream, 5646 /*ShouldPreserveUseListOrder=*/false, &Index), 5647 ModHash(&ModHash) {} 5648 5649 void write(); 5650 5651 private: 5652 void writeSimplifiedModuleInfo(); 5653 }; 5654 5655 } // end anonymous namespace 5656 5657 // This function writes a simpilified module info for thin link bitcode file. 5658 // It only contains the source file name along with the name(the offset and 5659 // size in strtab) and linkage for global values. For the global value info 5660 // entry, in order to keep linkage at offset 5, there are three zeros used 5661 // as padding. 5662 void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() { 5663 SmallVector<unsigned, 64> Vals; 5664 // Emit the module's source file name. 5665 { 5666 StringEncoding Bits = getStringEncoding(M.getSourceFileName()); 5667 BitCodeAbbrevOp AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8); 5668 if (Bits == SE_Char6) 5669 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Char6); 5670 else if (Bits == SE_Fixed7) 5671 AbbrevOpToUse = BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7); 5672 5673 // MODULE_CODE_SOURCE_FILENAME: [namechar x N] 5674 auto Abbv = std::make_shared<BitCodeAbbrev>(); 5675 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_SOURCE_FILENAME)); 5676 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); 5677 Abbv->Add(AbbrevOpToUse); 5678 unsigned FilenameAbbrev = Stream.EmitAbbrev(std::move(Abbv)); 5679 5680 for (const auto P : M.getSourceFileName()) 5681 Vals.push_back((unsigned char)P); 5682 5683 Stream.EmitRecord(bitc::MODULE_CODE_SOURCE_FILENAME, Vals, FilenameAbbrev); 5684 Vals.clear(); 5685 } 5686 5687 // Emit the global variable information. 5688 for (const GlobalVariable &GV : M.globals()) { 5689 // GLOBALVAR: [strtab offset, strtab size, 0, 0, 0, linkage] 5690 Vals.push_back(StrtabBuilder.add(GV.getName())); 5691 Vals.push_back(GV.getName().size()); 5692 Vals.push_back(0); 5693 Vals.push_back(0); 5694 Vals.push_back(0); 5695 Vals.push_back(getEncodedLinkage(GV)); 5696 5697 Stream.EmitRecord(bitc::MODULE_CODE_GLOBALVAR, Vals); 5698 Vals.clear(); 5699 } 5700 5701 // Emit the function proto information. 5702 for (const Function &F : M) { 5703 // FUNCTION: [strtab offset, strtab size, 0, 0, 0, linkage] 5704 Vals.push_back(StrtabBuilder.add(F.getName())); 5705 Vals.push_back(F.getName().size()); 5706 Vals.push_back(0); 5707 Vals.push_back(0); 5708 Vals.push_back(0); 5709 Vals.push_back(getEncodedLinkage(F)); 5710 5711 Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals); 5712 Vals.clear(); 5713 } 5714 5715 // Emit the alias information. 5716 for (const GlobalAlias &A : M.aliases()) { 5717 // ALIAS: [strtab offset, strtab size, 0, 0, 0, linkage] 5718 Vals.push_back(StrtabBuilder.add(A.getName())); 5719 Vals.push_back(A.getName().size()); 5720 Vals.push_back(0); 5721 Vals.push_back(0); 5722 Vals.push_back(0); 5723 Vals.push_back(getEncodedLinkage(A)); 5724 5725 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals); 5726 Vals.clear(); 5727 } 5728 5729 // Emit the ifunc information. 5730 for (const GlobalIFunc &I : M.ifuncs()) { 5731 // IFUNC: [strtab offset, strtab size, 0, 0, 0, linkage] 5732 Vals.push_back(StrtabBuilder.add(I.getName())); 5733 Vals.push_back(I.getName().size()); 5734 Vals.push_back(0); 5735 Vals.push_back(0); 5736 Vals.push_back(0); 5737 Vals.push_back(getEncodedLinkage(I)); 5738 5739 Stream.EmitRecord(bitc::MODULE_CODE_IFUNC, Vals); 5740 Vals.clear(); 5741 } 5742 } 5743 5744 void ThinLinkBitcodeWriter::write() { 5745 Stream.EnterSubblock(bitc::MODULE_BLOCK_ID, 3); 5746 5747 writeModuleVersion(); 5748 5749 writeSimplifiedModuleInfo(); 5750 5751 writePerModuleGlobalValueSummary(); 5752 5753 // Write module hash. 5754 Stream.EmitRecord(bitc::MODULE_CODE_HASH, ArrayRef<uint32_t>(*ModHash)); 5755 5756 Stream.ExitBlock(); 5757 } 5758 5759 void BitcodeWriter::writeThinLinkBitcode(const Module &M, 5760 const ModuleSummaryIndex &Index, 5761 const ModuleHash &ModHash) { 5762 assert(!WroteStrtab); 5763 5764 // The Mods vector is used by irsymtab::build, which requires non-const 5765 // Modules in case it needs to materialize metadata. But the bitcode writer 5766 // requires that the module is materialized, so we can cast to non-const here, 5767 // after checking that it is in fact materialized. 5768 assert(M.isMaterialized()); 5769 Mods.push_back(const_cast<Module *>(&M)); 5770 5771 ThinLinkBitcodeWriter ThinLinkWriter(M, StrtabBuilder, *Stream, Index, 5772 ModHash); 5773 ThinLinkWriter.write(); 5774 } 5775 5776 // Write the specified thin link bitcode file to the given raw output stream, 5777 // where it will be written in a new bitcode block. This is used when 5778 // writing the per-module index file for ThinLTO. 5779 void llvm::writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out, 5780 const ModuleSummaryIndex &Index, 5781 const ModuleHash &ModHash) { 5782 SmallVector<char, 0> Buffer; 5783 Buffer.reserve(256 * 1024); 5784 5785 BitcodeWriter Writer(Buffer); 5786 Writer.writeThinLinkBitcode(M, Index, ModHash); 5787 Writer.writeSymtab(); 5788 Writer.writeStrtab(); 5789 5790 Out.write((char *)&Buffer.front(), Buffer.size()); 5791 } 5792 5793 static const char *getSectionNameForBitcode(const Triple &T) { 5794 switch (T.getObjectFormat()) { 5795 case Triple::MachO: 5796 return "__LLVM,__bitcode"; 5797 case Triple::COFF: 5798 case Triple::ELF: 5799 case Triple::Wasm: 5800 case Triple::UnknownObjectFormat: 5801 return ".llvmbc"; 5802 case Triple::GOFF: 5803 llvm_unreachable("GOFF is not yet implemented"); 5804 break; 5805 case Triple::SPIRV: 5806 if (T.getVendor() == Triple::AMD) 5807 return ".llvmbc"; 5808 llvm_unreachable("SPIRV is not yet implemented"); 5809 break; 5810 case Triple::XCOFF: 5811 llvm_unreachable("XCOFF is not yet implemented"); 5812 break; 5813 case Triple::DXContainer: 5814 llvm_unreachable("DXContainer is not yet implemented"); 5815 break; 5816 } 5817 llvm_unreachable("Unimplemented ObjectFormatType"); 5818 } 5819 5820 static const char *getSectionNameForCommandline(const Triple &T) { 5821 switch (T.getObjectFormat()) { 5822 case Triple::MachO: 5823 return "__LLVM,__cmdline"; 5824 case Triple::COFF: 5825 case Triple::ELF: 5826 case Triple::Wasm: 5827 case Triple::UnknownObjectFormat: 5828 return ".llvmcmd"; 5829 case Triple::GOFF: 5830 llvm_unreachable("GOFF is not yet implemented"); 5831 break; 5832 case Triple::SPIRV: 5833 if (T.getVendor() == Triple::AMD) 5834 return ".llvmcmd"; 5835 llvm_unreachable("SPIRV is not yet implemented"); 5836 break; 5837 case Triple::XCOFF: 5838 llvm_unreachable("XCOFF is not yet implemented"); 5839 break; 5840 case Triple::DXContainer: 5841 llvm_unreachable("DXC is not yet implemented"); 5842 break; 5843 } 5844 llvm_unreachable("Unimplemented ObjectFormatType"); 5845 } 5846 5847 void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, 5848 bool EmbedBitcode, bool EmbedCmdline, 5849 const std::vector<uint8_t> &CmdArgs) { 5850 // Save llvm.compiler.used and remove it. 5851 SmallVector<Constant *, 2> UsedArray; 5852 SmallVector<GlobalValue *, 4> UsedGlobals; 5853 GlobalVariable *Used = collectUsedGlobalVariables(M, UsedGlobals, true); 5854 Type *UsedElementType = Used ? Used->getValueType()->getArrayElementType() 5855 : PointerType::getUnqual(M.getContext()); 5856 for (auto *GV : UsedGlobals) { 5857 if (GV->getName() != "llvm.embedded.module" && 5858 GV->getName() != "llvm.cmdline") 5859 UsedArray.push_back( 5860 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 5861 } 5862 if (Used) 5863 Used->eraseFromParent(); 5864 5865 // Embed the bitcode for the llvm module. 5866 std::string Data; 5867 ArrayRef<uint8_t> ModuleData; 5868 Triple T(M.getTargetTriple()); 5869 5870 if (EmbedBitcode) { 5871 if (Buf.getBufferSize() == 0 || 5872 !isBitcode((const unsigned char *)Buf.getBufferStart(), 5873 (const unsigned char *)Buf.getBufferEnd())) { 5874 // If the input is LLVM Assembly, bitcode is produced by serializing 5875 // the module. Use-lists order need to be preserved in this case. 5876 llvm::raw_string_ostream OS(Data); 5877 llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true); 5878 ModuleData = 5879 ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size()); 5880 } else 5881 // If the input is LLVM bitcode, write the input byte stream directly. 5882 ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(), 5883 Buf.getBufferSize()); 5884 } 5885 llvm::Constant *ModuleConstant = 5886 llvm::ConstantDataArray::get(M.getContext(), ModuleData); 5887 llvm::GlobalVariable *GV = new llvm::GlobalVariable( 5888 M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, 5889 ModuleConstant); 5890 GV->setSection(getSectionNameForBitcode(T)); 5891 // Set alignment to 1 to prevent padding between two contributions from input 5892 // sections after linking. 5893 GV->setAlignment(Align(1)); 5894 UsedArray.push_back( 5895 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 5896 if (llvm::GlobalVariable *Old = 5897 M.getGlobalVariable("llvm.embedded.module", true)) { 5898 assert(Old->hasZeroLiveUses() && 5899 "llvm.embedded.module can only be used once in llvm.compiler.used"); 5900 GV->takeName(Old); 5901 Old->eraseFromParent(); 5902 } else { 5903 GV->setName("llvm.embedded.module"); 5904 } 5905 5906 // Skip if only bitcode needs to be embedded. 5907 if (EmbedCmdline) { 5908 // Embed command-line options. 5909 ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CmdArgs.data()), 5910 CmdArgs.size()); 5911 llvm::Constant *CmdConstant = 5912 llvm::ConstantDataArray::get(M.getContext(), CmdData); 5913 GV = new llvm::GlobalVariable(M, CmdConstant->getType(), true, 5914 llvm::GlobalValue::PrivateLinkage, 5915 CmdConstant); 5916 GV->setSection(getSectionNameForCommandline(T)); 5917 GV->setAlignment(Align(1)); 5918 UsedArray.push_back( 5919 ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); 5920 if (llvm::GlobalVariable *Old = M.getGlobalVariable("llvm.cmdline", true)) { 5921 assert(Old->hasZeroLiveUses() && 5922 "llvm.cmdline can only be used once in llvm.compiler.used"); 5923 GV->takeName(Old); 5924 Old->eraseFromParent(); 5925 } else { 5926 GV->setName("llvm.cmdline"); 5927 } 5928 } 5929 5930 if (UsedArray.empty()) 5931 return; 5932 5933 // Recreate llvm.compiler.used. 5934 ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size()); 5935 auto *NewUsed = new GlobalVariable( 5936 M, ATy, false, llvm::GlobalValue::AppendingLinkage, 5937 llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); 5938 NewUsed->setSection("llvm.metadata"); 5939 } 5940