blob: 2c80a60a8cf707b2421d8b4489d2d6c1a14d53c3 [file] [log] [blame]
Devang Patela4f43fb2009-07-28 21:49:47 +00001//===-- 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
Owen Anderson0087fe62009-07-31 21:35:40 +000014#include "LLVMContextImpl.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000015#include "llvm/Metadata.h"
Owen Anderson0087fe62009-07-31 21:35:40 +000016#include "llvm/LLVMContext.h"
Devang Patel05a26fb2009-07-29 00:33:07 +000017#include "llvm/Module.h"
Devang Pateld5497a4b2009-09-16 18:09:00 +000018#include "llvm/Instruction.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000019#include "SymbolTableListTraitsImpl.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Devang Pateld7fd6ab2009-08-03 22:51:10 +000023//MetadataBase implementation
24//
25
26/// resizeOperands - Metadata keeps track of other metadata uses using
27/// OperandList. Resize this list to hold anticipated number of metadata
28/// operands.
29void MetadataBase::resizeOperands(unsigned NumOps) {
30 unsigned e = getNumOperands();
31 if (NumOps == 0) {
32 NumOps = e*2;
33 if (NumOps < 2) NumOps = 2;
34 } else if (NumOps > NumOperands) {
35 // No resize needed.
36 if (ReservedSpace >= NumOps) return;
37 } else if (NumOps == NumOperands) {
38 if (ReservedSpace == NumOps) return;
39 } else {
40 return;
41 }
42
43 ReservedSpace = NumOps;
44 Use *OldOps = OperandList;
45 Use *NewOps = allocHungoffUses(NumOps);
46 std::copy(OldOps, OldOps + e, NewOps);
47 OperandList = NewOps;
48 if (OldOps) Use::zap(OldOps, OldOps + e, true);
49}
50//===----------------------------------------------------------------------===//
Owen Anderson0087fe62009-07-31 21:35:40 +000051//MDString implementation
52//
53MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
54 LLVMContextImpl *pImpl = Context.pImpl;
55 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
56 StringMapEntry<MDString *> &Entry =
57 pImpl->MDStringCache.GetOrCreateValue(Str);
58 MDString *&S = Entry.getValue();
Owen Anderson55f1c092009-08-13 21:58:54 +000059 if (!S) S = new MDString(Context, Entry.getKeyData(),
Owen Anderson0087fe62009-07-31 21:35:40 +000060 Entry.getKeyLength());
61
62 return S;
63}
64
65//===----------------------------------------------------------------------===//
Devang Patela4f43fb2009-07-28 21:49:47 +000066//MDNode implementation
67//
Owen Anderson55f1c092009-08-13 21:58:54 +000068MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals)
69 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Devang Pateld7fd6ab2009-08-03 22:51:10 +000070 NumOperands = 0;
71 resizeOperands(NumVals);
72 for (unsigned i = 0; i != NumVals; ++i) {
73 // Only record metadata uses.
74 if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
75 OperandList[NumOperands++] = MB;
Devang Patela33c5a92009-09-04 21:32:05 +000076 else if(Vals[i] &&
77 Vals[i]->getType()->getTypeID() == Type::MetadataTyID)
78 OperandList[NumOperands++] = Vals[i];
Devang Patelf7188322009-09-03 01:39:20 +000079 Node.push_back(ElementVH(Vals[i], this));
Devang Pateld7fd6ab2009-08-03 22:51:10 +000080 }
Devang Patela4f43fb2009-07-28 21:49:47 +000081}
82
Devang Patelf7188322009-09-03 01:39:20 +000083void MDNode::Profile(FoldingSetNodeID &ID) const {
84 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
85 ID.AddPointer(*I);
86}
87
Owen Anderson0087fe62009-07-31 21:35:40 +000088MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
Devang Patelf7188322009-09-03 01:39:20 +000089 LLVMContextImpl *pImpl = Context.pImpl;
90 FoldingSetNodeID ID;
91 for (unsigned i = 0; i != NumVals; ++i)
92 ID.AddPointer(Vals[i]);
93
94 pImpl->ConstantsLock.reader_acquire();
95 void *InsertPoint;
96 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
97 pImpl->ConstantsLock.reader_release();
Devang Patel9a767cb2009-09-09 17:30:04 +000098
Devang Patelf7188322009-09-03 01:39:20 +000099 if (!N) {
100 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
101 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
102 if (!N) {
103 // InsertPoint will have been set by the FindNodeOrInsertPos call.
104 N = new MDNode(Context, Vals, NumVals);
105 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
106 }
107 }
108
109 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000110}
111
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000112/// dropAllReferences - Remove all uses and clear node vector.
113void MDNode::dropAllReferences() {
114 User::dropAllReferences();
115 Node.clear();
116}
117
118MDNode::~MDNode() {
Devang Patelcbde0732009-09-09 17:44:26 +0000119 {
120 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
121 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
122 pImpl->MDNodeSet.RemoveNode(this);
123 }
Devang Patel7b8f61f2009-08-27 15:32:38 +0000124 dropAllReferences();
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000125}
Devang Patel5c310be2009-08-11 18:01:24 +0000126
Devang Patelf7188322009-09-03 01:39:20 +0000127// Replace value from this node's element list.
128void MDNode::replaceElement(Value *From, Value *To) {
129 if (From == To || !getType())
130 return;
131 LLVMContext &Context = getType()->getContext();
132 LLVMContextImpl *pImpl = Context.pImpl;
133
134 // Find value. This is a linear search, do something if it consumes
135 // lot of time. It is possible that to have multiple instances of
136 // From in this MDNode's element list.
137 SmallVector<unsigned, 4> Indexes;
138 unsigned Index = 0;
139 for (SmallVector<ElementVH, 4>::iterator I = Node.begin(),
140 E = Node.end(); I != E; ++I, ++Index) {
141 Value *V = *I;
142 if (V && V == From)
143 Indexes.push_back(Index);
144 }
145
146 if (Indexes.empty())
147 return;
148
149 // Remove "this" from the context map.
150 {
151 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
152 pImpl->MDNodeSet.RemoveNode(this);
153 }
154
Devang Patela33c5a92009-09-04 21:32:05 +0000155 // MDNode only lists metadata elements in operand list, because MDNode
156 // used by MDNode is considered a valid use. However on the side, MDNode
157 // using a non-metadata value is not considered a "use" of non-metadata
158 // value.
159 SmallVector<unsigned, 4> OpIndexes;
160 unsigned OpIndex = 0;
161 for (User::op_iterator OI = op_begin(), OE = op_end();
162 OI != OE; ++OI, OpIndex++) {
163 if (*OI == From)
164 OpIndexes.push_back(OpIndex);
165 }
166 if (MetadataBase *MDTo = dyn_cast_or_null<MetadataBase>(To)) {
167 for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
168 OE = OpIndexes.end(); OI != OE; ++OI)
169 setOperand(*OI, MDTo);
170 } else {
171 for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
172 OE = OpIndexes.end(); OI != OE; ++OI)
173 setOperand(*OI, 0);
174 }
175
Devang Patelf7188322009-09-03 01:39:20 +0000176 // Replace From element(s) in place.
177 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
178 I != E; ++I) {
179 unsigned Index = *I;
180 Node[Index] = ElementVH(To, this);
181 }
182
183 // Insert updated "this" into the context's folding node set.
184 // If a node with same element list already exist then before inserting
185 // updated "this" into the folding node set, replace all uses of existing
186 // node with updated "this" node.
187 FoldingSetNodeID ID;
188 Profile(ID);
189 pImpl->ConstantsLock.reader_acquire();
190 void *InsertPoint;
191 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
192 pImpl->ConstantsLock.reader_release();
193
194 if (N) {
195 N->replaceAllUsesWith(this);
196 delete N;
197 N = 0;
198 }
199
200 {
201 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
202 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
203 if (!N) {
204 // InsertPoint will have been set by the FindNodeOrInsertPos call.
205 N = this;
206 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
207 }
208 }
209}
210
Devang Patel05a26fb2009-07-29 00:33:07 +0000211//===----------------------------------------------------------------------===//
212//NamedMDNode implementation
213//
Owen Anderson55f1c092009-08-13 21:58:54 +0000214NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
215 MetadataBase*const* MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000216 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000217 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000218 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000219 NumOperands = 0;
220 resizeOperands(NumMDs);
221
222 for (unsigned i = 0; i != NumMDs; ++i) {
223 if (MDs[i])
224 OperandList[NumOperands++] = MDs[i];
Devang Patel05a26fb2009-07-29 00:33:07 +0000225 Node.push_back(WeakMetadataVH(MDs[i]));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000226 }
Devang Patel4a942d02009-07-29 21:58:56 +0000227 if (ParentModule)
228 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000229}
Devang Patel79238d72009-08-03 06:19:01 +0000230
Devang Patel5c310be2009-08-11 18:01:24 +0000231NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
232 assert (NMD && "Invalid source NamedMDNode!");
233 SmallVector<MetadataBase *, 4> Elems;
234 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
235 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000236 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
237 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000238}
239
Devang Patel79238d72009-08-03 06:19:01 +0000240/// eraseFromParent - Drop all references and remove the node from parent
241/// module.
242void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000243 getParent()->getNamedMDList().erase(this);
244}
245
246/// dropAllReferences - Remove all uses and clear node vector.
247void NamedMDNode::dropAllReferences() {
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000248 User::dropAllReferences();
Devang Patel79238d72009-08-03 06:19:01 +0000249 Node.clear();
250}
251
252NamedMDNode::~NamedMDNode() {
253 dropAllReferences();
254}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000255
256//===----------------------------------------------------------------------===//
257//Metadata implementation
258//
259
260/// RegisterMDKind - Register a new metadata kind and return its ID.
261/// A metadata kind can be registered only once.
262MDKindID Metadata::RegisterMDKind(const char *Name) {
263 MDKindID Count = MDHandlerNames.size();
264 StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
265 assert(I == MDHandlerNames.end() && "Already registered MDKind!");
266 MDHandlerNames[Name] = Count + 1;
267 return Count + 1;
268}
269
270/// getMDKind - Return metadata kind. If the requested metadata kind
271/// is not registered then return 0.
272MDKindID Metadata::getMDKind(const char *Name) {
273 StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
274 if (I == MDHandlerNames.end())
275 return 0;
276
277 return I->getValue();
278}
279
280/// setMD - Attach the metadata of given kind with an Instruction.
281void Metadata::setMD(MDKindID MDKind, MDNode *Node, Instruction *Inst) {
282 MDStoreTy::iterator I = MetadataStore.find(Inst);
283 Inst->HasMetadata = true;
284 if (I == MetadataStore.end()) {
285 MDMapTy Info;
286 Info.push_back(std::make_pair(MDKind, Node));
287 MetadataStore.insert(std::make_pair(Inst, Info));
288 return;
289 }
290
291 MDMapTy &Info = I->second;
292 Info.push_back(std::make_pair(MDKind, Node));
293 return;
294}
295
296/// getMD - Get the metadata of given kind attached with an Instruction.
297/// If the metadata is not found then return 0.
298MDNode *Metadata::getMD(MDKindID MDKind, const Instruction *Inst) {
299 MDNode *Node = NULL;
300 MDStoreTy::iterator I = MetadataStore.find(Inst);
301 if (I == MetadataStore.end())
302 return Node;
303
304 MDMapTy &Info = I->second;
305 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
306 if (I->first == MDKind)
307 Node = dyn_cast_or_null<MDNode>(I->second);
308 return Node;
309}
310
Devang Pateldec23fd2009-09-16 20:21:17 +0000311/// getMDs - Get the metadata attached with an Instruction.
312const Metadata::MDMapTy *Metadata::getMDs(const Instruction *Inst) {
313 MDStoreTy::iterator I = MetadataStore.find(Inst);
314 if (I == MetadataStore.end())
315 return NULL;
316
317 return &(I->second);
318}
319
Devang Pateld5497a4b2009-09-16 18:09:00 +0000320/// ValueIsDeleted - This handler is used to update metadata store
321/// when a value is deleted.
322void Metadata::ValueIsDeleted(const Instruction *Inst) {
323 // Find Metadata handles for this instruction.
324 MDStoreTy::iterator I = MetadataStore.find(Inst);
325 if (I == MetadataStore.end())
326 return;
327 MDMapTy &Info = I->second;
328
329 // FIXME : Give all metadata handlers a chance to adjust.
330
331 // Remove the entries for this instruction.
332 Info.clear();
333 MetadataStore.erase(Inst);
334}