blob: 0bed865bbae36ed63884a60e26665360fb8d3f64 [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
14#include "llvm/Metadata.h"
Chris Lattner1300f452009-12-28 08:24:16 +000015#include "LLVMContextImpl.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 Patel1155fdf2009-10-22 19:36:54 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000021#include "SymbolTableListTraitsImpl.h"
Chris Lattner1300f452009-12-28 08:24:16 +000022#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000026// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000027//
Chris Lattner5a409bd2009-12-28 08:30:43 +000028
29MDString::MDString(LLVMContext &C, StringRef S)
30 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
31
Devang Pateldcb99d32009-10-22 00:10:15 +000032MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000033 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson0087fe62009-07-31 21:35:40 +000034 StringMapEntry<MDString *> &Entry =
35 pImpl->MDStringCache.GetOrCreateValue(Str);
36 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000037 if (!S) S = new MDString(Context, Entry.getKey());
38 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000039}
40
Devang Patel862ef782009-11-12 00:50:58 +000041MDString *MDString::get(LLVMContext &Context, const char *Str) {
42 LLVMContextImpl *pImpl = Context.pImpl;
43 StringMapEntry<MDString *> &Entry =
44 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
45 MDString *&S = Entry.getValue();
Nick Lewycky6e052512009-11-27 19:57:53 +000046 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000047 return S;
Devang Patel862ef782009-11-12 00:50:58 +000048}
49
Owen Anderson0087fe62009-07-31 21:35:40 +000050//===----------------------------------------------------------------------===//
Chris Lattner74a6ad62009-12-28 07:41:54 +000051// MDNodeElement implementation.
52//
53
54// Use CallbackVH to hold MDNode elements.
55namespace llvm {
56class MDNodeElement : public CallbackVH {
57 MDNode *Parent;
58public:
59 MDNodeElement() {}
60 MDNodeElement(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
61 ~MDNodeElement() {}
62
Chris Lattnerd944cf72009-12-28 09:10:16 +000063 void set(Value *V, MDNode *P) {
64 setValPtr(V);
65 Parent = P;
66 }
67
Chris Lattner74a6ad62009-12-28 07:41:54 +000068 virtual void deleted();
69 virtual void allUsesReplacedWith(Value *NV);
70};
71} // end namespace llvm.
72
73
74void MDNodeElement::deleted() {
Chris Lattner95c445d2009-12-28 09:32:10 +000075 Parent->replaceElement(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000076}
77
78void MDNodeElement::allUsesReplacedWith(Value *NV) {
Chris Lattner95c445d2009-12-28 09:32:10 +000079 Parent->replaceElement(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000080}
81
82
83
84//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000085// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000086//
Chris Lattner74a6ad62009-12-28 07:41:54 +000087
Chris Lattnerf543eff2009-12-28 09:12:35 +000088/// ~MDNode - Destroy MDNode.
89MDNode::~MDNode() {
Chris Lattner30ae06b2009-12-30 21:42:11 +000090 if (!isNotUniqued()) {
91 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
92 pImpl->MDNodeSet.RemoveNode(this);
93 }
Chris Lattnerf543eff2009-12-28 09:12:35 +000094 delete [] Operands;
95 Operands = NULL;
96}
97
Victor Hernandezdd7418a2009-12-16 02:52:09 +000098MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000099 bool isFunctionLocal)
Owen Anderson55f1c092009-08-13 21:58:54 +0000100 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000101 NumOperands = NumVals;
Chris Lattner30ae06b2009-12-30 21:42:11 +0000102 // FIXME: Coallocate the operand list. These have fixed arity.
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000103 Operands = new MDNodeElement[NumOperands];
Chris Lattnerf543eff2009-12-28 09:12:35 +0000104
Devang Patel27e0be22009-10-21 23:57:35 +0000105 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattnerf543eff2009-12-28 09:12:35 +0000106 Operands[i].set(Vals[i], this);
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000107
Victor Hernandez0471abd2009-12-18 20:09:14 +0000108 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000109 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Devang Patela4f43fb2009-07-28 21:49:47 +0000110}
111
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000112MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000113 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000114 LLVMContextImpl *pImpl = Context.pImpl;
115 FoldingSetNodeID ID;
116 for (unsigned i = 0; i != NumVals; ++i)
117 ID.AddPointer(Vals[i]);
118
Devang Patelf7188322009-09-03 01:39:20 +0000119 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000120 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000121 if (!N) {
122 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +0000123 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000124 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000125 }
Devang Patelf7188322009-09-03 01:39:20 +0000126 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000127}
128
Chris Lattnerf543eff2009-12-28 09:12:35 +0000129void MDNode::Profile(FoldingSetNodeID &ID) const {
130 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
131 ID.AddPointer(getElement(i));
Chris Lattner9a258772009-12-28 19:49:00 +0000132 // HASH TABLE COLLISIONS?
133 // DO NOT REINSERT AFTER AN OPERAND DROPS TO NULL!
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000134}
Devang Patel5c310be2009-08-11 18:01:24 +0000135
Chris Lattnerf543eff2009-12-28 09:12:35 +0000136
Chris Lattner74a6ad62009-12-28 07:41:54 +0000137/// getElement - Return specified element.
138Value *MDNode::getElement(unsigned i) const {
139 assert(i < getNumElements() && "Invalid element number!");
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000140 return Operands[i];
Chris Lattner74a6ad62009-12-28 07:41:54 +0000141}
142
143
144
Devang Patelf7188322009-09-03 01:39:20 +0000145// Replace value from this node's element list.
Chris Lattner95c445d2009-12-28 09:32:10 +0000146void MDNode::replaceElement(MDNodeElement *Op, Value *To) {
147 Value *From = *Op;
148
149 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000150 return;
Devang Patelf7188322009-09-03 01:39:20 +0000151
Chris Lattner30ae06b2009-12-30 21:42:11 +0000152 // Update the operand.
153 Op->set(To, this);
154
155 // If this node is already not being uniqued (because one of the operands
156 // already went to null), then there is nothing else to do here.
157 if (isNotUniqued()) return;
158
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000159 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
160
161 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
162 // this node to remove it, so we don't care what state the operands are in.
163 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000164
Chris Lattner30ae06b2009-12-30 21:42:11 +0000165 // If we are dropping an argument to null, we choose to not unique the MDNode
166 // anymore. This commonly occurs during destruction, and uniquing these
167 // brings little reuse.
168 if (To == 0) {
169 setIsNotUniqued();
170 return;
171 }
172
173 // Now that the node is out of the folding set, get ready to reinsert it.
174 // First, check to see if another node with the same operands already exists
175 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000176 FoldingSetNodeID ID;
177 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000178 void *InsertPoint;
179 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000180
181 if (N) {
Chris Lattner30ae06b2009-12-30 21:42:11 +0000182 // FIXME:
183 // If it already exists in the set, we don't reinsert it, we just claim it
184 // isn't uniqued.
185
Devang Patelf7188322009-09-03 01:39:20 +0000186 N->replaceAllUsesWith(this);
187 delete N;
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000188 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
189 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000190 }
191
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000192 // InsertPoint will have been set by the FindNodeOrInsertPos call.
193 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000194}
195
Devang Patel05a26fb2009-07-29 00:33:07 +0000196//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000197// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000198//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000199static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
200 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
201}
202
Owen Anderson55f1c092009-08-13 21:58:54 +0000203NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000204 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000205 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000206 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000207 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000208
209 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
210
211 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000212 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000213 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000214
Devang Patel4a942d02009-07-29 21:58:56 +0000215 if (ParentModule)
216 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000217}
Devang Patel79238d72009-08-03 06:19:01 +0000218
Devang Patel5c310be2009-08-11 18:01:24 +0000219NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000220 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000221 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000222 Elems.reserve(NMD->getNumElements());
223
Devang Patel5c310be2009-08-11 18:01:24 +0000224 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
225 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000226 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
227 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000228}
229
Chris Lattner1bc810b2009-12-28 08:07:14 +0000230NamedMDNode::~NamedMDNode() {
231 dropAllReferences();
232 delete &getNMDOps(Operands);
233}
234
235/// getNumElements - Return number of NamedMDNode elements.
236unsigned NamedMDNode::getNumElements() const {
237 return (unsigned)getNMDOps(Operands).size();
238}
239
240/// getElement - Return specified element.
241MetadataBase *NamedMDNode::getElement(unsigned i) const {
242 assert(i < getNumElements() && "Invalid element number!");
243 return getNMDOps(Operands)[i];
244}
245
246/// addElement - Add metadata element.
247void NamedMDNode::addElement(MetadataBase *M) {
248 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
249}
250
Devang Patel79238d72009-08-03 06:19:01 +0000251/// eraseFromParent - Drop all references and remove the node from parent
252/// module.
253void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000254 getParent()->getNamedMDList().erase(this);
255}
256
257/// dropAllReferences - Remove all uses and clear node vector.
258void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000259 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000260}
261
Devang Pateld5497a4b2009-09-16 18:09:00 +0000262
263//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000264// MetadataContext implementation.
265//
Devang Patel1155fdf2009-10-22 19:36:54 +0000266
Chris Lattner5508da32009-12-29 07:56:15 +0000267#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000268/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000269static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000270 if (MDName.empty())
271 return false;
272
273 if (!isalpha(MDName[0]))
274 return false;
275
276 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
277 ++I) {
278 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
279 return false;
280 }
281 return true;
282}
Chris Lattner5508da32009-12-29 07:56:15 +0000283#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000284
Chris Lattner70939462009-12-28 20:45:51 +0000285/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000286unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000287 assert(isValidName(Name) && "Invalid MDNode name");
Chris Lattnera0566972009-12-29 09:01:33 +0000288
289 unsigned &Entry = pImpl->CustomMDKindNames[Name];
290
291 // If this is new, assign it its ID.
292 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
293 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000294}
295
Devang Patel1155fdf2009-10-22 19:36:54 +0000296/// getHandlerNames - Populate client supplied smallvector using custome
297/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000298void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
299 Names.resize(pImpl->CustomMDKindNames.size()+1);
300 Names[0] = "";
301 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
302 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
303 // MD Handlers are numbered from 1.
304 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000305}
306
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000307//===----------------------------------------------------------------------===//
308// Instruction Metadata method implementations.
309//
310
311void Instruction::setMetadata(const char *Kind, MDNode *Node) {
312 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000313 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000314}
315
316MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000317 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000318}
319
320/// setMetadata - Set the metadata of of the specified kind to the specified
321/// node. This updates/replaces metadata if already present, or removes it if
322/// Node is null.
323void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
324 if (Node == 0 && !hasMetadata()) return;
325
Chris Lattnera0566972009-12-29 09:01:33 +0000326 // Handle the case when we're adding/updating metadata on an instruction.
327 if (Node) {
328 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
329 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
330 if (Info.empty()) {
331 setHasMetadata(true);
332 } else {
333 // Handle replacement of an existing value.
334 for (unsigned i = 0, e = Info.size(); i != e; ++i)
335 if (Info[i].first == KindID) {
336 Info[i].second = Node;
337 return;
338 }
339 }
340
341 // No replacement, just add it to the list.
342 Info.push_back(std::make_pair(KindID, Node));
343 return;
344 }
345
346 // Otherwise, we're removing metadata from an instruction.
347 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
348 "HasMetadata bit out of date!");
349 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
350
351 // Common case is removing the only entry.
352 if (Info.size() == 1 && Info[0].first == KindID) {
353 getContext().pImpl->MetadataStore.erase(this);
354 setHasMetadata(false);
355 return;
356 }
357
358 // Handle replacement of an existing value.
359 for (unsigned i = 0, e = Info.size(); i != e; ++i)
360 if (Info[i].first == KindID) {
361 Info[i] = Info.back();
362 Info.pop_back();
363 assert(!Info.empty() && "Removing last entry should be handled above");
364 return;
365 }
366 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000367}
368
369MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000370 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
371 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
372
373 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
374 I != E; ++I)
375 if (I->first == KindID)
376 return I->second;
377 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000378}
379
380void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
381 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000382 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
383 "Shouldn't have called this");
384 const LLVMContextImpl::MDMapTy &Info =
385 getContext().pImpl->MetadataStore.find(this)->second;
386 assert(!Info.empty() && "Shouldn't have called this");
387
388 Result.clear();
389 Result.append(Info.begin(), Info.end());
390
391 // Sort the resulting array so it is stable.
392 if (Result.size() > 1)
393 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000394}
395
Chris Lattner68017802009-12-29 07:44:16 +0000396/// removeAllMetadata - Remove all metadata from this instruction.
397void Instruction::removeAllMetadata() {
398 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000399 getContext().pImpl->MetadataStore.erase(this);
400 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000401}
402