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