[llvm-branch-commits] [llvm] [RISCV] Attach VLMAX range attribute for vsetvli/vsetvlimax in InstCombine (PR #218652)

Pengcheng Wang via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Aug 25 21:34:01 PDT 2026


https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/218652

>From 24ac21e39c0d0089ca35bddad9e386b3be6f100a Mon Sep 17 00:00:00 2001
From: Pengcheng Wang <wangpengcheng.pp at bytedance.com>
Date: Wed, 26 Aug 2026 12:33:49 +0800
Subject: [PATCH] Address comments

Created using spr 1.3.6-beta.1
---
 .../Target/RISCV/RISCVTargetTransformInfo.cpp | 23 ++++++++++---------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
index bf920faa1c066..8b4f8f5c3bce6 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetTransformInfo.cpp
@@ -3756,13 +3756,13 @@ RISCVTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
   // so generic value analyses can reason about it. The verifier guarantees an
   // XLen result and constant VSEW/VLMUL encoding a valid vtype, so no defensive
   // validation is needed here.
-  if (II.getIntrinsicID() == Intrinsic::riscv_vsetvli ||
-      II.getIntrinsicID() == Intrinsic::riscv_vsetvlimax) {
+  if (is_contained({Intrinsic::riscv_vsetvli, Intrinsic::riscv_vsetvlimax},
+                   II.getIntrinsicID())) {
     bool HasAVL = II.getIntrinsicID() == Intrinsic::riscv_vsetvli;
     unsigned Offset = HasAVL ? 1 : 0;
-    unsigned Width = II.getType()->getScalarSizeInBits();
-    ConstantRange VLenRange(APInt(Width, ST->getRealMinVLen()),
-                            APInt(Width, ST->getRealMaxVLen()) + 1);
+    unsigned BitWidth = II.getType()->getIntegerBitWidth();
+    ConstantRange VLenRange(APInt(BitWidth, ST->getRealMinVLen()),
+                            APInt(BitWidth, ST->getRealMaxVLen()) + 1);
 
     uint64_t VSEW = cast<ConstantInt>(II.getArgOperand(Offset))->getZExtValue();
     auto VLMUL = static_cast<RISCVVType::VLMUL>(
@@ -3772,8 +3772,8 @@ RISCVTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
 
     // VLMAX = VLEN / (SEW / LMUL), clamped to >= 1 for any usable vtype.
     ConstantRange VLMAXRange =
-        VLenRange.udiv(ConstantRange(APInt(Width, Ratio)))
-            .umax(ConstantRange(APInt(Width, 1)));
+        VLenRange.udiv(ConstantRange(APInt(BitWidth, Ratio)))
+            .umax(ConstantRange(APInt(BitWidth, 1)));
 
     // vsetvlimax returns exactly VLMAX; vsetvli returns vl with
     // 0 <= vl <= min(AVL, VLMAX). vl == AVL only when AVL <= the smallest
@@ -3787,15 +3787,16 @@ RISCVTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
         if (C.ule(VLMAXRange.getUnsignedMin()))
           VLRange = ConstantRange(C);
         else
-          VLRange = ConstantRange::getNonEmpty(APInt::getZero(Width),
-                                               (C.ult(MaxVL) ? C : MaxVL) + 1);
+          VLRange = ConstantRange::getNonEmpty(APInt::getZero(BitWidth),
+                                               APIntOps::umin(C, MaxVL) + 1);
       } else {
-        VLRange = ConstantRange::getNonEmpty(APInt::getZero(Width), MaxVL + 1);
+        VLRange =
+            ConstantRange::getNonEmpty(APInt::getZero(BitWidth), MaxVL + 1);
       }
     }
 
     ConstantRange OldRange =
-        II.getRange().value_or(ConstantRange::getFull(Width));
+        II.getRange().value_or(ConstantRange::getFull(BitWidth));
     ConstantRange NewRange = VLRange.intersectWith(OldRange);
     if (NewRange != OldRange) {
       II.addRangeRetAttr(NewRange);



More information about the llvm-branch-commits mailing list