blob: acc3b8ee5ef0f97f07f4b52e566de0f0b3aef00e [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) {
Victor Hernandezd0b2ff92010-01-20 06:22:33 +0000125 assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000126 if (!V) return NULL;
127 if (Instruction *I = dyn_cast<Instruction>(V))
128 return I->getParent()->getParent();
129 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) return BB->getParent();
130 if (Argument *A = dyn_cast<Argument>(V)) return A->getParent();
131 return NULL;
132}
133
Victor Hernandez8296da72010-01-14 20:12:34 +0000134#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000135static const Function *assertLocalFunction(const MDNode *N) {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000136 if (!N->isFunctionLocal()) return NULL;
Victor Hernandez8296da72010-01-14 20:12:34 +0000137
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000138 const Function *F = NULL, *NewF = NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000139 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000140 if (Value *V = N->getOperand(i)) {
141 if (MDNode *MD = dyn_cast<MDNode>(V)) NewF = assertLocalFunction(MD);
142 else NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000143 }
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000144 if (F && NewF) assert(F == NewF && "inconsistent function-local metadata");
145 else if (!F) F = NewF;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000146 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000147 return F;
148}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000149#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000150
151// getFunction - If this metadata is function-local and recursively has a
152// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000153// Otherwise, return null. getFunction() should not be used for performance-
154// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000155const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000156#ifndef NDEBUG
157 return assertLocalFunction(this);
158#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000159 if (!isFunctionLocal()) return NULL;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000160
161 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000162 if (Value *V = getOperand(i)) {
Chandler Carruthd6514b12010-01-20 06:01:02 +0000163 if (MDNode *MD = dyn_cast<MDNode>(V)) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000164 if (const Function *F = MD->getFunction()) return F;
Victor Hernandezd0b2ff92010-01-20 06:22:33 +0000165 } else {
166 return getFunctionForValue(V);
Chandler Carruthd6514b12010-01-20 06:01:02 +0000167 }
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000168 }
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000169 }
170 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000171}
172
Chris Lattner8cb6c342009-12-31 01:05:46 +0000173// destroy - Delete this node. Only when there are no uses.
174void MDNode::destroy() {
175 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
176 // Placement delete, the free the memory.
177 this->~MDNode();
178 free(this);
179}
180
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000181MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
182 unsigned NumVals, FunctionLocalness FL) {
Devang Patelf7188322009-09-03 01:39:20 +0000183 LLVMContextImpl *pImpl = Context.pImpl;
184 FoldingSetNodeID ID;
185 for (unsigned i = 0; i != NumVals; ++i)
186 ID.AddPointer(Vals[i]);
187
Devang Patelf7188322009-09-03 01:39:20 +0000188 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000189 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000190 if (!N) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000191 bool isFunctionLocal = false;
192 switch (FL) {
193 case FL_Unknown:
194 for (unsigned i = 0; i != NumVals; ++i) {
195 Value *V = Vals[i];
196 if (!V) continue;
197 if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000198 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000199 isFunctionLocal = true;
200 break;
201 }
202 }
203 break;
204 case FL_No:
205 isFunctionLocal = false;
206 break;
207 case FL_Yes:
208 isFunctionLocal = true;
209 break;
210 }
211
Chris Lattner9b493022009-12-31 01:22:29 +0000212 // Coallocate space for the node and Operands together, then placement new.
213 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
Chris Lattner8cb6c342009-12-31 01:05:46 +0000214 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000215
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000216 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000217 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000218 }
Devang Patelf7188322009-09-03 01:39:20 +0000219 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000220}
221
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000222MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
223 return getMDNode(Context, Vals, NumVals, FL_Unknown);
224}
225
226MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
227 unsigned NumVals, bool isFunctionLocal) {
228 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
229}
230
Chris Lattner9b493022009-12-31 01:22:29 +0000231/// getOperand - Return specified operand.
232Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000233 return *getOperandPtr(const_cast<MDNode*>(this), i);
234}
235
Chris Lattnerf543eff2009-12-28 09:12:35 +0000236void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000237 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
238 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000239}
Devang Patel5c310be2009-08-11 18:01:24 +0000240
Chris Lattnerf543eff2009-12-28 09:12:35 +0000241
Chris Lattner9b493022009-12-31 01:22:29 +0000242// Replace value from this node's operand list.
243void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000244 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000245
Chris Lattner95c445d2009-12-28 09:32:10 +0000246 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000247 return;
Devang Patelf7188322009-09-03 01:39:20 +0000248
Chris Lattner30ae06b2009-12-30 21:42:11 +0000249 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000250 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000251
252 // If this node is already not being uniqued (because one of the operands
253 // already went to null), then there is nothing else to do here.
254 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000255
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000256 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
257
258 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
259 // this node to remove it, so we don't care what state the operands are in.
260 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000261
Chris Lattner30ae06b2009-12-30 21:42:11 +0000262 // If we are dropping an argument to null, we choose to not unique the MDNode
263 // anymore. This commonly occurs during destruction, and uniquing these
264 // brings little reuse.
265 if (To == 0) {
266 setIsNotUniqued();
267 return;
268 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000269
Chris Lattner30ae06b2009-12-30 21:42:11 +0000270 // Now that the node is out of the folding set, get ready to reinsert it.
271 // First, check to see if another node with the same operands already exists
272 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000273 FoldingSetNodeID ID;
274 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000275 void *InsertPoint;
276 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000277
278 if (N) {
279 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000280 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000281 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
282 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000283 }
284
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000285 // InsertPoint will have been set by the FindNodeOrInsertPos call.
286 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000287}
288
Devang Patel05a26fb2009-07-29 00:33:07 +0000289//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000290// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000291//
Devang Patel943ddf62010-01-12 18:34:06 +0000292
293namespace llvm {
294// SymbolTableListTraits specialization for MDSymbolTable.
295void ilist_traits<NamedMDNode>
296::addNodeToList(NamedMDNode *N) {
297 assert(N->getParent() == 0 && "Value already in a container!!");
298 Module *Owner = getListOwner();
299 N->setParent(Owner);
300 MDSymbolTable &ST = Owner->getMDSymbolTable();
301 ST.insert(N->getName(), N);
302}
303
304void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
305 N->setParent(0);
306 Module *Owner = getListOwner();
307 MDSymbolTable &ST = Owner->getMDSymbolTable();
308 ST.remove(N->getName());
309}
310}
311
Devang Patele3073482010-01-05 20:41:31 +0000312static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
313 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000314}
315
Devang Patelf76941e2010-01-12 18:57:56 +0000316NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000317 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000318 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000319 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000320 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000321 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000322
Devang Patele3073482010-01-05 20:41:31 +0000323 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000324 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000325 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000326
Devang Patel943ddf62010-01-12 18:34:06 +0000327 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000328 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000329}
Devang Patel79238d72009-08-03 06:19:01 +0000330
Devang Patel5c310be2009-08-11 18:01:24 +0000331NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000332 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000333 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000334 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000335
Chris Lattner9b493022009-12-31 01:22:29 +0000336 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
337 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000338 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
339 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000340}
341
Chris Lattner1bc810b2009-12-28 08:07:14 +0000342NamedMDNode::~NamedMDNode() {
343 dropAllReferences();
344 delete &getNMDOps(Operands);
345}
346
Chris Lattner9b493022009-12-31 01:22:29 +0000347/// getNumOperands - Return number of NamedMDNode operands.
348unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000349 return (unsigned)getNMDOps(Operands).size();
350}
351
Chris Lattner9b493022009-12-31 01:22:29 +0000352/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000353MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000354 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000355 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000356}
357
Chris Lattner9b493022009-12-31 01:22:29 +0000358/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000359void NamedMDNode::addOperand(MDNode *M) {
360 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000361}
362
Devang Patel79238d72009-08-03 06:19:01 +0000363/// eraseFromParent - Drop all references and remove the node from parent
364/// module.
365void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000366 getParent()->getNamedMDList().erase(this);
367}
368
369/// dropAllReferences - Remove all uses and clear node vector.
370void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000371 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000372}
373
Devang Patelfcfee0f2010-01-07 19:39:36 +0000374/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000375void NamedMDNode::setName(const Twine &NewName) {
376 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
377
378 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000379 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000380
Devang Patelf76941e2010-01-12 18:57:56 +0000381 // Name isn't changing?
382 if (getName() == NameRef)
383 return;
384
385 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000386 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000387 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000388}
389
390/// getName - Return a constant reference to this named metadata's name.
391StringRef NamedMDNode::getName() const {
392 return StringRef(Name);
393}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000394
395//===----------------------------------------------------------------------===//
Chris Lattner8cb6c342009-12-31 01:05:46 +0000396// LLVMContext MDKind naming implementation.
Devang Patel1155fdf2009-10-22 19:36:54 +0000397//
Devang Patel1155fdf2009-10-22 19:36:54 +0000398
Chris Lattner5508da32009-12-29 07:56:15 +0000399#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000400/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000401static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000402 if (MDName.empty())
403 return false;
404
405 if (!isalpha(MDName[0]))
406 return false;
407
408 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
409 ++I) {
410 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
411 return false;
412 }
413 return true;
414}
Chris Lattner5508da32009-12-29 07:56:15 +0000415#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000416
Chris Lattner70939462009-12-28 20:45:51 +0000417/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000418unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000419 assert(isValidName(Name) && "Invalid MDNode name");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000420
Chris Lattnera0566972009-12-29 09:01:33 +0000421 unsigned &Entry = pImpl->CustomMDKindNames[Name];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000422
Chris Lattnera0566972009-12-29 09:01:33 +0000423 // If this is new, assign it its ID.
424 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
425 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000426}
427
Devang Patel1155fdf2009-10-22 19:36:54 +0000428/// getHandlerNames - Populate client supplied smallvector using custome
429/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000430void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
431 Names.resize(pImpl->CustomMDKindNames.size()+1);
432 Names[0] = "";
433 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000434 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
Chris Lattnera0566972009-12-29 09:01:33 +0000435 // MD Handlers are numbered from 1.
436 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000437}
438
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000439//===----------------------------------------------------------------------===//
440// Instruction Metadata method implementations.
441//
442
443void Instruction::setMetadata(const char *Kind, MDNode *Node) {
444 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000445 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000446}
447
448MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000449 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000450}
451
452/// setMetadata - Set the metadata of of the specified kind to the specified
453/// node. This updates/replaces metadata if already present, or removes it if
454/// Node is null.
455void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
456 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000457
Chris Lattnera0566972009-12-29 09:01:33 +0000458 // Handle the case when we're adding/updating metadata on an instruction.
459 if (Node) {
460 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
461 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
462 if (Info.empty()) {
463 setHasMetadata(true);
464 } else {
465 // Handle replacement of an existing value.
466 for (unsigned i = 0, e = Info.size(); i != e; ++i)
467 if (Info[i].first == KindID) {
468 Info[i].second = Node;
469 return;
470 }
471 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000472
Chris Lattnera0566972009-12-29 09:01:33 +0000473 // No replacement, just add it to the list.
474 Info.push_back(std::make_pair(KindID, Node));
475 return;
476 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // Otherwise, we're removing metadata from an instruction.
479 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
480 "HasMetadata bit out of date!");
481 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000482
Chris Lattnera0566972009-12-29 09:01:33 +0000483 // Common case is removing the only entry.
484 if (Info.size() == 1 && Info[0].first == KindID) {
485 getContext().pImpl->MetadataStore.erase(this);
486 setHasMetadata(false);
487 return;
488 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000489
Chris Lattnera0566972009-12-29 09:01:33 +0000490 // Handle replacement of an existing value.
491 for (unsigned i = 0, e = Info.size(); i != e; ++i)
492 if (Info[i].first == KindID) {
493 Info[i] = Info.back();
494 Info.pop_back();
495 assert(!Info.empty() && "Removing last entry should be handled above");
496 return;
497 }
498 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000499}
500
501MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000502 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
503 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000504
Chris Lattnera0566972009-12-29 09:01:33 +0000505 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
506 I != E; ++I)
507 if (I->first == KindID)
508 return I->second;
509 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000510}
511
512void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
513 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000514 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
515 "Shouldn't have called this");
516 const LLVMContextImpl::MDMapTy &Info =
517 getContext().pImpl->MetadataStore.find(this)->second;
518 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000519
Chris Lattnera0566972009-12-29 09:01:33 +0000520 Result.clear();
521 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000522
Chris Lattnera0566972009-12-29 09:01:33 +0000523 // Sort the resulting array so it is stable.
524 if (Result.size() > 1)
525 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000526}
527
Chris Lattner68017802009-12-29 07:44:16 +0000528/// removeAllMetadata - Remove all metadata from this instruction.
529void Instruction::removeAllMetadata() {
530 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000531 getContext().pImpl->MetadataStore.erase(this);
532 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000533}
534