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

@@ -1839,10 +1839,10 @@
18391839 buf_printf(b, "sp_int_mul(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return;
18401840 }
18411841 if (!strcmp(name, "/") && ac2 == 1) {
1842- buf_printf(b, "sp_idiv(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return;
1842+ buf_printf(b, "sp_idiv(%s, ", s); emit_int_divisor(c, av2[0], b); buf_puts(b, ")"); return;
18431843 }
18441844 if (!strcmp(name, "%") && ac2 == 1) {
1845- buf_printf(b, "sp_imod(%s, ", s); emit_expr(c, av2[0], b); buf_puts(b, ")"); return;
1845+ buf_printf(b, "sp_imod(%s, ", s); emit_int_divisor(c, av2[0], b); buf_puts(b, ")"); return;
18461846 }
18471847 }
18481848 else if (brt == TY_FLOAT) {
@@ -3482,8 +3482,11 @@
34823482 else if (rt == TY_INT && (a0 == TY_INT || a0 == TY_UNKNOWN)) eff_res = TY_INT;
34833483 }
34843484 if (eff_res == TY_INT) {
3485+ int isdivmod = !strcmp(name, "/") || !strcmp(name, "%");
34853486 buf_printf(b, "%s(", int_arith_fn(name));
3486- emit_expr(c, recv, b); buf_puts(b, ", "); emit_expr(c, argv[0], b);
3487+ emit_expr(c, recv, b); buf_puts(b, ", ");
3488+ if (isdivmod) emit_int_divisor(c, argv[0], b);
3489+ else emit_expr(c, argv[0], b);
34873490 buf_puts(b, ")");
34883491 return;
34893492 }
@@ -7530,11 +7533,11 @@
75307533 buf_printf(b, "; sp_IntArray *_t%d = sp_IntArray_new(); sp_IntArray_push(_t%d, sp_idiv(%s, _t%d));"
75317534 " sp_IntArray_push(_t%d, sp_imod(%s, _t%d)); _t%d; })", o, o, r, tb, o, r, tb, o);
75327535 }
7533- else if (!strcmp(name, "div") && argc == 1) { buf_printf(b, "sp_idiv(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
7536+ else if (!strcmp(name, "div") && argc == 1) { buf_printf(b, "sp_idiv(%s, ", r); emit_int_divisor(c, argv[0], b); buf_puts(b, ")"); }
75347537 else if (!strcmp(name, "gcd") && argc == 1) { buf_printf(b, "sp_gcd(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
75357538 else if (!strcmp(name, "lcm") && argc == 1) { buf_printf(b, "sp_lcm(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
75367539 else if (!strcmp(name, "magnitude") && argc == 0) buf_printf(b, "((%s) < 0 ? -(%s) : (%s))", r, r, r);
7537- else if (!strcmp(name, "modulo") && argc == 1) { buf_printf(b, "sp_imod(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
7540+ else if (!strcmp(name, "modulo") && argc == 1) { buf_printf(b, "sp_imod(%s, ", r); emit_int_divisor(c, argv[0], b); buf_puts(b, ")"); }
75387541 else if (!strcmp(name, "remainder") && argc == 1) { buf_printf(b, "((%s) %% (", r); emit_expr(c, argv[0], b); buf_puts(b, "))"); }
75397542 else if (!strcmp(name, "size") && argc == 0) buf_puts(b, "((mrb_int)sizeof(mrb_int))");
75407543 else if (!strcmp(name, "gcdlcm") && argc == 1) {