Skip to content

Commit 6d2eea6

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf, arm64: optimize 32/64 immediate emission
Improve the JIT to emit 64 and 32 bit immediates, the current algorithm is not optimal and we often emit more instructions than actually needed. arm64 has movz, movn, movk variants but for the current 64 bit immediates we only use movz with a series of movk when needed. For example loading ffffffffffffabab emits the following 4 instructions in the JIT today: * movz: abab, shift: 0, result: 000000000000abab * movk: ffff, shift: 16, result: 00000000ffffabab * movk: ffff, shift: 32, result: 0000ffffffffabab * movk: ffff, shift: 48, result: ffffffffffffabab Whereas after the patch the same load only needs a single instruction: * movn: 5454, shift: 0, result: ffffffffffffabab Another example where two extra instructions can be saved: * movz: abab, shift: 0, result: 000000000000abab * movk: 1f2f, shift: 16, result: 000000001f2fabab * movk: ffff, shift: 32, result: 0000ffff1f2fabab * movk: ffff, shift: 48, result: ffffffff1f2fabab After the patch: * movn: e0d0, shift: 16, result: ffffffff1f2fffff * movk: abab, shift: 0, result: ffffffff1f2fabab Another example with movz, before: * movz: 0000, shift: 0, result: 0000000000000000 * movk: fea0, shift: 32, result: 0000fea000000000 After: * movz: fea0, shift: 32, result: 0000fea000000000 Moreover, reuse emit_a64_mov_i() for 32 bit immediates that are loaded via emit_a64_mov_i64() which is a similar optimization as done in 6fe8b9c ("bpf, x64: save several bytes by using mov over movabsq when possible"). On arm64, the latter allows to use a single instruction with movn due to zero extension where otherwise two would be needed. And last but not least add a missing optimization in emit_a64_mov_i() where movn is used but the subsequent movk not needed. With some of the Cilium programs in use, this shrinks the needed instructions by about three percent. Tested on Cavium ThunderX CN8890. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 09ece3d commit 6d2eea6

1 file changed

Lines changed: 54 additions & 31 deletions

File tree

‎arch/arm64/net/bpf_jit_comp.c‎

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,66 @@ static inline void emit(const u32 insn, struct jit_ctx *ctx)
7979
ctx->idx++;
8080
}
8181

82+
static inline void emit_a64_mov_i(const int is64, const int reg,
83+
const s32 val, struct jit_ctx *ctx)
84+
{
85+
u16 hi = val >> 16;
86+
u16 lo = val & 0xffff;
87+
88+
if (hi & 0x8000) {
89+
if (hi == 0xffff) {
90+
emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
91+
} else {
92+
emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
93+
if (lo != 0xffff)
94+
emit(A64_MOVK(is64, reg, lo, 0), ctx);
95+
}
96+
} else {
97+
emit(A64_MOVZ(is64, reg, lo, 0), ctx);
98+
if (hi)
99+
emit(A64_MOVK(is64, reg, hi, 16), ctx);
100+
}
101+
}
102+
103+
static int i64_i16_blocks(const u64 val, bool inverse)
104+
{
105+
return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
106+
(((val >> 16) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
107+
(((val >> 32) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
108+
(((val >> 48) & 0xffff) != (inverse ? 0xffff : 0x0000));
109+
}
110+
82111
static inline void emit_a64_mov_i64(const int reg, const u64 val,
83112
struct jit_ctx *ctx)
84113
{
85-
u64 tmp = val;
86-
int shift = 0;
87-
88-
emit(A64_MOVZ(1, reg, tmp & 0xffff, shift), ctx);
89-
tmp >>= 16;
90-
shift += 16;
91-
while (tmp) {
92-
if (tmp & 0xffff)
93-
emit(A64_MOVK(1, reg, tmp & 0xffff, shift), ctx);
94-
tmp >>= 16;
95-
shift += 16;
114+
u64 nrm_tmp = val, rev_tmp = ~val;
115+
bool inverse;
116+
int shift;
117+
118+
if (!(nrm_tmp >> 32))
119+
return emit_a64_mov_i(0, reg, (u32)val, ctx);
120+
121+
inverse = i64_i16_blocks(nrm_tmp, true) < i64_i16_blocks(nrm_tmp, false);
122+
shift = max(round_down((inverse ? (fls64(rev_tmp) - 1) :
123+
(fls64(nrm_tmp) - 1)), 16), 0);
124+
if (inverse)
125+
emit(A64_MOVN(1, reg, (rev_tmp >> shift) & 0xffff, shift), ctx);
126+
else
127+
emit(A64_MOVZ(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
128+
shift -= 16;
129+
while (shift >= 0) {
130+
if (((nrm_tmp >> shift) & 0xffff) != (inverse ? 0xffff : 0x0000))
131+
emit(A64_MOVK(1, reg, (nrm_tmp >> shift) & 0xffff, shift), ctx);
132+
shift -= 16;
96133
}
97134
}
98135

136+
/*
137+
* This is an unoptimized 64 immediate emission used for BPF to BPF call
138+
* addresses. It will always do a full 64 bit decomposition as otherwise
139+
* more complexity in the last extra pass is required since we previously
140+
* reserved 4 instructions for the address.
141+
*/
99142
static inline void emit_addr_mov_i64(const int reg, const u64 val,
100143
struct jit_ctx *ctx)
101144
{
@@ -110,26 +153,6 @@ static inline void emit_addr_mov_i64(const int reg, const u64 val,
110153
}
111154
}
112155

113-
static inline void emit_a64_mov_i(const int is64, const int reg,
114-
const s32 val, struct jit_ctx *ctx)
115-
{
116-
u16 hi = val >> 16;
117-
u16 lo = val & 0xffff;
118-
119-
if (hi & 0x8000) {
120-
if (hi == 0xffff) {
121-
emit(A64_MOVN(is64, reg, (u16)~lo, 0), ctx);
122-
} else {
123-
emit(A64_MOVN(is64, reg, (u16)~hi, 16), ctx);
124-
emit(A64_MOVK(is64, reg, lo, 0), ctx);
125-
}
126-
} else {
127-
emit(A64_MOVZ(is64, reg, lo, 0), ctx);
128-
if (hi)
129-
emit(A64_MOVK(is64, reg, hi, 16), ctx);
130-
}
131-
}
132-
133156
static inline int bpf2a64_offset(int bpf_to, int bpf_from,
134157
const struct jit_ctx *ctx)
135158
{

0 commit comments

Comments
 (0)