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/SmallSet.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/IR/ConstantRange.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/LeakDetector.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/ValueHandle.h"
30 //===----------------------------------------------------------------------===//
31 // MDString implementation.
34 void MDString::anchor() { }
36 MDString::MDString(LLVMContext &C)
37 : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
39 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
40 LLVMContextImpl *pImpl = Context.pImpl;
41 StringMapEntry<Value*> &Entry =
42 pImpl->MDStringCache.GetOrCreateValue(Str);
43 Value *&S = Entry.getValue();
44 if (!S) S = new MDString(Context);
45 S->setValueName(&Entry);
46 return cast<MDString>(S);
49 //===----------------------------------------------------------------------===//
50 // MDNodeOperand implementation.
53 // Use CallbackVH to hold MDNode operands.
55 class MDNodeOperand : public CallbackVH {
57 MDNodeOperand *Cur = this;
59 while (Cur->getValPtrInt() != 1)
62 assert(Cur->getValPtrInt() == 1 &&
63 "Couldn't find the beginning of the operand list!");
64 return reinterpret_cast<MDNode*>(Cur) - 1;
68 MDNodeOperand(Value *V) : CallbackVH(V) {}
69 virtual ~MDNodeOperand();
72 unsigned IsFirst = this->getValPtrInt();
74 this->setAsFirstOperand(IsFirst);
77 /// \brief Accessor method to mark the operand as the first in the list.
78 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
80 void deleted() override;
81 void allUsesReplacedWith(Value *NV) override;
83 } // end namespace llvm.
85 // Provide out-of-line definition to prevent weak vtable.
86 MDNodeOperand::~MDNodeOperand() {}
88 void MDNodeOperand::deleted() {
89 getParent()->replaceOperand(this, nullptr);
92 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
93 getParent()->replaceOperand(this, NV);
96 //===----------------------------------------------------------------------===//
97 // MDNode implementation.
100 /// \brief Get the MDNodeOperand's coallocated on the end of the MDNode.
101 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
102 // Use <= instead of < to permit a one-past-the-end address.
103 assert(Op <= N->getNumOperands() && "Invalid operand number");
104 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
107 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
108 MDNodeOperand *Op = getOperandPtr(this, i);
109 replaceOperand(Op, Val);
112 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
113 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
114 NumOperands = Vals.size();
117 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
119 // Initialize the operand list, which is co-allocated on the end of the node.
121 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
122 Op != E; ++Op, ++i) {
123 new (Op) MDNodeOperand(Vals[i]);
125 // Mark the first MDNodeOperand as being the first in the list of operands.
127 Op->setAsFirstOperand(1);
131 /// ~MDNode - Destroy MDNode.
133 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
134 "Not being destroyed through destroy()?");
135 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
136 if (isNotUniqued()) {
137 pImpl->NonUniquedMDNodes.erase(this);
139 pImpl->MDNodeSet.RemoveNode(this);
142 // Destroy the operands.
143 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
145 Op->~MDNodeOperand();
148 static const Function *getFunctionForValue(Value *V) {
149 if (!V) return nullptr;
150 if (Instruction *I = dyn_cast<Instruction>(V)) {
151 BasicBlock *BB = I->getParent();
152 return BB ? BB->getParent() : nullptr;
154 if (Argument *A = dyn_cast<Argument>(V))
155 return A->getParent();
156 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
157 return BB->getParent();
158 if (MDNode *MD = dyn_cast<MDNode>(V))
159 return MD->getFunction();
164 static const Function *assertLocalFunction(const MDNode *N) {
165 if (!N->isFunctionLocal()) return nullptr;
167 // FIXME: This does not handle cyclic function local metadata.
168 const Function *F = nullptr, *NewF = nullptr;
169 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
170 if (Value *V = N->getOperand(i)) {
171 if (MDNode *MD = dyn_cast<MDNode>(V))
172 NewF = assertLocalFunction(MD);
174 NewF = getFunctionForValue(V);
179 assert((NewF == nullptr || F == NewF) &&
180 "inconsistent function-local metadata");
186 // getFunction - If this metadata is function-local and recursively has a
187 // function-local operand, return the first such operand's parent function.
188 // Otherwise, return null. getFunction() should not be used for performance-
189 // critical code because it recursively visits all the MDNode's operands.
190 const Function *MDNode::getFunction() const {
192 return assertLocalFunction(this);
194 if (!isFunctionLocal()) return nullptr;
195 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
196 if (const Function *F = getFunctionForValue(getOperand(i)))
202 // destroy - Delete this node. Only when there are no uses.
203 void MDNode::destroy() {
204 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
205 // Placement delete, then free the memory.
210 /// \brief Check if the Value would require a function-local MDNode.
211 static bool isFunctionLocalValue(Value *V) {
212 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
213 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
216 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
217 FunctionLocalness FL, bool Insert) {
218 LLVMContextImpl *pImpl = Context.pImpl;
220 // Add all the operand pointers. Note that we don't have to add the
221 // isFunctionLocal bit because that's implied by the operands.
222 // Note that if the operands are later nulled out, the node will be
223 // removed from the uniquing map.
225 for (Value *V : Vals)
229 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
234 bool isFunctionLocal = false;
237 for (Value *V : Vals) {
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 /// \brief Return specified operand.
305 Value *MDNode::getOperand(unsigned i) const {
306 assert(i < getNumOperands() && "Invalid operand number");
307 return *getOperandPtr(const_cast<MDNode*>(this), i);
310 void MDNode::Profile(FoldingSetNodeID &ID) const {
311 // Add all the operand pointers. Note that we don't have to add the
312 // isFunctionLocal bit because that's implied by the operands.
313 // Note that if the operands are later nulled out, the node will be
314 // removed from the uniquing map.
315 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
316 ID.AddPointer(getOperand(i));
319 void MDNode::setIsNotUniqued() {
320 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
321 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
322 pImpl->NonUniquedMDNodes.insert(this);
325 // Replace value from this node's operand list.
326 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
329 // If is possible that someone did GV->RAUW(inst), replacing a global variable
330 // with an instruction or some other function-local object. If this is a
331 // non-function-local MDNode, it can't point to a function-local object.
332 // Handle this case by implicitly dropping the MDNode reference to null.
333 // Likewise if the MDNode is function-local but for a different function.
334 if (To && isFunctionLocalValue(To)) {
335 if (!isFunctionLocal())
338 const Function *F = getFunction();
339 const Function *FV = getFunctionForValue(To);
340 // Metadata can be function-local without having an associated function.
341 // So only consider functions to have changed if non-null.
342 if (F && FV && F != FV)
350 // Update the operand.
353 // If this node is already not being uniqued (because one of the operands
354 // already went to null), then there is nothing else to do here.
355 if (isNotUniqued()) return;
357 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
359 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
360 // this node to remove it, so we don't care what state the operands are in.
361 pImpl->MDNodeSet.RemoveNode(this);
363 // If we are dropping an argument to null, we choose to not unique the MDNode
364 // anymore. This commonly occurs during destruction, and uniquing these
365 // brings little reuse. Also, this means we don't need to include
366 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
372 // Now that the node is out of the folding set, get ready to reinsert it.
373 // First, check to see if another node with the same operands already exists
374 // in the set. If so, then this node is redundant.
378 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
379 replaceAllUsesWith(N);
384 // Cache the operand hash.
385 Hash = ID.ComputeHash();
386 // InsertPoint will have been set by the FindNodeOrInsertPos call.
387 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
389 // If this MDValue was previously function-local but no longer is, clear
390 // its function-local flag.
391 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
392 bool isStillFunctionLocal = false;
393 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
394 Value *V = getOperand(i);
396 if (isFunctionLocalValue(V)) {
397 isStillFunctionLocal = true;
401 if (!isStillFunctionLocal)
402 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
406 MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
412 SmallVector<Value *, 4> Vals(A->getNumOperands() +
413 B->getNumOperands());
416 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
417 Vals[j++] = A->getOperand(i);
418 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
419 Vals[j++] = B->getOperand(i);
421 return MDNode::get(A->getContext(), Vals);
424 MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
428 SmallVector<Value *, 4> Vals;
429 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
430 Value *V = A->getOperand(i);
431 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
432 if (V == B->getOperand(j)) {
438 return MDNode::get(A->getContext(), Vals);
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(SmallVectorImpl<Value *> &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(SmallVectorImpl<Value *> &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(nullptr),
564 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
566 NamedMDNode::~NamedMDNode() {
568 delete &getNMDOps(Operands);
571 unsigned NamedMDNode::getNumOperands() const {
572 return (unsigned)getNMDOps(Operands).size();
575 MDNode *NamedMDNode::getOperand(unsigned i) const {
576 assert(i < getNumOperands() && "Invalid Operand number!");
577 return &*getNMDOps(Operands)[i];
580 void NamedMDNode::addOperand(MDNode *M) {
581 assert(!M->isFunctionLocal() &&
582 "NamedMDNode operands must not be function-local!");
583 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
586 void NamedMDNode::eraseFromParent() {
587 getParent()->eraseNamedMetadata(this);
590 void NamedMDNode::dropAllReferences() {
591 getNMDOps(Operands).clear();
594 StringRef NamedMDNode::getName() const {
595 return StringRef(Name);
598 //===----------------------------------------------------------------------===//
599 // Instruction Metadata method implementations.
602 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
603 if (!Node && !hasMetadata())
605 setMetadata(getContext().getMDKindID(Kind), Node);
608 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
609 return getMetadataImpl(getContext().getMDKindID(Kind));
612 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
613 SmallSet<unsigned, 5> KnownSet;
614 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
616 // Drop debug if needed
617 if (KnownSet.erase(LLVMContext::MD_dbg))
620 if (!hasMetadataHashEntry())
621 return; // Nothing to remove!
623 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
624 getContext().pImpl->MetadataStore;
626 if (KnownSet.empty()) {
627 // Just drop our entry at the store.
628 MetadataStore.erase(this);
629 setHasMetadataHashEntry(false);
633 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
636 // Walk the array and drop any metadata we don't know.
637 for (I = 0, E = Info.size(); I != E;) {
638 if (KnownSet.count(Info[I].first)) {
643 Info[I] = Info.back();
647 assert(E == Info.size());
650 // Drop our entry at the store.
651 MetadataStore.erase(this);
652 setHasMetadataHashEntry(false);
656 /// setMetadata - Set the metadata of of the specified kind to the specified
657 /// node. This updates/replaces metadata if already present, or removes it if
659 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
660 if (!Node && !hasMetadata())
663 // Handle 'dbg' as a special case since it is not stored in the hash table.
664 if (KindID == LLVMContext::MD_dbg) {
665 DbgLoc = DebugLoc::getFromDILocation(Node);
669 // Handle the case when we're adding/updating metadata on an instruction.
671 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
672 assert(!Info.empty() == hasMetadataHashEntry() &&
673 "HasMetadata bit is wonked");
675 setHasMetadataHashEntry(true);
677 // Handle replacement of an existing value.
679 if (P.first == KindID) {
685 // No replacement, just add it to the list.
686 Info.push_back(std::make_pair(KindID, Node));
690 // Otherwise, we're removing metadata from an instruction.
691 assert((hasMetadataHashEntry() ==
692 (getContext().pImpl->MetadataStore.count(this) > 0)) &&
693 "HasMetadata bit out of date!");
694 if (!hasMetadataHashEntry())
695 return; // Nothing to remove!
696 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
698 // Common case is removing the only entry.
699 if (Info.size() == 1 && Info[0].first == KindID) {
700 getContext().pImpl->MetadataStore.erase(this);
701 setHasMetadataHashEntry(false);
705 // Handle removal of an existing value.
706 for (unsigned i = 0, e = Info.size(); i != e; ++i)
707 if (Info[i].first == KindID) {
708 Info[i] = Info.back();
710 assert(!Info.empty() && "Removing last entry should be handled above");
713 // Otherwise, removing an entry that doesn't exist on the instruction.
716 void Instruction::setAAMetadata(const AAMDNodes &N) {
717 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
718 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
719 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
722 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
723 // Handle 'dbg' as a special case since it is not stored in the hash table.
724 if (KindID == LLVMContext::MD_dbg)
725 return DbgLoc.getAsMDNode(getContext());
727 if (!hasMetadataHashEntry()) return nullptr;
729 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
730 assert(!Info.empty() && "bit out of sync with hash table");
732 for (const auto &I : Info)
733 if (I.first == KindID)
738 void Instruction::getAllMetadataImpl(
739 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
742 // Handle 'dbg' as a special case since it is not stored in the hash table.
743 if (!DbgLoc.isUnknown()) {
744 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
745 DbgLoc.getAsMDNode(getContext())));
746 if (!hasMetadataHashEntry()) return;
749 assert(hasMetadataHashEntry() &&
750 getContext().pImpl->MetadataStore.count(this) &&
751 "Shouldn't have called this");
752 const LLVMContextImpl::MDMapTy &Info =
753 getContext().pImpl->MetadataStore.find(this)->second;
754 assert(!Info.empty() && "Shouldn't have called this");
756 Result.append(Info.begin(), Info.end());
758 // Sort the resulting array so it is stable.
759 if (Result.size() > 1)
760 array_pod_sort(Result.begin(), Result.end());
763 void Instruction::getAllMetadataOtherThanDebugLocImpl(
764 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
766 assert(hasMetadataHashEntry() &&
767 getContext().pImpl->MetadataStore.count(this) &&
768 "Shouldn't have called this");
769 const LLVMContextImpl::MDMapTy &Info =
770 getContext().pImpl->MetadataStore.find(this)->second;
771 assert(!Info.empty() && "Shouldn't have called this");
772 Result.append(Info.begin(), Info.end());
774 // Sort the resulting array so it is stable.
775 if (Result.size() > 1)
776 array_pod_sort(Result.begin(), Result.end());
779 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
780 /// this instruction.
781 void Instruction::clearMetadataHashEntries() {
782 assert(hasMetadataHashEntry() && "Caller should check");
783 getContext().pImpl->MetadataStore.erase(this);
784 setHasMetadataHashEntry(false);