aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssa/stackalloc.go
blob: 3eb5c3cf4ae7633ffc8fb42747d9bdf9c496a6b5 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2015 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.

// TODO: live at start of block instead?

package ssa

// stackalloc allocates storage in the stack frame for
// all Values that did not get a register.
func stackalloc(f *Func) {
	// Cache value types by ID.
	types := make([]Type, f.NumValues())
	for _, b := range f.Blocks {
		for _, v := range b.Values {
			types[v.ID] = v.Type
		}
	}

	// Build interference graph among StoreReg and stack phi ops.
	live := f.liveSpills()
	interfere := make([][]ID, f.NumValues())
	s := newSparseSet(f.NumValues())
	for _, b := range f.Blocks {
		// Start with known live values at the end of the block.
		s.clear()
		for i := 0; i < len(b.Succs); i++ {
			s.addAll(live[b.ID][i])
		}

		// Propagate backwards to the start of the block.
		// Remember interfering sets.
		for i := len(b.Values) - 1; i >= 0; i-- {
			v := b.Values[i]
			switch {
			case v.Op == OpStoreReg, v.isStackPhi():
				s.remove(v.ID)
				for _, id := range s.contents() {
					if v.Type.Equal(types[id]) {
						// Only need interferences between equivalent types.
						interfere[v.ID] = append(interfere[v.ID], id)
						interfere[id] = append(interfere[id], v.ID)
					}
				}
			case v.Op == OpLoadReg:
				s.add(v.Args[0].ID)
			case v.Op == OpArg:
				// This is an input argument which is pre-spilled.  It is kind of
				// like a StoreReg, but we don't remove v.ID here because we want
				// this value to appear live even before this point.  Being live
				// all the way to the start of the entry block prevents other
				// values from being allocated to the same slot and clobbering
				// the input value before we have a chance to load it.
			}
		}
	}

	// Build map from values to their names, if any.
	// A value may be associated with more than one name (e.g. after
	// the assignment i=j). This step picks one name per value arbitrarily.
	names := make([]LocalSlot, f.NumValues())
	for _, name := range f.Names {
		// Note: not "range f.NamedValues" above, because
		// that would be nondeterministic.
		for _, v := range f.NamedValues[name] {
			names[v.ID] = name
		}
	}

	// Figure out which StoreReg ops are phi args.  We don't pick slots for
	// phi args because a stack phi and its args must all use the same stack slot.
	phiArg := make([]bool, f.NumValues())
	for _, b := range f.Blocks {
		for _, v := range b.Values {
			if !v.isStackPhi() {
				continue
			}
			for _, a := range v.Args {
				phiArg[a.ID] = true
			}
		}
	}

	// Allocate args to their assigned locations.
	for _, v := range f.Entry.Values {
		if v.Op != OpArg {
			continue
		}
		f.setHome(v, LocalSlot{v.Aux.(GCNode), v.Type, v.AuxInt})
	}

	// For each type, we keep track of all the stack slots we
	// have allocated for that type.
	locations := map[Type][]LocalSlot{}

	// Each time we assign a stack slot to a value v, we remember
	// the slot we used via an index into locations[v.Type].
	// TODO: share slots among equivalent types.
	slots := make([]int, f.NumValues())
	for i := f.NumValues() - 1; i >= 0; i-- {
		slots[i] = -1
	}

	// Pick a stack slot for each non-phi-arg StoreReg and each stack phi.
	used := make([]bool, f.NumValues())
	for _, b := range f.Blocks {
		for _, v := range b.Values {
			if v.Op != OpStoreReg && !v.isStackPhi() {
				continue
			}
			if phiArg[v.ID] {
				continue
			}

			// If this is a named value, try to use the name as
			// the spill location.
			var name LocalSlot
			if v.Op == OpStoreReg {
				name = names[v.Args[0].ID]
			} else {
				name = names[v.ID]
			}
			if name.N != nil && v.Type.Equal(name.Type) {
				for _, id := range interfere[v.ID] {
					h := f.getHome(id)
					if h != nil && h.(LocalSlot) == name {
						// A variable can interfere with itself.
						// It is rare, but but it can happen.
						goto noname
					}
				}
				if v.Op == OpPhi {
					for _, a := range v.Args {
						for _, id := range interfere[a.ID] {
							h := f.getHome(id)
							if h != nil && h.(LocalSlot) == name {
								goto noname
							}
						}
					}
				}
				f.setHome(v, name)
				if v.Op == OpPhi {
					for _, a := range v.Args {
						f.setHome(a, name)
					}
				}
				continue
			}

		noname:
			// Set of stack slots we could reuse.
			locs := locations[v.Type]
			// Mark all positions in locs used by interfering values.
			for i := 0; i < len(locs); i++ {
				used[i] = false
			}
			for _, xid := range interfere[v.ID] {
				slot := slots[xid]
				if slot >= 0 {
					used[slot] = true
				}
			}
			if v.Op == OpPhi {
				// Stack phi and args must get the same stack slot, so
				// anything the args interfere with is something the phi
				// interferes with.
				for _, a := range v.Args {
					for _, xid := range interfere[a.ID] {
						slot := slots[xid]
						if slot >= 0 {
							used[slot] = true
						}
					}
				}
			}
			// Find an unused stack slot.
			var i int
			for i = 0; i < len(locs); i++ {
				if !used[i] {
					break
				}
			}
			// If there is no unused stack slot, allocate a new one.
			if i == len(locs) {
				locs = append(locs, LocalSlot{N: f.Config.fe.Auto(v.Type), Type: v.Type, Off: 0})
				locations[v.Type] = locs
			}
			// Use the stack variable at that index for v.
			loc := locs[i]
			f.setHome(v, loc)
			slots[v.ID] = i
			if v.Op == OpPhi {
				for _, a := range v.Args {
					f.setHome(a, loc)
					slots[a.ID] = i
				}
			}
		}
	}
}

