bpf, x64: Save bytes for DIV by reducing reg copies
authorJie Meng <jmeng@fb.com>
Sat, 2 Oct 2021 03:56:26 +0000 (20:56 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 6 Oct 2021 13:24:36 +0000 (15:24 +0200)
commit57a610f1c58fa493315e1c24eef6d992cdf4c4a9
treeb19776aea72a4c4a6bf2d48e8cebccd281375f6d
parent0640c77c46cb84328d5e08aa85a781a60be8b02b
bpf, x64: Save bytes for DIV by reducing reg copies

Instead of unconditionally performing push/pop on %rax/%rdx in case of
division/modulo, we can save a few bytes in case of destination register
being either BPF r0 (%rax) or r3 (%rdx) since the result is written in
there anyway.

Also, we do not need to copy the source to %r11 unless the source is either
%rax, %rdx or an immediate.

For example, before the patch:

  22:   push   %rax
  23:   push   %rdx
  24:   mov    %rsi,%r11
  27:   xor    %edx,%edx
  29:   div    %r11
  2c:   mov    %rax,%r11
  2f:   pop    %rdx
  30:   pop    %rax
  31:   mov    %r11,%rax

After:

  22:   push   %rdx
  23:   xor    %edx,%edx
  25:   div    %rsi
  28:   pop    %rdx

Signed-off-by: Jie Meng <jmeng@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211002035626.2041910-1-jmeng@fb.com
arch/x86/net/bpf_jit_comp.c
tools/testing/selftests/bpf/verifier/jit.c