Skip to contentSkip to conversation

target/arm: Use tcg_gen_goto_jc* for aarch64

PATCH In review Richard Henderson 2 participants 18 messages Oct 30 – Nov 27

lore
Share & tools
lore
#0 lore raw

cover target/arm: Use tcg_gen_goto_jc* for aarch64

Based-on: [email protected] ("[PATCH v7 00/14] accel/tcg: cut per-block dispatch overhead (parts 4-6)")

Because btype is in tbflags, using goto_jc in aarch64 is more complicated, and requires accurately tracking changes to tbflags.

r~

Richard Henderson (9):
target/arm: Add tbflags to DisasContext target/arm: Retain btype in DisasContext.tbflags target/arm: Add second value to DisasCompare64 target/arm: Split out gen_goto_tb_int target/arm: Split out gen_indirect_branch target/arm: Split out gen_cbranch_goto_tb target/arm: Use DISAS_TOO_MANY for simple TB breaks target/arm: Rework gen_goto_tb to gen_direct_branch target/arm: Use tcg_gen_goto_jc3_i64 for aa64 branches

 target/arm/tcg/translate.h     |  16 +-
 target/arm/tcg/translate-a64.c | 400 +++++++++++++++++----------------
 target/arm/tcg/translate.c     |  60 ++---
 3 files changed, 251 insertions(+), 225 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/9 target/arm: Add tbflags to DisasContext

Prepare to track changes to tbflags across the TB.

---
 target/arm/tcg/translate.h     |  9 ++++
 target/arm/tcg/translate-a64.c | 95 ++++++++++++++++++----------------
 target/arm/tcg/translate.c     | 60 ++++++++++-----------
 3 files changed, 89 insertions(+), 75 deletions(-)
target/arm/tcg/translate.h +9 -0
index 57e45ea872..b9254d5524 100644
@@ -75,6 +75,15 @@ typedef struct DisasContext {
     bool eci_handled;
     int sctlr_b;
     MemOp be_data;
+
+    /*
+     * Current tbflags.  During translation, starts as tb->flags
+     * but updated for known changes to cpu state.  If at some
+     * point we encounter some unknowable change, set tbflags_invalid.
+     */
+    CPUARMTBFlags tbflags;
+    bool tbflags_invalid;
+
 #if !defined(CONFIG_USER_ONLY)
     int user;
 #endif
target/arm/tcg/translate-a64.c +49 -46

131 lines not on this page · raw message

target/arm/tcg/translate.c +31 -29

95 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/9 target/arm: Retain btype in DisasContext.tbflags

In reply to Richard Henderson

Replace btype = -1 with btype_set = true. This is both less confusing and keeps the state separate from the per-insn iteration. Set tbflags_invalid when we need to read the btype from the page tables and therefore we cannot keep DisasContext.tbflags up to date.

---
 target/arm/tcg/translate.h     |  7 ++-----
 target/arm/tcg/translate-a64.c | 28 ++++++++++++----------------
 2 files changed, 14 insertions(+), 21 deletions(-)
target/arm/tcg/translate.h +2 -5
index b9254d5524..029107f4c0 100644
@@ -206,11 +206,8 @@ typedef struct DisasContext {
     bool gcs_rvcen;
     /* GCSSTR exception EL or 0 if enabled */
     uint8_t gcsstr_el;
-    /*
-     * >= 0, a copy of PSTATE.BTYPE, which will be 0 without v8.5-BTI.
-     *  < 0, set by the current instruction.
-     */
-    int8_t btype;
+    /* True if the current insn set PSTATE.BTYPE. */
+    bool btype_set;
     /* A copy of DCZID_EL0.BS. */
     uint8_t dcz_blocksize;
     /* A copy of cpu->gm_blocksize. */
target/arm/tcg/translate-a64.c +12 -16
index d8b6c1df41..64e28a089a 100644
@@ -173,14 +173,15 @@ static void set_btype(DisasContext *s, int val)
     /* BTYPE is a 2-bit field, and 0 should be done with reset_btype.  */
     tcg_debug_assert(val >= 1 && val <= 3);
     set_btype_raw(val);
-    s->btype = -1;
+    DP_TBFLAG_A64(s->tbflags, BTYPE, val);
+    s->btype_set = true;
 }
 
 static void reset_btype(DisasContext *s)
 {
-    if (s->btype != 0) {
+    if (EX_TBFLAG_A64(s->tbflags, BTYPE) != 0) {
         set_btype_raw(0);
-        s->btype = 0;
+        DP_TBFLAG_A64(s->tbflags, BTYPE, 0);
     }
 }
 
@@ -1894,7 +1895,8 @@ static void set_btype_for_br(DisasContext *s, int rn)
             TCGv_i64 pc = tcg_temp_new_i64();
             gen_pc_plus_diff(s, pc, 0);
             gen_helper_guarded_page_br(tcg_env, pc);
-            s->btype = -1;
+            s->btype_set = true;
+            s->tbflags_invalid = true;
         }
     }
 }
