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

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 03:17:01 PDT 2026


================
@@ -375,15 +375,23 @@ 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});
+      unsigned MinEC = EC.getKnownMinValue();
+      if (MinEC == 1)
+        return VScale;
+      // TODO: Move this optimization into createOverflowingOp directly.
+      else if (isPowerOf2_32(MinEC)) {
+        VPValue *ShtAmt = Plan.getConstantInt(Ty, Log2_32(MinEC));
+        return createOverflowingOp(Instruction::Shl, {VScale, ShtAmt},
+                                   {true, false});
+      } else {
+        return createOverflowingOp(Instruction::Mul,
+                                   {VScale, Plan.getConstantInt(Ty, MinEC)},
+                                   {true, false});
+      }
----------------
david-arm wrote:

Done!

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


More information about the llvm-commits mailing list