Skip to content

Commit 78ef487

Browse files
sophie-zhaoabner-chenc
authored andcommitted
cmd/compile: fix the issue of shift amount exceeding the valid range
Fixes golang#75479 Change-Id: I362d3e49090e94f91a840dd5a475978b59222a00 Reviewed-on: https://go-review.googlesource.com/c/go/+/704135 Reviewed-by: Mark Freeman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Meidan Li <[email protected]> Reviewed-by: abner chenc <[email protected]>
1 parent 77aac7b commit 78ef487

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@
717717
(SRLVconst [rc] (MOVBUreg x)) && rc >= 8 => (MOVVconst [0])
718718

719719
// (x + x) << c -> x << c+1
720-
((SLLV|SLL)const [c] (ADDV x x)) => ((SLLV|SLL)const [c+1] x)
720+
((SLLV|SLL)const <t> [c] (ADDV x x)) && c < t.Size() * 8 - 1 => ((SLLV|SLL)const [c+1] x)
721+
((SLLV|SLL)const <t> [c] (ADDV x x)) && c >= t.Size() * 8 - 1 => (MOVVconst [0])
721722

722723
// mul by constant
723724
(MULV _ (MOVVconst [0])) => (MOVVconst [0])

src/cmd/compile/internal/ssa/_gen/LOONG64Ops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func init() {
247247
{name: "SLL", argLength: 2, reg: gp21, asm: "SLL"}, // arg0 << arg1, shift amount is mod 32
248248
{name: "SLLV", argLength: 2, reg: gp21, asm: "SLLV"}, // arg0 << arg1, shift amount is mod 64
249249
{name: "SLLconst", argLength: 1, reg: gp11, asm: "SLL", aux: "Int64"}, // arg0 << auxInt, auxInt should be in the range 0 to 31.
250-
{name: "SLLVconst", argLength: 1, reg: gp11, asm: "SLLV", aux: "Int64"}, // arg0 << auxInt
250+
{name: "SLLVconst", argLength: 1, reg: gp11, asm: "SLLV", aux: "Int64"}, // arg0 << auxInt, auxInt should be in the range 0 to 63.
251251
{name: "SRL", argLength: 2, reg: gp21, asm: "SRL"}, // arg0 >> arg1, shift amount is mod 32
252252
{name: "SRLV", argLength: 2, reg: gp21, asm: "SRLV"}, // arg0 >> arg1, unsigned, shift amount is mod 64
253253
{name: "SRLconst", argLength: 1, reg: gp11, asm: "SRL", aux: "Int64"}, // arg0 >> auxInt, auxInt should be in the range 0 to 31.

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

Lines changed: 42 additions & 4 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,13 @@ func lshConst64x2Add(x int64) int64 {
148148
}
149149

150150
func lshConst32x31Add(x int32) int32 {
151+
// loong64:-"SLL\t","MOVV\tR0"
151152
// riscv64:-"SLLI","MOV\t[$]0"
152153
return (x + x) << 31
153154
}
154155

155156
func lshConst64x63Add(x int64) int64 {
157+
// loong64:-"SLLV","MOVV\tR0"
156158
// riscv64:-"SLLI","MOV\t[$]0"
157159
return (x + x) << 63
158160
}

0 commit comments

Comments
 (0)