Skip to content

Commit f404980

Browse files
authored
Add fused get_loc0_loc1 opcode (#55)
get_loc0 and get_loc1 are individually very frequent opcodes _and_ they are very often paired together, making them ideal candidates for opcode fusion. Reduces microbench.js running time by about 4%.
1 parent ba9569c commit f404980

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

quickjs-opcode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ DEF( get_loc8, 2, 0, 1, loc8)
302302
DEF( put_loc8, 2, 1, 0, loc8)
303303
DEF( set_loc8, 2, 1, 1, loc8)
304304

305+
DEF( get_loc0_loc1, 1, 0, 2, none_loc)
305306
DEF( get_loc0, 1, 0, 1, none_loc)
306307
DEF( get_loc1, 1, 0, 1, none_loc)
307308
DEF( get_loc2, 1, 0, 1, none_loc)

quickjs.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14976,6 +14976,14 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1497614976
CASE(OP_put_loc8): set_value(ctx, &var_buf[*pc++], *--sp); BREAK;
1497714977
CASE(OP_set_loc8): set_value(ctx, &var_buf[*pc++], JS_DupValue(ctx, sp[-1])); BREAK;
1497814978

14979+
// Observation: get_loc0 and get_loc1 are individually very
14980+
// frequent opcodes _and_ they are very often paired together,
14981+
// making them ideal candidates for opcode fusion.
14982+
CASE(OP_get_loc0_loc1):
14983+
*sp++ = JS_DupValue(ctx, var_buf[0]);
14984+
*sp++ = JS_DupValue(ctx, var_buf[1]);
14985+
BREAK;
14986+
1497914987
CASE(OP_get_loc0): *sp++ = JS_DupValue(ctx, var_buf[0]); BREAK;
1498014988
CASE(OP_get_loc1): *sp++ = JS_DupValue(ctx, var_buf[1]); BREAK;
1498114989
CASE(OP_get_loc2): *sp++ = JS_DupValue(ctx, var_buf[2]); BREAK;
@@ -29661,6 +29669,14 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
2966129669
pos_next = cc.pos;
2966229670
break;
2966329671
}
29672+
/* transformation: get_loc(0) get_loc(1) -> get_loc0_loc1 */
29673+
if (idx == 0 && code_match(&cc, pos_next, OP_get_loc, 1, -1)) {
29674+
if (cc.line_num >= 0) line_num = cc.line_num;
29675+
add_pc2line_info(s, bc_out.size, line_num);
29676+
dbuf_putc(&bc_out, OP_get_loc0_loc1);
29677+
pos_next = cc.pos;
29678+
break;
29679+
}
2966429680
add_pc2line_info(s, bc_out.size, line_num);
2966529681
put_short_code(&bc_out, op, idx);
2966629682
}

0 commit comments

Comments
 (0)