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