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

View File

@@ -0,0 +1,29 @@
// +build ignore
package main
import (
. "github.com/mmcloughlin/avo/build"
. "github.com/mmcloughlin/avo/reg"
)
func main() {
TEXT("Issue76", NOSPLIT, "func(x, y uint64) uint64")
x := Load(Param("x"), GP64())
y := Load(Param("y"), GP64())
s := add(x, y)
Store(s, ReturnIndex(0))
RET()
Generate()
}
// add generates code to add x and y. The intent here is to demonstrate how a
// natural subroutine in avo typically requires temporary registers, which in
// turn can be "optimized out" by the register allocator and result in redundant
// self-moves.
func add(x, y Register) Register {
s := GP64()
MOVQ(x, s) // likely to become a self move
ADDQ(y, s)
return s
}

View File

@@ -0,0 +1,4 @@
// Package issue76 deliberately produces redundant MOV instructions.
//
// The intent is to confirm redundant self-move instructions are correctly removed.
package issue76

View File

@@ -0,0 +1,11 @@
// Code generated by command: go run asm.go -out issue76.s -stubs stub.go. DO NOT EDIT.
#include "textflag.h"
// func Issue76(x uint64, y uint64) uint64
TEXT ·Issue76(SB), NOSPLIT, $0-24
MOVQ x+0(FP), AX
MOVQ y+8(FP), CX
ADDQ CX, AX
MOVQ AX, ret+16(FP)
RET

View File

@@ -0,0 +1,15 @@
package issue76
import (
"testing"
"testing/quick"
)
//go:generate go run asm.go -out issue76.s -stubs stub.go
func TestIssue76(t *testing.T) {
expect := func(x, y uint64) uint64 { return x + y }
if err := quick.CheckEqual(Issue76, expect, nil); err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,5 @@
// Code generated by command: go run asm.go -out issue76.s -stubs stub.go. DO NOT EDIT.
package issue76
func Issue76(x uint64, y uint64) uint64