@@ -11044,7 +11046,6 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
     dc->max_any_vl = MAX(dc->max_svl, arm_cpu->sve_max_vq * 16);
     dc->pauth_active = EX_TBFLAG_A64(dc->tbflags, PAUTH_ACTIVE);
     dc->bt = EX_TBFLAG_A64(dc->tbflags, BT);
-    dc->btype = EX_TBFLAG_A64(dc->tbflags, BTYPE);
     dc->unpriv = EX_TBFLAG_A64(dc->tbflags, UNPRIV);
     dc->ata[0] = EX_TBFLAG_A64(dc->tbflags, ATA);
     dc->ata[1] = EX_TBFLAG_A64(dc->tbflags, ATA0);
@@ -11182,10 +11183,9 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
     }
 
     if (dc_isar_feature(aa64_bti, s)) {
+        int btype = EX_TBFLAG_A64(s->tbflags, BTYPE);
+        tcg_debug_assert(!s->btype_set);
         if (s->base.num_insns == 1) {
-            /* First insn can have btype set to non-zero.  */
-            tcg_debug_assert(s->btype >= 0);
-
             /*
              * Note that the Branch Target Exception has fairly high
              * priority -- below debugging exceptions but above most
@@ -11195,13 +11195,12 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
              * We can check all but the guarded page check here;
              * defer the latter to a helper.
              */
-            if (s->btype != 0
-                && !btype_destination_ok(insn, s->bt, s->btype)) {
+            if (btype && !btype_destination_ok(insn, s->bt, btype)) {
                 gen_helper_guarded_page_check(tcg_env);
             }
         } else {
             /* Not the first insn: btype must be 0.  */
-            tcg_debug_assert(s->btype == 0);
+            tcg_debug_assert(btype == 0);
         }
     }
 
@@ -11216,11 +11215,8 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
         unallocated_encoding(s);
     }
 
-    /*
-     * After execution of most insns, btype is reset to 0.
-     * Note that we set btype == -1 when the insn sets btype.
-     */
-    if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
+    /* After execution of most insns, btype is reset to 0. */
+    if (!s->btype_set && s->base.is_jmp != DISAS_NORETURN) {
         reset_btype(s);
     }
 }
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH 2/9] target/arm: Retain btype in DisasContext.tbflags

Quoted 9 lines · Richard Henderson

Replace btype = -1 with btype_set = true. This is both less confusing and keeps the state separate from the per-insn iteration. Set tbflags_invalid when we need to read the btype from the page tables and therefore we cannot keep DisasContext.tbflags up to date.

---

This looks OK, but how can we know when a TBFLAGS flag is like btype (must be kept in s->tbflags and update s->tbflags_invalid if we emit something that changes it) versus it being like most of the TBFLAGS (OK to extract and keep in a more convenient form in some other s->whatever field, no need to worry about updating s->tbflags) ?

thanks
-- PMM

#4 lore raw

PATCH 3/9 target/arm: Add second value to DisasCompare64

In reply to Richard Henderson

Do not assume zero for the second value.
But do set it to zero in a64_test_cc.

---
 target/arm/tcg/translate-a64.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)
target/arm/tcg/translate-a64.c +9 -11
index 64e28a089a..884b7d78e2 100644
@@ -477,7 +477,8 @@ static void gen_load_check_gcs_record(DisasContext *s, TCGv_i64 target,
 
 typedef struct DisasCompare64 {
     TCGCond cond;
-    TCGv_i64 value;
+    TCGv_i64 v1;
+    TCGv_i64 v2;
 } DisasCompare64;
 
 static void a64_test_cc(DisasCompare64 *c64, int cc)
@@ -491,8 +492,9 @@ static void a64_test_cc(DisasCompare64 *c64, int cc)
      * properly.  The NE/EQ comparisons are also fine with this choice.
       */
     c64->cond = c32.cond;
-    c64->value = tcg_temp_new_i64();
-    tcg_gen_ext_i32_i64(c64->value, c32.value);
+    c64->v1 = tcg_temp_new_i64();
+    tcg_gen_ext_i32_i64(c64->v1, c32.value);
+    c64->v2 = tcg_constant_i64(0);
 }
 
 static void gen_rebuild_hflags(DisasContext *s)
@@ -7803,8 +7805,7 @@ static bool trans_FCSEL(DisasContext *s, arg_FCSEL *a)
     read_vec_element(s, t_false, a->rm, 0, a->esz);
 
     a64_test_cc(&c, a->cond);
