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 /// resizeOperands - Metadata keeps track of other metadata uses using
27 /// OperandList. Resize this list to hold anticipated number of metadata
29 void MetadataBase::resizeOperands(unsigned NumOps
) {
30 unsigned e
= getNumOperands();
33 if (NumOps
< 2) NumOps
= 2;
34 } else if (NumOps
> NumOperands
) {
36 if (ReservedSpace
>= NumOps
) return;
37 } else if (NumOps
== NumOperands
) {
38 if (ReservedSpace
== NumOps
) return;
43 ReservedSpace
= NumOps
;
44 Use
*OldOps
= OperandList
;
45 Use
*NewOps
= allocHungoffUses(NumOps
);
46 std::copy(OldOps
, OldOps
+ e
, 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());
65 //===----------------------------------------------------------------------===//
66 //MDNode implementation
68 MDNode::MDNode(LLVMContext
&C
, Value
*const* Vals
, unsigned NumVals
)
69 : MetadataBase(Type::getMetadataTy(C
), Value::MDNodeVal
) {
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
;
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
)
88 MDNode
*MDNode::get(LLVMContext
&Context
, Value
*const* Vals
, unsigned NumVals
) {
89 LLVMContextImpl
*pImpl
= Context
.pImpl
;
91 for (unsigned i
= 0; i
!= NumVals
; ++i
)
92 ID
.AddPointer(Vals
[i
]);
94 pImpl
->ConstantsLock
.reader_acquire();
96 MDNode
*N
= pImpl
->MDNodeSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
97 pImpl
->ConstantsLock
.reader_release();
100 sys::SmartScopedWriter
<true> Writer(pImpl
->ConstantsLock
);
101 N
= pImpl
->MDNodeSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
103 // InsertPoint will have been set by the FindNodeOrInsertPos call.
104 N
= new MDNode(Context
, Vals
, NumVals
);
105 pImpl
->MDNodeSet
.InsertNode(N
, InsertPoint
);
112 /// dropAllReferences - Remove all uses and clear node vector.
113 void MDNode::dropAllReferences() {
114 User::dropAllReferences();
120 LLVMContextImpl
*pImpl
= getType()->getContext().pImpl
;
121 sys::SmartScopedWriter
<true> Writer(pImpl
->ConstantsLock
);
122 pImpl
->MDNodeSet
.RemoveNode(this);
127 // Replace value from this node's element list.
128 void MDNode::replaceElement(Value
*From
, Value
*To
) {
129 if (From
== To
|| !getType())
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
;
139 for (SmallVector
<ElementVH
, 4>::iterator I
= Node
.begin(),
140 E
= Node
.end(); I
!= E
; ++I
, ++Index
) {
143 Indexes
.push_back(Index
);
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
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
++) {
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
);
171 for (SmallVector
<unsigned, 4>::iterator OI
= OpIndexes
.begin(),
172 OE
= OpIndexes
.end(); OI
!= OE
; ++OI
)
176 // Replace From element(s) in place.
177 for (SmallVector
<unsigned, 4>::iterator I
= Indexes
.begin(), E
= Indexes
.end();
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.
189 pImpl
->ConstantsLock
.reader_acquire();
191 MDNode
*N
= pImpl
->MDNodeSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
192 pImpl
->ConstantsLock
.reader_release();
195 N
->replaceAllUsesWith(this);
201 sys::SmartScopedWriter
<true> Writer(pImpl
->ConstantsLock
);
202 N
= pImpl
->MDNodeSet
.FindNodeOrInsertPos(ID
, InsertPoint
);
204 // InsertPoint will have been set by the FindNodeOrInsertPos call.
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) {
220 resizeOperands(NumMDs
);
222 for (unsigned i
= 0; i
!= NumMDs
; ++i
) {
224 OperandList
[NumOperands
++] = MDs
[i
];
225 Node
.push_back(WeakMetadataVH(MDs
[i
]));
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
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();
252 NamedMDNode::~NamedMDNode() {
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;
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())
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()) {
286 Info
.push_back(std::make_pair(MDKind
, Node
));
287 MetadataStore
.insert(std::make_pair(Inst
, Info
));
291 MDMapTy
&Info
= I
->second
;
292 Info
.push_back(std::make_pair(MDKind
, Node
));
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
) {
300 MDStoreTy::iterator I
= MetadataStore
.find(Inst
);
301 if (I
== MetadataStore
.end())
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
);
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())
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())
327 MDMapTy
&Info
= I
->second
;
329 // FIXME : Give all metadata handlers a chance to adjust.
331 // Remove the entries for this instruction.
333 MetadataStore
.erase(Inst
);