1 //===-- Metadata.cpp - Implement Metadata classes -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/Metadata.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instruction.h"
19 #include "SymbolTableListTraitsImpl.h"
22 //===----------------------------------------------------------------------===//
23 // MetadataBase implementation.
26 //===----------------------------------------------------------------------===//
27 // MDString implementation.
29 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
30 LLVMContextImpl *pImpl = Context.pImpl;
31 StringMapEntry<MDString *> &Entry =
32 pImpl->MDStringCache.GetOrCreateValue(Str);
33 MDString *&S = Entry.getValue();
37 new MDString(Context, StringRef(Entry.getKeyData(), Entry.getKeyLength()));
40 //===----------------------------------------------------------------------===//
41 // MDNode implementation.
43 MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals)
44 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
46 Node = new ElementVH[NodeSize];
47 ElementVH *Ptr = Node;
48 for (unsigned i = 0; i != NumVals; ++i)
49 *Ptr++ = ElementVH(Vals[i], this);
52 void MDNode::Profile(FoldingSetNodeID &ID) const {
53 for (unsigned i = 0, e = getNumElements(); i != e; ++i)
54 ID.AddPointer(getElement(i));
57 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
58 LLVMContextImpl *pImpl = Context.pImpl;
60 for (unsigned i = 0; i != NumVals; ++i)
61 ID.AddPointer(Vals[i]);
66 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
70 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
72 // InsertPoint will have been set by the FindNodeOrInsertPos call.
73 N = new MDNode(Context, Vals, NumVals);
74 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
80 /// ~MDNode - Destroy MDNode.
83 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
84 pImpl->MDNodeSet.RemoveNode(this);
90 // Replace value from this node's element list.
91 void MDNode::replaceElement(Value *From, Value *To) {
92 if (From == To || !getType())
94 LLVMContext &Context = getType()->getContext();
95 LLVMContextImpl *pImpl = Context.pImpl;
97 // Find value. This is a linear search, do something if it consumes
98 // lot of time. It is possible that to have multiple instances of
99 // From in this MDNode's element list.
100 SmallVector<unsigned, 4> Indexes;
102 for (unsigned i = 0, e = getNumElements(); i != e; ++i, ++Index) {
103 Value *V = getElement(i);
105 Indexes.push_back(Index);
111 // Remove "this" from the context map.
112 pImpl->MDNodeSet.RemoveNode(this);
114 // Replace From element(s) in place.
115 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
118 Node[Index] = ElementVH(To, this);
121 // Insert updated "this" into the context's folding node set.
122 // If a node with same element list already exist then before inserting
123 // updated "this" into the folding node set, replace all uses of existing
124 // node with updated "this" node.
128 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
131 N->replaceAllUsesWith(this);
136 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
138 // InsertPoint will have been set by the FindNodeOrInsertPos call.
140 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
144 //===----------------------------------------------------------------------===//
145 // NamedMDNode implementation.
147 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
148 MetadataBase *const *MDs,
149 unsigned NumMDs, Module *ParentModule)
150 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
153 for (unsigned i = 0; i != NumMDs; ++i)
154 Node.push_back(WeakMetadataVH(MDs[i]));
157 ParentModule->getNamedMDList().push_back(this);
160 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
161 assert(NMD && "Invalid source NamedMDNode!");
162 SmallVector<MetadataBase *, 4> Elems;
163 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
164 Elems.push_back(NMD->getElement(i));
165 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
166 Elems.data(), Elems.size(), M);
169 /// eraseFromParent - Drop all references and remove the node from parent
171 void NamedMDNode::eraseFromParent() {
172 getParent()->getNamedMDList().erase(this);
175 /// dropAllReferences - Remove all uses and clear node vector.
176 void NamedMDNode::dropAllReferences() {
180 NamedMDNode::~NamedMDNode() {
184 //===----------------------------------------------------------------------===//
185 // MetadataContext implementation.
188 /// registerMDKind - Register a new metadata kind and return its ID.
189 /// A metadata kind can be registered only once.
190 unsigned MetadataContext::registerMDKind(StringRef Name) {
191 assert(isValidName(Name) && "Invalid custome metadata name!");
192 unsigned Count = MDHandlerNames.size();
193 assert(MDHandlerNames.count(Name) == 0 && "Already registered MDKind!");
194 return MDHandlerNames[Name] = Count + 1;
197 /// isValidName - Return true if Name is a valid custom metadata handler name.
198 bool MetadataContext::isValidName(StringRef MDName) {
202 if (!isalpha(MDName[0]))
205 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
207 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
213 /// getMDKind - Return metadata kind. If the requested metadata kind
214 /// is not registered then return 0.
215 unsigned MetadataContext::getMDKind(StringRef Name) const {
216 StringMap<unsigned>::const_iterator I = MDHandlerNames.find(Name);
217 if (I == MDHandlerNames.end()) {
218 assert(isValidName(Name) && "Invalid custome metadata name!");
222 return I->getValue();
225 /// addMD - Attach the metadata of given kind to an Instruction.
226 void MetadataContext::addMD(unsigned MDKind, MDNode *Node, Instruction *Inst) {
227 assert(Node && "Invalid null MDNode");
228 Inst->HasMetadata = true;
229 MDMapTy &Info = MetadataStore[Inst];
231 Info.push_back(std::make_pair(MDKind, Node));
232 MetadataStore.insert(std::make_pair(Inst, Info));
236 // If there is an entry for this MDKind then replace it.
237 for (unsigned i = 0, e = Info.size(); i != e; ++i) {
238 MDPairTy &P = Info[i];
239 if (P.first == MDKind) {
240 Info[i] = std::make_pair(MDKind, Node);
245 // Otherwise add a new entry.
246 Info.push_back(std::make_pair(MDKind, Node));
249 /// removeMD - Remove metadata of given kind attached with an instuction.
250 void MetadataContext::removeMD(unsigned Kind, Instruction *Inst) {
251 MDStoreTy::iterator I = MetadataStore.find(Inst);
252 if (I == MetadataStore.end())
255 MDMapTy &Info = I->second;
256 for (MDMapTy::iterator MI = Info.begin(), ME = Info.end(); MI != ME; ++MI) {
258 if (P.first == Kind) {
265 /// removeAllMetadata - Remove all metadata attached with an instruction.
266 void MetadataContext::removeAllMetadata(Instruction *Inst) {
267 MetadataStore.erase(Inst);
268 Inst->HasMetadata = false;
271 /// copyMD - If metadata is attached with Instruction In1 then attach
272 /// the same metadata to In2.
273 void MetadataContext::copyMD(Instruction *In1, Instruction *In2) {
274 assert(In1 && In2 && "Invalid instruction!");
275 MDMapTy &In1Info = MetadataStore[In1];
279 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
280 if (MDNode *MD = dyn_cast_or_null<MDNode>(I->second))
281 addMD(I->first, MD, In2);
284 /// getMD - Get the metadata of given kind attached to an Instruction.
285 /// If the metadata is not found then return 0.
286 MDNode *MetadataContext::getMD(unsigned MDKind, const Instruction *Inst) {
287 MDMapTy &Info = MetadataStore[Inst];
291 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
292 if (I->first == MDKind)
293 return dyn_cast_or_null<MDNode>(I->second);
297 /// getMDs - Get the metadata attached to an Instruction.
298 const MetadataContext::MDMapTy *
299 MetadataContext::getMDs(const Instruction *Inst) {
300 MDStoreTy::iterator I = MetadataStore.find(Inst);
301 if (I == MetadataStore.end())
307 /// getHandlerNames - Populate client supplied smallvector using custome
308 /// metadata name and ID.
309 void MetadataContext::
310 getHandlerNames(SmallVectorImpl<std::pair<unsigned, StringRef> >&Names) const {
311 for (StringMap<unsigned>::const_iterator I = MDHandlerNames.begin(),
312 E = MDHandlerNames.end(); I != E; ++I)
313 Names.push_back(std::make_pair(I->second, I->first()));
314 std::sort(Names.begin(), Names.end());
317 /// ValueIsCloned - This handler is used to update metadata store
318 /// when In1 is cloned to create In2.
319 void MetadataContext::ValueIsCloned(const Instruction *In1, Instruction *In2) {
320 // Find Metadata handles for In1.
321 MDStoreTy::iterator I = MetadataStore.find(In1);
322 assert(I != MetadataStore.end() && "Invalid custom metadata info!");
324 // FIXME : Give all metadata handlers a chance to adjust.
326 MDMapTy &In1Info = I->second;
328 for (MDMapTy::iterator I = In1Info.begin(), E = In1Info.end(); I != E; ++I)
329 if (MDNode *MD = dyn_cast_or_null<MDNode>(I->second))
330 addMD(I->first, MD, In2);
333 /// ValueIsRAUWd - This handler is used when V1's all uses are replaced by
335 void MetadataContext::ValueIsRAUWd(Value *V1, Value *V2) {
336 Instruction *I1 = dyn_cast<Instruction>(V1);
337 Instruction *I2 = dyn_cast<Instruction>(V2);
341 // FIXME : Give custom handlers a chance to override this.
342 ValueIsCloned(I1, I2);