-    tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
-                        t_true, t_false);
+    tcg_gen_movcond_i64(c.cond, t_true, c.v1, c.v2, t_true, t_false);
 
     /*
      * Note that sregs & hregs write back zeros to the high bits,
@@ -9485,7 +9486,6 @@ static bool trans_CCMP(DisasContext *s, arg_CCMP *a)
 static bool trans_CSEL(DisasContext *s, arg_CSEL *a)
 {
     TCGv_i64 tcg_rd = cpu_reg(s, a->rd);
-    TCGv_i64 zero = tcg_constant_i64(0);
     DisasCompare64 c;
 
     a64_test_cc(&c, a->cond);
@@ -9493,11 +9493,9 @@ static bool trans_CSEL(DisasContext *s, arg_CSEL *a)
     if (a->rn == 31 && a->rm == 31 && (a->else_inc ^ a->else_inv)) {
         /* CSET & CSETM.  */
         if (a->else_inv) {
-            tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond),
-                                   tcg_rd, c.value, zero);
+            tcg_gen_negsetcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.v1, c.v2);
         } else {
-            tcg_gen_setcond_i64(tcg_invert_cond(c.cond),
-                                tcg_rd, c.value, zero);
+            tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.v1, c.v2);
         }
     } else {
         TCGv_i64 t_true = cpu_reg(s, a->rn);
@@ -9510,7 +9508,7 @@ static bool trans_CSEL(DisasContext *s, arg_CSEL *a)
         } else if (a->else_inc) {
             tcg_gen_addi_i64(t_false, t_false, 1);
         }
-        tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
+        tcg_gen_movcond_i64(c.cond, tcg_rd, c.v1, c.v2, t_true, t_false);
     }
 
     if (!a->sf) {
Signature · 1 line
2.53.0
#6 lore raw

PATCH 4/9 target/arm: Split out gen_goto_tb_int

In reply to Richard Henderson
---
 target/arm/tcg/translate-a64.c | 38 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)
target/arm/tcg/translate-a64.c +22 -16
index 884b7d78e2..4289b65fdd 100644
@@ -541,25 +541,31 @@ static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
     return translator_use_goto_tb(&s->base, dest);
 }
 
