From: Cuong Manh Le Date: Tue, 29 Jul 2025 16:55:20 +0000 (+0700) Subject: cmd/compile: removing log2uint32 function X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=880ca333d708b957325b6ef4798699372d4c090e;p=gostls13.git 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 Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI Auto-Submit: Cuong Manh Le Reviewed-by: David Chase --- diff --git a/src/cmd/compile/internal/ssa/_gen/MIPS.rules b/src/cmd/compile/internal/ssa/_gen/MIPS.rules index a9bac5fabe..d417bafa1d 100644 --- a/src/cmd/compile/internal/ssa/_gen/MIPS.rules +++ b/src/cmd/compile/internal/ssa/_gen/MIPS.rules @@ -607,13 +607,13 @@ (Select0 (MULTU (MOVWconst [1]) _ )) => (MOVWconst [0]) (Select1 (MULTU (MOVWconst [-1]) x )) => (NEG x) (Select0 (MULTU (MOVWconst [-1]) x )) => (CMOVZ (ADDconst [-1] x) (MOVWconst [0]) x) -(Select1 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) => (SLLconst [int32(log2uint32(int64(c)))] x) -(Select0 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) => (SRLconst [int32(32-log2uint32(int64(c)))] x) +(Select1 (MULTU (MOVWconst [c]) x )) && isUnsignedPowerOfTwo(uint32(c)) => (SLLconst [int32(log32u(uint32(c)))] x) +(Select0 (MULTU (MOVWconst [c]) x )) && isUnsignedPowerOfTwo(uint32(c)) => (SRLconst [int32(32-log32u(uint32(c)))] x) (MUL (MOVWconst [0]) _ ) => (MOVWconst [0]) (MUL (MOVWconst [1]) x ) => 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 (ADD x (NEG y)) => (SUB x y) diff --git a/src/cmd/compile/internal/ssa/rewrite.go b/src/cmd/compile/internal/ssa/rewrite.go index 6229992bd1..f6bd4cee57 100644 --- a/src/cmd/compile/internal/ssa/rewrite.go +++ b/src/cmd/compile/internal/ssa/rewrite.go @@ -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 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. func isPowerOfTwo[T int8 | int16 | int32 | int64](n T) bool { return n > 0 && n&(n-1) == 0 diff --git a/src/cmd/compile/internal/ssa/rewriteMIPS.go b/src/cmd/compile/internal/ssa/rewriteMIPS.go index 4c5edb8694..97847f0823 100644 --- a/src/cmd/compile/internal/ssa/rewriteMIPS.go +++ b/src/cmd/compile/internal/ssa/rewriteMIPS.go @@ -4058,8 +4058,8 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool { break } // match: (MUL (MOVWconst [c]) x ) - // cond: isPowerOfTwo(int64(uint32(c))) - // result: (SLLconst [int32(log2uint32(int64(c)))] x) + // cond: isUnsignedPowerOfTwo(uint32(c)) + // result: (SLLconst [int32(log32u(uint32(c)))] x) for { for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 { if v_0.Op != OpMIPSMOVWconst { @@ -4067,11 +4067,11 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool { } c := auxIntToInt32(v_0.AuxInt) x := v_1 - if !(isPowerOfTwo(int64(uint32(c)))) { + if !(isUnsignedPowerOfTwo(uint32(c))) { continue } v.reset(OpMIPSSLLconst) - v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c)))) + v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c)))) v.AddArg(x) return true } @@ -6611,8 +6611,8 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool { break } // match: (Select0 (MULTU (MOVWconst [c]) x )) - // cond: isPowerOfTwo(int64(uint32(c))) - // result: (SRLconst [int32(32-log2uint32(int64(c)))] x) + // cond: isUnsignedPowerOfTwo(uint32(c)) + // result: (SRLconst [int32(32-log32u(uint32(c)))] x) for { if v_0.Op != OpMIPSMULTU { break @@ -6626,11 +6626,11 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool { } c := auxIntToInt32(v_0_0.AuxInt) x := v_0_1 - if !(isPowerOfTwo(int64(uint32(c)))) { + if !(isUnsignedPowerOfTwo(uint32(c))) { continue } v.reset(OpMIPSSRLconst) - v.AuxInt = int32ToAuxInt(int32(32 - log2uint32(int64(c)))) + v.AuxInt = int32ToAuxInt(int32(32 - log32u(uint32(c)))) v.AddArg(x) return true } @@ -6807,8 +6807,8 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool { break } // match: (Select1 (MULTU (MOVWconst [c]) x )) - // cond: isPowerOfTwo(int64(uint32(c))) - // result: (SLLconst [int32(log2uint32(int64(c)))] x) + // cond: isUnsignedPowerOfTwo(uint32(c)) + // result: (SLLconst [int32(log32u(uint32(c)))] x) for { if v_0.Op != OpMIPSMULTU { break @@ -6822,11 +6822,11 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool { } c := auxIntToInt32(v_0_0.AuxInt) x := v_0_1 - if !(isPowerOfTwo(int64(uint32(c)))) { + if !(isUnsignedPowerOfTwo(uint32(c))) { continue } v.reset(OpMIPSSLLconst) - v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c)))) + v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c)))) v.AddArg(x) return true }