[committed] MIPS/GAS: Reinstate INSN_MULT interlock optimization

Maciej W. Rozycki macro@orcam.me.uk
Sun Nov 15 11:12:30 GMT 2026


Only integer division instructions in the Toshiba TX39 processor are 
unsafe to be preceded by a HI and/or LO register read operation as the 
first or second instruction ahead.

This optimization used to be there in GAS, but was silently dropped with 
commit 71400594f26d ("Rework MIPS nop-insertion code, add -mfix-vr4130"), 
<https://inbox.sourceware.org/binutils/wvnfyz6ch5y.fsf_-_@talisman.cambridge.redhat.com/>, 
causing unnecesary NOPs to be emitted ahead of multiplication opcodes.  
The INSN_MULT flag used in the opcode table to mark eligible entries was 
left behind, unused, but the GAS parts were removed.

Reinstate the missing parts, observing that unlike with other opcode 
flags INSN_MULT uses negative logic and marks entries that don't imply 
rather than do a data dependency.  Therefore INSN_MULT needs to be 
cleared rather than set when no previous instruction is available in 
`insns_between'.

Otherwise filler NOPs would fail to be produced for DIV, DIVU, MULO, 
MULOU macros preceded by an instruction that reads the HI and/or LO 
register, showing as R3900 test regressions:

FAIL: MIPS div (r3900)
FAIL: MIPS div with traps (r3900)
FAIL: MIPS mul (r3900)
FAIL: MIPS mul with traps (r3900)

This peculiarity will be removed with the next change.

Adjust the testsuite for the optimization now re-enabled.

Update the comment in `insns_between' saying that this optimization does 
not apply for most targets since this has long become not the case.
---
 gas/config/tc-mips.c                       |   22 +++++++---
 gas/testsuite/gas/mips/allegrex@mul-trap.d |    2 
 gas/testsuite/gas/mips/allegrex@mul.d      |   59 -----------------------------
 gas/testsuite/gas/mips/r10000@mul.d        |    2 
 gas/testsuite/gas/mips/r3900@mul-trap.d    |    2 
 gas/testsuite/gas/mips/r3900@mul.d         |   59 ++++++++++++++++++++++++++++-
 6 files changed, 78 insertions(+), 68 deletions(-)

binutils-mips-insn-mult.diff
Index: binutils-gdb/gas/config/tc-mips.c
===================================================================
--- binutils-gdb.orig/gas/config/tc-mips.c
+++ binutils-gdb/gas/config/tc-mips.c
@@ -584,6 +584,13 @@ static int mips_32bitmode = 0;
    || mips_opts.micromips                             \
    )
 
+/* True if MFLO and MFHI can be immediately followed by multiplication
+   instructions.  This applies to instructions marked INSN_MULT.  */
+#define hilo_mul_interlocks				\
+  (hilo_interlocks					\
+   || mips_opts.arch == CPU_R3900			\
+   )
+
 /* Whether the processor uses hardware interlocks to protect reads
    from the GPRs after they are loaded from memory, and thus does not
    require nops to be inserted.  This applies to instructions marked
@@ -6702,14 +6709,17 @@ insns_between (const struct mips_cl_insn
   unsigned long pinfo1, pinfo2;
   unsigned int mask;
 
-  /* If INFO2 is null, pessimistically assume that all flags are set for
-     the second instruction.  */
+  /* If INFO2 is null, pessimistically assume that all flags except for
+     INSN_MULT are set for the second instruction.  We need to exclude
+     this flag because, exceptionally, it marks instructions that do not
+     have a pipeline hazard avoidance requirement.  */
   pinfo1 = insn1->insn_mo->pinfo;
-  pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
+  pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~(unsigned long) INSN_MULT;
 
