pass: first attempt at register allocation

This commit is contained in:
Michael McLoughlin
2018-12-05 00:05:57 -08:00
parent 9376a230cf
commit 022cbb7792
9 changed files with 394 additions and 24 deletions

View File

@@ -1,12 +1,18 @@
package pass
import (
"errors"
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/reg"
)
// Liveness computes register liveness.
func Liveness(fn *avo.Function) error {
// Note this implementation is initially naive so as to be "obviously correct".
// There are a well-known optimizations we can apply if necessary.
is := fn.Instructions()
// Initialize to empty sets.
@@ -51,5 +57,58 @@ func Liveness(fn *avo.Function) error {
}
func AllocateRegisters(fn *avo.Function) error {
// Build one allocator per register kind and record register interferences.
as := map[reg.Kind]*Allocator{}
for _, i := range fn.Instructions() {
for _, d := range i.OutputRegisters() {
k := d.Kind()
if _, found := as[k]; !found {
a, err := NewAllocatorForKind(k)
if err != nil {
return err
}
as[k] = a
}
out := i.LiveOut.OfKind(k)
out.Discard(d)
as[k].AddInterferenceSet(d, out)
}
}
// Execute register allocation.
fn.Allocation = reg.NewEmptyAllocation()
for _, a := range as {
al, err := a.Allocate()
if err != nil {
return err
}
if err := fn.Allocation.Merge(al); err != nil {
return err
}
}
return nil
}
func BindRegisters(fn *avo.Function) error {
for _, i := range fn.Instructions() {
for idx := range i.Operands {
i.Operands[idx] = operand.ApplyAllocation(i.Operands[idx], fn.Allocation)
}
}
return nil
}
func VerifyAllocation(fn *avo.Function) error {
// All registers should be physical.
for _, i := range fn.Instructions() {
for _, r := range i.Registers() {
if reg.ToPhysical(r) == nil {
return errors.New("non physical register found")
}
}
}
return nil
}