From 1f0db5ddd6784b2e7e9267bd87feda034d1bdae7 Mon Sep 17 00:00:00 2001 From: EgorBo Date: Wed, 28 Oct 2020 16:54:45 +0300 Subject: [PATCH 1/2] Fold "ldstr".Length to a const --- src/mono/mono/mini/interp/transform.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index f6376ba352c9a1..d3f49530592ade 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -1629,8 +1629,18 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas if (tm [0] == 'g') { if (strcmp (tm, "get_Chars") == 0) *op = MINT_GETCHR; - else if (strcmp (tm, "get_Length") == 0) + else if (strcmp(tm, "get_Length") == 0) { + // try to fold "ldstr".get_Length into a constant + if (td->last_ins && td->last_ins->opcode == MINT_LDSTR && interp_ip_in_cbb(td, td->ip - td->il_code)) { + MonoString* str = (MonoString*)td->data_items[td->last_ins->data[0]]; + g_assert(str && str->length >= 0); + interp_clear_ins(td->last_ins); + interp_get_ldc_i4_from_const(td, NULL, str->length); + td->ip += 5; + return TRUE; + } *op = MINT_STRLEN; + } } } else if (type_index >= 0) { return interp_handle_magic_type_intrinsics (td, target_method, csignature, type_index); From fef6f6d80f378778cd3dbfc1e79d4a4715689fd3 Mon Sep 17 00:00:00 2001 From: Egor Bogatov Date: Thu, 29 Oct 2020 14:58:27 +0300 Subject: [PATCH 2/2] Update transform.c --- src/mono/mono/mini/interp/transform.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mono/mono/mini/interp/transform.c b/src/mono/mono/mini/interp/transform.c index d3f49530592ade..79e621d43b297a 100644 --- a/src/mono/mono/mini/interp/transform.c +++ b/src/mono/mono/mini/interp/transform.c @@ -1636,6 +1636,7 @@ interp_handle_intrinsics (TransformData *td, MonoMethod *target_method, MonoClas g_assert(str && str->length >= 0); interp_clear_ins(td->last_ins); interp_get_ldc_i4_from_const(td, NULL, str->length); + SET_SIMPLE_TYPE(td->sp - 1, STACK_TYPE_I4); td->ip += 5; return TRUE; }