-  /* For most targets, write-after-read dependencies on the HI and LO
-     registers must be separated by at least two instructions.  */
-  if (!hilo_interlocks)
+  /* For some legacy ISA targets, write-after-read dependencies on the HI
+     and LO registers must be separated by at least two instructions.  */
+  if (!(hilo_interlocks
+	|| (hilo_mul_interlocks && (pinfo2 & INSN_MULT))))
     {
       if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
 	return 2;
Index: binutils-gdb/gas/testsuite/gas/mips/allegrex@mul-trap.d
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/allegrex@mul-trap.d
+++ binutils-gdb/gas/testsuite/gas/mips/allegrex@mul-trap.d
@@ -2,4 +2,4 @@
 #objdump: -dr --prefix-addresses
 #name: MIPS mul with traps
 #source: mul.s
-#dump: allegrex@mul.d
+#dump: r3900@mul.d
Index: binutils-gdb/gas/testsuite/gas/mips/allegrex@mul.d
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/allegrex@mul.d
+++ binutils-gdb/gas/testsuite/gas/mips/allegrex@mul.d
@@ -2,61 +2,4 @@
 #objdump: -dr --prefix-addresses
 #name: MIPS mul
 #source: mul.s
-
-# Test the mul macro.
-
-.*: +file format .*mips.*
-
-Disassembly of section .text:
-0+0000 <[^>]*> multu	a0,a1
-0+0004 <[^>]*> mflo	a0
-0+0008 <[^>]*> multu	a1,a2
-0+000c <[^>]*> mflo	a0
-0+0010 <[^>]*> li	at,0
-0+0014 <[^>]*> mult	a1,at
-0+0018 <[^>]*> mflo	a0
-0+001c <[^>]*> li	at,1
-0+0020 <[^>]*> mult	a1,at
-0+0024 <[^>]*> mflo	a0
-0+0028 <[^>]*> li	at,0x8000
-0+002c <[^>]*> mult	a1,at
-0+0030 <[^>]*> mflo	a0
-0+0034 <[^>]*> li	at,-32768
-0+0038 <[^>]*> mult	a1,at
-0+003c <[^>]*> mflo	a0
-0+0040 <[^>]*> lui	at,0x1
-0+0044 <[^>]*> mult	a1,at
-0+0048 <[^>]*> mflo	a0
-0+004c <[^>]*> lui	at,0x1
-0+0050 <[^>]*> ori	at,at,0xa5a5
-0+0054 <[^>]*> mult	a1,at
-0+0058 <[^>]*> mflo	a0
-0+005c <[^>]*> mult	a0,a1
-0+0060 <[^>]*> mflo	a0
-0+0064 <[^>]*> sra	a0,a0,0x1f
-0+0068 <[^>]*> mfhi	at
-0+006c <[^>]*> beq	a0,at,0+78 <foo\+(0x|)78>
-0+0070 <[^>]*> nop
-0+0074 <[^>]*> break	(0x0,0x6|0x6)
-0+0078 <[^>]*> mflo	a0
-0+007c <[^>]*> mult	a1,a2
-0+0080 <[^>]*> mflo	a0
-0+0084 <[^>]*> sra	a0,a0,0x1f
-0+0088 <[^>]*> mfhi	at
-0+008c <[^>]*> beq	a0,at,0+98 <foo\+(0x|)98>
-0+0090 <[^>]*> nop
-0+0094 <[^>]*> break	(0x0,0x6|0x6)
-0+0098 <[^>]*> mflo	a0
-0+009c <[^>]*> multu	a0,a1
-0+00a0 <[^>]*> mfhi	at
-0+00a4 <[^>]*> mflo	a0
-0+00a8 <[^>]*> beqz	at,0+b4 <foo\+(0x|)b4>
-0+00ac <[^>]*> nop
-0+00b0 <[^>]*> break	(0x0,0x6|0x6)
-0+00b4 <[^>]*> multu	a1,a2
-0+00b8 <[^>]*> mfhi	at
-0+00bc <[^>]*> mflo	a0
-0+00c0 <[^>]*> beqz	at,0+cc <foo\+(0x|)cc>
-0+00c4 <[^>]*> nop
-0+00c8 <[^>]*> break	(0x0,0x6|0x6)
-	...
+#dump: r3900@mul.d
Index: binutils-gdb/gas/testsuite/gas/mips/r10000@mul.d
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/r10000@mul.d
+++ binutils-gdb/gas/testsuite/gas/mips/r10000@mul.d
@@ -2,4 +2,4 @@
 #objdump: -dr --prefix-addresses
 #name: MIPS mul
 #source: mul.s
-#dump: allegrex@mul.d
+#dump: r3900@mul.d
Index: binutils-gdb/gas/testsuite/gas/mips/r3900@mul-trap.d
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/r3900@mul-trap.d
+++ binutils-gdb/gas/testsuite/gas/mips/r3900@mul-trap.d
@@ -2,4 +2,4 @@
 #objdump: -dr --prefix-addresses
 #name: MIPS mul with traps
 #source: mul.s
-#dump: mips1@mul.d
+#dump: r3900@mul.d
Index: binutils-gdb/gas/testsuite/gas/mips/r3900@mul.d
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/r3900@mul.d
+++ binutils-gdb/gas/testsuite/gas/mips/r3900@mul.d
@@ -2,4 +2,61 @@
 #objdump: -dr --prefix-addresses
 #name: MIPS mul
 #source: mul.s
-#dump: mips1@mul.d
+
+# Test the mul macro.
+
+.*: +file format .*mips.*
+
+Disassembly of section .text:
+0+0000 <[^>]*> multu	a0,a1
+0+0004 <[^>]*> mflo	a0
+0+0008 <[^>]*> multu	a1,a2
+0+000c <[^>]*> mflo	a0
+0+0010 <[^>]*> li	at,0
+0+0014 <[^>]*> mult	a1,at
+0+0018 <[^>]*> mflo	a0
+0+001c <[^>]*> li	at,1
+0+0020 <[^>]*> mult	a1,at
+0+0024 <[^>]*> mflo	a0
+0+0028 <[^>]*> li	at,0x8000
+0+002c <[^>]*> mult	a1,at
+0+0030 <[^>]*> mflo	a0
+0+0034 <[^>]*> li	at,-32768
+0+0038 <[^>]*> mult	a1,at
+0+003c <[^>]*> mflo	a0
+0+0040 <[^>]*> lui	at,0x1
+0+0044 <[^>]*> mult	a1,at
+0+0048 <[^>]*> mflo	a0
+0+004c <[^>]*> lui	at,0x1
+0+0050 <[^>]*> ori	at,at,0xa5a5
+0+0054 <[^>]*> mult	a1,at
+0+0058 <[^>]*> mflo	a0
+0+005c <[^>]*> mult	a0,a1
+0+0060 <[^>]*> mflo	a0
+0+0064 <[^>]*> sra	a0,a0,0x1f
+0+0068 <[^>]*> mfhi	at
+0+006c <[^>]*> beq	a0,at,0+78 <foo\+(0x|)78>
+0+0070 <[^>]*> nop
+0+0074 <[^>]*> break	(0x0,0x6|0x6)
+0+0078 <[^>]*> mflo	a0
+0+007c <[^>]*> mult	a1,a2
+0+0080 <[^>]*> mflo	a0
+0+0084 <[^>]*> sra	a0,a0,0x1f
+0+0088 <[^>]*> mfhi	at
+0+008c <[^>]*> beq	a0,at,0+98 <foo\+(0x|)98>
+0+0090 <[^>]*> nop
+0+0094 <[^>]*> break	(0x0,0x6|0x6)
+0+0098 <[^>]*> mflo	a0
+0+009c <[^>]*> multu	a0,a1
+0+00a0 <[^>]*> mfhi	at
+0+00a4 <[^>]*> mflo	a0
+0+00a8 <[^>]*> beqz	at,0+b4 <foo\+(0x|)b4>
+0+00ac <[^>]*> nop
+0+00b0 <[^>]*> break	(0x0,0x6|0x6)
+0+00b4 <[^>]*> multu	a1,a2
+0+00b8 <[^>]*> mfhi	at
+0+00bc <[^>]*> mflo	a0
+0+00c0 <[^>]*> beqz	at,0+cc <foo\+(0x|)cc>
+0+00c4 <[^>]*> nop
+0+00c8 <[^>]*> break	(0x0,0x6|0x6)
+	...


More information about the Binutils mailing list