]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: combine negation and word sign extension on riscv64
authorJoel Sing <joel@sing.id.au>
Wed, 12 Oct 2022 06:37:23 +0000 (17:37 +1100)
committerJoel Sing <joel@sing.id.au>
Sat, 15 Mar 2025 13:05:16 +0000 (06:05 -0700)
Use NEGW to produce a negated and sign extended word, rather than doing
the same via two instructions:

   neg     t0, t0
   sext.w  a0, t0

Becomes:

   negw    t0, t0

Change-Id: I824ab25001bd3304bdbd435e7b244fcc036ef212
Reviewed-on: https://go-review.googlesource.com/c/go/+/652319
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mark Ryan <markdryan@rivosinc.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/cmd/compile/internal/ssa/_gen/RISCV64.rules
src/cmd/compile/internal/ssa/rewriteRISCV64.go
test/codegen/arithmetic.go

index 5c9ce19773f7ddb91427f1a57f9e632eb545ce13..770a9095f6d1c3f063bbb77fd39467f01168b3f6 100644 (file)
 (MOVHUreg (ANDI [c] x)) && c < 0 => (ANDI [int64(uint16(c))] x)
 (MOVWUreg (ANDI [c] x)) && c < 0 => (AND (MOVDconst [int64(uint32(c))]) x)
 
+// Combine negation and sign extension.
+(MOVWreg (NEG x)) => (NEGW x)
+
 // Avoid sign/zero extension for consts.
 (MOVBreg  (MOVDconst [c])) => (MOVDconst [int64(int8(c))])
 (MOVHreg  (MOVDconst [c])) => (MOVDconst [int64(int16(c))])
index eb0480e08032e2ab514bd127cffe66acb84b3fc0..e19e28ea239b625cf0f0ee4bc4b23f725491c45c 100644 (file)
@@ -5644,6 +5644,17 @@ func rewriteValueRISCV64_OpRISCV64MOVWreg(v *Value) bool {
                v.copyOf(x)
                return true
        }
+       // match: (MOVWreg (NEG x))
+       // result: (NEGW x)
+       for {
+               if v_0.Op != OpRISCV64NEG {
+                       break
+               }
+               x := v_0.Args[0]
+               v.reset(OpRISCV64NEGW)
+               v.AddArg(x)
+               return true
+       }
        // match: (MOVWreg (MOVDconst [c]))
        // result: (MOVDconst [int64(int32(c))])
        for {
index 07fd0c961fe96326f2982a63dc88d507e8336771..530891293e02fb47d8406f2e96a466d589a6653f 100644 (file)
@@ -174,7 +174,7 @@ func AddAddSubSimplify(a, b, c int) int {
 }
 
 func NegToInt32(a int) int {
-       // riscv64: "NEG","MOVW"
+       // riscv64: "NEGW",-"MOVW"
        r := int(int32(-a))
        return r
 }