dfilter: Small optimization for "not all zero" code

Remove extra NOT instruction. Also remove unused ANY_ZERO opcode.
This commit is contained in:
João Valverde 2022-07-05 09:51:49 +01:00
parent dfa160832d
commit 5e3a7e9ab8
3 changed files with 9 additions and 29 deletions

View File

@ -44,8 +44,6 @@ dfvm_opcode_tostr(dfvm_opcode_t code)
case DFVM_ANY_LT: return "ANY_LT";
case DFVM_ALL_LE: return "ALL_LE";
case DFVM_ANY_LE: return "ANY_LE";
case DFVM_ALL_ZERO: return "ALL_ZERO";
case DFVM_ANY_ZERO: return "ANY_ZERO";
case DFVM_ALL_CONTAINS: return "ALL_CONTAINS";
case DFVM_ANY_CONTAINS: return "ANY_CONTAINS";
case DFVM_ALL_MATCHES: return "ALL_MATCHES";
@ -64,6 +62,7 @@ dfvm_opcode_tostr(dfvm_opcode_t code)
case DFVM_CALL_FUNCTION: return "CALL_FUNCTION";
case DFVM_STACK_PUSH: return "STACK_PUSH";
case DFVM_STACK_POP: return "STACK_POP";
case DFVM_NOT_ALL_ZERO: return "NOT_ALL_ZERO";
}
return "(fix-opcode-string)";
}
@ -392,9 +391,8 @@ dfvm_dump_str(wmem_allocator_t *alloc, dfilter_t *df, gboolean print_references)
id, opcode_str, arg1_str, arg2_str);
break;
case DFVM_ALL_ZERO:
case DFVM_ANY_ZERO:
wmem_strbuf_append_printf(buf, "%05d %s\t\t%s\n",
case DFVM_NOT_ALL_ZERO:
wmem_strbuf_append_printf(buf, "%05d %s\t%s\n",
id, opcode_str, arg1_str);
break;
@ -785,14 +783,6 @@ cmp_test_unary(enum match_how how, DFVMTestFunc test_func, GSList *arg1)
return want_all;
}
static gboolean
any_test_unary(dfilter_t *df, DFVMTestFunc func, dfvm_value_t *arg1)
{
ws_assert(arg1->type == REGISTER);
GSList *list1 = df->registers[arg1->value.numeric];
return cmp_test_unary(MATCH_ANY, func, list1);
}
static gboolean
all_test_unary(dfilter_t *df, DFVMTestFunc func, dfvm_value_t *arg1)
{
@ -1422,12 +1412,8 @@ dfvm_apply(dfilter_t *df, proto_tree *tree)
mk_binary(df, fvalue_modulo, arg1, arg2, arg3);
break;
case DFVM_ALL_ZERO:
accum = all_test_unary(df, fvalue_is_zero, arg1);
break;
case DFVM_ANY_ZERO:
accum = any_test_unary(df, fvalue_is_zero, arg1);
case DFVM_NOT_ALL_ZERO:
accum = !all_test_unary(df, fvalue_is_zero, arg1);
break;
case DFVM_ALL_CONTAINS:

View File

@ -70,8 +70,6 @@ typedef enum {
DFVM_ANY_LT,
DFVM_ALL_LE,
DFVM_ANY_LE,
DFVM_ALL_ZERO,
DFVM_ANY_ZERO,
DFVM_ALL_CONTAINS,
DFVM_ANY_CONTAINS,
DFVM_ALL_MATCHES,
@ -90,6 +88,7 @@ typedef enum {
DFVM_CALL_FUNCTION,
DFVM_STACK_PUSH,
DFVM_STACK_POP,
DFVM_NOT_ALL_ZERO,
} dfvm_opcode_t;
const char *

View File

@ -42,7 +42,6 @@ select_opcode(dfvm_opcode_t op, stmatch_t how)
case DFVM_ALL_GE:
case DFVM_ALL_LT:
case DFVM_ALL_LE:
case DFVM_ALL_ZERO:
case DFVM_ALL_CONTAINS:
case DFVM_ALL_MATCHES:
case DFVM_ALL_IN_RANGE:
@ -53,11 +52,11 @@ select_opcode(dfvm_opcode_t op, stmatch_t how)
case DFVM_ANY_GE:
case DFVM_ANY_LT:
case DFVM_ANY_LE:
case DFVM_ANY_ZERO:
case DFVM_ANY_CONTAINS:
case DFVM_ANY_MATCHES:
case DFVM_ANY_IN_RANGE:
return how == STNODE_MATCH_ANY ? op : op - 1;
case DFVM_NOT_ALL_ZERO:
case DFVM_IF_TRUE_GOTO:
case DFVM_IF_FALSE_GOTO:
case DFVM_CHECK_EXISTS:
@ -621,11 +620,9 @@ gen_notzero(dfwork_t *dfw, stnode_t *st_node)
GSList *jumps = NULL;
val1 = gen_arithmetic(dfw, st_node, &jumps);
insn = dfvm_insn_new(DFVM_ALL_ZERO);
insn = dfvm_insn_new(DFVM_NOT_ALL_ZERO);
insn->arg1 = dfvm_value_ref(val1);
dfw_append_insn(dfw, insn);
insn = dfvm_insn_new(DFVM_NOT);
dfw_append_insn(dfw, insn);
g_slist_foreach(jumps, fixup_jumps, dfw);
g_slist_free(jumps);
}
@ -645,11 +642,9 @@ gen_exists_slice(dfwork_t *dfw, stnode_t *st_node)
insn->arg2 = dfvm_value_ref(reg_val);
dfw_append_insn(dfw, insn);
/* Check length is not zero. */
insn = dfvm_insn_new(DFVM_ALL_ZERO);
insn = dfvm_insn_new(DFVM_NOT_ALL_ZERO);
insn->arg1 = dfvm_value_ref(reg_val);
dfw_append_insn(dfw, insn);
insn = dfvm_insn_new(DFVM_NOT);
dfw_append_insn(dfw, insn);
/* Fixup jumps. */
g_slist_foreach(jumps, fixup_jumps, dfw);
g_slist_free(jumps);