blob: eddd8b070a6e9b69beb7dd72bd00dd4d85d2b99f [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// MetadataBase implementation.
Devang Pateld7fd6ab2009-08-03 22:51:10 +000027//
28
Devang Pateld7fd6ab2009-08-03 22:51:10 +000029//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000030// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000031//
Chris Lattner5a409bd2009-12-28 08:30:43 +000032
33MDString::MDString(LLVMContext &C, StringRef S)
34 : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
35
Devang Pateldcb99d32009-10-22 00:10:15 +000036MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000037 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson0087fe62009-07-31 21:35:40 +000038 StringMapEntry<MDString *> &Entry =
39 pImpl->MDStringCache.GetOrCreateValue(Str);
40 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000041 if (!S) S = new MDString(Context, Entry.getKey());
42 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000043}
44
Devang Patel862ef782009-11-12 00:50:58 +000045MDString *MDString::get(LLVMContext &Context, const char *Str) {
46 LLVMContextImpl *pImpl = Context.pImpl;
47 StringMapEntry<MDString *> &Entry =
48 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
49 MDString *&S = Entry.getValue();
Nick Lewycky6e052512009-11-27 19:57:53 +000050 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000051 return S;
Devang Patel862ef782009-11-12 00:50:58 +000052}
53
Owen Anderson0087fe62009-07-31 21:35:40 +000054//===----------------------------------------------------------------------===//
Chris Lattner74a6ad62009-12-28 07:41:54 +000055// MDNodeElement implementation.
56//
57
58// Use CallbackVH to hold MDNode elements.
59namespace llvm {
60class MDNodeElement : public CallbackVH {
61 MDNode *Parent;
62public:
63 MDNodeElement() {}
64 MDNodeElement(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
65 ~MDNodeElement() {}
66
Chris Lattnerd944cf72009-12-28 09:10:16 +000067 void set(Value *V, MDNode *P) {
68 setValPtr(V);
69 Parent = P;
70 }
71
Chris Lattner74a6ad62009-12-28 07:41:54 +000072 virtual void deleted();
73 virtual void allUsesReplacedWith(Value *NV);
74};
75} // end namespace llvm.
76
77
78void MDNodeElement::deleted() {
79 Parent->replaceElement(this->operator Value*(), 0);
80}
81
82void MDNodeElement::allUsesReplacedWith(Value *NV) {
83 Parent->replaceElement(this->operator Value*(), NV);
84}
85
86
87
88//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000089// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000090//
Chris Lattner74a6ad62009-12-28 07:41:54 +000091
Chris Lattnerf543eff2009-12-28 09:12:35 +000092/// ~MDNode - Destroy MDNode.
93MDNode::~MDNode() {
94 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
95 pImpl->MDNodeSet.RemoveNode(this);
96 delete [] Operands;
97 Operands = NULL;
98}
99
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000100MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000101 bool isFunctionLocal)
Owen Anderson55f1c092009-08-13 21:58:54 +0000102 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000103 NumOperands = NumVals;
104 Operands = new MDNodeElement[NumOperands];
Chris Lattnerf543eff2009-12-28 09:12:35 +0000105
Devang Patel27e0be22009-10-21 23:57:35 +0000106 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattnerf543eff2009-12-28 09:12:35 +0000107 Operands[i].set(Vals[i], this);
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000108
Victor Hernandez0471abd2009-12-18 20:09:14 +0000109 if (isFunctionLocal)
110 SubclassData |= FunctionLocalBit;
Devang Patela4f43fb2009-07-28 21:49:47 +0000111}
112
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000113MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000114 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000115 LLVMContextImpl *pImpl = Context.pImpl;
116 FoldingSetNodeID ID;
117 for (unsigned i = 0; i != NumVals; ++i)
118 ID.AddPointer(Vals[i]);
119
Devang Patelf7188322009-09-03 01:39:20 +0000120 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000121 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000122 if (!N) {
123 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +0000124 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000125 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000126 }
Devang Patelf7188322009-09-03 01:39:20 +0000127 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000128}
129
Chris Lattnerf543eff2009-12-28 09:12:35 +0000130void MDNode::Profile(FoldingSetNodeID &ID) const {
131 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
132 ID.AddPointer(getElement(i));
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000133}
Devang Patel5c310be2009-08-11 18:01:24 +0000134
Chris Lattnerf543eff2009-12-28 09:12:35 +0000135
Chris Lattner74a6ad62009-12-28 07:41:54 +0000136/// getElement - Return specified element.
137Value *MDNode::getElement(unsigned i) const {
138 assert(i < getNumElements() && "Invalid element number!");
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000139 return Operands[i];
Chris Lattner74a6ad62009-12-28 07:41:54 +0000140}
141
142
143
Devang Patelf7188322009-09-03 01:39:20 +0000144// Replace value from this node's element list.
145void MDNode::replaceElement(Value *From, Value *To) {
146 if (From == To || !getType())
147 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);
154
Devang Patelf7188322009-09-03 01:39:20 +0000155 // Find value. This is a linear search, do something if it consumes
156 // lot of time. It is possible that to have multiple instances of
157 // From in this MDNode's element list.
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000158 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
159 if (Operands[i] == From)
160 Operands[i].set(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000161 }
162
Devang Patelf7188322009-09-03 01:39:20 +0000163 // Insert updated "this" into the context's folding node set.
164 // If a node with same element list already exist then before inserting
165 // updated "this" into the folding node set, replace all uses of existing
166 // node with updated "this" node.
167 FoldingSetNodeID ID;
168 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000169 void *InsertPoint;
170 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000171
172 if (N) {
173 N->replaceAllUsesWith(this);
174 delete N;
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000175 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
176 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000177 }
178
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000179 // InsertPoint will have been set by the FindNodeOrInsertPos call.
180 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000181}
182
Devang Patel05a26fb2009-07-29 00:33:07 +0000183//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000184// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000185//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000186static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
187 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
188}
189
Owen Anderson55f1c092009-08-13 21:58:54 +0000190NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000191 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000192 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000193 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000194 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000195
196 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
197
198 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000199 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000200 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000201
Devang Patel4a942d02009-07-29 21:58:56 +0000202 if (ParentModule)
203 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000204}
Devang Patel79238d72009-08-03 06:19:01 +0000205
Devang Patel5c310be2009-08-11 18:01:24 +0000206NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000207 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000208 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000209 Elems.reserve(NMD->getNumElements());
210
Devang Patel5c310be2009-08-11 18:01:24 +0000211 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
212 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000213 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
214 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000215}
216
Chris Lattner1bc810b2009-12-28 08:07:14 +0000217NamedMDNode::~NamedMDNode() {
218 dropAllReferences();
219 delete &getNMDOps(Operands);
220}
221
222/// getNumElements - Return number of NamedMDNode elements.
223unsigned NamedMDNode::getNumElements() const {
224 return (unsigned)getNMDOps(Operands).size();
225}
226
227/// getElement - Return specified element.
228MetadataBase *NamedMDNode::getElement(unsigned i) const {
229 assert(i < getNumElements() && "Invalid element number!");
230 return getNMDOps(Operands)[i];
231}
232
233/// addElement - Add metadata element.
234void NamedMDNode::addElement(MetadataBase *M) {
235 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
236}
237
Devang Patel79238d72009-08-03 06:19:01 +0000238/// eraseFromParent - Drop all references and remove the node from parent
239/// module.
240void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000241 getParent()->getNamedMDList().erase(this);
242}
243
244/// dropAllReferences - Remove all uses and clear node vector.
245void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000246 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000247}
248
Devang Pateld5497a4b2009-09-16 18:09:00 +0000249
250//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000251// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000252//
Devang Patel1155fdf2009-10-22 19:36:54 +0000253namespace llvm {
254class MetadataContextImpl {
255public:
256 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
257 typedef SmallVector<MDPairTy, 2> MDMapTy;
258 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
259 friend class BitcodeReader;
260private:
261
262 /// MetadataStore - Collection of metadata used in this context.
263 MDStoreTy MetadataStore;
264
265 /// MDHandlerNames - Map to hold metadata handler names.
266 StringMap<unsigned> MDHandlerNames;
267
268public:
269 /// registerMDKind - Register a new metadata kind and return its ID.
270 /// A metadata kind can be registered only once.
271 unsigned registerMDKind(StringRef Name);
272
273 /// getMDKind - Return metadata kind. If the requested metadata kind
274 /// is not registered then return 0.
275 unsigned getMDKind(StringRef Name) const;
276
277 /// getMD - Get the metadata of given kind attached to an Instruction.
278 /// If the metadata is not found then return 0.
279 MDNode *getMD(unsigned Kind, const Instruction *Inst);
280
281 /// getMDs - Get the metadata attached to an Instruction.
Chris Lattner53bb5e42009-12-28 08:14:54 +0000282 void getMDs(const Instruction *Inst,
283 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000284
285 /// addMD - Attach the metadata of given kind to an Instruction.
286 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
287
Nick Lewyckya75fe182009-11-26 23:19:05 +0000288 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000289 void removeMD(unsigned Kind, Instruction *Inst);
290
291 /// removeAllMetadata - Remove all metadata attached with an instruction.
292 void removeAllMetadata(Instruction *Inst);
293
294 /// copyMD - If metadata is attached with Instruction In1 then attach
295 /// the same metadata to In2.
296 void copyMD(Instruction *In1, Instruction *In2);
297
Nick Lewycky898e8f72009-11-26 22:54:26 +0000298 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000299 /// metadata name and ID.
300 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
301
302 /// ValueIsDeleted - This handler is used to update metadata store
303 /// when a value is deleted.
304 void ValueIsDeleted(const Value *) {}
305 void ValueIsDeleted(Instruction *Inst) {
306 removeAllMetadata(Inst);
307 }
308 void ValueIsRAUWd(Value *V1, Value *V2);
309
310 /// ValueIsCloned - This handler is used to update metadata store
311 /// when In1 is cloned to create In2.
312 void ValueIsCloned(const Instruction *In1, Instruction *In2);
313};
314}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000315
Devang Patel0c35dbd2009-10-20 22:50:27 +0000316/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000317/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000318unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000319 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000320 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
321 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000322}
323
324/// getMDKind - Return metadata kind. If the requested metadata kind
325/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000326unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000327 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000328 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000329 return 0;
330
331 return I->getValue();
332}
333
Devang Patel0c35dbd2009-10-20 22:50:27 +0000334/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000335void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
336 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000337 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000338 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000339 MDMapTy &Info = MetadataStore[Inst];
340 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000341 Info.push_back(std::make_pair(MDKind, Node));
342 MetadataStore.insert(std::make_pair(Inst, Info));
343 return;
344 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000345
Devang Patel5bf7a492009-09-29 20:30:57 +0000346 // If there is an entry for this MDKind then replace it.
347 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
348 MDPairTy &P = Info[i];
349 if (P.first == MDKind) {
350 Info[i] = std::make_pair(MDKind, Node);
351 return;
352 }
353 }
354
355 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000356 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000357}
358
Nick Lewyckya75fe182009-11-26 23:19:05 +0000359/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000360void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000361 MDStoreTy::iterator I = MetadataStore.find(Inst);
362 if (I == MetadataStore.end())
363 return;
364
365 MDMapTy &Info = I->second;
366 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
367 MDPairTy &P = *MI;
368 if (P.first == Kind) {
369 Info.erase(MI);
370 return;
371 }
372 }
Devang Patelb4034362009-09-29 20:42:25 +0000373}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000374
Devang Patel3eb5d332009-10-21 17:33:41 +0000375/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000376void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000377 MetadataStore.erase(Inst);
378 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000379}
380
Devang Patelebaa76e2009-10-14 17:02:49 +0000381/// copyMD - If metadata is attached with Instruction In1 then attach
382/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000383void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000384 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000385 MDMapTy &In1Info = MetadataStore[In1];
386 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000387 return;
388
Devang Patelebaa76e2009-10-14 17:02:49 +0000389 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000390 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000391}
Devang Patelb4034362009-09-29 20:42:25 +0000392
Devang Patel0c35dbd2009-10-20 22:50:27 +0000393/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000394/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000395MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000396 MDMapTy &Info = MetadataStore[Inst];
397 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000398 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000399
Devang Pateld5497a4b2009-09-16 18:09:00 +0000400 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
401 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000402 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000403 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000404}
405
Devang Patel0c35dbd2009-10-20 22:50:27 +0000406/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000407void MetadataContextImpl::
Chris Lattner53bb5e42009-12-28 08:14:54 +0000408getMDs(const Instruction *Inst,
409 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000410 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000411 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000412 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000413 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000414 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000415 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000416 // MD kinds are numbered from 1.
417 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000418}
419
Devang Patel09c319e2009-10-22 17:40:37 +0000420/// getHandlerNames - Populate client supplied smallvector using custome
421/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000422void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000423getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000424 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000425 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
426 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000427 // MD Handlers are numbered from 1.
428 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000429}
430
Devang Pateladd58652009-09-23 18:32:25 +0000431/// ValueIsCloned - This handler is used to update metadata store
432/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000433void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
434 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000435 // Find Metadata handles for In1.
436 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000437 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000438
439 // FIXME : Give all metadata handlers a chance to adjust.
440
441 MDMapTy &In1Info = I->second;
442 MDMapTy In2Info;
443 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000444 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000445}
Devang Patele6f26a72009-10-13 17:00:54 +0000446
447/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
448/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000449void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000450 Instruction *I1 = dyn_cast<Instruction>(V1);
451 Instruction *I2 = dyn_cast<Instruction>(V2);
452 if (!I1 || !I2)
453 return;
454
455 // FIXME : Give custom handlers a chance to override this.
456 ValueIsCloned(I1, I2);
457}
Devang Patelebaa76e2009-10-14 17:02:49 +0000458
Devang Patel1155fdf2009-10-22 19:36:54 +0000459//===----------------------------------------------------------------------===//
460// MetadataContext implementation.
461//
462MetadataContext::MetadataContext()
463 : pImpl(new MetadataContextImpl()) { }
464MetadataContext::~MetadataContext() { delete pImpl; }
465
466/// isValidName - Return true if Name is a valid custom metadata handler name.
467bool MetadataContext::isValidName(StringRef MDName) {
468 if (MDName.empty())
469 return false;
470
471 if (!isalpha(MDName[0]))
472 return false;
473
474 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
475 ++I) {
476 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
477 return false;
478 }
479 return true;
480}
481
482/// registerMDKind - Register a new metadata kind and return its ID.
483/// A metadata kind can be registered only once.
484unsigned MetadataContext::registerMDKind(StringRef Name) {
485 assert(isValidName(Name) && "Invalid custome metadata name!");
486 return pImpl->registerMDKind(Name);
487}
488
489/// getMDKind - Return metadata kind. If the requested metadata kind
490/// is not registered then return 0.
491unsigned MetadataContext::getMDKind(StringRef Name) const {
492 return pImpl->getMDKind(Name);
493}
494
495/// getMD - Get the metadata of given kind attached to an Instruction.
496/// If the metadata is not found then return 0.
497MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
498 return pImpl->getMD(Kind, Inst);
499}
500
501/// getMDs - Get the metadata attached to an Instruction.
502void MetadataContext::
503getMDs(const Instruction *Inst,
Chris Lattner53bb5e42009-12-28 08:14:54 +0000504 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Devang Patel1155fdf2009-10-22 19:36:54 +0000505 return pImpl->getMDs(Inst, MDs);
506}
507
508/// addMD - Attach the metadata of given kind to an Instruction.
509void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
510 pImpl->addMD(Kind, Node, Inst);
511}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000512
Nick Lewyckya75fe182009-11-26 23:19:05 +0000513/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000514void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
515 pImpl->removeMD(Kind, Inst);
516}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000517
Devang Patel1155fdf2009-10-22 19:36:54 +0000518/// removeAllMetadata - Remove all metadata attached with an instruction.
519void MetadataContext::removeAllMetadata(Instruction *Inst) {
520 pImpl->removeAllMetadata(Inst);
521}
522
523/// copyMD - If metadata is attached with Instruction In1 then attach
524/// the same metadata to In2.
525void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
526 pImpl->copyMD(In1, In2);
527}
528
529/// getHandlerNames - Populate client supplied smallvector using custome
530/// metadata name and ID.
531void MetadataContext::
532getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
533 pImpl->getHandlerNames(N);
534}
535
536/// ValueIsDeleted - This handler is used to update metadata store
537/// when a value is deleted.
538void MetadataContext::ValueIsDeleted(Instruction *Inst) {
539 pImpl->ValueIsDeleted(Inst);
540}
541void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
542 pImpl->ValueIsRAUWd(V1, V2);
543}
544
545/// ValueIsCloned - This handler is used to update metadata store
546/// when In1 is cloned to create In2.
547void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
548 pImpl->ValueIsCloned(In1, In2);
549}