Skip to content

Commit ee522e2

Browse files
committed
cmd/compile/internal/amd64: improve fix up code for signed division
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 <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 2a41dbf commit ee522e2

File tree

1 file changed

+20
-16
lines changed
  • src/cmd/compile/internal/amd64

1 file changed

+20
-16
lines changed

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

+20-16
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
336336
// Result[0] (the quotient) is in AX.
337337
// Result[1] (the remainder) is in DX.
338338
r := v.Args[1].Reg()
339-
var j1 *obj.Prog
340339

341340
var opCMP, opNEG, opSXD obj.As
342341
switch v.Op {
@@ -350,28 +349,19 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
350349

351350
// CPU faults upon signed overflow, which occurs when the most
352351
// negative int is divided by -1. Handle divide by -1 as a special case.
352+
var j1, j2 *obj.Prog
353353
if ssa.DivisionNeedsFixUp(v) {
354354
c := s.Prog(opCMP)
355355
c.From.Type = obj.TYPE_REG
356356
c.From.Reg = r
357357
c.To.Type = obj.TYPE_CONST
358358
c.To.Offset = -1
359-
j1 = s.Prog(x86.AJEQ)
360-
j1.To.Type = obj.TYPE_BRANCH
361-
}
362-
363-
// Sign extend dividend and perform division.
364-
s.Prog(opSXD)
365-
p := s.Prog(v.Op.Asm())
366-
p.From.Type = obj.TYPE_REG
367-
p.From.Reg = r
368359

369-
if j1 != nil {
370-
// Skip over -1 fixup code.
371-
j2 := s.Prog(obj.AJMP)
372-
j2.To.Type = obj.TYPE_BRANCH
360+
// Divisor is not -1, proceed with normal division.
361+
j1 = s.Prog(x86.AJNE)
362+
j1.To.Type = obj.TYPE_BRANCH
373363

374-
// Issue -1 fixup code.
364+
// Divisor is -1, manually compute quotient and remainder via fixup code.
375365
// n / -1 = -n
376366
n1 := s.Prog(opNEG)
377367
n1.To.Type = obj.TYPE_REG
@@ -383,7 +373,21 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
383373
// TODO(khr): issue only the -1 fixup code we need.
384374
// For instance, if only the quotient is used, no point in zeroing the remainder.
385375

386-
j1.To.SetTarget(n1)
376+
// Skip over normal division.
377+
j2 = s.Prog(obj.AJMP)
378+
j2.To.Type = obj.TYPE_BRANCH
379+
}
380+
381+
// Sign extend dividend and perform division.
382+
p := s.Prog(opSXD)
383+
if j1 != nil {
384+
j1.To.SetTarget(p)
385+
}
386+
p = s.Prog(v.Op.Asm())
387+
p.From.Type = obj.TYPE_REG
388+
p.From.Reg = r
389+
390+
if j2 != nil {
387391
j2.To.SetTarget(s.Pc())
388392
}
389393

0 commit comments

Comments
 (0)