blob: 01b47d676798d73f780899a1ecec0e51ead76591 [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
67 virtual void deleted();
68 virtual void allUsesReplacedWith(Value *NV);
69};
70} // end namespace llvm.
71
72
73void MDNodeElement::deleted() {
74 Parent->replaceElement(this->operator Value*(), 0);
75}
76
77void MDNodeElement::allUsesReplacedWith(Value *NV) {
78 Parent->replaceElement(this->operator Value*(), NV);
79}
80
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000084// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000085//
Chris Lattner74a6ad62009-12-28 07:41:54 +000086
Victor Hernandezdd7418a2009-12-16 02:52:09 +000087MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000088 bool isFunctionLocal)
Owen Anderson55f1c092009-08-13 21:58:54 +000089 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +000090 NumOperands = NumVals;
91 Operands = new MDNodeElement[NumOperands];
92 MDNodeElement *Ptr = Operands;
Devang Patel27e0be22009-10-21 23:57:35 +000093 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattner66a4c3d2009-12-28 08:48:12 +000094 Ptr[i] = MDNodeElement(Vals[i], this);
95
Victor Hernandez0471abd2009-12-18 20:09:14 +000096 if (isFunctionLocal)
97 SubclassData |= FunctionLocalBit;
Devang Patela4f43fb2009-07-28 21:49:47 +000098}
99
Devang Patelf7188322009-09-03 01:39:20 +0000100void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +0000101 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
102 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +0000103}
104
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000105MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000106 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000107 LLVMContextImpl *pImpl = Context.pImpl;
108 FoldingSetNodeID ID;
109 for (unsigned i = 0; i != NumVals; ++i)
110 ID.AddPointer(Vals[i]);
111
Devang Patelf7188322009-09-03 01:39:20 +0000112 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000113 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000114 if (!N) {
115 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +0000116 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000117 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000118 }
Devang Patelf7188322009-09-03 01:39:20 +0000119 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000120}
121
Devang Patel27e0be22009-10-21 23:57:35 +0000122/// ~MDNode - Destroy MDNode.
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000123MDNode::~MDNode() {
Nick Lewycky898e8f72009-11-26 22:54:26 +0000124 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
125 pImpl->MDNodeSet.RemoveNode(this);
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000126 delete [] Operands;
127 Operands = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000128}
Devang Patel5c310be2009-08-11 18:01:24 +0000129
Chris Lattner74a6ad62009-12-28 07:41:54 +0000130/// getElement - Return specified element.
131Value *MDNode::getElement(unsigned i) const {
132 assert(i < getNumElements() && "Invalid element number!");
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000133 return Operands[i];
Chris Lattner74a6ad62009-12-28 07:41:54 +0000134}
135
136
137
Devang Patelf7188322009-09-03 01:39:20 +0000138// Replace value from this node's element list.
139void MDNode::replaceElement(Value *From, Value *To) {
140 if (From == To || !getType())
141 return;
142 LLVMContext &Context = getType()->getContext();
143 LLVMContextImpl *pImpl = Context.pImpl;
144
145 // Find value. This is a linear search, do something if it consumes
146 // lot of time. It is possible that to have multiple instances of
147 // From in this MDNode's element list.
148 SmallVector<unsigned, 4> Indexes;
149 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000150 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
151 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000152 if (V && V == From)
153 Indexes.push_back(Index);
154 }
155
156 if (Indexes.empty())
157 return;
158
159 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000160 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000161
162 // Replace From element(s) in place.
163 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
164 I != E; ++I) {
Chris Lattner66a4c3d2009-12-28 08:48:12 +0000165 Operands[*I] = MDNodeElement(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000166 }
167
168 // Insert updated "this" into the context's folding node set.
169 // If a node with same element list already exist then before inserting
170 // updated "this" into the folding node set, replace all uses of existing
171 // node with updated "this" node.
172 FoldingSetNodeID ID;
173 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000174 void *InsertPoint;
175 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000176
177 if (N) {
178 N->replaceAllUsesWith(this);
179 delete N;
180 N = 0;
181 }
182
Owen Anderson5dab84c2009-10-19 20:11:52 +0000183 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
184 if (!N) {
185 // InsertPoint will have been set by the FindNodeOrInsertPos call.
186 N = this;
187 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000188 }
189}
190
Victor Hernandez0471abd2009-12-18 20:09:14 +0000191// getLocalFunction - Return false if MDNode's recursive function-localness is
192// invalid (local to more than one function). Return true otherwise. If MDNode
193// has one function to which it is local, set LocalFunction to that function.
194bool MDNode::getLocalFunction(Function *LocalFunction,
195 SmallPtrSet<MDNode *, 32> *VisitedMDNodes) {
196 if (!isFunctionLocal())
197 return true;
198
199 if (!VisitedMDNodes)
200 VisitedMDNodes = new SmallPtrSet<MDNode *, 32>();
201
202 if (!VisitedMDNodes->insert(this))
203 // MDNode has already been visited, nothing to do.
204 return true;
205
206 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
207 Value *V = getElement(i);
208 if (!V) continue;
209
210 Function *LocalFunctionTemp = NULL;
211 if (Instruction *I = dyn_cast<Instruction>(V))
212 LocalFunctionTemp = I->getParent()->getParent();
213 else if (MDNode *MD = dyn_cast<MDNode>(V))
214 if (!MD->getLocalFunction(LocalFunctionTemp, VisitedMDNodes))
215 // This MDNode's operand is function-locally invalid or local to a
216 // different function.
217 return false;
218
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000219 if (LocalFunctionTemp) {
Victor Hernandez0471abd2009-12-18 20:09:14 +0000220 if (!LocalFunction)
221 LocalFunction = LocalFunctionTemp;
222 else if (LocalFunction != LocalFunctionTemp)
223 // This MDNode contains operands that are local to different functions.
224 return false;
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000225 }
Victor Hernandez0471abd2009-12-18 20:09:14 +0000226 }
227
228 return true;
229}
230
Devang Patel05a26fb2009-07-29 00:33:07 +0000231//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000232// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000233//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000234static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
235 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
236}
237
Owen Anderson55f1c092009-08-13 21:58:54 +0000238NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000239 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000240 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000241 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000242 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000243
244 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
245
246 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000247 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000248 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000249
Devang Patel4a942d02009-07-29 21:58:56 +0000250 if (ParentModule)
251 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000252}
Devang Patel79238d72009-08-03 06:19:01 +0000253
Devang Patel5c310be2009-08-11 18:01:24 +0000254NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000255 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000256 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000257 Elems.reserve(NMD->getNumElements());
258
Devang Patel5c310be2009-08-11 18:01:24 +0000259 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
260 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000261 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
262 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000263}
264
Chris Lattner1bc810b2009-12-28 08:07:14 +0000265NamedMDNode::~NamedMDNode() {
266 dropAllReferences();
267 delete &getNMDOps(Operands);
268}
269
270/// getNumElements - Return number of NamedMDNode elements.
271unsigned NamedMDNode::getNumElements() const {
272 return (unsigned)getNMDOps(Operands).size();
273}
274
275/// getElement - Return specified element.
276MetadataBase *NamedMDNode::getElement(unsigned i) const {
277 assert(i < getNumElements() && "Invalid element number!");
278 return getNMDOps(Operands)[i];
279}
280
281/// addElement - Add metadata element.
282void NamedMDNode::addElement(MetadataBase *M) {
283 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
284}
285
Devang Patel79238d72009-08-03 06:19:01 +0000286/// eraseFromParent - Drop all references and remove the node from parent
287/// module.
288void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000289 getParent()->getNamedMDList().erase(this);
290}
291
292/// dropAllReferences - Remove all uses and clear node vector.
293void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000294 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000295}
296
Devang Pateld5497a4b2009-09-16 18:09:00 +0000297
298//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000299// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000300//
Devang Patel1155fdf2009-10-22 19:36:54 +0000301namespace llvm {
302class MetadataContextImpl {
303public:
304 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
305 typedef SmallVector<MDPairTy, 2> MDMapTy;
306 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
307 friend class BitcodeReader;
308private:
309
310 /// MetadataStore - Collection of metadata used in this context.
311 MDStoreTy MetadataStore;
312
313 /// MDHandlerNames - Map to hold metadata handler names.
314 StringMap<unsigned> MDHandlerNames;
315
316public:
317 /// registerMDKind - Register a new metadata kind and return its ID.
318 /// A metadata kind can be registered only once.
319 unsigned registerMDKind(StringRef Name);
320
321 /// getMDKind - Return metadata kind. If the requested metadata kind
322 /// is not registered then return 0.
323 unsigned getMDKind(StringRef Name) const;
324
325 /// getMD - Get the metadata of given kind attached to an Instruction.
326 /// If the metadata is not found then return 0.
327 MDNode *getMD(unsigned Kind, const Instruction *Inst);
328
329 /// getMDs - Get the metadata attached to an Instruction.
Chris Lattner53bb5e42009-12-28 08:14:54 +0000330 void getMDs(const Instruction *Inst,
331 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000332
333 /// addMD - Attach the metadata of given kind to an Instruction.
334 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
335
Nick Lewyckya75fe182009-11-26 23:19:05 +0000336 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000337 void removeMD(unsigned Kind, Instruction *Inst);
338
339 /// removeAllMetadata - Remove all metadata attached with an instruction.
340 void removeAllMetadata(Instruction *Inst);
341
342 /// copyMD - If metadata is attached with Instruction In1 then attach
343 /// the same metadata to In2.
344 void copyMD(Instruction *In1, Instruction *In2);
345
Nick Lewycky898e8f72009-11-26 22:54:26 +0000346 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000347 /// metadata name and ID.
348 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
349
350 /// ValueIsDeleted - This handler is used to update metadata store
351 /// when a value is deleted.
352 void ValueIsDeleted(const Value *) {}
353 void ValueIsDeleted(Instruction *Inst) {
354 removeAllMetadata(Inst);
355 }
356 void ValueIsRAUWd(Value *V1, Value *V2);
357
358 /// ValueIsCloned - This handler is used to update metadata store
359 /// when In1 is cloned to create In2.
360 void ValueIsCloned(const Instruction *In1, Instruction *In2);
361};
362}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000363
Devang Patel0c35dbd2009-10-20 22:50:27 +0000364/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000365/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000366unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000367 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000368 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
369 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000370}
371
372/// getMDKind - Return metadata kind. If the requested metadata kind
373/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000374unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000375 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000376 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000377 return 0;
378
379 return I->getValue();
380}
381
Devang Patel0c35dbd2009-10-20 22:50:27 +0000382/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000383void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
384 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000385 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000386 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000387 MDMapTy &Info = MetadataStore[Inst];
388 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000389 Info.push_back(std::make_pair(MDKind, Node));
390 MetadataStore.insert(std::make_pair(Inst, Info));
391 return;
392 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000393
Devang Patel5bf7a492009-09-29 20:30:57 +0000394 // If there is an entry for this MDKind then replace it.
395 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
396 MDPairTy &P = Info[i];
397 if (P.first == MDKind) {
398 Info[i] = std::make_pair(MDKind, Node);
399 return;
400 }
401 }
402
403 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000404 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000405}
406
Nick Lewyckya75fe182009-11-26 23:19:05 +0000407/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000408void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000409 MDStoreTy::iterator I = MetadataStore.find(Inst);
410 if (I == MetadataStore.end())
411 return;
412
413 MDMapTy &Info = I->second;
414 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
415 MDPairTy &P = *MI;
416 if (P.first == Kind) {
417 Info.erase(MI);
418 return;
419 }
420 }
Devang Patelb4034362009-09-29 20:42:25 +0000421}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000422
Devang Patel3eb5d332009-10-21 17:33:41 +0000423/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000424void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000425 MetadataStore.erase(Inst);
426 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000427}
428
Devang Patelebaa76e2009-10-14 17:02:49 +0000429/// copyMD - If metadata is attached with Instruction In1 then attach
430/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000431void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000432 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000433 MDMapTy &In1Info = MetadataStore[In1];
434 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000435 return;
436
Devang Patelebaa76e2009-10-14 17:02:49 +0000437 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000438 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000439}
Devang Patelb4034362009-09-29 20:42:25 +0000440
Devang Patel0c35dbd2009-10-20 22:50:27 +0000441/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000442/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000443MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000444 MDMapTy &Info = MetadataStore[Inst];
445 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000446 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000447
Devang Pateld5497a4b2009-09-16 18:09:00 +0000448 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
449 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000450 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000451 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000452}
453
Devang Patel0c35dbd2009-10-20 22:50:27 +0000454/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000455void MetadataContextImpl::
Chris Lattner53bb5e42009-12-28 08:14:54 +0000456getMDs(const Instruction *Inst,
457 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000458 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000459 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000460 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000461 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000462 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000463 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000464 // MD kinds are numbered from 1.
465 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000466}
467
Devang Patel09c319e2009-10-22 17:40:37 +0000468/// getHandlerNames - Populate client supplied smallvector using custome
469/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000470void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000471getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000472 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000473 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
474 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000475 // MD Handlers are numbered from 1.
476 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000477}
478
Devang Pateladd58652009-09-23 18:32:25 +0000479/// ValueIsCloned - This handler is used to update metadata store
480/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000481void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
482 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000483 // Find Metadata handles for In1.
484 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000485 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000486
487 // FIXME : Give all metadata handlers a chance to adjust.
488
489 MDMapTy &In1Info = I->second;
490 MDMapTy In2Info;
491 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000492 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000493}
Devang Patele6f26a72009-10-13 17:00:54 +0000494
495/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
496/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000497void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000498 Instruction *I1 = dyn_cast<Instruction>(V1);
499 Instruction *I2 = dyn_cast<Instruction>(V2);
500 if (!I1 || !I2)
501 return;
502
503 // FIXME : Give custom handlers a chance to override this.
504 ValueIsCloned(I1, I2);
505}
Devang Patelebaa76e2009-10-14 17:02:49 +0000506
Devang Patel1155fdf2009-10-22 19:36:54 +0000507//===----------------------------------------------------------------------===//
508// MetadataContext implementation.
509//
510MetadataContext::MetadataContext()
511 : pImpl(new MetadataContextImpl()) { }
512MetadataContext::~MetadataContext() { delete pImpl; }
513
514/// isValidName - Return true if Name is a valid custom metadata handler name.
515bool MetadataContext::isValidName(StringRef MDName) {
516 if (MDName.empty())
517 return false;
518
519 if (!isalpha(MDName[0]))
520 return false;
521
522 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
523 ++I) {
524 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
525 return false;
526 }
527 return true;
528}
529
530/// registerMDKind - Register a new metadata kind and return its ID.
531/// A metadata kind can be registered only once.
532unsigned MetadataContext::registerMDKind(StringRef Name) {
533 assert(isValidName(Name) && "Invalid custome metadata name!");
534 return pImpl->registerMDKind(Name);
535}
536
537/// getMDKind - Return metadata kind. If the requested metadata kind
538/// is not registered then return 0.
539unsigned MetadataContext::getMDKind(StringRef Name) const {
540 return pImpl->getMDKind(Name);
541}
542
543/// getMD - Get the metadata of given kind attached to an Instruction.
544/// If the metadata is not found then return 0.
545MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
546 return pImpl->getMD(Kind, Inst);
547}
548
549/// getMDs - Get the metadata attached to an Instruction.
550void MetadataContext::
551getMDs(const Instruction *Inst,
Chris Lattner53bb5e42009-12-28 08:14:54 +0000552 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Devang Patel1155fdf2009-10-22 19:36:54 +0000553 return pImpl->getMDs(Inst, MDs);
554}
555
556/// addMD - Attach the metadata of given kind to an Instruction.
557void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
558 pImpl->addMD(Kind, Node, Inst);
559}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000560
Nick Lewyckya75fe182009-11-26 23:19:05 +0000561/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000562void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
563 pImpl->removeMD(Kind, Inst);
564}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000565
Devang Patel1155fdf2009-10-22 19:36:54 +0000566/// removeAllMetadata - Remove all metadata attached with an instruction.
567void MetadataContext::removeAllMetadata(Instruction *Inst) {
568 pImpl->removeAllMetadata(Inst);
569}
570
571/// copyMD - If metadata is attached with Instruction In1 then attach
572/// the same metadata to In2.
573void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
574 pImpl->copyMD(In1, In2);
575}
576
577/// getHandlerNames - Populate client supplied smallvector using custome
578/// metadata name and ID.
579void MetadataContext::
580getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
581 pImpl->getHandlerNames(N);
582}
583
584/// ValueIsDeleted - This handler is used to update metadata store
585/// when a value is deleted.
586void MetadataContext::ValueIsDeleted(Instruction *Inst) {
587 pImpl->ValueIsDeleted(Inst);
588}
589void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
590 pImpl->ValueIsRAUWd(V1, V2);
591}
592
593/// ValueIsCloned - This handler is used to update metadata store
594/// when In1 is cloned to create In2.
595void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
596 pImpl->ValueIsCloned(In1, In2);
597}