1 //===-- Value.cpp - Implement the Value class -----------------------------===//
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 Value, ValueHandle, and User classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/Value.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/Constant.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/DerivedUser.h"
25 #include "llvm/IR/GetElementPtrTypeIterator.h"
26 #include "llvm/IR/InstrTypes.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/Statepoint.h"
32 #include "llvm/IR/ValueHandle.h"
33 #include "llvm/IR/ValueSymbolTable.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/raw_ostream.h"
42 //===----------------------------------------------------------------------===//
44 //===----------------------------------------------------------------------===//
45 static inline Type *checkType(Type *Ty) {
46 assert(Ty && "Value defined with a null type: Error!");
50 Value::Value(Type *ty, unsigned scid)
51 : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
52 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
53 NumUserOperands(0), IsUsedByMD(false), HasName(false) {
54 static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
55 // FIXME: Why isn't this in the subclass gunk??
56 // Note, we cannot call isa<CallInst> before the CallInst has been
58 if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke)
59 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
60 "invalid CallInst type!");
61 else if (SubclassID != BasicBlockVal &&
62 (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))
63 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
64 "Cannot create non-first-class values except for constants!");
65 static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
70 // Notify all ValueHandles (if present) that this value is going away.
72 ValueHandleBase::ValueIsDeleted(this);
73 if (isUsedByMetadata())
74 ValueAsMetadata::handleDeletion(this);
76 #ifndef NDEBUG // Only in -g mode...
77 // Check to make sure that there are no uses of this value that are still
78 // around when the value is destroyed. If there are, then we have a dangling
79 // reference and something is wrong. This code is here to print out where
80 // the value is still being referenced.
83 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
84 for (auto *U : users())
85 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
88 assert(use_empty() && "Uses remain when a value is destroyed!");
90 // If this value is named, destroy the name. This should not be in a symtab
95 void Value::deleteValue() {
96 switch (getValueID()) {
97 #define HANDLE_VALUE(Name) \
98 case Value::Name##Val: \
99 delete static_cast<Name *>(this); \
101 #define HANDLE_MEMORY_VALUE(Name) \
102 case Value::Name##Val: \
103 static_cast<DerivedUser *>(this)->DeleteValue( \
104 static_cast<DerivedUser *>(this)); \
106 #define HANDLE_INSTRUCTION(Name) /* nothing */
107 #include "llvm/IR/Value.def"
109 #define HANDLE_INST(N, OPC, CLASS) \
110 case Value::InstructionVal + Instruction::OPC: \
111 delete static_cast<CLASS *>(this); \
113 #define HANDLE_USER_INST(N, OPC, CLASS)
114 #include "llvm/IR/Instruction.def"
117 llvm_unreachable("attempting to delete unknown value kind");
121 void Value::destroyValueName() {
122 ValueName *Name = getValueName();
125 setValueName(nullptr);
128 bool Value::hasNUses(unsigned N) const {
129 const_use_iterator UI = use_begin(), E = use_end();
132 if (UI == E) return false; // Too few.
136 bool Value::hasNUsesOrMore(unsigned N) const {
137 const_use_iterator UI = use_begin(), E = use_end();
140 if (UI == E) return false; // Too few.
145 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
146 // This can be computed either by scanning the instructions in BB, or by
147 // scanning the use list of this Value. Both lists can be very long, but
148 // usually one is quite short.
150 // Scan both lists simultaneously until one is exhausted. This limits the
151 // search to the shorter list.
152 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
153 const_user_iterator UI = user_begin(), UE = user_end();
154 for (; BI != BE && UI != UE; ++BI, ++UI) {
155 // Scan basic block: Check if this Value is used by the instruction at BI.
156 if (is_contained(BI->operands(), this))
158 // Scan use list: Check if the use at UI is in BB.
159 const auto *User = dyn_cast<Instruction>(*UI);
160 if (User && User->getParent() == BB)
166 unsigned Value::getNumUses() const {
167 return (unsigned)std::distance(use_begin(), use_end());
170 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
172 if (Instruction *I = dyn_cast<Instruction>(V)) {
173 if (BasicBlock *P = I->getParent())
174 if (Function *PP = P->getParent())
175 ST = PP->getValueSymbolTable();
176 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
177 if (Function *P = BB->getParent())
178 ST = P->getValueSymbolTable();
179 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
180 if (Module *P = GV->getParent())
181 ST = &P->getValueSymbolTable();
182 } else if (Argument *A = dyn_cast<Argument>(V)) {
183 if (Function *P = A->getParent())
184 ST = P->getValueSymbolTable();
186 assert(isa<Constant>(V) && "Unknown value type!");
187 return true; // no name is setable for this.
192 ValueName *Value::getValueName() const {
193 if (!HasName) return nullptr;
195 LLVMContext &Ctx = getContext();
196 auto I = Ctx.pImpl->ValueNames.find(this);
197 assert(I != Ctx.pImpl->ValueNames.end() &&
198 "No name entry found!");
203 void Value::setValueName(ValueName *VN) {
204 LLVMContext &Ctx = getContext();
206 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
207 "HasName bit out of sync!");
211 Ctx.pImpl->ValueNames.erase(this);
217 Ctx.pImpl->ValueNames[this] = VN;
220 StringRef Value::getName() const {
221 // Make sure the empty string is still a C string. For historical reasons,
222 // some clients want to call .data() on the result and expect it to be null
225 return StringRef("", 0);
226 return getValueName()->getKey();
229 void Value::setNameImpl(const Twine &NewName) {
230 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
231 if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
234 // Fast path for common IRBuilder case of setName("") when there is no name.
235 if (NewName.isTriviallyEmpty() && !hasName())
238 SmallString<256> NameData;
239 StringRef NameRef = NewName.toStringRef(NameData);
240 assert(NameRef.find_first_of(0) == StringRef::npos &&
241 "Null bytes are not allowed in names");
243 // Name isn't changing?
244 if (getName() == NameRef)
247 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
249 // Get the symbol table to update for this object.
250 ValueSymbolTable *ST;
251 if (getSymTab(this, ST))
252 return; // Cannot set a name on this value (e.g. constant).
254 if (!ST) { // No symbol table to update? Just do the change.
255 if (NameRef.empty()) {
256 // Free the name for this value.
261 // NOTE: Could optimize for the case the name is shrinking to not deallocate
265 // Create the new name.
266 setValueName(ValueName::Create(NameRef));
267 getValueName()->setValue(this);
271 // NOTE: Could optimize for the case the name is shrinking to not deallocate
275 ST->removeValueName(getValueName());
282 // Name is changing to something new.
283 setValueName(ST->createValueName(NameRef, this));
286 void Value::setName(const Twine &NewName) {
287 setNameImpl(NewName);
288 if (Function *F = dyn_cast<Function>(this))
289 F->recalculateIntrinsicID();
292 void Value::takeName(Value *V) {
293 ValueSymbolTable *ST = nullptr;
294 // If this value has a name, drop it.
296 // Get the symtab this is in.
297 if (getSymTab(this, ST)) {
298 // We can't set a name on this value, but we need to clear V's name if
300 if (V->hasName()) V->setName("");
301 return; // Cannot set a name on this value (e.g. constant).
306 ST->removeValueName(getValueName());
310 // Now we know that this has no name.
312 // If V has no name either, we're done.
313 if (!V->hasName()) return;
315 // Get this's symtab if we didn't before.
317 if (getSymTab(this, ST)) {
320 return; // Cannot set a name on this value (e.g. constant).
324 // Get V's ST, this should always succed, because V has a name.
325 ValueSymbolTable *VST;
326 bool Failure = getSymTab(V, VST);
327 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
329 // If these values are both in the same symtab, we can do this very fast.
330 // This works even if both values have no symtab yet.
333 setValueName(V->getValueName());
334 V->setValueName(nullptr);
335 getValueName()->setValue(this);
339 // Otherwise, things are slightly more complex. Remove V's name from VST and
340 // then reinsert it into ST.
343 VST->removeValueName(V->getValueName());
344 setValueName(V->getValueName());
345 V->setValueName(nullptr);
346 getValueName()->setValue(this);
349 ST->reinsertValue(this);
352 void Value::assertModuleIsMaterializedImpl() const {
354 const GlobalValue *GV = dyn_cast<GlobalValue>(this);
357 const Module *M = GV->getParent();
360 assert(M->isMaterialized());
365 static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
367 if (!Cache.insert(Expr).second)
370 for (auto &O : Expr->operands()) {
373 auto *CE = dyn_cast<ConstantExpr>(O);
376 if (contains(Cache, CE, C))
382 static bool contains(Value *Expr, Value *V) {
386 auto *C = dyn_cast<Constant>(V);
390 auto *CE = dyn_cast<ConstantExpr>(Expr);
394 SmallPtrSet<ConstantExpr *, 4> Cache;
395 return contains(Cache, CE, C);
399 void Value::doRAUW(Value *New, bool NoMetadata) {
400 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
401 assert(!contains(New, this) &&
402 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
403 assert(New->getType() == getType() &&
404 "replaceAllUses of value with new value of different type!");
406 // Notify all ValueHandles (if present) that this value is going away.
408 ValueHandleBase::ValueIsRAUWd(this, New);
409 if (!NoMetadata && isUsedByMetadata())
410 ValueAsMetadata::handleRAUW(this, New);
412 while (!materialized_use_empty()) {
414 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
415 // constant because they are uniqued.
416 if (auto *C = dyn_cast<Constant>(U.getUser())) {
417 if (!isa<GlobalValue>(C)) {
418 C->handleOperandChange(this, New);
426 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
427 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
430 void Value::replaceAllUsesWith(Value *New) {
431 doRAUW(New, false /* NoMetadata */);
434 void Value::replaceNonMetadataUsesWith(Value *New) {
435 doRAUW(New, true /* NoMetadata */);
438 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
439 // This routine leaves uses within BB.
440 void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
441 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
442 assert(!contains(New, this) &&
443 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
444 assert(New->getType() == getType() &&
445 "replaceUses of value with new value of different type!");
446 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
448 use_iterator UI = use_begin(), E = use_end();
452 auto *Usr = dyn_cast<Instruction>(U.getUser());
453 if (Usr && Usr->getParent() == BB)
459 void Value::replaceUsesExceptBlockAddr(Value *New) {
460 SmallSetVector<Constant *, 4> Constants;
461 use_iterator UI = use_begin(), E = use_end();
466 if (isa<BlockAddress>(U.getUser()))
469 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
470 // constant because they are uniqued.
471 if (auto *C = dyn_cast<Constant>(U.getUser())) {
472 if (!isa<GlobalValue>(C)) {
473 // Save unique users to avoid processing operand replacement
483 // Process operand replacement of saved constants.
484 for (auto *C : Constants)
485 C->handleOperandChange(this, New);
489 // Various metrics for how much to strip off of pointers.
490 enum PointerStripKind {
492 PSK_ZeroIndicesAndAliases,
493 PSK_ZeroIndicesAndAliasesAndBarriers,
494 PSK_InBoundsConstantIndices,
498 template <PointerStripKind StripKind>
499 static const Value *stripPointerCastsAndOffsets(const Value *V) {
500 if (!V->getType()->isPointerTy())
503 // Even though we don't look through PHI nodes, we could be called on an
504 // instruction in an unreachable block, which may be on a cycle.
505 SmallPtrSet<const Value *, 4> Visited;
509 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
511 case PSK_ZeroIndicesAndAliases:
512 case PSK_ZeroIndicesAndAliasesAndBarriers:
513 case PSK_ZeroIndices:
514 if (!GEP->hasAllZeroIndices())
517 case PSK_InBoundsConstantIndices:
518 if (!GEP->hasAllConstantIndices())
522 if (!GEP->isInBounds())
526 V = GEP->getPointerOperand();
527 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
528 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
529 V = cast<Operator>(V)->getOperand(0);
530 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
531 if (StripKind == PSK_ZeroIndices || GA->isInterposable())
533 V = GA->getAliasee();
535 if (auto CS = ImmutableCallSite(V)) {
536 if (const Value *RV = CS.getReturnedArgOperand()) {
540 // The result of invariant.group.barrier must alias it's argument,
541 // but it can't be marked with returned attribute, that's why it needs
543 if (StripKind == PSK_ZeroIndicesAndAliasesAndBarriers &&
544 CS.getIntrinsicID() == Intrinsic::invariant_group_barrier) {
545 V = CS.getArgOperand(0);
551 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
552 } while (Visited.insert(V).second);
556 } // end anonymous namespace
558 const Value *Value::stripPointerCasts() const {
559 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
562 const Value *Value::stripPointerCastsNoFollowAliases() const {
563 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
566 const Value *Value::stripInBoundsConstantOffsets() const {
567 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
570 const Value *Value::stripPointerCastsAndBarriers() const {
571 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndBarriers>(
576 Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL,
577 APInt &Offset) const {
578 if (!getType()->isPointerTy())
581 assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>(
582 getType())->getAddressSpace()) &&
583 "The offset must have exactly as many bits as our pointer.");
585 // Even though we don't look through PHI nodes, we could be called on an
586 // instruction in an unreachable block, which may be on a cycle.
587 SmallPtrSet<const Value *, 4> Visited;
588 Visited.insert(this);
589 const Value *V = this;
591 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
592 if (!GEP->isInBounds())
594 APInt GEPOffset(Offset);
595 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
598 V = GEP->getPointerOperand();
599 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
600 V = cast<Operator>(V)->getOperand(0);
601 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
602 V = GA->getAliasee();
604 if (auto CS = ImmutableCallSite(V))
605 if (const Value *RV = CS.getReturnedArgOperand()) {
612 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
613 } while (Visited.insert(V).second);
618 const Value *Value::stripInBoundsOffsets() const {
619 return stripPointerCastsAndOffsets<PSK_InBounds>(this);
622 uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
623 bool &CanBeNull) const {
624 assert(getType()->isPointerTy() && "must be pointer");
626 uint64_t DerefBytes = 0;
628 if (const Argument *A = dyn_cast<Argument>(this)) {
629 DerefBytes = A->getDereferenceableBytes();
630 if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) {
631 Type *PT = cast<PointerType>(A->getType())->getElementType();
633 DerefBytes = DL.getTypeStoreSize(PT);
635 if (DerefBytes == 0) {
636 DerefBytes = A->getDereferenceableOrNullBytes();
639 } else if (auto CS = ImmutableCallSite(this)) {
640 DerefBytes = CS.getDereferenceableBytes(AttributeList::ReturnIndex);
641 if (DerefBytes == 0) {
642 DerefBytes = CS.getDereferenceableOrNullBytes(AttributeList::ReturnIndex);
645 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
646 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
647 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
648 DerefBytes = CI->getLimitedValue();
650 if (DerefBytes == 0) {
652 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
653 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
654 DerefBytes = CI->getLimitedValue();
658 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
659 if (!AI->isArrayAllocation()) {
660 DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType());
663 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
664 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
665 // TODO: Don't outright reject hasExternalWeakLinkage but set the
667 DerefBytes = DL.getTypeStoreSize(GV->getValueType());
674 unsigned Value::getPointerAlignment(const DataLayout &DL) const {
675 assert(getType()->isPointerTy() && "must be pointer");
678 if (auto *GO = dyn_cast<GlobalObject>(this)) {
679 Align = GO->getAlignment();
681 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
682 Type *ObjectType = GVar->getValueType();
683 if (ObjectType->isSized()) {
684 // If the object is defined in the current Module, we'll be giving
685 // it the preferred alignment. Otherwise, we have to assume that it
686 // may only have the minimum ABI alignment.
687 if (GVar->isStrongDefinitionForLinker())
688 Align = DL.getPreferredAlignment(GVar);
690 Align = DL.getABITypeAlignment(ObjectType);
694 } else if (const Argument *A = dyn_cast<Argument>(this)) {
695 Align = A->getParamAlignment();
697 if (!Align && A->hasStructRetAttr()) {
698 // An sret parameter has at least the ABI alignment of the return type.
699 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
700 if (EltTy->isSized())
701 Align = DL.getABITypeAlignment(EltTy);
703 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
704 Align = AI->getAlignment();
706 Type *AllocatedType = AI->getAllocatedType();
707 if (AllocatedType->isSized())
708 Align = DL.getPrefTypeAlignment(AllocatedType);
710 } else if (auto CS = ImmutableCallSite(this))
711 Align = CS.getAttributes().getRetAlignment();
712 else if (const LoadInst *LI = dyn_cast<LoadInst>(this))
713 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
714 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
715 Align = CI->getLimitedValue();
721 const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
722 const BasicBlock *PredBB) const {
723 auto *PN = dyn_cast<PHINode>(this);
724 if (PN && PN->getParent() == CurBB)
725 return PN->getIncomingValueForBlock(PredBB);
729 LLVMContext &Value::getContext() const { return VTy->getContext(); }
731 void Value::reverseUseList() {
732 if (!UseList || !UseList->Next)
733 // No need to reverse 0 or 1 uses.
737 Use *Current = UseList->Next;
738 Head->Next = nullptr;
740 Use *Next = Current->Next;
741 Current->Next = Head;
742 Head->setPrev(&Current->Next);
747 Head->setPrev(&UseList);
750 bool Value::isSwiftError() const {
751 auto *Arg = dyn_cast<Argument>(this);
753 return Arg->hasSwiftErrorAttr();
754 auto *Alloca = dyn_cast<AllocaInst>(this);
757 return Alloca->isSwiftError();
760 //===----------------------------------------------------------------------===//
761 // ValueHandleBase Class
762 //===----------------------------------------------------------------------===//
764 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
765 assert(List && "Handle list is null?");
767 // Splice ourselves into the list.
772 Next->setPrevPtr(&Next);
773 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
777 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
778 assert(List && "Must insert after existing node");
781 setPrevPtr(&List->Next);
784 Next->setPrevPtr(&Next);
787 void ValueHandleBase::AddToUseList() {
788 assert(getValPtr() && "Null pointer doesn't have a use list!");
790 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
792 if (getValPtr()->HasValueHandle) {
793 // If this value already has a ValueHandle, then it must be in the
794 // ValueHandles map already.
795 ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
796 assert(Entry && "Value doesn't have any handles?");
797 AddToExistingUseList(&Entry);
801 // Ok, it doesn't have any handles yet, so we must insert it into the
802 // DenseMap. However, doing this insertion could cause the DenseMap to
803 // reallocate itself, which would invalidate all of the PrevP pointers that
804 // point into the old table. Handle this by checking for reallocation and
805 // updating the stale pointers only if needed.
806 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
807 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
809 ValueHandleBase *&Entry = Handles[getValPtr()];
810 assert(!Entry && "Value really did already have handles?");
811 AddToExistingUseList(&Entry);
812 getValPtr()->HasValueHandle = true;
814 // If reallocation didn't happen or if this was the first insertion, don't
816 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
817 Handles.size() == 1) {
821 // Okay, reallocation did happen. Fix the Prev Pointers.
822 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
823 E = Handles.end(); I != E; ++I) {
824 assert(I->second && I->first == I->second->getValPtr() &&
825 "List invariant broken!");
826 I->second->setPrevPtr(&I->second);
830 void ValueHandleBase::RemoveFromUseList() {
831 assert(getValPtr() && getValPtr()->HasValueHandle &&
832 "Pointer doesn't have a use list!");
834 // Unlink this from its use list.
835 ValueHandleBase **PrevPtr = getPrevPtr();
836 assert(*PrevPtr == this && "List invariant broken");
840 assert(Next->getPrevPtr() == &Next && "List invariant broken");
841 Next->setPrevPtr(PrevPtr);
845 // If the Next pointer was null, then it is possible that this was the last
846 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
848 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
849 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
850 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
851 Handles.erase(getValPtr());
852 getValPtr()->HasValueHandle = false;
856 void ValueHandleBase::ValueIsDeleted(Value *V) {
857 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
859 // Get the linked list base, which is guaranteed to exist since the
860 // HasValueHandle flag is set.
861 LLVMContextImpl *pImpl = V->getContext().pImpl;
862 ValueHandleBase *Entry = pImpl->ValueHandles[V];
863 assert(Entry && "Value bit set but no entries exist");
865 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
866 // and remove themselves from the list without breaking our iteration. This
867 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
868 // Note that we deliberately do not the support the case when dropping a value
869 // handle results in a new value handle being permanently added to the list
870 // (as might occur in theory for CallbackVH's): the new value handle will not
871 // be processed and the checking code will mete out righteous punishment if
872 // the handle is still present once we have finished processing all the other
873 // value handles (it is fine to momentarily add then remove a value handle).
874 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
875 Iterator.RemoveFromUseList();
876 Iterator.AddToExistingUseListAfter(Entry);
877 assert(Entry->Next == &Iterator && "Loop invariant broken.");
879 switch (Entry->getKind()) {
884 // WeakTracking and Weak just go to null, which unlinks them
886 Entry->operator=(nullptr);
889 // Forward to the subclass's implementation.
890 static_cast<CallbackVH*>(Entry)->deleted();
895 // All callbacks, weak references, and assertingVHs should be dropped by now.
896 if (V->HasValueHandle) {
897 #ifndef NDEBUG // Only in +Asserts mode...
898 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
900 if (pImpl->ValueHandles[V]->getKind() == Assert)
901 llvm_unreachable("An asserting value handle still pointed to this"
905 llvm_unreachable("All references to V were not removed?");
909 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
910 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
911 assert(Old != New && "Changing value into itself!");
912 assert(Old->getType() == New->getType() &&
913 "replaceAllUses of value with new value of different type!");
915 // Get the linked list base, which is guaranteed to exist since the
916 // HasValueHandle flag is set.
917 LLVMContextImpl *pImpl = Old->getContext().pImpl;
918 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
920 assert(Entry && "Value bit set but no entries exist");
922 // We use a local ValueHandleBase as an iterator so that
923 // ValueHandles can add and remove themselves from the list without
924 // breaking our iteration. This is not really an AssertingVH; we
925 // just have to give ValueHandleBase some kind.
926 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
927 Iterator.RemoveFromUseList();
928 Iterator.AddToExistingUseListAfter(Entry);
929 assert(Entry->Next == &Iterator && "Loop invariant broken.");
931 switch (Entry->getKind()) {
934 // Asserting and Weak handles do not follow RAUW implicitly.
937 // Weak goes to the new value, which will unlink it from Old's list.
938 Entry->operator=(New);
941 // Forward to the subclass's implementation.
942 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
948 // If any new weak value handles were added while processing the
949 // list, then complain about it now.
950 if (Old->HasValueHandle)
951 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
952 switch (Entry->getKind()) {
954 dbgs() << "After RAUW from " << *Old->getType() << " %"
955 << Old->getName() << " to " << *New->getType() << " %"
956 << New->getName() << "\n";
958 "A weak tracking value handle still pointed to the old value!\n");
965 // Pin the vtable to this file.
966 void CallbackVH::anchor() {}