blob: ba89e09ae569ed73be01c943e0f46b3eeac8cd5e [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
176 if (LocalFunctionTemp)
177 if (!LocalFunction)
178 LocalFunction = LocalFunctionTemp;
179 else if (LocalFunction != LocalFunctionTemp)
180 // This MDNode contains operands that are local to different functions.
181 return false;
182 }
183
184 return true;
185}
186
Devang Patel05a26fb2009-07-29 00:33:07 +0000187//===----------------------------------------------------------------------===//
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000188// NamedMDNode implementation.
Devang Patel05a26fb2009-07-29 00:33:07 +0000189//
Owen Anderson55f1c092009-08-13 21:58:54 +0000190NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000191 MetadataBase *const *MDs,
Devang Patel4a942d02009-07-29 21:58:56 +0000192 unsigned NumMDs, Module *ParentModule)
Owen Anderson55f1c092009-08-13 21:58:54 +0000193 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
Devang Patel18dfdc92009-07-29 17:16:17 +0000194 setName(N);
Devang Pateld7fd6ab2009-08-03 22:51:10 +0000195
Devang Patel27e0be22009-10-21 23:57:35 +0000196 for (unsigned i = 0; i != NumMDs; ++i)
Devang Patel084679e2009-10-22 18:25:28 +0000197 Node.push_back(TrackingVH<MetadataBase>(MDs[i]));
Devang Patel27e0be22009-10-21 23:57:35 +0000198
Devang Patel4a942d02009-07-29 21:58:56 +0000199 if (ParentModule)
200 ParentModule->getNamedMDList().push_back(this);
Devang Patel05a26fb2009-07-29 00:33:07 +0000201}
Devang Patel79238d72009-08-03 06:19:01 +0000202
Devang Patel5c310be2009-08-11 18:01:24 +0000203NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000204 assert(NMD && "Invalid source NamedMDNode!");
Devang Patel5c310be2009-08-11 18:01:24 +0000205 SmallVector<MetadataBase *, 4> Elems;
206 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
207 Elems.push_back(NMD->getElement(i));
Owen Anderson55f1c092009-08-13 21:58:54 +0000208 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
209 Elems.data(), Elems.size(), M);
Devang Patel5c310be2009-08-11 18:01:24 +0000210}
211
Devang Patel79238d72009-08-03 06:19:01 +0000212/// eraseFromParent - Drop all references and remove the node from parent
213/// module.
214void NamedMDNode::eraseFromParent() {
Devang Patel79238d72009-08-03 06:19:01 +0000215 getParent()->getNamedMDList().erase(this);
216}
217
218/// dropAllReferences - Remove all uses and clear node vector.
219void NamedMDNode::dropAllReferences() {
Devang Patel79238d72009-08-03 06:19:01 +0000220 Node.clear();
221}
222
223NamedMDNode::~NamedMDNode() {
224 dropAllReferences();
225}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000226
227//===----------------------------------------------------------------------===//
Devang Patel1155fdf2009-10-22 19:36:54 +0000228// MetadataContextImpl implementation.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000229//
Devang Patel1155fdf2009-10-22 19:36:54 +0000230namespace llvm {
231class MetadataContextImpl {
232public:
233 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;
234 typedef SmallVector<MDPairTy, 2> MDMapTy;
235 typedef DenseMap<const Instruction *, MDMapTy> MDStoreTy;
236 friend class BitcodeReader;
237private:
238
239 /// MetadataStore - Collection of metadata used in this context.
240 MDStoreTy MetadataStore;
241
242 /// MDHandlerNames - Map to hold metadata handler names.
243 StringMap<unsigned> MDHandlerNames;
244
245public:
246 /// registerMDKind - Register a new metadata kind and return its ID.
247 /// A metadata kind can be registered only once.
248 unsigned registerMDKind(StringRef Name);
249
250 /// getMDKind - Return metadata kind. If the requested metadata kind
251 /// is not registered then return 0.
252 unsigned getMDKind(StringRef Name) const;
253
254 /// getMD - Get the metadata of given kind attached to an Instruction.
255 /// If the metadata is not found then return 0.
256 MDNode *getMD(unsigned Kind, const Instruction *Inst);
257
258 /// getMDs - Get the metadata attached to an Instruction.
259 void getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const;
260
261 /// addMD - Attach the metadata of given kind to an Instruction.
262 void addMD(unsigned Kind, MDNode *Node, Instruction *Inst);
263
Nick Lewyckya75fe182009-11-26 23:19:05 +0000264 /// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000265 void removeMD(unsigned Kind, Instruction *Inst);
266
267 /// removeAllMetadata - Remove all metadata attached with an instruction.
268 void removeAllMetadata(Instruction *Inst);
269
270 /// copyMD - If metadata is attached with Instruction In1 then attach
271 /// the same metadata to In2.
272 void copyMD(Instruction *In1, Instruction *In2);
273
Nick Lewycky898e8f72009-11-26 22:54:26 +0000274 /// getHandlerNames - Populate client-supplied smallvector using custom
Devang Patel1155fdf2009-10-22 19:36:54 +0000275 /// metadata name and ID.
276 void getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&) const;
277
278 /// ValueIsDeleted - This handler is used to update metadata store
279 /// when a value is deleted.
280 void ValueIsDeleted(const Value *) {}
281 void ValueIsDeleted(Instruction *Inst) {
282 removeAllMetadata(Inst);
283 }
284 void ValueIsRAUWd(Value *V1, Value *V2);
285
286 /// ValueIsCloned - This handler is used to update metadata store
287 /// when In1 is cloned to create In2.
288 void ValueIsCloned(const Instruction *In1, Instruction *In2);
289};
290}
Devang Pateld5497a4b2009-09-16 18:09:00 +0000291
Devang Patel0c35dbd2009-10-20 22:50:27 +0000292/// registerMDKind - Register a new metadata kind and return its ID.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000293/// A metadata kind can be registered only once.
Devang Patel1155fdf2009-10-22 19:36:54 +0000294unsigned MetadataContextImpl::registerMDKind(StringRef Name) {
Devang Patelb1a44772009-09-28 21:14:55 +0000295 unsigned Count = MDHandlerNames.size();
Devang Patel3eb5d332009-10-21 17:33:41 +0000296 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
297 return MDHandlerNames[Name] = Count + 1;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000298}
299
300/// getMDKind - Return metadata kind. If the requested metadata kind
301/// is not registered then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000302unsigned MetadataContextImpl::getMDKind(StringRef Name) const {
Devang Patel2505c1e2009-10-21 21:57:13 +0000303 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
Devang Patel1155fdf2009-10-22 19:36:54 +0000304 if (I == MDHandlerNames.end())
Devang Pateld5497a4b2009-09-16 18:09:00 +0000305 return 0;
306
307 return I->getValue();
308}
309
Devang Patel0c35dbd2009-10-20 22:50:27 +0000310/// addMD - Attach the metadata of given kind to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000311void MetadataContextImpl::addMD(unsigned MDKind, MDNode *Node,
312 Instruction *Inst) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000313 assert(Node && "Invalid null MDNode");
Devang Pateld5497a4b2009-09-16 18:09:00 +0000314 Inst->HasMetadata = true;
Devang Patel3eb5d332009-10-21 17:33:41 +0000315 MDMapTy &Info = MetadataStore[Inst];
316 if (Info.empty()) {
Devang Pateld5497a4b2009-09-16 18:09:00 +0000317 Info.push_back(std::make_pair(MDKind, Node));
318 MetadataStore.insert(std::make_pair(Inst, Info));
319 return;
320 }
Devang Patel5bf7a492009-09-29 20:30:57 +0000321
Devang Patel5bf7a492009-09-29 20:30:57 +0000322 // If there is an entry for this MDKind then replace it.
323 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
324 MDPairTy &P = Info[i];
325 if (P.first == MDKind) {
326 Info[i] = std::make_pair(MDKind, Node);
327 return;
328 }
329 }
330
331 // Otherwise add a new entry.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000332 Info.push_back(std::make_pair(MDKind, Node));
Devang Pateld5497a4b2009-09-16 18:09:00 +0000333}
334
Nick Lewyckya75fe182009-11-26 23:19:05 +0000335/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000336void MetadataContextImpl::removeMD(unsigned Kind, Instruction *Inst) {
Devang Patelb4034362009-09-29 20:42:25 +0000337 MDStoreTy::iterator I = MetadataStore.find(Inst);
338 if (I == MetadataStore.end())
339 return;
340
341 MDMapTy &Info = I->second;
342 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
343 MDPairTy &P = *MI;
344 if (P.first == Kind) {
345 Info.erase(MI);
346 return;
347 }
348 }
Devang Patelb4034362009-09-29 20:42:25 +0000349}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000350
Devang Patel3eb5d332009-10-21 17:33:41 +0000351/// removeAllMetadata - Remove all metadata attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000352void MetadataContextImpl::removeAllMetadata(Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000353 MetadataStore.erase(Inst);
354 Inst->HasMetadata = false;
Devang Patelb4034362009-09-29 20:42:25 +0000355}
356
Devang Patelebaa76e2009-10-14 17:02:49 +0000357/// copyMD - If metadata is attached with Instruction In1 then attach
358/// the same metadata to In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000359void MetadataContextImpl::copyMD(Instruction *In1, Instruction *In2) {
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000360 assert(In1 && In2 && "Invalid instruction!");
Devang Patel3eb5d332009-10-21 17:33:41 +0000361 MDMapTy &In1Info = MetadataStore[In1];
362 if (In1Info.empty())
Devang Patelebaa76e2009-10-14 17:02:49 +0000363 return;
364
Devang Patelebaa76e2009-10-14 17:02:49 +0000365 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000366 addMD(I->first, I->second, In2);
Devang Patelebaa76e2009-10-14 17:02:49 +0000367}
Devang Patelb4034362009-09-29 20:42:25 +0000368
Devang Patel0c35dbd2009-10-20 22:50:27 +0000369/// getMD - Get the metadata of given kind attached to an Instruction.
Devang Pateld5497a4b2009-09-16 18:09:00 +0000370/// If the metadata is not found then return 0.
Devang Patel1155fdf2009-10-22 19:36:54 +0000371MDNode *MetadataContextImpl::getMD(unsigned MDKind, const Instruction *Inst) {
Devang Patel3eb5d332009-10-21 17:33:41 +0000372 MDMapTy &Info = MetadataStore[Inst];
373 if (Info.empty())
Devang Patel5bf7a492009-09-29 20:30:57 +0000374 return NULL;
Devang Patel3eb5d332009-10-21 17:33:41 +0000375
Devang Pateld5497a4b2009-09-16 18:09:00 +0000376 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
377 if (I->first == MDKind)
Devang Patel084679e2009-10-22 18:25:28 +0000378 return I->second;
Devang Patel5bf7a492009-09-29 20:30:57 +0000379 return NULL;
Devang Pateld5497a4b2009-09-16 18:09:00 +0000380}
381
Devang Patel0c35dbd2009-10-20 22:50:27 +0000382/// getMDs - Get the metadata attached to an Instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000383void MetadataContextImpl::
Devang Patel6da5dbf2009-10-22 18:55:16 +0000384getMDs(const Instruction *Inst, SmallVectorImpl<MDPairTy> &MDs) const {
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000385 MDStoreTy::const_iterator I = MetadataStore.find(Inst);
Devang Pateldec23fd2009-09-16 20:21:17 +0000386 if (I == MetadataStore.end())
Devang Patel6da5dbf2009-10-22 18:55:16 +0000387 return;
Devang Pateld6dd2a02009-10-26 17:09:00 +0000388 MDs.resize(I->second.size());
Jeffrey Yasskinb40d3f72009-11-10 01:02:17 +0000389 for (MDMapTy::const_iterator MI = I->second.begin(), ME = I->second.end();
Devang Patel6da5dbf2009-10-22 18:55:16 +0000390 MI != ME; ++MI)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000391 // MD kinds are numbered from 1.
392 MDs[MI->first - 1] = std::make_pair(MI->first, MI->second);
Devang Pateldec23fd2009-09-16 20:21:17 +0000393}
394
Devang Patel09c319e2009-10-22 17:40:37 +0000395/// getHandlerNames - Populate client supplied smallvector using custome
396/// metadata name and ID.
Devang Patel1155fdf2009-10-22 19:36:54 +0000397void MetadataContextImpl::
Devang Patel0fffb492009-10-22 01:01:24 +0000398getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
Devang Pateld6dd2a02009-10-26 17:09:00 +0000399 Names.resize(MDHandlerNames.size());
Devang Patel0fffb492009-10-22 01:01:24 +0000400 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
401 E = MDHandlerNames.end(); I != E; ++I)
Devang Pateld6dd2a02009-10-26 17:09:00 +0000402 // MD Handlers are numbered from 1.
403 Names[I->second - 1] = std::make_pair(I->second, I->first());
Devang Patelaf206b82009-09-18 19:26:43 +0000404}
405
Devang Pateladd58652009-09-23 18:32:25 +0000406/// ValueIsCloned - This handler is used to update metadata store
407/// when In1 is cloned to create In2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000408void MetadataContextImpl::ValueIsCloned(const Instruction *In1,
409 Instruction *In2) {
Devang Pateladd58652009-09-23 18:32:25 +0000410 // Find Metadata handles for In1.
411 MDStoreTy::iterator I = MetadataStore.find(In1);
Chris Lattnerb0c23e82009-10-19 07:10:59 +0000412 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
Devang Pateladd58652009-09-23 18:32:25 +0000413
414 // FIXME : Give all metadata handlers a chance to adjust.
415
416 MDMapTy &In1Info = I->second;
417 MDMapTy In2Info;
418 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
Devang Patel084679e2009-10-22 18:25:28 +0000419 addMD(I->first, I->second, In2);
Devang Pateladd58652009-09-23 18:32:25 +0000420}
Devang Patele6f26a72009-10-13 17:00:54 +0000421
422/// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
423/// V2.
Devang Patel1155fdf2009-10-22 19:36:54 +0000424void MetadataContextImpl::ValueIsRAUWd(Value *V1, Value *V2) {
Devang Patele6f26a72009-10-13 17:00:54 +0000425 Instruction *I1 = dyn_cast<Instruction>(V1);
426 Instruction *I2 = dyn_cast<Instruction>(V2);
427 if (!I1 || !I2)
428 return;
429
430 // FIXME : Give custom handlers a chance to override this.
431 ValueIsCloned(I1, I2);
432}
Devang Patelebaa76e2009-10-14 17:02:49 +0000433
Devang Patel1155fdf2009-10-22 19:36:54 +0000434//===----------------------------------------------------------------------===//
435// MetadataContext implementation.
436//
437MetadataContext::MetadataContext()
438 : pImpl(new MetadataContextImpl()) { }
439MetadataContext::~MetadataContext() { delete pImpl; }
440
441/// isValidName - Return true if Name is a valid custom metadata handler name.
442bool MetadataContext::isValidName(StringRef MDName) {
443 if (MDName.empty())
444 return false;
445
446 if (!isalpha(MDName[0]))
447 return false;
448
449 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
450 ++I) {
451 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
452 return false;
453 }
454 return true;
455}
456
457/// registerMDKind - Register a new metadata kind and return its ID.
458/// A metadata kind can be registered only once.
459unsigned MetadataContext::registerMDKind(StringRef Name) {
460 assert(isValidName(Name) && "Invalid custome metadata name!");
461 return pImpl->registerMDKind(Name);
462}
463
464/// getMDKind - Return metadata kind. If the requested metadata kind
465/// is not registered then return 0.
466unsigned MetadataContext::getMDKind(StringRef Name) const {
467 return pImpl->getMDKind(Name);
468}
469
470/// getMD - Get the metadata of given kind attached to an Instruction.
471/// If the metadata is not found then return 0.
472MDNode *MetadataContext::getMD(unsigned Kind, const Instruction *Inst) {
473 return pImpl->getMD(Kind, Inst);
474}
475
476/// getMDs - Get the metadata attached to an Instruction.
477void MetadataContext::
478getMDs(const Instruction *Inst,
479 SmallVectorImpl<std::pair<unsigned, TrackingVH<MDNode> > > &MDs) const {
480 return pImpl->getMDs(Inst, MDs);
481}
482
483/// addMD - Attach the metadata of given kind to an Instruction.
484void MetadataContext::addMD(unsigned Kind, MDNode *Node, Instruction *Inst) {
485 pImpl->addMD(Kind, Node, Inst);
486}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000487
Nick Lewyckya75fe182009-11-26 23:19:05 +0000488/// removeMD - Remove metadata of given kind attached with an instruction.
Devang Patel1155fdf2009-10-22 19:36:54 +0000489void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
490 pImpl->removeMD(Kind, Inst);
491}
Nick Lewycky898e8f72009-11-26 22:54:26 +0000492
Devang Patel1155fdf2009-10-22 19:36:54 +0000493/// removeAllMetadata - Remove all metadata attached with an instruction.
494void MetadataContext::removeAllMetadata(Instruction *Inst) {
495 pImpl->removeAllMetadata(Inst);
496}
497
498/// copyMD - If metadata is attached with Instruction In1 then attach
499/// the same metadata to In2.
500void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
501 pImpl->copyMD(In1, In2);
502}
503
504/// getHandlerNames - Populate client supplied smallvector using custome
505/// metadata name and ID.
506void MetadataContext::
507getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&N) const {
508 pImpl->getHandlerNames(N);
509}
510
511/// ValueIsDeleted - This handler is used to update metadata store
512/// when a value is deleted.
513void MetadataContext::ValueIsDeleted(Instruction *Inst) {
514 pImpl->ValueIsDeleted(Inst);
515}
516void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
517 pImpl->ValueIsRAUWd(V1, V2);
518}
519
520/// ValueIsCloned - This handler is used to update metadata store
521/// when In1 is cloned to create In2.
522void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
523 pImpl->ValueIsCloned(In1, In2);
524}