Skip to content

Commit 6c59500

Browse files
Jakub Kicinskiborkmann
authored andcommitted
nfp: bpf: optimize add/sub of a negative constant
NFP instruction set can fit small immediates into the instruction. Negative integers, however, will never fit because they will have highest bit set. If we swap the ALU op between ADD and SUB and negate the constant we have a better chance of fitting small negative integers into the instruction itself and saving one or two cycles. immed[gprB_21, 0xfffffffc] alu[gprA_4, gprA_4, +, gprB_21], gpr_wrboth immed[gprB_21, 0xffffffff] alu[gprA_5, gprA_5, +carry, gprB_21], gpr_wrboth now becomes: alu[gprA_4, gprA_4, -, 4], gpr_wrboth alu[gprA_5, gprA_5, -carry, 0], gpr_wrboth Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 9c9e532 commit 6c59500

File tree

1 file changed

+35
-0
lines changed
  • drivers/net/ethernet/netronome/nfp/bpf

1 file changed

+35
-0
lines changed

drivers/net/ethernet/netronome/nfp/bpf/jit.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,40 @@ static void nfp_bpf_opt_reg_init(struct nfp_prog *nfp_prog)
27772777
}
27782778
}
27792779

2780+
/* abs(insn.imm) will fit better into unrestricted reg immediate -
2781+
* convert add/sub of a negative number into a sub/add of a positive one.
2782+
*/
2783+
static void nfp_bpf_opt_neg_add_sub(struct nfp_prog *nfp_prog)
2784+
{
2785+
struct nfp_insn_meta *meta;
2786+
2787+
list_for_each_entry(meta, &nfp_prog->insns, l) {
2788+
struct bpf_insn insn = meta->insn;
2789+
2790+
if (meta->skip)
2791+
continue;
2792+
2793+
if (BPF_CLASS(insn.code) != BPF_ALU &&
2794+
BPF_CLASS(insn.code) != BPF_ALU64)
2795+
continue;
2796+
if (BPF_SRC(insn.code) != BPF_K)
2797+
continue;
2798+
if (insn.imm >= 0)
2799+
continue;
2800+
2801+
if (BPF_OP(insn.code) == BPF_ADD)
2802+
insn.code = BPF_CLASS(insn.code) | BPF_SUB;
2803+
else if (BPF_OP(insn.code) == BPF_SUB)
2804+
insn.code = BPF_CLASS(insn.code) | BPF_ADD;
2805+
else
2806+
continue;
2807+
2808+
meta->insn.code = insn.code | BPF_K;
2809+
2810+
meta->insn.imm = -insn.imm;
2811+
}
2812+
}
2813+
27802814
/* Remove masking after load since our load guarantees this is not needed */
27812815
static void nfp_bpf_opt_ld_mask(struct nfp_prog *nfp_prog)
27822816
{
@@ -3212,6 +3246,7 @@ static int nfp_bpf_optimize(struct nfp_prog *nfp_prog)
32123246
{
32133247
nfp_bpf_opt_reg_init(nfp_prog);
32143248

3249+
nfp_bpf_opt_neg_add_sub(nfp_prog);
32153250
nfp_bpf_opt_ld_mask(nfp_prog);
32163251
nfp_bpf_opt_ld_shift(nfp_prog);
32173252
nfp_bpf_opt_ldst_gather(nfp_prog);

0 commit comments

Comments
 (0)