blob: 236ddaa15b2a833a5086b04baafcc7526915f60b [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"
Dan Gohman16a5d982010-08-20 22:02:26 +000023#include "llvm/Support/LeakDetector.h"
Chris Lattner1300f452009-12-28 08:24:16 +000024#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000028// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000029//
Chris Lattner5a409bd2009-12-28 08:30:43 +000030
31MDString::MDString(LLVMContext &C, StringRef S)
Devang Patelac277eb2010-01-22 22:52:10 +000032 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000033
Devang Pateldcb99d32009-10-22 00:10:15 +000034MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000035 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000036 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000037 pImpl->MDStringCache.GetOrCreateValue(Str);
38 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000039 if (!S) S = new MDString(Context, Entry.getKey());
40 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000041}
42
43//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000044// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000045//
46
Chris Lattner9b493022009-12-31 01:22:29 +000047// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000048namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000049class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000050 MDNode *Parent;
51public:
Chris Lattner9b493022009-12-31 01:22:29 +000052 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
53 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000054
Chris Lattner8cb6c342009-12-31 01:05:46 +000055 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000056 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000057 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000058
Chris Lattner74a6ad62009-12-28 07:41:54 +000059 virtual void deleted();
60 virtual void allUsesReplacedWith(Value *NV);
61};
62} // end namespace llvm.
63
64
Chris Lattner9b493022009-12-31 01:22:29 +000065void MDNodeOperand::deleted() {
66 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000067}
68
Chris Lattner9b493022009-12-31 01:22:29 +000069void MDNodeOperand::allUsesReplacedWith(Value *NV) {
70 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000071}
72
73
74
75//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000076// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000077//
Chris Lattner74a6ad62009-12-28 07:41:54 +000078
Chris Lattner9b493022009-12-31 01:22:29 +000079/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000080/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000081static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000082 // Use <= instead of < to permit a one-past-the-end address.
83 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000084 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000085}
86
Victor Hernandezdd7418a2009-12-16 02:52:09 +000087MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000088 bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000089: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000090 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000091
Victor Hernandez0471abd2009-12-18 20:09:14 +000092 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000093 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +000094
95 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +000096 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +000097 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +000098 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +000099}
100
Chris Lattner8cb6c342009-12-31 01:05:46 +0000101
102/// ~MDNode - Destroy MDNode.
103MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000104 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000105 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000106 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
107 if (isNotUniqued()) {
108 pImpl->NonUniquedMDNodes.erase(this);
109 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000110 pImpl->MDNodeSet.RemoveNode(this);
111 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000112
Chris Lattner8cb6c342009-12-31 01:05:46 +0000113 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000114 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000115 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000116 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000117}
118
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000119static const Function *getFunctionForValue(Value *V) {
120 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000121 if (Instruction *I = dyn_cast<Instruction>(V)) {
122 BasicBlock *BB = I->getParent();
123 return BB ? BB->getParent() : 0;
124 }
Chris Lattner5522f052010-01-21 21:01:47 +0000125 if (Argument *A = dyn_cast<Argument>(V))
126 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000127 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
128 return BB->getParent();
129 if (MDNode *MD = dyn_cast<MDNode>(V))
130 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000131 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) {
Chris Lattner5522f052010-01-21 21:01:47 +0000136 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000137
Devang Patelb36df172010-07-06 21:05:17 +0000138 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000139 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000140 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000141 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000142 if (MDNode *MD = dyn_cast<MDNode>(V))
143 NewF = assertLocalFunction(MD);
144 else
145 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000146 }
Chris Lattner5522f052010-01-21 21:01:47 +0000147 if (F == 0)
148 F = NewF;
149 else
150 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000151 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000152 return F;
153}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000154#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000155
156// getFunction - If this metadata is function-local and recursively has a
157// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000158// Otherwise, return null. getFunction() should not be used for performance-
159// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000160const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000161#ifndef NDEBUG
162 return assertLocalFunction(this);
163#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000164 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000165 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
166 if (const Function *F = getFunctionForValue(getOperand(i)))
167 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000168 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
Chris Lattner450e29c2010-04-28 20:16:12 +0000179/// isFunctionLocalValue - Return true if this is a value that would require a
180/// function-local MDNode.
181static bool isFunctionLocalValue(Value *V) {
182 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
183 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
184}
185
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000186MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000187 unsigned NumVals, FunctionLocalness FL,
188 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000189 LLVMContextImpl *pImpl = Context.pImpl;
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000190 bool isFunctionLocal = false;
191 switch (FL) {
192 case FL_Unknown:
193 for (unsigned i = 0; i != NumVals; ++i) {
194 Value *V = Vals[i];
195 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000196 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000197 isFunctionLocal = true;
198 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000199 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000200 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000201 break;
202 case FL_No:
203 isFunctionLocal = false;
204 break;
205 case FL_Yes:
206 isFunctionLocal = true;
207 break;
Devang Patelf7188322009-09-03 01:39:20 +0000208 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000209
Devang Patel44147112010-03-25 06:04:47 +0000210 FoldingSetNodeID ID;
211 for (unsigned i = 0; i != NumVals; ++i)
212 ID.AddPointer(Vals[i]);
213 ID.AddBoolean(isFunctionLocal);
214
215 void *InsertPoint;
216 MDNode *N = NULL;
217
218 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
219 return N;
220
221 if (!Insert)
222 return NULL;
223
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000224 // Coallocate space for the node and Operands together, then placement new.
225 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
226 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
227
228 // InsertPoint will have been set by the FindNodeOrInsertPos call.
229 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
230
Devang Patelf7188322009-09-03 01:39:20 +0000231 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000232}
233
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000234MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
235 return getMDNode(Context, Vals, NumVals, FL_Unknown);
236}
237
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000238MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000239 unsigned NumVals, bool isFunctionLocal) {
240 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
241}
242
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000243MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
244 unsigned NumVals) {
245 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
246}
247
Dan Gohman16a5d982010-08-20 22:02:26 +0000248MDNode *MDNode::getTemporary(LLVMContext &Context, Value *const *Vals,
249 unsigned NumVals) {
250 MDNode *N = (MDNode *)malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
251 N = new (N) MDNode(Context, Vals, NumVals, FL_No);
252 N->setValueSubclassData(N->getSubclassDataFromValue() |
253 NotUniquedBit);
254 LeakDetector::addGarbageObject(N);
255 return N;
256}
257
258void MDNode::deleteTemporary(MDNode *N) {
259 assert(N->use_empty() && "Temporary MDNode has uses!");
260 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
261 "Temporary MDNode does not have NotUniquedBit set!");
262 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
263 "Temporary MDNode does has DestroyFlag set!");
264 N->setValueSubclassData(N->getSubclassDataFromValue() |
265 DestroyFlag);
266 LeakDetector::removeGarbageObject(N);
267 delete N;
268}
269
Chris Lattner9b493022009-12-31 01:22:29 +0000270/// getOperand - Return specified operand.
271Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000272 return *getOperandPtr(const_cast<MDNode*>(this), i);
273}
274
Chris Lattnerf543eff2009-12-28 09:12:35 +0000275void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000276 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
277 ID.AddPointer(getOperand(i));
Devang Patelc95e6072010-03-25 05:36:13 +0000278 ID.AddBoolean(isFunctionLocal());
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000279}
Devang Patel5c310be2009-08-11 18:01:24 +0000280
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000281void MDNode::setIsNotUniqued() {
282 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
283 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
284 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000285}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000286
Chris Lattner9b493022009-12-31 01:22:29 +0000287// Replace value from this node's operand list.
288void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000289 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000290
Chris Lattner450e29c2010-04-28 20:16:12 +0000291 // If is possible that someone did GV->RAUW(inst), replacing a global variable
292 // with an instruction or some other function-local object. If this is a
293 // non-function-local MDNode, it can't point to a function-local object.
294 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000295 // Likewise if the MDNode is function-local but for a different function.
296 if (To && isFunctionLocalValue(To)) {
297 if (!isFunctionLocal())
298 To = 0;
299 else {
300 const Function *F = getFunction();
301 const Function *FV = getFunctionForValue(To);
302 // Metadata can be function-local without having an associated function.
303 // So only consider functions to have changed if non-null.
304 if (F && FV && F != FV)
305 To = 0;
306 }
307 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000308
Chris Lattner95c445d2009-12-28 09:32:10 +0000309 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000310 return;
Devang Patelf7188322009-09-03 01:39:20 +0000311
Chris Lattner30ae06b2009-12-30 21:42:11 +0000312 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000313 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000314
315 // If this node is already not being uniqued (because one of the operands
316 // already went to null), then there is nothing else to do here.
317 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000318
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000319 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
320
321 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
322 // this node to remove it, so we don't care what state the operands are in.
323 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000324
Chris Lattner30ae06b2009-12-30 21:42:11 +0000325 // If we are dropping an argument to null, we choose to not unique the MDNode
326 // anymore. This commonly occurs during destruction, and uniquing these
327 // brings little reuse.
328 if (To == 0) {
329 setIsNotUniqued();
330 return;
331 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000332
Chris Lattner30ae06b2009-12-30 21:42:11 +0000333 // Now that the node is out of the folding set, get ready to reinsert it.
334 // First, check to see if another node with the same operands already exists
335 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000336 FoldingSetNodeID ID;
337 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000338 void *InsertPoint;
339 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000340
341 if (N) {
342 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000343 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000344 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
345 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000346 }
347
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000348 // InsertPoint will have been set by the FindNodeOrInsertPos call.
349 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000350}
351
Devang Patel05a26fb2009-07-29 00:33:07 +0000352//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000353// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000354//
Devang Patel943ddf62010-01-12 18:34:06 +0000355
Dan Gohman846b9e12010-07-21 18:01:42 +0000356static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
357 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000358}
359
Dan Gohman2637cc12010-07-21 23:38:33 +0000360NamedMDNode::NamedMDNode(const Twine &N)
361 : Name(N.str()), Parent(0),
362 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000363}
364
Chris Lattner1bc810b2009-12-28 08:07:14 +0000365NamedMDNode::~NamedMDNode() {
366 dropAllReferences();
367 delete &getNMDOps(Operands);
368}
369
Chris Lattner9b493022009-12-31 01:22:29 +0000370/// getNumOperands - Return number of NamedMDNode operands.
371unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000372 return (unsigned)getNMDOps(Operands).size();
373}
374
Chris Lattner9b493022009-12-31 01:22:29 +0000375/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000376MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000377 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000378 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000379}
380
Chris Lattner9b493022009-12-31 01:22:29 +0000381/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000382void NamedMDNode::addOperand(MDNode *M) {
Dan Gohman846b9e12010-07-21 18:01:42 +0000383 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000384}
385
Devang Patel79238d72009-08-03 06:19:01 +0000386/// eraseFromParent - Drop all references and remove the node from parent
387/// module.
388void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000389 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000390}
391
392/// dropAllReferences - Remove all uses and clear node vector.
393void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000394 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000395}
396
Devang Patelfcfee0f2010-01-07 19:39:36 +0000397/// getName - Return a constant reference to this named metadata's name.
398StringRef NamedMDNode::getName() const {
399 return StringRef(Name);
400}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000401
402//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000403// Instruction Metadata method implementations.
404//
405
406void Instruction::setMetadata(const char *Kind, MDNode *Node) {
407 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000408 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000409}
410
411MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000412 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000413}
414
415/// setMetadata - Set the metadata of of the specified kind to the specified
416/// node. This updates/replaces metadata if already present, or removes it if
417/// Node is null.
418void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
419 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000420
Chris Lattnerc263b422010-03-30 23:03:27 +0000421 // Handle 'dbg' as a special case since it is not stored in the hash table.
422 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000423 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000424 return;
425 }
426
Chris Lattnera0566972009-12-29 09:01:33 +0000427 // Handle the case when we're adding/updating metadata on an instruction.
428 if (Node) {
429 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000430 assert(!Info.empty() == hasMetadataHashEntry() &&
431 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000432 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000433 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000434 } else {
435 // Handle replacement of an existing value.
436 for (unsigned i = 0, e = Info.size(); i != e; ++i)
437 if (Info[i].first == KindID) {
438 Info[i].second = Node;
439 return;
440 }
441 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000442
Chris Lattnera0566972009-12-29 09:01:33 +0000443 // No replacement, just add it to the list.
444 Info.push_back(std::make_pair(KindID, Node));
445 return;
446 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000447
Chris Lattnera0566972009-12-29 09:01:33 +0000448 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000449 assert(hasMetadataHashEntry() &&
450 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000451 "HasMetadata bit out of date!");
452 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000453
Chris Lattnera0566972009-12-29 09:01:33 +0000454 // Common case is removing the only entry.
455 if (Info.size() == 1 && Info[0].first == KindID) {
456 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000457 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000458 return;
459 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000460
Chris Lattnerc263b422010-03-30 23:03:27 +0000461 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000462 for (unsigned i = 0, e = Info.size(); i != e; ++i)
463 if (Info[i].first == KindID) {
464 Info[i] = Info.back();
465 Info.pop_back();
466 assert(!Info.empty() && "Removing last entry should be handled above");
467 return;
468 }
469 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000470}
471
472MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000473 // Handle 'dbg' as a special case since it is not stored in the hash table.
474 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000475 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000476
477 if (!hasMetadataHashEntry()) return 0;
478
Chris Lattnera0566972009-12-29 09:01:33 +0000479 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000480 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000481
Chris Lattnera0566972009-12-29 09:01:33 +0000482 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
483 I != E; ++I)
484 if (I->first == KindID)
485 return I->second;
486 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000487}
488
489void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000490 MDNode*> > &Result) const {
491 Result.clear();
492
493 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000494 if (!DbgLoc.isUnknown()) {
495 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
496 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000497 if (!hasMetadataHashEntry()) return;
498 }
499
500 assert(hasMetadataHashEntry() &&
501 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000502 "Shouldn't have called this");
503 const LLVMContextImpl::MDMapTy &Info =
504 getContext().pImpl->MetadataStore.find(this)->second;
505 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000506
Chris Lattnera0566972009-12-29 09:01:33 +0000507 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000508
Chris Lattnera0566972009-12-29 09:01:33 +0000509 // Sort the resulting array so it is stable.
510 if (Result.size() > 1)
511 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000512}
513
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000514void Instruction::
515getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
516 MDNode*> > &Result) const {
517 Result.clear();
518 assert(hasMetadataHashEntry() &&
519 getContext().pImpl->MetadataStore.count(this) &&
520 "Shouldn't have called this");
521 const LLVMContextImpl::MDMapTy &Info =
522 getContext().pImpl->MetadataStore.find(this)->second;
523 assert(!Info.empty() && "Shouldn't have called this");
524
525 Result.append(Info.begin(), Info.end());
526
527 // Sort the resulting array so it is stable.
528 if (Result.size() > 1)
529 array_pod_sort(Result.begin(), Result.end());
530}
531
532
Dan Gohman48a995f2010-07-20 22:25:04 +0000533/// clearMetadataHashEntries - Clear all hashtable-based metadata from
534/// this instruction.
535void Instruction::clearMetadataHashEntries() {
536 assert(hasMetadataHashEntry() && "Caller should check");
537 getContext().pImpl->MetadataStore.erase(this);
538 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000539}
540