Constants and Metadata share ValueList. This means they must be emitted interleaved (using appropriate BLOCK_IDs) otherwise ValuePtrs index gets out of sync.

llvm-svn: 78033
This commit is contained in:
Devang Patel 2009-08-04 02:26:56 +00:00
parent f43cf709cb
commit 8abe6bc291
2 changed files with 91 additions and 92 deletions

View File

@ -473,50 +473,48 @@ static uint64_t GetOptimizationFlags(const Value *V) {
return Flags; return Flags;
} }
static void WriteMDNode(const MDNode *N, /// WriteValues - Write Constants and Metadata.
/// This function could use some refactoring help.
static void WriteValues(unsigned FirstVal, unsigned LastVal,
const ValueEnumerator &VE, const ValueEnumerator &VE,
BitstreamWriter &Stream, BitstreamWriter &Stream, bool isGlobal) {
SmallVector<uint64_t, 64> &Record) { if (FirstVal == LastVal) return;
for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
if (N->getElement(i)) {
Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
Record.push_back(VE.getValueID(N->getElement(i)));
} else {
Record.push_back(VE.getTypeID(Type::VoidTy));
Record.push_back(0);
}
}
Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
Record.clear();
}
static void WriteModuleMetadata(const ValueEnumerator &VE, // MODULE_BLOCK_ID is 0, which is not handled here. So it is OK to use
BitstreamWriter &Stream) { // 0 as the initializer to indicate that block is not set.
const ValueEnumerator::ValueList &Vals = VE.getValues(); enum bitc::BlockIDs LastBlockID = bitc::MODULE_BLOCK_ID;
bool StartedMetadataBlock = false;
unsigned AggregateAbbrev = 0;
unsigned String8Abbrev = 0;
unsigned CString7Abbrev = 0;
unsigned CString6Abbrev = 0;
unsigned MDSAbbrev = 0; unsigned MDSAbbrev = 0;
SmallVector<uint64_t, 64> Record; SmallVector<uint64_t, 64> Record;
for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
const ValueEnumerator::ValueList &Vals = VE.getValues();
if (const MDNode *N = dyn_cast<MDNode>(Vals[i].first)) { const Type *LastTy = 0;
if (!StartedMetadataBlock) { for (unsigned i = FirstVal; i != LastVal; ++i) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); const Value *V = Vals[i].first;
StartedMetadataBlock = true; if (isa<MetadataBase>(V)) {
} if (LastBlockID != bitc::METADATA_BLOCK_ID) {
WriteMDNode(N, VE, Stream, Record); // Exit privious block.
} else if (const MDString *MDS = dyn_cast<MDString>(Vals[i].first)) { if (LastBlockID != bitc::MODULE_BLOCK_ID)
if (!StartedMetadataBlock) { Stream.ExitBlock();
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
LastBlockID = bitc::METADATA_BLOCK_ID;
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3);
}
}
if (const MDString *MDS = dyn_cast<MDString>(V)) {
if (MDSAbbrev == 0) {
// Abbrev for METADATA_STRING. // Abbrev for METADATA_STRING.
BitCodeAbbrev *Abbv = new BitCodeAbbrev(); BitCodeAbbrev *Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING)); Abbv->Add(BitCodeAbbrevOp(bitc::METADATA_STRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8));
MDSAbbrev = Stream.EmitAbbrev(Abbv); MDSAbbrev = Stream.EmitAbbrev(Abbv);
StartedMetadataBlock = true;
} }
// Code: [strchar x N] // Code: [strchar x N]
const char *StrBegin = MDS->begin(); const char *StrBegin = MDS->begin();
for (unsigned i = 0, e = MDS->length(); i != e; ++i) for (unsigned i = 0, e = MDS->length(); i != e; ++i)
@ -525,12 +523,21 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
// Emit the finished record. // Emit the finished record.
Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev); Stream.EmitRecord(bitc::METADATA_STRING, Record, MDSAbbrev);
Record.clear(); Record.clear();
} else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(Vals[i].first)) { continue;
if (!StartedMetadataBlock) { } else if (const MDNode *N = dyn_cast<MDNode>(V)) {
Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
StartedMetadataBlock = true; if (N->getElement(i)) {
Record.push_back(VE.getTypeID(N->getElement(i)->getType()));
Record.push_back(VE.getValueID(N->getElement(i)));
} else {
Record.push_back(VE.getTypeID(Type::VoidTy));
Record.push_back(0);
}
} }
Stream.EmitRecord(bitc::METADATA_NODE, Record, 0);
Record.clear();
continue;
} else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V)) {
// Write name. // Write name.
std::string Str = NMD->getNameStr(); std::string Str = NMD->getNameStr();
const char *StrBegin = Str.c_str(); const char *StrBegin = Str.c_str();
@ -538,7 +545,7 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
Record.push_back(StrBegin[i]); Record.push_back(StrBegin[i]);
Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/); Stream.EmitRecord(bitc::METADATA_NAME, Record, 0/*TODO*/);
Record.clear(); Record.clear();
// Write named metadata elements. // Write named metadata elements.
for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) { for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
if (NMD->getElement(i)) if (NMD->getElement(i))
@ -548,59 +555,49 @@ static void WriteModuleMetadata(const ValueEnumerator &VE,
} }
Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0); Stream.EmitRecord(bitc::METADATA_NAMED_NODE, Record, 0);
Record.clear(); Record.clear();
continue;
} }
}
if (StartedMetadataBlock)
Stream.ExitBlock();
}
static void WriteConstants(unsigned FirstVal, unsigned LastVal, // If we need to switch block, do so now.
const ValueEnumerator &VE, if (LastBlockID != bitc::CONSTANTS_BLOCK_ID) {
BitstreamWriter &Stream, bool isGlobal) { // Exit privious block.
if (FirstVal == LastVal) return; if (LastBlockID != bitc::MODULE_BLOCK_ID)
Stream.ExitBlock();
Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
unsigned AggregateAbbrev = 0; LastBlockID = bitc::CONSTANTS_BLOCK_ID;
unsigned String8Abbrev = 0; Stream.EnterSubblock(bitc::CONSTANTS_BLOCK_ID, 4);
unsigned CString7Abbrev = 0; // If this is a constant pool for the module, emit module-specific abbrevs.
unsigned CString6Abbrev = 0; if (isGlobal) {
// If this is a constant pool for the module, emit module-specific abbrevs. // Abbrev for CST_CODE_AGGREGATE.
if (isGlobal) { BitCodeAbbrev *Abbv = new BitCodeAbbrev();
// Abbrev for CST_CODE_AGGREGATE. Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE));
BitCodeAbbrev *Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_AGGREGATE)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); AggregateAbbrev = Stream.EmitAbbrev(Abbv);
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, Log2_32_Ceil(LastVal+1)));
AggregateAbbrev = Stream.EmitAbbrev(Abbv); // Abbrev for CST_CODE_STRING.
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);
// Abbrev for CST_CODE_STRING. // Abbrev for CST_CODE_CSTRING.
Abbv = new BitCodeAbbrev(); Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_STRING)); Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
String8Abbrev = Stream.EmitAbbrev(Abbv); CString7Abbrev = Stream.EmitAbbrev(Abbv);
// Abbrev for CST_CODE_CSTRING.
Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));
CString7Abbrev = Stream.EmitAbbrev(Abbv);
// Abbrev for CST_CODE_CSTRING.
Abbv = new BitCodeAbbrev();
Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
CString6Abbrev = Stream.EmitAbbrev(Abbv);
}
SmallVector<uint64_t, 64> Record;
const ValueEnumerator::ValueList &Vals = VE.getValues(); // Abbrev for CST_CODE_CSTRING.
const Type *LastTy = 0; Abbv = new BitCodeAbbrev();
for (unsigned i = FirstVal; i != LastVal; ++i) { Abbv->Add(BitCodeAbbrevOp(bitc::CST_CODE_CSTRING));
const Value *V = Vals[i].first; Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Char6));
CString6Abbrev = Stream.EmitAbbrev(Abbv);
}
}
if (isa<MetadataBase>(V)) if (isa<MetadataBase>(V))
continue; continue;
// If we need to switch types, do so now. // If we need to switch types, do so now.
@ -802,7 +799,7 @@ static void WriteModuleConstants(const ValueEnumerator &VE,
// We know globalvalues have been emitted by WriteModuleInfo. // We know globalvalues have been emitted by WriteModuleInfo.
for (unsigned i = 0, e = Vals.size(); i != e; ++i) { for (unsigned i = 0, e = Vals.size(); i != e; ++i) {
if (!isa<GlobalValue>(Vals[i].first)) { if (!isa<GlobalValue>(Vals[i].first)) {
WriteConstants(i, Vals.size(), VE, Stream, true); WriteValues(i, Vals.size(), VE, Stream, true);
return; return;
} }
} }
@ -1131,7 +1128,7 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE,
// If there are function-local constants, emit them now. // If there are function-local constants, emit them now.
unsigned CstStart, CstEnd; unsigned CstStart, CstEnd;
VE.getFunctionConstantRange(CstStart, CstEnd); VE.getFunctionConstantRange(CstStart, CstEnd);
WriteConstants(CstStart, CstEnd, VE, Stream, false); WriteValues(CstStart, CstEnd, VE, Stream, false);
// Keep a running idea of what the instruction ID is. // Keep a running idea of what the instruction ID is.
unsigned InstID = CstEnd; unsigned InstID = CstEnd;
@ -1384,9 +1381,6 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
// Emit constants. // Emit constants.
WriteModuleConstants(VE, Stream); WriteModuleConstants(VE, Stream);
// Emit metadata.
WriteModuleMetadata(VE, Stream);
// Emit function bodies. // Emit function bodies.
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
if (!I->isDeclaration()) if (!I->isDeclaration())
@ -1397,7 +1391,7 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
// Emit names for globals/functions etc. // Emit names for globals/functions etc.
WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream); WriteValueSymbolTable(M->getValueSymbolTable(), VE, Stream);
Stream.ExitBlock(); Stream.ExitBlock();
} }

View File

@ -0,0 +1,5 @@
; RUN: llvm-as < %s | llvm-dis -f -o /dev/null
!llvm.foo = !{!0}
!0 = metadata !{i32 42}
@my.str = internal constant [4 x i8] c"foo\00"