Skip to content

Instantly share code, notes, and snippets.

@flamewing
Last active September 1, 2024 02:44
Show Gist options
  • Save flamewing/ad17bf22875be36ad4ae26f159a94f8b to your computer and use it in GitHub Desktop.
Save flamewing/ad17bf22875be36ad4ae26f159a94f8b to your computer and use it in GitHub Desktop.
Some peephole optimizations for M68000

M68000 Peephole Optimizations

TODO

Sources to go through:

General

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
move.b <ea>,dN
andi.b #mask,dN
moveq #mask,dN
and.b <ea>,dN
4(1/0) 2 bytes -128 ≤ mask ≤ 127
top bits of dN different
clr.l dN moveq #0,dN 2(0/0) 0 bytes
andi.l #$FFFF,dN moveq #0,dM
move.w dN,dM
8(1/0) 2 bytes Wrong flags
Result in dM instead of dN
Need free dM
andi.l #$FFFF,dN swap dN
clr.w dN
swap dN
4(0/0) 0 bytes Wrong flags
No spare registers
andi.l #$FFFF0000,dN clr.w dN 12(2/0) 4 bytes Wrong flags

Address Register Manipulations

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
movea.l #0,aN suba.l aN,aN 4(2/0) 4 bytes
movea.l #0,aN movea.l XM,aN 8(2/0) 4 bytes XM is already zero
movea.l #val,aN movea.w #val,aN 4(1/0) 4 bytes -32768 ≤ val ≤ 32767
adda.l #val,aN adda.w #val,aN 4(1/0) 2 bytes -32768 ≤ val ≤ 32767
adda.w #val,aN addq.w #val,aN 4(1/0) 2 bytes 1 ≤ val ≤ 8
adda.w #val,aN subq.w #-val,aN 4(1/0) 2 bytes -1 ≤ val ≤ -8
adda.w #val,aN lea val(aN),aN 4(0/0) 0 bytes -32768 ≤ val ≤ -9 or 9 ≤ val ≤ 32767
adda.w #val,aN
adda.S XN,aN
lea val(aN,XN.S),aN 8(1/0) 2 bytes -128 ≤ val ≤ 127
suba.w #val,aN subq.w #val,aN 4(1/0) 2 bytes 1 ≤ val ≤ 8
suba.w #val,aN addq.w #-val,aN 4(1/0) 2 bytes -1 ≤ val ≤ -8
suba.w #val,aN lea -val(aN),aN 4(0/0) 0 bytes -32767 ≤ val ≤ -9 or 9 ≤ val ≤ 32767
suba.w #val,aN
adda.S XN,aN
lea -val(aN,XN.S),aN 8(1/0) 2 bytes -127 ≤ val ≤ 128

