| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mmcloughlin/avo/ir" |
| 5 | "github.com/mmcloughlin/avo/operand" |
| 6 | ) |
| 7 | |
| 8 | // PruneJumpToFollowingLabel removes jump instructions that target an |
| 9 | // immediately following label. |
| 10 | func PruneJumpToFollowingLabel(fn *ir.Function) error { |
| 11 | for i := 0; i+1 < len(fn.Nodes); i++ { |
| 12 | node := fn.Nodes[i] |
| 13 | next := fn.Nodes[i+1] |
| 14 | |
| 15 | // This node is an unconditional jump. |
| 16 | inst, ok := node.(*ir.Instruction) |
| 17 | if !ok || !inst.IsBranch || inst.IsConditional { |
| 18 | continue |
| 19 | } |
| 20 | |
| 21 | target := inst.TargetLabel() |
| 22 | if target == nil { |
| 23 | continue |
| 24 | } |
| 25 | |
| 26 | // And the jump target is the immediately following node. |
| 27 | lbl, ok := next.(ir.Label) |
| 28 | if !ok || lbl != *target { |
| 29 | continue |
| 30 | } |
| 31 | |
| 32 | // Then the jump is unnecessary and can be removed. |
| 33 | fn.Nodes = deletenode(fn.Nodes, i) |
| 34 | i-- |
| 35 | } |
| 36 | |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | // PruneDanglingLabels removes labels that are not referenced by any branches. |
| 41 | func PruneDanglingLabels(fn *ir.Function) error { |
| 42 | // Count label references. |
| 43 | count := map[ir.Label]int{} |
| 44 | for _, n := range fn.Nodes { |
| 45 | i, ok := n.(*ir.Instruction) |
| 46 | if !ok || !i.IsBranch { |
| 47 | continue |
| 48 | } |
| 49 | |
| 50 | target := i.TargetLabel() |
| 51 | if target == nil { |
| 52 | continue |
| 53 | } |
| 54 | |
| 55 | count[*target]++ |
| 56 | } |
| 57 | |
| 58 | // Look for labels with no references. |
| 59 | for i := 0; i < len(fn.Nodes); i++ { |
| 60 | node := fn.Nodes[i] |
| 61 | lbl, ok := node.(ir.Label) |
| 62 | if !ok { |
| 63 | continue |
| 64 | } |
| 65 | |
| 66 | if count[lbl] == 0 { |
| 67 | fn.Nodes = deletenode(fn.Nodes, i) |
| 68 | i-- |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // PruneSelfMoves removes move instructions from one register to itself. |
| 76 | func PruneSelfMoves(fn *ir.Function) error { |
| 77 | return removeinstructions(fn, func(i *ir.Instruction) bool { |
| 78 | switch i.Opcode { |
| 79 | case "MOVB", "MOVW", "MOVL", "MOVQ": |
| 80 | default: |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | return operand.IsRegister(i.Operands[0]) && operand.IsRegister(i.Operands[1]) && i.Operands[0] == i.Operands[1] |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | // removeinstructions deletes instructions from the given function which match predicate. |
| 89 | func removeinstructions(fn *ir.Function, predicate func(*ir.Instruction) bool) error { |
| 90 | // Removal of instructions has the potential to invalidate CFG structures. |
| 91 | // Clear them to prevent accidental use of stale structures after this pass. |
| 92 | invalidatecfg(fn) |
| 93 | |
| 94 | for i := 0; i < len(fn.Nodes); i++ { |
| 95 | n := fn.Nodes[i] |
| 96 | |
| 97 | inst, ok := n.(*ir.Instruction) |
| 98 | if !ok || !predicate(inst) { |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | fn.Nodes = deletenode(fn.Nodes, i) |
| 103 | } |
| 104 | |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // deletenode deletes node i from nodes and returns the resulting slice. |
| 109 | func deletenode(nodes []ir.Node, i int) []ir.Node { |
| 110 | n := len(nodes) |
| 111 | copy(nodes[i:], nodes[i+1:]) |
| 112 | nodes[n-1] = nil |
| 113 | return nodes[:n-1] |
| 114 | } |
| 115 | |
| 116 | // invalidatecfg clears CFG structures. |
| 117 | func invalidatecfg(fn *ir.Function) { |
| 118 | fn.LabelTarget = nil |
| 119 | for _, i := range fn.Instructions() { |
| 120 | i.Pred = nil |
| 121 | i.Succ = nil |
| 122 | } |
| 123 | } |