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>
This commit is contained in:
Xiaolin Zhao 2025-05-14 14:35:41 +08:00 committed by abner chenc
parent de86d02c32
commit c31a5c571f
4 changed files with 108 additions and 0 deletions

View file

@ -91,6 +91,7 @@ func SubFromConst(a int) int {
}
func SubFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$40"
// ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$40",-"NEG"
c := 40 - (-a)
@ -98,6 +99,7 @@ func SubFromConstNeg(a int) int {
}
func SubSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$20"
// ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$20",-"NEG"
c := 40 - (20 - a)
@ -112,6 +114,7 @@ func AddSubFromConst(a int) int {
}
func NegSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$-20"
// ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$-20"
c := -(20 - a)
@ -119,6 +122,7 @@ func NegSubFromConst(a int) int {
}
func NegAddFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$-40","SUBV"
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
// riscv64: "ADDI\t\\$-40","NEG"
c := -(-40 + a)
@ -127,6 +131,7 @@ func NegAddFromConstNeg(a int) int {
func SubSubNegSimplify(a, b int) int {
// amd64:"NEGQ"
// loong64:"SUBV"
// ppc64x:"NEG"
// riscv64:"NEG",-"SUB"
r := (a - b) - a
@ -135,6 +140,7 @@ func SubSubNegSimplify(a, b int) int {
func SubAddSimplify(a, b int) int {
// amd64:-"SUBQ",-"ADDQ"
// loong64:-"SUBV",-"ADDV"
// ppc64x:-"SUB",-"ADD"
// riscv64:-"SUB",-"ADD"
r := a + (b - a)
@ -143,6 +149,7 @@ func SubAddSimplify(a, b int) int {
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ"
// loong64:"SUBV",-"ADDV"
r := (a + b) - (a + c)
// amd64:-"ADDQ"
r1 := (a + b) - (c + a)
@ -151,6 +158,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ"
r3 := (b + a) - (c + a)
// amd64:-"SUBQ"
// loong64:"ADDV",-"SUBV"
r4 := (a - c) + (c + b)
// amd64:-"SUBQ"
r5 := (a - c) + (b + c)
@ -159,6 +167,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
func SubAddNegSimplify(a, b int) int {
// amd64:"NEGQ",-"ADDQ",-"SUBQ"
// loong64:"SUBV",-"ADDV"
// ppc64x:"NEG",-"ADD",-"SUB"
// riscv64:"NEG",-"ADD",-"SUB"
r := a - (b + a)
@ -167,6 +176,7 @@ func SubAddNegSimplify(a, b int) int {
func AddAddSubSimplify(a, b, c int) int {
// amd64:-"SUBQ"
// loong64:"ADDV",-"SUBV"
// ppc64x:-"SUB"
// riscv64:"ADD","ADD",-"SUB"
r := a + (b + (c - a))