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

Chaitanya Koparkar via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 05:09:39 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/8] 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 cdf790035b5402ad0bd4c1770d52769fbf1d0802 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/8] 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..2ac98701be435 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 =

>From ce6668b0649202cc9f2551140b28302b17e4ed87 Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Fri, 20 Feb 2026 08:47:39 -0500
Subject: [PATCH 3/8] Remove incorrect use of changeVectorElementCount

---
 llvm/include/llvm/CodeGen/ValueTypes.h        | 1 +
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 4 ++--
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index c33956888c718..724c8d585cb2c 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -111,6 +111,7 @@ 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.
     EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const {
+      assert(isVector() && "Not a vector EVT!");
       if (isSimple()) {
         MVT M = MVT::getVectorVT(getSimpleVT(), EC);
         if (M != MVT::INVALID_SIMPLE_VALUE_TYPE)
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 2ac98701be435..3c5101e752446 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -26146,7 +26146,7 @@ static SDValue combineConcatVectorOfCasts(SDNode *N, SelectionDAG &DAG) {
   EVT SrcEltVT = SrcVT.getVectorElementType();
   ElementCount NumElts = SrcVT.getVectorElementCount() * N->getNumOperands();
   EVT ConcatSrcVT =
-      SrcEltVT.changeVectorElementCount(*DAG.getContext(), NumElts);
+      SrcVT.changeVectorElementCount(*DAG.getContext(), NumElts);
   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
   switch (CastOpcode) {
   case ISD::SINT_TO_FP:
@@ -26963,7 +26963,7 @@ SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode *N) {
         if ((ExtIdx % DestSrcRatio) == 0) {
           unsigned IndexValScaled = ExtIdx / DestSrcRatio;
           EVT NewExtVT =
-              ScalarVT.changeVectorElementCount(*DAG.getContext(), NewExtEC);
+              EVT::getVectorVT(*DAG.getContext(), ScalarVT, NewExtEC);
           if (TLI.isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, NewExtVT)) {
             SDValue NewIndex = DAG.getVectorIdxConstant(IndexValScaled, DL);
             SDValue NewExtract =

>From 55c8d40b4980b632cc0e119d7bf823fc216f4607 Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Fri, 20 Feb 2026 09:01:03 -0500
Subject: [PATCH 4/8] Remove unused variable in DagCombiner

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 3c5101e752446..ecaf47068a456 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -26143,10 +26143,8 @@ static SDValue combineConcatVectorOfCasts(SDNode *N, SelectionDAG &DAG) {
   // the operation support type parameter depends on the opcode. In addition,
   // check the other type in the cast to make sure this is really legal.
   EVT VT = N->getValueType(0);
-  EVT SrcEltVT = SrcVT.getVectorElementType();
   ElementCount NumElts = SrcVT.getVectorElementCount() * N->getNumOperands();
-  EVT ConcatSrcVT =
-      SrcVT.changeVectorElementCount(*DAG.getContext(), NumElts);
+  EVT ConcatSrcVT = SrcVT.changeVectorElementCount(*DAG.getContext(), NumElts);
   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
   switch (CastOpcode) {
   case ISD::SINT_TO_FP:

>From dfe962c4129bbda4d03653d085def6701622c04d Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Mon, 23 Feb 2026 08:12:09 -0500
Subject: [PATCH 5/8] Edit

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index ecaf47068a456..8084b92c694f4 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(*DAG.getContext(), NewEltCnt);
+      EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, NewEltCnt);
       assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
 
       SDValue EltNo = Src->getOperand(1);

>From 985154cc50f1f11b41f6f76170d1dcbb23a0a728 Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Mon, 23 Feb 2026 11:15:05 -0500
Subject: [PATCH 6/8] Use MVT::changeVectorElementCount in one place

---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 7af6db793892b..1ec77075d7350 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -12638,7 +12638,8 @@ SDValue AArch64TargetLowering::LowerSELECT(SDValue Op,
     // for the moment we can't deal with fixed i1 vector types properly, so
     // instead extend the predicate to a result type sized integer vector.
     MVT SplatValVT = MVT::getIntegerVT(Ty.getScalarSizeInBits());
-    MVT PredVT = MVT::getVectorVT(SplatValVT, Ty.getVectorElementCount());
+    MVT PredVT =
+        SplatValVT.changeVectorElementCount(Ty.getVectorElementCount());
     SDValue SplatVal = DAG.getSExtOrTrunc(CCVal, DL, SplatValVT);
     SDValue SplatPred = DAG.getNode(ISD::SPLAT_VECTOR, DL, PredVT, SplatVal);
     return DAG.getNode(ISD::VSELECT, DL, Ty, SplatPred, TVal, FVal);

>From 5f9fd26bc82bbb91521a5857ef6e928edcaa8a88 Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Mon, 23 Feb 2026 11:37:18 -0500
Subject: [PATCH 7/8] Revert "Use MVT::changeVectorElementCount in one place"

This reverts commit 985154cc50f1f11b41f6f76170d1dcbb23a0a728.
---
 llvm/lib/Target/AArch64/AArch64ISelLowering.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index 1ec77075d7350..7af6db793892b 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -12638,8 +12638,7 @@ SDValue AArch64TargetLowering::LowerSELECT(SDValue Op,
     // for the moment we can't deal with fixed i1 vector types properly, so
     // instead extend the predicate to a result type sized integer vector.
     MVT SplatValVT = MVT::getIntegerVT(Ty.getScalarSizeInBits());
-    MVT PredVT =
-        SplatValVT.changeVectorElementCount(Ty.getVectorElementCount());
+    MVT PredVT = MVT::getVectorVT(SplatValVT, Ty.getVectorElementCount());
     SDValue SplatVal = DAG.getSExtOrTrunc(CCVal, DL, SplatValVT);
     SDValue SplatPred = DAG.getNode(ISD::SPLAT_VECTOR, DL, PredVT, SplatVal);
     return DAG.getNode(ISD::VSELECT, DL, Ty, SplatPred, TVal, FVal);

>From 6411a240727b75da7cf3c354a8792a3ea88ff92c Mon Sep 17 00:00:00 2001
From: Chaitanya Koparkar <ckoparkar at gmail.com>
Date: Wed, 4 Mar 2026 08:06:23 -0500
Subject: [PATCH 8/8] Apply RKSimon's suggestion

---
 llvm/include/llvm/CodeGen/ValueTypes.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index 724c8d585cb2c..e8ce9ad8abfca 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -113,7 +113,7 @@ namespace llvm {
     EVT changeVectorElementCount(LLVMContext &Context, ElementCount EC) const {
       assert(isVector() && "Not a vector EVT!");
       if (isSimple()) {
-        MVT M = MVT::getVectorVT(getSimpleVT(), EC);
+        MVT M = getSimpleVT().changeVectorElementCount(EC);
         if (M != MVT::INVALID_SIMPLE_VALUE_TYPE)
           return M;
       }



More information about the llvm-commits mailing list