func invalidatecfg(fn *ir.Function) {
	fn.LabelTarget = nil
	for _, i := range fn.Instructions() {
		i.Pred = nil
		i.Succ = nil
	}
}
invalidatecfg clears CFG structures.
package pass
import (
	"github.com/mmcloughlin/avo/ir"
	"github.com/mmcloughlin/avo/operand"
)
func PruneJumpToFollowingLabel(fn *ir.Function) error {
	for i := 0; i+1 < len(fn.Nodes); i++ {
		node := fn.Nodes[i]
		next := fn.Nodes[i+1]
		inst, ok := node.(*ir.Instruction)
		if !ok || !inst.IsBranch || inst.IsConditional {
			continue
		}
		target := inst.TargetLabel()
		if target == nil {
			continue
		}
		lbl, ok := next.(ir.Label)
		if !ok || lbl != *target {
			continue
		}
		fn.Nodes = deletenode(fn.Nodes, i)
		i--
	}
	return nil
}
func PruneDanglingLabels(fn *ir.Function) error {
	count := map[ir.Label]int{}
	for _, n := range fn.Nodes {
		i, ok := n.(*ir.Instruction)
		if !ok || !i.IsBranch {
			continue
		}
		target := i.TargetLabel()
		if target == nil {
			continue
		}
		count[*target]++
	}
	for i := 0; i < len(fn.Nodes); i++ {
		node := fn.Nodes[i]
		lbl, ok := node.(ir.Label)
		if !ok {
			continue
		}
		if count[lbl] == 0 {
			fn.Nodes = deletenode(fn.Nodes, i)
			i--
		}
	}
	return nil
}
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]
	})
}
func removeinstructions(fn *ir.Function, predicate func(*ir.Instruction) bool) error {
	invalidatecfg(fn)
	for i := 0; i < len(fn.Nodes); i++ {
		n := fn.Nodes[i]
		inst, ok := n.(*ir.Instruction)
		if !ok || !predicate(inst) {
			continue
		}
		fn.Nodes = deletenode(fn.Nodes, i)
	}
	return nil
}
func deletenode(nodes []ir.Node, i int) []ir.Node {
	n := len(nodes)
	copy(nodes[i:], nodes[i+1:])
	nodes[n-1] = nil
	return nodes[:n-1]
}