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 //MDString implementation
24 MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
25 LLVMContextImpl *pImpl = Context.pImpl;
26 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
27 StringMapEntry<MDString *> &Entry =
28 pImpl->MDStringCache.GetOrCreateValue(Str);
29 MDString *&S = Entry.getValue();
30 if (!S) S = new MDString(Entry.getKeyData(),
31 Entry.getKeyLength());
36 //===----------------------------------------------------------------------===//
37 //MDNode implementation
39 MDNode::MDNode(Value*const* Vals, unsigned NumVals)
40 : MetadataBase(Type::MetadataTy, Value::MDNodeVal) {
41 for (unsigned i = 0; i != NumVals; ++i)
42 Node.push_back(WeakVH(Vals[i]));
45 void MDNode::Profile(FoldingSetNodeID &ID) const {
46 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
50 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
51 LLVMContextImpl *pImpl = Context.pImpl;
53 for (unsigned i = 0; i != NumVals; ++i)
54 ID.AddPointer(Vals[i]);
56 pImpl->ConstantsLock.reader_acquire();
58 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
59 pImpl->ConstantsLock.reader_release();
62 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
63 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
65 // InsertPoint will have been set by the FindNodeOrInsertPos call.
66 N = new MDNode(Vals, NumVals);
67 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
74 //===----------------------------------------------------------------------===//
75 //NamedMDNode implementation
77 NamedMDNode::NamedMDNode(const Twine &N, MetadataBase*const* MDs,
78 unsigned NumMDs, Module *ParentModule)
79 : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal), Parent(0) {
81 for (unsigned i = 0; i != NumMDs; ++i)
82 Node.push_back(WeakMetadataVH(MDs[i]));
84 ParentModule->getNamedMDList().push_back(this);
87 /// eraseFromParent - Drop all references and remove the node from parent
89 void NamedMDNode::eraseFromParent() {
91 getParent()->getNamedMDList().erase(this);
94 /// dropAllReferences - Remove all uses and clear node vector.
95 void NamedMDNode::dropAllReferences() {
96 // FIXME: Update metadata use list.
100 NamedMDNode::~NamedMDNode() {