Skip to content

Commit 4274ffd

Browse files
committed
cmd/compile: fold negation into subtraction on riscv64
Fold negation into subtraction and avoid double negation. This removes around 500 instructions from the Go binary on riscv64. Change-Id: I4aac6c87baa2a0759b180ba87876d488a23df6d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/431105 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Reviewed-by: Wayne Zuo <[email protected]> Run-TryBot: Joel Sing <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent ba8c94b commit 4274ffd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,13 @@
778778
(SUB (MOVDconst [0]) x) => (NEG x)
779779
(SUBW (MOVDconst [0]) x) => (NEGW x)
780780

781+
// Fold negation into subtraction.
782+
(NEG (SUB x y)) => (SUB y x)
783+
(NEG <t> s:(ADDI [val] (SUB x y))) && s.Uses == 1 && is32Bit(-val) => (ADDI [-val] (SUB <t> y x))
784+
785+
// Double negation.
786+
(NEG (NEG x)) => x
787+
781788
// Addition of zero or two constants.
782789
(ADDI [0] x) => x
783790
(ADDI [x] (MOVDconst [y])) && is32Bit(x + y) => (MOVDconst [x + y])

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

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

0 commit comments

Comments
 (0)