blob: cc4a68ec418a614afdae862d1716f924143bdc1b [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
Devang Patel943ddf62010-01-12 18:34:06 +0000333// SymbolTableListTraits specialization for MDSymbolTable.
Dan Gohman2b68cc62010-07-21 17:53:53 +0000334void ilist_traits<NamedMDNode>::addNodeToList(NamedMDNode *N) {
Devang Patel943ddf62010-01-12 18:34:06 +0000335 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}
Devang Patel943ddf62010-01-12 18:34:06 +0000348
Dan Gohman846b9e12010-07-21 18:01:42 +0000349static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
350 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000351}
352
Devang Patelf76941e2010-01-12 18:57:56 +0000353NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000354 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000355 unsigned NumMDs, Module *ParentModule)
Devang Patel99ff5a82010-01-09 00:30:14 +0000356 : Value(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000357 setName(N);
Dan Gohman846b9e12010-07-21 18:01:42 +0000358 Operands = new SmallVector<TrackingVH<MDNode>, 4>();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000359
Dan Gohman846b9e12010-07-21 18:01:42 +0000360 SmallVector<TrackingVH<MDNode>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000361 for (unsigned i = 0; i != NumMDs; ++i)
Dan Gohman846b9e12010-07-21 18:01:42 +0000362 Node.push_back(TrackingVH<MDNode>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000363
Devang Patel943ddf62010-01-12 18:34:06 +0000364 if (ParentModule)
Devang Patel4a942d02009-07-29 21:58:56 +0000365 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000366}
Devang Patel79238d72009-08-03 06:19:01 +0000367
Devang Patel5c310be2009-08-11 18:01:24 +0000368NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000369 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000370 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000371 Elems.reserve(NMD->getNumOperands());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000372
Chris Lattner9b493022009-12-31 01:22:29 +0000373 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
374 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000375 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
376 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000377}
378
Chris Lattner1bc810b2009-12-28 08:07:14 +0000379NamedMDNode::~NamedMDNode() {
380 dropAllReferences();
381 delete &getNMDOps(Operands);
382}
383
Chris Lattner9b493022009-12-31 01:22:29 +0000384/// getNumOperands - Return number of NamedMDNode operands.
385unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000386 return (unsigned)getNMDOps(Operands).size();
387}
388
Chris Lattner9b493022009-12-31 01:22:29 +0000389/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000390MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000391 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000392 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000393}
394
Chris Lattner9b493022009-12-31 01:22:29 +0000395/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000396void NamedMDNode::addOperand(MDNode *M) {
Dan Gohman846b9e12010-07-21 18:01:42 +0000397 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000398}
399
Devang Patel79238d72009-08-03 06:19:01 +0000400/// eraseFromParent - Drop all references and remove the node from parent
401/// module.
402void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000403 getParent()->getNamedMDList().erase(this);
404}
405
406/// dropAllReferences - Remove all uses and clear node vector.
407void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000408 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000409}
410
Devang Patelfcfee0f2010-01-07 19:39:36 +0000411/// setName - Set the name of this named metadata.
Devang Patelf76941e2010-01-12 18:57:56 +0000412void NamedMDNode::setName(const Twine &NewName) {
413 assert (!NewName.isTriviallyEmpty() && "Invalid named metadata name!");
414
415 SmallString<256> NameData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +0000416 StringRef NameRef = NewName.toStringRef(NameData);
Devang Patelf76941e2010-01-12 18:57:56 +0000417
Devang Patelf76941e2010-01-12 18:57:56 +0000418 // Name isn't changing?
419 if (getName() == NameRef)
420 return;
421
422 Name = NameRef.str();
Devang Patel943ddf62010-01-12 18:34:06 +0000423 if (Parent)
Devang Patelf76941e2010-01-12 18:57:56 +0000424 Parent->getMDSymbolTable().insert(NameRef, this);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000425}
426
427/// getName - Return a constant reference to this named metadata's name.
428StringRef NamedMDNode::getName() const {
429 return StringRef(Name);
430}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000431
432//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000433// Instruction Metadata method implementations.
434//
435
436void Instruction::setMetadata(const char *Kind, MDNode *Node) {
437 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000438 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000439}
440
441MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000442 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000443}
444
445/// setMetadata - Set the metadata of of the specified kind to the specified
446/// node. This updates/replaces metadata if already present, or removes it if
447/// Node is null.
448void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
449 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000450
Chris Lattnerc263b422010-03-30 23:03:27 +0000451 // Handle 'dbg' as a special case since it is not stored in the hash table.
452 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000453 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000454 return;
455 }
456
Chris Lattnera0566972009-12-29 09:01:33 +0000457 // Handle the case when we're adding/updating metadata on an instruction.
458 if (Node) {
459 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000460 assert(!Info.empty() == hasMetadataHashEntry() &&
461 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000462 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000463 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000464 } else {
465 // Handle replacement of an existing value.
466 for (unsigned i = 0, e = Info.size(); i != e; ++i)
467 if (Info[i].first == KindID) {
468 Info[i].second = Node;
469 return;
470 }
471 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000472
Chris Lattnera0566972009-12-29 09:01:33 +0000473 // No replacement, just add it to the list.
474 Info.push_back(std::make_pair(KindID, Node));
475 return;
476 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000479 assert(hasMetadataHashEntry() &&
480 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000481 "HasMetadata bit out of date!");
482 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000483
Chris Lattnera0566972009-12-29 09:01:33 +0000484 // Common case is removing the only entry.
485 if (Info.size() == 1 && Info[0].first == KindID) {
486 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000487 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000488 return;
489 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000490
Chris Lattnerc263b422010-03-30 23:03:27 +0000491 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000492 for (unsigned i = 0, e = Info.size(); i != e; ++i)
493 if (Info[i].first == KindID) {
494 Info[i] = Info.back();
495 Info.pop_back();
496 assert(!Info.empty() && "Removing last entry should be handled above");
497 return;
498 }
499 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000500}
501
502MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000503 // Handle 'dbg' as a special case since it is not stored in the hash table.
504 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000505 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000506
507 if (!hasMetadataHashEntry()) return 0;
508
Chris Lattnera0566972009-12-29 09:01:33 +0000509 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000510 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000511
Chris Lattnera0566972009-12-29 09:01:33 +0000512 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
513 I != E; ++I)
514 if (I->first == KindID)
515 return I->second;
516 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000517}
518
519void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000520 MDNode*> > &Result) const {
521 Result.clear();
522
523 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000524 if (!DbgLoc.isUnknown()) {
525 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
526 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000527 if (!hasMetadataHashEntry()) return;
528 }
529
530 assert(hasMetadataHashEntry() &&
531 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000532 "Shouldn't have called this");
533 const LLVMContextImpl::MDMapTy &Info =
534 getContext().pImpl->MetadataStore.find(this)->second;
535 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000536
Chris Lattnera0566972009-12-29 09:01:33 +0000537 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000538
Chris Lattnera0566972009-12-29 09:01:33 +0000539 // Sort the resulting array so it is stable.
540 if (Result.size() > 1)
541 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000542}
543
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000544void Instruction::
545getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
546 MDNode*> > &Result) const {
547 Result.clear();
548 assert(hasMetadataHashEntry() &&
549 getContext().pImpl->MetadataStore.count(this) &&
550 "Shouldn't have called this");
551 const LLVMContextImpl::MDMapTy &Info =
552 getContext().pImpl->MetadataStore.find(this)->second;
553 assert(!Info.empty() && "Shouldn't have called this");
554
555 Result.append(Info.begin(), Info.end());
556
557 // Sort the resulting array so it is stable.
558 if (Result.size() > 1)
559 array_pod_sort(Result.begin(), Result.end());
560}
561
562
Dan Gohman48a995f2010-07-20 22:25:04 +0000563/// clearMetadataHashEntries - Clear all hashtable-based metadata from
564/// this instruction.
565void Instruction::clearMetadataHashEntries() {
566 assert(hasMetadataHashEntry() && "Caller should check");
567 getContext().pImpl->MetadataStore.erase(this);
568 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000569}
570