[committed] MIPS: Correct Toshiba TX39 MADD/MADDU HI/LO instruction flags
Maciej W. Rozycki
macro@orcam.me.uk
Sun Nov 15 11:12:24 GMT 2026
Toshiba TX39 integer multiply-add instructions suffer from a pipeline
hazard on the HI and LO registers[1]:
"The value of the HI and LO registers are undefined for the two
instructions immediately preceding a DIV or DIVU instruction.
Accordingly, do not place a DIV or DIVU instruction as the next
or next-but-one instruction after a MADD, MADDU, MFHI, or MFLO
instruction that reads the HI or LO registers."
This is further asserted in the instruction list for MADD[2]:
"To guarantee correct operation even if an interrupt occurs, neither of
the two instructions following MADD should be DIV or DIVU instructions
which modify the HI and LO register contents."
and similarly for MADDU[3].
In GAS we already handle this hazard correctly in `insns_between', which
works for MFHI and MFLO instructions via their INSN_READ_HI/INSN_READ_LO
flags, however TX39 MADD and MADDU instructions only have INSN_WRITE_HI
and INSN_WRITE_LO flags set. Consequently the hazard is not observed.
Add the missing INSN_READ_HI/INSN_READ_LO flags then across the family,
so as to cause extra NOP instructions to be emitted as required to move
the instructions affected apart. Add test coverage.
References:
[1] "32-Bit TX System RISC TX39 Family Architecture", Toshiba
Corporation, Document Number 44137D-0001, Volume "Architecture",
Section 4.9 "Restrictions on Instruction Placement", p. 4-9
[2] same, Appendix A "Instruction Set Details", p. A-60
[3] same, p. A-61
---
gas/testsuite/gas/mips/madd.d | 24 ++++++++++++++++++++++++
gas/testsuite/gas/mips/madd.s | 14 ++++++++++++++
gas/testsuite/gas/mips/mips.exp | 4 ++++
opcodes/mips-opc.c | 8 ++++----
4 files changed, 46 insertions(+), 4 deletions(-)
binutils-mips-madd-mod-hilo.diff
Index: binutils-gdb/gas/testsuite/gas/mips/madd.d
===================================================================
--- /dev/null
+++ binutils-gdb/gas/testsuite/gas/mips/madd.d
@@ -0,0 +1,24 @@
+#as: -32
+#objdump: -drz --prefix-addresses --show-raw-insn
+#name: MIPS integer multiply-add hazard
+
+.*: +file format .*mips.*
+
+Disassembly of section \.text:
+[0-9a-f]+ <[^>]*> 70850000 madd a0,a1
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00c7001a div zero,a2,a3
+[0-9a-f]+ <[^>]*> 70851000 madd v0,a0,a1
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00c7001a div zero,a2,a3
+[0-9a-f]+ <[^>]*> 70850001 maddu a0,a1
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00c7001b divu zero,a2,a3
+[0-9a-f]+ <[^>]*> 70851001 maddu v0,a0,a1
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00000000 nop
+[0-9a-f]+ <[^>]*> 00c7001b divu zero,a2,a3
+#pass
Index: binutils-gdb/gas/testsuite/gas/mips/madd.s
===================================================================
--- /dev/null
+++ binutils-gdb/gas/testsuite/gas/mips/madd.s
@@ -0,0 +1,14 @@
+# Source file used to test the integer multiply-add hazard.
+
+ .text
+ .ent foo
+foo:
+ madd $4, $5
+ div $0, $6, $7
+ madd $2, $4, $5
+ div $0, $6, $7
+ maddu $4, $5
+ divu $0, $6, $7
+ maddu $2, $4, $5
+ divu $0, $6, $7
+ .end foo
Index: binutils-gdb/gas/testsuite/gas/mips/mips.exp
===================================================================
--- binutils-gdb.orig/gas/testsuite/gas/mips/mips.exp
+++ binutils-gdb/gas/testsuite/gas/mips/mips.exp
@@ -893,6 +893,10 @@ if { [istarget mips*-*-vxworks*] } {
run_dump_test "lifloat"
run_dump_test "lif-svr4pic"
run_dump_test "lif-xgot"
+
+ run_dump_test "madd" [list [list name (r3900)] \
+ [list as "-march=r3900"]]
+
run_dump_test_arches "mips4" [mips_arch_list_matching mips4]
run_dump_test_arches "mips4-fp" "-32" \
[mips_arch_list_matching fpisa4]
Index: binutils-gdb/opcodes/mips-opc.c
===================================================================
--- binutils-gdb.orig/opcodes/mips-opc.c
+++ binutils-gdb/opcodes/mips-opc.c
@@ -1375,18 +1375,18 @@ const struct mips_opcode mips_builtin_op
{"madd.ps", "D,S,T", 0x72c00018, 0xffe0003f, WR_1|RD_2|RD_3|FP_D, 0, IL2F, 0, 0 },
{"madd", "s,t", 0x0000001c, 0xfc00ffff, RD_1|RD_2|WR_HILO, 0, L1|ALX, 0, 0 },
{"madd", "s,t", 0x70000000, 0xfc00ffff, RD_1|RD_2|MOD_HILO, 0, I32|N55, 0, I37 },
-{"madd", "s,t", 0x70000000, 0xfc00ffff, RD_1|RD_2|WR_HILO|IS_M, 0, G1, 0, 0 },
+{"madd", "s,t", 0x70000000, 0xfc00ffff, RD_1|RD_2|MOD_HILO|IS_M, 0, G1, 0, 0 },
{"madd", "7,s,t", 0x70000000, 0xfc00e7ff, RD_2|RD_3|MOD_a, 0, 0, D32, 0 },
-{"madd", "d,s,t", 0x70000000, 0xfc0007ff, WR_1|RD_2|RD_3|WR_HILO|IS_M, 0, G1, 0, 0 },
+{"madd", "d,s,t", 0x70000000, 0xfc0007ff, WR_1|RD_2|RD_3|MOD_HILO|IS_M, 0, G1, 0, 0 },
{"madd1", "s,t", 0x70000020, 0xfc00ffff, RD_1|RD_2|WR_HILO|IS_M, 0, EE, 0, 0 },
{"madd1", "d,s,t", 0x70000020, 0xfc0007ff, WR_1|RD_2|RD_3|WR_HILO|IS_M, 0, EE, 0, 0 },
{"madda.s", "S,T", 0x4600001e, 0xffe007ff, RD_1|RD_2|FP_S, 0, EE, 0, 0 },
{"maddp", "s,t", 0x70000441, 0xfc00ffff, RD_1|RD_2|MOD_HILO, 0, 0, SMT, 0 },
{"maddu", "s,t", 0x0000001d, 0xfc00ffff, RD_1|RD_2|WR_HILO, 0, L1|ALX, 0, 0 },
{"maddu", "s,t", 0x70000001, 0xfc00ffff, RD_1|RD_2|MOD_HILO, 0, I32|N55, 0, I37 },
-{"maddu", "s,t", 0x70000001, 0xfc00ffff, RD_1|RD_2|WR_HILO|IS_M, 0, G1, 0, 0 },
+{"maddu", "s,t", 0x70000001, 0xfc00ffff, RD_1|RD_2|MOD_HILO|IS_M, 0, G1, 0, 0 },
{"maddu", "7,s,t", 0x70000001, 0xfc00e7ff, RD_2|RD_3|MOD_a, 0, 0, D32, 0 },
-{"maddu", "d,s,t", 0x70000001, 0xfc0007ff, WR_1|RD_2|RD_3|WR_HILO|IS_M, 0, G1, 0, 0 },
+{"maddu", "d,s,t", 0x70000001, 0xfc0007ff, WR_1|RD_2|RD_3|MOD_HILO|IS_M, 0, G1, 0, 0 },
{"maddu1", "s,t", 0x70000021, 0xfc00ffff, RD_1|RD_2|WR_HILO|IS_M, 0, EE, 0, 0 },
{"maddu1", "d,s,t", 0x70000021, 0xfc0007ff, WR_1|RD_2|RD_3|WR_HILO|IS_M, 0, EE, 0, 0 },
{"madd16", "s,t", 0x00000028, 0xfc00ffff, RD_1|RD_2|MOD_HILO, 0, N411, 0, 0 },
More information about the Binutils
mailing list