Skip to content

Commit

Permalink
Rename optimize_coerce To _in, Add coerce_ni
Browse files Browse the repository at this point in the history
in cases where a coerce_ni is used on the result of
a coerce_in, we can just use the original integer.

Normally, the combination of these ops would cause
truncation of integers to only ~52 bits of precision,
but for many operations, like elems, it's likely that
the full integer was meant to be used.

This optimization may want to be limited to nqp, or
perhaps limited to the result of certain ops.
  • Loading branch information
timo committed Dec 19, 2018
1 parent caf1070 commit e735564
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/spesh/optimize.c
Expand Up @@ -894,7 +894,7 @@ static void optimize_can_op(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *
}

/* If we have a const_i and a coerce_in, we can emit a const_n instead. */
static void optimize_coerce(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
static void optimize_coerce_in(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g, ins->operands[1]);

if (facts->flags & MVM_SPESH_FACT_KNOWN_VALUE) {
Expand All @@ -912,6 +912,24 @@ static void optimize_coerce(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *
}
}

/* A coerce_ni after a coerce_in can become a set. Hooray! */
static void optimize_coerce_ni(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
MVMSpeshFacts *facts = MVM_spesh_get_facts(tc, g, ins->operands[1]);

if (facts->writer && facts->writer->info->opcode == MVM_OP_coerce_in) {
MVMSpeshFacts *result_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);

MVM_spesh_usages_delete(tc, g, facts, ins);

ins->info = MVM_op_get_op(MVM_OP_set);
ins->operands[1].reg = facts->writer->operands[1].reg;

MVM_spesh_usages_add_by_reg(tc, g, ins->operands[1], ins);

MVM_spesh_graph_add_comment(tc, g, ins, "Removed redundant int-to-num-to-int coerce");
}
}

/* If we know the type of a significant operand, we might try to specialize by
* representation. */
static void optimize_repr_op(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb,
Expand Down Expand Up @@ -2654,7 +2672,10 @@ static void optimize_bb_switch(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshB
break;
}
case MVM_OP_coerce_in:
optimize_coerce(tc, g, bb, ins);
optimize_coerce_in(tc, g, bb, ins);
break;
case MVM_OP_coerce_ni:
optimize_coerce_ni(tc, g, bb, ins);
break;
case MVM_OP_smrt_numify:
case MVM_OP_smrt_strify:
Expand Down

0 comments on commit e735564

Please sign in to comment.