LLVM API Documentation
00001 //===-- Metadata.cpp - Implement Metadata classes -------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the Metadata classes. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "LLVMContextImpl.h" 00015 #include "llvm/Metadata.h" 00016 #include "llvm/LLVMContext.h" 00017 #include "llvm/Module.h" 00018 #include "SymbolTableListTraitsImpl.h" 00019 using namespace llvm; 00020 00021 //===----------------------------------------------------------------------===// 00022 //MetadataBase implementation 00023 // 00024 00025 /// resizeOperands - Metadata keeps track of other metadata uses using 00026 /// OperandList. Resize this list to hold anticipated number of metadata 00027 /// operands. 00028 void MetadataBase::resizeOperands(unsigned NumOps) { 00029 unsigned e = getNumOperands(); 00030 if (NumOps == 0) { 00031 NumOps = e*2; 00032 if (NumOps < 2) NumOps = 2; 00033 } else if (NumOps > NumOperands) { 00034 // No resize needed. 00035 if (ReservedSpace >= NumOps) return; 00036 } else if (NumOps == NumOperands) { 00037 if (ReservedSpace == NumOps) return; 00038 } else { 00039 return; 00040 } 00041 00042 ReservedSpace = NumOps; 00043 Use *OldOps = OperandList; 00044 Use *NewOps = allocHungoffUses(NumOps); 00045 std::copy(OldOps, OldOps + e, NewOps); 00046 OperandList = NewOps; 00047 if (OldOps) Use::zap(OldOps, OldOps + e, true); 00048 } 00049 //===----------------------------------------------------------------------===// 00050 //MDString implementation 00051 // 00052 MDString *MDString::get(LLVMContext &Context, const StringRef &Str) { 00053 LLVMContextImpl *pImpl = Context.pImpl; 00054 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock); 00055 StringMapEntry<MDString *> &Entry = 00056 pImpl->MDStringCache.GetOrCreateValue(Str); 00057 MDString *&S = Entry.getValue(); 00058 if (!S) S = new MDString(Context, Entry.getKeyData(), 00059 Entry.getKeyLength()); 00060 00061 return S; 00062 } 00063 00064 //===----------------------------------------------------------------------===// 00065 //MDNode implementation 00066 // 00067 MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals) 00068 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) { 00069 NumOperands = 0; 00070 resizeOperands(NumVals); 00071 for (unsigned i = 0; i != NumVals; ++i) { 00072 // Only record metadata uses. 00073 if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i])) 00074 OperandList[NumOperands++] = MB; 00075 Node.push_back(WeakVH(Vals[i])); 00076 } 00077 } 00078 00079 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) { 00080 LLVMContextImpl *pImpl = Context.pImpl; 00081 std::vector<Value*> V; 00082 V.reserve(NumVals); 00083 for (unsigned i = 0; i < NumVals; ++i) 00084 V.push_back(Vals[i]); 00085 00086 return pImpl->MDNodes.getOrCreate(Type::getMetadataTy(Context), V); 00087 } 00088 00089 /// dropAllReferences - Remove all uses and clear node vector. 00090 void MDNode::dropAllReferences() { 00091 User::dropAllReferences(); 00092 Node.clear(); 00093 } 00094 00095 static std::vector<Value*> getValType(MDNode *N) { 00096 std::vector<Value*> Elements; 00097 Elements.reserve(N->getNumElements()); 00098 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) 00099 Elements.push_back(N->getElement(i)); 00100 return Elements; 00101 } 00102 00103 MDNode::~MDNode() { 00104 dropAllReferences(); 00105 getType()->getContext().pImpl->MDNodes.remove(this); 00106 } 00107 00108 //===----------------------------------------------------------------------===// 00109 //NamedMDNode implementation 00110 // 00111 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N, 00112 MetadataBase*const* MDs, 00113 unsigned NumMDs, Module *ParentModule) 00114 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) { 00115 setName(N); 00116 NumOperands = 0; 00117 resizeOperands(NumMDs); 00118 00119 for (unsigned i = 0; i != NumMDs; ++i) { 00120 if (MDs[i]) 00121 OperandList[NumOperands++] = MDs[i]; 00122 Node.push_back(WeakMetadataVH(MDs[i])); 00123 } 00124 if (ParentModule) 00125 ParentModule->getNamedMDList().push_back(this); 00126 } 00127 00128 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) { 00129 assert (NMD && "Invalid source NamedMDNode!"); 00130 SmallVector<MetadataBase *, 4> Elems; 00131 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) 00132 Elems.push_back(NMD->getElement(i)); 00133 return new NamedMDNode(NMD->getContext(), NMD->getName().data(), 00134 Elems.data(), Elems.size(), M); 00135 } 00136 00137 /// eraseFromParent - Drop all references and remove the node from parent 00138 /// module. 00139 void NamedMDNode::eraseFromParent() { 00140 getParent()->getNamedMDList().erase(this); 00141 } 00142 00143 /// dropAllReferences - Remove all uses and clear node vector. 00144 void NamedMDNode::dropAllReferences() { 00145 User::dropAllReferences(); 00146 Node.clear(); 00147 } 00148 00149 NamedMDNode::~NamedMDNode() { 00150 dropAllReferences(); 00151 }