Speed: what makes amiga-gcc 6.5 emit faster code
A field report from the benchmark bench
The 6.5 toolchain is the long standing default for amiga-gcc, but on a set of 33 real world benchmarks it trailed gcc 16.2 by 2 to 10 percent on every CPU. Chasing that gap turned up five separate causes, four of them in the compiler itself and one in the cost model. This article documents what was measured, what was changed, and what did not work.
All numbers below are emulated cycles on volamos at 25 MHz, built with -O2 -fomit-frame-pointer, geometric mean over the 33 benchmarks of the Benchwork suite. A lower number is faster.
The result
CPU 6.5 before 6.5 now gcc 16.2 6.5 vs 16.2
68000 5982.6 5872.5 6638.8 -11.5%
68030 2070.6 2030.5 2077.9 -2.3%
68040 507.8 499.8 518.9 -3.7%
68060 411.9 403.4 415.5 -2.9%
6.5 is ahead of 16.2 on all four CPUs now. The individual wins on the 68000:
scene-2d 33103.6 -> 24317.7 -26.5%
poly1305 4163.4 -> 1578.5 -62.1%
x25519 30983.4 -> 11897.6 -61.6%
chacha20poly1305 10244.6 -> 5068.8 -50.5%
lha-pack 8457.9 -> 7544.3 -10.8%
memmove-large 10064.5 -> 8221.3 -18.3%
zlib-deflate 6110.6 -> 5187.0 -15.1%
sha512 12090.8 -> 11682.9 -3.4%
1. Jump threading: give it room
The old jump threader refuses to duplicate more than fifteen statements to thread a path. The render loop of scene-2d, which tests a loop invariant condition inside the pixel loop, needs more than that, so 6.5 threaded nothing and re-tested the condition for every pixel.
--param max-jump-thread-duplication-stmts=100
The limit is set in m68k_option_override, so it is a property of the target and a --param on the command line still wins. Effect on the render kernel alone: 801709368 -> 624877074 cycles, 22 percent. Overall scene-2d: 16.7 percent, nothing else measurable.
2. Early inlining: 160 statements
The early inliner only inlines a function when its body is smaller than early-inlining-insns, eleven statements by default. That is far too small for the helper a benchmark loop calls once, and inlining such a helper is nearly free because its out of line copy disappears.
A binary search over the parameter found a very sharp threshold:
150 703644 bytes scene-2d 32962
155 706384 bytes scene-2d 32962
160 708624 bytes scene-2d 24318 <- the jump
165 710440 bytes scene-2d 24318
200 943512 bytes scene-2d 24318
400 1188068 bytes scene-2d 24318
At 160 the render function gets inlined and scene-2d drops by 26 percent; everything above 175 only costs size. The full suite at 160:
GEOMEAN 5982.6 -> 5882.5 -1.67% with +8.5% code size
For comparison, enabling -finline-functions at -O2 (which is what gcc 12 and later do) gives -0.62% for +14.7% size. The early inlining limit is the better lever, and it is set in m68k_option_override next to the threading limit.
3. doloop: keep the counter
On 6.5 the loop optimizer replaces a counted loop by pointer comparisons, which the backend can no longer turn into dbra. gcc 16 adds a dedicated doloop induction variable candidate for exactly this; the port to 6.5 touches tree-ssa-loop-ivopts.c:
- analyze_and_mark_doloop_use predicts whether the RTL doloop pass will convert the loop, and marks the comparison use
- add_iv_candidate_for_doloop adds the candidate {(niter + 1), +, -1}
- determine_iv_cost treats a doloop candidate's step as free, the pair (doloop candidate, doloop comparison) is costed at zero minus one so it wins the tie against the pointer form
The effect is visible in newlib's memmove, which now copies a longword per iteration with two instructions instead of three:
6.5 before: move.l -(a0),-(a3) 6.5 now: move.l -(a2),-(a3)
cmp.l a0,d3 dbra d1,.L3
jne .L3
memmove-small on the 68000: 6569.0 -> 5459.2 ms, 17 percent. dbra is also fine on the 68060, where a copy loop measures within three cycles of the branch version over 86 million.
4. Multiplication by a negative constant
On a 68000 there is no 32 bit multiply instruction, so a product with a constant is turned into shifts and adds. For a negative constant that never happened: the coefficient went to synth_mult as an unsigned value, which for -4 is 0xFFFFFFFC, no shift and add sequence fits, and the compiler emitted a call to the mulsi3 helper. The cost function even reported the cheap shift, which made the limit smaller still.
The fix is in expmed.c: negate the coefficient, synthesize the positive multiplier, negate the result, and only do so when it is actually cheaper than what the target can do.
x * -2 neg.l d0 (was jsr ___mulsi3)
x * -4 lsl.l #2; neg.l d0 (was jsr ___mulsi3)
x * -8 lsl.l #3; neg.l d0 (was jsr ___mulsi3)
x * 4 lsl.l #2 d0 (unchanged)
On the 68020, 68030, 68040 and 68060 the same comparison keeps the hardware multiply, because there it is one instruction and cheaper.
5. 32x32 to 64 bits without MULU.L
The 68020, 68030 and 68040 have a 64 bit result form of MULU.L and MULS.L. The 68060 does not, and a 68000 has no long multiply at all. Both therefore ended up calling the muldi3 helper for every 32x32 product, four times per Poly1305 block.
The fix is a define_insn that computes the product from four MULU.W partial products with ADDX for the carry, plus an expansion routine that corrects the high word for the two sign terms when the multiply is signed:
hi -= ((a >> 31) & b);
hi -= ((b >> 31) & a);
The pattern applies to the 68060 always, and to the 68000 only when optimizing for speed, because the sequence is much bigger than a call and size still wins at -Os.
poly1305 4188.8 -> 1578.5 ms -62.3%
chacha20poly1305 10299.8 -> 5068.8 ms -50.8%
x25519 31782.9 -> 11897.6 ms -62.6%
Verified against an x86 reference over eleven cases with mixed signs, INT_MAX squared, minus one times minus one and friends, on 68000, 68040 and 68060.
What did not work
- -finline-functions as a default: 0.6 percent for 15 percent more code, and the gain sits almost entirely in one benchmark.
- Function CSE: caching the address of a library helper in a register looks expensive on paper, disabling it changed the runtime by 0.1 percent.
- Loop unswitching: the conditions in the pixel loop are loop invariant, but neither 6.5 nor 16.2 unswitches them.
- Cost model as a lever for the loop form: setting cost_const_int_q and cost_const_int_l_2 to values that make ivopts keep the counter does work, but applying it globally makes the rest of the suite slower than it gains.
- Raising the early inlining limit past 175: no further gain, only size.
Reproducing
Everything was measured with Benchwork, 33 benchmarks, built with the toolchain under test and run on volamos:
volamos --cpu 68000 --clock-mhz 25 --stack 65536 --ram 8192k --fpu -- ./benchwork REPEAT 1
For the 68040 and 68060 add --fpsp-emulate, and build with -m68881. Keep the clock and the cache options identical between the two toolchains, or the comparison is meaningless: an earlier run of the same suite at 25 MHz and at 50 MHz differs by a factor of two, obviously, but a run with and without --cache does not look obviously wrong and still is.
The cost model search from the companion article continues in parallel; the two levers add up, since one changes what the compiler is told and the other what it does with it.
rev: No