replace restart {
        sta	%1
        stx	%2
	lda     %1
	ldx     %2
} by {
    ; Peephole 1sta-lda   - eliminated redundant lda/ldx
        sta	%1
        stx	%2
} if notVolatile(%1), notVolatile(%2)


replace restart {
        %1	%2
        %1	%3
} by {
    ; Peephole 1ldld   - eliminated unused load
	%1	%3
} if same(%1 'lda' 'ldx' 'ldy'), notVolatile(%2)

replace restart {
	pla
	pha
	lda	%1
} by {
	; Peephole 1ldst - removed redundant pla/pha followed by lda
	lda	%1
}

replace restart {
	pla
	pha
	%1
} by {
	; Peephole 1txst - removed redundant pla/pha followed by txa/tya
	%1
}  if same(%1 'txa' 'tya')


replace restart {
	php
	%1 %2
	plp
} by {
	; Peephole stack 3 - removed redundant php/plp around a store
	%1 %2
}  if same(%1 'sta' 'stx' 'sty' )


replace restart {
	php
	plp
} by {
	; Peephole stack 4 - removed redundant php/plp
}

replace restart {
	%1
	lda	%2
} by {
	; Peephole 2txld - removed unused txa/tya folowed by lda
	lda	%2
} if same(%1 'txa' 'tya')

replace restart {
	%1
	ldx	%2
} by {
	; Peephole 2b - removed unused tax/inx/tsx followed by ldx
	ldx	%2
} if same(%1 'tax' 'inx' 'tsx')

replace restart {
	tax
	tsx
} by {
	; Peephole 2c - removed unused tax followed by tsx
	tsx
}

replace restart {
	tsx
	tax
} by {
	; Peephole 2d - removed unused tsx followed by tax
	tax
}

replace restart {
	%1
	ldy	%2
} by {
	; Peephole 2e - removed unused tay/iny followed by ldy
	ldy	%2
} if same(%1 'tay' 'iny')

replace restart {
	tax
	txa
} by {
	; Peephole 2f - removed unused txa preceded by tax
	tax
}

replace restart {
	tay
	tya
} by {
	; Peephole 2g - removed unused tya preceded by tay
	tay
}

replace restart {
	ldx %1
	tax
} by {
	; Peephole 2f - removed unused ldx followed by tax
	tax
}

replace restart {
	ldy %1
	tay
} by {
	; Peephole 2g - removed unused ldy followed by tay
	tay
}

////////////////////////////////////////////////////////
// Remove unnecessary cmp/cpx/cpy #00
////////////////////////////////////////////////////////

replace restart {
	lda	%1
	cmp	#0x00
} by {
	; Peephole 3a	- eliminated redundant cmp #0x00
	lda	%1
}

replace restart {
	pla
	cmp	#0x00
} by {
	; Peephole 3b	- eliminated redundant cmp #0x00
	pla
}

replace restart {
	ldx	%1
	cpx	#0x00
} by {
	; Peephole 3c	- eliminated redundant cpx #0x00
	ldx	%1
}

replace restart {
	ldy	%1
	cpy	#0x00
} by {
	; Peephole 3d	- eliminated redundant cpy #0x00
	ldy	%1
}

replace restart {
	%1
	cpx	#0x00
} by {
	; Peephole 3e	- eliminated redundant cpx #0x00
	%1
} if same(%1 'inx' 'dex' )

replace restart {
	%1
	cpy	#0x00
} by {
	; Peephole 3f	- eliminated redundant cpy #0x00
	%1
} if same(%1 'iny' 'dey' )

////////////////////////////////////////////////////////
// 
////////////////////////////////////////////////////////

replace restart {
	lda	%2
	sta	%2
} by {
	; Peephole 4sta - removed redundant sta
	lda	%2
}

replace restart {
	lda	%1
	pla
} by {
	; Peephole 4lda - removed dead lda
	pla
}

replace restart {
	ldx	%2
	stx	%2
} by {
	; Peephole 4stx - removed redundant stx
	ldx	%2
}

replace restart {
	ldy	%2
	sty	%2
} by {
	; Peephole 4sty - removed redundant sty
	ldy	%2
}



replace restart {
	lda	%2
	%1 	a
	sta	%2
} by {
	; Peephole 4st - removed redundant sta
	%1	%2
	lda	%2
} if same(%1 'asl' 'rol' 'ror')

