]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/amd64: improve fix up code for signed division
authorJoel Sing <joel@sing.id.au>
Wed, 5 Apr 2023 20:11:14 +0000 (06:11 +1000)
committerJoel Sing <joel@sing.id.au>
Sat, 8 Apr 2023 15:35:34 +0000 (15:35 +0000)
In order to avoid a CPU exception resulting from signed overflow, the signed
division code tests if the divisor is -1 and if it is, runs fix up code to
manually compute the quotient and remainder (thus avoiding IDIV and potential
signed overflow).

However, the way that this is currently structured means that the normal code
path for the case where the divisor is not -1 results in five instructions
and two branches (CMP, JEQ, followed by sign extension, IDIV and another JMP
to skip over the fix up code).

Rework the fix up code such that the final JMP is incurred by the less likely
divisor is -1 code path, rather than more likely code path (which is already
more expensive due to IDIV). This result in a four instruction sequence
(CMP, JNE, sign extension, IDIV), with only a single branch.

Updates #59089

Change-Id: Ie8d065750a178518d7397e194920b201afeb0530
Reviewed-on: https://go-review.googlesource.com/c/go/+/482658
Run-TryBot: Joel Sing <joel@sing.id.au>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/cmd/compile/internal/amd64/ssa.go

index f9a43af3611e582f423d6bf9706d048926431ac3..bf45fb2e45433cbd9d6a07e8bb138c68ec36b50e 100644 (file)
@@ -336,7 +336,6 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
                // Result[0] (the quotient) is in AX.
                // Result[1] (the remainder) is in DX.
                r := v.Args[1].Reg()
-               var j1 *obj.Prog
 
                var opCMP, opNEG, opSXD obj.As
                switch v.Op {
@@ -350,28 +349,19 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
 
                // CPU faults upon signed overflow, which occurs when the most
                // negative int is divided by -1. Handle divide by -1 as a special case.
+               var j1, j2 *obj.Prog
                if ssa.DivisionNeedsFixUp(v) {
                        c := s.Prog(opCMP)
                        c.From.Type = obj.TYPE_REG
                        c.From.Reg = r
                        c.To.Type = obj.TYPE_CONST
                        c.To.Offset = -1
-                       j1 = s.Prog(x86.AJEQ)
-                       j1.To.Type = obj.TYPE_BRANCH
-               }
-
-               // Sign extend dividend and perform division.
-               s.Prog(opSXD)
-               p := s.Prog(v.Op.Asm())
-               p.From.Type = obj.TYPE_REG
-               p.From.Reg = r
 
-               if j1 != nil {
-                       // Skip over -1 fixup code.
-                       j2 := s.Prog(obj.AJMP)
-                       j2.To.Type = obj.TYPE_BRANCH
+                       // Divisor is not -1, proceed with normal division.
+                       j1 = s.Prog(x86.AJNE)
+                       j1.To.Type = obj.TYPE_BRANCH
 
-                       // Issue -1 fixup code.
+                       // Divisor is -1, manually compute quotient and remainder via fixup code.
                        // n / -1 = -n
                        n1 := s.Prog(opNEG)
                        n1.To.Type = obj.TYPE_REG
@@ -383,7 +373,21 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
                        // TODO(khr): issue only the -1 fixup code we need.
                        // For instance, if only the quotient is used, no point in zeroing the remainder.
 
-                       j1.To.SetTarget(n1)
+                       // Skip over normal division.
+                       j2 = s.Prog(obj.AJMP)
+                       j2.To.Type = obj.TYPE_BRANCH
+               }
+
+               // Sign extend dividend and perform division.
+               p := s.Prog(opSXD)
+               if j1 != nil {
+                       j1.To.SetTarget(p)
+               }
+               p = s.Prog(v.Op.Asm())
+               p.From.Type = obj.TYPE_REG
+               p.From.Reg = r
+
+               if j2 != nil {
                        j2.To.SetTarget(s.Pc())
                }