blob: b894ea30aa981b138647723f79673dcb87b42acb [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) {
118 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000119 if (Instruction *I = dyn_cast<Instruction>(V)) {
120 BasicBlock *BB = I->getParent();
121 return BB ? BB->getParent() : 0;
122 }
Chris Lattner5522f052010-01-21 21:01:47 +0000123 if (Argument *A = dyn_cast<Argument>(V))
124 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000125 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
126 return BB->getParent();
127 if (MDNode *MD = dyn_cast<MDNode>(V))
128 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000129 return NULL;
130}
131
Victor Hernandez8296da72010-01-14 20:12:34 +0000132#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000133static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000134 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000135
Chris Lattner5522f052010-01-21 21:01:47 +0000136 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000137 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000138 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000139 if (MDNode *MD = dyn_cast<MDNode>(V))
140 NewF = assertLocalFunction(MD);
141 else
142 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000143 }
Chris Lattner5522f052010-01-21 21:01:47 +0000144 if (F == 0)
145 F = NewF;
146 else
147 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000148 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000149 return F;
150}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000151#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000152
153// getFunction - If this metadata is function-local and recursively has a
154// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000155// Otherwise, return null. getFunction() should not be used for performance-
156// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000157const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000158#ifndef NDEBUG
159 return assertLocalFunction(this);
160#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000161 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000162 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
163 if (const Function *F = getFunctionForValue(getOperand(i)))
164 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000165 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000166}
167
Chris Lattner8cb6c342009-12-31 01:05:46 +0000168// destroy - Delete this node. Only when there are no uses.
169void MDNode::destroy() {
170 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
171 // Placement delete, the free the memory.
172 this->~MDNode();
173 free(this);
174}
175
Chris Lattner450e29c2010-04-28 20:16:12 +0000176/// isFunctionLocalValue - Return true if this is a value that would require a
177/// function-local MDNode.
178static bool isFunctionLocalValue(Value *V) {
179 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
180 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
181}
182
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000183MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000184 unsigned NumVals, FunctionLocalness FL,
185 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000186 LLVMContextImpl *pImpl = Context.pImpl;
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000187 bool isFunctionLocal = false;
188 switch (FL) {
189 case FL_Unknown:
190 for (unsigned i = 0; i != NumVals; ++i) {
191 Value *V = Vals[i];
192 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000193 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000194 isFunctionLocal = true;
195 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000196 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000197 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000198 break;
199 case FL_No:
200 isFunctionLocal = false;
201 break;
202 case FL_Yes:
203 isFunctionLocal = true;
204 break;
Devang Patelf7188322009-09-03 01:39:20 +0000205 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000206
Devang Patel44147112010-03-25 06:04:47 +0000207 FoldingSetNodeID ID;
208 for (unsigned i = 0; i != NumVals; ++i)
209 ID.AddPointer(Vals[i]);
210 ID.AddBoolean(isFunctionLocal);
211
212 void *InsertPoint;
213 MDNode *N = NULL;
214
215 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
216 return N;
217
218 if (!Insert)
219 return NULL;
220
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000221 // Coallocate space for the node and Operands together, then placement new.
222 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
223 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
224
225 // InsertPoint will have been set by the FindNodeOrInsertPos call.
226 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
227
Devang Patelf7188322009-09-03 01:39:20 +0000228 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000229}
230
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000231MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
232 return getMDNode(Context, Vals, NumVals, FL_Unknown);
233}
234
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000235MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000236 unsigned NumVals, bool isFunctionLocal) {
237 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
238}
239
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000240MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
241 unsigned NumVals) {
242 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
243}
244
Chris Lattner9b493022009-12-31 01:22:29 +0000245/// getOperand - Return specified operand.
246Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000247 return *getOperandPtr(const_cast<MDNode*>(this), i);
248}
249
Chris Lattnerf543eff2009-12-28 09:12:35 +0000250void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000251 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
252 ID.AddPointer(getOperand(i));
Devang Patelc95e6072010-03-25 05:36:13 +0000253 ID.AddBoolean(isFunctionLocal());
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000254}
Devang Patel5c310be2009-08-11 18:01:24 +0000255
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000256void MDNode::setIsNotUniqued() {
257 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
258 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
259 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000260}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000261
Chris Lattner9b493022009-12-31 01:22:29 +0000262// Replace value from this node's operand list.
263void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000264 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000265
Chris Lattner450e29c2010-04-28 20:16:12 +0000266 // If is possible that someone did GV->RAUW(inst), replacing a global variable
267 // with an instruction or some other function-local object. If this is a
268 // non-function-local MDNode, it can't point to a function-local object.
269 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000270 // Likewise if the MDNode is function-local but for a different function.
271 if (To && isFunctionLocalValue(To)) {
272 if (!isFunctionLocal())
273 To = 0;
274 else {
275 const Function *F = getFunction();
276 const Function *FV = getFunctionForValue(To);
277 // Metadata can be function-local without having an associated function.
278 // So only consider functions to have changed if non-null.
279 if (F && FV && F != FV)
280 To = 0;
281 }
282 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000283
Chris Lattner95c445d2009-12-28 09:32:10 +0000284 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000285 return;
Devang Patelf7188322009-09-03 01:39:20 +0000286
Chris Lattner30ae06b2009-12-30 21:42:11 +0000287 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000288 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000289
290 // If this node is already not being uniqued (because one of the operands
291 // already went to null), then there is nothing else to do here.
292 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000293
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000294 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
295
296 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
297 // this node to remove it, so we don't care what state the operands are in.
298 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000299
Chris Lattner30ae06b2009-12-30 21:42:11 +0000300 // If we are dropping an argument to null, we choose to not unique the MDNode
301 // anymore. This commonly occurs during destruction, and uniquing these
302 // brings little reuse.
303 if (To == 0) {
304 setIsNotUniqued();
305 return;
306 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000307
Chris Lattner30ae06b2009-12-30 21:42:11 +0000308 // Now that the node is out of the folding set, get ready to reinsert it.
309 // First, check to see if another node with the same operands already exists
310 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000311 FoldingSetNodeID ID;
312 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000313 void *InsertPoint;
314 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000315
316 if (N) {
317 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000318 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000319 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
320 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000321 }
322
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000323 // InsertPoint will have been set by the FindNodeOrInsertPos call.
324 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000325}
326
Devang Patel05a26fb2009-07-29 00:33:07 +0000327//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000328// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000329//
Devang Patel943ddf62010-01-12 18:34:06 +0000330
331namespace llvm {
332// SymbolTableListTraits specialization for MDSymbolTable.
333void ilist_traits<NamedMDNode>
334::addNodeToList(NamedMDNode *N) {
335 assert(N->getParent() == 0 && "Value already in a container!!");
336 Module *Owner = getListOwner();
337 N->setParent(Owner);
338 MDSymbolTable &ST = Owner->getMDSymbolTable();
339 ST.insert(N->getName(), N);
340}
341
342void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
343 N->setParent(0);
344 Module *Owner = getListOwner();
345 MDSymbolTable &ST = Owner->getMDSymbolTable();
346 ST.remove(N->getName());
347}
348}
349
Devang Patele3073482010-01-05 20:41:31 +0000350static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
351 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000352}
353
Devang Patelf76941e2010-01-12 18:57:56 +0000354NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000355 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000356 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000357 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000358 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000359 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000360
Devang Patele3073482010-01-05 20:41:31 +0000361 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000362 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000363 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000364
Devang Patel943ddf62010-01-12 18:34:06 +0000365 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000366 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000367}
Devang Patel79238d72009-08-03 06:19:01 +0000368
Devang Patel5c310be2009-08-11 18:01:24 +0000369NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000370 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000371 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000372 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000373
Chris Lattner9b493022009-12-31 01:22:29 +0000374 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
375 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000376 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
377 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000378}
379
Chris Lattner1bc810b2009-12-28 08:07:14 +0000380NamedMDNode::~NamedMDNode() {
381 dropAllReferences();
382 delete &getNMDOps(Operands);
383}
384
Chris Lattner9b493022009-12-31 01:22:29 +0000385/// getNumOperands - Return number of NamedMDNode operands.
386unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000387 return (unsigned)getNMDOps(Operands).size();
388}
389
Chris Lattner9b493022009-12-31 01:22:29 +0000390/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000391MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000392 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000393 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000394}
395
Chris Lattner9b493022009-12-31 01:22:29 +0000396/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000397void NamedMDNode::addOperand(MDNode *M) {
398 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000399}
400
Devang Patel79238d72009-08-03 06:19:01 +0000401/// eraseFromParent - Drop all references and remove the node from parent
402/// module.
403void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000404 getParent()->getNamedMDList().erase(this);
405}
406
407/// dropAllReferences - Remove all uses and clear node vector.
408void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000409 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000410}
411
Devang Patelfcfee0f2010-01-07 19:39:36 +0000412/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000413void NamedMDNode::setName(const Twine &NewName) {
414 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
415
416 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000417 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000418
Devang Patelf76941e2010-01-12 18:57:56 +0000419 // Name isn't changing?
420 if (getName() == NameRef)
421 return;
422
423 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000424 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000425 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000426}
427
428/// getName - Return a constant reference to this named metadata's name.
429StringRef NamedMDNode::getName() const {
430 return StringRef(Name);
431}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000432
433//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000434// Instruction Metadata method implementations.
435//
436
437void Instruction::setMetadata(const char *Kind, MDNode *Node) {
438 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000439 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000440}
441
442MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000443 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000444}
445
Chris Lattner009de332010-03-31 03:34:40 +0000446void Instruction::setDbgMetadata(MDNode *Node) {
Chris Lattner593916d2010-04-02 20:21:22 +0000447 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattner009de332010-03-31 03:34:40 +0000448}
449
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000450/// setMetadata - Set the metadata of of the specified kind to the specified
451/// node. This updates/replaces metadata if already present, or removes it if
452/// Node is null.
453void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
454 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000455
Chris Lattnerc263b422010-03-30 23:03:27 +0000456 // Handle 'dbg' as a special case since it is not stored in the hash table.
457 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000458 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000459 return;
460 }
461
Chris Lattnera0566972009-12-29 09:01:33 +0000462 // Handle the case when we're adding/updating metadata on an instruction.
463 if (Node) {
464 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000465 assert(!Info.empty() == hasMetadataHashEntry() &&
466 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000467 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000468 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000469 } else {
470 // Handle replacement of an existing value.
471 for (unsigned i = 0, e = Info.size(); i != e; ++i)
472 if (Info[i].first == KindID) {
473 Info[i].second = Node;
474 return;
475 }
476 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // No replacement, just add it to the list.
479 Info.push_back(std::make_pair(KindID, Node));
480 return;
481 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000482
Chris Lattnera0566972009-12-29 09:01:33 +0000483 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000484 assert(hasMetadataHashEntry() &&
485 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000486 "HasMetadata bit out of date!");
487 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000488
Chris Lattnera0566972009-12-29 09:01:33 +0000489 // Common case is removing the only entry.
490 if (Info.size() == 1 && Info[0].first == KindID) {
491 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000492 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000493 return;
494 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000495
Chris Lattnerc263b422010-03-30 23:03:27 +0000496 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000497 for (unsigned i = 0, e = Info.size(); i != e; ++i)
498 if (Info[i].first == KindID) {
499 Info[i] = Info.back();
500 Info.pop_back();
501 assert(!Info.empty() && "Removing last entry should be handled above");
502 return;
503 }
504 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000505}
506
507MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000508 // Handle 'dbg' as a special case since it is not stored in the hash table.
509 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000510 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000511
512 if (!hasMetadataHashEntry()) return 0;
513
Chris Lattnera0566972009-12-29 09:01:33 +0000514 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000515 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000516
Chris Lattnera0566972009-12-29 09:01:33 +0000517 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
518 I != E; ++I)
519 if (I->first == KindID)
520 return I->second;
521 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000522}
523
524void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000525 MDNode*> > &Result) const {
526 Result.clear();
527
528 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000529 if (!DbgLoc.isUnknown()) {
530 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
531 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000532 if (!hasMetadataHashEntry()) return;
533 }
534
535 assert(hasMetadataHashEntry() &&
536 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000537 "Shouldn't have called this");
538 const LLVMContextImpl::MDMapTy &Info =
539 getContext().pImpl->MetadataStore.find(this)->second;
540 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000541
Chris Lattnera0566972009-12-29 09:01:33 +0000542 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000543
Chris Lattnera0566972009-12-29 09:01:33 +0000544 // Sort the resulting array so it is stable.
545 if (Result.size() > 1)
546 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000547}
548
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000549void Instruction::
550getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
551 MDNode*> > &Result) const {
552 Result.clear();
553 assert(hasMetadataHashEntry() &&
554 getContext().pImpl->MetadataStore.count(this) &&
555 "Shouldn't have called this");
556 const LLVMContextImpl::MDMapTy &Info =
557 getContext().pImpl->MetadataStore.find(this)->second;
558 assert(!Info.empty() && "Shouldn't have called this");
559
560 Result.append(Info.begin(), Info.end());
561
562 // Sort the resulting array so it is stable.
563 if (Result.size() > 1)
564 array_pod_sort(Result.begin(), Result.end());
565}
566
567
Chris Lattner68017802009-12-29 07:44:16 +0000568/// removeAllMetadata - Remove all metadata from this instruction.
569void Instruction::removeAllMetadata() {
570 assert(hasMetadata() && "Caller should check");
Chris Lattner593916d2010-04-02 20:21:22 +0000571 DbgLoc = DebugLoc();
Chris Lattnerc263b422010-03-30 23:03:27 +0000572 if (hasMetadataHashEntry()) {
573 getContext().pImpl->MetadataStore.erase(this);
574 setHasMetadataHashEntry(false);
575 }
Chris Lattner68017802009-12-29 07:44:16 +0000576}
577