blob: 1d3a058693035d61fcab06b6dbe8c8e7cd01da04 [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
Devang Patelb36df172010-07-06 21:05:17 +0000136 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000137 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000138 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000139 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000140 if (MDNode *MD = dyn_cast<MDNode>(V))
141 NewF = assertLocalFunction(MD);
142 else
143 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000144 }
Chris Lattner5522f052010-01-21 21:01:47 +0000145 if (F == 0)
146 F = NewF;
147 else
148 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000149 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000150 return F;
151}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000152#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000153
154// getFunction - If this metadata is function-local and recursively has a
155// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000156// Otherwise, return null. getFunction() should not be used for performance-
157// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000158const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000159#ifndef NDEBUG
160 return assertLocalFunction(this);
161#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000162 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000163 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
164 if (const Function *F = getFunctionForValue(getOperand(i)))
165 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000166 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000167}
168
Chris Lattner8cb6c342009-12-31 01:05:46 +0000169// destroy - Delete this node. Only when there are no uses.
170void MDNode::destroy() {
171 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
172 // Placement delete, the free the memory.
173 this->~MDNode();
174 free(this);
175}
176
Chris Lattner450e29c2010-04-28 20:16:12 +0000177/// isFunctionLocalValue - Return true if this is a value that would require a
178/// function-local MDNode.
179static bool isFunctionLocalValue(Value *V) {
180 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
181 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
182}
183
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000184MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000185 unsigned NumVals, FunctionLocalness FL,
186 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000187 LLVMContextImpl *pImpl = Context.pImpl;
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000188 bool isFunctionLocal = false;
189 switch (FL) {
190 case FL_Unknown:
191 for (unsigned i = 0; i != NumVals; ++i) {
192 Value *V = Vals[i];
193 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000194 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000195 isFunctionLocal = true;
196 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000197 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000198 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000199 break;
200 case FL_No:
201 isFunctionLocal = false;
202 break;
203 case FL_Yes:
204 isFunctionLocal = true;
205 break;
Devang Patelf7188322009-09-03 01:39:20 +0000206 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000207
Devang Patel44147112010-03-25 06:04:47 +0000208 FoldingSetNodeID ID;
209 for (unsigned i = 0; i != NumVals; ++i)
210 ID.AddPointer(Vals[i]);
211 ID.AddBoolean(isFunctionLocal);
212
213 void *InsertPoint;
214 MDNode *N = NULL;
215
216 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
217 return N;
218
219 if (!Insert)
220 return NULL;
221
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000222 // Coallocate space for the node and Operands together, then placement new.
223 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
224 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
225
226 // InsertPoint will have been set by the FindNodeOrInsertPos call.
227 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
228
Devang Patelf7188322009-09-03 01:39:20 +0000229 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000230}
231
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000232MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
233 return getMDNode(Context, Vals, NumVals, FL_Unknown);
234}
235
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000236MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000237 unsigned NumVals, bool isFunctionLocal) {
238 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
239}
240
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000241MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
242 unsigned NumVals) {
243 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
244}
245
Chris Lattner9b493022009-12-31 01:22:29 +0000246/// getOperand - Return specified operand.
247Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000248 return *getOperandPtr(const_cast<MDNode*>(this), i);
249}
250
Chris Lattnerf543eff2009-12-28 09:12:35 +0000251void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000252 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
253 ID.AddPointer(getOperand(i));
Devang Patelc95e6072010-03-25 05:36:13 +0000254 ID.AddBoolean(isFunctionLocal());
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000255}
Devang Patel5c310be2009-08-11 18:01:24 +0000256
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000257void MDNode::setIsNotUniqued() {
258 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
259 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
260 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000261}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000262
Chris Lattner9b493022009-12-31 01:22:29 +0000263// Replace value from this node's operand list.
264void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000265 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000266
Chris Lattner450e29c2010-04-28 20:16:12 +0000267 // If is possible that someone did GV->RAUW(inst), replacing a global variable
268 // with an instruction or some other function-local object. If this is a
269 // non-function-local MDNode, it can't point to a function-local object.
270 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000271 // Likewise if the MDNode is function-local but for a different function.
272 if (To && isFunctionLocalValue(To)) {
273 if (!isFunctionLocal())
274 To = 0;
275 else {
276 const Function *F = getFunction();
277 const Function *FV = getFunctionForValue(To);
278 // Metadata can be function-local without having an associated function.
279 // So only consider functions to have changed if non-null.
280 if (F && FV && F != FV)
281 To = 0;
282 }
283 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000284
Chris Lattner95c445d2009-12-28 09:32:10 +0000285 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000286 return;
Devang Patelf7188322009-09-03 01:39:20 +0000287
Chris Lattner30ae06b2009-12-30 21:42:11 +0000288 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000289 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000290
291 // If this node is already not being uniqued (because one of the operands
292 // already went to null), then there is nothing else to do here.
293 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000294
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000295 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
296
297 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
298 // this node to remove it, so we don't care what state the operands are in.
299 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000300
Chris Lattner30ae06b2009-12-30 21:42:11 +0000301 // If we are dropping an argument to null, we choose to not unique the MDNode
302 // anymore. This commonly occurs during destruction, and uniquing these
303 // brings little reuse.
304 if (To == 0) {
305 setIsNotUniqued();
306 return;
307 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000308
Chris Lattner30ae06b2009-12-30 21:42:11 +0000309 // Now that the node is out of the folding set, get ready to reinsert it.
310 // First, check to see if another node with the same operands already exists
311 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000312 FoldingSetNodeID ID;
313 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000314 void *InsertPoint;
315 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000316
317 if (N) {
318 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000319 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000320 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
321 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000322 }
323
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000324 // InsertPoint will have been set by the FindNodeOrInsertPos call.
325 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000326}
327
Devang Patel05a26fb2009-07-29 00:33:07 +0000328//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000329// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000330//
Devang Patel943ddf62010-01-12 18:34:06 +0000331
332namespace llvm {
333// SymbolTableListTraits specialization for MDSymbolTable.
334void ilist_traits<NamedMDNode>
335::addNodeToList(NamedMDNode *N) {
336 assert(N->getParent() == 0 && "Value already in a container!!");
337 Module *Owner = getListOwner();
338 N->setParent(Owner);
339 MDSymbolTable &ST = Owner->getMDSymbolTable();
340 ST.insert(N->getName(), N);
341}
342
343void ilist_traits<NamedMDNode>::removeNodeFromList(NamedMDNode *N) {
344 N->setParent(0);
345 Module *Owner = getListOwner();
346 MDSymbolTable &ST = Owner->getMDSymbolTable();
347 ST.remove(N->getName());
348}
349}
350
Devang Patele3073482010-01-05 20:41:31 +0000351static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
352 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000353}
354
Devang Patelf76941e2010-01-12 18:57:56 +0000355NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000356 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000357 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000358 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000359 setName(N);
Devang Patele3073482010-01-05 20:41:31 +0000360 Operands = new SmallVector<WeakVH, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000361
Devang Patele3073482010-01-05 20:41:31 +0000362 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000363 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000364 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000365
Devang Patel943ddf62010-01-12 18:34:06 +0000366 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000367 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000368}
Devang Patel79238d72009-08-03 06:19:01 +0000369
Devang Patel5c310be2009-08-11 18:01:24 +0000370NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000371 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000372 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000373 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000374
Chris Lattner9b493022009-12-31 01:22:29 +0000375 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
376 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000377 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
378 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000379}
380
Chris Lattner1bc810b2009-12-28 08:07:14 +0000381NamedMDNode::~NamedMDNode() {
382 dropAllReferences();
383 delete &getNMDOps(Operands);
384}
385
Chris Lattner9b493022009-12-31 01:22:29 +0000386/// getNumOperands - Return number of NamedMDNode operands.
387unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000388 return (unsigned)getNMDOps(Operands).size();
389}
390
Chris Lattner9b493022009-12-31 01:22:29 +0000391/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000392MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000393 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000394 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000395}
396
Chris Lattner9b493022009-12-31 01:22:29 +0000397/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000398void NamedMDNode::addOperand(MDNode *M) {
399 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000400}
401
Devang Patel79238d72009-08-03 06:19:01 +0000402/// eraseFromParent - Drop all references and remove the node from parent
403/// module.
404void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000405 getParent()->getNamedMDList().erase(this);
406}
407
408/// dropAllReferences - Remove all uses and clear node vector.
409void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000410 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000411}
412
Devang Patelfcfee0f2010-01-07 19:39:36 +0000413/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000414void NamedMDNode::setName(const Twine &NewName) {
415 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
416
417 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000418 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000419
Devang Patelf76941e2010-01-12 18:57:56 +0000420 // Name isn't changing?
421 if (getName() == NameRef)
422 return;
423
424 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000425 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000426 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000427}
428
429/// getName - Return a constant reference to this named metadata's name.
430StringRef NamedMDNode::getName() const {
431 return StringRef(Name);
432}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000433
434//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000435// Instruction Metadata method implementations.
436//
437
438void Instruction::setMetadata(const char *Kind, MDNode *Node) {
439 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000440 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000441}
442
443MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000444 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000445}
446
Chris Lattner009de332010-03-31 03:34:40 +0000447void Instruction::setDbgMetadata(MDNode *Node) {
Chris Lattner593916d2010-04-02 20:21:22 +0000448 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattner009de332010-03-31 03:34:40 +0000449}
450
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000451/// setMetadata - Set the metadata of of the specified kind to the specified
452/// node. This updates/replaces metadata if already present, or removes it if
453/// Node is null.
454void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
455 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000456
Chris Lattnerc263b422010-03-30 23:03:27 +0000457 // Handle 'dbg' as a special case since it is not stored in the hash table.
458 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000459 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000460 return;
461 }
462
Chris Lattnera0566972009-12-29 09:01:33 +0000463 // Handle the case when we're adding/updating metadata on an instruction.
464 if (Node) {
465 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000466 assert(!Info.empty() == hasMetadataHashEntry() &&
467 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000468 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000469 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000470 } else {
471 // Handle replacement of an existing value.
472 for (unsigned i = 0, e = Info.size(); i != e; ++i)
473 if (Info[i].first == KindID) {
474 Info[i].second = Node;
475 return;
476 }
477 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000478
Chris Lattnera0566972009-12-29 09:01:33 +0000479 // No replacement, just add it to the list.
480 Info.push_back(std::make_pair(KindID, Node));
481 return;
482 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000483
Chris Lattnera0566972009-12-29 09:01:33 +0000484 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000485 assert(hasMetadataHashEntry() &&
486 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000487 "HasMetadata bit out of date!");
488 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000489
Chris Lattnera0566972009-12-29 09:01:33 +0000490 // Common case is removing the only entry.
491 if (Info.size() == 1 && Info[0].first == KindID) {
492 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000493 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000494 return;
495 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000496
Chris Lattnerc263b422010-03-30 23:03:27 +0000497 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000498 for (unsigned i = 0, e = Info.size(); i != e; ++i)
499 if (Info[i].first == KindID) {
500 Info[i] = Info.back();
501 Info.pop_back();
502 assert(!Info.empty() && "Removing last entry should be handled above");
503 return;
504 }
505 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000506}
507
508MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000509 // Handle 'dbg' as a special case since it is not stored in the hash table.
510 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000511 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000512
513 if (!hasMetadataHashEntry()) return 0;
514
Chris Lattnera0566972009-12-29 09:01:33 +0000515 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000516 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000517
Chris Lattnera0566972009-12-29 09:01:33 +0000518 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
519 I != E; ++I)
520 if (I->first == KindID)
521 return I->second;
522 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000523}
524
525void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000526 MDNode*> > &Result) const {
527 Result.clear();
528
529 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000530 if (!DbgLoc.isUnknown()) {
531 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
532 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000533 if (!hasMetadataHashEntry()) return;
534 }
535
536 assert(hasMetadataHashEntry() &&
537 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000538 "Shouldn't have called this");
539 const LLVMContextImpl::MDMapTy &Info =
540 getContext().pImpl->MetadataStore.find(this)->second;
541 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000542
Chris Lattnera0566972009-12-29 09:01:33 +0000543 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000544
Chris Lattnera0566972009-12-29 09:01:33 +0000545 // Sort the resulting array so it is stable.
546 if (Result.size() > 1)
547 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000548}
549
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000550void Instruction::
551getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
552 MDNode*> > &Result) const {
553 Result.clear();
554 assert(hasMetadataHashEntry() &&
555 getContext().pImpl->MetadataStore.count(this) &&
556 "Shouldn't have called this");
557 const LLVMContextImpl::MDMapTy &Info =
558 getContext().pImpl->MetadataStore.find(this)->second;
559 assert(!Info.empty() && "Shouldn't have called this");
560
561 Result.append(Info.begin(), Info.end());
562
563 // Sort the resulting array so it is stable.
564 if (Result.size() > 1)
565 array_pod_sort(Result.begin(), Result.end());
566}
567
568
Chris Lattner68017802009-12-29 07:44:16 +0000569/// removeAllMetadata - Remove all metadata from this instruction.
570void Instruction::removeAllMetadata() {
571 assert(hasMetadata() && "Caller should check");
Chris Lattner593916d2010-04-02 20:21:22 +0000572 DbgLoc = DebugLoc();
Chris Lattnerc263b422010-03-30 23:03:27 +0000573 if (hasMetadataHashEntry()) {
574 getContext().pImpl->MetadataStore.erase(this);
575 setHasMetadataHashEntry(false);
576 }
Chris Lattner68017802009-12-29 07:44:16 +0000577}
578