Rotates

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
rol.b #4+x,dN ror.b #4-x,dN 4*x(0/0) 0 bytes 1 ≤ x ≤ 3
Wrong flags, dM different
moveq #8+x,dM
rol.w dM,dN
ror.w #8-x,dN 4+4*x(1/0) 2 bytes 0 ≤ x ≤ 7
Wrong flags, dM different
moveq #8+x,dM
rol.l dM,dN
swap dN
ror.l #8-x,dN
4*x(0/0) 0 bytes 1 ≤ x ≤ 7
Wrong flags, dM different
moveq #16,dM
rol.l dM,dN
swap dN 40(1/0) 2 bytes Wrong flags, dM different
moveq #16+x,dM
rol.l dM,dN
swap dN
rol.l #x,dN
32(0/0) 0 bytes 1 ≤ x ≤ 7
Wrong flags, dM different
moveq #16+x,dM
rol.l dM,dN
ror.l #16-x,dN 4+4*x(1/0) 2 bytes 8 ≤ x ≤ 15
Wrong flags, dM different
ror.b #4+x,dN rol.b #4-x,dN 4*x(0/0) 0 bytes 1 ≤ x ≤ 3
Wrong flags, dM different
moveq #8+x,dM
ror.w dM,dN
rol.w #8-x,dN 4+4*x(1/0) 2 bytes 0 ≤ x ≤ 7
Wrong flags, dM different
moveq #8+x,dM
ror.l dM,dN
swap dN
rol.l #8-x,dN
4*x(0/0) 0 bytes 1 ≤ x ≤ 7
Wrong flags, dM different
moveq #16,dM
ror.l dM,dN
swap dN 40(1/0) 2 bytes Wrong flags, dM different
moveq #16+x,dM
ror.l dM,dN
swap dN
ror.l #x,dN
32(0/0) 0 bytes 1 ≤ x ≤ 7
Wrong flags, dM different
moveq #16+x,dM
ror.l dM,dN
rol.l #16-x,dN 4+4*x(1/0) 2 bytes 8 ≤ x ≤ 15
Wrong flags, dM different
roxl.b #1,dN addx.b dN,dN 4(0/0) 0 bytes Wrong flags
roxl.b #2,dN addx.b dN,dN
addx.b dN,dN
2(-1/0) -2 bytes Wrong flags
roxl.w #1,dN addx.w dN,dN 4(0/0) 0 bytes Wrong flags
roxl.w #2,dN addx.w dN,dN
addx.w dN,dN
2(-1/0) -2 bytes Wrong flags
roxl.l #1,dN addx.l dN,dN 2(0/0) 0 bytes Wrong flags

