Skip to content

Commit bd80f74

Browse files
amusmangopherbot
authored andcommitted
cmd/compile: fold shift through AND for slice operations
Fold a shift through AND when the AND gets a zero-or-one operand (e.g. from arithmetic shift by 63 of a 64-bit value) for a common case with slice operations: ASR $63, R2, R2 AND R3<<3, R2, R2 ADD R2, R0, R2 As the operands are 64-bit, we can transform it to: AND R2->63, R3, R2 ADD R2<<3, R0, R2 Code size improvement: compile: .text: 9088004 -> 9086292 (-0.02%) etcd: .text: 10500276 -> 10498964 (-0.01%) Change-Id: Ibcd5e67173da39b77ceff77ca67812fb8be5a7b5 Reviewed-on: https://go-review.googlesource.com/c/go/+/679895 Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Auto-Submit: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 5c45fe1 commit bd80f74

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,10 @@
16661666
(SRLconst [rc] (MOVHUreg x)) && rc >= 16 => (MOVDconst [0])
16671667
(SRLconst [rc] (MOVBUreg x)) && rc >= 8 => (MOVDconst [0])
16681668

1669+
// Special cases for slice operations
1670+
(ADD x0 x1:(ANDshiftRA x2:(SLLconst [sl] y) z [63])) && x1.Uses == 1 && x2.Uses == 1 => (ADDshiftLL x0 (ANDshiftRA <y.Type> y z [63]) [sl])
1671+
(ADD x0 x1:(ANDshiftLL x2:(SRAconst [63] z) y [sl])) && x1.Uses == 1 && x2.Uses == 1 => (ADDshiftLL x0 (ANDshiftRA <y.Type> y z [63]) [sl])
1672+
16691673
// bitfield ops
16701674

16711675
// sbfiz

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

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

test/codegen/slices.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ func SliceWithSubtractBound(a []int, b int) []int {
417417
return a[(3 - b):]
418418
}
419419

420+
// --------------------------------------- //
421+
// ARM64 folding for slice masks //
422+
// --------------------------------------- //
423+
424+
func SliceAndIndex(a []int, b int) int {
425+
// arm64:"AND\tR[0-9]+->63","ADD\tR[0-9]+<<3"
426+
return a[b:][b]
427+
}
428+
420429
// --------------------------------------- //
421430
// Code generation for unsafe.Slice //
422431
// --------------------------------------- //

0 commit comments

Comments
 (0)