[llvm] [M68k] Implement `SWAP` instruction encoding, patterns, and tests (PR #215597)

Dan Salvato via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 15 20:22:30 PDT 2026


https://github.com/dansalvato updated https://github.com/llvm/llvm-project/pull/215597

>From be85f427a11a521120e3570c4ea25973a4953f86 Mon Sep 17 00:00:00 2001
From: Dan Salvato <dan at teamsalvato.com>
Date: Tue, 11 Aug 2026 10:12:56 -0400
Subject: [PATCH 1/2] [M68k] Implement `SWAP` instruction encoding, patterns,
 and tests

This adds instruction encoding for `SWAP` and some basic patterns:

- Rotating left/right by 16 bits matches to `SWAP`.
- Existing mul/div/rem patterns that would fetch the high word of the
result by shifting it down 16 bits now use `SWAP` instead.
- LLVM `bswap` on i32 is given a MIR pattern that uses `SWAP` and `ROL`
for a much more efficient outcome.

Some tests are added for `rem`, `ror`/`rol`, and `bswap` that trigger
the conditions for `SWAP` to be used.

Since `SWAP` is used for lowering `rem`, a related error is fixed where
div/rem operations on `i8` were declared as Promote instead of Legal.
Although m68k can't natively divide `i8`, there are patterns already
defined that explicitly handle the integer promotion in a
target-specific way. So, declaring as Legal enables those patterns to be
matched.
---
 llvm/lib/Target/M68k/M68kISelLowering.cpp     | 39 +++++++----
 llvm/lib/Target/M68k/M68kInstrArithmetic.td   | 27 ++++----
 llvm/lib/Target/M68k/M68kInstrShiftRotate.td  | 30 ++++++++
 .../CodeGen/M68k/Arith/divide-by-constant.ll  | 14 ++--
 llvm/test/CodeGen/M68k/Arith/rem.ll           | 69 +++++++++++++++++++
 llvm/test/CodeGen/M68k/ShiftRotate/bswap.ll   | 28 ++++++++
 llvm/test/CodeGen/M68k/ShiftRotate/rol.ll     | 11 +++
 llvm/test/CodeGen/M68k/ShiftRotate/ror.ll     | 11 +++
 8 files changed, 195 insertions(+), 34 deletions(-)
 create mode 100644 llvm/test/CodeGen/M68k/Arith/rem.ll
 create mode 100644 llvm/test/CodeGen/M68k/ShiftRotate/bswap.ll

diff --git a/llvm/lib/Target/M68k/M68kISelLowering.cpp b/llvm/lib/Target/M68k/M68kISelLowering.cpp
index d167e7f6597b6..5adfbbf2f795b 100644
--- a/llvm/lib/Target/M68k/M68kISelLowering.cpp
+++ b/llvm/lib/Target/M68k/M68kISelLowering.cpp
@@ -77,17 +77,29 @@ M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine &TM,
   setTruncStoreAction(MVT::i32, MVT::i8, Expand);
   setTruncStoreAction(MVT::i16, MVT::i8, Expand);
 
-  setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i8, Promote);
-  setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i16, Legal);
-  if (Subtarget.atLeastM68020())
-    setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i32, Legal);
-  else
-    setOperationAction({ISD::MUL, ISD::SDIV, ISD::UDIV}, MVT::i32, LibCall);
-  setOperationAction(ISD::MUL, MVT::i64, LibCall);
+  // M68k can't natively div/rem 8-bit values, but we define our own patterns
+  // that handle the integer promotion, so it's marked as legal here.
+  setOperationAction(ISD::MUL, MVT::i8, Promote);
+  setOperationAction(ISD::MUL, MVT::i16, Legal);
+  setOperationAction({ISD::SDIV, ISD::UDIV}, MVT::i8, Legal);
+  setOperationAction({ISD::SDIV, ISD::UDIV}, MVT::i16, Legal);
+
+  if (Subtarget.atLeastM68020()) {
+    setOperationAction(ISD::MUL, MVT::i32, Legal);
+    setOperationAction(ISD::SDIV, MVT::i32, Legal);
+    setOperationAction(ISD::UDIV, MVT::i32, Legal);
+  } else {
+    setOperationAction(ISD::MUL, MVT::i32, LibCall);
+    setOperationAction(ISD::SDIV, MVT::i32, LibCall);
+    setOperationAction(ISD::UDIV, MVT::i32, LibCall);
+  }
 
