Optimize table length computation with hinting.

10x faster on loop with t[#t+1] = x idiom. Also used by table.insert.
This commit is contained in:
Mike Pall
2020-05-27 19:20:44 +02:00
parent b2307c8ad8
commit 1a4ff13117
12 changed files with 135 additions and 82 deletions

View File

@@ -363,7 +363,7 @@ TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J)
IRIns *ir;
/* Check for any intervening guards (includes conflicting loads). */
for (ir = IR(J->cur.nins-1); ir > store; ir--)
if (irt_isguard(ir->t) || ir->o == IR_CALLL)
if (irt_isguard(ir->t) || ir->o == IR_ALEN)
goto doemit; /* No elimination possible. */
/* Remove redundant store from chain and replace with NOP. */
*refp = store->prev;
@@ -381,6 +381,67 @@ doemit:
return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
}
/* ALEN forwarding. */
TRef LJ_FASTCALL lj_opt_fwd_alen(jit_State *J)
{
IRRef tab = fins->op1; /* Table reference. */
IRRef lim = tab; /* Search limit. */
IRRef ref;
/* Search for conflicting HSTORE with numeric key. */
ref = J->chain[IR_HSTORE];
while (ref > lim) {
IRIns *store = IR(ref);
IRIns *href = IR(store->op1);
IRIns *key = IR(href->op2);
if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) {
lim = ref; /* Conflicting store found, limits search for ALEN. */
break;
}
ref = store->prev;
}
/* Try to find a matching ALEN. */
ref = J->chain[IR_ALEN];
while (ref > lim) {
/* CSE for ALEN only depends on the table, not the hint. */
if (IR(ref)->op1 == tab) {
IRRef sref;
/* Search for aliasing table.clear. */
if (!fwd_aa_tab_clear(J, ref, tab))
break;
/* Search for hint-forwarding or conflicting store. */
sref = J->chain[IR_ASTORE];
while (sref > ref) {
IRIns *store = IR(sref);
IRIns *aref = IR(store->op1);
IRIns *fref = IR(aref->op1);
if (tab == fref->op1) { /* ASTORE to the same table. */
/* Detect t[#t+1] = x idiom for push. */
IRIns *idx = IR(aref->op2);
if (!irt_isnil(store->t) &&
idx->o == IR_ADD && idx->op1 == ref &&
IR(idx->op2)->o == IR_KINT && IR(idx->op2)->i == 1) {
/* Note: this requires an extra PHI check in loop unroll. */
fins->op2 = aref->op2; /* Set ALEN hint. */
}
goto doemit; /* Conflicting store, possibly giving a hint. */
} else if (aa_table(J, tab, fref->op1) == ALIAS_NO) {
goto doemit; /* Conflicting store. */
}
sref = store->prev;
}
return ref; /* Plain ALEN forwarding. */
}
ref = IR(ref)->prev;
}
doemit:
return EMITFOLD;
}
/* -- ULOAD forwarding ---------------------------------------------------- */
/* The current alias analysis for upvalues is very simplistic. It only
@@ -430,7 +491,6 @@ TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J)
cselim:
/* Try to find a matching load. Below the conflicting store, if any. */
ref = J->chain[IR_ULOAD];
while (ref > lim) {
IRIns *ir = IR(ref);
@@ -845,39 +905,6 @@ doemit:
return EMITFOLD; /* Otherwise we have a conflict or simply no match. */
}
/* -- Forwarding of lj_tab_len -------------------------------------------- */
/* This is rather simplistic right now, but better than nothing. */
TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J)
{
IRRef tab = fins->op1; /* Table reference. */
IRRef lim = tab; /* Search limit. */
IRRef ref;
/* Any ASTORE is a conflict and limits the search. */
if (J->chain[IR_ASTORE] > lim) lim = J->chain[IR_ASTORE];
/* Search for conflicting HSTORE with numeric key. */
ref = J->chain[IR_HSTORE];
while (ref > lim) {
IRIns *store = IR(ref);
IRIns *href = IR(store->op1);
IRIns *key = IR(href->op2);
if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) {
lim = ref; /* Conflicting store found, limits search for TLEN. */
break;
}
ref = store->prev;
}
/* Search for aliasing table.clear. */
if (!fwd_aa_tab_clear(J, lim, tab))
return lj_ir_emit(J);
/* Try to find a matching load. Below the conflicting store, if any. */
return lj_opt_cselim(J, lim);
}
/* -- ASTORE/HSTORE previous type analysis -------------------------------- */
/* Check whether the previous value for a table store is non-nil.