Strength-reduce 32 to 64 bit widening using scalar evolution analysis.

This commit is contained in:
Mike Pall 2010-12-09 16:12:59 +01:00
parent 36fed9f3e1
commit 1d86090926
2 changed files with 49 additions and 6 deletions

View File

@ -335,15 +335,17 @@ void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
IRIns *ir = IR(tref_ref(idx)); IRIns *ir = IR(tref_ref(idx));
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) &&
ir->o == IR_ADD && irref_isk(ir->op2)) { ir->o == IR_ADD && irref_isk(ir->op2)) {
IRIns *irk = IR(ir->op2);
idx = ir->op1;
/* This would be rather difficult in FOLD, so do it here: /* This would be rather difficult in FOLD, so do it here:
** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz) ** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
*/ */
idx = ir->op1;
#if LJ_64 #if LJ_64
ofs += (int64_t)ir_kint64(IR(ir->op2))->u64 * sz; if (irk->o == IR_KINT64)
#else ofs += (ptrdiff_t)ir_kint64(irk)->u64 * sz;
ofs += IR(ir->op2)->i * sz; else
#endif #endif
ofs += (ptrdiff_t)irk->i * sz;
} }
idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz)); idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr); ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);

View File

@ -644,8 +644,8 @@ LJFOLD(TOINT ADD any)
LJFOLD(TOINT SUB any) LJFOLD(TOINT SUB any)
LJFOLD(TOBIT ADD KNUM) LJFOLD(TOBIT ADD KNUM)
LJFOLD(TOBIT SUB KNUM) LJFOLD(TOBIT SUB KNUM)
LJFOLD(TOI64 ADD any) LJFOLD(TOI64 ADD 5) /* IRTOINT_TRUNCI64 */
LJFOLD(TOI64 SUB any) LJFOLD(TOI64 SUB 5) /* IRTOINT_TRUNCI64 */
LJFOLDF(narrow_convert) LJFOLDF(narrow_convert)
{ {
PHIBARRIER(fleft); PHIBARRIER(fleft);
@ -669,6 +669,47 @@ LJFOLDF(cse_toint)
return EMITFOLD; /* No fallthrough to regular CSE. */ return EMITFOLD; /* No fallthrough to regular CSE. */
} }
/* -- Strength reduction of widening -------------------------------------- */
LJFOLD(TOI64 any 3) /* IRTOINT_ZEXT64 */
LJFOLDF(simplify_zext64)
{
#if LJ_TARGET_X64
/* Eliminate widening. All 32 bit ops implicitly zero-extend the result. */
return LEFTFOLD;
#else
UNUSED(J);
return NEXTFOLD;
#endif
}
LJFOLD(TOI64 any 4) /* IRTOINT_SEXT64 */
LJFOLDF(simplify_sext64)
{
IRRef ref = fins->op1;
int64_t ofs = 0;
if (fleft->o == IR_ADD && irref_isk(fleft->op2)) {
ofs = (int64_t)IR(fleft->op2)->i;
ref = fleft->op1;
}
/* Use scalar evolution analysis results to strength-reduce sign-extension. */
if (ref == J->scev.idx) {
IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop;
lua_assert(irt_isint(J->scev.t));
if (lo && IR(lo)->i + ofs >= 0) {
#if LJ_TARGET_X64
/* Eliminate widening. All 32 bit ops do an implicit zero-extension. */
return LEFTFOLD;
#else
/* Reduce to a (cheaper) zero-extension. */
fins->op2 = IRTOINT_ZEXT64;
return RETRYFOLD;
#endif
}
}
return NEXTFOLD;
}
/* -- Integer algebraic simplifications ----------------------------------- */ /* -- Integer algebraic simplifications ----------------------------------- */
LJFOLD(ADD any KINT) LJFOLD(ADD any KINT)