From bbd0bc22ea4bc447a4d009db7d5137688820896d Mon Sep 17 00:00:00 2001 From: Jorropo Date: Wed, 10 Jan 2024 07:31:57 +0100 Subject: cmd/compile: improve integer comparisons with numeric bounds This do: - Fold always false or always true comparisons for ints and uint. - Reduce < and <= where the true set is only one value to == with such value. Change-Id: Ie9e3f70efd1845bef62db56543f051a50ad2532e Reviewed-on: https://go-review.googlesource.com/c/go/+/555135 Auto-Submit: Keith Randall Reviewed-by: Keith Randall Reviewed-by: Cherry Mui Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/ssa/_gen/generic.rules | 46 +++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'src/cmd/compile/internal/ssa/_gen/generic.rules') diff --git a/src/cmd/compile/internal/ssa/_gen/generic.rules b/src/cmd/compile/internal/ssa/_gen/generic.rules index aeda62591a..4c475d31e0 100644 --- a/src/cmd/compile/internal/ssa/_gen/generic.rules +++ b/src/cmd/compile/internal/ssa/_gen/generic.rules @@ -601,9 +601,49 @@ (Or(64|32|16|8) x (Or(64|32|16|8) x y)) => (Or(64|32|16|8) x y) (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) => y -// Unsigned comparisons to zero. -(Less(64U|32U|16U|8U) _ (Const(64|32|16|8) [0])) => (ConstBool [false]) -(Leq(64U|32U|16U|8U) (Const(64|32|16|8) [0]) _) => (ConstBool [true]) +// Fold comparisons with numeric bounds +(Less(64|32|16|8)U _ (Const(64|32|16|8) [0])) => (ConstBool [false]) +(Leq(64|32|16|8)U (Const(64|32|16|8) [0]) _) => (ConstBool [true]) +(Less(64|32|16|8)U (Const(64|32|16|8) [-1]) _) => (ConstBool [false]) +(Leq(64|32|16|8)U _ (Const(64|32|16|8) [-1])) => (ConstBool [true]) +(Less64 _ (Const64 [math.MinInt64])) => (ConstBool [false]) +(Less32 _ (Const32 [math.MinInt32])) => (ConstBool [false]) +(Less16 _ (Const16 [math.MinInt16])) => (ConstBool [false]) +(Less8 _ (Const8 [math.MinInt8 ])) => (ConstBool [false]) +(Leq64 (Const64 [math.MinInt64]) _) => (ConstBool [true]) +(Leq32 (Const32 [math.MinInt32]) _) => (ConstBool [true]) +(Leq16 (Const16 [math.MinInt16]) _) => (ConstBool [true]) +(Leq8 (Const8 [math.MinInt8 ]) _) => (ConstBool [true]) +(Less64 (Const64 [math.MaxInt64]) _) => (ConstBool [false]) +(Less32 (Const32 [math.MaxInt32]) _) => (ConstBool [false]) +(Less16 (Const16 [math.MaxInt16]) _) => (ConstBool [false]) +(Less8 (Const8 [math.MaxInt8 ]) _) => (ConstBool [false]) +(Leq64 _ (Const64 [math.MaxInt64])) => (ConstBool [true]) +(Leq32 _ (Const32 [math.MaxInt32])) => (ConstBool [true]) +(Leq16 _ (Const16 [math.MaxInt16])) => (ConstBool [true]) +(Leq8 _ (Const8 [math.MaxInt8 ])) => (ConstBool [true]) + +// Canonicalize <= on numeric bounds and < near numeric bounds to == +(Leq(64|32|16|8)U x c:(Const(64|32|16|8) [0])) => (Eq(64|32|16|8) x c) +(Leq(64|32|16|8)U c:(Const(64|32|16|8) [-1]) x) => (Eq(64|32|16|8) x c) +(Less(64|32|16|8)U x (Const(64|32|16|8) [1])) => (Eq(64|32|16|8) x (Const(64|32|16|8) [0])) +(Less(64|32|16|8)U (Const(64|32|16|8) [-2]) x) => (Eq(64|32|16|8) x (Const(64|32|16|8) [-1])) +(Leq64 x c:(Const64 [math.MinInt64])) => (Eq64 x c) +(Leq32 x c:(Const32 [math.MinInt32])) => (Eq32 x c) +(Leq16 x c:(Const16 [math.MinInt16])) => (Eq16 x c) +(Leq8 x c:(Const8 [math.MinInt8 ])) => (Eq8 x c) +(Leq64 c:(Const64 [math.MaxInt64]) x) => (Eq64 x c) +(Leq32 c:(Const32 [math.MaxInt32]) x) => (Eq32 x c) +(Leq16 c:(Const16 [math.MaxInt16]) x) => (Eq16 x c) +(Leq8 c:(Const8 [math.MaxInt8 ]) x) => (Eq8 x c) +(Less64 x (Const64 [math.MinInt64+1])) => (Eq64 x (Const64 [math.MinInt64])) +(Less32 x (Const32 [math.MinInt32+1])) => (Eq32 x (Const32 [math.MinInt32])) +(Less16 x (Const16 [math.MinInt16+1])) => (Eq16 x (Const16 [math.MinInt16])) +(Less8 x (Const8 [math.MinInt8 +1])) => (Eq8 x (Const8 [math.MinInt8 ])) +(Less64 (Const64 [math.MaxInt64-1]) x) => (Eq64 x (Const64 [math.MaxInt64])) +(Less32 (Const32 [math.MaxInt32-1]) x) => (Eq32 x (Const32 [math.MaxInt32])) +(Less16 (Const16 [math.MaxInt16-1]) x) => (Eq16 x (Const16 [math.MaxInt16])) +(Less8 (Const8 [math.MaxInt8 -1]) x) => (Eq8 x (Const8 [math.MaxInt8 ])) // Ands clear bits. Ors set bits. // If a subsequent Or will set all the bits -- cgit v1.3-6-g1900