+/* We must have already tested use_goto_tb. */
+static void gen_goto_tb_int(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
+{
+    /*
+     * For pcrel, the pc must always be up-to-date on entry to
+     * the linked TB, so that it can use simple additions for all
+     * further adjustments.  For !pcrel, the linked TB is compiled
+     * to know its full virtual address, so we can delay the
+     * update to pc to the unlinked path.  A long chain of links
+     * can thus avoid many updates to the PC.
+     */
+    if (tb_cflags(s->base.tb) & CF_PCREL) {
+        gen_a64_update_pc(s, diff);
+        tcg_gen_goto_tb(tb_slot_idx);
+    } else {
+        tcg_gen_goto_tb(tb_slot_idx);
+        gen_a64_update_pc(s, diff);
+    }
+    tcg_gen_exit_tb(s->base.tb, tb_slot_idx);
+}
+
 static void gen_goto_tb(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
 {
     if (use_goto_tb(s, s->pc_curr + diff)) {
-        /*
-         * For pcrel, the pc must always be up-to-date on entry to
-         * the linked TB, so that it can use simple additions for all
-         * further adjustments.  For !pcrel, the linked TB is compiled
-         * to know its full virtual address, so we can delay the
-         * update to pc to the unlinked path.  A long chain of links
-         * can thus avoid many updates to the PC.
-         */
-        if (tb_cflags(s->base.tb) & CF_PCREL) {
-            gen_a64_update_pc(s, diff);
-            tcg_gen_goto_tb(tb_slot_idx);
-        } else {
-            tcg_gen_goto_tb(tb_slot_idx);
-            gen_a64_update_pc(s, diff);
-        }
-        tcg_gen_exit_tb(s->base.tb, tb_slot_idx);
+        gen_goto_tb_int(s, tb_slot_idx, diff);
         s->base.is_jmp = DISAS_NORETURN;
     } else {
         gen_a64_update_pc(s, diff);
Signature · 1 line
2.53.0
#8 lore raw

PATCH 5/9 target/arm: Split out gen_indirect_branch

In reply to Richard Henderson

Use this instead of DISAS_JUMP. Existing indirect branches now end the TB with DISAS_NORETURN. Share the code with gen_goto_tb when use_goto_tb is false.

---
 target/arm/tcg/translate-a64.c | 51 +++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 25 deletions(-)
target/arm/tcg/translate-a64.c +26 -25
index 4289b65fdd..c8d1251f13 100644
@@ -562,6 +562,17 @@ static void gen_goto_tb_int(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
     tcg_gen_exit_tb(s->base.tb, tb_slot_idx);
 }
 
+static bool gen_indirect_branch(DisasContext *s)
+{
+    if (s->ss_active) {
+        gen_step_complete_exception(s);
+    } else {
+        tcg_gen_lookup_and_goto_ptr();
+    }
+    s->base.is_jmp = DISAS_NORETURN;
+    return true;
+}
+
 static void gen_goto_tb(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
 {
     if (use_goto_tb(s, s->pc_curr + diff)) {
@@ -569,12 +580,7 @@ static void gen_goto_tb(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
         s->base.is_jmp = DISAS_NORETURN;
     } else {
         gen_a64_update_pc(s, diff);
-        if (s->ss_active) {
-            gen_step_complete_exception(s);
-        } else {
-            tcg_gen_lookup_and_goto_ptr();
-            s->base.is_jmp = DISAS_NORETURN;
-        }
+        gen_indirect_branch(s);
     }
 }
 
@@ -1921,8 +1927,7 @@ static bool trans_BR(DisasContext *s, arg_r *a)
 {
     set_btype_for_br(s, a->rn);
     gen_a64_set_pc(s, cpu_reg(s, a->rn));
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_BLR(DisasContext *s, arg_r *a)
@@ -1937,8 +1942,7 @@ static bool trans_BLR(DisasContext *s, arg_r *a)
     tcg_gen_mov_i64(cpu_reg(s, 30), link);
 
     set_btype_for_blr(s);
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_RET(DisasContext *s, arg_r *a)
@@ -1950,8 +1954,7 @@ static bool trans_RET(DisasContext *s, arg_r *a)
     } else {
         gen_a64_set_pc(s, target);
     }
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static TCGv_i64 auth_branch_target(DisasContext *s, TCGv_i64 dst,
@@ -1987,8 +1990,7 @@ static bool trans_BRAZ(DisasContext *s, arg_braz *a)
     dst = auth_branch_target(s, cpu_reg(s, a->rn), tcg_constant_i64(0), !a->m);
     set_btype_for_br(s, a->rn);
     gen_a64_set_pc(s, dst);
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
@@ -2009,8 +2011,7 @@ static bool trans_BLRAZ(DisasContext *s, arg_braz *a)
     tcg_gen_mov_i64(cpu_reg(s, 30), link);
 
     set_btype_for_blr(s);
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_RETA(DisasContext *s, arg_reta *a)
@@ -2028,8 +2029,7 @@ static bool trans_RETA(DisasContext *s, arg_reta *a)
     } else {
         gen_a64_set_pc(s, dst);
     }
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_BRA(DisasContext *s, arg_bra *a)
@@ -2042,8 +2042,7 @@ static bool trans_BRA(DisasContext *s, arg_bra *a)
     dst = auth_branch_target(s, cpu_reg(s,a->rn), cpu_reg_sp(s, a->rm), !a->m);
     set_btype_for_br(s, a->rn);
     gen_a64_set_pc(s, dst);
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_BLRA(DisasContext *s, arg_bra *a)
@@ -2064,8 +2063,7 @@ static bool trans_BLRA(DisasContext *s, arg_bra *a)
     tcg_gen_mov_i64(cpu_reg(s, 30), link);
 
     set_btype_for_blr(s);
-    s->base.is_jmp = DISAS_JUMP;
-    return true;
+    return gen_indirect_branch(s);
 }
 
 static bool trans_ERET(DisasContext *s, arg_ERET *a)
@@ -11240,11 +11238,13 @@ static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
             gen_a64_update_pc(dc, 4);
             /* fall through */
         case DISAS_EXIT:
-        case DISAS_JUMP:
             gen_step_complete_exception(dc);
             break;
         case DISAS_NORETURN:
             break;
+        case DISAS_JUMP:
+            /* Use gen_indirect_branch instead. */
+            g_assert_not_reached();
         }
     } else {
         switch (dc->base.is_jmp) {
@@ -11261,10 +11261,11 @@ static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
             break;
         case DISAS_UPDATE_NOCHAIN:
             gen_a64_update_pc(dc, 4);
-            /* fall through */
-        case DISAS_JUMP:
             tcg_gen_lookup_and_goto_ptr();
             break;
+        case DISAS_JUMP:
+            /* Use gen_indirect_branch instead. */
+            g_assert_not_reached();
         case DISAS_NORETURN:
         case DISAS_SWI:
             break;
Signature · 1 line
2.53.0
Reviewed-by on v1 5/9
#9 lore raw

Re: [PATCH 5/9] target/arm: Split out gen_indirect_branch

Quoted 10 lines · Richard Henderson

Use this instead of DISAS_JUMP. Existing indirect branches now end the TB with DISAS_NORETURN. Share the code with gen_goto_tb when use_goto_tb is false.

---
 target/arm/tcg/translate-a64.c | 51 +++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 25 deletions(-)

thanks
-- PMM

#10 lore raw

PATCH 6/9 target/arm: Split out gen_cbranch_goto_tb

In reply to Richard Henderson

A common helper for all conditional branches, using DisasCompare64.

---
 target/arm/tcg/translate-a64.c | 142 ++++++++++++++++++---------------
 1 file changed, 79 insertions(+), 63 deletions(-)
target/arm/tcg/translate-a64.c +79 -63
index c8d1251f13..e66ccdff6f 100644
@@ -584,6 +584,50 @@ static void gen_goto_tb(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
     }
 }
 
+static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
+                        int64_t taken_diff)
+{
+    int64_t not_taken_diff = 4;
+    bool may_goto_t = use_goto_tb(s, s->pc_curr + taken_diff);
+    bool may_goto_f = use_goto_tb(s, s->pc_curr + not_taken_diff);
+
+    /*
+     * If there is a choice, place a real goto_tb path first.
+     * If neither path can use goto_tb, convert to an indirect branch.
+     */
+    if (may_goto_f) {
+        DisasLabel l = gen_disas_label(s);
+
+        tcg_gen_brcond_i64(c->cond, c->v1, c->v2, l.label);
+        gen_goto_tb_int(s, 0, not_taken_diff);
+
+        set_disas_label(s, l);
+        if (may_goto_t) {
+            gen_goto_tb_int(s, 1, taken_diff);
+            s->base.is_jmp = DISAS_NORETURN;
+            return true;
+        }
+        gen_a64_update_pc(s, taken_diff);
+    } else if (may_goto_t) {
+        DisasLabel l = gen_disas_label(s);
+
+        tcg_gen_brcond_i64(tcg_invert_cond(c->cond), c->v1, c->v2, l.label);
+        gen_goto_tb_int(s, 1, taken_diff);
+
+        set_disas_label(s, l);
+        gen_a64_update_pc(s, not_taken_diff);
+    } else {
+        TCGv_i64 pc_t = tcg_temp_new_i64();
+        TCGv_i64 pc_f = tcg_temp_new_i64();
+
+        gen_pc_plus_diff(s, pc_t, taken_diff);
+        gen_pc_plus_diff(s, pc_f, not_taken_diff);
+        tcg_gen_movcond_i64(c->cond, cpu_pc, c->v1, c->v2, pc_t, pc_f);
+        s->pc_save = -1;
+    }
+    return gen_indirect_branch(s);
+}
+
 /*
  * Register access functions
  *
@@ -1758,38 +1802,26 @@ static bool trans_BL(DisasContext *s, arg_i *a)
 
 static bool trans_CBZ(DisasContext *s, arg_cbz *a)
 {
-    DisasLabel match;
-    TCGv_i64 tcg_cmp;
+    DisasCompare64 c = {
+        .cond = a->nz ? TCG_COND_NE : TCG_COND_EQ,
+        .v1 = read_cpu_reg(s, a->rt, a->sf),
+        .v2 = tcg_constant_i64(0),
+    };
 
-    tcg_cmp = read_cpu_reg(s, a->rt, a->sf);
     reset_btype(s);
-
-    match = gen_disas_label(s);
-    tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
-                        tcg_cmp, 0, match.label);
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm);
-    return true;
+    return gen_cbranch(s, &c, a->imm);
 }
 
 static bool trans_TBZ(DisasContext *s, arg_tbz *a)
 {
-    DisasLabel match;
-    TCGv_i64 tcg_cmp;
-
-    tcg_cmp = tcg_temp_new_i64();
-    tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
+    DisasCompare64 c = {
+        .cond = a->nz ? TCG_COND_TSTNE : TCG_COND_TSTEQ,
+        .v1 = cpu_reg(s, a->rt),
+        .v2 = tcg_constant_i64(1ULL << a->bitpos),
+    };
 
     reset_btype(s);
-
-    match = gen_disas_label(s);
-    tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
-                        tcg_cmp, 0, match.label);
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm);
-    return true;
+    return gen_cbranch(s, &c, a->imm);
 }
 
 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
@@ -1801,11 +1833,10 @@ static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
     reset_btype(s);
     if (a->cond < 0x0e) {
         /* genuinely conditional branches */
-        DisasLabel match = gen_disas_label(s);
-        arm_gen_test_cc(a->cond, match.label);
-        gen_goto_tb(s, 0, 4);
-        set_disas_label(s, match);
-        gen_goto_tb(s, 1, a->imm);
+        DisasCompare64 c;
+
+        a64_test_cc(&c, a->cond);
+        return gen_cbranch(s, &c, a->imm);
     } else {
         /* 0xe and 0xf are both "always" conditions */
         gen_goto_tb(s, 0, a->imm);
@@ -1825,35 +1856,27 @@ static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a)
         [6] = TCG_COND_EQ,
         [7] = TCG_COND_NE,
     };
-    TCGCond cond = cb_cond[a->cc];
-    TCGv_i64 t, m;
-    DisasLabel match;
+    DisasCompare64 c = { .cond = cb_cond[a->cc] };
 
-    if (!dc_isar_feature(aa64_cmpbr, s) || cond == TCG_COND_NEVER) {
+    if (!dc_isar_feature(aa64_cmpbr, s) || c.cond == TCG_COND_NEVER) {
         return false;
     }
 
-    t = cpu_reg(s, a->rt);
-    m = cpu_reg(s, a->rm);
+    c.v1 = cpu_reg(s, a->rt);
+    c.v1 = cpu_reg(s, a->rm);
     if (a->esz != MO_64) {
-        MemOp mop = a->esz | (is_signed_cond(cond) ? MO_SIGN : 0);
+        MemOp mop = a->esz | (is_signed_cond(c.cond) ? MO_SIGN : 0);
         TCGv_i64 tt = tcg_temp_new_i64();
         TCGv_i64 tm = tcg_temp_new_i64();
 
-        tcg_gen_ext_i64(tt, t, mop);
-        tcg_gen_ext_i64(tm, m, mop);
-        t = tt;
-        m = tm;
+        tcg_gen_ext_i64(tt, c.v1, mop);
+        tcg_gen_ext_i64(tm, c.v1, mop);
+        c.v1 = tt;
+        c.v1 = tm;
     }
 
     reset_btype(s);
-    match = gen_disas_label(s);
-
-    tcg_gen_brcond_i64(cond, t, m, match.label);
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm);
-    return true;
+    return gen_cbranch(s, &c, a->imm);
 }
 
 static bool trans_CB_cond_imm(DisasContext *s, arg_CB_cond_imm *a)
