Skip to content

Commit

Permalink
Specialize isnull
Browse files Browse the repository at this point in the history
It can sometimes turn into a constant integer, which may lead to some
branch elimination.
  • Loading branch information
jnthn committed Apr 19, 2018
1 parent e88b24b commit b8cd9ca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/spesh/optimize.c
Expand Up @@ -80,6 +80,24 @@ MVMint16 MVM_spesh_add_spesh_slot(MVMThreadContext *tc, MVMSpeshGraph *g, MVMCol
return g->num_spesh_slots++;
}

/* If an `isnull` is on something we know the type of or value of, then we
* can quickly verify the type is not based on the null REPR and turn it
* into a constant. */
static void optimize_isnull(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb,
MVMSpeshIns *ins) {
MVMSpeshFacts *obj_facts = MVM_spesh_get_facts(tc, g, ins->operands[1]);
if (obj_facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) {
MVMint32 is_null = REPR(obj_facts->type)->ID == MVM_REPR_ID_MVMNull;
MVMSpeshFacts *result_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);
MVM_spesh_use_facts(tc, g, obj_facts);
ins->info = MVM_op_get_op(MVM_OP_const_i64_16);
ins->operands[1].lit_i16 = is_null;
result_facts->flags |= MVM_SPESH_FACT_KNOWN_VALUE;
result_facts->value.i = is_null;
MVM_spesh_facts_depend(tc, g, result_facts, obj_facts);
}
}

static void optimize_repr_op(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb,
MVMSpeshIns *ins, MVMint32 type_operand);

Expand Down Expand Up @@ -2066,6 +2084,9 @@ static void optimize_bb_switch(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshB
case MVM_OP_set:
copy_facts(tc, g, ins->operands[0], ins->operands[1]);
break;
case MVM_OP_isnull:
optimize_isnull(tc, g, bb, ins);
break;
case MVM_OP_istrue:
case MVM_OP_isfalse:
optimize_istrue_isfalse(tc, g, bb, ins);
Expand Down

0 comments on commit b8cd9ca

Please sign in to comment.