Skip to content

Commit a0e738c

Browse files
markdryangopherbot
authored andcommitted
cmd/compile/internal: remove incorrect riscv64 SLTI rule
The rule (SLTI [x] (ORI [y] _)) && y >= 0 && int64(y) >= int64(x) => (MOVDconst [0]) is incorrect as it only generates correct code if the unknown value being compared is >= 0. If the unknown value is < 0 the rule will incorrectly produce a constant value of 0, whereas the code optimized away by the rule would have produced a value of 1. A new test that causes the faulty rule to generate incorrect code is also added to ensure that the error does not return. Change-Id: I69224e0776596f1b9538acf9dacf9009d305f966 Reviewed-on: https://go-review.googlesource.com/c/go/+/720220 Reviewed-by: Meng Zhuo <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Joel Sing <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 2cdcc41 commit a0e738c

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@
792792
// SLTI/SLTIU with known outcomes.
793793
(SLTI [x] (ANDI [y] _)) && y >= 0 && int64(y) < int64(x) => (MOVDconst [1])
794794
(SLTIU [x] (ANDI [y] _)) && y >= 0 && uint64(y) < uint64(x) => (MOVDconst [1])
795-
(SLTI [x] (ORI [y] _)) && y >= 0 && int64(y) >= int64(x) => (MOVDconst [0])
796795
(SLTIU [x] (ORI [y] _)) && y >= 0 && uint64(y) >= uint64(x) => (MOVDconst [0])
797796

798797
// SLT/SLTU with known outcomes.

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

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

src/cmd/compile/internal/test/testdata/arith_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,19 @@ func testBitwiseRshU_ssa(a uint32, b, c uint32) uint32 {
444444
return a >> b >> c
445445
}
446446

447+
//go:noinline
448+
func orLt_ssa(x int) bool {
449+
y := x - x
450+
return (x | 2) < y
451+
}
452+
453+
// test riscv64 SLTI rules
454+
func testSetIfLessThan(t *testing.T) {
455+
if want, got := true, orLt_ssa(-7); got != want {
456+
t.Errorf("orLt_ssa(-7) = %t want %t", got, want)
457+
}
458+
}
459+
447460
//go:noinline
448461
func testShiftCX_ssa() int {
449462
v1 := uint8(3)
@@ -977,6 +990,7 @@ func TestArithmetic(t *testing.T) {
977990
testRegallocCVSpill(t)
978991
testSubqToNegq(t)
979992
testBitwiseLogic(t)
993+
testSetIfLessThan(t)
980994
testOcom(t)
981995
testLrot(t)
982996
testShiftCX(t)

0 commit comments

Comments
 (0)