+/// Write the module path strings, currently only used when generating
+/// a combined index file.
+static void WriteModStrings(const FunctionInfoIndex &I,
+ BitstreamWriter &Stream) {
+ Stream.EnterSubblock(bitc::MODULE_STRTAB_BLOCK_ID, 3);
+
+ // TODO: See which abbrev sizes we actually need to emit
+
+ // 8-bit fixed-width MST_ENTRY strings.
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
+ unsigned Abbrev8Bit = Stream.EmitAbbrev(Abbv);
+
+ // 7-bit fixed width MST_ENTRY strings.
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
+ unsigned Abbrev7Bit = Stream.EmitAbbrev(Abbv);
+
+ // 6-bit char6 MST_ENTRY strings.
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::MST_CODE_ENTRY));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
+ unsigned Abbrev6Bit = Stream.EmitAbbrev(Abbv);
+
+ SmallVector<unsigned, 64> NameVals;
+ for (const StringMapEntry<uint64_t> &MPSE : I.modPathStringEntries()) {
+ StringEncoding Bits =
+ getStringEncoding(MPSE.getKey().data(), MPSE.getKey().size());
+ unsigned AbbrevToUse = Abbrev8Bit;
+ if (Bits == SE_Char6)
+ AbbrevToUse = Abbrev6Bit;
+ else if (Bits == SE_Fixed7)
+ AbbrevToUse = Abbrev7Bit;
+
+ NameVals.push_back(MPSE.getValue());
+
+ for (const auto P : MPSE.getKey())
+ NameVals.push_back((unsigned char)P);
+
+ // Emit the finished record.
+ Stream.EmitRecord(bitc::MST_CODE_ENTRY, NameVals, AbbrevToUse);
+ NameVals.clear();
+ }
+ Stream.ExitBlock();
+}
+
+// Helper to emit a single function summary record.
+static void WritePerModuleFunctionSummaryRecord(
+ SmallVector<unsigned, 64> &NameVals, FunctionSummary *FS, unsigned ValueID,
+ unsigned FSAbbrev, BitstreamWriter &Stream) {
+ assert(FS);
+ NameVals.push_back(ValueID);
+ NameVals.push_back(FS->isLocalFunction());
+ NameVals.push_back(FS->instCount());
+
+ // Emit the finished record.
+ Stream.EmitRecord(bitc::FS_CODE_PERMODULE_ENTRY, NameVals, FSAbbrev);
+ NameVals.clear();
+}
+
+/// Emit the per-module function summary section alongside the rest of
+/// the module's bitcode.
+static void WritePerModuleFunctionSummary(
+ DenseMap<const Function *, std::unique_ptr<FunctionInfo>> &FunctionIndex,
+ const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) {
+ Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
+
+ // Abbrev for FS_CODE_PERMODULE_ENTRY.
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_PERMODULE_ENTRY));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // valueid
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // islocal
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
+ unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
+
+ SmallVector<unsigned, 64> NameVals;
+ for (auto &I : FunctionIndex) {
+ // Skip anonymous functions. We will emit a function summary for
+ // any aliases below.
+ if (!I.first->hasName())
+ continue;
+
+ WritePerModuleFunctionSummaryRecord(
+ NameVals, I.second->functionSummary(),
+ VE.getValueID(M->getValueSymbolTable().lookup(I.first->getName())),
+ FSAbbrev, Stream);
+ }
+
+ for (const GlobalAlias &A : M->aliases()) {
+ if (!A.getBaseObject())
+ continue;
+ const Function *F = dyn_cast<Function>(A.getBaseObject());
+ if (!F || F->isDeclaration())
+ continue;
+
+ assert(FunctionIndex.count(F) == 1);
+ WritePerModuleFunctionSummaryRecord(
+ NameVals, FunctionIndex[F]->functionSummary(),
+ VE.getValueID(M->getValueSymbolTable().lookup(A.getName())), FSAbbrev,
+ Stream);
+ }
+
+ Stream.ExitBlock();
+}
+
+/// Emit the combined function summary section into the combined index
+/// file.
+static void WriteCombinedFunctionSummary(const FunctionInfoIndex &I,
+ BitstreamWriter &Stream) {
+ Stream.EnterSubblock(bitc::FUNCTION_SUMMARY_BLOCK_ID, 3);
+
+ // Abbrev for FS_CODE_COMBINED_ENTRY.
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::FS_CODE_COMBINED_ENTRY));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // modid
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // instcount
+ unsigned FSAbbrev = Stream.EmitAbbrev(Abbv);
+
+ SmallVector<unsigned, 64> NameVals;
+ for (const auto &FII : I) {
+ for (auto &FI : FII.getValue()) {
+ FunctionSummary *FS = FI->functionSummary();
+ assert(FS);
+
+ NameVals.push_back(I.getModuleId(FS->modulePath()));
+ NameVals.push_back(FS->instCount());
+
+ // Record the starting offset of this summary entry for use
+ // in the VST entry. Add the current code size since the
+ // reader will invoke readRecord after the abbrev id read.
+ FI->setBitcodeIndex(Stream.GetCurrentBitNo() + Stream.GetAbbrevIDWidth());
+
+ // Emit the finished record.
+ Stream.EmitRecord(bitc::FS_CODE_COMBINED_ENTRY, NameVals, FSAbbrev);
+ NameVals.clear();
+ }
+ }
+
+ Stream.ExitBlock();
+}
+
+// Create the "IDENTIFICATION_BLOCK_ID" containing a single string with the
+// current llvm version, and a record for the epoch number.
+static void WriteIdentificationBlock(const Module *M, BitstreamWriter &Stream) {
+ Stream.EnterSubblock(bitc::IDENTIFICATION_BLOCK_ID, 5);
+
+ // Write the "user readable" string identifying the bitcode producer
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_STRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
+ auto StringAbbrev = Stream.EmitAbbrev(Abbv);
+ WriteStringRecord(bitc::IDENTIFICATION_CODE_STRING,
+ "LLVM" LLVM_VERSION_STRING, StringAbbrev, Stream);
+
+ // Write the epoch version
+ Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::IDENTIFICATION_CODE_EPOCH));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
+ auto EpochAbbrev = Stream.EmitAbbrev(Abbv);
+ SmallVector<unsigned, 1> Vals = {bitc::BITCODE_CURRENT_EPOCH};
+ Stream.EmitRecord(bitc::IDENTIFICATION_CODE_EPOCH, Vals, EpochAbbrev);
+ Stream.ExitBlock();
+}
+