Skip to content

Commit a0f03d7

Browse files
borkmannintel-lab-lkp
authored andcommitted
bpf, x86: Small optimization in comparing against imm0
Replace 'cmp reg, 0' with 'test reg, reg' for comparisons against zero. Saves 1 byte of instruction encoding per occurrence. The flag results of test 'reg, reg' are identical to 'cmp reg, 0' in all cases except for AF which we don't use/care about. In terms of macro-fusibility in combination with a subsequent conditional jump instruction, both have the same properties for the jumps used in the JIT translation. For example, same JITed Cilium program can shrink a bit from e.g. 12,455 to 12,317 bytes as tests with 0 are used quite frequently. Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 03bd477 commit a0f03d7

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

arch/x86/net/bpf_jit_comp.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,16 @@ xadd: if (is_imm8(insn->off))
909909
case BPF_JMP32 | BPF_JSLT | BPF_K:
910910
case BPF_JMP32 | BPF_JSGE | BPF_K:
911911
case BPF_JMP32 | BPF_JSLE | BPF_K:
912+
/* test dst_reg, dst_reg to save one extra byte */
913+
if (imm32 == 0) {
914+
if (BPF_CLASS(insn->code) == BPF_JMP)
915+
EMIT1(add_2mod(0x48, dst_reg, dst_reg));
916+
else if (is_ereg(dst_reg))
917+
EMIT1(add_2mod(0x40, dst_reg, dst_reg));
918+
EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg));
919+
goto emit_cond_jmp;
920+
}
921+
912922
/* cmp dst_reg, imm8/32 */
913923
if (BPF_CLASS(insn->code) == BPF_JMP)
914924
EMIT1(add_1mod(0x48, dst_reg));

0 commit comments

Comments
 (0)