Skip to content

Commit

Permalink
cmd/compile: deadcode for LoweredMuluhilo on riscv64
Browse files Browse the repository at this point in the history
This is a follow up of CL 425101 on RISCV64.

According to RISCV Volume 1, Unprivileged Spec v. 20191213 Chapter 7.1:
If both the high and low bits of the same product are required, then the
recommended code sequence is: MULH[[S]U] rdh, rs1, rs2; MUL rdl, rs1, rs2
(source register specifiers must be in same order and rdh cannot be the
same as rs1 or rs2). Microarchitectures can then fuse these into a single
multiply operation instead of performing two separate multiplies.

So we should not split Muluhilo to separate instructions.

Updates golang#54607

Change-Id: If47461f3aaaf00e27cd583a9990e144fb8bcdb17
Reviewed-on: https://go-review.googlesource.com/c/go/+/425203
Auto-Submit: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Wayne Zuo <wdvxdr@golangcn.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
  • Loading branch information
wdvxdr1123 authored and Raj Barik committed Aug 29, 2022
1 parent f4b3427 commit 53b6bbd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cmd/compile/internal/ssa/gen/RISCV64.rules
Expand Up @@ -743,6 +743,10 @@
(SLTI [x] (MOVDconst [y])) => (MOVDconst [b2i(int64(y) < int64(x))])
(SLTIU [x] (MOVDconst [y])) => (MOVDconst [b2i(uint64(y) < uint64(x))])

// deadcode for LoweredMuluhilo
(Select0 m:(LoweredMuluhilo x y)) && m.Uses == 1 => (MULHU x y)
(Select1 m:(LoweredMuluhilo x y)) && m.Uses == 1 => (MUL x y)

// Merge negation into fused multiply-add and multiply-subtract.
//
// Key:
Expand Down
46 changes: 46 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteRISCV64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/codegen/mathbits.go
Expand Up @@ -800,12 +800,14 @@ func Mul64(x, y uint64) (hi, lo uint64) {

func Mul64HiOnly(x, y uint64) uint64 {
// arm64:"UMULH",-"MUL"
// riscv64:"MULHU",-"MUL\t"
hi, _ := bits.Mul64(x, y)
return hi
}

func Mul64LoOnly(x, y uint64) uint64 {
// arm64:"MUL",-"UMULH"
// riscv64:"MUL\t",-"MULHU"
_, lo := bits.Mul64(x, y)
return lo
}
Expand Down

0 comments on commit 53b6bbd

Please sign in to comment.