cmd/compile: improve known bits debug print

Visualize the known bits on screen.

Change-Id: I9a291af21dbbde4a0c3a5be93be914e7a9850d61
Reviewed-on: https://go-review.googlesource.com/c/go/+/778080
Auto-Submit: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Jorropo 2026-05-15 01:15:40 +02:00 committed by Gopher Robot
parent 7d2eb15103
commit e73e73470e
2 changed files with 24 additions and 5 deletions

View file

@ -4,7 +4,10 @@
package ssa
import "slices"
import (
"slices"
"strings"
)
func (kb *knownBitsState) fold(v *Value) (value, known int64) {
if kb.seenValues.Test(uint32(v.ID)) {
@ -37,11 +40,11 @@ func (kb *knownBitsState) fold(v *Value) (value, known int64) {
// 1. unknown bits are always set to 0 inside value
value &= known
if v.Block.Func.pass.debug > 1 {
v.Block.Func.Warnl(v.Pos, "known bits state %v: k:%d v:%d", v, known, value)
}
kb.entries[v.ID].known = known
kb.entries[v.ID].value = value
if v.Block.Func.pass.debug > 1 {
v.Block.Func.Warnl(v.Pos, "known bits state %v: %v", v, kb.entries[v.ID])
}
}()
kb.seenValues.Set(uint32(v.ID)) // set seen early to give up on loops
@ -228,6 +231,22 @@ type knownBitsEntry struct {
known, value int64
}
func (kbe knownBitsEntry) String() string {
lut := []rune{ // indexed by knownBit<<1 | valueBit
0b00: '?',
0b01: '¿', // violates invariant 1
0b10: '0',
0b11: '1',
}
var sb strings.Builder
sb.Grow(64)
for i := 63; i >= 0; i-- {
bits := (kbe.known>>i&1)<<1 | (kbe.value >> i & 1)
sb.WriteRune(lut[bits])
}
return sb.String()
}
func (kb *knownBitsState) isLiveInEdge(b *Block, index uint) bool {
inEdge := b.Preds[index]
return kb.isLiveOutEdge(inEdge.b, uint(inEdge.i))

View file

@ -41,7 +41,7 @@ func TestAllPossibleValues(t *testing.T) {
known := i | ^tryMask
for value := range allPossibleValuesRejection(0, unknown, tryMask) { // don't use allPossibleValues since it's what we are about to test.
t.Run(fmt.Sprintf("known=%b,value=%b", uint64(known), uint64(value)), func(t *testing.T) {
t.Run(fmt.Sprintf("%v", knownBitsEntry{known: known, value: value}), func(t *testing.T) {
truth, truthStop := iter.Pull(allPossibleValuesRejection(value, known, tryMask))
defer truthStop()
dut, dutStop := iter.Pull(allPossibleValues(value, known))