1 //===-- Metadata.cpp - Implement Metadata classes -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/Metadata.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Module.h"
18 #include "SymbolTableListTraitsImpl.h"
21 //===----------------------------------------------------------------------===//
22 //MetadataBase implementation
25 /// resizeOperands - Metadata keeps track of other metadata uses using
26 /// OperandList. Resize this list to hold anticipated number of metadata
28 void MetadataBase::resizeOperands(unsigned NumOps) {
29 unsigned e = getNumOperands();
32 if (NumOps < 2) NumOps = 2;
33 } else if (NumOps > NumOperands) {
35 if (ReservedSpace >= NumOps) return;
36 } else if (NumOps == NumOperands) {
37 if (ReservedSpace == NumOps) return;
42 ReservedSpace = NumOps;
43 Use *OldOps = OperandList;
44 Use *NewOps = allocHungoffUses(NumOps);
45 std::copy(OldOps, OldOps + e, NewOps);
47 if (OldOps) Use::zap(OldOps, OldOps + e, true);
49 //===----------------------------------------------------------------------===//
50 //MDString implementation
52 MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
53 LLVMContextImpl *pImpl = Context.pImpl;
54 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
55 StringMapEntry<MDString *> &Entry =
56 pImpl->MDStringCache.GetOrCreateValue(Str);
57 MDString *&S = Entry.getValue();
58 if (!S) S = new MDString(Context, Entry.getKeyData(),
59 Entry.getKeyLength());
64 //===----------------------------------------------------------------------===//
65 //MDNode implementation
67 MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals)
68 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
70 resizeOperands(NumVals);
71 for (unsigned i = 0; i != NumVals; ++i) {
72 // Only record metadata uses.
73 if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
74 OperandList[NumOperands++] = MB;
75 Node.push_back(WeakVH(Vals[i]));
79 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
80 LLVMContextImpl *pImpl = Context.pImpl;
81 std::vector<Value*> V;
83 for (unsigned i = 0; i < NumVals; ++i)
86 return pImpl->MDNodes.getOrCreate(Type::getMetadataTy(Context), V);
89 /// dropAllReferences - Remove all uses and clear node vector.
90 void MDNode::dropAllReferences() {
91 User::dropAllReferences();
95 static std::vector<Value*> getValType(MDNode *N) {
96 std::vector<Value*> Elements;
97 Elements.reserve(N->getNumElements());
98 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
99 Elements.push_back(N->getElement(i));
105 getType()->getContext().pImpl->MDNodes.remove(this);
108 //===----------------------------------------------------------------------===//
109 //NamedMDNode implementation
111 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
112 MetadataBase*const* MDs,
113 unsigned NumMDs, Module *ParentModule)
114 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
117 resizeOperands(NumMDs);
119 for (unsigned i = 0; i != NumMDs; ++i) {
121 OperandList[NumOperands++] = MDs[i];
122 Node.push_back(WeakMetadataVH(MDs[i]));
125 ParentModule->getNamedMDList().push_back(this);
128 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
129 assert (NMD && "Invalid source NamedMDNode!");
130 SmallVector<MetadataBase *, 4> Elems;
131 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
132 Elems.push_back(NMD->getElement(i));
133 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
134 Elems.data(), Elems.size(), M);
137 /// eraseFromParent - Drop all references and remove the node from parent
139 void NamedMDNode::eraseFromParent() {
140 getParent()->getNamedMDList().erase(this);
143 /// dropAllReferences - Remove all uses and clear node vector.
144 void NamedMDNode::dropAllReferences() {
145 User::dropAllReferences();
149 NamedMDNode::~NamedMDNode() {