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 "llvm/Metadata.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/LLVMContext.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "SymbolTableListTraitsImpl.h"
24 #include "llvm/Support/LeakDetector.h"
25 #include "llvm/Support/ValueHandle.h"
28 //===----------------------------------------------------------------------===//
29 // MDString implementation.
32 void MDString::anchor() { }
34 MDString::MDString(LLVMContext &C)
35 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
37 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
38 LLVMContextImpl *pImpl = Context.pImpl;
39 StringMapEntry<Value*> &Entry =
40 pImpl->MDStringCache.GetOrCreateValue(Str);
41 Value *&S = Entry.getValue();
42 if (!S) S = new MDString(Context);
43 S->setValueName(&Entry);
44 return cast<MDString>(S);
47 //===----------------------------------------------------------------------===//
48 // MDNodeOperand implementation.
51 // Use CallbackVH to hold MDNode operands.
53 class MDNodeOperand : public CallbackVH {
55 MDNodeOperand *Cur = this;
57 while (Cur->getValPtrInt() != 1)
60 assert(Cur->getValPtrInt() == 1 &&
61 "Couldn't find the beginning of the operand list!");
62 return reinterpret_cast<MDNode*>(Cur) - 1;
66 MDNodeOperand(Value *V) : CallbackVH(V) {}
70 unsigned IsFirst = this->getValPtrInt();
72 this->setAsFirstOperand(IsFirst);
75 /// setAsFirstOperand - Accessor method to mark the operand as the first in
77 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
79 virtual void deleted();
80 virtual void allUsesReplacedWith(Value *NV);
82 } // end namespace llvm.
85 void MDNodeOperand::deleted() {
86 getParent()->replaceOperand(this, 0);
89 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
90 getParent()->replaceOperand(this, NV);
93 //===----------------------------------------------------------------------===//
94 // MDNode implementation.
97 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
98 /// the end of the MDNode.
99 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
100 // Use <= instead of < to permit a one-past-the-end address.
101 assert(Op <= N->getNumOperands() && "Invalid operand number");
102 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
105 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
106 MDNodeOperand *Op = getOperandPtr(this, i);
107 replaceOperand(Op, Val);
110 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
111 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
112 NumOperands = Vals.size();
115 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
117 // Initialize the operand list, which is co-allocated on the end of the node.
119 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
120 Op != E; ++Op, ++i) {
121 new (Op) MDNodeOperand(Vals[i]);
123 // Mark the first MDNodeOperand as being the first in the list of operands.
125 Op->setAsFirstOperand(1);
129 /// ~MDNode - Destroy MDNode.
131 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
132 "Not being destroyed through destroy()?");
133 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
134 if (isNotUniqued()) {
135 pImpl->NonUniquedMDNodes.erase(this);
137 pImpl->MDNodeSet.RemoveNode(this);
140 // Destroy the operands.
141 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
143 Op->~MDNodeOperand();
146 static const Function *getFunctionForValue(Value *V) {
148 if (Instruction *I = dyn_cast<Instruction>(V)) {
149 BasicBlock *BB = I->getParent();
150 return BB ? BB->getParent() : 0;
152 if (Argument *A = dyn_cast<Argument>(V))
153 return A->getParent();
154 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
155 return BB->getParent();
156 if (MDNode *MD = dyn_cast<MDNode>(V))
157 return MD->getFunction();
162 static const Function *assertLocalFunction(const MDNode *N) {
163 if (!N->isFunctionLocal()) return 0;
165 // FIXME: This does not handle cyclic function local metadata.
166 const Function *F = 0, *NewF = 0;
167 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
168 if (Value *V = N->getOperand(i)) {
169 if (MDNode *MD = dyn_cast<MDNode>(V))
170 NewF = assertLocalFunction(MD);
172 NewF = getFunctionForValue(V);
177 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
183 // getFunction - If this metadata is function-local and recursively has a
184 // function-local operand, return the first such operand's parent function.
185 // Otherwise, return null. getFunction() should not be used for performance-
186 // critical code because it recursively visits all the MDNode's operands.
187 const Function *MDNode::getFunction() const {
189 return assertLocalFunction(this);
191 if (!isFunctionLocal()) return NULL;
192 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
193 if (const Function *F = getFunctionForValue(getOperand(i)))
199 // destroy - Delete this node. Only when there are no uses.
200 void MDNode::destroy() {
201 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
202 // Placement delete, the free the memory.
207 /// isFunctionLocalValue - Return true if this is a value that would require a
208 /// function-local MDNode.
209 static bool isFunctionLocalValue(Value *V) {
210 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
211 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
214 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
215 FunctionLocalness FL, bool Insert) {
216 LLVMContextImpl *pImpl = Context.pImpl;
218 // Add all the operand pointers. Note that we don't have to add the
219 // isFunctionLocal bit because that's implied by the operands.
220 // Note that if the operands are later nulled out, the node will be
221 // removed from the uniquing map.
223 for (unsigned i = 0; i != Vals.size(); ++i)
224 ID.AddPointer(Vals[i]);
227 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
232 bool isFunctionLocal = false;
235 for (unsigned i = 0; i != Vals.size(); ++i) {
238 if (isFunctionLocalValue(V)) {
239 isFunctionLocal = true;
245 isFunctionLocal = false;
248 isFunctionLocal = true;
252 // Coallocate space for the node and Operands together, then placement new.
253 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
254 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
256 // Cache the operand hash.
257 N->Hash = ID.ComputeHash();
259 // InsertPoint will have been set by the FindNodeOrInsertPos call.
260 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
265 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
266 return getMDNode(Context, Vals, FL_Unknown);
269 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
270 ArrayRef<Value*> Vals,
271 bool isFunctionLocal) {
272 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
275 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
276 return getMDNode(Context, Vals, FL_Unknown, false);
279 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
281 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
282 N = new (N) MDNode(Context, Vals, FL_No);
283 N->setValueSubclassData(N->getSubclassDataFromValue() |
285 LeakDetector::addGarbageObject(N);
289 void MDNode::deleteTemporary(MDNode *N) {
290 assert(N->use_empty() && "Temporary MDNode has uses!");
291 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
292 "Deleting a non-temporary uniqued node!");
293 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
294 "Deleting a non-temporary non-uniqued node!");
295 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
296 "Temporary MDNode does not have NotUniquedBit set!");
297 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
298 "Temporary MDNode has DestroyFlag set!");
299 LeakDetector::removeGarbageObject(N);
303 /// getOperand - Return specified operand.
304 Value *MDNode::getOperand(unsigned i) const {
305 return *getOperandPtr(const_cast<MDNode*>(this), i);
308 void MDNode::Profile(FoldingSetNodeID &ID) const {
309 // Add all the operand pointers. Note that we don't have to add the
310 // isFunctionLocal bit because that's implied by the operands.
311 // Note that if the operands are later nulled out, the node will be
312 // removed from the uniquing map.
313 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
314 ID.AddPointer(getOperand(i));
317 void MDNode::setIsNotUniqued() {
318 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
319 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
320 pImpl->NonUniquedMDNodes.insert(this);
323 // Replace value from this node's operand list.
324 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
327 // If is possible that someone did GV->RAUW(inst), replacing a global variable
328 // with an instruction or some other function-local object. If this is a
329 // non-function-local MDNode, it can't point to a function-local object.
330 // Handle this case by implicitly dropping the MDNode reference to null.
331 // Likewise if the MDNode is function-local but for a different function.
332 if (To && isFunctionLocalValue(To)) {
333 if (!isFunctionLocal())
336 const Function *F = getFunction();
337 const Function *FV = getFunctionForValue(To);
338 // Metadata can be function-local without having an associated function.
339 // So only consider functions to have changed if non-null.
340 if (F && FV && F != FV)
348 // Update the operand.
351 // If this node is already not being uniqued (because one of the operands
352 // already went to null), then there is nothing else to do here.
353 if (isNotUniqued()) return;
355 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
357 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
358 // this node to remove it, so we don't care what state the operands are in.
359 pImpl->MDNodeSet.RemoveNode(this);
361 // If we are dropping an argument to null, we choose to not unique the MDNode
362 // anymore. This commonly occurs during destruction, and uniquing these
363 // brings little reuse. Also, this means we don't need to include
364 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
370 // Now that the node is out of the folding set, get ready to reinsert it.
371 // First, check to see if another node with the same operands already exists
372 // in the set. If so, then this node is redundant.
376 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
377 replaceAllUsesWith(N);
382 // Cache the operand hash.
383 Hash = ID.ComputeHash();
384 // InsertPoint will have been set by the FindNodeOrInsertPos call.
385 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
387 // If this MDValue was previously function-local but no longer is, clear
388 // its function-local flag.
389 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
390 bool isStillFunctionLocal = false;
391 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
392 Value *V = getOperand(i);
394 if (isFunctionLocalValue(V)) {
395 isStillFunctionLocal = true;
399 if (!isStillFunctionLocal)
400 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
404 //===----------------------------------------------------------------------===//
405 // NamedMDNode implementation.
408 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
409 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
412 NamedMDNode::NamedMDNode(const Twine &N)
413 : Name(N.str()), Parent(0),
414 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
417 NamedMDNode::~NamedMDNode() {
419 delete &getNMDOps(Operands);
422 /// getNumOperands - Return number of NamedMDNode operands.
423 unsigned NamedMDNode::getNumOperands() const {
424 return (unsigned)getNMDOps(Operands).size();
427 /// getOperand - Return specified operand.
428 MDNode *NamedMDNode::getOperand(unsigned i) const {
429 assert(i < getNumOperands() && "Invalid Operand number!");
430 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
433 /// addOperand - Add metadata Operand.
434 void NamedMDNode::addOperand(MDNode *M) {
435 assert(!M->isFunctionLocal() &&
436 "NamedMDNode operands must not be function-local!");
437 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
440 /// eraseFromParent - Drop all references and remove the node from parent
442 void NamedMDNode::eraseFromParent() {
443 getParent()->eraseNamedMetadata(this);
446 /// dropAllReferences - Remove all uses and clear node vector.
447 void NamedMDNode::dropAllReferences() {
448 getNMDOps(Operands).clear();
451 /// getName - Return a constant reference to this named metadata's name.
452 StringRef NamedMDNode::getName() const {
453 return StringRef(Name);
456 //===----------------------------------------------------------------------===//
457 // Instruction Metadata method implementations.
460 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
461 if (Node == 0 && !hasMetadata()) return;
462 setMetadata(getContext().getMDKindID(Kind), Node);
465 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
466 return getMetadataImpl(getContext().getMDKindID(Kind));
469 /// setMetadata - Set the metadata of of the specified kind to the specified
470 /// node. This updates/replaces metadata if already present, or removes it if
472 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
473 if (Node == 0 && !hasMetadata()) return;
475 // Handle 'dbg' as a special case since it is not stored in the hash table.
476 if (KindID == LLVMContext::MD_dbg) {
477 DbgLoc = DebugLoc::getFromDILocation(Node);
481 // Handle the case when we're adding/updating metadata on an instruction.
483 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
484 assert(!Info.empty() == hasMetadataHashEntry() &&
485 "HasMetadata bit is wonked");
487 setHasMetadataHashEntry(true);
489 // Handle replacement of an existing value.
490 for (unsigned i = 0, e = Info.size(); i != e; ++i)
491 if (Info[i].first == KindID) {
492 Info[i].second = Node;
497 // No replacement, just add it to the list.
498 Info.push_back(std::make_pair(KindID, Node));
502 // Otherwise, we're removing metadata from an instruction.
503 assert((hasMetadataHashEntry() ==
504 getContext().pImpl->MetadataStore.count(this)) &&
505 "HasMetadata bit out of date!");
506 if (!hasMetadataHashEntry())
507 return; // Nothing to remove!
508 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
510 // Common case is removing the only entry.
511 if (Info.size() == 1 && Info[0].first == KindID) {
512 getContext().pImpl->MetadataStore.erase(this);
513 setHasMetadataHashEntry(false);
517 // Handle removal of an existing value.
518 for (unsigned i = 0, e = Info.size(); i != e; ++i)
519 if (Info[i].first == KindID) {
520 Info[i] = Info.back();
522 assert(!Info.empty() && "Removing last entry should be handled above");
525 // Otherwise, removing an entry that doesn't exist on the instruction.
528 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
529 // Handle 'dbg' as a special case since it is not stored in the hash table.
530 if (KindID == LLVMContext::MD_dbg)
531 return DbgLoc.getAsMDNode(getContext());
533 if (!hasMetadataHashEntry()) return 0;
535 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
536 assert(!Info.empty() && "bit out of sync with hash table");
538 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
540 if (I->first == KindID)
545 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
546 MDNode*> > &Result) const {
549 // Handle 'dbg' as a special case since it is not stored in the hash table.
550 if (!DbgLoc.isUnknown()) {
551 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
552 DbgLoc.getAsMDNode(getContext())));
553 if (!hasMetadataHashEntry()) return;
556 assert(hasMetadataHashEntry() &&
557 getContext().pImpl->MetadataStore.count(this) &&
558 "Shouldn't have called this");
559 const LLVMContextImpl::MDMapTy &Info =
560 getContext().pImpl->MetadataStore.find(this)->second;
561 assert(!Info.empty() && "Shouldn't have called this");
563 Result.append(Info.begin(), Info.end());
565 // Sort the resulting array so it is stable.
566 if (Result.size() > 1)
567 array_pod_sort(Result.begin(), Result.end());
571 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
572 MDNode*> > &Result) const {
574 assert(hasMetadataHashEntry() &&
575 getContext().pImpl->MetadataStore.count(this) &&
576 "Shouldn't have called this");
577 const LLVMContextImpl::MDMapTy &Info =
578 getContext().pImpl->MetadataStore.find(this)->second;
579 assert(!Info.empty() && "Shouldn't have called this");
580 Result.append(Info.begin(), Info.end());
582 // Sort the resulting array so it is stable.
583 if (Result.size() > 1)
584 array_pod_sort(Result.begin(), Result.end());
587 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
588 /// this instruction.
589 void Instruction::clearMetadataHashEntries() {
590 assert(hasMetadataHashEntry() && "Caller should check");
591 getContext().pImpl->MetadataStore.erase(this);
592 setHasMetadataHashEntry(false);