@@ -1869,34 +1892,27 @@ static bool trans_CB_cond_imm(DisasContext *s, arg_CB_cond_imm *a)
         [6] = TCG_COND_EQ,
         [7] = TCG_COND_NE,
     };
-    TCGCond cond = cb_cond[a->cc];
-    TCGv_i64 t;
-    DisasLabel match;
+    DisasCompare64 c = { .cond = cb_cond[a->cc] };
 
-    if (!dc_isar_feature(aa64_cmpbr, s) || cond == TCG_COND_NEVER) {
+    if (!dc_isar_feature(aa64_cmpbr, s) || c.cond == TCG_COND_NEVER) {
         return false;
     }
 
-    t = cpu_reg(s, a->rt);
+    c.v1 = cpu_reg(s, a->rt);
     if (!a->sf) {
         TCGv_i64 tt = tcg_temp_new_i64();
 
-        if (is_signed_cond(cond)) {
-            tcg_gen_ext32s_i64(tt, t);
+        if (is_signed_cond(c.cond)) {
+            tcg_gen_ext32s_i64(tt, c.v1);
         } else {
-            tcg_gen_ext32u_i64(tt, t);
+            tcg_gen_ext32u_i64(tt, c.v1);
         }
-        t = tt;
+        c.v1 = tt;
     }
+    c.v2 = tcg_constant_i64(a->imm6);
 
     reset_btype(s);
-    match = gen_disas_label(s);
-
-    tcg_gen_brcondi_i64(cond, t, a->imm6, match.label);
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm9);
-    return true;
+    return gen_cbranch(s, &c, a->imm9);
 }
 
 static void set_btype_for_br(DisasContext *s, int rn)
