diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 8152799bbce..1741d0363b2 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -783,52 +783,44 @@ static void WriteModuleMetadata(const Module *M, const ValueEnumerator &VE, BitstreamWriter &Stream) { const auto &MDs = VE.getMDs(); - bool StartedMetadataBlock = false; + if (MDs.empty() && M->named_metadata_empty()) + return; + + Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); + unsigned MDSAbbrev = 0; + if (VE.hasMDString()) { + // Abbrev for METADATA_STRING. + BitCodeAbbrev *Abbv = new BitCodeAbbrev(); + Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); + MDSAbbrev = Stream.EmitAbbrev(Abbv); + } + SmallVector Record; for (unsigned i = 0, e = MDs.size(); i != e; ++i) { if (const MDNode *N = dyn_cast(MDs[i])) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; - } WriteMDNode(N, VE, Stream, Record); - } else if (const auto *MDC = dyn_cast(MDs[i])) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; - } - WriteValueAsMetadata(MDC, VE, Stream, Record); - } else if (const MDString *MDS = dyn_cast(MDs[i])) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - - // Abbrev for METADATA_STRING. - BitCodeAbbrev *Abbv = new BitCodeAbbrev(); - Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); - Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); - MDSAbbrev = Stream.EmitAbbrev(Abbv); - StartedMetadataBlock = true; - } - - // Code: [strchar x N] - Record.append(MDS->bytes_begin(), MDS->bytes_end()); - - // Emit the finished record. - Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); - Record.clear(); + continue; } + if (const auto *MDC = dyn_cast(MDs[i])) { + WriteValueAsMetadata(MDC, VE, Stream, Record); + continue; + } + const MDString *MDS = cast(MDs[i]); + // Code: [strchar x N] + Record.append(MDS->bytes_begin(), MDS->bytes_end()); + + // Emit the finished record. + Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); + Record.clear(); } // Write named metadata. for (Module::const_named_metadata_iterator I = M->named_metadata_begin(), E = M->named_metadata_end(); I != E; ++I) { const NamedMDNode *NMD = I; - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; - } // Write name. StringRef Str = NMD->getName(); @@ -844,8 +836,7 @@ static void WriteModuleMetadata(const Module *M, Record.clear(); } - if (StartedMetadataBlock) - Stream.ExitBlock(); + Stream.ExitBlock(); } static void WriteFunctionLocalMetadata(const Function &F, diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index 2c0fdc00ba4..b117d7bb49a 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -282,7 +282,7 @@ static bool isIntOrIntVectorValue(const std::pair &V) { return V.first->getType()->isIntOrIntVectorTy(); } -ValueEnumerator::ValueEnumerator(const Module &M) { +ValueEnumerator::ValueEnumerator(const Module &M) : HasMDString(false) { if (shouldPreserveBitcodeUseListOrder()) UseListOrders = predictUseListOrder(M); @@ -546,6 +546,8 @@ void ValueEnumerator::EnumerateMetadata(const Metadata *MD) { else if (auto *C = dyn_cast(MD)) EnumerateValue(C->getValue()); + HasMDString |= isa(MD); + // Replace the dummy ID inserted above with the correct one. MDValueMap may // have changed by inserting operands, so we need a fresh lookup here. MDs.push_back(MD); diff --git a/lib/Bitcode/Writer/ValueEnumerator.h b/lib/Bitcode/Writer/ValueEnumerator.h index 3b1b3137e39..e63394fefa5 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.h +++ b/lib/Bitcode/Writer/ValueEnumerator.h @@ -64,6 +64,7 @@ private: SmallVector FunctionLocalMDs; typedef DenseMap MetadataMapType; MetadataMapType MDValueMap; + bool HasMDString; typedef DenseMap AttributeGroupMapType; AttributeGroupMapType AttributeGroupMap; @@ -109,6 +110,8 @@ public: unsigned getValueID(const Value *V) const; unsigned getMetadataID(const Metadata *V) const; + bool hasMDString() const { return HasMDString; } + unsigned getTypeID(Type *T) const { TypeMapType::const_iterator I = TypeMap.find(T); assert(I != TypeMap.end() && "Type not in ValueEnumerator!");