Logical Shifts

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
lsl.b #1,dN add.b dN,dN 4(0/0) 0 bytes
lsl.b #2,dN add.b dN,dN
add.b dN,dN
2(-1/0) -2 bytes
lsl.b #7,dN ror.b #1,dN
andi.b #$80,dN
4(-2/0) -4 bytes Wrong flags
lsl.b #8,dN clr.b dN 18(0/0) 0 bytes Wrong flags
moveq #8+x,dM
lsl.b dM,dN
clr.b dN 18+2*x(1/0) 2 bytes 1 ≤ x ≤ 47
Wrong flags, dM different
lsl.w #1,dN add.w dN,dN 4(0/0) 0 bytes
lsl.w #2,dN add.w dN,dN
add.w dN,dN
2(-1/0) -2 bytes
lsl.w #8,dN move.b dN,-(sp)
move.w (sp)+,dN
clr.b dN
2(-3/-1) -4 bytes Wrong flags
moveq #9,dM
lsl.w dM,dN
move.b dN,-(sp)
move.w (sp)+,dN
clr.b dN
add.w dN,dN
4(-3/-1) -4 bytes Wrong flags, dM different
moveq #8+x,dM
lsl.w dM,dN
ror.w #8-x,dN
andi.w #~((1<<(8+x))-1),dN
4*x-4(-1/0) -2 bytes 2 ≤ x ≤ 7
Wrong flags, dM different
moveq #16+x,dM
lsl.w dM,dN
clr.w dN 38+2*x(1/0) 2 bytes 0 ≤ x ≤ 47
Wrong flags, dM different
lsl.l #1,dN add.l dN,dN 2(0/0) 0 bytes
moveq #8+x,dM
lsl.l dM,dN
swap dN
ror.l #8-x,dN
andi.w #~((1<<(8+x))-1),dN
4*x-8(-2/0) -4 bytes 3 ≤ x ≤ 7
Wrong flags, dM different
moveq #16,dM
lsl.l dM,dN
swap dN
clr.w dN
36(0/0) 0 bytes Wrong flags, dM different
moveq #17,dM
lsl.l dM,dN
add.w dN,dN
swap dN
clr.w dN
34(-1/0) -2 bytes Wrong flags, dM different
moveq #18,dM
lsl.l dM,dN
add.w dN,dN
add.w dN,dN
swap dN
clr.w dN
32(-2/0) -4 bytes Wrong flags, dM different
moveq #16+x,dM
lsl.l dM,dN
lsl.w #x,dN
swap dN
clr.w dN
30(-1/0) -2 bytes 3 ≤ x ≤ 7
dM different
moveq #24,dM
lsl.l dM,dN
move.b dN,-(sp)
move.w (sp)+,dN
clr.b dN
swap dN
clr.w dN
32(-4/-1) -6 bytes dM different
moveq #25,dM
lsl.l dM,dN
move.b dN,-(sp)
move.w (sp)+,dN
clr.b dN
add.w dN,dN
swap dN
clr.w dN
30(-5/-1) -8 bytes dM different
moveq #24+x,dM
lsl.l dM,dN
ror.w #8-x,dN
andi.w #~((1<<(8+x))-1),dN
swap dN
clr.w dN
4*x+22(-3/0) -6 bytes 2 ≤ x ≤ 7
dM different
moveq #32+x,dM
lsl.l dM,dN
moveq #0,dN 72+2*x(1/0) 2 bytes 0 ≤ x ≤ 31
Wrong flags, dM different
lsr.b #7,dN add.b dN,dN
subx.b dN,dN
neg.b dN
8(-2/0) -4 bytes Wrong flags
lsr.b #8,dN clr.b dN 18(0/0) 0 bytes Wrong flags
moveq #8+x,dM
lsr.b dM,dN
clr.b dN 18+2*x(1/0) 2 bytes 1 ≤ x ≤ 47
Wrong flags, dM different
lsr.w #8,dN move.w dN,-(sp)
clr.w dN
move.b (sp)+,dN
2(-2/-1) -4 bytes Wrong flags
moveq #8+x,dM
lsr.w dM,dN
andi.w #~((1<<(8+x))-1),dN
rol.w #8-x,dN
4*x-4(-1/0) -2 bytes 2 ≤ x ≤ 6
Wrong flags, dM different
moveq #15,dM
lsr.w dM,dN
add.w dN,dN
subx.w dN,dN
neg.w dN
28(-1/0) -2 bytes Wrong flags, dM different
moveq #16+x,dM
lsr.w dM,dN
clr.w dN 38+2*x(1/0) 2 bytes 0 ≤ x ≤ 47
Wrong flags, dM different
moveq #8+x,dM
lsr.l dM,dN
andi.w #~((1<<(8+x))-1),dN
swap dN
rol.l #8-x,dN
4*x-8(-2/0) -4 bytes 3 ≤ x ≤ 7
Wrong flags, dM different
moveq #16,dM
lsr.l dM,dN
clr.w dN
swap dN
36(0/0) 0 bytes Wrong flags, dM different
moveq #16+x,dM
lsr.l dM,dN
clr.w dN
swap dN
lsr.w #x,dN
30(-1/0) -2 bytes 1 ≤ x ≤ 7
dM different
moveq #24,dM
lsr.l dM,dN
swap dN
move.w dN,-(sp)
moveq #0,dN
move.b (sp)+,dN
36(-3/-1) -4 bytes Wrong flags
moveq #24+x,dM
lsr.l dM,dN
clr.w dN
swap dN
andi.w #~((1<<(8+x))-1),dN
rol.w #8-x,dN
4*x+22(-3/-0) -4 bytes 1 ≤ x ≤ 6
dM different
moveq #31,dM
lsr.l dM,dN
add.l dN,dN
moveq #0,dN
addx.w dN,dN
58(-1/0) -2 bytes Wrong flags, dM different
moveq #32+x,dM
lsr.l dM,dN
moveq #0,dN 72+2*x(1/0) 2 bytes 0 ≤ x ≤ 31
Wrong flags, dM different

