blob: 0a3ddcbbe1b6361cde546a7baa7ac77c57660d5e [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() {
Chris Lattner95c445d2009-12-28 09:32:10 +000079 Parent->replaceElement(this, 0);
Chris Lattner74a6ad62009-12-28 07:41:54 +000080}
81
82void MDNodeElement::allUsesReplacedWith(Value *NV) {
Chris Lattner95c445d2009-12-28 09:32:10 +000083 Parent->replaceElement(this, NV);
Chris Lattner74a6ad62009-12-28 07:41:54 +000084}
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.
Chris Lattner95c445d2009-12-28 09:32:10 +0000145void MDNode::replaceElement(MDNodeElement *Op, Value *To) {
146 Value *From = *Op;
147
148 if (From == To)
Devang Patelf7188322009-09-03 01:39:20 +0000149 return;
Devang Patelf7188322009-09-03 01:39:20 +0000150
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000151 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
152
153 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
154 // this node to remove it, so we don't care what state the operands are in.
155 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner95c445d2009-12-28 09:32:10 +0000156
157 // Update the operand.
158 Op->set(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000159
Devang Patelf7188322009-09-03 01:39:20 +0000160 // Insert updated "this" into the context's folding node set.
161 // If a node with same element list already exist then before inserting
162 // updated "this" into the folding node set, replace all uses of existing
163 // node with updated "this" node.
164 FoldingSetNodeID ID;
165 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000166 void *InsertPoint;
167 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000168
169 if (N) {
170 N->replaceAllUsesWith(this);
171 delete N;
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000172 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
173 assert(N == 0 && "shouldn't be in the map now!"); (void)N;
Devang Patelf7188322009-09-03 01:39:20 +0000174 }
175
Chris Lattnerc6d17e22009-12-28 09:24:53 +0000176 // InsertPoint will have been set by the FindNodeOrInsertPos call.
177 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000178}
179
Devang Patel05a26fb2009-07-29 00:33:07 +0000180//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000181// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000182//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000183static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
184 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
185}
186
Owen Anderson55f1c092009-08-13 21:58:54 +0000187NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000188 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000189 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000190 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000191 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000192
193 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
194
195 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000196 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000197 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000198
Devang Patel4a942d02009-07-29 21:58:56 +0000199 if (ParentModule)
200 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000201}
Devang Patel79238d72009-08-03 06:19:01 +0000202
Devang Patel5c310be2009-08-11 18:01:24 +0000203NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000204 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000205 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000206 Elems.reserve(NMD->getNumElements());
207
Devang Patel5c310be2009-08-11 18:01:24 +0000208 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
209 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000210 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
211 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000212}
213
Chris Lattner1bc810b2009-12-28 08:07:14 +0000214NamedMDNode::~NamedMDNode() {
215 dropAllReferences();
216 delete &getNMDOps(Operands);
217}
218
219/// getNumElements - Return number of NamedMDNode elements.
220unsigned NamedMDNode::getNumElements() const {
221 return (unsigned)getNMDOps(Operands).size();
222}
223
224/// getElement - Return specified element.
225MetadataBase *NamedMDNode::getElement(unsigned i) const {
226 assert(i < getNumElements() && "Invalid element number!");
227 return getNMDOps(Operands)[i];
228}
229
230/// addElement - Add metadata element.
231void NamedMDNode::addElement(MetadataBase *M) {
232 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
233}
234
Devang Patel79238d72009-08-03 06:19:01 +0000235/// eraseFromParent - Drop all references and remove the node from parent
236/// module.
237void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000238 getParent()->getNamedMDList().erase(this);
239}
240
241/// dropAllReferences - Remove all uses and clear node vector.
242void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000243 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000244}
245
Devang Pateld5497a4b2009-09-16 18:09:00 +0000246
247//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000248// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000249//
Devang Patel1155fdf2009-10-22 19:36:54 +0000250namespace llvm {
251class MetadataContextImpl {
252public:
253 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
254 typedef SmallVector<MDPairTy, 2> MDMapTy;
255 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
256 friend class BitcodeReader;
257private:
258
259 /// MetadataStore - Collection of metadata used in this context.
260 MDStoreTy MetadataStore;
261
262 /// MDHandlerNames - Map to hold metadata handler names.
263 StringMap<unsigned> MDHandlerNames;
264
265public:
266 /// registerMDKind - Register a new metadata kind and return its ID.
267 /// A metadata kind can be registered only once.
268 unsigned registerMDKind(StringRef Name);
269
270 /// getMDKind - Return metadata kind. If the requested metadata kind
271 /// is not registered then return 0.
272 unsigned getMDKind(StringRef Name) const;
273
274 /// getMD - Get the metadata of given kind attached to an Instruction.
275 /// If the metadata is not found then return 0.
276 MDNode *getMD(unsigned Kind, const Instruction *Inst);
277
278 /// getMDs - Get the metadata attached to an Instruction.
Chris Lattner53bb5e42009-12-28 08:14:54 +0000279 void getMDs(const Instruction *Inst,
280 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000281
282 /// addMD - Attach the metadata of given kind to an Instruction.
283 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
284
Nick Lewyckya75fe182009-11-26 23:19:05 +0000285 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000286 void removeMD(unsigned Kind, Instruction *Inst);
287
288 /// removeAllMetadata - Remove all metadata attached with an instruction.
289 void removeAllMetadata(Instruction *Inst);
290
291 /// copyMD - If metadata is attached with Instruction In1 then attach
292 /// the same metadata to In2.
293 void copyMD(Instruction *In1, Instruction *In2);
294
Nick Lewycky898e8f72009-11-26 22:54:26 +0000295 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000296 /// metadata name and ID.
297 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
298
299 /// ValueIsDeleted - This handler is used to update metadata store
300 /// when a value is deleted.
301 void ValueIsDeleted(const Value *) {}
302 void ValueIsDeleted(Instruction *Inst) {
303 removeAllMetadata(Inst);
304 }
305 void ValueIsRAUWd(Value *V1, Value *V2);
306
307 /// ValueIsCloned - This handler is used to update metadata store
308 /// when In1 is cloned to create In2.
309 void ValueIsCloned(const Instruction *In1, Instruction *In2);
310};
311}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000312
Devang Patel0c35dbd2009-10-20 22:50:27 +0000313/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000314/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000315unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000316 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000317 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
318 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000319}
320
321/// getMDKind - Return metadata kind. If the requested metadata kind
322/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000323unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000324 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000325 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000326 return 0;
327
328 return I->getValue();
329}
330
Devang Patel0c35dbd2009-10-20 22:50:27 +0000331/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000332void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
333 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000334 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000335 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000336 MDMapTy &Info = MetadataStore[Inst];
337 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000338 Info.push_back(std::make_pair(MDKind, Node));
339 MetadataStore.insert(std::make_pair(Inst, Info));
340 return;
341 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000342
Devang Patel5bf7a492009-09-29 20:30:57 +0000343 // If there is an entry for this MDKind then replace it.
344 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
345 MDPairTy &P = Info[i];
346 if (P.first == MDKind) {
347 Info[i] = std::make_pair(MDKind, Node);
348 return;
349 }
350 }
351
352 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000353 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000354}
355
Nick Lewyckya75fe182009-11-26 23:19:05 +0000356/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000357void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000358 MDStoreTy::iterator I = MetadataStore.find(Inst);
359 if (I == MetadataStore.end())
360 return;
361
362 MDMapTy &Info = I->second;
363 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
364 MDPairTy &P = *MI;
365 if (P.first == Kind) {
366 Info.erase(MI);
367 return;
368 }
369 }
Devang Patelb4034362009-09-29 20:42:25 +0000370}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000371
Devang Patel3eb5d332009-10-21 17:33:41 +0000372/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000373void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000374 MetadataStore.erase(Inst);
375 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000376}
377
Devang Patelebaa76e2009-10-14 17:02:49 +0000378/// copyMD - If metadata is attached with Instruction In1 then attach
379/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000380void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000381 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000382 MDMapTy &In1Info = MetadataStore[In1];
383 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000384 return;
385
Devang Patelebaa76e2009-10-14 17:02:49 +0000386 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000387 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000388}
Devang Patelb4034362009-09-29 20:42:25 +0000389
Devang Patel0c35dbd2009-10-20 22:50:27 +0000390/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000391/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000392MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000393 MDMapTy &Info = MetadataStore[Inst];
394 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000395 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000396
Devang Pateld5497a4b2009-09-16 18:09:00 +0000397 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
398 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000399 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000400 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000401}
402
Devang Patel0c35dbd2009-10-20 22:50:27 +0000403/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000404void MetadataContextImpl::
Chris Lattner53bb5e42009-12-28 08:14:54 +0000405getMDs(const Instruction *Inst,
406 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000407 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000408 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000409 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000410 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000411 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000412 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000413 // MD kinds are numbered from 1.
414 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000415}
416
Devang Patel09c319e2009-10-22 17:40:37 +0000417/// getHandlerNames - Populate client supplied smallvector using custome
418/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000419void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000420getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000421 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000422 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
423 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000424 // MD Handlers are numbered from 1.
425 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000426}
427
Devang Pateladd58652009-09-23 18:32:25 +0000428/// ValueIsCloned - This handler is used to update metadata store
429/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000430void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
431 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000432 // Find Metadata handles for In1.
433 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000434 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000435
436 // FIXME : Give all metadata handlers a chance to adjust.
437
438 MDMapTy &In1Info = I->second;
439 MDMapTy In2Info;
440 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000441 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000442}
Devang Patele6f26a72009-10-13 17:00:54 +0000443
444/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
445/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000446void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000447 Instruction *I1 = dyn_cast<Instruction>(V1);
448 Instruction *I2 = dyn_cast<Instruction>(V2);
449 if (!I1 || !I2)
450 return;
451
452 // FIXME : Give custom handlers a chance to override this.
453 ValueIsCloned(I1, I2);
454}
Devang Patelebaa76e2009-10-14 17:02:49 +0000455
Devang Patel1155fdf2009-10-22 19:36:54 +0000456//===----------------------------------------------------------------------===//
457// MetadataContext implementation.
458//
459MetadataContext::MetadataContext()
460 : pImpl(new MetadataContextImpl()) { }
461MetadataContext::~MetadataContext() { delete pImpl; }
462
463/// isValidName - Return true if Name is a valid custom metadata handler name.
464bool MetadataContext::isValidName(StringRef MDName) {
465 if (MDName.empty())
466 return false;
467
468 if (!isalpha(MDName[0]))
469 return false;
470
471 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
472 ++I) {
473 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
474 return false;
475 }
476 return true;
477}
478
479/// registerMDKind - Register a new metadata kind and return its ID.
480/// A metadata kind can be registered only once.
481unsigned MetadataContext::registerMDKind(StringRef Name) {
482 assert(isValidName(Name) && "Invalid custome metadata name!");
483 return pImpl->registerMDKind(Name);
484}
485
486/// getMDKind - Return metadata kind. If the requested metadata kind
487/// is not registered then return 0.
488unsigned MetadataContext::getMDKind(StringRef Name) const {
489 return pImpl->getMDKind(Name);
490}
491
492/// getMD - Get the metadata of given kind attached to an Instruction.
493/// If the metadata is not found then return 0.
494MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
495 return pImpl->getMD(Kind, Inst);
496}
497
498/// getMDs - Get the metadata attached to an Instruction.
499void MetadataContext::
500getMDs(const Instruction *Inst,
Chris Lattner53bb5e42009-12-28 08:14:54 +0000501 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Devang Patel1155fdf2009-10-22 19:36:54 +0000502 return pImpl->getMDs(Inst, MDs);
503}
504
505/// addMD - Attach the metadata of given kind to an Instruction.
506void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
507 pImpl->addMD(Kind, Node, Inst);
508}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000509
Nick Lewyckya75fe182009-11-26 23:19:05 +0000510/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000511void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
512 pImpl->removeMD(Kind, Inst);
513}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000514
Devang Patel1155fdf2009-10-22 19:36:54 +0000515/// removeAllMetadata - Remove all metadata attached with an instruction.
516void MetadataContext::removeAllMetadata(Instruction *Inst) {
517 pImpl->removeAllMetadata(Inst);
518}
519
520/// copyMD - If metadata is attached with Instruction In1 then attach
521/// the same metadata to In2.
522void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
523 pImpl->copyMD(In1, In2);
524}
525
526/// getHandlerNames - Populate client supplied smallvector using custome
527/// metadata name and ID.
528void MetadataContext::
529getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
530 pImpl->getHandlerNames(N);
531}
532
533/// ValueIsDeleted - This handler is used to update metadata store
534/// when a value is deleted.
535void MetadataContext::ValueIsDeleted(Instruction *Inst) {
536 pImpl->ValueIsDeleted(Inst);
537}
538void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
539 pImpl->ValueIsRAUWd(V1, V2);
540}
541
542/// ValueIsCloned - This handler is used to update metadata store
543/// when In1 is cloned to create In2.
544void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
545 pImpl->ValueIsCloned(In1, In2);
546}