Skip to content

Commit 6035b3f

Browse files
daviddaneydavem330
authored andcommitted
MIPS,bpf: Cache value of BPF_OP(insn->code) in eBPF JIT.
The code looks a little cleaner if we replace BPF_OP(insn->code) with the local variable bpf_op. Caching the value this way also saves 300 bytes (about 1%) in the code size of the JIT. Signed-off-by: David Daney <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a67b375 commit 6035b3f

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

arch/mips/net/ebpf_jit.c

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
670670
unsigned int target;
671671
u64 t64;
672672
s64 t64s;
673+
int bpf_op = BPF_OP(insn->code);
673674

674675
switch (insn->code) {
675676
case BPF_ALU64 | BPF_ADD | BPF_K: /* ALU64_IMM */
@@ -758,13 +759,13 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
758759
emit_instr(ctx, sll, dst, dst, 0);
759760
if (insn->imm == 1) {
760761
/* div by 1 is a nop, mod by 1 is zero */
761-
if (BPF_OP(insn->code) == BPF_MOD)
762+
if (bpf_op == BPF_MOD)
762763
emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
763764
break;
764765
}
765766
gen_imm_to_reg(insn, MIPS_R_AT, ctx);
766767
emit_instr(ctx, divu, dst, MIPS_R_AT);
767-
if (BPF_OP(insn->code) == BPF_DIV)
768+
if (bpf_op == BPF_DIV)
768769
emit_instr(ctx, mflo, dst);
769770
else
770771
emit_instr(ctx, mfhi, dst);
@@ -786,13 +787,13 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
786787

787788
if (insn->imm == 1) {
788789
/* div by 1 is a nop, mod by 1 is zero */
789-
if (BPF_OP(insn->code) == BPF_MOD)
790+
if (bpf_op == BPF_MOD)
790791
emit_instr(ctx, addu, dst, MIPS_R_ZERO, MIPS_R_ZERO);
791792
break;
792793
}
793794
gen_imm_to_reg(insn, MIPS_R_AT, ctx);
794795
emit_instr(ctx, ddivu, dst, MIPS_R_AT);
795-
if (BPF_OP(insn->code) == BPF_DIV)
796+
if (bpf_op == BPF_DIV)
796797
emit_instr(ctx, mflo, dst);
797798
else
798799
emit_instr(ctx, mfhi, dst);
@@ -817,7 +818,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
817818
emit_instr(ctx, dinsu, dst, MIPS_R_ZERO, 32, 32);
818819
did_move = false;
819820
if (insn->src_reg == BPF_REG_10) {
820-
if (BPF_OP(insn->code) == BPF_MOV) {
821+
if (bpf_op == BPF_MOV) {
821822
emit_instr(ctx, daddiu, dst, MIPS_R_SP, MAX_BPF_STACK);
822823
did_move = true;
823824
} else {
@@ -827,15 +828,15 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
827828
} else if (get_reg_val_type(ctx, this_idx, insn->src_reg) == REG_32BIT) {
828829
int tmp_reg = MIPS_R_AT;
829830

830-
if (BPF_OP(insn->code) == BPF_MOV) {
831+
if (bpf_op == BPF_MOV) {
831832
tmp_reg = dst;
832833
did_move = true;
833834
}
834835
emit_instr(ctx, daddu, tmp_reg, src, MIPS_R_ZERO);
835836
emit_instr(ctx, dinsu, tmp_reg, MIPS_R_ZERO, 32, 32);
836837
src = MIPS_R_AT;
837838
}
838-
switch (BPF_OP(insn->code)) {
839+
switch (bpf_op) {
839840
case BPF_MOV:
840841
if (!did_move)
841842
emit_instr(ctx, daddu, dst, src, MIPS_R_ZERO);
@@ -867,7 +868,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
867868
emit_instr(ctx, beq, src, MIPS_R_ZERO, b_off);
868869
emit_instr(ctx, movz, MIPS_R_V0, MIPS_R_ZERO, src);
869870
emit_instr(ctx, ddivu, dst, src);
870-
if (BPF_OP(insn->code) == BPF_DIV)
871+
if (bpf_op == BPF_DIV)
871872
emit_instr(ctx, mflo, dst);
872873
else
873874
emit_instr(ctx, mfhi, dst);
@@ -911,15 +912,15 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
911912
if (ts == REG_64BIT || ts == REG_32BIT_ZERO_EX) {
912913
int tmp_reg = MIPS_R_AT;
913914

914-
if (BPF_OP(insn->code) == BPF_MOV) {
915+
if (bpf_op == BPF_MOV) {
915916
tmp_reg = dst;
916917
did_move = true;
917918
}
918919
/* sign extend */
919920
emit_instr(ctx, sll, tmp_reg, src, 0);
920921
src = MIPS_R_AT;
921922
}
922-
switch (BPF_OP(insn->code)) {
923+
switch (bpf_op) {
923924
case BPF_MOV:
924925
if (!did_move)
925926
emit_instr(ctx, addu, dst, src, MIPS_R_ZERO);
@@ -950,7 +951,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
950951
emit_instr(ctx, beq, src, MIPS_R_ZERO, b_off);
951952
emit_instr(ctx, movz, MIPS_R_V0, MIPS_R_ZERO, src);
952953
emit_instr(ctx, divu, dst, src);
953-
if (BPF_OP(insn->code) == BPF_DIV)
954+
if (bpf_op == BPF_DIV)
954955
emit_instr(ctx, mflo, dst);
955956
else
956957
emit_instr(ctx, mfhi, dst);
@@ -977,7 +978,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
977978
break;
978979
case BPF_JMP | BPF_JEQ | BPF_K: /* JMP_IMM */
979980
case BPF_JMP | BPF_JNE | BPF_K: /* JMP_IMM */
980-
cmp_eq = (BPF_OP(insn->code) == BPF_JEQ);
981+
cmp_eq = (bpf_op == BPF_JEQ);
981982
dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
982983
if (dst < 0)
983984
return dst;
@@ -1012,18 +1013,18 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
10121013
emit_instr(ctx, sll, MIPS_R_AT, dst, 0);
10131014
dst = MIPS_R_AT;
10141015
}
1015-
if (BPF_OP(insn->code) == BPF_JSET) {
1016+
if (bpf_op == BPF_JSET) {
10161017
emit_instr(ctx, and, MIPS_R_AT, dst, src);
10171018
cmp_eq = false;
10181019
dst = MIPS_R_AT;
10191020
src = MIPS_R_ZERO;
1020-
} else if (BPF_OP(insn->code) == BPF_JSGT || BPF_OP(insn->code) == BPF_JSLE) {
1021+
} else if (bpf_op == BPF_JSGT || bpf_op == BPF_JSLE) {
10211022
emit_instr(ctx, dsubu, MIPS_R_AT, dst, src);
10221023
if ((insn + 1)->code == (BPF_JMP | BPF_EXIT) && insn->off == 1) {
10231024
b_off = b_imm(exit_idx, ctx);
10241025
if (is_bad_offset(b_off))
10251026
return -E2BIG;
1026-
if (BPF_OP(insn->code) == BPF_JSGT)
1027+
if (bpf_op == BPF_JSGT)
10271028
emit_instr(ctx, blez, MIPS_R_AT, b_off);
10281029
else
10291030
emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
@@ -1033,35 +1034,35 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
10331034
b_off = b_imm(this_idx + insn->off + 1, ctx);
10341035
if (is_bad_offset(b_off))
10351036
return -E2BIG;
1036-
if (BPF_OP(insn->code) == BPF_JSGT)
1037+
if (bpf_op == BPF_JSGT)
10371038
emit_instr(ctx, bgtz, MIPS_R_AT, b_off);
10381039
else
10391040
emit_instr(ctx, blez, MIPS_R_AT, b_off);
10401041
emit_instr(ctx, nop);
10411042
break;
1042-
} else if (BPF_OP(insn->code) == BPF_JSGE || BPF_OP(insn->code) == BPF_JSLT) {
1043+
} else if (bpf_op == BPF_JSGE || bpf_op == BPF_JSLT) {
10431044
emit_instr(ctx, slt, MIPS_R_AT, dst, src);
1044-
cmp_eq = BPF_OP(insn->code) == BPF_JSGE;
1045+
cmp_eq = bpf_op == BPF_JSGE;
10451046
dst = MIPS_R_AT;
10461047
src = MIPS_R_ZERO;
1047-
} else if (BPF_OP(insn->code) == BPF_JGT || BPF_OP(insn->code) == BPF_JLE) {
1048+
} else if (bpf_op == BPF_JGT || bpf_op == BPF_JLE) {
10481049
/* dst or src could be AT */
10491050
emit_instr(ctx, dsubu, MIPS_R_T8, dst, src);
10501051
emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
10511052
/* SP known to be non-zero, movz becomes boolean not */
10521053
emit_instr(ctx, movz, MIPS_R_T9, MIPS_R_SP, MIPS_R_T8);
10531054
emit_instr(ctx, movn, MIPS_R_T9, MIPS_R_ZERO, MIPS_R_T8);
10541055
emit_instr(ctx, or, MIPS_R_AT, MIPS_R_T9, MIPS_R_AT);
1055-
cmp_eq = BPF_OP(insn->code) == BPF_JGT;
1056+
cmp_eq = bpf_op == BPF_JGT;
10561057
dst = MIPS_R_AT;
10571058
src = MIPS_R_ZERO;
1058-
} else if (BPF_OP(insn->code) == BPF_JGE || BPF_OP(insn->code) == BPF_JLT) {
1059+
} else if (bpf_op == BPF_JGE || bpf_op == BPF_JLT) {
10591060
emit_instr(ctx, sltu, MIPS_R_AT, dst, src);
1060-
cmp_eq = BPF_OP(insn->code) == BPF_JGE;
1061+
cmp_eq = bpf_op == BPF_JGE;
10611062
dst = MIPS_R_AT;
10621063
src = MIPS_R_ZERO;
10631064
} else { /* JNE/JEQ case */
1064-
cmp_eq = (BPF_OP(insn->code) == BPF_JEQ);
1065+
cmp_eq = (bpf_op == BPF_JEQ);
10651066
}
10661067
jeq_common:
10671068
/*
@@ -1122,7 +1123,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
11221123
case BPF_JMP | BPF_JSGE | BPF_K: /* JMP_IMM */
11231124
case BPF_JMP | BPF_JSLT | BPF_K: /* JMP_IMM */
11241125
case BPF_JMP | BPF_JSLE | BPF_K: /* JMP_IMM */
1125-
cmp_eq = (BPF_OP(insn->code) == BPF_JSGE);
1126+
cmp_eq = (bpf_op == BPF_JSGE);
11261127
dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
11271128
if (dst < 0)
11281129
return dst;
@@ -1132,7 +1133,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
11321133
b_off = b_imm(exit_idx, ctx);
11331134
if (is_bad_offset(b_off))
11341135
return -E2BIG;
1135-
switch (BPF_OP(insn->code)) {
1136+
switch (bpf_op) {
11361137
case BPF_JSGT:
11371138
emit_instr(ctx, blez, dst, b_off);
11381139
break;
@@ -1152,7 +1153,7 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
11521153
b_off = b_imm(this_idx + insn->off + 1, ctx);
11531154
if (is_bad_offset(b_off))
11541155
return -E2BIG;
1155-
switch (BPF_OP(insn->code)) {
1156+
switch (bpf_op) {
11561157
case BPF_JSGT:
11571158
emit_instr(ctx, bgtz, dst, b_off);
11581159
break;
@@ -1173,14 +1174,14 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
11731174
* only "LT" compare available, so we must use imm + 1
11741175
* to generate "GT" and imm -1 to generate LE
11751176
*/
1176-
if (BPF_OP(insn->code) == BPF_JSGT)
1177+
if (bpf_op == BPF_JSGT)
11771178
t64s = insn->imm + 1;
1178-
else if (BPF_OP(insn->code) == BPF_JSLE)
1179+
else if (bpf_op == BPF_JSLE)
11791180
t64s = insn->imm + 1;
11801181
else
11811182
t64s = insn->imm;
11821183

1183-
cmp_eq = BPF_OP(insn->code) == BPF_JSGT || BPF_OP(insn->code) == BPF_JSGE;
1184+
cmp_eq = bpf_op == BPF_JSGT || bpf_op == BPF_JSGE;
11841185
if (t64s >= S16_MIN && t64s <= S16_MAX) {
11851186
emit_instr(ctx, slti, MIPS_R_AT, dst, (int)t64s);
11861187
src = MIPS_R_AT;
@@ -1197,22 +1198,22 @@ static int build_one_insn(const struct bpf_insn *insn, struct jit_ctx *ctx,
11971198
case BPF_JMP | BPF_JGE | BPF_K:
11981199
case BPF_JMP | BPF_JLT | BPF_K:
11991200
case BPF_JMP | BPF_JLE | BPF_K:
1200-
cmp_eq = (BPF_OP(insn->code) == BPF_JGE);
1201+
cmp_eq = (bpf_op == BPF_JGE);
12011202
dst = ebpf_to_mips_reg(ctx, insn, dst_reg_fp_ok);
12021203
if (dst < 0)
12031204
return dst;
12041205
/*
12051206
* only "LT" compare available, so we must use imm + 1
12061207
* to generate "GT" and imm -1 to generate LE
12071208
*/
1208-
if (BPF_OP(insn->code) == BPF_JGT)
1209+
if (bpf_op == BPF_JGT)
12091210
t64s = (u64)(u32)(insn->imm) + 1;
1210-
else if (BPF_OP(insn->code) == BPF_JLE)
1211+
else if (bpf_op == BPF_JLE)
12111212
t64s = (u64)(u32)(insn->imm) + 1;
12121213
else
12131214
t64s = (u64)(u32)(insn->imm);
12141215

1215-
cmp_eq = BPF_OP(insn->code) == BPF_JGT || BPF_OP(insn->code) == BPF_JGE;
1216+
cmp_eq = bpf_op == BPF_JGT || bpf_op == BPF_JGE;
12161217

12171218
emit_const_to_reg(ctx, MIPS_R_AT, (u64)t64s);
12181219
emit_instr(ctx, sltu, MIPS_R_AT, dst, MIPS_R_AT);

0 commit comments

Comments
 (0)