pass machinemoduleinfo down into getSymbolForDwarfGlobalReference,
[llvm/avr.git] / lib / VMCore / Metadata.cpp
blob2c80a60a8cf707b2421d8b4489d2d6c1a14d53c3
1 //===-- 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.
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"
20 using namespace llvm;
22 //===----------------------------------------------------------------------===//
23 //MetadataBase implementation
26 /// resizeOperands - Metadata keeps track of other metadata uses using
27 /// OperandList. Resize this list to hold anticipated number of metadata
28 /// operands.
29 void MetadataBase::resizeOperands(unsigned NumOps) {
30 unsigned e = getNumOperands();
31 if (NumOps == 0) {
32 NumOps = e*2;
33 if (NumOps < 2) NumOps = 2;
34 } else if (NumOps > NumOperands) {
35 // No resize needed.
36 if (ReservedSpace >= NumOps) return;
37 } else if (NumOps == NumOperands) {
38 if (ReservedSpace == NumOps) return;
39 } else {
40 return;
43 ReservedSpace = NumOps;
44 Use *OldOps = OperandList;
45 Use *NewOps = allocHungoffUses(NumOps);
46 std::copy(OldOps, OldOps + e, NewOps);
47 OperandList = NewOps;
48 if (OldOps) Use::zap(OldOps, OldOps + e, true);
50 //===----------------------------------------------------------------------===//
51 //MDString implementation
53 MDString *MDString::get(LLVMContext &Context, const StringRef &Str) {
54 LLVMContextImpl *pImpl = Context.pImpl;
55 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
56 StringMapEntry<MDString *> &Entry =
57 pImpl->MDStringCache.GetOrCreateValue(Str);
58 MDString *&S = Entry.getValue();
59 if (!S) S = new MDString(Context, Entry.getKeyData(),
60 Entry.getKeyLength());
62 return S;
65 //===----------------------------------------------------------------------===//
66 //MDNode implementation
68 MDNode::MDNode(LLVMContext &C, Value*const* Vals, unsigned NumVals)
69 : MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
70 NumOperands = 0;
71 resizeOperands(NumVals);
72 for (unsigned i = 0; i != NumVals; ++i) {
73 // Only record metadata uses.
74 if (MetadataBase *MB = dyn_cast_or_null<MetadataBase>(Vals[i]))
75 OperandList[NumOperands++] = MB;
76 else if(Vals[i] &&
77 Vals[i]->getType()->getTypeID() == Type::MetadataTyID)
78 OperandList[NumOperands++] = Vals[i];
79 Node.push_back(ElementVH(Vals[i], this));
83 void MDNode::Profile(FoldingSetNodeID &ID) const {
84 for (const_elem_iterator I = elem_begin(), E = elem_end(); I != E; ++I)
85 ID.AddPointer(*I);
88 MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
89 LLVMContextImpl *pImpl = Context.pImpl;
90 FoldingSetNodeID ID;
91 for (unsigned i = 0; i != NumVals; ++i)
92 ID.AddPointer(Vals[i]);
94 pImpl->ConstantsLock.reader_acquire();
95 void *InsertPoint;
96 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
97 pImpl->ConstantsLock.reader_release();
99 if (!N) {
100 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
101 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
102 if (!N) {
103 // InsertPoint will have been set by the FindNodeOrInsertPos call.
104 N = new MDNode(Context, Vals, NumVals);
105 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
109 return N;
112 /// dropAllReferences - Remove all uses and clear node vector.
113 void MDNode::dropAllReferences() {
114 User::dropAllReferences();
115 Node.clear();
118 MDNode::~MDNode() {
120 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
121 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
122 pImpl->MDNodeSet.RemoveNode(this);
124 dropAllReferences();
127 // Replace value from this node's element list.
128 void MDNode::replaceElement(Value *From, Value *To) {
129 if (From == To || !getType())
130 return;
131 LLVMContext &Context = getType()->getContext();
132 LLVMContextImpl *pImpl = Context.pImpl;
134 // Find value. This is a linear search, do something if it consumes
135 // lot of time. It is possible that to have multiple instances of
136 // From in this MDNode's element list.
137 SmallVector<unsigned, 4> Indexes;
138 unsigned Index = 0;
139 for (SmallVector<ElementVH, 4>::iterator I = Node.begin(),
140 E = Node.end(); I != E; ++I, ++Index) {
141 Value *V = *I;
142 if (V && V == From)
143 Indexes.push_back(Index);
146 if (Indexes.empty())
147 return;
149 // Remove "this" from the context map.
151 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
152 pImpl->MDNodeSet.RemoveNode(this);
155 // MDNode only lists metadata elements in operand list, because MDNode
156 // used by MDNode is considered a valid use. However on the side, MDNode
157 // using a non-metadata value is not considered a "use" of non-metadata
158 // value.
159 SmallVector<unsigned, 4> OpIndexes;
160 unsigned OpIndex = 0;
161 for (User::op_iterator OI = op_begin(), OE = op_end();
162 OI != OE; ++OI, OpIndex++) {
163 if (*OI == From)
164 OpIndexes.push_back(OpIndex);
166 if (MetadataBase *MDTo = dyn_cast_or_null<MetadataBase>(To)) {
167 for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
168 OE = OpIndexes.end(); OI != OE; ++OI)
169 setOperand(*OI, MDTo);
170 } else {
171 for (SmallVector<unsigned, 4>::iterator OI = OpIndexes.begin(),
172 OE = OpIndexes.end(); OI != OE; ++OI)
173 setOperand(*OI, 0);
176 // Replace From element(s) in place.
177 for (SmallVector<unsigned, 4>::iterator I = Indexes.begin(), E = Indexes.end();
178 I != E; ++I) {
179 unsigned Index = *I;
180 Node[Index] = ElementVH(To, this);
183 // Insert updated "this" into the context's folding node set.
184 // If a node with same element list already exist then before inserting
185 // updated "this" into the folding node set, replace all uses of existing
186 // node with updated "this" node.
187 FoldingSetNodeID ID;
188 Profile(ID);
189 pImpl->ConstantsLock.reader_acquire();
190 void *InsertPoint;
191 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
192 pImpl->ConstantsLock.reader_release();
194 if (N) {
195 N->replaceAllUsesWith(this);
196 delete N;
197 N = 0;
201 sys::SmartScopedWriter<true> Writer(pImpl->ConstantsLock);
202 N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
203 if (!N) {
204 // InsertPoint will have been set by the FindNodeOrInsertPos call.
205 N = this;
206 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
211 //===----------------------------------------------------------------------===//
212 //NamedMDNode implementation
214 NamedMDNode::NamedMDNode(LLVMContext &C, const Twine &N,
215 MetadataBase*const* MDs,
216 unsigned NumMDs, Module *ParentModule)
217 : MetadataBase(Type::getMetadataTy(C), Value::NamedMDNodeVal), Parent(0) {
218 setName(N);
219 NumOperands = 0;
220 resizeOperands(NumMDs);
222 for (unsigned i = 0; i != NumMDs; ++i) {
223 if (MDs[i])
224 OperandList[NumOperands++] = MDs[i];
225 Node.push_back(WeakMetadataVH(MDs[i]));
227 if (ParentModule)
228 ParentModule->getNamedMDList().push_back(this);
231 NamedMDNode *NamedMDNode::Create(const NamedMDNode *NMD, Module *M) {
232 assert (NMD && "Invalid source NamedMDNode!");
233 SmallVector<MetadataBase *, 4> Elems;
234 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i)
235 Elems.push_back(NMD->getElement(i));
236 return new NamedMDNode(NMD->getContext(), NMD->getName().data(),
237 Elems.data(), Elems.size(), M);
240 /// eraseFromParent - Drop all references and remove the node from parent
241 /// module.
242 void NamedMDNode::eraseFromParent() {
243 getParent()->getNamedMDList().erase(this);
246 /// dropAllReferences - Remove all uses and clear node vector.
247 void NamedMDNode::dropAllReferences() {
248 User::dropAllReferences();
249 Node.clear();
252 NamedMDNode::~NamedMDNode() {
253 dropAllReferences();
256 //===----------------------------------------------------------------------===//
257 //Metadata implementation
260 /// RegisterMDKind - Register a new metadata kind and return its ID.
261 /// A metadata kind can be registered only once.
262 MDKindID Metadata::RegisterMDKind(const char *Name) {
263 MDKindID Count = MDHandlerNames.size();
264 StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
265 assert(I == MDHandlerNames.end() && "Already registered MDKind!");
266 MDHandlerNames[Name] = Count + 1;
267 return Count + 1;
270 /// getMDKind - Return metadata kind. If the requested metadata kind
271 /// is not registered then return 0.
272 MDKindID Metadata::getMDKind(const char *Name) {
273 StringMap<unsigned>::iterator I = MDHandlerNames.find(Name);
274 if (I == MDHandlerNames.end())
275 return 0;
277 return I->getValue();
280 /// setMD - Attach the metadata of given kind with an Instruction.
281 void Metadata::setMD(MDKindID MDKind, MDNode *Node, Instruction *Inst) {
282 MDStoreTy::iterator I = MetadataStore.find(Inst);
283 Inst->HasMetadata = true;
284 if (I == MetadataStore.end()) {
285 MDMapTy Info;
286 Info.push_back(std::make_pair(MDKind, Node));
287 MetadataStore.insert(std::make_pair(Inst, Info));
288 return;
291 MDMapTy &Info = I->second;
292 Info.push_back(std::make_pair(MDKind, Node));
293 return;
296 /// getMD - Get the metadata of given kind attached with an Instruction.
297 /// If the metadata is not found then return 0.
298 MDNode *Metadata::getMD(MDKindID MDKind, const Instruction *Inst) {
299 MDNode *Node = NULL;
300 MDStoreTy::iterator I = MetadataStore.find(Inst);
301 if (I == MetadataStore.end())
302 return Node;
304 MDMapTy &Info = I->second;
305 for (MDMapTy::iterator I = Info.begin(), E = Info.end(); I != E; ++I)
306 if (I->first == MDKind)
307 Node = dyn_cast_or_null<MDNode>(I->second);
308 return Node;
311 /// getMDs - Get the metadata attached with an Instruction.
312 const Metadata::MDMapTy *Metadata::getMDs(const Instruction *Inst) {
313 MDStoreTy::iterator I = MetadataStore.find(Inst);
314 if (I == MetadataStore.end())
315 return NULL;
317 return &(I->second);
320 /// ValueIsDeleted - This handler is used to update metadata store
321 /// when a value is deleted.
322 void Metadata::ValueIsDeleted(const Instruction *Inst) {
323 // Find Metadata handles for this instruction.
324 MDStoreTy::iterator I = MetadataStore.find(Inst);
325 if (I == MetadataStore.end())
326 return;
327 MDMapTy &Info = I->second;
329 // FIXME : Give all metadata handlers a chance to adjust.
331 // Remove the entries for this instruction.
332 Info.clear();
333 MetadataStore.erase(Inst);