pass: add PruneSelfMoves cleanup pass (#80)

In some cases natural use of abstraction in avo programs can lead to redundant move instructions, specifically self-moves such as MOVQ AX, AX. This does not produce incorrect code but it is incorrect and inelegant.

This diff introduces a PruneSelfMoves pass that removes such unnecessary instructions.

Closes #76
This commit is contained in:
Michael McLoughlin
2019-04-14 14:26:28 -07:00
committed by GitHub
parent 5d3176b111
commit 57c23b967e
10 changed files with 196 additions and 25 deletions

50
pass/cleanup.go Normal file
View File

@@ -0,0 +1,50 @@
package pass
import (
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/operand"
)
// PruneSelfMoves removes move instructions from one register to itself.
func PruneSelfMoves(fn *ir.Function) error {
return removeinstructions(fn, func(i *ir.Instruction) bool {
switch i.Opcode {
case "MOVB", "MOVW", "MOVL", "MOVQ":
default:
return false
}
return operand.IsRegister(i.Operands[0]) && operand.IsRegister(i.Operands[1]) && i.Operands[0] == i.Operands[1]
})
}
// removeinstructions deletes instructions from the given function which match predicate.
func removeinstructions(fn *ir.Function, predicate func(*ir.Instruction) bool) error {
// Removal of instructions has the potential to invalidate CFG structures.
// Clear them to prevent accidental use of stale structures after this pass.
invalidatecfg(fn)
for i := 0; i < len(fn.Nodes); i++ {
n := fn.Nodes[i]
inst, ok := n.(*ir.Instruction)
if !ok || !predicate(inst) {
continue
}
copy(fn.Nodes[i:], fn.Nodes[i+1:])
fn.Nodes[len(fn.Nodes)-1] = nil
fn.Nodes = fn.Nodes[:len(fn.Nodes)-1]
}
return nil
}
// invalidatecfg clears CFG structures.
func invalidatecfg(fn *ir.Function) {
fn.LabelTarget = nil
for _, i := range fn.Instructions() {
i.Pred = nil
i.Succ = nil
}
}

44
pass/cleanup_test.go Normal file
View File

@@ -0,0 +1,44 @@
package pass_test
import (
"reflect"
"testing"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/operand"
"github.com/mmcloughlin/avo/pass"
"github.com/mmcloughlin/avo/reg"
)
func TestPruneSelfMoves(t *testing.T) {
// Construct a function containing a self-move.
ctx := build.NewContext()
ctx.Function("add")
ctx.MOVQ(operand.U64(1), reg.RAX)
ctx.MOVQ(operand.U64(2), reg.RCX)
ctx.MOVQ(reg.RAX, reg.RAX) // self move
ctx.MOVQ(reg.RCX, reg.R8)
ctx.ADDQ(reg.R8, reg.RAX)
// Build the function without the pass and save the nodes.
fn := BuildFunction(t, ctx)
pre := append([]ir.Node{}, fn.Nodes...)
// Apply the pass.
if err := pass.PruneSelfMoves(fn); err != nil {
t.Fatal(err)
}
// Confirm the self-move was removed and everything else was untouched.
expect := []ir.Node{}
for i, n := range pre {
if i != 2 {
expect = append(expect, n)
}
}
if !reflect.DeepEqual(fn.Nodes, expect) {
t.Fatal("unexpected result from self-move pruning")
}
}

View File

@@ -18,6 +18,7 @@ var Compile = Concat(
FunctionPass(BindRegisters),
FunctionPass(VerifyAllocation),
Func(IncludeTextFlagHeader),
FunctionPass(PruneSelfMoves),
)
// Interface for a processing pass.

View File

@@ -3,7 +3,6 @@ package pass_test
import (
"testing"
"github.com/mmcloughlin/avo/internal/test"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/reg"
@@ -58,28 +57,5 @@ func AssertRegistersMatchSet(t *testing.T, rs []reg.Register, s reg.Set) {
}
func ConstructLiveness(t *testing.T, ctx *build.Context) *ir.Function {
f, err := ctx.Result()
if err != nil {
build.LogError(test.Logger(t), err, 0)
t.FailNow()
}
fns := f.Functions()
if len(fns) != 1 {
t.Fatalf("expect 1 function")
}
fn := fns[0]
passes := []func(*ir.Function) error{
pass.LabelTarget,
pass.CFG,
pass.Liveness,
}
for _, p := range passes {
if err := p(fn); err != nil {
t.Fatal(err)
}
}
return fn
return BuildFunction(t, ctx, pass.LabelTarget, pass.CFG, pass.Liveness)
}

36
pass/util_test.go Normal file
View File

@@ -0,0 +1,36 @@
package pass_test
import (
"testing"
"github.com/mmcloughlin/avo/build"
"github.com/mmcloughlin/avo/internal/test"
"github.com/mmcloughlin/avo/ir"
"github.com/mmcloughlin/avo/pass"
)
// BuildFunction is a helper to compile a build context containing a single
// function and (optionally) apply a list of FunctionPasses to it.
func BuildFunction(t *testing.T, ctx *build.Context, passes ...pass.FunctionPass) *ir.Function {
t.Helper()
f, err := ctx.Result()
if err != nil {
build.LogError(test.Logger(t), err, 0)
t.FailNow()
}
fns := f.Functions()
if len(fns) != 1 {
t.Fatalf("expect 1 function")
}
fn := fns[0]
for _, p := range passes {
if err := p(fn); err != nil {
t.Fatal(err)
}
}
return fn
}