f548e549441f82a361eade32f2b16619d7410022
[oota-llvm.git] / lib / VMCore / Metadata.cpp
1 //===-- Metadata.cpp - Implement Metadata classes -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Metadata classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/Metadata.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Module.h"
18 #include "SymbolTableListTraitsImpl.h"
19 using namespace llvm;
20
21 //===----------------------------------------------------------------------===//
22 //MDString implementation
23 //
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());
32
33   return S;
34 }
35
36 //===----------------------------------------------------------------------===//
37 //MDNode implementation
38 //
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]));
43 }
44
45 void MDNode::Profile(FoldingSetNodeID &ID) const {
46   for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
47     ID.AddPointer(*I);
48 }
49
50 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
51   LLVMContextImpl *pImpl = Context.pImpl;
52   FoldingSetNodeID ID;
53   for (unsigned i = 0; i != NumVals; ++i)
54     ID.AddPointer(Vals[i]);
55
56   pImpl->ConstantsLock.reader_acquire();
57   void *InsertPoint;
58   MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
59   pImpl->ConstantsLock.reader_release();
60   
61   if (!N) {
62     sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
63     N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
64     if (!N) {
65       // InsertPoint will have been set by the FindNodeOrInsertPos call.
66       N = new MDNode(Vals, NumVals);
67       pImpl->MDNodeSet.InsertNode(N, InsertPoint);
68     }
69   }
70
71   return N;
72 }
73
74 //===----------------------------------------------------------------------===//
75 //NamedMDNode implementation
76 //
77 NamedMDNode::NamedMDNode(const Twine &N, MetadataBase*const* MDs, 
78                          unsigned NumMDs, Module *ParentModule)
79   : MetadataBase(Type::MetadataTy, Value::NamedMDNodeVal), Parent(0) {
80   setName(N);
81   for (unsigned i = 0; i != NumMDs; ++i)
82     Node.push_back(WeakMetadataVH(MDs[i]));
83   if (ParentModule)
84     ParentModule->getNamedMDList().push_back(this);
85 }
86
87 /// eraseFromParent - Drop all references and remove the node from parent
88 /// module.
89 void NamedMDNode::eraseFromParent() {
90   dropAllReferences();
91   getParent()->getNamedMDList().erase(this);
92 }
93
94 /// dropAllReferences - Remove all uses and clear node vector.
95 void NamedMDNode::dropAllReferences() {
96   // FIXME: Update metadata use list.
97   Node.clear();
98 }
99
100 NamedMDNode::~NamedMDNode() {
101   dropAllReferences();
102 }