blob: dd6a0f61348ac867ac0f60700ecbaac189fccbc2 [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;
148 LLVMContext &Context = getType()->getContext();
149 LLVMContextImpl *pImpl = Context.pImpl;
150
151 // Find value. This is a linear search, do something if it consumes
152 // lot of time. It is possible that to have multiple instances of
153 // From in this MDNode's element list.
154 SmallVector<unsigned, 4> Indexes;
155 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000156 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
157 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000158 if (V && V == From)
159 Indexes.push_back(Index);
160 }
161
162 if (Indexes.empty())
163 return;
164
165 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000166 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000167
168 // Replace From element(s) in place.
169 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
Chris Lattnerd944cf72009-12-28 09:10:16 +0000170 I != E; ++I)
171 Operands[*I].set(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000172
173 // Insert updated "this" into the context's folding node set.
174 // If a node with same element list already exist then before inserting
175 // updated "this" into the folding node set, replace all uses of existing
176 // node with updated "this" node.
177 FoldingSetNodeID ID;
178 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000179 void *InsertPoint;
180 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000181
182 if (N) {
183 N->replaceAllUsesWith(this);
184 delete N;
185 N = 0;
186 }
187
Owen Anderson5dab84c2009-10-19 20:11:52 +0000188 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
189 if (!N) {
190 // InsertPoint will have been set by the FindNodeOrInsertPos call.
191 N = this;
192 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000193 }
194}
195
Devang Patel05a26fb2009-07-29 00:33:07 +0000196//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000197// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000198//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000199static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
200 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
201}
202
Owen Anderson55f1c092009-08-13 21:58:54 +0000203NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000204 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000205 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000206 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000207 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000208
209 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
210
211 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000212 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000213 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000214
Devang Patel4a942d02009-07-29 21:58:56 +0000215 if (ParentModule)
216 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000217}
Devang Patel79238d72009-08-03 06:19:01 +0000218
Devang Patel5c310be2009-08-11 18:01:24 +0000219NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000220 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000221 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000222 Elems.reserve(NMD->getNumElements());
223
Devang Patel5c310be2009-08-11 18:01:24 +0000224 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
225 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000226 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
227 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000228}
229
Chris Lattner1bc810b2009-12-28 08:07:14 +0000230NamedMDNode::~NamedMDNode() {
231 dropAllReferences();
232 delete &getNMDOps(Operands);
233}
234
235/// getNumElements - Return number of NamedMDNode elements.
236unsigned NamedMDNode::getNumElements() const {
237 return (unsigned)getNMDOps(Operands).size();
238}
239
240/// getElement - Return specified element.
241MetadataBase *NamedMDNode::getElement(unsigned i) const {
242 assert(i < getNumElements() && "Invalid element number!");
243 return getNMDOps(Operands)[i];
244}
245
246/// addElement - Add metadata element.
247void NamedMDNode::addElement(MetadataBase *M) {
248 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
249}
250
Devang Patel79238d72009-08-03 06:19:01 +0000251/// eraseFromParent - Drop all references and remove the node from parent
252/// module.
253void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000254 getParent()->getNamedMDList().erase(this);
255}
256
257/// dropAllReferences - Remove all uses and clear node vector.
258void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000259 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000260}
261
Devang Pateld5497a4b2009-09-16 18:09:00 +0000262
263//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000264// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000265//
Devang Patel1155fdf2009-10-22 19:36:54 +0000266namespace llvm {
267class MetadataContextImpl {
268public:
269 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
270 typedef SmallVector<MDPairTy, 2> MDMapTy;
271 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
272 friend class BitcodeReader;
273private:
274
275 /// MetadataStore - Collection of metadata used in this context.
276 MDStoreTy MetadataStore;
277
278 /// MDHandlerNames - Map to hold metadata handler names.
279 StringMap<unsigned> MDHandlerNames;
280
281public:
282 /// registerMDKind - Register a new metadata kind and return its ID.
283 /// A metadata kind can be registered only once.
284 unsigned registerMDKind(StringRef Name);
285
286 /// getMDKind - Return metadata kind. If the requested metadata kind
287 /// is not registered then return 0.
288 unsigned getMDKind(StringRef Name) const;
289
290 /// getMD - Get the metadata of given kind attached to an Instruction.
291 /// If the metadata is not found then return 0.
292 MDNode *getMD(unsigned Kind, const Instruction *Inst);
293
294 /// getMDs - Get the metadata attached to an Instruction.
Chris Lattner53bb5e42009-12-28 08:14:54 +0000295 void getMDs(const Instruction *Inst,
296 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000297
298 /// addMD - Attach the metadata of given kind to an Instruction.
299 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
300
Nick Lewyckya75fe182009-11-26 23:19:05 +0000301 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000302 void removeMD(unsigned Kind, Instruction *Inst);
303
304 /// removeAllMetadata - Remove all metadata attached with an instruction.
305 void removeAllMetadata(Instruction *Inst);
306
307 /// copyMD - If metadata is attached with Instruction In1 then attach
308 /// the same metadata to In2.
309 void copyMD(Instruction *In1, Instruction *In2);
310
Nick Lewycky898e8f72009-11-26 22:54:26 +0000311 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000312 /// metadata name and ID.
313 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
314
315 /// ValueIsDeleted - This handler is used to update metadata store
316 /// when a value is deleted.
317 void ValueIsDeleted(const Value *) {}
318 void ValueIsDeleted(Instruction *Inst) {
319 removeAllMetadata(Inst);
320 }
321 void ValueIsRAUWd(Value *V1, Value *V2);
322
323 /// ValueIsCloned - This handler is used to update metadata store
324 /// when In1 is cloned to create In2.
325 void ValueIsCloned(const Instruction *In1, Instruction *In2);
326};
327}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000328
Devang Patel0c35dbd2009-10-20 22:50:27 +0000329/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000330/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000331unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000332 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000333 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
334 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000335}
336
337/// getMDKind - Return metadata kind. If the requested metadata kind
338/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000339unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000340 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000341 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000342 return 0;
343
344 return I->getValue();
345}
346
Devang Patel0c35dbd2009-10-20 22:50:27 +0000347/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000348void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
349 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000350 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000351 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000352 MDMapTy &Info = MetadataStore[Inst];
353 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000354 Info.push_back(std::make_pair(MDKind, Node));
355 MetadataStore.insert(std::make_pair(Inst, Info));
356 return;
357 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000358
Devang Patel5bf7a492009-09-29 20:30:57 +0000359 // If there is an entry for this MDKind then replace it.
360 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
361 MDPairTy &P = Info[i];
362 if (P.first == MDKind) {
363 Info[i] = std::make_pair(MDKind, Node);
364 return;
365 }
366 }
367
368 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000369 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000370}
371
Nick Lewyckya75fe182009-11-26 23:19:05 +0000372/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000373void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000374 MDStoreTy::iterator I = MetadataStore.find(Inst);
375 if (I == MetadataStore.end())
376 return;
377
378 MDMapTy &Info = I->second;
379 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
380 MDPairTy &P = *MI;
381 if (P.first == Kind) {
382 Info.erase(MI);
383 return;
384 }
385 }
Devang Patelb4034362009-09-29 20:42:25 +0000386}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000387
Devang Patel3eb5d332009-10-21 17:33:41 +0000388/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000389void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000390 MetadataStore.erase(Inst);
391 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000392}
393
Devang Patelebaa76e2009-10-14 17:02:49 +0000394/// copyMD - If metadata is attached with Instruction In1 then attach
395/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000396void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000397 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000398 MDMapTy &In1Info = MetadataStore[In1];
399 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000400 return;
401
Devang Patelebaa76e2009-10-14 17:02:49 +0000402 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000403 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000404}
Devang Patelb4034362009-09-29 20:42:25 +0000405
Devang Patel0c35dbd2009-10-20 22:50:27 +0000406/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000407/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000408MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000409 MDMapTy &Info = MetadataStore[Inst];
410 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000411 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000412
Devang Pateld5497a4b2009-09-16 18:09:00 +0000413 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
414 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000415 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000416 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000417}
418
Devang Patel0c35dbd2009-10-20 22:50:27 +0000419/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000420void MetadataContextImpl::
Chris Lattner53bb5e42009-12-28 08:14:54 +0000421getMDs(const Instruction *Inst,
422 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000423 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000424 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000425 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000426 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000427 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000428 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000429 // MD kinds are numbered from 1.
430 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000431}
432
Devang Patel09c319e2009-10-22 17:40:37 +0000433/// getHandlerNames - Populate client supplied smallvector using custome
434/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000435void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000436getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000437 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000438 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
439 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000440 // MD Handlers are numbered from 1.
441 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000442}
443
Devang Pateladd58652009-09-23 18:32:25 +0000444/// ValueIsCloned - This handler is used to update metadata store
445/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000446void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
447 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000448 // Find Metadata handles for In1.
449 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000450 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000451
452 // FIXME : Give all metadata handlers a chance to adjust.
453
454 MDMapTy &In1Info = I->second;
455 MDMapTy In2Info;
456 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000457 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000458}
Devang Patele6f26a72009-10-13 17:00:54 +0000459
460/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
461/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000462void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000463 Instruction *I1 = dyn_cast<Instruction>(V1);
464 Instruction *I2 = dyn_cast<Instruction>(V2);
465 if (!I1 || !I2)
466 return;
467
468 // FIXME : Give custom handlers a chance to override this.
469 ValueIsCloned(I1, I2);
470}
Devang Patelebaa76e2009-10-14 17:02:49 +0000471
Devang Patel1155fdf2009-10-22 19:36:54 +0000472//===----------------------------------------------------------------------===//
473// MetadataContext implementation.
474//
475MetadataContext::MetadataContext()
476 : pImpl(new MetadataContextImpl()) { }
477MetadataContext::~MetadataContext() { delete pImpl; }
478
479/// isValidName - Return true if Name is a valid custom metadata handler name.
480bool MetadataContext::isValidName(StringRef MDName) {
481 if (MDName.empty())
482 return false;
483
484 if (!isalpha(MDName[0]))
485 return false;
486
487 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
488 ++I) {
489 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
490 return false;
491 }
492 return true;
493}
494
495/// registerMDKind - Register a new metadata kind and return its ID.
496/// A metadata kind can be registered only once.
497unsigned MetadataContext::registerMDKind(StringRef Name) {
498 assert(isValidName(Name) && "Invalid custome metadata name!");
499 return pImpl->registerMDKind(Name);
500}
501
502/// getMDKind - Return metadata kind. If the requested metadata kind
503/// is not registered then return 0.
504unsigned MetadataContext::getMDKind(StringRef Name) const {
505 return pImpl->getMDKind(Name);
506}
507
508/// getMD - Get the metadata of given kind attached to an Instruction.
509/// If the metadata is not found then return 0.
510MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
511 return pImpl->getMD(Kind, Inst);
512}
513
514/// getMDs - Get the metadata attached to an Instruction.
515void MetadataContext::
516getMDs(const Instruction *Inst,
Chris Lattner53bb5e42009-12-28 08:14:54 +0000517 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Devang Patel1155fdf2009-10-22 19:36:54 +0000518 return pImpl->getMDs(Inst, MDs);
519}
520
521/// addMD - Attach the metadata of given kind to an Instruction.
522void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
523 pImpl->addMD(Kind, Node, Inst);
524}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000525
Nick Lewyckya75fe182009-11-26 23:19:05 +0000526/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000527void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
528 pImpl->removeMD(Kind, Inst);
529}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000530
Devang Patel1155fdf2009-10-22 19:36:54 +0000531/// removeAllMetadata - Remove all metadata attached with an instruction.
532void MetadataContext::removeAllMetadata(Instruction *Inst) {
533 pImpl->removeAllMetadata(Inst);
534}
535
536/// copyMD - If metadata is attached with Instruction In1 then attach
537/// the same metadata to In2.
538void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
539 pImpl->copyMD(In1, In2);
540}
541
542/// getHandlerNames - Populate client supplied smallvector using custome
543/// metadata name and ID.
544void MetadataContext::
545getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
546 pImpl->getHandlerNames(N);
547}
548
549/// ValueIsDeleted - This handler is used to update metadata store
550/// when a value is deleted.
551void MetadataContext::ValueIsDeleted(Instruction *Inst) {
552 pImpl->ValueIsDeleted(Inst);
553}
554void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
555 pImpl->ValueIsRAUWd(V1, V2);
556}
557
558/// ValueIsCloned - This handler is used to update metadata store
559/// when In1 is cloned to create In2.
560void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
561 pImpl->ValueIsCloned(In1, In2);
562}