[llvm] [ValueType] Add widenIntegerElementType method (PR #187816)

Max Graey via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 22 09:09:33 PDT 2026


https://github.com/MaxGraey updated https://github.com/llvm/llvm-project/pull/187816

>From 448addfc87ac66a873ce1a68190308ac5f6bbb5e Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sat, 21 Mar 2026 00:30:25 +0200
Subject: [PATCH 1/4] implement widenIntegerElementType for EVT

---
 llvm/include/llvm/CodeGen/ValueTypes.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index 97aea02535e3f..a20691145b31e 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -447,8 +447,16 @@ namespace llvm {
       return getIntegerVT(Context, (EVTSize + 1) / 2);
     }
 
+    /// Return a VT for an integer scalar type with doubled bit width.
+    /// The type returned may be an extended type.
+    EVT widenIntegerElementType(LLVMContext &Context) const {
+      assert(isInteger() && !isVector() && "Invalid integer type!");
+      unsigned EVTSize = getSizeInBits();
+      return EVT::getIntegerVT(Context, 2 * EVTSize);
+    }
+
     /// Return a VT for an integer vector type with the size of the
-    /// elements doubled. The typed returned may be an extended type.
+    /// elements doubled. The type returned may be an extended type.
     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
       EVT EltVT = getVectorElementType();
       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());

>From 12368972b311e7008fc3f3f7f198934a1d22ad8a Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 22 Mar 2026 15:04:18 +0200
Subject: [PATCH 2/4] make widenIntegerElementType universal for vectors or
 scalars

---
 llvm/include/llvm/CodeGen/ValueTypes.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/ValueTypes.h b/llvm/include/llvm/CodeGen/ValueTypes.h
index a20691145b31e..bd7c46c922a88 100644
--- a/llvm/include/llvm/CodeGen/ValueTypes.h
+++ b/llvm/include/llvm/CodeGen/ValueTypes.h
@@ -447,12 +447,12 @@ namespace llvm {
       return getIntegerVT(Context, (EVTSize + 1) / 2);
     }
 
-    /// Return a VT for an integer scalar type with doubled bit width.
+    /// Return a VT for an integer element type with doubled bit width.
     /// The type returned may be an extended type.
     EVT widenIntegerElementType(LLVMContext &Context) const {
-      assert(isInteger() && !isVector() && "Invalid integer type!");
-      unsigned EVTSize = getSizeInBits();
-      return EVT::getIntegerVT(Context, 2 * EVTSize);
+      unsigned EVTSize = getScalarSizeInBits();
+      EVT EltVT = EVT::getIntegerVT(Context, 2 * EVTSize);
+      return changeElementType(Context, EltVT);
     }
 
     /// Return a VT for an integer vector type with the size of the

>From f0ed14293234543c24f91e88783feff830e7b023 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 22 Mar 2026 18:05:52 +0200
Subject: [PATCH 3/4] add widenIntegerElementType usage in TL

---
 llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index c9bddf636c751..435aebd907735 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -8666,7 +8666,7 @@ SDValue TargetLowering::expandCLMUL(SDNode *Node, SelectionDAG &DAG) const {
       // Strategy 2: Promote to double-element-width CLMUL.
       // CLMUL(X, Y) = Trunc(CLMUL(AnyExt(X), AnyExt(Y)))
       {
-        EVT ExtVT = VT.changeElementType(Ctx, EVT::getIntegerVT(Ctx, 2 * BW));
+        EVT ExtVT = VT.widenIntegerElementType(Ctx);
         if (isTypeLegal(ExtVT) && isOperationLegalOrCustom(ISD::CLMUL, ExtVT)) {
           // If CLMUL on ExtVT is Custom (not Legal), the target may
           // scalarize it, costing O(NumElements) scalar ops. The bit-by-bit
@@ -10308,8 +10308,7 @@ SDValue TargetLowering::expandAVG(SDNode *N, SelectionDAG &DAG) const {
 
   // For scalars, see if we can efficiently extend/truncate to use add+shift.
   if (VT.isScalarInteger()) {
-    unsigned BW = VT.getScalarSizeInBits();
-    EVT ExtVT = VT.getIntegerVT(*DAG.getContext(), 2 * BW);
+    EVT ExtVT = VT.widenIntegerElementType(*DAG.getContext());
     if (isTypeLegal(ExtVT) && isTruncateFree(ExtVT, VT)) {
       LHS = DAG.getNode(ExtOpc, dl, ExtVT, LHS);
       RHS = DAG.getNode(ExtOpc, dl, ExtVT, RHS);
@@ -12016,13 +12015,10 @@ bool TargetLowering::expandMULO(SDNode *Node, SDValue &Result,
     }
   }
 
-  EVT WideVT = EVT::getIntegerVT(*DAG.getContext(), VT.getScalarSizeInBits() * 2);
-  if (VT.isVector())
-    WideVT =
-        EVT::getVectorVT(*DAG.getContext(), WideVT, VT.getVectorElementCount());
-
-  SDValue BottomHalf;
   SDValue TopHalf;
+  SDValue BottomHalf;
+  EVT WideVT = VT.widenIntegerElementType(*DAG.getContext());
+
   static const unsigned Ops[2][3] =
       { { ISD::MULHU, ISD::UMUL_LOHI, ISD::ZERO_EXTEND },
         { ISD::MULHS, ISD::SMUL_LOHI, ISD::SIGN_EXTEND }};

>From 0439f5e728bcb5e32af02ec8d32def92a7d3a6e8 Mon Sep 17 00:00:00 2001
From: MaxGraey <maxgraey at gmail.com>
Date: Sun, 22 Mar 2026 18:09:14 +0200
Subject: [PATCH 4/4] more

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

diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 435aebd907735..e2abcdb1bf2c7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -8746,7 +8746,7 @@ SDValue TargetLowering::expandCLMUL(SDNode *Node, SelectionDAG &DAG) const {
     }
     [[fallthrough]];
   case ISD::CLMULH: {
-    EVT ExtVT = VT.changeElementType(Ctx, EVT::getIntegerVT(Ctx, 2 * BW));
+    EVT ExtVT = VT.widenIntegerElementType(Ctx);
     // Use bitreverse-based lowering (CLMULR/H = rev(CLMUL(rev,rev)) >> S)
     // when any of these hold:
     // (a) ZERO_EXTEND to ExtVT or SRL on ExtVT isn't legal.



More information about the llvm-commits mailing list