From 5ad6116f20caa5bd67447fef53386ea00e22b1cd Mon Sep 17 00:00:00 2001 From: "Emilio G. Cota" Date: Wed, 14 Mar 2018 10:04:48 -0400 Subject: [PATCH] tcg: allocate optimizer temps with tcg_malloc Groundwork for supporting multiple TCG contexts. While at it, also allocate temps_used directly as a bitmap of the required size, instead of using a bitmap of TCG_MAX_TEMPS via TCGTempSet. Performance-wise we lose about 1.12% in a translation-heavy workload such as booting+shutting down debian-arm: Performance counter stats for 'taskset -c 0 arm-softmmu/qemu-system-arm \ -machine type=virt -nographic -smp 1 -m 4096 \ -netdev user,id=unet,hostfwd=tcp::2222-:22 \ -device virtio-net-device,netdev=unet \ -drive file=die-on-boot.qcow2,id=myblock,index=0,if=none \ -device virtio-blk-device,drive=myblock \ -kernel kernel.img -append console=ttyAMA0 root=/dev/vda1 \ -name arm,debug-threads=on -smp 1' (10 runs): exec time (s) Relative slowdown wrt original (%) --------------------------------------------------------------- original 20.213321616 0. tcg_malloc 20.441130078 1.1270214 TCGContext 20.477846517 1.3086662 g_malloc 20.780527895 2.8061013 The other two alternatives shown in the table are: - TCGContext: embed temps[TCG_MAX_TEMPS] and TCGTempSet used_temps in TCGContext. This is simple enough but it isn't faster than using tcg_malloc; moreover, it wastes memory. - g_malloc: allocate/deallocate both temps and used_temps every time tcg_optimize is executed. Backports commit 34184b071817b4f9edbfd1aa2225c196f05a0947 from qemu --- qemu/include/qemu/bitmap.h | 11 ++++++++++ qemu/tcg/optimize.c | 41 +++++++++++++++++--------------------- qemu/tcg/tcg.h | 4 ---- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/qemu/include/qemu/bitmap.h b/qemu/include/qemu/bitmap.h index 378ed3f9..959dc3bf 100644 --- a/qemu/include/qemu/bitmap.h +++ b/qemu/include/qemu/bitmap.h @@ -70,6 +70,17 @@ static inline unsigned long *bitmap_new(long nbits) return ptr; } + +static inline void bitmap_zero(unsigned long *dst, long nbits) +{ + if (small_nbits(nbits)) { + *dst = 0UL; + } else { + long len = BITS_TO_LONGS(nbits) * sizeof(unsigned long); + memset(dst, 0, len); + } +} + static inline long bitmap_count_one(const unsigned long *bitmap, long nbits) { if (small_nbits(nbits)) { diff --git a/qemu/tcg/optimize.c b/qemu/tcg/optimize.c index c9f8b920..1e46526b 100644 --- a/qemu/tcg/optimize.c +++ b/qemu/tcg/optimize.c @@ -82,22 +82,13 @@ static void reset_temp(TCGContext *s, TCGArg arg) reset_ts(arg_temp(arg)); } -/* Reset all temporaries, given that there are NB_TEMPS of them. */ -static void reset_all_temps(TCGContext *s, int nb_temps) -{ - long len = BITS_TO_LONGS(nb_temps) * sizeof(unsigned long); - memset(&s->temps2_used.l, 0, len); -} - /* Initialize and activate a temporary. */ -static void init_ts_info(TCGContext *s, TCGTemp *ts) +static void init_ts_info(TCGContext *s, struct tcg_temp_info *infos, + TCGTempSet *temps_used, TCGTemp *ts) { - struct tcg_temp_info *temps = s->temps2; - TCGTempSet *temps_used = &s->temps2_used; - size_t idx = temp_idx(s, ts); if (!test_bit(idx, temps_used->l)) { - struct tcg_temp_info *ti = &temps[idx]; + struct tcg_temp_info *ti = &infos[idx]; ts->state_ptr = ti; ti->next_copy = ts; @@ -108,9 +99,10 @@ static void init_ts_info(TCGContext *s, TCGTemp *ts) } } -static void init_arg_info(TCGContext *s, TCGArg arg) +static void init_arg_info(TCGContext *s, struct tcg_temp_info *infos, + TCGTempSet *temps_used, TCGArg arg) { - init_ts_info(s, arg_temp(arg)); + init_ts_info(s, infos, temps_used, arg_temp(arg)); } static TCGTemp *find_better_copy(TCGTemp *ts) @@ -594,6 +586,8 @@ void tcg_optimize(TCGContext *s) { int nb_temps, nb_globals; TCGOp *op, *op_next, *prev_mb = NULL; + struct tcg_temp_info *infos; + TCGTempSet temps_used; /* Array VALS has an element for each temp. If this temp holds a constant then its value is kept in VALS' element. @@ -602,7 +596,8 @@ void tcg_optimize(TCGContext *s) nb_temps = s->nb_temps; nb_globals = s->nb_globals; - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); + infos = tcg_malloc(s, sizeof(struct tcg_temp_info) * nb_temps); QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { tcg_target_ulong mask, partmask, affected; @@ -619,14 +614,14 @@ void tcg_optimize(TCGContext *s) for (i = 0; i < nb_oargs + nb_iargs; i++) { TCGTemp *ts = arg_temp(op->args[i]); if (ts) { - init_ts_info(s, ts); + init_ts_info(s, infos, &temps_used, ts); } } } else { nb_oargs = def->nb_oargs; nb_iargs = def->nb_iargs; for (i = 0; i < nb_oargs + nb_iargs; i++) { - init_arg_info(s, op->args[i]); + init_arg_info(s, infos, &temps_used, op->args[i]); } } @@ -1203,7 +1198,7 @@ void tcg_optimize(TCGContext *s) op->args[1], op->args[2]); if (tmp != 2) { if (tmp) { - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); op->opc = INDEX_op_br; op->args[0] = op->args[3]; } else { @@ -1286,7 +1281,7 @@ void tcg_optimize(TCGContext *s) if (tmp != 2) { if (tmp) { do_brcond_true: - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); op->opc = INDEX_op_br; op->args[0] = op->args[5]; } else { @@ -1302,7 +1297,7 @@ void tcg_optimize(TCGContext *s) /* Simplify LT/GE comparisons vs zero to a single compare vs the high word of the input. */ do_brcond_high: - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); op->opc = INDEX_op_brcond_i32; op->args[0] = op->args[1]; op->args[1] = op->args[3]; @@ -1328,7 +1323,7 @@ void tcg_optimize(TCGContext *s) goto do_default; } do_brcond_low: - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); op->opc = INDEX_op_brcond_i32; op->args[1] = op->args[2]; op->args[2] = op->args[4]; @@ -1433,7 +1428,7 @@ void tcg_optimize(TCGContext *s) if (!(op->args[nb_oargs + nb_iargs + 1] & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) { for (i = 0; i < nb_globals; i++) { - if (test_bit(i, s->temps2_used.l)) { + if (test_bit(i, temps_used.l)) { reset_ts(&s->temps[i]); } } @@ -1448,7 +1443,7 @@ void tcg_optimize(TCGContext *s) block, otherwise we only trash the output args. "mask" is the non-zero bits mask for the first output arg. */ if (def->flags & TCG_OPF_BB_END) { - reset_all_temps(s, nb_temps); + bitmap_zero(temps_used.l, nb_temps); } else { do_reset_output: for (i = 0; i < nb_oargs; i++) { diff --git a/qemu/tcg/tcg.h b/qemu/tcg/tcg.h index 19c4f3ec..c510bb46 100644 --- a/qemu/tcg/tcg.h +++ b/qemu/tcg/tcg.h @@ -851,10 +851,6 @@ struct TCGContext { int indirect_reg_alloc_order[50]; TCGOpDef *tcg_op_defs; - /* qemu/tcg/optimize.c */ - struct tcg_temp_info temps2[TCG_MAX_TEMPS]; - TCGTempSet temps2_used; - /* qemu/target-m68k/translate.c */ TCGv_i32 cpu_halted; char cpu_reg_names[2 * 8 * 3 + 5 * 4];