cmd/compile: use the builtin clear

To simplify the code a bit.

Change-Id: Ia72f576de59ff161ec389a4992bb635f89783540
GitHub-Last-Rev: eaec8216be964418a085649fcca53a042f28ce1a
GitHub-Pull-Request: golang/go#73411
Reviewed-on: https://go-review.googlesource.com/c/go/+/666117
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
apocelipes 2025-04-17 07:49:35 +00:00 committed by Gopher Robot
parent b89988c5ca
commit 8a8efafa88
16 changed files with 28 additions and 92 deletions

View File

@ -196,7 +196,5 @@ func (bv BitVec) String() string {
} }
func (bv BitVec) Clear() { func (bv BitVec) Clear() {
for i := range bv.B { clear(bv.B)
bv.B[i] = 0
}
} }

View File

@ -1414,9 +1414,7 @@ func Compute(curfn *ir.Func, f *ssa.Func, stkptrsize int64, pp *objw.Progs, retL
{ {
cache := f.Cache.Liveness.(*livenessFuncCache) cache := f.Cache.Liveness.(*livenessFuncCache)
if cap(lv.be) < 2000 { // Threshold from ssa.Cache slices. if cap(lv.be) < 2000 { // Threshold from ssa.Cache slices.
for i := range lv.be { clear(lv.be)
lv.be[i] = blockEffects{}
}
cache.be = lv.be cache.be = lv.be
} }
if len(lv.livenessMap.Vals) < 2000 { if len(lv.livenessMap.Vals) < 2000 {

View File

@ -116,10 +116,7 @@ func (pp *Progs) Flush() {
func (pp *Progs) Free() { func (pp *Progs) Free() {
if base.Ctxt.CanReuseProgs() { if base.Ctxt.CanReuseProgs() {
// Clear progs to enable GC and avoid abuse. // Clear progs to enable GC and avoid abuse.
s := pp.Cache[:pp.CacheIndex] clear(pp.Cache[:pp.CacheIndex])
for i := range s {
s[i] = obj.Prog{}
}
} }
// Clear pp to avoid abuse. // Clear pp to avoid abuse.
*pp = Progs{} *pp = Progs{}

View File

@ -1284,9 +1284,7 @@ func dgcptrmask(t *types.Type, write bool) *obj.LSym {
// word offsets in t that hold pointers. // word offsets in t that hold pointers.
// ptrmask is assumed to fit at least types.PtrDataSize(t)/PtrSize bits. // ptrmask is assumed to fit at least types.PtrDataSize(t)/PtrSize bits.
func fillptrmask(t *types.Type, ptrmask []byte) { func fillptrmask(t *types.Type, ptrmask []byte) {
for i := range ptrmask { clear(ptrmask)
ptrmask[i] = 0
}
if !t.HasPointers() { if !t.HasPointers() {
return return
} }

View File

@ -41,7 +41,7 @@ func genAllocators() {
capacity: "cap(%s)", capacity: "cap(%s)",
mak: "make([]*Value, %s)", mak: "make([]*Value, %s)",
resize: "%s[:%s]", resize: "%s[:%s]",
clear: "for i := range %[1]s {\n%[1]s[i] = nil\n}", clear: "clear(%s)",
minLog: 5, minLog: 5,
maxLog: 32, maxLog: 32,
}, },
@ -51,7 +51,7 @@ func genAllocators() {
capacity: "cap(%s)", capacity: "cap(%s)",
mak: "make([]limit, %s)", mak: "make([]limit, %s)",
resize: "%s[:%s]", resize: "%s[:%s]",
clear: "for i := range %[1]s {\n%[1]s[i] = limit{}\n}", clear: "clear(%s)",
minLog: 3, minLog: 3,
maxLog: 30, maxLog: 30,
}, },

View File

@ -31,9 +31,7 @@ func (c *Cache) allocValueSlice(n int) []*Value {
return s return s
} }
func (c *Cache) freeValueSlice(s []*Value) { func (c *Cache) freeValueSlice(s []*Value) {
for i := range s { clear(s)
s[i] = nil
}
b := bits.Len(uint(cap(s)) - 1) b := bits.Len(uint(cap(s)) - 1)
var sp *[]*Value var sp *[]*Value
if len(c.hdrValueSlice) == 0 { if len(c.hdrValueSlice) == 0 {
@ -69,9 +67,7 @@ func (c *Cache) allocLimitSlice(n int) []limit {
return s return s
} }
func (c *Cache) freeLimitSlice(s []limit) { func (c *Cache) freeLimitSlice(s []limit) {
for i := range s { clear(s)
s[i] = limit{}
}
b := bits.Len(uint(cap(s)) - 1) b := bits.Len(uint(cap(s)) - 1)
var sp *[]limit var sp *[]limit
if len(c.hdrLimitSlice) == 0 { if len(c.hdrLimitSlice) == 0 {

View File

@ -264,10 +264,7 @@ func (b *Block) resetWithControl2(kind BlockKind, v, w *Value) {
// The values in b.Values after i must already have had their args reset, // The values in b.Values after i must already have had their args reset,
// to maintain correct value uses counts. // to maintain correct value uses counts.
func (b *Block) truncateValues(i int) { func (b *Block) truncateValues(i int) {
tail := b.Values[i:] clear(b.Values[i:])
for j := range tail {
tail[j] = nil
}
b.Values = b.Values[:i] b.Values = b.Values[:i]
} }

View File

@ -39,24 +39,13 @@ type Cache struct {
func (c *Cache) Reset() { func (c *Cache) Reset() {
nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 }) nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 })
xv := c.values[:nv] clear(c.values[:nv])
for i := range xv {
xv[i] = Value{}
}
nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 }) nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 })
xb := c.blocks[:nb] clear(c.blocks[:nb])
for i := range xb {
xb[i] = Block{}
}
nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil }) nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil })
xl := c.locs[:nl] clear(c.locs[:nl])
for i := range xl {
xl[i] = nil
}
// regalloc sets the length of c.regallocValues to whatever it may use, // regalloc sets the length of c.regallocValues to whatever it may use,
// so clear according to length. // so clear according to length.
for i := range c.regallocValues { clear(c.regallocValues)
c.regallocValues[i] = valState{}
}
} }

View File

@ -232,10 +232,7 @@ func deadcode(f *Func) {
f.NamedValues[*name] = values[:j] f.NamedValues[*name] = values[:j]
} }
} }
clearNames := f.Names[i:] clear(f.Names[i:])
for j := range clearNames {
clearNames[j] = nil
}
f.Names = f.Names[:i] f.Names = f.Names[:i]
pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
@ -303,10 +300,7 @@ func deadcode(f *Func) {
} }
} }
// zero remainder to help GC // zero remainder to help GC
tail := f.Blocks[i:] clear(f.Blocks[i:])
for j := range tail {
tail[j] = nil
}
f.Blocks = f.Blocks[:i] f.Blocks = f.Blocks[:i]
} }

