Skip to content

Commit 5216fd5

Browse files
committed
cmd/compile: move loong64 over to new bounds check strategy
Change-Id: I5dec33d10d16a5d5c0dc7231cd1f764a6d1d7598 Reviewed-on: https://go-review.googlesource.com/c/go/+/682399 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: abner chenc <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: sophie zhao <[email protected]>
1 parent 89a0af8 commit 5216fd5

File tree

6 files changed

+236
-161
lines changed

6 files changed

+236
-161
lines changed

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

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"cmd/compile/internal/types"
1717
"cmd/internal/obj"
1818
"cmd/internal/obj/loong64"
19+
"internal/abi"
1920
)
2021

2122
// isFPreg reports whether r is an FP register.
@@ -663,12 +664,92 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
663664
p.From.Type = obj.TYPE_CONST
664665
p.From.Offset = 0x1A
665666

666-
case ssa.OpLOONG64LoweredPanicBoundsA, ssa.OpLOONG64LoweredPanicBoundsB, ssa.OpLOONG64LoweredPanicBoundsC:
667-
p := s.Prog(obj.ACALL)
667+
case ssa.OpLOONG64LoweredPanicBoundsRR, ssa.OpLOONG64LoweredPanicBoundsRC, ssa.OpLOONG64LoweredPanicBoundsCR, ssa.OpLOONG64LoweredPanicBoundsCC:
668+
// Compute the constant we put in the PCData entry for this call.
669+
code, signed := ssa.BoundsKind(v.AuxInt).Code()
670+
xIsReg := false
671+
yIsReg := false
672+
xVal := 0
673+
yVal := 0
674+
switch v.Op {
675+
case ssa.OpLOONG64LoweredPanicBoundsRR:
676+
xIsReg = true
677+
xVal = int(v.Args[0].Reg() - loong64.REG_R4)
678+
yIsReg = true
679+
yVal = int(v.Args[1].Reg() - loong64.REG_R4)
680+
case ssa.OpLOONG64LoweredPanicBoundsRC:
681+
xIsReg = true
682+
xVal = int(v.Args[0].Reg() - loong64.REG_R4)
683+
c := v.Aux.(ssa.PanicBoundsC).C
684+
if c >= 0 && c <= abi.BoundsMaxConst {
685+
yVal = int(c)
686+
} else {
687+
// Move constant to a register
688+
yIsReg = true
689+
if yVal == xVal {
690+
yVal = 1
691+
}
692+
p := s.Prog(loong64.AMOVV)
693+
p.From.Type = obj.TYPE_CONST
694+
p.From.Offset = c
695+
p.To.Type = obj.TYPE_REG
696+
p.To.Reg = loong64.REG_R4 + int16(yVal)
697+
}
698+
case ssa.OpLOONG64LoweredPanicBoundsCR:
699+
yIsReg = true
700+
yVal := int(v.Args[0].Reg() - loong64.REG_R4)
701+
c := v.Aux.(ssa.PanicBoundsC).C
702+
if c >= 0 && c <= abi.BoundsMaxConst {
703+
xVal = int(c)
704+
} else {
705+
// Move constant to a register
706+
xIsReg = true
707+
if xVal == yVal {
708+
xVal = 1
709+
}
710+
p := s.Prog(loong64.AMOVV)
711+
p.From.Type = obj.TYPE_CONST
712+
p.From.Offset = c
713+
p.To.Type = obj.TYPE_REG
714+
p.To.Reg = loong64.REG_R4 + int16(xVal)
715+
}
716+
case ssa.OpLOONG64LoweredPanicBoundsCC:
717+
c := v.Aux.(ssa.PanicBoundsCC).Cx
718+
if c >= 0 && c <= abi.BoundsMaxConst {
719+
xVal = int(c)
720+
} else {
721+
// Move constant to a register
722+
xIsReg = true
723+
p := s.Prog(loong64.AMOVV)
724+
p.From.Type = obj.TYPE_CONST
725+
p.From.Offset = c
726+
p.To.Type = obj.TYPE_REG
727+
p.To.Reg = loong64.REG_R4 + int16(xVal)
728+
}
729+
c = v.Aux.(ssa.PanicBoundsCC).Cy
730+
if c >= 0 && c <= abi.BoundsMaxConst {
731+
yVal = int(c)
732+
} else {
733+
// Move constant to a register
734+
yIsReg = true
735+
yVal = 1
736+
p := s.Prog(loong64.AMOVV)
737+
p.From.Type = obj.TYPE_CONST
738+
p.From.Offset = c
739+
p.To.Type = obj.TYPE_REG
740+
p.To.Reg = loong64.REG_R4 + int16(yVal)
741+
}
742+
}
743+
c := abi.BoundsEncode(code, signed, xIsReg, yIsReg, xVal, yVal)
744+
745+
p := s.Prog(obj.APCDATA)
746+
p.From.SetConst(abi.PCDATA_PanicBounds)
747+
p.To.SetConst(int64(c))
748+
p = s.Prog(obj.ACALL)
668749
p.To.Type = obj.TYPE_MEM
669750
p.To.Name = obj.NAME_EXTERN
670-
p.To.Sym = ssagen.BoundsCheckFunc[v.AuxInt]
671-
s.UseArgs(16) // space used in callee args area by assembly stubs
751+
p.To.Sym = ir.Syms.PanicBounds
752+
672753
case ssa.OpLOONG64LoweredAtomicLoad8, ssa.OpLOONG64LoweredAtomicLoad32, ssa.OpLOONG64LoweredAtomicLoad64:
673754
// MOVB (Rarg0), Rout
674755
// DBAR 0x14

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,11 @@
527527
// Publication barrier as intrinsic
528528
(PubBarrier ...) => (LoweredPubBarrier ...)
529529

530-
(PanicBounds [kind] x y mem) && boundsABI(kind) == 0 => (LoweredPanicBoundsA [kind] x y mem)
531-
(PanicBounds [kind] x y mem) && boundsABI(kind) == 1 => (LoweredPanicBoundsB [kind] x y mem)
532-
(PanicBounds [kind] x y mem) && boundsABI(kind) == 2 => (LoweredPanicBoundsC [kind] x y mem)
530+
(PanicBounds ...) => (LoweredPanicBoundsRR ...)
531+
(LoweredPanicBoundsRR [kind] x (MOVVconst [c]) mem) => (LoweredPanicBoundsRC [kind] x {PanicBoundsC{C:c}} mem)
532+
(LoweredPanicBoundsRR [kind] (MOVVconst [c]) y mem) => (LoweredPanicBoundsCR [kind] {PanicBoundsC{C:c}} y mem)
533+
(LoweredPanicBoundsRC [kind] {p} (MOVVconst [c]) mem) => (LoweredPanicBoundsCC [kind] {PanicBoundsCC{Cx:c, Cy:p.C}} mem)
534+
(LoweredPanicBoundsCR [kind] {p} (MOVVconst [c]) mem) => (LoweredPanicBoundsCC [kind] {PanicBoundsCC{Cx:p.C, Cy:c}} mem)
533535

534536
(CondSelect <t> x y cond) => (OR (MASKEQZ <t> x cond) (MASKNEZ <t> y cond))
535537

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ func init() {
130130
gpspsbg = gpspg | buildReg("SB")
131131
fp = buildReg("F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24 F25 F26 F27 F28 F29 F30 F31")
132132
callerSave = gp | fp | buildReg("g") // runtime.setg (and anything calling it) may clobber g
133-
r1 = buildReg("R20")
134-
r2 = buildReg("R21")
135-
r3 = buildReg("R23")
136-
r4 = buildReg("R24")
133+
first16 = buildReg("R4 R5 R6 R7 R8 R9 R10 R11 R12 R13 R14 R15 R16 R17 R18 R19")
137134
)
138135
// Common regInfo
139136
var (
@@ -563,12 +560,15 @@ func init() {
563560
// Do data barrier. arg0=memorys
564561
{name: "LoweredPubBarrier", argLength: 1, asm: "DBAR", hasSideEffects: true},
565562

566-
// There are three of these functions so that they can have three different register inputs.
567-
// When we check 0 <= c <= cap (A), then 0 <= b <= c (B), then 0 <= a <= b (C), we want the
568-
// default registers to match so we don't need to copy registers around unnecessarily.
569-
{name: "LoweredPanicBoundsA", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{r3, r4}}, typ: "Mem", call: true}, // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
570-
{name: "LoweredPanicBoundsB", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{r2, r3}}, typ: "Mem", call: true}, // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
571-
{name: "LoweredPanicBoundsC", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{r1, r2}}, typ: "Mem", call: true}, // arg0=idx, arg1=len, arg2=mem, returns memory. AuxInt contains report code (see PanicBounds in genericOps.go).
563+
// LoweredPanicBoundsRR takes x and y, two values that caused a bounds check to fail.
564+
// the RC and CR versions are used when one of the arguments is a constant. CC is used
565+
// when both are constant (normally both 0, as prove derives the fact that a [0] bounds
566+
// failure means the length must have also been 0).
567+
// AuxInt contains a report code (see PanicBounds in genericOps.go).
568+
{name: "LoweredPanicBoundsRR", argLength: 3, aux: "Int64", reg: regInfo{inputs: []regMask{first16, first16}}, typ: "Mem", call: true}, // arg0=x, arg1=y, arg2=mem, returns memory.
569+
{name: "LoweredPanicBoundsRC", argLength: 2, aux: "PanicBoundsC", reg: regInfo{inputs: []regMask{first16}}, typ: "Mem", call: true}, // arg0=x, arg1=mem, returns memory.
570+
{name: "LoweredPanicBoundsCR", argLength: 2, aux: "PanicBoundsC", reg: regInfo{inputs: []regMask{first16}}, typ: "Mem", call: true}, // arg0=y, arg1=mem, returns memory.
571+
{name: "LoweredPanicBoundsCC", argLength: 1, aux: "PanicBoundsCC", reg: regInfo{}, typ: "Mem", call: true}, // arg0=mem, returns memory.
572572

573573
// Prefetch instruction
574574
// Do prefetch arg0 address with option aux. arg0=addr, arg1=memory, aux=option.

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

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

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

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

0 commit comments

Comments
 (0)