Skip to content

Commit 268f462

Browse files
wdvxdr1123gopherbot
authored andcommitted
cmd/compile: enable brachelim pass on loong64
Change-Id: I4fd1c307901c265ab9865bf8a74460ddc15e5d14 Reviewed-on: https://go-review.googlesource.com/c/go/+/416735 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: xiaodong liu <[email protected]> Auto-Submit: Wayne Zuo <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: David Chase <[email protected]> Run-TryBot: Wayne Zuo <[email protected]>
1 parent 6b45863 commit 268f462

File tree

7 files changed

+127
-1
lines changed

7 files changed

+127
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,13 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
786786
p := s.Prog(obj.AGETCALLERPC)
787787
p.To.Type = obj.TYPE_REG
788788
p.To.Reg = v.Reg()
789+
case ssa.OpLOONG64MASKEQZ, ssa.OpLOONG64MASKNEZ:
790+
p := s.Prog(v.Op.Asm())
791+
p.From.Type = obj.TYPE_REG
792+
p.From.Reg = v.Args[1].Reg()
793+
p.Reg = v.Args[0].Reg()
794+
p.To.Type = obj.TYPE_REG
795+
p.To.Reg = v.Reg()
789796
case ssa.OpClobber, ssa.OpClobberReg:
790797
// TODO: implement for clobberdead experiment. Nop is ok for now.
791798
default:

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@
422422
(PanicBounds [kind] x y mem) && boundsABI(kind) == 1 => (LoweredPanicBoundsB [kind] x y mem)
423423
(PanicBounds [kind] x y mem) && boundsABI(kind) == 2 => (LoweredPanicBoundsC [kind] x y mem)
424424

425+
(CondSelect <t> x y cond) => (OR (MASKEQZ <t> x cond) (MASKNEZ <t> y cond))
426+
425427
// Optimizations
426428

427429
// Absorb boolean tests into block
@@ -615,6 +617,8 @@
615617
(ORconst [-1] _) => (MOVVconst [-1])
616618
(XORconst [0] x) => x
617619
(XORconst [-1] x) => (NORconst [0] x)
620+
(MASKEQZ (MOVVconst [0]) cond) => (MOVVconst [0])
621+
(MASKNEZ (MOVVconst [0]) cond) => (MOVVconst [0])
618622

619623
// generic constant folding
620624
(ADDVconst [c] (MOVVconst [d])) => (MOVVconst [c+d])

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ func init() {
192192
{name: "SQRTD", argLength: 1, reg: fp11, asm: "SQRTD"}, // sqrt(arg0), float64
193193
{name: "SQRTF", argLength: 1, reg: fp11, asm: "SQRTF"}, // sqrt(arg0), float32
194194

195+
{name: "MASKEQZ", argLength: 2, reg: gp21, asm: "MASKEQZ"}, // returns 0 if arg1 == 0, otherwise returns arg0
196+
{name: "MASKNEZ", argLength: 2, reg: gp21, asm: "MASKNEZ"}, // returns 0 if arg1 != 0, otherwise returns arg0
197+
195198
// shifts
196199
{name: "SLLV", argLength: 2, reg: gp21, asm: "SLLV"}, // arg0 << arg1, shift amount is mod 64
197200
{name: "SLLVconst", argLength: 1, reg: gp11, asm: "SLLV", aux: "Int64"}, // arg0 << auxInt

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import "cmd/internal/src"
2222
func branchelim(f *Func) {
2323
// FIXME: add support for lowering CondSelects on more architectures
2424
switch f.Config.arch {
25-
case "arm64", "ppc64le", "ppc64", "amd64", "wasm":
25+
case "arm64", "ppc64le", "ppc64", "amd64", "wasm", "loong64":
2626
// implemented
2727
default:
2828
return
@@ -83,6 +83,15 @@ func canCondSelect(v *Value, arch string, loadAddr *sparseSet) bool {
8383
// See issue #26306.
8484
return false
8585
}
86+
if arch == "loong64" {
87+
// We should not generate conditional moves if neither of the arguments is constant zero,
88+
// because it requires three instructions (OR, MASKEQZ, MASKNEZ) and will increase the
89+
// register pressure.
90+
if !(v.Args[0].isGenericIntConst() && v.Args[0].AuxInt == 0) &&
91+
!(v.Args[1].isGenericIntConst() && v.Args[1].AuxInt == 0) {
92+
return false
93+
}
94+
}
8695
// For now, stick to simple scalars that fit in registers
8796
switch {
8897
case v.Type.Size() > v.Block.Func.Config.RegSize:

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

Lines changed: 30 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/rewriteLOONG64.go

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

test/codegen/condmove.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,21 @@ func cmovFcmp1(s, t float64, a, b int) {
422422
// arm64:"CSINC\tEQ", -"CSEL"
423423
r5 = x5
424424
}
425+
426+
func cmovzero1(c bool) int {
427+
var x int
428+
if c {
429+
x = 182
430+
}
431+
// loong64:"MASKEQZ", -"MASKNEZ"
432+
return x
433+
}
434+
435+
func cmovzero2(c bool) int {
436+
var x int
437+
if !c {
438+
x = 182
439+
}
440+
// loong64:"MASKNEZ", -"MASKEQZ"
441+
return x
442+
}

0 commit comments

Comments
 (0)