-  for (auto OP :
-       {ISD::SREM, ISD::UREM, ISD::UDIVREM, ISD::SDIVREM,
-        ISD::MULHS, ISD::MULHU, ISD::UMUL_LOHI, ISD::SMUL_LOHI}) {
+  setOperationAction({ISD::SREM, ISD::UREM}, MVT::i8, Legal);
+  setOperationAction({ISD::SREM, ISD::UREM}, MVT::i16, Legal);
+  setOperationAction({ISD::SREM, ISD::UREM}, MVT::i32, LibCall);
+
+  for (auto OP : {ISD::UDIVREM, ISD::SDIVREM, ISD::MULHS, ISD::MULHU,
+                  ISD::UMUL_LOHI, ISD::SMUL_LOHI}) {
     setOperationAction(OP, MVT::i8, Promote);
     setOperationAction(OP, MVT::i16, Legal);
     setOperationAction(OP, MVT::i32, LibCall);
@@ -99,7 +111,7 @@ M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine &TM,
   }
 
   for (auto OP : {ISD::SMULO, ISD::UMULO}) {
-    setOperationAction(OP, MVT::i8,  Custom);
+    setOperationAction(OP, MVT::i8, Custom);
     setOperationAction(OP, MVT::i16, Custom);
     setOperationAction(OP, MVT::i32, Custom);
   }
@@ -134,8 +146,11 @@ M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine &TM,
     setOperationAction(ISD::SETCCCARRY, VT, Custom);
   }
 
+  setOperationAction(ISD::BSWAP, MVT::i8, Expand);
+  setOperationAction(ISD::BSWAP, MVT::i16, Expand);
+  setOperationAction(ISD::BSWAP, MVT::i32, Legal);
+
   for (auto VT : {MVT::i8, MVT::i16, MVT::i32}) {
-    setOperationAction(ISD::BSWAP, VT, Expand);
     setOperationAction(ISD::CTTZ, VT, Expand);
     setOperationAction(ISD::CTLZ, VT, Expand);
     setOperationAction(ISD::CTPOP, VT, Expand);
diff --git a/llvm/lib/Target/M68k/M68kInstrArithmetic.td b/llvm/lib/Target/M68k/M68kInstrArithmetic.td
index 56b71db2d1d2f..300b098716313 100644
--- a/llvm/lib/Target/M68k/M68kInstrArithmetic.td
+++ b/llvm/lib/Target/M68k/M68kInstrArithmetic.td
@@ -642,6 +642,9 @@ def as_i16imm : SDNodeXForm<imm, [{
   return CurDAG->getTargetConstant(N->getSExtValue(), SDLoc(N), MVT::i16);
 }]>;
 
+// M68k divs/divu outputs the quotient in the lower 16 bits and the remainder
+// in the upper 16 bits of the register.
+
 // RR i8
 def : Pat<(sdiv i8:$dst, i8:$opd),
           (EXTRACT_SUBREG
@@ -655,12 +658,12 @@ def : Pat<(udiv i8:$dst, i8:$opd),
 
 def : Pat<(srem i8:$dst, i8:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SDIVd32d16 (MOVSXd32d8 $dst), (MOVSXd16d8 $opd)), 8), 8),
+            (SWAP (SDIVd32d16 (MOVSXd32d8 $dst), (MOVSXd16d8 $opd))),
              MxSubRegIndex8Lo)>;
 
 def : Pat<(urem i8:$dst, i8:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UDIVd32d16 (MOVZXd32d8 $dst), (MOVZXd16d8 $opd)), 8), 8),
+            (SWAP (UDIVd32d16 (MOVZXd32d8 $dst), (MOVZXd16d8 $opd))),
              MxSubRegIndex8Lo)>;
 
 // RR i16
@@ -676,12 +679,12 @@ def : Pat<(udiv i16:$dst, i16:$opd),
 
 def : Pat<(srem i16:$dst, i16:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SDIVd32d16 (MOVSXd32d16 $dst), $opd), 8), 8),
+            (SWAP (SDIVd32d16 (MOVSXd32d16 $dst), $opd)),
              MxSubRegIndex16Lo)>;
 
 def : Pat<(urem i16:$dst, i16:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UDIVd32d16 (MOVZXd32d16 $dst), $opd), 8), 8),
