Skip to content

Instantly share code, notes, and snippets.

@timo
Created May 11, 2014 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/716656be19b892dda8ab to your computer and use it in GitHub Desktop.
Save timo/716656be19b892dda8ab to your computer and use it in GitHub Desktop.
try to optimize can and can_s
diff --git a/src/spesh/optimize.c b/src/spesh/optimize.c
index 05fd641..ebf402b 100644
--- a/src/spesh/optimize.c
+++ b/src/spesh/optimize.c
@@ -266,6 +266,44 @@ static void optimize_assertparamcheck(MVMThreadContext *tc, MVMSpeshGraph *g, MV
}
}
+static void optimize_can_op(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, MVMSpeshIns *ins) {
+ MVMSpeshFacts *obj_facts = MVM_spesh_get_facts(tc, g, ins->operands[1]);
+ MVMString *method_name;
+ MVMint64 can_result;
+
+ if (!(obj_facts->flags & MVM_SPESH_FACT_KNOWN_TYPE) || !obj_facts->type)
+ return;
+
+ printf("trying to optimize a can_op %d\n", ins->info->opcode);
+
+ if (ins->info->opcode == MVM_OP_can_s) {
+ MVMSpeshFacts *name_facts = MVM_spesh_get_facts(tc, g, ins->operands[2]);
+ if (!(name_facts->flags & MVM_SPESH_FACT_KNOWN_VALUE)) {
+ return;
+ }
+ method_name = name_facts->value.s;
+ } else {
+ method_name = MVM_spesh_get_string(tc, g, ins->operands[2]);
+ }
+
+ can_result = MVM_6model_can_method_cache_only(tc, obj_facts->type, method_name);
+
+ if (can_result == -1) {
+ printf("cache not authoritative\n");
+ return;
+ } else {
+ MVMSpeshFacts *result_facts = MVM_spesh_get_facts(tc, g, ins->operands[0]);
+ ins->info = MVM_op_get_op(MVM_OP_const_i64);
+ result_facts->flags |= MVM_SPESH_FACT_KNOWN_VALUE;
+ ins->operands[0].lit_i64 = can_result;
+ result_facts->value.i64 = can_result;
+ obj_facts->usages--;
+ if (ins->info->opcode == MVM_OP_can_s)
+ MVM_spesh_get_facts(tc, g, ins->operands[2])->usages--;
+ printf("did a can optimization to %d\n", can_result);
+ }
+}
+
/* 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,
@@ -456,6 +494,10 @@ static void optimize_bb(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb)
case MVM_OP_findmeth:
optimize_method_lookup(tc, g, ins);
break;
+ case MVM_OP_can:
+ case MVM_OP_can_s:
+ optimize_can_op(tc, g, bb, ins);
+ break;
case MVM_OP_create:
optimize_repr_op(tc, g, bb, ins, 1);
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment