Skip to content

Commit d7a52f9

Browse files
committed
cmd/compile: use MOV(D|F) with const for Const(64|32)F on riscv64
The original Const64F using: AUIPC + LD + FMVDX to load float64 const, we can use AUIPC + FLD instead, same as Const32F. Change-Id: I8ca0a0e90d820a26e69b74cd25df3cc662132bf7 Reviewed-on: https://go-review.googlesource.com/c/go/+/703215 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Joel Sing <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 6f04a92 commit d7a52f9

File tree

7 files changed

+101
-76
lines changed

7 files changed

+101
-76
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,14 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
446446
p.From.Offset = v.AuxInt
447447
p.To.Type = obj.TYPE_REG
448448
p.To.Reg = v.Reg()
449+
case ssa.OpRISCV64FMOVDconst, ssa.OpRISCV64FMOVFconst:
450+
p := s.Prog(v.Op.Asm())
451+
p.From.Type = obj.TYPE_FCONST
452+
p.From.Val = v.AuxFloat()
453+
p.From.Name = obj.NAME_NONE
454+
p.From.Reg = obj.REG_NONE
455+
p.To.Type = obj.TYPE_REG
456+
p.To.Reg = v.Reg()
449457
case ssa.OpRISCV64MOVaddr:
450458
p := s.Prog(v.Op.Asm())
451459
p.From.Type = obj.TYPE_ADDR

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@
467467
(OffPtr [off] ptr) => (ADD (MOVDconst [off]) ptr)
468468

469469
(Const(64|32|16|8) [val]) => (MOVDconst [int64(val)])
470-
(Const32F [val]) => (FMVSX (MOVDconst [int64(math.Float32bits(val))]))
471-
(Const64F [val]) => (FMVDX (MOVDconst [int64(math.Float64bits(val))]))
470+
(Const(64|32)F ...) => (FMOV(D|F)const ...)
472471
(ConstNil) => (MOVDconst [0])
473472
(ConstBool [val]) => (MOVDconst [int64(b2i(val))])
474473

@@ -824,16 +823,16 @@
824823
(F(MADD|NMADD|MSUB|NMSUB)D x y neg:(FNEGD z)) && neg.Uses == 1 => (F(MSUB|NMSUB|MADD|NMADD)D x y z)
825824

826825
// Test for -∞ (bit 0) using 64 bit classify instruction.
827-
(FLTD x (FMVDX (MOVDconst [int64(math.Float64bits(-math.MaxFloat64))]))) => (ANDI [1] (FCLASSD x))
828-
(FLED (FMVDX (MOVDconst [int64(math.Float64bits(-math.MaxFloat64))])) x) => (SNEZ (ANDI <typ.Int64> [0xff &^ 1] (FCLASSD x)))
829-
(FEQD x (FMVDX (MOVDconst [int64(math.Float64bits(math.Inf(-1)))]))) => (ANDI [1] (FCLASSD x))
830-
(FNED x (FMVDX (MOVDconst [int64(math.Float64bits(math.Inf(-1)))]))) => (SEQZ (ANDI <typ.Int64> [1] (FCLASSD x)))
826+
(FLTD x (FMOVDconst [c])) && float64ExactBits(c, -math.MaxFloat64) => (ANDI [1] (FCLASSD x))
827+
(FLED (FMOVDconst [c]) x) && float64ExactBits(c, -math.MaxFloat64) => (SNEZ (ANDI <typ.Int64> [0xff &^ 1] (FCLASSD x)))
828+
(FEQD x (FMOVDconst [c])) && float64ExactBits(c, math.Inf(-1)) => (ANDI [1] (FCLASSD x))
829+
(FNED x (FMOVDconst [c])) && float64ExactBits(c, math.Inf(-1)) => (SEQZ (ANDI <typ.Int64> [1] (FCLASSD x)))
831830

