8000 py/emit: Remove logic to detect last-emit-was-return-value. · micropython/micropython@e85a096 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e85a096

Browse files
committed
py/emit: Remove logic to detect last-emit-was-return-value.
This optimisation to remove dead code is not as good as it could be. Signed-off-by: Damien George <damien@micropython.org>
1 parent 0db046b commit e85a096

File tree

5 files changed

+16
-48
lines changed

5 files changed

+16
-48
lines changed

py/compile.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,12 +1330,8 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13301330
goto done;
13311331
}
13321332

1333-
if (
1334-
// optimisation: don't jump over non-existent elif/else blocks
1335-
!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))
1336-
// optimisation: don't jump if last instruction was return
1337-
&& !EMIT(last_emit_was_return_value)
1338-
) {
1333+
// optimisation: don't jump over non-existent elif/else blocks
1334+
if (!(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3]))) {
13391335
// jump over elif/else blocks
13401336
EMIT_ARG(jump, l_end);
13411337
}
@@ -1362,10 +1358,7 @@ STATIC void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
13621358
goto done;
13631359
}
13641360

1365-
// optimisation: don't jump if last instruction was return
1366-
if (!EMIT(last_emit_was_return_value)) {
1367-
EMIT_ARG(jump, l_end);
1368-
}
1361+
EMIT_ARG(jump, l_end);
13691362
EMIT_ARG(label_assign, l_fail);
13701363
}
13711364
}
@@ -1580,9 +1573,7 @@ STATIC void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
15801573
EMIT_ARG(for_iter, pop_label);
15811574
c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
15821575
compile_node(comp, pns->nodes[2]); // body
1583-
if (!EMIT(last_emit_was_return_value)) {
1584-
EMIT_ARG(jump, continue_label);
1585-
}
1576+
EMIT_ARG(jump, continue_label);
15861577
EMIT_ARG(label_assign, pop_label);
15871578
EMIT(for_iter_end);
15881579

@@ -3048,11 +3039,8 @@ STATIC bool compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
30483039
}
30493040

30503041
compile_node(comp, pns->nodes[3]); // 3 is function body
3051-
// emit return if it wasn't the last opcode
3052-
if (!EMIT(last_emit_was_return_value)) {
3053-
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3054-
EMIT(return_value);
3055-
}
3042+
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
3043+
EMIT(return_value);
30563044
} else if (scope->kind == SCOPE_LAMBDA) {
30573045
assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
30583046
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t *)scope->pn;

py/emit.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ typedef struct _emit_method_table_t {
115115

116116
void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
117117
bool (*end_pass)(emit_t *emit);
118-
bool (*last_emit_was_return_value)(emit_t *emit);
119118
void (*adjust_stack_size)(emit_t *emit, mp_int_t delta);
120119
void (*set_source_line)(emit_t *emit, mp_uint_t line);
121120

@@ -227,7 +226,6 @@ void emit_native_xtensawin_free(emit_t *emit);
227226

228227
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope);
229228
bool mp_emit_bc_end_pass(emit_t *emit);
230-
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit);
231229
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta);
232230
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line);
233231

