Skip to content

Commit

Permalink
cmd/compile: wire up Bswap/ReverseBytes intrinsics for loong64
Browse files Browse the repository at this point in the history
Updates golang#59120

Change-Id: I39c1edbd7363f454ad1e848a25abeced722b16ac
  • Loading branch information
xen0n committed Mar 20, 2023
1 parent 54f6862 commit 28c3b2a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/cmd/compile/internal/loong64/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,9 @@ func ssaGenValue(s *ssagen.State, v *ssa.Value) {
ssa.OpLOONG64CTOV,
ssa.OpLOONG64CLOW,
ssa.OpLOONG64CLOV,
ssa.OpLOONG64REVB2H,
ssa.OpLOONG64REVB2W,
ssa.OpLOONG64REVBV,
ssa.OpLOONG64SQRTD,
ssa.OpLOONG64SQRTF:
p := s.Prog(v.Op.Asm())
Expand Down
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ssa/_gen/LOONG64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
(Ctz(8|16|32|64)NonZero ...) => (Ctz(8|16|32|64) ...)
(BitLen64 <t> x) => (NEGV <t> (SUBVconst <t> [64] (CLZV <t> x)))
(BitLen32 <t> x) => (NEGV <t> (SUBVconst <t> [32] (CLZW <t> x)))
(Bswap(16|32|64) ...) => (REVB(2H|2W|V) ...)

(Com(64|32|16|8) x) => (NOR (MOVVconst [0]) x)
(NOR (MOVVconst [0]) (C(TZW|TZV|LZW|LZV) x)) => (C(TOW|TOV|LOW|LOV) x)
Expand Down
29 changes: 16 additions & 13 deletions src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,22 @@ func init() {
{name: "NOR", argLength: 2, reg: gp21, asm: "NOR", commutative: true}, // ^(arg0 | arg1)
{name: "NORconst", argLength: 1, reg: gp11, asm: "NOR", aux: "Int64"}, // ^(arg0 | auxInt)

{name: "NEGV", argLength: 1, reg: gp11}, // -arg0
{name: "NEGF", argLength: 1, reg: fp11, asm: "NEGF"}, // -arg0, float32
{name: "NEGD", argLength: 1, reg: fp11, asm: "NEGD"}, // -arg0, float64
{name: "SQRTD", argLength: 1, reg: fp11, asm: "SQRTD"}, // sqrt(arg0), float64
{name: "SQRTF", argLength: 1, reg: fp11, asm: "SQRTF"}, // sqrt(arg0), float32
{name: "CTZW", argLength: 1, reg: gp11, asm: "CTZW"}, // Count trailing (low order) zeroes (returns 0-32)
{name: "CTZV", argLength: 1, reg: gp11, asm: "CTZV"}, // Count trailing (low order) zeroes (returns 0-64)
{name: "CLZW", argLength: 1, reg: gp11, asm: "CLZW"}, // Count leading (high order) zeroes (returns 0-32)
{name: "CLZV", argLength: 1, reg: gp11, asm: "CLZV"}, // Count leading (high order) zeroes (returns 0-64)
{name: "CTOW", argLength: 1, reg: gp11, asm: "CTOW"}, // Count trailing (low order) ones (returns 0-32)
{name: "CTOV", argLength: 1, reg: gp11, asm: "CTOV"}, // Count trailing (low order) ones (returns 0-64)
{name: "CLOW", argLength: 1, reg: gp11, asm: "CLOW"}, // Count leading (high order) ones (returns 0-32)
{name: "CLOV", argLength: 1, reg: gp11, asm: "CLOV"}, // Count leading (high order) ones (returns 0-64)
{name: "NEGV", argLength: 1, reg: gp11}, // -arg0
{name: "NEGF", argLength: 1, reg: fp11, asm: "NEGF"}, // -arg0, float32
{name: "NEGD", argLength: 1, reg: fp11, asm: "NEGD"}, // -arg0, float64
{name: "SQRTD", argLength: 1, reg: fp11, asm: "SQRTD"}, // sqrt(arg0), float64
{name: "SQRTF", argLength: 1, reg: fp11, asm: "SQRTF"}, // sqrt(arg0), float32
{name: "CTZW", argLength: 1, reg: gp11, asm: "CTZW"}, // Count trailing (low order) zeroes (returns 0-32)
{name: "CTZV", argLength: 1, reg: gp11, asm: "CTZV"}, // Count trailing (low order) zeroes (returns 0-64)
{name: "CLZW", argLength: 1, reg: gp11, asm: "CLZW"}, // Count leading (high order) zeroes (returns 0-32)
{name: "CLZV", argLength: 1, reg: gp11, asm: "CLZV"}, // Count leading (high order) zeroes (returns 0-64)
{name: "CTOW", argLength: 1, reg: gp11, asm: "CTOW"}, // Count trailing (low order) ones (returns 0-32)
{name: "CTOV", argLength: 1, reg: gp11, asm: "CTOV"}, // Count trailing (low order) ones (returns 0-64)
{name: "CLOW", argLength: 1, reg: gp11, asm: "CLOW"}, // Count leading (high order) ones (returns 0-32)
{name: "CLOV", argLength: 1, reg: gp11, asm: "CLOV"}, // Count leading (high order) ones (returns 0-64)
{name: "REVB2H", argLength: 1, reg: gp11, asm: "REVB2H"}, // Swap bytes: 0x11223344 -> 0x22114433 (sign extends to 64 bits)
{name: "REVB2W", argLength: 1, reg: gp11, asm: "REVB2W"}, // Swap bytes: 0x1122334455667788 -> 0x4433221188776655
{name: "REVBV", argLength: 1, reg: gp11, asm: "REVBV"}, // Swap bytes: 0x1122334455667788 -> 0x8877665544332211

{name: "MASKEQZ", argLength: 2, reg: gp21, asm: "MASKEQZ"}, // returns 0 if arg1 == 0, otherwise returns arg0
{name: "MASKNEZ", argLength: 2, reg: gp21, asm: "MASKNEZ"}, // returns 0 if arg1 != 0, otherwise returns arg0
Expand Down
42 changes: 42 additions & 0 deletions src/cmd/compile/internal/ssa/opGen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteLOONG64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/cmd/compile/internal/ssagen/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,7 @@ func InitTables() {
},
sys.ARM64, sys.PPC64)

brev_arch := []sys.ArchFamily{sys.AMD64, sys.I386, sys.ARM64, sys.ARM, sys.S390X}
brev_arch := []sys.ArchFamily{sys.AMD64, sys.I386, sys.ARM64, sys.ARM, sys.Loong64, sys.S390X}
if buildcfg.GOPPC64 >= 10 {
// Use only on Power10 as the new byte reverse instructions that Power10 provide
// make it worthwhile as an intrinsic
Expand Down Expand Up @@ -4542,6 +4542,11 @@ func InitTables() {
sys.Loong64, sys.S390X)
alias("math/bits", "ReverseBytes64", "runtime/internal/sys", "Bswap64", all...)
alias("math/bits", "ReverseBytes32", "runtime/internal/sys", "Bswap32", all...)
addF("math/bits", "ReverseBytes16",
func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value {
return s.newValue1(ssa.OpBswap16, types.Types[types.TUINT16], args[0])
},
sys.Loong64)
// ReverseBytes inlines correctly, no need to intrinsify it.
// Nothing special is needed for targets where ReverseBytes16 lowers to a rotate
// On Power10, 16-bit rotate is not available so use BRH instruction
Expand Down
4 changes: 4 additions & 0 deletions test/codegen/mathbits.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func ReverseBytes(n uint) uint {
// 386:"BSWAPL"
// s390x:"MOVDBR"
// arm64:"REV"
// loong64:"REVBV"
return bits.ReverseBytes(n)
}

Expand All @@ -211,6 +212,7 @@ func ReverseBytes64(n uint64) uint64 {
// 386:"BSWAPL"
// s390x:"MOVDBR"
// arm64:"REV"
// loong64:"REVBV"
// ppc64x/power10: "BRD"
return bits.ReverseBytes64(n)
}
Expand All @@ -220,6 +222,7 @@ func ReverseBytes32(n uint32) uint32 {
// 386:"BSWAPL"
// s390x:"MOVWBR"
// arm64:"REVW"
// loong64:"REVB2W"
// ppc64x/power10: "BRW"
return bits.ReverseBytes32(n)
}
Expand All @@ -230,6 +233,7 @@ func ReverseBytes16(n uint16) uint16 {
// arm/5:"SLL","SRL","ORR"
// arm/6:"REV16"
// arm/7:"REV16"
// loong64:"REVB2H"
// ppc64x/power10: "BRH"
return bits.ReverseBytes16(n)
}
Expand Down

0 comments on commit 28c3b2a

Please sign in to comment.