replace restart {
	lda	%4
	%1	%2
	lda	%3
} by {
	; Peephole 4a - removed redundant lda not followed by sta or branch
	%1	%2
	lda	%3
} if notSame(%1 'sta' 'adc' 'sbc' 'cmp' 'bne' 'beq' 'bpl' 'bmi' 'jsr' 'jmp'), notSame(%2 'a')

//same(%1 'ldx' 'ldy' 'stx' 'sty' 'cpx' 'cpy')

replace restart {
	ldx	%4
	%1	%2
	ldx	%3
} by {
	; Peephole 4x - removed redundant lda not followed by sta or branch
	%1	%2
	ldx	%3
} if notSame(%1 'stx' 'cpx' 'bne' 'beq' 'bpl' 'bmi' 'jsr' 'jmp')

replace restart {
	ldy	%4
	%1 	%2
	ldy	%3
} by {
	; Peephole 4y - removed redundant lda not followed by sta or branch
	%1	%2
	ldy	%3
} if notSame(%1 'sty' 'cpy' 'bne' 'beq' 'bpl' 'bmi' 'jsr' 'jmp')

replace restart {
	lda	%3
	%1
	lda	%2
} by {
	; Peephole 4a1 - removed redundant lda (followed by inx/iny/dex/dey)
	%1 
	lda	%2
} if notSame(%1 'pha' 'php' 'tax' 'tay')

replace restart {
	ldx	%3
	%1
	ldx	%2
} by {
	; Peephole 4x1 - removed redundant ldx (followed by inx/iny/dex/dey)
	%1 
	ldx	%2
} if notSame(%1 'txa' 'txs')


////////////////////////////////////////////////////////
// Replace jmp with branch
////////////////////////////////////////////////////////

replace restart {
	bne	%1
	jmp	%5
%1:
} by {
	; Peephole 5a	- eliminated jmp
	beq	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	beq	%1
	jmp	%5
%1:
} by {
	; Peephole 5b	- eliminated jmp
	bne	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bcc	%1
	jmp	%5
%1:
} by {
	; Peephole 5c	- eliminated jmp
	bcs	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bcs	%1
	jmp	%5
%1:
} by {
	; Peephole 5d	- eliminated jmp
	bcc	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bpl	%1
	jmp	%5
%1:
} by {
	; Peephole 5e	- eliminated jmp
	bmi	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bmi	%1
	jmp	%5
%1:
} by {
	; Peephole 5f	- eliminated jmp
	bpl	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bvc	%1
	jmp	%5
%1:
} by {
	; Peephole 5g	- eliminated jmp
	bvs	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

replace restart {
	bvs	%1
	jmp	%5
%1:
} by {
	; Peephole 5h	- eliminated jmp
	bvc	%5
%1:
} if labelInRange(%5), labelRefCountChange(%1 -1)

////////////////////////////////////////////////////////
// Replace branch to jmp with branch
////////////////////////////////////////////////////////

replace restart {
	%1	%5
} by {
	; Peephole 6b  - replaced branch to jmp with branch
	%1	%6
} if same(%1 'bne' 'beq' 'bcc' 'bcs' 'bvc' 'bvs' 'bpl' 'bmi'), labelIsUncondJump(%5), notSame(%5 %6), labelInRange(%6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)

replace restart {
	jmp	%5
} by {
	; Peephole 6c  - replaced jmp to jmp with jmp
	jmp	%6
} if labelIsUncondJump(%5), notSame(%5 %6), labelRefCountChange(%5 -1), labelRefCountChange(%6 +1)

replace restart {
	jmp	%5
} by {
	; Peephole 6a  - replaced jmp to rts with rts
	rts
} if optimizeReturn(), labelIsReturnOnly(%5), labelRefCountChange(%5 -1)

////////////////////////////////////////////////////////
// Remove branch/jump to following label
////////////////////////////////////////////////////////

replace restart {
        %1	%2
%2:
} by {
        ; Peephole 7   - eliminated branch
%2:
} if same(%1 'bne' 'beq' 'bcc' 'bcs' 'bvc' 'bvs' 'bpl' 'bmi' 'jmp' 'bra'), labelRefCountChange(%2 -1)


// should be one of the last peepholes
replace restart {
%1:
} by {
	; Peephole 500    removed redundant label %1
} if labelRefCount(%1 0)

replace restart {
	jsr	%1
	rts
} by {
	; Peephole 501 did tail call optimization.
	jmp	%1
} if optimizeReturn()
