From fa471f1d8c2a7a1888f8fa53ad2a7332dba6fb09 Mon Sep 17 00:00:00 2001 From: Jonathan Worthington Date: Wed, 4 Jul 2018 15:07:04 +0200 Subject: [PATCH] Fact discovery determines usage counts Some time ago, a call was added to do fact discovery on inlinees. This process sets usage counts along the way. However, the code to set the usage counts in inlining was also in place, so we doubled all of the counts, which would frustrate dead code elimination possibilities that arise post-inline. With this change we only do fact discovery, and that call is moved to inline.c instead, where it feels a little more natural. Also don't pass along the inliner's plan to go with inlinee fact discovery; it doesn't relate to the inlinee, and could cause bogus guard insertion if we're unlucky. --- src/spesh/facts.c | 2 +- src/spesh/inline.c | 22 +++------------------- src/spesh/optimize.c | 1 - 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/spesh/facts.c b/src/spesh/facts.c index 3cbe02a8f8..1566d0707e 100644 --- a/src/spesh/facts.c +++ b/src/spesh/facts.c @@ -461,7 +461,7 @@ static void add_bb_facts(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb, } ann = ann->next; } - if (ann_deopt_one && ann_logged) { + if (p && ann_deopt_one && ann_logged) { if (ins->info->opcode == MVM_OP_speshresolve) plugin_facts(tc, g, bb, ins, p, ann_deopt_one, ann_logged); else diff --git a/src/spesh/inline.c b/src/spesh/inline.c index 2756323adb..d89d9d355a 100644 --- a/src/spesh/inline.c +++ b/src/spesh/inline.c @@ -182,25 +182,9 @@ MVMSpeshGraph * MVM_spesh_inline_try_get_graph(MVMThreadContext *tc, MVMSpeshGra * inline the graph. */ ig = MVM_spesh_graph_create_from_cand(tc, target_sf, cand, 0); if (is_graph_inlineable(tc, inliner, target_sf, invoke_ins, ig, no_inline_reason)) { - /* We can inline it. Bump usage counts and return the graph. */ - MVMSpeshBB *bb = ig->entry; - while (bb) { - MVMSpeshIns *ins = bb->first_ins; - while (ins) { - MVMint32 opcode = ins->info->opcode; - MVMint32 is_phi = opcode == MVM_SSA_PHI; - MVMuint8 i; - for (i = 0; i < ins->info->num_operands; i++) - if ((is_phi && i > 0) - || (!is_phi && (ins->info->operands[i] & MVM_operand_rw_mask) == MVM_operand_read_reg)) - ig->facts[ins->operands[i].reg.orig][ins->operands[i].reg.i].usages++; - if (opcode == MVM_OP_inc_i || opcode == MVM_OP_inc_u || - opcode == MVM_OP_dec_i || opcode == MVM_OP_dec_u) - ig->facts[ins->operands[0].reg.orig][ins->operands[0].reg.i - 1].usages++; - ins = ins->next; - } - bb = bb->linear_next; - } + /* We can inline it. Do facts discovery, which also sets usage counts, and + * return it. */ + MVM_spesh_facts_discover(tc, ig, NULL); return ig; } else { diff --git a/src/spesh/optimize.c b/src/spesh/optimize.c index bbda549ec1..3b0291a774 100644 --- a/src/spesh/optimize.c +++ b/src/spesh/optimize.c @@ -1746,7 +1746,6 @@ static void optimize_call(MVMThreadContext *tc, MVMSpeshGraph *g, MVMSpeshBB *bb MVMSpeshOperand code_ref_reg = ins->info->opcode == MVM_OP_invoke_v ? ins->operands[0] : ins->operands[1]; - MVM_spesh_facts_discover(tc, inline_graph, p); MVM_spesh_get_facts(tc, g, code_ref_reg)->usages++; MVM_spesh_inline(tc, g, arg_info, bb, ins, inline_graph, target_sf, code_ref_reg);