cmd/compile: removing log2uint32 function

Just using isUnsignedPowerOfTwo and log32u is enough.

Change-Id: I93d49ab71c6245d05f6507adbcb9ef2a696e75d6
Reviewed-on: https://go-review.googlesource.com/c/go/+/691476
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Cuong Manh Le 2025-07-29 23:55:20 +07:00 committed by Gopher Robot
parent 1513661dc3
commit 880ca333d7
3 changed files with 15 additions and 21 deletions

View file

@ -607,13 +607,13 @@
(Select0 (MULTU (MOVWconst [1]) _ )) => (MOVWconst [0]) (Select0 (MULTU (MOVWconst [1]) _ )) => (MOVWconst [0])
(Select1 (MULTU (MOVWconst [-1]) x )) => (NEG <x.Type> x) (Select1 (MULTU (MOVWconst [-1]) x )) => (NEG <x.Type> x)
(Select0 (MULTU (MOVWconst [-1]) x )) => (CMOVZ (ADDconst <x.Type> [-1] x) (MOVWconst [0]) x) (Select0 (MULTU (MOVWconst [-1]) x )) => (CMOVZ (ADDconst <x.Type> [-1] x) (MOVWconst [0]) x)
(Select1 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) => (SLLconst [int32(log2uint32(int64(c)))] x) (Select1 (MULTU (MOVWconst [c]) x )) && isUnsignedPowerOfTwo(uint32(c)) => (SLLconst [int32(log32u(uint32(c)))] x)
(Select0 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) => (SRLconst [int32(32-log2uint32(int64(c)))] x) (Select0 (MULTU (MOVWconst [c]) x )) && isUnsignedPowerOfTwo(uint32(c)) => (SRLconst [int32(32-log32u(uint32(c)))] x)
(MUL (MOVWconst [0]) _ ) => (MOVWconst [0]) (MUL (MOVWconst [0]) _ ) => (MOVWconst [0])
(MUL (MOVWconst [1]) x ) => x (MUL (MOVWconst [1]) x ) => x
(MUL (MOVWconst [-1]) x ) => (NEG x) (MUL (MOVWconst [-1]) x ) => (NEG x)
(MUL (MOVWconst [c]) x ) && isPowerOfTwo(int64(uint32(c))) => (SLLconst [int32(log2uint32(int64(c)))] x) (MUL (MOVWconst [c]) x ) && isUnsignedPowerOfTwo(uint32(c)) => (SLLconst [int32(log32u(uint32(c)))] x)
// generic simplifications // generic simplifications
(ADD x (NEG y)) => (SUB x y) (ADD x (NEG y)) => (SUB x y)

View file

@ -492,12 +492,6 @@ func log16u(n uint16) int64 { return int64(bits.Len16(n)) - 1 }
func log32u(n uint32) int64 { return int64(bits.Len32(n)) - 1 } func log32u(n uint32) int64 { return int64(bits.Len32(n)) - 1 }
func log64u(n uint64) int64 { return int64(bits.Len64(n)) - 1 } func log64u(n uint64) int64 { return int64(bits.Len64(n)) - 1 }
// log2uint32 returns logarithm in base 2 of uint32(n), with log2(0) = -1.
// Rounds down.
func log2uint32(n int64) int64 {
return int64(bits.Len32(uint32(n))) - 1
}
// isPowerOfTwoX functions report whether n is a power of 2. // isPowerOfTwoX functions report whether n is a power of 2.
func isPowerOfTwo[T int8 | int16 | int32 | int64](n T) bool { func isPowerOfTwo[T int8 | int16 | int32 | int64](n T) bool {
return n > 0 && n&(n-1) == 0 return n > 0 && n&(n-1) == 0

View file

@ -4058,8 +4058,8 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool {
break break
} }
// match: (MUL (MOVWconst [c]) x ) // match: (MUL (MOVWconst [c]) x )
// cond: isPowerOfTwo(int64(uint32(c))) // cond: isUnsignedPowerOfTwo(uint32(c))
// result: (SLLconst [int32(log2uint32(int64(c)))] x) // result: (SLLconst [int32(log32u(uint32(c)))] x)
for { for {
for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 { for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
if v_0.Op != OpMIPSMOVWconst { if v_0.Op != OpMIPSMOVWconst {
@ -4067,11 +4067,11 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool {
} }
c := auxIntToInt32(v_0.AuxInt) c := auxIntToInt32(v_0.AuxInt)
x := v_1 x := v_1
if !(isPowerOfTwo(int64(uint32(c)))) { if !(isUnsignedPowerOfTwo(uint32(c))) {
continue continue
} }
v.reset(OpMIPSSLLconst) v.reset(OpMIPSSLLconst)
v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c)))) v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c))))
v.AddArg(x) v.AddArg(x)
return true return true
} }
@ -6611,8 +6611,8 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool {
break break
} }
// match: (Select0 (MULTU (MOVWconst [c]) x )) // match: (Select0 (MULTU (MOVWconst [c]) x ))
// cond: isPowerOfTwo(int64(uint32(c))) // cond: isUnsignedPowerOfTwo(uint32(c))
// result: (SRLconst [int32(32-log2uint32(int64(c)))] x) // result: (SRLconst [int32(32-log32u(uint32(c)))] x)
for { for {
if v_0.Op != OpMIPSMULTU { if v_0.Op != OpMIPSMULTU {
break break
@ -6626,11 +6626,11 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool {
} }
c := auxIntToInt32(v_0_0.AuxInt) c := auxIntToInt32(v_0_0.AuxInt)
x := v_0_1 x := v_0_1
if !(isPowerOfTwo(int64(uint32(c)))) { if !(isUnsignedPowerOfTwo(uint32(c))) {
continue continue
} }
v.reset(OpMIPSSRLconst) v.reset(OpMIPSSRLconst)
v.AuxInt = int32ToAuxInt(int32(32 - log2uint32(int64(c)))) v.AuxInt = int32ToAuxInt(int32(32 - log32u(uint32(c))))
v.AddArg(x) v.AddArg(x)
return true return true
} }
@ -6807,8 +6807,8 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool {
break break
} }
// match: (Select1 (MULTU (MOVWconst [c]) x )) // match: (Select1 (MULTU (MOVWconst [c]) x ))
// cond: isPowerOfTwo(int64(uint32(c))) // cond: isUnsignedPowerOfTwo(uint32(c))
// result: (SLLconst [int32(log2uint32(int64(c)))] x) // result: (SLLconst [int32(log32u(uint32(c)))] x)
for { for {
if v_0.Op != OpMIPSMULTU { if v_0.Op != OpMIPSMULTU {
break break
@ -6822,11 +6822,11 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool {
} }
c := auxIntToInt32(v_0_0.AuxInt) c := auxIntToInt32(v_0_0.AuxInt)
x := v_0_1 x := v_0_1
if !(isPowerOfTwo(int64(uint32(c)))) { if !(isUnsignedPowerOfTwo(uint32(c))) {
continue continue
} }
v.reset(OpMIPSSLLconst) v.reset(OpMIPSSLLconst)
v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c)))) v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c))))
v.AddArg(x) v.AddArg(x)
return true return true
} }