Arithmetic Shifts

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
asr.b #7+x,dN add.b dN,dN
subx.b dN,dN
12+2*x(-1/0) -2 bytes 0 ≤ x ≤ 1
Wrong flags
asr.w #8,dN move.w dN,-(sp)
move.b (sp)+,dN
ext.w dN
2(-3/-1) -4 bytes Wrong flags, dM wrong
moveq #8+x,dM
asr.w dM,dN
ext.l dN
swap dN
rol.l #8-x,dN
4*x-6(-1/0) -2 bytes 2 ≤ x ≤ 6
Wrong flags, dM different
High word of dN different
moveq #15+x,dM
asr.w dM,dN
add.w dN,dN
subx.w dN,dN
32+2*x(0/0) -2 bytes 0 ≤ x ≤ 48
Wrong flags, dM wrong
moveq #16,dM
asr.l dM,dN
swap dN
ext.l dN
36(0/0) 0 bytes Wrong flags, dM different
moveq #16+x,dM
asr.l dM,dN
swap dN
ext.l dN
asr.w #x,dN
30(-1/0) -2 bytes 1 ≤ x ≤ 7
dM different
moveq #24,dM
asr.l dM,dN
swap dN
ext.l dN
move.w dN,-(sp)
move.b (sp)+,dN
ext.w dN
28(-4/-1) -6 bytes Wrong flags
dM different
moveq #25,dM
asr.l dM,dN
swap dN
ext.l dN
moveq #9,dM
asr.w dM,dN
26(-2/0) -4 bytes dM different
moveq #24+x,dM
asr.l dM,dN
swap dN
ext.l dN
swap dN
rol.l #8-x,dN
ext.l dN
20+4*x(-3/0) -6 bytes 2 ≤ x ≤ 6
dM different
moveq #31+x,dM
asr.l dM,dN
add.l dN,dN
subx.l dN,dN
58+2*x(0/0) -2 bytes 0 ≤ x ≤ 32
Wrong flags, dM wrong

All lsl peephole optimizations also apply to asl.

Multiplication by constants

