Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -1151,6 +1151,25 @@ Query(DL, AC, safeCxtI(I, CxtI), DT, UseInstrInfo, ORE)); } +static ConstantRange getVScaleRange(const Function *F, unsigned BitWidth) { + Attribute Attr = F->getFnAttribute(Attribute::VScaleRange); + // Without vscale_range, we only know that vscale is non-zero. + if (!Attr.isValid()) + return ConstantRange(APInt(BitWidth, 1), APInt::getZero(BitWidth)); + + unsigned AttrMin = Attr.getVScaleRangeMin(); + // Minimum is larger than vscale width, result is always poison. + if ((unsigned)llvm::bit_width(AttrMin) > BitWidth) + return ConstantRange::getEmpty(BitWidth); + + APInt Min(BitWidth, AttrMin); + std::optional AttrMax = Attr.getVScaleRangeMax(); + if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth) + return ConstantRange(Min, APInt::getZero(BitWidth)); + + return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1); +} + static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, @@ -1819,31 +1838,10 @@ Known.Zero.setBitsFrom(17); break; case Intrinsic::vscale: { - if (!II->getParent() || !II->getFunction() || - !II->getFunction()->hasFnAttribute(Attribute::VScaleRange)) - break; - - auto Attr = II->getFunction()->getFnAttribute(Attribute::VScaleRange); - std::optional VScaleMax = Attr.getVScaleRangeMax(); - - if (!VScaleMax) + if (!II->getParent() || !II->getFunction()) break; - unsigned VScaleMin = Attr.getVScaleRangeMin(); - - // If vscale min = max then we know the exact value at compile time - // and hence we know the exact bits. - if (VScaleMin == VScaleMax) { - Known.One = VScaleMin; - Known.Zero = VScaleMin; - Known.Zero.flipAllBits(); - break; - } - - unsigned FirstZeroHighBit = llvm::bit_width(*VScaleMax); - if (FirstZeroHighBit < BitWidth) - Known.Zero.setBitsFrom(FirstZeroHighBit); - + Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits(); break; } } @@ -7452,6 +7450,14 @@ else Upper = APInt::getSignedMinValue(Width) + 1; break; + case Intrinsic::vscale: { + if (!II.getParent() || !II.getFunction()) + break; + ConstantRange CR = getVScaleRange(II.getFunction(), Width); + Lower = CR.getLower(); + Upper = CR.getUpper(); + break; + } default: break; } Index: llvm/test/Transforms/InstCombine/icmp-vscale.ll =================================================================== --- llvm/test/Transforms/InstCombine/icmp-vscale.ll +++ llvm/test/Transforms/InstCombine/icmp-vscale.ll @@ -84,9 +84,7 @@ define i1 @vscale_ule_max() vscale_range(5,10) { ; CHECK-LABEL: @vscale_ule_max( -; CHECK-NEXT: [[VSCALE:%.*]] = call i16 @llvm.vscale.i16() -; CHECK-NEXT: [[RES:%.*]] = icmp ult i16 [[VSCALE]], 11 -; CHECK-NEXT: ret i1 [[RES]] +; CHECK-NEXT: ret i1 true ; %vscale = call i16 @llvm.vscale.i16() %res = icmp ule i16 %vscale, 10 @@ -106,9 +104,7 @@ define i1 @vscale_uge_min() vscale_range(5,10) { ; CHECK-LABEL: @vscale_uge_min( -; CHECK-NEXT: [[VSCALE:%.*]] = call i16 @llvm.vscale.i16() -; CHECK-NEXT: [[RES:%.*]] = icmp ugt i16 [[VSCALE]], 4 -; CHECK-NEXT: ret i1 [[RES]] +; CHECK-NEXT: ret i1 true ; %vscale = call i16 @llvm.vscale.i16() %res = icmp uge i16 %vscale, 5