blob: 5d6eb144c3e7ac619bbe3a51d031d9db0233cd1b [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);
David Blaikie46a9f012012-01-20 21:51:11 +0000166#else
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;
David Blaikie46a9f012012-01-20 21:51:11 +0000172#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000173}
174
Chris Lattner8cb6c342009-12-31 01:05:46 +0000175// destroy - Delete this node. Only when there are no uses.
176void MDNode::destroy() {
177 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
178 // Placement delete, the free the memory.
179 this->~MDNode();
180 free(this);
181}
182
Chris Lattner450e29c2010-04-28 20:16:12 +0000183/// isFunctionLocalValue - Return true if this is a value that would require a
184/// function-local MDNode.
185static bool isFunctionLocalValue(Value *V) {
186 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
187 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
188}
189
Jay Foad5514afe2011-04-21 19:59:31 +0000190MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
191 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000192 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000193
194 // Add all the operand pointers. Note that we don't have to add the
195 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000196 // Note that if the operands are later nulled out, the node will be
197 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000198 FoldingSetNodeID ID;
Jay Foad5514afe2011-04-21 19:59:31 +0000199 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman01579b22010-08-24 23:21:12 +0000200 ID.AddPointer(Vals[i]);
201
202 void *InsertPoint;
203 MDNode *N = NULL;
204
205 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
206 return N;
207
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000208 bool isFunctionLocal = false;
209 switch (FL) {
210 case FL_Unknown:
Jay Foad5514afe2011-04-21 19:59:31 +0000211 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000212 Value *V = Vals[i];
213 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000214 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000215 isFunctionLocal = true;
216 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000217 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000218 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000219 break;
220 case FL_No:
221 isFunctionLocal = false;
222 break;
223 case FL_Yes:
224 isFunctionLocal = true;
225 break;
Devang Patelf7188322009-09-03 01:39:20 +0000226 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000227
228 // Coallocate space for the node and Operands together, then placement new.
Jay Foad5514afe2011-04-21 19:59:31 +0000229 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
230 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000231
232 // InsertPoint will have been set by the FindNodeOrInsertPos call.
233 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
234
Devang Patelf7188322009-09-03 01:39:20 +0000235 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000236}
237
Devang Patel213264c2011-03-04 01:20:33 +0000238MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000239 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000240}
241
Jay Foad5514afe2011-04-21 19:59:31 +0000242MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
243 ArrayRef<Value*> Vals,
244 bool isFunctionLocal) {
245 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000246}
247
Jay Foad5514afe2011-04-21 19:59:31 +0000248MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
249 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000250}
251
Jay Foad5514afe2011-04-21 19:59:31 +0000252MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
253 MDNode *N =
254 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
255 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000256 N->setValueSubclassData(N->getSubclassDataFromValue() |
257 NotUniquedBit);
258 LeakDetector::addGarbageObject(N);
259 return N;
260}
261
262void MDNode::deleteTemporary(MDNode *N) {
263 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000264 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000265 "Deleting a non-temporary uniqued node!");
266 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
267 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000268 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
269 "Temporary MDNode does not have NotUniquedBit set!");
270 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000271 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000272 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000273 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000274}
275
Chris Lattner9b493022009-12-31 01:22:29 +0000276/// getOperand - Return specified operand.
277Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000278 return *getOperandPtr(const_cast<MDNode*>(this), i);
279}
280
Chris Lattnerf543eff2009-12-28 09:12:35 +0000281void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000282 // Add all the operand pointers. Note that we don't have to add the
283 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000284 // Note that if the operands are later nulled out, the node will be
285 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000286 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
287 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000288}
Devang Patel5c310be2009-08-11 18:01:24 +0000289
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000290void MDNode::setIsNotUniqued() {
291 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
292 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
293 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000294}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000295
Chris Lattner9b493022009-12-31 01:22:29 +0000296// Replace value from this node's operand list.
297void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000298 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000299
Chris Lattner450e29c2010-04-28 20:16:12 +0000300 // If is possible that someone did GV->RAUW(inst), replacing a global variable
301 // with an instruction or some other function-local object. If this is a
302 // non-function-local MDNode, it can't point to a function-local object.
303 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000304 // Likewise if the MDNode is function-local but for a different function.
305 if (To && isFunctionLocalValue(To)) {
306 if (!isFunctionLocal())
307 To = 0;
308 else {
309 const Function *F = getFunction();
310 const Function *FV = getFunctionForValue(To);
311 // Metadata can be function-local without having an associated function.
312 // So only consider functions to have changed if non-null.
313 if (F && FV && F != FV)
314 To = 0;
315 }
316 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000317
Chris Lattner95c445d2009-12-28 09:32:10 +0000318 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000319 return;
Devang Patelf7188322009-09-03 01:39:20 +0000320
Chris Lattner30ae06b2009-12-30 21:42:11 +0000321 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000322 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000323
324 // If this node is already not being uniqued (because one of the operands
325 // already went to null), then there is nothing else to do here.
326 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000327
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000328 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
329
330 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
331 // this node to remove it, so we don't care what state the operands are in.
332 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000333
Chris Lattner30ae06b2009-12-30 21:42:11 +0000334 // If we are dropping an argument to null, we choose to not unique the MDNode
335 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000336 // brings little reuse. Also, this means we don't need to include
337 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000338 if (To == 0) {
339 setIsNotUniqued();
340 return;
341 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000342
Chris Lattner30ae06b2009-12-30 21:42:11 +0000343 // Now that the node is out of the folding set, get ready to reinsert it.
344 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000345 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000346 FoldingSetNodeID ID;
347 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000348 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000349 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000350 replaceAllUsesWith(N);
351 destroy();
352 return;
Devang Patelf7188322009-09-03 01:39:20 +0000353 }
354
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000355 // InsertPoint will have been set by the FindNodeOrInsertPos call.
356 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000357
358 // If this MDValue was previously function-local but no longer is, clear
359 // its function-local flag.
360 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
361 bool isStillFunctionLocal = false;
362 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
363 Value *V = getOperand(i);
364 if (!V) continue;
365 if (isFunctionLocalValue(V)) {
366 isStillFunctionLocal = true;
367 break;
368 }
369 }
370 if (!isStillFunctionLocal)
371 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
372 }
Devang Patelf7188322009-09-03 01:39:20 +0000373}
374
Devang Patel05a26fb2009-07-29 00:33:07 +0000375//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000376// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000377//
Devang Patel943ddf62010-01-12 18:34:06 +0000378
Dan Gohman846b9e12010-07-21 18:01:42 +0000379static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
380 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000381}
382
Dan Gohman2637cc12010-07-21 23:38:33 +0000383NamedMDNode::NamedMDNode(const Twine &N)
384 : Name(N.str()), Parent(0),
385 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000386}
387
Chris Lattner1bc810b2009-12-28 08:07:14 +0000388NamedMDNode::~NamedMDNode() {
389 dropAllReferences();
390 delete &getNMDOps(Operands);
391}
392
Chris Lattner9b493022009-12-31 01:22:29 +0000393/// getNumOperands - Return number of NamedMDNode operands.
394unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000395 return (unsigned)getNMDOps(Operands).size();
396}
397
Chris Lattner9b493022009-12-31 01:22:29 +0000398/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000399MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000400 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000401 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000402}
403
Chris Lattner9b493022009-12-31 01:22:29 +0000404/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000405void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000406 assert(!M->isFunctionLocal() &&
407 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000408 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000409}
410
Devang Patel79238d72009-08-03 06:19:01 +0000411/// eraseFromParent - Drop all references and remove the node from parent
412/// module.
413void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000414 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000415}
416
417/// dropAllReferences - Remove all uses and clear node vector.
418void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000419 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000420}
421
Devang Patelfcfee0f2010-01-07 19:39:36 +0000422/// getName - Return a constant reference to this named metadata's name.
423StringRef NamedMDNode::getName() const {
424 return StringRef(Name);
425}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000426
427//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000428// Instruction Metadata method implementations.
429//
430
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000431void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000432 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000433 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000434}
435
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000436MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000437 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000438}
439
440/// setMetadata - Set the metadata of of the specified kind to the specified
441/// node. This updates/replaces metadata if already present, or removes it if
442/// Node is null.
443void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
444 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000445
Chris Lattnerc263b422010-03-30 23:03:27 +0000446 // Handle 'dbg' as a special case since it is not stored in the hash table.
447 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000448 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000449 return;
450 }
451
Chris Lattnera0566972009-12-29 09:01:33 +0000452 // Handle the case when we're adding/updating metadata on an instruction.
453 if (Node) {
454 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000455 assert(!Info.empty() == hasMetadataHashEntry() &&
456 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000457 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000458 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000459 } else {
460 // Handle replacement of an existing value.
461 for (unsigned i = 0, e = Info.size(); i != e; ++i)
462 if (Info[i].first == KindID) {
463 Info[i].second = Node;
464 return;
465 }
466 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000467
Chris Lattnera0566972009-12-29 09:01:33 +0000468 // No replacement, just add it to the list.
469 Info.push_back(std::make_pair(KindID, Node));
470 return;
471 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000472
Chris Lattnera0566972009-12-29 09:01:33 +0000473 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000474 assert((hasMetadataHashEntry() ==
475 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000476 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000477 if (!hasMetadataHashEntry())
478 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000479 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000480
Chris Lattnera0566972009-12-29 09:01:33 +0000481 // Common case is removing the only entry.
482 if (Info.size() == 1 && Info[0].first == KindID) {
483 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000484 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000485 return;
486 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000487
Chris Lattnerc263b422010-03-30 23:03:27 +0000488 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000489 for (unsigned i = 0, e = Info.size(); i != e; ++i)
490 if (Info[i].first == KindID) {
491 Info[i] = Info.back();
492 Info.pop_back();
493 assert(!Info.empty() && "Removing last entry should be handled above");
494 return;
495 }
496 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000497}
498
499MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000500 // Handle 'dbg' as a special case since it is not stored in the hash table.
501 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000502 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000503
504 if (!hasMetadataHashEntry()) return 0;
505
Chris Lattnera0566972009-12-29 09:01:33 +0000506 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000507 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000508
Chris Lattnera0566972009-12-29 09:01:33 +0000509 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
510 I != E; ++I)
511 if (I->first == KindID)
512 return I->second;
513 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000514}
515
516void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000517 MDNode*> > &Result) const {
518 Result.clear();
519
520 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000521 if (!DbgLoc.isUnknown()) {
522 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
523 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000524 if (!hasMetadataHashEntry()) return;
525 }
526
527 assert(hasMetadataHashEntry() &&
528 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000529 "Shouldn't have called this");
530 const LLVMContextImpl::MDMapTy &Info =
531 getContext().pImpl->MetadataStore.find(this)->second;
532 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000533
Chris Lattnera0566972009-12-29 09:01:33 +0000534 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000535
Chris Lattnera0566972009-12-29 09:01:33 +0000536 // Sort the resulting array so it is stable.
537 if (Result.size() > 1)
538 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000539}
540
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000541void Instruction::
542getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
543 MDNode*> > &Result) const {
544 Result.clear();
545 assert(hasMetadataHashEntry() &&
546 getContext().pImpl->MetadataStore.count(this) &&
547 "Shouldn't have called this");
548 const LLVMContextImpl::MDMapTy &Info =
549 getContext().pImpl->MetadataStore.find(this)->second;
550 assert(!Info.empty() && "Shouldn't have called this");
551
552 Result.append(Info.begin(), Info.end());
553
554 // Sort the resulting array so it is stable.
555 if (Result.size() > 1)
556 array_pod_sort(Result.begin(), Result.end());
557}
558
559
Dan Gohman48a995f2010-07-20 22:25:04 +0000560/// clearMetadataHashEntries - Clear all hashtable-based metadata from
561/// this instruction.
562void Instruction::clearMetadataHashEntries() {
563 assert(hasMetadataHashEntry() && "Caller should check");
564 getContext().pImpl->MetadataStore.erase(this);
565 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000566}
567