[llvm] [Mips] Add microMIPS CP0 move encodings (PR #228884)
Jiaxun Yang via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 4 07:49:09 PDT 2026
https://github.com/FlyGoat updated https://github.com/llvm/llvm-project/pull/228884
>From 3808d143072c30ab0a96be7b3ba9dd3eba411a4b Mon Sep 17 00:00:00 2001
From: Jiaxun Yang <jiaxun.yang at flygoat.com>
Date: Sun, 4 Oct 2026 12:23:43 +0100
Subject: [PATCH] [Mips] Add microMIPS CP0 move encodings
Define the microMIPS MFC0 and MTC0 formats and instruction records for
pre-R6 targets, including the select field. Accept the two-operand forms
with an implicit select value of zero and add generic scheduling entries.
Extend the control-instruction tests with explicit and implicit select
operands, checking both byte orders and disassembly.
Assisted-by: OpenAI Codex # Testcases
---
llvm/lib/Target/Mips/MicroMipsInstrFormats.td | 15 +++++++++++++++
llvm/lib/Target/Mips/MicroMipsInstrInfo.td | 11 +++++++++++
llvm/lib/Target/Mips/MipsScheduleGeneric.td | 4 ++--
.../MC/Mips/micromips-control-instructions.s | 19 +++++++++++++++++++
4 files changed, 47 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/Mips/MicroMipsInstrFormats.td b/llvm/lib/Target/Mips/MicroMipsInstrFormats.td
index ff2b0ff491a3c..faaa9bc15efdf 100644
--- a/llvm/lib/Target/Mips/MicroMipsInstrFormats.td
+++ b/llvm/lib/Target/Mips/MicroMipsInstrFormats.td
@@ -868,6 +868,21 @@ class CMov_I_F_FM_MM<bits<8> funct, bits<2> fmt> : MMArch {
let Inst{7-0} = funct;
}
+class MFC0_FM_MM<bits<11> funct> : MMArch {
+ bits<5> rt;
+ bits<5> rd;
+ bits<3> sel;
+
+ bits<32> Inst;
+
+ let Inst{31-26} = 0;
+ let Inst{25-21} = rt;
+ let Inst{20-16} = rd;
+ let Inst{15-14} = 0;
+ let Inst{13-11} = sel;
+ let Inst{10-0} = funct;
+}
+
class MFC1_FM_MM<bits<8> funct> : MMArch {
bits<5> rt;
bits<5> fs;
diff --git a/llvm/lib/Target/Mips/MicroMipsInstrInfo.td b/llvm/lib/Target/Mips/MicroMipsInstrInfo.td
index dff57a5d59dc1..a4fa5e73b24df 100644
--- a/llvm/lib/Target/Mips/MicroMipsInstrInfo.td
+++ b/llvm/lib/Target/Mips/MicroMipsInstrInfo.td
@@ -1051,6 +1051,11 @@ let DecoderNamespace = "MicroMips" in {
def PAUSE_MM : MMRel, Barrier<"pause">, BARRIER_FM_MM<0x5>,
ISA_MICROMIPS;
+ def MFC0_MM : MFC3OP<"mfc0", GPR32Opnd, COP0Opnd>,
+ MFC0_FM_MM<0x0fc>, ISA_MICROMIPS32_NOT_MIPS32R6;
+ def MTC0_MM : MTC3OP<"mtc0", COP0Opnd, GPR32Opnd>,
+ MFC0_FM_MM<0x2fc>, ISA_MICROMIPS32_NOT_MIPS32R6;
+
def TLBP_MM : MMRel, TLB<"tlbp">, COP0_TLB_FM_MM<0x0d>,
ISA_MICROMIPS;
def TLBR_MM : MMRel, TLB<"tlbr">, COP0_TLB_FM_MM<0x4d>,
@@ -1287,6 +1292,12 @@ class UncondBranchMMPseudo<string opstr> :
def B_MM_Pseudo : UncondBranchMMPseudo<"b">, ISA_MICROMIPS;
let EncodingPredicates = [InMicroMips] in {
+ def : MipsInstAlias<"mfc0 $rt, $rd",
+ (MFC0_MM GPR32Opnd:$rt, COP0Opnd:$rd, 0), 0>,
+ ISA_MICROMIPS32_NOT_MIPS32R6;
+ def : MipsInstAlias<"mtc0 $rt, $rd",
+ (MTC0_MM COP0Opnd:$rd, GPR32Opnd:$rt, 0), 0>,
+ ISA_MICROMIPS32_NOT_MIPS32R6;
def SDIV_MM_Pseudo : MultDivPseudo<SDIV_MM, ACC64, GPR32Opnd, MipsDivRem,
0, 1, 1>, ISA_MIPS1_NOT_32R6_64R6;
def UDIV_MM_Pseudo : MultDivPseudo<UDIV_MM, ACC64, GPR32Opnd, MipsDivRemU,
diff --git a/llvm/lib/Target/Mips/MipsScheduleGeneric.td b/llvm/lib/Target/Mips/MipsScheduleGeneric.td
index d2526ee238f72..5ebe336b3443d 100644
--- a/llvm/lib/Target/Mips/MipsScheduleGeneric.td
+++ b/llvm/lib/Target/Mips/MipsScheduleGeneric.td
@@ -446,8 +446,8 @@ def GenericWriteCOP0Short : SchedWriteRes<[GenericIssueCOP0]>;
def : InstRW<[GenericWriteCOP0TLB], (instrs TLBP, TLBR, TLBWI, TLBWR)>;
def : InstRW<[GenericWriteCOP0TLB], (instrs TLBINV, TLBINVF)>;
-def : InstRW<[GenericReadCOP0], (instrs MFC0)>;
-def : InstRW<[GenericWriteCOP0], (instrs MTC0)>;
+def : InstRW<[GenericReadCOP0], (instrs MFC0, MFC0_MM)>;
+def : InstRW<[GenericWriteCOP0], (instrs MTC0, MTC0_MM)>;
def : InstRW<[GenericWriteCOP0], (instrs EVP, DVP)>;
diff --git a/llvm/test/MC/Mips/micromips-control-instructions.s b/llvm/test/MC/Mips/micromips-control-instructions.s
index e4946be5c34db..718e9217f0486 100644
--- a/llvm/test/MC/Mips/micromips-control-instructions.s
+++ b/llvm/test/MC/Mips/micromips-control-instructions.s
@@ -1,3 +1,5 @@
+# RUN: llvm-mc -triple=mipsel -mcpu=mips32r2 -mattr=+micromips -filetype=obj %s | llvm-objdump -d -j .text.cp0 --no-print-imm-hex --mattr=+micromips - | FileCheck %s --check-prefix=DIS
+# RUN: llvm-mc -triple=mips -mcpu=mips32r2 -mattr=+micromips -filetype=obj %s | llvm-objdump -d -j .text.cp0 --no-print-imm-hex --mattr=+micromips - | FileCheck %s --check-prefix=DIS
# RUN: llvm-mc %s -triple=mipsel -show-encoding -mcpu=mips32r2 -mattr=micromips -show-inst \
# RUN: | FileCheck -check-prefix=CHECK-EL %s
# RUN: llvm-mc %s -triple=mips -show-encoding -mcpu=mips32r2 -mattr=micromips -show-inst \
@@ -115,3 +117,20 @@
tlbwr
prefx 1, $3($5)
+ .section .text.cp0,"ax", at progbits
+# CHECK-EL: mfc0 $2, $12, 0 # encoding: [0x4c,0x00,0xfc,0x00]
+# CHECK-EB: mfc0 $2, $12, 0 # encoding: [0x00,0x4c,0x00,0xfc]
+# DIS: mfc0 $2, $12, 0
+ mfc0 $2, $12
+# CHECK-EL: mfc0 $3, $16, 7 # encoding: [0x70,0x00,0xfc,0x38]
+# CHECK-EB: mfc0 $3, $16, 7 # encoding: [0x00,0x70,0x38,0xfc]
+# DIS: mfc0 $3, $16, 7
+ mfc0 $3, $16, 7
+# CHECK-EL: mtc0 $4, $12, 0 # encoding: [0x8c,0x00,0xfc,0x02]
+# CHECK-EB: mtc0 $4, $12, 0 # encoding: [0x00,0x8c,0x02,0xfc]
+# DIS: mtc0 $4, $12, 0
+ mtc0 $4, $12
+# CHECK-EL: mtc0 $5, $16, 7 # encoding: [0xb0,0x00,0xfc,0x3a]
+# CHECK-EB: mtc0 $5, $16, 7 # encoding: [0x00,0xb0,0x3a,0xfc]
+# DIS: mtc0 $5, $16, 7
+ mtc0 $5, $16, 7
More information about the llvm-commits
mailing list