aboutsummaryrefslogtreecommitdiff
path: root/test/codegen
diff options
context:
space:
mode:
authorJoel Sing <joel@sing.id.au>2023-11-26 02:48:05 +1100
committerCherry Mui <cherryyz@google.com>2023-12-01 19:30:59 +0000
commit70c7fb75e9768630ca23ff5cbf79c9b597bc068e (patch)
treef3abaad695dc42bce092b78fdc41c38aa70917f1 /test/codegen
parent5a2161ce9ec130271ec67566ecb5a842497e8742 (diff)
downloadgo-70c7fb75e9768630ca23ff5cbf79c9b597bc068e.tar.gz
go-70c7fb75e9768630ca23ff5cbf79c9b597bc068e.zip
cmd/compile: correct code generation for right shifts on riscv64
The code generation on riscv64 will currently result in incorrect assembly when a 32 bit integer is right shifted by an amount that exceeds the size of the type. In particular, this occurs when an int32 or uint32 is cast to a 64 bit type and right shifted by a value larger than 31. Fix this by moving the SRAW/SRLW conversion into the right shift rules and removing the SignExt32to64/ZeroExt32to64. Add additional rules that rewrite to SRAIW/SRLIW when the shift is less than the size of the type, or replace/eliminate the shift when it exceeds the size of the type. Add SSA tests that would have caught this issue. Also add additional codegen tests to ensure that the resulting assembly is what we expect in these overflow cases. Fixes #64285 Change-Id: Ie97b05668597cfcb91413afefaab18ee1aa145ec Reviewed-on: https://go-review.googlesource.com/c/go/+/545035 Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: M Zhuo <mzh@golangcn.org> Reviewed-by: Mark Ryan <markdryan@rivosinc.com> Run-TryBot: Joel Sing <joel@sing.id.au> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'test/codegen')
-rw-r--r--test/codegen/shift.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/codegen/shift.go b/test/codegen/shift.go
index 32cfaffae0..50d60426d0 100644
--- a/test/codegen/shift.go
+++ b/test/codegen/shift.go
@@ -22,12 +22,42 @@ func rshConst64Ux64(v uint64) uint64 {
return v >> uint64(33)
}
+func rshConst64Ux64Overflow32(v uint32) uint64 {
+ // riscv64:"MOV\t\\$0,",-"SRL"
+ return uint64(v) >> 32
+}
+
+func rshConst64Ux64Overflow16(v uint16) uint64 {
+ // riscv64:"MOV\t\\$0,",-"SRL"
+ return uint64(v) >> 16
+}
+
+func rshConst64Ux64Overflow8(v uint8) uint64 {
+ // riscv64:"MOV\t\\$0,",-"SRL"
+ return uint64(v) >> 8
+}
+
func rshConst64x64(v int64) int64 {
// ppc64x:"SRAD"
// riscv64:"SRAI\t",-"OR",-"SLTIU"
return v >> uint64(33)
}
+func rshConst64x64Overflow32(v int32) int64 {
+ // riscv64:"SRAIW",-"SLLI",-"SRAI\t"
+ return int64(v) >> 32
+}
+
+func rshConst64x64Overflow16(v int16) int64 {
+ // riscv64:"SLLI","SRAI",-"SRAIW"
+ return int64(v) >> 16
+}
+
+func rshConst64x64Overflow8(v int8) int64 {
+ // riscv64:"SLLI","SRAI",-"SRAIW"
+ return int64(v) >> 8
+}
+
func lshConst32x64(v int32) int32 {
// ppc64x:"SLW"
// riscv64:"SLLI",-"AND",-"SLTIU", -"MOVW"