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/IR/Metadata.h"
15 #include "LLVMContextImpl.h"
16 #include "SymbolTableListTraitsImpl.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/ConstantRange.h"
25 #include "llvm/Support/LeakDetector.h"
26 #include "llvm/Support/ValueHandle.h"
29 //===----------------------------------------------------------------------===//
30 // MDString implementation.
33 void MDString::anchor() { }
35 MDString::MDString(LLVMContext &C)
36 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
38 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
39 LLVMContextImpl *pImpl = Context.pImpl;
40 StringMapEntry<Value*> &Entry =
41 pImpl->MDStringCache.GetOrCreateValue(Str);
42 Value *&S = Entry.getValue();
43 if (!S) S = new MDString(Context);
44 S->setValueName(&Entry);
45 return cast<MDString>(S);
48 //===----------------------------------------------------------------------===//
49 // MDNodeOperand implementation.
52 // Use CallbackVH to hold MDNode operands.
54 class MDNodeOperand : public CallbackVH {
56 MDNodeOperand *Cur = this;
58 while (Cur->getValPtrInt() != 1)
61 assert(Cur->getValPtrInt() == 1 &&
62 "Couldn't find the beginning of the operand list!");
63 return reinterpret_cast<MDNode*>(Cur) - 1;
67 MDNodeOperand(Value *V) : CallbackVH(V) {}
71 unsigned IsFirst = this->getValPtrInt();
73 this->setAsFirstOperand(IsFirst);
76 /// setAsFirstOperand - Accessor method to mark the operand as the first in
78 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
80 virtual void deleted();
81 virtual void allUsesReplacedWith(Value *NV);
83 } // end namespace llvm.
86 void MDNodeOperand::deleted() {
87 getParent()->replaceOperand(this, 0);
90 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
91 getParent()->replaceOperand(this, NV);
94 //===----------------------------------------------------------------------===//
95 // MDNode implementation.
98 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
99 /// the end of the MDNode.
100 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
101 // Use <= instead of < to permit a one-past-the-end address.
102 assert(Op <= N->getNumOperands() && "Invalid operand number");
103 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
106 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
107 MDNodeOperand *Op = getOperandPtr(this, i);
108 replaceOperand(Op, Val);
111 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
112 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
113 NumOperands = Vals.size();
116 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
118 // Initialize the operand list, which is co-allocated on the end of the node.
120 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
121 Op != E; ++Op, ++i) {
122 new (Op) MDNodeOperand(Vals[i]);
124 // Mark the first MDNodeOperand as being the first in the list of operands.
126 Op->setAsFirstOperand(1);
130 /// ~MDNode - Destroy MDNode.
132 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
133 "Not being destroyed through destroy()?");
134 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
135 if (isNotUniqued()) {
136 pImpl->NonUniquedMDNodes.erase(this);
138 pImpl->MDNodeSet.RemoveNode(this);
141 // Destroy the operands.
142 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
144 Op->~MDNodeOperand();
147 static const Function *getFunctionForValue(Value *V) {
149 if (Instruction *I = dyn_cast<Instruction>(V)) {
150 BasicBlock *BB = I->getParent();
151 return BB ? BB->getParent() : 0;
153 if (Argument *A = dyn_cast<Argument>(V))
154 return A->getParent();
155 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
156 return BB->getParent();
157 if (MDNode *MD = dyn_cast<MDNode>(V))
158 return MD->getFunction();
163 static const Function *assertLocalFunction(const MDNode *N) {
164 if (!N->isFunctionLocal()) return 0;
166 // FIXME: This does not handle cyclic function local metadata.
167 const Function *F = 0, *NewF = 0;
168 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
169 if (Value *V = N->getOperand(i)) {
170 if (MDNode *MD = dyn_cast<MDNode>(V))
171 NewF = assertLocalFunction(MD);
173 NewF = getFunctionForValue(V);
178 assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
184 // getFunction - If this metadata is function-local and recursively has a
185 // function-local operand, return the first such operand's parent function.
186 // Otherwise, return null. getFunction() should not be used for performance-
187 // critical code because it recursively visits all the MDNode's operands.
188 const Function *MDNode::getFunction() const {
190 return assertLocalFunction(this);
192 if (!isFunctionLocal()) return NULL;
193 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
194 if (const Function *F = getFunctionForValue(getOperand(i)))
200 // destroy - Delete this node. Only when there are no uses.
201 void MDNode::destroy() {
202 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
203 // Placement delete, then free the memory.
208 /// isFunctionLocalValue - Return true if this is a value that would require a
209 /// function-local MDNode.
210 static bool isFunctionLocalValue(Value *V) {
211 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
212 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
215 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
216 FunctionLocalness FL, bool Insert) {
217 LLVMContextImpl *pImpl = Context.pImpl;
219 // Add all the operand pointers. Note that we don't have to add the
220 // isFunctionLocal bit because that's implied by the operands.
221 // Note that if the operands are later nulled out, the node will be
222 // removed from the uniquing map.
224 for (unsigned i = 0; i != Vals.size(); ++i)
225 ID.AddPointer(Vals[i]);
228 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
233 bool isFunctionLocal = false;
236 for (unsigned i = 0; i != Vals.size(); ++i) {
239 if (isFunctionLocalValue(V)) {
240 isFunctionLocal = true;
246 isFunctionLocal = false;
249 isFunctionLocal = true;
253 // Coallocate space for the node and Operands together, then placement new.
254 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
255 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
257 // Cache the operand hash.
258 N->Hash = ID.ComputeHash();
260 // InsertPoint will have been set by the FindNodeOrInsertPos call.
261 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
266 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
267 return getMDNode(Context, Vals, FL_Unknown);
270 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
271 ArrayRef<Value*> Vals,
272 bool isFunctionLocal) {
273 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
276 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
277 return getMDNode(Context, Vals, FL_Unknown, false);
280 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
282 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
283 N = new (N) MDNode(Context, Vals, FL_No);
284 N->setValueSubclassData(N->getSubclassDataFromValue() |
286 LeakDetector::addGarbageObject(N);
290 void MDNode::deleteTemporary(MDNode *N) {
291 assert(N->use_empty() && "Temporary MDNode has uses!");
292 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
293 "Deleting a non-temporary uniqued node!");
294 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
295 "Deleting a non-temporary non-uniqued node!");
296 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
297 "Temporary MDNode does not have NotUniquedBit set!");
298 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
299 "Temporary MDNode has DestroyFlag set!");
300 LeakDetector::removeGarbageObject(N);
304 /// getOperand - Return specified operand.
305 Value *MDNode::getOperand(unsigned i) const {
306 return *getOperandPtr(const_cast<MDNode*>(this), i);
309 void MDNode::Profile(FoldingSetNodeID &ID) const {
310 // Add all the operand pointers. Note that we don't have to add the
311 // isFunctionLocal bit because that's implied by the operands.
312 // Note that if the operands are later nulled out, the node will be
313 // removed from the uniquing map.
314 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
315 ID.AddPointer(getOperand(i));
318 void MDNode::setIsNotUniqued() {
319 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
320 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
321 pImpl->NonUniquedMDNodes.insert(this);
324 // Replace value from this node's operand list.
325 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
328 // If is possible that someone did GV->RAUW(inst), replacing a global variable
329 // with an instruction or some other function-local object. If this is a
330 // non-function-local MDNode, it can't point to a function-local object.
331 // Handle this case by implicitly dropping the MDNode reference to null.
332 // Likewise if the MDNode is function-local but for a different function.
333 if (To && isFunctionLocalValue(To)) {
334 if (!isFunctionLocal())
337 const Function *F = getFunction();
338 const Function *FV = getFunctionForValue(To);
339 // Metadata can be function-local without having an associated function.
340 // So only consider functions to have changed if non-null.
341 if (F && FV && F != FV)
349 // Update the operand.
352 // If this node is already not being uniqued (because one of the operands
353 // already went to null), then there is nothing else to do here.
354 if (isNotUniqued()) return;
356 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
358 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
359 // this node to remove it, so we don't care what state the operands are in.
360 pImpl->MDNodeSet.RemoveNode(this);
362 // If we are dropping an argument to null, we choose to not unique the MDNode
363 // anymore. This commonly occurs during destruction, and uniquing these
364 // brings little reuse. Also, this means we don't need to include
365 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
371 // Now that the node is out of the folding set, get ready to reinsert it.
372 // First, check to see if another node with the same operands already exists
373 // in the set. If so, then this node is redundant.
377 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
378 replaceAllUsesWith(N);
383 // Cache the operand hash.
384 Hash = ID.ComputeHash();
385 // InsertPoint will have been set by the FindNodeOrInsertPos call.
386 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
388 // If this MDValue was previously function-local but no longer is, clear
389 // its function-local flag.
390 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
391 bool isStillFunctionLocal = false;
392 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
393 Value *V = getOperand(i);
395 if (isFunctionLocalValue(V)) {
396 isStillFunctionLocal = true;
400 if (!isStillFunctionLocal)
401 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
405 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
412 SmallVector<MDNode *, 4> PathA;
416 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
419 SmallVector<MDNode *, 4> PathB;
423 T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
426 int IA = PathA.size() - 1;
427 int IB = PathB.size() - 1;
430 while (IA >= 0 && IB >=0) {
431 if (PathA[IA] == PathB[IB])
441 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
445 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
446 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
447 if (AVal.compare(BVal) == APFloat::cmpLessThan)
452 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
453 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
456 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
457 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
460 static bool tryMergeRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
462 ConstantRange NewRange(Low->getValue(), High->getValue());
463 unsigned Size = EndPoints.size();
464 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
465 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
466 ConstantRange LastRange(LB, LE);
467 if (canBeMerged(NewRange, LastRange)) {
468 ConstantRange Union = LastRange.unionWith(NewRange);
469 Type *Ty = High->getType();
470 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
471 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
477 static void addRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
479 if (!EndPoints.empty())
480 if (tryMergeRange(EndPoints, Low, High))
483 EndPoints.push_back(Low);
484 EndPoints.push_back(High);
487 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
488 // Given two ranges, we want to compute the union of the ranges. This
489 // is slightly complitade by having to combine the intervals and merge
490 // the ones that overlap.
498 // First, walk both lists in older of the lower boundary of each interval.
499 // At each step, try to merge the new interval to the last one we adedd.
500 SmallVector<Value*, 4> EndPoints;
503 int AN = A->getNumOperands() / 2;
504 int BN = B->getNumOperands() / 2;
505 while (AI < AN && BI < BN) {
506 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
507 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
509 if (ALow->getValue().slt(BLow->getValue())) {
510 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
513 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
518 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
519 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
523 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
524 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
528 // If we have more than 2 ranges (4 endpoints) we have to try to merge
529 // the last and first ones.
530 unsigned Size = EndPoints.size();
532 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
533 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
534 if (tryMergeRange(EndPoints, FB, FE)) {
535 for (unsigned i = 0; i < Size - 2; ++i) {
536 EndPoints[i] = EndPoints[i + 2];
538 EndPoints.resize(Size - 2);
542 // If in the end we have a single range, it is possible that it is now the
543 // full range. Just drop the metadata in that case.
544 if (EndPoints.size() == 2) {
545 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
546 cast<ConstantInt>(EndPoints[1])->getValue());
547 if (Range.isFullSet())
551 return MDNode::get(A->getContext(), EndPoints);
554 //===----------------------------------------------------------------------===//
555 // NamedMDNode implementation.
558 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
559 return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
562 NamedMDNode::NamedMDNode(const Twine &N)
563 : Name(N.str()), Parent(0),
564 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
567 NamedMDNode::~NamedMDNode() {
569 delete &getNMDOps(Operands);
572 /// getNumOperands - Return number of NamedMDNode operands.
573 unsigned NamedMDNode::getNumOperands() const {
574 return (unsigned)getNMDOps(Operands).size();
577 /// getOperand - Return specified operand.
578 MDNode *NamedMDNode::getOperand(unsigned i) const {
579 assert(i < getNumOperands() && "Invalid Operand number!");
580 return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
583 /// addOperand - Add metadata Operand.
584 void NamedMDNode::addOperand(MDNode *M) {
585 assert(!M->isFunctionLocal() &&
586 "NamedMDNode operands must not be function-local!");
587 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
590 /// eraseFromParent - Drop all references and remove the node from parent
592 void NamedMDNode::eraseFromParent() {
593 getParent()->eraseNamedMetadata(this);
596 /// dropAllReferences - Remove all uses and clear node vector.
597 void NamedMDNode::dropAllReferences() {
598 getNMDOps(Operands).clear();
601 /// getName - Return a constant reference to this named metadata's name.
602 StringRef NamedMDNode::getName() const {
603 return StringRef(Name);
606 //===----------------------------------------------------------------------===//
607 // Instruction Metadata method implementations.
610 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
611 if (Node == 0 && !hasMetadata()) return;
612 setMetadata(getContext().getMDKindID(Kind), Node);
615 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
616 return getMetadataImpl(getContext().getMDKindID(Kind));
619 /// setMetadata - Set the metadata of of the specified kind to the specified
620 /// node. This updates/replaces metadata if already present, or removes it if
622 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
623 if (Node == 0 && !hasMetadata()) return;
625 // Handle 'dbg' as a special case since it is not stored in the hash table.
626 if (KindID == LLVMContext::MD_dbg) {
627 DbgLoc = DebugLoc::getFromDILocation(Node);
631 // Handle the case when we're adding/updating metadata on an instruction.
633 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
634 assert(!Info.empty() == hasMetadataHashEntry() &&
635 "HasMetadata bit is wonked");
637 setHasMetadataHashEntry(true);
639 // Handle replacement of an existing value.
640 for (unsigned i = 0, e = Info.size(); i != e; ++i)
641 if (Info[i].first == KindID) {
642 Info[i].second = Node;
647 // No replacement, just add it to the list.
648 Info.push_back(std::make_pair(KindID, Node));
652 // Otherwise, we're removing metadata from an instruction.
653 assert((hasMetadataHashEntry() ==
654 getContext().pImpl->MetadataStore.count(this)) &&
655 "HasMetadata bit out of date!");
656 if (!hasMetadataHashEntry())
657 return; // Nothing to remove!
658 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
660 // Common case is removing the only entry.
661 if (Info.size() == 1 && Info[0].first == KindID) {
662 getContext().pImpl->MetadataStore.erase(this);
663 setHasMetadataHashEntry(false);
667 // Handle removal of an existing value.
668 for (unsigned i = 0, e = Info.size(); i != e; ++i)
669 if (Info[i].first == KindID) {
670 Info[i] = Info.back();
672 assert(!Info.empty() && "Removing last entry should be handled above");
675 // Otherwise, removing an entry that doesn't exist on the instruction.
678 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
679 // Handle 'dbg' as a special case since it is not stored in the hash table.
680 if (KindID == LLVMContext::MD_dbg)
681 return DbgLoc.getAsMDNode(getContext());
683 if (!hasMetadataHashEntry()) return 0;
685 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
686 assert(!Info.empty() && "bit out of sync with hash table");
688 for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
690 if (I->first == KindID)
695 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
696 MDNode*> > &Result) const {
699 // Handle 'dbg' as a special case since it is not stored in the hash table.
700 if (!DbgLoc.isUnknown()) {
701 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
702 DbgLoc.getAsMDNode(getContext())));
703 if (!hasMetadataHashEntry()) return;
706 assert(hasMetadataHashEntry() &&
707 getContext().pImpl->MetadataStore.count(this) &&
708 "Shouldn't have called this");
709 const LLVMContextImpl::MDMapTy &Info =
710 getContext().pImpl->MetadataStore.find(this)->second;
711 assert(!Info.empty() && "Shouldn't have called this");
713 Result.append(Info.begin(), Info.end());
715 // Sort the resulting array so it is stable.
716 if (Result.size() > 1)
717 array_pod_sort(Result.begin(), Result.end());
721 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
722 MDNode*> > &Result) const {
724 assert(hasMetadataHashEntry() &&
725 getContext().pImpl->MetadataStore.count(this) &&
726 "Shouldn't have called this");
727 const LLVMContextImpl::MDMapTy &Info =
728 getContext().pImpl->MetadataStore.find(this)->second;
729 assert(!Info.empty() && "Shouldn't have called this");
730 Result.append(Info.begin(), Info.end());
732 // Sort the resulting array so it is stable.
733 if (Result.size() > 1)
734 array_pod_sort(Result.begin(), Result.end());
737 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
738 /// this instruction.
739 void Instruction::clearMetadataHashEntries() {
740 assert(hasMetadataHashEntry() && "Caller should check");
741 getContext().pImpl->MetadataStore.erase(this);
742 setHasMetadataHashEntry(false);