Commit 9067494c61 for golang
commit 9067494c61e3bf29c0d578ca75e884655398f9ed
Author: wangboyao <wangboyao@bytedance.com>
Date: Thu Dec 3 14:16:31 2026 +0800
cmd/compile: avoid folding conditional SUBW on riscv64
The Zicond folds require op(x, 0) == x. SUBW(x, 0) sign-extends the
low 32 bits of x, so the fold can truncate the selected 64-bit value.
Fixes #83608
Change-Id: Ic3e82853585f178112abe88db7ea56e585db1135
Reviewed-on: https://go-review.googlesource.com/c/go/+/869581
Reviewed-by: Xueqi Luo <luo.xueqi@zte.com.cn>
Reviewed-by: Joel Sing <joel@sing.id.au>
Reviewed-by: Julian Zhu <jz531210@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
index 3476b969c4..cc2527dc9f 100644
--- a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
+++ b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
@@ -919,8 +919,8 @@
(CZERO(EQ|NE)Z (MOVDconst [0]) _) => (MOVDconst [0])
// Optimize conditional arithmetic operations
-(OR (CZEROEQZ <t> x cond) (CZERONEZ <t> ((ADD|SUB|OR|XOR|SUBW) x y) cond)) => ((ADD|SUB|OR|XOR|SUBW) x (CZERONEZ <t> y cond))
-(OR (CZEROEQZ <t> ((ADD|SUB|OR|XOR|SUBW) x y) cond) (CZERONEZ <t> x cond)) => ((ADD|SUB|OR|XOR|SUBW) x (CZEROEQZ <t> y cond))
+(OR (CZEROEQZ <t> x cond) (CZERONEZ <t> ((ADD|SUB|OR|XOR) x y) cond)) => ((ADD|SUB|OR|XOR) x (CZERONEZ <t> y cond))
+(OR (CZEROEQZ <t> ((ADD|SUB|OR|XOR) x y) cond) (CZERONEZ <t> x cond)) => ((ADD|SUB|OR|XOR) x (CZEROEQZ <t> y cond))
(OR x:(CZEROEQZ z cond) (CZERONEZ y:(AND z _) cond)) => (OR y x)
(OR (CZEROEQZ x:(AND z _) cond) y:(CZERONEZ z cond)) => (OR x y)
(OR x:(CZEROEQZ z cond) (CZERONEZ y:(ANDI <t> [c] z) cond)) => (OR y x)
diff --git a/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go b/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
index d8badeb6e7..f0f649a30a 100644
--- a/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
+++ b/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
@@ -7544,36 +7544,6 @@ func rewriteValue_OpRISCV64OR(v *ssa.Value) bool {
}
break
}
- // match: (OR (CZEROEQZ <t> x cond) (CZERONEZ <t> (SUBW x y) cond))
- // result: (SUBW x (CZERONEZ <t> y cond))
- for {
- for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
- if v_0.Op != ssaop.OpRISCV64CZEROEQZ {
- continue
- }
- t := v_0.Type
- cond := v_0.Args[1]
- x := v_0.Args[0]
- if v_1.Op != ssaop.OpRISCV64CZERONEZ || v_1.Type != t {
- continue
- }
- _ = v_1.Args[1]
- v_1_0 := v_1.Args[0]
- if v_1_0.Op != ssaop.OpRISCV64SUBW {
- continue
- }
- y := v_1_0.Args[1]
- if x != v_1_0.Args[0] || cond != v_1.Args[1] {
- continue
- }
- v.Reset(ssaop.OpRISCV64SUBW)
- v0 := b.NewValue0(v.Pos, ssaop.OpRISCV64CZERONEZ, t)
- v0.AddArg2(y, cond)
- v.AddArg2(x, v0)
- return true
- }
- break
- }
// match: (OR (CZEROEQZ <t> (ADD x y) cond) (CZERONEZ <t> x cond))
// result: (ADD x (CZEROEQZ <t> y cond))
for {
@@ -7709,36 +7679,6 @@ func rewriteValue_OpRISCV64OR(v *ssa.Value) bool {
}
break
}
- // match: (OR (CZEROEQZ <t> (SUBW x y) cond) (CZERONEZ <t> x cond))
- // result: (SUBW x (CZEROEQZ <t> y cond))
- for {
- for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
- if v_0.Op != ssaop.OpRISCV64CZEROEQZ {
- continue
- }
- t := v_0.Type
- cond := v_0.Args[1]
- v_0_0 := v_0.Args[0]
- if v_0_0.Op != ssaop.OpRISCV64SUBW {
- continue
- }
- y := v_0_0.Args[1]
- x := v_0_0.Args[0]
- if v_1.Op != ssaop.OpRISCV64CZERONEZ || v_1.Type != t {
- continue
- }
- _ = v_1.Args[1]
- if x != v_1.Args[0] || cond != v_1.Args[1] {
- continue
- }
- v.Reset(ssaop.OpRISCV64SUBW)
- v0 := b.NewValue0(v.Pos, ssaop.OpRISCV64CZEROEQZ, t)
- v0.AddArg2(y, cond)
- v.AddArg2(x, v0)
- return true
- }
- break
- }
// match: (OR x:(CZEROEQZ z cond) (CZERONEZ y:(AND z _) cond))
// result: (OR y x)
for {
diff --git a/src/cmd/compile/internal/ssacompile/export_test.go b/src/cmd/compile/internal/ssacompile/export_test.go
index 19fb9addd8..c4fa5ea39d 100644
--- a/src/cmd/compile/internal/ssacompile/export_test.go
+++ b/src/cmd/compile/internal/ssacompile/export_test.go
@@ -14,6 +14,7 @@ import (
"cmd/compile/internal/types"
"cmd/internal/obj"
"cmd/internal/obj/arm64"
+ "cmd/internal/obj/riscv"
"cmd/internal/obj/s390x"
"cmd/internal/obj/x86"
"cmd/internal/src"
@@ -26,9 +27,10 @@ var Deadcode = deadcode
var Copyelim = copyelim
var testCtxts = map[string]*obj.Link{
- "amd64": obj.Linknew(&x86.Linkamd64),
- "s390x": obj.Linknew(&s390x.Links390x),
- "arm64": obj.Linknew(&arm64.Linkarm64),
+ "amd64": obj.Linknew(&x86.Linkamd64),
+ "s390x": obj.Linknew(&s390x.Links390x),
+ "arm64": obj.Linknew(&arm64.Linkarm64),
+ "riscv64": obj.Linknew(&riscv.LinkRISCV64),
}
func testConfig(tb testing.TB) *Conf { return testConfigArch(tb, "amd64") }
diff --git a/src/cmd/compile/internal/ssacompile/rewrite_test.go b/src/cmd/compile/internal/ssacompile/rewrite_test.go
index 78600a5008..1d09f5c283 100644
--- a/src/cmd/compile/internal/ssacompile/rewrite_test.go
+++ b/src/cmd/compile/internal/ssacompile/rewrite_test.go
@@ -13,6 +13,8 @@ import (
"cmd/compile/internal/rttype"
"cmd/compile/internal/ssa"
+ "cmd/compile/internal/ssa/ssaop"
+ "cmd/compile/internal/types"
)
// We generate memmove for copy(x[1:], x[:]), however we may change it to OpMove,
@@ -375,3 +377,36 @@ func TestDisjointTypesRun(t *testing.T) {
t.Errorf("disjointTypes gives an incorrect answer that leads to an incorrect optimization.")
}
}
+
+// SUBW(x, 0) sign-extends the low word. Folding it across a select
+// would truncate x when the other arm is selected. See issue 83608.
+func TestRISCV64ConditionalSubtract(t *testing.T) {
+ c := testConfigArch(t, "riscv64")
+ i64 := c.config.Types.Int64
+ for _, tc := range []struct {
+ sub ssaop.Op
+ yes, no string
+ want ssaop.Op
+ }{
+ {ssaop.OpRISCV64SUBW, "x", "sub", ssaop.OpRISCV64OR},
+ {ssaop.OpRISCV64SUBW, "sub", "x", ssaop.OpRISCV64OR},
+ {ssaop.OpRISCV64SUB, "x", "sub", ssaop.OpRISCV64SUB},
+ {ssaop.OpRISCV64SUB, "sub", "x", ssaop.OpRISCV64SUB},
+ } {
+ f := c.Fun("entry", Bloc("entry",
+ Valu("x", ssaop.OpArg, i64, 0, nil),
+ Valu("y", ssaop.OpArg, i64, 0, nil),
+ Valu("cond", ssaop.OpArg, i64, 0, nil),
+ Valu("sub", tc.sub, i64, 0, nil, "x", "y"),
+ Valu("yes", ssaop.OpRISCV64CZEROEQZ, i64, 0, nil, tc.yes, "cond"),
+ Valu("no", ssaop.OpRISCV64CZERONEZ, i64, 0, nil, tc.no, "cond"),
+ Valu("select", ssaop.OpRISCV64OR, i64, 0, nil, "yes", "no"),
+ Valu("mem", ssaop.OpInitMem, types.TypeMem, 0, nil),
+ Exit("mem")))
+ v := f.values["select"]
+ c.config.LowerValue(v)
+ if v.Op != tc.want {
+ t.Errorf("%s, selecting %s or %s: got %s, want %s", tc.sub, tc.yes, tc.no, v.Op, tc.want)
+ }
+ }
+}