View File

@ -102,9 +102,7 @@ type stateAtPC struct {
// reset fills state with the live variables from live. // reset fills state with the live variables from live.
func (state *stateAtPC) reset(live abt.T) { func (state *stateAtPC) reset(live abt.T) {
slots, registers := state.slots, state.registers slots, registers := state.slots, state.registers
for i := range slots { clear(slots)
slots[i] = VarLoc{}
}
for i := range registers { for i := range registers {
registers[i] = registers[i][:0] registers[i] = registers[i][:0]
} }
@ -242,12 +240,7 @@ func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
if cap(state.blockDebug) < f.NumBlocks() { if cap(state.blockDebug) < f.NumBlocks() {
state.blockDebug = make([]BlockDebug, f.NumBlocks()) state.blockDebug = make([]BlockDebug, f.NumBlocks())
} else { } else {
// This local variable, and the ones like it below, enable compiler clear(state.blockDebug[:f.NumBlocks()])
// optimizations. Don't inline them.
b := state.blockDebug[:f.NumBlocks()]
for i := range b {
b[i] = BlockDebug{}
}
} }
// A list of slots per Value. Reuse the previous child slices. // A list of slots per Value. Reuse the previous child slices.
@ -285,10 +278,7 @@ func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
if cap(state.pendingSlotLocs) < numPieces { if cap(state.pendingSlotLocs) < numPieces {
state.pendingSlotLocs = make([]VarLoc, numPieces) state.pendingSlotLocs = make([]VarLoc, numPieces)
} else { } else {
psl := state.pendingSlotLocs[:numPieces] clear(state.pendingSlotLocs[:numPieces])
for i := range psl {
psl[i] = VarLoc{}
}
} }
if cap(state.pendingEntries) < numVars { if cap(state.pendingEntries) < numVars {
state.pendingEntries = make([]pendingEntry, numVars) state.pendingEntries = make([]pendingEntry, numVars)
@ -307,9 +297,7 @@ func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
state.lists = make([][]byte, numVars) state.lists = make([][]byte, numVars)
} else { } else {
state.lists = state.lists[:numVars] state.lists = state.lists[:numVars]
for i := range state.lists { clear(state.lists)
state.lists[i] = nil
}
} }
} }
@ -1191,9 +1179,7 @@ func (e *pendingEntry) clear() {
e.present = false e.present = false
e.startBlock = 0 e.startBlock = 0
e.startValue = 0 e.startValue = 0
for i := range e.pieces { clear(e.pieces)
e.pieces[i] = VarLoc{}
}
} }
// canMerge reports whether a new location description is a superset // canMerge reports whether a new location description is a superset

