blob: bac89bfd7b6633e9e2dc1973206405e06b085f3f [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 Patelf76941e2010-01-12 18:57:56 +000021#include "llvm/ADT/SmallString.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000022#include "SymbolTableListTraitsImpl.h"
Chris Lattner1300f452009-12-28 08:24:16 +000023#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000024using namespace llvm;
25
26//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000027// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000028//
Chris Lattner5a409bd2009-12-28 08:30:43 +000029
30MDString::MDString(LLVMContext &C, StringRef S)
31 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
32
Devang Pateldcb99d32009-10-22 00:10:15 +000033MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000034 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000035 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000036 pImpl->MDStringCache.GetOrCreateValue(Str);
37 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000038 if (!S) S = new MDString(Context, Entry.getKey());
39 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000040}
41
Devang Patel862ef782009-11-12 00:50:58 +000042MDString *MDString::get(LLVMContext &Context, const char *Str) {
43 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000044 StringMapEntry<MDString *> &Entry =
Devang Patel862ef782009-11-12 00:50:58 +000045 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
46 MDString *&S = Entry.getValue();
Nick Lewycky6e052512009-11-27 19:57:53 +000047 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000048 return S;
Devang Patel862ef782009-11-12 00:50:58 +000049}
50
Owen Anderson0087fe62009-07-31 21:35:40 +000051//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000052// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000053//
54
Chris Lattner9b493022009-12-31 01:22:29 +000055// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000056namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000057class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000058 MDNode *Parent;
59public:
Chris Lattner9b493022009-12-31 01:22:29 +000060 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
61 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000062
Chris Lattner8cb6c342009-12-31 01:05:46 +000063 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000064 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000065 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000066
Chris Lattner74a6ad62009-12-28 07:41:54 +000067 virtual void deleted();
68 virtual void allUsesReplacedWith(Value *NV);
69};
70} // end namespace llvm.
71
72
Chris Lattner9b493022009-12-31 01:22:29 +000073void MDNodeOperand::deleted() {
74 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000075}
76
Chris Lattner9b493022009-12-31 01:22:29 +000077void MDNodeOperand::allUsesReplacedWith(Value *NV) {
78 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000079}
80
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000084// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000085//
Chris Lattner74a6ad62009-12-28 07:41:54 +000086
Chris Lattner9b493022009-12-31 01:22:29 +000087/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000088/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000089static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
90 assert(Op < N->getNumOperands() && "Invalid operand number");
91 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000092}
93
Victor Hernandezdd7418a2009-12-16 02:52:09 +000094MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000095 bool isFunctionLocal)
Chris Lattner8cb6c342009-12-31 01:05:46 +000096: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000097 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000098
Victor Hernandez0471abd2009-12-18 20:09:14 +000099 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000100 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000101
102 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +0000103 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000104 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +0000105 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +0000106}
107
Chris Lattner8cb6c342009-12-31 01:05:46 +0000108
109/// ~MDNode - Destroy MDNode.
110MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000111 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000112 "Not being destroyed through destroy()?");
113 if (!isNotUniqued()) {
114 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
115 pImpl->MDNodeSet.RemoveNode(this);
116 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000117
Chris Lattner8cb6c342009-12-31 01:05:46 +0000118 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000119 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000120 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000121 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000122}
123
124// destroy - Delete this node. Only when there are no uses.
125void MDNode::destroy() {
126 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
127 // Placement delete, the free the memory.
128 this->~MDNode();
129 free(this);
130}
131
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000132MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
133 unsigned NumVals, FunctionLocalness FL) {
Devang Patelf7188322009-09-03 01:39:20 +0000134 LLVMContextImpl *pImpl = Context.pImpl;
135 FoldingSetNodeID ID;
136 for (unsigned i = 0; i != NumVals; ++i)
137 ID.AddPointer(Vals[i]);
138
Devang Patelf7188322009-09-03 01:39:20 +0000139 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000140 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000141 if (!N) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000142 bool isFunctionLocal = false;
143 switch (FL) {
144 case FL_Unknown:
145 for (unsigned i = 0; i != NumVals; ++i) {
146 Value *V = Vals[i];
147 if (!V) continue;
148 if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000149 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000150 isFunctionLocal = true;
151 break;
152 }
153 }
154 break;
155 case FL_No:
156 isFunctionLocal = false;
157 break;
158 case FL_Yes:
159 isFunctionLocal = true;
160 break;
161 }
162
Chris Lattner9b493022009-12-31 01:22:29 +0000163 // Coallocate space for the node and Operands together, then placement new.
164 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
Chris Lattner8cb6c342009-12-31 01:05:46 +0000165 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000166
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000167 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000168 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000169 }
Devang Patelf7188322009-09-03 01:39:20 +0000170 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000171}
172
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000173MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
174 return getMDNode(Context, Vals, NumVals, FL_Unknown);
175}
176
177MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
178 unsigned NumVals, bool isFunctionLocal) {
179 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
180}
181
Chris Lattner9b493022009-12-31 01:22:29 +0000182/// getOperand - Return specified operand.
183Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000184 return *getOperandPtr(const_cast<MDNode*>(this), i);
185}
186
Chris Lattnerf543eff2009-12-28 09:12:35 +0000187void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000188 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
189 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000190}
Devang Patel5c310be2009-08-11 18:01:24 +0000191
Chris Lattnerf543eff2009-12-28 09:12:35 +0000192
Chris Lattner9b493022009-12-31 01:22:29 +0000193// Replace value from this node's operand list.
194void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000195 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000196
Chris Lattner95c445d2009-12-28 09:32:10 +0000197 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000198 return;
Devang Patelf7188322009-09-03 01:39:20 +0000199
Chris Lattner30ae06b2009-12-30 21:42:11 +0000200 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000201 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000202
203 // If this node is already not being uniqued (because one of the operands
204 // already went to null), then there is nothing else to do here.
205 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000206
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000207 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
208
209 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
210 // this node to remove it, so we don't care what state the operands are in.
211 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000212
Chris Lattner30ae06b2009-12-30 21:42:11 +0000213 // If we are dropping an argument to null, we choose to not unique the MDNode
214 // anymore. This commonly occurs during destruction, and uniquing these
215 // brings little reuse.
216 if (To == 0) {
217 setIsNotUniqued();
218 return;
219 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000220
Chris Lattner30ae06b2009-12-30 21:42:11 +0000221 // Now that the node is out of the folding set, get ready to reinsert it.
222 // First, check to see if another node with the same operands already exists
223 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000224 FoldingSetNodeID ID;
225 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000226 void *InsertPoint;
227 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000228
229 if (N) {
230 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000231 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000232 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
233 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000234 }
235
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000236 // InsertPoint will have been set by the FindNodeOrInsertPos call.
237 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000238}
239
Devang Patel05a26fb2009-07-29 00:33:07 +0000240//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000241// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000242//
Devang Patel943ddf62010-01-12 18:34:06 +0000243
244namespace llvm {
245// SymbolTableListTraits specialization for MDSymbolTable.
246void ilist_traits<NamedMDNode>
247::addNodeToList(NamedMDNode *N) {
248 assert(N->getParent() == 0 && "Value already in a container!!");
249 Module *Owner = getListOwner();
250 N->setParent(Owner);
251 MDSymbolTable &ST = Owner->getMDSymbolTable();
252 ST.insert(N->getName(), N);
253}
254
255void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
256 N->setParent(0);
257 Module *Owner = getListOwner();
258 MDSymbolTable &ST = Owner->getMDSymbolTable();
259 ST.remove(N->getName());
260}
261}
262
Devang Patele3073482010-01-05 20:41:31 +0000263static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
264 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000265}
266
Devang Patelf76941e2010-01-12 18:57:56 +0000267NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000268 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000269 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000270 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000271 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000272 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000273
Devang Patele3073482010-01-05 20:41:31 +0000274 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000275 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000276 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000277
Devang Patel943ddf62010-01-12 18:34:06 +0000278 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000279 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000280}
Devang Patel79238d72009-08-03 06:19:01 +0000281
Devang Patel5c310be2009-08-11 18:01:24 +0000282NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000283 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000284 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000285 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000286
Chris Lattner9b493022009-12-31 01:22:29 +0000287 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
288 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000289 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
290 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000291}
292
Chris Lattner1bc810b2009-12-28 08:07:14 +0000293NamedMDNode::~NamedMDNode() {
294 dropAllReferences();
295 delete &getNMDOps(Operands);
296}
297
Chris Lattner9b493022009-12-31 01:22:29 +0000298/// getNumOperands - Return number of NamedMDNode operands.
299unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000300 return (unsigned)getNMDOps(Operands).size();
301}
302
Chris Lattner9b493022009-12-31 01:22:29 +0000303/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000304MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000305 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000306 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000307}
308
Chris Lattner9b493022009-12-31 01:22:29 +0000309/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000310void NamedMDNode::addOperand(MDNode *M) {
311 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000312}
313
Devang Patel79238d72009-08-03 06:19:01 +0000314/// eraseFromParent - Drop all references and remove the node from parent
315/// module.
316void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000317 getParent()->getNamedMDList().erase(this);
318}
319
320/// dropAllReferences - Remove all uses and clear node vector.
321void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000322 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000323}
324
Devang Patelfcfee0f2010-01-07 19:39:36 +0000325/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000326void NamedMDNode::setName(const Twine &NewName) {
327 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
328
329 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000330 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000331
Devang Patelf76941e2010-01-12 18:57:56 +0000332 // Name isn't changing?
333 if (getName() == NameRef)
334 return;
335
336 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000337 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000338 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000339}
340
341/// getName - Return a constant reference to this named metadata's name.
342StringRef NamedMDNode::getName() const {
343 return StringRef(Name);
344}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000345
346//===----------------------------------------------------------------------===//
Chris Lattner8cb6c342009-12-31 01:05:46 +0000347// LLVMContext MDKind naming implementation.
Devang Patel1155fdf2009-10-22 19:36:54 +0000348//
Devang Patel1155fdf2009-10-22 19:36:54 +0000349
Chris Lattner5508da32009-12-29 07:56:15 +0000350#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000351/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000352static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000353 if (MDName.empty())
354 return false;
355
356 if (!isalpha(MDName[0]))
357 return false;
358
359 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
360 ++I) {
361 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
362 return false;
363 }
364 return true;
365}
Chris Lattner5508da32009-12-29 07:56:15 +0000366#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000367
Chris Lattner70939462009-12-28 20:45:51 +0000368/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000369unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000370 assert(isValidName(Name) && "Invalid MDNode name");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000371
Chris Lattnera0566972009-12-29 09:01:33 +0000372 unsigned &Entry = pImpl->CustomMDKindNames[Name];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000373
Chris Lattnera0566972009-12-29 09:01:33 +0000374 // If this is new, assign it its ID.
375 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
376 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000377}
378
Devang Patel1155fdf2009-10-22 19:36:54 +0000379/// getHandlerNames - Populate client supplied smallvector using custome
380/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000381void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
382 Names.resize(pImpl->CustomMDKindNames.size()+1);
383 Names[0] = "";
384 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000385 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera0566972009-12-29 09:01:33 +0000386 // MD Handlers are numbered from 1.
387 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000388}
389
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000390//===----------------------------------------------------------------------===//
391// Instruction Metadata method implementations.
392//
393
394void Instruction::setMetadata(const char *Kind, MDNode *Node) {
395 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000396 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000397}
398
399MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000400 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000401}
402
403/// setMetadata - Set the metadata of of the specified kind to the specified
404/// node. This updates/replaces metadata if already present, or removes it if
405/// Node is null.
406void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
407 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000408
Chris Lattnera0566972009-12-29 09:01:33 +0000409 // Handle the case when we're adding/updating metadata on an instruction.
410 if (Node) {
411 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
412 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
413 if (Info.empty()) {
414 setHasMetadata(true);
415 } else {
416 // Handle replacement of an existing value.
417 for (unsigned i = 0, e = Info.size(); i != e; ++i)
418 if (Info[i].first == KindID) {
419 Info[i].second = Node;
420 return;
421 }
422 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000423
Chris Lattnera0566972009-12-29 09:01:33 +0000424 // No replacement, just add it to the list.
425 Info.push_back(std::make_pair(KindID, Node));
426 return;
427 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000428
Chris Lattnera0566972009-12-29 09:01:33 +0000429 // Otherwise, we're removing metadata from an instruction.
430 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
431 "HasMetadata bit out of date!");
432 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000433
Chris Lattnera0566972009-12-29 09:01:33 +0000434 // Common case is removing the only entry.
435 if (Info.size() == 1 && Info[0].first == KindID) {
436 getContext().pImpl->MetadataStore.erase(this);
437 setHasMetadata(false);
438 return;
439 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000440
Chris Lattnera0566972009-12-29 09:01:33 +0000441 // Handle replacement of an existing value.
442 for (unsigned i = 0, e = Info.size(); i != e; ++i)
443 if (Info[i].first == KindID) {
444 Info[i] = Info.back();
445 Info.pop_back();
446 assert(!Info.empty() && "Removing last entry should be handled above");
447 return;
448 }
449 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000450}
451
452MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000453 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
454 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000455
Chris Lattnera0566972009-12-29 09:01:33 +0000456 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
457 I != E; ++I)
458 if (I->first == KindID)
459 return I->second;
460 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000461}
462
463void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
464 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000465 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
466 "Shouldn't have called this");
467 const LLVMContextImpl::MDMapTy &Info =
468 getContext().pImpl->MetadataStore.find(this)->second;
469 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000470
Chris Lattnera0566972009-12-29 09:01:33 +0000471 Result.clear();
472 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000473
Chris Lattnera0566972009-12-29 09:01:33 +0000474 // Sort the resulting array so it is stable.
475 if (Result.size() > 1)
476 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000477}
478
Chris Lattner68017802009-12-29 07:44:16 +0000479/// removeAllMetadata - Remove all metadata from this instruction.
480void Instruction::removeAllMetadata() {
481 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000482 getContext().pImpl->MetadataStore.erase(this);
483 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000484}
485