blob: 442b5c51b65510e0c1fe07c5bd683e9ddc5f8904 [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) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000081 // Use <= instead of < to permit a one-past-the-end address.
82 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000083 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000084}
85
Victor Hernandezdd7418a2009-12-16 02:52:09 +000086MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000087 bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000088: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000089 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000090
Victor Hernandez0471abd2009-12-18 20:09:14 +000091 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000092 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +000093
94 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +000095 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +000096 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +000097 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +000098}
99
Chris Lattner8cb6c342009-12-31 01:05:46 +0000100
101/// ~MDNode - Destroy MDNode.
102MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000103 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000104 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000105 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
106 if (isNotUniqued()) {
107 pImpl->NonUniquedMDNodes.erase(this);
108 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000109 pImpl->MDNodeSet.RemoveNode(this);
110 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000111
Chris Lattner8cb6c342009-12-31 01:05:46 +0000112 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000113 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000114 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000115 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000116}
117
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000118static const Function *getFunctionForValue(Value *V) {
119 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000120 if (Instruction *I = dyn_cast<Instruction>(V)) {
121 BasicBlock *BB = I->getParent();
122 return BB ? BB->getParent() : 0;
123 }
Chris Lattner5522f052010-01-21 21:01:47 +0000124 if (Argument *A = dyn_cast<Argument>(V))
125 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000126 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
127 return BB->getParent();
128 if (MDNode *MD = dyn_cast<MDNode>(V))
129 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000130 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) {
Chris Lattner5522f052010-01-21 21:01:47 +0000135 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000136
Devang Patelb36df172010-07-06 21:05:17 +0000137 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000138 const Function *F = 0, *NewF = 0;
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)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000141 if (MDNode *MD = dyn_cast<MDNode>(V))
142 NewF = assertLocalFunction(MD);
143 else
144 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000145 }
Chris Lattner5522f052010-01-21 21:01:47 +0000146 if (F == 0)
147 F = NewF;
148 else
149 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000150 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000151 return F;
152}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000153#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000154
155// getFunction - If this metadata is function-local and recursively has a
156// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000157// Otherwise, return null. getFunction() should not be used for performance-
158// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000159const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000160#ifndef NDEBUG
161 return assertLocalFunction(this);
162#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000163 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000164 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
165 if (const Function *F = getFunctionForValue(getOperand(i)))
166 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000167 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000168}
169
Chris Lattner8cb6c342009-12-31 01:05:46 +0000170// destroy - Delete this node. Only when there are no uses.
171void MDNode::destroy() {
172 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
173 // Placement delete, the free the memory.
174 this->~MDNode();
175 free(this);
176}
177
Chris Lattner450e29c2010-04-28 20:16:12 +0000178/// isFunctionLocalValue - Return true if this is a value that would require a
179/// function-local MDNode.
180static bool isFunctionLocalValue(Value *V) {
181 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
182 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
183}
184
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000185MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000186 unsigned NumVals, FunctionLocalness FL,
187 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000188 LLVMContextImpl *pImpl = Context.pImpl;
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +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;
Chris Lattner450e29c2010-04-28 20:16:12 +0000195 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000196 isFunctionLocal = true;
197 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000198 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000199 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000200 break;
201 case FL_No:
202 isFunctionLocal = false;
203 break;
204 case FL_Yes:
205 isFunctionLocal = true;
206 break;
Devang Patelf7188322009-09-03 01:39:20 +0000207 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000208
Devang Patel44147112010-03-25 06:04:47 +0000209 FoldingSetNodeID ID;
210 for (unsigned i = 0; i != NumVals; ++i)
211 ID.AddPointer(Vals[i]);
212 ID.AddBoolean(isFunctionLocal);
213
214 void *InsertPoint;
215 MDNode *N = NULL;
216
217 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
218 return N;
219
220 if (!Insert)
221 return NULL;
222
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000223 // Coallocate space for the node and Operands together, then placement new.
224 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
225 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
226
227 // InsertPoint will have been set by the FindNodeOrInsertPos call.
228 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
229
Devang Patelf7188322009-09-03 01:39:20 +0000230 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000231}
232
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000233MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
234 return getMDNode(Context, Vals, NumVals, FL_Unknown);
235}
236
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000237MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000238 unsigned NumVals, bool isFunctionLocal) {
239 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
240}
241
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000242MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
243 unsigned NumVals) {
244 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
245}
246
Chris Lattner9b493022009-12-31 01:22:29 +0000247/// getOperand - Return specified operand.
248Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000249 return *getOperandPtr(const_cast<MDNode*>(this), i);
250}
251
Chris Lattnerf543eff2009-12-28 09:12:35 +0000252void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000253 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
254 ID.AddPointer(getOperand(i));
Devang Patelc95e6072010-03-25 05:36:13 +0000255 ID.AddBoolean(isFunctionLocal());
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000256}
Devang Patel5c310be2009-08-11 18:01:24 +0000257
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000258void MDNode::setIsNotUniqued() {
259 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
260 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
261 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000262}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000263
Chris Lattner9b493022009-12-31 01:22:29 +0000264// Replace value from this node's operand list.
265void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000266 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000267
Chris Lattner450e29c2010-04-28 20:16:12 +0000268 // If is possible that someone did GV->RAUW(inst), replacing a global variable
269 // with an instruction or some other function-local object. If this is a
270 // non-function-local MDNode, it can't point to a function-local object.
271 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000272 // Likewise if the MDNode is function-local but for a different function.
273 if (To && isFunctionLocalValue(To)) {
274 if (!isFunctionLocal())
275 To = 0;
276 else {
277 const Function *F = getFunction();
278 const Function *FV = getFunctionForValue(To);
279 // Metadata can be function-local without having an associated function.
280 // So only consider functions to have changed if non-null.
281 if (F && FV && F != FV)
282 To = 0;
283 }
284 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000285
Chris Lattner95c445d2009-12-28 09:32:10 +0000286 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000287 return;
Devang Patelf7188322009-09-03 01:39:20 +0000288
Chris Lattner30ae06b2009-12-30 21:42:11 +0000289 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000290 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000291
292 // If this node is already not being uniqued (because one of the operands
293 // already went to null), then there is nothing else to do here.
294 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000295
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000296 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
297
298 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
299 // this node to remove it, so we don't care what state the operands are in.
300 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000301
Chris Lattner30ae06b2009-12-30 21:42:11 +0000302 // If we are dropping an argument to null, we choose to not unique the MDNode
303 // anymore. This commonly occurs during destruction, and uniquing these
304 // brings little reuse.
305 if (To == 0) {
306 setIsNotUniqued();
307 return;
308 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000309
Chris Lattner30ae06b2009-12-30 21:42:11 +0000310 // Now that the node is out of the folding set, get ready to reinsert it.
311 // First, check to see if another node with the same operands already exists
312 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000313 FoldingSetNodeID ID;
314 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000315 void *InsertPoint;
316 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000317
318 if (N) {
319 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000320 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000321 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
322 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000323 }
324
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000325 // InsertPoint will have been set by the FindNodeOrInsertPos call.
326 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000327}
328
Devang Patel05a26fb2009-07-29 00:33:07 +0000329//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000330// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000331//
Devang Patel943ddf62010-01-12 18:34:06 +0000332
Dan Gohman846b9e12010-07-21 18:01:42 +0000333static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
334 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000335}
336
Dan Gohman2637cc12010-07-21 23:38:33 +0000337NamedMDNode::NamedMDNode(const Twine &N)
338 : Name(N.str()), Parent(0),
339 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
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!");
Dan Gohman093cb792010-07-21 18:54:18 +0000355 return dyn_cast<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) {
Dan Gohman846b9e12010-07-21 18:01:42 +0000360 getNMDOps(Operands).push_back(TrackingVH<MDNode>(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() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000366 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000367}
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/// getName - Return a constant reference to this named metadata's name.
375StringRef NamedMDNode::getName() const {
376 return StringRef(Name);
377}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000378
379//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000380// Instruction Metadata method implementations.
381//
382
383void Instruction::setMetadata(const char *Kind, MDNode *Node) {
384 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000385 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000386}
387
388MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000389 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000390}
391
392/// setMetadata - Set the metadata of of the specified kind to the specified
393/// node. This updates/replaces metadata if already present, or removes it if
394/// Node is null.
395void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
396 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000397
Chris Lattnerc263b422010-03-30 23:03:27 +0000398 // Handle 'dbg' as a special case since it is not stored in the hash table.
399 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000400 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000401 return;
402 }
403
Chris Lattnera0566972009-12-29 09:01:33 +0000404 // Handle the case when we're adding/updating metadata on an instruction.
405 if (Node) {
406 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000407 assert(!Info.empty() == hasMetadataHashEntry() &&
408 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000409 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000410 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000411 } else {
412 // Handle replacement of an existing value.
413 for (unsigned i = 0, e = Info.size(); i != e; ++i)
414 if (Info[i].first == KindID) {
415 Info[i].second = Node;
416 return;
417 }
418 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000419
Chris Lattnera0566972009-12-29 09:01:33 +0000420 // No replacement, just add it to the list.
421 Info.push_back(std::make_pair(KindID, Node));
422 return;
423 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000424
Chris Lattnera0566972009-12-29 09:01:33 +0000425 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000426 assert(hasMetadataHashEntry() &&
427 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000428 "HasMetadata bit out of date!");
429 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000430
Chris Lattnera0566972009-12-29 09:01:33 +0000431 // Common case is removing the only entry.
432 if (Info.size() == 1 && Info[0].first == KindID) {
433 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000434 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000435 return;
436 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000437
Chris Lattnerc263b422010-03-30 23:03:27 +0000438 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000439 for (unsigned i = 0, e = Info.size(); i != e; ++i)
440 if (Info[i].first == KindID) {
441 Info[i] = Info.back();
442 Info.pop_back();
443 assert(!Info.empty() && "Removing last entry should be handled above");
444 return;
445 }
446 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000447}
448
449MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000450 // Handle 'dbg' as a special case since it is not stored in the hash table.
451 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000452 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000453
454 if (!hasMetadataHashEntry()) return 0;
455
Chris Lattnera0566972009-12-29 09:01:33 +0000456 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000457 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000458
Chris Lattnera0566972009-12-29 09:01:33 +0000459 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
460 I != E; ++I)
461 if (I->first == KindID)
462 return I->second;
463 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000464}
465
466void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000467 MDNode*> > &Result) const {
468 Result.clear();
469
470 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000471 if (!DbgLoc.isUnknown()) {
472 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
473 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000474 if (!hasMetadataHashEntry()) return;
475 }
476
477 assert(hasMetadataHashEntry() &&
478 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000479 "Shouldn't have called this");
480 const LLVMContextImpl::MDMapTy &Info =
481 getContext().pImpl->MetadataStore.find(this)->second;
482 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000483
Chris Lattnera0566972009-12-29 09:01:33 +0000484 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000485
Chris Lattnera0566972009-12-29 09:01:33 +0000486 // Sort the resulting array so it is stable.
487 if (Result.size() > 1)
488 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000489}
490
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000491void Instruction::
492getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
493 MDNode*> > &Result) const {
494 Result.clear();
495 assert(hasMetadataHashEntry() &&
496 getContext().pImpl->MetadataStore.count(this) &&
497 "Shouldn't have called this");
498 const LLVMContextImpl::MDMapTy &Info =
499 getContext().pImpl->MetadataStore.find(this)->second;
500 assert(!Info.empty() && "Shouldn't have called this");
501
502 Result.append(Info.begin(), Info.end());
503
504 // Sort the resulting array so it is stable.
505 if (Result.size() > 1)
506 array_pod_sort(Result.begin(), Result.end());
507}
508
509
Dan Gohman48a995f2010-07-20 22:25:04 +0000510/// clearMetadataHashEntries - Clear all hashtable-based metadata from
511/// this instruction.
512void Instruction::clearMetadataHashEntries() {
513 assert(hasMetadataHashEntry() && "Caller should check");
514 getContext().pImpl->MetadataStore.erase(this);
515 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000516}
517