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