[llvm] [SPARC] Emit a TRUNCATE when combining truncating BSWAP-STOREs (PR #210483)

Sergei Barannikov via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 31 06:53:20 PDT 2026


================
@@ -3333,6 +3333,9 @@ SDValue SparcTargetLowering::PerformSTORECombine(SDNode *N,
       unsigned Shift = VT.getSizeInBits() - MemVT.getSizeInBits();
       BSwapOp = DAG.getNode(ISD::SRL, DL, VT, BSwapOp,
                             DAG.getShiftAmountConstant(Shift, VT, DL));
+      // Need to truncate if this is a bswap of i64 stored as i32/i16.
+      if (VT == MVT::i64)
+        BSwapOp = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BSwapOp);
----------------
s-barannikov wrote:

> `SparcTargetLowering::SparcTargetLowering` only defines operations on i32 and i64

The i32/i64 are the legal types for the target, which means there will be no other types in the DAG after type legalization (*). But DAGCombiner runs before type legalization too, and (if I understand correctly), this code will create a truncate with an invalid type in this case.

(*) When DAGCombiner runs after type legalization, it can still see MVT::i16 *memory* type. It cannot be legalized to i32 or i64 as that would change the semantics.

If we say that SPARCIDS::STORE_BIG/STORE_LITTLE have truncating semantics, I guess it is OK to truncate to i32 instead of i16. But then, why do we need the truncate in the first place if the store will truncate the value? I'm a bit confused.


https://github.com/llvm/llvm-project/pull/210483


More information about the llvm-commits mailing list