llvm-mos
llvm-mos copied to clipboard
Small constant addition optimization
It may be a good idea to consider repeated increment/decrement operations for adding small constants to the X and Y registers instead of going through A and an ADC. I've found that for example
y += 2;
Tends to result in expensive register shuffles:
clc
tax
tya
adc #2
tay
txa
Instead of simply
iny
iny
Here's a synthetic example
__attribute((noinline))
uint8_t foo(uint8_t x, uint8_t y) {
if (x >= 3) { y += 2; x -= 3; }
return x+y;
}
foo:
stx __rc2
cmp #3
bcc .LBB0_2
clc
tax
lda __rc2
adc #2
sta __rc2
txa
clc
adc #253
.LBB0_2:
clc
adc __rc2
rts
vs the 10 cycles faster and 7 bytes shorter:
foo:
cmp #3
bcc .LBB0_2
inx
inx
clc
adc #253
.LBB0_2:
stx __rc2
clc
adc __rc2
rts