A 32-bit immediate with a zero or an all ones half took a movz or movn and a movk even where the shifted movz or movn alone produces it, and a bitmask immediate, a run of ones or a repeating pattern such as all but the sign bit, took two moves where an orr with the zero register does it in one, at the same single cycle of latency. A 64-bit immediate with a 16-bit chunk repeating, whose replication is a bitmask immediate, takes that orr and a movk per other chunk, which is the form of the magic numbers of the divisions by 3, 5, 15 and 17, among others, that a later patch emits. Try these forms before falling back to the move pairs. Before/after for w1 = 0x10000, r1 = 0x7fffffff, r1 = 0xffffffff00000000 ll and r1 = 0xcccccccccccccccd ll: 52800000 mov w0, #0x0 72a00020 movk w0, #0x1, lsl #16 52a00020 mov w0, #0x10000 d29fffe0 mov x0, #0xffff f2afffe0 movk x0, #0x7fff, lsl #16 b2407be0 mov x0, #0x7fffffff d2ffffe0 mov x0, #0xffff000000000000 f2dfffe0 movk x0, #0xffff, lsl #32 b2607fe0 mov x0, #0xffffffff00000000 d2f99980 mov x0, #0xcccc000000000000 f2d99980 movk x0, #0xcccc, lsl #32 f2b99980 movk x0, #0xcccc, lsl #16 f29999a0 movk x0, #0xcccd b202e7e0 mov x0, #0xcccccccccccccccc f29999a0 movk x0, #0xcccd Signed-off-by: Daniel Borkmann --- arch/arm64/net/bpf_jit_comp.c | 77 ++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 606e788facd0..a327c90fe2c7 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -123,24 +123,49 @@ static inline void emit_u32_data(const u32 data, struct jit_ctx *ctx) ctx->idx++; } +/* + * A 32-bit immediate, sign extended for the 64-bit variant: a single movz + * or movn when one half is zero or all ones and the other half agrees with + * the sign the 64-bit variant extends, a single orr when it is a bitmask + * immediate, and the pair of a movz or movn with a movk otherwise. + */ static inline void emit_a64_mov_i(const int is64, const int reg, const s32 val, struct jit_ctx *ctx) { u16 hi = val >> 16; u16 lo = val & 0xffff; + u32 insn; + + if (hi == 0xffff) { + emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx); + return; + } + if (!hi) { + emit(A64_MOVZ(is64, reg, lo, 0), ctx); + return; + } + /* The 64-bit variant has the upper half set or clear with the sign */ + if (!lo && (!is64 || !(hi & 0x8000))) { + emit(A64_MOVZ(is64, reg, hi, 16), ctx); + return; + } + if (lo == 0xffff && (!is64 || (hi & 0x8000))) { + emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); + return; + } + + insn = A64_ORR_I(is64, reg, A64_ZR, val); + if (insn != AARCH64_BREAK_FAULT) { + emit(insn, ctx); + return; + } if (hi & 0x8000) { - if (hi == 0xffff) { - emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx); - } else { - emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); - if (lo != 0xffff) - emit(A64_MOVK(is64, reg, lo, 0), ctx); - } + emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx); + emit(A64_MOVK(is64, reg, lo, 0), ctx); } else { emit(A64_MOVZ(is64, reg, lo, 0), ctx); - if (hi) - emit(A64_MOVK(is64, reg, hi, 16), ctx); + emit(A64_MOVK(is64, reg, hi, 16), ctx); } } @@ -168,10 +193,42 @@ static inline void emit_a64_mov_i64(const int reg, const u64 val, { u64 nrm_tmp = val, rev_tmp = ~val; bool inverse; - int shift; + u32 insn; + int shift, blocks; if (!(nrm_tmp >> 32)) return emit_a64_mov_i(0, reg, (u32)val, ctx); + if ((s64)(s32)val == (s64)val) + return emit_a64_mov_i(1, reg, (s32)val, ctx); + + /* A bitmask immediate in one orr rather than two or more moves */ + insn = A64_ORR_I(1, reg, A64_ZR, val); + if (insn != AARCH64_BREAK_FAULT) + return emit(insn, ctx); + + /* + * A 16-bit chunk occurring more than once whose replication is a + * bitmask immediate comes from one orr, with a movk for each of + * the other chunks, when that beats the moves of the chunks. + */ + blocks = min(i64_i16_blocks(nrm_tmp, true), i64_i16_blocks(nrm_tmp, false)); + for (shift = 0; shift < 64; shift += 16) { + u16 chunk = val >> shift; + int i, n = 0; + + for (i = 0; i < 64; i += 16) + n += (u16)(val >> i) == chunk; + if (n < 2 || 1 + 4 - n >= blocks) + continue; + insn = A64_ORR_I(1, reg, A64_ZR, chunk * 0x0001000100010001ULL); + if (insn == AARCH64_BREAK_FAULT) + continue; + emit(insn, ctx); + for (i = 0; i < 64; i += 16) + if ((u16)(val >> i) != chunk) + emit(A64_MOVK(1, reg, (u16)(val >> i), i), ctx); + return; + } inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false); shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) : -- 2.43.0