blob: d632f8d4fc1204ed4ef6a8da54d428ef48b4319f [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 Hernandez0471abd2009-12-18 20:09:14 +000053 bool isFunctionLocal)
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 Hernandez0471abd2009-12-18 20:09:14 +000060 if (isFunctionLocal)
61 SubclassData |= FunctionLocalBit;
Devang Patela4f43fb2009-07-28 21:49:47 +000062}
63
Devang Patelf7188322009-09-03 01:39:20 +000064void MDNode::Profile(FoldingSetNodeID &ID) const {
Devang Patel49914e6e2009-10-21 21:25:09 +000065 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
66 ID.AddPointer(getElement(i));
Devang Patelf7188322009-09-03 01:39:20 +000067}
68
Victor Hernandezdd7418a2009-12-16 02:52:09 +000069MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals,
Victor Hernandez0471abd2009-12-18 20:09:14 +000070 bool isFunctionLocal) {
Devang Patelf7188322009-09-03 01:39:20 +000071 LLVMContextImpl *pImpl = Context.pImpl;
72 FoldingSetNodeID ID;
73 for (unsigned i = 0; i != NumVals; ++i)
74 ID.AddPointer(Vals[i]);
75
Devang Patelf7188322009-09-03 01:39:20 +000076 void *InsertPoint;
Nick Lewycky898e8f72009-11-26 22:54:26 +000077 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000078 if (!N) {
79 // InsertPoint will have been set by the FindNodeOrInsertPos call.
Victor Hernandez0471abd2009-12-18 20:09:14 +000080 N = new MDNode(Context, Vals, NumVals, isFunctionLocal);
Chris Lattnerb0c23e82009-10-19 07:10:59 +000081 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +000082 }
Devang Patelf7188322009-09-03 01:39:20 +000083 return N;
Owen Anderson0087fe62009-07-31 21:35:40 +000084}
85
Devang Patel27e0be22009-10-21 23:57:35 +000086/// ~MDNode - Destroy MDNode.
Devang Pateld7fd6ab2009-08-03 22:51:10 +000087MDNode::~MDNode() {
Nick Lewycky898e8f72009-11-26 22:54:26 +000088 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
89 pImpl->MDNodeSet.RemoveNode(this);
Devang Patel27e0be22009-10-21 23:57:35 +000090 delete [] Node;
91 Node = NULL;
Devang Pateld7fd6ab2009-08-03 22:51:10 +000092}
Devang Patel5c310be2009-08-11 18:01:24 +000093
Devang Patelf7188322009-09-03 01:39:20 +000094// Replace value from this node's element list.
95void MDNode::replaceElement(Value *From, Value *To) {
96 if (From == To || !getType())
97 return;
98 LLVMContext &Context = getType()->getContext();
99 LLVMContextImpl *pImpl = Context.pImpl;
100
101 // Find value. This is a linear search, do something if it consumes
102 // lot of time. It is possible that to have multiple instances of
103 // From in this MDNode's element list.
104 SmallVector<unsigned, 4> Indexes;
105 unsigned Index = 0;
Devang Patel49914e6e2009-10-21 21:25:09 +0000106 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
107 Value *V = getElement(i);
Devang Patelf7188322009-09-03 01:39:20 +0000108 if (V && V == From)
109 Indexes.push_back(Index);
110 }
111
112 if (Indexes.empty())
113 return;
114
115 // Remove "this" from the context map.
Owen Anderson5dab84c2009-10-19 20:11:52 +0000116 pImpl->MDNodeSet.RemoveNode(this);
Devang Patelf7188322009-09-03 01:39:20 +0000117
118 // Replace From element(s) in place.
119 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
120 I != E; ++I) {
121 unsigned Index = *I;
122 Node[Index] = ElementVH(To, this);
123 }
124
125 // Insert updated "this" into the context's folding node set.
126 // If a node with same element list already exist then before inserting
127 // updated "this" into the folding node set, replace all uses of existing
128 // node with updated "this" node.
129 FoldingSetNodeID ID;
130 Profile(ID);
Devang Patelf7188322009-09-03 01:39:20 +0000131 void *InsertPoint;
132 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000133
134 if (N) {
135 N->replaceAllUsesWith(this);
136 delete N;
137 N = 0;
138 }
139
Owen Anderson5dab84c2009-10-19 20:11:52 +0000140 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
141 if (!N) {
142 // InsertPoint will have been set by the FindNodeOrInsertPos call.
143 N = this;
144 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
Devang Patelf7188322009-09-03 01:39:20 +0000145 }
146}
147
Victor Hernandez0471abd2009-12-18 20:09:14 +0000148// getLocalFunction - Return false if MDNode's recursive function-localness is
149// invalid (local to more than one function). Return true otherwise. If MDNode
150// has one function to which it is local, set LocalFunction to that function.
151bool MDNode::getLocalFunction(Function *LocalFunction,
152 SmallPtrSet<MDNode *, 32> *VisitedMDNodes) {
153 if (!isFunctionLocal())
154 return true;
155
156 if (!VisitedMDNodes)
157 VisitedMDNodes = new SmallPtrSet<MDNode *, 32>();
158
159 if (!VisitedMDNodes->insert(this))
160 // MDNode has already been visited, nothing to do.
161 return true;
162
163 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
164 Value *V = getElement(i);
165 if (!V) continue;
166
167 Function *LocalFunctionTemp = NULL;
168 if (Instruction *I = dyn_cast<Instruction>(V))
169 LocalFunctionTemp = I->getParent()->getParent();
170 else if (MDNode *MD = dyn_cast<MDNode>(V))
171 if (!MD->getLocalFunction(LocalFunctionTemp, VisitedMDNodes))
172 // This MDNode's operand is function-locally invalid or local to a
173 // different function.
174 return false;
175
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000176 if (LocalFunctionTemp) {
Victor Hernandez0471abd2009-12-18 20:09:14 +0000177 if (!LocalFunction)
178 LocalFunction = LocalFunctionTemp;
179 else if (LocalFunction != LocalFunctionTemp)
180 // This MDNode contains operands that are local to different functions.
181 return false;
Eli Friedman3a7cdac2009-12-18 21:07:18 +0000182 }
Victor Hernandez0471abd2009-12-18 20:09:14 +0000183 }
184
185 return true;
186}
187
Devang Patel05a26fb2009-07-29 00:33:07 +0000188//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000189// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000190//
Owen Anderson55f1c092009-08-13 21:58:54 +0000191NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000192 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000193 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000194 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000195 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000196
Devang Patel27e0be22009-10-21 23:57:35 +0000197 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000198 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000199
Devang Patel4a942d02009-07-29 21:58:56 +0000200 if (ParentModule)
201 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000202}
Devang Patel79238d72009-08-03 06:19:01 +0000203
Devang Patel5c310be2009-08-11 18:01:24 +0000204NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000205 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000206 SmallVector<MetadataBase *, 4> Elems;
207 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
208 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000209 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
210 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000211}
212
Devang Patel79238d72009-08-03 06:19:01 +0000213/// eraseFromParent - Drop all references and remove the node from parent
214/// module.
215void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000216 getParent()->getNamedMDList().erase(this);
217}
218
219/// dropAllReferences - Remove all uses and clear node vector.
220void NamedMDNode::dropAllReferences() {
Devang Patel79238d72009-08-03 06:19:01 +0000221 Node.clear();
222}
223
224NamedMDNode::~NamedMDNode() {
225 dropAllReferences();
226}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000227
228//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000229// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000230//
Devang Patel1155fdf2009-10-22 19:36:54 +0000231namespace llvm {
232class MetadataContextImpl {
233public:
234 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
235 typedef SmallVector<MDPairTy, 2> MDMapTy;
236 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
237 friend class BitcodeReader;
238private:
239
240 /// MetadataStore - Collection of metadata used in this context.
241 MDStoreTy MetadataStore;
242
243 /// MDHandlerNames - Map to hold metadata handler names.
244 StringMap<unsigned> MDHandlerNames;
245
246public:
247 /// registerMDKind - Register a new metadata kind and return its ID.
248 /// A metadata kind can be registered only once.
249 unsigned registerMDKind(StringRef Name);
250
251 /// getMDKind - Return metadata kind. If the requested metadata kind
252 /// is not registered then return 0.
253 unsigned getMDKind(StringRef Name) const;
254
255 /// getMD - Get the metadata of given kind attached to an Instruction.
256 /// If the metadata is not found then return 0.
257 MDNode *getMD(unsigned Kind, const Instruction *Inst);
258
259 /// getMDs - Get the metadata attached to an Instruction.
260 void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
261
262 /// addMD - Attach the metadata of given kind to an Instruction.
263 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
264
Nick Lewyckya75fe182009-11-26 23:19:05 +0000265 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000266 void removeMD(unsigned Kind, Instruction *Inst);
267
268 /// removeAllMetadata - Remove all metadata attached with an instruction.
269 void removeAllMetadata(Instruction *Inst);
270
271 /// copyMD - If metadata is attached with Instruction In1 then attach
272 /// the same metadata to In2.
273 void copyMD(Instruction *In1, Instruction *In2);
274
Nick Lewycky898e8f72009-11-26 22:54:26 +0000275 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000276 /// metadata name and ID.
277 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
278
279 /// ValueIsDeleted - This handler is used to update metadata store
280 /// when a value is deleted.
281 void ValueIsDeleted(const Value *) {}
282 void ValueIsDeleted(Instruction *Inst) {
283 removeAllMetadata(Inst);
284 }
285 void ValueIsRAUWd(Value *V1, Value *V2);
286
287 /// ValueIsCloned - This handler is used to update metadata store
288 /// when In1 is cloned to create In2.
289 void ValueIsCloned(const Instruction *In1, Instruction *In2);
290};
291}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000292
Devang Patel0c35dbd2009-10-20 22:50:27 +0000293/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000294/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000295unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000296 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000297 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
298 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000299}
300
301/// getMDKind - Return metadata kind. If the requested metadata kind
302/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000303unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000304 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000305 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000306 return 0;
307
308 return I->getValue();
309}
310
Devang Patel0c35dbd2009-10-20 22:50:27 +0000311/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000312void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
313 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000314 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000315 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000316 MDMapTy &Info = MetadataStore[Inst];
317 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000318 Info.push_back(std::make_pair(MDKind, Node));
319 MetadataStore.insert(std::make_pair(Inst, Info));
320 return;
321 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000322
Devang Patel5bf7a492009-09-29 20:30:57 +0000323 // If there is an entry for this MDKind then replace it.
324 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
325 MDPairTy &P = Info[i];
326 if (P.first == MDKind) {
327 Info[i] = std::make_pair(MDKind, Node);
328 return;
329 }
330 }
331
332 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000333 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000334}
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 +0000337void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000338 MDStoreTy::iterator I = MetadataStore.find(Inst);
339 if (I == MetadataStore.end())
340 return;
341
342 MDMapTy &Info = I->second;
343 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
344 MDPairTy &P = *MI;
345 if (P.first == Kind) {
346 Info.erase(MI);
347 return;
348 }
349 }
Devang Patelb4034362009-09-29 20:42:25 +0000350}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000351
Devang Patel3eb5d332009-10-21 17:33:41 +0000352/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000353void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000354 MetadataStore.erase(Inst);
355 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000356}
357
Devang Patelebaa76e2009-10-14 17:02:49 +0000358/// copyMD - If metadata is attached with Instruction In1 then attach
359/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000360void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000361 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000362 MDMapTy &In1Info = MetadataStore[In1];
363 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000364 return;
365
Devang Patelebaa76e2009-10-14 17:02:49 +0000366 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000367 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000368}
Devang Patelb4034362009-09-29 20:42:25 +0000369
Devang Patel0c35dbd2009-10-20 22:50:27 +0000370/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000371/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000372MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000373 MDMapTy &Info = MetadataStore[Inst];
374 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000375 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000376
Devang Pateld5497a4b2009-09-16 18:09:00 +0000377 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
378 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000379 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000380 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000381}
382
Devang Patel0c35dbd2009-10-20 22:50:27 +0000383/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000384void MetadataContextImpl::
Devang Patel6da5dbf2009-10-22 18:55:16 +0000385getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000386 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000387 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000388 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000389 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000390 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000391 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000392 // MD kinds are numbered from 1.
393 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000394}
395
Devang Patel09c319e2009-10-22 17:40:37 +0000396/// getHandlerNames - Populate client supplied smallvector using custome
397/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000398void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000399getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000400 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000401 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
402 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000403 // MD Handlers are numbered from 1.
404 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000405}
406
Devang Pateladd58652009-09-23 18:32:25 +0000407/// ValueIsCloned - This handler is used to update metadata store
408/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000409void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
410 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000411 // Find Metadata handles for In1.
412 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000413 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000414
415 // FIXME : Give all metadata handlers a chance to adjust.
416
417 MDMapTy &In1Info = I->second;
418 MDMapTy In2Info;
419 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000420 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000421}
Devang Patele6f26a72009-10-13 17:00:54 +0000422
423/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
424/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000425void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000426 Instruction *I1 = dyn_cast<Instruction>(V1);
427 Instruction *I2 = dyn_cast<Instruction>(V2);
428 if (!I1 || !I2)
429 return;
430
431 // FIXME : Give custom handlers a chance to override this.
432 ValueIsCloned(I1, I2);
433}
Devang Patelebaa76e2009-10-14 17:02:49 +0000434
Devang Patel1155fdf2009-10-22 19:36:54 +0000435//===----------------------------------------------------------------------===//
436// MetadataContext implementation.
437//
438MetadataContext::MetadataContext()
439 : pImpl(new MetadataContextImpl()) { }
440MetadataContext::~MetadataContext() { delete pImpl; }
441
442/// isValidName - Return true if Name is a valid custom metadata handler name.
443bool MetadataContext::isValidName(StringRef MDName) {
444 if (MDName.empty())
445 return false;
446
447 if (!isalpha(MDName[0]))
448 return false;
449
450 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
451 ++I) {
452 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
453 return false;
454 }
455 return true;
456}
457
458/// registerMDKind - Register a new metadata kind and return its ID.
459/// A metadata kind can be registered only once.
460unsigned MetadataContext::registerMDKind(StringRef Name) {
461 assert(isValidName(Name) && "Invalid custome metadata name!");
462 return pImpl->registerMDKind(Name);
463}
464
465/// getMDKind - Return metadata kind. If the requested metadata kind
466/// is not registered then return 0.
467unsigned MetadataContext::getMDKind(StringRef Name) const {
468 return pImpl->getMDKind(Name);
469}
470
471/// getMD - Get the metadata of given kind attached to an Instruction.
472/// If the metadata is not found then return 0.
473MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
474 return pImpl->getMD(Kind, Inst);
475}
476
477/// getMDs - Get the metadata attached to an Instruction.
478void MetadataContext::
479getMDs(const Instruction *Inst,
480 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
481 return pImpl->getMDs(Inst, MDs);
482}
483
484/// addMD - Attach the metadata of given kind to an Instruction.
485void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
486 pImpl->addMD(Kind, Node, Inst);
487}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000488
Nick Lewyckya75fe182009-11-26 23:19:05 +0000489/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000490void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
491 pImpl->removeMD(Kind, Inst);
492}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000493
Devang Patel1155fdf2009-10-22 19:36:54 +0000494/// removeAllMetadata - Remove all metadata attached with an instruction.
495void MetadataContext::removeAllMetadata(Instruction *Inst) {
496 pImpl->removeAllMetadata(Inst);
497}
498
499/// copyMD - If metadata is attached with Instruction In1 then attach
500/// the same metadata to In2.
501void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
502 pImpl->copyMD(In1, In2);
503}
504
505/// getHandlerNames - Populate client supplied smallvector using custome
506/// metadata name and ID.
507void MetadataContext::
508getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
509 pImpl->getHandlerNames(N);
510}
511
512/// ValueIsDeleted - This handler is used to update metadata store
513/// when a value is deleted.
514void MetadataContext::ValueIsDeleted(Instruction *Inst) {
515 pImpl->ValueIsDeleted(Inst);
516}
517void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
518 pImpl->ValueIsRAUWd(V1, V2);
519}
520
521/// ValueIsCloned - This handler is used to update metadata store
522/// when In1 is cloned to create In2.
523void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
524 pImpl->ValueIsCloned(In1, In2);
525}