Use CallbackVH, instead of WeakVH, to hold MDNode elements.
[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 //MetadataBase implementation
23 //
24
25 /// resizeOperands - Metadata keeps track of other metadata uses using 
26 /// OperandList. Resize this list to hold anticipated number of metadata
27 /// operands.
28 void MetadataBase::resizeOperands(unsigned NumOps) {
29   unsigned e = getNumOperands();
30   if (NumOps == 0) {
31     NumOps = e*2;
32     if (NumOps < 2) NumOps = 2;  
33   } else if (NumOps > NumOperands) {
34     // No resize needed.
35     if (ReservedSpace >= NumOps) return;
36   } else if (NumOps == NumOperands) {
37     if (ReservedSpace == NumOps) return;
38   } else {
39     return;
40   }
41
42   ReservedSpace = NumOps;
43   Use *OldOps = OperandList;
44   Use *NewOps = allocHungoffUses(NumOps);
45   std::copy(OldOps, OldOps + e, NewOps);
46   OperandList = NewOps;
47   if (OldOps) Use::zap(OldOps, OldOps + e, true);
48 }
49 //===----------------------------------------------------------------------===//
50 //MDString implementation
51 //
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());
60
61   return S;
62 }
63
64 //===----------------------------------------------------------------------===//
65 //MDNode implementation
66 //
67 MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals)
68   : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
69   NumOperands = 0;
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(ElementVH(Vals[i], this));
76   }
77 }
78
79 void MDNode::Profile(FoldingSetNodeID &ID) const {
80   for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
81     ID.AddPointer(*I);
82 }
83
84 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
85   LLVMContextImpl *pImpl = Context.pImpl;
86   FoldingSetNodeID ID;
87   for (unsigned i = 0; i != NumVals; ++i)
88     ID.AddPointer(Vals[i]);
89
90   pImpl->ConstantsLock.reader_acquire();
91   void *InsertPoint;
92   MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
93   pImpl->ConstantsLock.reader_release();
94   
95   if (!N) {
96     sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
97     N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
98     if (!N) {
99       // InsertPoint will have been set by the FindNodeOrInsertPos call.
100       N = new MDNode(Context, Vals, NumVals);
101       pImpl->MDNodeSet.InsertNode(N, InsertPoint);
102     }
103   }
104
105   return N;
106 }
107
108 /// dropAllReferences - Remove all uses and clear node vector.
109 void MDNode::dropAllReferences() {
110   User::dropAllReferences();
111   Node.clear();
112 }
113
114 MDNode::~MDNode() {
115   getType()->getContext().pImpl->MDNodeSet.RemoveNode(this);
116   dropAllReferences();
117 }
118
119 // Replace value from this node's element list.
120 void MDNode::replaceElement(Value *From, Value *To) {
121   if (From == To || !getType())
122     return;
123   LLVMContext &Context = getType()->getContext();
124   LLVMContextImpl *pImpl = Context.pImpl;
125
126   // Find value. This is a linear search, do something if it consumes 
127   // lot of time. It is possible that to have multiple instances of
128   // From in this MDNode's element list.
129   SmallVector<unsigned, 4> Indexes;
130   unsigned Index = 0;
131   for (SmallVector<ElementVH, 4>::iterator I = Node.begin(),
132          E = Node.end(); I != E; ++I, ++Index) {
133     Value *V = *I;
134     if (V && V == From) 
135       Indexes.push_back(Index);
136   }
137
138   if (Indexes.empty())
139     return;
140
141   // Remove "this" from the context map. 
142   {
143     sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
144     pImpl->MDNodeSet.RemoveNode(this);
145   }
146
147   // Replace From element(s) in place.
148   for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end(); 
149        I != E; ++I) {
150     unsigned Index = *I;
151     Node[Index] = ElementVH(To, this);
152   }
153
154   // Insert updated "this" into the context's folding node set.
155   // If a node with same element list already exist then before inserting 
156   // updated "this" into the folding node set, replace all uses of existing 
157   // node with updated "this" node.
158   FoldingSetNodeID ID;
159   Profile(ID);
160   pImpl->ConstantsLock.reader_acquire();
161   void *InsertPoint;
162   MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
163   pImpl->ConstantsLock.reader_release();
164
165   if (N) {
166     N->replaceAllUsesWith(this);
167     delete N;
168     N = 0;
169   }
170
171   {
172     sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
173     N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
174     if (!N) {
175       // InsertPoint will have been set by the FindNodeOrInsertPos call.
176       N = this;
177       pImpl->MDNodeSet.InsertNode(N, InsertPoint);
178     }
179   }
180 }
181
182 //===----------------------------------------------------------------------===//
183 //NamedMDNode implementation
184 //
185 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
186                          MetadataBase*const* MDs, 
187                          unsigned NumMDs, Module *ParentModule)
188   : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
189   setName(N);
190   NumOperands = 0;
191   resizeOperands(NumMDs);
192
193   for (unsigned i = 0; i != NumMDs; ++i) {
194     if (MDs[i])
195       OperandList[NumOperands++] = MDs[i];
196     Node.push_back(WeakMetadataVH(MDs[i]));
197   }
198   if (ParentModule)
199     ParentModule->getNamedMDList().push_back(this);
200 }
201
202 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
203   assert (NMD && "Invalid source NamedMDNode!");
204   SmallVector<MetadataBase *, 4> Elems;
205   for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
206     Elems.push_back(NMD->getElement(i));
207   return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
208                          Elems.data(), Elems.size(), M);
209 }
210
211 /// eraseFromParent - Drop all references and remove the node from parent
212 /// module.
213 void NamedMDNode::eraseFromParent() {
214   getParent()->getNamedMDList().erase(this);
215 }
216
217 /// dropAllReferences - Remove all uses and clear node vector.
218 void NamedMDNode::dropAllReferences() {
219   User::dropAllReferences();
220   Node.clear();
221 }
222
223 NamedMDNode::~NamedMDNode() {
224   dropAllReferences();
225 }