2015-03-23 17:02:11 -07:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
package ssa
|
|
|
|
|
|
2016-03-04 14:19:49 -05:00
|
|
|
import "container/heap"
|
|
|
|
|
|
2015-09-11 16:40:05 -04:00
|
|
|
const (
|
|
|
|
|
ScorePhi = iota // towards top of block
|
|
|
|
|
ScoreVarDef
|
|
|
|
|
ScoreMemory
|
|
|
|
|
ScoreDefault
|
|
|
|
|
ScoreFlags
|
|
|
|
|
ScoreControl // towards bottom of block
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-04 14:19:49 -05:00
|
|
|
type ValHeap struct {
|
|
|
|
|
a []*Value
|
|
|
|
|
less func(a, b *Value) bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h ValHeap) Len() int { return len(h.a) }
|
|
|
|
|
func (h ValHeap) Swap(i, j int) { a := h.a; a[i], a[j] = a[j], a[i] }
|
|
|
|
|
|
|
|
|
|
func (h *ValHeap) Push(x interface{}) {
|
|
|
|
|
// Push and Pop use pointer receivers because they modify the slice's length,
|
|
|
|
|
// not just its contents.
|
|
|
|
|
v := x.(*Value)
|
|
|
|
|
h.a = append(h.a, v)
|
|
|
|
|
}
|
|
|
|
|
func (h *ValHeap) Pop() interface{} {
|
|
|
|
|
old := h.a
|
|
|
|
|
n := len(old)
|
|
|
|
|
x := old[n-1]
|
|
|
|
|
h.a = old[0 : n-1]
|
|
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
func (h ValHeap) Less(i, j int) bool { return h.less(h.a[i], h.a[j]) }
|
|
|
|
|
|
2016-03-01 23:21:55 +00:00
|
|
|
// Schedule the Values in each Block. After this phase returns, the
|
2015-03-23 17:02:11 -07:00
|
|
|
// order of b.Values matters and is the order in which those values
|
2016-03-01 23:21:55 +00:00
|
|
|
// will appear in the assembly output. For now it generates a
|
|
|
|
|
// reasonable valid schedule using a priority queue. TODO(khr):
|
2015-03-23 17:02:11 -07:00
|
|
|
// schedule smarter.
|
|
|
|
|
func schedule(f *Func) {
|
2015-08-03 12:33:03 -07:00
|
|
|
// For each value, the number of times it is used in the block
|
|
|
|
|
// by values that have not been scheduled yet.
|
2016-03-04 14:19:49 -05:00
|
|
|
uses := make([]int32, f.NumValues())
|
2015-08-03 12:33:03 -07:00
|
|
|
|
|
|
|
|
// "priority" for a value
|
2016-03-04 14:19:49 -05:00
|
|
|
score := make([]int8, f.NumValues())
|
2015-08-03 12:33:03 -07:00
|
|
|
|
2016-03-01 23:21:55 +00:00
|
|
|
// scheduling order. We queue values in this list in reverse order.
|
2015-03-23 17:02:11 -07:00
|
|
|
var order []*Value
|
|
|
|
|
|
2015-08-10 11:10:53 -07:00
|
|
|
// maps mem values to the next live memory value
|
|
|
|
|
nextMem := make([]*Value, f.NumValues())
|
2016-03-01 23:21:55 +00:00
|
|
|
// additional pretend arguments for each Value. Used to enforce load/store ordering.
|
2015-08-10 11:10:53 -07:00
|
|
|
additionalArgs := make([][]*Value, f.NumValues())
|
2015-08-03 12:33:03 -07:00
|
|
|
|
2016-03-04 14:19:49 -05:00
|
|
|
for _, b := range f.Blocks {
|
|
|
|
|
// Compute score. Larger numbers are scheduled closer to the end of the block.
|
|
|
|
|
for _, v := range b.Values {
|
|
|
|
|
switch {
|
|
|
|
|
case v.Op == OpAMD64LoweredGetClosurePtr:
|
|
|
|
|
// We also score GetLoweredClosurePtr as early as possible to ensure that the
|
|
|
|
|
// context register is not stomped. GetLoweredClosurePtr should only appear
|
|
|
|
|
// in the entry block where there are no phi functions, so there is no
|
|
|
|
|
// conflict or ambiguity here.
|
|
|
|
|
if b != f.Entry {
|
|
|
|
|
f.Fatalf("LoweredGetClosurePtr appeared outside of entry block, b=%s", b.String())
|
|
|
|
|
}
|
|
|
|
|
score[v.ID] = ScorePhi
|
|
|
|
|
case v.Op == OpPhi:
|
|
|
|
|
// We want all the phis first.
|
|
|
|
|
score[v.ID] = ScorePhi
|
|
|
|
|
case v.Op == OpVarDef:
|
|
|
|
|
// We want all the vardefs next.
|
|
|
|
|
score[v.ID] = ScoreVarDef
|
|
|
|
|
case v.Type.IsMemory():
|
|
|
|
|
// Schedule stores as early as possible. This tends to
|
|
|
|
|
// reduce register pressure. It also helps make sure
|
|
|
|
|
// VARDEF ops are scheduled before the corresponding LEA.
|
|
|
|
|
score[v.ID] = ScoreMemory
|
|
|
|
|
case v.Type.IsFlags():
|
|
|
|
|
// Schedule flag register generation as late as possible.
|
|
|
|
|
// This makes sure that we only have one live flags
|
|
|
|
|
// value at a time.
|
|
|
|
|
score[v.ID] = ScoreFlags
|
|
|
|
|
default:
|
|
|
|
|
score[v.ID] = ScoreDefault
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 17:02:11 -07:00
|
|
|
for _, b := range f.Blocks {
|
2015-08-10 11:10:53 -07:00
|
|
|
// Find store chain for block.
|
2015-08-10 13:40:28 -07:00
|
|
|
// Store chains for different blocks overwrite each other, so
|
|
|
|
|
// the calculated store chain is good only for this block.
|
2015-08-10 11:10:53 -07:00
|
|
|
for _, v := range b.Values {
|
|
|
|
|
if v.Op != OpPhi && v.Type.IsMemory() {
|
|
|
|
|
for _, w := range v.Args {
|
|
|
|
|
if w.Type.IsMemory() {
|
|
|
|
|
nextMem[w.ID] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
// Compute uses.
|
2015-05-30 01:03:40 -04:00
|
|
|
for _, v := range b.Values {
|
2015-08-10 11:10:53 -07:00
|
|
|
if v.Op == OpPhi {
|
|
|
|
|
// If a value is used by a phi, it does not induce
|
2015-08-03 12:33:03 -07:00
|
|
|
// a scheduling edge because that use is from the
|
|
|
|
|
// previous iteration.
|
2015-08-10 11:10:53 -07:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for _, w := range v.Args {
|
|
|
|
|
if w.Block == b {
|
|
|
|
|
uses[w.ID]++
|
|
|
|
|
}
|
|
|
|
|
// Any load must come before the following store.
|
|
|
|
|
if v.Type.IsMemory() || !w.Type.IsMemory() {
|
|
|
|
|
continue // not a load
|
2015-05-30 01:03:40 -04:00
|
|
|
}
|
2015-08-10 11:10:53 -07:00
|
|
|
s := nextMem[w.ID]
|
|
|
|
|
if s == nil || s.Block != b {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
additionalArgs[s.ID] = append(additionalArgs[s.ID], v)
|
|
|
|
|
uses[v.ID]++
|
2015-05-30 01:03:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-04 14:19:49 -05:00
|
|
|
|
2015-08-05 16:07:13 -07:00
|
|
|
if b.Control != nil && b.Control.Op != OpPhi {
|
|
|
|
|
// Force the control value to be scheduled at the end,
|
|
|
|
|
// unless it is a phi value (which must be first).
|
2015-09-11 16:40:05 -04:00
|
|
|
score[b.Control.ID] = ScoreControl
|
2015-08-29 12:51:04 -05:00
|
|
|
|
|
|
|
|
// Schedule values dependent on the control value at the end.
|
|
|
|
|
// This reduces the number of register spills. We don't find
|
|
|
|
|
// all values that depend on the control, just values with a
|
2016-03-01 23:21:55 +00:00
|
|
|
// direct dependency. This is cheaper and in testing there
|
2015-08-29 12:51:04 -05:00
|
|
|
// was no difference in the number of spills.
|
|
|
|
|
for _, v := range b.Values {
|
|
|
|
|
if v.Op != OpPhi {
|
|
|
|
|
for _, a := range v.Args {
|
|
|
|
|
if a == b.Control {
|
2015-09-11 16:40:05 -04:00
|
|
|
score[v.ID] = ScoreControl
|
2015-08-29 12:51:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
}
|
2015-05-30 01:03:40 -04:00
|
|
|
|
2016-03-04 14:19:49 -05:00
|
|
|
// To put things into a priority queue
|
|
|
|
|
// The values that should come last are least.
|
|
|
|
|
priq := &ValHeap{
|
|
|
|
|
a: make([]*Value, 0, 8), // TODO allocate once and reuse.
|
|
|
|
|
less: func(x, y *Value) bool {
|
|
|
|
|
sx := score[x.ID]
|
|
|
|
|
sy := score[y.ID]
|
|
|
|
|
if c := sx - sy; c != 0 {
|
|
|
|
|
return c > 0 // higher score comes later.
|
|
|
|
|
}
|
|
|
|
|
if x.Line != y.Line { // Favor in-order line stepping
|
|
|
|
|
return x.Line > y.Line
|
|
|
|
|
}
|
|
|
|
|
if x.Op != OpPhi {
|
|
|
|
|
if c := len(x.Args) - len(y.Args); c != 0 {
|
|
|
|
|
return c < 0 // smaller args comes later
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return x.ID > y.ID
|
|
|
|
|
},
|
2015-08-03 12:33:03 -07:00
|
|
|
}
|
2016-03-04 14:19:49 -05:00
|
|
|
|
|
|
|
|
// Initialize priority queue with schedulable values.
|
2015-03-23 17:02:11 -07:00
|
|
|
for _, v := range b.Values {
|
2015-08-03 12:33:03 -07:00
|
|
|
if uses[v.ID] == 0 {
|
2016-03-04 14:19:49 -05:00
|
|
|
heap.Push(priq, v)
|
2015-07-13 23:52:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
// Schedule highest priority value, update use counts, repeat.
|
|
|
|
|
order = order[:0]
|
|
|
|
|
for {
|
|
|
|
|
// Find highest priority schedulable value.
|
2016-03-04 14:19:49 -05:00
|
|
|
// Note that schedule is assembled backwards.
|
|
|
|
|
|
|
|
|
|
if priq.Len() == 0 {
|
2015-08-03 12:33:03 -07:00
|
|
|
break
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
|
2016-03-04 14:19:49 -05:00
|
|
|
v := heap.Pop(priq).(*Value)
|
|
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
// Add it to the schedule.
|
|
|
|
|
order = append(order, v)
|
|
|
|
|
|
|
|
|
|
// Update use counts of arguments.
|
|
|
|
|
for _, w := range v.Args {
|
|
|
|
|
if w.Block != b {
|
|
|
|
|
continue
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
uses[w.ID]--
|
|
|
|
|
if uses[w.ID] == 0 {
|
|
|
|
|
// All uses scheduled, w is now schedulable.
|
2016-03-04 14:19:49 -05:00
|
|
|
heap.Push(priq, w)
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-10 11:10:53 -07:00
|
|
|
for _, w := range additionalArgs[v.ID] {
|
|
|
|
|
uses[w.ID]--
|
|
|
|
|
if uses[w.ID] == 0 {
|
|
|
|
|
// All uses scheduled, w is now schedulable.
|
2016-03-04 14:19:49 -05:00
|
|
|
heap.Push(priq, w)
|
2015-08-10 11:10:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
if len(order) != len(b.Values) {
|
|
|
|
|
f.Fatalf("schedule does not include all values")
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < len(b.Values); i++ {
|
|
|
|
|
b.Values[i] = order[len(b.Values)-1-i]
|
2015-06-13 19:27:26 +01:00
|
|
|
}
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
|
|
|
|
|
f.scheduled = true
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|