From 090a19bd3cab8b4247bb41eecf20e9f0f211332b Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Thu, 8 Jan 2015 22:38:29 +0000 Subject: IR: Add 'distinct' MDNodes to bitcode and assembly Propagate whether `MDNode`s are 'distinct' through the other types of IR (assembly and bitcode). This adds the `distinct` keyword to assembly. Currently, no one actually calls `MDNode::getDistinct()`, so these nodes only get created for: - self-references, which are never uniqued, and - nodes whose operands are replaced that hit a uniquing collision. The concept of distinct nodes is still not quite first-class, since distinct-ness doesn't yet survive across `MapMetadata()`. Part of PR22111. llvm-svn: 225474 --- llvm/lib/AsmParser/LLLexer.cpp | 3 +++ llvm/lib/AsmParser/LLParser.cpp | 10 +++++++--- llvm/lib/AsmParser/LLParser.h | 2 +- llvm/lib/AsmParser/LLToken.h | 3 +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 8 +++++++- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 4 +++- llvm/lib/IR/AsmWriter.cpp | 2 ++ 7 files changed, 26 insertions(+), 6 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index ca3c9e8b6f8..de5db1a4093 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -655,6 +655,9 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(x); KEYWORD(blockaddress); + // Metadata types. + KEYWORD(distinct); + // Use-list order directives. KEYWORD(uselistorder); KEYWORD(uselistorder_bb); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp index 232c580b88f..bcb314dfbd7 100644 --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -618,8 +618,9 @@ bool LLParser::ParseStandaloneMetadata() { if (Lex.getKind() == lltok::Type) return TokError("unexpected type in metadata definition"); + bool IsDistinct = EatIfPresent(lltok::kw_distinct); if (ParseToken(lltok::exclaim, "Expected '!' here") || - ParseMDNode(Init)) + ParseMDNode(Init, IsDistinct)) return true; // See if this was forward referenced, if so, handle it. @@ -2945,12 +2946,15 @@ bool LLParser::ParseGlobalValueVector(SmallVectorImpl &Elts) { return false; } -bool LLParser::ParseMDNode(MDNode *&MD) { +bool LLParser::ParseMDNode(MDNode *&MD, bool IsDistinct) { SmallVector Elts; if (ParseMDNodeVector(Elts)) return true; - MD = MDNode::get(Context, Elts); + if (IsDistinct) + MD = MDNode::getDistinct(Context, Elts); + else + MD = MDNode::get(Context, Elts); return false; } diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h index d8d272b500d..f1df5b8e54b 100644 --- a/llvm/lib/AsmParser/LLParser.h +++ b/llvm/lib/AsmParser/LLParser.h @@ -389,7 +389,7 @@ namespace llvm { bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS); bool ParseValueAsMetadata(Metadata *&MD, PerFunctionState *PFS); bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS); - bool ParseMDNode(MDNode *&MD); + bool ParseMDNode(MDNode *&MD, bool IsDistinct = false); bool ParseMDNodeVector(SmallVectorImpl &MDs); bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h index a2111ca96e9..4fb01824d9b 100644 --- a/llvm/lib/AsmParser/LLToken.h +++ b/llvm/lib/AsmParser/LLToken.h @@ -182,6 +182,9 @@ namespace lltok { kw_extractelement, kw_insertelement, kw_shufflevector, kw_extractvalue, kw_insertvalue, kw_blockaddress, + // Metadata types. + kw_distinct, + // Use-list order directives. kw_uselistorder, kw_uselistorder_bb, diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 7c7eebde1df..1792f8b8dd4 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1106,6 +1106,7 @@ std::error_code BitcodeReader::ParseMetadata() { // Read a record. Record.clear(); unsigned Code = Stream.readRecord(Entry.ID, Record); + bool IsDistinct = false; switch (Code) { default: // Default behavior: ignore. break; @@ -1196,12 +1197,17 @@ std::error_code BitcodeReader::ParseMetadata() { NextMDValueNo++); break; } + case bitc::METADATA_DISTINCT_NODE: + IsDistinct = true; + // fallthrough... case bitc::METADATA_NODE: { SmallVector Elts; Elts.reserve(Record.size()); for (unsigned ID : Record) Elts.push_back(ID ? MDValueList.getValueFwdRef(ID - 1) : nullptr); - MDValueList.AssignValue(MDNode::get(Context, Elts), NextMDValueNo++); + MDValueList.AssignValue(IsDistinct ? MDNode::getDistinct(Context, Elts) + : MDNode::get(Context, Elts), + NextMDValueNo++); break; } case bitc::METADATA_STRING: { diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index 2eb67219c8a..8152799bbce 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -773,7 +773,9 @@ static void WriteMDNode(const MDNode *N, assert(!isa(MD) && "Unexpected function-local metadata"); Record.push_back(VE.getMetadataID(MD) + 1); } - Stream.EmitRecord(bitc::METADATA_NODE, Record); + Stream.EmitRecord(N->isDistinct() ? bitc::METADATA_DISTINCT_NODE + : bitc::METADATA_NODE, + Record); Record.clear(); } diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index 175e20f9794..dc2c5934c2a 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -1253,6 +1253,8 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, TypePrinting *TypePrinter, SlotTracker *Machine, const Module *Context) { + if (Node->isDistinct()) + Out << "distinct "; Out << "!{"; for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { const Metadata *MD = Node->getOperand(mi); -- cgit v1.2.3