832831
// Test for +∞ (bit 7) using 64 bit classify instruction.
833-
(FLTD (FMVDX (MOVDconst [int64(math.Float64bits(math.MaxFloat64))])) x) => (SNEZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
834-
(FLED x (FMVDX (MOVDconst [int64(math.Float64bits(math.MaxFloat64))]))) => (SNEZ (ANDI <typ.Int64> [0xff &^ (1<<7)] (FCLASSD x)))
835-
(FEQD x (FMVDX (MOVDconst [int64(math.Float64bits(math.Inf(1)))]))) => (SNEZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
836-
(FNED x (FMVDX (MOVDconst [int64(math.Float64bits(math.Inf(1)))]))) => (SEQZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
832+
(FLTD (FMOVDconst [c]) x) && float64ExactBits(c, math.MaxFloat64) => (SNEZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
833+
(FLED x (FMOVDconst [c])) && float64ExactBits(c, math.MaxFloat64) => (SNEZ (ANDI <typ.Int64> [0xff &^ (1<<7)] (FCLASSD x)))
834+
(FEQD x (FMOVDconst [c])) && float64ExactBits(c, math.Inf(1)) => (SNEZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
835+
(FNED x (FMOVDconst [c])) && float64ExactBits(c, math.Inf(1)) => (SEQZ (ANDI <typ.Int64> [1<<7] (FCLASSD x)))
837836

838837
//
839838
// Optimisations for rva22u64 and above.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func init() {
132132
gpcas = regInfo{inputs: []regMask{gpspsbgMask, gpgMask, gpgMask}, outputs: []regMask{gpMask}}
133133
gpatomic = regInfo{inputs: []regMask{gpspsbgMask, gpgMask}}
134134

135+
fp01 = regInfo{outputs: []regMask{fpMask}}
135136
fp11 = regInfo{inputs: []regMask{fpMask}, outputs: []regMask{fpMask}}
136137
fp21 = regInfo{inputs: []regMask{fpMask, fpMask}, outputs: []regMask{fpMask}}
137138
fp31 = regInfo{inputs: []regMask{fpMask, fpMask, fpMask}, outputs: []regMask{fpMask}}
@@ -176,7 +177,9 @@ func init() {
176177
{name: "MOVaddr", argLength: 1, reg: gp11sb, asm: "MOV", aux: "SymOff", rematerializeable: true, symEffect: "Addr"}, // arg0 + auxint + offset encoded in aux
177178
// auxint+aux == add auxint and the offset of the symbol in aux (if any) to the effective address
178179

179-
{name: "MOVDconst", reg: gp01, asm: "MOV", typ: "UInt64", aux: "Int64", rematerializeable: true}, // auxint
180+
{name: "MOVDconst", reg: gp01, asm: "MOV", typ: "UInt64", aux: "Int64", rematerializeable: true}, // auxint
181+
{name: "FMOVDconst", reg: fp01, asm: "MOVD", typ: "Float64", aux: "Float64", rematerializeable: true}, // auxint
182+
{name: "FMOVFconst", reg: fp01, asm: "MOVF", typ: "Float32", aux: "Float32", rematerializeable: true}, // auxint
180183

181184
// Loads: load <size> bits from arg0+auxint+aux and extend to 64 bits; arg1=mem
182185
{name: "MOVBload", argLength: 2, reg: gpload, asm: "MOVB", aux: "SymOff", typ: "Int8", faultOnNilArg0: true, symEffect: "Read"}, // 8 bits, sign extend

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

Lines changed: 26 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/rewrite.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,11 @@ func arm64ConditionalParamsToAuxInt(v arm64ConditionalParams) int64 {
757757
i |= int64(v.cond)
758758
return i
759759
}
760+
761+
func float64ExactBits(f float64, c float64) bool {
762+
return math.Float64bits(f) == math.Float64bits(c)
763+
}
764+
760765
func flagConstantToAuxInt(x flagConstant) int64 {
761766
return int64(x)
762767
}

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

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

test/codegen/floats.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ func Float64DenormalFloat32Constant() float64 {
268268

269269
func Float32ConstantStore(p *float32) {
270270
// amd64:"MOVL\t[$]1085133554"
271+
// riscv64: "MOVF\t[$]f32.40add2f2"
271272
*p = 5.432
272273
}
273274

274275
func Float64ConstantStore(p *float64) {
275-
// amd64:"MOVQ\t[$]4617801906721357038"
276+
// amd64: "MOVQ\t[$]4617801906721357038"
277+
// riscv64: "MOVD\t[$]f64.4015ba5e353f7cee"
276278
*p = 5.432
277279
}

0 commit comments

Comments
 (0)