cmd/compile: add constant folding for bits.Add64

Change-Id: I0ed4ebeaaa68e274e5902485ccc1165c039440bd
Reviewed-on: https://go-review.googlesource.com/c/go/+/656275
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>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
This commit is contained in:
Jorropo 2025-03-09 16:39:36 +01:00
parent 4ff70cf868
commit b60b9cf21f
3 changed files with 47 additions and 0 deletions

View File

@ -74,6 +74,7 @@
(PopCount32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount32(uint32(c)))])
(PopCount16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount16(uint16(c)))])
(PopCount8 (Const8 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount8(uint8(c)))])
(Add64carry (Const64 <t> [x]) (Const64 [y]) (Const64 [c])) && c >= 0 && c <= 1 => (MakeTuple (Const64 <t> [bitsAdd64(x, y, c).sum]) (Const64 <t> [bitsAdd64(x, y, c).carry]))
(Trunc16to8 (ZeroExt8to16 x)) => x
(Trunc32to8 (ZeroExt8to32 x)) => x

View File

@ -2554,3 +2554,9 @@ func isDirectIface2(v *Value, depth int) bool {
}
return false
}
func bitsAdd64(x, y, carry int64) (r struct{ sum, carry int64 }) {
s, c := bits.Add64(uint64(x), uint64(y), uint64(carry))
r.sum, r.carry = int64(s), int64(c)
return
}

View File

@ -20,6 +20,8 @@ func rewriteValuegeneric(v *Value) bool {
return rewriteValuegeneric_OpAdd64(v)
case OpAdd64F:
return rewriteValuegeneric_OpAdd64F(v)
case OpAdd64carry:
return rewriteValuegeneric_OpAdd64carry(v)
case OpAdd8:
return rewriteValuegeneric_OpAdd8(v)
case OpAddPtr:
@ -2376,6 +2378,44 @@ func rewriteValuegeneric_OpAdd64F(v *Value) bool {
}
return false
}
func rewriteValuegeneric_OpAdd64carry(v *Value) bool {
v_2 := v.Args[2]
v_1 := v.Args[1]
v_0 := v.Args[0]
b := v.Block
// match: (Add64carry (Const64 <t> [x]) (Const64 [y]) (Const64 [c]))
// cond: c >= 0 && c <= 1
// result: (MakeTuple (Const64 <t> [bitsAdd64(x, y, c).sum]) (Const64 <t> [bitsAdd64(x, y, c).carry]))
for {
for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
if v_0.Op != OpConst64 {
continue
}
t := v_0.Type
x := auxIntToInt64(v_0.AuxInt)
if v_1.Op != OpConst64 {
continue
}
y := auxIntToInt64(v_1.AuxInt)
if v_2.Op != OpConst64 {
continue
}
c := auxIntToInt64(v_2.AuxInt)
if !(c >= 0 && c <= 1) {
continue
}
v.reset(OpMakeTuple)
v0 := b.NewValue0(v.Pos, OpConst64, t)
v0.AuxInt = int64ToAuxInt(bitsAdd64(x, y, c).sum)
v1 := b.NewValue0(v.Pos, OpConst64, t)
v1.AuxInt = int64ToAuxInt(bitsAdd64(x, y, c).carry)
v.AddArg2(v0, v1)
return true
}
break
}
return false
}
func rewriteValuegeneric_OpAdd8(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]