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