Skip to content

Commit cca94f6

Browse files
committed
JIT: Optimize calls to TERM_COMPARE
Signed-off-by: Paul Guyot <[email protected]>
1 parent 4a7865e commit cca94f6

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

libs/jit/src/jit.erl

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,26 @@ first_pass(<<?OP_IS_EQ_EXACT, Rest0/binary>>, MMod, MSt0, State0) ->
528528
{MSt1, Arg1, Rest2} = decode_compact_term(Rest1, MMod, MSt0, State0),
529529
{MSt2, Arg2, Rest3} = decode_compact_term(Rest2, MMod, MSt1, State0),
530530
?TRACE("OP_IS_EQ_EXACT ~p, ~p, ~p\n", [Label, Arg1, Arg2]),
531-
{MSt3, ResultReg} = MMod:call_primitive(MSt2, ?PRIM_TERM_COMPARE, [
532-
ctx, jit_state, {free, Arg1}, {free, Arg2}, ?TERM_COMPARE_EXACT
533-
]),
534-
MSt4 = handle_error_if({'(int)', ResultReg, '==', ?TERM_COMPARE_MEMORY_ALLOC_FAIL}, MMod, MSt3),
535-
MSt5 = cond_jump_to_label(
536-
{{free, ResultReg}, '&', ?TERM_LESS_THAN + ?TERM_GREATER_THAN, '!=', 0},
537-
Label,
538-
MMod,
539-
MSt4
540-
),
531+
% If Arg2 is an immediate, we don't need to call term_compare
532+
MSt5 =
533+
if
534+
is_integer(Arg2) ->
535+
{MSt3, Arg1Reg} = MMod:move_to_native_register(MSt2, Arg1),
536+
cond_jump_to_label({{free, Arg1Reg}, '!=', Arg2}, Label, MMod, MSt3);
537+
true ->
538+
{MSt3, ResultReg} = MMod:call_primitive(MSt2, ?PRIM_TERM_COMPARE, [
539+
ctx, jit_state, {free, Arg1}, {free, Arg2}, ?TERM_COMPARE_EXACT
540+
]),
541+
MSt4 = handle_error_if(
542+
{'(int)', ResultReg, '==', ?TERM_COMPARE_MEMORY_ALLOC_FAIL}, MMod, MSt3
543+
),
544+
cond_jump_to_label(
545+
{{free, ResultReg}, '&', ?TERM_LESS_THAN + ?TERM_GREATER_THAN, '!=', 0},
546+
Label,
547+
MMod,
548+
MSt4
549+
)
550+
end,
541551
?ASSERT_ALL_NATIVE_FREE(MSt5),
542552
first_pass(Rest3, MMod, MSt5, State0);
543553
% 44
@@ -547,11 +557,22 @@ first_pass(<<?OP_IS_NOT_EQ_EXACT, Rest0/binary>>, MMod, MSt0, State0) ->
547557
{MSt1, Arg1, Rest2} = decode_compact_term(Rest1, MMod, MSt0, State0),
548558
{MSt2, Arg2, Rest3} = decode_compact_term(Rest2, MMod, MSt1, State0),
549559
?TRACE("OP_IS_NOT_EQ_EXACT ~p, ~p, ~p\n", [Label, Arg1, Arg2]),
550-
{MSt3, ResultReg} = MMod:call_primitive(MSt2, ?PRIM_TERM_COMPARE, [
551-
ctx, jit_state, {free, Arg1}, {free, Arg2}, ?TERM_COMPARE_EXACT
552-
]),
553-
MSt4 = handle_error_if({'(int)', ResultReg, '==', ?TERM_COMPARE_MEMORY_ALLOC_FAIL}, MMod, MSt3),
554-
MSt5 = cond_jump_to_label({'(int)', {free, ResultReg}, '==', ?TERM_EQUALS}, Label, MMod, MSt4),
560+
MSt5 =
561+
if
562+
is_integer(Arg2) ->
563+
{MSt3, Arg1Reg} = MMod:move_to_native_register(MSt2, Arg1),
564+
cond_jump_to_label({{free, Arg1Reg}, '==', Arg2}, Label, MMod, MSt3);
565+
true ->
566+
{MSt3, ResultReg} = MMod:call_primitive(MSt2, ?PRIM_TERM_COMPARE, [
567+
ctx, jit_state, {free, Arg1}, {free, Arg2}, ?TERM_COMPARE_EXACT
568+
]),
569+
MSt4 = handle_error_if(
570+
{'(int)', ResultReg, '==', ?TERM_COMPARE_MEMORY_ALLOC_FAIL}, MMod, MSt3
571+
),
572+
cond_jump_to_label(
573+
{'(int)', {free, ResultReg}, '==', ?TERM_EQUALS}, Label, MMod, MSt4
574+
)
575+
end,
555576
?ASSERT_ALL_NATIVE_FREE(MSt5),
556577
first_pass(Rest3, MMod, MSt5, State0);
557578
% 45

0 commit comments

Comments
 (0)