py/emitbc.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ struct _emit_t {
4747
byte dummy_data[DUMMY_DATA_SIZE];
4848

4949
pass_kind_t pass : 8;
50-
mp_uint_t last_emit_was_return_value : 8;
5150

5251
int stack_size;
5352

@@ -272,7 +271,6 @@ STATIC void emit_write_bytecode_byte_label(emit_t *emit, int stack_adj, byte b1,
272271
void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
273272
emit->pass = pass;
274273
emit->stack_size = 0;
275-
emit->last_emit_was_return_value = false;
276274
emit->scope = scope;
277275
emit->last_source_line_offset = 0;
278276
emit->last_source_line = 1;
@@ -397,10 +395,6 @@ bool mp_emit_bc_end_pass(emit_t *emit) {
397395
return true;
398396
}
399397

400-
bool mp_emit_bc_last_emit_was_return_value(emit_t *emit) {
401-
return emit->last_emit_was_return_value;
402-
}
403-
404398
void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
405399
if (emit->pass == MP_PASS_SCOPE) {
406400
return;
@@ -410,7 +404,6 @@ void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta) {
410404
if (emit->stack_size > emit->scope->stack_size) {
411405
emit->scope->stack_size = emit->stack_size;
412406
}
413-
emit->last_emit_was_return_value = false;
414407
}
415408

416409
void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t source_line) {
@@ -773,7 +766,6 @@ void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_ke
773766

774767
void mp_emit_bc_return_value(emit_t *emit) {
775768
emit_write_bytecode_byte(emit, -1, MP_BC_RETURN_VALUE);
776-
emit->last_emit_was_return_value = true;
777769
}
778770

779771
void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args) {
@@ -806,7 +798,6 @@ const emit_method_table_t emit_bc_method_table = {
806798

807799
mp_emit_bc_start_pass,
808800
mp_emit_bc_end_pass,
809-
mp_emit_bc_last_emit_was_return_value,
810801
mp_emit_bc_adjust_stack_size,
811802
mp_emit_bc_set_source_line,
812803

py/emitnative.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ struct _emit_t {
276276
uint16_t n_info;
277277
uint16_t n_cell;
278278

279-
bool last_emit_was_return_value;
280-
281279
scope_t *scope;
282280

283281
ASM_T *as;
@@ -370,7 +368,6 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
370368
emit->pass = pass;
371369
emit->do_viper_types = scope->emit_options == MP_EMIT_OPT_VIPER;
372370
emit->stack_size = 0;
373-
emit->last_emit_was_return_value = false;
374371
emit->scope = scope;
375372

376373
// allocate memory for keeping track of the types of locals
@@ -733,10 +730,6 @@ STATIC bool emit_native_end_pass(emit_t *emit) {
733730
return true;
734731
}
735732

736-
STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
737-
return emit->last_emit_was_return_value;
738-
}
739-
740733
STATIC void ensure_extra_stack(emit_t *emit, size_t delta) {
741734
if (emit->stack_size + delta > emit->stack_info_alloc) {
742735
size_t new_alloc = (emit->stack_size + delta + 8) & ~3;
@@ -793,7 +786,7 @@ STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
793786

794787
// this must be called at start of emit functions
795788
STATIC void emit_native_pre(emit_t *emit) {
796-
emit->last_emit_was_return_value = false;
789+
(void)emit;
797790
}
798791

799792
// depth==0 is top, depth==1 is before top, etc
@@ -917,7 +910,6 @@ STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
917910
// If stacked value is in a register and the register is not r1 or r2, then
918911
// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
919912
STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
920-
emit->last_emit_was_return_value = false;
921913
stack_info_t *si = peek_stack(emit, 0);
922914
if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
923915
*vtype = si->vtype;
@@ -930,12 +922,10 @@ STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *re
930922
}
931923

932924
STATIC void emit_pre_pop_discard(emit_t *emit) {
933-
emit->last_emit_was_return_value = false;
934925
adjust_stack(emit, -1);
935926
}
936927

937928
STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
938-
emit->last_emit_was_return_value = false;
939929
emit_access_stack(emit, 1, vtype, reg_dest);
940930
adjust_stack(emit, -1);
941931
}
@@ -2771,7 +2761,6 @@ STATIC void emit_native_return_value(emit_t *emit) {
27712761

27722762
// Do the unwinding jump to get to the return handler
27732763
emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
2774-
emit->last_emit_was_return_value = true;
27752764
return;
27762765
}
27772766

@@ -2809,7 +2798,6 @@ STATIC void emit_native_return_value(emit_t *emit) {
28092798
ASM_MOV_LOCAL_REG(emit->as, LOCAL_IDX_RET_VAL(emit), REG_PARENT_RET);
28102799
}
28112800
emit_native_unwind_jump(emit, emit->exit_label, emit->exc_stack_size);
2812-
emit->last_emit_was_return_value = true;
28132801
}
28142802

28152803
STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
@@ -2928,7 +2916,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
29282916

29292917
emit_native_start_pass,
29302918
emit_native_end_pass,
2931-
emit_native_last_emit_was_return_value,
29322919
emit_native_adjust_stack_size,
29332920
emit_native_set_source_line,
29342921

tests/cmdline/cmd_showbc.py.exp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ arg names:
4848
43 LOAD_CONST_NONE
4949
44 RETURN_VALUE
5050
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 45\[46\] bytes)
51-
Raw bytecode (code_info_size=8\[46\], bytecode_size=370):
51+
Raw bytecode (code_info_size=8\[46\], bytecode_size=372):
5252
a8 12 9\[bf\] 03 05 60 60 26 22 24 64 22 24 25 25 24
5353
26 23 63 22 22 25 23 23 2f 6c 25 65 25 25 69 68
5454
26 65 27 6a 62 20 23 62 2a 29 69 24 25 28 67 26
5555
########
56-
\.\+81 63
56+
\.\+51 63
5757
arg names:
5858
(N_STATE 22)
5959
(N_EXC_STACK 2)
@@ -403,6 +403,8 @@ arg names:
403403
367 RETURN_VALUE
404404
368 LOAD_CONST_SMALL_INT 1
405405
369 RETURN_VALUE
406+
370 LOAD_CONST_NONE
407+
371 RETURN_VALUE
406408
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 59 bytes)
407409
Raw bytecode (code_info_size=8, bytecode_size=51):
408410
a8 10 0a 05 80 82 34 38 81 57 c0 57 c1 57 c2 57
@@ -621,9 +623,9 @@ arg names: *
621623
08 DELETE_DEREF 0
622624
10 LOAD_CONST_NONE
623625
11 RETURN_VALUE
624-
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 13 bytes)
625-
Raw bytecode (code_info_size=8, bytecode_size=5):
626-
9a 01 0a 05 03 08 80 8b b1 25 00 f2 63
626+
File cmdline/cmd_showbc.py, code block 'f' (descriptor: \.\+, bytecode @\.\+ 15 bytes)
627+
Raw bytecode (code_info_size=8, bytecode_size=7):
628+
9a 01 0a 05 03 08 80 8b b1 25 00 f2 63 51 63
627629
arg names: * b
628630
(N_STATE 4)
629631
(N_EXC_STACK 0)
@@ -633,6 +635,8 @@ arg names: * b
633635
01 LOAD_DEREF 0
634636
03 BINARY_OP 27 __add__
635637
04 RETURN_VALUE
638+
05 LOAD_CONST_NONE
639+
06 RETURN_VALUE
636640
mem: total=\\d\+, current=\\d\+, peak=\\d\+
637641
stack: \\d\+ out of \\d\+
638642
GC: total: \\d\+, used: \\d\+, free: \\d\+

0 commit comments

Comments
 (0)
0