[llvm] [LV] Optimise code created by createElementCount (PR #216981)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 18 03:41:05 PDT 2026


================
@@ -375,15 +375,18 @@ class VPBuilder {
 
   VPValue *createElementCount(Type *Ty, ElementCount EC) {
     VPlan &Plan = getPlan();
-    VPValue *RuntimeEC = Plan.getConstantInt(Ty, EC.getKnownMinValue());
     if (EC.isScalable()) {
       VPValue *VScale = createVScale(Ty);
-      RuntimeEC = EC.getKnownMinValue() == 1
-                      ? VScale
-                      : createOverflowingOp(Instruction::Mul,
-                                            {VScale, RuntimeEC}, {true, false});
+      if (EC.getKnownMinValue() == 1)
+        return VScale;
+      assert(isPowerOf2_32(EC.getKnownMinValue()));
+      VPValue *ShtAmt = Plan.getConstantInt(Ty, Log2_32(EC.getKnownMinValue()));
+      // TODO: Setting nuw for this operation may be incorrect without checking
+      // the vscale_range attribute on the function.
----------------
paulwalker-arm wrote:

If the runtime element count cannot fit within `Ty` then the result is poison, hence we use NUW. This is a cornerstone of scalable vectorisation because if you cannot represent the runtime number of elements then the whole vector loop is bogus.

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


More information about the llvm-commits mailing list