blob: f1361c4fe38b65fa0d5b2f734178f0e4f018c68f [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//
Devang Pateldcb99d32009-10-22 00:10:15 +000032MDString *MDString::get(LLVMContext &Context, StringRef Str) {
Owen Anderson0087fe62009-07-31 21:35:40 +000033 LLVMContextImpl *pImpl = Context.pImpl;
Owen Anderson0087fe62009-07-31 21:35:40 +000034 StringMapEntry<MDString *> &Entry =
35 pImpl->MDStringCache.GetOrCreateValue(Str);
36 MDString *&S = Entry.getValue();
Nick Lewycky898e8f72009-11-26 22:54:26 +000037 if (!S) S = new MDString(Context, Entry.getKey());
38 return S;
Owen Anderson0087fe62009-07-31 21:35:40 +000039}
40
Devang Patel862ef782009-11-12 00:50:58 +000041MDString *MDString::get(LLVMContext &Context, const char *Str) {
42 LLVMContextImpl *pImpl = Context.pImpl;
43 StringMapEntry<MDString *> &Entry =
44 pImpl->MDStringCache.GetOrCreateValue(Str ? StringRef(Str) : StringRef());
45 MDString *&S = Entry.getValue();
Nick Lewycky6e052512009-11-27 19:57:53 +000046 if (!S) S = new MDString(Context, Entry.getKey());
Nick Lewycky898e8f72009-11-26 22:54:26 +000047 return S;
Devang Patel862ef782009-11-12 00:50:58 +000048}
49
Owen Anderson0087fe62009-07-31 21:35:40 +000050//===----------------------------------------------------------------------===//
Chris Lattner74a6ad62009-12-28 07:41:54 +000051// MDNodeElement implementation.
52//
53
54// Use CallbackVH to hold MDNode elements.
55namespace llvm {
56class MDNodeElement : public CallbackVH {
57 MDNode *Parent;
58public:
59 MDNodeElement() {}
60 MDNodeElement(Value *V, MDNode *P) : CallbackVH(V), Parent(P) {}
61 ~MDNodeElement() {}
62
63 virtual void deleted();
64 virtual void allUsesReplacedWith(Value *NV);
65};
66} // end namespace llvm.
67
68
69void MDNodeElement::deleted() {
70 Parent->replaceElement(this->operator Value*(), 0);
71}
72
73void MDNodeElement::allUsesReplacedWith(Value *NV) {
74 Parent->replaceElement(this->operator Value*(), NV);
75}
76
77
78
79//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +000080// MDNode implementation.
Devang Patela4f43fb2009-07-28 21:49:47 +000081//
Chris Lattner74a6ad62009-12-28 07:41:54 +000082
Victor Hernandezdd7418a2009-12-16 02:52:09 +000083MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000084 bool isFunctionLocal)
Owen Anderson55f1c092009-08-13 21:58:54 +000085 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
Devang Patel49914e6e2009-10-21 21:25:09 +000086 NodeSize = NumVals;
Chris Lattner74a6ad62009-12-28 07:41:54 +000087 Node = new MDNodeElement[NodeSize];
88 MDNodeElement *Ptr = Node;
Devang Patel27e0be22009-10-21 23:57:35 +000089 for (unsigned i = 0; i != NumVals; ++i)
Chris Lattner74a6ad62009-12-28 07:41:54 +000090 *Ptr++ = MDNodeElement(Vals[i], this);
Victor Hernandez0471abd2009-12-18 20:09:14 +000091 if (isFunctionLocal)
92 SubclassData |= FunctionLocalBit;
Devang Patela4f43fb2009-07-28 21:49:47 +000093}
94
Devang Patelf7188322009-09-03 01:39:20 +000095void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +000096 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
97 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +000098}
99
Victor Hernandezdd7418a2009-12-16 02:52:09 +0000100MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +0000101 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +0000102 LLVMContextImpl *pImpl = Context.pImpl;
103 FoldingSetNodeID ID;
104 for (unsigned i = 0; i != NumVals; ++i)
105 ID.AddPointer(Vals[i]);
106
Devang Patelf7188322009-09-03 01:39:20 +0000107 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +0000108 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000109 if (!N) {
110 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +0000111 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000112 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000113 }
Devang Patelf7188322009-09-03 01:39:20 +0000114 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +0000115}
116
Devang Patel27e0be22009-10-21 23:57:35 +0000117/// ~MDNode - Destroy MDNode.
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000118MDNode::~MDNode() {
Nick Lewycky898e8f72009-11-26 22:54:26 +0000119 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
120 pImpl->MDNodeSet.RemoveNode(this);
Devang Patel27e0be22009-10-21 23:57:35 +0000121 delete [] Node;
122 Node = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000123}
Devang Patel5c310be2009-08-11 18:01:24 +0000124
Chris Lattner74a6ad62009-12-28 07:41:54 +0000125/// getElement - Return specified element.
126Value *MDNode::getElement(unsigned i) const {
127 assert(i < getNumElements() && "Invalid element number!");
128 return Node[i];
129}
130
131
132
Devang Patelf7188322009-09-03 01:39:20 +0000133// Replace value from this node's element list.
134void MDNode::replaceElement(Value *From, Value *To) {
135 if (From == To || !getType())
136 return;
137 LLVMContext &Context = getType()->getContext();
138 LLVMContextImpl *pImpl = Context.pImpl;
139
140 // Find value. This is a linear search, do something if it consumes
141 // lot of time. It is possible that to have multiple instances of
142 // From in this MDNode's element list.
143 SmallVector<unsigned, 4> Indexes;
144 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000145 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
146 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000147 if (V && V == From)
148 Indexes.push_back(Index);
149 }
150
151 if (Indexes.empty())
152 return;
153
154 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000155 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000156
157 // Replace From element(s) in place.
158 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
159 I != E; ++I) {
160 unsigned Index = *I;
Chris Lattner74a6ad62009-12-28 07:41:54 +0000161 Node[Index] = MDNodeElement(To, this);
Devang Patelf7188322009-09-03 01:39:20 +0000162 }
163
164 // Insert updated "this" into the context's folding node set.
165 // If a node with same element list already exist then before inserting
166 // updated "this" into the folding node set, replace all uses of existing
167 // node with updated "this" node.
168 FoldingSetNodeID ID;
169 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000170 void *InsertPoint;
171 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000172
173 if (N) {
174 N->replaceAllUsesWith(this);
175 delete N;
176 N = 0;
177 }
178
Owen Anderson5dab84c2009-10-19 20:11:52 +0000179 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
180 if (!N) {
181 // InsertPoint will have been set by the FindNodeOrInsertPos call.
182 N = this;
183 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000184 }
185}
186
Victor Hernandez0471abd2009-12-18 20:09:14 +0000187// getLocalFunction - Return false if MDNode's recursive function-localness is
188// invalid (local to more than one function). Return true otherwise. If MDNode
189// has one function to which it is local, set LocalFunction to that function.
190bool MDNode::getLocalFunction(Function *LocalFunction,
191 SmallPtrSet<MDNode *, 32> *VisitedMDNodes) {
192 if (!isFunctionLocal())
193 return true;
194
195 if (!VisitedMDNodes)
196 VisitedMDNodes = new SmallPtrSet<MDNode *, 32>();
197
198 if (!VisitedMDNodes->insert(this))
199 // MDNode has already been visited, nothing to do.
200 return true;
201
202 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
203 Value *V = getElement(i);
204 if (!V) continue;
205
206 Function *LocalFunctionTemp = NULL;
207 if (Instruction *I = dyn_cast<Instruction>(V))
208 LocalFunctionTemp = I->getParent()->getParent();
209 else if (MDNode *MD = dyn_cast<MDNode>(V))
210 if (!MD->getLocalFunction(LocalFunctionTemp, VisitedMDNodes))
211 // This MDNode's operand is function-locally invalid or local to a
212 // different function.
213 return false;
214
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000215 if (LocalFunctionTemp) {
Victor Hernandez0471abd2009-12-18 20:09:14 +0000216 if (!LocalFunction)
217 LocalFunction = LocalFunctionTemp;
218 else if (LocalFunction != LocalFunctionTemp)
219 // This MDNode contains operands that are local to different functions.
220 return false;
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000221 }
Victor Hernandez0471abd2009-12-18 20:09:14 +0000222 }
223
224 return true;
225}
226
Devang Patel05a26fb2009-07-29 00:33:07 +0000227//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000228// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000229//
Chris Lattner1bc810b2009-12-28 08:07:14 +0000230static SmallVector<TrackingVH<MetadataBase>, 4> &getNMDOps(void *Operands) {
231 return *(SmallVector<TrackingVH<MetadataBase>, 4>*)Operands;
232}
233
Owen Anderson55f1c092009-08-13 21:58:54 +0000234NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000235 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000236 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000237 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000238 setName(N);
Chris Lattner1bc810b2009-12-28 08:07:14 +0000239
240 Operands = new SmallVector<TrackingVH<MetadataBase>, 4>();
241
242 SmallVector<TrackingVH<MetadataBase>, 4> &Node = getNMDOps(Operands);
Devang Patel27e0be22009-10-21 23:57:35 +0000243 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000244 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000245
Devang Patel4a942d02009-07-29 21:58:56 +0000246 if (ParentModule)
247 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000248}
Devang Patel79238d72009-08-03 06:19:01 +0000249
Devang Patel5c310be2009-08-11 18:01:24 +0000250NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000251 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000252 SmallVector<MetadataBase *, 4> Elems;
Chris Lattner1bc810b2009-12-28 08:07:14 +0000253 Elems.reserve(NMD->getNumElements());
254
Devang Patel5c310be2009-08-11 18:01:24 +0000255 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
256 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000257 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
258 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000259}
260
Chris Lattner1bc810b2009-12-28 08:07:14 +0000261NamedMDNode::~NamedMDNode() {
262 dropAllReferences();
263 delete &getNMDOps(Operands);
264}
265
266/// getNumElements - Return number of NamedMDNode elements.
267unsigned NamedMDNode::getNumElements() const {
268 return (unsigned)getNMDOps(Operands).size();
269}
270
271/// getElement - Return specified element.
272MetadataBase *NamedMDNode::getElement(unsigned i) const {
273 assert(i < getNumElements() && "Invalid element number!");
274 return getNMDOps(Operands)[i];
275}
276
277/// addElement - Add metadata element.
278void NamedMDNode::addElement(MetadataBase *M) {
279 getNMDOps(Operands).push_back(TrackingVH<MetadataBase>(M));
280}
281
Devang Patel79238d72009-08-03 06:19:01 +0000282/// eraseFromParent - Drop all references and remove the node from parent
283/// module.
284void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000285 getParent()->getNamedMDList().erase(this);
286}
287
288/// dropAllReferences - Remove all uses and clear node vector.
289void NamedMDNode::dropAllReferences() {
Chris Lattner1bc810b2009-12-28 08:07:14 +0000290 getNMDOps(Operands).clear();
Devang Patel79238d72009-08-03 06:19:01 +0000291}
292
Devang Pateld5497a4b2009-09-16 18:09:00 +0000293
294//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000295// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000296//
Devang Patel1155fdf2009-10-22 19:36:54 +0000297namespace llvm {
298class MetadataContextImpl {
299public:
300 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
301 typedef SmallVector<MDPairTy, 2> MDMapTy;
302 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
303 friend class BitcodeReader;
304private:
305
306 /// MetadataStore - Collection of metadata used in this context.
307 MDStoreTy MetadataStore;
308
309 /// MDHandlerNames - Map to hold metadata handler names.
310 StringMap<unsigned> MDHandlerNames;
311
312public:
313 /// registerMDKind - Register a new metadata kind and return its ID.
314 /// A metadata kind can be registered only once.
315 unsigned registerMDKind(StringRef Name);
316
317 /// getMDKind - Return metadata kind. If the requested metadata kind
318 /// is not registered then return 0.
319 unsigned getMDKind(StringRef Name) const;
320
321 /// getMD - Get the metadata of given kind attached to an Instruction.
322 /// If the metadata is not found then return 0.
323 MDNode *getMD(unsigned Kind, const Instruction *Inst);
324
325 /// getMDs - Get the metadata attached to an Instruction.
Chris Lattner53bb5e42009-12-28 08:14:54 +0000326 void getMDs(const Instruction *Inst,
327 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const;
Devang Patel1155fdf2009-10-22 19:36:54 +0000328
329 /// addMD - Attach the metadata of given kind to an Instruction.
330 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
331
Nick Lewyckya75fe182009-11-26 23:19:05 +0000332 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000333 void removeMD(unsigned Kind, Instruction *Inst);
334
335 /// removeAllMetadata - Remove all metadata attached with an instruction.
336 void removeAllMetadata(Instruction *Inst);
337
338 /// copyMD - If metadata is attached with Instruction In1 then attach
339 /// the same metadata to In2.
340 void copyMD(Instruction *In1, Instruction *In2);
341
Nick Lewycky898e8f72009-11-26 22:54:26 +0000342 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000343 /// metadata name and ID.
344 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
345
346 /// ValueIsDeleted - This handler is used to update metadata store
347 /// when a value is deleted.
348 void ValueIsDeleted(const Value *) {}
349 void ValueIsDeleted(Instruction *Inst) {
350 removeAllMetadata(Inst);
351 }
352 void ValueIsRAUWd(Value *V1, Value *V2);
353
354 /// ValueIsCloned - This handler is used to update metadata store
355 /// when In1 is cloned to create In2.
356 void ValueIsCloned(const Instruction *In1, Instruction *In2);
357};
358}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000359
Devang Patel0c35dbd2009-10-20 22:50:27 +0000360/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000361/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000362unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000363 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000364 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
365 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000366}
367
368/// getMDKind - Return metadata kind. If the requested metadata kind
369/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000370unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000371 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000372 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000373 return 0;
374
375 return I->getValue();
376}
377
Devang Patel0c35dbd2009-10-20 22:50:27 +0000378/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000379void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
380 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000381 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000382 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000383 MDMapTy &Info = MetadataStore[Inst];
384 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000385 Info.push_back(std::make_pair(MDKind, Node));
386 MetadataStore.insert(std::make_pair(Inst, Info));
387 return;
388 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000389
Devang Patel5bf7a492009-09-29 20:30:57 +0000390 // If there is an entry for this MDKind then replace it.
391 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
392 MDPairTy &P = Info[i];
393 if (P.first == MDKind) {
394 Info[i] = std::make_pair(MDKind, Node);
395 return;
396 }
397 }
398
399 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000400 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000401}
402
Nick Lewyckya75fe182009-11-26 23:19:05 +0000403/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000404void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000405 MDStoreTy::iterator I = MetadataStore.find(Inst);
406 if (I == MetadataStore.end())
407 return;
408
409 MDMapTy &Info = I->second;
410 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
411 MDPairTy &P = *MI;
412 if (P.first == Kind) {
413 Info.erase(MI);
414 return;
415 }
416 }
Devang Patelb4034362009-09-29 20:42:25 +0000417}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000418
Devang Patel3eb5d332009-10-21 17:33:41 +0000419/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000420void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000421 MetadataStore.erase(Inst);
422 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000423}
424
Devang Patelebaa76e2009-10-14 17:02:49 +0000425/// copyMD - If metadata is attached with Instruction In1 then attach
426/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000427void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000428 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000429 MDMapTy &In1Info = MetadataStore[In1];
430 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000431 return;
432
Devang Patelebaa76e2009-10-14 17:02:49 +0000433 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000434 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000435}
Devang Patelb4034362009-09-29 20:42:25 +0000436
Devang Patel0c35dbd2009-10-20 22:50:27 +0000437/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000438/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000439MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000440 MDMapTy &Info = MetadataStore[Inst];
441 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000442 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000443
Devang Pateld5497a4b2009-09-16 18:09:00 +0000444 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
445 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000446 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000447 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000448}
449
Devang Patel0c35dbd2009-10-20 22:50:27 +0000450/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000451void MetadataContextImpl::
Chris Lattner53bb5e42009-12-28 08:14:54 +0000452getMDs(const Instruction *Inst,
453 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000454 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000455 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000456 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000457 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000458 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000459 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000460 // MD kinds are numbered from 1.
461 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000462}
463
Devang Patel09c319e2009-10-22 17:40:37 +0000464/// getHandlerNames - Populate client supplied smallvector using custome
465/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000466void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000467getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000468 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000469 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
470 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000471 // MD Handlers are numbered from 1.
472 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000473}
474
Devang Pateladd58652009-09-23 18:32:25 +0000475/// ValueIsCloned - This handler is used to update metadata store
476/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000477void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
478 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000479 // Find Metadata handles for In1.
480 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000481 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000482
483 // FIXME : Give all metadata handlers a chance to adjust.
484
485 MDMapTy &In1Info = I->second;
486 MDMapTy In2Info;
487 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000488 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000489}
Devang Patele6f26a72009-10-13 17:00:54 +0000490
491/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
492/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000493void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000494 Instruction *I1 = dyn_cast<Instruction>(V1);
495 Instruction *I2 = dyn_cast<Instruction>(V2);
496 if (!I1 || !I2)
497 return;
498
499 // FIXME : Give custom handlers a chance to override this.
500 ValueIsCloned(I1, I2);
501}
Devang Patelebaa76e2009-10-14 17:02:49 +0000502
Devang Patel1155fdf2009-10-22 19:36:54 +0000503//===----------------------------------------------------------------------===//
504// MetadataContext implementation.
505//
506MetadataContext::MetadataContext()
507 : pImpl(new MetadataContextImpl()) { }
508MetadataContext::~MetadataContext() { delete pImpl; }
509
510/// isValidName - Return true if Name is a valid custom metadata handler name.
511bool MetadataContext::isValidName(StringRef MDName) {
512 if (MDName.empty())
513 return false;
514
515 if (!isalpha(MDName[0]))
516 return false;
517
518 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
519 ++I) {
520 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
521 return false;
522 }
523 return true;
524}
525
526/// registerMDKind - Register a new metadata kind and return its ID.
527/// A metadata kind can be registered only once.
528unsigned MetadataContext::registerMDKind(StringRef Name) {
529 assert(isValidName(Name) && "Invalid custome metadata name!");
530 return pImpl->registerMDKind(Name);
531}
532
533/// getMDKind - Return metadata kind. If the requested metadata kind
534/// is not registered then return 0.
535unsigned MetadataContext::getMDKind(StringRef Name) const {
536 return pImpl->getMDKind(Name);
537}
538
539/// getMD - Get the metadata of given kind attached to an Instruction.
540/// If the metadata is not found then return 0.
541MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
542 return pImpl->getMD(Kind, Inst);
543}
544
545/// getMDs - Get the metadata attached to an Instruction.
546void MetadataContext::
547getMDs(const Instruction *Inst,
Chris Lattner53bb5e42009-12-28 08:14:54 +0000548 SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs) const {
Devang Patel1155fdf2009-10-22 19:36:54 +0000549 return pImpl->getMDs(Inst, MDs);
550}
551
552/// addMD - Attach the metadata of given kind to an Instruction.
553void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
554 pImpl->addMD(Kind, Node, Inst);
555}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000556
Nick Lewyckya75fe182009-11-26 23:19:05 +0000557/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000558void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
559 pImpl->removeMD(Kind, Inst);
560}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000561
Devang Patel1155fdf2009-10-22 19:36:54 +0000562/// removeAllMetadata - Remove all metadata attached with an instruction.
563void MetadataContext::removeAllMetadata(Instruction *Inst) {
564 pImpl->removeAllMetadata(Inst);
565}
566
567/// copyMD - If metadata is attached with Instruction In1 then attach
568/// the same metadata to In2.
569void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
570 pImpl->copyMD(In1, In2);
571}
572
573/// getHandlerNames - Populate client supplied smallvector using custome
574/// metadata name and ID.
575void MetadataContext::
576getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
577 pImpl->getHandlerNames(N);
578}
579
580/// ValueIsDeleted - This handler is used to update metadata store
581/// when a value is deleted.
582void MetadataContext::ValueIsDeleted(Instruction *Inst) {
583 pImpl->ValueIsDeleted(Inst);
584}
585void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
586 pImpl->ValueIsRAUWd(V1, V2);
587}
588
589/// ValueIsCloned - This handler is used to update metadata store
590/// when In1 is cloned to create In2.
591void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
592 pImpl->ValueIsCloned(In1, In2);
593}