+            (SWAP (UDIVd32d16 (MOVZXd32d16 $dst), $opd)),
              MxSubRegIndex16Lo)>;
 
 // RI i8
@@ -697,12 +700,12 @@ def : Pat<(udiv i8:$dst, Mxi8immSExt8:$opd),
 
 def : Pat<(srem i8:$dst, Mxi8immSExt8:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SDIVd32i16 (MOVSXd32d8 $dst), (as_i16imm $opd)), 8), 8),
+            (SWAP (SDIVd32i16 (MOVSXd32d8 $dst), (as_i16imm $opd))),
              MxSubRegIndex8Lo)>;
 
 def : Pat<(urem i8:$dst, Mxi8immSExt8:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UDIVd32i16 (MOVZXd32d8 $dst), (as_i16imm $opd)), 8), 8),
+            (SWAP (UDIVd32i16 (MOVZXd32d8 $dst), (as_i16imm $opd))),
              MxSubRegIndex8Lo)>;
 
 // RI i16
@@ -718,12 +721,12 @@ def : Pat<(udiv i16:$dst, Mxi16immSExt16:$opd),
 
 def : Pat<(srem i16:$dst, Mxi16immSExt16:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SDIVd32i16 (MOVSXd32d16 $dst), imm:$opd), 8), 8),
+            (SWAP (SDIVd32i16 (MOVSXd32d16 $dst), imm:$opd)),
              MxSubRegIndex16Lo)>;
 
 def : Pat<(urem i16:$dst, Mxi16immSExt16:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UDIVd32i16 (MOVZXd32d16 $dst), imm:$opd), 8), 8),
+            (SWAP (UDIVd32i16 (MOVZXd32d16 $dst), imm:$opd)),
              MxSubRegIndex16Lo)>;
 
 
@@ -740,12 +743,12 @@ def : Pat<(mul i16:$dst, i16:$opd),
 
 def : Pat<(mulhs i16:$dst, i16:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SMULd32d16 (MOVXd32d16 $dst), $opd), 8), 8),
+            (SWAP (SMULd32d16 (MOVXd32d16 $dst), $opd)),
              MxSubRegIndex16Lo)>;
 
 def : Pat<(mulhu i16:$dst, i16:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UMULd32d16 (MOVXd32d16 $dst), $opd), 8), 8),
+            (SWAP (UMULd32d16 (MOVXd32d16 $dst), $opd)),
              MxSubRegIndex16Lo)>;
 
 def : Pat<(mul i32:$dst, i32:$opd), (SMULd32d32 $dst, $opd)>;
@@ -759,12 +762,12 @@ def : Pat<(mul i16:$dst, Mxi16immSExt16:$opd),
 
 def : Pat<(mulhs i16:$dst, Mxi16immSExt16:$opd),
           (EXTRACT_SUBREG
-            (ASR32di (ASR32di (SMULd32i16 (MOVXd32d16 $dst), imm:$opd), 8), 8),
+            (SWAP (SMULd32i16 (MOVXd32d16 $dst), imm:$opd)),
              MxSubRegIndex16Lo)>;
 
 def : Pat<(mulhu i16:$dst, Mxi16immSExt16:$opd),
           (EXTRACT_SUBREG
-            (LSR32di (LSR32di (UMULd32i16 (MOVXd32d16 $dst), imm:$opd), 8), 8),
+            (SWAP (UMULd32i16 (MOVXd32d16 $dst), imm:$opd)),
              MxSubRegIndex16Lo)>;
 
 
diff --git a/llvm/lib/Target/M68k/M68kInstrShiftRotate.td b/llvm/lib/Target/M68k/M68kInstrShiftRotate.td
index 7de9946260144..d9918c9b99ded 100644
--- a/llvm/lib/Target/M68k/M68kInstrShiftRotate.td
+++ b/llvm/lib/Target/M68k/M68kInstrShiftRotate.td
@@ -96,3 +96,33 @@ defm ASR : MxSROp<"asr", sra, MxRODI_R, MxROOP_AS>;
 
 defm ROL : MxSROp<"rol", rotl, MxRODI_L, MxROOP_RO>;
 defm ROR : MxSROp<"ror", rotr, MxRODI_R, MxROOP_RO>;
