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
12
ir/ir.go
12
ir/ir.go
@@ -212,6 +212,18 @@ func (f *Function) Instructions() []*Instruction {
|
||||
return is
|
||||
}
|
||||
|
||||
// Labels returns just the list of label nodes.
|
||||
func (f *Function) Labels() []Label {
|
||||
var lbls []Label
|
||||
for _, n := range f.Nodes {
|
||||
lbl, ok := n.(Label)
|
||||
if ok {
|
||||
lbls = append(lbls, lbl)
|
||||
}
|
||||
}
|
||||
return lbls
|
||||
}
|
||||
|
||||
// Stub returns the Go function declaration.
|
||||
func (f *Function) Stub() string {
|
||||
return "func " + f.Name + f.Signature.String()
|
||||
|
||||
22
ir/ir_test.go
Normal file
22
ir/ir_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package ir
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFunctionLabels(t *testing.T) {
|
||||
f := NewFunction("labels")
|
||||
f.AddInstruction(&Instruction{})
|
||||
f.AddLabel("a")
|
||||
f.AddInstruction(&Instruction{})
|
||||
f.AddLabel("b")
|
||||
f.AddInstruction(&Instruction{})
|
||||
f.AddLabel("c")
|
||||
f.AddInstruction(&Instruction{})
|
||||
|
||||
expect := []Label{"a", "b", "c"}
|
||||
if got := f.Labels(); !reflect.DeepEqual(expect, got) {
|
||||
t.Fatalf("f.Labels() = %v; expect %v", got, expect)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user