// live returns a map from block ID and successor edge index to a list
// of StoreReg/stackphi value IDs live on that edge.
// TODO: this could be quadratic if lots of variables are live across lots of
// basic blocks.  Figure out a way to make this function (or, more precisely, the user
// of this function) require only linear size & time.
func (f *Func) liveSpills() [][][]ID {
	live := make([][][]ID, f.NumBlocks())
	for _, b := range f.Blocks {
		live[b.ID] = make([][]ID, len(b.Succs))
	}
	var phis []*Value

	s := newSparseSet(f.NumValues())
	t := newSparseSet(f.NumValues())

	// Instead of iterating over f.Blocks, iterate over their postordering.
	// Liveness information flows backward, so starting at the end
	// increases the probability that we will stabilize quickly.
	po := postorder(f)
	for {
		changed := false
		for _, b := range po {
			// Start with known live values at the end of the block
			s.clear()
			for i := 0; i < len(b.Succs); i++ {
				s.addAll(live[b.ID][i])
			}

			// Propagate backwards to the start of the block
			phis = phis[:0]
			for i := len(b.Values) - 1; i >= 0; i-- {
				v := b.Values[i]
				switch {
				case v.Op == OpStoreReg:
					s.remove(v.ID)
				case v.Op == OpLoadReg:
					s.add(v.Args[0].ID)
				case v.isStackPhi():
					s.remove(v.ID)
					// save stack phi ops for later
					phis = append(phis, v)
				}
			}

			// for each predecessor of b, expand its list of live-at-end values
			// invariant: s contains the values live at the start of b (excluding phi inputs)
			for i, p := range b.Preds {
				// Find index of b in p's successors.
				var j int
				for j = 0; j < len(p.Succs); j++ {
					if p.Succs[j] == b {
						break
					}
				}
				t.clear()
				t.addAll(live[p.ID][j])
				t.addAll(s.contents())
				for _, v := range phis {
					t.add(v.Args[i].ID)
				}
				if t.size() == len(live[p.ID][j]) {
					continue
				}
				// grow p's live set
				live[p.ID][j] = append(live[p.ID][j][:0], t.contents()...)
				changed = true
			}
		}

		if !changed {
			break
		}
	}
	return live
}

func (f *Func) getHome(vid ID) Location {
	if int(vid) >= len(f.RegAlloc) {
		return nil
	}
	return f.RegAlloc[vid]
}

func (f *Func) setHome(v *Value, loc Location) {
	for v.ID >= ID(len(f.RegAlloc)) {
		f.RegAlloc = append(f.RegAlloc, nil)
	}
	f.RegAlloc[v.ID] = loc
}

func (v *Value) isStackPhi() bool {
	if v.Op != OpPhi {
		return false
	}
	if v.Type == TypeMem {
		return false
	}
	if int(v.ID) >= len(v.Block.Func.RegAlloc) {
		return true
	}
	return v.Block.Func.RegAlloc[v.ID] == nil
	// TODO: use a separate opcode for StackPhi?
}