+
+
+/// ---------------------------------------+---------
+///  F  E  D  C  B  A  9  8  7  6  5  4  3 | 2  1  0
+/// ---------------------------------------+---------
+///  0  1  0  0  1  0  0  0  0  1  0  0  0 |   REG
+/// ---------------------------------------+---------
+
+let isAsCheapAsAMove = 1, Defs = [CCR] in {
+let Constraints = "$src = $dst" in {
+def SWAP
+      : MxInst<(outs MxDRD32:$dst), (ins MxDRD32:$src), "swap\t$src",
+        []> {
+    let Inst = (descend 0b0100, 0b1000, 0b0100, 0b0, (operand "$src", 3));
+  }
+} // Constraints
+} // Defs = [CCR]
+
+def : Pat<(rotl i32:$src, (i32 16)), (SWAP MxDRD32:$src)>;
+def : Pat<(rotr i32:$src, (i32 16)), (SWAP MxDRD32:$src)>;
+
+// bswap i32 (0x00112233 -> 0x33221100)
+// lowers to: rol.w #8,dn -> swap dn -> rol.w #8,dn
+// LLVM's intrinsic expansion of bswap uses a shifting/masking algorithm that
+// strictly operates on the i32. But we can do much better by operating
+// in-place on the 16-bit subregister and casting back to the 32-bit register.
+def : Pat<(bswap i32:$src),
+  (SUBREG_TO_REG (ROL16di (EXTRACT_SUBREG (SWAP (SUBREG_TO_REG (ROL16di
+  (EXTRACT_SUBREG $src, MxSubRegIndex16Lo), 8), MxSubRegIndex16Lo)),
+  MxSubRegIndex16Lo), 8), MxSubRegIndex16Lo)>;
diff --git a/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll b/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll
index 7d4caf1996d63..2aa94fb47beb7 100644
--- a/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll
+++ b/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll
@@ -1,14 +1,12 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc < %s -mtriple=m68k-linux -verify-machineinstrs | FileCheck %s
 
