Commit 416ebcfa
416ebcfa1092Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Fold compile-time-constant integer divisors to literals
Division and modulo by a named integer constant (e.g. i % W where
W = 80) emitted sp_imod(lv_i, cst_W), a runtime global read the C
compiler cannot strength-reduce. Legacy emits the literal 80, letting
GCC turn the modulo into a magic-number multiply. This left bm_life at
1.59x legacy.
Add emit_int_divisor, which folds an integer-constant expression
(literals, references to other int-literal constants, and binary integer
arithmetic over folded operands) to its literal value, and use it at the
//% divisor positions: the binary int-arith path, the direct
sp_idiv/sp_imod and div/modulo builtins, and the int op-assign paths.
Folding is restricted to divisor positions on purpose: literalizing arbitrary constants regresses optcarrot ~4% (layout-sensitive hot loops), whereas divisor-only folding leaves its fps in the noise band. A constant with any compound/or/and reassignment is not folded.
bm_life: 1.59x -> 0.92x (spinelc now faster than legacy), output matches. make test 845, make bench 57, optcarrot checksum 59662.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 416ebcfa1092
- Parent
- b10e51c2499c
src/codegen_call.cmodifiedView file
buf_printf(b, "sp_int_mul(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return; } if (!strcmp(name, "/") && ac2 == 1) { buf_printf(b, "sp_idiv(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return; buf_printf(b, "sp_idiv(%s, ", s); emit_int_divisor(c, av2[0], b); buf_puts(b, ")"); return; } if (!strcmp(name, "%") && ac2 == 1) { buf_printf(b, "sp_imod(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return; buf_printf(b, "sp_imod(%s, ", s); emit_int_divisor(c, av2[0], b); buf_puts(b, ")"); return; } } else if (brt == TY_FLOAT) { else if (rt == TY_INT && (a0 == TY_INT || a0 == TY_UNKNOWN)) eff_res = TY_INT; } if (eff_res == TY_INT) { int isdivmod = !strcmp(name, "/") || !strcmp(name, "%"); buf_printf(b, "%s(", int_arith_fn(name)); emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b); emit_expr(c, recv, b); buf_puts(b, ", "); if (isdivmod) emit_int_divisor(c, argv[0], b); else emit_expr(c, argv[0], b); buf_puts(b, ")"); return; } buf_printf(b, "; sp_IntArray *_t%d = sp_IntArray_new(); sp_IntArray_push(_t%d, sp_idiv(%s, _t%d));" " sp_IntArray_push(_t%d, sp_imod(%s, _t%d)); _t%d; })", o, o, r, tb, o, r, tb, o); } else if (!strcmp(name, "div") && argc == 1) { buf_printf(b, "sp_idiv(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "div") && argc == 1) { buf_printf(b, "sp_idiv(%s, ", r); emit_int_divisor(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "gcd") && argc == 1) { buf_printf(b, "sp_gcd(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "lcm") && argc == 1) { buf_printf(b, "sp_lcm(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "magnitude") && argc == 0) buf_printf(b, "((%s) < 0 ? -(%s) : (%s))", r, r, r); else if (!strcmp(name, "modulo") && argc == 1) { buf_printf(b, "sp_imod(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "modulo") && argc == 1) { buf_printf(b, "sp_imod(%s, ", r); emit_int_divisor(c, argv[0], b); buf_puts(b, ")"); } else if (!strcmp(name, "remainder") && argc == 1) { buf_printf(b, "((%s) %% (", r); emit_expr(c, argv[0], b); buf_puts(b, "))"); } else if (!strcmp(name, "size") && argc == 0) buf_puts(b, "((mrb_int)sizeof(mrb_int))"); else if (!strcmp(name, "gcdlcm") && argc == 1) {src/codegen_expr.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
src/codegen_stmt.cmodifiedView file
Loading diff…