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
5package ssa
6
7import (
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.
14type 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	hdrInt64Slice []*[]int64
38}
39
40func (c *Cache) Reset() {
41	nv := sort.Search(len(c.values), func(i int) bool { return c.values[i].ID == 0 })
42	xv := c.values[:nv]
43	for i := range xv {
44		xv[i] = Value{}
45	}
46	nb := sort.Search(len(c.blocks), func(i int) bool { return c.blocks[i].ID == 0 })
47	xb := c.blocks[:nb]
48	for i := range xb {
49		xb[i] = Block{}
50	}
51	nl := sort.Search(len(c.locs), func(i int) bool { return c.locs[i] == nil })
52	xl := c.locs[:nl]
53	for i := range xl {
54		xl[i] = nil
55	}
56
57	// regalloc sets the length of c.regallocValues to whatever it may use,
58	// so clear according to length.
59	for i := range c.regallocValues {
60		c.regallocValues[i] = valState{}
61	}
62}
63