aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bitcode
diff options
context:
space:
mode:
authorDevang Patel <dpatel@apple.com>2009-07-29 22:34:41 +0000
committerDevang Patel <dpatel@apple.com>2009-07-29 22:34:41 +0000
commit3e1ef939fd4605caa628ad50c7530b2225ad1841 (patch)
tree66b8db375a3310bedc27056dba65816ed7d9ac26 /lib/Bitcode
parent6b6e2d9392782724c48110bcca4bee2c8fbbd7b9 (diff)
downloadexternal_llvm-3e1ef939fd4605caa628ad50c7530b2225ad1841.zip
external_llvm-3e1ef939fd4605caa628ad50c7530b2225ad1841.tar.gz
external_llvm-3e1ef939fd4605caa628ad50c7530b2225ad1841.tar.bz2
Read and write NamedMDNode.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77517 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bitcode')
-rw-r--r--lib/Bitcode/Reader/BitcodeReader.cpp27
-rw-r--r--lib/Bitcode/Writer/BitcodeWriter.cpp30
-rw-r--r--lib/Bitcode/Writer/ValueEnumerator.cpp12
3 files changed, 68 insertions, 1 deletions
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 1e6f5b6..35fe7b2 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -734,6 +734,33 @@ bool BitcodeReader::ParseMetadata() {
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");
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index a41b810..ff47cf4 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -495,6 +495,7 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
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 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
// 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 @@ static void WriteConstants(unsigned FirstVal, unsigned LastVal,
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) {
diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp
index 302791a..a980e90 100644
--- a/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -221,6 +221,18 @@ void ValueEnumerator::EnumerateValue(const Value *V) {
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();