[llvm] [LV] Use SCEV to check if minimum iteration check is known. (PR #111310)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 13 13:29:14 PDT 2024


================
@@ -2440,12 +2440,28 @@ void InnerLoopVectorizer::emitIterationCountCheck(BasicBlock *Bypass) {
   };
 
   TailFoldingStyle Style = Cost->getTailFoldingStyle();
-  if (Style == TailFoldingStyle::None)
-    CheckMinIters =
-        Builder.CreateICmp(P, Count, CreateStep(), "min.iters.check");
-  else if (VF.isScalable() &&
-           !isIndvarOverflowCheckKnownFalse(Cost, VF, UF) &&
-           Style != TailFoldingStyle::DataAndControlFlowWithoutRuntimeCheck) {
+  if (Style == TailFoldingStyle::None) {
+    Value *Step = CreateStep();
+    ScalarEvolution &SE = *PSE.getSE();
+    // TODO: Emit unconditional branch to vector preheader instead of
+    // conditional branch with known condition.
+    const SCEV *TripCountSCEV = SE.getSCEV(Count);
+    // Check if the trip count is < the step.
+    if (SE.isKnownPredicate(P, SE.applyLoopGuards(TripCountSCEV, OrigLoop),
+                            SE.getSCEV(Step))) {
+      // TODO: Should not attempt to vectorize when the vector loop is known to
+      // never execute.
+      CheckMinIters = Builder.getTrue();
+    } else if (!SE.isKnownPredicate(CmpInst::getInversePredicate(P),
+                                    SE.applyLoopGuards(TripCountSCEV, OrigLoop),
+                                    SE.getSCEV(Step))) {
+      // Only generate the minimum iteration check only if we cannot prove the
----------------
fhahn wrote:

Updated, thanks!

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


More information about the llvm-commits mailing list