aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssa/cache.go
blob: 8434084bde28e4a2fa680c6137f2ba4e70d388fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ssa

import "sort"

// A Cache holds reusable compiler state.
// It is intended to be re-used for multiple Func compilations.
type Cache struct {
	// Storage for low-numbered values and blocks.
	values [2000]Value
	blocks [200]Block
	locs   [2000]Location

	// Storage for DWARF variable locations. Lazily allocated
	// since location lists are off by default.
	varLocs   []VarLoc
	curVarLoc int

	// Reusable stackAllocState.
	// See stackalloc.go's {new,put}StackAllocState.
	stackAllocState *stackAllocState

	domblockstore []ID         // scratch space for computing dominators
	scrSparse     []*sparseSet // scratch sparse sets to be re-used.
}

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{}
	}
	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{}
	}
	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
	}
	xvl := c.varLocs[:c.curVarLoc]
	for i := range xvl {
		xvl[i] = VarLoc{}
	}
	c.curVarLoc = 0
}

func (c *Cache) NewVarLoc() *VarLoc {
	if c.varLocs == nil {
		c.varLocs = make([]VarLoc, 4000)
	}
	if c.curVarLoc == len(c.varLocs) {
		return &VarLoc{}
	}
	vl := &c.varLocs[c.curVarLoc]
	c.curVarLoc++
	return vl
}