Skip to content

Commit c31a5c5

Browse files
sophie-zhaoabner-chenc
authored andcommitted
cmd/compile: fold negation into addition/subtraction on loong64
This change also avoid double negation, and add loong64 codegen for arithmetic tests. Reduce the number of go toolchain instructions on loong64 as follows. file before after Δ % addr2line 279972 279896 -76 -0.0271% asm 556390 556310 -80 -0.0144% buildid 272376 272300 -76 -0.0279% cgo 481534 481550 +16 +0.0033% compile 2457992 2457396 -596 -0.0242% covdata 323488 323404 -84 -0.0260% cover 518630 518490 -140 -0.0270% dist 340894 340814 -80 -0.0235% distpack 282568 282484 -84 -0.0297% doc 790224 789984 -240 -0.0304% fix 324408 324348 -60 -0.0185% link 704910 704666 -244 -0.0346% nm 277220 277144 -76 -0.0274% objdump 508026 507878 -148 -0.0291% pack 221810 221786 -24 -0.0108% pprof 1470284 1469880 -404 -0.0275% test2json 254896 254852 -44 -0.0173% trace 1100390 1100074 -316 -0.0287% vet 781398 781142 -256 -0.0328% go 1529668 1529128 -540 -0.0353% gofmt 318668 318568 -100 -0.0314% total 13795746 13792094 -3652 -0.0265% Change-Id: I88d1f12cfc4be0e92687c48e06a57213aa484aca Reviewed-on: https://go-review.googlesource.com/c/go/+/672555 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: abner chenc <chenguoqi@loongson.cn> Reviewed-by: Michael Knyszek <mknyszek@google.com>
1 parent de86d02 commit c31a5c5

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,12 +776,22 @@
776776

777777
// generic simplifications
778778
(ADDV x (NEGV y)) => (SUBV x y)
779+
(SUBV x (NEGV y)) => (ADDV x y)
779780
(SUBV x x) => (MOVVconst [0])
780781
(SUBV (MOVVconst [0]) x) => (NEGV x)
781782
(AND x x) => x
782783
(OR x x) => x
783784
(XOR x x) => (MOVVconst [0])
784785

786+
// Fold negation into subtraction.
787+
(NEGV (SUBV x y)) => (SUBV y x)
788+
(NEGV <t> s:(ADDVconst [c] (SUBV x y))) && s.Uses == 1 && is12Bit(-c) => (ADDVconst [-c] (SUBV <t> y x))
789+
790+
// Double negation.
791+
(NEGV (NEGV x)) => x
792+
// Fold NEGV into ADDVconst. Take care to keep c in 12 bit range.
793+
(NEGV <t> s:(ADDVconst [c] (NEGV x))) && s.Uses == 1 && is12Bit(-c) => (ADDVconst [-c] x)
794+
785795
// remove redundant *const ops
786796
(ADDVconst [0] x) => x
787797
(SUBVconst [0] x) => x

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ func isU8Bit(n int64) bool {
524524
return n == int64(uint8(n))
525525
}
526526

527+
// is12Bit reports whether n can be represented as a signed 12 bit integer.
528+
func is12Bit(n int64) bool {
529+
return -(1<<11) <= n && n < (1<<11)
530+
}
531+
527532
// isU12Bit reports whether n can be represented as an unsigned 12 bit integer.
528533
func isU12Bit(n int64) bool {
529534
return 0 <= n && n < (1<<12)

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

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

test/codegen/arithmetic.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ func SubFromConst(a int) int {
9191
}
9292

9393
func SubFromConstNeg(a int) int {
94+
// loong64: "ADDV[U]\t\\$40"
9495
// ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
9596
// riscv64: "ADDI\t\\$40",-"NEG"
9697
c := 40 - (-a)
9798
return c
9899
}
99100

100101
func SubSubFromConst(a int) int {
102+
// loong64: "ADDV[U]\t\\$20"
101103
// ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
102104
// riscv64: "ADDI\t\\$20",-"NEG"
103105
c := 40 - (20 - a)
@@ -112,13 +114,15 @@ func AddSubFromConst(a int) int {
112114
}
113115

114116
func NegSubFromConst(a int) int {
117+
// loong64: "ADDV[U]\t\\$-20"
115118
// ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
116119
// riscv64: "ADDI\t\\$-20"
117120
c := -(20 - a)
118121
return c
119122
}
120123

121124
func NegAddFromConstNeg(a int) int {
125+
// loong64: "ADDV[U]\t\\$-40","SUBV"
122126
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
123127
// riscv64: "ADDI\t\\$-40","NEG"
124128
c := -(-40 + a)
@@ -127,6 +131,7 @@ func NegAddFromConstNeg(a int) int {
127131

128132
func SubSubNegSimplify(a, b int) int {
129133
// amd64:"NEGQ"
134+
// loong64:"SUBV"
130135
// ppc64x:"NEG"
131136
// riscv64:"NEG",-"SUB"
132137
r := (a - b) - a
@@ -135,6 +140,7 @@ func SubSubNegSimplify(a, b int) int {
135140

136141
func SubAddSimplify(a, b int) int {
137142
// amd64:-"SUBQ",-"ADDQ"
143+
// loong64:-"SUBV",-"ADDV"
138144
// ppc64x:-"SUB",-"ADD"
139145
// riscv64:-"SUB",-"ADD"
140146
r := a + (b - a)
@@ -143,6 +149,7 @@ func SubAddSimplify(a, b int) int {
143149

144150
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
145151
// amd64:-"ADDQ"
152+
// loong64:"SUBV",-"ADDV"
146153
r := (a + b) - (a + c)
147154
// amd64:-"ADDQ"
148155
r1 := (a + b) - (c + a)
@@ -151,6 +158,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
151158
// amd64:-"ADDQ"
152159
r3 := (b + a) - (c + a)
153160
// amd64:-"SUBQ"
161+
// loong64:"ADDV",-"SUBV"
154162
r4 := (a - c) + (c + b)
155163
// amd64:-"SUBQ"
156164
r5 := (a - c) + (b + c)
@@ -159,6 +167,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
159167

160168
func SubAddNegSimplify(a, b int) int {
161169
// amd64:"NEGQ",-"ADDQ",-"SUBQ"
170+
// loong64:"SUBV",-"ADDV"
162171
// ppc64x:"NEG",-"ADD",-"SUB"
163172
// riscv64:"NEG",-"ADD",-"SUB"
164173
r := a - (b + a)
@@ -167,6 +176,7 @@ func SubAddNegSimplify(a, b int) int {
167176

168177
func AddAddSubSimplify(a, b, c int) int {
169178
// amd64:-"SUBQ"
179+
// loong64:"ADDV",-"SUBV"
170180
// ppc64x:-"SUB"
171181
// riscv64:"ADD","ADD",-"SUB"
172182
r := a + (b + (c - a))

0 commit comments

Comments
 (0)