cmd/compile: add unsigned power-of-two detector

Fixes #74485

Change-Id: Ia22a58ac43bdc36c8414d555672a3a3eafc749ca
Reviewed-on: https://go-review.googlesource.com/c/go/+/689815
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Cuong Manh Le 2025-07-23 18:48:18 +07:00 committed by Gopher Robot
parent f7d167fe71
commit f3582fc80e
4 changed files with 59 additions and 34 deletions

View file

@ -6990,20 +6990,20 @@ func rewriteValuegeneric_OpDiv16u(v *Value) bool {
return true
}
// match: (Div16u n (Const16 [c]))
// cond: isPowerOfTwo(c)
// result: (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
// cond: isUnsignedPowerOfTwo(uint16(c))
// result: (Rsh16Ux64 n (Const64 <typ.UInt64> [log16u(uint16(c))]))
for {
n := v_0
if v_1.Op != OpConst16 {
break
}
c := auxIntToInt16(v_1.AuxInt)
if !(isPowerOfTwo(c)) {
if !(isUnsignedPowerOfTwo(uint16(c))) {
break
}
v.reset(OpRsh16Ux64)
v0 := b.NewValue0(v.Pos, OpConst64, typ.UInt64)
v0.AuxInt = int64ToAuxInt(log16(c))
v0.AuxInt = int64ToAuxInt(log16u(uint16(c)))
v.AddArg2(n, v0)
return true
}
@ -7400,20 +7400,20 @@ func rewriteValuegeneric_OpDiv32u(v *Value) bool {
return true
}
// match: (Div32u n (Const32 [c]))
// cond: isPowerOfTwo(c)
// result: (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
// cond: isUnsignedPowerOfTwo(uint32(c))
// result: (Rsh32Ux64 n (Const64 <typ.UInt64> [log32u(uint32(c))]))
for {
n := v_0
if v_1.Op != OpConst32 {
break
}
c := auxIntToInt32(v_1.AuxInt)
if !(isPowerOfTwo(c)) {
if !(isUnsignedPowerOfTwo(uint32(c))) {
break
}
v.reset(OpRsh32Ux64)
v0 := b.NewValue0(v.Pos, OpConst64, typ.UInt64)
v0.AuxInt = int64ToAuxInt(log32(c))
v0.AuxInt = int64ToAuxInt(log32u(uint32(c)))
v.AddArg2(n, v0)
return true
}
@ -7839,33 +7839,20 @@ func rewriteValuegeneric_OpDiv64u(v *Value) bool {
return true
}
// match: (Div64u n (Const64 [c]))
// cond: isPowerOfTwo(c)
// result: (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
// cond: isUnsignedPowerOfTwo(uint64(c))
// result: (Rsh64Ux64 n (Const64 <typ.UInt64> [log64u(uint64(c))]))
for {
n := v_0
if v_1.Op != OpConst64 {
break
}
c := auxIntToInt64(v_1.AuxInt)
if !(isPowerOfTwo(c)) {
if !(isUnsignedPowerOfTwo(uint64(c))) {
break
}
v.reset(OpRsh64Ux64)
v0 := b.NewValue0(v.Pos, OpConst64, typ.UInt64)
v0.AuxInt = int64ToAuxInt(log64(c))
v.AddArg2(n, v0)
return true
}
// match: (Div64u n (Const64 [-1<<63]))
// result: (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
for {
n := v_0
if v_1.Op != OpConst64 || auxIntToInt64(v_1.AuxInt) != -1<<63 {
break
}
v.reset(OpRsh64Ux64)
v0 := b.NewValue0(v.Pos, OpConst64, typ.UInt64)
v0.AuxInt = int64ToAuxInt(63)
v0.AuxInt = int64ToAuxInt(log64u(uint64(c)))
v.AddArg2(n, v0)
return true
}
@ -8175,20 +8162,20 @@ func rewriteValuegeneric_OpDiv8u(v *Value) bool {
return true
}
// match: (Div8u n (Const8 [c]))
// cond: isPowerOfTwo(c)
// result: (Rsh8Ux64 n (Const64 <typ.UInt64> [log8(c)]))
// cond: isUnsignedPowerOfTwo(uint8(c))
// result: (Rsh8Ux64 n (Const64 <typ.UInt64> [log8u(uint8(c))]))
for {
n := v_0
if v_1.Op != OpConst8 {
break
}
c := auxIntToInt8(v_1.AuxInt)
if !(isPowerOfTwo(c)) {
if !(isUnsignedPowerOfTwo(uint8(c))) {
break
}
v.reset(OpRsh8Ux64)
v0 := b.NewValue0(v.Pos, OpConst64, typ.UInt64)
v0.AuxInt = int64ToAuxInt(log8(c))
v0.AuxInt = int64ToAuxInt(log8u(uint8(c)))
v.AddArg2(n, v0)
return true
}