aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2025-09-30 16:27:32 -0400
committerDavid Chase <drchase@google.com>2025-10-09 08:23:30 -0700
commit78d75b37992be01326b9bd2666195aaba9bf2ae2 (patch)
treeb63e9a0f0079973aadd9018cc21de898bd86d724 /src/cmd/compile
parent0e466a8d1d89e15e953c7d35bcd9e02d3c89f62b (diff)
downloadgo-78d75b37992be01326b9bd2666195aaba9bf2ae2.tar.xz
cmd/compile: make 386 float-to-int conversions match amd64
Change-Id: Iff13b4471f94a6a91d8b159603a9338cb9c89747 Reviewed-on: https://go-review.googlesource.com/c/go/+/708295 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd/compile')
-rw-r--r--src/cmd/compile/internal/ssa/_gen/386.rules6
-rw-r--r--src/cmd/compile/internal/ssa/_gen/386Ops.go2
-rw-r--r--src/cmd/compile/internal/ssa/opGen.go13
-rw-r--r--src/cmd/compile/internal/ssa/rewrite386.go99
-rw-r--r--src/cmd/compile/internal/x86/ssa.go7
5 files changed, 121 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/ssa/_gen/386.rules b/src/cmd/compile/internal/ssa/_gen/386.rules
index 5f11502419..4e3d3203c7 100644
--- a/src/cmd/compile/internal/ssa/_gen/386.rules
+++ b/src/cmd/compile/internal/ssa/_gen/386.rules
@@ -88,8 +88,10 @@
(Cvt32to32F ...) => (CVTSL2SS ...)
(Cvt32to64F ...) => (CVTSL2SD ...)
-(Cvt32Fto32 ...) => (CVTTSS2SL ...)
-(Cvt64Fto32 ...) => (CVTTSD2SL ...)
+(Cvt32Fto32 <t> x) && base.ConvertHash.MatchPos(v.Pos, nil) => (XORL <t> y (SARLconst <t> [31] (ANDL <t> y:(CVTTSS2SL <t> x) (NOTL <typ.Int32> (MOVLf2i x)))))
+(Cvt64Fto32 <t> x) && base.ConvertHash.MatchPos(v.Pos, nil) => (XORL <t> y (SARLconst <t> [31] (ANDL <t> y:(CVTTSD2SL <t> x) (NOTL <typ.Int32> (MOVLf2i (CVTSD2SS <typ.Float32> x))))))
+(Cvt32Fto32 <t> x) && !base.ConvertHash.MatchPos(v.Pos, nil) => (CVTTSS2SL <t> x)
+(Cvt64Fto32 <t> x) && !base.ConvertHash.MatchPos(v.Pos, nil) => (CVTTSD2SL <t> x)
(Cvt32Fto64F ...) => (CVTSS2SD ...)
(Cvt64Fto32F ...) => (CVTSD2SS ...)
diff --git a/src/cmd/compile/internal/ssa/_gen/386Ops.go b/src/cmd/compile/internal/ssa/_gen/386Ops.go
index 60599a33ab..86c2f9c8f0 100644
--- a/src/cmd/compile/internal/ssa/_gen/386Ops.go
+++ b/src/cmd/compile/internal/ssa/_gen/386Ops.go
@@ -342,6 +342,8 @@ func init() {
{name: "MOVWLSX", argLength: 1, reg: gp11, asm: "MOVWLSX"}, // sign extend arg0 from int16 to int32
{name: "MOVWLZX", argLength: 1, reg: gp11, asm: "MOVWLZX"}, // zero extend arg0 from int16 to int32
+ {name: "MOVLf2i", argLength: 1, reg: fpgp, typ: "UInt32"}, // move 32 bits from float to int reg, zero extend
+
{name: "MOVLconst", reg: gp01, asm: "MOVL", typ: "UInt32", aux: "Int32", rematerializeable: true}, // 32 low bits of auxint
{name: "CVTTSD2SL", argLength: 1, reg: fpgp, asm: "CVTTSD2SL"}, // convert float64 to int32
diff --git a/src/cmd/compile/internal/ssa/opGen.go b/src/cmd/compile/internal/ssa/opGen.go
index 92adf5341b..2a60398bc6 100644
--- a/src/cmd/compile/internal/ssa/opGen.go
+++ b/src/cmd/compile/internal/ssa/opGen.go
@@ -499,6 +499,7 @@ const (
Op386MOVBLZX
Op386MOVWLSX
Op386MOVWLZX
+ Op386MOVLf2i
Op386MOVLconst
Op386CVTTSD2SL
Op386CVTTSS2SL
@@ -5602,6 +5603,18 @@ var opcodeTable = [...]opInfo{
},
},
{
+ name: "MOVLf2i",
+ argLen: 1,
+ reg: regInfo{
+ inputs: []inputInfo{
+ {0, 65280}, // X0 X1 X2 X3 X4 X5 X6 X7
+ },
+ outputs: []outputInfo{
+ {0, 239}, // AX CX DX BX BP SI DI
+ },
+ },
+ },
+ {
name: "MOVLconst",
auxType: auxInt32,
argLen: 0,
diff --git a/src/cmd/compile/internal/ssa/rewrite386.go b/src/cmd/compile/internal/ssa/rewrite386.go
index 0495438710..4845f1e025 100644
--- a/src/cmd/compile/internal/ssa/rewrite386.go
+++ b/src/cmd/compile/internal/ssa/rewrite386.go
@@ -3,6 +3,7 @@
package ssa
import "math"
+import "cmd/compile/internal/base"
import "cmd/compile/internal/types"
func rewriteValue386(v *Value) bool {
@@ -340,8 +341,7 @@ func rewriteValue386(v *Value) bool {
v.Op = Op386BSFL
return true
case OpCvt32Fto32:
- v.Op = Op386CVTTSS2SL
- return true
+ return rewriteValue386_OpCvt32Fto32(v)
case OpCvt32Fto64F:
v.Op = Op386CVTSS2SD
return true
@@ -352,8 +352,7 @@ func rewriteValue386(v *Value) bool {
v.Op = Op386CVTSL2SD
return true
case OpCvt64Fto32:
- v.Op = Op386CVTTSD2SL
- return true
+ return rewriteValue386_OpCvt64Fto32(v)
case OpCvt64Fto32F:
v.Op = Op386CVTSD2SS
return true
@@ -7964,6 +7963,98 @@ func rewriteValue386_OpCtz8(v *Value) bool {
return true
}
}
+func rewriteValue386_OpCvt32Fto32(v *Value) bool {
+ v_0 := v.Args[0]
+ b := v.Block
+ typ := &b.Func.Config.Types
+ // match: (Cvt32Fto32 <t> x)
+ // cond: base.ConvertHash.MatchPos(v.Pos, nil)
+ // result: (XORL <t> y (SARLconst <t> [31] (ANDL <t> y:(CVTTSS2SL <t> x) (NOTL <typ.Int32> (MOVLf2i x)))))
+ for {
+ t := v.Type
+ x := v_0
+ if !(base.ConvertHash.MatchPos(v.Pos, nil)) {
+ break
+ }
+ v.reset(Op386XORL)
+ v.Type = t
+ v0 := b.NewValue0(v.Pos, Op386SARLconst, t)
+ v0.AuxInt = int32ToAuxInt(31)
+ v1 := b.NewValue0(v.Pos, Op386ANDL, t)
+ y := b.NewValue0(v.Pos, Op386CVTTSS2SL, t)
+ y.AddArg(x)
+ v3 := b.NewValue0(v.Pos, Op386NOTL, typ.Int32)
+ v4 := b.NewValue0(v.Pos, Op386MOVLf2i, typ.UInt32)
+ v4.AddArg(x)
+ v3.AddArg(v4)
+ v1.AddArg2(y, v3)
+ v0.AddArg(v1)
+ v.AddArg2(y, v0)
+ return true
+ }
+ // match: (Cvt32Fto32 <t> x)
+ // cond: !base.ConvertHash.MatchPos(v.Pos, nil)
+ // result: (CVTTSS2SL <t> x)
+ for {
+ t := v.Type
+ x := v_0
+ if !(!base.ConvertHash.MatchPos(v.Pos, nil)) {
+ break
+ }
+ v.reset(Op386CVTTSS2SL)
+ v.Type = t
+ v.AddArg(x)
+ return true
+ }
+ return false
+}
+func rewriteValue386_OpCvt64Fto32(v *Value) bool {
+ v_0 := v.Args[0]
+ b := v.Block
+ typ := &b.Func.Config.Types
+ // match: (Cvt64Fto32 <t> x)
+ // cond: base.ConvertHash.MatchPos(v.Pos, nil)
+ // result: (XORL <t> y (SARLconst <t> [31] (ANDL <t> y:(CVTTSD2SL <t> x) (NOTL <typ.Int32> (MOVLf2i (CVTSD2SS <typ.Float32> x))))))
+ for {
+ t := v.Type
+ x := v_0
+ if !(base.ConvertHash.MatchPos(v.Pos, nil)) {
+ break
+ }
+ v.reset(Op386XORL)
+ v.Type = t
+ v0 := b.NewValue0(v.Pos, Op386SARLconst, t)
+ v0.AuxInt = int32ToAuxInt(31)
+ v1 := b.NewValue0(v.Pos, Op386ANDL, t)
+ y := b.NewValue0(v.Pos, Op386CVTTSD2SL, t)
+ y.AddArg(x)
+ v3 := b.NewValue0(v.Pos, Op386NOTL, typ.Int32)
+ v4 := b.NewValue0(v.Pos, Op386MOVLf2i, typ.UInt32)
+ v5 := b.NewValue0(v.Pos, Op386CVTSD2SS, typ.Float32)
+ v5.AddArg(x)
+ v4.AddArg(v5)
+ v3.AddArg(v4)
+ v1.AddArg2(y, v3)
+ v0.AddArg(v1)
+ v.AddArg2(y, v0)
+ return true
+ }
+ // match: (Cvt64Fto32 <t> x)
+ // cond: !base.ConvertHash.MatchPos(v.Pos, nil)
+ // result: (CVTTSD2SL <t> x)
+ for {
+ t := v.Type
+ x := v_0
+ if !(!base.ConvertHash.MatchPos(v.Pos, nil)) {
+ break
+ }
+ v.reset(Op386CVTTSD2SL)
+ v.Type = t
+ v.AddArg(x)
+ return true
+ }
+ return false
+}
func rewriteValue386_OpDiv8(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
diff --git a/src/cmd/compile/internal/x86/ssa.go b/src/cmd/compile/internal/x86/ssa.go
index d0aad08849..2858a81b4b 100644
--- a/src/cmd/compile/internal/x86/ssa.go
+++ b/src/cmd/compile/internal/x86/ssa.go
@@ -538,6 +538,13 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
p.To.Type = obj.TYPE_MEM
p.To.Reg = v.Args[0].Reg()
ssagen.AddAux(&p.To, v)
+ case ssa.Op386MOVLf2i:
+ var p *obj.Prog
+ p = s.Prog(x86.AMOVL)
+ p.From.Type = obj.TYPE_REG
+ p.From.Reg = v.Args[0].Reg()
+ p.To.Type = obj.TYPE_REG
+ p.To.Reg = v.Reg()
case ssa.Op386ADDLconstmodify:
sc := v.AuxValAndOff()
val := sc.Val()