]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use the builtin clear
authorapocelipes <seve3r@outlook.com>
Thu, 17 Apr 2025 07:49:35 +0000 (07:49 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 18 Apr 2025 11:21:12 +0000 (04:21 -0700)
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>
16 files changed:
src/cmd/compile/internal/bitvec/bv.go
src/cmd/compile/internal/liveness/plive.go
src/cmd/compile/internal/objw/prog.go
src/cmd/compile/internal/reflectdata/reflect.go
src/cmd/compile/internal/ssa/_gen/allocators.go
src/cmd/compile/internal/ssa/allocators.go
src/cmd/compile/internal/ssa/block.go
src/cmd/compile/internal/ssa/cache.go
src/cmd/compile/internal/ssa/deadcode.go
src/cmd/compile/internal/ssa/debug.go
src/cmd/compile/internal/ssa/poset.go
src/cmd/compile/internal/ssa/regalloc.go
src/cmd/compile/internal/ssa/stackalloc.go
src/cmd/compile/internal/ssa/tighten.go
src/cmd/compile/internal/ssa/trim.go
src/cmd/compile/internal/ssagen/ssa.go

index aab10433c8f181d6ce6873f70d337d02998baf43..d3133dcd2dc679912d41d3f5452f9bd0b1d8f6bb 100644 (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)
 }
index 6c97858cf6efbb2ccfc616cd05b23856740bdf7c..5a2a22ee8f5c82fe8a12d013ae5f0657a4bc83d7 100644 (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 {
index 84fb9967235ea0a6f9ae8f466365a40bfacad847..753fd8615ca9c53e84c1b119dcefc335cb8d0bf8 100644 (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{}
index 4202ff3358f51dbbca3a90cfc152261a5fddf428..fb60569670ea662d042a5eb57dc9e21171341a03 100644 (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
        }
index 56e6d69a312e1d5ab974fbffb8b75e74d688b14f..682fc5f20217e94350e907139a1584314b3963e3 100644 (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,
                },
index 222ae73f2b80e0e3860cdcf1d955f2d518300a2a..10b1c582801db21bf031718a1d53a662b0013c54 100644 (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 {
index 0c9aea8f96844d790a98d314b752d7bdb38d6ad3..1240bfd6556a1108a2717cb72726097bebdcab01 100644 (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]
 }
 
index fd19cb3c0980a0fc0db81b9e657369b66f0d5b4c..0c16efcd57d6acbb990d99ba08b81f448db5a985 100644 (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)
 }
index 3bd1737babbb252aaba47ea094ef3af40c28222c..1aa0f9e29603e9ead2dbb93109316eafc9640ada 100644 (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]
 }
 
index 6faef7c25592e66582406014a145de50edd3757f..aa503eda879ea14f64f801871c59290bf4d0f4a3 100644 (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
index b7859166f48a98bdb3532fd062836bde79ae16b0..f4f75fbe09d8c713e777628fd5ef12732d956620 100644 (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) {
index f52dad8a5d2fce7c69c496bb20e988eb82ea0179..2981bceb2e6910e330aceb9def501eb8f3072260 100644 (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.
index 8290e1730efbe9aae80c360ef77f41861087b210..11ffe5b55ea65fed0bdea4660530b9352c1a8fae 100644 (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
index f3056d42aa8171206338eb0f7bf63d71793beba3..93bb8d048ac3daa5e16886745ae4304d6218ea0e 100644 (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.
index 13798c6c397eb82513cb29b68607cc7ea46e58bc..a607a57a76e88aa90e795855f69c7d900e18da51 100644 (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]
        }
 }
index b0e0262d450e201b813ae53008329dfad05bf0a2..80e91436bbfd2e6914fd4b5c5482adb7293a8833 100644 (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,