blob: ace4dc2de2715176999a51ac3a4fef51afe6044a [file] [log] [blame]
Devang Patel0a9f7b92009-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 Lattner37da0ad2009-12-28 08:24:16 +000015#include "LLVMContextImpl.h"
Owen Anderson647e3012009-07-31 21:35:40 +000016#include "llvm/LLVMContext.h"
Devang Patelf457d132009-07-29 00:33:07 +000017#include "llvm/Module.h"
Devang Patel937b1e92009-09-16 18:09:00 +000018#include "llvm/Instruction.h"
Devang Patel9d89df12009-10-22 19:36:54 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
Devang Pateld77fdba2010-01-12 18:57:56 +000021#include "llvm/ADT/SmallString.h"
Chris Lattner1afcace2011-07-09 17:41:24 +000022#include "llvm/ADT/STLExtras.h"
Devang Patel28bc9d82009-07-29 17:16:17 +000023#include "SymbolTableListTraitsImpl.h"
Dan Gohman489b29b2010-08-20 22:02:26 +000024#include "llvm/Support/LeakDetector.h"
Chris Lattner37da0ad2009-12-28 08:24:16 +000025#include "llvm/Support/ValueHandle.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +000029// MDString implementation.
Owen Anderson647e3012009-07-31 21:35:40 +000030//
Chris Lattner5e9cd432009-12-28 08:30:43 +000031
32MDString::MDString(LLVMContext &C, StringRef S)
Devang Patelbc5201f2010-01-22 22:52:10 +000033 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5e9cd432009-12-28 08:30:43 +000034
Devang Patel49b63a12009-10-22 00:10:15 +000035MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson647e3012009-07-31 21:35:40 +000036 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000037 StringMapEntry<MDString *> &Entry =
Owen Anderson647e3012009-07-31 21:35:40 +000038 pImpl->MDStringCache.GetOrCreateValue(Str);
39 MDString *&S = Entry.getValue();
Nick Lewycky23531222009-11-26 22:54:26 +000040 if (!S) S = new MDString(Context, Entry.getKey());
41 return S;
Owen Anderson647e3012009-07-31 21:35:40 +000042}
43
44//===----------------------------------------------------------------------===//
Chris Lattner5d0cacd2009-12-31 01:22:29 +000045// MDNodeOperand implementation.
Chris Lattnerc5e08a92009-12-28 07:41:54 +000046//
47
Chris Lattner5d0cacd2009-12-31 01:22:29 +000048// Use CallbackVH to hold MDNode operands.
Chris Lattnerc5e08a92009-12-28 07:41:54 +000049namespace llvm {
Chris Lattner5d0cacd2009-12-31 01:22:29 +000050class MDNodeOperand : public CallbackVH {
Chris Lattnerc5e08a92009-12-28 07:41:54 +000051 MDNode *Parent;
52public:
Chris Lattner5d0cacd2009-12-31 01:22:29 +000053 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
54 ~MDNodeOperand() {}
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000055
Chris Lattnerb76359e2009-12-31 01:05:46 +000056 void set(Value *V) {
Chris Lattner1d880e52009-12-28 09:10:16 +000057 setValPtr(V);
Chris Lattner1d880e52009-12-28 09:10:16 +000058 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000059
Chris Lattnerc5e08a92009-12-28 07:41:54 +000060 virtual void deleted();
61 virtual void allUsesReplacedWith(Value *NV);
62};
63} // end namespace llvm.
64
65
Chris Lattner5d0cacd2009-12-31 01:22:29 +000066void MDNodeOperand::deleted() {
67 Parent->replaceOperand(this, 0);
Chris Lattnerc5e08a92009-12-28 07:41:54 +000068}
69
Chris Lattner5d0cacd2009-12-31 01:22:29 +000070void MDNodeOperand::allUsesReplacedWith(Value *NV) {
71 Parent->replaceOperand(this, NV);
Chris Lattnerc5e08a92009-12-28 07:41:54 +000072}
73
74
75
76//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +000077// MDNode implementation.
Devang Patel0a9f7b92009-07-28 21:49:47 +000078//
Chris Lattnerc5e08a92009-12-28 07:41:54 +000079
Chris Lattner5d0cacd2009-12-31 01:22:29 +000080/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattnerb76359e2009-12-31 01:05:46 +000081/// the end of the MDNode.
Chris Lattner5d0cacd2009-12-31 01:22:29 +000082static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohmanac809752010-07-13 19:33:27 +000083 // Use <= instead of < to permit a one-past-the-end address.
84 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner5d0cacd2009-12-31 01:22:29 +000085 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf2410182009-12-28 09:12:35 +000086}
87
Jay Foadec9186b2011-04-21 19:59:31 +000088MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelbc5201f2010-01-22 22:52:10 +000089: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foadec9186b2011-04-21 19:59:31 +000090 NumOperands = Vals.size();
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +000091
Victor Hernandez5d301622009-12-18 20:09:14 +000092 if (isFunctionLocal)
Chris Lattnercafe9bb2009-12-29 02:14:09 +000093 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattnerb76359e2009-12-31 01:05:46 +000094
95 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foadec9186b2011-04-21 19:59:31 +000096 unsigned i = 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +000097 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Jay Foadec9186b2011-04-21 19:59:31 +000098 Op != E; ++Op, ++i)
99 new (Op) MDNodeOperand(Vals[i], this);
Devang Patel0a9f7b92009-07-28 21:49:47 +0000100}
101
Chris Lattnerb76359e2009-12-31 01:05:46 +0000102
103/// ~MDNode - Destroy MDNode.
104MDNode::~MDNode() {
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000105 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattnerb76359e2009-12-31 01:05:46 +0000106 "Not being destroyed through destroy()?");
Jeffrey Yasskin6f555ca2010-03-13 01:26:15 +0000107 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
108 if (isNotUniqued()) {
109 pImpl->NonUniquedMDNodes.erase(this);
110 } else {
Chris Lattnerb76359e2009-12-31 01:05:46 +0000111 pImpl->MDNodeSet.RemoveNode(this);
112 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000113
Chris Lattnerb76359e2009-12-31 01:05:46 +0000114 // Destroy the operands.
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000115 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattnerb76359e2009-12-31 01:05:46 +0000116 Op != E; ++Op)
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000117 Op->~MDNodeOperand();
Chris Lattnerb76359e2009-12-31 01:05:46 +0000118}
119
Victor Hernandez8fffff52010-01-20 04:45:57 +0000120static const Function *getFunctionForValue(Value *V) {
121 if (!V) return NULL;
Duncan Sands203f7cb2010-05-04 12:43:36 +0000122 if (Instruction *I = dyn_cast<Instruction>(V)) {
123 BasicBlock *BB = I->getParent();
124 return BB ? BB->getParent() : 0;
125 }
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000126 if (Argument *A = dyn_cast<Argument>(V))
127 return A->getParent();
Duncan Sands203f7cb2010-05-04 12:43:36 +0000128 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
129 return BB->getParent();
130 if (MDNode *MD = dyn_cast<MDNode>(V))
131 return MD->getFunction();
Victor Hernandez8fffff52010-01-20 04:45:57 +0000132 return NULL;
133}
134
Victor Hernandeze05f66e2010-01-14 20:12:34 +0000135#ifndef NDEBUG
Victor Hernandez8fffff52010-01-20 04:45:57 +0000136static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000137 if (!N->isFunctionLocal()) return 0;
Victor Hernandeze05f66e2010-01-14 20:12:34 +0000138
Devang Patel6db23892010-07-06 21:05:17 +0000139 // FIXME: This does not handle cyclic function local metadata.
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000140 const Function *F = 0, *NewF = 0;
Victor Hernandezc7650b42010-01-14 01:45:14 +0000141 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandez8fffff52010-01-20 04:45:57 +0000142 if (Value *V = N->getOperand(i)) {
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000143 if (MDNode *MD = dyn_cast<MDNode>(V))
144 NewF = assertLocalFunction(MD);
145 else
146 NewF = getFunctionForValue(V);
Victor Hernandez54630e12010-01-18 20:36:54 +0000147 }
Chris Lattnerd3a6d902010-01-21 21:01:47 +0000148 if (F == 0)
149 F = NewF;
150 else
151 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandezc7650b42010-01-14 01:45:14 +0000152 }
Victor Hernandezc7650b42010-01-14 01:45:14 +0000153 return F;
154}
Victor Hernandez54630e12010-01-18 20:36:54 +0000155#endif
Victor Hernandezc7650b42010-01-14 01:45:14 +0000156
157// getFunction - If this metadata is function-local and recursively has a
158// function-local operand, return the first such operand's parent function.
Victor Hernandez6cead782010-01-18 22:55:08 +0000159// Otherwise, return null. getFunction() should not be used for performance-
160// critical code because it recursively visits all the MDNode's operands.
Victor Hernandez8fffff52010-01-20 04:45:57 +0000161const Function *MDNode::getFunction() const {
Victor Hernandez54630e12010-01-18 20:36:54 +0000162#ifndef NDEBUG
163 return assertLocalFunction(this);
164#endif
Victor Hernandezc7650b42010-01-14 01:45:14 +0000165 if (!isFunctionLocal()) return NULL;
Duncan Sandsd29f5282010-05-04 14:25:42 +0000166 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
167 if (const Function *F = getFunctionForValue(getOperand(i)))
168 return F;
Victor Hernandez54630e12010-01-18 20:36:54 +0000169 return NULL;
Victor Hernandezc7650b42010-01-14 01:45:14 +0000170}
171
Chris Lattnerb76359e2009-12-31 01:05:46 +0000172// destroy - Delete this node. Only when there are no uses.
173void MDNode::destroy() {
174 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
175 // Placement delete, the free the memory.
176 this->~MDNode();
177 free(this);
178}
179
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000180/// isFunctionLocalValue - Return true if this is a value that would require a
181/// function-local MDNode.
182static bool isFunctionLocalValue(Value *V) {
183 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
184 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
185}
186
Jay Foadec9186b2011-04-21 19:59:31 +0000187MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
188 FunctionLocalness FL, bool Insert) {
Devang Patel5f4ac842009-09-03 01:39:20 +0000189 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman7548fb32010-08-24 23:21:12 +0000190
191 // Add all the operand pointers. Note that we don't have to add the
192 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman56b092e2010-08-30 21:18:41 +0000193 // Note that if the operands are later nulled out, the node will be
194 // removed from the uniquing map.
Dan Gohman7548fb32010-08-24 23:21:12 +0000195 FoldingSetNodeID ID;
Jay Foadec9186b2011-04-21 19:59:31 +0000196 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman7548fb32010-08-24 23:21:12 +0000197 ID.AddPointer(Vals[i]);
198
199 void *InsertPoint;
200 MDNode *N = NULL;
201
202 if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
203 return N;
204
Victor Hernandeze685f232010-01-26 02:36:35 +0000205 bool isFunctionLocal = false;
206 switch (FL) {
207 case FL_Unknown:
Jay Foadec9186b2011-04-21 19:59:31 +0000208 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandeze685f232010-01-26 02:36:35 +0000209 Value *V = Vals[i];
210 if (!V) continue;
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000211 if (isFunctionLocalValue(V)) {
Victor Hernandeze685f232010-01-26 02:36:35 +0000212 isFunctionLocal = true;
213 break;
Victor Hernandez24e64df2010-01-10 07:14:18 +0000214 }
Victor Hernandez24e64df2010-01-10 07:14:18 +0000215 }
Victor Hernandeze685f232010-01-26 02:36:35 +0000216 break;
217 case FL_No:
218 isFunctionLocal = false;
219 break;
220 case FL_Yes:
221 isFunctionLocal = true;
222 break;
Devang Patel5f4ac842009-09-03 01:39:20 +0000223 }
Victor Hernandeze685f232010-01-26 02:36:35 +0000224
225 // Coallocate space for the node and Operands together, then placement new.
Jay Foadec9186b2011-04-21 19:59:31 +0000226 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
227 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandeze685f232010-01-26 02:36:35 +0000228
229 // InsertPoint will have been set by the FindNodeOrInsertPos call.
230 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
231
Devang Patel5f4ac842009-09-03 01:39:20 +0000232 return N;
Owen Anderson647e3012009-07-31 21:35:40 +0000233}
234
Devang Patel566bd122011-03-04 01:20:33 +0000235MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foadec9186b2011-04-21 19:59:31 +0000236 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandez24e64df2010-01-10 07:14:18 +0000237}
238
Jay Foadec9186b2011-04-21 19:59:31 +0000239MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
240 ArrayRef<Value*> Vals,
241 bool isFunctionLocal) {
242 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandez24e64df2010-01-10 07:14:18 +0000243}
244
Jay Foadec9186b2011-04-21 19:59:31 +0000245MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
246 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandeze685f232010-01-26 02:36:35 +0000247}
248
Jay Foadec9186b2011-04-21 19:59:31 +0000249MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
250 MDNode *N =
251 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
252 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman489b29b2010-08-20 22:02:26 +0000253 N->setValueSubclassData(N->getSubclassDataFromValue() |
254 NotUniquedBit);
255 LeakDetector::addGarbageObject(N);
256 return N;
257}
258
259void MDNode::deleteTemporary(MDNode *N) {
260 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman990bdd52010-08-21 02:52:29 +0000261 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman97cfa7d2010-08-23 22:32:05 +0000262 "Deleting a non-temporary uniqued node!");
263 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
264 "Deleting a non-temporary non-uniqued node!");
Dan Gohman489b29b2010-08-20 22:02:26 +0000265 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
266 "Temporary MDNode does not have NotUniquedBit set!");
267 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman990bdd52010-08-21 02:52:29 +0000268 "Temporary MDNode has DestroyFlag set!");
Dan Gohman489b29b2010-08-20 22:02:26 +0000269 LeakDetector::removeGarbageObject(N);
Benjamin Kramer9dd3e922010-08-21 15:07:23 +0000270 N->destroy();
Dan Gohman489b29b2010-08-20 22:02:26 +0000271}
272
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000273/// getOperand - Return specified operand.
274Value *MDNode::getOperand(unsigned i) const {
Chris Lattnerb76359e2009-12-31 01:05:46 +0000275 return *getOperandPtr(const_cast<MDNode*>(this), i);
276}
277
Chris Lattnerf2410182009-12-28 09:12:35 +0000278void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman7548fb32010-08-24 23:21:12 +0000279 // Add all the operand pointers. Note that we don't have to add the
280 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman56b092e2010-08-30 21:18:41 +0000281 // Note that if the operands are later nulled out, the node will be
282 // removed from the uniquing map.
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000283 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
284 ID.AddPointer(getOperand(i));
Devang Patel4771e162009-08-03 22:51:10 +0000285}
Devang Patelab67e702009-08-11 18:01:24 +0000286
Jeffrey Yasskin6f555ca2010-03-13 01:26:15 +0000287void MDNode::setIsNotUniqued() {
288 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
289 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
290 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel06fdacc2010-02-18 20:53:16 +0000291}
Chris Lattnerf2410182009-12-28 09:12:35 +0000292
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000293// Replace value from this node's operand list.
294void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattnerdf583892009-12-28 09:32:10 +0000295 Value *From = *Op;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000296
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000297 // If is possible that someone did GV->RAUW(inst), replacing a global variable
298 // with an instruction or some other function-local object. If this is a
299 // non-function-local MDNode, it can't point to a function-local object.
300 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sands203f7cb2010-05-04 12:43:36 +0000301 // Likewise if the MDNode is function-local but for a different function.
302 if (To && isFunctionLocalValue(To)) {
303 if (!isFunctionLocal())
304 To = 0;
305 else {
306 const Function *F = getFunction();
307 const Function *FV = getFunctionForValue(To);
308 // Metadata can be function-local without having an associated function.
309 // So only consider functions to have changed if non-null.
310 if (F && FV && F != FV)
311 To = 0;
312 }
313 }
Chris Lattnerf3f325b2010-04-28 20:16:12 +0000314
Chris Lattnerdf583892009-12-28 09:32:10 +0000315 if (From == To)
Devang Patel5f4ac842009-09-03 01:39:20 +0000316 return;
Devang Patel5f4ac842009-09-03 01:39:20 +0000317
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000318 // Update the operand.
Chris Lattnerb76359e2009-12-31 01:05:46 +0000319 Op->set(To);
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000320
321 // If this node is already not being uniqued (because one of the operands
322 // already went to null), then there is nothing else to do here.
323 if (isNotUniqued()) return;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000324
Chris Lattner49642572009-12-28 09:24:53 +0000325 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
326
327 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
328 // this node to remove it, so we don't care what state the operands are in.
329 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattnerdf583892009-12-28 09:32:10 +0000330
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000331 // If we are dropping an argument to null, we choose to not unique the MDNode
332 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman56b092e2010-08-30 21:18:41 +0000333 // brings little reuse. Also, this means we don't need to include
334 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000335 if (To == 0) {
336 setIsNotUniqued();
337 return;
338 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000339
Chris Lattner54a1f9f2009-12-30 21:42:11 +0000340 // Now that the node is out of the folding set, get ready to reinsert it.
341 // First, check to see if another node with the same operands already exists
Dan Gohmanf53458f2010-09-28 22:07:19 +0000342 // in the set. If so, then this node is redundant.
Devang Patel5f4ac842009-09-03 01:39:20 +0000343 FoldingSetNodeID ID;
344 Profile(ID);
Devang Patel5f4ac842009-09-03 01:39:20 +0000345 void *InsertPoint;
Dan Gohmanf375ebe2010-09-28 21:02:55 +0000346 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohmanf53458f2010-09-28 22:07:19 +0000347 replaceAllUsesWith(N);
348 destroy();
349 return;
Devang Patel5f4ac842009-09-03 01:39:20 +0000350 }
351
Chris Lattner49642572009-12-28 09:24:53 +0000352 // InsertPoint will have been set by the FindNodeOrInsertPos call.
353 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohman5781f3e2010-09-14 01:37:57 +0000354
355 // If this MDValue was previously function-local but no longer is, clear
356 // its function-local flag.
357 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
358 bool isStillFunctionLocal = false;
359 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
360 Value *V = getOperand(i);
361 if (!V) continue;
362 if (isFunctionLocalValue(V)) {
363 isStillFunctionLocal = true;
364 break;
365 }
366 }
367 if (!isStillFunctionLocal)
368 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
369 }
Devang Patel5f4ac842009-09-03 01:39:20 +0000370}
371
Devang Patelf457d132009-07-29 00:33:07 +0000372//===----------------------------------------------------------------------===//
Chris Lattnerb2a33b462009-10-19 07:10:59 +0000373// NamedMDNode implementation.
Devang Patelf457d132009-07-29 00:33:07 +0000374//
Devang Patel26028f22010-01-12 18:34:06 +0000375
Dan Gohmand9c86dc2010-07-21 18:01:42 +0000376static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
377 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner57109692009-12-28 08:07:14 +0000378}
379
Dan Gohman17aa92c2010-07-21 23:38:33 +0000380NamedMDNode::NamedMDNode(const Twine &N)
381 : Name(N.str()), Parent(0),
382 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patelab67e702009-08-11 18:01:24 +0000383}
384
Chris Lattner57109692009-12-28 08:07:14 +0000385NamedMDNode::~NamedMDNode() {
386 dropAllReferences();
387 delete &getNMDOps(Operands);
388}
389
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000390/// getNumOperands - Return number of NamedMDNode operands.
391unsigned NamedMDNode::getNumOperands() const {
Chris Lattner57109692009-12-28 08:07:14 +0000392 return (unsigned)getNMDOps(Operands).size();
393}
394
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000395/// getOperand - Return specified operand.
Devang Patel3e30c2a2010-01-05 20:41:31 +0000396MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000397 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman872814a2010-07-21 18:54:18 +0000398 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner57109692009-12-28 08:07:14 +0000399}
400
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000401/// addOperand - Add metadata Operand.
Devang Patel3e30c2a2010-01-05 20:41:31 +0000402void NamedMDNode::addOperand(MDNode *M) {
Dan Gohman5781f3e2010-09-14 01:37:57 +0000403 assert(!M->isFunctionLocal() &&
404 "NamedMDNode operands must not be function-local!");
Dan Gohmand9c86dc2010-07-21 18:01:42 +0000405 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner57109692009-12-28 08:07:14 +0000406}
407
Devang Patela82f8832009-08-03 06:19:01 +0000408/// eraseFromParent - Drop all references and remove the node from parent
409/// module.
410void NamedMDNode::eraseFromParent() {
Dan Gohman17aa92c2010-07-21 23:38:33 +0000411 getParent()->eraseNamedMetadata(this);
Devang Patela82f8832009-08-03 06:19:01 +0000412}
413
414/// dropAllReferences - Remove all uses and clear node vector.
415void NamedMDNode::dropAllReferences() {
Chris Lattner57109692009-12-28 08:07:14 +0000416 getNMDOps(Operands).clear();
Devang Patela82f8832009-08-03 06:19:01 +0000417}
418
Devang Patel0386f012010-01-07 19:39:36 +0000419/// getName - Return a constant reference to this named metadata's name.
420StringRef NamedMDNode::getName() const {
421 return StringRef(Name);
422}
Devang Patel937b1e92009-09-16 18:09:00 +0000423
424//===----------------------------------------------------------------------===//
Chris Lattner3990b122009-12-28 23:41:32 +0000425// Instruction Metadata method implementations.
426//
427
428void Instruction::setMetadata(const char *Kind, MDNode *Node) {
429 if (Node == 0 && !hasMetadata()) return;
Chris Lattner08113472009-12-29 09:01:33 +0000430 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner3990b122009-12-28 23:41:32 +0000431}
432
433MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattner08113472009-12-29 09:01:33 +0000434 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner3990b122009-12-28 23:41:32 +0000435}
436
437/// setMetadata - Set the metadata of of the specified kind to the specified
438/// node. This updates/replaces metadata if already present, or removes it if
439/// Node is null.
440void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
441 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000442
Chris Lattnerec39f092010-03-30 23:03:27 +0000443 // Handle 'dbg' as a special case since it is not stored in the hash table.
444 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner84e679b2010-04-02 20:21:22 +0000445 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerec39f092010-03-30 23:03:27 +0000446 return;
447 }
448
Chris Lattner08113472009-12-29 09:01:33 +0000449 // Handle the case when we're adding/updating metadata on an instruction.
450 if (Node) {
451 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerec39f092010-03-30 23:03:27 +0000452 assert(!Info.empty() == hasMetadataHashEntry() &&
453 "HasMetadata bit is wonked");
Chris Lattner08113472009-12-29 09:01:33 +0000454 if (Info.empty()) {
Chris Lattnerec39f092010-03-30 23:03:27 +0000455 setHasMetadataHashEntry(true);
Chris Lattner08113472009-12-29 09:01:33 +0000456 } else {
457 // Handle replacement of an existing value.
458 for (unsigned i = 0, e = Info.size(); i != e; ++i)
459 if (Info[i].first == KindID) {
460 Info[i].second = Node;
461 return;
462 }
463 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000464
Chris Lattner08113472009-12-29 09:01:33 +0000465 // No replacement, just add it to the list.
466 Info.push_back(std::make_pair(KindID, Node));
467 return;
468 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000469
Chris Lattner08113472009-12-29 09:01:33 +0000470 // Otherwise, we're removing metadata from an instruction.
Chris Lattnerec39f092010-03-30 23:03:27 +0000471 assert(hasMetadataHashEntry() &&
472 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattner08113472009-12-29 09:01:33 +0000473 "HasMetadata bit out of date!");
474 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000475
Chris Lattner08113472009-12-29 09:01:33 +0000476 // Common case is removing the only entry.
477 if (Info.size() == 1 && Info[0].first == KindID) {
478 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerec39f092010-03-30 23:03:27 +0000479 setHasMetadataHashEntry(false);
Chris Lattner08113472009-12-29 09:01:33 +0000480 return;
481 }
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000482
Chris Lattnerec39f092010-03-30 23:03:27 +0000483 // Handle removal of an existing value.
Chris Lattner08113472009-12-29 09:01:33 +0000484 for (unsigned i = 0, e = Info.size(); i != e; ++i)
485 if (Info[i].first == KindID) {
486 Info[i] = Info.back();
487 Info.pop_back();
488 assert(!Info.empty() && "Removing last entry should be handled above");
489 return;
490 }
491 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner3990b122009-12-28 23:41:32 +0000492}
493
494MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerec39f092010-03-30 23:03:27 +0000495 // Handle 'dbg' as a special case since it is not stored in the hash table.
496 if (KindID == LLVMContext::MD_dbg)
Chris Lattner61336ae2010-04-01 05:23:13 +0000497 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerec39f092010-03-30 23:03:27 +0000498
499 if (!hasMetadataHashEntry()) return 0;
500
Chris Lattner08113472009-12-29 09:01:33 +0000501 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerec39f092010-03-30 23:03:27 +0000502 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000503
Chris Lattner08113472009-12-29 09:01:33 +0000504 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
505 I != E; ++I)
506 if (I->first == KindID)
507 return I->second;
508 return 0;
Chris Lattner3990b122009-12-28 23:41:32 +0000509}
510
511void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerec39f092010-03-30 23:03:27 +0000512 MDNode*> > &Result) const {
513 Result.clear();
514
515 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattner61336ae2010-04-01 05:23:13 +0000516 if (!DbgLoc.isUnknown()) {
517 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
518 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerec39f092010-03-30 23:03:27 +0000519 if (!hasMetadataHashEntry()) return;
520 }
521
522 assert(hasMetadataHashEntry() &&
523 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattner08113472009-12-29 09:01:33 +0000524 "Shouldn't have called this");
525 const LLVMContextImpl::MDMapTy &Info =
526 getContext().pImpl->MetadataStore.find(this)->second;
527 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000528
Chris Lattner08113472009-12-29 09:01:33 +0000529 Result.append(Info.begin(), Info.end());
Mikhail Glushenkov0d75d872010-01-10 18:48:49 +0000530
Chris Lattner08113472009-12-29 09:01:33 +0000531 // Sort the resulting array so it is stable.
532 if (Result.size() > 1)
533 array_pod_sort(Result.begin(), Result.end());
Chris Lattner3990b122009-12-28 23:41:32 +0000534}
535
Chris Lattner61336ae2010-04-01 05:23:13 +0000536void Instruction::
537getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
538 MDNode*> > &Result) const {
539 Result.clear();
540 assert(hasMetadataHashEntry() &&
541 getContext().pImpl->MetadataStore.count(this) &&
542 "Shouldn't have called this");
543 const LLVMContextImpl::MDMapTy &Info =
544 getContext().pImpl->MetadataStore.find(this)->second;
545 assert(!Info.empty() && "Shouldn't have called this");
546
547 Result.append(Info.begin(), Info.end());
548
549 // Sort the resulting array so it is stable.
550 if (Result.size() > 1)
551 array_pod_sort(Result.begin(), Result.end());
552}
553
554
Dan Gohman4f1be4a2010-07-20 22:25:04 +0000555/// clearMetadataHashEntries - Clear all hashtable-based metadata from
556/// this instruction.
557void Instruction::clearMetadataHashEntries() {
558 assert(hasMetadataHashEntry() && "Caller should check");
559 getContext().pImpl->MetadataStore.erase(this);
560 setHasMetadataHashEntry(false);
Chris Lattner508b19a2009-12-29 07:44:16 +0000561}
562