blob: 3e24eaed60cb247431986797d2ee1a54eac676b0 [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
Owen Anderson0087fe62009-07-31 21:35:40 +000014#include "LLVMContextImpl.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000015#include "llvm/Metadata.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"
Devang Patela4f43fb2009-07-28 21:49:47 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000025// MetadataBase implementation.
Devang Pateld7fd6ab2009-08-03 22:51:10 +000026//
27
Devang Pateld7fd6ab2009-08-03 22:51:10 +000028//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000029// MDString implementation.
Owen Anderson0087fe62009-07-31 21:35:40 +000030//
Devang Pateldcb99d32009-10-22 00:10:15 +000031MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000032 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson0087fe62009-07-31 21:35:40 +000033 StringMapEntry<MDString *> &Entry =
34 pImpl->MDStringCache.GetOrCreateValue(Str);
35 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000036 if (!S) S = new MDString(Context, Entry.getKey());
37 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000038}
39
Devang Patel862ef782009-11-12 00:50:58 +000040MDString *MDString::get(LLVMContext &Context, const char *Str) {
41 LLVMContextImpl *pImpl = Context.pImpl;
42 StringMapEntry<MDString *> &Entry =
43 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
44 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000045 if (!S) new MDString(Context, Entry.getKey());
46 return S;
Devang Patel862ef782009-11-12 00:50:58 +000047}
48
Owen Anderson0087fe62009-07-31 21:35:40 +000049//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000050// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000051//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000052MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
Owen Anderson55f1c092009-08-13 21:58:54 +000053 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Devang Patel49914e6e2009-10-21 21:25:09 +000054 NodeSize = NumVals;
55 Node = new ElementVH[NodeSize];
56 ElementVH *Ptr = Node;
Devang Patel27e0be22009-10-21 23:57:35 +000057 for (unsigned i = 0; i != NumVals; ++i)
Devang Patel49914e6e2009-10-21 21:25:09 +000058 *Ptr++ = ElementVH(Vals[i], this);
Devang Patela4f43fb2009-07-28 21:49:47 +000059}
60
Devang Patelf7188322009-09-03 01:39:20 +000061void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +000062 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
63 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +000064}
65
Owen Anderson0087fe62009-07-31 21:35:40 +000066MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
Devang Patelf7188322009-09-03 01:39:20 +000067 LLVMContextImpl *pImpl = Context.pImpl;
68 FoldingSetNodeID ID;
69 for (unsigned i = 0; i != NumVals; ++i)
70 ID.AddPointer(Vals[i]);
71
Devang Patelf7188322009-09-03 01:39:20 +000072 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +000073 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000074 if (!N) {
75 // InsertPoint will have been set by the FindNodeOrInsertPos call.
76 N = new MDNode(Context, Vals, NumVals);
77 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +000078 }
Devang Patelf7188322009-09-03 01:39:20 +000079 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +000080}
81
Devang Patel27e0be22009-10-21 23:57:35 +000082/// ~MDNode - Destroy MDNode.
Devang Pateld7fd6ab2009-08-03 22:51:10 +000083MDNode::~MDNode() {
Nick Lewycky898e8f72009-11-26 22:54:26 +000084 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
85 pImpl->MDNodeSet.RemoveNode(this);
Devang Patel27e0be22009-10-21 23:57:35 +000086 delete [] Node;
87 Node = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +000088}
Devang Patel5c310be2009-08-11 18:01:24 +000089
Devang Patelf7188322009-09-03 01:39:20 +000090// Replace value from this node's element list.
91void MDNode::replaceElement(Value *From, Value *To) {
92 if (From == To || !getType())
93 return;
94 LLVMContext &Context = getType()->getContext();
95 LLVMContextImpl *pImpl = Context.pImpl;
96
97 // Find value. This is a linear search, do something if it consumes
98 // lot of time. It is possible that to have multiple instances of
99 // From in this MDNode's element list.
100 SmallVector<unsigned, 4> Indexes;
101 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000102 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
103 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000104 if (V && V == From)
105 Indexes.push_back(Index);
106 }
107
108 if (Indexes.empty())
109 return;
110
111 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000112 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000113
114 // Replace From element(s) in place.
115 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
116 I != E; ++I) {
117 unsigned Index = *I;
118 Node[Index] = ElementVH(To, this);
119 }
120
121 // Insert updated "this" into the context's folding node set.
122 // If a node with same element list already exist then before inserting
123 // updated "this" into the folding node set, replace all uses of existing
124 // node with updated "this" node.
125 FoldingSetNodeID ID;
126 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000127 void *InsertPoint;
128 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000129
130 if (N) {
131 N->replaceAllUsesWith(this);
132 delete N;
133 N = 0;
134 }
135
Owen Anderson5dab84c2009-10-19 20:11:52 +0000136 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
137 if (!N) {
138 // InsertPoint will have been set by the FindNodeOrInsertPos call.
139 N = this;
140 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000141 }
142}
143
Devang Patel05a26fb2009-07-29 00:33:07 +0000144//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000145// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000146//
Owen Anderson55f1c092009-08-13 21:58:54 +0000147NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000148 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000149 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000150 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000151 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000152
Devang Patel27e0be22009-10-21 23:57:35 +0000153 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000154 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000155
Devang Patel4a942d02009-07-29 21:58:56 +0000156 if (ParentModule)
157 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000158}
Devang Patel79238d72009-08-03 06:19:01 +0000159
Devang Patel5c310be2009-08-11 18:01:24 +0000160NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000161 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000162 SmallVector<MetadataBase *, 4> Elems;
163 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
164 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000165 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
166 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000167}
168
Devang Patel79238d72009-08-03 06:19:01 +0000169/// eraseFromParent - Drop all references and remove the node from parent
170/// module.
171void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000172 getParent()->getNamedMDList().erase(this);
173}
174
175/// dropAllReferences - Remove all uses and clear node vector.
176void NamedMDNode::dropAllReferences() {
Devang Patel79238d72009-08-03 06:19:01 +0000177 Node.clear();
178}
179
180NamedMDNode::~NamedMDNode() {
181 dropAllReferences();
182}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000183
184//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000185// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000186//
Devang Patel1155fdf2009-10-22 19:36:54 +0000187namespace llvm {
188class MetadataContextImpl {
189public:
190 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
191 typedef SmallVector<MDPairTy, 2> MDMapTy;
192 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
193 friend class BitcodeReader;
194private:
195
196 /// MetadataStore - Collection of metadata used in this context.
197 MDStoreTy MetadataStore;
198
199 /// MDHandlerNames - Map to hold metadata handler names.
200 StringMap<unsigned> MDHandlerNames;
201
202public:
203 /// registerMDKind - Register a new metadata kind and return its ID.
204 /// A metadata kind can be registered only once.
205 unsigned registerMDKind(StringRef Name);
206
207 /// getMDKind - Return metadata kind. If the requested metadata kind
208 /// is not registered then return 0.
209 unsigned getMDKind(StringRef Name) const;
210
211 /// getMD - Get the metadata of given kind attached to an Instruction.
212 /// If the metadata is not found then return 0.
213 MDNode *getMD(unsigned Kind, const Instruction *Inst);
214
215 /// getMDs - Get the metadata attached to an Instruction.
216 void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
217
218 /// addMD - Attach the metadata of given kind to an Instruction.
219 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
220
221 /// removeMD - Remove metadata of given kind attached with an instuction.
222 void removeMD(unsigned Kind, Instruction *Inst);
223
224 /// removeAllMetadata - Remove all metadata attached with an instruction.
225 void removeAllMetadata(Instruction *Inst);
226
227 /// copyMD - If metadata is attached with Instruction In1 then attach
228 /// the same metadata to In2.
229 void copyMD(Instruction *In1, Instruction *In2);
230
Nick Lewycky898e8f72009-11-26 22:54:26 +0000231 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000232 /// metadata name and ID.
233 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
234
235 /// ValueIsDeleted - This handler is used to update metadata store
236 /// when a value is deleted.
237 void ValueIsDeleted(const Value *) {}
238 void ValueIsDeleted(Instruction *Inst) {
239 removeAllMetadata(Inst);
240 }
241 void ValueIsRAUWd(Value *V1, Value *V2);
242
243 /// ValueIsCloned - This handler is used to update metadata store
244 /// when In1 is cloned to create In2.
245 void ValueIsCloned(const Instruction *In1, Instruction *In2);
246};
247}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000248
Devang Patel0c35dbd2009-10-20 22:50:27 +0000249/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000250/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000251unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000252 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000253 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
254 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000255}
256
257/// getMDKind - Return metadata kind. If the requested metadata kind
258/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000259unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000260 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000261 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000262 return 0;
263
264 return I->getValue();
265}
266
Devang Patel0c35dbd2009-10-20 22:50:27 +0000267/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000268void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
269 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000270 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000271 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000272 MDMapTy &Info = MetadataStore[Inst];
273 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000274 Info.push_back(std::make_pair(MDKind, Node));
275 MetadataStore.insert(std::make_pair(Inst, Info));
276 return;
277 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000278
Devang Patel5bf7a492009-09-29 20:30:57 +0000279 // If there is an entry for this MDKind then replace it.
280 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
281 MDPairTy &P = Info[i];
282 if (P.first == MDKind) {
283 Info[i] = std::make_pair(MDKind, Node);
284 return;
285 }
286 }
287
288 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000289 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000290}
291
Devang Patelb4034362009-09-29 20:42:25 +0000292/// removeMD - Remove metadata of given kind attached with an instuction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000293void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000294 MDStoreTy::iterator I = MetadataStore.find(Inst);
295 if (I == MetadataStore.end())
296 return;
297
298 MDMapTy &Info = I->second;
299 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
300 MDPairTy &P = *MI;
301 if (P.first == Kind) {
302 Info.erase(MI);
303 return;
304 }
305 }
Devang Patelb4034362009-09-29 20:42:25 +0000306}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000307
Devang Patel3eb5d332009-10-21 17:33:41 +0000308/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000309void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000310 MetadataStore.erase(Inst);
311 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000312}
313
Devang Patelebaa76e2009-10-14 17:02:49 +0000314/// copyMD - If metadata is attached with Instruction In1 then attach
315/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000316void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000317 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000318 MDMapTy &In1Info = MetadataStore[In1];
319 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000320 return;
321
Devang Patelebaa76e2009-10-14 17:02:49 +0000322 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000323 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000324}
Devang Patelb4034362009-09-29 20:42:25 +0000325
Devang Patel0c35dbd2009-10-20 22:50:27 +0000326/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000327/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000328MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000329 MDMapTy &Info = MetadataStore[Inst];
330 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000331 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000332
Devang Pateld5497a4b2009-09-16 18:09:00 +0000333 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
334 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000335 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000336 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000337}
338
Devang Patel0c35dbd2009-10-20 22:50:27 +0000339/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000340void MetadataContextImpl::
Devang Patel6da5dbf2009-10-22 18:55:16 +0000341getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000342 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000343 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000344 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000345 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000346 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000347 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000348 // MD kinds are numbered from 1.
349 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000350}
351
Devang Patel09c319e2009-10-22 17:40:37 +0000352/// getHandlerNames - Populate client supplied smallvector using custome
353/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000354void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000355getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000356 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000357 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
358 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000359 // MD Handlers are numbered from 1.
360 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000361}
362
Devang Pateladd58652009-09-23 18:32:25 +0000363/// ValueIsCloned - This handler is used to update metadata store
364/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000365void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
366 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000367 // Find Metadata handles for In1.
368 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000369 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000370
371 // FIXME : Give all metadata handlers a chance to adjust.
372
373 MDMapTy &In1Info = I->second;
374 MDMapTy In2Info;
375 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000376 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000377}
Devang Patele6f26a72009-10-13 17:00:54 +0000378
379/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
380/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000381void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000382 Instruction *I1 = dyn_cast<Instruction>(V1);
383 Instruction *I2 = dyn_cast<Instruction>(V2);
384 if (!I1 || !I2)
385 return;
386
387 // FIXME : Give custom handlers a chance to override this.
388 ValueIsCloned(I1, I2);
389}
Devang Patelebaa76e2009-10-14 17:02:49 +0000390
Devang Patel1155fdf2009-10-22 19:36:54 +0000391//===----------------------------------------------------------------------===//
392// MetadataContext implementation.
393//
394MetadataContext::MetadataContext()
395 : pImpl(new MetadataContextImpl()) { }
396MetadataContext::~MetadataContext() { delete pImpl; }
397
398/// isValidName - Return true if Name is a valid custom metadata handler name.
399bool MetadataContext::isValidName(StringRef MDName) {
400 if (MDName.empty())
401 return false;
402
403 if (!isalpha(MDName[0]))
404 return false;
405
406 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
407 ++I) {
408 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
409 return false;
410 }
411 return true;
412}
413
414/// registerMDKind - Register a new metadata kind and return its ID.
415/// A metadata kind can be registered only once.
416unsigned MetadataContext::registerMDKind(StringRef Name) {
417 assert(isValidName(Name) && "Invalid custome metadata name!");
418 return pImpl->registerMDKind(Name);
419}
420
421/// getMDKind - Return metadata kind. If the requested metadata kind
422/// is not registered then return 0.
423unsigned MetadataContext::getMDKind(StringRef Name) const {
424 return pImpl->getMDKind(Name);
425}
426
427/// getMD - Get the metadata of given kind attached to an Instruction.
428/// If the metadata is not found then return 0.
429MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
430 return pImpl->getMD(Kind, Inst);
431}
432
433/// getMDs - Get the metadata attached to an Instruction.
434void MetadataContext::
435getMDs(const Instruction *Inst,
436 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
437 return pImpl->getMDs(Inst, MDs);
438}
439
440/// addMD - Attach the metadata of given kind to an Instruction.
441void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
442 pImpl->addMD(Kind, Node, Inst);
443}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000444
Devang Patel1155fdf2009-10-22 19:36:54 +0000445/// removeMD - Remove metadata of given kind attached with an instuction.
446void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
447 pImpl->removeMD(Kind, Inst);
448}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000449
Devang Patel1155fdf2009-10-22 19:36:54 +0000450/// removeAllMetadata - Remove all metadata attached with an instruction.
451void MetadataContext::removeAllMetadata(Instruction *Inst) {
452 pImpl->removeAllMetadata(Inst);
453}
454
455/// copyMD - If metadata is attached with Instruction In1 then attach
456/// the same metadata to In2.
457void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
458 pImpl->copyMD(In1, In2);
459}
460
461/// getHandlerNames - Populate client supplied smallvector using custome
462/// metadata name and ID.
463void MetadataContext::
464getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
465 pImpl->getHandlerNames(N);
466}
467
468/// ValueIsDeleted - This handler is used to update metadata store
469/// when a value is deleted.
470void MetadataContext::ValueIsDeleted(Instruction *Inst) {
471 pImpl->ValueIsDeleted(Inst);
472}
473void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
474 pImpl->ValueIsRAUWd(V1, V2);
475}
476
477/// ValueIsCloned - This handler is used to update metadata store
478/// when In1 is cloned to create In2.
479void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
480 pImpl->ValueIsCloned(In1, In2);
481}