@@ -2759,27 +2759,33 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2759
2759
auto *LI = cast<LoadInst>(I);
2760
2760
// A Load tagged with nonnull or dereferenceable with null pointer undefined
2761
2761
// is never null.
2762
- if (auto *PtrT = dyn_cast<PointerType>(I->getType ()))
2762
+ if (auto *PtrT = dyn_cast<PointerType>(I->getType ())) {
2763
2763
if (Q.IIQ .getMetadata (LI, LLVMContext::MD_nonnull) ||
2764
2764
(Q.IIQ .getMetadata (LI, LLVMContext::MD_dereferenceable) &&
2765
2765
!NullPointerIsDefined (LI->getFunction (), PtrT->getAddressSpace ())))
2766
2766
return true ;
2767
+ } else if (MDNode *Ranges = Q.IIQ .getMetadata (LI, LLVMContext::MD_range)) {
2768
+ return rangeMetadataExcludesValue (Ranges, APInt::getZero (BitWidth));
2769
+ }
2767
2770
2768
2771
// No need to fall through to computeKnownBits as range metadata is already
2769
2772
// handled in isKnownNonZero.
2770
2773
return false ;
2771
2774
}
2772
2775
case Instruction::Call:
2773
- case Instruction::Invoke:
2776
+ case Instruction::Invoke: {
2777
+ const auto *Call = cast<CallBase>(I);
2774
2778
if (I->getType ()->isPointerTy ()) {
2775
- const auto *Call = cast<CallBase>(I);
2776
2779
if (Call->isReturnNonNull ())
2777
2780
return true ;
2778
2781
if (const auto *RP = getArgumentAliasingToReturnedPointer (Call, true ))
2779
2782
return isKnownNonZero (RP, Depth, Q);
2780
- } else if (const Value *RV = cast<CallBase>(I)->getReturnedArgOperand ()) {
2781
- if (RV->getType () == I->getType () && isKnownNonZero (RV, Depth, Q))
2782
- return true ;
2783
+ } else {
2784
+ if (MDNode *Ranges = Q.IIQ .getMetadata (Call, LLVMContext::MD_range))
2785
+ return rangeMetadataExcludesValue (Ranges, APInt::getZero (BitWidth));
2786
+ if (const Value *RV = Call->getReturnedArgOperand ())
2787
+ if (RV->getType () == I->getType () && isKnownNonZero (RV, Depth, Q))
2788
+ return true ;
2783
2789
}
2784
2790
2785
2791
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
@@ -2849,6 +2855,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
2849
2855
2850
2856
return false ;
2851
2857
}
2858
+ }
2852
2859
2853
2860
KnownBits Known (BitWidth);
2854
2861
computeKnownBits (I, DemandedElts, Known, Depth, Q);
@@ -2914,17 +2921,6 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
2914
2921
return false ;
2915
2922
}
2916
2923
2917
- if (auto *I = dyn_cast<Instruction>(V)) {
2918
- if (MDNode *Ranges = Q.IIQ .getMetadata (I, LLVMContext::MD_range)) {
2919
- // If the possible ranges don't contain zero, then the value is
2920
- // definitely non-zero.
2921
- assert (Ty->isIntOrIntVectorTy () && " Range on non-integer?" );
2922
- const APInt ZeroValue (Ty->getScalarSizeInBits (), 0 );
2923
- if (rangeMetadataExcludesValue (Ranges, ZeroValue))
2924
- return true ;
2925
- }
2926
- }
2927
-
2928
2924
if (!isa<Constant>(V) && isKnownNonZeroFromAssume (V, Q))
2929
2925
return true ;
2930
2926
0 commit comments