blob: 77926f9af3466d39ee00d937a2ac31f4298782e3 [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)
Devang Patelac277eb2010-01-22 22:52:10 +000031 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000032
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
42//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000043// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000044//
45
Chris Lattner9b493022009-12-31 01:22:29 +000046// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000047namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000048class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000049 MDNode *Parent;
50public:
Chris Lattner9b493022009-12-31 01:22:29 +000051 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
52 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000053
Chris Lattner8cb6c342009-12-31 01:05:46 +000054 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000055 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000056 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000057
Chris Lattner74a6ad62009-12-28 07:41:54 +000058 virtual void deleted();
59 virtual void allUsesReplacedWith(Value *NV);
60};
61} // end namespace llvm.
62
63
Chris Lattner9b493022009-12-31 01:22:29 +000064void MDNodeOperand::deleted() {
65 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000066}
67
Chris Lattner9b493022009-12-31 01:22:29 +000068void MDNodeOperand::allUsesReplacedWith(Value *NV) {
69 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000070}
71
72
73
74//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000075// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000076//
Chris Lattner74a6ad62009-12-28 07:41:54 +000077
Chris Lattner9b493022009-12-31 01:22:29 +000078/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000079/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000080static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
81 assert(Op < N->getNumOperands() && "Invalid operand number");
82 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000083}
84
Victor Hernandezdd7418a2009-12-16 02:52:09 +000085MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000086 bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000087: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000088 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000089
Victor Hernandez0471abd2009-12-18 20:09:14 +000090 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000091 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +000092
93 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +000094 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +000095 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +000096 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +000097}
98
Chris Lattner8cb6c342009-12-31 01:05:46 +000099
100/// ~MDNode - Destroy MDNode.
101MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000102 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000103 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000104 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
105 if (isNotUniqued()) {
106 pImpl->NonUniquedMDNodes.erase(this);
107 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000108 pImpl->MDNodeSet.RemoveNode(this);
109 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000110
Chris Lattner8cb6c342009-12-31 01:05:46 +0000111 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000112 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000113 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000114 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000115}
116
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000117static const Function *getFunctionForValue(Value *V) {
Victor Hernandezd0b2ff92010-01-20 06:22:33 +0000118 assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000119 if (!V) return NULL;
120 if (Instruction *I = dyn_cast<Instruction>(V))
121 return I->getParent()->getParent();
Chris Lattner5522f052010-01-21 21:01:47 +0000122 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
123 return BB->getParent();
124 if (Argument *A = dyn_cast<Argument>(V))
125 return A->getParent();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000126 return NULL;
127}
128
Victor Hernandez8296da72010-01-14 20:12:34 +0000129#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000130static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000131 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000132
Chris Lattner5522f052010-01-21 21:01:47 +0000133 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000134 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000135 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000136 if (MDNode *MD = dyn_cast<MDNode>(V))
137 NewF = assertLocalFunction(MD);
138 else
139 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000140 }
Chris Lattner5522f052010-01-21 21:01:47 +0000141 if (F == 0)
142 F = NewF;
143 else
144 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
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)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000163 if (const Function *F = MD->getFunction())
164 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,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000182 unsigned NumVals, FunctionLocalness FL,
183 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000184 LLVMContextImpl *pImpl = Context.pImpl;
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000185 bool isFunctionLocal = false;
186 switch (FL) {
187 case FL_Unknown:
188 for (unsigned i = 0; i != NumVals; ++i) {
189 Value *V = Vals[i];
190 if (!V) continue;
191 if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
192 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
193 isFunctionLocal = true;
194 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000195 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000196 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000197 break;
198 case FL_No:
199 isFunctionLocal = false;
200 break;
201 case FL_Yes:
202 isFunctionLocal = true;
203 break;
Devang Patelf7188322009-09-03 01:39:20 +0000204 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000205
Devang Patel44147112010-03-25 06:04:47 +0000206 FoldingSetNodeID ID;
207 for (unsigned i = 0; i != NumVals; ++i)
208 ID.AddPointer(Vals[i]);
209 ID.AddBoolean(isFunctionLocal);
210
211 void *InsertPoint;
212 MDNode *N = NULL;
213
214 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
215 return N;
216
217 if (!Insert)
218 return NULL;
219
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000220 // Coallocate space for the node and Operands together, then placement new.
221 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
222 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
223
224 // InsertPoint will have been set by the FindNodeOrInsertPos call.
225 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
226
Devang Patelf7188322009-09-03 01:39:20 +0000227 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000228}
229
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000230MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
231 return getMDNode(Context, Vals, NumVals, FL_Unknown);
232}
233
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000234MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000235 unsigned NumVals, bool isFunctionLocal) {
236 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
237}
238
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000239MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
240 unsigned NumVals) {
241 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
242}
243
Chris Lattner9b493022009-12-31 01:22:29 +0000244/// getOperand - Return specified operand.
245Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000246 return *getOperandPtr(const_cast<MDNode*>(this), i);
247}
248
Chris Lattnerf543eff2009-12-28 09:12:35 +0000249void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000250 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
251 ID.AddPointer(getOperand(i));
Devang Patelc95e6072010-03-25 05:36:13 +0000252 ID.AddBoolean(isFunctionLocal());
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000253}
Devang Patel5c310be2009-08-11 18:01:24 +0000254
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000255void MDNode::setIsNotUniqued() {
256 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
257 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
258 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000259}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000260
Chris Lattner9b493022009-12-31 01:22:29 +0000261// Replace value from this node's operand list.
262void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000263 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000264
Chris Lattner95c445d2009-12-28 09:32:10 +0000265 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000266 return;
Devang Patelf7188322009-09-03 01:39:20 +0000267
Chris Lattner30ae06b2009-12-30 21:42:11 +0000268 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000269 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000270
271 // If this node is already not being uniqued (because one of the operands
272 // already went to null), then there is nothing else to do here.
273 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000274
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000275 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
276
277 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
278 // this node to remove it, so we don't care what state the operands are in.
279 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000280
Chris Lattner30ae06b2009-12-30 21:42:11 +0000281 // If we are dropping an argument to null, we choose to not unique the MDNode
282 // anymore. This commonly occurs during destruction, and uniquing these
283 // brings little reuse.
284 if (To == 0) {
285 setIsNotUniqued();
286 return;
287 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000288
Chris Lattner30ae06b2009-12-30 21:42:11 +0000289 // Now that the node is out of the folding set, get ready to reinsert it.
290 // First, check to see if another node with the same operands already exists
291 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000292 FoldingSetNodeID ID;
293 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000294 void *InsertPoint;
295 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000296
297 if (N) {
298 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000299 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000300 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
301 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000302 }
303
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000304 // InsertPoint will have been set by the FindNodeOrInsertPos call.
305 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000306}
307
Devang Patel05a26fb2009-07-29 00:33:07 +0000308//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000309// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000310//
Devang Patel943ddf62010-01-12 18:34:06 +0000311
312namespace llvm {
313// SymbolTableListTraits specialization for MDSymbolTable.
314void ilist_traits<NamedMDNode>
315::addNodeToList(NamedMDNode *N) {
316 assert(N->getParent() == 0 && "Value already in a container!!");
317 Module *Owner = getListOwner();
318 N->setParent(Owner);
319 MDSymbolTable &ST = Owner->getMDSymbolTable();
320 ST.insert(N->getName(), N);
321}
322
323void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
324 N->setParent(0);
325 Module *Owner = getListOwner();
326 MDSymbolTable &ST = Owner->getMDSymbolTable();
327 ST.remove(N->getName());
328}
329}
330
Devang Patele3073482010-01-05 20:41:31 +0000331static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
332 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000333}
334
Devang Patelf76941e2010-01-12 18:57:56 +0000335NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000336 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000337 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000338 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000339 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000340 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000341
Devang Patele3073482010-01-05 20:41:31 +0000342 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000343 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000344 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000345
Devang Patel943ddf62010-01-12 18:34:06 +0000346 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000347 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000348}
Devang Patel79238d72009-08-03 06:19:01 +0000349
Devang Patel5c310be2009-08-11 18:01:24 +0000350NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000351 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000352 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000353 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000354
Chris Lattner9b493022009-12-31 01:22:29 +0000355 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
356 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000357 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
358 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000359}
360
Chris Lattner1bc810b2009-12-28 08:07:14 +0000361NamedMDNode::~NamedMDNode() {
362 dropAllReferences();
363 delete &getNMDOps(Operands);
364}
365
Chris Lattner9b493022009-12-31 01:22:29 +0000366/// getNumOperands - Return number of NamedMDNode operands.
367unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000368 return (unsigned)getNMDOps(Operands).size();
369}
370
Chris Lattner9b493022009-12-31 01:22:29 +0000371/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000372MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000373 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000374 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000375}
376
Chris Lattner9b493022009-12-31 01:22:29 +0000377/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000378void NamedMDNode::addOperand(MDNode *M) {
379 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000380}
381
Devang Patel79238d72009-08-03 06:19:01 +0000382/// eraseFromParent - Drop all references and remove the node from parent
383/// module.
384void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000385 getParent()->getNamedMDList().erase(this);
386}
387
388/// dropAllReferences - Remove all uses and clear node vector.
389void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000390 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000391}
392
Devang Patelfcfee0f2010-01-07 19:39:36 +0000393/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000394void NamedMDNode::setName(const Twine &NewName) {
395 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
396
397 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000398 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000399
Devang Patelf76941e2010-01-12 18:57:56 +0000400 // Name isn't changing?
401 if (getName() == NameRef)
402 return;
403
404 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000405 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000406 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000407}
408
409/// getName - Return a constant reference to this named metadata's name.
410StringRef NamedMDNode::getName() const {
411 return StringRef(Name);
412}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000413
414//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000415// Instruction Metadata method implementations.
416//
417
418void Instruction::setMetadata(const char *Kind, MDNode *Node) {
419 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000420 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000421}
422
423MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000424 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000425}
426
427/// setMetadata - Set the metadata of of the specified kind to the specified
428/// node. This updates/replaces metadata if already present, or removes it if
429/// Node is null.
430void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
431 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000432
Chris Lattnerc263b422010-03-30 23:03:27 +0000433 // Handle 'dbg' as a special case since it is not stored in the hash table.
434 if (KindID == LLVMContext::MD_dbg) {
435 DbgInfo = Node;
436 return;
437 }
438
Chris Lattnera0566972009-12-29 09:01:33 +0000439 // Handle the case when we're adding/updating metadata on an instruction.
440 if (Node) {
441 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000442 assert(!Info.empty() == hasMetadataHashEntry() &&
443 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000444 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000445 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000446 } else {
447 // Handle replacement of an existing value.
448 for (unsigned i = 0, e = Info.size(); i != e; ++i)
449 if (Info[i].first == KindID) {
450 Info[i].second = Node;
451 return;
452 }
453 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000454
Chris Lattnera0566972009-12-29 09:01:33 +0000455 // No replacement, just add it to the list.
456 Info.push_back(std::make_pair(KindID, Node));
457 return;
458 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000459
Chris Lattnera0566972009-12-29 09:01:33 +0000460 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000461 assert(hasMetadataHashEntry() &&
462 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000463 "HasMetadata bit out of date!");
464 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000465
Chris Lattnera0566972009-12-29 09:01:33 +0000466 // Common case is removing the only entry.
467 if (Info.size() == 1 && Info[0].first == KindID) {
468 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000469 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000470 return;
471 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000472
Chris Lattnerc263b422010-03-30 23:03:27 +0000473 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000474 for (unsigned i = 0, e = Info.size(); i != e; ++i)
475 if (Info[i].first == KindID) {
476 Info[i] = Info.back();
477 Info.pop_back();
478 assert(!Info.empty() && "Removing last entry should be handled above");
479 return;
480 }
481 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000482}
483
484MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000485 // Handle 'dbg' as a special case since it is not stored in the hash table.
486 if (KindID == LLVMContext::MD_dbg)
487 return DbgInfo;
488
489 if (!hasMetadataHashEntry()) return 0;
490
Chris Lattnera0566972009-12-29 09:01:33 +0000491 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000492 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000493
Chris Lattnera0566972009-12-29 09:01:33 +0000494 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
495 I != E; ++I)
496 if (I->first == KindID)
497 return I->second;
498 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000499}
500
501void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000502 MDNode*> > &Result) const {
503 Result.clear();
504
505 // Handle 'dbg' as a special case since it is not stored in the hash table.
506 if (DbgInfo) {
507 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg, DbgInfo));
508 if (!hasMetadataHashEntry()) return;
509 }
510
511 assert(hasMetadataHashEntry() &&
512 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000513 "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.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000519
Chris Lattnera0566972009-12-29 09:01:33 +0000520 // Sort the resulting array so it is stable.
521 if (Result.size() > 1)
522 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000523}
524
Chris Lattner68017802009-12-29 07:44:16 +0000525/// removeAllMetadata - Remove all metadata from this instruction.
526void Instruction::removeAllMetadata() {
527 assert(hasMetadata() && "Caller should check");
Chris Lattnerc263b422010-03-30 23:03:27 +0000528 DbgInfo = 0;
529 if (hasMetadataHashEntry()) {
530 getContext().pImpl->MetadataStore.erase(this);
531 setHasMetadataHashEntry(false);
532 }
Chris Lattner68017802009-12-29 07:44:16 +0000533}
534