Skip to content

Commit 6fe8b9c

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, x64: save several bytes by using mov over movabsq when possible
While analyzing some of the more complex BPF programs from Cilium, I found that LLVM generally prefers to emit LD_IMM64 instead of MOV32 BPF instructions for loading unsigned 32-bit immediates into a register. Given we cannot change the current/stable LLVM versions that are already out there, lets optimize this case such that the JIT prefers to emit 'mov %eax, imm32' over 'movabsq %rax, imm64' whenever suitable in order to reduce the image size by 4-5 bytes per such load in the typical case, reducing image size on some of the bigger programs by up to 4%. emit_mov_imm32() and emit_mov_imm64() have been added as helpers. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 88e69a1 commit 6fe8b9c

1 file changed

Lines changed: 74 additions & 51 deletions

File tree

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ static bool is_imm8(int value)
6060

6161
static bool is_simm32(s64 value)
6262
{
63-
return value == (s64) (s32) value;
63+
return value == (s64)(s32)value;
64+
}
65+
66+
static bool is_uimm32(u64 value)
67+
{
68+
return value == (u64)(u32)value;
6469
}
6570

6671
/* mov dst, src */
@@ -355,6 +360,68 @@ static void emit_load_skb_data_hlen(u8 **pprog)
355360
*pprog = prog;
356361
}
357362

363+
static void emit_mov_imm32(u8 **pprog, bool sign_propagate,
364+
u32 dst_reg, const u32 imm32)
365+
{
366+
u8 *prog = *pprog;
367+
u8 b1, b2, b3;
368+
int cnt = 0;
369+
370+
/* optimization: if imm32 is positive, use 'mov %eax, imm32'
371+
* (which zero-extends imm32) to save 2 bytes.
372+
*/
373+
if (sign_propagate && (s32)imm32 < 0) {
374+
/* 'mov %rax, imm32' sign extends imm32 */
375+
b1 = add_1mod(0x48, dst_reg);
376+
b2 = 0xC7;
377+
b3 = 0xC0;
378+
EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
379+
goto done;
380+
}
381+
382+
/* optimization: if imm32 is zero, use 'xor %eax, %eax'
383+
* to save 3 bytes.
384+
*/
385+
if (imm32 == 0) {
386+
if (is_ereg(dst_reg))
387+
EMIT1(add_2mod(0x40, dst_reg, dst_reg));
388+
b2 = 0x31; /* xor */
389+
b3 = 0xC0;
390+
EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
391+
goto done;
392+
}
393+
394+
/* mov %eax, imm32 */
395+
if (is_ereg(dst_reg))
396+
EMIT1(add_1mod(0x40, dst_reg));
397+
EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
398+
done:
399+
*pprog = prog;
400+
}
401+
402+
static void emit_mov_imm64(u8 **pprog, u32 dst_reg,
403+
const u32 imm32_hi, const u32 imm32_lo)
404+
{
405+
u8 *prog = *pprog;
406+
int cnt = 0;
407+
408+
if (is_uimm32(((u64)imm32_hi << 32) | (u32)imm32_lo)) {
409+
/* For emitting plain u32, where sign bit must not be
410+
* propagated LLVM tends to load imm64 over mov32
411+
* directly, so save couple of bytes by just doing
412+
* 'mov %eax, imm32' instead.
413+
*/
414+
emit_mov_imm32(&prog, false, dst_reg, imm32_lo);
415+
} else {
416+
/* movabsq %rax, imm64 */
417+
EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
418+
EMIT(imm32_lo, 4);
419+
EMIT(imm32_hi, 4);
420+
}
421+
422+
*pprog = prog;
423+
}
424+
358425
static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
359426
int oldproglen, struct jit_context *ctx)
360427
{
@@ -377,7 +444,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
377444
const s32 imm32 = insn->imm;
378445
u32 dst_reg = insn->dst_reg;
379446
u32 src_reg = insn->src_reg;
380-
u8 b1 = 0, b2 = 0, b3 = 0;
447+
u8 b2 = 0, b3 = 0;
381448
s64 jmp_offset;
382449
u8 jmp_cond;
383450
bool reload_skb_data;
@@ -485,58 +552,13 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
485552
break;
486553

487554
case BPF_ALU64 | BPF_MOV | BPF_K:
488-
/* optimization: if imm32 is positive,
489-
* use 'mov eax, imm32' (which zero-extends imm32)
490-
* to save 2 bytes
491-
*/
492-
if (imm32 < 0) {
493-
/* 'mov rax, imm32' sign extends imm32 */
494-
b1 = add_1mod(0x48, dst_reg);
495-
b2 = 0xC7;
496-
b3 = 0xC0;
497-
EMIT3_off32(b1, b2, add_1reg(b3, dst_reg), imm32);
498-
break;
499-
}
500-
501555
case BPF_ALU | BPF_MOV | BPF_K:
502-
/* optimization: if imm32 is zero, use 'xor <dst>,<dst>'
503-
* to save 3 bytes.
504-
*/
505-
if (imm32 == 0) {
506-
if (is_ereg(dst_reg))
507-
EMIT1(add_2mod(0x40, dst_reg, dst_reg));
508-
b2 = 0x31; /* xor */
509-
b3 = 0xC0;
510-
EMIT2(b2, add_2reg(b3, dst_reg, dst_reg));
511-
break;
512-
}
513-
514-
/* mov %eax, imm32 */
515-
if (is_ereg(dst_reg))
516-
EMIT1(add_1mod(0x40, dst_reg));
517-
EMIT1_off32(add_1reg(0xB8, dst_reg), imm32);
556+
emit_mov_imm32(&prog, BPF_CLASS(insn->code) == BPF_ALU64,
557+
dst_reg, imm32);
518558
break;
519559

520560
case BPF_LD | BPF_IMM | BPF_DW:
521-
/* optimization: if imm64 is zero, use 'xor <dst>,<dst>'
522-
* to save 7 bytes.
523-
*/
524-
if (insn[0].imm == 0 && insn[1].imm == 0) {
525-
b1 = add_2mod(0x48, dst_reg, dst_reg);
526-
b2 = 0x31; /* xor */
527-
b3 = 0xC0;
528-
EMIT3(b1, b2, add_2reg(b3, dst_reg, dst_reg));
529-
530-
insn++;
531-
i++;
532-
break;
533-
}
534-
535-
/* movabsq %rax, imm64 */
536-
EMIT2(add_1mod(0x48, dst_reg), add_1reg(0xB8, dst_reg));
537-
EMIT(insn[0].imm, 4);
538-
EMIT(insn[1].imm, 4);
539-
561+
emit_mov_imm64(&prog, dst_reg, insn[1].imm, insn[0].imm);
540562
insn++;
541563
i++;
542564
break;
@@ -604,7 +626,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,
604626
EMIT_mov(BPF_REG_0, src_reg);
605627
else
606628
/* mov rax, imm32 */
607-
EMIT3_off32(0x48, 0xC7, 0xC0, imm32);
629+
emit_mov_imm32(&prog, true,
630+
BPF_REG_0, imm32);
608631

609632
if (BPF_CLASS(insn->code) == BPF_ALU64)
610633
EMIT1(add_1mod(0x48, AUX_REG));

0 commit comments

Comments
 (0)