@@ -2759,27 +2759,33 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
27592759 auto *LI = cast<LoadInst>(I);
27602760 // A Load tagged with nonnull or dereferenceable with null pointer undefined
27612761 // is never null.
2762- if (auto *PtrT = dyn_cast<PointerType>(I->getType ()))
2762+ if (auto *PtrT = dyn_cast<PointerType>(I->getType ())) {
27632763 if (Q.IIQ .getMetadata (LI, LLVMContext::MD_nonnull) ||
27642764 (Q.IIQ .getMetadata (LI, LLVMContext::MD_dereferenceable) &&
27652765 !NullPointerIsDefined (LI->getFunction (), PtrT->getAddressSpace ())))
27662766 return true ;
2767+ } else if (MDNode *Ranges = Q.IIQ .getMetadata (LI, LLVMContext::MD_range)) {
2768+ return rangeMetadataExcludesValue (Ranges, APInt::getZero (BitWidth));
2769+ }
27672770
27682771 // No need to fall through to computeKnownBits as range metadata is already
27692772 // handled in isKnownNonZero.
27702773 return false ;
27712774 }
27722775 case Instruction::Call:
2773- case Instruction::Invoke:
2776+ case Instruction::Invoke: {
2777+ const auto *Call = cast<CallBase>(I);
27742778 if (I->getType ()->isPointerTy ()) {
2775- const auto *Call = cast<CallBase>(I);
27762779 if (Call->isReturnNonNull ())
27772780 return true ;
27782781 if (const auto *RP = getArgumentAliasingToReturnedPointer (Call, true ))
27792782 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 ;
27832789 }
27842790
27852791 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
@@ -2849,6 +2855,7 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
28492855
28502856 return false ;
28512857 }
2858+ }
28522859
28532860 KnownBits Known (BitWidth);
28542861 computeKnownBits (I, DemandedElts, Known, Depth, Q);
@@ -2914,17 +2921,6 @@ bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
29142921 return false ;
29152922 }
29162923
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-
29282924 if (!isa<Constant>(V) && isKnownNonZeroFromAssume (V, Q))
29292925 return true ;
29302926
0 commit comments