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() {
for i := range bv.B {
bv.B[i] = 0
}
clear(bv.B)
}

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)
if cap(lv.be) < 2000 { // Threshold from ssa.Cache slices.
for i := range lv.be {
lv.be[i] = blockEffects{}
}
clear(lv.be)
cache.be = lv.be
}
if len(lv.livenessMap.Vals) < 2000 {

View File

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

View File

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

View File

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

View File

@ -31,9 +31,7 @@ func (c *Cache) allocValueSlice(n int) []*Value {
return s
}
func (c *Cache) freeValueSlice(s []*Value) {
for i := range s {
s[i] = nil
}
clear(s)
b := bits.Len(uint(cap(s)) - 1)
var sp *[]*Value
if len(c.hdrValueSlice) == 0 {
@ -69,9 +67,7 @@ func (c *Cache) allocLimitSlice(n int) []limit {
return s
}
func (c *Cache) freeLimitSlice(s []limit) {
for i := range s {
s[i] = limit{}
}
clear(s)
b := bits.Len(uint(cap(s)) - 1)
var sp *[]limit
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,
// to maintain correct value uses counts.
func (b *Block) truncateValues(i int) {
tail := b.Values[i:]
for j := range tail {
tail[j] = nil
}
clear(b.Values[i:])
b.Values = b.Values[:i]
}

View File

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

View File

@ -232,10 +232,7 @@ func deadcode(f *Func) {
f.NamedValues[*name] = values[:j]
}
}
clearNames := f.Names[i:]
for j := range clearNames {
clearNames[j] = nil
}
clear(f.Names[i:])
f.Names = f.Names[:i]
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
tail := f.Blocks[i:]
for j := range tail {
tail[j] = nil
}
clear(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.
func (state *stateAtPC) reset(live abt.T) {
slots, registers := state.slots, state.registers
for i := range slots {
slots[i] = VarLoc{}
}
clear(slots)
for i := range registers {
registers[i] = registers[i][:0]
}
@ -242,12 +240,7 @@ func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
if cap(state.blockDebug) < f.NumBlocks() {
state.blockDebug = make([]BlockDebug, f.NumBlocks())
} else {
// This local variable, and the ones like it below, enable compiler
// optimizations. Don't inline them.
b := state.blockDebug[:f.NumBlocks()]
for i := range b {
b[i] = BlockDebug{}
}
clear(state.blockDebug[:f.NumBlocks()])
}
// 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 {
state.pendingSlotLocs = make([]VarLoc, numPieces)
} else {
psl := state.pendingSlotLocs[:numPieces]
for i := range psl {
psl[i] = VarLoc{}
}
clear(state.pendingSlotLocs[:numPieces])
}
if cap(state.pendingEntries) < numVars {
state.pendingEntries = make([]pendingEntry, numVars)
@ -307,9 +297,7 @@ func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
state.lists = make([][]byte, numVars)
} else {
state.lists = state.lists[:numVars]
for i := range state.lists {
state.lists[i] = nil
}
clear(state.lists)
}
}
@ -1191,9 +1179,7 @@ func (e *pendingEntry) clear() {
e.present = false
e.startBlock = 0
e.startValue = 0
for i := range e.pieces {
e.pieces[i] = VarLoc{}
}
clear(e.pieces)
}
// 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() {
for i := range bs {
bs[i] = 0
}
clear(bs)
}
func (bs bitset) Set(idx uint32) {

View File

@ -1301,9 +1301,7 @@ func (s *regAllocState) regalloc(f *Func) {
dinfo = make([]dentry, l)
} else {
dinfo = dinfo[:l]
for i := range dinfo {
dinfo[i] = dentry{}
}
clear(dinfo)
}
// 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) {
for i := range s.values {
s.values[i] = stackValState{}
}
for i := range s.interfere {
s.interfere[i] = nil
}
for i := range s.names {
s.names[i] = LocalSlot{}
}
clear(s.values)
clear(s.interfere)
clear(s.names)
s.f.Cache.stackAllocState = s
s.f = nil
s.live = nil

View File

@ -85,9 +85,7 @@ func tighten(f *Func) {
changed = false
// Reset target
for i := range target {
target[i] = nil
}
clear(target)
// Compute target locations (for moveable values only).
// 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) {
f.invalidateCFG()
tail := f.Blocks[n:]
for i := range tail {
tail[i] = nil
}
clear(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())
}
valueToProgAfter = f.Cache.ValueToProgAfter[:f.NumValues()]
for i := range valueToProgAfter {
valueToProgAfter[i] = nil
}
clear(valueToProgAfter)
}
// If the very first instruction is not tagged as a statement,