[llvm] [LV] Refine tripcount estimate using minimum iteration count rt check. (PR #188135)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 24 05:15:33 PDT 2026
================
@@ -460,6 +460,26 @@ static ElementCount getSmallConstantTripCount(ScalarEvolution *SE,
return ElementCount::getFixed(0);
}
+/// Try to refine the maximum trip count for \p L using the unsigned range of
+/// the trip count, discarding the case we warp around to zero. The minimum
+/// iteration check ensures the vector loop won't get entered if computing the
+/// trip count wraps around to 0. Note that this only applies when not folding
+/// the tail. Returns 0 if the range cannot be determined.
+static unsigned tryToRefineConstantMaxTripCount(PredicatedScalarEvolution &PSE,
+ Loop *L) {
+ const SCEV *BTC = PSE.getBackedgeTakenCount();
+ if (isa<SCEVCouldNotCompute>(BTC))
+ return 0;
+ ScalarEvolution *SE = PSE.getSE();
+ const SCEV *TripCount = SE->getTripCountFromExitCount(BTC, BTC->getType(), L);
+ ConstantRange TCRange = SE->getUnsignedRange(TripCount);
+ APInt MaxTCFromRange = TCRange.getUnsignedMax();
+ if (MaxTCFromRange.ugt(0) &&
+ MaxTCFromRange.ule(std::numeric_limits<unsigned>::max()))
+ return MaxTCFromRange.getZExtValue();
+ return 0;
----------------
artagnon wrote:
Hm, if I understand correctly, this is a fallback when SCEV's getSmallConstantMaxTripCount fails? Wouldn't this be best absorbed into the CanUseConstantMax case of getSmallBestKnownTC? Also, "refineConstantMaxTripCount" would imply that we operate on the constant-max trip count, and perform computations to "refine" it, which is not the case here?
https://github.com/llvm/llvm-project/pull/188135
More information about the llvm-commits
mailing list