Skip to content

Commit

Permalink
[ValueTracking] Move MD_range handling to isKnownNonZeroFromOperator()
Browse files Browse the repository at this point in the history
All the isKnownNonZero() handling for instructions should be inside
this function. This makes the structure more similar to
computeKnownBitsFromOperator() as well.

This may not be entirely NFC due to different depth handling.
  • Loading branch information
nikic committed Mar 19, 2024
1 parent a1fb514 commit 2cc75ae
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2759,27 +2759,33 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
auto *LI = cast<LoadInst>(I);
// A Load tagged with nonnull or dereferenceable with null pointer undefined
// is never null.
if (auto *PtrT = dyn_cast<PointerType>(I->getType()))
if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
(Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
!NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
return true;
} else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
return rangeMetadataExcludesValue(Ranges, APInt::getZero(BitWidth));
}

// No need to fall through to computeKnownBits as range metadata is already
// handled in isKnownNonZero.
return false;
}
case Instruction::Call:
case Instruction::Invoke:
case Instruction::Invoke: {
const auto *Call = cast<CallBase>(I);
if (I->getType()->isPointerTy()) {
const auto *Call = cast<CallBase>(I);
if (Call->isReturnNonNull())
return true;
if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
return isKnownNonZero(RP, Depth, Q);
} else if (const Value *RV = cast<CallBase>(I)->getReturnedArgOperand()) {
if (RV->getType() == I->getType() && isKnownNonZero(RV, Depth, Q))
return true;
} else {
if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
return rangeMetadataExcludesValue(Ranges, APInt::getZero(BitWidth));
if (const Value *RV = Call->getReturnedArgOperand())
if (RV->getType() == I->getType() && isKnownNonZero(RV, Depth, Q))
return true;
}

if (auto *II = dyn_cast<IntrinsicInst>(I)) {
Expand Down Expand Up @@ -2849,6 +2855,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,

return false;
}
}

KnownBits Known(BitWidth);
computeKnownBits(I, DemandedElts, Known, Depth, Q);
Expand Down Expand Up @@ -2914,17 +2921,6 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
return false;
}

if (auto *I = dyn_cast<Instruction>(V)) {
if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
// If the possible ranges don't contain zero, then the value is
// definitely non-zero.
assert(Ty->isIntOrIntVectorTy() && "Range on non-integer?");
const APInt ZeroValue(Ty->getScalarSizeInBits(), 0);
if (rangeMetadataExcludesValue(Ranges, ZeroValue))
return true;
}
}

if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
return true;

Expand Down

0 comments on commit 2cc75ae

Please sign in to comment.