diff options
| -rw-r--r-- | src/Makefile.dep | 2 | ||||
| -rw-r--r-- | src/lj_opt_fold.c | 53 |
2 files changed, 54 insertions, 1 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep index 1534ac27..cecaf403 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep | |||
| @@ -119,7 +119,7 @@ lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | |||
| 119 | lj_ir.h lj_jit.h lj_iropt.h | 119 | lj_ir.h lj_jit.h lj_iropt.h |
| 120 | lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | 120 | lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ |
| 121 | lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ | 121 | lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ |
| 122 | lj_bc.h lj_traceerr.h lj_vm.h lj_folddef.h | 122 | lj_bc.h lj_traceerr.h lj_carith.h lj_vm.h lj_folddef.h |
| 123 | lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ | 123 | lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ |
| 124 | lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h \ | 124 | lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h \ |
| 125 | lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h | 125 | lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h |
diff --git a/src/lj_opt_fold.c b/src/lj_opt_fold.c index 03caf80d..93fb782c 100644 --- a/src/lj_opt_fold.c +++ b/src/lj_opt_fold.c | |||
| @@ -18,6 +18,7 @@ | |||
| 18 | #include "lj_jit.h" | 18 | #include "lj_jit.h" |
| 19 | #include "lj_iropt.h" | 19 | #include "lj_iropt.h" |
| 20 | #include "lj_trace.h" | 20 | #include "lj_trace.h" |
| 21 | #include "lj_carith.h" | ||
| 21 | #include "lj_vm.h" | 22 | #include "lj_vm.h" |
| 22 | 23 | ||
| 23 | /* Here's a short description how the FOLD engine processes instructions: | 24 | /* Here's a short description how the FOLD engine processes instructions: |
| @@ -296,12 +297,16 @@ LJFOLDF(kfold_intcomp0) | |||
| 296 | static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op) | 297 | static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op) |
| 297 | { | 298 | { |
| 298 | switch (op) { | 299 | switch (op) { |
| 300 | #if LJ_64 || LJ_HASFFI | ||
| 299 | case IR_ADD: k1 += k2; break; | 301 | case IR_ADD: k1 += k2; break; |
| 300 | case IR_SUB: k1 -= k2; break; | 302 | case IR_SUB: k1 -= k2; break; |
| 303 | #endif | ||
| 304 | #if LJ_HASFFI | ||
| 301 | case IR_MUL: k1 *= k2; break; | 305 | case IR_MUL: k1 *= k2; break; |
| 302 | case IR_BAND: k1 &= k2; break; | 306 | case IR_BAND: k1 &= k2; break; |
| 303 | case IR_BOR: k1 |= k2; break; | 307 | case IR_BOR: k1 |= k2; break; |
| 304 | case IR_BXOR: k1 ^= k2; break; | 308 | case IR_BXOR: k1 ^= k2; break; |
| 309 | #endif | ||
| 305 | default: lua_assert(0); break; | 310 | default: lua_assert(0); break; |
| 306 | } | 311 | } |
| 307 | return k1; | 312 | return k1; |
| @@ -319,6 +324,28 @@ LJFOLDF(kfold_int64arith) | |||
| 319 | ir_k64(fright)->u64, (IROp)fins->o)); | 324 | ir_k64(fright)->u64, (IROp)fins->o)); |
| 320 | } | 325 | } |
| 321 | 326 | ||
| 327 | LJFOLD(DIV KINT64 KINT64) | ||
| 328 | LJFOLD(MOD KINT64 KINT64) | ||
| 329 | LJFOLD(POWI KINT64 KINT64) | ||
| 330 | LJFOLDF(kfold_int64arith2) | ||
| 331 | { | ||
| 332 | #if LJ_HASFFI | ||
| 333 | uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64; | ||
| 334 | if (irt_isi64(fins->t)) { | ||
| 335 | k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) : | ||
| 336 | fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) : | ||
| 337 | lj_carith_powi64((int64_t)k1, (int64_t)k2); | ||
| 338 | } else { | ||
| 339 | k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) : | ||
| 340 | fins->o == IR_MOD ? lj_carith_modu64(k1, k2) : | ||
| 341 | lj_carith_powu64(k1, k2); | ||
| 342 | } | ||
| 343 | return INT64FOLD(k1); | ||
| 344 | #else | ||
| 345 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 346 | #endif | ||
| 347 | } | ||
| 348 | |||
| 322 | LJFOLD(BSHL KINT64 KINT) | 349 | LJFOLD(BSHL KINT64 KINT) |
| 323 | LJFOLD(BSHR KINT64 KINT) | 350 | LJFOLD(BSHR KINT64 KINT) |
| 324 | LJFOLD(BSAR KINT64 KINT) | 351 | LJFOLD(BSAR KINT64 KINT) |
| @@ -326,29 +353,43 @@ LJFOLD(BROL KINT64 KINT) | |||
| 326 | LJFOLD(BROR KINT64 KINT) | 353 | LJFOLD(BROR KINT64 KINT) |
| 327 | LJFOLDF(kfold_int64shift) | 354 | LJFOLDF(kfold_int64shift) |
| 328 | { | 355 | { |
| 356 | #if LJ_HASFFI || LJ_64 | ||
| 329 | uint64_t k = ir_k64(fleft)->u64; | 357 | uint64_t k = ir_k64(fleft)->u64; |
| 330 | int32_t sh = (fright->i & 63); | 358 | int32_t sh = (fright->i & 63); |
| 331 | switch ((IROp)fins->o) { | 359 | switch ((IROp)fins->o) { |
| 332 | case IR_BSHL: k <<= sh; break; | 360 | case IR_BSHL: k <<= sh; break; |
| 361 | #if LJ_HASFFI | ||
| 333 | case IR_BSHR: k >>= sh; break; | 362 | case IR_BSHR: k >>= sh; break; |
| 334 | case IR_BSAR: k = (uint64_t)((int64_t)k >> sh); break; | 363 | case IR_BSAR: k = (uint64_t)((int64_t)k >> sh); break; |
| 335 | case IR_BROL: k = lj_rol(k, sh); break; | 364 | case IR_BROL: k = lj_rol(k, sh); break; |
| 336 | case IR_BROR: k = lj_ror(k, sh); break; | 365 | case IR_BROR: k = lj_ror(k, sh); break; |
| 366 | #endif | ||
| 337 | default: lua_assert(0); break; | 367 | default: lua_assert(0); break; |
| 338 | } | 368 | } |
| 339 | return INT64FOLD(k); | 369 | return INT64FOLD(k); |
| 370 | #else | ||
| 371 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 372 | #endif | ||
| 340 | } | 373 | } |
| 341 | 374 | ||
| 342 | LJFOLD(BNOT KINT64) | 375 | LJFOLD(BNOT KINT64) |
| 343 | LJFOLDF(kfold_bnot64) | 376 | LJFOLDF(kfold_bnot64) |
| 344 | { | 377 | { |
| 378 | #if LJ_HASFFI | ||
| 345 | return INT64FOLD(~ir_k64(fleft)->u64); | 379 | return INT64FOLD(~ir_k64(fleft)->u64); |
| 380 | #else | ||
| 381 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 382 | #endif | ||
| 346 | } | 383 | } |
| 347 | 384 | ||
| 348 | LJFOLD(BSWAP KINT64) | 385 | LJFOLD(BSWAP KINT64) |
| 349 | LJFOLDF(kfold_bswap64) | 386 | LJFOLDF(kfold_bswap64) |
| 350 | { | 387 | { |
| 388 | #if LJ_HASFFI | ||
| 351 | return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64)); | 389 | return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64)); |
| 390 | #else | ||
| 391 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 392 | #endif | ||
| 352 | } | 393 | } |
| 353 | 394 | ||
| 354 | LJFOLD(LT KINT64 KINT) | 395 | LJFOLD(LT KINT64 KINT) |
| @@ -361,6 +402,7 @@ LJFOLD(ULE KINT64 KINT) | |||
| 361 | LJFOLD(UGT KINT64 KINT) | 402 | LJFOLD(UGT KINT64 KINT) |
| 362 | LJFOLDF(kfold_int64comp) | 403 | LJFOLDF(kfold_int64comp) |
| 363 | { | 404 | { |
| 405 | #if LJ_HASFFI | ||
| 364 | uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64; | 406 | uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64; |
| 365 | switch ((IROp)fins->o) { | 407 | switch ((IROp)fins->o) { |
| 366 | case IR_LT: return CONDFOLD(a < b); | 408 | case IR_LT: return CONDFOLD(a < b); |
| @@ -373,14 +415,21 @@ LJFOLDF(kfold_int64comp) | |||
| 373 | case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b); | 415 | case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b); |
| 374 | default: lua_assert(0); return FAILFOLD; | 416 | default: lua_assert(0); return FAILFOLD; |
| 375 | } | 417 | } |
| 418 | #else | ||
| 419 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 420 | #endif | ||
| 376 | } | 421 | } |
| 377 | 422 | ||
| 378 | LJFOLD(UGE any KINT64) | 423 | LJFOLD(UGE any KINT64) |
| 379 | LJFOLDF(kfold_int64comp0) | 424 | LJFOLDF(kfold_int64comp0) |
| 380 | { | 425 | { |
| 426 | #if LJ_HASFFI | ||
| 381 | if (ir_k64(fright)->u64 == 0) | 427 | if (ir_k64(fright)->u64 == 0) |
| 382 | return DROPFOLD; | 428 | return DROPFOLD; |
| 383 | return NEXTFOLD; | 429 | return NEXTFOLD; |
| 430 | #else | ||
| 431 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 432 | #endif | ||
| 384 | } | 433 | } |
| 385 | 434 | ||
| 386 | /* -- Constant folding for strings ---------------------------------------- */ | 435 | /* -- Constant folding for strings ---------------------------------------- */ |
| @@ -1249,6 +1298,7 @@ LJFOLD(BOR BOR KINT64) | |||
| 1249 | LJFOLD(BXOR BXOR KINT64) | 1298 | LJFOLD(BXOR BXOR KINT64) |
| 1250 | LJFOLDF(reassoc_intarith_k64) | 1299 | LJFOLDF(reassoc_intarith_k64) |
| 1251 | { | 1300 | { |
| 1301 | #if LJ_HASFFI || LJ_64 | ||
| 1252 | IRIns *irk = IR(fleft->op2); | 1302 | IRIns *irk = IR(fleft->op2); |
| 1253 | if (irk->o == IR_KINT64) { | 1303 | if (irk->o == IR_KINT64) { |
| 1254 | uint64_t k = kfold_int64arith(ir_k64(irk)->u64, | 1304 | uint64_t k = kfold_int64arith(ir_k64(irk)->u64, |
| @@ -1259,6 +1309,9 @@ LJFOLDF(reassoc_intarith_k64) | |||
| 1259 | return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ | 1309 | return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ |
| 1260 | } | 1310 | } |
| 1261 | return NEXTFOLD; | 1311 | return NEXTFOLD; |
| 1312 | #else | ||
| 1313 | UNUSED(J); lua_assert(0); return FAILFOLD; | ||
| 1314 | #endif | ||
| 1262 | } | 1315 | } |
| 1263 | 1316 | ||
| 1264 | LJFOLD(MIN MIN any) | 1317 | LJFOLD(MIN MIN any) |
