#include "SymbolTableListTraitsImpl.h"
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
//
//
MDNode::MDNode(Value*const* Vals, unsigned NumVals)
: MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
- for (unsigned i = 0; i != NumVals; ++i)
+ NumOperands = 0;
+ resizeOperands(NumVals);
+ for (unsigned i = 0; i != NumVals; ++i) {
+ // Only record metadata uses.
+ if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
+ OperandList[NumOperands++] = MB;
Node.push_back(WeakVH(Vals[i]));
-}
-
-void MDNode::Profile(FoldingSetNodeID &ID) const {
- for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
- ID.AddPointer(*I);
+ }
}
MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
LLVMContextImpl *pImpl = Context.pImpl;
- FoldingSetNodeID ID;
- for (unsigned i = 0; i != NumVals; ++i)
- ID.AddPointer(Vals[i]);
-
- pImpl->ConstantsLock.reader_acquire();
- void *InsertPoint;
- MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
- pImpl->ConstantsLock.reader_release();
+ std::vector<Value*> V;
+ V.reserve(NumVals);
+ for (unsigned i = 0; i < NumVals; ++i)
+ V.push_back(Vals[i]);
- if (!N) {
- sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
- N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
- if (!N) {
- // InsertPoint will have been set by the FindNodeOrInsertPos call.
- N = new MDNode(Vals, NumVals);
- pImpl->MDNodeSet.InsertNode(N, InsertPoint);
- }
- }
+ return pImpl->MDNodes.getOrCreate(Type::MetadataTy, V);
+}
+
+/// dropAllReferences - Remove all uses and clear node vector.
+void MDNode::dropAllReferences() {
+ User::dropAllReferences();
+ Node.clear();
+}
- return N;
+static std::vector<Value*> getValType(MDNode *N) {
+ std::vector<Value*> Elements;
+ Elements.reserve(N->getNumElements());
+ for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
+ Elements.push_back(N->getElement(i));
+ return Elements;
+}
+
+MDNode::~MDNode() {
+ dropAllReferences();
+ getType()->getContext().pImpl->MDNodes.remove(this);
}
//===----------------------------------------------------------------------===//
unsigned NumMDs, Module *ParentModule)
: MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal), Parent(0) {
setName(N);
- for (unsigned i = 0; i != NumMDs; ++i)
+ NumOperands = 0;
+ resizeOperands(NumMDs);
+
+ for (unsigned i = 0; i != NumMDs; ++i) {
+ if (MDs[i])
+ OperandList[NumOperands++] = MDs[i];
Node.push_back(WeakMetadataVH(MDs[i]));
+ }
if (ParentModule)
ParentModule->getNamedMDList().push_back(this);
}
+
+NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
+ assert (NMD && "Invalid source NamedMDNode!");
+ SmallVector<MetadataBase *, 4> Elems;
+ for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
+ Elems.push_back(NMD->getElement(i));
+ return new NamedMDNode(NMD->getName().data(), Elems.data(), Elems.size(), M);
+}
+
+/// eraseFromParent - Drop all references and remove the node from parent
+/// module.
+void NamedMDNode::eraseFromParent() {
+ getParent()->getNamedMDList().erase(this);
+}
+
+/// dropAllReferences - Remove all uses and clear node vector.
+void NamedMDNode::dropAllReferences() {
+ User::dropAllReferences();
+ Node.clear();
+}
+
+NamedMDNode::~NamedMDNode() {
+ dropAllReferences();
+}