diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index ca001c1f09d..99ddeb682b8 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -512,6 +512,12 @@ bool LLParser::ParseNamedMetadata() { SmallVector Elts; do { + // Null is a special case since it is typeless. + if (EatIfPresent(lltok::kw_null)) { + Elts.push_back(0); + continue; + } + if (ParseToken(lltok::exclaim, "Expected '!' here")) return true; diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index c8097a09996..33da5e4d611 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -789,9 +789,13 @@ bool BitcodeReader::ParseMetadata() { unsigned Size = Record.size(); SmallVector Elts; for (unsigned i = 0; i != Size; ++i) { - Value *MD = MDValueList.getValueFwdRef(Record[i]); - if (MDNode *B = dyn_cast_or_null(MD)) - Elts.push_back(B); + if (Record[i] == ~0U) + Elts.push_back(NULL); + else { + Value *MD = MDValueList.getValueFwdRef(Record[i]); + if (MDNode *B = dyn_cast_or_null(MD)) + Elts.push_back(B); + } } Value *V = NamedMDNode::Create(Context, Name.str(), Elts.data(), Elts.size(), TheModule); diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index c78a30e8690..ce9036bd06e 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -540,7 +540,7 @@ static void WriteModuleMetadata(const ValueEnumerator &VE, if (NMD->getOperand(i)) Record.push_back(VE.getValueID(NMD->getOperand(i))); else - Record.push_back(0); + Record.push_back(~0U); } Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); Record.clear(); diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp index 861de84e34b..c409c2066d1 100644 --- a/lib/Bitcode/Writer/ValueEnumerator.cpp +++ b/lib/Bitcode/Writer/ValueEnumerator.cpp @@ -223,7 +223,8 @@ void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) { if (const NamedMDNode *N = dyn_cast(MD)) { for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) - EnumerateValue(N->getOperand(i)); + if (MDNode *E = N->getOperand(i)) + EnumerateValue(E); MDValues.push_back(std::make_pair(MD, 1U)); MDValueMap[MD] = Values.size(); return; diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 950eb0b05cb..7cb5a7c7598 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -615,8 +615,7 @@ void SlotTracker::processModule() { E = TheModule->named_metadata_end(); I != E; ++I) { const NamedMDNode *NMD = I; for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { - // FIXME: Change accessor to be type safe. - if (MDNode *MD = cast_or_null(NMD->getOperand(i))) + if (MDNode *MD = NMD->getOperand(i)) CreateMetadataSlot(MD); } } @@ -1370,10 +1369,10 @@ void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) { Out << "!" << NMD->getName() << " = !{"; for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { if (i) Out << ", "; - // FIXME: Change accessor to be typesafe. - // FIXME: This doesn't handle null?? - MDNode *MD = cast_or_null(NMD->getOperand(i)); - Out << '!' << Machine.getMetadataSlot(MD); + if (MDNode *MD = NMD->getOperand(i)) + Out << '!' << Machine.getMetadataSlot(MD); + else + Out << "null"; } Out << "}\n"; } diff --git a/test/Feature/NamedMDNode.ll b/test/Feature/NamedMDNode.ll index 56fc349d3e6..a56480fceb4 100644 --- a/test/Feature/NamedMDNode.ll +++ b/test/Feature/NamedMDNode.ll @@ -3,4 +3,4 @@ ;; Simple NamedMDNode !0 = metadata !{i32 42} !1 = metadata !{metadata !"foo"} -!llvm.stuff = !{!0, !1} +!llvm.stuff = !{!0, !1, null}