1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00

Derive metadata hierarchy from Value instead of User.

llvm-svn: 84801
This commit is contained in:
Devang Patel 2009-10-21 23:57:35 +00:00
parent bf5a89d82b
commit f9d56f4830
7 changed files with 10 additions and 139 deletions

View File

@ -33,7 +33,6 @@ class LLVMContext {
public:
LLVMContextImpl* const pImpl;
MetadataContext &getMetadata();
bool RemoveDeadMetadata();
LLVMContext();
~LLVMContext();
};

View File

@ -16,7 +16,7 @@
#ifndef LLVM_METADATA_H
#define LLVM_METADATA_H
#include "llvm/User.h"
#include "llvm/Value.h"
#include "llvm/Type.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/SmallVector.h"
@ -33,17 +33,11 @@ class LLVMContext;
//===----------------------------------------------------------------------===//
// MetadataBase - A base class for MDNode, MDString and NamedMDNode.
class MetadataBase : public User {
private:
/// ReservedSpace - The number of operands actually allocated. NumOperands is
/// the number actually in use.
unsigned ReservedSpace;
class MetadataBase : public Value {
protected:
MetadataBase(const Type *Ty, unsigned scid)
: User(Ty, scid, NULL, 0), ReservedSpace(0) {}
: Value(Ty, scid) {}
void resizeOperands(unsigned NumOps);
public:
/// Methods for support type inquiry through isa, cast, and dyn_cast:
@ -60,8 +54,6 @@ public:
/// MDString is always unnamd.
class MDString : public MetadataBase {
MDString(const MDString &); // DO NOT IMPLEMENT
void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
unsigned getNumOperands(); // DO NOT IMPLEMENT
StringRef Str;
protected:
@ -69,10 +61,6 @@ protected:
: MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(begin, l) {}
public:
// Do not allocate any space for operands.
void *operator new(size_t s) {
return User::operator new(s, 0);
}
static MDString *get(LLVMContext &Context, const StringRef &Str);
StringRef getString() const { return Str; }
@ -102,9 +90,6 @@ public:
/// MDNode is always unnamed.
class MDNode : public MetadataBase, public FoldingSetNode {
MDNode(const MDNode &); // DO NOT IMPLEMENT
void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
// getNumOperands - Make this only available for private uses.
unsigned getNumOperands() { return User::getNumOperands(); }
friend class ElementVH;
// Use CallbackVH to hold MDNOde elements.
@ -131,17 +116,10 @@ class MDNode : public MetadataBase, public FoldingSetNode {
protected:
explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals);
public:
// Do not allocate any space for operands.
void *operator new(size_t s) {
return User::operator new(s, 0);
}
// Constructors and destructors.
static MDNode *get(LLVMContext &Context,
Value *const *Vals, unsigned NumVals);
/// dropAllReferences - Remove all uses and clear node vector.
void dropAllReferences();
/// ~MDNode - Destroy MDNode.
~MDNode();
@ -193,9 +171,6 @@ class NamedMDNode : public MetadataBase, public ilist_node<NamedMDNode> {
friend class LLVMContextImpl;
NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT
void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
// getNumOperands - Make this only available for private uses.
unsigned getNumOperands() { return User::getNumOperands(); }
Module *Parent;
SmallVector<WeakMetadataVH, 4> Node;
@ -206,10 +181,6 @@ protected:
explicit NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase*const *Vals,
unsigned NumVals, Module *M = 0);
public:
// Do not allocate any space for operands.
void *operator new(size_t s) {
return User::operator new(s, 0);
}
static NamedMDNode *Create(LLVMContext &C, const Twine &N,
MetadataBase *const *MDs,
unsigned NumMDs, Module *M = 0) {
@ -245,8 +216,6 @@ public:
/// addElement - Add metadata element.
void addElement(MetadataBase *M) {
resizeOperands(NumOperands + 1);
OperandList[NumOperands++] = M;
Node.push_back(WeakMetadataVH(M));
}

View File

@ -149,9 +149,6 @@ bool GlobalDCE::runOnModule(Module &M) {
// Make sure that all memory is released
AliveGlobals.clear();
// Remove dead metadata.
// FIXME - Enable this.
// Changed |= M.getContext().RemoveDeadMetadata();
return Changed;
}

View File

@ -250,8 +250,6 @@ static bool StripDebugInfo(Module &M) {
if (NMD)
NMD->eraseFromParent();
// Remove dead metadata.
M.getContext().RemoveDeadMetadata();
return true;
}

View File

@ -45,32 +45,6 @@ GetElementPtrConstantExpr::GetElementPtrConstantExpr
OperandList[i+1] = IdxList[i];
}
bool LLVMContext::RemoveDeadMetadata() {
std::vector<WeakVH> DeadMDNodes;
bool Changed = false;
while (1) {
for (FoldingSet<MDNode>::iterator
I = pImpl->MDNodeSet.begin(),
E = pImpl->MDNodeSet.end(); I != E; ++I) {
MDNode *N = &(*I);
if (N->use_empty())
DeadMDNodes.push_back(WeakVH(N));
}
if (DeadMDNodes.empty())
return Changed;
while (!DeadMDNodes.empty()) {
Value *V = DeadMDNodes.back(); DeadMDNodes.pop_back();
if (const MDNode *N = dyn_cast_or_null<MDNode>(V))
if (N->use_empty())
delete N;
}
}
return Changed;
}
MetadataContext &LLVMContext::getMetadata() {
return pImpl->TheMetadata;
}

View File

@ -203,9 +203,6 @@ public:
AggZeroConstants.freeConstants();
NullPtrConstants.freeConstants();
UndefValueConstants.freeConstants();
for (FoldingSet<MDNode>::iterator I = MDNodeSet.begin(),
E = MDNodeSet.end(); I != E; ++I)
I->dropAllReferences();
for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end();
I != E; ++I) {
if (I->second->use_empty())
@ -216,6 +213,7 @@ public:
if (I->second->use_empty())
delete I->second;
}
MDNodeSet.clear();
}
};

View File

@ -23,30 +23,6 @@ using namespace llvm;
// MetadataBase implementation.
//
/// resizeOperands - Metadata keeps track of other metadata uses using
/// OperandList. Resize this list to hold anticipated number of metadata
/// operands.
void MetadataBase::resizeOperands(unsigned NumOps) {
unsigned e = getNumOperands();
if (NumOps == 0) {
NumOps = e*2;
if (NumOps < 2) NumOps = 2;
} else if (NumOps > NumOperands) {
// No resize needed.
if (ReservedSpace >= NumOps) return;
} else if (NumOps == NumOperands) {
if (ReservedSpace == NumOps) return;
} else {
return;
}
ReservedSpace = NumOps;
Use *OldOps = OperandList;
Use *NewOps = allocHungoffUses(NumOps);
std::copy(OldOps, OldOps + e, NewOps);
OperandList = NewOps;
if (OldOps) Use::zap(OldOps, OldOps + e, true);
}
//===----------------------------------------------------------------------===//
// MDString implementation.
//
@ -65,20 +41,11 @@ MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
//
MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
NumOperands = 0;
resizeOperands(NumVals);
NodeSize = NumVals;
Node = new ElementVH[NodeSize];
ElementVH *Ptr = Node;
for (unsigned i = 0; i != NumVals; ++i) {
// Only record metadata uses.
if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
OperandList[NumOperands++] = MB;
else if(Vals[i] &&
Vals[i]->getType()->getTypeID() == Type::MetadataTyID)
OperandList[NumOperands++] = Vals[i];
for (unsigned i = 0; i != NumVals; ++i)
*Ptr++ = ElementVH(Vals[i], this);
}
}
void MDNode::Profile(FoldingSetNodeID &ID) const {
@ -109,19 +76,14 @@ MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
return N;
}
/// dropAllReferences - Remove all uses and clear node vector.
void MDNode::dropAllReferences() {
User::dropAllReferences();
delete [] Node;
Node = NULL;
}
/// ~MDNode - Destroy MDNode.
MDNode::~MDNode() {
{
LLVMContextImpl *pImpl = getType()->getContext().pImpl;
pImpl->MDNodeSet.RemoveNode(this);
}
dropAllReferences();
delete [] Node;
Node = NULL;
}
// Replace value from this node's element list.
@ -148,27 +110,6 @@ void MDNode::replaceElement(Value *From, Value *To) {
// Remove "this" from the context map.
pImpl->MDNodeSet.RemoveNode(this);
// MDNode only lists metadata elements in operand list, because MDNode
// used by MDNode is considered a valid use. However on the side, MDNode
// using a non-metadata value is not considered a "use" of non-metadata
// value.
SmallVector<unsigned, 4> OpIndexes;
unsigned OpIndex = 0;
for (User::op_iterator OI = op_begin(), OE = op_end();
OI != OE; ++OI, OpIndex++) {
if (*OI == From)
OpIndexes.push_back(OpIndex);
}
if (MetadataBase *MDTo = dyn_cast_or_null<MetadataBase>(To)) {
for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
OE = OpIndexes.end(); OI != OE; ++OI)
setOperand(*OI, MDTo);
} else {
for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
OE = OpIndexes.end(); OI != OE; ++OI)
setOperand(*OI, 0);
}
// Replace From element(s) in place.
for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
I != E; ++I) {
@ -207,14 +148,10 @@ NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
unsigned NumMDs, Module *ParentModule)
: MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
setName(N);
NumOperands = 0;
resizeOperands(NumMDs);
for (unsigned i = 0; i != NumMDs; ++i) {
if (MDs[i])
OperandList[NumOperands++] = MDs[i];
for (unsigned i = 0; i != NumMDs; ++i)
Node.push_back(WeakMetadataVH(MDs[i]));
}
if (ParentModule)
ParentModule->getNamedMDList().push_back(this);
}
@ -236,7 +173,6 @@ void NamedMDNode::eraseFromParent() {
/// dropAllReferences - Remove all uses and clear node vector.
void NamedMDNode::dropAllReferences() {
User::dropAllReferences();
Node.clear();
}