blob: 14993134c7fff25a67393a6225360a4bbc368149 [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;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000034 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000035 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;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000043 StringMapEntry<MDString *> &Entry =
Devang Patel862ef782009-11-12 00:50:58 +000044 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 Lattner9b493022009-12-31 01:22:29 +000051// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000052//
53
Chris Lattner9b493022009-12-31 01:22:29 +000054// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000055namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000056class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000057 MDNode *Parent;
58public:
Chris Lattner9b493022009-12-31 01:22:29 +000059 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
60 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000061
Chris Lattner8cb6c342009-12-31 01:05:46 +000062 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000063 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000064 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000065
Chris Lattner74a6ad62009-12-28 07:41:54 +000066 virtual void deleted();
67 virtual void allUsesReplacedWith(Value *NV);
68};
69} // end namespace llvm.
70
71
Chris Lattner9b493022009-12-31 01:22:29 +000072void MDNodeOperand::deleted() {
73 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000074}
75
Chris Lattner9b493022009-12-31 01:22:29 +000076void MDNodeOperand::allUsesReplacedWith(Value *NV) {
77 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000078}
79
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000083// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000084//
Chris Lattner74a6ad62009-12-28 07:41:54 +000085
Chris Lattner9b493022009-12-31 01:22:29 +000086/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000087/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000088static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
89 assert(Op < N->getNumOperands() && "Invalid operand number");
90 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000091}
92
Victor Hernandezdd7418a2009-12-16 02:52:09 +000093MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000094 bool isFunctionLocal)
Chris Lattner8cb6c342009-12-31 01:05:46 +000095: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000096 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000097
Victor Hernandez0471abd2009-12-18 20:09:14 +000098 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000099 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000100
101 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +0000102 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000103 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +0000104 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +0000105}
106
Chris Lattner8cb6c342009-12-31 01:05:46 +0000107
108/// ~MDNode - Destroy MDNode.
109MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000110 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000111 "Not being destroyed through destroy()?");
112 if (!isNotUniqued()) {
113 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
114 pImpl->MDNodeSet.RemoveNode(this);
115 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000116
Chris Lattner8cb6c342009-12-31 01:05:46 +0000117 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000118 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000119 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000120 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000121}
122
123// destroy - Delete this node. Only when there are no uses.
124void MDNode::destroy() {
125 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
126 // Placement delete, the free the memory.
127 this->~MDNode();
128 free(this);
129}
130
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000131MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
132 unsigned NumVals, FunctionLocalness FL) {
Devang Patelf7188322009-09-03 01:39:20 +0000133 LLVMContextImpl *pImpl = Context.pImpl;
134 FoldingSetNodeID ID;
135 for (unsigned i = 0; i != NumVals; ++i)
136 ID.AddPointer(Vals[i]);
137
Devang Patelf7188322009-09-03 01:39:20 +0000138 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000139 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000140 if (!N) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000141 bool isFunctionLocal = false;
142 switch (FL) {
143 case FL_Unknown:
144 for (unsigned i = 0; i != NumVals; ++i) {
145 Value *V = Vals[i];
146 if (!V) continue;
147 if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000148 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000149 isFunctionLocal = true;
150 break;
151 }
152 }
153 break;
154 case FL_No:
155 isFunctionLocal = false;
156 break;
157 case FL_Yes:
158 isFunctionLocal = true;
159 break;
160 }
161
Chris Lattner9b493022009-12-31 01:22:29 +0000162 // Coallocate space for the node and Operands together, then placement new.
163 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
Chris Lattner8cb6c342009-12-31 01:05:46 +0000164 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000165
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000166 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000167 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000168 }
Devang Patelf7188322009-09-03 01:39:20 +0000169 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000170}
171
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000172MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
173 return getMDNode(Context, Vals, NumVals, FL_Unknown);
174}
175
176MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
177 unsigned NumVals, bool isFunctionLocal) {
178 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
179}
180
Chris Lattner9b493022009-12-31 01:22:29 +0000181/// getOperand - Return specified operand.
182Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000183 return *getOperandPtr(const_cast<MDNode*>(this), i);
184}
185
Chris Lattnerf543eff2009-12-28 09:12:35 +0000186void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000187 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
188 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000189}
Devang Patel5c310be2009-08-11 18:01:24 +0000190
Chris Lattnerf543eff2009-12-28 09:12:35 +0000191
Chris Lattner9b493022009-12-31 01:22:29 +0000192// Replace value from this node's operand list.
193void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000194 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000195
Chris Lattner95c445d2009-12-28 09:32:10 +0000196 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000197 return;
Devang Patelf7188322009-09-03 01:39:20 +0000198
Chris Lattner30ae06b2009-12-30 21:42:11 +0000199 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000200 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000201
202 // If this node is already not being uniqued (because one of the operands
203 // already went to null), then there is nothing else to do here.
204 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000205
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000206 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
207
208 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
209 // this node to remove it, so we don't care what state the operands are in.
210 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000211
Chris Lattner30ae06b2009-12-30 21:42:11 +0000212 // If we are dropping an argument to null, we choose to not unique the MDNode
213 // anymore. This commonly occurs during destruction, and uniquing these
214 // brings little reuse.
215 if (To == 0) {
216 setIsNotUniqued();
217 return;
218 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000219
Chris Lattner30ae06b2009-12-30 21:42:11 +0000220 // Now that the node is out of the folding set, get ready to reinsert it.
221 // First, check to see if another node with the same operands already exists
222 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000223 FoldingSetNodeID ID;
224 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000225 void *InsertPoint;
226 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000227
228 if (N) {
229 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000230 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000231 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
232 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000233 }
234
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000235 // InsertPoint will have been set by the FindNodeOrInsertPos call.
236 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000237}
238
Devang Patel05a26fb2009-07-29 00:33:07 +0000239//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000240// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000241//
Devang Patele3073482010-01-05 20:41:31 +0000242static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
243 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000244}
245
Devang Patelfcfee0f2010-01-07 19:39:36 +0000246NamedMDNode::NamedMDNode(LLVMContext &C, StringRef N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000247 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000248 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000249 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000250 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000251 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000252
Devang Patele3073482010-01-05 20:41:31 +0000253 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000254 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000255 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000256
Devang Patelfcfee0f2010-01-07 19:39:36 +0000257 if (ParentModule) {
Devang Patel4a942d02009-07-29 21:58:56 +0000258 ParentModule->getNamedMDList().push_back(this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000259 ParentModule->addMDNodeName(N, this);
260 }
Devang Patel05a26fb2009-07-29 00:33:07 +0000261}
Devang Patel79238d72009-08-03 06:19:01 +0000262
Devang Patel5c310be2009-08-11 18:01:24 +0000263NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000264 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000265 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000266 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000267
Chris Lattner9b493022009-12-31 01:22:29 +0000268 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
269 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000270 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
271 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000272}
273
Chris Lattner1bc810b2009-12-28 08:07:14 +0000274NamedMDNode::~NamedMDNode() {
275 dropAllReferences();
276 delete &getNMDOps(Operands);
277}
278
Chris Lattner9b493022009-12-31 01:22:29 +0000279/// getNumOperands - Return number of NamedMDNode operands.
280unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000281 return (unsigned)getNMDOps(Operands).size();
282}
283
Chris Lattner9b493022009-12-31 01:22:29 +0000284/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000285MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000286 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000287 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000288}
289
Chris Lattner9b493022009-12-31 01:22:29 +0000290/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000291void NamedMDNode::addOperand(MDNode *M) {
292 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000293}
294
Devang Patel79238d72009-08-03 06:19:01 +0000295/// eraseFromParent - Drop all references and remove the node from parent
296/// module.
297void NamedMDNode::eraseFromParent() {
Devang Patelfcfee0f2010-01-07 19:39:36 +0000298 getParent()->getMDSymbolTable().remove(getName());
Devang Patel79238d72009-08-03 06:19:01 +0000299 getParent()->getNamedMDList().erase(this);
300}
301
302/// dropAllReferences - Remove all uses and clear node vector.
303void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000304 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000305}
306
Devang Patelfcfee0f2010-01-07 19:39:36 +0000307/// setName - Set the name of this named metadata.
308void NamedMDNode::setName(StringRef N) {
309 if (!N.empty())
310 Name = N.str();
311}
312
313/// getName - Return a constant reference to this named metadata's name.
314StringRef NamedMDNode::getName() const {
315 return StringRef(Name);
316}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000317
318//===----------------------------------------------------------------------===//
Chris Lattner8cb6c342009-12-31 01:05:46 +0000319// LLVMContext MDKind naming implementation.
Devang Patel1155fdf2009-10-22 19:36:54 +0000320//
Devang Patel1155fdf2009-10-22 19:36:54 +0000321
Chris Lattner5508da32009-12-29 07:56:15 +0000322#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000323/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000324static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000325 if (MDName.empty())
326 return false;
327
328 if (!isalpha(MDName[0]))
329 return false;
330
331 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
332 ++I) {
333 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
334 return false;
335 }
336 return true;
337}
Chris Lattner5508da32009-12-29 07:56:15 +0000338#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000339
Chris Lattner70939462009-12-28 20:45:51 +0000340/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000341unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000342 assert(isValidName(Name) && "Invalid MDNode name");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000343
Chris Lattnera0566972009-12-29 09:01:33 +0000344 unsigned &Entry = pImpl->CustomMDKindNames[Name];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000345
Chris Lattnera0566972009-12-29 09:01:33 +0000346 // If this is new, assign it its ID.
347 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
348 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000349}
350
Devang Patel1155fdf2009-10-22 19:36:54 +0000351/// getHandlerNames - Populate client supplied smallvector using custome
352/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000353void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
354 Names.resize(pImpl->CustomMDKindNames.size()+1);
355 Names[0] = "";
356 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000357 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera0566972009-12-29 09:01:33 +0000358 // MD Handlers are numbered from 1.
359 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000360}
361
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000362//===----------------------------------------------------------------------===//
363// Instruction Metadata method implementations.
364//
365
366void Instruction::setMetadata(const char *Kind, MDNode *Node) {
367 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000368 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000369}
370
371MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000372 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000373}
374
375/// setMetadata - Set the metadata of of the specified kind to the specified
376/// node. This updates/replaces metadata if already present, or removes it if
377/// Node is null.
378void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
379 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000380
Chris Lattnera0566972009-12-29 09:01:33 +0000381 // Handle the case when we're adding/updating metadata on an instruction.
382 if (Node) {
383 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
384 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
385 if (Info.empty()) {
386 setHasMetadata(true);
387 } else {
388 // Handle replacement of an existing value.
389 for (unsigned i = 0, e = Info.size(); i != e; ++i)
390 if (Info[i].first == KindID) {
391 Info[i].second = Node;
392 return;
393 }
394 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000395
Chris Lattnera0566972009-12-29 09:01:33 +0000396 // No replacement, just add it to the list.
397 Info.push_back(std::make_pair(KindID, Node));
398 return;
399 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000400
Chris Lattnera0566972009-12-29 09:01:33 +0000401 // Otherwise, we're removing metadata from an instruction.
402 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
403 "HasMetadata bit out of date!");
404 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000405
Chris Lattnera0566972009-12-29 09:01:33 +0000406 // Common case is removing the only entry.
407 if (Info.size() == 1 && Info[0].first == KindID) {
408 getContext().pImpl->MetadataStore.erase(this);
409 setHasMetadata(false);
410 return;
411 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000412
Chris Lattnera0566972009-12-29 09:01:33 +0000413 // Handle replacement of an existing value.
414 for (unsigned i = 0, e = Info.size(); i != e; ++i)
415 if (Info[i].first == KindID) {
416 Info[i] = Info.back();
417 Info.pop_back();
418 assert(!Info.empty() && "Removing last entry should be handled above");
419 return;
420 }
421 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000422}
423
424MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000425 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
426 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000427
Chris Lattnera0566972009-12-29 09:01:33 +0000428 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
429 I != E; ++I)
430 if (I->first == KindID)
431 return I->second;
432 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000433}
434
435void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
436 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000437 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
438 "Shouldn't have called this");
439 const LLVMContextImpl::MDMapTy &Info =
440 getContext().pImpl->MetadataStore.find(this)->second;
441 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000442
Chris Lattnera0566972009-12-29 09:01:33 +0000443 Result.clear();
444 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000445
Chris Lattnera0566972009-12-29 09:01:33 +0000446 // Sort the resulting array so it is stable.
447 if (Result.size() > 1)
448 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000449}
450
Chris Lattner68017802009-12-29 07:44:16 +0000451/// removeAllMetadata - Remove all metadata from this instruction.
452void Instruction::removeAllMetadata() {
453 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000454 getContext().pImpl->MetadataStore.erase(this);
455 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000456}
457