blob: 55de0dc6515de9958cacd0ce2f74e4aac9fb0220 [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
Bill Wendlingc4c568b2012-04-10 20:12:16 +000034MDString::MDString(LLVMContext &C)
35 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
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;
Bill Wendlingc4c568b2012-04-10 20:12:16 +000039 StringMapEntry<Value*> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000040 pImpl->MDStringCache.GetOrCreateValue(Str);
Bill Wendlingc4c568b2012-04-10 20:12:16 +000041 Value *&S = Entry.getValue();
42 if (!S) S = new MDString(Context);
43 S->setValueName(&Entry);
44 return cast<MDString>(S);
Owen Anderson0087fe62009-07-31 21:35:40 +000045}
46
47//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000048// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000049//
50
Chris Lattner9b493022009-12-31 01:22:29 +000051// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000052namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000053class MDNodeOperand : public CallbackVH {
Bill Wendling5c0068f2012-04-08 10:20:49 +000054 MDNode *getParent() {
55 MDNodeOperand *Cur = this;
56
57 while (Cur->getValPtrInt() != 1)
58 --Cur;
59
60 assert(Cur->getValPtrInt() == 1 &&
61 "Couldn't find the beginning of the operand list!");
62 return reinterpret_cast<MDNode*>(Cur) - 1;
63 }
64
Chris Lattner74a6ad62009-12-28 07:41:54 +000065public:
Bill Wendling5c0068f2012-04-08 10:20:49 +000066 MDNodeOperand(Value *V) : CallbackVH(V) {}
Chris Lattner9b493022009-12-31 01:22:29 +000067 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000068
Bill Wendling5c0068f2012-04-08 10:20:49 +000069 void set(Value *V) { this->setValPtr(V); }
70
71 /// setAsFirstOperand - Accessor method to mark the operand as the first in
72 /// the list.
73 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000074
Chris Lattner74a6ad62009-12-28 07:41:54 +000075 virtual void deleted();
76 virtual void allUsesReplacedWith(Value *NV);
77};
78} // end namespace llvm.
79
80
Chris Lattner9b493022009-12-31 01:22:29 +000081void MDNodeOperand::deleted() {
Bill Wendling5c0068f2012-04-08 10:20:49 +000082 getParent()->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000083}
84
Chris Lattner9b493022009-12-31 01:22:29 +000085void MDNodeOperand::allUsesReplacedWith(Value *NV) {
Bill Wendling5c0068f2012-04-08 10:20:49 +000086 getParent()->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000087}
88
Chris Lattner74a6ad62009-12-28 07:41:54 +000089//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000090// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000091//
Chris Lattner74a6ad62009-12-28 07:41:54 +000092
Chris Lattner9b493022009-12-31 01:22:29 +000093/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000094/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000095static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000096 // Use <= instead of < to permit a one-past-the-end address.
97 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000098 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000099}
100
Eric Christopher24e51b72012-02-15 09:09:29 +0000101void MDNode::replaceOperandWith(unsigned i, Value *Val) {
102 MDNodeOperand *Op = getOperandPtr(this, i);
103 replaceOperand(Op, Val);
104}
105
Jay Foad5514afe2011-04-21 19:59:31 +0000106MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +0000107: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foad5514afe2011-04-21 19:59:31 +0000108 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000109
Victor Hernandez0471abd2009-12-18 20:09:14 +0000110 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000111 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000112
113 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +0000114 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000115 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Bill Wendling5c0068f2012-04-08 10:20:49 +0000116 Op != E; ++Op, ++i) {
117 new (Op) MDNodeOperand(Vals[i]);
118
119 // Mark the first MDNodeOperand as being the first in the list of operands.
120 if (i == 0)
121 Op->setAsFirstOperand(1);
122 }
Devang Patela4f43fb2009-07-28 21:49:47 +0000123}
124
Chris Lattner8cb6c342009-12-31 01:05:46 +0000125
126/// ~MDNode - Destroy MDNode.
127MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000128 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000129 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000130 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
131 if (isNotUniqued()) {
132 pImpl->NonUniquedMDNodes.erase(this);
133 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000134 pImpl->MDNodeSet.RemoveNode(this);
135 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000136
Chris Lattner8cb6c342009-12-31 01:05:46 +0000137 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000138 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000139 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000140 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000141}
142
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000143static const Function *getFunctionForValue(Value *V) {
144 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000145 if (Instruction *I = dyn_cast<Instruction>(V)) {
146 BasicBlock *BB = I->getParent();
147 return BB ? BB->getParent() : 0;
148 }
Chris Lattner5522f052010-01-21 21:01:47 +0000149 if (Argument *A = dyn_cast<Argument>(V))
150 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000151 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
152 return BB->getParent();
153 if (MDNode *MD = dyn_cast<MDNode>(V))
154 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000155 return NULL;
156}
157
Victor Hernandez8296da72010-01-14 20:12:34 +0000158#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000159static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000160 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000161
Devang Patelb36df172010-07-06 21:05:17 +0000162 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000163 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000164 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000165 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000166 if (MDNode *MD = dyn_cast<MDNode>(V))
167 NewF = assertLocalFunction(MD);
168 else
169 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000170 }
Chris Lattner5522f052010-01-21 21:01:47 +0000171 if (F == 0)
172 F = NewF;
173 else
174 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000175 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000176 return F;
177}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000178#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000179
180// getFunction - If this metadata is function-local and recursively has a
181// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000182// Otherwise, return null. getFunction() should not be used for performance-
183// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000184const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000185#ifndef NDEBUG
186 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000187#else
Victor Hernandez8c85e252010-01-14 01:45:14 +0000188 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000189 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
190 if (const Function *F = getFunctionForValue(getOperand(i)))
191 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000192 return NULL;
David Blaikie46a9f012012-01-20 21:51:11 +0000193#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000194}
195
Chris Lattner8cb6c342009-12-31 01:05:46 +0000196// destroy - Delete this node. Only when there are no uses.
197void MDNode::destroy() {
198 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
199 // Placement delete, the free the memory.
200 this->~MDNode();
201 free(this);
202}
203
Chris Lattner450e29c2010-04-28 20:16:12 +0000204/// isFunctionLocalValue - Return true if this is a value that would require a
205/// function-local MDNode.
206static bool isFunctionLocalValue(Value *V) {
207 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
208 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
209}
210
Jay Foad5514afe2011-04-21 19:59:31 +0000211MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
212 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000213 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000214
215 // Add all the operand pointers. Note that we don't have to add the
216 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000217 // Note that if the operands are later nulled out, the node will be
218 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000219 FoldingSetNodeID ID;
Jay Foad5514afe2011-04-21 19:59:31 +0000220 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman01579b22010-08-24 23:21:12 +0000221 ID.AddPointer(Vals[i]);
222
223 void *InsertPoint;
Duncan Sands26a80f32012-03-31 08:20:11 +0000224 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
225
226 if (N || !Insert)
Dan Gohman01579b22010-08-24 23:21:12 +0000227 return N;
Duncan Sands26a80f32012-03-31 08:20:11 +0000228
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000229 bool isFunctionLocal = false;
230 switch (FL) {
231 case FL_Unknown:
Jay Foad5514afe2011-04-21 19:59:31 +0000232 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000233 Value *V = Vals[i];
234 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000235 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000236 isFunctionLocal = true;
237 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000238 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000239 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000240 break;
241 case FL_No:
242 isFunctionLocal = false;
243 break;
244 case FL_Yes:
245 isFunctionLocal = true;
246 break;
Devang Patelf7188322009-09-03 01:39:20 +0000247 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000248
249 // Coallocate space for the node and Operands together, then placement new.
Jay Foad5514afe2011-04-21 19:59:31 +0000250 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
251 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000252
253 // InsertPoint will have been set by the FindNodeOrInsertPos call.
254 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
255
Devang Patelf7188322009-09-03 01:39:20 +0000256 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000257}
258
Devang Patel213264c2011-03-04 01:20:33 +0000259MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000260 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000261}
262
Jay Foad5514afe2011-04-21 19:59:31 +0000263MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
264 ArrayRef<Value*> Vals,
265 bool isFunctionLocal) {
266 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000267}
268
Jay Foad5514afe2011-04-21 19:59:31 +0000269MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
270 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000271}
272
Jay Foad5514afe2011-04-21 19:59:31 +0000273MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
274 MDNode *N =
275 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
276 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000277 N->setValueSubclassData(N->getSubclassDataFromValue() |
278 NotUniquedBit);
279 LeakDetector::addGarbageObject(N);
280 return N;
281}
282
283void MDNode::deleteTemporary(MDNode *N) {
284 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000285 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000286 "Deleting a non-temporary uniqued node!");
287 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
288 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000289 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
290 "Temporary MDNode does not have NotUniquedBit set!");
291 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000292 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000293 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000294 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000295}
296
Chris Lattner9b493022009-12-31 01:22:29 +0000297/// getOperand - Return specified operand.
298Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000299 return *getOperandPtr(const_cast<MDNode*>(this), i);
300}
301
Chris Lattnerf543eff2009-12-28 09:12:35 +0000302void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000303 // Add all the operand pointers. Note that we don't have to add the
304 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000305 // Note that if the operands are later nulled out, the node will be
306 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000307 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
308 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000309}
Devang Patel5c310be2009-08-11 18:01:24 +0000310
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000311void MDNode::setIsNotUniqued() {
312 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
313 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
314 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000315}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000316
Chris Lattner9b493022009-12-31 01:22:29 +0000317// Replace value from this node's operand list.
318void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000319 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000320
Chris Lattner450e29c2010-04-28 20:16:12 +0000321 // If is possible that someone did GV->RAUW(inst), replacing a global variable
322 // with an instruction or some other function-local object. If this is a
323 // non-function-local MDNode, it can't point to a function-local object.
324 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000325 // Likewise if the MDNode is function-local but for a different function.
326 if (To && isFunctionLocalValue(To)) {
327 if (!isFunctionLocal())
328 To = 0;
329 else {
330 const Function *F = getFunction();
331 const Function *FV = getFunctionForValue(To);
332 // Metadata can be function-local without having an associated function.
333 // So only consider functions to have changed if non-null.
334 if (F && FV && F != FV)
335 To = 0;
336 }
337 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000338
Chris Lattner95c445d2009-12-28 09:32:10 +0000339 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000340 return;
Devang Patelf7188322009-09-03 01:39:20 +0000341
Chris Lattner30ae06b2009-12-30 21:42:11 +0000342 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000343 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000344
345 // If this node is already not being uniqued (because one of the operands
346 // already went to null), then there is nothing else to do here.
347 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000348
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000349 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
350
351 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
352 // this node to remove it, so we don't care what state the operands are in.
353 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000354
Chris Lattner30ae06b2009-12-30 21:42:11 +0000355 // If we are dropping an argument to null, we choose to not unique the MDNode
356 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000357 // brings little reuse. Also, this means we don't need to include
358 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000359 if (To == 0) {
360 setIsNotUniqued();
361 return;
362 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000363
Chris Lattner30ae06b2009-12-30 21:42:11 +0000364 // Now that the node is out of the folding set, get ready to reinsert it.
365 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000366 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000367 FoldingSetNodeID ID;
368 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000369 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000370 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000371 replaceAllUsesWith(N);
372 destroy();
373 return;
Devang Patelf7188322009-09-03 01:39:20 +0000374 }
375
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000376 // InsertPoint will have been set by the FindNodeOrInsertPos call.
377 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000378
379 // If this MDValue was previously function-local but no longer is, clear
380 // its function-local flag.
381 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
382 bool isStillFunctionLocal = false;
383 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
384 Value *V = getOperand(i);
385 if (!V) continue;
386 if (isFunctionLocalValue(V)) {
387 isStillFunctionLocal = true;
388 break;
389 }
390 }
391 if (!isStillFunctionLocal)
392 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
393 }
Devang Patelf7188322009-09-03 01:39:20 +0000394}
395
Devang Patel05a26fb2009-07-29 00:33:07 +0000396//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000397// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000398//
Devang Patel943ddf62010-01-12 18:34:06 +0000399
Dan Gohman846b9e12010-07-21 18:01:42 +0000400static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
401 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000402}
403
Dan Gohman2637cc12010-07-21 23:38:33 +0000404NamedMDNode::NamedMDNode(const Twine &N)
405 : Name(N.str()), Parent(0),
406 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000407}
408
Chris Lattner1bc810b2009-12-28 08:07:14 +0000409NamedMDNode::~NamedMDNode() {
410 dropAllReferences();
411 delete &getNMDOps(Operands);
412}
413
Chris Lattner9b493022009-12-31 01:22:29 +0000414/// getNumOperands - Return number of NamedMDNode operands.
415unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000416 return (unsigned)getNMDOps(Operands).size();
417}
418
Chris Lattner9b493022009-12-31 01:22:29 +0000419/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000420MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000421 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000422 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000423}
424
Chris Lattner9b493022009-12-31 01:22:29 +0000425/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000426void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000427 assert(!M->isFunctionLocal() &&
428 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000429 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000430}
431
Devang Patel79238d72009-08-03 06:19:01 +0000432/// eraseFromParent - Drop all references and remove the node from parent
433/// module.
434void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000435 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000436}
437
438/// dropAllReferences - Remove all uses and clear node vector.
439void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000440 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000441}
442
Devang Patelfcfee0f2010-01-07 19:39:36 +0000443/// getName - Return a constant reference to this named metadata's name.
444StringRef NamedMDNode::getName() const {
445 return StringRef(Name);
446}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000447
448//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000449// Instruction Metadata method implementations.
450//
451
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000452void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000453 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000454 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000455}
456
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000457MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000458 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000459}
460
461/// setMetadata - Set the metadata of of the specified kind to the specified
462/// node. This updates/replaces metadata if already present, or removes it if
463/// Node is null.
464void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
465 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000466
Chris Lattnerc263b422010-03-30 23:03:27 +0000467 // Handle 'dbg' as a special case since it is not stored in the hash table.
468 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000469 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000470 return;
471 }
472
Chris Lattnera0566972009-12-29 09:01:33 +0000473 // Handle the case when we're adding/updating metadata on an instruction.
474 if (Node) {
475 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000476 assert(!Info.empty() == hasMetadataHashEntry() &&
477 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000478 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000479 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000480 } else {
481 // Handle replacement of an existing value.
482 for (unsigned i = 0, e = Info.size(); i != e; ++i)
483 if (Info[i].first == KindID) {
484 Info[i].second = Node;
485 return;
486 }
487 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000488
Chris Lattnera0566972009-12-29 09:01:33 +0000489 // No replacement, just add it to the list.
490 Info.push_back(std::make_pair(KindID, Node));
491 return;
492 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000493
Chris Lattnera0566972009-12-29 09:01:33 +0000494 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000495 assert((hasMetadataHashEntry() ==
496 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000497 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000498 if (!hasMetadataHashEntry())
499 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000500 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000501
Chris Lattnera0566972009-12-29 09:01:33 +0000502 // Common case is removing the only entry.
503 if (Info.size() == 1 && Info[0].first == KindID) {
504 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000505 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000506 return;
507 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000508
Chris Lattnerc263b422010-03-30 23:03:27 +0000509 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000510 for (unsigned i = 0, e = Info.size(); i != e; ++i)
511 if (Info[i].first == KindID) {
512 Info[i] = Info.back();
513 Info.pop_back();
514 assert(!Info.empty() && "Removing last entry should be handled above");
515 return;
516 }
517 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000518}
519
520MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000521 // Handle 'dbg' as a special case since it is not stored in the hash table.
522 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000523 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000524
525 if (!hasMetadataHashEntry()) return 0;
526
Chris Lattnera0566972009-12-29 09:01:33 +0000527 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000528 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000529
Chris Lattnera0566972009-12-29 09:01:33 +0000530 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
531 I != E; ++I)
532 if (I->first == KindID)
533 return I->second;
534 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000535}
536
537void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000538 MDNode*> > &Result) const {
539 Result.clear();
540
541 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000542 if (!DbgLoc.isUnknown()) {
543 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
544 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000545 if (!hasMetadataHashEntry()) return;
546 }
547
548 assert(hasMetadataHashEntry() &&
549 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000550 "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");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000554
Chris Lattnera0566972009-12-29 09:01:33 +0000555 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000556
Chris Lattnera0566972009-12-29 09:01:33 +0000557 // Sort the resulting array so it is stable.
558 if (Result.size() > 1)
559 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000560}
561
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000562void Instruction::
563getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
564 MDNode*> > &Result) const {
565 Result.clear();
566 assert(hasMetadataHashEntry() &&
567 getContext().pImpl->MetadataStore.count(this) &&
568 "Shouldn't have called this");
569 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000570 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000571 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000572 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000573
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000574 // Sort the resulting array so it is stable.
575 if (Result.size() > 1)
576 array_pod_sort(Result.begin(), Result.end());
577}
578
Dan Gohman48a995f2010-07-20 22:25:04 +0000579/// clearMetadataHashEntries - Clear all hashtable-based metadata from
580/// this instruction.
581void Instruction::clearMetadataHashEntries() {
582 assert(hasMetadataHashEntry() && "Caller should check");
583 getContext().pImpl->MetadataStore.erase(this);
584 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000585}
586