blob: ac91a40e10db2bb08251fa8f866e3ef52ced441d [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 Lewycky6e052512009-11-27 19:57:53 +000045 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000046 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//
Victor Hernandezdd7418a2009-12-16 02:52:09 +000052MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez2003b902009-12-16 08:10:57 +000053 Function *LocalFunc)
Owen Anderson55f1c092009-08-13 21:58:54 +000054 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Devang Patel49914e6e2009-10-21 21:25:09 +000055 NodeSize = NumVals;
56 Node = new ElementVH[NodeSize];
57 ElementVH *Ptr = Node;
Devang Patel27e0be22009-10-21 23:57:35 +000058 for (unsigned i = 0; i != NumVals; ++i)
Devang Patel49914e6e2009-10-21 21:25:09 +000059 *Ptr++ = ElementVH(Vals[i], this);
Victor Hernandez2003b902009-12-16 08:10:57 +000060 LocalFunction = LocalFunc;
Devang Patela4f43fb2009-07-28 21:49:47 +000061}
62
Devang Patelf7188322009-09-03 01:39:20 +000063void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +000064 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
65 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +000066}
67
Victor Hernandezdd7418a2009-12-16 02:52:09 +000068MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
69 Function *LocalFunction) {
Devang Patelf7188322009-09-03 01:39:20 +000070 LLVMContextImpl *pImpl = Context.pImpl;
71 FoldingSetNodeID ID;
72 for (unsigned i = 0; i != NumVals; ++i)
73 ID.AddPointer(Vals[i]);
Victor Hernandezdd7418a2009-12-16 02:52:09 +000074 if (LocalFunction)
75 ID.AddPointer(LocalFunction);
Devang Patelf7188322009-09-03 01:39:20 +000076
Devang Patelf7188322009-09-03 01:39:20 +000077 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +000078 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000079 if (!N) {
80 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandezdd7418a2009-12-16 02:52:09 +000081 N = new MDNode(Context, Vals, NumVals, LocalFunction);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000082 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +000083 }
Devang Patelf7188322009-09-03 01:39:20 +000084 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +000085}
86
Devang Patel27e0be22009-10-21 23:57:35 +000087/// ~MDNode - Destroy MDNode.
Devang Pateld7fd6ab2009-08-03 22:51:10 +000088MDNode::~MDNode() {
Nick Lewycky898e8f72009-11-26 22:54:26 +000089 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
90 pImpl->MDNodeSet.RemoveNode(this);
Devang Patel27e0be22009-10-21 23:57:35 +000091 delete [] Node;
92 Node = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +000093}
Devang Patel5c310be2009-08-11 18:01:24 +000094
Devang Patelf7188322009-09-03 01:39:20 +000095// Replace value from this node's element list.
96void MDNode::replaceElement(Value *From, Value *To) {
97 if (From == To || !getType())
98 return;
99 LLVMContext &Context = getType()->getContext();
100 LLVMContextImpl *pImpl = Context.pImpl;
101
102 // Find value. This is a linear search, do something if it consumes
103 // lot of time. It is possible that to have multiple instances of
104 // From in this MDNode's element list.
105 SmallVector<unsigned, 4> Indexes;
106 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000107 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
108 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000109 if (V && V == From)
110 Indexes.push_back(Index);
111 }
112
113 if (Indexes.empty())
114 return;
115
116 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000117 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000118
119 // Replace From element(s) in place.
120 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
121 I != E; ++I) {
122 unsigned Index = *I;
123 Node[Index] = ElementVH(To, this);
124 }
125
126 // Insert updated "this" into the context's folding node set.
127 // If a node with same element list already exist then before inserting
128 // updated "this" into the folding node set, replace all uses of existing
129 // node with updated "this" node.
130 FoldingSetNodeID ID;
131 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000132 void *InsertPoint;
133 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000134
135 if (N) {
136 N->replaceAllUsesWith(this);
137 delete N;
138 N = 0;
139 }
140
Owen Anderson5dab84c2009-10-19 20:11:52 +0000141 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
142 if (!N) {
143 // InsertPoint will have been set by the FindNodeOrInsertPos call.
144 N = this;
145 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000146 }
147}
148
Devang Patel05a26fb2009-07-29 00:33:07 +0000149//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000150// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000151//
Owen Anderson55f1c092009-08-13 21:58:54 +0000152NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000153 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000154 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000155 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000156 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000157
Devang Patel27e0be22009-10-21 23:57:35 +0000158 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000159 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000160
Devang Patel4a942d02009-07-29 21:58:56 +0000161 if (ParentModule)
162 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000163}
Devang Patel79238d72009-08-03 06:19:01 +0000164
Devang Patel5c310be2009-08-11 18:01:24 +0000165NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000166 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000167 SmallVector<MetadataBase *, 4> Elems;
168 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
169 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000170 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
171 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000172}
173
Devang Patel79238d72009-08-03 06:19:01 +0000174/// eraseFromParent - Drop all references and remove the node from parent
175/// module.
176void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000177 getParent()->getNamedMDList().erase(this);
178}
179
180/// dropAllReferences - Remove all uses and clear node vector.
181void NamedMDNode::dropAllReferences() {
Devang Patel79238d72009-08-03 06:19:01 +0000182 Node.clear();
183}
184
185NamedMDNode::~NamedMDNode() {
186 dropAllReferences();
187}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000188
189//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000190// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000191//
Devang Patel1155fdf2009-10-22 19:36:54 +0000192namespace llvm {
193class MetadataContextImpl {
194public:
195 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
196 typedef SmallVector<MDPairTy, 2> MDMapTy;
197 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
198 friend class BitcodeReader;
199private:
200
201 /// MetadataStore - Collection of metadata used in this context.
202 MDStoreTy MetadataStore;
203
204 /// MDHandlerNames - Map to hold metadata handler names.
205 StringMap<unsigned> MDHandlerNames;
206
207public:
208 /// registerMDKind - Register a new metadata kind and return its ID.
209 /// A metadata kind can be registered only once.
210 unsigned registerMDKind(StringRef Name);
211
212 /// getMDKind - Return metadata kind. If the requested metadata kind
213 /// is not registered then return 0.
214 unsigned getMDKind(StringRef Name) const;
215
216 /// getMD - Get the metadata of given kind attached to an Instruction.
217 /// If the metadata is not found then return 0.
218 MDNode *getMD(unsigned Kind, const Instruction *Inst);
219
220 /// getMDs - Get the metadata attached to an Instruction.
221 void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
222
223 /// addMD - Attach the metadata of given kind to an Instruction.
224 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
225
Nick Lewyckya75fe182009-11-26 23:19:05 +0000226 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000227 void removeMD(unsigned Kind, Instruction *Inst);
228
229 /// removeAllMetadata - Remove all metadata attached with an instruction.
230 void removeAllMetadata(Instruction *Inst);
231
232 /// copyMD - If metadata is attached with Instruction In1 then attach
233 /// the same metadata to In2.
234 void copyMD(Instruction *In1, Instruction *In2);
235
Nick Lewycky898e8f72009-11-26 22:54:26 +0000236 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000237 /// metadata name and ID.
238 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
239
240 /// ValueIsDeleted - This handler is used to update metadata store
241 /// when a value is deleted.
242 void ValueIsDeleted(const Value *) {}
243 void ValueIsDeleted(Instruction *Inst) {
244 removeAllMetadata(Inst);
245 }
246 void ValueIsRAUWd(Value *V1, Value *V2);
247
248 /// ValueIsCloned - This handler is used to update metadata store
249 /// when In1 is cloned to create In2.
250 void ValueIsCloned(const Instruction *In1, Instruction *In2);
251};
252}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000253
Devang Patel0c35dbd2009-10-20 22:50:27 +0000254/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000255/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000256unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000257 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000258 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
259 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000260}
261
262/// getMDKind - Return metadata kind. If the requested metadata kind
263/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000264unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000265 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000266 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000267 return 0;
268
269 return I->getValue();
270}
271
Devang Patel0c35dbd2009-10-20 22:50:27 +0000272/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000273void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
274 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000275 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000276 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000277 MDMapTy &Info = MetadataStore[Inst];
278 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000279 Info.push_back(std::make_pair(MDKind, Node));
280 MetadataStore.insert(std::make_pair(Inst, Info));
281 return;
282 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000283
Devang Patel5bf7a492009-09-29 20:30:57 +0000284 // If there is an entry for this MDKind then replace it.
285 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
286 MDPairTy &P = Info[i];
287 if (P.first == MDKind) {
288 Info[i] = std::make_pair(MDKind, Node);
289 return;
290 }
291 }
292
293 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000294 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000295}
296
Nick Lewyckya75fe182009-11-26 23:19:05 +0000297/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000298void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000299 MDStoreTy::iterator I = MetadataStore.find(Inst);
300 if (I == MetadataStore.end())
301 return;
302
303 MDMapTy &Info = I->second;
304 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
305 MDPairTy &P = *MI;
306 if (P.first == Kind) {
307 Info.erase(MI);
308 return;
309 }
310 }
Devang Patelb4034362009-09-29 20:42:25 +0000311}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000312
Devang Patel3eb5d332009-10-21 17:33:41 +0000313/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000314void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000315 MetadataStore.erase(Inst);
316 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000317}
318
Devang Patelebaa76e2009-10-14 17:02:49 +0000319/// copyMD - If metadata is attached with Instruction In1 then attach
320/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000321void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000322 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000323 MDMapTy &In1Info = MetadataStore[In1];
324 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000325 return;
326
Devang Patelebaa76e2009-10-14 17:02:49 +0000327 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000328 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000329}
Devang Patelb4034362009-09-29 20:42:25 +0000330
Devang Patel0c35dbd2009-10-20 22:50:27 +0000331/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000332/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000333MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000334 MDMapTy &Info = MetadataStore[Inst];
335 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000336 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000337
Devang Pateld5497a4b2009-09-16 18:09:00 +0000338 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
339 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000340 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000341 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000342}
343
Devang Patel0c35dbd2009-10-20 22:50:27 +0000344/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000345void MetadataContextImpl::
Devang Patel6da5dbf2009-10-22 18:55:16 +0000346getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000347 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000348 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000349 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000350 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000351 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000352 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000353 // MD kinds are numbered from 1.
354 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000355}
356
Devang Patel09c319e2009-10-22 17:40:37 +0000357/// getHandlerNames - Populate client supplied smallvector using custome
358/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000359void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000360getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000361 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000362 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
363 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000364 // MD Handlers are numbered from 1.
365 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000366}
367
Devang Pateladd58652009-09-23 18:32:25 +0000368/// ValueIsCloned - This handler is used to update metadata store
369/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000370void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
371 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000372 // Find Metadata handles for In1.
373 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000374 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000375
376 // FIXME : Give all metadata handlers a chance to adjust.
377
378 MDMapTy &In1Info = I->second;
379 MDMapTy In2Info;
380 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000381 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000382}
Devang Patele6f26a72009-10-13 17:00:54 +0000383
384/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
385/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000386void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000387 Instruction *I1 = dyn_cast<Instruction>(V1);
388 Instruction *I2 = dyn_cast<Instruction>(V2);
389 if (!I1 || !I2)
390 return;
391
392 // FIXME : Give custom handlers a chance to override this.
393 ValueIsCloned(I1, I2);
394}
Devang Patelebaa76e2009-10-14 17:02:49 +0000395
Devang Patel1155fdf2009-10-22 19:36:54 +0000396//===----------------------------------------------------------------------===//
397// MetadataContext implementation.
398//
399MetadataContext::MetadataContext()
400 : pImpl(new MetadataContextImpl()) { }
401MetadataContext::~MetadataContext() { delete pImpl; }
402
403/// isValidName - Return true if Name is a valid custom metadata handler name.
404bool MetadataContext::isValidName(StringRef MDName) {
405 if (MDName.empty())
406 return false;
407
408 if (!isalpha(MDName[0]))
409 return false;
410
411 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
412 ++I) {
413 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
414 return false;
415 }
416 return true;
417}
418
419/// registerMDKind - Register a new metadata kind and return its ID.
420/// A metadata kind can be registered only once.
421unsigned MetadataContext::registerMDKind(StringRef Name) {
422 assert(isValidName(Name) && "Invalid custome metadata name!");
423 return pImpl->registerMDKind(Name);
424}
425
426/// getMDKind - Return metadata kind. If the requested metadata kind
427/// is not registered then return 0.
428unsigned MetadataContext::getMDKind(StringRef Name) const {
429 return pImpl->getMDKind(Name);
430}
431
432/// getMD - Get the metadata of given kind attached to an Instruction.
433/// If the metadata is not found then return 0.
434MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
435 return pImpl->getMD(Kind, Inst);
436}
437
438/// getMDs - Get the metadata attached to an Instruction.
439void MetadataContext::
440getMDs(const Instruction *Inst,
441 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
442 return pImpl->getMDs(Inst, MDs);
443}
444
445/// addMD - Attach the metadata of given kind to an Instruction.
446void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
447 pImpl->addMD(Kind, Node, Inst);
448}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000449
Nick Lewyckya75fe182009-11-26 23:19:05 +0000450/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000451void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
452 pImpl->removeMD(Kind, Inst);
453}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000454
Devang Patel1155fdf2009-10-22 19:36:54 +0000455/// removeAllMetadata - Remove all metadata attached with an instruction.
456void MetadataContext::removeAllMetadata(Instruction *Inst) {
457 pImpl->removeAllMetadata(Inst);
458}
459
460/// copyMD - If metadata is attached with Instruction In1 then attach
461/// the same metadata to In2.
462void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
463 pImpl->copyMD(In1, In2);
464}
465
466/// getHandlerNames - Populate client supplied smallvector using custome
467/// metadata name and ID.
468void MetadataContext::
469getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
470 pImpl->getHandlerNames(N);
471}
472
473/// ValueIsDeleted - This handler is used to update metadata store
474/// when a value is deleted.
475void MetadataContext::ValueIsDeleted(Instruction *Inst) {
476 pImpl->ValueIsDeleted(Inst);
477}
478void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
479 pImpl->ValueIsRAUWd(V1, V2);
480}
481
482/// ValueIsCloned - This handler is used to update metadata store
483/// when In1 is cloned to create In2.
484void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
485 pImpl->ValueIsCloned(In1, In2);
486}