Skip to content

Commit

Permalink
cmd/compile/internal/ssa: optimize (AND (MOVDconst [-1] x)) on PPC64
Browse files Browse the repository at this point in the history
This sequence can show up in the lowering pass on PPC64. If it
makes it to the latelower pass, it will cause an error because
it looks like it can be turned into RLDICL, but -1 isn't an
accepted mask.

Also, print more debug info if panic is called from
encodePPC64RotateMask.

Fixes #62698

Change-Id: I0f3322e2205357abe7fc28f96e05e3f7ad65567c
Reviewed-on: https://go-review.googlesource.com/c/go/+/529195
Reviewed-by: Lynn Boger <laboger@linux.vnet.ibm.com>
Run-TryBot: Paul Murphy <murp@ibm.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
  • Loading branch information
pmur committed Sep 22, 2023
1 parent 795414d commit c8caad4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ssa/_gen/PPC64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@
(NOR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [^(c|d)])

// Discover consts
(AND x (MOVDconst [-1])) => x
(AND x (MOVDconst [c])) && isU16Bit(c) => (Select0 (ANDCCconst [c] x))
(XOR x (MOVDconst [c])) && isU32Bit(c) => (XORconst [c] x)
(OR x (MOVDconst [c])) && isU32Bit(c) => (ORconst [c] x)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/ssa/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ func encodePPC64RotateMask(rotate, mask, nbits int64) int64 {

// Determine boundaries and then decode them
if mask == 0 || ^mask == 0 || rotate >= nbits {
panic("Invalid PPC64 rotate mask")
panic(fmt.Sprintf("invalid PPC64 rotate mask: %x %d %d", uint64(mask), rotate, nbits))
} else if nbits == 32 {
mb = bits.LeadingZeros32(uint32(mask))
me = 32 - bits.TrailingZeros32(uint32(mask))
Expand Down
13 changes: 13 additions & 0 deletions src/cmd/compile/internal/ssa/rewritePPC64.go

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

6 changes: 6 additions & 0 deletions test/codegen/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ func ptrBothOffset() {
// s390x:-"BEQ",-"BNE"
copy(x[1:], x[2:])
}

// Verify #62698 on PPC64.
func noMaskOnCopy(a []int, s string, x int) int {
// ppc64x:-"MOVD\t$-1", -"AND"
return a[x&^copy([]byte{}, s)]
}

0 comments on commit c8caad4

Please sign in to comment.