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/DebugInfo.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/DerivedUser.h"
24 #include "llvm/IR/GetElementPtrTypeIterator.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/Operator.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> UseDerefAtPointSemantics(
42 "use-dereferenceable-at-point-semantics", cl::Hidden
, cl::init(false),
43 cl::desc("Deref attributes and metadata infer facts at definition only"));
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
), HasValueHandle(0),
55 SubclassOptionalData(0), SubclassData(0), NumUserOperands(0),
56 IsUsedByMD(false), HasName(false), HasMetadata(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
62 if (SubclassID
>= InstructionVal
)
63 OpCode
= SubclassID
- InstructionVal
;
64 if (OpCode
== Instruction::Call
|| OpCode
== Instruction::Invoke
||
65 OpCode
== Instruction::CallBr
)
66 assert((VTy
->isFirstClassType() || VTy
->isVoidTy() || VTy
->isStructTy()) &&
67 "invalid CallBase type!");
68 else if (SubclassID
!= BasicBlockVal
&&
69 (/*SubclassID < ConstantFirstVal ||*/ SubclassID
> ConstantLastVal
))
70 assert((VTy
->isFirstClassType() || VTy
->isVoidTy()) &&
71 "Cannot create non-first-class values except for constants!");
72 static_assert(sizeof(Value
) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
77 // Notify all ValueHandles (if present) that this value is going away.
79 ValueHandleBase::ValueIsDeleted(this);
80 if (isUsedByMetadata())
81 ValueAsMetadata::handleDeletion(this);
83 // Remove associated metadata from context.
87 #ifndef NDEBUG // Only in -g mode...
88 // Check to make sure that there are no uses of this value that are still
89 // around when the value is destroyed. If there are, then we have a dangling
90 // reference and something is wrong. This code is here to print out where
91 // the value is still being referenced.
93 // Note that use_empty() cannot be called here, as it eventually downcasts
94 // 'this' to GlobalValue (derived class of Value), but GlobalValue has already
95 // been destructed, so accessing it is UB.
97 if (!materialized_use_empty()) {
98 dbgs() << "While deleting: " << *VTy
<< " %" << getName() << "\n";
99 for (auto *U
: users())
100 dbgs() << "Use still stuck around after Def is destroyed:" << *U
<< "\n";
103 assert(materialized_use_empty() && "Uses remain when a value is destroyed!");
105 // If this value is named, destroy the name. This should not be in a symtab
110 void Value::deleteValue() {
111 switch (getValueID()) {
112 #define HANDLE_VALUE(Name) \
113 case Value::Name##Val: \
114 delete static_cast<Name *>(this); \
116 #define HANDLE_MEMORY_VALUE(Name) \
117 case Value::Name##Val: \
118 static_cast<DerivedUser *>(this)->DeleteValue( \
119 static_cast<DerivedUser *>(this)); \
121 #define HANDLE_CONSTANT(Name) \
122 case Value::Name##Val: \
123 llvm_unreachable("constants should be destroyed with destroyConstant"); \
125 #define HANDLE_INSTRUCTION(Name) /* nothing */
126 #include "llvm/IR/Value.def"
128 #define HANDLE_INST(N, OPC, CLASS) \
129 case Value::InstructionVal + Instruction::OPC: \
130 delete static_cast<CLASS *>(this); \
132 #define HANDLE_USER_INST(N, OPC, CLASS)
133 #include "llvm/IR/Instruction.def"
136 llvm_unreachable("attempting to delete unknown value kind");
140 void Value::destroyValueName() {
141 ValueName
*Name
= getValueName();
143 MallocAllocator Allocator
;
144 Name
->Destroy(Allocator
);
146 setValueName(nullptr);
149 bool Value::hasNUses(unsigned N
) const {
150 return hasNItems(use_begin(), use_end(), N
);
153 bool Value::hasNUsesOrMore(unsigned N
) const {
154 return hasNItemsOrMore(use_begin(), use_end(), N
);
157 bool Value::hasOneUser() const {
162 return std::equal(++user_begin(), user_end(), user_begin());
165 static bool isUnDroppableUser(const User
*U
) { return !U
->isDroppable(); }
167 Use
*Value::getSingleUndroppableUse() {
168 Use
*Result
= nullptr;
169 for (Use
&U
: uses()) {
170 if (!U
.getUser()->isDroppable()) {
179 bool Value::hasNUndroppableUses(unsigned int N
) const {
180 return hasNItems(user_begin(), user_end(), N
, isUnDroppableUser
);
183 bool Value::hasNUndroppableUsesOrMore(unsigned int N
) const {
184 return hasNItemsOrMore(user_begin(), user_end(), N
, isUnDroppableUser
);
187 void Value::dropDroppableUses(
188 llvm::function_ref
<bool(const Use
*)> ShouldDrop
) {
189 SmallVector
<Use
*, 8> ToBeEdited
;
190 for (Use
&U
: uses())
191 if (U
.getUser()->isDroppable() && ShouldDrop(&U
))
192 ToBeEdited
.push_back(&U
);
193 for (Use
*U
: ToBeEdited
)
194 dropDroppableUse(*U
);
197 void Value::dropDroppableUsesIn(User
&Usr
) {
198 assert(Usr
.isDroppable() && "Expected a droppable user!");
199 for (Use
&UsrOp
: Usr
.operands()) {
200 if (UsrOp
.get() == this)
201 dropDroppableUse(UsrOp
);
205 void Value::dropDroppableUse(Use
&U
) {
207 if (auto *Assume
= dyn_cast
<AssumeInst
>(U
.getUser())) {
208 unsigned OpNo
= U
.getOperandNo();
210 U
.set(ConstantInt::getTrue(Assume
->getContext()));
212 U
.set(UndefValue::get(U
.get()->getType()));
213 CallInst::BundleOpInfo
&BOI
= Assume
->getBundleOpInfoForOperand(OpNo
);
214 BOI
.Tag
= Assume
->getContext().pImpl
->getOrInsertBundleTag("ignore");
219 llvm_unreachable("unkown droppable use");
222 bool Value::isUsedInBasicBlock(const BasicBlock
*BB
) const {
223 // This can be computed either by scanning the instructions in BB, or by
224 // scanning the use list of this Value. Both lists can be very long, but
225 // usually one is quite short.
227 // Scan both lists simultaneously until one is exhausted. This limits the
228 // search to the shorter list.
229 BasicBlock::const_iterator BI
= BB
->begin(), BE
= BB
->end();
230 const_user_iterator UI
= user_begin(), UE
= user_end();
231 for (; BI
!= BE
&& UI
!= UE
; ++BI
, ++UI
) {
232 // Scan basic block: Check if this Value is used by the instruction at BI.
233 if (is_contained(BI
->operands(), this))
235 // Scan use list: Check if the use at UI is in BB.
236 const auto *User
= dyn_cast
<Instruction
>(*UI
);
237 if (User
&& User
->getParent() == BB
)
243 unsigned Value::getNumUses() const {
244 return (unsigned)std::distance(use_begin(), use_end());
247 static bool getSymTab(Value
*V
, ValueSymbolTable
*&ST
) {
249 if (Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
250 if (BasicBlock
*P
= I
->getParent())
251 if (Function
*PP
= P
->getParent())
252 ST
= PP
->getValueSymbolTable();
253 } else if (BasicBlock
*BB
= dyn_cast
<BasicBlock
>(V
)) {
254 if (Function
*P
= BB
->getParent())
255 ST
= P
->getValueSymbolTable();
256 } else if (GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
257 if (Module
*P
= GV
->getParent())
258 ST
= &P
->getValueSymbolTable();
259 } else if (Argument
*A
= dyn_cast
<Argument
>(V
)) {
260 if (Function
*P
= A
->getParent())
261 ST
= P
->getValueSymbolTable();
263 assert(isa
<Constant
>(V
) && "Unknown value type!");
264 return true; // no name is setable for this.
269 ValueName
*Value::getValueName() const {
270 if (!HasName
) return nullptr;
272 LLVMContext
&Ctx
= getContext();
273 auto I
= Ctx
.pImpl
->ValueNames
.find(this);
274 assert(I
!= Ctx
.pImpl
->ValueNames
.end() &&
275 "No name entry found!");
280 void Value::setValueName(ValueName
*VN
) {
281 LLVMContext
&Ctx
= getContext();
283 assert(HasName
== Ctx
.pImpl
->ValueNames
.count(this) &&
284 "HasName bit out of sync!");
288 Ctx
.pImpl
->ValueNames
.erase(this);
294 Ctx
.pImpl
->ValueNames
[this] = VN
;
297 StringRef
Value::getName() const {
298 // Make sure the empty string is still a C string. For historical reasons,
299 // some clients want to call .data() on the result and expect it to be null
302 return StringRef("", 0);
303 return getValueName()->getKey();
306 void Value::setNameImpl(const Twine
&NewName
) {
307 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names
308 if (getContext().shouldDiscardValueNames() && !isa
<GlobalValue
>(this))
311 // Fast path for common IRBuilder case of setName("") when there is no name.
312 if (NewName
.isTriviallyEmpty() && !hasName())
315 SmallString
<256> NameData
;
316 StringRef NameRef
= NewName
.toStringRef(NameData
);
317 assert(NameRef
.find_first_of(0) == StringRef::npos
&&
318 "Null bytes are not allowed in names");
320 // Name isn't changing?
321 if (getName() == NameRef
)
324 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
326 // Get the symbol table to update for this object.
327 ValueSymbolTable
*ST
;
328 if (getSymTab(this, ST
))
329 return; // Cannot set a name on this value (e.g. constant).
331 if (!ST
) { // No symbol table to update? Just do the change.
332 if (NameRef
.empty()) {
333 // Free the name for this value.
338 // NOTE: Could optimize for the case the name is shrinking to not deallocate
342 // Create the new name.
343 MallocAllocator Allocator
;
344 setValueName(ValueName::Create(NameRef
, Allocator
));
345 getValueName()->setValue(this);
349 // NOTE: Could optimize for the case the name is shrinking to not deallocate
353 ST
->removeValueName(getValueName());
360 // Name is changing to something new.
361 setValueName(ST
->createValueName(NameRef
, this));
364 void Value::setName(const Twine
&NewName
) {
365 setNameImpl(NewName
);
366 if (Function
*F
= dyn_cast
<Function
>(this))
367 F
->recalculateIntrinsicID();
370 void Value::takeName(Value
*V
) {
371 ValueSymbolTable
*ST
= nullptr;
372 // If this value has a name, drop it.
374 // Get the symtab this is in.
375 if (getSymTab(this, ST
)) {
376 // We can't set a name on this value, but we need to clear V's name if
378 if (V
->hasName()) V
->setName("");
379 return; // Cannot set a name on this value (e.g. constant).
384 ST
->removeValueName(getValueName());
388 // Now we know that this has no name.
390 // If V has no name either, we're done.
391 if (!V
->hasName()) return;
393 // Get this's symtab if we didn't before.
395 if (getSymTab(this, ST
)) {
398 return; // Cannot set a name on this value (e.g. constant).
402 // Get V's ST, this should always succed, because V has a name.
403 ValueSymbolTable
*VST
;
404 bool Failure
= getSymTab(V
, VST
);
405 assert(!Failure
&& "V has a name, so it should have a ST!"); (void)Failure
;
407 // If these values are both in the same symtab, we can do this very fast.
408 // This works even if both values have no symtab yet.
411 setValueName(V
->getValueName());
412 V
->setValueName(nullptr);
413 getValueName()->setValue(this);
417 // Otherwise, things are slightly more complex. Remove V's name from VST and
418 // then reinsert it into ST.
421 VST
->removeValueName(V
->getValueName());
422 setValueName(V
->getValueName());
423 V
->setValueName(nullptr);
424 getValueName()->setValue(this);
427 ST
->reinsertValue(this);
431 std::string
Value::getNameOrAsOperand() const {
432 if (!getName().empty())
433 return std::string(getName());
436 raw_string_ostream
OS(BBName
);
437 printAsOperand(OS
, false);
442 void Value::assertModuleIsMaterializedImpl() const {
444 const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(this);
447 const Module
*M
= GV
->getParent();
450 assert(M
->isMaterialized());
455 static bool contains(SmallPtrSetImpl
<ConstantExpr
*> &Cache
, ConstantExpr
*Expr
,
457 if (!Cache
.insert(Expr
).second
)
460 for (auto &O
: Expr
->operands()) {
463 auto *CE
= dyn_cast
<ConstantExpr
>(O
);
466 if (contains(Cache
, CE
, C
))
472 static bool contains(Value
*Expr
, Value
*V
) {
476 auto *C
= dyn_cast
<Constant
>(V
);
480 auto *CE
= dyn_cast
<ConstantExpr
>(Expr
);
484 SmallPtrSet
<ConstantExpr
*, 4> Cache
;
485 return contains(Cache
, CE
, C
);
489 void Value::doRAUW(Value
*New
, ReplaceMetadataUses ReplaceMetaUses
) {
490 assert(New
&& "Value::replaceAllUsesWith(<null>) is invalid!");
491 assert(!contains(New
, this) &&
492 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
493 assert(New
->getType() == getType() &&
494 "replaceAllUses of value with new value of different type!");
496 // Notify all ValueHandles (if present) that this value is going away.
498 ValueHandleBase::ValueIsRAUWd(this, New
);
499 if (ReplaceMetaUses
== ReplaceMetadataUses::Yes
&& isUsedByMetadata())
500 ValueAsMetadata::handleRAUW(this, New
);
502 while (!materialized_use_empty()) {
504 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
505 // constant because they are uniqued.
506 if (auto *C
= dyn_cast
<Constant
>(U
.getUser())) {
507 if (!isa
<GlobalValue
>(C
)) {
508 C
->handleOperandChange(this, New
);
516 if (BasicBlock
*BB
= dyn_cast
<BasicBlock
>(this))
517 BB
->replaceSuccessorsPhiUsesWith(cast
<BasicBlock
>(New
));
520 void Value::replaceAllUsesWith(Value
*New
) {
521 doRAUW(New
, ReplaceMetadataUses::Yes
);
524 void Value::replaceNonMetadataUsesWith(Value
*New
) {
525 doRAUW(New
, ReplaceMetadataUses::No
);
528 void Value::replaceUsesWithIf(Value
*New
,
529 llvm::function_ref
<bool(Use
&U
)> ShouldReplace
) {
530 assert(New
&& "Value::replaceUsesWithIf(<null>) is invalid!");
531 assert(New
->getType() == getType() &&
532 "replaceUses of value with new value of different type!");
534 SmallVector
<TrackingVH
<Constant
>, 8> Consts
;
535 SmallPtrSet
<Constant
*, 8> Visited
;
537 for (use_iterator UI
= use_begin(), E
= use_end(); UI
!= E
;) {
540 if (!ShouldReplace(U
))
542 // Must handle Constants specially, we cannot call replaceUsesOfWith on a
543 // constant because they are uniqued.
544 if (auto *C
= dyn_cast
<Constant
>(U
.getUser())) {
545 if (!isa
<GlobalValue
>(C
)) {
546 if (Visited
.insert(C
).second
)
547 Consts
.push_back(TrackingVH
<Constant
>(C
));
554 while (!Consts
.empty()) {
555 // FIXME: handleOperandChange() updates all the uses in a given Constant,
556 // not just the one passed to ShouldReplace
557 Consts
.pop_back_val()->handleOperandChange(this, New
);
561 /// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
563 static void replaceDbgUsesOutsideBlock(Value
*V
, Value
*New
, BasicBlock
*BB
) {
564 SmallVector
<DbgVariableIntrinsic
*> DbgUsers
;
565 findDbgUsers(DbgUsers
, V
);
566 for (auto *DVI
: DbgUsers
) {
567 if (DVI
->getParent() != BB
)
568 DVI
->replaceVariableLocationOp(V
, New
);
572 // Like replaceAllUsesWith except it does not handle constants or basic blocks.
573 // This routine leaves uses within BB.
574 void Value::replaceUsesOutsideBlock(Value
*New
, BasicBlock
*BB
) {
575 assert(New
&& "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
576 assert(!contains(New
, this) &&
577 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
578 assert(New
->getType() == getType() &&
579 "replaceUses of value with new value of different type!");
580 assert(BB
&& "Basic block that may contain a use of 'New' must be defined\n");
582 replaceDbgUsesOutsideBlock(this, New
, BB
);
583 replaceUsesWithIf(New
, [BB
](Use
&U
) {
584 auto *I
= dyn_cast
<Instruction
>(U
.getUser());
585 // Don't replace if it's an instruction in the BB basic block.
586 return !I
|| I
->getParent() != BB
;
591 // Various metrics for how much to strip off of pointers.
592 enum PointerStripKind
{
594 PSK_ZeroIndicesAndAliases
,
595 PSK_ZeroIndicesSameRepresentation
,
596 PSK_ForAliasAnalysis
,
597 PSK_InBoundsConstantIndices
,
601 template <PointerStripKind StripKind
> static void NoopCallback(const Value
*) {}
603 template <PointerStripKind StripKind
>
604 static const Value
*stripPointerCastsAndOffsets(
606 function_ref
<void(const Value
*)> Func
= NoopCallback
<StripKind
>) {
607 if (!V
->getType()->isPointerTy())
610 // Even though we don't look through PHI nodes, we could be called on an
611 // instruction in an unreachable block, which may be on a cycle.
612 SmallPtrSet
<const Value
*, 4> Visited
;
617 if (auto *GEP
= dyn_cast
<GEPOperator
>(V
)) {
619 case PSK_ZeroIndices
:
620 case PSK_ZeroIndicesAndAliases
:
621 case PSK_ZeroIndicesSameRepresentation
:
622 case PSK_ForAliasAnalysis
:
623 if (!GEP
->hasAllZeroIndices())
626 case PSK_InBoundsConstantIndices
:
627 if (!GEP
->hasAllConstantIndices())
631 if (!GEP
->isInBounds())
635 V
= GEP
->getPointerOperand();
636 } else if (Operator::getOpcode(V
) == Instruction::BitCast
) {
637 V
= cast
<Operator
>(V
)->getOperand(0);
638 if (!V
->getType()->isPointerTy())
640 } else if (StripKind
!= PSK_ZeroIndicesSameRepresentation
&&
641 Operator::getOpcode(V
) == Instruction::AddrSpaceCast
) {
642 // TODO: If we know an address space cast will not change the
643 // representation we could look through it here as well.
644 V
= cast
<Operator
>(V
)->getOperand(0);
645 } else if (StripKind
== PSK_ZeroIndicesAndAliases
&& isa
<GlobalAlias
>(V
)) {
646 V
= cast
<GlobalAlias
>(V
)->getAliasee();
647 } else if (StripKind
== PSK_ForAliasAnalysis
&& isa
<PHINode
>(V
) &&
648 cast
<PHINode
>(V
)->getNumIncomingValues() == 1) {
649 V
= cast
<PHINode
>(V
)->getIncomingValue(0);
651 if (const auto *Call
= dyn_cast
<CallBase
>(V
)) {
652 if (const Value
*RV
= Call
->getReturnedArgOperand()) {
656 // The result of launder.invariant.group must alias it's argument,
657 // but it can't be marked with returned attribute, that's why it needs
659 if (StripKind
== PSK_ForAliasAnalysis
&&
660 (Call
->getIntrinsicID() == Intrinsic::launder_invariant_group
||
661 Call
->getIntrinsicID() == Intrinsic::strip_invariant_group
)) {
662 V
= Call
->getArgOperand(0);
668 assert(V
->getType()->isPointerTy() && "Unexpected operand type!");
669 } while (Visited
.insert(V
).second
);
673 } // end anonymous namespace
675 const Value
*Value::stripPointerCasts() const {
676 return stripPointerCastsAndOffsets
<PSK_ZeroIndices
>(this);
679 const Value
*Value::stripPointerCastsAndAliases() const {
680 return stripPointerCastsAndOffsets
<PSK_ZeroIndicesAndAliases
>(this);
683 const Value
*Value::stripPointerCastsSameRepresentation() const {
684 return stripPointerCastsAndOffsets
<PSK_ZeroIndicesSameRepresentation
>(this);
687 const Value
*Value::stripInBoundsConstantOffsets() const {
688 return stripPointerCastsAndOffsets
<PSK_InBoundsConstantIndices
>(this);
691 const Value
*Value::stripPointerCastsForAliasAnalysis() const {
692 return stripPointerCastsAndOffsets
<PSK_ForAliasAnalysis
>(this);
695 const Value
*Value::stripAndAccumulateConstantOffsets(
696 const DataLayout
&DL
, APInt
&Offset
, bool AllowNonInbounds
,
697 function_ref
<bool(Value
&, APInt
&)> ExternalAnalysis
) const {
698 if (!getType()->isPtrOrPtrVectorTy())
701 unsigned BitWidth
= Offset
.getBitWidth();
702 assert(BitWidth
== DL
.getIndexTypeSizeInBits(getType()) &&
703 "The offset bit width does not match the DL specification.");
705 // Even though we don't look through PHI nodes, we could be called on an
706 // instruction in an unreachable block, which may be on a cycle.
707 SmallPtrSet
<const Value
*, 4> Visited
;
708 Visited
.insert(this);
709 const Value
*V
= this;
711 if (auto *GEP
= dyn_cast
<GEPOperator
>(V
)) {
712 // If in-bounds was requested, we do not strip non-in-bounds GEPs.
713 if (!AllowNonInbounds
&& !GEP
->isInBounds())
716 // If one of the values we have visited is an addrspacecast, then
717 // the pointer type of this GEP may be different from the type
718 // of the Ptr parameter which was passed to this function. This
719 // means when we construct GEPOffset, we need to use the size
720 // of GEP's pointer type rather than the size of the original
722 APInt
GEPOffset(DL
.getIndexTypeSizeInBits(V
->getType()), 0);
723 if (!GEP
->accumulateConstantOffset(DL
, GEPOffset
, ExternalAnalysis
))
726 // Stop traversal if the pointer offset wouldn't fit in the bit-width
727 // provided by the Offset argument. This can happen due to AddrSpaceCast
729 if (GEPOffset
.getMinSignedBits() > BitWidth
)
732 // External Analysis can return a result higher/lower than the value
733 // represents. We need to detect overflow/underflow.
734 APInt GEPOffsetST
= GEPOffset
.sextOrTrunc(BitWidth
);
735 if (!ExternalAnalysis
) {
736 Offset
+= GEPOffsetST
;
738 bool Overflow
= false;
739 APInt OldOffset
= Offset
;
740 Offset
= Offset
.sadd_ov(GEPOffsetST
, Overflow
);
746 V
= GEP
->getPointerOperand();
747 } else if (Operator::getOpcode(V
) == Instruction::BitCast
||
748 Operator::getOpcode(V
) == Instruction::AddrSpaceCast
) {
749 V
= cast
<Operator
>(V
)->getOperand(0);
750 } else if (auto *GA
= dyn_cast
<GlobalAlias
>(V
)) {
751 if (!GA
->isInterposable())
752 V
= GA
->getAliasee();
753 } else if (const auto *Call
= dyn_cast
<CallBase
>(V
)) {
754 if (const Value
*RV
= Call
->getReturnedArgOperand())
757 assert(V
->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
758 } while (Visited
.insert(V
).second
);
764 Value::stripInBoundsOffsets(function_ref
<void(const Value
*)> Func
) const {
765 return stripPointerCastsAndOffsets
<PSK_InBounds
>(this, Func
);
768 bool Value::canBeFreed() const {
769 assert(getType()->isPointerTy());
771 // Cases that can simply never be deallocated
772 // *) Constants aren't allocated per se, thus not deallocated either.
773 if (isa
<Constant
>(this))
776 // Handle byval/byref/sret/inalloca/preallocated arguments. The storage
777 // lifetime is guaranteed to be longer than the callee's lifetime.
778 if (auto *A
= dyn_cast
<Argument
>(this)) {
779 if (A
->hasPointeeInMemoryValueAttr())
781 // A pointer to an object in a function which neither frees, nor can arrange
782 // for another thread to free on its behalf, can not be freed in the scope
783 // of the function. Note that this logic is restricted to memory
784 // allocations in existance before the call; a nofree function *is* allowed
785 // to free memory it allocated.
786 const Function
*F
= A
->getParent();
787 if (F
->doesNotFreeMemory() && F
->hasNoSync())
791 const Function
*F
= nullptr;
792 if (auto *I
= dyn_cast
<Instruction
>(this))
793 F
= I
->getFunction();
794 if (auto *A
= dyn_cast
<Argument
>(this))
800 // With garbage collection, deallocation typically occurs solely at or after
801 // safepoints. If we're compiling for a collector which uses the
802 // gc.statepoint infrastructure, safepoints aren't explicitly present
803 // in the IR until after lowering from abstract to physical machine model.
804 // The collector could chose to mix explicit deallocation and gc'd objects
805 // which is why we need the explicit opt in on a per collector basis.
809 const auto &GCName
= F
->getGC();
810 if (GCName
== "statepoint-example") {
811 auto *PT
= cast
<PointerType
>(this->getType());
812 if (PT
->getAddressSpace() != 1)
813 // For the sake of this example GC, we arbitrarily pick addrspace(1) as
814 // our GC managed heap. This must match the same check in
815 // RewriteStatepointsForGC (and probably needs better factored.)
818 // It is cheaper to scan for a declaration than to scan for a use in this
819 // function. Note that gc.statepoint is a type overloaded function so the
820 // usual trick of requesting declaration of the intrinsic from the module
822 for (auto &Fn
: *F
->getParent())
823 if (Fn
.getIntrinsicID() == Intrinsic::experimental_gc_statepoint
)
830 uint64_t Value::getPointerDereferenceableBytes(const DataLayout
&DL
,
832 bool &CanBeFreed
) const {
833 assert(getType()->isPointerTy() && "must be pointer");
835 uint64_t DerefBytes
= 0;
837 CanBeFreed
= UseDerefAtPointSemantics
&& canBeFreed();
838 if (const Argument
*A
= dyn_cast
<Argument
>(this)) {
839 DerefBytes
= A
->getDereferenceableBytes();
840 if (DerefBytes
== 0) {
841 // Handle byval/byref/inalloca/preallocated arguments
842 if (Type
*ArgMemTy
= A
->getPointeeInMemoryValueType()) {
843 if (ArgMemTy
->isSized()) {
844 // FIXME: Why isn't this the type alloc size?
845 DerefBytes
= DL
.getTypeStoreSize(ArgMemTy
).getKnownMinSize();
850 if (DerefBytes
== 0) {
851 DerefBytes
= A
->getDereferenceableOrNullBytes();
854 } else if (const auto *Call
= dyn_cast
<CallBase
>(this)) {
855 DerefBytes
= Call
->getRetDereferenceableBytes();
856 if (DerefBytes
== 0) {
857 DerefBytes
= Call
->getRetDereferenceableOrNullBytes();
860 } else if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(this)) {
861 if (MDNode
*MD
= LI
->getMetadata(LLVMContext::MD_dereferenceable
)) {
862 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(0));
863 DerefBytes
= CI
->getLimitedValue();
865 if (DerefBytes
== 0) {
867 LI
->getMetadata(LLVMContext::MD_dereferenceable_or_null
)) {
868 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(0));
869 DerefBytes
= CI
->getLimitedValue();
873 } else if (auto *IP
= dyn_cast
<IntToPtrInst
>(this)) {
874 if (MDNode
*MD
= IP
->getMetadata(LLVMContext::MD_dereferenceable
)) {
875 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(0));
876 DerefBytes
= CI
->getLimitedValue();
878 if (DerefBytes
== 0) {
880 IP
->getMetadata(LLVMContext::MD_dereferenceable_or_null
)) {
881 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(0));
882 DerefBytes
= CI
->getLimitedValue();
886 } else if (auto *AI
= dyn_cast
<AllocaInst
>(this)) {
887 if (!AI
->isArrayAllocation()) {
889 DL
.getTypeStoreSize(AI
->getAllocatedType()).getKnownMinSize();
893 } else if (auto *GV
= dyn_cast
<GlobalVariable
>(this)) {
894 if (GV
->getValueType()->isSized() && !GV
->hasExternalWeakLinkage()) {
895 // TODO: Don't outright reject hasExternalWeakLinkage but set the
897 DerefBytes
= DL
.getTypeStoreSize(GV
->getValueType()).getFixedSize();
905 Align
Value::getPointerAlignment(const DataLayout
&DL
) const {
906 assert(getType()->isPointerTy() && "must be pointer");
907 if (auto *GO
= dyn_cast
<GlobalObject
>(this)) {
908 if (isa
<Function
>(GO
)) {
909 Align FunctionPtrAlign
= DL
.getFunctionPtrAlign().valueOrOne();
910 switch (DL
.getFunctionPtrAlignType()) {
911 case DataLayout::FunctionPtrAlignType::Independent
:
912 return FunctionPtrAlign
;
913 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign
:
914 return std::max(FunctionPtrAlign
, GO
->getAlign().valueOrOne());
916 llvm_unreachable("Unhandled FunctionPtrAlignType");
918 const MaybeAlign
Alignment(GO
->getAlignment());
920 if (auto *GVar
= dyn_cast
<GlobalVariable
>(GO
)) {
921 Type
*ObjectType
= GVar
->getValueType();
922 if (ObjectType
->isSized()) {
923 // If the object is defined in the current Module, we'll be giving
924 // it the preferred alignment. Otherwise, we have to assume that it
925 // may only have the minimum ABI alignment.
926 if (GVar
->isStrongDefinitionForLinker())
927 return DL
.getPreferredAlign(GVar
);
929 return DL
.getABITypeAlign(ObjectType
);
933 return Alignment
.valueOrOne();
934 } else if (const Argument
*A
= dyn_cast
<Argument
>(this)) {
935 const MaybeAlign Alignment
= A
->getParamAlign();
936 if (!Alignment
&& A
->hasStructRetAttr()) {
937 // An sret parameter has at least the ABI alignment of the return type.
938 Type
*EltTy
= A
->getParamStructRetType();
939 if (EltTy
->isSized())
940 return DL
.getABITypeAlign(EltTy
);
942 return Alignment
.valueOrOne();
943 } else if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(this)) {
944 return AI
->getAlign();
945 } else if (const auto *Call
= dyn_cast
<CallBase
>(this)) {
946 MaybeAlign Alignment
= Call
->getRetAlign();
947 if (!Alignment
&& Call
->getCalledFunction())
948 Alignment
= Call
->getCalledFunction()->getAttributes().getRetAlignment();
949 return Alignment
.valueOrOne();
950 } else if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(this)) {
951 if (MDNode
*MD
= LI
->getMetadata(LLVMContext::MD_align
)) {
952 ConstantInt
*CI
= mdconst::extract
<ConstantInt
>(MD
->getOperand(0));
953 return Align(CI
->getLimitedValue());
955 } else if (auto *CstPtr
= dyn_cast
<Constant
>(this)) {
956 if (auto *CstInt
= dyn_cast_or_null
<ConstantInt
>(ConstantExpr::getPtrToInt(
957 const_cast<Constant
*>(CstPtr
), DL
.getIntPtrType(getType()),
958 /*OnlyIfReduced=*/true))) {
959 size_t TrailingZeros
= CstInt
->getValue().countTrailingZeros();
960 // While the actual alignment may be large, elsewhere we have
961 // an arbitrary upper alignmet limit, so let's clamp to it.
962 return Align(TrailingZeros
< Value::MaxAlignmentExponent
963 ? uint64_t(1) << TrailingZeros
964 : Value::MaximumAlignment
);
970 const Value
*Value::DoPHITranslation(const BasicBlock
*CurBB
,
971 const BasicBlock
*PredBB
) const {
972 auto *PN
= dyn_cast
<PHINode
>(this);
973 if (PN
&& PN
->getParent() == CurBB
)
974 return PN
->getIncomingValueForBlock(PredBB
);
978 LLVMContext
&Value::getContext() const { return VTy
->getContext(); }
980 void Value::reverseUseList() {
981 if (!UseList
|| !UseList
->Next
)
982 // No need to reverse 0 or 1 uses.
986 Use
*Current
= UseList
->Next
;
987 Head
->Next
= nullptr;
989 Use
*Next
= Current
->Next
;
990 Current
->Next
= Head
;
991 Head
->Prev
= &Current
->Next
;
996 Head
->Prev
= &UseList
;
999 bool Value::isSwiftError() const {
1000 auto *Arg
= dyn_cast
<Argument
>(this);
1002 return Arg
->hasSwiftErrorAttr();
1003 auto *Alloca
= dyn_cast
<AllocaInst
>(this);
1006 return Alloca
->isSwiftError();
1009 bool Value::isTransitiveUsedByMetadataOnly() const {
1012 llvm::SmallVector
<const User
*, 32> WorkList
;
1013 llvm::SmallPtrSet
<const User
*, 32> Visited
;
1014 WorkList
.insert(WorkList
.begin(), user_begin(), user_end());
1015 while (!WorkList
.empty()) {
1016 const User
*U
= WorkList
.back();
1017 WorkList
.pop_back();
1019 // If it is transitively used by a global value or a non-constant value,
1020 // it's obviously not only used by metadata.
1021 if (!isa
<Constant
>(U
) || isa
<GlobalValue
>(U
))
1023 for (const User
*UU
: U
->users())
1024 if (!Visited
.count(UU
))
1025 WorkList
.push_back(UU
);
1030 //===----------------------------------------------------------------------===//
1031 // ValueHandleBase Class
1032 //===----------------------------------------------------------------------===//
1034 void ValueHandleBase::AddToExistingUseList(ValueHandleBase
**List
) {
1035 assert(List
&& "Handle list is null?");
1037 // Splice ourselves into the list.
1042 Next
->setPrevPtr(&Next
);
1043 assert(getValPtr() == Next
->getValPtr() && "Added to wrong list?");
1047 void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase
*List
) {
1048 assert(List
&& "Must insert after existing node");
1051 setPrevPtr(&List
->Next
);
1054 Next
->setPrevPtr(&Next
);
1057 void ValueHandleBase::AddToUseList() {
1058 assert(getValPtr() && "Null pointer doesn't have a use list!");
1060 LLVMContextImpl
*pImpl
= getValPtr()->getContext().pImpl
;
1062 if (getValPtr()->HasValueHandle
) {
1063 // If this value already has a ValueHandle, then it must be in the
1064 // ValueHandles map already.
1065 ValueHandleBase
*&Entry
= pImpl
->ValueHandles
[getValPtr()];
1066 assert(Entry
&& "Value doesn't have any handles?");
1067 AddToExistingUseList(&Entry
);
1071 // Ok, it doesn't have any handles yet, so we must insert it into the
1072 // DenseMap. However, doing this insertion could cause the DenseMap to
1073 // reallocate itself, which would invalidate all of the PrevP pointers that
1074 // point into the old table. Handle this by checking for reallocation and
1075 // updating the stale pointers only if needed.
1076 DenseMap
<Value
*, ValueHandleBase
*> &Handles
= pImpl
->ValueHandles
;
1077 const void *OldBucketPtr
= Handles
.getPointerIntoBucketsArray();
1079 ValueHandleBase
*&Entry
= Handles
[getValPtr()];
1080 assert(!Entry
&& "Value really did already have handles?");
1081 AddToExistingUseList(&Entry
);
1082 getValPtr()->HasValueHandle
= true;
1084 // If reallocation didn't happen or if this was the first insertion, don't
1086 if (Handles
.isPointerIntoBucketsArray(OldBucketPtr
) ||
1087 Handles
.size() == 1) {
1091 // Okay, reallocation did happen. Fix the Prev Pointers.
1092 for (DenseMap
<Value
*, ValueHandleBase
*>::iterator I
= Handles
.begin(),
1093 E
= Handles
.end(); I
!= E
; ++I
) {
1094 assert(I
->second
&& I
->first
== I
->second
->getValPtr() &&
1095 "List invariant broken!");
1096 I
->second
->setPrevPtr(&I
->second
);
1100 void ValueHandleBase::RemoveFromUseList() {
1101 assert(getValPtr() && getValPtr()->HasValueHandle
&&
1102 "Pointer doesn't have a use list!");
1104 // Unlink this from its use list.
1105 ValueHandleBase
**PrevPtr
= getPrevPtr();
1106 assert(*PrevPtr
== this && "List invariant broken");
1110 assert(Next
->getPrevPtr() == &Next
&& "List invariant broken");
1111 Next
->setPrevPtr(PrevPtr
);
1115 // If the Next pointer was null, then it is possible that this was the last
1116 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
1118 LLVMContextImpl
*pImpl
= getValPtr()->getContext().pImpl
;
1119 DenseMap
<Value
*, ValueHandleBase
*> &Handles
= pImpl
->ValueHandles
;
1120 if (Handles
.isPointerIntoBucketsArray(PrevPtr
)) {
1121 Handles
.erase(getValPtr());
1122 getValPtr()->HasValueHandle
= false;
1126 void ValueHandleBase::ValueIsDeleted(Value
*V
) {
1127 assert(V
->HasValueHandle
&& "Should only be called if ValueHandles present");
1129 // Get the linked list base, which is guaranteed to exist since the
1130 // HasValueHandle flag is set.
1131 LLVMContextImpl
*pImpl
= V
->getContext().pImpl
;
1132 ValueHandleBase
*Entry
= pImpl
->ValueHandles
[V
];
1133 assert(Entry
&& "Value bit set but no entries exist");
1135 // We use a local ValueHandleBase as an iterator so that ValueHandles can add
1136 // and remove themselves from the list without breaking our iteration. This
1137 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.
1138 // Note that we deliberately do not the support the case when dropping a value
1139 // handle results in a new value handle being permanently added to the list
1140 // (as might occur in theory for CallbackVH's): the new value handle will not
1141 // be processed and the checking code will mete out righteous punishment if
1142 // the handle is still present once we have finished processing all the other
1143 // value handles (it is fine to momentarily add then remove a value handle).
1144 for (ValueHandleBase
Iterator(Assert
, *Entry
); Entry
; Entry
= Iterator
.Next
) {
1145 Iterator
.RemoveFromUseList();
1146 Iterator
.AddToExistingUseListAfter(Entry
);
1147 assert(Entry
->Next
== &Iterator
&& "Loop invariant broken.");
1149 switch (Entry
->getKind()) {
1154 // WeakTracking and Weak just go to null, which unlinks them
1156 Entry
->operator=(nullptr);
1159 // Forward to the subclass's implementation.
1160 static_cast<CallbackVH
*>(Entry
)->deleted();
1165 // All callbacks, weak references, and assertingVHs should be dropped by now.
1166 if (V
->HasValueHandle
) {
1167 #ifndef NDEBUG // Only in +Asserts mode...
1168 dbgs() << "While deleting: " << *V
->getType() << " %" << V
->getName()
1170 if (pImpl
->ValueHandles
[V
]->getKind() == Assert
)
1171 llvm_unreachable("An asserting value handle still pointed to this"
1175 llvm_unreachable("All references to V were not removed?");
1179 void ValueHandleBase::ValueIsRAUWd(Value
*Old
, Value
*New
) {
1180 assert(Old
->HasValueHandle
&&"Should only be called if ValueHandles present");
1181 assert(Old
!= New
&& "Changing value into itself!");
1182 assert(Old
->getType() == New
->getType() &&
1183 "replaceAllUses of value with new value of different type!");
1185 // Get the linked list base, which is guaranteed to exist since the
1186 // HasValueHandle flag is set.
1187 LLVMContextImpl
*pImpl
= Old
->getContext().pImpl
;
1188 ValueHandleBase
*Entry
= pImpl
->ValueHandles
[Old
];
1190 assert(Entry
&& "Value bit set but no entries exist");
1192 // We use a local ValueHandleBase as an iterator so that
1193 // ValueHandles can add and remove themselves from the list without
1194 // breaking our iteration. This is not really an AssertingVH; we
1195 // just have to give ValueHandleBase some kind.
1196 for (ValueHandleBase
Iterator(Assert
, *Entry
); Entry
; Entry
= Iterator
.Next
) {
1197 Iterator
.RemoveFromUseList();
1198 Iterator
.AddToExistingUseListAfter(Entry
);
1199 assert(Entry
->Next
== &Iterator
&& "Loop invariant broken.");
1201 switch (Entry
->getKind()) {
1204 // Asserting and Weak handles do not follow RAUW implicitly.
1207 // Weak goes to the new value, which will unlink it from Old's list.
1208 Entry
->operator=(New
);
1211 // Forward to the subclass's implementation.
1212 static_cast<CallbackVH
*>(Entry
)->allUsesReplacedWith(New
);
1218 // If any new weak value handles were added while processing the
1219 // list, then complain about it now.
1220 if (Old
->HasValueHandle
)
1221 for (Entry
= pImpl
->ValueHandles
[Old
]; Entry
; Entry
= Entry
->Next
)
1222 switch (Entry
->getKind()) {
1224 dbgs() << "After RAUW from " << *Old
->getType() << " %"
1225 << Old
->getName() << " to " << *New
->getType() << " %"
1226 << New
->getName() << "\n";
1228 "A weak tracking value handle still pointed to the old value!\n");
1235 // Pin the vtable to this file.
1236 void CallbackVH::anchor() {}