blob: e3a08d4949ce1eb548b2c4c1f7fd52b9615bb21d [file] [log] [blame]
Devang Patela4f43fb2009-07-28 21:49:47 +00001//===-- Metadata.cpp - Implement Metadata classes -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Metadata classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Metadata.h"
Chris Lattner1300f452009-12-28 08:24:16 +000015#include "LLVMContextImpl.h"
Owen Anderson0087fe62009-07-31 21:35:40 +000016#include "llvm/LLVMContext.h"
Devang Patel05a26fb2009-07-29 00:33:07 +000017#include "llvm/Module.h"
Devang Pateld5497a4b2009-09-16 18:09:00 +000018#include "llvm/Instruction.h"
Devang Patel1155fdf2009-10-22 19:36:54 +000019#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringMap.h"
Devang Patelf76941e2010-01-12 18:57:56 +000021#include "llvm/ADT/SmallString.h"
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000022#include "llvm/ADT/STLExtras.h"
Devang Patel18dfdc92009-07-29 17:16:17 +000023#include "SymbolTableListTraitsImpl.h"
Dan Gohman16a5d982010-08-20 22:02:26 +000024#include "llvm/Support/LeakDetector.h"
Chris Lattner1300f452009-12-28 08:24:16 +000025#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000026using namespace llvm;
27
28//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000029// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000030//
Chris Lattner5a409bd2009-12-28 08:30:43 +000031
David Blaikiea379b1812011-12-20 02:50:00 +000032void MDString::anchor() { }
33
Chris Lattner5a409bd2009-12-28 08:30:43 +000034MDString::MDString(LLVMContext &C, StringRef S)
Devang Patelac277eb2010-01-22 22:52:10 +000035 : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
Chris Lattner5a409bd2009-12-28 08:30:43 +000036
Devang Pateldcb99d32009-10-22 00:10:15 +000037MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000038 LLVMContextImpl *pImpl = Context.pImpl;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000039 StringMapEntry<MDString *> &Entry =
Owen Anderson0087fe62009-07-31 21:35:40 +000040 pImpl->MDStringCache.GetOrCreateValue(Str);
41 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000042 if (!S) S = new MDString(Context, Entry.getKey());
43 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000044}
45
46//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000047// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000048//
49
Chris Lattner9b493022009-12-31 01:22:29 +000050// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000051namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000052class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000053 MDNode *Parent;
54public:
Chris Lattner9b493022009-12-31 01:22:29 +000055 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
56 ~MDNodeOperand() {}
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000057
Chris Lattner8cb6c342009-12-31 01:05:46 +000058 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000059 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000060 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000061
Chris Lattner74a6ad62009-12-28 07:41:54 +000062 virtual void deleted();
63 virtual void allUsesReplacedWith(Value *NV);
64};
65} // end namespace llvm.
66
67
Chris Lattner9b493022009-12-31 01:22:29 +000068void MDNodeOperand::deleted() {
69 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000070}
71
Chris Lattner9b493022009-12-31 01:22:29 +000072void MDNodeOperand::allUsesReplacedWith(Value *NV) {
73 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000074}
75
76
77
78//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000079// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000080//
Chris Lattner74a6ad62009-12-28 07:41:54 +000081
Chris Lattner9b493022009-12-31 01:22:29 +000082/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000083/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000084static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
Dan Gohman1e0213a2010-07-13 19:33:27 +000085 // Use <= instead of < to permit a one-past-the-end address.
86 assert(Op <= N->getNumOperands() && "Invalid operand number");
Chris Lattner9b493022009-12-31 01:22:29 +000087 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000088}
89
Eric Christopher24e51b72012-02-15 09:09:29 +000090void MDNode::replaceOperandWith(unsigned i, Value *Val) {
91 MDNodeOperand *Op = getOperandPtr(this, i);
92 replaceOperand(Op, Val);
93}
94
Jay Foad5514afe2011-04-21 19:59:31 +000095MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
Devang Patelac277eb2010-01-22 22:52:10 +000096: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
Jay Foad5514afe2011-04-21 19:59:31 +000097 NumOperands = Vals.size();
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +000098
Victor Hernandez0471abd2009-12-18 20:09:14 +000099 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +0000100 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000101
102 // Initialize the operand list, which is co-allocated on the end of the node.
Jay Foad5514afe2011-04-21 19:59:31 +0000103 unsigned i = 0;
Chris Lattner9b493022009-12-31 01:22:29 +0000104 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Jay Foad5514afe2011-04-21 19:59:31 +0000105 Op != E; ++Op, ++i)
106 new (Op) MDNodeOperand(Vals[i], this);
Devang Patela4f43fb2009-07-28 21:49:47 +0000107}
108
Chris Lattner8cb6c342009-12-31 01:05:46 +0000109
110/// ~MDNode - Destroy MDNode.
111MDNode::~MDNode() {
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000112 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
Chris Lattner8cb6c342009-12-31 01:05:46 +0000113 "Not being destroyed through destroy()?");
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000114 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
115 if (isNotUniqued()) {
116 pImpl->NonUniquedMDNodes.erase(this);
117 } else {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000118 pImpl->MDNodeSet.RemoveNode(this);
119 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000120
Chris Lattner8cb6c342009-12-31 01:05:46 +0000121 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000122 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000123 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000124 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000125}
126
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000127static const Function *getFunctionForValue(Value *V) {
128 if (!V) return NULL;
Duncan Sandsc2928c62010-05-04 12:43:36 +0000129 if (Instruction *I = dyn_cast<Instruction>(V)) {
130 BasicBlock *BB = I->getParent();
131 return BB ? BB->getParent() : 0;
132 }
Chris Lattner5522f052010-01-21 21:01:47 +0000133 if (Argument *A = dyn_cast<Argument>(V))
134 return A->getParent();
Duncan Sandsc2928c62010-05-04 12:43:36 +0000135 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
136 return BB->getParent();
137 if (MDNode *MD = dyn_cast<MDNode>(V))
138 return MD->getFunction();
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000139 return NULL;
140}
141
Victor Hernandez8296da72010-01-14 20:12:34 +0000142#ifndef NDEBUG
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000143static const Function *assertLocalFunction(const MDNode *N) {
Chris Lattner5522f052010-01-21 21:01:47 +0000144 if (!N->isFunctionLocal()) return 0;
Victor Hernandez8296da72010-01-14 20:12:34 +0000145
Devang Patelb36df172010-07-06 21:05:17 +0000146 // FIXME: This does not handle cyclic function local metadata.
Chris Lattner5522f052010-01-21 21:01:47 +0000147 const Function *F = 0, *NewF = 0;
Victor Hernandez8c85e252010-01-14 01:45:14 +0000148 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000149 if (Value *V = N->getOperand(i)) {
Chris Lattner5522f052010-01-21 21:01:47 +0000150 if (MDNode *MD = dyn_cast<MDNode>(V))
151 NewF = assertLocalFunction(MD);
152 else
153 NewF = getFunctionForValue(V);
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000154 }
Chris Lattner5522f052010-01-21 21:01:47 +0000155 if (F == 0)
156 F = NewF;
157 else
158 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
Victor Hernandez8c85e252010-01-14 01:45:14 +0000159 }
Victor Hernandez8c85e252010-01-14 01:45:14 +0000160 return F;
161}
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000162#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000163
164// getFunction - If this metadata is function-local and recursively has a
165// function-local operand, return the first such operand's parent function.
Victor Hernandez06828f02010-01-18 22:55:08 +0000166// Otherwise, return null. getFunction() should not be used for performance-
167// critical code because it recursively visits all the MDNode's operands.
Victor Hernandeze5f2af72010-01-20 04:45:57 +0000168const Function *MDNode::getFunction() const {
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000169#ifndef NDEBUG
170 return assertLocalFunction(this);
David Blaikie46a9f012012-01-20 21:51:11 +0000171#else
Victor Hernandez8c85e252010-01-14 01:45:14 +0000172 if (!isFunctionLocal()) return NULL;
Duncan Sands8815f382010-05-04 14:25:42 +0000173 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
174 if (const Function *F = getFunctionForValue(getOperand(i)))
175 return F;
Victor Hernandezfdf27a62010-01-18 20:36:54 +0000176 return NULL;
David Blaikie46a9f012012-01-20 21:51:11 +0000177#endif
Victor Hernandez8c85e252010-01-14 01:45:14 +0000178}
179
Chris Lattner8cb6c342009-12-31 01:05:46 +0000180// destroy - Delete this node. Only when there are no uses.
181void MDNode::destroy() {
182 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
183 // Placement delete, the free the memory.
184 this->~MDNode();
185 free(this);
186}
187
Chris Lattner450e29c2010-04-28 20:16:12 +0000188/// isFunctionLocalValue - Return true if this is a value that would require a
189/// function-local MDNode.
190static bool isFunctionLocalValue(Value *V) {
191 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
192 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
193}
194
Jay Foad5514afe2011-04-21 19:59:31 +0000195MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
196 FunctionLocalness FL, bool Insert) {
Devang Patelf7188322009-09-03 01:39:20 +0000197 LLVMContextImpl *pImpl = Context.pImpl;
Dan Gohman01579b22010-08-24 23:21:12 +0000198
199 // Add all the operand pointers. Note that we don't have to add the
200 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000201 // Note that if the operands are later nulled out, the node will be
202 // removed from the uniquing map.
Dan Gohman01579b22010-08-24 23:21:12 +0000203 FoldingSetNodeID ID;
Jay Foad5514afe2011-04-21 19:59:31 +0000204 for (unsigned i = 0; i != Vals.size(); ++i)
Dan Gohman01579b22010-08-24 23:21:12 +0000205 ID.AddPointer(Vals[i]);
206
207 void *InsertPoint;
Duncan Sands26a80f32012-03-31 08:20:11 +0000208 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
209
210 if (N || !Insert)
Dan Gohman01579b22010-08-24 23:21:12 +0000211 return N;
Duncan Sands26a80f32012-03-31 08:20:11 +0000212
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000213 bool isFunctionLocal = false;
214 switch (FL) {
215 case FL_Unknown:
Jay Foad5514afe2011-04-21 19:59:31 +0000216 for (unsigned i = 0; i != Vals.size(); ++i) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000217 Value *V = Vals[i];
218 if (!V) continue;
Chris Lattner450e29c2010-04-28 20:16:12 +0000219 if (isFunctionLocalValue(V)) {
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000220 isFunctionLocal = true;
221 break;
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000222 }
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000223 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000224 break;
225 case FL_No:
226 isFunctionLocal = false;
227 break;
228 case FL_Yes:
229 isFunctionLocal = true;
230 break;
Devang Patelf7188322009-09-03 01:39:20 +0000231 }
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000232
233 // Coallocate space for the node and Operands together, then placement new.
Jay Foad5514afe2011-04-21 19:59:31 +0000234 void *Ptr = malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
235 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000236
237 // InsertPoint will have been set by the FindNodeOrInsertPos call.
238 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
239
Devang Patelf7188322009-09-03 01:39:20 +0000240 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000241}
242
Devang Patel213264c2011-03-04 01:20:33 +0000243MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
Jay Foad5514afe2011-04-21 19:59:31 +0000244 return getMDNode(Context, Vals, FL_Unknown);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000245}
246
Jay Foad5514afe2011-04-21 19:59:31 +0000247MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
248 ArrayRef<Value*> Vals,
249 bool isFunctionLocal) {
250 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
Victor Hernandezb8fd1522010-01-10 07:14:18 +0000251}
252
Jay Foad5514afe2011-04-21 19:59:31 +0000253MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
254 return getMDNode(Context, Vals, FL_Unknown, false);
Victor Hernandez7e8ce9a2010-01-26 02:36:35 +0000255}
256
Jay Foad5514afe2011-04-21 19:59:31 +0000257MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
258 MDNode *N =
259 (MDNode *)malloc(sizeof(MDNode)+Vals.size()*sizeof(MDNodeOperand));
260 N = new (N) MDNode(Context, Vals, FL_No);
Dan Gohman16a5d982010-08-20 22:02:26 +0000261 N->setValueSubclassData(N->getSubclassDataFromValue() |
262 NotUniquedBit);
263 LeakDetector::addGarbageObject(N);
264 return N;
265}
266
267void MDNode::deleteTemporary(MDNode *N) {
268 assert(N->use_empty() && "Temporary MDNode has uses!");
Dan Gohman573869f2010-08-21 02:52:29 +0000269 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
Dan Gohman5d296732010-08-23 22:32:05 +0000270 "Deleting a non-temporary uniqued node!");
271 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
272 "Deleting a non-temporary non-uniqued node!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000273 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
274 "Temporary MDNode does not have NotUniquedBit set!");
275 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
Dan Gohman573869f2010-08-21 02:52:29 +0000276 "Temporary MDNode has DestroyFlag set!");
Dan Gohman16a5d982010-08-20 22:02:26 +0000277 LeakDetector::removeGarbageObject(N);
Benjamin Kramer1f3b0c02010-08-21 15:07:23 +0000278 N->destroy();
Dan Gohman16a5d982010-08-20 22:02:26 +0000279}
280
Chris Lattner9b493022009-12-31 01:22:29 +0000281/// getOperand - Return specified operand.
282Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000283 return *getOperandPtr(const_cast<MDNode*>(this), i);
284}
285
Chris Lattnerf543eff2009-12-28 09:12:35 +0000286void MDNode::Profile(FoldingSetNodeID &ID) const {
Dan Gohman01579b22010-08-24 23:21:12 +0000287 // Add all the operand pointers. Note that we don't have to add the
288 // isFunctionLocal bit because that's implied by the operands.
Dan Gohman62ddc152010-08-30 21:18:41 +0000289 // Note that if the operands are later nulled out, the node will be
290 // removed from the uniquing map.
Chris Lattner9b493022009-12-31 01:22:29 +0000291 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
292 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000293}
Devang Patel5c310be2009-08-11 18:01:24 +0000294
Jeffrey Yasskin2cc24762010-03-13 01:26:15 +0000295void MDNode::setIsNotUniqued() {
296 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
297 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
298 pImpl->NonUniquedMDNodes.insert(this);
Devang Patel82ab3f82010-02-18 20:53:16 +0000299}
Chris Lattnerf543eff2009-12-28 09:12:35 +0000300
Chris Lattner9b493022009-12-31 01:22:29 +0000301// Replace value from this node's operand list.
302void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000303 Value *From = *Op;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000304
Chris Lattner450e29c2010-04-28 20:16:12 +0000305 // If is possible that someone did GV->RAUW(inst), replacing a global variable
306 // with an instruction or some other function-local object. If this is a
307 // non-function-local MDNode, it can't point to a function-local object.
308 // Handle this case by implicitly dropping the MDNode reference to null.
Duncan Sandsc2928c62010-05-04 12:43:36 +0000309 // Likewise if the MDNode is function-local but for a different function.
310 if (To && isFunctionLocalValue(To)) {
311 if (!isFunctionLocal())
312 To = 0;
313 else {
314 const Function *F = getFunction();
315 const Function *FV = getFunctionForValue(To);
316 // Metadata can be function-local without having an associated function.
317 // So only consider functions to have changed if non-null.
318 if (F && FV && F != FV)
319 To = 0;
320 }
321 }
Chris Lattner450e29c2010-04-28 20:16:12 +0000322
Chris Lattner95c445d2009-12-28 09:32:10 +0000323 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000324 return;
Devang Patelf7188322009-09-03 01:39:20 +0000325
Chris Lattner30ae06b2009-12-30 21:42:11 +0000326 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000327 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000328
329 // If this node is already not being uniqued (because one of the operands
330 // already went to null), then there is nothing else to do here.
331 if (isNotUniqued()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000332
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000333 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
334
335 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
336 // this node to remove it, so we don't care what state the operands are in.
337 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000338
Chris Lattner30ae06b2009-12-30 21:42:11 +0000339 // If we are dropping an argument to null, we choose to not unique the MDNode
340 // anymore. This commonly occurs during destruction, and uniquing these
Dan Gohman62ddc152010-08-30 21:18:41 +0000341 // brings little reuse. Also, this means we don't need to include
342 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
Chris Lattner30ae06b2009-12-30 21:42:11 +0000343 if (To == 0) {
344 setIsNotUniqued();
345 return;
346 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000347
Chris Lattner30ae06b2009-12-30 21:42:11 +0000348 // Now that the node is out of the folding set, get ready to reinsert it.
349 // First, check to see if another node with the same operands already exists
Dan Gohman25a7bc92010-09-28 22:07:19 +0000350 // in the set. If so, then this node is redundant.
Devang Patelf7188322009-09-03 01:39:20 +0000351 FoldingSetNodeID ID;
352 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000353 void *InsertPoint;
Dan Gohman1a450c62010-09-28 21:02:55 +0000354 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
Dan Gohman25a7bc92010-09-28 22:07:19 +0000355 replaceAllUsesWith(N);
356 destroy();
357 return;
Devang Patelf7188322009-09-03 01:39:20 +0000358 }
359
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000360 // InsertPoint will have been set by the FindNodeOrInsertPos call.
361 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Dan Gohmane1328dc2010-09-14 01:37:57 +0000362
363 // If this MDValue was previously function-local but no longer is, clear
364 // its function-local flag.
365 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
366 bool isStillFunctionLocal = false;
367 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
368 Value *V = getOperand(i);
369 if (!V) continue;
370 if (isFunctionLocalValue(V)) {
371 isStillFunctionLocal = true;
372 break;
373 }
374 }
375 if (!isStillFunctionLocal)
376 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
377 }
Devang Patelf7188322009-09-03 01:39:20 +0000378}
379
Devang Patel05a26fb2009-07-29 00:33:07 +0000380//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000381// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000382//
Devang Patel943ddf62010-01-12 18:34:06 +0000383
Dan Gohman846b9e12010-07-21 18:01:42 +0000384static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
385 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000386}
387
Dan Gohman2637cc12010-07-21 23:38:33 +0000388NamedMDNode::NamedMDNode(const Twine &N)
389 : Name(N.str()), Parent(0),
390 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
Devang Patel5c310be2009-08-11 18:01:24 +0000391}
392
Chris Lattner1bc810b2009-12-28 08:07:14 +0000393NamedMDNode::~NamedMDNode() {
394 dropAllReferences();
395 delete &getNMDOps(Operands);
396}
397
Chris Lattner9b493022009-12-31 01:22:29 +0000398/// getNumOperands - Return number of NamedMDNode operands.
399unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000400 return (unsigned)getNMDOps(Operands).size();
401}
402
Chris Lattner9b493022009-12-31 01:22:29 +0000403/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000404MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000405 assert(i < getNumOperands() && "Invalid Operand number!");
Dan Gohman093cb792010-07-21 18:54:18 +0000406 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000407}
408
Chris Lattner9b493022009-12-31 01:22:29 +0000409/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000410void NamedMDNode::addOperand(MDNode *M) {
Dan Gohmane1328dc2010-09-14 01:37:57 +0000411 assert(!M->isFunctionLocal() &&
412 "NamedMDNode operands must not be function-local!");
Dan Gohman846b9e12010-07-21 18:01:42 +0000413 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000414}
415
Devang Patel79238d72009-08-03 06:19:01 +0000416/// eraseFromParent - Drop all references and remove the node from parent
417/// module.
418void NamedMDNode::eraseFromParent() {
Dan Gohman2637cc12010-07-21 23:38:33 +0000419 getParent()->eraseNamedMetadata(this);
Devang Patel79238d72009-08-03 06:19:01 +0000420}
421
422/// dropAllReferences - Remove all uses and clear node vector.
423void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000424 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000425}
426
Devang Patelfcfee0f2010-01-07 19:39:36 +0000427/// getName - Return a constant reference to this named metadata's name.
428StringRef NamedMDNode::getName() const {
429 return StringRef(Name);
430}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000431
432//===----------------------------------------------------------------------===//
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000433// Instruction Metadata method implementations.
434//
435
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000436void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000437 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000438 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000439}
440
Benjamin Kramerb3bd0192011-12-06 11:50:26 +0000441MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000442 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000443}
444
445/// setMetadata - Set the metadata of of the specified kind to the specified
446/// node. This updates/replaces metadata if already present, or removes it if
447/// Node is null.
448void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
449 if (Node == 0 && !hasMetadata()) return;
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000450
Chris Lattnerc263b422010-03-30 23:03:27 +0000451 // Handle 'dbg' as a special case since it is not stored in the hash table.
452 if (KindID == LLVMContext::MD_dbg) {
Chris Lattner593916d2010-04-02 20:21:22 +0000453 DbgLoc = DebugLoc::getFromDILocation(Node);
Chris Lattnerc263b422010-03-30 23:03:27 +0000454 return;
455 }
456
Chris Lattnera0566972009-12-29 09:01:33 +0000457 // Handle the case when we're adding/updating metadata on an instruction.
458 if (Node) {
459 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000460 assert(!Info.empty() == hasMetadataHashEntry() &&
461 "HasMetadata bit is wonked");
Chris Lattnera0566972009-12-29 09:01:33 +0000462 if (Info.empty()) {
Chris Lattnerc263b422010-03-30 23:03:27 +0000463 setHasMetadataHashEntry(true);
Chris Lattnera0566972009-12-29 09:01:33 +0000464 } else {
465 // Handle replacement of an existing value.
466 for (unsigned i = 0, e = Info.size(); i != e; ++i)
467 if (Info[i].first == KindID) {
468 Info[i].second = Node;
469 return;
470 }
471 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000472
Chris Lattnera0566972009-12-29 09:01:33 +0000473 // No replacement, just add it to the list.
474 Info.push_back(std::make_pair(KindID, Node));
475 return;
476 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000477
Chris Lattnera0566972009-12-29 09:01:33 +0000478 // Otherwise, we're removing metadata from an instruction.
Nick Lewycky4c131382011-12-27 01:17:40 +0000479 assert((hasMetadataHashEntry() ==
480 getContext().pImpl->MetadataStore.count(this)) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000481 "HasMetadata bit out of date!");
Nick Lewycky4c131382011-12-27 01:17:40 +0000482 if (!hasMetadataHashEntry())
483 return; // Nothing to remove!
Chris Lattnera0566972009-12-29 09:01:33 +0000484 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000485
Chris Lattnera0566972009-12-29 09:01:33 +0000486 // Common case is removing the only entry.
487 if (Info.size() == 1 && Info[0].first == KindID) {
488 getContext().pImpl->MetadataStore.erase(this);
Chris Lattnerc263b422010-03-30 23:03:27 +0000489 setHasMetadataHashEntry(false);
Chris Lattnera0566972009-12-29 09:01:33 +0000490 return;
491 }
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000492
Chris Lattnerc263b422010-03-30 23:03:27 +0000493 // Handle removal of an existing value.
Chris Lattnera0566972009-12-29 09:01:33 +0000494 for (unsigned i = 0, e = Info.size(); i != e; ++i)
495 if (Info[i].first == KindID) {
496 Info[i] = Info.back();
497 Info.pop_back();
498 assert(!Info.empty() && "Removing last entry should be handled above");
499 return;
500 }
501 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000502}
503
504MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnerc263b422010-03-30 23:03:27 +0000505 // Handle 'dbg' as a special case since it is not stored in the hash table.
506 if (KindID == LLVMContext::MD_dbg)
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000507 return DbgLoc.getAsMDNode(getContext());
Chris Lattnerc263b422010-03-30 23:03:27 +0000508
509 if (!hasMetadataHashEntry()) return 0;
510
Chris Lattnera0566972009-12-29 09:01:33 +0000511 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
Chris Lattnerc263b422010-03-30 23:03:27 +0000512 assert(!Info.empty() && "bit out of sync with hash table");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000513
Chris Lattnera0566972009-12-29 09:01:33 +0000514 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
515 I != E; ++I)
516 if (I->first == KindID)
517 return I->second;
518 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000519}
520
521void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
Chris Lattnerc263b422010-03-30 23:03:27 +0000522 MDNode*> > &Result) const {
523 Result.clear();
524
525 // Handle 'dbg' as a special case since it is not stored in the hash table.
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000526 if (!DbgLoc.isUnknown()) {
527 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
528 DbgLoc.getAsMDNode(getContext())));
Chris Lattnerc263b422010-03-30 23:03:27 +0000529 if (!hasMetadataHashEntry()) return;
530 }
531
532 assert(hasMetadataHashEntry() &&
533 getContext().pImpl->MetadataStore.count(this) &&
Chris Lattnera0566972009-12-29 09:01:33 +0000534 "Shouldn't have called this");
535 const LLVMContextImpl::MDMapTy &Info =
536 getContext().pImpl->MetadataStore.find(this)->second;
537 assert(!Info.empty() && "Shouldn't have called this");
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000538
Chris Lattnera0566972009-12-29 09:01:33 +0000539 Result.append(Info.begin(), Info.end());
Mikhail Glushenkoved3bd132010-01-10 18:48:49 +0000540
Chris Lattnera0566972009-12-29 09:01:33 +0000541 // Sort the resulting array so it is stable.
542 if (Result.size() > 1)
543 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000544}
545
Chris Lattnerc0f5ce32010-04-01 05:23:13 +0000546void Instruction::
547getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
548 MDNode*> > &Result) const {
549 Result.clear();
550 assert(hasMetadataHashEntry() &&
551 getContext().pImpl->MetadataStore.count(this) &&
552 "Shouldn't have called this");
553 const LLVMContextImpl::MDMapTy &Info =
554 getContext().pImpl->MetadataStore.find(this)->second;
555 assert(!Info.empty() && "Shouldn't have called this");
556
557 Result.append(Info.begin(), Info.end());
558
559 // Sort the resulting array so it is stable.
560 if (Result.size() > 1)
561 array_pod_sort(Result.begin(), Result.end());
562}
563
564
Dan Gohman48a995f2010-07-20 22:25:04 +0000565/// clearMetadataHashEntries - Clear all hashtable-based metadata from
566/// this instruction.
567void Instruction::clearMetadataHashEntries() {
568 assert(hasMetadataHashEntry() && "Caller should check");
569 getContext().pImpl->MetadataStore.erase(this);
570 setHasMetadataHashEntry(false);
Chris Lattner68017802009-12-29 07:44:16 +0000571}
572