Signature · 1 line
2.53.0
#11 lore raw

Re: [PATCH 6/9] target/arm: Split out gen_cbranch_goto_tb

Quoted 7 lines · Richard Henderson

A common helper for all conditional branches, using DisasCompare64.

---
 target/arm/tcg/translate-a64.c | 142 ++++++++++++++++++---------------
 1 file changed, 79 insertions(+), 63 deletions(-)

Converting all the potential callsites at once makes this a bit tiring to review. Could we split out at least the complicated ones (B_cond and CB_cond) into their own commits?

Quoted 8 lines

static bool trans_TBZ(DisasContext *s, arg_tbz *a) {

target/arm/tcg/translate-a64.c +1 -5
-    DisasLabel match;
-    TCGv_i64 tcg_cmp;
-
-    tcg_cmp = tcg_temp_new_i64();
-    tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, a->rt), 1ULL << a->bitpos);
+    DisasCompare64 c = {

target/arm/tcg/translate-a64.c static bool trans_BL(DisasContext *s, arg_i *a) · line 1788

Quoted 10 lines
target/arm/tcg/translate-a64.c +4 -4
+        .cond = a->nz ? TCG_COND_TSTNE : TCG_COND_TSTEQ,
+        .v1 = cpu_reg(s, a->rt),
+        .v2 = tcg_constant_i64(1ULL << a->bitpos),
+    };
 
     reset_btype(s);
-
-    match = gen_disas_label(s);
-    tcg_gen_brcondi_i64(a->nz ? TCG_COND_NE : TCG_COND_EQ,
-                        tcg_cmp, 0, match.label);

Could we avoid converting the NE/EQ conditional branch into a TSTNE/TSTEQ branch in the same commit we're doing this refactoring ?

Quoted 32 lines
target/arm/tcg/translate-a64.c +6 -12
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm);
-    return true;
+    return gen_cbranch(s, &c, a->imm);
 }
 
 static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
@@ -1801,11 +1833,10 @@ static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
     reset_btype(s);
     if (a->cond < 0x0e) {
         /* genuinely conditional branches */
-        DisasLabel match = gen_disas_label(s);
-        arm_gen_test_cc(a->cond, match.label);
-        gen_goto_tb(s, 0, 4);
-        set_disas_label(s, match);
-        gen_goto_tb(s, 1, a->imm);
+        DisasCompare64 c;
+
+        a64_test_cc(&c, a->cond);
+        return gen_cbranch(s, &c, a->imm);
     } else {
         /* 0xe and 0xf are both "always" conditions */
         gen_goto_tb(s, 0, a->imm);
@@ -1825,35 +1856,27 @@ static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a)
         [6] = TCG_COND_EQ,
         [7] = TCG_COND_NE,
     };
-    TCGCond cond = cb_cond[a->cc];
-    TCGv_i64 t, m;
-    DisasLabel match;
+    DisasCompare64 c = { .cond = cb_cond[a->cc] };

target/arm/tcg/translate-a64.c static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a) · line 1866