View File

@ -23,9 +23,7 @@ func newBitset(n int) bitset {
} }
func (bs bitset) Reset() { func (bs bitset) Reset() {
for i := range bs { clear(bs)
bs[i] = 0
}
} }
func (bs bitset) Set(idx uint32) { func (bs bitset) Set(idx uint32) {

View File

@ -1301,9 +1301,7 @@ func (s *regAllocState) regalloc(f *Func) {
dinfo = make([]dentry, l) dinfo = make([]dentry, l)
} else { } else {
dinfo = dinfo[:l] dinfo = dinfo[:l]
for i := range dinfo { clear(dinfo)
dinfo[i] = dentry{}
}
} }
// Load static desired register info at the end of the block. // Load static desired register info at the end of the block.

View File

@ -46,15 +46,9 @@ func newStackAllocState(f *Func) *stackAllocState {
} }
func putStackAllocState(s *stackAllocState) { func putStackAllocState(s *stackAllocState) {
for i := range s.values { clear(s.values)
s.values[i] = stackValState{} clear(s.interfere)
} clear(s.names)
for i := range s.interfere {
s.interfere[i] = nil
}
for i := range s.names {
s.names[i] = LocalSlot{}
}
s.f.Cache.stackAllocState = s s.f.Cache.stackAllocState = s
s.f = nil s.f = nil
s.live = nil s.live = nil

View File

@ -85,9 +85,7 @@ func tighten(f *Func) {
changed = false changed = false
// Reset target // Reset target
for i := range target { clear(target)
target[i] = nil
}
// Compute target locations (for moveable values only). // Compute target locations (for moveable values only).
// target location = the least common ancestor of all uses in the dominator tree. // target location = the least common ancestor of all uses in the dominator tree.

View File

@ -109,10 +109,7 @@ func trim(f *Func) {
} }
if n < len(f.Blocks) { if n < len(f.Blocks) {
f.invalidateCFG() f.invalidateCFG()
tail := f.Blocks[n:] clear(f.Blocks[n:])
for i := range tail {
tail[i] = nil
}
f.Blocks = f.Blocks[:n] f.Blocks = f.Blocks[:n]
} }
} }

View File

@ -6512,9 +6512,7 @@ func genssa(f *ssa.Func, pp *objw.Progs) {
f.Cache.ValueToProgAfter = make([]*obj.Prog, f.NumValues()) f.Cache.ValueToProgAfter = make([]*obj.Prog, f.NumValues())
} }
valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()] valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()]
for i := range valueToProgAfter { clear(valueToProgAfter)
valueToProgAfter[i] = nil
}
} }
// If the very first instruction is not tagged as a statement, // If the very first instruction is not tagged as a statement,