[llvm] [SelectionDAG] Avoid unsafe scalarization of widened extract binops (PR #221165)

Oscar Priego via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 4 01:16:51 PDT 2026


https://github.com/Opriego updated https://github.com/llvm/llvm-project/pull/221165

>From 0694d6da458c98e6d6c0213c1bb3754d46f10429 Mon Sep 17 00:00:00 2001
From: Oscar Priego Verdugo <oscar.priegov at gmail.com>
Date: Fri, 4 Sep 2026 02:05:47 -0600
Subject: [PATCH] [SelectionDAG] Avoid unsafe scalarization of widened extract
 binops

---
 llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 29 +++++++++++++++++--
 .../RISCV/rvv/extract-binop-widened.ll        | 26 +++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/CodeGen/RISCV/rvv/extract-binop-widened.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index e08eaf1ba81d4..bad126953bd6f 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -25571,6 +25571,24 @@ SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
   return SDValue();
 }
 
+// Return true if:
+//   trunc(binop(anyext(x), anyext(y))) == binop(x, y)
+// for any choice of high bits introduced by widening the operands.
+static bool isBinOpLowBitsInvariantWithWidenedOperands(unsigned Opc) {
+  switch (Opc) {
+  case ISD::ADD:
+  case ISD::SUB:
+  case ISD::MUL:
+  case ISD::AND:
+  case ISD::OR:
+  case ISD::XOR:
+  case ISD::CLMUL:
+    return true;
+  default:
+    return false;
+  }
+}
+
 /// Transform a vector binary operation into a scalar binary operation by moving
 /// the math/logic after an extract element of a vector.
 static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
@@ -25589,8 +25607,15 @@ static SDValue scalarizeExtractedBinOp(SDNode *ExtElt, SelectionDAG &DAG,
     return SDValue();
 
   EVT ResVT = ExtElt->getValueType(0);
-  if (Opc == ISD::SETCC &&
-      (ResVT != Vec.getValueType().getVectorElementType() || LegalTypes))
+  EVT EltVT = Vec.getValueType().getVectorElementType();
+
+  // EXTRACT_VECTOR_ELT may return an integer type wider than the vector element
+  // type, with undefined high bits. Only scalarize binops in that widened type
+  // if their low bits cannot depend on those undefined high bits.
+  if (ResVT != EltVT && !isBinOpLowBitsInvariantWithWidenedOperands(Opc))
+    return SDValue();
+
+  if (Opc == ISD::SETCC && (ResVT != EltVT || LegalTypes))
     return SDValue();
 
   // Extracting an element of a vector constant is constant-folded, so this
diff --git a/llvm/test/CodeGen/RISCV/rvv/extract-binop-widened.ll b/llvm/test/CodeGen/RISCV/rvv/extract-binop-widened.ll
new file mode 100644
index 0000000000000..b8a9a6ba93251
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvv/extract-binop-widened.ll
@@ -0,0 +1,26 @@
+; RUN: llc -O0 -mtriple=riscv64-unknown-linux-gnu -mattr=+m,+v -verify-machineinstrs < %s | FileCheck %s
+
+define i64 @rotl_extract_widened() {
+; CHECK-LABEL: rotl_extract_widened:
+; CHECK:       lui a0, 15
+; CHECK-NEXT:  addi a0, a0, -106
+; CHECK-NEXT:  ret
+entry:
+  %vec.zero = insertelement <2 x i16> zeroinitializer, i16 0, i32 0
+  %vec.const = xor <2 x i16> %vec.zero, <i16 -7273, i16 -10240>
+  %vec.shifted = call <2 x i16> @llvm.fshl.v2i16(
+      <2 x i16> %vec.const, <2 x i16> %vec.const,
+      <2 x i16> <i16 0, i16 12>)
+
+  %a = extractelement <2 x i16> %vec.shifted, i32 0
+  %az = zext i16 %a to i64
+  %ax = xor i64 %az, 1
+
+  %b = extractelement <2 x i16> %vec.shifted, i32 1
+  %bz = zext i16 %b to i64
+
+  %r = or i64 %ax, %bz
+  ret i64 %r
+}
+
+declare <2 x i16> @llvm.fshl.v2i16(<2 x i16>, <2 x i16>, <2 x i16>)



More information about the llvm-commits mailing list