[llvm] Add EVT::changeVectorElementCount and MVT:changeVectorElementCount (PR #182266)

Chaitanya Koparkar via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 20 05:22:32 PST 2026


https://github.com/ckoparkar updated https://github.com/llvm/llvm-project/pull/182266

>From efe22f7a4a2d4d2ec28ba5ef94ade35b32f879fa Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Thu, 19 Feb 2026 08:25:43 -0500
Subject: [PATCH 1/2] Add EVT::changeVectorElementCount and
 MVT:changeVectorElementCount

---
 llvm/include/llvm/CodeGen/ValueTypes.h        | 23 +++++++++++++++++++
 .../llvm/CodeGenTypes/MachineValueType.h      | 18 +++++++++++++++
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp |  2 +-
 3 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index 3897cb8c18127..15b01b634f730 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -108,6 +108,29 @@ namespace llvm {
       return getVectorVT(Context, EltVT, getVectorElementCount());
     }
 
+    /// Return a VT for a vector type whose attributes match ourselves with
+    /// the exception of the element count that is chosen by the caller.
+    EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const {
+      if (isSimple()) {
+        MVT M = MVT::getVectorVT(getSimpleVT(), EC);
+        if (M != MVT::INVALID_SIMPLE_VALUE_TYPE)
+          return M;
+      }
+      return getVectorVT(Context, getVectorElementType(), EC);
+    }
+
+    /// Return a VT for a vector type whose attributes match ourselves with
+    /// the exception of the number of elements that is chosen by the caller.
+    EVT changeVectorNumElements(LLVMContext &Context, unsigned NumElements,
+                                bool IsScalable = false) const {
+      if (isSimple()) {
+        MVT M = MVT::getVectorVT(getSimpleVT(), NumElements, IsScalable);
+        if (M != MVT::INVALID_SIMPLE_VALUE_TYPE)
+          return M;
+      }
+      return getVectorVT(Context, getVectorElementType(), NumElements, IsScalable);
+    }
+
     /// Return a VT for a type whose attributes match ourselves with the
     /// exception of the element type that is chosen by the caller.
     EVT changeElementType(LLVMContext &Context, EVT EltVT) const {
diff --git a/llvm/include/llvm/CodeGenTypes/MachineValueType.h b/llvm/include/llvm/CodeGenTypes/MachineValueType.h
index 08a9c85a213e0..5aff9fcdb83d1 100644
--- a/llvm/include/llvm/CodeGenTypes/MachineValueType.h
+++ b/llvm/include/llvm/CodeGenTypes/MachineValueType.h
@@ -217,6 +217,24 @@ namespace llvm {
       return VecTy;
     }
 
+    /// Return a VT for a vector type whose attributes match ourselves with
+    /// the exception of the element count that is chosen by the caller.
+    MVT changeVectorElementCount(ElementCount EC) const {
+      MVT VecTy = MVT::getVectorVT(getVectorElementType(), EC);
+      assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
+             "Simple vector VT not representable by simple vector VT!");
+      return VecTy;
+    }
+
+    /// Return a VT for a vector type whose attributes match ourselves with
+    /// the exception of the number of elements that is chosen by the caller.
+    MVT changeVectorNumElements(unsigned NumElements, bool IsScalable) const {
+      MVT VecTy = MVT::getVectorVT(getVectorElementType(), NumElements, IsScalable);
+      assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
+             "Simple vector VT not representable by simple vector VT!");
+      return VecTy;
+    }
+
     /// Return the type converted to an equivalently sized integer or vector
     /// with integer element type. Similar to changeVectorElementTypeToInteger,
     /// but also handles scalars.
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 723f838da1246..599685733bcde 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16654,7 +16654,7 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
       unsigned SizeRatio = ExTy.getSizeInBits() / TrTy.getSizeInBits();
       auto NewEltCnt = EltCnt * SizeRatio;
 
-      EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, NewEltCnt);
+      EVT NVT = TrTy.changeVectorElementCount(NewEltCnt);
       assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
 
       SDValue EltNo = Src->getOperand(1);

>From 9921f1d494182865c1367cf60052ea5e341960d6 Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Fri, 20 Feb 2026 08:19:46 -0500
Subject: [PATCH 2/2] Edits

---
 llvm/include/llvm/CodeGen/ValueTypes.h           | 16 ++--------------
 .../include/llvm/CodeGenTypes/MachineValueType.h | 15 ++-------------
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp    |  7 ++++---
 3 files changed, 8 insertions(+), 30 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index 15b01b634f730..c33956888c718 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -108,8 +108,8 @@ namespace llvm {
       return getVectorVT(Context, EltVT, getVectorElementCount());
     }
 
