Skip to content

Commit 2d82829

Browse files
authored
[mono][interp] Lower div.un to shr.un.imm (#83498)
* [mono][interp] Remove redundant opcode Generate sub opcode instead which is detected by other optimizations. * [mono][interp] Lower div.un to shr.un
1 parent d7d637a commit 2d82829

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

src/mono/mono/mini/interp/interp.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5963,11 +5963,6 @@ MINT_IN_CASE(MINT_BRTRUE_I8_SP) ZEROP_SP(gint64, !=); MINT_IN_BREAK;
59635963
ip += 5;
59645964
MINT_IN_BREAK;
59655965
}
5966-
MINT_IN_CASE(MINT_INTRINS_UNSAFE_BYTE_OFFSET) {
5967-
LOCAL_VAR (ip [1], mono_u) = LOCAL_VAR (ip [3], guint8*) - LOCAL_VAR (ip [2], guint8*);
5968-
ip += 4;
5969-
MINT_IN_BREAK;
5970-
}
59715966
MINT_IN_CASE(MINT_INTRINS_RUNTIMEHELPERS_OBJECT_HAS_COMPONENT_SIZE) {
59725967
MonoObject *obj = LOCAL_VAR (ip [2], MonoObject*);
59735968
LOCAL_VAR (ip [1], gint32) = (obj->vtable->flags & MONO_VT_FLAG_ARRAY_OR_STRING) != 0;

src/mono/mono/mini/interp/jiterpreter.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ jiterp_should_abort_trace (InterpInst *ins, gboolean *inside_branch_block)
666666
case MINT_GETITEM_SPAN:
667667
case MINT_GETITEM_LOCALSPAN:
668668
case MINT_INTRINS_SPAN_CTOR:
669-
case MINT_INTRINS_UNSAFE_BYTE_OFFSET:
670669
case MINT_INTRINS_GET_TYPE:
671670
case MINT_INTRINS_MEMORYMARSHAL_GETARRAYDATAREF:
672671
case MINT_CASTCLASS:

src/mono/mono/mini/interp/mintops.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ OPDEF(MINT_INTRINS_GET_HASHCODE, "intrins_get_hashcode", 3, 1, 1, MintOpNoArgs)
824824
OPDEF(MINT_INTRINS_TRY_GET_HASHCODE, "intrins_try_get_hashcode", 3, 1, 1, MintOpNoArgs)
825825
OPDEF(MINT_INTRINS_GET_TYPE, "intrins_get_type", 3, 1, 1, MintOpNoArgs)
826826
OPDEF(MINT_INTRINS_SPAN_CTOR, "intrins_span_ctor", 4, 1, 2, MintOpNoArgs)
827-
OPDEF(MINT_INTRINS_UNSAFE_BYTE_OFFSET, "intrins_unsafe_byte_offset", 4, 1, 2, MintOpNoArgs)
828827
OPDEF(MINT_INTRINS_RUNTIMEHELPERS_OBJECT_HAS_COMPONENT_SIZE, "intrins_runtimehelpers_object_has_component_size", 3, 1, 1, MintOpNoArgs)
829828
OPDEF(MINT_INTRINS_CLEAR_WITH_REFERENCES, "intrin_clear_with_references", 3, 0, 2, MintOpNoArgs)
830829
OPDEF(MINT_INTRINS_MARVIN_BLOCK, "intrins_marvin_block", 3, 0, 2, MintOpNoArgs)

src/mono/mono/mini/interp/transform.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,17 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas
21192119
} else if (!strcmp (tm, "AreSame")) {
21202120
*op = MINT_CEQ_P;
21212121
} else if (!strcmp (tm, "ByteOffset")) {
2122-
*op = MINT_INTRINS_UNSAFE_BYTE_OFFSET;
2122+
#if SIZEOF_VOID_P == 4
2123+
interp_add_ins (td, MINT_SUB_I4);
2124+
#else
2125+
interp_add_ins (td, MINT_SUB_I8);
2126+
#endif
2127+
td->sp -= 2;
2128+
interp_ins_set_sregs2 (td->last_ins, td->sp [1].local, td->sp [0].local);
2129+
push_simple_type (td, STACK_TYPE_I);
2130+
interp_ins_set_dreg (td->last_ins, td->sp [-1].local);
2131+
td->ip += 5;
2132+
return TRUE;
21232133
} else if (!strcmp (tm, "Unbox")) {
21242134
MonoGenericContext *ctx = mono_method_get_context (target_method);
21252135
g_assert (ctx);
@@ -9873,6 +9883,39 @@ interp_super_instructions (TransformData *td)
98739883
dump_interp_inst (new_inst);
98749884
}
98759885
}
9886+
} else if (opcode == MINT_DIV_UN_I4 || opcode == MINT_DIV_UN_I8) {
9887+
// ldc + div.un -> shr.imm
9888+
int sreg_imm = ins->sregs [1];
9889+
InterpInst *def = td->locals [sreg_imm].def;
9890+
if (def != NULL && td->local_ref_count [sreg_imm] == 1) {
9891+
int power2 = -1;
9892+
if (MINT_IS_LDC_I4 (def->opcode)) {
9893+
guint32 ct = interp_get_const_from_ldc_i4 (def);
9894+
power2 = mono_is_power_of_two ((guint32)ct);
9895+
} else if (MINT_IS_LDC_I8 (def->opcode)) {
9896+
guint64 ct = interp_get_const_from_ldc_i8 (def);
9897+
if (ct < G_MAXUINT32)
9898+
power2 = mono_is_power_of_two ((guint32)ct);
9899+
}
9900+
if (power2 > 0) {
9901+
InterpInst *new_inst;
9902+
if (opcode == MINT_DIV_UN_I4)
9903+
new_inst = interp_insert_ins (td, ins, MINT_SHR_UN_I4_IMM);
9904+
else
9905+
new_inst = interp_insert_ins (td, ins, MINT_SHR_UN_I8_IMM);
9906+
new_inst->dreg = ins->dreg;
9907+
new_inst->sregs [0] = ins->sregs [0];
9908+
new_inst->data [0] = power2;
9909+
9910+
interp_clear_ins (def);
9911+
interp_clear_ins (ins);
9912+
local_ref_count [sreg_imm]--;
9913+
if (td->verbose_level) {
9914+
g_print ("lower div.un: ");
9915+
dump_interp_inst (new_inst);
9916+
}
9917+
}
9918+
}
98769919
} else if (MINT_IS_LDIND_INT (opcode)) {
98779920
int sreg_base = ins->sregs [0];
98789921
InterpInst *def = td->locals [sreg_base].def;

src/mono/wasm/runtime/jiterpreter-trace-generator.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,6 @@ export function generate_wasm_body (
613613
builder.callImport("ldtsflda");
614614
break;
615615
}
616-
case MintOpcode.MINT_INTRINS_UNSAFE_BYTE_OFFSET:
617-
builder.local("pLocals");
618-
append_ldloc(builder, getArgU16(ip, 3), WasmOpcode.i32_load);
619-
append_ldloc(builder, getArgU16(ip, 2), WasmOpcode.i32_load);
620-
builder.appendU8(WasmOpcode.i32_sub);
621-
append_stloc_tail(builder, getArgU16(ip, 1), WasmOpcode.i32_store);
622-
break;
623616
case MintOpcode.MINT_INTRINS_GET_TYPE:
624617
builder.block();
625618
// dest, src

0 commit comments

Comments
 (0)