A 32-bit immediate with a zero or all-ones low half often took two moves where a single shifted movz or movn does. A bitmask immediate, 32 or 64 bits, took two or more moves where a single orr does. A 64-bit immediate with a repeated 16-bit chunk now takes one orr of that chunk replicated, if that is a bitmask immediate, plus a movk for each other chunk. The verifier's magic numbers for dividing by 3, 5, 15 and 17 have that form. 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 | 75 ++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 10 deletions(-) diff --git a/arch/arm64/net/bpf_jit_comp.c b/arch/arm64/net/bpf_jit_comp.c index 5a88740d9d0c..62d2f70cc412 100644 --- a/arch/arm64/net/bpf_jit_comp.c +++ b/arch/arm64/net/bpf_jit_comp.c @@ -125,24 +125,47 @@ 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: one movz, movn or + * orr where one encodes it, else a movz or movn and a movk. + */ 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); } } @@ -170,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 a single orr */ + 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