From 83d94daec2636badf02ecda18f7a7c1f4eb1fd74 Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Tue, 30 Aug 2022 18:59:53 +1000 Subject: [PATCH] cmd/compile: avoid the use of XOR for boolean equality on riscv64 The use of SEQZ/SNEZ and SUB allows for other optimisations to be utilised, particularly absorption into branch equality conditions. Change-Id: I74e7d6a07a8decc1bdb651660c322bcc6eb6a10a Reviewed-on: https://go-review.googlesource.com/c/go/+/428216 Run-TryBot: Joel Sing TryBot-Result: Gopher Robot Reviewed-by: David Chase Reviewed-by: Meng Zhuo Reviewed-by: Cherry Mui --- .../compile/internal/ssa/gen/RISCV64.rules | 4 ++-- .../compile/internal/ssa/rewriteRISCV64.go | 24 +++++++++++++++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/cmd/compile/internal/ssa/gen/RISCV64.rules b/src/cmd/compile/internal/ssa/gen/RISCV64.rules index c2f1c16b6b..5c3c862571 100644 --- a/src/cmd/compile/internal/ssa/gen/RISCV64.rules +++ b/src/cmd/compile/internal/ssa/gen/RISCV64.rules @@ -534,8 +534,8 @@ // Boolean ops; 0=false, 1=true (AndB ...) => (AND ...) (OrB ...) => (OR ...) -(EqB x y) => (SEQZ (XOR x y)) -(NeqB ...) => (XOR ...) +(EqB x y) => (SEQZ (SUB x y)) +(NeqB x y) => (SNEZ (SUB x y)) (Not ...) => (SEQZ ...) // Lowering pointer arithmetic diff --git a/src/cmd/compile/internal/ssa/rewriteRISCV64.go b/src/cmd/compile/internal/ssa/rewriteRISCV64.go index 097232ab18..05c646e4a6 100644 --- a/src/cmd/compile/internal/ssa/rewriteRISCV64.go +++ b/src/cmd/compile/internal/ssa/rewriteRISCV64.go @@ -406,8 +406,7 @@ func rewriteValueRISCV64(v *Value) bool { case OpNeq8: return rewriteValueRISCV64_OpNeq8(v) case OpNeqB: - v.Op = OpRISCV64XOR - return true + return rewriteValueRISCV64_OpNeqB(v) case OpNeqPtr: return rewriteValueRISCV64_OpNeqPtr(v) case OpNilCheck: @@ -1121,12 +1120,12 @@ func rewriteValueRISCV64_OpEqB(v *Value) bool { b := v.Block typ := &b.Func.Config.Types // match: (EqB x y) - // result: (SEQZ (XOR x y)) + // result: (SEQZ (SUB x y)) for { x := v_0 y := v_1 v.reset(OpRISCV64SEQZ) - v0 := b.NewValue0(v.Pos, OpRISCV64XOR, typ.Bool) + v0 := b.NewValue0(v.Pos, OpRISCV64SUB, typ.Bool) v0.AddArg2(x, y) v.AddArg(v0) return true @@ -2970,6 +2969,23 @@ func rewriteValueRISCV64_OpNeq8(v *Value) bool { return true } } +func rewriteValueRISCV64_OpNeqB(v *Value) bool { + v_1 := v.Args[1] + v_0 := v.Args[0] + b := v.Block + typ := &b.Func.Config.Types + // match: (NeqB x y) + // result: (SNEZ (SUB x y)) + for { + x := v_0 + y := v_1 + v.reset(OpRISCV64SNEZ) + v0 := b.NewValue0(v.Pos, OpRISCV64SUB, typ.Bool) + v0.AddArg2(x, y) + v.AddArg(v0) + return true + } +} func rewriteValueRISCV64_OpNeqPtr(v *Value) bool { v_1 := v.Args[1] v_0 := v.Args[0] -- 2.48.1