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(Entry.getKeyData(),
59 Entry.getKeyLength());
64 //===----------------------------------------------------------------------===//
65 //MDNode implementation
67 MDNode::MDNode(Value*const* Vals, unsigned NumVals)
68 : MetadataBase(Type::MetadataTy, 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 void MDNode::Profile(FoldingSetNodeID &ID) const {
80 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
84 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
85 LLVMContextImpl *pImpl = Context.pImpl;
87 for (unsigned i = 0; i != NumVals; ++i)
88 ID.AddPointer(Vals[i]);
90 pImpl->ConstantsLock.reader_acquire();
92 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
93 pImpl->ConstantsLock.reader_release();
96 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
97 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
99 // InsertPoint will have been set by the FindNodeOrInsertPos call.
100 N = new MDNode(Vals, NumVals);
101 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
108 /// dropAllReferences - Remove all uses and clear node vector.
109 void MDNode::dropAllReferences() {
110 User::dropAllReferences();
117 //===----------------------------------------------------------------------===//
118 //NamedMDNode implementation
120 NamedMDNode::NamedMDNode(const Twine &N, MetadataBase*const* MDs,
121 unsigned NumMDs, Module *ParentModule)
122 : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal), Parent(0) {
125 resizeOperands(NumMDs);
127 for (unsigned i = 0; i != NumMDs; ++i) {
129 OperandList[NumOperands++] = MDs[i];
130 Node.push_back(WeakMetadataVH(MDs[i]));
133 ParentModule->getNamedMDList().push_back(this);
136 /// eraseFromParent - Drop all references and remove the node from parent
138 void NamedMDNode::eraseFromParent() {
139 getParent()->getNamedMDList().erase(this);
142 /// dropAllReferences - Remove all uses and clear node vector.
143 void NamedMDNode::dropAllReferences() {
144 User::dropAllReferences();
148 NamedMDNode::~NamedMDNode() {