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.
11 //
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"
29 using namespace llvm;
31 Metadata::Metadata(LLVMContext &Context, unsigned ID)
32 : Value(Type::getMetadataTy(Context), ID) {}
34 //===----------------------------------------------------------------------===//
35 // MDString implementation.
36 //
38 void MDString::anchor() { }
40 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
41 auto &Store = Context.pImpl->MDStringCache;
42 auto I = Store.find(Str);
43 if (I != Store.end())
44 return &I->second;
46 auto *Entry =
47 StringMapEntry<MDString>::Create(Str, Store.getAllocator(), Context);
48 bool WasInserted = Store.insert(Entry);
49 (void)WasInserted;
50 assert(WasInserted && "Expected entry to be inserted");
51 return &Entry->second;
52 }
54 StringRef MDString::getString() const {
55 return StringMapEntry<MDString>::GetStringMapEntryFromValue(*this).first();
56 }
58 //===----------------------------------------------------------------------===//
59 // MDNodeOperand implementation.
60 //
62 // Use CallbackVH to hold MDNode operands.
63 namespace llvm {
64 class MDNodeOperand : public CallbackVH {
65 MDNode *getParent() {
66 MDNodeOperand *Cur = this;
68 while (Cur->getValPtrInt() != 1)
69 --Cur;
71 assert(Cur->getValPtrInt() == 1 &&
72 "Couldn't find the beginning of the operand list!");
73 return reinterpret_cast<MDNode*>(Cur) - 1;
74 }
76 public:
77 MDNodeOperand(Value *V) : CallbackVH(V) {}
78 virtual ~MDNodeOperand();
80 void set(Value *V) {
81 unsigned IsFirst = this->getValPtrInt();
82 this->setValPtr(V);
83 this->setAsFirstOperand(IsFirst);
84 }
86 /// \brief Accessor method to mark the operand as the first in the list.
87 void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
89 void deleted() override;
90 void allUsesReplacedWith(Value *NV) override;
91 };
92 } // end namespace llvm.
94 // Provide out-of-line definition to prevent weak vtable.
95 MDNodeOperand::~MDNodeOperand() {}
97 void MDNodeOperand::deleted() {
98 getParent()->replaceOperand(this, nullptr);
99 }
101 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
102 getParent()->replaceOperand(this, NV);
103 }
105 //===----------------------------------------------------------------------===//
106 // MDNode implementation.
107 //
109 /// \brief Get the MDNodeOperand's coallocated on the end of the MDNode.
110 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
111 // Use <= instead of < to permit a one-past-the-end address.
112 assert(Op <= N->getNumOperands() && "Invalid operand number");
113 return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
114 }
116 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
117 MDNodeOperand *Op = getOperandPtr(this, i);
118 replaceOperand(Op, Val);
119 }
121 MDNode::MDNode(LLVMContext &C, ArrayRef<Value *> Vals, bool isFunctionLocal)
122 : Metadata(C, Value::MDNodeVal) {
123 NumOperands = Vals.size();
125 if (isFunctionLocal)
126 setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
128 // Initialize the operand list, which is co-allocated on the end of the node.
129 unsigned i = 0;
130 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
131 Op != E; ++Op, ++i) {
132 new (Op) MDNodeOperand(Vals[i]);
134 // Mark the first MDNodeOperand as being the first in the list of operands.
135 if (i == 0)
136 Op->setAsFirstOperand(1);
137 }
138 }
140 /// ~MDNode - Destroy MDNode.
141 MDNode::~MDNode() {
142 assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
143 "Not being destroyed through destroy()?");
144 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
145 if (isNotUniqued()) {
146 pImpl->NonUniquedMDNodes.erase(this);
147 } else {
148 pImpl->MDNodeSet.RemoveNode(this);
149 }
151 // Destroy the operands.
152 for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
153 Op != E; ++Op)
154 Op->~MDNodeOperand();
155 }
157 static const Function *getFunctionForValue(Value *V) {
158 if (!V) return nullptr;
159 if (Instruction *I = dyn_cast<Instruction>(V)) {
160 BasicBlock *BB = I->getParent();
161 return BB ? BB->getParent() : nullptr;
162 }
163 if (Argument *A = dyn_cast<Argument>(V))
164 return A->getParent();
165 if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
166 return BB->getParent();
167 if (MDNode *MD = dyn_cast<MDNode>(V))
168 return MD->getFunction();
169 return nullptr;
170 }
172 #ifndef NDEBUG
173 static const Function *assertLocalFunction(const MDNode *N) {
174 if (!N->isFunctionLocal()) return nullptr;
176 // FIXME: This does not handle cyclic function local metadata.
177 const Function *F = nullptr, *NewF = nullptr;
178 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
179 if (Value *V = N->getOperand(i)) {
180 if (MDNode *MD = dyn_cast<MDNode>(V))
181 NewF = assertLocalFunction(MD);
182 else
183 NewF = getFunctionForValue(V);
184 }
185 if (!F)
186 F = NewF;
187 else
188 assert((NewF == nullptr || F == NewF) &&
189 "inconsistent function-local metadata");
190 }
191 return F;
192 }
193 #endif
195 // getFunction - If this metadata is function-local and recursively has a
196 // function-local operand, return the first such operand's parent function.
197 // Otherwise, return null. getFunction() should not be used for performance-
198 // critical code because it recursively visits all the MDNode's operands.
199 const Function *MDNode::getFunction() const {
200 #ifndef NDEBUG
201 return assertLocalFunction(this);
202 #else
203 if (!isFunctionLocal()) return nullptr;
204 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
205 if (const Function *F = getFunctionForValue(getOperand(i)))
206 return F;
207 return nullptr;
208 #endif
209 }
211 // destroy - Delete this node. Only when there are no uses.
212 void MDNode::destroy() {
213 setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
214 // Placement delete, then free the memory.
215 this->~MDNode();
216 free(this);
217 }
219 /// \brief Check if the Value would require a function-local MDNode.
220 static bool isFunctionLocalValue(Value *V) {
221 return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
222 (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
223 }
225 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
226 FunctionLocalness FL, bool Insert) {
227 LLVMContextImpl *pImpl = Context.pImpl;
229 // Add all the operand pointers. Note that we don't have to add the
230 // isFunctionLocal bit because that's implied by the operands.
231 // Note that if the operands are later nulled out, the node will be
232 // removed from the uniquing map.
233 FoldingSetNodeID ID;
234 for (Value *V : Vals)
235 ID.AddPointer(V);
237 void *InsertPoint;
238 MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
240 if (N || !Insert)
241 return N;
243 bool isFunctionLocal = false;
244 switch (FL) {
245 case FL_Unknown:
246 for (Value *V : Vals) {
247 if (!V) continue;
248 if (isFunctionLocalValue(V)) {
249 isFunctionLocal = true;
250 break;
251 }
252 }
253 break;
254 case FL_No:
255 isFunctionLocal = false;
256 break;
257 case FL_Yes:
258 isFunctionLocal = true;
259 break;
260 }
262 // Coallocate space for the node and Operands together, then placement new.
263 void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
264 N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
266 // Cache the operand hash.
267 N->Hash = ID.ComputeHash();
269 // InsertPoint will have been set by the FindNodeOrInsertPos call.
270 pImpl->MDNodeSet.InsertNode(N, InsertPoint);
272 return N;
273 }
275 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
276 return getMDNode(Context, Vals, FL_Unknown);
277 }
279 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
280 ArrayRef<Value*> Vals,
281 bool isFunctionLocal) {
282 return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
283 }
285 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
286 return getMDNode(Context, Vals, FL_Unknown, false);
287 }
289 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
290 MDNode *N =
291 (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
292 N = new (N) MDNode(Context, Vals, FL_No);
293 N->setValueSubclassData(N->getSubclassDataFromValue() |
294 NotUniquedBit);
295 LeakDetector::addGarbageObject(N);
296 return N;
297 }
299 void MDNode::deleteTemporary(MDNode *N) {
300 assert(N->use_empty() && "Temporary MDNode has uses!");
301 assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
302 "Deleting a non-temporary uniqued node!");
303 assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
304 "Deleting a non-temporary non-uniqued node!");
305 assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
306 "Temporary MDNode does not have NotUniquedBit set!");
307 assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
308 "Temporary MDNode has DestroyFlag set!");
309 LeakDetector::removeGarbageObject(N);
310 N->destroy();
311 }
313 /// \brief Return specified operand.
314 Value *MDNode::getOperand(unsigned i) const {
315 assert(i < getNumOperands() && "Invalid operand number");
316 return *getOperandPtr(const_cast<MDNode*>(this), i);
317 }
319 void MDNode::Profile(FoldingSetNodeID &ID) const {
320 // Add all the operand pointers. Note that we don't have to add the
321 // isFunctionLocal bit because that's implied by the operands.
322 // Note that if the operands are later nulled out, the node will be
323 // removed from the uniquing map.
324 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
325 ID.AddPointer(getOperand(i));
326 }
328 void MDNode::setIsNotUniqued() {
329 setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
330 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
331 pImpl->NonUniquedMDNodes.insert(this);
332 }
334 // Replace value from this node's operand list.
335 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
336 Value *From = *Op;
338 // If is possible that someone did GV->RAUW(inst), replacing a global variable
339 // with an instruction or some other function-local object. If this is a
340 // non-function-local MDNode, it can't point to a function-local object.
341 // Handle this case by implicitly dropping the MDNode reference to null.
342 // Likewise if the MDNode is function-local but for a different function.
343 if (To && isFunctionLocalValue(To)) {
344 if (!isFunctionLocal())
345 To = nullptr;
346 else {
347 const Function *F = getFunction();
348 const Function *FV = getFunctionForValue(To);
349 // Metadata can be function-local without having an associated function.
350 // So only consider functions to have changed if non-null.
351 if (F && FV && F != FV)
352 To = nullptr;
353 }
354 }
356 if (From == To)
357 return;
359 // Update the operand.
360 Op->set(To);
362 // If this node is already not being uniqued (because one of the operands
363 // already went to null), then there is nothing else to do here.
364 if (isNotUniqued()) return;
366 LLVMContextImpl *pImpl = getType()->getContext().pImpl;
368 // Remove "this" from the context map. FoldingSet doesn't have to reprofile
369 // this node to remove it, so we don't care what state the operands are in.
370 pImpl->MDNodeSet.RemoveNode(this);
372 // If we are dropping an argument to null, we choose to not unique the MDNode
373 // anymore. This commonly occurs during destruction, and uniquing these
374 // brings little reuse. Also, this means we don't need to include
375 // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
376 if (!To) {
377 setIsNotUniqued();
378 return;
379 }
381 // Now that the node is out of the folding set, get ready to reinsert it.
382 // First, check to see if another node with the same operands already exists
383 // in the set. If so, then this node is redundant.
384 FoldingSetNodeID ID;
385 Profile(ID);
386 void *InsertPoint;
387 if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
388 replaceAllUsesWith(N);
389 destroy();
390 return;
391 }
393 // Cache the operand hash.
394 Hash = ID.ComputeHash();
395 // InsertPoint will have been set by the FindNodeOrInsertPos call.
396 pImpl->MDNodeSet.InsertNode(this, InsertPoint);
398 // If this MDValue was previously function-local but no longer is, clear
399 // its function-local flag.
400 if (isFunctionLocal() && !isFunctionLocalValue(To)) {
401 bool isStillFunctionLocal = false;
402 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
403 Value *V = getOperand(i);
404 if (!V) continue;
405 if (isFunctionLocalValue(V)) {
406 isStillFunctionLocal = true;
407 break;
408 }
409 }
410 if (!isStillFunctionLocal)
411 setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
412 }
413 }
415 MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
416 if (!A)
417 return B;
418 if (!B)
419 return A;
421 SmallVector<Value *, 4> Vals(A->getNumOperands() +
422 B->getNumOperands());
424 unsigned j = 0;
425 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i)
426 Vals[j++] = A->getOperand(i);
427 for (unsigned i = 0, ie = B->getNumOperands(); i != ie; ++i)
428 Vals[j++] = B->getOperand(i);
430 return MDNode::get(A->getContext(), Vals);
431 }
433 MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
434 if (!A || !B)
435 return nullptr;
437 SmallVector<Value *, 4> Vals;
438 for (unsigned i = 0, ie = A->getNumOperands(); i != ie; ++i) {
439 Value *V = A->getOperand(i);
440 for (unsigned j = 0, je = B->getNumOperands(); j != je; ++j)
441 if (V == B->getOperand(j)) {
442 Vals.push_back(V);
443 break;
444 }
445 }
447 return MDNode::get(A->getContext(), Vals);
448 }
450 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
451 if (!A || !B)
452 return nullptr;
454 APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
455 APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
456 if (AVal.compare(BVal) == APFloat::cmpLessThan)
457 return A;
458 return B;
459 }
461 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
462 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
463 }
465 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
466 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
467 }
469 static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
470 ConstantInt *High) {
471 ConstantRange NewRange(Low->getValue(), High->getValue());
472 unsigned Size = EndPoints.size();
473 APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
474 APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
475 ConstantRange LastRange(LB, LE);
476 if (canBeMerged(NewRange, LastRange)) {
477 ConstantRange Union = LastRange.unionWith(NewRange);
478 Type *Ty = High->getType();
479 EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
480 EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
481 return true;
482 }
483 return false;
484 }
486 static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
487 ConstantInt *High) {
488 if (!EndPoints.empty())
489 if (tryMergeRange(EndPoints, Low, High))
490 return;
492 EndPoints.push_back(Low);
493 EndPoints.push_back(High);
494 }
496 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
497 // Given two ranges, we want to compute the union of the ranges. This
498 // is slightly complitade by having to combine the intervals and merge
499 // the ones that overlap.
501 if (!A || !B)
502 return nullptr;
504 if (A == B)
505 return A;
507 // First, walk both lists in older of the lower boundary of each interval.
508 // At each step, try to merge the new interval to the last one we adedd.
509 SmallVector<Value*, 4> EndPoints;
510 int AI = 0;
511 int BI = 0;
512 int AN = A->getNumOperands() / 2;
513 int BN = B->getNumOperands() / 2;
514 while (AI < AN && BI < BN) {
515 ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
516 ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
518 if (ALow->getValue().slt(BLow->getValue())) {
519 addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
520 ++AI;
521 } else {
522 addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
523 ++BI;
524 }
525 }
526 while (AI < AN) {
527 addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
528 cast<ConstantInt>(A->getOperand(2 * AI + 1)));
529 ++AI;
530 }
531 while (BI < BN) {
532 addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
533 cast<ConstantInt>(B->getOperand(2 * BI + 1)));
534 ++BI;
535 }
537 // If we have more than 2 ranges (4 endpoints) we have to try to merge
538 // the last and first ones.
539 unsigned Size = EndPoints.size();
540 if (Size > 4) {
541 ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
542 ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
543 if (tryMergeRange(EndPoints, FB, FE)) {
544 for (unsigned i = 0; i < Size - 2; ++i) {
545 EndPoints[i] = EndPoints[i + 2];
546 }
547 EndPoints.resize(Size - 2);
548 }
549 }
551 // If in the end we have a single range, it is possible that it is now the
552 // full range. Just drop the metadata in that case.
553 if (EndPoints.size() == 2) {
554 ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
555 cast<ConstantInt>(EndPoints[1])->getValue());
556 if (Range.isFullSet())
557 return nullptr;
558 }
560 return MDNode::get(A->getContext(), EndPoints);
561 }
563 //===----------------------------------------------------------------------===//
564 // NamedMDNode implementation.
565 //
567 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
568 return *(SmallVector<TrackingVH<MDNode>, 4> *)Operands;
569 }
571 NamedMDNode::NamedMDNode(const Twine &N)
572 : Name(N.str()), Parent(nullptr),
573 Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {}
575 NamedMDNode::~NamedMDNode() {
576 dropAllReferences();
577 delete &getNMDOps(Operands);
578 }
580 unsigned NamedMDNode::getNumOperands() const {
581 return (unsigned)getNMDOps(Operands).size();
582 }
584 MDNode *NamedMDNode::getOperand(unsigned i) const {
585 assert(i < getNumOperands() && "Invalid Operand number!");
586 return &*getNMDOps(Operands)[i];
587 }
589 void NamedMDNode::addOperand(MDNode *M) {
590 assert(!M->isFunctionLocal() &&
591 "NamedMDNode operands must not be function-local!");
592 getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
593 }
595 void NamedMDNode::eraseFromParent() {
596 getParent()->eraseNamedMetadata(this);
597 }
599 void NamedMDNode::dropAllReferences() {
600 getNMDOps(Operands).clear();
601 }
603 StringRef NamedMDNode::getName() const {
604 return StringRef(Name);
605 }
607 //===----------------------------------------------------------------------===//
608 // Instruction Metadata method implementations.
609 //
611 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
612 if (!Node && !hasMetadata())
613 return;
614 setMetadata(getContext().getMDKindID(Kind), Node);
615 }
617 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
618 return getMetadataImpl(getContext().getMDKindID(Kind));
619 }
621 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
622 SmallSet<unsigned, 5> KnownSet;
623 KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
625 // Drop debug if needed
626 if (KnownSet.erase(LLVMContext::MD_dbg))
627 DbgLoc = DebugLoc();
629 if (!hasMetadataHashEntry())
630 return; // Nothing to remove!
632 DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
633 getContext().pImpl->MetadataStore;
635 if (KnownSet.empty()) {
636 // Just drop our entry at the store.
637 MetadataStore.erase(this);
638 setHasMetadataHashEntry(false);
639 return;
640 }
642 LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
643 unsigned I;
644 unsigned E;
645 // Walk the array and drop any metadata we don't know.
646 for (I = 0, E = Info.size(); I != E;) {
647 if (KnownSet.count(Info[I].first)) {
648 ++I;
649 continue;
650 }
652 Info[I] = Info.back();
653 Info.pop_back();
654 --E;
655 }
656 assert(E == Info.size());
658 if (E == 0) {
659 // Drop our entry at the store.
660 MetadataStore.erase(this);
661 setHasMetadataHashEntry(false);
662 }
663 }
665 /// setMetadata - Set the metadata of of the specified kind to the specified
666 /// node. This updates/replaces metadata if already present, or removes it if
667 /// Node is null.
668 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
669 if (!Node && !hasMetadata())
670 return;
672 // Handle 'dbg' as a special case since it is not stored in the hash table.
673 if (KindID == LLVMContext::MD_dbg) {
674 DbgLoc = DebugLoc::getFromDILocation(Node);
675 return;
676 }
678 // Handle the case when we're adding/updating metadata on an instruction.
679 if (Node) {
680 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
681 assert(!Info.empty() == hasMetadataHashEntry() &&
682 "HasMetadata bit is wonked");
683 if (Info.empty()) {
684 setHasMetadataHashEntry(true);
685 } else {
686 // Handle replacement of an existing value.
687 for (auto &P : Info)
688 if (P.first == KindID) {
689 P.second = Node;
690 return;
691 }
692 }
694 // No replacement, just add it to the list.
695 Info.push_back(std::make_pair(KindID, Node));
696 return;
697 }
699 // Otherwise, we're removing metadata from an instruction.
700 assert((hasMetadataHashEntry() ==
701 (getContext().pImpl->MetadataStore.count(this) > 0)) &&
702 "HasMetadata bit out of date!");
703 if (!hasMetadataHashEntry())
704 return; // Nothing to remove!
705 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
707 // Common case is removing the only entry.
708 if (Info.size() == 1 && Info[0].first == KindID) {
709 getContext().pImpl->MetadataStore.erase(this);
710 setHasMetadataHashEntry(false);
711 return;
712 }
714 // Handle removal of an existing value.
715 for (unsigned i = 0, e = Info.size(); i != e; ++i)
716 if (Info[i].first == KindID) {
717 Info[i] = Info.back();
718 Info.pop_back();
719 assert(!Info.empty() && "Removing last entry should be handled above");
720 return;
721 }
722 // Otherwise, removing an entry that doesn't exist on the instruction.
723 }
725 void Instruction::setAAMetadata(const AAMDNodes &N) {
726 setMetadata(LLVMContext::MD_tbaa, N.TBAA);
727 setMetadata(LLVMContext::MD_alias_scope, N.Scope);
728 setMetadata(LLVMContext::MD_noalias, N.NoAlias);
729 }
731 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
732 // Handle 'dbg' as a special case since it is not stored in the hash table.
733 if (KindID == LLVMContext::MD_dbg)
734 return DbgLoc.getAsMDNode(getContext());
736 if (!hasMetadataHashEntry()) return nullptr;
738 LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
739 assert(!Info.empty() && "bit out of sync with hash table");
741 for (const auto &I : Info)
742 if (I.first == KindID)
743 return I.second;
744 return nullptr;
745 }
747 void Instruction::getAllMetadataImpl(
748 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
749 Result.clear();
751 // Handle 'dbg' as a special case since it is not stored in the hash table.
752 if (!DbgLoc.isUnknown()) {
753 Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
754 DbgLoc.getAsMDNode(getContext())));
755 if (!hasMetadataHashEntry()) return;
756 }
758 assert(hasMetadataHashEntry() &&
759 getContext().pImpl->MetadataStore.count(this) &&
760 "Shouldn't have called this");
761 const LLVMContextImpl::MDMapTy &Info =
762 getContext().pImpl->MetadataStore.find(this)->second;
763 assert(!Info.empty() && "Shouldn't have called this");
765 Result.append(Info.begin(), Info.end());
767 // Sort the resulting array so it is stable.
768 if (Result.size() > 1)
769 array_pod_sort(Result.begin(), Result.end());
770 }
772 void Instruction::getAllMetadataOtherThanDebugLocImpl(
773 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
774 Result.clear();
775 assert(hasMetadataHashEntry() &&
776 getContext().pImpl->MetadataStore.count(this) &&
777 "Shouldn't have called this");
778 const LLVMContextImpl::MDMapTy &Info =
779 getContext().pImpl->MetadataStore.find(this)->second;
780 assert(!Info.empty() && "Shouldn't have called this");
781 Result.append(Info.begin(), Info.end());
783 // Sort the resulting array so it is stable.
784 if (Result.size() > 1)
785 array_pod_sort(Result.begin(), Result.end());
786 }
788 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
789 /// this instruction.
790 void Instruction::clearMetadataHashEntries() {
791 assert(hasMetadataHashEntry() && "Caller should check");
792 getContext().pImpl->MetadataStore.erase(this);
793 setHasMetadataHashEntry(false);
794 }