[llvm-commits] [llvm] r77517 - in /llvm/trunk: include/llvm/Bitcode/LLVMBitCodes.h lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Writer/BitcodeWriter.cpp lib/Bitcode/Writer/ValueEnumerator.cpp test/Feature/NamedMDNode.ll
Devang Patel
dpatel at apple.com
Wed Jul 29 15:34:42 PDT 2009
Author: dpatel
Date: Wed Jul 29 17:34:41 2009
New Revision: 77517
URL: http://llvm.org/viewvc/llvm-project?rev=77517&view=rev
Log:
Read and write NamedMDNode.
Added:
llvm/trunk/test/Feature/NamedMDNode.ll
Modified:
llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
Modified: llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h?rev=77517&r1=77516&r2=77517&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h (original)
+++ llvm/trunk/include/llvm/Bitcode/LLVMBitCodes.h Wed Jul 29 17:34:41 2009
@@ -108,8 +108,10 @@
};
enum MetadataCodes {
- METADATA_STRING = 1, // MDString: [values]
- METADATA_NODE = 2 // MDNODE: [n x (type num, value num)]
+ METADATA_STRING = 1, // MDSTRING: [values]
+ METADATA_NODE = 2, // MDNODE: [n x (type num, value num)]
+ METADATA_NAME = 3, // STRING: [values]
+ METADATA_NAMED_NODE = 4 // NAMEDMDNODE: [n x mdnodes]
};
// The constants block (CONSTANTS_BLOCK_ID) describes emission for each
// constant and maintains an implicit current type value.
Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=77517&r1=77516&r2=77517&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Wed Jul 29 17:34:41 2009
@@ -734,6 +734,33 @@
switch (Stream.ReadRecord(Code, Record)) {
default: // Default behavior: ignore.
break;
+ case bitc::METADATA_NAME: {
+ // Read named of the named metadata.
+ unsigned NameLength = Record.size();
+ SmallString<8> Name;
+ Name.resize(NameLength);
+ for (unsigned i = 0; i != NameLength; ++i)
+ Name[i] = Record[i];
+ Record.clear();
+ Code = Stream.ReadCode();
+
+ // METADATA_NAME is always followed by METADATA_NAMED_NODE.
+ if (Stream.ReadRecord(Code, Record) != bitc::METADATA_NAMED_NODE)
+ assert ( 0 && "Inavlid Named Metadata record");
+
+ // Read named metadata elements.
+ unsigned Size = Record.size();
+ SmallVector<MetadataBase*, 8> Elts;
+ for (unsigned i = 0; i != Size; ++i) {
+ Value *MD = ValueList.getValueFwdRef(Record[i], Type::MetadataTy);
+ if (MetadataBase *B = dyn_cast<MetadataBase>(MD))
+ Elts.push_back(B);
+ }
+ Value *V = NamedMDNode::Create(Name.c_str(), Elts.data(), Elts.size(),
+ TheModule);
+ ValueList.AssignValue(V, NextValueNo++);
+ break;
+ }
case bitc::METADATA_NODE: {
if (Record.empty() || Record.size() % 2 == 1)
return Error("Invalid METADATA_NODE record");
Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=77517&r1=77516&r2=77517&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Wed Jul 29 17:34:41 2009
@@ -495,6 +495,7 @@
const ValueEnumerator::ValueList &Vals = VE.getValues();
bool StartedMetadataBlock = false;
unsigned MDSAbbrev = 0;
+ unsigned String8Abbrev = 0;
SmallVector<uint64_t, 64> Record;
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
@@ -525,6 +526,33 @@
// Emit the finished record.
Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
Record.clear();
+ } else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) {
+ if (!StartedMetadataBlock) {
+ Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
+ StartedMetadataBlock = true;
+ BitCodeAbbrev *Abbv = new BitCodeAbbrev();
+ Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
+ Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
+ String8Abbrev = Stream.EmitAbbrev(Abbv);
+ }
+
+ // Write name.
+ const char *StrBegin = NMD->getName().data();
+ for (unsigned i = 0, e = NMD->getName().size(); i != e; ++i)
+ Record.push_back(StrBegin[i]);
+ Stream.EmitRecord(bitc::METADATA_NAME, Record, String8Abbrev);
+ Record.clear();
+
+ // Write named metadata elements.
+ for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
+ if (NMD->getElement(i))
+ Record.push_back(VE.getValueID(NMD->getElement(i)));
+ else
+ Record.push_back(0);
+ }
+ Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
+ Record.clear();
}
}
@@ -578,7 +606,7 @@
const Type *LastTy = 0;
for (unsigned i = FirstVal; i != LastVal; ++i) {
const Value *V = Vals[i].first;
- if (isa<MDString>(V) || isa<MDNode>(V))
+ if (isa<MetadataBase>(V))
continue;
// If we need to switch types, do so now.
if (V->getType() != LastTy) {
Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=77517&r1=77516&r2=77517&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Wed Jul 29 17:34:41 2009
@@ -221,6 +221,18 @@
return;
}
+ if (const NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
+ Values.push_back(std::make_pair(V, 1U));
+ ValueMap[V] = Values.size();
+ ValueID = Values.size();
+ for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
+ E = N->elem_end(); I != E; ++I) {
+ MetadataBase *M = *I;
+ EnumerateValue(M);
+ }
+ return;
+ }
+
// Add the value.
Values.push_back(std::make_pair(V, 1U));
ValueID = Values.size();
Added: llvm/trunk/test/Feature/NamedMDNode.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Feature/NamedMDNode.ll?rev=77517&view=auto
==============================================================================
--- llvm/trunk/test/Feature/NamedMDNode.ll (added)
+++ llvm/trunk/test/Feature/NamedMDNode.ll Wed Jul 29 17:34:41 2009
@@ -0,0 +1,6 @@
+; RUN: llvm-as < %s | llvm-dis | grep "llvm.stuff = "
+
+;; Simple NamedMDNode
+!0 = metadata !{i32 42}
+!1 = metadata !{metadata !"foo"}
+!llvm.stuff = !{!0, !1}
More information about the llvm-commits
mailing list