1 //===-- Value.cpp - Implement the Value class -----------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Value, ValueHandle, and User classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/Value.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/IR/Constant.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/DerivedUser.h"
23 #include "llvm/IR/GetElementPtrTypeIterator.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/Statepoint.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/IR/ValueSymbolTable.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Support/raw_ostream.h"
41 static cl::opt<unsigned> NonGlobalValueMaxNameSize(
42 "non-global-value-max-name-size", cl::Hidden, cl::init(1024),
43 cl::desc("Maximum size for the name of non-global values."));
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
48 static inline Type *checkType(Type *Ty) {
49 assert(Ty && "Value defined with a null type: Error!");
53 Value::Value(Type *ty, unsigned scid)
54 : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid),
55 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
56 NumUserOperands(0), IsUsedByMD(false), HasName(false) {
57 static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
58 // FIXME: Why isn't this in the subclass gunk??
59 // Note, we cannot call isa<CallInst> before the CallInst has been
61 if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke ||
62 SubclassID == Instruction::CallBr)
63 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
64 "invalid CallInst type!");
65 else if (SubclassID != BasicBlockVal &&
66 (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))
67 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
68 "Cannot create non-first-class values except for constants!");
69 static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
74 // Notify all ValueHandles (if present) that this value is going away.
76 ValueHandleBase::ValueIsDeleted(this);
77 if (isUsedByMetadata())
78 ValueAsMetadata::handleDeletion(this);
80 #ifndef NDEBUG // Only in -g mode...
81 // Check to make sure that there are no uses of this value that are still
82 // around when the value is destroyed. If there are, then we have a dangling
83 // reference and something is wrong. This code is here to print out where
84 // the value is still being referenced.
87 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
88 for (auto *U : users())
89 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
92 assert(use_empty() && "Uses remain when a value is destroyed!");
94 // If this value is named, destroy the name. This should not be in a symtab
99 void Value::deleteValue() {
100 switch (getValueID()) {
101 #define HANDLE_VALUE(Name) \
102 case Value::Name##Val: \
103 delete static_cast<Name *>(this); \
105 #define HANDLE_MEMORY_VALUE(Name) \
106 case Value::Name##Val: \
107 static_cast<DerivedUser *>(this)->DeleteValue( \
108 static_cast<DerivedUser *>(this)); \
110 #define HANDLE_INSTRUCTION(Name) /* nothing */
111 #include "llvm/IR/Value.def"
113 #define HANDLE_INST(N, OPC, CLASS) \
114 case Value::InstructionVal + Instruction::OPC: \
115 delete static_cast<CLASS *>(this); \
117 #define HANDLE_USER_INST(N, OPC, CLASS)
118 #include "llvm/IR/Instruction.def"
121 llvm_unreachable("attempting to delete unknown value kind");
125 void Value::destroyValueName() {
126 ValueName *Name = getValueName();
129 setValueName(nullptr);
132 bool Value::hasNUses(unsigned N) const {
133 return hasNItems(use_begin(), use_end(), N);
136 bool Value::hasNUsesOrMore(unsigned N) const {
137 return hasNItemsOrMore(use_begin(), use_end(), N);
140 bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
141 // This can be computed either by scanning the instructions in BB, or by
142 // scanning the use list of this Value. Both lists can be very long, but
143 // usually one is quite short.
145 // Scan both lists simultaneously until one is exhausted. This limits the
146 // search to the shorter list.
147 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
148 const_user_iterator UI = user_begin(), UE = user_end();
149 for (; BI != BE && UI != UE; ++BI, ++UI) {
150 // Scan basic block: Check if this Value is used by the instruction at BI.
151 if (is_contained(BI->operands(), this))
153 // Scan use list: Check if the use at UI is in BB.
154 const auto *User = dyn_cast<Instruction>(*UI);
155 if (User && User->getParent() == BB)
161 unsigned Value::getNumUses() const {
162 return (unsigned)std::distance(use_begin(), use_end());
165 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
167 if (Instruction *I = dyn_cast<Instruction>(V)) {
168 if (BasicBlock *P = I->getParent())
169 if (Function *PP = P->getParent())
170 ST = PP->getValueSymbolTable();
171 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
172 if (Function *P = BB->getParent())
173 ST = P->getValueSymbolTable();
174 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
175 if (Module *P = GV->getParent())
176 ST = &P->getValueSymbolTable();
177 } else if (Argument *A = dyn_cast<Argument>(V)) {
178 if (Function *P = A->getParent())
179 ST = P->getValueSymbolTable();
181 assert(isa<Constant>(V) && "Unknown value type!");
182 return true; // no name is setable for this.
187 ValueName *Value::getValueName() const {
188 if (!HasName) return nullptr;
190 LLVMContext &Ctx = getContext();
191 auto I = Ctx.pImpl->ValueNames.find(this);
192 assert(I != Ctx.pImpl->ValueNames.end() &&
193 "No name entry found!");
198 void Value::setValueName(ValueName *VN) {
199 LLVMContext &Ctx = getContext();
201 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
202 "HasName bit out of sync!");
206 Ctx.pImpl->ValueNames.erase(this);
212 Ctx.pImpl->ValueNames[this] = VN;
215 StringRef Value::getName() const {
216 // Make sure the empty string is still a C string. For historical reasons,
217 // some clients want to call .data() on the result and expect it to be null
220 return StringRef("", 0);
221 return getValueName()->getKey();
224 void Value::setNameImpl(const Twine &NewName) {
225 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
226 if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
229 // Fast path for common IRBuilder case of setName("") when there is no name.
230 if (NewName.isTriviallyEmpty() && !hasName())
233 SmallString<256> NameData;
234 StringRef NameRef = NewName.toStringRef(NameData);
235 assert(NameRef.find_first_of(0) == StringRef::npos &&
236 "Null bytes are not allowed in names");
238 // Name isn't changing?
239 if (getName() == NameRef)
242 // Cap the size of non-GlobalValue names.
243 if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this))
245 NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize));
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, ReplaceMetadataUses ReplaceMetaUses) {
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 (ReplaceMetaUses == ReplaceMetadataUses::Yes && 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, ReplaceMetadataUses::Yes);
434 void Value::replaceNonMetadataUsesWith(Value *New) {
435 doRAUW(New, ReplaceMetadataUses::No);
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 replaceUsesWithIf(New, [BB](Use &U) {
449 auto *I = dyn_cast<Instruction>(U.getUser());
450 // Don't replace if it's an instruction in the BB basic block.
451 return !I || I->getParent() != BB;
456 // Various metrics for how much to strip off of pointers.
457 enum PointerStripKind {
459 PSK_ZeroIndicesAndAliases,
460 PSK_ZeroIndicesSameRepresentation,
461 PSK_ZeroIndicesAndInvariantGroups,
462 PSK_InBoundsConstantIndices,
466 template <PointerStripKind StripKind>
467 static const Value *stripPointerCastsAndOffsets(const Value *V) {
468 if (!V->getType()->isPointerTy())
471 // Even though we don't look through PHI nodes, we could be called on an
472 // instruction in an unreachable block, which may be on a cycle.
473 SmallPtrSet<const Value *, 4> Visited;
477 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
479 case PSK_ZeroIndices:
480 case PSK_ZeroIndicesAndAliases:
481 case PSK_ZeroIndicesSameRepresentation:
482 case PSK_ZeroIndicesAndInvariantGroups:
483 if (!GEP->hasAllZeroIndices())
486 case PSK_InBoundsConstantIndices:
487 if (!GEP->hasAllConstantIndices())
491 if (!GEP->isInBounds())
495 V = GEP->getPointerOperand();
496 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
497 V = cast<Operator>(V)->getOperand(0);
498 } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
499 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
500 // TODO: If we know an address space cast will not change the
501 // representation we could look through it here as well.
502 V = cast<Operator>(V)->getOperand(0);
503 } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
504 V = cast<GlobalAlias>(V)->getAliasee();
506 if (const auto *Call = dyn_cast<CallBase>(V)) {
507 if (const Value *RV = Call->getReturnedArgOperand()) {
511 // The result of launder.invariant.group must alias it's argument,
512 // but it can't be marked with returned attribute, that's why it needs
514 if (StripKind == PSK_ZeroIndicesAndInvariantGroups &&
515 (Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
516 Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {
517 V = Call->getArgOperand(0);
523 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
524 } while (Visited.insert(V).second);
528 } // end anonymous namespace
530 const Value *Value::stripPointerCasts() const {
531 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
534 const Value *Value::stripPointerCastsAndAliases() const {
535 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
538 const Value *Value::stripPointerCastsSameRepresentation() const {
539 return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);
542 const Value *Value::stripInBoundsConstantOffsets() const {
543 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
546 const Value *Value::stripPointerCastsAndInvariantGroups() const {
547 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndInvariantGroups>(this);
551 Value::stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset,
552 bool AllowNonInbounds) const {
553 if (!getType()->isPtrOrPtrVectorTy())
556 unsigned BitWidth = Offset.getBitWidth();
557 assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
558 "The offset bit width does not match the DL specification.");
560 // Even though we don't look through PHI nodes, we could be called on an
561 // instruction in an unreachable block, which may be on a cycle.
562 SmallPtrSet<const Value *, 4> Visited;
563 Visited.insert(this);
564 const Value *V = this;
566 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
567 // If in-bounds was requested, we do not strip non-in-bounds GEPs.
568 if (!AllowNonInbounds && !GEP->isInBounds())
571 // If one of the values we have visited is an addrspacecast, then
572 // the pointer type of this GEP may be different from the type
573 // of the Ptr parameter which was passed to this function. This
574 // means when we construct GEPOffset, we need to use the size
575 // of GEP's pointer type rather than the size of the original
577 APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
578 if (!GEP->accumulateConstantOffset(DL, GEPOffset))
581 // Stop traversal if the pointer offset wouldn't fit in the bit-width
582 // provided by the Offset argument. This can happen due to AddrSpaceCast
584 if (GEPOffset.getMinSignedBits() > BitWidth)
587 Offset += GEPOffset.sextOrTrunc(BitWidth);
588 V = GEP->getPointerOperand();
589 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
590 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
591 V = cast<Operator>(V)->getOperand(0);
592 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
593 if (!GA->isInterposable())
594 V = GA->getAliasee();
595 } else if (const auto *Call = dyn_cast<CallBase>(V)) {
596 if (const Value *RV = Call->getReturnedArgOperand())
599 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
600 } while (Visited.insert(V).second);
605 const Value *Value::stripInBoundsOffsets() const {
606 return stripPointerCastsAndOffsets<PSK_InBounds>(this);
609 uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
610 bool &CanBeNull) const {
611 assert(getType()->isPointerTy() && "must be pointer");
613 uint64_t DerefBytes = 0;
615 if (const Argument *A = dyn_cast<Argument>(this)) {
616 DerefBytes = A->getDereferenceableBytes();
617 if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) {
618 Type *PT = cast<PointerType>(A->getType())->getElementType();
620 DerefBytes = DL.getTypeStoreSize(PT);
622 if (DerefBytes == 0) {
623 DerefBytes = A->getDereferenceableOrNullBytes();
626 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
627 DerefBytes = Call->getDereferenceableBytes(AttributeList::ReturnIndex);
628 if (DerefBytes == 0) {
630 Call->getDereferenceableOrNullBytes(AttributeList::ReturnIndex);
633 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
634 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
635 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
636 DerefBytes = CI->getLimitedValue();
638 if (DerefBytes == 0) {
640 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
641 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
642 DerefBytes = CI->getLimitedValue();
646 } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {
647 if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {
648 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
649 DerefBytes = CI->getLimitedValue();
651 if (DerefBytes == 0) {
653 IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
654 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
655 DerefBytes = CI->getLimitedValue();
659 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {
660 if (!AI->isArrayAllocation()) {
661 DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType());
664 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
665 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
666 // TODO: Don't outright reject hasExternalWeakLinkage but set the
668 DerefBytes = DL.getTypeStoreSize(GV->getValueType());
675 MaybeAlign Value::getPointerAlignment(const DataLayout &DL) const {
676 assert(getType()->isPointerTy() && "must be pointer");
677 if (auto *GO = dyn_cast<GlobalObject>(this)) {
678 if (isa<Function>(GO)) {
679 const MaybeAlign FunctionPtrAlign = DL.getFunctionPtrAlign();
680 switch (DL.getFunctionPtrAlignType()) {
681 case DataLayout::FunctionPtrAlignType::Independent:
682 return FunctionPtrAlign;
683 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
684 return std::max(FunctionPtrAlign, MaybeAlign(GO->getAlignment()));
686 llvm_unreachable("Unhandled FunctionPtrAlignType");
688 const MaybeAlign Alignment(GO->getAlignment());
690 if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
691 Type *ObjectType = GVar->getValueType();
692 if (ObjectType->isSized()) {
693 // If the object is defined in the current Module, we'll be giving
694 // it the preferred alignment. Otherwise, we have to assume that it
695 // may only have the minimum ABI alignment.
696 if (GVar->isStrongDefinitionForLinker())
697 return MaybeAlign(DL.getPreferredAlignment(GVar));
699 return Align(DL.getABITypeAlignment(ObjectType));
704 } else if (const Argument *A = dyn_cast<Argument>(this)) {
705 const MaybeAlign Alignment(A->getParamAlignment());
706 if (!Alignment && A->hasStructRetAttr()) {
707 // An sret parameter has at least the ABI alignment of the return type.
708 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
709 if (EltTy->isSized())
710 return Align(DL.getABITypeAlignment(EltTy));
713 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
714 const MaybeAlign Alignment(AI->getAlignment());
716 Type *AllocatedType = AI->getAllocatedType();
717 if (AllocatedType->isSized())
718 return MaybeAlign(DL.getPrefTypeAlignment(AllocatedType));
721 } else if (const auto *Call = dyn_cast<CallBase>(this)) {
722 const MaybeAlign Alignment(Call->getRetAlignment());
723 if (!Alignment && Call->getCalledFunction())
725 Call->getCalledFunction()->getAttributes().getRetAlignment());
727 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
728 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
729 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
730 return MaybeAlign(CI->getLimitedValue());
736 const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
737 const BasicBlock *PredBB) const {
738 auto *PN = dyn_cast<PHINode>(this);
739 if (PN && PN->getParent() == CurBB)
740 return PN->getIncomingValueForBlock(PredBB);
744 LLVMContext &Value::getContext() const { return VTy->getContext(); }
746 void Value::reverseUseList() {
747 if (!UseList || !UseList->Next)
748 // No need to reverse 0 or 1 uses.
752 Use *Current = UseList->Next;
753 Head->Next = nullptr;
755 Use *Next = Current->Next;
756 Current->Next = Head;
757 Head->setPrev(&Current->Next);
762 Head->setPrev(&UseList);
765 bool Value::isSwiftError() const {
766 auto *Arg = dyn_cast<Argument>(this);
768 return Arg->hasSwiftErrorAttr();
769 auto *Alloca = dyn_cast<AllocaInst>(this);
772 return Alloca->isSwiftError();
775 //===----------------------------------------------------------------------===//
776 // ValueHandleBase Class
777 //===----------------------------------------------------------------------===//
779 void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
780 assert(List && "Handle list is null?");
782 // Splice ourselves into the list.
787 Next->setPrevPtr(&Next);
788 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
792 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
793 assert(List && "Must insert after existing node");
796 setPrevPtr(&List->Next);
799 Next->setPrevPtr(&Next);
802 void ValueHandleBase::AddToUseList() {
803 assert(getValPtr() && "Null pointer doesn't have a use list!");
805 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
807 if (getValPtr()->HasValueHandle) {
808 // If this value already has a ValueHandle, then it must be in the
809 // ValueHandles map already.
810 ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
811 assert(Entry && "Value doesn't have any handles?");
812 AddToExistingUseList(&Entry);
816 // Ok, it doesn't have any handles yet, so we must insert it into the
817 // DenseMap. However, doing this insertion could cause the DenseMap to
818 // reallocate itself, which would invalidate all of the PrevP pointers that
819 // point into the old table. Handle this by checking for reallocation and
820 // updating the stale pointers only if needed.
821 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
822 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
824 ValueHandleBase *&Entry = Handles[getValPtr()];
825 assert(!Entry && "Value really did already have handles?");
826 AddToExistingUseList(&Entry);
827 getValPtr()->HasValueHandle = true;
829 // If reallocation didn't happen or if this was the first insertion, don't
831 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
832 Handles.size() == 1) {
836 // Okay, reallocation did happen. Fix the Prev Pointers.
837 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
838 E = Handles.end(); I != E; ++I) {
839 assert(I->second && I->first == I->second->getValPtr() &&
840 "List invariant broken!");
841 I->second->setPrevPtr(&I->second);
845 void ValueHandleBase::RemoveFromUseList() {
846 assert(getValPtr() && getValPtr()->HasValueHandle &&
847 "Pointer doesn't have a use list!");
849 // Unlink this from its use list.
850 ValueHandleBase **PrevPtr = getPrevPtr();
851 assert(*PrevPtr == this && "List invariant broken");
855 assert(Next->getPrevPtr() == &Next && "List invariant broken");
856 Next->setPrevPtr(PrevPtr);
860 // If the Next pointer was null, then it is possible that this was the last
861 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
863 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
864 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
865 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
866 Handles.erase(getValPtr());
867 getValPtr()->HasValueHandle = false;
871 void ValueHandleBase::ValueIsDeleted(Value *V) {
872 assert(V->HasValueHandle && "Should only be called if ValueHandles present");
874 // Get the linked list base, which is guaranteed to exist since the
875 // HasValueHandle flag is set.
876 LLVMContextImpl *pImpl = V->getContext().pImpl;
877 ValueHandleBase *Entry = pImpl->ValueHandles[V];
878 assert(Entry && "Value bit set but no entries exist");
880 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
881 // and remove themselves from the list without breaking our iteration. This
882 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
883 // Note that we deliberately do not the support the case when dropping a value
884 // handle results in a new value handle being permanently added to the list
885 // (as might occur in theory for CallbackVH's): the new value handle will not
886 // be processed and the checking code will mete out righteous punishment if
887 // the handle is still present once we have finished processing all the other
888 // value handles (it is fine to momentarily add then remove a value handle).
889 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
890 Iterator.RemoveFromUseList();
891 Iterator.AddToExistingUseListAfter(Entry);
892 assert(Entry->Next == &Iterator && "Loop invariant broken.");
894 switch (Entry->getKind()) {
899 // WeakTracking and Weak just go to null, which unlinks them
901 Entry->operator=(nullptr);
904 // Forward to the subclass's implementation.
905 static_cast<CallbackVH*>(Entry)->deleted();
910 // All callbacks, weak references, and assertingVHs should be dropped by now.
911 if (V->HasValueHandle) {
912 #ifndef NDEBUG // Only in +Asserts mode...
913 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
915 if (pImpl->ValueHandles[V]->getKind() == Assert)
916 llvm_unreachable("An asserting value handle still pointed to this"
920 llvm_unreachable("All references to V were not removed?");
924 void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
925 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
926 assert(Old != New && "Changing value into itself!");
927 assert(Old->getType() == New->getType() &&
928 "replaceAllUses of value with new value of different type!");
930 // Get the linked list base, which is guaranteed to exist since the
931 // HasValueHandle flag is set.
932 LLVMContextImpl *pImpl = Old->getContext().pImpl;
933 ValueHandleBase *Entry = pImpl->ValueHandles[Old];
935 assert(Entry && "Value bit set but no entries exist");
937 // We use a local ValueHandleBase as an iterator so that
938 // ValueHandles can add and remove themselves from the list without
939 // breaking our iteration. This is not really an AssertingVH; we
940 // just have to give ValueHandleBase some kind.
941 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
942 Iterator.RemoveFromUseList();
943 Iterator.AddToExistingUseListAfter(Entry);
944 assert(Entry->Next == &Iterator && "Loop invariant broken.");
946 switch (Entry->getKind()) {
949 // Asserting and Weak handles do not follow RAUW implicitly.
952 // Weak goes to the new value, which will unlink it from Old's list.
953 Entry->operator=(New);
956 // Forward to the subclass's implementation.
957 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
963 // If any new weak value handles were added while processing the
964 // list, then complain about it now.
965 if (Old->HasValueHandle)
966 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
967 switch (Entry->getKind()) {
969 dbgs() << "After RAUW from " << *Old->getType() << " %"
970 << Old->getName() << " to " << *New->getType() << " %"
971 << New->getName() << "\n";
973 "A weak tracking value handle still pointed to the old value!\n");
980 // Pin the vtable to this file.
981 void CallbackVH::anchor() {}