Perform actual constant folding for ADD, SUB and MUL operations. Signed-off-by: Kirill Batuzov <batuz...@ispras.ru> --- tcg/optimize.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 102 insertions(+), 0 deletions(-)
diff --git a/tcg/optimize.c b/tcg/optimize.c index a761c51..4073f05 100644 --- a/tcg/optimize.c +++ b/tcg/optimize.c @@ -82,6 +82,79 @@ static void reset_temp(tcg_temp_state *state, tcg_target_ulong *vals, state[temp] = TCG_TEMP_ANY; } +static int op_bits(int op) +{ + switch (op) { + case INDEX_op_mov_i32: + case INDEX_op_add_i32: + case INDEX_op_sub_i32: + case INDEX_op_mul_i32: + return 32; +#if TCG_TARGET_REG_BITS == 64 + case INDEX_op_mov_i64: + case INDEX_op_add_i64: + case INDEX_op_sub_i64: + case INDEX_op_mul_i64: + return 64; +#endif + default: + fprintf(stderr, "Unrecognized operation %d in op_bits.\n", op); + tcg_abort(); + } +} + +static int op_to_movi(int op) +{ + if (op_bits(op) == 32) { + return INDEX_op_movi_i32; + } +#if TCG_TARGET_REG_BITS == 64 + if (op_bits(op) == 64) { + return INDEX_op_movi_i64; + } +#endif + tcg_abort(); +} + +static TCGArg do_constant_folding_2(int op, TCGArg x, TCGArg y) +{ + switch (op) { + case INDEX_op_add_i32: +#if TCG_TARGET_REG_BITS == 64 + case INDEX_op_add_i64: +#endif + return x + y; + + case INDEX_op_sub_i32: +#if TCG_TARGET_REG_BITS == 64 + case INDEX_op_sub_i64: +#endif + return x - y; + + case INDEX_op_mul_i32: +#if TCG_TARGET_REG_BITS == 64 + case INDEX_op_mul_i64: +#endif + return x * y; + + default: + fprintf(stderr, + "Unrecognized operation %d in do_constant_folding.\n", op); + tcg_abort(); + } +} + +static TCGArg do_constant_folding(int op, TCGArg x, TCGArg y) +{ + TCGArg res = do_constant_folding_2(op, x, y); +#if TCG_TARGET_REG_BITS == 64 + if (op_bits(op) == 32) { + res &= 0xffffffff; + } +#endif + return res; +} + /* Propagate constants and copies, fold constant expressions. */ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, TCGArg *args, TCGOpDef *tcg_op_defs) @@ -164,6 +237,35 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, gen_args += 2; args += 2; break; + case INDEX_op_add_i32: + case INDEX_op_sub_i32: + case INDEX_op_mul_i32: +#if TCG_TARGET_REG_BITS == 64 + case INDEX_op_add_i64: + case INDEX_op_sub_i64: + case INDEX_op_mul_i64: +#endif + if (state[args[1]] == TCG_TEMP_CONST + && state[args[2]] == TCG_TEMP_CONST) { + gen_opc_buf[op_index] = op_to_movi(op); + gen_args[0] = args[0]; + gen_args[1] = + do_constant_folding(op, vals[args[1]], vals[args[2]]); + reset_temp(state, vals, gen_args[0], nb_temps, nb_globals); + state[gen_args[0]] = TCG_TEMP_CONST; + vals[gen_args[0]] = gen_args[1]; + gen_args += 2; + args += 3; + break; + } else { + reset_temp(state, vals, args[0], nb_temps, nb_globals); + gen_args[0] = args[0]; + gen_args[1] = args[1]; + gen_args[2] = args[2]; + gen_args += 3; + args += 3; + break; + } case INDEX_op_call: case INDEX_op_jmp: case INDEX_op_br: -- 1.7.4.1