github.com/bir3/gocompiler@v0.3.205/src/cmd/compile/internal/ssa/cache.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ssa
     6  
     7  import (
     8  	"github.com/bir3/gocompiler/src/cmd/internal/obj"
     9  	"sort"
    10  )
    11  
    12  // A Cache holds reusable compiler state.
    13  // It is intended to be re-used for multiple Func compilations.
    14  type Cache struct {
    15  	// Storage for low-numbered values and blocks.
    16  	values [2000]Value
    17  	blocks [200]Block
    18  	locs   [2000]Location
    19  
    20  	// Reusable stackAllocState.
    21  	// See stackalloc.go's {new,put}StackAllocState.
    22  	stackAllocState *stackAllocState
    23  
    24  	scrPoset []*poset // scratch poset to be reused
    25  
    26  	// Reusable regalloc state.
    27  	regallocValues []valState
    28  
    29  	ValueToProgAfter []*obj.Prog
    30  	debugState       debugState
    31  
    32  	Liveness interface{} // *gc.livenessFuncCache
    33  
    34  	// Free "headers" for use by the allocators in allocators.go.
    35  	// Used to put slices in sync.Pools without allocation.
    36  	hdrValueSlice []*[]*Value
    37  	hdrBlockSlice []*[]*Block
    38  	hdrBoolSlice  []*[]bool
    39  	hdrIntSlice   []*[]int
    40  	hdrInt32Slice []*[]int32
    41  	hdrInt8Slice  []*[]int8
    42  	hdrIDSlice    []*[]ID
    43  }
    44  
    45  func (c *Cache) Reset() {
    46  	nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 })
    47  	xv := c.values[:nv]
    48  	for i := range xv {
    49  		xv[i] = Value{}
    50  	}
    51  	nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 })
    52  	xb := c.blocks[:nb]
    53  	for i := range xb {
    54  		xb[i] = Block{}
    55  	}
    56  	nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil })
    57  	xl := c.locs[:nl]
    58  	for i := range xl {
    59  		xl[i] = nil
    60  	}
    61  
    62  	// regalloc sets the length of c.regallocValues to whatever it may use,
    63  	// so clear according to length.
    64  	for i := range c.regallocValues {
    65  		c.regallocValues[i] = valState{}
    66  	}
    67  }