-    /// Return a VT for a vector type whose attributes match ourselves with
-    /// the exception of the element count that is chosen by the caller.
+    /// Return a VT for a vector type whose attributes match ourselves
+    /// with the exception of the element count that is chosen by the caller.
     EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const {
       if (isSimple()) {
         MVT M = MVT::getVectorVT(getSimpleVT(), EC);
@@ -119,18 +119,6 @@ namespace llvm {
       return getVectorVT(Context, getVectorElementType(), EC);
     }
 
-    /// Return a VT for a vector type whose attributes match ourselves with
-    /// the exception of the number of elements that is chosen by the caller.
-    EVT changeVectorNumElements(LLVMContext &Context, unsigned NumElements,
-                                bool IsScalable = false) const {
-      if (isSimple()) {
-        MVT M = MVT::getVectorVT(getSimpleVT(), NumElements, IsScalable);
-        if (M != MVT::INVALID_SIMPLE_VALUE_TYPE)
-          return M;
-      }
-      return getVectorVT(Context, getVectorElementType(), NumElements, IsScalable);
-    }
-
     /// Return a VT for a type whose attributes match ourselves with the
     /// exception of the element type that is chosen by the caller.
     EVT changeElementType(LLVMContext &Context, EVT EltVT) const {
diff --git a/llvm/include/llvm/CodeGenTypes/MachineValueType.h b/llvm/include/llvm/CodeGenTypes/MachineValueType.h
index 5aff9fcdb83d1..99f0bb6aaac2d 100644
--- a/llvm/include/llvm/CodeGenTypes/MachineValueType.h
+++ b/llvm/include/llvm/CodeGenTypes/MachineValueType.h
@@ -220,19 +220,8 @@ namespace llvm {
     /// Return a VT for a vector type whose attributes match ourselves with
     /// the exception of the element count that is chosen by the caller.
     MVT changeVectorElementCount(ElementCount EC) const {
-      MVT VecTy = MVT::getVectorVT(getVectorElementType(), EC);
-      assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
-             "Simple vector VT not representable by simple vector VT!");
-      return VecTy;
-    }
-
-    /// Return a VT for a vector type whose attributes match ourselves with
-    /// the exception of the number of elements that is chosen by the caller.
-    MVT changeVectorNumElements(unsigned NumElements, bool IsScalable) const {
-      MVT VecTy = MVT::getVectorVT(getVectorElementType(), NumElements, IsScalable);
-      assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
-             "Simple vector VT not representable by simple vector VT!");
-      return VecTy;
+      assert(isVector() && "Not a vector MVT!");
+      return MVT::getVectorVT(getVectorElementType(), EC);
     }
 
     /// Return the type converted to an equivalently sized integer or vector
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 599685733bcde..f4fb4f16aeea7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16654,7 +16654,7 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
       unsigned SizeRatio = ExTy.getSizeInBits() / TrTy.getSizeInBits();
       auto NewEltCnt = EltCnt * SizeRatio;
 
-      EVT NVT = TrTy.changeVectorElementCount(NewEltCnt);
+      EVT NVT = TrTy.changeVectorElementCount(*DAG.getContext(), NewEltCnt);
       assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
 
       SDValue EltNo = Src->getOperand(1);
@@ -26145,7 +26145,8 @@ static SDValue combineConcatVectorOfCasts(SDNode *N, SelectionDAG &DAG) {
   EVT VT = N->getValueType(0);
   EVT SrcEltVT = SrcVT.getVectorElementType();
   ElementCount NumElts = SrcVT.getVectorElementCount() * N->getNumOperands();
-  EVT ConcatSrcVT = EVT::getVectorVT(*DAG.getContext(), SrcEltVT, NumElts);
+  EVT ConcatSrcVT =
+    SrcEltVT.changeVectorElementCount(*DAG.getContext(), NumElts);
   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
   switch (CastOpcode) {
   case ISD::SINT_TO_FP:
@@ -26962,7 +26963,7 @@ SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode *N) {
         if ((ExtIdx % DestSrcRatio) == 0) {
           unsigned IndexValScaled = ExtIdx / DestSrcRatio;
           EVT NewExtVT =
-              EVT::getVectorVT(*DAG.getContext(), ScalarVT, NewExtEC);
+            ScalarVT.changeVectorElementCount(*DAG.getContext(), NewExtEC);
           if (TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NewExtVT)) {
             SDValue NewIndex = DAG.getVectorIdxConstant(IndexValScaled, DL);
             SDValue NewExtract =



More information about the llvm-commits mailing list