Skip to content

Commit

Permalink
Add FOLD rules for mixed BAND/BOR with constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Pall committed Apr 30, 2017
1 parent fbfbd7b commit cf8a5be
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/lj_opt_fold.c
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,47 @@ LJFOLDF(simplify_andk_shiftk)
return NEXTFOLD;
}

LJFOLD(BAND BOR KINT)
LJFOLD(BOR BAND KINT)
LJFOLDF(simplify_andor_k)
{
IRIns *irk = IR(fleft->op2);
PHIBARRIER(fleft);
if (irk->o == IR_KINT) {
int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o);
/* (i | k1) & k2 ==> i & k2, if (k1 & k2) == 0. */
/* (i & k1) | k2 ==> i | k2, if (k1 | k2) == -1. */
if (k == (fins->o == IR_BAND ? 0 : -1)) {
fins->op1 = fleft->op1;
return RETRYFOLD;
}
}
return NEXTFOLD;
}

LJFOLD(BAND BOR KINT64)
LJFOLD(BOR BAND KINT64)
LJFOLDF(simplify_andor_k64)
{
#if LJ_HASFFI
IRIns *irk = IR(fleft->op2);
PHIBARRIER(fleft);
if (irk->o == IR_KINT64) {
uint64_t k = kfold_int64arith(ir_k64(irk)->u64,
ir_k64(fright)->u64, (IROp)fins->o);
/* (i | k1) & k2 ==> i & k2, if (k1 & k2) == 0. */
/* (i & k1) | k2 ==> i | k2, if (k1 | k2) == -1. */
if (k == (fins->o == IR_BAND ? (uint64_t)0 : ~(uint64_t)0)) {
fins->op1 = fleft->op1;
return RETRYFOLD;
}
}
return NEXTFOLD;
#else
UNUSED(J); lua_assert(0); return FAILFOLD;
#endif
}

/* -- Reassociation ------------------------------------------------------- */

LJFOLD(ADD ADD KINT)
Expand Down

0 comments on commit cf8a5be

Please sign in to comment.