Skip to content

Commit

Permalink
cmd/compile: eliminate successive swaps
Browse files Browse the repository at this point in the history
The code generated when storing eight bytes loaded from memory in big
endian introduced two successive byte swaps that did not actually
modified the data.

The new rules match this specific pattern both for amd64 and for arm64,
eliminating the double swap.

Fixes golang#41684

Change-Id: Icb6dc20b68e4393cef4fe6a07b33aba0d18c3ff3
Reviewed-on: https://go-review.googlesource.com/c/go/+/320073
Reviewed-by: Keith Randall <[email protected]>
Run-TryBot: Keith Randall <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Trust: Daniel Martí <[email protected]>
Trust: Dmitri Shuralyov <[email protected]>
  • Loading branch information
agarciamontoro authored and randall77 committed Oct 9, 2021
1 parent 74abcab commit e1c294a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cmd/compile/internal/ssa/gen/AMD64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2217,3 +2217,5 @@
(AND(Q|L) x (NEG(Q|L) x)) && buildcfg.GOAMD64 >= 3 => (BLSI(Q|L) x)
(XOR(Q|L) x (ADD(Q|L)const [-1] x)) && buildcfg.GOAMD64 >= 3 => (BLSMSK(Q|L) x)
(AND(Q|L) x (ADD(Q|L)const [-1] x)) && buildcfg.GOAMD64 >= 3 => (BLSR(Q|L) x)

(BSWAP(Q|L) (BSWAP(Q|L) p)) => p
2 changes: 2 additions & 0 deletions src/cmd/compile/internal/ssa/gen/ARM64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2931,3 +2931,5 @@
&& isInlinableMemmove(dst, src, sz, config)
&& clobber(call)
=> (Move [sz] dst src mem)

((REV|REVW) ((REV|REVW) p)) => p
32 changes: 32 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteAMD64.go

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

32 changes: 32 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteARM64.go

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

12 changes: 12 additions & 0 deletions test/codegen/memcombine.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ func store_be32(b []byte) {
binary.BigEndian.PutUint32(b, sink32)
}

func store_be64_load(b, x *[8]byte) {
// arm64:-`REV`
// amd64:-`BSWAPQ`
binary.BigEndian.PutUint64(b[:], binary.BigEndian.Uint64(x[:]))
}

func store_be32_load(b, x *[8]byte) {
// arm64:-`REVW`
// amd64:-`BSWAPL`
binary.BigEndian.PutUint32(b[:], binary.BigEndian.Uint32(x[:]))
}

func store_be32_idx(b []byte, idx int) {
// amd64:`BSWAPL`,-`SHR.`
// arm64:`REVW`,`MOVW\sR[0-9]+,\s\(R[0-9]+\)\(R[0-9]+\)`,-`MOV[BH]`,-`REV16W`
Expand Down

0 comments on commit e1c294a

Please sign in to comment.