blob: 090b09a4ccd7bb6d874ae146c1a46d95e894d271 [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
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000253 // Cache the operand hash.
254 N->Hash = ID.ComputeHash();
255
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000256 // InsertPoint will have been set by the FindNodeOrInsertPos call.
257 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
258
Devang Patelf7188322009-09-03 01:39:20 +0000259 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000260}
261
Devang Patel213264c2011-03-04 01:20:33 +0000262MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000263 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000264}
265
Jay Foad5514afe2011-04-21 19:59:31 +0000266MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
267 ArrayRef<Value*> Vals,
268 bool isFunctionLocal) {
269 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000270}
271
Jay Foad5514afe2011-04-21 19:59:31 +0000272MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
273 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000274}
275
Jay Foad5514afe2011-04-21 19:59:31 +0000276MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
277 MDNode *N =
278 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
279 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000280 N->setValueSubclassData(N->getSubclassDataFromValue() |
281 NotUniquedBit);
282 LeakDetector::addGarbageObject(N);
283 return N;
284}
285
286void MDNode::deleteTemporary(MDNode *N) {
287 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000288 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000289 "Deleting a non-temporary uniqued node!");
290 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
291 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000292 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
293 "Temporary MDNode does not have NotUniquedBit set!");
294 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000295 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000296 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000297 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000298}
299
Chris Lattner9b493022009-12-31 01:22:29 +0000300/// getOperand - Return specified operand.
301Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000302 return *getOperandPtr(const_cast<MDNode*>(this), i);
303}
304
Chris Lattnerf543eff2009-12-28 09:12:35 +0000305void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000306 // Add all the operand pointers. Note that we don't have to add the
307 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000308 // Note that if the operands are later nulled out, the node will be
309 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000310 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
311 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000312}
Devang Patel5c310be2009-08-11 18:01:24 +0000313
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000314void MDNode::setIsNotUniqued() {
315 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
316 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
317 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000318}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000319
Chris Lattner9b493022009-12-31 01:22:29 +0000320// Replace value from this node's operand list.
321void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000322 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000323
Chris Lattner450e29c2010-04-28 20:16:12 +0000324 // If is possible that someone did GV->RAUW(inst), replacing a global variable
325 // with an instruction or some other function-local object. If this is a
326 // non-function-local MDNode, it can't point to a function-local object.
327 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000328 // Likewise if the MDNode is function-local but for a different function.
329 if (To && isFunctionLocalValue(To)) {
330 if (!isFunctionLocal())
331 To = 0;
332 else {
333 const Function *F = getFunction();
334 const Function *FV = getFunctionForValue(To);
335 // Metadata can be function-local without having an associated function.
336 // So only consider functions to have changed if non-null.
337 if (F && FV && F != FV)
338 To = 0;
339 }
340 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000341
Chris Lattner95c445d2009-12-28 09:32:10 +0000342 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000343 return;
Devang Patelf7188322009-09-03 01:39:20 +0000344
Chris Lattner30ae06b2009-12-30 21:42:11 +0000345 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000346 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000347
348 // If this node is already not being uniqued (because one of the operands
349 // already went to null), then there is nothing else to do here.
350 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000351
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000352 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
353
354 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
355 // this node to remove it, so we don't care what state the operands are in.
356 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000357
Chris Lattner30ae06b2009-12-30 21:42:11 +0000358 // If we are dropping an argument to null, we choose to not unique the MDNode
359 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000360 // brings little reuse. Also, this means we don't need to include
361 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000362 if (To == 0) {
363 setIsNotUniqued();
364 return;
365 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000366
Chris Lattner30ae06b2009-12-30 21:42:11 +0000367 // Now that the node is out of the folding set, get ready to reinsert it.
368 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000369 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000370 FoldingSetNodeID ID;
371 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000372 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000373 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000374 replaceAllUsesWith(N);
375 destroy();
376 return;
Devang Patelf7188322009-09-03 01:39:20 +0000377 }
378
Benjamin Kramer2335a5c2012-04-11 14:06:54 +0000379 // Cache the operand hash.
380 Hash = ID.ComputeHash();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000381 // InsertPoint will have been set by the FindNodeOrInsertPos call.
382 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000383
384 // If this MDValue was previously function-local but no longer is, clear
385 // its function-local flag.
386 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
387 bool isStillFunctionLocal = false;
388 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
389 Value *V = getOperand(i);
390 if (!V) continue;
391 if (isFunctionLocalValue(V)) {
392 isStillFunctionLocal = true;
393 break;
394 }
395 }
396 if (!isStillFunctionLocal)
397 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
398 }
Devang Patelf7188322009-09-03 01:39:20 +0000399}
400
Devang Patel05a26fb2009-07-29 00:33:07 +0000401//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000402// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000403//
Devang Patel943ddf62010-01-12 18:34:06 +0000404
Dan Gohman846b9e12010-07-21 18:01:42 +0000405static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
406 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000407}
408
Dan Gohman2637cc12010-07-21 23:38:33 +0000409NamedMDNode::NamedMDNode(const Twine &N)
410 : Name(N.str()), Parent(0),
411 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000412}
413
Chris Lattner1bc810b2009-12-28 08:07:14 +0000414NamedMDNode::~NamedMDNode() {
415 dropAllReferences();
416 delete &getNMDOps(Operands);
417}
418
Chris Lattner9b493022009-12-31 01:22:29 +0000419/// getNumOperands - Return number of NamedMDNode operands.
420unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000421 return (unsigned)getNMDOps(Operands).size();
422}
423
Chris Lattner9b493022009-12-31 01:22:29 +0000424/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000425MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000426 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000427 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000428}
429
Chris Lattner9b493022009-12-31 01:22:29 +0000430/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000431void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000432 assert(!M->isFunctionLocal() &&
433 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000434 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000435}
436
Devang Patel79238d72009-08-03 06:19:01 +0000437/// eraseFromParent - Drop all references and remove the node from parent
438/// module.
439void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000440 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000441}
442
443/// dropAllReferences - Remove all uses and clear node vector.
444void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000445 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000446}
447
Devang Patelfcfee0f2010-01-07 19:39:36 +0000448/// getName - Return a constant reference to this named metadata's name.
449StringRef NamedMDNode::getName() const {
450 return StringRef(Name);
451}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000452
453//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000454// Instruction Metadata method implementations.
455//
456
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000457void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000458 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000459 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000460}
461
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000462MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000463 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000464}
465
466/// setMetadata - Set the metadata of of the specified kind to the specified
467/// node. This updates/replaces metadata if already present, or removes it if
468/// Node is null.
469void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
470 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000471
Chris Lattnerc263b422010-03-30 23:03:27 +0000472 // Handle 'dbg' as a special case since it is not stored in the hash table.
473 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000474 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000475 return;
476 }
477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // Handle the case when we're adding/updating metadata on an instruction.
479 if (Node) {
480 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000481 assert(!Info.empty() == hasMetadataHashEntry() &&
482 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000483 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000484 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000485 } else {
486 // Handle replacement of an existing value.
487 for (unsigned i = 0, e = Info.size(); i != e; ++i)
488 if (Info[i].first == KindID) {
489 Info[i].second = Node;
490 return;
491 }
492 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000493
Chris Lattnera0566972009-12-29 09:01:33 +0000494 // No replacement, just add it to the list.
495 Info.push_back(std::make_pair(KindID, Node));
496 return;
497 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000498
Chris Lattnera0566972009-12-29 09:01:33 +0000499 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000500 assert((hasMetadataHashEntry() ==
501 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000502 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000503 if (!hasMetadataHashEntry())
504 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000505 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000506
Chris Lattnera0566972009-12-29 09:01:33 +0000507 // Common case is removing the only entry.
508 if (Info.size() == 1 && Info[0].first == KindID) {
509 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000510 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000511 return;
512 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000513
Chris Lattnerc263b422010-03-30 23:03:27 +0000514 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000515 for (unsigned i = 0, e = Info.size(); i != e; ++i)
516 if (Info[i].first == KindID) {
517 Info[i] = Info.back();
518 Info.pop_back();
519 assert(!Info.empty() && "Removing last entry should be handled above");
520 return;
521 }
522 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000523}
524
525MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000526 // Handle 'dbg' as a special case since it is not stored in the hash table.
527 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000528 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000529
530 if (!hasMetadataHashEntry()) return 0;
531
Chris Lattnera0566972009-12-29 09:01:33 +0000532 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000533 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000534
Chris Lattnera0566972009-12-29 09:01:33 +0000535 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
536 I != E; ++I)
537 if (I->first == KindID)
538 return I->second;
539 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000540}
541
542void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000543 MDNode*> > &Result) const {
544 Result.clear();
545
546 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000547 if (!DbgLoc.isUnknown()) {
548 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
549 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000550 if (!hasMetadataHashEntry()) return;
551 }
552
553 assert(hasMetadataHashEntry() &&
554 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000555 "Shouldn't have called this");
556 const LLVMContextImpl::MDMapTy &Info =
557 getContext().pImpl->MetadataStore.find(this)->second;
558 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000559
Chris Lattnera0566972009-12-29 09:01:33 +0000560 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000561
Chris Lattnera0566972009-12-29 09:01:33 +0000562 // Sort the resulting array so it is stable.
563 if (Result.size() > 1)
564 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000565}
566
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000567void Instruction::
568getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
569 MDNode*> > &Result) const {
570 Result.clear();
571 assert(hasMetadataHashEntry() &&
572 getContext().pImpl->MetadataStore.count(this) &&
573 "Shouldn't have called this");
574 const LLVMContextImpl::MDMapTy &Info =
Bill Wendlingdd91e732012-04-03 10:50:09 +0000575 getContext().pImpl->MetadataStore.find(this)->second;
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000576 assert(!Info.empty() && "Shouldn't have called this");
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000577 Result.append(Info.begin(), Info.end());
Bill Wendlingdd91e732012-04-03 10:50:09 +0000578
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000579 // Sort the resulting array so it is stable.
580 if (Result.size() > 1)
581 array_pod_sort(Result.begin(), Result.end());
582}
583
Dan Gohman48a995f2010-07-20 22:25:04 +0000584/// clearMetadataHashEntries - Clear all hashtable-based metadata from
585/// this instruction.
586void Instruction::clearMetadataHashEntries() {
587 assert(hasMetadataHashEntry() && "Caller should check");
588 getContext().pImpl->MetadataStore.erase(this);
589 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000590}
591