[llvm] 39ed392 - [DAG] Fix illegal type in srl(bitcast(build_vector)) fold (#205074)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 24 17:48:03 PDT 2026


Author: Kito Cheng
Date: 2026-06-25T08:47:58+08:00
New Revision: 39ed392c4e890d6261e80dcbd98ebf3487c012bc

URL: https://github.com/llvm/llvm-project/commit/39ed392c4e890d6261e80dcbd98ebf3487c012bc
DIFF: https://github.com/llvm/llvm-project/commit/39ed392c4e890d6261e80dcbd98ebf3487c012bc.diff

LOG: [DAG] Fix illegal type in srl(bitcast(build_vector)) fold (#205074)

The fold

```
  (srl (bitcast (build_vector e1, ..., eN)), (N-1) * eltsize) -> (zext eN)
```

added in #181412 built the result through a narrow element integer type,
which
can be illegal (e.g. i16 on RV32 with the P extension, where `<2 x i16>`
is
legal). When the fold runs in the last DAG combine that illegal type
hits the
"Unexpected illegal type!" assert.

Build the result directly in the result type `VT` and mask off the high
bits
instead:

```
  (and (zext/trunc eN to VT), (1 << eltsize) - 1)
```

`VT` is always legal, so no illegal type is created and the fold no
longer needs
to be gated on legalization.

Assisted-by: Opus 4.8

---------

Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>

Added: 
    llvm/test/CodeGen/RISCV/rvp-srl-bitcast-bv.ll

Modified: 
    llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 3c92f395a6453..2f2d5cb709642 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -11705,11 +11705,12 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
         SDValue LastElt = BV.getOperand(NumElts - 1);
         assert(LastElt.getScalarValueSizeInBits() >= EltSizeInBits &&
                "Expected BUILD_VECTOR operand as wide as element type");
-        EVT IntEltVT = EVT::getIntegerVT(*DAG.getContext(), EltSizeInBits);
         LastElt = DAG.getBitcast(LastElt.getValueType().changeTypeToInteger(),
                                  LastElt);
-        return DAG.getZExtOrTrunc(DAG.getZExtOrTrunc(LastElt, DL, IntEltVT), DL,
-                                  VT);
+        SDValue Ext = DAG.getZExtOrTrunc(LastElt, DL, VT);
+        APInt Mask = APInt::getLowBitsSet(VT.getSizeInBits(), EltSizeInBits);
+        return DAG.getNode(ISD::AND, DL, VT, Ext,
+                           DAG.getConstant(Mask, DL, VT));
       }
     }
   }

diff  --git a/llvm/test/CodeGen/RISCV/rvp-srl-bitcast-bv.ll b/llvm/test/CodeGen/RISCV/rvp-srl-bitcast-bv.ll
new file mode 100644
index 0000000000000..0a9b3ff7c5505
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/rvp-srl-bitcast-bv.ll
@@ -0,0 +1,36 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc -mtriple=riscv32 -mattr=+experimental-p,+m,+zbb < %s | \
+; RUN:   FileCheck --check-prefixes=CHECK-RV32 %s
+; RUN: llc -mtriple=riscv64 -mattr=+experimental-p,+m,+zbb < %s | \
+; RUN:   FileCheck --check-prefixes=CHECK-RV64 %s
+
+define i16 @srl_bitcast_buildvector_extract_last(<2 x i16> %v, ptr %p) {
+; CHECK-RV32-LABEL: srl_bitcast_buildvector_extract_last:
+; CHECK-RV32:       # %bb.0:
+; CHECK-RV32-NEXT:    li a2, 1
+; CHECK-RV32-NEXT:    srai a3, a0, 16
+; CHECK-RV32-NEXT:    div a3, a2, a3
+; CHECK-RV32-NEXT:    sext.h a0, a0
+; CHECK-RV32-NEXT:    div a2, a2, a0
+; CHECK-RV32-NEXT:    zext.h a0, a3
+; CHECK-RV32-NEXT:    pack a2, a2, a3
+; CHECK-RV32-NEXT:    sw a2, 0(a1)
+; CHECK-RV32-NEXT:    ret
+;
+; CHECK-RV64-LABEL: srl_bitcast_buildvector_extract_last:
+; CHECK-RV64:       # %bb.0:
+; CHECK-RV64-NEXT:    li a2, 1
+; CHECK-RV64-NEXT:    sext.h a3, a0
+; CHECK-RV64-NEXT:    divw a3, a2, a3
+; CHECK-RV64-NEXT:    slli a0, a0, 32
+; CHECK-RV64-NEXT:    srai a0, a0, 48
+; CHECK-RV64-NEXT:    divw a2, a2, a0
+; CHECK-RV64-NEXT:    sext.h a0, a2
+; CHECK-RV64-NEXT:    ppaire.h a2, a3, a2
+; CHECK-RV64-NEXT:    sw a2, 0(a1)
+; CHECK-RV64-NEXT:    ret
+  %div = sdiv <2 x i16> splat (i16 1), %v
+  store <2 x i16> %div, ptr %p
+  %e = extractelement <2 x i16> %div, i64 1
+  ret i16 %e
+}


        


More information about the llvm-commits mailing list