Skip to content

Commit 61945fc

Browse files
committed
cmd/compile: don't generate panicshift for masked int shifts
We know that a & 31 is non-negative for all a, signed or not. We can avoid checking that and needing to write out an unreachable call to panicshift. Change-Id: I32f32fb2c950d2b2b35ac5c0e99b7b2dbd47f917 Reviewed-on: https://go-review.googlesource.com/c/go/+/167499 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 2d21bf4 commit 61945fc

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

src/cmd/compile/internal/ssa/gen/generic.rules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,11 @@
420420
(Less(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c < d)])
421421
(Leq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c <= d)])
422422

423+
(Geq8 (And8 _ (Const8 [c])) (Const8 [0])) && int8(c) >= 0 -> (ConstBool [1])
424+
(Geq16 (And16 _ (Const16 [c])) (Const16 [0])) && int16(c) >= 0 -> (ConstBool [1])
425+
(Geq32 (And32 _ (Const32 [c])) (Const32 [0])) && int32(c) >= 0 -> (ConstBool [1])
426+
(Geq64 (And64 _ (Const64 [c])) (Const64 [0])) && int64(c) >= 0 -> (ConstBool [1])
427+
423428
(Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
424429
(Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
425430
(Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])

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

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

test/codegen/shift.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ func rshMask64x32Ext(v int64, s int32) int64 {
7070
return v >> uint(s&63)
7171
}
7272

73+
// --------------- //
74+
// signed shifts //
75+
// --------------- //
76+
77+
// We do want to generate a test + panicshift for these cases.
78+
func lshSigned(v8 int8, v16 int16, v32 int32, v64 int64, x int) {
79+
// amd64:"TESTB"
80+
_ = x << v8
81+
// amd64:"TESTW"
82+
_ = x << v16
83+
// amd64:"TESTL"
84+
_ = x << v32
85+
// amd64:"TESTQ"
86+
_ = x << v64
87+
}
88+
89+
// We want to avoid generating a test + panicshift for these cases.
90+
func lshSignedMasked(v8 int8, v16 int16, v32 int32, v64 int64, x int) {
91+
// amd64:-"TESTB"
92+
_ = x << (v8 & 7)
93+
// amd64:-"TESTW"
94+
_ = x << (v16 & 15)
95+
// amd64:-"TESTL"
96+
_ = x << (v32 & 31)
97+
// amd64:-"TESTQ"
98+
_ = x << (v64 & 63)
99+
}
100+
73101
// ------------------ //
74102
// bounded shifts //
75103
// ------------------ //

0 commit comments

Comments
 (0)