Add FOLD rule for ALEN of TNEW/TDUP.

This commit is contained in:
Mike Pall 2026-08-19 20:57:39 +02:00
parent 8c23d6aad7
commit 1ee778a4e3

View File

@ -397,6 +397,7 @@ TRef LJ_FASTCALL lj_opt_fwd_alen(jit_State *J)
IRRef tab = fins->op1; /* Table reference. */
IRRef lim = tab; /* Search limit. */
IRRef ref;
IROp op;
/* Search for conflicting HSTORE with numeric key. */
ref = J->chain[IR_HSTORE];
@ -448,6 +449,28 @@ TRef LJ_FASTCALL lj_opt_fwd_alen(jit_State *J)
}
ref = IR(ref)->prev;
}
/* Try to const-fold length. */
op = IR(tab)->o;
if (lim == tab &&
(op == IR_TNEW || op == IR_TDUP) &&
fwd_aa_tab_clear(J, tab, tab)) {
/* Search for conflicting store. */
int32_t len = 0;
IRRef 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 || aa_table(J, tab, fref->op1) != ALIAS_NO) {
goto doemit; /* Conflicting store. */
}
sref = store->prev;
}
if (op == IR_TDUP) len = (int32_t)lj_tab_len(ir_ktab(IR(IR(tab)->op1)));
return lj_ir_kint(J, len);
}
doemit:
return EMITFOLD;
}