blob: 13747547318374af2a5d1c3fb1d3bbfaebe46e05 [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 Lattner74a6ad62009-12-28 07:41:54 +000051// MDNodeElement implementation.
52//
53
54// Use CallbackVH to hold MDNode elements.
55namespace llvm {
56class MDNodeElement : public CallbackVH {
57 MDNode *Parent;
58public:
59 MDNodeElement() {}
60 MDNodeElement(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
61 ~MDNodeElement() {}
62
Chris Lattnerd944cf72009-12-28 09:10:16 +000063 void set(Value *V, MDNode *P) {
64 setValPtr(V);
65 Parent = P;
66 }
67
Chris Lattner74a6ad62009-12-28 07:41:54 +000068 virtual void deleted();
69 virtual void allUsesReplacedWith(Value *NV);
70};
71} // end namespace llvm.
72
73
74void MDNodeElement::deleted() {
Chris Lattner95c445d2009-12-28 09:32:10 +000075 Parent->replaceElement(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000076}
77
78void MDNodeElement::allUsesReplacedWith(Value *NV) {
Chris Lattner95c445d2009-12-28 09:32:10 +000079 Parent->replaceElement(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000080}
81
82
83
84//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000085// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000086//
Chris Lattner74a6ad62009-12-28 07:41:54 +000087
Chris Lattnerf543eff2009-12-28 09:12:35 +000088/// ~MDNode - Destroy MDNode.
89MDNode::~MDNode() {
90 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
91 pImpl->MDNodeSet.RemoveNode(this);
92 delete [] Operands;
93 Operands = NULL;
94}
95
Victor Hernandezdd7418a2009-12-16 02:52:09 +000096MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000097 bool isFunctionLocal)
Owen Anderson55f1c092009-08-13 21:58:54 +000098 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000099 NumOperands = NumVals;
100 Operands = new MDNodeElement[NumOperands];
Chris Lattnerf543eff2009-12-28 09:12:35 +0000101
Devang Patel27e0be22009-10-21 23:57:35 +0000102 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattnerf543eff2009-12-28 09:12:35 +0000103 Operands[i].set(Vals[i], this);
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000104
Victor Hernandez0471abd2009-12-18 20:09:14 +0000105 if (isFunctionLocal)
106 SubclassData |= FunctionLocalBit;
Devang Patela4f43fb2009-07-28 21:49:47 +0000107}
108
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000109MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000110 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000111 LLVMContextImpl *pImpl = Context.pImpl;
112 FoldingSetNodeID ID;
113 for (unsigned i = 0; i != NumVals; ++i)
114 ID.AddPointer(Vals[i]);
115
Devang Patelf7188322009-09-03 01:39:20 +0000116 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000117 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000118 if (!N) {
119 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +0000120 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000121 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000122 }
Devang Patelf7188322009-09-03 01:39:20 +0000123 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000124}
125
Chris Lattnerf543eff2009-12-28 09:12:35 +0000126void MDNode::Profile(FoldingSetNodeID &ID) const {
127 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
128 ID.AddPointer(getElement(i));
Chris Lattner9a258772009-12-28 19:49:00 +0000129 // HASH TABLE COLLISIONS?
130 // DO NOT REINSERT AFTER AN OPERAND DROPS TO NULL!
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000131}
Devang Patel5c310be2009-08-11 18:01:24 +0000132
Chris Lattnerf543eff2009-12-28 09:12:35 +0000133
Chris Lattner74a6ad62009-12-28 07:41:54 +0000134/// getElement - Return specified element.
135Value *MDNode::getElement(unsigned i) const {
136 assert(i < getNumElements() && "Invalid element number!");
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000137 return Operands[i];
Chris Lattner74a6ad62009-12-28 07:41:54 +0000138}
139
140
141
Devang Patelf7188322009-09-03 01:39:20 +0000142// Replace value from this node's element list.
Chris Lattner95c445d2009-12-28 09:32:10 +0000143void MDNode::replaceElement(MDNodeElement *Op, Value *To) {
144 Value *From = *Op;
145
146 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000147 return;
Devang Patelf7188322009-09-03 01:39:20 +0000148
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000149 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
150
151 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
152 // this node to remove it, so we don't care what state the operands are in.
153 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000154
155 // Update the operand.
156 Op->set(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000157
Devang Patelf7188322009-09-03 01:39:20 +0000158 // Insert updated "this" into the context's folding node set.
159 // If a node with same element list already exist then before inserting
160 // updated "this" into the folding node set, replace all uses of existing
161 // node with updated "this" node.
162 FoldingSetNodeID ID;
163 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000164 void *InsertPoint;
165 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000166
167 if (N) {
168 N->replaceAllUsesWith(this);
169 delete N;
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000170 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
171 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000172 }
173
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000174 // InsertPoint will have been set by the FindNodeOrInsertPos call.
175 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000176}
177
Devang Patel05a26fb2009-07-29 00:33:07 +0000178//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000179// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000180//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000181static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
182 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
183}
184
Owen Anderson55f1c092009-08-13 21:58:54 +0000185NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000186 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000187 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000188 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000189 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000190
191 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
192
193 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000194 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000195 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000196
Devang Patel4a942d02009-07-29 21:58:56 +0000197 if (ParentModule)
198 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000199}
Devang Patel79238d72009-08-03 06:19:01 +0000200
Devang Patel5c310be2009-08-11 18:01:24 +0000201NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000202 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000203 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000204 Elems.reserve(NMD->getNumElements());
205
Devang Patel5c310be2009-08-11 18:01:24 +0000206 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
207 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000208 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
209 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000210}
211
Chris Lattner1bc810b2009-12-28 08:07:14 +0000212NamedMDNode::~NamedMDNode() {
213 dropAllReferences();
214 delete &getNMDOps(Operands);
215}
216
217/// getNumElements - Return number of NamedMDNode elements.
218unsigned NamedMDNode::getNumElements() const {
219 return (unsigned)getNMDOps(Operands).size();
220}
221
222/// getElement - Return specified element.
223MetadataBase *NamedMDNode::getElement(unsigned i) const {
224 assert(i < getNumElements() && "Invalid element number!");
225 return getNMDOps(Operands)[i];
226}
227
228/// addElement - Add metadata element.
229void NamedMDNode::addElement(MetadataBase *M) {
230 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
231}
232
Devang Patel79238d72009-08-03 06:19:01 +0000233/// eraseFromParent - Drop all references and remove the node from parent
234/// module.
235void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000236 getParent()->getNamedMDList().erase(this);
237}
238
239/// dropAllReferences - Remove all uses and clear node vector.
240void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000241 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000242}
243
Devang Pateld5497a4b2009-09-16 18:09:00 +0000244
245//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000246// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000247//
Devang Patel1155fdf2009-10-22 19:36:54 +0000248namespace llvm {
249class MetadataContextImpl {
250public:
251 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
252 typedef SmallVector<MDPairTy, 2> MDMapTy;
253 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
254 friend class BitcodeReader;
255private:
256
257 /// MetadataStore - Collection of metadata used in this context.
258 MDStoreTy MetadataStore;
259
260 /// MDHandlerNames - Map to hold metadata handler names.
261 StringMap<unsigned> MDHandlerNames;
262
263public:
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000264 // Name <-> ID mapping methods.
Chris Lattner70939462009-12-28 20:45:51 +0000265 unsigned getMDKindID(StringRef Name);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000266 void getMDKindNames(SmallVectorImpl<StringRef> &) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000267
Devang Patel1155fdf2009-10-22 19:36:54 +0000268
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000269 // Instruction metadata methods.
270 MDNode *getMetadata(const Instruction *Inst, unsigned Kind);
271 void getAllMetadata(const Instruction *Inst,
272 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const;
273
274 void setMetadata(Instruction *Inst, unsigned Kind, MDNode *Node);
275
Devang Patel1155fdf2009-10-22 19:36:54 +0000276 /// removeAllMetadata - Remove all metadata attached with an instruction.
277 void removeAllMetadata(Instruction *Inst);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000278
279
280
Devang Patel1155fdf2009-10-22 19:36:54 +0000281 /// copyMD - If metadata is attached with Instruction In1 then attach
282 /// the same metadata to In2.
283 void copyMD(Instruction *In1, Instruction *In2);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000284
Devang Patel1155fdf2009-10-22 19:36:54 +0000285
286 /// ValueIsDeleted - This handler is used to update metadata store
287 /// when a value is deleted.
288 void ValueIsDeleted(const Value *) {}
289 void ValueIsDeleted(Instruction *Inst) {
290 removeAllMetadata(Inst);
291 }
292 void ValueIsRAUWd(Value *V1, Value *V2);
293
294 /// ValueIsCloned - This handler is used to update metadata store
295 /// when In1 is cloned to create In2.
296 void ValueIsCloned(const Instruction *In1, Instruction *In2);
297};
298}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000299
Chris Lattner70939462009-12-28 20:45:51 +0000300/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
301unsigned MetadataContextImpl::getMDKindID(StringRef Name) {
302 unsigned &Entry = MDHandlerNames[Name];
Devang Pateld5497a4b2009-09-16 18:09:00 +0000303
Chris Lattner70939462009-12-28 20:45:51 +0000304 // If this is new, assign it its ID.
305 if (Entry == 0) Entry = MDHandlerNames.size();
Chris Lattner70939462009-12-28 20:45:51 +0000306 return Entry;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000307}
308
Devang Patel09c319e2009-10-22 17:40:37 +0000309/// getHandlerNames - Populate client supplied smallvector using custome
310/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000311void MetadataContextImpl::
Chris Lattnerc9558df2009-12-28 20:10:43 +0000312getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
313 Names.resize(MDHandlerNames.size()+1);
314 Names[0] = "";
Devang Patel0fffb492009-10-22 01:01:24 +0000315 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000316 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000317 // MD Handlers are numbered from 1.
Chris Lattnerc9558df2009-12-28 20:10:43 +0000318 Names[I->second] = I->first();
Devang Patelaf206b82009-09-18 19:26:43 +0000319}
320
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000321
322/// getMetadata - Get the metadata of given kind attached to an Instruction.
323/// If the metadata is not found then return 0.
324MDNode *MetadataContextImpl::
325getMetadata(const Instruction *Inst, unsigned MDKind) {
326 MDMapTy &Info = MetadataStore[Inst];
327 assert(Inst->hasMetadata() && !Info.empty() && "Shouldn't have called this");
328
329 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
330 if (I->first == MDKind)
331 return I->second;
332 return 0;
333}
334
335/// getAllMetadata - Get all of the metadata attached to an Instruction.
336void MetadataContextImpl::
337getAllMetadata(const Instruction *Inst,
338 SmallVectorImpl<std::pair<unsigned, MDNode*> > &Result) const {
339 assert(Inst->hasMetadata() && MetadataStore.find(Inst) != MetadataStore.end()
340 && "Shouldn't have called this");
341 const MDMapTy &Info = MetadataStore.find(Inst)->second;
342 assert(!Info.empty() && "Shouldn't have called this");
343
344 Result.clear();
345 Result.append(Info.begin(), Info.end());
346
347 // Sort the resulting array so it is stable.
348 if (Result.size() > 1)
349 array_pod_sort(Result.begin(), Result.end());
350}
351
352
353void MetadataContextImpl::setMetadata(Instruction *Inst, unsigned Kind,
354 MDNode *Node) {
355 // Handle the case when we're adding/updating metadata on an instruction.
356 if (Node) {
357 MDMapTy &Info = MetadataStore[Inst];
358 assert(!Info.empty() == Inst->HasMetadata && "HasMetadata bit is wonked");
359 if (Info.empty()) {
360 Inst->HasMetadata = true;
361 } else {
362 // Handle replacement of an existing value.
363 for (unsigned i = 0, e = Info.size(); i != e; ++i)
364 if (Info[i].first == Kind) {
365 Info[i].second = Node;
366 return;
367 }
368 }
369
370 // No replacement, just add it to the list.
371 Info.push_back(std::make_pair(Kind, Node));
372 return;
373 }
374
375 // Otherwise, we're removing metadata from an instruction.
376 assert(Inst->HasMetadata && MetadataStore.count(Inst) &&
377 "HasMetadata bit out of date!");
378 MDMapTy &Info = MetadataStore[Inst];
379
380 // Common case is removing the only entry.
381 if (Info.size() == 1 && Info[0].first == Kind) {
382 MetadataStore.erase(Inst);
383 Inst->HasMetadata = false;
384 return;
385 }
386
387 // Handle replacement of an existing value.
388 for (unsigned i = 0, e = Info.size(); i != e; ++i)
389 if (Info[i].first == Kind) {
390 Info[i] = Info.back();
391 Info.pop_back();
392 assert(!Info.empty() && "Removing last entry should be handled above");
393 return;
394 }
395 // Otherwise, removing an entry that doesn't exist on the instruction.
396}
397
398/// removeAllMetadata - Remove all metadata attached with an instruction.
399void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
400 MetadataStore.erase(Inst);
401 Inst->HasMetadata = false;
402}
403
404
405/// copyMD - If metadata is attached with Instruction In1 then attach
406/// the same metadata to In2.
407void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
408 assert(In1 && In2 && "Invalid instruction!");
409 MDMapTy &In1Info = MetadataStore[In1];
410 if (In1Info.empty())
411 return;
412
413 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
414 In2->setMetadata(I->first, I->second);
415}
416
Devang Pateladd58652009-09-23 18:32:25 +0000417/// ValueIsCloned - This handler is used to update metadata store
418/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000419void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
420 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000421 // Find Metadata handles for In1.
422 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000423 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000424
Chris Lattner9a258772009-12-28 19:49:00 +0000425 // FIXME: Give all metadata handlers a chance to adjust.
Devang Pateladd58652009-09-23 18:32:25 +0000426 MDMapTy &In1Info = I->second;
Devang Pateladd58652009-09-23 18:32:25 +0000427 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000428 In2->setMetadata(I->first, I->second);
Devang Pateladd58652009-09-23 18:32:25 +0000429}
Devang Patele6f26a72009-10-13 17:00:54 +0000430
431/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
432/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000433void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000434 Instruction *I1 = dyn_cast<Instruction>(V1);
435 Instruction *I2 = dyn_cast<Instruction>(V2);
436 if (!I1 || !I2)
437 return;
438
Chris Lattner9a258772009-12-28 19:49:00 +0000439 // FIXME: Give custom handlers a chance to override this.
Devang Patele6f26a72009-10-13 17:00:54 +0000440 ValueIsCloned(I1, I2);
441}
Devang Patelebaa76e2009-10-14 17:02:49 +0000442
Devang Patel1155fdf2009-10-22 19:36:54 +0000443//===----------------------------------------------------------------------===//
444// MetadataContext implementation.
445//
Chris Lattner9a258772009-12-28 19:49:00 +0000446MetadataContext::MetadataContext() : pImpl(new MetadataContextImpl()) { }
Devang Patel1155fdf2009-10-22 19:36:54 +0000447MetadataContext::~MetadataContext() { delete pImpl; }
448
449/// isValidName - Return true if Name is a valid custom metadata handler name.
450bool MetadataContext::isValidName(StringRef MDName) {
451 if (MDName.empty())
452 return false;
453
454 if (!isalpha(MDName[0]))
455 return false;
456
457 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
458 ++I) {
459 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
460 return false;
461 }
462 return true;
463}
464
Chris Lattner70939462009-12-28 20:45:51 +0000465/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
466unsigned MetadataContext::getMDKindID(StringRef Name) const {
467 return pImpl->getMDKindID(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000468}
469
Devang Patel1155fdf2009-10-22 19:36:54 +0000470/// copyMD - If metadata is attached with Instruction In1 then attach
471/// the same metadata to In2.
472void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
473 pImpl->copyMD(In1, In2);
474}
475
476/// getHandlerNames - Populate client supplied smallvector using custome
477/// metadata name and ID.
Chris Lattnerc9558df2009-12-28 20:10:43 +0000478void MetadataContext::getMDKindNames(SmallVectorImpl<StringRef> &N) const {
479 pImpl->getMDKindNames(N);
Devang Patel1155fdf2009-10-22 19:36:54 +0000480}
481
482/// ValueIsDeleted - This handler is used to update metadata store
483/// when a value is deleted.
484void MetadataContext::ValueIsDeleted(Instruction *Inst) {
485 pImpl->ValueIsDeleted(Inst);
486}
487void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
488 pImpl->ValueIsRAUWd(V1, V2);
489}
490
491/// ValueIsCloned - This handler is used to update metadata store
492/// when In1 is cloned to create In2.
493void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
494 pImpl->ValueIsCloned(In1, In2);
495}
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000496
497//===----------------------------------------------------------------------===//
498// Instruction Metadata method implementations.
499//
500
501void Instruction::setMetadata(const char *Kind, MDNode *Node) {
502 if (Node == 0 && !hasMetadata()) return;
503 setMetadata(getContext().getMetadata().getMDKindID(Kind), Node);
504}
505
506MDNode *Instruction::getMetadataImpl(const char *Kind) const {
507 return getMetadataImpl(getContext().getMetadata().getMDKindID(Kind));
508}
509
510/// setMetadata - Set the metadata of of the specified kind to the specified
511/// node. This updates/replaces metadata if already present, or removes it if
512/// Node is null.
513void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
514 if (Node == 0 && !hasMetadata()) return;
515
516 getContext().getMetadata().pImpl->setMetadata(this, KindID, Node);
517}
518
519MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
520 return getContext().getMetadata().pImpl->getMetadata(this, KindID);
521}
522
523void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
524 MDNode*> > &Result)const {
525 getContext().getMetadata().pImpl->getAllMetadata(this, Result);
526}
527