On 10/30/26 7:41 AM, exuro wrote:
From: Loeka Rogge <[email protected]>
RPX-100 allows a mulh(s/u) followed by a mul operation to be bonded,
saving 3 cycles in the multiplier and allowing the mul's result to be
used 1 cycle earlier than expected.
- Adjust pipeline multiplier reservations for bonded case. Typically
mulh(s/u) is always emitted together with mul, otherwise this is a bit
optimistic.
- Define_expand <us>mulditi3 is modified to emit the high part first for
rpx100
- Added RISCV_FUSE_BONDED_MUL fusion
- riscv_sched_adjust_cost reflects the mul result being available 1
cycle sooner in this case
Regtested on rv32gc, rv64gc and with rhx100 config and mtune.
gcc/ChangeLog:
* config/riscv/arcv-rhx100.md: Adjust mul reservation for
typical bonded case.
* config/riscv/riscv-cores.def (RISCV_TUNE): Make rpx100 a
separate tune instead of reusing rhx100 tune
* config/riscv/riscv-fusion.cc (riscv_set_is_multiply_high_pattern_p):
Check if a set is a mulh(s/u) pattern.
(riscv_get_mulh_operands): Get operands from a mulh(s/u)
pattern.
(riscv_fuse_bonded_mul): Check for bonded mul pairs to fuse.
(riscv_depends_on_lowpart_bonded_mul_p): check if dependency is
on low part of a bonded mul operation.
* config/riscv/riscv-protos.h (enum riscv_fusion_pairs): Add
RISCV_FUSE_BONDED_MUL fusion
(riscv_depends_on_lowpart_bonded_mul_p): Add function header
for use in riscv.cc .
* config/riscv/riscv.cc (riscv_sched_adjust_cost): Adjust cost
for rpx100 bonded mul.
* config/riscv/riscv.md: Change mul mulh emit order for if the
fusion is enabled.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/fusion-bonded-mul-1.c: New test.
Signed-off-by: Loeka Rogge <[email protected]>
---
/* Check the common RTL for ZEXTW, ZEXTWS and ZEXTH fusion. */
static bool
@@ -744,6 +776,111 @@ riscv_fuse_b_alui (rtx_insn *prev, rtx_insn *curr)
return false;
}
+/* Extract register operands and destination from multiply high-part pattern.
+ Operands are returned in the order of the assembly instruction. The usmul
+ pattern emits the operands in the reverse order.
+ Returns true if successful. */
+
+static bool
+riscv_get_mulh_operands (rtx set_rtx, rtx *op0, rtx *op1, rtx *dest)
+{
+ if (!riscv_set_is_multiply_high_pattern_p (set_rtx))
+ return false;
+
+ rtx mulh_pattern = SET_SRC (set_rtx);
+
+ rtx lshiftrt = XEXP (mulh_pattern, 0);
+ rtx mult = XEXP (lshiftrt, 0);
+
+ rtx operand0 = XEXP (mult, 0);
+ rtx operand1 = XEXP (mult, 1);
+
+ /* Swap operands for usmul pattern. */
+ if (GET_CODE (operand0) == ZERO_EXTEND
+ && GET_CODE (operand1) == SIGN_EXTEND)
+ std::swap (operand0, operand1);
+
+ /* Strip zero_extend/sign_extend. */
+ if (GET_CODE (operand0) == ZERO_EXTEND || GET_CODE (operand0) == SIGN_EXTEND)
+ operand0 = XEXP (operand0, 0);
+ if (GET_CODE (operand1) == ZERO_EXTEND || GET_CODE (operand1) == SIGN_EXTEND)
+ operand1 = XEXP (operand1, 0);
+
+ /* Strip subreg. */
+ if (GET_CODE (operand0) == SUBREG)
+ operand0 = SUBREG_REG (operand0);
+ if (GET_CODE (operand1) == SUBREG)
+ operand1 = SUBREG_REG (operand1);
You should probably verify that you're getting the right part of the
subreg expression here. If you want the low word, here's
subreg_lowpart_p to verify that's what you're really getting.
+}
+
+/* Check for RISCV_FUSE_BONDED_MUL fusion.
+ prev (mulh/mulhu/mulhsu) and curr (mul) of the same inputs, with the
+ high-part destination distinct from both inputs. */
+
+static bool
+riscv_fuse_bonded_mul (rtx_insn *prev, rtx_insn *curr)
You should check that PREV is not marked for fusion. It's likely to be
marked, but better to be sure.
+
+bool
+riscv_depends_on_lowpart_bonded_mul_p (int dep_type, rtx_insn *dep_insn)
+{
+ if (dep_type != REG_DEP_TRUE)
+ return false;
+
+ rtx_insn *prev_insn = prev_nondebug_insn (dep_insn);
I think you want prev_nonnote_nondebug_insn here. I think you can see
things like NOTE_INSN_DELETED_DEBUG_LABEL and such here.
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index eb188ff5514..c1493193a53 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -1285,10 +1285,18 @@ (define_expand "<u>mulditi3"
"(TARGET_ZMMUL || TARGET_MUL) && TARGET_64BIT"
{
rtx low = gen_reg_rtx (DImode);
- emit_insn (gen_muldi3 (low, operands[1], operands[2]));
-
rtx high = gen_reg_rtx (DImode);
- emit_insn (gen_<su>muldi3_highpart (high, operands[1], operands[2]));
+
+ if (riscv_get_fusible_ops () & RISCV_FUSE_BONDED_MUL)
+ {
+ emit_insn (gen_<su>muldi3_highpart (high, operands[1], operands[2]));
+ emit_insn (gen_muldi3 (low, operands[1], operands[2]));
+ }
+ else
+ {
+ emit_insn (gen_muldi3 (low, operands[1], operands[2]));
+ emit_insn (gen_<su>muldi3_highpart (high, operands[1], operands[2]));
+ }
emit_move_insn (gen_lowpart (DImode, operands[0]), low);
emit_move_insn (gen_highpart (DImode, operands[0]), high);
@@ -1317,10 +1325,18 @@ (define_expand "usmulditi3"
"(TARGET_ZMMUL || TARGET_MUL) && TARGET_64BIT"
{
rtx low = gen_reg_rtx (DImode);
- emit_insn (gen_muldi3 (low, operands[1], operands[2]));
-
rtx high = gen_reg_rtx (DImode);
- emit_insn (gen_usmuldi3_highpart (high, operands[1], operands[2]));
+
+ if (riscv_get_fusible_ops () & RISCV_FUSE_BONDED_MUL)
+ {
+ emit_insn (gen_usmuldi3_highpart (high, operands[1], operands[2]));
+ emit_insn (gen_muldi3 (low, operands[2], operands[1]));
+ }
+ else
+ {
+ emit_insn (gen_muldi3 (low, operands[1], operands[2]));
+ emit_insn (gen_usmuldi3_highpart (high, operands[1], operands[2]));
+
Huh? If you're changing code generation in an expander based on FUSION
things, then you're likely doing something wrong. I'd suggest dropping
these hunks for now and returning to whatever caused you to want to add
it later. It's a sign you're tackling a problem in the wrong way.
Jeff