Files
avo/pass/reg.go

56 lines
1.0 KiB
Go
Raw Normal View History

2018-12-02 23:59:29 -08:00
package pass
import (
"github.com/mmcloughlin/avo"
"github.com/mmcloughlin/avo/reg"
)
// Liveness computes register liveness.
func Liveness(fn *avo.Function) error {
is := fn.Instructions()
// Initialize to empty sets.
for _, i := range is {
2018-12-03 22:39:43 -08:00
i.LiveIn = reg.NewEmptySet()
i.LiveOut = reg.NewEmptySet()
2018-12-02 23:59:29 -08:00
}
// Iterative dataflow analysis.
for {
changes := false
for _, i := range is {
// in[n] = use[n] UNION (out[n] - def[n])
nin := len(i.LiveIn)
2018-12-03 22:39:43 -08:00
i.LiveIn.Update(reg.NewSetFromSlice(i.InputRegisters()))
def := reg.NewSetFromSlice(i.OutputRegisters())
i.LiveIn.Update(i.LiveOut.Difference(def))
2018-12-02 23:59:29 -08:00
if len(i.LiveIn) != nin {
changes = true
}
// out[n] = UNION[s IN succ[n]] in[s]
nout := len(i.LiveOut)
for _, s := range i.Succ {
if s == nil {
continue
}
2018-12-03 22:39:43 -08:00
i.LiveOut.Update(s.LiveIn)
2018-12-02 23:59:29 -08:00
}
if len(i.LiveOut) != nout {
changes = true
}
}
if !changes {
break
}
}
return nil
}
2018-12-03 22:39:43 -08:00
func AllocateRegisters(fn *avo.Function) error {
return nil
}