[llvm] [LoopVectorize] Improve Vectorization of Low Trip Count Loops (PR #195823)

Jack Styles via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 4 09:30:49 PDT 2026


================
@@ -3064,6 +3064,29 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
       }
     }
 
+    // Allow cases where the ExactTC == (VF * IC) or ExactTC == (VF * IC) + 1.
+    //
+    // This produces at most 1 vector iteration, and at most 1 scalar iteration
+    // with no remainder. Later passes will eliminate the loop and leave
+    // straight-line code as the both iteration counts are statically known.
+    //
+    // If a function is marked as minsize/optsize or OptForSize is set, do not
+    // allow this form of transformation as this will increase CodeSize.
+    ElementCount ExactTC = getSmallConstantTripCount(PSE.getSE(), TheLoop);
+    unsigned TC = ExactTC.getFixedValue();
+    unsigned EffectiveIC = UserIC > 0 ? UserIC : 1;
+    unsigned MaxVFForTC = 1ULL << Log2_32(TC);
+    if (TC - MaxVFForTC <= 1 && !TheFunction->hasOptSize() &&
+        MaxVFForTC <= (MaxFactors.FixedVF.getFixedValue() * EffectiveIC) &&
+        !Config.OptForSize) {
+      unsigned VF = MaxVFForTC / EffectiveIC;
----------------
Stylie777 wrote:

I've added sve-low-trip-count-ic-3.ll to test this.

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


More information about the llvm-commits mailing list