public:
LLVMContextImpl* const pImpl;
MetadataContext &getMetadata();
- bool RemoveDeadMetadata();
LLVMContext();
~LLVMContext();
};
#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"
//===----------------------------------------------------------------------===//
// 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:
/// 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:
: 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; }
/// 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.
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();
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;
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) {
/// addElement - Add metadata element.
void addElement(MetadataBase *M) {
- resizeOperands(NumOperands + 1);
- OperandList[NumOperands++] = M;
Node.push_back(WeakMetadataVH(M));
}
// Make sure that all memory is released
AliveGlobals.clear();
- // Remove dead metadata.
- // FIXME - Enable this.
- // Changed |= M.getContext().RemoveDeadMetadata();
return Changed;
}
if (NMD)
NMD->eraseFromParent();
- // Remove dead metadata.
- M.getContext().RemoveDeadMetadata();
return true;
}
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;
}
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())
if (I->second->use_empty())
delete I->second;
}
+ MDNodeSet.clear();
}
};
// 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.
//
//
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 {
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.
// 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) {
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);
}
/// dropAllReferences - Remove all uses and clear node vector.
void NamedMDNode::dropAllReferences() {
- User::dropAllReferences();
Node.clear();
}