diff --git a/include/llvm/Metadata.h b/include/llvm/Metadata.h index 18da7a5b18c..676811ebb4c 100644 --- a/include/llvm/Metadata.h +++ b/include/llvm/Metadata.h @@ -158,7 +158,7 @@ class NamedMDNode : public MetadataBase, public ilist_node { NamedMDNode(const NamedMDNode &); // DO NOT IMPLEMENT Module *Parent; - SmallVector, 4> Node; + void *Operands; // SmallVector, 4> void setParent(Module *M) { Parent = M; } protected: @@ -188,21 +188,14 @@ public: inline const Module *getParent() const { return Parent; } /// getElement - Return specified element. - MetadataBase *getElement(unsigned i) const { - assert(i < getNumElements() && "Invalid element number!"); - return Node[i]; - } - + MetadataBase *getElement(unsigned i) const; + /// getNumElements - Return number of NamedMDNode elements. - unsigned getNumElements() const { - return (unsigned)Node.size(); - } + unsigned getNumElements() const; /// addElement - Add metadata element. - void addElement(MetadataBase *M) { - Node.push_back(TrackingVH(M)); - } - + void addElement(MetadataBase *M); + /// Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const NamedMDNode *) { return true; } static bool classof(const Value *V) { diff --git a/lib/VMCore/Metadata.cpp b/lib/VMCore/Metadata.cpp index 84cab624c69..26b5294ac36 100644 --- a/lib/VMCore/Metadata.cpp +++ b/lib/VMCore/Metadata.cpp @@ -226,12 +226,19 @@ bool MDNode::getLocalFunction(Function *LocalFunction, //===----------------------------------------------------------------------===// // NamedMDNode implementation. // +static SmallVector, 4> &getNMDOps(void *Operands) { + return *(SmallVector, 4>*)Operands; +} + NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, MetadataBase *const *MDs, unsigned NumMDs, Module *ParentModule) : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) { setName(N); - + + Operands = new SmallVector, 4>(); + + SmallVector, 4> &Node = getNMDOps(Operands); for (unsigned i = 0; i != NumMDs; ++i) Node.push_back(TrackingVH(MDs[i])); @@ -242,12 +249,35 @@ NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) { assert(NMD && "Invalid source NamedMDNode!"); SmallVector Elems; + Elems.reserve(NMD->getNumElements()); + for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) Elems.push_back(NMD->getElement(i)); return new NamedMDNode(NMD->getContext(), NMD->getName().data(), Elems.data(), Elems.size(), M); } +NamedMDNode::~NamedMDNode() { + dropAllReferences(); + delete &getNMDOps(Operands); +} + +/// getNumElements - Return number of NamedMDNode elements. +unsigned NamedMDNode::getNumElements() const { + return (unsigned)getNMDOps(Operands).size(); +} + +/// getElement - Return specified element. +MetadataBase *NamedMDNode::getElement(unsigned i) const { + assert(i < getNumElements() && "Invalid element number!"); + return getNMDOps(Operands)[i]; +} + +/// addElement - Add metadata element. +void NamedMDNode::addElement(MetadataBase *M) { + getNMDOps(Operands).push_back(TrackingVH(M)); +} + /// eraseFromParent - Drop all references and remove the node from parent /// module. void NamedMDNode::eraseFromParent() { @@ -256,12 +286,9 @@ void NamedMDNode::eraseFromParent() { /// dropAllReferences - Remove all uses and clear node vector. void NamedMDNode::dropAllReferences() { - Node.clear(); + getNMDOps(Operands).clear(); } -NamedMDNode::~NamedMDNode() { - dropAllReferences(); -} //===----------------------------------------------------------------------===// // MetadataContextImpl implementation.