Quoted 10 lines
target/arm/tcg/translate-a64.c +3 -3
 
-    if (!dc_isar_feature(aa64_cmpbr, s) || cond == TCG_COND_NEVER) {
+    if (!dc_isar_feature(aa64_cmpbr, s) || c.cond == TCG_COND_NEVER) {
         return false;
     }
 
-    t = cpu_reg(s, a->rt);
-    m = cpu_reg(s, a->rm);
+    c.v1 = cpu_reg(s, a->rt);
+    c.v1 = cpu_reg(s, a->rm);

Presumably one of these should be c.v2 ...

target/arm/tcg/translate-a64.c static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a) · line 1873

Quoted 12 lines
target/arm/tcg/translate-a64.c +3 -5
     if (a->esz != MO_64) {
-        MemOp mop = a->esz | (is_signed_cond(cond) ? MO_SIGN : 0);
+        MemOp mop = a->esz | (is_signed_cond(c.cond) ? MO_SIGN : 0);
         TCGv_i64 tt = tcg_temp_new_i64();
         TCGv_i64 tm = tcg_temp_new_i64();
 
-        tcg_gen_ext_i64(tt, t, mop);
-        tcg_gen_ext_i64(tm, m, mop);
-        t = tt;
-        m = tm;
+        tcg_gen_ext_i64(tt, c.v1, mop);
+        tcg_gen_ext_i64(tm, c.v1, mop);

...and also here...

target/arm/tcg/translate-a64.c static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a) · line 1875

Quoted 2 lines
target/arm/tcg/translate-a64.c +2 -0
+        c.v1 = tt;
+        c.v1 = tm;

...and here.

target/arm/tcg/translate-a64.c static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a) · line 1881

Quoted 13 lines
target/arm/tcg/translate-a64.c +1 -7
     }
 
     reset_btype(s);
-    match = gen_disas_label(s);
-
-    tcg_gen_brcond_i64(cond, t, m, match.label);
-    gen_goto_tb(s, 0, 4);
-    set_disas_label(s, match);
-    gen_goto_tb(s, 1, a->imm);
-    return true;
+    return gen_cbranch(s, &c, a->imm);
 }
 

-- PMM

#12 lore raw

PATCH 7/9 target/arm: Use DISAS_TOO_MANY for simple TB breaks

In reply to Richard Henderson
---
 target/arm/tcg/translate-a64.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
target/arm/tcg/translate-a64.c +2 -3
index e66ccdff6f..0f6331a695 100644
@@ -2420,8 +2420,7 @@ static bool trans_ISB(DisasContext *s, arg_ISB *a)
      * self-modifying code correctly and also to take
      * any pending interrupts immediately.
      */
-    reset_btype(s);
-    gen_goto_tb(s, 0, 4);
+    s->base.is_jmp = DISAS_TOO_MANY;
     return true;
 }
 
@@ -2435,7 +2434,7 @@ static bool trans_SB(DisasContext *s, arg_SB *a)
      * MB and end the TB instead.
      */
     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
-    gen_goto_tb(s, 0, 4);
+    s->base.is_jmp = DISAS_TOO_MANY;
     return true;
 }
 
Signature · 1 line
2.53.0
Reviewed-by on v1 7/9
#13 lore raw

Re: [PATCH 7/9] target/arm: Use DISAS_TOO_MANY for simple TB breaks

Quoted 21 lines · Richard Henderson
---
 target/arm/tcg/translate-a64.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
target/arm/tcg/translate-a64.c +1 -2
index e66ccdff6f..0f6331a695 100644
@@ -2420,8 +2420,7 @@ static bool trans_ISB(DisasContext *s, arg_ISB *a)
      * self-modifying code correctly and also to take
      * any pending interrupts immediately.
      */
-    reset_btype(s);
-    gen_goto_tb(s, 0, 4);
+    s->base.is_jmp = DISAS_TOO_MANY;
     return true;
 }
 

target/arm/tcg/translate-a64.c static bool trans_SB(DisasContext *s, arg_SB *a) · line 2439

Quoted 7 lines
target/arm/tcg/translate-a64.c +1 -1
@@ -2435,7 +2434,7 @@ static bool trans_SB(DisasContext *s, arg_SB *a)
      * MB and end the TB instead.
      */
     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
-    gen_goto_tb(s, 0, 4);
+    s->base.is_jmp = DISAS_TOO_MANY;
     return true;
 }

We already use DISAS_TOO_MANY for some other kinds of "just break the TB" (e.g. various trans_MSR_* insns) so this should be fine.

I notice that the end-of-tb code will do a gen_goto_tb(s, 1, 4) where here we have (s, 0, 4) but I assume there's no requirement to not use TB slot index 1 unless you're using slot index 0, or we'd have seen it causing problems on the existing uses.

thanks
-- PMM

#14 lore raw

PATCH 8/9 target/arm: Rework gen_goto_tb to gen_direct_branch

In reply to Richard Henderson

The only remaining uses are true direct branches, or simple chaining from one TB to the next via fallthru.

