Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile/internal/ssa: use sequence of shNadd instructions instead of mul #71179

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
cmd/compile/internal/ssa: use sequence of shNadd instructions instead…
… of mul

In some cases, we can replace a mul instruction with a sequence of shNadd instructions.
Using a sequence of three shNadd instructions does not provide a performance benefit.
However, a sequence of two shNadd instructions is still faster than using a single mul instruction.

Co-Authored-By: Andrei <therain.i@yahoo.com>
vacmannnn and therain7 committed Jan 8, 2025
commit ef1cca481527816a1c6f4f8b6108c5d657ed1f91
18 changes: 18 additions & 0 deletions src/cmd/compile/internal/ssa/_gen/RISCV64.rules
Original file line number Diff line number Diff line change
@@ -843,6 +843,24 @@
(ADD (SLLI [2] x) y) && buildcfg.GORISCV64 >= 22 => (SH2ADD x y)
(ADD (SLLI [3] x) y) && buildcfg.GORISCV64 >= 22 => (SH3ADD x y)

// Mul on some constants
(MUL x (MOVDconst [3])) && buildcfg.GORISCV64 >= 22 => (SH1ADD x x)
(MUL x (MOVDconst [5])) && buildcfg.GORISCV64 >= 22 => (SH2ADD x x)
(MUL x (MOVDconst [9])) && buildcfg.GORISCV64 >= 22 => (SH3ADD x x)

(MUL <t> x (MOVDconst [11])) && buildcfg.GORISCV64 >= 22 => (SH1ADD (SH2ADD <t> x x) x)
(MUL <t> x (MOVDconst [13])) && buildcfg.GORISCV64 >= 22 => (SH2ADD (SH1ADD <t> x x) x)
(MUL <t> x (MOVDconst [19])) && buildcfg.GORISCV64 >= 22 => (SH1ADD (SH3ADD <t> x x) x)
(MUL <t> x (MOVDconst [21])) && buildcfg.GORISCV64 >= 22 => (SH2ADD (SH2ADD <t> x x) x)
(MUL <t> x (MOVDconst [25])) && buildcfg.GORISCV64 >= 22 => (SH3ADD (SH1ADD <t> x x) x)
(MUL <t> x (MOVDconst [27])) && buildcfg.GORISCV64 >= 22 => (SH1ADD (SH3ADD <t> x x) (SH3ADD <t> x x))
(MUL <t> x (MOVDconst [37])) && buildcfg.GORISCV64 >= 22 => (SH2ADD (SH3ADD <t> x x) x)
(MUL <t> x (MOVDconst [41])) && buildcfg.GORISCV64 >= 22 => (SH3ADD (SH2ADD <t> x x) x)
(MUL <t> x (MOVDconst [45])) && buildcfg.GORISCV64 >= 22 => (SH2ADD (SH3ADD <t> x x) (SH3ADD <t> x x))
(MUL <t> x (MOVDconst [73])) && buildcfg.GORISCV64 >= 22 => (SH3ADD (SH3ADD <t> x x) x)
(MUL <t> x (MOVDconst [81])) && buildcfg.GORISCV64 >= 22 => (SH3ADD (SH3ADD <t> x x) (SH3ADD <t> x x))


// Integer minimum and maximum.
(Min64 x y) && buildcfg.GORISCV64 >= 22 => (MIN x y)
(Max64 x y) && buildcfg.GORISCV64 >= 22 => (MAX x y)
251 changes: 251 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.

23 changes: 23 additions & 0 deletions test/codegen/shift.go
Original file line number Diff line number Diff line change
@@ -531,3 +531,26 @@ func checkLeftShiftWithAddition(a int64, b int64) int64 {
a = a + b<<3
return a
}

//
// Multiplication by some constants
//

func checkMulByConsts(a int64, b int64, c int64, d int64, e int64) (int64, int64, int64, int64, int64) {
// riscv64/rva20u64: "MUL"
// riscv64/rva22u64: "SH1ADD"
a = a * 3
// riscv64/rva20u64: "MUL"
// riscv64/rva22u64: "SH2ADD"
b = b * 5
// riscv64/rva20u64: "MUL"
// riscv64/rva22u64: "SH2ADD", "SH1ADD"
c = c * 13
// riscv64/rva20u64: "MUL"
// riscv64/rva22u64: "SH1ADD", "SH3ADD"
d = d * 27
// riscv64/rva20u64: "MUL"
// riscv64/rva22u64: "SH3ADD", "SH3ADD"
e = e * 73
return a, b, c, d, e
}