blob: 09cd1d5cbae29514a9df460831b4bceb177191c3 [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 Patel18dfdc92009-07-29 17:16:17 +000021#include "SymbolTableListTraitsImpl.h"
Chris Lattner1300f452009-12-28 08:24:16 +000022#include "llvm/Support/ValueHandle.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000026// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000027//
Chris Lattner5a409bd2009-12-28 08:30:43 +000028
29MDString::MDString(LLVMContext &C, StringRef S)
30 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
31
Devang Pateldcb99d32009-10-22 00:10:15 +000032MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000033 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson0087fe62009-07-31 21:35:40 +000034 StringMapEntry<MDString *> &Entry =
35 pImpl->MDStringCache.GetOrCreateValue(Str);
36 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000037 if (!S) S = new MDString(Context, Entry.getKey());
38 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000039}
40
Devang Patel862ef782009-11-12 00:50:58 +000041MDString *MDString::get(LLVMContext &Context, const char *Str) {
42 LLVMContextImpl *pImpl = Context.pImpl;
43 StringMapEntry<MDString *> &Entry =
44 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
45 MDString *&S = Entry.getValue();
Nick Lewycky6e052512009-11-27 19:57:53 +000046 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000047 return S;
Devang Patel862ef782009-11-12 00:50:58 +000048}
49
Owen Anderson0087fe62009-07-31 21:35:40 +000050//===----------------------------------------------------------------------===//
Chris Lattner9b493022009-12-31 01:22:29 +000051// MDNodeOperand implementation.
Chris Lattner74a6ad62009-12-28 07:41:54 +000052//
53
Chris Lattner9b493022009-12-31 01:22:29 +000054// Use CallbackVH to hold MDNode operands.
Chris Lattner74a6ad62009-12-28 07:41:54 +000055namespace llvm {
Chris Lattner9b493022009-12-31 01:22:29 +000056class MDNodeOperand : public CallbackVH {
Chris Lattner74a6ad62009-12-28 07:41:54 +000057 MDNode *Parent;
58public:
Chris Lattner9b493022009-12-31 01:22:29 +000059 MDNodeOperand(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
60 ~MDNodeOperand() {}
Chris Lattner74a6ad62009-12-28 07:41:54 +000061
Chris Lattner8cb6c342009-12-31 01:05:46 +000062 void set(Value *V) {
Chris Lattnerd944cf72009-12-28 09:10:16 +000063 setValPtr(V);
Chris Lattnerd944cf72009-12-28 09:10:16 +000064 }
65
Chris Lattner74a6ad62009-12-28 07:41:54 +000066 virtual void deleted();
67 virtual void allUsesReplacedWith(Value *NV);
68};
69} // end namespace llvm.
70
71
Chris Lattner9b493022009-12-31 01:22:29 +000072void MDNodeOperand::deleted() {
73 Parent->replaceOperand(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000074}
75
Chris Lattner9b493022009-12-31 01:22:29 +000076void MDNodeOperand::allUsesReplacedWith(Value *NV) {
77 Parent->replaceOperand(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000078}
79
80
81
82//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000083// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000084//
Chris Lattner74a6ad62009-12-28 07:41:54 +000085
Chris Lattner9b493022009-12-31 01:22:29 +000086/// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
Chris Lattner8cb6c342009-12-31 01:05:46 +000087/// the end of the MDNode.
Chris Lattner9b493022009-12-31 01:22:29 +000088static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
89 assert(Op < N->getNumOperands() && "Invalid operand number");
90 return reinterpret_cast<MDNodeOperand*>(N+1)+Op;
Chris Lattnerf543eff2009-12-28 09:12:35 +000091}
92
Victor Hernandezdd7418a2009-12-16 02:52:09 +000093MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000094 bool isFunctionLocal)
Chris Lattner8cb6c342009-12-31 01:05:46 +000095: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000096 NumOperands = NumVals;
Chris Lattner8cb6c342009-12-31 01:05:46 +000097
Victor Hernandez0471abd2009-12-18 20:09:14 +000098 if (isFunctionLocal)
Chris Lattnerb9c86512009-12-29 02:14:09 +000099 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000100
101 // Initialize the operand list, which is co-allocated on the end of the node.
Chris Lattner9b493022009-12-31 01:22:29 +0000102 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000103 Op != E; ++Op, ++Vals)
Chris Lattner9b493022009-12-31 01:22:29 +0000104 new (Op) MDNodeOperand(*Vals, this);
Devang Patela4f43fb2009-07-28 21:49:47 +0000105}
106
Chris Lattner8cb6c342009-12-31 01:05:46 +0000107
108/// ~MDNode - Destroy MDNode.
109MDNode::~MDNode() {
110 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
111 "Not being destroyed through destroy()?");
112 if (!isNotUniqued()) {
113 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
114 pImpl->MDNodeSet.RemoveNode(this);
115 }
116
117 // Destroy the operands.
Chris Lattner9b493022009-12-31 01:22:29 +0000118 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
Chris Lattner8cb6c342009-12-31 01:05:46 +0000119 Op != E; ++Op)
Chris Lattner9b493022009-12-31 01:22:29 +0000120 Op->~MDNodeOperand();
Chris Lattner8cb6c342009-12-31 01:05:46 +0000121}
122
123// destroy - Delete this node. Only when there are no uses.
124void MDNode::destroy() {
125 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
126 // Placement delete, the free the memory.
127 this->~MDNode();
128 free(this);
129}
130
131
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000132MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000133 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000134 LLVMContextImpl *pImpl = Context.pImpl;
135 FoldingSetNodeID ID;
136 for (unsigned i = 0; i != NumVals; ++i)
137 ID.AddPointer(Vals[i]);
138
Devang Patelf7188322009-09-03 01:39:20 +0000139 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000140 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000141 if (!N) {
Chris Lattner9b493022009-12-31 01:22:29 +0000142 // Coallocate space for the node and Operands together, then placement new.
143 void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
Chris Lattner8cb6c342009-12-31 01:05:46 +0000144 N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
145
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000146 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000147 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000148 }
Devang Patelf7188322009-09-03 01:39:20 +0000149 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000150}
151
Chris Lattner9b493022009-12-31 01:22:29 +0000152/// getOperand - Return specified operand.
153Value *MDNode::getOperand(unsigned i) const {
Chris Lattner8cb6c342009-12-31 01:05:46 +0000154 return *getOperandPtr(const_cast<MDNode*>(this), i);
155}
156
Chris Lattnerf543eff2009-12-28 09:12:35 +0000157void MDNode::Profile(FoldingSetNodeID &ID) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000158 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
159 ID.AddPointer(getOperand(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000160}
Devang Patel5c310be2009-08-11 18:01:24 +0000161
Chris Lattnerf543eff2009-12-28 09:12:35 +0000162
Chris Lattner9b493022009-12-31 01:22:29 +0000163// Replace value from this node's operand list.
164void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
Chris Lattner95c445d2009-12-28 09:32:10 +0000165 Value *From = *Op;
166
167 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000168 return;
Devang Patelf7188322009-09-03 01:39:20 +0000169
Chris Lattner30ae06b2009-12-30 21:42:11 +0000170 // Update the operand.
Chris Lattner8cb6c342009-12-31 01:05:46 +0000171 Op->set(To);
Chris Lattner30ae06b2009-12-30 21:42:11 +0000172
173 // If this node is already not being uniqued (because one of the operands
174 // already went to null), then there is nothing else to do here.
175 if (isNotUniqued()) return;
176
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000177 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
178
179 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
180 // this node to remove it, so we don't care what state the operands are in.
181 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000182
Chris Lattner30ae06b2009-12-30 21:42:11 +0000183 // If we are dropping an argument to null, we choose to not unique the MDNode
184 // anymore. This commonly occurs during destruction, and uniquing these
185 // brings little reuse.
186 if (To == 0) {
187 setIsNotUniqued();
188 return;
189 }
190
191 // Now that the node is out of the folding set, get ready to reinsert it.
192 // First, check to see if another node with the same operands already exists
193 // in the set. If it doesn't exist, this returns the position to insert it.
Devang Patelf7188322009-09-03 01:39:20 +0000194 FoldingSetNodeID ID;
195 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000196 void *InsertPoint;
197 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000198
199 if (N) {
200 N->replaceAllUsesWith(this);
Chris Lattner8cb6c342009-12-31 01:05:46 +0000201 N->destroy();
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000202 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
203 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000204 }
205
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000206 // InsertPoint will have been set by the FindNodeOrInsertPos call.
207 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000208}
209
Devang Patel05a26fb2009-07-29 00:33:07 +0000210//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000211// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000212//
Devang Patele3073482010-01-05 20:41:31 +0000213static SmallVector<WeakVH, 4> &getNMDOps(void *Operands) {
214 return *(SmallVector<WeakVH, 4>*)Operands;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000215}
216
Owen Anderson55f1c092009-08-13 21:58:54 +0000217NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Devang Patele3073482010-01-05 20:41:31 +0000218 MDNode *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000219 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000220 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000221 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000222
Devang Patele3073482010-01-05 20:41:31 +0000223 Operands = new SmallVector<WeakVH, 4>();
Chris Lattner1bc810b2009-12-28 08:07:14 +0000224
Devang Patele3073482010-01-05 20:41:31 +0000225 SmallVector<WeakVH, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000226 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patele3073482010-01-05 20:41:31 +0000227 Node.push_back(WeakVH(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000228
Devang Patel4a942d02009-07-29 21:58:56 +0000229 if (ParentModule)
230 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000231}
Devang Patel79238d72009-08-03 06:19:01 +0000232
Devang Patel5c310be2009-08-11 18:01:24 +0000233NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000234 assert(NMD && "Invalid source NamedMDNode!");
Devang Patele3073482010-01-05 20:41:31 +0000235 SmallVector<MDNode *, 4> Elems;
Chris Lattner9b493022009-12-31 01:22:29 +0000236 Elems.reserve(NMD->getNumOperands());
Chris Lattner1bc810b2009-12-28 08:07:14 +0000237
Chris Lattner9b493022009-12-31 01:22:29 +0000238 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
239 Elems.push_back(NMD->getOperand(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000240 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
241 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000242}
243
Chris Lattner1bc810b2009-12-28 08:07:14 +0000244NamedMDNode::~NamedMDNode() {
245 dropAllReferences();
246 delete &getNMDOps(Operands);
247}
248
Chris Lattner9b493022009-12-31 01:22:29 +0000249/// getNumOperands - Return number of NamedMDNode operands.
250unsigned NamedMDNode::getNumOperands() const {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000251 return (unsigned)getNMDOps(Operands).size();
252}
253
Chris Lattner9b493022009-12-31 01:22:29 +0000254/// getOperand - Return specified operand.
Devang Patele3073482010-01-05 20:41:31 +0000255MDNode *NamedMDNode::getOperand(unsigned i) const {
Chris Lattner9b493022009-12-31 01:22:29 +0000256 assert(i < getNumOperands() && "Invalid Operand number!");
Devang Patele3073482010-01-05 20:41:31 +0000257 return dyn_cast_or_null<MDNode>(getNMDOps(Operands)[i]);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000258}
259
Chris Lattner9b493022009-12-31 01:22:29 +0000260/// addOperand - Add metadata Operand.
Devang Patele3073482010-01-05 20:41:31 +0000261void NamedMDNode::addOperand(MDNode *M) {
262 getNMDOps(Operands).push_back(WeakVH(M));
Chris Lattner1bc810b2009-12-28 08:07:14 +0000263}
264
Devang Patel79238d72009-08-03 06:19:01 +0000265/// eraseFromParent - Drop all references and remove the node from parent
266/// module.
267void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000268 getParent()->getNamedMDList().erase(this);
269}
270
271/// dropAllReferences - Remove all uses and clear node vector.
272void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000273 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000274}
275
Devang Pateld5497a4b2009-09-16 18:09:00 +0000276
277//===----------------------------------------------------------------------===//
Chris Lattner8cb6c342009-12-31 01:05:46 +0000278// LLVMContext MDKind naming implementation.
Devang Patel1155fdf2009-10-22 19:36:54 +0000279//
Devang Patel1155fdf2009-10-22 19:36:54 +0000280
Chris Lattner5508da32009-12-29 07:56:15 +0000281#ifndef NDEBUG
Devang Patel1155fdf2009-10-22 19:36:54 +0000282/// isValidName - Return true if Name is a valid custom metadata handler name.
Chris Lattner5508da32009-12-29 07:56:15 +0000283static bool isValidName(StringRef MDName) {
Devang Patel1155fdf2009-10-22 19:36:54 +0000284 if (MDName.empty())
285 return false;
286
287 if (!isalpha(MDName[0]))
288 return false;
289
290 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
291 ++I) {
292 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
293 return false;
294 }
295 return true;
296}
Chris Lattner5508da32009-12-29 07:56:15 +0000297#endif
Devang Patel1155fdf2009-10-22 19:36:54 +0000298
Chris Lattner70939462009-12-28 20:45:51 +0000299/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
Chris Lattnera0566972009-12-29 09:01:33 +0000300unsigned LLVMContext::getMDKindID(StringRef Name) const {
Chris Lattner5508da32009-12-29 07:56:15 +0000301 assert(isValidName(Name) && "Invalid MDNode name");
Chris Lattnera0566972009-12-29 09:01:33 +0000302
303 unsigned &Entry = pImpl->CustomMDKindNames[Name];
304
305 // If this is new, assign it its ID.
306 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
307 return Entry;
Devang Patel1155fdf2009-10-22 19:36:54 +0000308}
309
Devang Patel1155fdf2009-10-22 19:36:54 +0000310/// getHandlerNames - Populate client supplied smallvector using custome
311/// metadata name and ID.
Chris Lattnera0566972009-12-29 09:01:33 +0000312void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
313 Names.resize(pImpl->CustomMDKindNames.size()+1);
314 Names[0] = "";
315 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
316 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
317 // MD Handlers are numbered from 1.
318 Names[I->second] = I->first();
Devang Patel1155fdf2009-10-22 19:36:54 +0000319}
320
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000321//===----------------------------------------------------------------------===//
322// Instruction Metadata method implementations.
323//
324
325void Instruction::setMetadata(const char *Kind, MDNode *Node) {
326 if (Node == 0 && !hasMetadata()) return;
Chris Lattnera0566972009-12-29 09:01:33 +0000327 setMetadata(getContext().getMDKindID(Kind), Node);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000328}
329
330MDNode *Instruction::getMetadataImpl(const char *Kind) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000331 return getMetadataImpl(getContext().getMDKindID(Kind));
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000332}
333
334/// setMetadata - Set the metadata of of the specified kind to the specified
335/// node. This updates/replaces metadata if already present, or removes it if
336/// Node is null.
337void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
338 if (Node == 0 && !hasMetadata()) return;
339
Chris Lattnera0566972009-12-29 09:01:33 +0000340 // Handle the case when we're adding/updating metadata on an instruction.
341 if (Node) {
342 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
343 assert(!Info.empty() == hasMetadata() && "HasMetadata bit is wonked");
344 if (Info.empty()) {
345 setHasMetadata(true);
346 } else {
347 // Handle replacement of an existing value.
348 for (unsigned i = 0, e = Info.size(); i != e; ++i)
349 if (Info[i].first == KindID) {
350 Info[i].second = Node;
351 return;
352 }
353 }
354
355 // No replacement, just add it to the list.
356 Info.push_back(std::make_pair(KindID, Node));
357 return;
358 }
359
360 // Otherwise, we're removing metadata from an instruction.
361 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
362 "HasMetadata bit out of date!");
363 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
364
365 // Common case is removing the only entry.
366 if (Info.size() == 1 && Info[0].first == KindID) {
367 getContext().pImpl->MetadataStore.erase(this);
368 setHasMetadata(false);
369 return;
370 }
371
372 // Handle replacement of an existing value.
373 for (unsigned i = 0, e = Info.size(); i != e; ++i)
374 if (Info[i].first == KindID) {
375 Info[i] = Info.back();
376 Info.pop_back();
377 assert(!Info.empty() && "Removing last entry should be handled above");
378 return;
379 }
380 // Otherwise, removing an entry that doesn't exist on the instruction.
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000381}
382
383MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
Chris Lattnera0566972009-12-29 09:01:33 +0000384 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
385 assert(hasMetadata() && !Info.empty() && "Shouldn't have called this");
386
387 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
388 I != E; ++I)
389 if (I->first == KindID)
390 return I->second;
391 return 0;
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000392}
393
394void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
395 MDNode*> > &Result)const {
Chris Lattnera0566972009-12-29 09:01:33 +0000396 assert(hasMetadata() && getContext().pImpl->MetadataStore.count(this) &&
397 "Shouldn't have called this");
398 const LLVMContextImpl::MDMapTy &Info =
399 getContext().pImpl->MetadataStore.find(this)->second;
400 assert(!Info.empty() && "Shouldn't have called this");
401
402 Result.clear();
403 Result.append(Info.begin(), Info.end());
404
405 // Sort the resulting array so it is stable.
406 if (Result.size() > 1)
407 array_pod_sort(Result.begin(), Result.end());
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000408}
409
Chris Lattner68017802009-12-29 07:44:16 +0000410/// removeAllMetadata - Remove all metadata from this instruction.
411void Instruction::removeAllMetadata() {
412 assert(hasMetadata() && "Caller should check");
Chris Lattnera0566972009-12-29 09:01:33 +0000413 getContext().pImpl->MetadataStore.erase(this);
414 setHasMetadata(false);
Chris Lattner68017802009-12-29 07:44:16 +0000415}
416