blob: b4a981f3a02677339a3479ec786527d16f7d52bd [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
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000124static const Function *getFunctionForValue(Value *V) {
125 if (!V) return NULL;
126 if (Instruction *I = dyn_cast<Instruction>(V))
127 return I->getParent()->getParent();
128 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) return BB->getParent();
129 if (Argument *A = dyn_cast<Argument>(V)) return A->getParent();
130 return NULL;
131}
132
Victor Hernandez8296da72010-01-14 20:12:34 +0000133#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000134static const Function *assertLocalFunction(const MDNode *N) {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000135 if (!N->isFunctionLocal()) return NULL;
Victor Hernandez8296da72010-01-14 20:12:34 +0000136
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000137 const Function *F = NULL, *NewF = NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000138 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000139 if (Value *V = N->getOperand(i)) {
140 if (MDNode *MD = dyn_cast<MDNode>(V)) NewF = assertLocalFunction(MD);
141 else NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000142 }
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000143 if (F && NewF) assert(F == NewF && "inconsistent function-local metadata");
144 else if (!F) F = NewF;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000145 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000146 return F;
147}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000148#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000149
150// getFunction - If this metadata is function-local and recursively has a
151// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000152// Otherwise, return null. getFunction() should not be used for performance-
153// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000154const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000155#ifndef NDEBUG
156 return assertLocalFunction(this);
157#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000158 if (!isFunctionLocal()) return NULL;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000159
160 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000161 if (Value *V = getOperand(i)) {
Chandler Carruthd6514b12010-01-20 06:01:02 +0000162 if (MDNode *MD = dyn_cast<MDNode>(V)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000163 if (const Function *F = MD->getFunction()) return F;
Chandler Carruthd6514b12010-01-20 06:01:02 +0000164 else return getFunctionForValue(V);
165 }
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000166 }
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000167 }
168 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000169}
170
Chris Lattner8cb6c342009-12-31 01:05:46 +0000171// destroy - Delete this node. Only when there are no uses.
172void MDNode::destroy() {
173 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
174 // Placement delete, the free the memory.
175 this->~MDNode();
176 free(this);
177}
178
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000179MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
180 unsigned NumVals, FunctionLocalness FL) {
Devang Patelf7188322009-09-03 01:39:20 +0000181 LLVMContextImpl *pImpl = Context.pImpl;
182 FoldingSetNodeID ID;
183 for (unsigned i = 0; i != NumVals; ++i)
184 ID.AddPointer(Vals[i]);
185
Devang Patelf7188322009-09-03 01:39:20 +0000186 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000187 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000188 if (!N) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000189 bool isFunctionLocal = false;
190 switch (FL) {
191 case FL_Unknown:
192 for (unsigned i = 0; i != NumVals; ++i) {
193 Value *V = Vals[i];
194 if (!V) continue;
195 if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000196 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000197 isFunctionLocal = true;
198 break;
199 }
200 }
201 break;
202 case FL_No:
203 isFunctionLocal = false;
204 break;
205 case FL_Yes:
206 isFunctionLocal = true;
207 break;
208 }
209
Chris Lattner9b493022009-12-31 01:22:29 +0000210 // Coallocate space for the node and Operands together, then placement new.
211 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
Chris Lattner8cb6c342009-12-31 01:05:46 +0000212 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000213
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000214 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000215 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000216 }
Devang Patelf7188322009-09-03 01:39:20 +0000217 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000218}
219
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000220MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
221 return getMDNode(Context, Vals, NumVals, FL_Unknown);
222}
223
224MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
225 unsigned NumVals, bool isFunctionLocal) {
226 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
227}
228
Chris Lattner9b493022009-12-31 01:22:29 +0000229/// getOperand - Return specified operand.
230Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000231 return *getOperandPtr(const_cast<MDNode*>(this), i);
232}
233
Chris Lattnerf543eff2009-12-28 09:12:35 +0000234void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000235 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
236 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000237}
Devang Patel5c310be2009-08-11 18:01:24 +0000238
Chris Lattnerf543eff2009-12-28 09:12:35 +0000239
Chris Lattner9b493022009-12-31 01:22:29 +0000240// Replace value from this node's operand list.
241void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000242 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000243
Chris Lattner95c445d2009-12-28 09:32:10 +0000244 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000245 return;
Devang Patelf7188322009-09-03 01:39:20 +0000246
Chris Lattner30ae06b2009-12-30 21:42:11 +0000247 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000248 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000249
250 // If this node is already not being uniqued (because one of the operands
251 // already went to null), then there is nothing else to do here.
252 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000253
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000254 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
255
256 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
257 // this node to remove it, so we don't care what state the operands are in.
258 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000259
Chris Lattner30ae06b2009-12-30 21:42:11 +0000260 // If we are dropping an argument to null, we choose to not unique the MDNode
261 // anymore. This commonly occurs during destruction, and uniquing these
262 // brings little reuse.
263 if (To == 0) {
264 setIsNotUniqued();
265 return;
266 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000267
Chris Lattner30ae06b2009-12-30 21:42:11 +0000268 // Now that the node is out of the folding set, get ready to reinsert it.
269 // First, check to see if another node with the same operands already exists
270 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000271 FoldingSetNodeID ID;
272 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000273 void *InsertPoint;
274 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000275
276 if (N) {
277 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000278 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000279 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
280 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000281 }
282
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000283 // InsertPoint will have been set by the FindNodeOrInsertPos call.
284 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000285}
286
Devang Patel05a26fb2009-07-29 00:33:07 +0000287//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000288// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000289//
Devang Patel943ddf62010-01-12 18:34:06 +0000290
291namespace llvm {
292// SymbolTableListTraits specialization for MDSymbolTable.
293void ilist_traits<NamedMDNode>
294::addNodeToList(NamedMDNode *N) {
295 assert(N->getParent() == 0 && "Value already in a container!!");
296 Module *Owner = getListOwner();
297 N->setParent(Owner);
298 MDSymbolTable &ST = Owner->getMDSymbolTable();
299 ST.insert(N->getName(), N);
300}
301
302void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
303 N->setParent(0);
304 Module *Owner = getListOwner();
305 MDSymbolTable &ST = Owner->getMDSymbolTable();
306 ST.remove(N->getName());
307}
308}
309
Devang Patele3073482010-01-05 20:41:31 +0000310static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
311 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000312}
313
Devang Patelf76941e2010-01-12 18:57:56 +0000314NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000315 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000316 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000317 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000318 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000319 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000320
Devang Patele3073482010-01-05 20:41:31 +0000321 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000322 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000323 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000324
Devang Patel943ddf62010-01-12 18:34:06 +0000325 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000326 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000327}
Devang Patel79238d72009-08-03 06:19:01 +0000328
Devang Patel5c310be2009-08-11 18:01:24 +0000329NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000330 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000331 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000332 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000333
Chris Lattner9b493022009-12-31 01:22:29 +0000334 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
335 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000336 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
337 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000338}
339
Chris Lattner1bc810b2009-12-28 08:07:14 +0000340NamedMDNode::~NamedMDNode() {
341 dropAllReferences();
342 delete &getNMDOps(Operands);
343}
344
Chris Lattner9b493022009-12-31 01:22:29 +0000345/// getNumOperands - Return number of NamedMDNode operands.
346unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000347 return (unsigned)getNMDOps(Operands).size();
348}
349
Chris Lattner9b493022009-12-31 01:22:29 +0000350/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000351MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000352 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000353 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000354}
355
Chris Lattner9b493022009-12-31 01:22:29 +0000356/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000357void NamedMDNode::addOperand(MDNode *M) {
358 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000359}
360
Devang Patel79238d72009-08-03 06:19:01 +0000361/// eraseFromParent - Drop all references and remove the node from parent
362/// module.
363void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000364 getParent()->getNamedMDList().erase(this);
365}
366
367/// dropAllReferences - Remove all uses and clear node vector.
368void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000369 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000370}
371
Devang Patelfcfee0f2010-01-07 19:39:36 +0000372/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000373void NamedMDNode::setName(const Twine &NewName) {
374 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
375
376 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000377 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000378
Devang Patelf76941e2010-01-12 18:57:56 +0000379 // Name isn't changing?
380 if (getName() == NameRef)
381 return;
382
383 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000384 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000385 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000386}
387
388/// getName - Return a constant reference to this named metadata's name.
389StringRef NamedMDNode::getName() const {
390 return StringRef(Name);
391}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000392
393//===----------------------------------------------------------------------===//
Chris Lattner8cb6c342009-12-31 01:05:46 +0000394// LLVMContext MDKind naming implementation.
Devang Patel1155fdf2009-10-22 19:36:54 +0000395//
Devang Patel1155fdf2009-10-22 19:36:54 +0000396
Chris Lattner5508da32009-12-29 07:56:15 +0000397#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000398/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000399static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000400 if (MDName.empty())
401 return false;
402
403 if (!isalpha(MDName[0]))
404 return false;
405
406 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
407 ++I) {
408 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
409 return false;
410 }
411 return true;
412}
Chris Lattner5508da32009-12-29 07:56:15 +0000413#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000414
Chris Lattner70939462009-12-28 20:45:51 +0000415/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000416unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000417 assert(isValidName(Name) && "Invalid MDNode name");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000418
Chris Lattnera0566972009-12-29 09:01:33 +0000419 unsigned &Entry = pImpl->CustomMDKindNames[Name];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000420
Chris Lattnera0566972009-12-29 09:01:33 +0000421 // If this is new, assign it its ID.
422 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
423 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000424}
425
Devang Patel1155fdf2009-10-22 19:36:54 +0000426/// getHandlerNames - Populate client supplied smallvector using custome
427/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000428void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
429 Names.resize(pImpl->CustomMDKindNames.size()+1);
430 Names[0] = "";
431 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000432 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera0566972009-12-29 09:01:33 +0000433 // MD Handlers are numbered from 1.
434 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000435}
436
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000437//===----------------------------------------------------------------------===//
438// Instruction Metadata method implementations.
439//
440
441void Instruction::setMetadata(const char *Kind, MDNode *Node) {
442 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000443 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000444}
445
446MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000447 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000448}
449
450/// setMetadata - Set the metadata of of the specified kind to the specified
451/// node. This updates/replaces metadata if already present, or removes it if
452/// Node is null.
453void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
454 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000455
Chris Lattnera0566972009-12-29 09:01:33 +0000456 // Handle the case when we're adding/updating metadata on an instruction.
457 if (Node) {
458 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
459 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
460 if (Info.empty()) {
461 setHasMetadata(true);
462 } else {
463 // Handle replacement of an existing value.
464 for (unsigned i = 0, e = Info.size(); i != e; ++i)
465 if (Info[i].first == KindID) {
466 Info[i].second = Node;
467 return;
468 }
469 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000470
Chris Lattnera0566972009-12-29 09:01:33 +0000471 // No replacement, just add it to the list.
472 Info.push_back(std::make_pair(KindID, Node));
473 return;
474 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000475
Chris Lattnera0566972009-12-29 09:01:33 +0000476 // Otherwise, we're removing metadata from an instruction.
477 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
478 "HasMetadata bit out of date!");
479 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000480
Chris Lattnera0566972009-12-29 09:01:33 +0000481 // Common case is removing the only entry.
482 if (Info.size() == 1 && Info[0].first == KindID) {
483 getContext().pImpl->MetadataStore.erase(this);
484 setHasMetadata(false);
485 return;
486 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000487
Chris Lattnera0566972009-12-29 09:01:33 +0000488 // Handle replacement of an existing value.
489 for (unsigned i = 0, e = Info.size(); i != e; ++i)
490 if (Info[i].first == KindID) {
491 Info[i] = Info.back();
492 Info.pop_back();
493 assert(!Info.empty() && "Removing last entry should be handled above");
494 return;
495 }
496 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000497}
498
499MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000500 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
501 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000502
Chris Lattnera0566972009-12-29 09:01:33 +0000503 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
504 I != E; ++I)
505 if (I->first == KindID)
506 return I->second;
507 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000508}
509
510void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
511 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000512 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
513 "Shouldn't have called this");
514 const LLVMContextImpl::MDMapTy &Info =
515 getContext().pImpl->MetadataStore.find(this)->second;
516 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000517
Chris Lattnera0566972009-12-29 09:01:33 +0000518 Result.clear();
519 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000520
Chris Lattnera0566972009-12-29 09:01:33 +0000521 // Sort the resulting array so it is stable.
522 if (Result.size() > 1)
523 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000524}
525
Chris Lattner68017802009-12-29 07:44:16 +0000526/// removeAllMetadata - Remove all metadata from this instruction.
527void Instruction::removeAllMetadata() {
528 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000529 getContext().pImpl->MetadataStore.erase(this);
530 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000531}
532