Use separate ValueList for metadata.

This fixes PR4666.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78056 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2009-08-04 06:00:18 +00:00
parent 3b24c01720
commit d5ac40457b
7 changed files with 242 additions and 44 deletions

View File

@ -34,6 +34,7 @@ void BitcodeReader::FreeState() {
Buffer = 0;
std::vector<PATypeHolder>().swap(TypeList);
ValueList.clear();
MDValueList.clear();
std::vector<AttrListPtr>().swap(MAttributes);
std::vector<BasicBlock*>().swap(FunctionBBs);
@ -312,6 +313,41 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
}
}
void BitcodeReaderMDValueList::AssignValue(Value *V, unsigned Idx) {
if (Idx == size()) {
push_back(V);
return;
}
if (Idx >= size())
resize(Idx+1);
WeakVH &OldV = MDValuePtrs[Idx];
if (OldV == 0) {
OldV = V;
return;
}
// If there was a forward reference to this value, replace it.
Value *PrevVal = OldV;
OldV->replaceAllUsesWith(V);
delete PrevVal;
}
Value *BitcodeReaderMDValueList::getValueFwdRef(unsigned Idx) {
if (Idx >= size())
resize(Idx + 1);
if (Value *V = MDValuePtrs[Idx]) {
assert(V->getType() == Type::MetadataTy && "Type mismatch in value table!");
return V;
}
// Create and return a placeholder, which will later be RAUW'd.
Value *V = new Argument(Type::MetadataTy);
MDValuePtrs[Idx] = V;
return V;
}
const Type *BitcodeReader::getTypeByID(unsigned ID, bool isTypeTable) {
// If the TypeID is in range, return it.
@ -700,7 +736,7 @@ bool BitcodeReader::ParseValueSymbolTable() {
}
bool BitcodeReader::ParseMetadata() {
unsigned NextValueNo = ValueList.size();
unsigned NextValueNo = MDValueList.size();
if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
return Error("Malformed block record");
@ -752,13 +788,13 @@ bool BitcodeReader::ParseMetadata() {
unsigned Size = Record.size();
SmallVector<MetadataBase*, 8> Elts;
for (unsigned i = 0; i != Size; ++i) {
Value *MD = ValueList.getValueFwdRef(Record[i], Type::MetadataTy);
Value *MD = MDValueList.getValueFwdRef(Record[i]);
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++);
MDValueList.AssignValue(V, NextValueNo++);
break;
}
case bitc::METADATA_NODE: {
@ -769,13 +805,15 @@ bool BitcodeReader::ParseMetadata() {
SmallVector<Value*, 8> Elts;
for (unsigned i = 0; i != Size; i += 2) {
const Type *Ty = getTypeByID(Record[i], false);
if (Ty != Type::VoidTy)
if (Ty == Type::MetadataTy)
Elts.push_back(MDValueList.getValueFwdRef(Record[i+1]));
else if (Ty != Type::VoidTy)
Elts.push_back(ValueList.getValueFwdRef(Record[i+1], Ty));
else
Elts.push_back(NULL);
}
Value *V = MDNode::get(Context, &Elts[0], Elts.size());
ValueList.AssignValue(V, NextValueNo++);
MDValueList.AssignValue(V, NextValueNo++);
break;
}
case bitc::METADATA_STRING: {
@ -786,7 +824,7 @@ bool BitcodeReader::ParseMetadata() {
String[i] = Record[i];
Value *V = MDString::get(Context,
StringRef(String.data(), String.size()));
ValueList.AssignValue(V, NextValueNo++);
MDValueList.AssignValue(V, NextValueNo++);
break;
}
}

View File

@ -86,6 +86,41 @@ public:
void ResolveConstantForwardRefs();
};
//===----------------------------------------------------------------------===//
// BitcodeReaderMDValueList Class
//===----------------------------------------------------------------------===//
class BitcodeReaderMDValueList {
std::vector<WeakVH> MDValuePtrs;
LLVMContext& Context;
public:
BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
// vector compatibility methods
unsigned size() const { return MDValuePtrs.size(); }
void resize(unsigned N) { MDValuePtrs.resize(N); }
void push_back(Value *V) { MDValuePtrs.push_back(V); }
void clear() { MDValuePtrs.clear(); }
Value *back() const { return MDValuePtrs.back(); }
void pop_back() { MDValuePtrs.pop_back(); }
bool empty() const { return MDValuePtrs.empty(); }
Value *operator[](unsigned i) const {
assert(i < MDValuePtrs.size());
return MDValuePtrs[i];
}
void shrinkTo(unsigned N) {
assert(N <= size() && "Invalid shrinkTo request!");
MDValuePtrs.resize(N);
}
Value *getValueFwdRef(unsigned Idx);
void AssignValue(Value *V, unsigned Idx);
};
class BitcodeReader : public ModuleProvider {
LLVMContext& Context;
MemoryBuffer *Buffer;
@ -96,6 +131,7 @@ class BitcodeReader : public ModuleProvider {
std::vector<PATypeHolder> TypeList;
BitcodeReaderValueList ValueList;
BitcodeReaderMDValueList MDValueList;
std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
@ -127,7 +163,7 @@ class BitcodeReader : public ModuleProvider {
DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
public:
explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
: Context(C), Buffer(buffer), ErrorString(0), ValueList(C) {
: Context(C), Buffer(buffer), ErrorString(0), ValueList(C), MDValueList(C) {
HasReversedFunctionsWithBodies = false;
}
~BitcodeReader() {
@ -160,7 +196,10 @@ public:
private:
const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Value *getFnValueByID(unsigned ID, const Type *Ty) {
return ValueList.getValueFwdRef(ID, Ty);
if (Ty == Type::MetadataTy)
return MDValueList.getValueFwdRef(ID);
else
return ValueList.getValueFwdRef(ID, Ty);
}
BasicBlock *getBasicBlock(unsigned ID) const {
if (ID >= FunctionBBs.size()) return 0; // Invalid ID

View File

@ -492,7 +492,7 @@ static void WriteMDNode(const MDNode *N,
static void WriteModuleMetadata(const ValueEnumerator &VE,
BitstreamWriter &Stream) {
const ValueEnumerator::ValueList &Vals = VE.getValues();
const ValueEnumerator::ValueList &Vals = VE.getMDValues();
bool StartedMetadataBlock = false;
unsigned MDSAbbrev = 0;
SmallVector<uint64_t, 64> Record;
@ -601,8 +601,6 @@ 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<MetadataBase>(V))
continue;
// If we need to switch types, do so now.
if (V->getType() != LastTy) {
LastTy = V->getType();

View File

@ -114,6 +114,18 @@ ValueEnumerator::ValueEnumerator(const Module *M) {
TypeMap[Types[i].first] = i+1;
}
unsigned ValueEnumerator::getValueID(const Value *V) const {
if (isa<MetadataBase>(V)) {
ValueMapType::const_iterator I = MDValueMap.find(V);
assert(I != MDValueMap.end() && "Value not in slotcalculator!");
return I->second-1;
}
ValueMapType::const_iterator I = ValueMap.find(V);
assert(I != ValueMap.end() && "Value not in slotcalculator!");
return I->second-1;
}
// Optimize constant ordering.
namespace {
struct CstSortPredicate {
@ -165,9 +177,51 @@ void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
EnumerateValue(VI->getValue());
}
void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
// Check to see if it's already in!
unsigned &MDValueID = MDValueMap[MD];
if (MDValueID) {
// Increment use count.
MDValues[MDValueID-1].second++;
return;
}
// Enumerate the type of this value.
EnumerateType(MD->getType());
if (const MDNode *N = dyn_cast<MDNode>(MD)) {
MDValues.push_back(std::make_pair(MD, 1U));
MDValueMap[MD] = MDValues.size();
MDValueID = MDValues.size();
for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
I != E; ++I) {
if (*I)
EnumerateValue(*I);
else
EnumerateType(Type::VoidTy);
}
return;
} else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
E = N->elem_end(); I != E; ++I) {
MetadataBase *M = *I;
EnumerateValue(M);
}
MDValues.push_back(std::make_pair(MD, 1U));
MDValueMap[MD] = Values.size();
return;
}
// Add the value.
MDValues.push_back(std::make_pair(MD, 1U));
MDValueID = MDValues.size();
}
void ValueEnumerator::EnumerateValue(const Value *V) {
assert(V->getType() != Type::VoidTy && "Can't insert void values!");
if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
return EnumerateMetadata(MB);
// Check to see if it's already in!
unsigned &ValueID = ValueMap[V];
if (ValueID) {
@ -207,31 +261,6 @@ void ValueEnumerator::EnumerateValue(const Value *V) {
}
}
if (const MDNode *N = dyn_cast<MDNode>(V)) {
Values.push_back(std::make_pair(V, 1U));
ValueMap[V] = Values.size();
ValueID = Values.size();
for (MDNode::const_elem_iterator I = N->elem_begin(), E = N->elem_end();
I != E; ++I) {
if (*I)
EnumerateValue(*I);
else
EnumerateType(Type::VoidTy);
}
return;
}
if (const NamedMDNode *N = dyn_cast<NamedMDNode>(V)) {
for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
E = N->elem_end(); I != E; ++I) {
MetadataBase *M = *I;
EnumerateValue(M);
}
Values.push_back(std::make_pair(V, 1U));
ValueMap[V] = Values.size();
return;
}
// Add the value.
Values.push_back(std::make_pair(V, 1U));
ValueID = Values.size();

View File

@ -25,6 +25,7 @@ class Value;
class BasicBlock;
class Function;
class Module;
class MetadataBase;
class AttrListPtr;
class TypeSymbolTable;
class ValueSymbolTable;
@ -44,7 +45,9 @@ private:
typedef DenseMap<const Value*, unsigned> ValueMapType;
ValueMapType ValueMap;
ValueList Values;
ValueList MDValues;
ValueMapType MDValueMap;
typedef DenseMap<void*, unsigned> AttributeMapType;
AttributeMapType AttributeMap;
std::vector<AttrListPtr> Attributes;
@ -64,12 +67,8 @@ private:
public:
ValueEnumerator(const Module *M);
unsigned getValueID(const Value *V) const {
ValueMapType::const_iterator I = ValueMap.find(V);
assert(I != ValueMap.end() && "Value not in slotcalculator!");
return I->second-1;
}
unsigned getValueID(const Value *V) const;
unsigned getTypeID(const Type *T) const {
TypeMapType::const_iterator I = TypeMap.find(T);
assert(I != TypeMap.end() && "Type not in ValueEnumerator!");
@ -91,6 +90,7 @@ public:
}
const ValueList &getValues() const { return Values; }
const ValueList &getMDValues() const { return MDValues; }
const TypeList &getTypes() const { return Types; }
const std::vector<const BasicBlock*> &getBasicBlocks() const {
return BasicBlocks;
@ -108,6 +108,7 @@ public:
private:
void OptimizeConstants(unsigned CstStart, unsigned CstEnd);
void EnumerateMetadata(const MetadataBase *MD);
void EnumerateValue(const Value *V);
void EnumerateType(const Type *T);
void EnumerateOperandType(const Value *V);

View File

@ -0,0 +1,87 @@
; RUN: llvm-as < %s | llvm-dis -f -o /dev/null
type { %object.ModuleInfo.__vtbl*, i8*, %"byte[]", %1, %"ClassInfo[]", i32, void ()*, void ()*, void ()*, i8*, void ()* } ; type %0
type { i64, %object.ModuleInfo* } ; type %1
type { i32, void ()* } ; type %2
%"ClassInfo[]" = type { i64, %object.ClassInfo** }
%"Interface[]" = type { i64, %object.Interface* }
%"ModuleInfo[]" = type { i64, %object.ModuleInfo** }
%ModuleReference = type { %ModuleReference*, %object.ModuleInfo* }
%"OffsetTypeInfo[]" = type { i64, %object.OffsetTypeInfo* }
%"byte[]" = type { i64, i8* }
%object.ClassInfo = type { %object.ClassInfo.__vtbl*, i8*, %"byte[]", %"byte[]", %"void*[]", %"Interface[]", %object.ClassInfo*, i8*, i8*, i32, i8*, %"OffsetTypeInfo[]", i8*, %object.TypeInfo* }
%object.ClassInfo.__vtbl = type { %object.ClassInfo*, %"byte[]" (%object.Object*)*, i64 (%object.Object*)*, i32 (%object.Object*, %object.Object*)*, i32 (%object.Object*, %object.Object*)*, %object.Object* (%object.ClassInfo*)* }
%object.Interface = type { %object.ClassInfo*, %"void*[]", i64 }
%object.ModuleInfo = type { %object.ModuleInfo.__vtbl*, i8*, %"byte[]", %"ModuleInfo[]", %"ClassInfo[]", i32, void ()*, void ()*, void ()*, i8*, void ()* }
%object.ModuleInfo.__vtbl = type { %object.ClassInfo*, %"byte[]" (%object.Object*)*, i64 (%object.Object*)*, i32 (%object.Object*, %object.Object*)*, i32 (%object.Object*, %object.Object*)* }
%object.Object = type { %object.ModuleInfo.__vtbl*, i8* }
%object.OffsetTypeInfo = type { i64, %object.TypeInfo* }
%object.TypeInfo = type { %object.TypeInfo.__vtbl*, i8* }
%object.TypeInfo.__vtbl = type { %object.ClassInfo*, %"byte[]" (%object.Object*)*, i64 (%object.Object*)*, i32 (%object.Object*, %object.Object*)*, i32 (%object.Object*, %object.Object*)*, i64 (%object.TypeInfo*, i8*)*, i32 (%object.TypeInfo*, i8*, i8*)*, i32 (%object.TypeInfo*, i8*, i8*)*, i64 (%object.TypeInfo*)*, void (%object.TypeInfo*, i8*, i8*)*, %object.TypeInfo* (%object.TypeInfo*)*, %"byte[]" (%object.TypeInfo*)*, i32 (%object.TypeInfo*)*, %"OffsetTypeInfo[]" (%object.TypeInfo*)* }
%"void*[]" = type { i64, i8** }
@_D10ModuleInfo6__vtblZ = external constant %object.ModuleInfo.__vtbl ; <%object.ModuleInfo.__vtbl*> [#uses=1]
@.str = internal constant [20 x i8] c"tango.core.BitManip\00" ; <[20 x i8]*> [#uses=1]
@_D5tango4core8BitManip8__ModuleZ = global %0 { %object.ModuleInfo.__vtbl* @_D10ModuleInfo6__vtblZ, i8* null, %"byte[]" { i64 19, i8* getelementptr ([20 x i8]* @.str, i32 0, i32 0) }, %1 zeroinitializer, %"ClassInfo[]" zeroinitializer, i32 4, void ()* null, void ()* null, void ()* null, i8* null, void ()* null } ; <%0*> [#uses=1]
@_D5tango4core8BitManip11__moduleRefZ = internal global %ModuleReference { %ModuleReference* null, %object.ModuleInfo* bitcast (%0* @_D5tango4core8BitManip8__ModuleZ to %object.ModuleInfo*) } ; <%ModuleReference*> [#uses=2]
@_Dmodule_ref = external global %ModuleReference* ; <%ModuleReference**> [#uses=2]
@llvm.global_ctors = appending constant [1 x %2] [%2 { i32 65535, void ()* @_D5tango4core8BitManip16__moduleinfoCtorZ }] ; <[1 x %2]*> [#uses=0]
define fastcc i32 @_D5tango4core8BitManip6popcntFkZi(i32 %x_arg) nounwind readnone {
entry:
%tmp1 = lshr i32 %x_arg, 1 ; <i32> [#uses=1]
%tmp2 = and i32 %tmp1, 1431655765 ; <i32> [#uses=1]
%tmp4 = sub i32 %x_arg, %tmp2 ; <i32> [#uses=2]
%tmp6 = lshr i32 %tmp4, 2 ; <i32> [#uses=1]
%tmp7 = and i32 %tmp6, 858993459 ; <i32> [#uses=1]
%tmp9 = and i32 %tmp4, 858993459 ; <i32> [#uses=1]
%tmp10 = add i32 %tmp7, %tmp9 ; <i32> [#uses=2]
%tmp12 = lshr i32 %tmp10, 4 ; <i32> [#uses=1]
%tmp14 = add i32 %tmp12, %tmp10 ; <i32> [#uses=1]
%tmp16 = and i32 %tmp14, 252645135 ; <i32> [#uses=2]
%tmp18 = lshr i32 %tmp16, 8 ; <i32> [#uses=1]
%tmp20 = add i32 %tmp18, %tmp16 ; <i32> [#uses=1]
%tmp22 = and i32 %tmp20, 16711935 ; <i32> [#uses=2]
%tmp24 = lshr i32 %tmp22, 16 ; <i32> [#uses=1]
%tmp26 = add i32 %tmp24, %tmp22 ; <i32> [#uses=1]
%tmp28 = and i32 %tmp26, 65535 ; <i32> [#uses=1]
ret i32 %tmp28
}
define fastcc i32 @_D5tango4core8BitManip7bitswapFkZk(i32 %x_arg) nounwind readnone {
entry:
%tmp1 = lshr i32 %x_arg, 1 ; <i32> [#uses=1]
%tmp2 = and i32 %tmp1, 1431655765 ; <i32> [#uses=1]
%tmp4 = shl i32 %x_arg, 1 ; <i32> [#uses=1]
%tmp5 = and i32 %tmp4, -1431655766 ; <i32> [#uses=1]
%tmp6 = or i32 %tmp2, %tmp5 ; <i32> [#uses=2]
%tmp8 = lshr i32 %tmp6, 2 ; <i32> [#uses=1]
%tmp9 = and i32 %tmp8, 858993459 ; <i32> [#uses=1]
%tmp11 = shl i32 %tmp6, 2 ; <i32> [#uses=1]
%tmp12 = and i32 %tmp11, -858993460 ; <i32> [#uses=1]
%tmp13 = or i32 %tmp9, %tmp12 ; <i32> [#uses=2]
%tmp15 = lshr i32 %tmp13, 4 ; <i32> [#uses=1]
%tmp16 = and i32 %tmp15, 252645135 ; <i32> [#uses=1]
%tmp18 = shl i32 %tmp13, 4 ; <i32> [#uses=1]
%tmp19 = and i32 %tmp18, -252645136 ; <i32> [#uses=1]
%tmp20 = or i32 %tmp16, %tmp19 ; <i32> [#uses=2]
%tmp22 = lshr i32 %tmp20, 8 ; <i32> [#uses=1]
%tmp23 = and i32 %tmp22, 16711935 ; <i32> [#uses=1]
%tmp25 = shl i32 %tmp20, 8 ; <i32> [#uses=1]
%tmp26 = and i32 %tmp25, -16711936 ; <i32> [#uses=1]
%tmp27 = or i32 %tmp23, %tmp26 ; <i32> [#uses=2]
%tmp29 = lshr i32 %tmp27, 16 ; <i32> [#uses=1]
%tmp31 = shl i32 %tmp27, 16 ; <i32> [#uses=1]
%tmp32 = or i32 %tmp29, %tmp31 ; <i32> [#uses=1]
ret i32 %tmp32
}
define internal void @_D5tango4core8BitManip16__moduleinfoCtorZ() nounwind {
moduleinfoCtorEntry:
%current = load %ModuleReference** @_Dmodule_ref ; <%ModuleReference*> [#uses=1]
store %ModuleReference* %current, %ModuleReference** getelementptr (%ModuleReference* @_D5tango4core8BitManip11__moduleRefZ, i32 0, i32 0)
store %ModuleReference* @_D5tango4core8BitManip11__moduleRefZ, %ModuleReference** @_Dmodule_ref
ret void
}
!llvm.ldc.classinfo._D6Object7__ClassZ = !{!0}
!llvm.ldc.classinfo._D10ModuleInfo7__ClassZ = !{!1}
!0 = metadata !{%object.Object undef, i1 false, i1 false}
!1 = metadata !{%object.ModuleInfo undef, i1 false, i1 false}

6
test/Bitcode/metadata.ll Normal file
View File

@ -0,0 +1,6 @@
; 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"