blob: 99664334962a011f1947c014cbd89d687225de4b [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"
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000022#include "llvm/ADT/STLExtras.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000023#include "SymbolTableListTraitsImpl.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000024#include "llvm/Support/LeakDetector.h"
Chris Lattner1300f452009-12-28 08:24:16 +000025#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000029// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000030//
Chris Lattner5a409bd2009-12-28 08:30:43 +000031
David Blaikiea379b1812011-12-20 02:50:00 +000032void MDString::anchor() { }
33
Chris Lattner5a409bd2009-12-28 08:30:43 +000034MDString::MDString(LLVMContext &C, StringRef S)
Devang Patelac277eb2010-01-22 22:52:10 +000035 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000036
Devang Pateldcb99d32009-10-22 00:10:15 +000037MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000038 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000039 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000040 pImpl->MDStringCache.GetOrCreateValue(Str);
41 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000042 if (!S) S = new MDString(Context, Entry.getKey());
43 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000044}
45
46//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000047// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000048//
49
Chris Lattner9b493022009-12-31 01:22:29 +000050// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000051namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000052class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000053 MDNode *Parent;
54public:
Chris Lattner9b493022009-12-31 01:22:29 +000055 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
56 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000057
Chris Lattner8cb6c342009-12-31 01:05:46 +000058 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000059 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000060 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000061
Chris Lattner74a6ad62009-12-28 07:41:54 +000062 virtual void deleted();
63 virtual void allUsesReplacedWith(Value *NV);
64};
65} // end namespace llvm.
66
67
Chris Lattner9b493022009-12-31 01:22:29 +000068void MDNodeOperand::deleted() {
69 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000070}
71
Chris Lattner9b493022009-12-31 01:22:29 +000072void MDNodeOperand::allUsesReplacedWith(Value *NV) {
73 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000074}
75
76
77
78//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000079// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000080//
Chris Lattner74a6ad62009-12-28 07:41:54 +000081
Chris Lattner9b493022009-12-31 01:22:29 +000082/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000083/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000084static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000085 // Use <= instead of < to permit a one-past-the-end address.
86 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000087 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000088}
89
Jay Foad5514afe2011-04-21 19:59:31 +000090MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000091: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foad5514afe2011-04-21 19:59:31 +000092 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000093
Victor Hernandez0471abd2009-12-18 20:09:14 +000094 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000095 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +000096
97 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +000098 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +000099 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Jay Foad5514afe2011-04-21 19:59:31 +0000100 Op != E; ++Op, ++i)
101 new (Op) MDNodeOperand(Vals[i], this);
Devang Patela4f43fb2009-07-28 21:49:47 +0000102}
103
Chris Lattner8cb6c342009-12-31 01:05:46 +0000104
105/// ~MDNode - Destroy MDNode.
106MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000107 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000108 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000109 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
110 if (isNotUniqued()) {
111 pImpl->NonUniquedMDNodes.erase(this);
112 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000113 pImpl->MDNodeSet.RemoveNode(this);
114 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000115
Chris Lattner8cb6c342009-12-31 01:05:46 +0000116 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000117 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000118 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000119 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000120}
121
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000122static const Function *getFunctionForValue(Value *V) {
123 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000124 if (Instruction *I = dyn_cast<Instruction>(V)) {
125 BasicBlock *BB = I->getParent();
126 return BB ? BB->getParent() : 0;
127 }
Chris Lattner5522f052010-01-21 21:01:47 +0000128 if (Argument *A = dyn_cast<Argument>(V))
129 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000130 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
131 return BB->getParent();
132 if (MDNode *MD = dyn_cast<MDNode>(V))
133 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000134 return NULL;
135}
136
Victor Hernandez8296da72010-01-14 20:12:34 +0000137#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000138static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000139 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000140
Devang Patelb36df172010-07-06 21:05:17 +0000141 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000142 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000143 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000144 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000145 if (MDNode *MD = dyn_cast<MDNode>(V))
146 NewF = assertLocalFunction(MD);
147 else
148 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000149 }
Chris Lattner5522f052010-01-21 21:01:47 +0000150 if (F == 0)
151 F = NewF;
152 else
153 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000154 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000155 return F;
156}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000157#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000158
159// getFunction - If this metadata is function-local and recursively has a
160// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000161// Otherwise, return null. getFunction() should not be used for performance-
162// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000163const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000164#ifndef NDEBUG
165 return assertLocalFunction(this);
166#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000167 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000168 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
169 if (const Function *F = getFunctionForValue(getOperand(i)))
170 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000171 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000172}
173
Chris Lattner8cb6c342009-12-31 01:05:46 +0000174// destroy - Delete this node. Only when there are no uses.
175void MDNode::destroy() {
176 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
177 // Placement delete, the free the memory.
178 this->~MDNode();
179 free(this);
180}
181
Chris Lattner450e29c2010-04-28 20:16:12 +0000182/// isFunctionLocalValue - Return true if this is a value that would require a
183/// function-local MDNode.
184static bool isFunctionLocalValue(Value *V) {
185 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
186 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
187}
188
Jay Foad5514afe2011-04-21 19:59:31 +0000189MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
190 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000191 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000192
193 // Add all the operand pointers. Note that we don't have to add the
194 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000195 // Note that if the operands are later nulled out, the node will be
196 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000197 FoldingSetNodeID ID;
Jay Foad5514afe2011-04-21 19:59:31 +0000198 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman01579b22010-08-24 23:21:12 +0000199 ID.AddPointer(Vals[i]);
200
201 void *InsertPoint;
202 MDNode *N = NULL;
203
204 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
205 return N;
206
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000207 bool isFunctionLocal = false;
208 switch (FL) {
209 case FL_Unknown:
Jay Foad5514afe2011-04-21 19:59:31 +0000210 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000211 Value *V = Vals[i];
212 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000213 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000214 isFunctionLocal = true;
215 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000216 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000217 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000218 break;
219 case FL_No:
220 isFunctionLocal = false;
221 break;
222 case FL_Yes:
223 isFunctionLocal = true;
224 break;
Devang Patelf7188322009-09-03 01:39:20 +0000225 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000226
227 // Coallocate space for the node and Operands together, then placement new.
Jay Foad5514afe2011-04-21 19:59:31 +0000228 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
229 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000230
231 // InsertPoint will have been set by the FindNodeOrInsertPos call.
232 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
233
Devang Patelf7188322009-09-03 01:39:20 +0000234 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000235}
236
Devang Patel213264c2011-03-04 01:20:33 +0000237MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000238 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000239}
240
Jay Foad5514afe2011-04-21 19:59:31 +0000241MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
242 ArrayRef<Value*> Vals,
243 bool isFunctionLocal) {
244 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000245}
246
Jay Foad5514afe2011-04-21 19:59:31 +0000247MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
248 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000249}
250
Jay Foad5514afe2011-04-21 19:59:31 +0000251MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
252 MDNode *N =
253 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
254 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000255 N->setValueSubclassData(N->getSubclassDataFromValue() |
256 NotUniquedBit);
257 LeakDetector::addGarbageObject(N);
258 return N;
259}
260
261void MDNode::deleteTemporary(MDNode *N) {
262 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000263 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000264 "Deleting a non-temporary uniqued node!");
265 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
266 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000267 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
268 "Temporary MDNode does not have NotUniquedBit set!");
269 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000270 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000271 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000272 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000273}
274
Chris Lattner9b493022009-12-31 01:22:29 +0000275/// getOperand - Return specified operand.
276Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000277 return *getOperandPtr(const_cast<MDNode*>(this), i);
278}
279
Chris Lattnerf543eff2009-12-28 09:12:35 +0000280void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000281 // Add all the operand pointers. Note that we don't have to add the
282 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000283 // Note that if the operands are later nulled out, the node will be
284 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000285 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
286 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000287}
Devang Patel5c310be2009-08-11 18:01:24 +0000288
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000289void MDNode::setIsNotUniqued() {
290 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
291 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
292 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000293}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000294
Chris Lattner9b493022009-12-31 01:22:29 +0000295// Replace value from this node's operand list.
296void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000297 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000298
Chris Lattner450e29c2010-04-28 20:16:12 +0000299 // If is possible that someone did GV->RAUW(inst), replacing a global variable
300 // with an instruction or some other function-local object. If this is a
301 // non-function-local MDNode, it can't point to a function-local object.
302 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000303 // Likewise if the MDNode is function-local but for a different function.
304 if (To && isFunctionLocalValue(To)) {
305 if (!isFunctionLocal())
306 To = 0;
307 else {
308 const Function *F = getFunction();
309 const Function *FV = getFunctionForValue(To);
310 // Metadata can be function-local without having an associated function.
311 // So only consider functions to have changed if non-null.
312 if (F && FV && F != FV)
313 To = 0;
314 }
315 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000316
Chris Lattner95c445d2009-12-28 09:32:10 +0000317 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000318 return;
Devang Patelf7188322009-09-03 01:39:20 +0000319
Chris Lattner30ae06b2009-12-30 21:42:11 +0000320 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000321 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000322
323 // If this node is already not being uniqued (because one of the operands
324 // already went to null), then there is nothing else to do here.
325 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000326
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000327 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
328
329 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
330 // this node to remove it, so we don't care what state the operands are in.
331 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000332
Chris Lattner30ae06b2009-12-30 21:42:11 +0000333 // If we are dropping an argument to null, we choose to not unique the MDNode
334 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000335 // brings little reuse. Also, this means we don't need to include
336 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000337 if (To == 0) {
338 setIsNotUniqued();
339 return;
340 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000341
Chris Lattner30ae06b2009-12-30 21:42:11 +0000342 // Now that the node is out of the folding set, get ready to reinsert it.
343 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000344 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000345 FoldingSetNodeID ID;
346 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000347 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000348 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000349 replaceAllUsesWith(N);
350 destroy();
351 return;
Devang Patelf7188322009-09-03 01:39:20 +0000352 }
353
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000354 // InsertPoint will have been set by the FindNodeOrInsertPos call.
355 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000356
357 // If this MDValue was previously function-local but no longer is, clear
358 // its function-local flag.
359 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
360 bool isStillFunctionLocal = false;
361 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
362 Value *V = getOperand(i);
363 if (!V) continue;
364 if (isFunctionLocalValue(V)) {
365 isStillFunctionLocal = true;
366 break;
367 }
368 }
369 if (!isStillFunctionLocal)
370 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
371 }
Devang Patelf7188322009-09-03 01:39:20 +0000372}
373
Devang Patel05a26fb2009-07-29 00:33:07 +0000374//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000375// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000376//
Devang Patel943ddf62010-01-12 18:34:06 +0000377
Dan Gohman846b9e12010-07-21 18:01:42 +0000378static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
379 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000380}
381
Dan Gohman2637cc12010-07-21 23:38:33 +0000382NamedMDNode::NamedMDNode(const Twine &N)
383 : Name(N.str()), Parent(0),
384 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000385}
386
Chris Lattner1bc810b2009-12-28 08:07:14 +0000387NamedMDNode::~NamedMDNode() {
388 dropAllReferences();
389 delete &getNMDOps(Operands);
390}
391
Chris Lattner9b493022009-12-31 01:22:29 +0000392/// getNumOperands - Return number of NamedMDNode operands.
393unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000394 return (unsigned)getNMDOps(Operands).size();
395}
396
Chris Lattner9b493022009-12-31 01:22:29 +0000397/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000398MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000399 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000400 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000401}
402
Chris Lattner9b493022009-12-31 01:22:29 +0000403/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000404void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000405 assert(!M->isFunctionLocal() &&
406 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000407 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000408}
409
Devang Patel79238d72009-08-03 06:19:01 +0000410/// eraseFromParent - Drop all references and remove the node from parent
411/// module.
412void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000413 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000414}
415
416/// dropAllReferences - Remove all uses and clear node vector.
417void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000418 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000419}
420
Devang Patelfcfee0f2010-01-07 19:39:36 +0000421/// getName - Return a constant reference to this named metadata's name.
422StringRef NamedMDNode::getName() const {
423 return StringRef(Name);
424}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000425
426//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000427// Instruction Metadata method implementations.
428//
429
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000430void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000431 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000432 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000433}
434
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000435MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000436 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000437}
438
439/// setMetadata - Set the metadata of of the specified kind to the specified
440/// node. This updates/replaces metadata if already present, or removes it if
441/// Node is null.
442void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
443 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000444
Chris Lattnerc263b422010-03-30 23:03:27 +0000445 // Handle 'dbg' as a special case since it is not stored in the hash table.
446 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000447 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000448 return;
449 }
450
Chris Lattnera0566972009-12-29 09:01:33 +0000451 // Handle the case when we're adding/updating metadata on an instruction.
452 if (Node) {
453 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000454 assert(!Info.empty() == hasMetadataHashEntry() &&
455 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000456 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000457 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000458 } else {
459 // Handle replacement of an existing value.
460 for (unsigned i = 0, e = Info.size(); i != e; ++i)
461 if (Info[i].first == KindID) {
462 Info[i].second = Node;
463 return;
464 }
465 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000466
Chris Lattnera0566972009-12-29 09:01:33 +0000467 // No replacement, just add it to the list.
468 Info.push_back(std::make_pair(KindID, Node));
469 return;
470 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000471
Chris Lattnera0566972009-12-29 09:01:33 +0000472 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000473 assert(hasMetadataHashEntry() &&
474 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000475 "HasMetadata bit out of date!");
476 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // Common case is removing the only entry.
479 if (Info.size() == 1 && Info[0].first == KindID) {
480 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000481 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000482 return;
483 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000484
Chris Lattnerc263b422010-03-30 23:03:27 +0000485 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000486 for (unsigned i = 0, e = Info.size(); i != e; ++i)
487 if (Info[i].first == KindID) {
488 Info[i] = Info.back();
489 Info.pop_back();
490 assert(!Info.empty() && "Removing last entry should be handled above");
491 return;
492 }
493 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000494}
495
496MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000497 // Handle 'dbg' as a special case since it is not stored in the hash table.
498 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000499 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000500
501 if (!hasMetadataHashEntry()) return 0;
502
Chris Lattnera0566972009-12-29 09:01:33 +0000503 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000504 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000505
Chris Lattnera0566972009-12-29 09:01:33 +0000506 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
507 I != E; ++I)
508 if (I->first == KindID)
509 return I->second;
510 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000511}
512
513void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000514 MDNode*> > &Result) const {
515 Result.clear();
516
517 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000518 if (!DbgLoc.isUnknown()) {
519 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
520 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000521 if (!hasMetadataHashEntry()) return;
522 }
523
524 assert(hasMetadataHashEntry() &&
525 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000526 "Shouldn't have called this");
527 const LLVMContextImpl::MDMapTy &Info =
528 getContext().pImpl->MetadataStore.find(this)->second;
529 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000530
Chris Lattnera0566972009-12-29 09:01:33 +0000531 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000532
Chris Lattnera0566972009-12-29 09:01:33 +0000533 // Sort the resulting array so it is stable.
534 if (Result.size() > 1)
535 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000536}
537
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000538void Instruction::
539getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
540 MDNode*> > &Result) const {
541 Result.clear();
542 assert(hasMetadataHashEntry() &&
543 getContext().pImpl->MetadataStore.count(this) &&
544 "Shouldn't have called this");
545 const LLVMContextImpl::MDMapTy &Info =
546 getContext().pImpl->MetadataStore.find(this)->second;
547 assert(!Info.empty() && "Shouldn't have called this");
548
549 Result.append(Info.begin(), Info.end());
550
551 // Sort the resulting array so it is stable.
552 if (Result.size() > 1)
553 array_pod_sort(Result.begin(), Result.end());
554}
555
556
Dan Gohman48a995f2010-07-20 22:25:04 +0000557/// clearMetadataHashEntries - Clear all hashtable-based metadata from
558/// this instruction.
559void Instruction::clearMetadataHashEntries() {
560 assert(hasMetadataHashEntry() && "Caller should check");
561 getContext().pImpl->MetadataStore.erase(this);
562 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000563}
564