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
|
|
|
|
|
|
|
|
|
|
// Schedule the Values in each Block. After this phase returns, the
|
|
|
|
|
// order of b.Values matters and is the order in which those values
|
2015-08-03 12:33:03 -07: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.
|
|
|
|
|
uses := make([]int, f.NumValues())
|
|
|
|
|
|
|
|
|
|
// "priority" for a value
|
2015-08-05 16:07:13 -07:00
|
|
|
score := make([]uint8, f.NumValues())
|
2015-08-03 12:33:03 -07: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-03 12:33:03 -07:00
|
|
|
// priority queue of legally schedulable (0 unscheduled uses) values
|
2015-08-10 11:10:53 -07:00
|
|
|
var priq [5][]*Value
|
|
|
|
|
|
|
|
|
|
// maps mem values to the next live memory value
|
|
|
|
|
nextMem := make([]*Value, f.NumValues())
|
|
|
|
|
// additional pretend arguments for each Value. Used to enforce load/store ordering.
|
|
|
|
|
additionalArgs := make([][]*Value, f.NumValues())
|
2015-08-03 12:33:03 -07:00
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
// Compute score. Larger numbers are scheduled closer to the end of the block.
|
2015-05-30 01:03:40 -04:00
|
|
|
for _, v := range b.Values {
|
2015-08-03 12:33:03 -07:00
|
|
|
switch {
|
|
|
|
|
case v.Op == OpPhi:
|
|
|
|
|
// We want all the phis first.
|
|
|
|
|
score[v.ID] = 0
|
|
|
|
|
case v.Type.IsMemory():
|
2015-08-10 11:10:53 -07:00
|
|
|
// Schedule stores as early as possible. This tends to
|
|
|
|
|
// reduce register pressure.
|
|
|
|
|
score[v.ID] = 1
|
2015-08-03 12:33:03 -07:00
|
|
|
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.
|
2015-08-10 11:10:53 -07:00
|
|
|
score[v.ID] = 3
|
2015-08-03 12:33:03 -07:00
|
|
|
default:
|
2015-08-10 11:10:53 -07:00
|
|
|
score[v.ID] = 2
|
2015-05-30 01:03:40 -04: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-08-10 11:10:53 -07:00
|
|
|
score[b.Control.ID] = 4
|
2015-08-03 12:33:03 -07:00
|
|
|
// TODO: some times control values are used by other values
|
|
|
|
|
// in the block. So the control value will not appear at
|
|
|
|
|
// the very end. Decide if this is a problem or not.
|
|
|
|
|
}
|
2015-05-30 01:03:40 -04:00
|
|
|
|
2015-08-03 12:33:03 -07:00
|
|
|
// Initialize priority queue with schedulable values.
|
|
|
|
|
for i := range priq {
|
|
|
|
|
priq[i] = priq[i][:0]
|
|
|
|
|
}
|
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 {
|
|
|
|
|
s := score[v.ID]
|
|
|
|
|
priq[s] = append(priq[s], 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.
|
|
|
|
|
var v *Value
|
|
|
|
|
for i := len(priq) - 1; i >= 0; i-- {
|
|
|
|
|
n := len(priq[i])
|
|
|
|
|
if n == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
v = priq[i][n-1]
|
|
|
|
|
priq[i] = priq[i][:n-1]
|
|
|
|
|
break
|
2015-07-13 23:52:59 -07:00
|
|
|
}
|
2015-08-03 12:33:03 -07:00
|
|
|
if v == nil {
|
|
|
|
|
break
|
2015-03-23 17:02:11 -07:00
|
|
|
}
|
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.
|
|
|
|
|
s := score[w.ID]
|
|
|
|
|
priq[s] = append(priq[s], 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.
|
|
|
|
|
s := score[w.ID]
|
|
|
|
|
priq[s] = append(priq[s], w)
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|