Source file src/cmd/compile/internal/ssa/cache.go

     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  	"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  	domblockstore []ID         // scratch space for computing dominators
    25  	scrSparseSet  []*sparseSet // scratch sparse sets to be re-used.
    26  	scrSparseMap  []*sparseMap // scratch sparse maps to be re-used.
    27  	scrPoset      []*poset     // scratch poset to be reused
    28  	// deadcode contains reusable slices specifically for the deadcode pass.
    29  	// It gets special treatment because of the frequency with which it is run.
    30  	deadcode struct {
    31  		liveOrderStmts []*Value
    32  		live           []bool
    33  		q              []*Value
    34  	}
    35  	// Reusable regalloc state.
    36  	regallocValues []valState
    37  
    38  	ValueToProgAfter []*obj.Prog
    39  	debugState       debugState
    40  
    41  	Liveness interface{} // *gc.livenessFuncCache
    42  }
    43  
    44  func (c *Cache) Reset() {
    45  	nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 })
    46  	xv := c.values[:nv]
    47  	for i := range xv {
    48  		xv[i] = Value{}
    49  	}
    50  	nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 })
    51  	xb := c.blocks[:nb]
    52  	for i := range xb {
    53  		xb[i] = Block{}
    54  	}
    55  	nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil })
    56  	xl := c.locs[:nl]
    57  	for i := range xl {
    58  		xl[i] = nil
    59  	}
    60  
    61  	// regalloc sets the length of c.regallocValues to whatever it may use,
    62  	// so clear according to length.
    63  	for i := range c.regallocValues {
    64  		c.regallocValues[i] = valState{}
    65  	}
    66  
    67  	// liveOrderStmts gets used multiple times during compilation of a function.
    68  	// We don't know where the high water mark was, so reslice to cap and search.
    69  	c.deadcode.liveOrderStmts = c.deadcode.liveOrderStmts[:cap(c.deadcode.liveOrderStmts)]
    70  	no := sort.Search(len(c.deadcode.liveOrderStmts), func(i int) bool { return c.deadcode.liveOrderStmts[i] == nil })
    71  	xo := c.deadcode.liveOrderStmts[:no]
    72  	for i := range xo {
    73  		xo[i] = nil
    74  	}
    75  	c.deadcode.q = c.deadcode.q[:cap(c.deadcode.q)]
    76  	nq := sort.Search(len(c.deadcode.q), func(i int) bool { return c.deadcode.q[i] == nil })
    77  	xq := c.deadcode.q[:nq]
    78  	for i := range xq {
    79  		xq[i] = nil
    80  	}
    81  }
    82  

View as plain text