-; TODO fold the shifts
 define zeroext i16 @test1(i16 zeroext %x) nounwind {
 ; CHECK-LABEL: test1:
 ; CHECK:       ; %bb.0: ; %entry
 ; CHECK-NEXT:    move.w (6,%sp), %d0
 ; CHECK-NEXT:    mulu #-1985, %d0
-; CHECK-NEXT:    lsr.l #8, %d0
-; CHECK-NEXT:    lsr.l #8, %d0
+; CHECK-NEXT:    swap %d0
 ; CHECK-NEXT:    lsr.w #5, %d0
 ; CHECK-NEXT:    and.l #65535, %d0
 ; CHECK-NEXT:    rts
@@ -23,8 +21,7 @@ define zeroext i16 @test2(i8 signext %x, i16 zeroext %c) {
 ; CHECK-NEXT:  ; %bb.0: ; %entry
 ; CHECK-NEXT:    move.w (10,%sp), %d0
 ; CHECK-NEXT:    mulu #-21845, %d0
-; CHECK-NEXT:    lsr.l #8, %d0
-; CHECK-NEXT:    lsr.l #8, %d0
+; CHECK-NEXT:    swap %d0
 ; CHECK-NEXT:    lsr.w #1, %d0
 ; CHECK-NEXT:    and.l #65535, %d0
 ; CHECK-NEXT:    rts
@@ -56,8 +53,7 @@ define signext i16 @test4(i16 signext %x) nounwind {
 ; CHECK-NEXT:    movem.l %d2, (0,%sp) ; 8-byte Folded Spill
 ; CHECK-NEXT:    move.w (10,%sp), %d0
 ; CHECK-NEXT:    muls #1986, %d0
-; CHECK-NEXT:    asr.l #8, %d0
-; CHECK-NEXT:    asr.l #8, %d0
+; CHECK-NEXT:    swap %d0
 ; CHECK-NEXT:    moveq #15, %d1
 ; CHECK-NEXT:    move.w %d0, %d2
 ; CHECK-NEXT:    lsr.w %d1, %d2
@@ -85,7 +81,6 @@ define i32 @test5(i32 %A) nounwind {
   ret i32 %tmp1
 }
 
-; TODO fold shift
 define signext i16 @test6(i16 signext %x) nounwind {
 ; CHECK-LABEL: test6:
 ; CHECK:       ; %bb.0: ; %entry
@@ -93,8 +88,7 @@ define signext i16 @test6(i16 signext %x) nounwind {
 ; CHECK-NEXT:    movem.l %d2, (0,%sp) ; 8-byte Folded Spill
 ; CHECK-NEXT:    move.w (10,%sp), %d0
 ; CHECK-NEXT:    muls #26215, %d0
-; CHECK-NEXT:    asr.l #8, %d0
-; CHECK-NEXT:    asr.l #8, %d0
+; CHECK-NEXT:    swap %d0
 ; CHECK-NEXT:    moveq #15, %d1
 ; CHECK-NEXT:    move.w %d0, %d2
 ; CHECK-NEXT:    lsr.w %d1, %d2
diff --git a/llvm/test/CodeGen/M68k/Arith/rem.ll b/llvm/test/CodeGen/M68k/Arith/rem.ll
new file mode 100644
index 0000000000000..877ea38c84221
--- /dev/null
+++ b/llvm/test/CodeGen/M68k/Arith/rem.ll
@@ -0,0 +1,69 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=m68k-linux -verify-machineinstrs | FileCheck %s
+
+; Remainder is expected to lower to DIV -> SWAP
+
+define zeroext i16 @test1(i16 zeroext %a, i16 zeroext %b) nounwind {
+; CHECK-LABEL: test1:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    move.w (6,%sp), %d0
+; CHECK-NEXT:    and.l #65535, %d0
+; CHECK-NEXT:    move.w (10,%sp), %d1
+; CHECK-NEXT:    divu %d1, %d0
+; CHECK-NEXT:    swap %d0
+; CHECK-NEXT:    and.l #65535, %d0
+; CHECK-NEXT:    rts
+entry:
+	%rem = urem i16 %a, %b
+	ret i16 %rem
+}
+
+define zeroext i16 @test2(i16 zeroext %a, i16 zeroext %b) nounwind {
+; CHECK-LABEL: test2:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    move.w (6,%sp), %d0
+; CHECK-NEXT:    ext.l %d0
+; CHECK-NEXT:    move.w (10,%sp), %d1
+; CHECK-NEXT:    divs %d1, %d0
+; CHECK-NEXT:    swap %d0
+; CHECK-NEXT:    and.l #65535, %d0
+; CHECK-NEXT:    rts
+entry:
+	%rem = srem i16 %a, %b
+	ret i16 %rem
+}
+
+define zeroext i8 @test3(i8 zeroext %a, i8 zeroext %b) nounwind {
+; CHECK-LABEL: test3:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    move.b (11,%sp), %d0
+; CHECK-NEXT:    and.w #255, %d0
+; CHECK-NEXT:    move.b (7,%sp), %d1
+; CHECK-NEXT:    and.l #255, %d1
+; CHECK-NEXT:    divu %d0, %d1
+; CHECK-NEXT:    swap %d1
+; CHECK-NEXT:    move.l %d1, %d0
+; CHECK-NEXT:    and.l #255, %d0
+; CHECK-NEXT:    rts
+entry:
+	%rem = urem i8 %a, %b
+	ret i8 %rem
+}
+
+define zeroext i8 @test4(i8 zeroext %a, i8 zeroext %b) nounwind {
+; CHECK-LABEL: test4:
+; CHECK:       ; %bb.0: ; %entry
+; CHECK-NEXT:    move.b (11,%sp), %d0
+; CHECK-NEXT:    ext.w %d0
+; CHECK-NEXT:    move.b (7,%sp), %d1
+; CHECK-NEXT:    ext.w %d1
+; CHECK-NEXT:    ext.l %d1
+; CHECK-NEXT:    divs %d0, %d1
+; CHECK-NEXT:    swap %d1
+; CHECK-NEXT:    move.l %d1, %d0
+; CHECK-NEXT:    and.l #255, %d0
+; CHECK-NEXT:    rts
+entry:
+	%rem = srem i8 %a, %b
+	ret i8 %rem
+}
diff --git a/llvm/test/CodeGen/M68k/ShiftRotate/bswap.ll b/llvm/test/CodeGen/M68k/ShiftRotate/bswap.ll
new file mode 100644
index 0000000000000..9a53916546f99
--- /dev/null
+++ b/llvm/test/CodeGen/M68k/ShiftRotate/bswap.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=m68k-linux -verify-machineinstrs | FileCheck %s
+
+declare i16 @llvm.bswap.i16(i16)
+declare i32 @llvm.bswap.i32(i32)
+
+define zeroext i16 @bswap16(i16 zeroext %a) nounwind {
+; CHECK-LABEL: bswap16:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    move.w (6,%sp), %d0
+; CHECK-NEXT:    rol.w #8, %d0
+; CHECK-NEXT:    and.l #65535, %d0
+; CHECK-NEXT:    rts
+  %1 = tail call i16 @llvm.bswap.i16(i16 %a)
+  ret i16 %1
+}
+
+define zeroext i32 @bswap32(i32 zeroext %a) nounwind {
+; CHECK-LABEL: bswap32:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    move.l (4,%sp), %d0
+; CHECK-NEXT:    rol.w #8, %d0
+; CHECK-NEXT:    swap %d0
+; CHECK-NEXT:    rol.w #8, %d0
+; CHECK-NEXT:    rts
+  %1 = tail call i32 @llvm.bswap.i32(i32 %a)
+  ret i32 %1
+}
diff --git a/llvm/test/CodeGen/M68k/ShiftRotate/rol.ll b/llvm/test/CodeGen/M68k/ShiftRotate/rol.ll
index bb2c071e11b98..4f0a4786903de 100644
--- a/llvm/test/CodeGen/M68k/ShiftRotate/rol.ll
+++ b/llvm/test/CodeGen/M68k/ShiftRotate/rol.ll
@@ -77,3 +77,14 @@ define i32 @rolil(i32 %a) nounwind {
   %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 7)
   ret i32 %1
 }
+
+; Rotating 16 bits should lower to SWAP
+define i32 @rolswap(i32 %a) nounwind {
+; CHECK-LABEL: rolswap:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    move.l (4,%sp), %d0
+; CHECK-NEXT:    swap %d0
+; CHECK-NEXT:    rts
+  %1 = tail call i32 @llvm.fshl.i32(i32 %a, i32 %a, i32 16)
+  ret i32 %1
+}
diff --git a/llvm/test/CodeGen/M68k/ShiftRotate/ror.ll b/llvm/test/CodeGen/M68k/ShiftRotate/ror.ll
index 7848db46f557a..11504418d8b37 100644
--- a/llvm/test/CodeGen/M68k/ShiftRotate/ror.ll
+++ b/llvm/test/CodeGen/M68k/ShiftRotate/ror.ll
@@ -77,3 +77,14 @@ define i32 @roril(i32 %a) nounwind {
   %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 7)
   ret i32 %1
 }
+
+; Rotating 16 bits should lower to SWAP
+define i32 @rorswap(i32 %a) nounwind {
+; CHECK-LABEL: rorswap:
+; CHECK:       ; %bb.0:
+; CHECK-NEXT:    move.l (4,%sp), %d0
+; CHECK-NEXT:    swap %d0
+; CHECK-NEXT:    rts
+  %1 = tail call i32 @llvm.fshr.i32(i32 %a, i32 %a, i32 16)
+  ret i32 %1
+}

>From 86d224c9c3e8b621e344a4966b5cfcf7f6b3c62e Mon Sep 17 00:00:00 2001
From: Dan Salvato <dan at teamsalvato.com>
Date: Sat, 15 Aug 2026 23:21:34 -0400
Subject: [PATCH 2/2] Restore omitted legalizer operation for i64 MUL

---
 llvm/lib/Target/M68k/M68kISelLowering.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/lib/Target/M68k/M68kISelLowering.cpp b/llvm/lib/Target/M68k/M68kISelLowering.cpp
index 5adfbbf2f795b..1775119e808b8 100644
--- a/llvm/lib/Target/M68k/M68kISelLowering.cpp
+++ b/llvm/lib/Target/M68k/M68kISelLowering.cpp
@@ -93,6 +93,7 @@ M68kTargetLowering::M68kTargetLowering(const M68kTargetMachine &TM,
     setOperationAction(ISD::SDIV, MVT::i32, LibCall);
     setOperationAction(ISD::UDIV, MVT::i32, LibCall);
   }
+  setOperationAction(ISD::MUL, MVT::i64, LibCall);
 
   setOperationAction({ISD::SREM, ISD::UREM}, MVT::i8, Legal);
   setOperationAction({ISD::SREM, ISD::UREM}, MVT::i16, Legal);



More information about the llvm-commits mailing list