pass: remove redundant jumps and dangling labels (#81)
In jump-table-like constructs, the natural way of writing the code can sometimes produce redundant jumps or labels. Therefore some basic cleanup steps have been proposed. This diff adds two transforms: 1. Remove unconditional jumps to a label immediately following. 2. Remove labels with no references at all. Fixes #75
This commit is contained in:
committed by
GitHub
parent
57c23b967e
commit
2e7d06bc7a
28
tests/labels/asm.go
Normal file
28
tests/labels/asm.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
. "github.com/mmcloughlin/avo/operand"
|
||||
. "github.com/mmcloughlin/avo/reg"
|
||||
)
|
||||
|
||||
func main() {
|
||||
TEXT("Labels", NOSPLIT, "func() uint64")
|
||||
XORQ(RAX, RAX)
|
||||
INCQ(RAX)
|
||||
Label("neverused")
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
JMP(LabelRef("next"))
|
||||
Label("next")
|
||||
INCQ(RAX)
|
||||
INCQ(RAX)
|
||||
Store(RAX, ReturnIndex(0))
|
||||
RET()
|
||||
|
||||
Generate()
|
||||
}
|
||||
2
tests/labels/doc.go
Normal file
2
tests/labels/doc.go
Normal file
@@ -0,0 +1,2 @@
|
||||
// Package labels tests for cleanup of redundant labels.
|
||||
package labels
|
||||
16
tests/labels/labels.s
Normal file
16
tests/labels/labels.s
Normal file
@@ -0,0 +1,16 @@
|
||||
// Code generated by command: go run asm.go -out labels.s -stubs stub.go. DO NOT EDIT.
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// func Labels() uint64
|
||||
TEXT ·Labels(SB), NOSPLIT, $0-8
|
||||
XORQ AX, AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
INCQ AX
|
||||
MOVQ AX, ret+0(FP)
|
||||
RET
|
||||
15
tests/labels/labels_test.go
Normal file
15
tests/labels/labels_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package labels
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"testing/quick"
|
||||
)
|
||||
|
||||
//go:generate go run asm.go -out labels.s -stubs stub.go
|
||||
|
||||
func TestLabels(t *testing.T) {
|
||||
expect := func() uint64 { return 7 }
|
||||
if err := quick.CheckEqual(Labels, expect, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
5
tests/labels/stub.go
Normal file
5
tests/labels/stub.go
Normal file
@@ -0,0 +1,5 @@
|
||||
// Code generated by command: go run asm.go -out labels.s -stubs stub.go. DO NOT EDIT.
|
||||
|
||||
package labels
|
||||
|
||||
func Labels() uint64
|
||||
Reference in New Issue
Block a user