---
 target/arm/tcg/translate-a64.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)
target/arm/tcg/translate-a64.c +10 -14
index 0f6331a695..edbf67b03c 100644
@@ -573,15 +573,15 @@ static bool gen_indirect_branch(DisasContext *s)
     return true;
 }
 
-static void gen_goto_tb(DisasContext *s, unsigned tb_slot_idx, int64_t diff)
+static bool gen_direct_branch(DisasContext *s, int64_t diff)
 {
     if (use_goto_tb(s, s->pc_curr + diff)) {
-        gen_goto_tb_int(s, tb_slot_idx, diff);
+        gen_goto_tb_int(s, 0, diff);
         s->base.is_jmp = DISAS_NORETURN;
-    } else {
-        gen_a64_update_pc(s, diff);
-        gen_indirect_branch(s);
+        return true;
     }
+    gen_a64_update_pc(s, diff);
+    return gen_indirect_branch(s);
 }
 
 static bool gen_cbranch(DisasContext *s, const DisasCompare64 *c,
@@ -1780,8 +1780,7 @@ static inline void gen_check_sp_alignment(DisasContext *s)
 static bool trans_B(DisasContext *s, arg_i *a)
 {
     reset_btype(s);
-    gen_goto_tb(s, 0, a->imm);
-    return true;
+    return gen_direct_branch(s, a->imm);
 }
 
 static bool trans_BL(DisasContext *s, arg_i *a)
@@ -1795,8 +1794,7 @@ static bool trans_BL(DisasContext *s, arg_i *a)
     tcg_gen_mov_i64(cpu_reg(s, 30), link);
 
     reset_btype(s);
-    gen_goto_tb(s, 0, a->imm);
-    return true;
+    return gen_direct_branch(s, a->imm);
 }
 
 
@@ -1837,11 +1835,9 @@ static bool trans_B_cond(DisasContext *s, arg_B_cond *a)
 
         a64_test_cc(&c, a->cond);
         return gen_cbranch(s, &c, a->imm);
-    } else {
-        /* 0xe and 0xf are both "always" conditions */
-        gen_goto_tb(s, 0, a->imm);
     }
-    return true;
+    /* 0xe and 0xf are both "always" conditions */
+    return gen_direct_branch(s, a->imm);
 }
 
 static bool trans_CB_cond(DisasContext *s, arg_CB_cond *a)
@@ -11265,7 +11261,7 @@ static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
         switch (dc->base.is_jmp) {
         case DISAS_NEXT:
         case DISAS_TOO_MANY:
-            gen_goto_tb(dc, 1, 4);
+            gen_direct_branch(dc, 4);
             break;
         default:
         case DISAS_UPDATE_EXIT:
Signature · 1 line
2.53.0
#16 lore raw

PATCH 9/9 target/arm: Use tcg_gen_goto_jc3_i64 for aa64 branches

In reply to Richard Henderson

Assuming FEAT_BTI hasn't needed a page-table lookup to determine the BTYPE for the destination, we can compute the tbflags for the target. Pass all that to goto_jc3.

---
 target/arm/tcg/translate-a64.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/arm/tcg/translate-a64.c +4 -1
index edbf67b03c..4a56fa70b7 100644
@@ -566,8 +566,11 @@ static bool gen_indirect_branch(DisasContext *s)
 {
     if (s->ss_active) {
         gen_step_complete_exception(s);
-    } else {
+    } else if (s->tbflags_invalid) {
         tcg_gen_lookup_and_goto_ptr();
+    } else {
+        tcg_gen_goto_jc3_i64(cpu_pc, tcg_constant_i64(s->tbflags.flags2),
+                             s->tbflags.flags);
     }
     s->base.is_jmp = DISAS_NORETURN;
     return true;
Signature · 1 line
2.53.0
#17 lore raw

Re: [PATCH 9/9] target/arm: Use tcg_gen_goto_jc3_i64 for aa64 branches

Quoted 15 lines · Richard Henderson

Assuming FEAT_BTI hasn't needed a page-table lookup to determine the BTYPE for the destination, we can compute the tbflags for the target. Pass all that to goto_jc3.

---
 target/arm/tcg/translate-a64.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/arm/tcg/translate-a64.c +0 -0
index edbf67b03c..4a56fa70b7 100644

target/arm/tcg/translate-a64.c static bool gen_indirect_branch(DisasContext *s) · line 573

Quoted 9 lines
target/arm/tcg/translate-a64.c +4 -1
@@ -566,8 +566,11 @@ static bool gen_indirect_branch(DisasContext *s)
 {
     if (s->ss_active) {
         gen_step_complete_exception(s);
-    } else {
+    } else if (s->tbflags_invalid) {
         tcg_gen_lookup_and_goto_ptr();
+    } else {
+        tcg_gen_goto_jc3_i64(cpu_pc, tcg_constant_i64(s->tbflags.flags2),
+                             s->tbflags.flags);

Why does tcg_gen_goto_jc3_i64() take "flags2, flags" rather than the more intuitive "flags, flags2" order ?

-- PMM