#include "llvm/Bitcode/BitcodeWriter.h"
#include "ValueEnumerator.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Bitcode/BitcodeCommon.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/LLVMBitCodes.h"
#include "llvm/Bitstream/BitCodes.h"
#include "llvm/Bitstream/BitstreamWriter.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Comdat.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/ConstantRangeList.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalIFunc.h"
#include "llvm/IR/GlobalObject.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/UseListOrder.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/IRSymtab.h"
#include "llvm/Support/AtomicOrdering.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/SHA1.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
usingnamespacellvm;
static cl::opt<unsigned>
IndexThreshold("bitcode-mdindex-threshold", cl::Hidden, cl::init(25),
cl::desc("Number of metadatas above which we emit an index "
"to enable lazy-loading"));
static cl::opt<uint32_t> FlushThreshold(
"bitcode-flush-threshold", cl::Hidden, cl::init(512),
cl::desc("The threshold (unit M) for flushing LLVM bitcode."));
static cl::opt<bool> WriteRelBFToSummary(
"write-relbf-to-summary", cl::Hidden, cl::init(false),
cl::desc("Write relative block frequency to function summary "));
namespace llvm {
extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold;
}
extern bool WriteNewDbgInfoFormatToBitcode;
extern llvm::cl::opt<bool> UseNewDbgInfoFormat;
namespace {
enum { … };
class BitcodeWriterBase { … };
void BitcodeWriterBase::writeModuleVersion() { … }
class ModuleBitcodeWriterBase : public BitcodeWriterBase { … };
class ModuleBitcodeWriter : public ModuleBitcodeWriterBase { … };
class IndexBitcodeWriter : public BitcodeWriterBase { … };
}
static unsigned getEncodedCastOpcode(unsigned Opcode) { … }
static unsigned getEncodedUnaryOpcode(unsigned Opcode) { … }
static unsigned getEncodedBinaryOpcode(unsigned Opcode) { … }
static unsigned getEncodedRMWOperation(AtomicRMWInst::BinOp Op) { … }
static unsigned getEncodedOrdering(AtomicOrdering Ordering) { … }
static void writeStringRecord(BitstreamWriter &Stream, unsigned Code,
StringRef Str, unsigned AbbrevToUse) { … }
static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) { … }
static void emitSignedInt64(SmallVectorImpl<uint64_t> &Vals, uint64_t V) { … }
static void emitWideAPInt(SmallVectorImpl<uint64_t> &Vals, const APInt &A) { … }
static void emitConstantRange(SmallVectorImpl<uint64_t> &Record,
const ConstantRange &CR, bool EmitBitWidth) { … }
void ModuleBitcodeWriter::writeAttributeGroupTable() { … }
void ModuleBitcodeWriter::writeAttributeTable() { … }
void ModuleBitcodeWriter::writeTypeTable() { … }
static unsigned getEncodedLinkage(const GlobalValue::LinkageTypes Linkage) { … }
static unsigned getEncodedLinkage(const GlobalValue &GV) { … }
static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) { … }
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags,
bool ImportAsDecl = false) { … }
static uint64_t getEncodedGVarFlags(GlobalVarSummary::GVarFlags Flags) { … }
static uint64_t getEncodedHotnessCallEdgeInfo(const CalleeInfo &CI) { … }
static uint64_t getEncodedRelBFCallEdgeInfo(const CalleeInfo &CI) { … }
static unsigned getEncodedVisibility(const GlobalValue &GV) { … }
static unsigned getEncodedDLLStorageClass(const GlobalValue &GV) { … }
static unsigned getEncodedThreadLocalMode(const GlobalValue &GV) { … }
static unsigned getEncodedComdatSelectionKind(const Comdat &C) { … }
static unsigned getEncodedUnnamedAddr(const GlobalValue &GV) { … }
size_t ModuleBitcodeWriter::addToStrtab(StringRef Str) { … }
void ModuleBitcodeWriter::writeComdats() { … }
void ModuleBitcodeWriter::writeValueSymbolTableForwardDecl() { … }
enum StringEncoding { … };
static StringEncoding getStringEncoding(StringRef Str) { … }
static_assert …;
static unsigned
serializeSanitizerMetadata(const GlobalValue::SanitizerMetadata &Meta) { … }
void ModuleBitcodeWriter::writeModuleInfo() { … }
static uint64_t getOptimizationFlags(const Value *V) { … }
void ModuleBitcodeWriter::writeValueAsMetadata(
const ValueAsMetadata *MD, SmallVectorImpl<uint64_t> &Record) { … }
void ModuleBitcodeWriter::writeMDTuple(const MDTuple *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
unsigned ModuleBitcodeWriter::createDILocationAbbrev() { … }
void ModuleBitcodeWriter::writeDILocation(const DILocation *N,
SmallVectorImpl<uint64_t> &Record,
unsigned &Abbrev) { … }
unsigned ModuleBitcodeWriter::createGenericDINodeAbbrev() { … }
void ModuleBitcodeWriter::writeGenericDINode(const GenericDINode *N,
SmallVectorImpl<uint64_t> &Record,
unsigned &Abbrev) { … }
void ModuleBitcodeWriter::writeDISubrange(const DISubrange *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIGenericSubrange(
const DIGenericSubrange *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIEnumerator(const DIEnumerator *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIBasicType(const DIBasicType *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIStringType(const DIStringType *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIDerivedType(const DIDerivedType *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDICompositeType(
const DICompositeType *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDISubroutineType(
const DISubroutineType *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIFile(const DIFile *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDICompileUnit(const DICompileUnit *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDISubprogram(const DISubprogram *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDILexicalBlock(const DILexicalBlock *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDILexicalBlockFile(
const DILexicalBlockFile *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDICommonBlock(const DICommonBlock *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDINamespace(const DINamespace *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIMacro(const DIMacro *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIMacroFile(const DIMacroFile *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIArgList(const DIArgList *N,
SmallVectorImpl<uint64_t> &Record) { … }
void ModuleBitcodeWriter::writeDIModule(const DIModule *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIAssignID(const DIAssignID *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDITemplateTypeParameter(
const DITemplateTypeParameter *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDITemplateValueParameter(
const DITemplateValueParameter *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIGlobalVariable(
const DIGlobalVariable *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDILocalVariable(
const DILocalVariable *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDILabel(
const DILabel *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIExpression(const DIExpression *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIGlobalVariableExpression(
const DIGlobalVariableExpression *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIObjCProperty(const DIObjCProperty *N,
SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
void ModuleBitcodeWriter::writeDIImportedEntity(
const DIImportedEntity *N, SmallVectorImpl<uint64_t> &Record,
unsigned Abbrev) { … }
unsigned ModuleBitcodeWriter::createNamedMetadataAbbrev() { … }
void ModuleBitcodeWriter::writeNamedMetadata(
SmallVectorImpl<uint64_t> &Record) { … }
unsigned ModuleBitcodeWriter::createMetadataStringsAbbrev() { … }
void ModuleBitcodeWriter::writeMetadataStrings(
ArrayRef<const Metadata *> Strings, SmallVectorImpl<uint64_t> &Record) { … }
enum MetadataAbbrev : unsigned { … };
void ModuleBitcodeWriter::writeMetadataRecords(
ArrayRef<const Metadata *> MDs, SmallVectorImpl<uint64_t> &Record,
std::vector<unsigned> *MDAbbrevs, std::vector<uint64_t> *IndexPos) { … }
void ModuleBitcodeWriter::writeModuleMetadata() { … }
void ModuleBitcodeWriter::writeFunctionMetadata(const Function &F) { … }
void ModuleBitcodeWriter::pushGlobalMetadataAttachment(
SmallVectorImpl<uint64_t> &Record, const GlobalObject &GO) { … }
void ModuleBitcodeWriter::writeFunctionMetadataAttachment(const Function &F) { … }
void ModuleBitcodeWriter::writeModuleMetadataKinds() { … }
void ModuleBitcodeWriter::writeOperandBundleTags() { … }
void ModuleBitcodeWriter::writeSyncScopeNames() { … }
void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
bool isGlobal) { … }
void ModuleBitcodeWriter::writeModuleConstants() { … }
bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals) { … }
bool ModuleBitcodeWriter::pushValueOrMetadata(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals) { … }
void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
unsigned InstID) { … }
void ModuleBitcodeWriter::pushValue(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals) { … }
void ModuleBitcodeWriter::pushValueSigned(const Value *V, unsigned InstID,
SmallVectorImpl<uint64_t> &Vals) { … }
void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
unsigned InstID,
SmallVectorImpl<unsigned> &Vals) { … }
void ModuleBitcodeWriter::writeGlobalValueSymbolTable(
DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) { … }
void ModuleBitcodeWriter::writeFunctionLevelValueSymbolTable(
const ValueSymbolTable &VST) { … }
void ModuleBitcodeWriter::writeUseList(UseListOrder &&Order) { … }
void ModuleBitcodeWriter::writeUseListBlock(const Function *F) { … }
void ModuleBitcodeWriter::writeFunction(
const Function &F,
DenseMap<const Function *, uint64_t> &FunctionToBitcodeIndex) { … }
void ModuleBitcodeWriter::writeBlockInfo() { … }
void IndexBitcodeWriter::writeModStrings() { … }
template <typename Fn>
static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
FunctionSummary *FS,
Fn GetValueID) { … }
static void
getReferencedTypeIds(FunctionSummary *FS,
std::set<GlobalValue::GUID> &ReferencedTypeIds) { … }
static void writeWholeProgramDevirtResolutionByArg(
SmallVector<uint64_t, 64> &NameVals, const std::vector<uint64_t> &args,
const WholeProgramDevirtResolution::ByArg &ByArg) { … }
static void writeWholeProgramDevirtResolution(
SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
uint64_t Id, const WholeProgramDevirtResolution &Wpd) { … }
static void writeTypeIdSummaryRecord(SmallVector<uint64_t, 64> &NameVals,
StringTableBuilder &StrtabBuilder,
const std::string &Id,
const TypeIdSummary &Summary) { … }
static void writeTypeIdCompatibleVtableSummaryRecord(
SmallVector<uint64_t, 64> &NameVals, StringTableBuilder &StrtabBuilder,
const std::string &Id, const TypeIdCompatibleVtableInfo &Summary,
ValueEnumerator &VE) { … }
static void writeFunctionHeapProfileRecords(
BitstreamWriter &Stream, FunctionSummary *FS, unsigned CallsiteAbbrev,
unsigned AllocAbbrev, bool PerModule,
std::function<unsigned(const ValueInfo &VI)> GetValueID,
std::function<unsigned(unsigned)> GetStackIndex) { … }
void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
SmallVector<uint64_t, 64> &NameVals, GlobalValueSummary *Summary,
unsigned ValueID, unsigned FSCallsRelBFAbbrev,
unsigned FSCallsProfileAbbrev, unsigned CallsiteAbbrev,
unsigned AllocAbbrev, const Function &F) { … }
void ModuleBitcodeWriterBase::writeModuleLevelReferences(
const GlobalVariable &V, SmallVector<uint64_t, 64> &NameVals,
unsigned FSModRefsAbbrev, unsigned FSModVTableRefsAbbrev) { … }
void ModuleBitcodeWriterBase::writePerModuleGlobalValueSummary() { … }
void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { … }
static void writeIdentificationBlock(BitstreamWriter &Stream) { … }
void ModuleBitcodeWriter::writeModuleHash(StringRef View) { … }
void ModuleBitcodeWriter::write() { … }
static void writeInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
uint32_t &Position) { … }
static void emitDarwinBCHeaderAndTrailer(SmallVectorImpl<char> &Buffer,
const Triple &TT) { … }
static void writeBitcodeHeader(BitstreamWriter &Stream) { … }
BitcodeWriter::BitcodeWriter(SmallVectorImpl<char> &Buffer)
: … { … }
BitcodeWriter::BitcodeWriter(raw_ostream &FS)
: … { … }
BitcodeWriter::~BitcodeWriter() { … }
void BitcodeWriter::writeBlob(unsigned Block, unsigned Record, StringRef Blob) { … }
void BitcodeWriter::writeSymtab() { … }
void BitcodeWriter::writeStrtab() { … }
void BitcodeWriter::copyStrtab(StringRef Strtab) { … }
void BitcodeWriter::writeModule(const Module &M,
bool ShouldPreserveUseListOrder,
const ModuleSummaryIndex *Index,
bool GenerateHash, ModuleHash *ModHash) { … }
void BitcodeWriter::writeIndex(
const ModuleSummaryIndex *Index,
const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
const GVSummaryPtrSet *DecSummaries) { … }
void llvm::WriteBitcodeToFile(const Module &M, raw_ostream &Out,
bool ShouldPreserveUseListOrder,
const ModuleSummaryIndex *Index,
bool GenerateHash, ModuleHash *ModHash) { … }
void IndexBitcodeWriter::write() { … }
void llvm::writeIndexToFile(
const ModuleSummaryIndex &Index, raw_ostream &Out,
const ModuleToSummariesForIndexTy *ModuleToSummariesForIndex,
const GVSummaryPtrSet *DecSummaries) { … }
namespace {
class ThinLinkBitcodeWriter : public ModuleBitcodeWriterBase { … };
}
void ThinLinkBitcodeWriter::writeSimplifiedModuleInfo() { … }
void ThinLinkBitcodeWriter::write() { … }
void BitcodeWriter::writeThinLinkBitcode(const Module &M,
const ModuleSummaryIndex &Index,
const ModuleHash &ModHash) { … }
void llvm::writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
const ModuleSummaryIndex &Index,
const ModuleHash &ModHash) { … }
static const char *getSectionNameForBitcode(const Triple &T) { … }
static const char *getSectionNameForCommandline(const Triple &T) { … }
void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
bool EmbedBitcode, bool EmbedCmdline,
const std::vector<uint8_t> &CmdArgs) { … }