blob: 4fadfed503be03e5fb14aa0cfc411188e404590c [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();
Chris Lattnerb0c23e82009-10-19 07:10:59 +000036 if (S) return S;
37
Devang Patel6746d432009-10-22 00:22:05 +000038 return S =
Devang Patel084679e2009-10-22 18:25:28 +000039 new MDString(Context, Entry.getKey());
Owen Anderson0087fe62009-07-31 21:35:40 +000040}
41
42//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000043// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000044//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000045MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
Owen Anderson55f1c092009-08-13 21:58:54 +000046 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Devang Patel49914e6e2009-10-21 21:25:09 +000047 NodeSize = NumVals;
48 Node = new ElementVH[NodeSize];
49 ElementVH *Ptr = Node;
Devang Patel27e0be22009-10-21 23:57:35 +000050 for (unsigned i = 0; i != NumVals; ++i)
Devang Patel49914e6e2009-10-21 21:25:09 +000051 *Ptr++ = ElementVH(Vals[i], this);
Devang Patela4f43fb2009-07-28 21:49:47 +000052}
53
Devang Patelf7188322009-09-03 01:39:20 +000054void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +000055 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
56 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +000057}
58
Owen Anderson0087fe62009-07-31 21:35:40 +000059MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
Devang Patelf7188322009-09-03 01:39:20 +000060 LLVMContextImpl *pImpl = Context.pImpl;
61 FoldingSetNodeID ID;
62 for (unsigned i = 0; i != NumVals; ++i)
63 ID.AddPointer(Vals[i]);
64
Devang Patelf7188322009-09-03 01:39:20 +000065 void *InsertPoint;
Chris Lattnerb0c23e82009-10-19 07:10:59 +000066 MDNode *N;
67 {
Devang Patelf7188322009-09-03 01:39:20 +000068 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000069 }
70 if (N) return N;
71
Chris Lattnerb0c23e82009-10-19 07:10:59 +000072 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
73 if (!N) {
74 // InsertPoint will have been set by the FindNodeOrInsertPos call.
75 N = new MDNode(Context, Vals, NumVals);
76 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +000077 }
78
79 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() {
Devang Patelcbde0732009-09-09 17:44:26 +000084 {
85 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
Devang Patelcbde0732009-09-09 17:44:26 +000086 pImpl->MDNodeSet.RemoveNode(this);
87 }
Devang Patel27e0be22009-10-21 23:57:35 +000088 delete [] Node;
89 Node = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +000090}
Devang Patel5c310be2009-08-11 18:01:24 +000091
Devang Patelf7188322009-09-03 01:39:20 +000092// Replace value from this node's element list.
93void MDNode::replaceElement(Value *From, Value *To) {
94 if (From == To || !getType())
95 return;
96 LLVMContext &Context = getType()->getContext();
97 LLVMContextImpl *pImpl = Context.pImpl;
98
99 // Find value. This is a linear search, do something if it consumes
100 // lot of time. It is possible that to have multiple instances of
101 // From in this MDNode's element list.
102 SmallVector<unsigned, 4> Indexes;
103 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000104 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
105 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000106 if (V && V == From)
107 Indexes.push_back(Index);
108 }
109
110 if (Indexes.empty())
111 return;
112
113 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000114 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000115
116 // Replace From element(s) in place.
117 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
118 I != E; ++I) {
119 unsigned Index = *I;
120 Node[Index] = ElementVH(To, this);
121 }
122
123 // Insert updated "this" into the context's folding node set.
124 // If a node with same element list already exist then before inserting
125 // updated "this" into the folding node set, replace all uses of existing
126 // node with updated "this" node.
127 FoldingSetNodeID ID;
128 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000129 void *InsertPoint;
130 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000131
132 if (N) {
133 N->replaceAllUsesWith(this);
134 delete N;
135 N = 0;
136 }
137
Owen Anderson5dab84c2009-10-19 20:11:52 +0000138 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
139 if (!N) {
140 // InsertPoint will have been set by the FindNodeOrInsertPos call.
141 N = this;
142 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000143 }
144}
145
Devang Patel05a26fb2009-07-29 00:33:07 +0000146//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000147// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000148//
Owen Anderson55f1c092009-08-13 21:58:54 +0000149NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000150 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000151 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000152 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000153 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000154
Devang Patel27e0be22009-10-21 23:57:35 +0000155 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000156 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000157
Devang Patel4a942d02009-07-29 21:58:56 +0000158 if (ParentModule)
159 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000160}
Devang Patel79238d72009-08-03 06:19:01 +0000161
Devang Patel5c310be2009-08-11 18:01:24 +0000162NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000163 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000164 SmallVector<MetadataBase *, 4> Elems;
165 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
166 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000167 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
168 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000169}
170
Devang Patel79238d72009-08-03 06:19:01 +0000171/// eraseFromParent - Drop all references and remove the node from parent
172/// module.
173void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000174 getParent()->getNamedMDList().erase(this);
175}
176
177/// dropAllReferences - Remove all uses and clear node vector.
178void NamedMDNode::dropAllReferences() {
Devang Patel79238d72009-08-03 06:19:01 +0000179 Node.clear();
180}
181
182NamedMDNode::~NamedMDNode() {
183 dropAllReferences();
184}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000185
186//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000187// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000188//
Devang Patel1155fdf2009-10-22 19:36:54 +0000189namespace llvm {
190class MetadataContextImpl {
191public:
192 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
193 typedef SmallVector<MDPairTy, 2> MDMapTy;
194 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
195 friend class BitcodeReader;
196private:
197
198 /// MetadataStore - Collection of metadata used in this context.
199 MDStoreTy MetadataStore;
200
201 /// MDHandlerNames - Map to hold metadata handler names.
202 StringMap<unsigned> MDHandlerNames;
203
204public:
205 /// registerMDKind - Register a new metadata kind and return its ID.
206 /// A metadata kind can be registered only once.
207 unsigned registerMDKind(StringRef Name);
208
209 /// getMDKind - Return metadata kind. If the requested metadata kind
210 /// is not registered then return 0.
211 unsigned getMDKind(StringRef Name) const;
212
213 /// getMD - Get the metadata of given kind attached to an Instruction.
214 /// If the metadata is not found then return 0.
215 MDNode *getMD(unsigned Kind, const Instruction *Inst);
216
217 /// getMDs - Get the metadata attached to an Instruction.
218 void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
219
220 /// addMD - Attach the metadata of given kind to an Instruction.
221 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
222
223 /// removeMD - Remove metadata of given kind attached with an instuction.
224 void removeMD(unsigned Kind, Instruction *Inst);
225
226 /// removeAllMetadata - Remove all metadata attached with an instruction.
227 void removeAllMetadata(Instruction *Inst);
228
229 /// copyMD - If metadata is attached with Instruction In1 then attach
230 /// the same metadata to In2.
231 void copyMD(Instruction *In1, Instruction *In2);
232
233 /// getHandlerNames - Populate client supplied smallvector using custome
234 /// metadata name and ID.
235 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
236
237 /// ValueIsDeleted - This handler is used to update metadata store
238 /// when a value is deleted.
239 void ValueIsDeleted(const Value *) {}
240 void ValueIsDeleted(Instruction *Inst) {
241 removeAllMetadata(Inst);
242 }
243 void ValueIsRAUWd(Value *V1, Value *V2);
244
245 /// ValueIsCloned - This handler is used to update metadata store
246 /// when In1 is cloned to create In2.
247 void ValueIsCloned(const Instruction *In1, Instruction *In2);
248};
249}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000250
Devang Patel0c35dbd2009-10-20 22:50:27 +0000251/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000252/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000253unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000254 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000255 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
256 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000257}
258
259/// getMDKind - Return metadata kind. If the requested metadata kind
260/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000261unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000262 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000263 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000264 return 0;
265
266 return I->getValue();
267}
268
Devang Patel0c35dbd2009-10-20 22:50:27 +0000269/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000270void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
271 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000272 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000273 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000274 MDMapTy &Info = MetadataStore[Inst];
275 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000276 Info.push_back(std::make_pair(MDKind, Node));
277 MetadataStore.insert(std::make_pair(Inst, Info));
278 return;
279 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000280
Devang Patel5bf7a492009-09-29 20:30:57 +0000281 // If there is an entry for this MDKind then replace it.
282 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
283 MDPairTy &P = Info[i];
284 if (P.first == MDKind) {
285 Info[i] = std::make_pair(MDKind, Node);
286 return;
287 }
288 }
289
290 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000291 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000292}
293
Devang Patelb4034362009-09-29 20:42:25 +0000294/// removeMD - Remove metadata of given kind attached with an instuction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000295void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000296 MDStoreTy::iterator I = MetadataStore.find(Inst);
297 if (I == MetadataStore.end())
298 return;
299
300 MDMapTy &Info = I->second;
301 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
302 MDPairTy &P = *MI;
303 if (P.first == Kind) {
304 Info.erase(MI);
305 return;
306 }
307 }
Devang Patelb4034362009-09-29 20:42:25 +0000308}
309
Devang Patel3eb5d332009-10-21 17:33:41 +0000310/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000311void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000312 MetadataStore.erase(Inst);
313 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000314}
315
Devang Patelebaa76e2009-10-14 17:02:49 +0000316/// copyMD - If metadata is attached with Instruction In1 then attach
317/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000318void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000319 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000320 MDMapTy &In1Info = MetadataStore[In1];
321 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000322 return;
323
Devang Patelebaa76e2009-10-14 17:02:49 +0000324 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000325 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000326}
Devang Patelb4034362009-09-29 20:42:25 +0000327
Devang Patel0c35dbd2009-10-20 22:50:27 +0000328/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000329/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000330MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000331 MDMapTy &Info = MetadataStore[Inst];
332 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000333 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000334
Devang Pateld5497a4b2009-09-16 18:09:00 +0000335 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
336 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000337 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000338 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000339}
340
Devang Patel0c35dbd2009-10-20 22:50:27 +0000341/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000342void MetadataContextImpl::
Devang Patel6da5dbf2009-10-22 18:55:16 +0000343getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
Devang Pateldec23fd2009-09-16 20:21:17 +0000344 MDStoreTy::iterator I = MetadataStore.find(Inst);
345 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000346 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000347 MDs.resize(I->second.size());
Devang Patel6da5dbf2009-10-22 18:55:16 +0000348 for (MDMapTy::iterator MI = I->second.begin(), ME = I->second.end();
349 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000350 // MD kinds are numbered from 1.
351 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000352}
353
Devang Patel09c319e2009-10-22 17:40:37 +0000354/// getHandlerNames - Populate client supplied smallvector using custome
355/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000356void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000357getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000358 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000359 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
360 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000361 // MD Handlers are numbered from 1.
362 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000363}
364
Devang Pateladd58652009-09-23 18:32:25 +0000365/// ValueIsCloned - This handler is used to update metadata store
366/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000367void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
368 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000369 // Find Metadata handles for In1.
370 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000371 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000372
373 // FIXME : Give all metadata handlers a chance to adjust.
374
375 MDMapTy &In1Info = I->second;
376 MDMapTy In2Info;
377 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000378 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000379}
Devang Patele6f26a72009-10-13 17:00:54 +0000380
381/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
382/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000383void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000384 Instruction *I1 = dyn_cast<Instruction>(V1);
385 Instruction *I2 = dyn_cast<Instruction>(V2);
386 if (!I1 || !I2)
387 return;
388
389 // FIXME : Give custom handlers a chance to override this.
390 ValueIsCloned(I1, I2);
391}
Devang Patelebaa76e2009-10-14 17:02:49 +0000392
Devang Patel1155fdf2009-10-22 19:36:54 +0000393//===----------------------------------------------------------------------===//
394// MetadataContext implementation.
395//
396MetadataContext::MetadataContext()
397 : pImpl(new MetadataContextImpl()) { }
398MetadataContext::~MetadataContext() { delete pImpl; }
399
400/// isValidName - Return true if Name is a valid custom metadata handler name.
401bool MetadataContext::isValidName(StringRef MDName) {
402 if (MDName.empty())
403 return false;
404
405 if (!isalpha(MDName[0]))
406 return false;
407
408 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
409 ++I) {
410 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
411 return false;
412 }
413 return true;
414}
415
416/// registerMDKind - Register a new metadata kind and return its ID.
417/// A metadata kind can be registered only once.
418unsigned MetadataContext::registerMDKind(StringRef Name) {
419 assert(isValidName(Name) && "Invalid custome metadata name!");
420 return pImpl->registerMDKind(Name);
421}
422
423/// getMDKind - Return metadata kind. If the requested metadata kind
424/// is not registered then return 0.
425unsigned MetadataContext::getMDKind(StringRef Name) const {
426 return pImpl->getMDKind(Name);
427}
428
429/// getMD - Get the metadata of given kind attached to an Instruction.
430/// If the metadata is not found then return 0.
431MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
432 return pImpl->getMD(Kind, Inst);
433}
434
435/// getMDs - Get the metadata attached to an Instruction.
436void MetadataContext::
437getMDs(const Instruction *Inst,
438 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
439 return pImpl->getMDs(Inst, MDs);
440}
441
442/// addMD - Attach the metadata of given kind to an Instruction.
443void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
444 pImpl->addMD(Kind, Node, Inst);
445}
446
447/// removeMD - Remove metadata of given kind attached with an instuction.
448void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
449 pImpl->removeMD(Kind, Inst);
450}
451
452/// removeAllMetadata - Remove all metadata attached with an instruction.
453void MetadataContext::removeAllMetadata(Instruction *Inst) {
454 pImpl->removeAllMetadata(Inst);
455}
456
457/// copyMD - If metadata is attached with Instruction In1 then attach
458/// the same metadata to In2.
459void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
460 pImpl->copyMD(In1, In2);
461}
462
463/// getHandlerNames - Populate client supplied smallvector using custome
464/// metadata name and ID.
465void MetadataContext::
466getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
467 pImpl->getHandlerNames(N);
468}
469
470/// ValueIsDeleted - This handler is used to update metadata store
471/// when a value is deleted.
472void MetadataContext::ValueIsDeleted(Instruction *Inst) {
473 pImpl->ValueIsDeleted(Inst);
474}
475void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
476 pImpl->ValueIsRAUWd(V1, V2);
477}
478
479/// ValueIsCloned - This handler is used to update metadata store
480/// when In1 is cloned to create In2.
481void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
482 pImpl->ValueIsCloned(In1, In2);
483}