From 95592de58413f0c1255b007a7a28b83bbde72e7f Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Thu, 22 Feb 2024 20:35:45 +0000 Subject: [PATCH] Allow jit_do_cprop to copy arbitrary values --- src/jit/jit-optim.c | 20 ++++++++++++++++++-- test/test_jit.c | 12 +++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/jit/jit-optim.c b/src/jit/jit-optim.c index ec7803a2..88c32792 100644 --- a/src/jit/jit-optim.c +++ b/src/jit/jit-optim.c @@ -1022,9 +1022,25 @@ void jit_do_cprop(jit_func_t *f) ir->arg2 = copy; } - if (ir->op == J_MOV && ir->arg1.kind == JIT_VALUE_INT64) + if (ir->result == JIT_REG_INVALID) + continue; + else if (ir->op == MACRO_MEMSET || ir->op == MACRO_COPY + || ir->op == MACRO_MOVE || ir->op == MACRO_BZERO) + continue; // Does not write to result + else if (map[ir->result].kind != JIT_VALUE_INVALID) { + // Invalidate any existing copies of this register + for (int j = 0; j < f->nregs; j++) { + if (map[j].kind == JIT_VALUE_REG && map[j].reg == ir->result) + map[j].kind = JIT_VALUE_INVALID; + } + } + + if (ir->op == J_MOV) { map[ir->result] = ir->arg1; - else if (ir->result != JIT_REG_INVALID) + if (ir->arg1.kind == JIT_VALUE_REG) + map[ir->arg1.reg] = ir->arg1; + } + else map[ir->result].kind = JIT_VALUE_INVALID; } } diff --git a/test/test_jit.c b/test/test_jit.c index dfb0b8fb..c93997de 100644 --- a/test/test_jit.c +++ b/test/test_jit.c @@ -2004,17 +2004,19 @@ START_TEST(test_cprop2) const char *text1 = " MOV R1, R0 \n" + " MOV R2, R1 \n" " ADD R0, R0, #1 \n" " ADD R1, R1, #1 \n"; jit_handle_t h1 = jit_assemble(j, ident_new("myfunc1"), text1); - jit_func_t *f = jit_get_func(j, h1); - jit_do_cprop(f); + jit_func_t *f1 = jit_get_func(j, h1); + jit_do_cprop(f1); - check_unary(f, 0, J_MOV, REG(0)); - check_binary(f, 1, J_ADD, REG(0), CONST(1)); - check_binary(f, 2, J_ADD, REG(1), CONST(1)); + check_unary(f1, 0, J_MOV, REG(0)); + check_unary(f1, 1, J_MOV, REG(0)); + check_binary(f1, 2, J_ADD, REG(0), CONST(1)); + check_binary(f1, 3, J_ADD, REG(1), CONST(1)); jit_free(j); } -- 2.39.2