This is a major cleanup of the instruction metadata interfaces that
I asked Devang to do back on Sep 27. Instead of going through the
MetadataContext class with methods like getMD() and getMDs(), just
ask the instruction directly for its metadata with getMetadata()
and getAllMetadata().
This includes a variety of other fixes and improvements: previously
all Value*'s were bloated because the HasMetadata bit was thrown into
value, adding a 9th bit to a byte. Now this is properly sunk down to
the Instruction class (the only place where it makes sense) and it
will be folded away somewhere soon.
This also fixes some confusion in getMDs and its clients about
whether the returned list is indexed by the MDID or densely packed.
This is now returned sorted and densely packed and the comments make
this clear.
This introduces a number of fixme's which I'll follow up on.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92235 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 0bda03e..568968d 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -18,7 +18,6 @@
#include "llvm/InlineAsm.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
-#include "llvm/Metadata.h"
#include "llvm/Module.h"
#include "llvm/Operator.h"
#include "llvm/AutoUpgrade.h"
@@ -1573,7 +1572,6 @@
if (Stream.EnterSubBlock(bitc::METADATA_ATTACHMENT_ID))
return Error("Malformed block record");
- MetadataContext &TheMetadata = Context.getMetadata();
SmallVector<uint64_t, 64> Record;
while(1) {
unsigned Code = Stream.ReadCode();
@@ -1599,7 +1597,7 @@
for (unsigned i = 1; i != RecordLength; i = i+2) {
unsigned Kind = Record[i];
Value *Node = MDValueList.getValueFwdRef(Record[i+1]);
- TheMetadata.addMD(Kind, cast<MDNode>(Node), Inst);
+ Inst->setMetadata(Kind, cast<MDNode>(Node));
}
break;
}
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index cf8f767..21548cd 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -561,32 +561,29 @@
// Write metadata attachments
// METADATA_ATTACHMENT - [m x [value, [n x [id, mdnode]]]
- MetadataContext &TheMetadata = F.getContext().getMetadata();
- typedef SmallVector<std::pair<unsigned, MDNode*>, 2> MDMapTy;
- MDMapTy MDs;
+ SmallVector<std::pair<unsigned, MDNode*>, 4> MDs;
+
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
I != E; ++I) {
MDs.clear();
- TheMetadata.getMDs(I, MDs);
- bool RecordedInstruction = false;
- for (MDMapTy::const_iterator PI = MDs.begin(), PE = MDs.end();
- PI != PE; ++PI) {
- if (RecordedInstruction == false) {
- Record.push_back(VE.getInstructionID(I));
- RecordedInstruction = true;
- }
- Record.push_back(PI->first);
- Record.push_back(VE.getValueID(PI->second));
+ I->getAllMetadata(MDs);
+
+ // If no metadata, ignore instruction.
+ if (MDs.empty()) continue;
+
+ Record.push_back(VE.getInstructionID(I));
+
+ for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
+ Record.push_back(MDs[i].first);
+ Record.push_back(VE.getValueID(MDs[i].second));
}
- if (!Record.empty()) {
- if (!StartedMetadataBlock) {
- Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
- StartedMetadataBlock = true;
- }
- Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
- Record.clear();
+ if (!StartedMetadataBlock) {
+ Stream.EnterSubblock(bitc::METADATA_ATTACHMENT_ID, 3);
+ StartedMetadataBlock = true;
}
+ Stream.EmitRecord(bitc::METADATA_ATTACHMENT, Record, 0);
+ Record.clear();
}
if (StartedMetadataBlock)
@@ -1208,7 +1205,7 @@
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
I != E; ++I) {
WriteInstruction(*I, InstID, VE, Stream, Vals);
- if (I->getType() != Type::getVoidTy(F.getContext()))
+ if (!I->getType()->isVoidTy())
++InstID;
}
diff --git a/lib/Bitcode/Writer/ValueEnumerator.cpp b/lib/Bitcode/Writer/ValueEnumerator.cpp
index 29c6d37..a6da44f 100644
--- a/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -14,8 +14,6 @@
#include "ValueEnumerator.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Metadata.h"
#include "llvm/Module.h"
#include "llvm/TypeSymbolTable.h"
#include "llvm/ValueSymbolTable.h"
@@ -87,9 +85,7 @@
I != E; ++I)
EnumerateType(I->getType());
- MetadataContext &TheMetadata = F->getContext().getMetadata();
- typedef SmallVector<std::pair<unsigned, MDNode*>, 2> MDMapTy;
- MDMapTy MDs;
+ SmallVector<std::pair<unsigned, MDNode*>, 2> MDs;
for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
@@ -103,10 +99,9 @@
// Enumerate metadata attached with this instruction.
MDs.clear();
- TheMetadata.getMDs(I, MDs);
- for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
- ++MI)
- EnumerateMetadata(MI->second);
+ I->getAllMetadata(MDs);
+ for (unsigned i = 0, e = MDs.size(); i != e; ++i)
+ EnumerateMetadata(MDs[i].second);
}
}