armlint examines AArch64 machine code to find suboptimal instruction
sequences. For example, building the constant 0x66666666 as
movz w0, #0x6666
movk w0, #0x6666, lsl #16
is two instructions where one would do, because 0x66666666 is encodable
as an AArch64 logical (bitmask) immediate:
mov w0, #0x66666666 ; orr w0, wzr, #0x66666666
armlint helps compiler writers and assembly authors generate tighter code, and documents corners of the A64 instruction set.
Design and limitations
armlint is a peephole analyzer. It decodes each 32-bit A64 instruction
directly from the binary and matches it by mask and value, resolving
aliases (for example MUL is MADD with a zero accumulator) so that
both spellings of a pattern are caught. It then looks for a short window
of adjacent instructions that a shorter or cheaper encoding can replace.
The overriding rule is soundness: armlint emits a finding only when the
rewrite provably preserves the architectural result. For a tool that
suggests code changes, a false positive is the worst failure, so it errs
toward false negatives -- a missed opportunity is cheaper than a wrong
one. Each check documents the exact conditions under which its rewrite is
equivalent; the constraints below are the ones they share, and
analyses.md's appendix
collects the near-miss folds that are deliberately never matched (FP
contraction, fcsel -> fmax, the SDIV remainder, and friends) with
the argument against each.
- Strict adjacency for matching; bounded lookahead for proof. The instructions of a matched pattern must be consecutive -- an unrelated instruction between a producer and its consumer suppresses the finding -- and armlint does not reorder code or match through intervening instructions. Emission, though, is not confined to the pattern window: as the following bullets describe, many findings are held back while a bounded forward scan (16 instructions) walks the fall-through path past the consumer to prove a register or the flags dead, so an instruction well after the pair can also suppress a finding. The scan only gates emission -- it never widens a match, so every reported rewrite still replaces only the adjacent instructions shown.
- Liveness is proved structurally, or by a bounded forward scan. A
producer-into-consumer fold fires when the consumer overwrites the
producer's destination register, proving the intermediate value is
dead. Folds whose saving is a deleted write with no such overwrite
defer instead: a bounded forward scan of the fall-through path must
see the register overwritten before any read or control transfer.
This is how the address folds admit stores and loads into a fresh
register --
add x8, sp, #32 ; str x0, [x8]folds tostr x0, [sp, #0x20]only oncex8provably dies -- and how the producer folds (shift, funnel, extend,MUL/SMULL,NEG,MVN) admit consumers that write a register other than the producer's:lsl w8, w1, #3 ; add w9, w2, w8folds toadd w9, w2, w1, lsl #3under the same proof. The single-bit and CSET branch folds additionally require the folded branch's taken edge to land inside that proven-clean span -- a general-purpose register, unlike NZCV, is routinely live into a branch target, so no block-locality assumption is made for it. - MOV-chain folds verify the constant dies. Folds that absorb a
materialized constant --
MUL/MNEG/UDIVby a constant,MOV+ADD/AND/ORR/EOR/CCMP/FMOV,MOV #0, the register-offset and MOVI-zeroing folds -- report only once the consumer's own overwrite or the forward scan proves the constant register dead. The consumer rewrite itself stays valid regardless. - Flag liveness uses a bounded forward scan. The branch- and
flag-folding checks drop a
CMP/TSTonly after a bounded scan of the fall-through path confirms that no later instruction reads N/C/V before they are overwritten. Every NZCV reader is recognized -- the integer conditionals (B.cond,CSEL/CSINC/...,CCMP/CCMN,ADC/SBC) and the floating-point ones (FCSEL,FCCMP/FCCMPE) -- and any branch off the path whose destination the scan cannot see -- an unconditionalB, or a conditionalCBZ/CBNZ/TBZ/TBNZ(which do not themselves touch NZCV but whose taken target may still observe it) -- ends it conservatively. The scan does not follow the folded branch's own taken edge, so most of these folds assume N/C/V is dead at every branch target. That holds for most compiled code, where the flags are defined within a basic block, but not for hand-written assembly that deliberately keeps a flag live into a branch target -- including the stubs a JIT inlines into its output. The folds that meet such code prove the taken edge with a second scan starting at the target: the compare-and-branch fold (-m cmpbr), whose producer is a general two-register compare that clang's three-way comparator really does read again at the branch target; theCCMPchain andCMP-to-CMNfolds; and the zero-compareCBZ/CBNZand sign-bitTBZ/TBNZfolds, since HotSpot's lock fast paths return their result to the branch target in the flags. Unlike the fall-through scan, the one at the target follows aBto its destination and aCBZ/CBNZ/TBZ/TBNZdown both edges, within fixed bounds.
Findings are opportunities, not guaranteed speedups: some -- the pre- and post-indexed addressing folds -- are code-size and front-end wins that are backend-neutral. Each check's notes say what its rewrite actually saves.
Implemented analyses
Each row links to its full description -- mechanics, soundness, and what the rewrite saves -- in analyses.md. Candidate checks not yet implemented live in TODO.md.
| Pattern | Rewrite |
|---|---|
movz/movn + movk (over-long constant) |
bitmask-immediate mov, or minimal movz/movn + movk chain |
lsl/lsr/asr/ror + add/sub/and/orr/eor |
add Rd, Rn, Rm, <shift> #n |
lsl/lsr + shifted orr/eor/add (funnel/rotate) |
extr Rd, Rhi, Rlo, #lsb (or ror when both halves match) |
sxtw/uxtb/sxtb + add/sub |
add Rd, Rn, Wm, sxtw |
cmp/cmn/tst zero-test + b.eq/b.ne (b.hi/b.ls after cmp) |
cbz/cbnz |
cmp/cmn/tst zero-test + b.lt/b.ge/b.mi/b.pl |
tbnz/tbz Rn, #(msb) |
tst #(1<<k) + b.eq/b.ne |
tbz/tbnz Rn, #k |
tst #(1<<k) + cset/csetm |
ubfx/sbfx Rd, Rn, #k, #1 |
single-bit and/ubfx/lsr #31 + cbz/cbnz |
tbz/tbnz Rs, #k |
neg + cbz/cbnz of the result |
cbz/cbnz of the source |
cset + cbz/cbnz |
b.<cond> / b.<inverse cond> |
cset + eor #1 |
cset <inverse cond> |
cset + neg |
csetm |
br x30 |
ret (engages the return-address predictor) |
| branch to the next instruction | delete (both outcomes fall through; bl excluded) |
lsl + lsr/asr |
ubfx/sbfx/ubfiz/sbfiz |
lsr/asr + and #mask |
ubfx |
and #mask + lsr |
ubfx |
and #mask/uxtb/uxth/uxtw/mov + lsl (or lsr + lsl) |
ubfiz (or clearing and) |
zeroing producer + uxtb/uxth/uxtw/and/mov Wd, Wd, adjacent or not |
drop the zero-extension |
mov xd, xd |
remove (architectural no-op) |
| instruction recomputing a value its register or NZCV already holds, across a gap | delete (an ADR/ADRP of a held page is reported apart: a relink fix) |
| register write overwritten before any read, across a gap | delete the write |
| conditional branch the path to it already decides | delete it (never taken) or b (always taken) |
csel/csinc/csinv/csneg whose condition the path decides |
mov of the operand it picks (add #1/mvn/neg of it, or the constant) |
| ALU operation whose inputs are all known values, or whose result the known bits pin | mov Rd, #result, or delete when Rd already holds it |
and #mask/ubfx #0 of a value its known bits already keep inside the mask |
delete in place, mov Rd, Rn out of place |
sign-extending producer + sxtb/sxth/sxtw |
drop the sign-extension |
and/uxt*/sxt*/mov Wd, Wm + a narrower and/ands/tst/uxt*/sxt* |
the second reading the first's source, or one and of the intersected masks |
and/orr/eor/sub/bic/orn/eon with Rs, Rs |
mov / zero / all-ones |
ldr+ldr / str+str (consecutive) |
ldp/stp (integer or FP/SIMD, and ldpsw); notes where a slot is in flight, since Apple cores do not forward a store to a pair load or a pair store to a load |
str wzr+str wzr (consecutive zero stores) |
str/stur xzr |
stp wzr, wzr (W-form) |
str/stur xzr |
movi #0 + vector cmeq/cmge/cmgt (or FP fcm*) |
cmeq/cmge/cmgt/cmle/cmlt Vd, X, #0 (drop the movi) |
and + and/ubfiz + orr (clear/isolate/merge) |
bfxil/bfi |
and #lowmask + orr Rm, lsl #k (or high mask + lsr; field reaches the top) |
bfi/bfxil |
csel Rd, Rn, Rn, cond |
mov Rd, Rn |
fcsel Vd, Vn, Vn, cond |
fmov Vd, Vn |
add/sub Rd, Rn, #0 |
mov Rd, Rn, or remove |
add/sub #a + add/sub #b (same register) |
one add/sub carrying the sum (or mov) |
adds/subs/ands + cmp #0 + b.eq/b.ne |
drop the redundant cmp/tst |
add/sub/and/bic + cmp #0 + b.eq/b.ne |
adds/subs/ands/bics (drop the cmp/tst) |
sub + cmp / add + cmn of the same operands (either order) |
subs/adds (flag-exact; drop the compare) |
subs/adds + cmp/cmn of its own operands |
drop the compare (flags already set) |
cmp/cmn/tst/ccmp whose flags are overwritten unread |
delete (the compare writes no register) |
cmp #0 + cset/csetm of lt/mi |
lsr/asr Rd, Rn, #(msb) (the sign bit; drop the compare) |
mov #2^N + mul |
lsl, or add Rd, Ra, Ra, lsl #N |
mov #C + mneg |
neg, or shifted neg/sub |
mov #2^N + madd/msub |
add/sub Rd, Ra, Rn, lsl #N |
mov #2^N + udiv |
lsr |
mov #2^N + udiv + msub (remainder) |
and Rd, Rn, #(2^N-1) |
mov #C + add/sub |
add/sub Rd, Rn, #C (sign-crossed add↔sub, cmp↔cmn for #-C) |
movz/movn + movk of a constant below 2^24 + add/sub |
add/sub Rd, Rn, #hi, lsl #12 + add/sub Rd, Rd, #lo (add↔sub for a negative constant) |
mov #C + and/orr/eor/ands or bic/orn/eon/bics |
and/orr/eor/ands Rd, Rn, #C (#~C for the inverting forms) |
mov #INT_MIN or #INT_MAX + cmp + b.eq/b.ne |
cmp xzr, Xn or cmn Xn, #1 + b.vs/b.vc |
mov #C + ccmp/ccmn |
ccmp/ccmn Rn, #C, #nzcv, cond (sign-crossed for #-C) |
cmp/cmn + ccmp/ccmn on one register + b.cond/csel, one compare deciding it |
that cmp/cmn (or cbz/cbnz) with the reader's condition adjusted |
mov #(2^32-k) + cmp Xn of a zero-extended value, read only through Z and C |
cmn Wn, #k |
mov #1 + csel |
csinc Rd, Rn, wzr, cc (cset when the other operand is ZR) |
mov #-1 + csel |
csinv Rd, Rn, wzr, cc (csetm when the other operand is ZR) |
mov #C + lsl/lsr/asr/ror (register amount) |
immediate-form shift, amount C mod 32/64 |
mov #bits/#C + fmov/scvtf/ucvtf from GPR |
fmov Sd/Dd, #imm |
fmov/scvtf/dup of wzr/xzr (or mov #0 + transfer) |
movi dN, #0 / movi vN.T, #0 |
sxtw/mov w, w + scvtf/ucvtf Xn |
scvtf/ucvtf of Wn |
ldr w8 + scvtf/ucvtf from GPR |
ldr s0 + FP-side convert (no cross-file transfer) |
ldr/ldrsw (literal) of an encodable constant |
mov #imm / fmov #imm8 / movi+mvni for Q (no memory access) |
adr + ldr [x8]/br x16 |
ldr Rt, <literal> / b L |
umov Wd, Vn.s[0] / Xd, Vn.d[0] |
fmov Wd, Sn / Xd, Dn (cheaper port; .h[0] under -m fp16) |
and Xd, Xn, #0xffffffff / ubfx Xd, Xn, #0, #32 |
mov Wd, Wn (a rename on Neoverse rather than an ALU op) |
cmp + csel (max/min shape) |
smax/smin/umax/umin (-m cssc) |
cmp #0 + cneg |
abs (-m cssc) |
rbit + clz |
ctz (-m cssc) |
| NEON popcount round trip | cnt Xd, Xn (-m cssc) |
[cmp + b.<cond>](https://github.com/gaul/armlint/bl |