tcg: optimize across branches
cover tcg: optimize across branches
In several cases, it's easy to optimize across a non-taken branch simply by *not* flushing the relevant tables. This is true both for value propagation and register allocation.
This comes up in quite a number of cases with arm, most simply in how conditional execution is implemented. But it also came up in discussion of how to implement low-overhead looping for v8.1m.
r~
Richard Henderson (2):
tcg: Do not kill globals at conditional branches tcg/optimize: Flush data at labels not TCG_OPF_BB_END
include/tcg/tcg-opc.h | 7 +++--- include/tcg/tcg.h | 4 +++- tcg/optimize.c | 35 ++++++++++++++------------- tcg/tcg.c | 55 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 23 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 tcg: Do not kill globals at conditional branches
We can easily register allocate the entire extended basic block (in this case, the set of blocks connected by fallthru), simply by not discarding the register state at the branch.
This does not help blocks starting with a label, as they are reached via a taken branch, and that would require saving the complete register state at the branch.
--- include/tcg/tcg-opc.h | 7 +++--- include/tcg/tcg.h | 4 +++- tcg/tcg.c | 55 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-)
include/tcg/tcg-opc.h +4 -3
@@ -81,7 +81,7 @@ DEF(extract_i32, 1, 1, 2, IMPL(TCG_TARGET_HAS_extract_i32)) DEF(sextract_i32, 1, 1, 2, IMPL(TCG_TARGET_HAS_sextract_i32)) DEF(extract2_i32, 1, 2, 1, IMPL(TCG_TARGET_HAS_extract2_i32)) -DEF(brcond_i32, 0, 2, 2, TCG_OPF_BB_END) +DEF(brcond_i32, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_COND_BRANCH) DEF(add2_i32, 2, 4, 0, IMPL(TCG_TARGET_HAS_add2_i32))DEF (sub2_i32,2 ,4 ,0 ,IMPL (TCG_TARGET_HAS_sub2_i32))
@@ -89,7 +89,8 @@ DEF(mulu2_i32, 2, 2, 0, IMPL(TCG_TARGET_HAS_mulu2_i32)) DEF(muls2_i32, 2, 2, 0, IMPL(TCG_TARGET_HAS_muls2_i32)) DEF(muluh_i32, 1, 2, 0, IMPL(TCG_TARGET_HAS_muluh_i32)) DEF(mulsh_i32, 1, 2, 0, IMPL(TCG_TARGET_HAS_mulsh_i32)) -DEF(brcond2_i32, 0, 4, 2, TCG_OPF_BB_END | IMPL(TCG_TARGET_REG_BITS == 32)) +DEF (brcond2_i32,0 ,4 ,2 , + TCG_OPF_BB_END | TCG_OPF_COND_BRANCH | IMPL(TCG_TARGET_REG_BITS == 32)) DEF(setcond2_i32, 1, 4, 1, IMPL(TCG_TARGET_REG_BITS == 32))DEF (ext8s_i32,1 ,1 ,0 ,IMPL (TCG_TARGET_HAS_ext8s_i32))
@@ -159,7 +160,7 @@ DEF(extrh_i64_i32, 1, 1, 0, IMPL(TCG_TARGET_HAS_extrh_i64_i32) | (TCG_TARGET_REG_BITS == 32 ? TCG_OPF_NOT_PRESENT : 0)) -DEF(brcond_i64, 0, 2, 2, TCG_OPF_BB_END | IMPL64) +DEF(brcond_i64, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_COND_BRANCH | IMPL64) DEF(ext8s_i64, 1, 1, 0, IMPL64 | IMPL(TCG_TARGET_HAS_ext8s_i64)) DEF(ext16s_i64, 1, 1, 0, IMPL64 | IMPL(TCG_TARGET_HAS_ext16s_i64))DEF (ext32s_i64,1 ,1 ,0 ,IMPL64 |IMPL (TCG_TARGET_HAS_ext32s_i64))
include/tcg/tcg.h +3 -1
@@ -990,7 +990,7 @@ typedef struct TCGArgConstraint {#define TCG_MAX_OP_ARGS 16 -/* Bits for TCGOpDef->flags, 8 bits available. */ +/* Bits for TCGOpDef->flags, 8 bits available, all used. */ enum {/* Instruction exits the translation block. */ TCG_OPF_BB_EXIT =0x01 ,
@@ -1008,6 +1008,8 @@ enum {TCG_OPF_NOT_PRESENT =0x20 ,/* Instruction operands are vectors. */ TCG_OPF_VECTOR =0x40 , +/* Instruction is a conditional branch. */ +TCG_OPF_COND_BRANCH =0x80 }; typedef struct TCGOpDef {
tcg/tcg.c +53 -2
85 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 1/2] tcg: Do not kill globals at conditional branches
Quoted 15 lines · Richard Henderson
We can easily register allocate the entire extended basic block (in this case, the set of blocks connected by fallthru), simply by not discarding the register state at the branch.
This does not help blocks starting with a label, as they are reached via a taken branch, and that would require saving the complete register state at the branch.
Signed-off-by Richard Henderson <[email protected]>--- include/tcg/tcg-opc.h | 7 +++--- include/tcg/tcg.h | 4 +++- tcg/tcg.c | 55 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-)
<snip>
Quoted 5 lines
tcg/tcg.c +4
-0+/* +* liveness analysis: conditional branch: all temps are dead, +* globals and local temps should be synced. +*/
tcg/tcg.c static void la_global_sync(TCGContext *s, int ng) · line 2535
Quoted 10 lines
tcg/tcg.c +10
-0+static void la_bb_sync (TCGContext *s,int ng,int nt) +{ +la_global_sync (s, ng); + +for (int i = ng; i < nt; ++i) { +if (s->temps[i].temp_local) { +int state = s->temps[i].state; + s->temps[i].state = state |TS_MEM ; +if (state !=TS_DEAD ) { +continue ;
It took me a few scans of this function before I realised the continue was to avoid the la_reset_pref at the end. Not sure if it can be made any neater though:
Signature · 1 line
Alex Bennée
PATCH 2/2 tcg/optimize: Flush data at labels not TCG_OPF_BB_END
In reply to Richard HendersonWe can easily propagate temp values through the entire extended basic block (in this case, the set of blocks connected by fallthru), simply by not discarding the register state at the branch.
--- tcg/optimize.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
tcg/optimize.c +18 -17
@@ -1484,29 +1484,30 @@ void tcg_optimize(TCGContext *s) } } } - goto do_reset_output; +/* fall through */ default : do_default: -/* Default case: we know nothing about operation (or were unable -to compute the operation result) so no propagation is done. -We trash everything if the operation is the end of a basic -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 ) { -bitmap_zero (temps_used.l, nb_temps); - }else { - do_reset_output: -for (i =0 ; i < nb_oargs; i++) { -reset_temp (op->args[i]); -/* Save the corresponding known-zero bits mask for the -first output argument (only one supported so far). */ -if (i ==0 ) { -arg_info (op->args[i])->mask = mask; - } +/* +* Default case: we know nothing about operation (or were unable +* to compute the operation result) so no propagation is done. +*/ +for (i =0 ; i < nb_oargs; i++) { +reset_temp (op->args[i]); +/* +* Save the corresponding known-zero bits mask for the +* first output argument (only one supported so far). +*/ +if (i ==0 ) { +arg_info (op->args[i])->mask = mask; } }break ; + +case INDEX_op_set_label: +/* Trash everything at the start of a new extended bb. */ +bitmap_zero (temps_used.l, nb_temps); +break ; }/* Eliminate duplicate and redundant fence instructions. */
Signature · 1 line
2.25.1
Re: [PATCH 2/2] tcg/optimize: Flush data at labels not TCG_OPF_BB_END
Quoted 14 lines · Richard Henderson
We can easily propagate temp values through the entire extended basic block (in this case, the set of blocks connected by fallthru), simply by not discarding the register state at the branch.
Signed-off-by Richard Henderson <[email protected]>--- tcg/optimize.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)tcg/optimize.c +0
-0
tcg/optimize.c void tcg_optimize(TCGContext *s) · line 1490
Quoted 8 lines
tcg/optimize.c +1
-1@@ -1484,29 +1484,30 @@ void tcg_optimize(TCGContext *s) } } } - goto do_reset_output; +/* fall through */ default : do_default:
A random aside:
The optimize function has a lot of goto's in it and I find generally hard to follow. Anyway:
Signature · 1 line
Alex Bennée
Re: [PATCH 2/2] tcg/optimize: Flush data at labels not TCG_OPF_BB_END
Quoted 2 lines · Alex Bennée
The optimize function has a lot of goto's in it and I find generally hard to follow. Anyway:
Yes. We'll call it inherited code that could use a re-factor.
r~
Ping.
Quoted 22 lines · Richard Henderson
In several cases, it's easy to optimize across a non-taken branch simply by *not* flushing the relevant tables. This is true both for value propagation and register allocation.
This comes up in quite a number of cases with arm, most simply in how conditional execution is implemented. But it also came up in discussion of how to implement low-overhead looping for v8.1m.
r~
Richard Henderson (2):
tcg: Do not kill globals at conditional branches tcg/optimize: Flush data at labels notTCG_OPF_BB_ENDinclude/tcg/tcg-opc.h | 7 +++--- include/tcg/tcg.h | 4 +++- tcg/optimize.c | 35 ++++++++++++++------------- tcg/tcg.c | 55 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 23 deletions(-)