If the high word of the result is important

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
muls.w #0,dN moveq #0,dN 38(1/0) 2 bytes Wrong flags
muls.w #1,dN ext.l dN 42(1/0) 2 bytes Wrong flags
muls.w #2,dN ext.l dN
add.l dN,dN
34(0/0) 0 bytes Wrong flags
muls.w #3,dN ext.l dN
move.l dN,dM
add.l dN,dN
add.l dM,dN
24(-2/0) -4 bytes Wrong flags
dM different
muls.w #4,dN ext.l dN
asl.l #2,dN
30(0/0) 0 bytes Wrong flags
muls.w #5,dN ext.l dN
move.l dN,dM
asl.l #2,dN
add.l dM,dN
22(-2/0) -4 bytes Wrong flags
dM different
muls.w #6,dN ext.l dN
add.l dN,dN
move.l dN,dM
add.l dN,dN
add.l dM,dN
16(-3/0) -6 bytes Wrong flags
dM different
muls.w #7,dN ext.l dN
move.l dN,dM
asl.l #3,dN
sub.l dM,dN
20(-2/0) -4 bytes Wrong flags
dM different
muls.w #8,dN ext.l dN
asl.l #3,dN
28(0/0) 0 bytes Wrong flags
muls.w #9,dN ext.l dN
move.l dN,dM
asl.l #3,dN
add.l dM,dN
20(-2/0) -4 bytes Wrong flags
dM different
muls.w #10,dN ext.l dN
move.l dN,dM
asl.l #2,dN
add.l dM,dN
add.l dN,dN
14(-3/0) -6 bytes Wrong flags
dM different
muls.w #11,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #2,dN
sub.l dM,dN
16(-3/0) -8 bytes Wrong flags
dM different
muls.w #12,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #2,dN
4(-4/0) -6 bytes Wrong flags
dM different
muls.w #13,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #2,dN
add.l dM,dN
8(-4/0) -8 bytes Wrong flags
dM different
muls.w #14,dN ext.l dN
move.l dN,dM
asl.l #3,dN
sub.l dM,dN
add.l dN,dN
12(-3/0) -6 bytes Wrong flags
dM different
muls.w #15,dN ext.l dN
move.l dN,dM
asl.l #4,dN
sub.l dM,dN
20(-2/0) -4 bytes Wrong flags
dM different
muls.w #16,dN ext.l dN
asl.l #4,dN
26(0/0) 0 bytes Wrong flags
muls.w #17,dN ext.l dN
move.l dN,dM
asl.l #4,dN
add.l dM,dN
18(-2/0) -4 bytes Wrong flags
dM different
muls.w #18,dN ext.l dN
add.l dN,dN
move.l dN,dM
asl.l #3,dN
add.l dM,dN
12(-3/0) -6 bytes Wrong flags
dM different
muls.w #19,dN ext.l dN
move.l dN,dM
asl.l #3,dN
add.l dM,dN
add.l dN,dN
add.l dM,dN
6(-4/0) -8 bytes Wrong flags
dM different
muls.w #20,dN ext.l dN
move.l dN,dM
asl.l #2,dN
add.l dM,dN
asl.l #2,dN
10(-3/0) -6 bytes Wrong flags
dM different
muls.w #21,dN ext.l dN
move.l dN,dM
asl.l #2,dN
add.l dM,dN
asl.l #2,dN
add.l dM,dN
6(-4/0) -8 bytes Wrong flags
dM different
muls.w #22,dN ext.l dN
add.l dN,dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #2,dN
sub.l dM,dN
8(-4/0) -10 bytes Wrong flags
dM different
muls.w #23,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #3,dN
sub.l dM,dN
6(-5/0) -8 bytes Wrong flags
dM different
muls.w #24,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #3,dN
8(-3/0) -6 bytes Wrong flags
dM different
muls.w #25,dN ext.l dN
move.l dN,dM
add.l dM,dN
add.l dM,dN
asl.l #3,dN
add.l dM,dN
4(-5/0) -8 bytes Wrong flags
dM different
muls.w #26,dN ext.l dN
move.l dN,dM
add.l dM,dM
add.l dM,dN
asl.l #3,dN
add.l dM,dN
4(-5/0) -8 bytes Wrong flags
dM different
muls.w #29,dN ext.l dN
move.l dN,dM
asl.l #5,dN
sub.l dM,dN
sub.l dM,dN
sub.l dM,dN
4(-4/0) -8 bytes Wrong flags
dM different
muls.w #30,dN ext.l dN
move.l dN,dM
asl.l #5,dN
sub.l dM,dN
sub.l dM,dN
10(-3/0) -6 bytes Wrong flags
dM different
muls.w #31,dN ext.l dN
move.l dN,dM
asl.l #5,dN
sub.l dM,dN
20(-2/0) -4 bytes Wrong flags
dM different
muls.w #32,dN ext.l dN
asl.l #5,dN
24(0/0) 0 bytes Wrong flags
muls.w #33,dN ext.l dN
move.l dN,dM
asl.l #5,dN
add.l dM,dN
16(-2/0) -4 bytes Wrong flags
dM different
muls.w #34,dN ext.l dN
move.l dN,dM
asl.l #5,dN
add.l dM,dN
add.l dM,dN
8(-3/0) -6 bytes Wrong flags
dM different
muls.w #35,dN ext.l dN
move.l dN,dM
asl.l #5,dN
add.l dM,dN
add.l dM,dN
add.l dM,dN
2(-4/0) -8 bytes Wrong flags
dM different
muls.w #64,dN ext.l dN
asl.l #6,dN
22(0/0) 0 bytes Wrong flags
muls.w #128,dN ext.l dN
asl.l #7,dN
20(0/0) 0 bytes Wrong flags
muls.w #256,dN ext.l dN
asl.l #8,dN
18(0/0) 0 bytes Wrong flags
mulu.w #0,dN moveq #0,dM 38(1/0) 2 bytes Wrong flags
mulu.w #1,dN moveq #0,dM
move.w dN,dM
36(0/0) 0 bytes Wrong flags
Result in dM instead of dN
mulu.w #2,dN moveq #0,dM
move.w dN,dM
add.l dM,dM
28(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #3,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
add.l dM,dM
add.l dN,dM
18(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #4,dN moveq #0,dM
move.w dN,dM
lsl.l #2,dM
24(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #5,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #2,dM
add.l dN,dM
14(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #6,dN moveq #0,dM
move.w dN,dM
add.l dM,dM
move.l dM,dN
add.l dM,dM
add.l dN,dM
10(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #7,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #3,dM
sub.l dN,dM
14(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #8,dN moveq #0,dM
move.w dN,dM
lsl.l #3,dM
22(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #9,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #3,dM
add.l dN,dM
12(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #10,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #2,dM
add.l dN,dM
add.l dM,dM
6(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #11,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
add.l dN,dM
add.l dN,dM
lsl.l #2,dM
sub.l dN,dM
8(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #14,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #3,dM
sub.l dN,dM
add.l dM,dM
6(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #15,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #4,dM
sub.l dN,dM
14(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #16,dN moveq #0,dM
move.w dN,dM
lsl.l #4,dM
20(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #17,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #4,dM
add.l dN,dM
10(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #18,dN moveq #0,dM
move.w dN,dM
add.l dM,dM
move.l dM,dN
lsl.l #3,dM
add.l dN,dM
4(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #20,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #2,dM
add.l dN,dM
lsl.l #2,dM
2(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #24,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
add.l dM,dM
add.l dN,dM
lsl.l #3,dM
4(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #31,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #5,dM
sub.l dN,dM
14(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #30,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #5,dM
sub.l dN,dM
sub.l dN,dM
4(-4/0) -8 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #32,dN moveq #0,dM
move.w dN,dM
lsl.l #5,dM
18(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #33,dN moveq #0,dM
move.w dN,dM
move.l dM,dN
lsl.l #5,dM
add.l dN,dM
8(-3/0) -6 bytes Wrong flags
dM different
Result in dM instead of dN
mulu.w #64,dN moveq #0,dM
move.w dN,dM
lsl.l #6,dM
16(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #128,dN moveq #0,dM
move.w dN,dM
lsl.l #7,dM
14(-1/0) -2 bytes Wrong flags
Result in dM instead of dN
mulu.w #256,dN moveq #0,dM
move.w dN,dM
lsl.l #8,dM
12(-1/0) -2 bytes Wrong flags
Result in dM instead of dN

Some of the asl and lsl can also be optimized by the respective peephole optimizations.

If the high word of the result is irrelevant

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
muls.w #0,dN moveq #0,dN 38(1/0) 2 bytes Wrong flags
muls.w #1,dN (nothing) 46(2/0) 4 bytes Wrong flags
High word of dN different
muls.w #2,dN add.w dN,dN 42(1/0) 2 bytes Wrong flags
High word of dN different
muls.w #3,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
36(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #4,dN add.w dN,dN
add.w dN,dN
38(0/0) 0 bytes Wrong flags
High word of dN different
muls.w #5,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
34(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #6,dN add.w dN,dN
move.w dN,dM
add.w dN,dN
add.w dM,dN
32(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #7,dN move.w dN,dM
asl.w #3,dN
sub.w dM,dN
30(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #8,dN asl.w #3,dN 34(1/0) 2 bytes Wrong flags
High word of dN different
dM different
muls.w #9,dN move.w dN,dM
asl.w #3,dN
add.w dM,dN
30(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #10,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
30(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #11,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
28(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #12,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
28(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #13,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
28(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #14,dN move.w dN,dM
asl.w #3,dN
sub.w dM,dN
add.w dN,dN
26(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #15,dN move.w dN,dM
asl.w #4,dN
sub.w dM,dN
30(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #16,dN asl.w #4,dN 32(1/0) 2 bytes Wrong flags
High word of dN different
dM different
muls.w #17,dN move.w dN,dM
asl.w #4,dN
add.w dM,dN
28(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #18,dN add.w dN,dN
move.w dN,dM
asl.w #3,dN
add.w dM,dN
26(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #19,dN move.w dN,dM
asl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dM,dN
24(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #20,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
26(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #21,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
26(-5/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #22,dN add.w dN,dN
move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
24(-5/0) -10 bytes Wrong flags
High word of dN different
dM different
muls.w #23,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
sub.w dM,dN
26(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #24,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
24(-2/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #25,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
add.w dM,dN
24(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #26,dN move.w dN,dM
add.w dM,dM
add.w dM,dN
asl.w #3,dN
add.w dM,dN
24(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #27,dN move.w dN,dM
asl.w #3,dN
sub.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
26(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #28,dN move.w dN,dM
asl.w #3,dN
sub.w dM,dN
add.w dN,dN
add.w dN,dN
26(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #29,dN move.w dN,dM
asl.w #5,dN
sub.w dM,dN
sub.w dM,dN
sub.w dM,dN
22(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #30,dN move.w dN,dM
asl.w #5,dN
sub.w dM,dN
sub.w dM,dN
24(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #31,dN move.w dN,dM
asl.w #5,dN
sub.w dM,dN
30(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #32,dN asl.w #5,dN 30(1/0) 2 bytes Wrong flags
High word of dN different
dM different
muls.w #33,dN move.w dN,dM
asl.w #5,dN
add.w dM,dN
26(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
muls.w #34,dN move.w dN,dM
asl.w #5,dN
add.w dM,dN
add.w dM,dN
22(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
muls.w #35,dN move.w dN,dM
asl.w #5,dN
add.w dM,dN
add.w dM,dN
add.w dM,dN
20(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #36,dN move.w dN,dM
asl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
22(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #37,dN move.w dN,dM
asl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
22(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #38,dN add.w dN,dN
move.w dN,dM
asl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dM,dN
20(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #39,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
asl.w #3,dN
sub.w dM,dN
22(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #40,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
asl.w #3,dN
22(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
muls.w #41,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
asl.w #3,dN
add.w dM,dN
22(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #42,dN move.w dN,dM
add.w dM,dM
add.w dM,dN
add.w dM,dN
asl.w #3,dN
add.w dM,dN
20(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
muls.w #64,dN asl.w #6,dN 28(1/0) 2 bytes Wrong flags
High word of dN different
dM different
muls.w #128,dN asl.w #7,dN 26(1/0) 2 bytes Wrong flags
High word of dN different
dM different
muls.w #256,dN asl.w #8,dN 24(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #0,dN moveq #0,dN 38(1/0) 2 bytes Wrong flags
mulu.w #1,dN (nothing) 44(2/0) 4 bytes Wrong flags
High word of dN different
mulu.w #2,dN add.w dN,dN 40(1/0) 2 bytes Wrong flags
High word of dN different
mulu.w #3,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
34(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #4,dN add.w dN,dN
add.w dN,dN
36(0/0) 0 bytes Wrong flags
High word of dN different
mulu.w #5,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
30(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #6,dN add.w dN,dN
move.w dN,dM
add.w dN,dN
add.w dM,dN
30(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #7,dN move.w dN,dM
lsl.w #3,dN
sub.w dM,dN
28(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #8,dN lsl.w #3,dN 32(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #9,dN move.w dN,dM
lsl.w #3,dN
add.w dM,dN
26(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #10,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
26(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #11,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
24(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #12,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
26(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #13,dN move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
24(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #14,dN move.w dN,dM
lsl.w #3,dN
sub.w dM,dN
add.w dN,dN
24(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #15,dN move.w dN,dM
lsl.w #4,dN
sub.w dM,dN
28(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #16,dN lsl.w #4,dN 30(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #17,dN move.w dN,dM
lsl.w #4,dN
add.w dM,dN
24(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #18,dN add.w dN,dN
move.w dN,dM
lsl.w #3,dN
add.w dM,dN
22(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #19,dN move.w dN,dM
lsl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dM,dN
20(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #20,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
22(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #21,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
20(-5/0) -10 bytes Wrong flags
High word of dN different
dM different
mulu.w #22,dN add.w dN,dN
move.w dN,dM
add.w dM,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
20(-5/0) -10 bytes Wrong flags
High word of dN different
dM different
mulu.w #23,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
sub.w dM,dN
22(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #24,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
22(-2/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #25,dN move.w dN,dM
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
add.w dM,dN
20(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #26,dN move.w dN,dM
add.w dM,dM
add.w dM,dN
lsl.w #3,dN
add.w dM,dN
20(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #27,dN move.w dN,dM
lsl.w #3,dN
sub.w dM,dN
add.w dN,dN
add.w dN,dN
sub.w dM,dN
22(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #28,dN move.w dN,dM
lsl.w #3,dN
sub.w dM,dN
add.w dN,dN
add.w dN,dN
24(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #29,dN move.w dN,dM
lsl.w #5,dN
sub.w dM,dN
sub.w dM,dN
sub.w dM,dN
18(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #30,dN move.w dN,dM
lsl.w #5,dN
sub.w dM,dN
sub.w dM,dN
22(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #31,dN move.w dN,dM
lsl.w #5,dN
sub.w dM,dN
28(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #32,dN lsl.w #5,dN 28(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #33,dN move.w dN,dM
lsl.w #5,dN
add.w dM,dN
22(-1/0) -2 bytes Wrong flags
High word of dN different
dM different
mulu.w #34,dN move.w dN,dM
lsl.w #5,dN
add.w dM,dN
add.w dM,dN
18(-2/0) -4 bytes Wrong flags
High word of dN different
dM different
mulu.w #35,dN move.w dN,dM
lsl.w #5,dN
add.w dM,dN
add.w dM,dN
add.w dM,dN
16(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #36,dN move.w dN,dM
lsl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
18(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #37,dN move.w dN,dM
lsl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dN,dN
add.w dM,dN
16(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #38,dN add.w dN,dN
move.w dN,dM
lsl.w #3,dN
add.w dM,dN
add.w dN,dN
add.w dM,dN
16(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #39,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
sub.w dM,dN
18(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #40,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
18(-3/0) -6 bytes Wrong flags
High word of dN different
dM different
mulu.w #41,dN move.w dN,dM
add.w dN,dN
add.w dN,dN
add.w dM,dN
lsl.w #3,dN
add.w dM,dN
16(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #42,dN move.w dN,dM
add.w dM,dM
add.w dM,dN
add.w dM,dN
lsl.w #3,dN
add.w dM,dN
16(-4/0) -8 bytes Wrong flags
High word of dN different
dM different
mulu.w #64,dN lsl.w #6,dN 26(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #128,dN lsl.w #7,dN 24(1/0) 2 bytes Wrong flags
High word of dN different
dM different
mulu.w #256,dN lsl.w #8,dN 22(1/0) 2 bytes Wrong flags
High word of dN different
dM different

Some of the asl and lsl can also be optimized by the respective peephole optimizations.

Division by constants

If the remainder is not needed

Don't use‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Use instead‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑ Time savings Space savings Notes‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
divu.w #1,dN (nothing) [76,136](1/0) 2 bytes Wrong flags
divu.w #1<<x,dN lsr.l #x,dN [66,126]-2*x(1/0) 2 bytes 1 ≤ x ≤ 8
Wrong flags
divu.w #1<<9,dN moveq #9,dM
lsr.l dM,dN
[46,106](0/0) 0 bytes Wrong flags
divu.w #1<<10,dN moveq #10,dM
lsr.l dM,dN
[44,104](0/0) 0 bytes Wrong flags
divu.w #1<<(8+x),dN andi.w #~((1<<(8+x))-1),dN
swap dN
rol.l #8-x,dN
[40,90]+2*x(0/0) 0 bytes 3 ≤ x ≤ 7
Wrong flags
divu.w #1<<16,dN clr.w dN
swap dN
[68,128](0/0) 0 bytes Wrong flags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment