Skip to content

Commit e126129

Browse files
committed
cmd/compile/internal/ssa: combine shift and addition for riscv64 rva22u64
When GORISCV64 enables rva22u64, combined shift and addition using the SH1ADD, SH2ADD and SH3ADD instructions that are available via the Zba extension. This results in more than 2000 instructions being removed from the Go binary on riscv64. Change-Id: Ia62ae7dda3d8083cff315113421bee73f518eea8 Reviewed-on: https://go-review.googlesource.com/c/go/+/606636 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mark Ryan <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Meng Zhuo <[email protected]>
1 parent aeac0b6 commit e126129

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed

src/cmd/compile/internal/riscv64/ssa.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
289289
ssa.OpRISCV64FEQS, ssa.OpRISCV64FNES, ssa.OpRISCV64FLTS, ssa.OpRISCV64FLES,
290290
ssa.OpRISCV64FADDD, ssa.OpRISCV64FSUBD, ssa.OpRISCV64FMULD, ssa.OpRISCV64FDIVD,
291291
ssa.OpRISCV64FEQD, ssa.OpRISCV64FNED, ssa.OpRISCV64FLTD, ssa.OpRISCV64FLED, ssa.OpRISCV64FSGNJD,
292-
ssa.OpRISCV64MIN, ssa.OpRISCV64MAX, ssa.OpRISCV64MINU, ssa.OpRISCV64MAXU:
292+
ssa.OpRISCV64MIN, ssa.OpRISCV64MAX, ssa.OpRISCV64MINU, ssa.OpRISCV64MAXU,
293+
ssa.OpRISCV64SH1ADD, ssa.OpRISCV64SH2ADD, ssa.OpRISCV64SH3ADD:
293294
r := v.Reg()
294295
r1 := v.Args[0].Reg()
295296
r2 := v.Args[1].Reg()

src/cmd/compile/internal/ssa/_gen/RISCV64.rules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,11 @@
839839
// Optimisations for rva22u64 and above.
840840
//
841841

842+
// Combine left shift and addition.
843+
(ADD (SLLI [1] x) y) && buildcfg.GORISCV64 >= 22 => (SH1ADD x y)
844+
(ADD (SLLI [2] x) y) && buildcfg.GORISCV64 >= 22 => (SH2ADD x y)
845+
(ADD (SLLI [3] x) y) && buildcfg.GORISCV64 >= 22 => (SH3ADD x y)
846+
842847
// Integer minimum and maximum.
843848
(Min64 x y) && buildcfg.GORISCV64 >= 22 => (MIN x y)
844849
(Max64 x y) && buildcfg.GORISCV64 >= 22 => (MAX x y)

src/cmd/compile/internal/ssa/_gen/RISCV64Ops.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ func init() {
220220
{name: "SRLI", argLength: 1, reg: gp11, asm: "SRLI", aux: "Int64"}, // arg0 >> auxint, shift amount 0-63, logical right shift
221221
{name: "SRLIW", argLength: 1, reg: gp11, asm: "SRLIW", aux: "Int64"}, // arg0 >> auxint, shift amount 0-31, logical right shift of 32 bit value, sign extended to 64 bits
222222

223+
// Shift and add
224+
{name: "SH1ADD", argLength: 2, reg: gp21, asm: "SH1ADD"}, // arg0 << 1 + arg1
225+
{name: "SH2ADD", argLength: 2, reg: gp21, asm: "SH2ADD"}, // arg0 << 2 + arg1
226+
{name: "SH3ADD", argLength: 2, reg: gp21, asm: "SH3ADD"}, // arg0 << 3 + arg1
227+
223228
// Bitwise ops
224229
{name: "AND", argLength: 2, reg: gp21, asm: "AND", commutative: true}, // arg0 & arg1
225230
{name: "ANDI", argLength: 1, reg: gp11, asm: "ANDI", aux: "Int64"}, // arg0 & auxint

src/cmd/compile/internal/ssa/opGen.go

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/rewriteRISCV64.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/shift.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,20 @@ func checkShiftToMask(u []uint64, s []int64) {
520520
// amd64:-"SHR",-"SHL","ANDQ"
521521
u[1] = u[1] << 5 >> 5
522522
}
523+
524+
//
525+
// Left shift with addition.
526+
//
527+
528+
func checkLeftShiftWithAddition(a int64, b int64) int64 {
529+
// riscv64/rva20u64: "SLLI","ADD"
530+
// riscv64/rva22u64: "SH1ADD"
531+
a = a + b<<1
532+
// riscv64/rva20u64: "SLLI","ADD"
533+
// riscv64/rva22u64: "SH2ADD"
534+
a = a + b<<2
535+
// riscv64/rva20u64: "SLLI","ADD"
536+
// riscv64/rva22u64: "SH3ADD"
537+
a = a + b<<3
538+
return a
539+
}

0 commit comments

Comments
 (0)