[llvm] [LV] Use SCEV to check if IV overflow check is known (PR #115705)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 11 18:36:05 PST 2024


================
@@ -2491,17 +2491,19 @@ void InnerLoopVectorizer::emitIterationCountCheck(BasicBlock *Bypass) {
     Value *LHS = Builder.CreateSub(MaxUIntTripCount, Count);
 
     Value *Step = CreateStep();
-#ifndef NDEBUG
     ScalarEvolution &SE = *PSE.getSE();
     const SCEV *TC2OverflowSCEV = SE.applyLoopGuards(SE.getSCEV(LHS), OrigLoop);
-    assert(
-        !isIndvarOverflowCheckKnownFalse(Cost, VF * UF) &&
-        !SE.isKnownPredicate(CmpInst::getInversePredicate(ICmpInst::ICMP_ULT),
-                             TC2OverflowSCEV, SE.getSCEV(Step)) &&
-        "unexpectedly proved overflow check to be known");
-#endif
-    // Don't execute the vector loop if (UMax - n) < (VF * UF).
-    CheckMinIters = Builder.CreateICmp(ICmpInst::ICMP_ULT, LHS, Step);
+    const SCEV *StepSCEV = SE.getSCEV(Step);
+
+    // Check if (UMax - n) < (VF * UF).
+    if (SE.isKnownPredicate(ICmpInst::ICMP_ULT, TC2OverflowSCEV, StepSCEV)) {
----------------
lukel97 wrote:

Thanks for the review and pointers! 2 seems good to me too, I'm happy to give it a stab.

> This is an interesting case. For the loop in the test case, the induction can overflow if `%tc == 0` I think, so `isIndvarOverflowCheckKnownFalse` should return false in general?

Oh woops, I think the exit condition in the test case should have been `icmp uge`. 

But if we did emit the overflow check would it even have caught it at runtime? If `%tc == 0` then I think `(UMax - %tc) < (VF * UF)` will always return false. 

https://github.com/llvm/llvm-project/pull/115705


More information about the llvm-commits mailing list