blob: 162884c5f689e6289a35d4cba54d5711db1e63f2 [file] [log] [blame]
Devang Patela4f43fb2009-07-28 21:49:47 +00001//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Metadata.h"
Chris Lattner1300f452009-12-28 08:24:16 +000015#include "LLVMContextImpl.h"
Owen Anderson0087fe62009-07-31 21:35:40 +000016#include "llvm/LLVMContext.h"
Devang Patel05a26fb2009-07-29 00:33:07 +000017#include "llvm/Module.h"
Devang Pateld5497a4b2009-09-16 18:09:00 +000018#include "llvm/Instruction.h"
Devang Patel1155fdf2009-10-22 19:36:54 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
Devang Patelf76941e2010-01-12 18:57:56 +000021#include "llvm/ADT/SmallString.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000022#include "SymbolTableListTraitsImpl.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000023#include "llvm/Support/LeakDetector.h"
Chris Lattner1300f452009-12-28 08:24:16 +000024#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000028// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000029//
Chris Lattner5a409bd2009-12-28 08:30:43 +000030
31MDString::MDString(LLVMContext &C, StringRef S)
Devang Patelac277eb2010-01-22 22:52:10 +000032 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000033
Devang Pateldcb99d32009-10-22 00:10:15 +000034MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000035 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000036 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000037 pImpl->MDStringCache.GetOrCreateValue(Str);
38 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000039 if (!S) S = new MDString(Context, Entry.getKey());
40 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000041}
42
43//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000044// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000045//
46
Chris Lattner9b493022009-12-31 01:22:29 +000047// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000048namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000049class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000050 MDNode *Parent;
51public:
Chris Lattner9b493022009-12-31 01:22:29 +000052 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
53 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000054
Chris Lattner8cb6c342009-12-31 01:05:46 +000055 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000056 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000057 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000058
Chris Lattner74a6ad62009-12-28 07:41:54 +000059 virtual void deleted();
60 virtual void allUsesReplacedWith(Value *NV);
61};
62} // end namespace llvm.
63
64
Chris Lattner9b493022009-12-31 01:22:29 +000065void MDNodeOperand::deleted() {
66 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000067}
68
Chris Lattner9b493022009-12-31 01:22:29 +000069void MDNodeOperand::allUsesReplacedWith(Value *NV) {
70 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000071}
72
73
74
75//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000076// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000077//
Chris Lattner74a6ad62009-12-28 07:41:54 +000078
Chris Lattner9b493022009-12-31 01:22:29 +000079/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000080/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000081static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000082 // Use <= instead of < to permit a one-past-the-end address.
83 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000084 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000085}
86
Victor Hernandezdd7418a2009-12-16 02:52:09 +000087MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000088 bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000089: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000090 NumOperands = NumVals;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000091
Victor Hernandez0471abd2009-12-18 20:09:14 +000092 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000093 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +000094
95 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +000096 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +000097 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +000098 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +000099}
100
Chris Lattner8cb6c342009-12-31 01:05:46 +0000101
102/// ~MDNode - Destroy MDNode.
103MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000104 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000105 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000106 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
107 if (isNotUniqued()) {
108 pImpl->NonUniquedMDNodes.erase(this);
109 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000110 pImpl->MDNodeSet.RemoveNode(this);
111 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000112
Chris Lattner8cb6c342009-12-31 01:05:46 +0000113 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000114 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000115 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000116 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000117}
118
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000119static const Function *getFunctionForValue(Value *V) {
120 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000121 if (Instruction *I = dyn_cast<Instruction>(V)) {
122 BasicBlock *BB = I->getParent();
123 return BB ? BB->getParent() : 0;
124 }
Chris Lattner5522f052010-01-21 21:01:47 +0000125 if (Argument *A = dyn_cast<Argument>(V))
126 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000127 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
128 return BB->getParent();
129 if (MDNode *MD = dyn_cast<MDNode>(V))
130 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000131 return NULL;
132}
133
Victor Hernandez8296da72010-01-14 20:12:34 +0000134#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000135static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000136 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000137
Devang Patelb36df172010-07-06 21:05:17 +0000138 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000139 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000140 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000141 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000142 if (MDNode *MD = dyn_cast<MDNode>(V))
143 NewF = assertLocalFunction(MD);
144 else
145 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000146 }
Chris Lattner5522f052010-01-21 21:01:47 +0000147 if (F == 0)
148 F = NewF;
149 else
150 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000151 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000152 return F;
153}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000154#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000155
156// getFunction - If this metadata is function-local and recursively has a
157// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000158// Otherwise, return null. getFunction() should not be used for performance-
159// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000160const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000161#ifndef NDEBUG
162 return assertLocalFunction(this);
163#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000164 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000165 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
166 if (const Function *F = getFunctionForValue(getOperand(i)))
167 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000168 return NULL;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000169}
170
Chris Lattner8cb6c342009-12-31 01:05:46 +0000171// destroy - Delete this node. Only when there are no uses.
172void MDNode::destroy() {
173 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
174 // Placement delete, the free the memory.
175 this->~MDNode();
176 free(this);
177}
178
Chris Lattner450e29c2010-04-28 20:16:12 +0000179/// isFunctionLocalValue - Return true if this is a value that would require a
180/// function-local MDNode.
181static bool isFunctionLocalValue(Value *V) {
182 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
183 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
184}
185
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000186MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000187 unsigned NumVals, FunctionLocalness FL,
188 bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000189 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000190
191 // Add all the operand pointers. Note that we don't have to add the
192 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000193 // Note that if the operands are later nulled out, the node will be
194 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000195 FoldingSetNodeID ID;
196 for (unsigned i = 0; i != NumVals; ++i)
197 ID.AddPointer(Vals[i]);
198
199 void *InsertPoint;
200 MDNode *N = NULL;
201
202 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
203 return N;
204
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000205 bool isFunctionLocal = false;
206 switch (FL) {
207 case FL_Unknown:
208 for (unsigned i = 0; i != NumVals; ++i) {
209 Value *V = Vals[i];
210 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000211 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000212 isFunctionLocal = true;
213 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000214 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000215 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000216 break;
217 case FL_No:
218 isFunctionLocal = false;
219 break;
220 case FL_Yes:
221 isFunctionLocal = true;
222 break;
Devang Patelf7188322009-09-03 01:39:20 +0000223 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000224
225 // Coallocate space for the node and Operands together, then placement new.
226 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
227 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
228
229 // InsertPoint will have been set by the FindNodeOrInsertPos call.
230 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
231
Devang Patelf7188322009-09-03 01:39:20 +0000232 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000233}
234
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000235MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
236 return getMDNode(Context, Vals, NumVals, FL_Unknown);
237}
238
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000239MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000240 unsigned NumVals, bool isFunctionLocal) {
241 return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
242}
243
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000244MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
245 unsigned NumVals) {
246 return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
247}
248
Dan Gohman16a5d982010-08-20 22:02:26 +0000249MDNode *MDNode::getTemporary(LLVMContext &Context, Value *const *Vals,
250 unsigned NumVals) {
251 MDNode *N = (MDNode *)malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
252 N = new (N) MDNode(Context, Vals, NumVals, FL_No);
253 N->setValueSubclassData(N->getSubclassDataFromValue() |
254 NotUniquedBit);
255 LeakDetector::addGarbageObject(N);
256 return N;
257}
258
259void MDNode::deleteTemporary(MDNode *N) {
260 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000261 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000262 "Deleting a non-temporary uniqued node!");
263 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
264 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000265 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
266 "Temporary MDNode does not have NotUniquedBit set!");
267 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000268 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000269 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000270 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000271}
272
Chris Lattner9b493022009-12-31 01:22:29 +0000273/// getOperand - Return specified operand.
274Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000275 return *getOperandPtr(const_cast<MDNode*>(this), i);
276}
277
Chris Lattnerf543eff2009-12-28 09:12:35 +0000278void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000279 // Add all the operand pointers. Note that we don't have to add the
280 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000281 // Note that if the operands are later nulled out, the node will be
282 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000283 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
284 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000285}
Devang Patel5c310be2009-08-11 18:01:24 +0000286
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000287void MDNode::setIsNotUniqued() {
288 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
289 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
290 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000291}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000292
Chris Lattner9b493022009-12-31 01:22:29 +0000293// Replace value from this node's operand list.
294void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000295 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000296
Chris Lattner450e29c2010-04-28 20:16:12 +0000297 // If is possible that someone did GV->RAUW(inst), replacing a global variable
298 // with an instruction or some other function-local object. If this is a
299 // non-function-local MDNode, it can't point to a function-local object.
300 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000301 // Likewise if the MDNode is function-local but for a different function.
302 if (To && isFunctionLocalValue(To)) {
303 if (!isFunctionLocal())
304 To = 0;
305 else {
306 const Function *F = getFunction();
307 const Function *FV = getFunctionForValue(To);
308 // Metadata can be function-local without having an associated function.
309 // So only consider functions to have changed if non-null.
310 if (F && FV && F != FV)
311 To = 0;
312 }
313 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000314
Chris Lattner95c445d2009-12-28 09:32:10 +0000315 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000316 return;
Devang Patelf7188322009-09-03 01:39:20 +0000317
Chris Lattner30ae06b2009-12-30 21:42:11 +0000318 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000319 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000320
321 // If this node is already not being uniqued (because one of the operands
322 // already went to null), then there is nothing else to do here.
323 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000324
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000325 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
326
327 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
328 // this node to remove it, so we don't care what state the operands are in.
329 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000330
Chris Lattner30ae06b2009-12-30 21:42:11 +0000331 // If we are dropping an argument to null, we choose to not unique the MDNode
332 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000333 // brings little reuse. Also, this means we don't need to include
334 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000335 if (To == 0) {
336 setIsNotUniqued();
337 return;
338 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000339
Chris Lattner30ae06b2009-12-30 21:42:11 +0000340 // Now that the node is out of the folding set, get ready to reinsert it.
341 // First, check to see if another node with the same operands already exists
342 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000343 FoldingSetNodeID ID;
344 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000345 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000346 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Devang Patelf7188322009-09-03 01:39:20 +0000347 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000348 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000349 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
350 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000351 }
352
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000353 // InsertPoint will have been set by the FindNodeOrInsertPos call.
354 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000355
356 // If this MDValue was previously function-local but no longer is, clear
357 // its function-local flag.
358 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
359 bool isStillFunctionLocal = false;
360 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
361 Value *V = getOperand(i);
362 if (!V) continue;
363 if (isFunctionLocalValue(V)) {
364 isStillFunctionLocal = true;
365 break;
366 }
367 }
368 if (!isStillFunctionLocal)
369 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
370 }
Devang Patelf7188322009-09-03 01:39:20 +0000371}
372
Devang Patel05a26fb2009-07-29 00:33:07 +0000373//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000374// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000375//
Devang Patel943ddf62010-01-12 18:34:06 +0000376
Dan Gohman846b9e12010-07-21 18:01:42 +0000377static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
378 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000379}
380
Dan Gohman2637cc12010-07-21 23:38:33 +0000381NamedMDNode::NamedMDNode(const Twine &N)
382 : Name(N.str()), Parent(0),
383 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000384}
385
Chris Lattner1bc810b2009-12-28 08:07:14 +0000386NamedMDNode::~NamedMDNode() {
387 dropAllReferences();
388 delete &getNMDOps(Operands);
389}
390
Chris Lattner9b493022009-12-31 01:22:29 +0000391/// getNumOperands - Return number of NamedMDNode operands.
392unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000393 return (unsigned)getNMDOps(Operands).size();
394}
395
Chris Lattner9b493022009-12-31 01:22:29 +0000396/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000397MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000398 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000399 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000400}
401
Chris Lattner9b493022009-12-31 01:22:29 +0000402/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000403void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000404 assert(!M->isFunctionLocal() &&
405 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000406 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000407}
408
Devang Patel79238d72009-08-03 06:19:01 +0000409/// eraseFromParent - Drop all references and remove the node from parent
410/// module.
411void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000412 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000413}
414
415/// dropAllReferences - Remove all uses and clear node vector.
416void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000417 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000418}
419
Devang Patelfcfee0f2010-01-07 19:39:36 +0000420/// getName - Return a constant reference to this named metadata's name.
421StringRef NamedMDNode::getName() const {
422 return StringRef(Name);
423}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000424
425//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000426// Instruction Metadata method implementations.
427//
428
429void Instruction::setMetadata(const char *Kind, MDNode *Node) {
430 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000431 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000432}
433
434MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000435 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000436}
437
438/// setMetadata - Set the metadata of of the specified kind to the specified
439/// node. This updates/replaces metadata if already present, or removes it if
440/// Node is null.
441void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
442 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000443
Chris Lattnerc263b422010-03-30 23:03:27 +0000444 // Handle 'dbg' as a special case since it is not stored in the hash table.
445 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000446 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000447 return;
448 }
449
Chris Lattnera0566972009-12-29 09:01:33 +0000450 // Handle the case when we're adding/updating metadata on an instruction.
451 if (Node) {
452 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000453 assert(!Info.empty() == hasMetadataHashEntry() &&
454 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000455 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000456 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000457 } else {
458 // Handle replacement of an existing value.
459 for (unsigned i = 0, e = Info.size(); i != e; ++i)
460 if (Info[i].first == KindID) {
461 Info[i].second = Node;
462 return;
463 }
464 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000465
Chris Lattnera0566972009-12-29 09:01:33 +0000466 // No replacement, just add it to the list.
467 Info.push_back(std::make_pair(KindID, Node));
468 return;
469 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000470
Chris Lattnera0566972009-12-29 09:01:33 +0000471 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerc263b422010-03-30 23:03:27 +0000472 assert(hasMetadataHashEntry() &&
473 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000474 "HasMetadata bit out of date!");
475 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000476
Chris Lattnera0566972009-12-29 09:01:33 +0000477 // Common case is removing the only entry.
478 if (Info.size() == 1 && Info[0].first == KindID) {
479 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000480 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000481 return;
482 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000483
Chris Lattnerc263b422010-03-30 23:03:27 +0000484 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000485 for (unsigned i = 0, e = Info.size(); i != e; ++i)
486 if (Info[i].first == KindID) {
487 Info[i] = Info.back();
488 Info.pop_back();
489 assert(!Info.empty() && "Removing last entry should be handled above");
490 return;
491 }
492 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000493}
494
495MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000496 // Handle 'dbg' as a special case since it is not stored in the hash table.
497 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000498 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000499
500 if (!hasMetadataHashEntry()) return 0;
501
Chris Lattnera0566972009-12-29 09:01:33 +0000502 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000503 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000504
Chris Lattnera0566972009-12-29 09:01:33 +0000505 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
506 I != E; ++I)
507 if (I->first == KindID)
508 return I->second;
509 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000510}
511
512void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000513 MDNode*> > &Result) const {
514 Result.clear();
515
516 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000517 if (!DbgLoc.isUnknown()) {
518 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
519 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000520 if (!hasMetadataHashEntry()) return;
521 }
522
523 assert(hasMetadataHashEntry() &&
524 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000525 "Shouldn't have called this");
526 const LLVMContextImpl::MDMapTy &Info =
527 getContext().pImpl->MetadataStore.find(this)->second;
528 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000529
Chris Lattnera0566972009-12-29 09:01:33 +0000530 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000531
Chris Lattnera0566972009-12-29 09:01:33 +0000532 // Sort the resulting array so it is stable.
533 if (Result.size() > 1)
534 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000535}
536
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000537void Instruction::
538getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
539 MDNode*> > &Result) const {
540 Result.clear();
541 assert(hasMetadataHashEntry() &&
542 getContext().pImpl->MetadataStore.count(this) &&
543 "Shouldn't have called this");
544 const LLVMContextImpl::MDMapTy &Info =
545 getContext().pImpl->MetadataStore.find(this)->second;
546 assert(!Info.empty() && "Shouldn't have called this");
547
548 Result.append(Info.begin(), Info.end());
549
550 // Sort the resulting array so it is stable.
551 if (Result.size() > 1)
552 array_pod_sort(Result.begin(), Result.end());
553}
554
555
Dan Gohman48a995f2010-07-20 22:25:04 +0000556/// clearMetadataHashEntries - Clear all hashtable-based metadata from
557/// this instruction.
558void Instruction::clearMetadataHashEntries() {
559 assert(hasMetadataHashEntry() && "Caller should check");
560 getContext().pImpl->MetadataStore.erase(this);
561 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000562}
563