Debian Codegraph @

src/cmd/compile/internal/liveness/mergelocals.go

raw
1 // Copyright 2024 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 liveness
6
7 import (
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/bitvec"
10 "cmd/compile/internal/ir"
11 "cmd/compile/internal/ssa"
12 "cmd/internal/src"
13 "fmt"
14 "os"
15 "path/filepath"
16 "slices"
17 "sort"
18 "strings"
19 )
20
21 // MergeLocalsState encapsulates information about which AUTO
22 // (stack-allocated) variables within a function can be safely
23 // merged/overlapped, e.g. share a stack slot with some other auto).
24 // An instance of MergeLocalsState is produced by MergeLocals() below
25 // and then consumed in ssagen.AllocFrame. The map 'partition'
26 // contains entries of the form <N,SL> where N is an *ir.Name and SL
27 // is a slice holding the indices (within 'vars') of other variables
28 // that share the same slot, specifically the slot of the first
29 // element in the partition, which we'll call the "leader". For
30 // example, if a function contains five variables where v1/v2/v3 are
31 // safe to overlap and v4/v5 are safe to overlap, the MergeLocalsState
32 // content might look like
33 //
34 // vars: [v1, v2, v3, v4, v5]
35 // partition: v1 -> [1, 0, 2], v2 -> [1, 0, 2], v3 -> [1, 0, 2]
36 // v4 -> [3, 4], v5 -> [3, 4]
37 //
38 // A nil MergeLocalsState indicates that no local variables meet the
39 // necessary criteria for overlap.
40 type MergeLocalsState struct {
41 // contains auto vars that participate in overlapping
42 vars []*ir.Name
43 // maps auto variable to overlap partition
44 partition map[*ir.Name][]int
45 }
46
47 // candRegion is a sub-range (start, end) corresponding to an interval
48 // [st,en] within the list of candidate variables.
49 type candRegion struct {
50 st, en int
51 }
52
53 // cstate holds state information we'll need during the analysis
54 // phase of stack slot merging but can be discarded when the analysis
55 // is done.
56 type cstate struct {
57 fn *ir.Func
58 f *ssa.Func
59 lv *Liveness
60 cands []*ir.Name
61 nameToSlot map[*ir.Name]int32
62 regions []candRegion
63 indirectUE map[ssa.ID][]*ir.Name
64 ivs []Intervals
65 hashDeselected map[*ir.Name]bool
66 trace int // debug trace level
67 }
68
69 // MergeLocals analyzes the specified ssa function f to determine which
70 // of its auto variables can safely share the same stack slot, returning
71 // a state object that describes how the overlap should be done.
72 func MergeLocals(fn *ir.Func, f *ssa.Func) *MergeLocalsState {
73
74 // Create a container object for useful state info and then
75 // call collectMergeCandidates to see if there are vars suitable
76 // for stack slot merging.
77 cs := &cstate{
78 fn: fn,
79 f: f,
80 trace: base.Debug.MergeLocalsTrace,
81 }
82 cs.collectMergeCandidates()
83 if len(cs.regions) == 0 {
84 return nil
85 }
86
87 // Kick off liveness analysis.
88 //
89 // If we have a local variable such as "r2" below that's written
90 // but then not read, something like:
91 //
92 // vardef r1
93 // r1.x = ...
94 // vardef r2
95 // r2.x = 0
96 // r2.y = ...
97 // <call foo>
98 // // no subsequent use of r2
99 // ... = r1.x
100 //
101 // then for the purpose of calculating stack maps at the call, we
102 // can ignore "r2" completely during liveness analysis for stack
103 // maps, however for stack slock merging we most definitely want
104 // to treat the writes as "uses".
105 cs.lv = newliveness(fn, f, cs.cands, cs.nameToSlot, 0)
106 cs.lv.conservativeWrites = true
107 cs.lv.prologue()
108 cs.lv.solve()
109
110 // Compute intervals for each candidate based on the liveness and
111 // on block effects.
112 cs.computeIntervals()
113
114 // Perform merging within each region of the candidates list.
115 rv := cs.performMerging()
116 if err := rv.check(); err != nil {
117 base.FatalfAt(fn.Pos(), "invalid mergelocals state: %v", err)
118 }
119 return rv
120 }
121
122 // Subsumed returns whether variable n is subsumed, e.g. appears
123 // in an overlap position but is not the leader in that partition.
124 func (mls *MergeLocalsState) Subsumed(n *ir.Name) bool {
125 if sl, ok := mls.partition[n]; ok && mls.vars[sl[0]] != n {
126 return true
127 }
128 return false
129 }
130
131 // IsLeader returns whether a variable n is the leader (first element)
132 // in a sharing partition.
133 func (mls *MergeLocalsState) IsLeader(n *ir.Name) bool {
134 if sl, ok := mls.partition[n]; ok && mls.vars[sl[0]] == n {
135 return true
136 }
137 return false
138 }
139
140 // Leader returns the leader variable for subsumed var n.
141 func (mls *MergeLocalsState) Leader(n *ir.Name) *ir.Name {
142 if sl, ok := mls.partition[n]; ok {
143 if mls.vars[sl[0]] == n {
144 panic("variable is not subsumed")
145 }
146 return mls.vars[sl[0]]
147 }
148 panic("not a merge candidate")
149 }
150
151 // Followers writes a list of the followers for leader n into the slice tmp.
152 func (mls *MergeLocalsState) Followers(n *ir.Name, tmp []*ir.Name) []*ir.Name {
153 tmp = tmp[:0]
154 sl, ok := mls.partition[n]
155 if !ok {
156 panic("no entry for leader")
157 }
158 if mls.vars[sl[0]] != n {
159 panic("followers invoked on subsumed var")
160 }
161 for _, k := range sl[1:] {
162 tmp = append(tmp, mls.vars[k])
163 }
164 slices.SortStableFunc(tmp, func(a, b *ir.Name) int {
165 return strings.Compare(a.Sym().Name, b.Sym().Name)
166 })
167 return tmp
168 }
169
170 // EstSavings returns the estimated reduction in stack size (number of bytes) for
171 // the given merge locals state via a pair of ints, the first for non-pointer types and the second for pointer types.
172 func (mls *MergeLocalsState) EstSavings() (int, int) {
173 totnp := 0
174 totp := 0
175 for n := range mls.partition {
176 if mls.Subsumed(n) {
177 sz := int(n.Type().Size())
178 if n.Type().HasPointers() {
179 totp += sz
180 } else {
181 totnp += sz
182 }
183 }
184 }
185 return totnp, totp
186 }
187
188 // check tests for various inconsistencies and problems in mls,
189 // returning an error if any problems are found.
190 func (mls *MergeLocalsState) check() error {
191 if mls == nil {
192 return nil
193 }
194 used := make(map[int]bool)
195 seenv := make(map[*ir.Name]int)
196 for ii, v := range mls.vars {
197 if prev, ok := seenv[v]; ok {
198 return fmt.Errorf("duplicate var %q in vslots: %d and %d\n",
199 v.Sym().Name, ii, prev)
200 }
201 seenv[v] = ii
202 }
203 for k, sl := range mls.partition {
204 // length of slice value needs to be more than 1
205 if len(sl) < 2 {
206 return fmt.Errorf("k=%q v=%+v slice len %d invalid",
207 k.Sym().Name, sl, len(sl))
208 }
209 // values in the slice need to be var indices
210 for i, v := range sl {
211 if v < 0 || v > len(mls.vars)-1 {
212 return fmt.Errorf("k=%q v=+%v slpos %d vslot %d out of range of m.v", k.Sym().Name, sl, i, v)
213 }
214 }
215 }
216 for k, sl := range mls.partition {
217 foundk := false
218 for i, v := range sl {
219 vv := mls.vars[v]
220 if i == 0 {
221 if !mls.IsLeader(vv) {
222 return fmt.Errorf("k=%s v=+%v slpos 0 vslot %d IsLeader(%q) is false should be true", k.Sym().Name, sl, v, vv.Sym().Name)
223 }
224 } else {
225 if !mls.Subsumed(vv) {
226 return fmt.Errorf("k=%s v=+%v slpos %d vslot %d Subsumed(%q) is false should be true", k.Sym().Name, sl, i, v, vv.Sym().Name)
227 }
228 if mls.Leader(vv) != mls.vars[sl[0]] {
229 return fmt.Errorf("k=%s v=+%v slpos %d vslot %d Leader(%q) got %v want %v", k.Sym().Name, sl, i, v, vv.Sym().Name, mls.Leader(vv), mls.vars[sl[0]])
230 }
231 }
232 if vv == k {
233 foundk = true
234 if used[v] {
235 return fmt.Errorf("k=%s v=+%v val slice used violation at slpos %d vslot %d", k.Sym().Name, sl, i, v)
236 }
237 used[v] = true
238 }
239 }
240 if !foundk {
241 return fmt.Errorf("k=%s v=+%v slice value missing k", k.Sym().Name, sl)
242 }
243 vl := mls.vars[sl[0]]
244 for _, v := range sl[1:] {
245 vv := mls.vars[v]
246 if vv.Type().Size() > vl.Type().Size() {
247 return fmt.Errorf("k=%s v=+%v follower %s size %d larger than leader %s size %d", k.Sym().Name, sl, vv.Sym().Name, vv.Type().Size(), vl.Sym().Name, vl.Type().Size())
248 }
249 if vv.Type().HasPointers() && !vl.Type().HasPointers() {
250 return fmt.Errorf("k=%s v=+%v follower %s hasptr=true but leader %s hasptr=false", k.Sym().Name, sl, vv.Sym().Name, vl.Sym().Name)
251 }
252 if vv.Type().Alignment() > vl.Type().Alignment() {
253 return fmt.Errorf("k=%s v=+%v follower %s align %d greater than leader %s align %d", k.Sym().Name, sl, vv.Sym().Name, vv.Type().Alignment(), vl.Sym().Name, vl.Type().Alignment())
254 }
255 }
256 }
257 for i := range used {
258 if !used[i] {
259 return fmt.Errorf("pos %d var %q unused", i, mls.vars[i])
260 }
261 }
262 return nil
263 }
264
265 func (mls *MergeLocalsState) String() string {
266 var leaders []*ir.Name
267 for n, sl := range mls.partition {
268 if n == mls.vars[sl[0]] {
269 leaders = append(leaders, n)
270 }
271 }
272 slices.SortFunc(leaders, func(a, b *ir.Name) int {
273 return strings.Compare(a.Sym().Name, b.Sym().Name)
274 })
275 var sb strings.Builder
276 for _, n := range leaders {
277 sb.WriteString(n.Sym().Name + ":")
278 sl := mls.partition[n]
279 for _, k := range sl[1:] {
280 n := mls.vars[k]
281 sb.WriteString(" " + n.Sym().Name)
282 }
283 sb.WriteString("\n")
284 }
285 return sb.String()
286 }
287
288 // collectMergeCandidates visits all of the AUTO vars declared in
289 // function fn and identifies a list of candidate variables for
290 // merging / overlapping. On return the "cands" field of cs will be
291 // filled in with our set of potentially overlappable candidate
292 // variables, the "regions" field will hold regions/sequence of
293 // compatible vars within the candidates list, "nameToSlot" field will
294 // be populated, and the "indirectUE" field will be filled in with
295 // information about indirect upwards-exposed uses in the func.
296 func (cs *cstate) collectMergeCandidates() {
297 var cands []*ir.Name
298
299 // Collect up the available set of appropriate AUTOs in the
300 // function as a first step, and bail if we have fewer than
301 // two candidates.
302 for _, n := range cs.fn.Dcl {
303 if !n.Used() {
304 continue
305 }
306 if !ssa.IsMergeCandidate(n) {
307 continue
308 }
309 cands = append(cands, n)
310 }
311 if len(cands) < 2 {
312 return
313 }
314
315 // Sort by pointerness, size, and then name.
316 sort.SliceStable(cands, func(i, j int) bool {
317 return nameLess(cands[i], cands[j])
318 })
319
320 if cs.trace > 1 {
321 fmt.Fprintf(os.Stderr, "=-= raw cand list for func %v:\n", cs.fn)
322 for i := range cands {
323 dumpCand(cands[i], i)
324 }
325 }
326
327 // Now generate an initial pruned candidate list and regions list.
328 // This may be empty if we don't have enough compatible candidates.
329 initial, _ := cs.genRegions(cands)
330 if len(initial) < 2 {
331 return
332 }
333
334 // Set up for hash bisection if enabled.
335 cs.setupHashBisection(initial)
336
337 // Create and populate an indirect use table that we'll use
338 // during interval construction. As part of this process we may
339 // wind up tossing out additional candidates, so check to make
340 // sure we still have something to work with.
341 cs.cands, cs.regions = cs.populateIndirectUseTable(initial)
342 if len(cs.cands) < 2 {
343 return
344 }
345
346 // At this point we have a final pruned set of candidates and a
347 // corresponding set of regions for the candidates. Build a
348 // name-to-slot map for the candidates.
349 cs.nameToSlot = make(map[*ir.Name]int32)
350 for i, n := range cs.cands {
351 cs.nameToSlot[n] = int32(i)
352 }
353
354 if cs.trace > 1 {
355 fmt.Fprintf(os.Stderr, "=-= pruned candidate list for fn %v:\n", cs.fn)
356 for i := range cs.cands {
357 dumpCand(cs.cands[i], i)
358 }
359 }
360 }
361
362 // genRegions generates a set of regions within cands corresponding
363 // to potentially overlappable/mergeable variables.
364 func (cs *cstate) genRegions(cands []*ir.Name) ([]*ir.Name, []candRegion) {
365 var pruned []*ir.Name
366 var regions []candRegion
367 st := 0
368 for {
369 en := nextRegion(cands, st)
370 if en == -1 {
371 break
372 }
373 if st == en {
374 // region has just one element, we can skip it
375 st++
376 continue
377 }
378 pst := len(pruned)
379 pen := pst + (en - st)
380 if cs.trace > 1 {
381 fmt.Fprintf(os.Stderr, "=-= addregion st=%d en=%d: add part %d -> %d\n", st, en, pst, pen)
382 }
383
384 // non-empty region, add to pruned
385 pruned = append(pruned, cands[st:en+1]...)
386 regions = append(regions, candRegion{st: pst, en: pen})
387 st = en + 1
388 }
389 if len(pruned) < 2 {
390 return nil, nil
391 }
392 return pruned, regions
393 }
394
395 func (cs *cstate) dumpFunc() {
396 fmt.Fprintf(os.Stderr, "=-= mergelocalsdumpfunc %v:\n", cs.fn)
397 ii := 0
398 for k, b := range cs.f.Blocks {
399 fmt.Fprintf(os.Stderr, "b%d:\n", k)
400 for _, v := range b.Values {
401 pos := base.Ctxt.PosTable.Pos(v.Pos)
402 fmt.Fprintf(os.Stderr, "=-= %d L%d|C%d %s\n", ii, pos.RelLine(), pos.RelCol(), v.LongString())
403 ii++
404 }
405 }
406 }
407
408 func (cs *cstate) dumpFuncIfSelected() {
409 if base.Debug.MergeLocalsDumpFunc == "" {
410 return
411 }
412 if !strings.HasSuffix(fmt.Sprintf("%v", cs.fn),
413 base.Debug.MergeLocalsDumpFunc) {
414 return
415 }
416 cs.dumpFunc()
417 }
418
419 // setupHashBisection checks to see if any of the candidate
420 // variables have been de-selected by our hash debug. Here
421 // we also implement the -d=mergelocalshtrace flag, which turns
422 // on debug tracing only if we have at least two candidates
423 // selected by the hash debug for this function.
424 func (cs *cstate) setupHashBisection(cands []*ir.Name) {
425 if base.Debug.MergeLocalsHash == "" {
426 return
427 }
428 deselected := make(map[*ir.Name]bool)
429 selCount := 0
430 for _, cand := range cands {
431 if !base.MergeLocalsHash.MatchPosWithInfo(cand.Pos(), "mergelocals", nil) {
432 deselected[cand] = true
433 } else {
434 deselected[cand] = false
435 selCount++
436 }
437 }
438 if selCount < len(cands) {
439 cs.hashDeselected = deselected
440 }
441 if base.Debug.MergeLocalsHTrace != 0 && selCount >= 2 {
442 cs.trace = base.Debug.MergeLocalsHTrace
443 }
444 }
445
446 // populateIndirectUseTable creates and populates the "indirectUE" table
447 // within cs by doing some additional analysis of how the vars in
448 // cands are accessed in the function.
449 //
450 // It is possible to have situations where a given ir.Name is
451 // non-address-taken at the source level, but whose address is
452 // materialized in order to accommodate the needs of
453 // architecture-dependent operations or one sort or another (examples
454 // include things like LoweredZero/DuffZero, etc). The issue here is
455 // that the SymAddr op will show up as touching a variable of
456 // interest, but the subsequent memory op will not. This is generally
457 // not an issue for computing whether something is live across a call,
458 // but it is problematic for collecting the more fine-grained live
459 // interval info that drives stack slot merging.
460 //
461 // To handle this problem, make a forward pass over each basic block
462 // looking for instructions of the form vK := SymAddr(N) where N is a
463 // raw candidate. Create an entry in a map at that point from vK to
464 // its use count. Continue the walk, looking for uses of vK: when we
465 // see one, record it in a side table as an upwards exposed use of N.
466 // Each time we see a use, decrement the use count in the map, and if
467 // we hit zero, remove the map entry. If we hit the end of the basic
468 // block and we still have map entries, then evict the name in
469 // question from the candidate set.
470 func (cs *cstate) populateIndirectUseTable(cands []*ir.Name) ([]*ir.Name, []candRegion) {
471
472 // main indirect UE table, this is what we're producing in this func
473 indirectUE := make(map[ssa.ID][]*ir.Name)
474
475 // this map holds the current set of candidates; the set may
476 // shrink if we have to evict any candidates.
477 rawcands := make(map[*ir.Name]struct{})
478
479 // maps ssa value V to the ir.Name it is taking the addr of,
480 // plus a count of the uses we've seen of V during a block walk.
481 pendingUses := make(map[ssa.ID]nameCount)
482
483 // A temporary indirect UE tab just for the current block
484 // being processed; used to help with evictions.
485 blockIndirectUE := make(map[ssa.ID][]*ir.Name)
486
487 // temporary map used to record evictions in a given block.
488 evicted := make(map[*ir.Name]bool)
489 for _, n := range cands {
490 rawcands[n] = struct{}{}
491 }
492 for k := 0; k < len(cs.f.Blocks); k++ {
493 clear(pendingUses)
494 clear(blockIndirectUE)
495 b := cs.f.Blocks[k]
496 for _, v := range b.Values {
497 if n, e := affectedVar(v); n != nil {
498 if _, ok := rawcands[n]; ok {
499 if e&ssa.SymAddr != 0 && v.Uses != 0 {
500 // we're taking the address of candidate var n
501 if _, ok := pendingUses[v.ID]; ok {
502 // should never happen
503 base.FatalfAt(v.Pos, "internal error: apparent multiple defs for SSA value %d", v.ID)
504 }
505 // Stash an entry in pendingUses recording
506 // that we took the address of "n" via this
507 // val.
508 pendingUses[v.ID] = nameCount{n: n, count: v.Uses}
509 if cs.trace > 2 {
510 fmt.Fprintf(os.Stderr, "=-= SymAddr(%s) on %s\n",
511 n.Sym().Name, v.LongString())
512 }
513 }
514 }
515 }
516 for idx, arg := range v.Args {
517 if nc, ok := pendingUses[arg.ID]; ok {
518 if !v.AddrSinkArg(idx) {
519 // If this op may propagate its input address
520 // to somewhere else, we must track where that
521 // somewhere else might be. See issue 80127.
522 if v.Type.IsMemory() {
523 // Might be stored to memory. Give up.
524 continue
525 }
526 // Some sort of address arithmetic.
527 if _, ok := pendingUses[v.ID]; ok {
528 // v has used multiple addresses, which is something
529 // we can't keep track of. Give up.
530 continue
531 }
532 // Treat this op as producing the address of the same variable
533 // that its argument was the address of.
534 pendingUses[v.ID] = nameCount{n: nc.n, count: v.Uses}
535 }
536 // We found a use of some value that took the
537 // address of nc.n. Record this inst as a
538 // potential indirect use.
539 if cs.trace > 2 {
540 fmt.Fprintf(os.Stderr, "=-= add indirectUE(%s) count=%d on %s\n", nc.n.Sym().Name, nc.count, v.LongString())
541 }
542 blockIndirectUE[v.ID] = append(blockIndirectUE[v.ID], nc.n)
543 nc.count--
544 if nc.count == 0 {
545 // That was the last use of the value. Clean
546 // up the entry in pendingUses.
547 if cs.trace > 2 {
548 fmt.Fprintf(os.Stderr, "=-= last use of v%d\n",
549 arg.ID)
550 }
551 delete(pendingUses, arg.ID)
552 } else {
553 // Not the last use; record the decremented
554 // use count and move on.
555 pendingUses[arg.ID] = nc
556 }
557 }
558 }
559 }
560
561 // We've reached the end of this basic block: if we have any
562 // leftover entries in pendingUses, then evict the
563 // corresponding names from the candidate set. The idea here
564 // is that if we materialized the address of some local and
565 // that value is flowing out of the block off somewhere else,
566 // we're going to treat that local as truly address-taken and
567 // not have it be a merge candidate.
568 clear(evicted)
569 if len(pendingUses) != 0 {
570 for id, nc := range pendingUses {
571 if cs.trace > 2 {
572 fmt.Fprintf(os.Stderr, "=-= evicting %q due to pendingUse %d count %d\n", nc.n.Sym().Name, id, nc.count)
573 }
574 delete(rawcands, nc.n)
575 evicted[nc.n] = true
576 }
577 }
578 // Copy entries from blockIndirectUE into final indirectUE. Skip
579 // anything that we evicted in the loop above.
580 for id, sl := range blockIndirectUE {
581 for _, n := range sl {
582 if evicted[n] {
583 continue
584 }
585 indirectUE[id] = append(indirectUE[id], n)
586 if cs.trace > 2 {
587 fmt.Fprintf(os.Stderr, "=-= add final indUE v%d name %s\n", id, n.Sym().Name)
588 }
589 }
590 }
591 }
592 if len(rawcands) < 2 {
593 return nil, nil
594 }
595 cs.indirectUE = indirectUE
596 if cs.trace > 2 {
597 fmt.Fprintf(os.Stderr, "=-= iuetab:\n")
598 ids := make([]ssa.ID, 0, len(indirectUE))
599 for k := range indirectUE {
600 ids = append(ids, k)
601 }
602 slices.Sort(ids)
603 for _, id := range ids {
604 fmt.Fprintf(os.Stderr, " v%d:", id)
605 for _, n := range indirectUE[id] {
606 fmt.Fprintf(os.Stderr, " %s", n.Sym().Name)
607 }
608 fmt.Fprintf(os.Stderr, "\n")
609 }
610 }
611
612 pruned := cands[:0]
613 for k := range rawcands {
614 pruned = append(pruned, k)
615 }
616 sort.Slice(pruned, func(i, j int) bool {
617 return nameLess(pruned[i], pruned[j])
618 })
619 var regions []candRegion
620 pruned, regions = cs.genRegions(pruned)
621 if len(pruned) < 2 {
622 return nil, nil
623 }
624 return pruned, regions
625 }
626
627 type nameCount struct {
628 n *ir.Name
629 count int32
630 }
631
632 // nameLess compares ci with cj to see if ci should be less than cj in
633 // a relative ordering of candidate variables. This is used to sort
634 // vars by pointerness (variables with pointers first), then in order
635 // of decreasing alignment, then by decreasing size. We are assuming a
636 // merging algorithm that merges later entries in the list into
637 // earlier entries. An example ordered candidate list produced by
638 // nameLess:
639 //
640 // idx name type align size
641 // 0: abc [10]*int 8 80
642 // 1: xyz [9]*int 8 72
643 // 2: qrs [2]*int 8 16
644 // 3: tuv [9]int 8 72
645 // 4: wxy [9]int32 4 36
646 // 5: jkl [8]int32 4 32
647 func nameLess(ci, cj *ir.Name) bool {
648 if ci.Type().HasPointers() != cj.Type().HasPointers() {
649 return ci.Type().HasPointers()
650 }
651 if ci.Type().Alignment() != cj.Type().Alignment() {
652 return cj.Type().Alignment() < ci.Type().Alignment()
653 }
654 if ci.Type().Size() != cj.Type().Size() {
655 return cj.Type().Size() < ci.Type().Size()
656 }
657 if ci.Sym().Name != cj.Sym().Name {
658 return ci.Sym().Name < cj.Sym().Name
659 }
660 return fmt.Sprintf("%v", ci.Pos()) < fmt.Sprintf("%v", cj.Pos())
661 }
662
663 // nextRegion starts at location idx and walks forward in the cands
664 // slice looking for variables that are "compatible" (potentially
665 // overlappable, in the sense that they could potentially share the
666 // stack slot of cands[idx]); it returns the end of the new region
667 // (range of compatible variables starting at idx).
668 func nextRegion(cands []*ir.Name, idx int) int {
669 n := len(cands)
670 if idx >= n {
671 return -1
672 }
673 c0 := cands[idx]
674 szprev := c0.Type().Size()
675 alnprev := c0.Type().Alignment()
676 for j := idx + 1; j < n; j++ {
677 cj := cands[j]
678 szj := cj.Type().Size()
679 if szj > szprev {
680 return j - 1
681 }
682 alnj := cj.Type().Alignment()
683 if alnj > alnprev {
684 return j - 1
685 }
686 szprev = szj
687 alnprev = alnj
688 }
689 return n - 1
690 }
691
692 // mergeVisitRegion tries to perform overlapping of variables with a
693 // given subrange of cands described by st and en (indices into our
694 // candidate var list), where the variables within this range have
695 // already been determined to be compatible with respect to type,
696 // size, etc. Overlapping is done in a greedy fashion: we select the
697 // first element in the st->en range, then walk the rest of the
698 // elements adding in vars whose lifetimes don't overlap with the
699 // first element, then repeat the process until we run out of work.
700 // Ordering of the candidates within the region [st,en] is important;
701 // within the list the assumption is that if we overlap two variables
702 // X and Y where X precedes Y in the list, we need to make X the
703 // "leader" (keep X's slot and set Y's frame offset to X's) as opposed
704 // to the other way around, since it's possible that Y is smaller in
705 // size than X.
706 func (cs *cstate) mergeVisitRegion(mls *MergeLocalsState, st, en int) {
707 if cs.trace > 1 {
708 fmt.Fprintf(os.Stderr, "=-= mergeVisitRegion(st=%d, en=%d)\n", st, en)
709 }
710 n := en - st + 1
711 used := bitvec.New(int32(n))
712
713 nxt := func(slot int) int {
714 for c := slot - st; c < n; c++ {
715 if used.Get(int32(c)) {
716 continue
717 }
718 return c + st
719 }
720 return -1
721 }
722
723 navail := n
724 cands := cs.cands
725 ivs := cs.ivs
726 if cs.trace > 1 {
727 fmt.Fprintf(os.Stderr, " =-= navail = %d\n", navail)
728 }
729 for navail >= 2 {
730 leader := nxt(st)
731 used.Set(int32(leader - st))
732 navail--
733
734 if cs.trace > 1 {
735 fmt.Fprintf(os.Stderr, " =-= begin leader %d used=%s\n", leader,
736 used.String())
737 }
738 elems := []int{leader}
739 lints := ivs[leader]
740
741 for succ := nxt(leader + 1); succ != -1; succ = nxt(succ + 1) {
742
743 // Skip if de-selected by merge locals hash.
744 if cs.hashDeselected != nil && cs.hashDeselected[cands[succ]] {
745 continue
746 }
747 // Skip if already used.
748 if used.Get(int32(succ - st)) {
749 continue
750 }
751 if cs.trace > 1 {
752 fmt.Fprintf(os.Stderr, " =-= overlap of %d[%v] {%s} with %d[%v] {%s} is: %v\n", leader, cands[leader], lints.String(), succ, cands[succ], ivs[succ].String(), lints.Overlaps(ivs[succ]))
753 }
754
755 // Can we overlap leader with this var?
756 if lints.Overlaps(ivs[succ]) {
757 continue
758 } else {
759 // Add to overlap set.
760 elems = append(elems, succ)
761 lints = lints.Merge(ivs[succ])
762 }
763 }
764 if len(elems) > 1 {
765 // We found some things to overlap with leader. Add the
766 // candidate elements to "vars" and update "partition".
767 off := len(mls.vars)
768 sl := make([]int, len(elems))
769 for i, candslot := range elems {
770 sl[i] = off + i
771 mls.vars = append(mls.vars, cands[candslot])
772 mls.partition[cands[candslot]] = sl
773 }
774 navail -= (len(elems) - 1)
775 for i := range elems {
776 used.Set(int32(elems[i] - st))
777 }
778 if cs.trace > 1 {
779 fmt.Fprintf(os.Stderr, "=-= overlapping %+v:\n", sl)
780 for i := range sl {
781 dumpCand(mls.vars[sl[i]], sl[i])
782 }
783 for i, v := range elems {
784 fmt.Fprintf(os.Stderr, "=-= %d: sl=%d %s\n", i, v, ivs[v])
785 }
786 }
787 }
788 }
789 }
790
791 // performMerging carries out variable merging within each of the
792 // candidate ranges in regions, returning a state object
793 // that describes the variable overlaps.
794 func (cs *cstate) performMerging() *MergeLocalsState {
795 cands := cs.cands
796
797 mls := &MergeLocalsState{
798 partition: make(map[*ir.Name][]int),
799 }
800
801 // Dump state before attempting overlap.
802 if cs.trace > 1 {
803 fmt.Fprintf(os.Stderr, "=-= cands live before overlap:\n")
804 for i := range cands {
805 c := cands[i]
806 fmt.Fprintf(os.Stderr, "%d: %v sz=%d ivs=%s\n",
807 i, c.Sym().Name, c.Type().Size(), cs.ivs[i].String())
808 }
809 fmt.Fprintf(os.Stderr, "=-= regions (%d): ", len(cs.regions))
810 for _, cr := range cs.regions {
811 fmt.Fprintf(os.Stderr, " [%d,%d]", cr.st, cr.en)
812 }
813 fmt.Fprintf(os.Stderr, "\n")
814 }
815
816 // Apply a greedy merge/overlap strategy within each region
817 // of compatible variables.
818 for _, cr := range cs.regions {
819 cs.mergeVisitRegion(mls, cr.st, cr.en)
820 }
821 if len(mls.vars) == 0 {
822 return nil
823 }
824 return mls
825 }
826
827 // computeIntervals performs a backwards sweep over the instructions
828 // of the function we're compiling, building up an Intervals object
829 // for each candidate variable by looking for upwards exposed uses
830 // and kills.
831 func (cs *cstate) computeIntervals() {
832 lv := cs.lv
833 ibuilders := make([]IntervalsBuilder, len(cs.cands))
834 nvars := int32(len(lv.vars))
835 liveout := bitvec.New(nvars)
836
837 cs.dumpFuncIfSelected()
838
839 // Count instructions.
840 ninstr := 0
841 for _, b := range lv.f.Blocks {
842 ninstr += len(b.Values)
843 }
844 // current instruction index during backwards walk
845 iidx := ninstr - 1
846
847 // Make a backwards pass over all blocks
848 for k := len(lv.f.Blocks) - 1; k >= 0; k-- {
849 b := lv.f.Blocks[k]
850 be := lv.blockEffects(b)
851
852 if cs.trace > 2 {
853 fmt.Fprintf(os.Stderr, "=-= liveout from tail of b%d: ", k)
854 for j := range lv.vars {
855 if be.liveout.Get(int32(j)) {
856 fmt.Fprintf(os.Stderr, " %q", lv.vars[j].Sym().Name)
857 }
858 }
859 fmt.Fprintf(os.Stderr, "\n")
860 }
861
862 // Take into account effects taking place at end of this basic
863 // block by comparing our current live set with liveout for
864 // the block. If a given var was not live before and is now
865 // becoming live we need to mark this transition with a
866 // builder "Live" call; similarly if a var was live before and
867 // is now no longer live, we need a "Kill" call.
868 for j := range lv.vars {
869 isLive := liveout.Get(int32(j))
870 blockLiveOut := be.liveout.Get(int32(j))
871 if isLive {
872 if !blockLiveOut {
873 if cs.trace > 2 {
874 fmt.Fprintf(os.Stderr, "=+= at instr %d block boundary kill of %v\n", iidx, lv.vars[j])
875 }
876 ibuilders[j].Kill(iidx)
877 }
878 } else if blockLiveOut {
879 if cs.trace > 2 {
880 fmt.Fprintf(os.Stderr, "=+= at block-end instr %d %v becomes live\n",
881 iidx, lv.vars[j])
882 }
883 ibuilders[j].Live(iidx)
884 }
885 }
886
887 // Set our working "currently live" set to the previously
888 // computed live out set for the block.
889 liveout.Copy(be.liveout)
890
891 // Now walk backwards through this block.
892 for i := len(b.Values) - 1; i >= 0; i-- {
893 v := b.Values[i]
894
895 if cs.trace > 2 {
896 fmt.Fprintf(os.Stderr, "=-= b%d instr %d: %s\n", k, iidx, v.LongString())
897 }
898
899 // Update liveness based on what we see happening in this
900 // instruction.
901 pos, e := lv.valueEffects(v)
902 becomeslive := e&uevar != 0
903 iskilled := e&varkill != 0
904 if becomeslive && iskilled {
905 // we do not ever expect to see both a kill and an
906 // upwards exposed use given our size constraints.
907 panic("should never happen")
908 }
909 if iskilled && liveout.Get(pos) {
910 ibuilders[pos].Kill(iidx)
911 liveout.Unset(pos)
912 if cs.trace > 2 {
913 fmt.Fprintf(os.Stderr, "=+= at instr %d kill of %v\n",
914 iidx, lv.vars[pos])
915 }
916 } else if becomeslive && !liveout.Get(pos) {
917 ibuilders[pos].Live(iidx)
918 liveout.Set(pos)
919 if cs.trace > 2 {
920 fmt.Fprintf(os.Stderr, "=+= at instr %d upwards-exposed use of %v\n",
921 iidx, lv.vars[pos])
922 }
923 }
924
925 if cs.indirectUE != nil {
926 // Now handle "indirect" upwards-exposed uses.
927 ues := cs.indirectUE[v.ID]
928 for _, n := range ues {
929 if pos, ok := lv.idx[n]; ok {
930 if !liveout.Get(pos) {
931 ibuilders[pos].Live(iidx)
932 liveout.Set(pos)
933 if cs.trace > 2 {
934 fmt.Fprintf(os.Stderr, "=+= at instr %d v%d indirect upwards-exposed use of %v\n", iidx, v.ID, lv.vars[pos])
935 }
936 }
937 }
938 }
939 }
940 iidx--
941 }
942
943 // This check disabled for now due to the way scheduling works
944 // for ops that materialize values of local variables. For
945 // many architecture we have rewrite rules of this form:
946 //
947 // (LocalAddr <t> {sym} base mem) && t.Elem().HasPointers() => (MOVDaddr {sym} (SPanchored base mem))
948 // (LocalAddr <t> {sym} base _) && !t.Elem().HasPointers() => (MOVDaddr {sym} base)
949 //
950 // which are designed to ensure that if you have a pointerful
951 // variable "abc" sequence
952 //
953 // v30 = VarDef <mem> {abc} v21
954 // v31 = LocalAddr <*SB> {abc} v2 v30
955 // v32 = Zero <mem> {SB} [2056] v31 v30
956 //
957 // this will be lowered into
958 //
959 // v30 = VarDef <mem> {sb} v21
960 // v106 = SPanchored <uintptr> v2 v30
961 // v31 = MOVDaddr <*SB> {sb} v106
962 // v3 = DUFFZERO <mem> [2056] v31 v30
963 //
964 // Note the SPanchored: this ensures that the scheduler won't
965 // move the MOVDaddr earlier than the vardef. With a variable
966 // "xyz" that has no pointers, however, if we start with
967 //
968 // v66 = VarDef <mem> {t2} v65
969 // v67 = LocalAddr <*T> {t2} v2 v66
970 // v68 = Zero <mem> {T} [2056] v67 v66
971 //
972 // we might lower to
973 //
974 // v66 = VarDef <mem> {t2} v65
975 // v29 = MOVDaddr <*T> {t2} [2032] v2
976 // v43 = LoweredZero <mem> v67 v29 v66
977 // v68 = Zero [2056] v2 v43
978 //
979 // where that MOVDaddr can float around arbitrarily, meaning
980 // that we may see an upwards-exposed use to it before the
981 // VarDef.
982 //
983 // One avenue to restoring the check below would be to change
984 // the rewrite rules to something like
985 //
986 // (LocalAddr <t> {sym} base mem) && (t.Elem().HasPointers() || isMergeCandidate(t) => (MOVDaddr {sym} (SPanchored base mem))
987 //
988 // however that change will have to be carefully evaluated,
989 // since it would constrain the scheduler for _all_ LocalAddr
990 // ops for potential merge candidates, even if we don't
991 // actually succeed in any overlaps. This will be revisitged in
992 // a later CL if possible.
993 //
994 const checkLiveOnEntry = false
995 if checkLiveOnEntry && b == lv.f.Entry {
996 for j, v := range lv.vars {
997 if liveout.Get(int32(j)) {
998 lv.f.Fatalf("%v %L recorded as live on entry",
999 lv.fn.Nname, v)
1000 }
1001 }
1002 }
1003 }
1004 if iidx != -1 {
1005 panic("iidx underflow")
1006 }
1007
1008 // Finish intervals construction.
1009 ivs := make([]Intervals, len(cs.cands))
1010 for i := range cs.cands {
1011 var err error
1012 ivs[i], err = ibuilders[i].Finish()
1013 if err != nil {
1014 cs.dumpFunc()
1015 base.FatalfAt(cs.cands[i].Pos(), "interval construct error for var %q in func %q (%d instrs): %v", cs.cands[i].Sym().Name, ir.FuncName(cs.fn), ninstr, err)
1016 }
1017 }
1018 cs.ivs = ivs
1019 }
1020
1021 func fmtFullPos(p src.XPos) string {
1022 var sb strings.Builder
1023 sep := ""
1024 base.Ctxt.AllPos(p, func(pos src.Pos) {
1025 sb.WriteString(sep)
1026 sep = "|"
1027 file := filepath.Base(pos.Filename())
1028 fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
1029 })
1030 return sb.String()
1031 }
1032
1033 func dumpCand(c *ir.Name, i int) {
1034 fmt.Fprintf(os.Stderr, " %d: %s %q sz=%d hp=%v align=%d t=%v\n",
1035 i, fmtFullPos(c.Pos()), c.Sym().Name, c.Type().Size(),
1036 c.Type().HasPointers(), c.Type().Alignment(), c.Type())
1037 }
1038
1039 // for unit testing only.
1040 func MakeMergeLocalsState(partition map[*ir.Name][]int, vars []*ir.Name) (*MergeLocalsState, error) {
1041 mls := &MergeLocalsState{partition: partition, vars: vars}
1042 if err := mls.check(); err != nil {
1043 return nil, err
1044 }
1045 return mls, nil
1046 }