Skip to contentSkip to conversation

Third try at fixing sparc register allocation

PATCH In review Richard Henderson 3 participants 25 messages Jun 24, 2016 – Aug 3, 2016

lore
Share & tools
lore
#0 lore raw

v3 cover Third try at fixing sparc register allocation

I was unhappy about the complexity of the second try.

Better to convert to normal temps, allowing in rare occasions, spilling the "globals" to the stack in order to satisfy register allocation.

I can no longer provoke an allocation failure on i686. Hopefully this fixes the OpenBSD case that Mark mentioned re the second attempt.

r~

Richard Henderson (9):
tcg: Fix name for high-half register
tcg: Optimize spills of constants
tcg: Require liveness analysis
tcg: Compress liveness data to 16 bits
tcg: Reorg TCGOp chaining
tcg: Fold life data into TCGOp
tcg: Compress dead_temps and mem_temps into a single array
tcg: Include liveness info in the dumps
tcg: Lower indirect registers in a separate pass

 include/exec/gen-icount.h    |   2 +-
 include/qemu/log.h           |   1 +
 tcg/aarch64/tcg-target.inc.c |  10 +
 tcg/arm/tcg-target.inc.c     |   6 +
 tcg/i386/tcg-target.inc.c    |  21 +-
 tcg/ia64/tcg-target.inc.c    |  10 +
 tcg/mips/tcg-target.inc.c    |  10 +
 tcg/optimize.c               |  37 +--
 tcg/ppc/tcg-target.inc.c     |   6 +
 tcg/s390/tcg-target.inc.c    |   6 +
 tcg/sparc/tcg-target.inc.c   |  10 +
 tcg/tcg-op.c                 |   2 +-
 tcg/tcg.c                    | 690 ++++++++++++++++++++++++++++---------------
 tcg/tcg.h                    |  50 ++--
 tcg/tci/tcg-target.inc.c     |   6 +
 util/log.c                   |   5 +-
 16 files changed, 563 insertions(+), 309 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH v3 1/9 tcg: Fix name for high-half register

---
 tcg/tcg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg.c +1 -1
index 254427b..154ffe8 100644
@@ -557,7 +557,7 @@ int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
         ts2->mem_offset = offset + (1 - bigendian) * 4;
         pstrcpy(buf, sizeof(buf), name);
         pstrcat(buf, sizeof(buf), "_1");
-        ts->name = strdup(buf);
+        ts2->name = strdup(buf);
     } else {
         ts->base_type = type;
         ts->type = type;
Signature · 1 line
2.5.5
Reviewed-by on v3 1/9
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/9] tcg: Fix name for high-half register

Quoted 11 lines · Richard Henderson
---
 tcg/tcg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcg/tcg.c +0 -0
index 254427b..154ffe8 100644

tcg/tcg.c int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base, · line 563

Quoted 8 lines
tcg/tcg.c +1 -1
@@ -557,7 +557,7 @@ int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
         ts2->mem_offset = offset + (1 - bigendian) * 4;
         pstrcpy(buf, sizeof(buf), name);
         pstrcat(buf, sizeof(buf), "_1");
-        ts->name = strdup(buf);
+        ts2->name = strdup(buf);
     } else {
         ts->base_type = type;
         ts->type = type;
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#3 lore raw

PATCH v3 2/9 tcg: Optimize spills of constants

In reply to Richard Henderson

While we can store constants via constrants on INDEX_op_st_i32 et al, we weren't able to spill constants to backing store.

Add a new backend interface, tcg_out_sti, which may store the constant (and is allowed to fail). Rearrange the temp_* helpers so that we only attempt to directly store a constant when the temp is becoming dead/free.

---
 tcg/aarch64/tcg-target.inc.c |  10 +++
 tcg/arm/tcg-target.inc.c     |   6 ++
 tcg/i386/tcg-target.inc.c    |  21 ++++--
 tcg/ia64/tcg-target.inc.c    |  10 +++
 tcg/mips/tcg-target.inc.c    |  10 +++
 tcg/ppc/tcg-target.inc.c     |   6 ++
 tcg/s390/tcg-target.inc.c    |   6 ++
 tcg/sparc/tcg-target.inc.c   |  10 +++
 tcg/tcg.c                    | 159 ++++++++++++++++++++++++-------------------
 tcg/tci/tcg-target.inc.c     |   6 ++
 10 files changed, 166 insertions(+), 78 deletions(-)
tcg/aarch64/tcg-target.inc.c +10 -0
index 1447f7c..5ac0091 100644
@@ -716,6 +716,16 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
                  arg, arg1, arg2);
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    if (val == 0) {
+        tcg_out_st(s, type, TCG_REG_XZR, base, ofs);
+        return true;
+    }
+    return false;
+}
+
 static inline void tcg_out_bfm(TCGContext *s, TCGType ext, TCGReg rd,
                                TCGReg rn, unsigned int a, unsigned int b)
 {
tcg/arm/tcg-target.inc.c +6 -0
index f9f54c6..172feba 100644
@@ -2046,6 +2046,12 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     tcg_out_st32(s, COND_AL, arg, arg1, arg2);
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    return false;
+}
+
 static inline void tcg_out_mov(TCGContext *s, TCGType type,
                                TCGReg ret, TCGReg arg)
 {
tcg/i386/tcg-target.inc.c +14 -7
index 317484c..bc34535 100644
@@ -710,12 +710,19 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     tcg_out_modrm_offset(s, opc, arg, arg1, arg2);
 }
 
-static inline void tcg_out_sti(TCGContext *s, TCGType type, TCGReg base,
-                               tcg_target_long ofs, tcg_target_long val)
+static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                        TCGReg base, intptr_t ofs)
 {
-    int opc = OPC_MOVL_EvIz + (type == TCG_TYPE_I64 ? P_REXW : 0);
-    tcg_out_modrm_offset(s, opc, 0, base, ofs);
+    int rexw = 0;
+    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) {
+        if (val != (int32_t)val) {
+            return false;
+        }
+        rexw = P_REXW;
+    }
+    tcg_out_modrm_offset(s, OPC_MOVL_EvIz | rexw, 0, base, ofs);
     tcg_out32(s, val);
+    return true;
 }
 
 static void tcg_out_shifti(TCGContext *s, int subopc, int reg, int count)
@@ -1321,10 +1328,10 @@ static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
             ofs += 4;
         }
 
-        tcg_out_sti(s, TCG_TYPE_I32, TCG_REG_ESP, ofs, oi);
+        tcg_out_sti(s, TCG_TYPE_I32, oi, TCG_REG_ESP, ofs);
         ofs += 4;
 
-        tcg_out_sti(s, TCG_TYPE_PTR, TCG_REG_ESP, ofs, (uintptr_t)l->raddr);
+        tcg_out_sti(s, TCG_TYPE_PTR, (uintptr_t)l->raddr, TCG_REG_ESP, ofs);
     } else {
         tcg_out_mov(s, TCG_TYPE_PTR, tcg_target_call_iarg_regs[0], TCG_AREG0);
         /* The second argument is already loaded with addrlo.  */
@@ -1413,7 +1420,7 @@ static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
             ofs += 4;
         }
 
-        tcg_out_sti(s, TCG_TYPE_I32, TCG_REG_ESP, ofs, oi);
+        tcg_out_sti(s, TCG_TYPE_I32, oi, TCG_REG_ESP, ofs);
         ofs += 4;
 
         retaddr = TCG_REG_EAX;
tcg/ia64/tcg-target.inc.c +10 -0
index 395223e..c91f392 100644
@@ -973,6 +973,16 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     }
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    if (val == 0) {
+        tcg_out_st(s, type, TCG_REG_R0, base, ofs);
+        return true;
+    }
+    return false;
+}
+
 static inline void tcg_out_alu(TCGContext *s, uint64_t opc_a1, uint64_t opc_a3,
                                TCGReg ret, TCGArg arg1, int const_arg1,
                                TCGArg arg2, int const_arg2)
tcg/mips/tcg-target.inc.c +10 -0
index 50e98ea..2f9be48 100644
@@ -576,6 +576,16 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     tcg_out_ldst(s, OPC_SW, arg, arg1, arg2);
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    if (val == 0) {
+        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
+        return true;
+    }
+    return false;
+}
+
 static inline void tcg_out_addi(TCGContext *s, TCGReg reg, TCGArg val)
 {
     if (val == (int16_t)val) {
tcg/ppc/tcg-target.inc.c +6 -0
index da10052..dba954c 100644
@@ -857,6 +857,12 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     tcg_out_mem_long(s, opi, opx, arg, arg1, arg2);
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    return false;
+}
+
 static void tcg_out_cmp(TCGContext *s, int cond, TCGArg arg1, TCGArg arg2,
                         int const_arg2, int cr, TCGType type)
 {
tcg/s390/tcg-target.inc.c +6 -0
index e0a60e6..314c183 100644
@@ -798,6 +798,12 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
     }
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    return false;
+}
+
 /* load data from an absolute host address */
 static void tcg_out_ld_abs(TCGContext *s, TCGType type, TCGReg dest, void *abs)
 {
tcg/sparc/tcg-target.inc.c +10 -0
index 9938a50..8e98172 100644
@@ -504,6 +504,16 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
     tcg_out_ldst(s, arg, arg1, arg2, (type == TCG_TYPE_I32 ? STW : STX));
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    if (val == 0) {
+        tcg_out_st(s, type, TCG_REG_G0, base, ofs);
+        return true;
+    }
+    return false;
+}
+
 static void tcg_out_ld_ptr(TCGContext *s, TCGReg ret, uintptr_t arg)
 {
     tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ff);
tcg/tcg.c +88 -71

238 lines not on this page · raw message

tcg/tci/tcg-target.inc.c +6 -0
index fa74d52..3c47ea7 100644
@@ -834,6 +834,12 @@ static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
     old_code_ptr[1] = s->code_ptr - old_code_ptr;
 }
 
+static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
+                               TCGReg base, intptr_t ofs)
+{
+    return false;
+}
+
 /* Test if a constant matches the constraint. */
 static int tcg_target_const_match(tcg_target_long val, TCGType type,
                                   const TCGArgConstraint *arg_ct)
Signature · 1 line
2.5.5
#4 lore raw

PATCH v3 3/9 tcg: Require liveness analysis

In reply to Richard Henderson
---
 tcg/tcg.c | 20 --------------------
 1 file changed, 20 deletions(-)
tcg/tcg.c +0 -20
index 44de991..64060c6 100644
@@ -23,7 +23,6 @@
  */
 
 /* define it to use liveness analysis (better code) */
-#define USE_LIVENESS_ANALYSIS
 #define USE_TCG_OPTIMIZATIONS
 
 #include "qemu/osdep.h"
@@ -1306,7 +1305,6 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
 #endif
 }
 
-#ifdef USE_LIVENESS_ANALYSIS
 /* liveness analysis: end of function: all temps are dead, and globals
    should be in memory. */
 static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
@@ -1575,18 +1573,6 @@ static void tcg_liveness_analysis(TCGContext *s)
         }
     }
 }
-#else
-/* dummy liveness analysis */
-static void tcg_liveness_analysis(TCGContext *s)
-{
-    int nb_ops = s->gen_next_op_idx;
-
-    s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
-    memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t));
-    s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
-    memset(s->op_sync_args, 0, nb_ops * sizeof(uint8_t));
-}
-#endif
 
 #ifdef CONFIG_DEBUG_TCG
 static void dump_regs(TCGContext *s)
@@ -1838,7 +1824,6 @@ static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
 static inline void temp_save(TCGContext *s, TCGTemp *ts,
                              TCGRegSet allocated_regs)
 {
-#ifdef USE_LIVENESS_ANALYSIS
     /* ??? Liveness does not yet incorporate indirect bases.  */
     if (!ts->indirect_base) {
         /* The liveness analysis already ensures that globals are back
@@ -1846,7 +1831,6 @@ static inline void temp_save(TCGContext *s, TCGTemp *ts,
         tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
         return;
     }
-#endif
     temp_sync(s, ts, allocated_regs, 1);
 }
 
@@ -1871,7 +1855,6 @@ static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
 
     for (i = 0; i < s->nb_globals; i++) {
         TCGTemp *ts = &s->temps[i];
-#ifdef USE_LIVENESS_ANALYSIS
         /* ??? Liveness does not yet incorporate indirect bases.  */
         if (!ts->indirect_base) {
             tcg_debug_assert(ts->val_type != TEMP_VAL_REG
@@ -1879,7 +1862,6 @@ static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
                              || ts->mem_coherent);
             continue;
         }
-#endif
         temp_sync(s, ts, allocated_regs, 0);
     }
 }
@@ -1895,7 +1877,6 @@ static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
         if (ts->temp_local) {
             temp_save(s, ts, allocated_regs);
         } else {
-#ifdef USE_LIVENESS_ANALYSIS
             /* ??? Liveness does not yet incorporate indirect bases.  */
             if (!ts->indirect_base) {
                 /* The liveness analysis already ensures that temps are dead.
@@ -1903,7 +1884,6 @@ static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
                 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
                 continue;
             }
-#endif
             temp_dead(s, ts);
         }
     }
Signature · 1 line
2.5.5
Reviewed-by on v3 3/9
#5 lore raw

Re: [Qemu-devel] [PATCH v3 3/9] tcg: Require liveness analysis

Quoted 5 lines · Richard Henderson
---
 tcg/tcg.c | 20 --------------------
 1 file changed, 20 deletions(-)

Thanks for doing that. The code without liveness analysis is not really tested anymore and given the kind of optimisations we are doing there it became quite difficult to compare the generated code with and without liveness.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#6 lore raw

PATCH v3 4/9 tcg: Compress liveness data to 16 bits

In reply to Richard Henderson

This reduces both memory usage and per-insn cacheline usage during code generation.

---
 tcg/tcg.c | 58 ++++++++++++++++++++++------------------------------------
 tcg/tcg.h | 16 ++++++++++------
 2 files changed, 32 insertions(+), 42 deletions(-)
tcg/tcg.c +22 -36
index 64060c6..400e69c 100644
@@ -1329,7 +1329,7 @@ static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
     }
 }
 
-/* Liveness analysis : update the opc_dead_args array to tell if a
+/* Liveness analysis : update the opc_arg_life array to tell if a
    given input arguments is dead. Instructions updating dead
    temporaries are removed. */
 static void tcg_liveness_analysis(TCGContext *s)
@@ -1338,9 +1338,8 @@ static void tcg_liveness_analysis(TCGContext *s)
     int oi, oi_prev, nb_ops;
 
     nb_ops = s->gen_next_op_idx;
-    s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
-    s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
-    
+    s->op_arg_life = tcg_malloc(nb_ops * sizeof(TCGLifeData));
+
     dead_temps = tcg_malloc(s->nb_temps);
     mem_temps = tcg_malloc(s->nb_temps);
     tcg_la_func_end(s, dead_temps, mem_temps);
@@ -1349,8 +1348,7 @@ static void tcg_liveness_analysis(TCGContext *s)
         int i, nb_iargs, nb_oargs;
         TCGOpcode opc_new, opc_new2;
         bool have_opc_new2;
-        uint16_t dead_args;
-        uint8_t sync_args;
+        TCGLifeData arg_life = 0;
         TCGArg arg;
 
         TCGOp * const op = &s->gen_op_buf[oi];
@@ -1382,15 +1380,13 @@ static void tcg_liveness_analysis(TCGContext *s)
                 do_not_remove_call:
 
                     /* output args are dead */
-                    dead_args = 0;
-                    sync_args = 0;
                     for (i = 0; i < nb_oargs; i++) {
                         arg = args[i];
                         if (dead_temps[arg]) {
-                            dead_args |= (1 << i);
+                            arg_life |= DEAD_ARG << i;
                         }
                         if (mem_temps[arg]) {
-                            sync_args |= (1 << i);
+                            arg_life |= SYNC_ARG << i;
                         }
                         dead_temps[arg] = 1;
                         mem_temps[arg] = 0;
@@ -1411,7 +1407,7 @@ static void tcg_liveness_analysis(TCGContext *s)
                         arg = args[i];
                         if (arg != TCG_CALL_DUMMY_ARG) {
                             if (dead_temps[arg]) {
-                                dead_args |= (1 << i);
+                                arg_life |= DEAD_ARG << i;
                             }
                         }
                     }
@@ -1420,8 +1416,6 @@ static void tcg_liveness_analysis(TCGContext *s)
                         arg = args[i];
                         dead_temps[arg] = 0;
                     }
-                    s->op_dead_args[oi] = dead_args;
-                    s->op_sync_args[oi] = sync_args;
                 }
             }
             break;
@@ -1532,15 +1526,13 @@ static void tcg_liveness_analysis(TCGContext *s)
             } else {
             do_not_remove:
                 /* output args are dead */
-                dead_args = 0;
-                sync_args = 0;
                 for (i = 0; i < nb_oargs; i++) {
                     arg = args[i];
                     if (dead_temps[arg]) {
-                        dead_args |= (1 << i);
+                        arg_life |= DEAD_ARG << i;
                     }
                     if (mem_temps[arg]) {
-                        sync_args |= (1 << i);
+                        arg_life |= SYNC_ARG << i;
                     }
                     dead_temps[arg] = 1;
                     mem_temps[arg] = 0;
@@ -1558,7 +1550,7 @@ static void tcg_liveness_analysis(TCGContext *s)
                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
                     arg = args[i];
                     if (dead_temps[arg]) {
-                        dead_args |= (1 << i);
+                        arg_life |= DEAD_ARG << i;
                     }
                 }
                 /* input arguments are live for preceding opcodes */
@@ -1566,11 +1558,10 @@ static void tcg_liveness_analysis(TCGContext *s)
                     arg = args[i];
                     dead_temps[arg] = 0;
                 }
-                s->op_dead_args[oi] = dead_args;
-                s->op_sync_args[oi] = sync_args;
             }
             break;
         }
+        s->op_arg_life[oi] = arg_life;
     }
 }
 
@@ -1891,11 +1882,11 @@ static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
     save_globals(s, allocated_regs);
 }
 
-#define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1)
-#define NEED_SYNC_ARG(n) ((sync_args >> (n)) & 1)
+#define IS_DEAD_ARG(n)   (arg_life & (DEAD_ARG << (n)))
+#define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
 
 static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
-                               uint16_t dead_args, uint8_t sync_args)
+                               TCGLifeData arg_life)
 {
     TCGTemp *ots;
     tcg_target_ulong val;
@@ -1924,8 +1915,7 @@ static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
 }
 
 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
-                              const TCGArg *args, uint16_t dead_args,
-                              uint8_t sync_args)
+                              const TCGArg *args, TCGLifeData arg_life)
 {
     TCGRegSet allocated_regs;
     TCGTemp *ts, *ots;
@@ -2010,8 +2000,7 @@ static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
 
 static void tcg_reg_alloc_op(TCGContext *s, 
                              const TCGOpDef *def, TCGOpcode opc,
-                             const TCGArg *args, uint16_t dead_args,
-                             uint8_t sync_args)
+                             const TCGArg *args, TCGLifeData arg_life)
 {
     TCGRegSet allocated_regs;
     int i, k, nb_iargs, nb_oargs;
@@ -2176,8 +2165,7 @@ static void tcg_reg_alloc_op(TCGContext *s,
 #endif
 
 static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
-                               const TCGArg * const args, uint16_t dead_args,
-                               uint8_t sync_args)
+                               const TCGArg * const args, TCGLifeData arg_life)
 {
     int flags, nb_regs, i;
     TCGReg reg;
@@ -2397,8 +2385,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
         TCGArg * const args = &s->gen_opparam_buf[op->args];
         TCGOpcode opc = op->opc;
         const TCGOpDef *def = &tcg_op_defs[opc];
-        uint16_t dead_args = s->op_dead_args[oi];
-        uint8_t sync_args = s->op_sync_args[oi];
+        TCGLifeData arg_life = s->op_arg_life[oi];
 
         oi_next = op->next;
 #ifdef CONFIG_PROFILER
@@ -2408,11 +2395,11 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
         switch (opc) {
         case INDEX_op_mov_i32:
         case INDEX_op_mov_i64:
-            tcg_reg_alloc_mov(s, def, args, dead_args, sync_args);
+            tcg_reg_alloc_mov(s, def, args, arg_life);
             break;
         case INDEX_op_movi_i32:
         case INDEX_op_movi_i64:
-            tcg_reg_alloc_movi(s, args, dead_args, sync_args);
+            tcg_reg_alloc_movi(s, args, arg_life);
             break;
         case INDEX_op_insn_start:
             if (num_insns >= 0) {
@@ -2437,8 +2424,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
             tcg_out_label(s, arg_label(args[0]), s->code_ptr);
             break;
         case INDEX_op_call:
-            tcg_reg_alloc_call(s, op->callo, op->calli, args,
-                               dead_args, sync_args);
+            tcg_reg_alloc_call(s, op->callo, op->calli, args, arg_life);
             break;
         default:
             /* Sanity check that we've not introduced any unhandled opcodes. */
@@ -2448,7 +2434,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
             /* Note: in order to speed up the code, it would be much
                faster to have specialized register allocator functions for
                some common argument patterns */
-            tcg_reg_alloc_op(s, def, opc, args, dead_args, sync_args);
+            tcg_reg_alloc_op(s, def, opc, args, arg_life);
             break;
         }
 #ifdef CONFIG_DEBUG_TCG
tcg/tcg.h +10 -6
index 66d7fc0..cc14560 100644
@@ -505,6 +505,14 @@ typedef struct TCGTempSet {
     unsigned long l[BITS_TO_LONGS(TCG_MAX_TEMPS)];
 } TCGTempSet;
 
+/* While we limit helpers to 6 arguments, for 32-bit hosts, with padding,
+   this imples a max of 6*2 (64-bit in) + 2 (64-bit out) = 14 operands.
+   There are never more than 2 outputs, which means that we can store all
+   dead + sync data within 16 bits.  */
+#define DEAD_ARG  4
+#define SYNC_ARG  1
+typedef uint16_t TCGLifeData;
+
 typedef struct TCGOp {
     TCGOpcode opc   : 8;
 
@@ -538,12 +546,8 @@ struct TCGContext {
     uintptr_t *tb_jmp_target_addr; /* tb->jmp_target_addr if !USE_DIRECT_JUMP */
 
     /* liveness analysis */
-    uint16_t *op_dead_args; /* for each operation, each bit tells if the
-                               corresponding argument is dead */
-    uint8_t *op_sync_args;  /* for each operation, each bit tells if the
-                               corresponding output argument needs to be
-                               sync to memory. */
-    
+    TCGLifeData *op_arg_life;
+
     TCGRegSet reserved_regs;
     intptr_t current_frame_offset;
     intptr_t frame_start;
Signature · 1 line
2.5.5
Reviewed-by on v3 4/9
#7 lore raw

Re: [Qemu-devel] [PATCH v3 4/9] tcg: Compress liveness data to 16 bits

Quoted 35 lines · Richard Henderson

This reduces both memory usage and per-insn cacheline usage during code generation.

---
 tcg/tcg.c | 58 ++++++++++++++++++++++------------------------------------
 tcg/tcg.h | 16 ++++++++++------
 2 files changed, 32 insertions(+), 42 deletions(-)
tcg/tcg.c +3 -4
index 64060c6..400e69c 100644
@@ -1329,7 +1329,7 @@ static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
     }
 }
 
-/* Liveness analysis : update the opc_dead_args array to tell if a
+/* Liveness analysis : update the opc_arg_life array to tell if a
    given input arguments is dead. Instructions updating dead
    temporaries are removed. */
 static void tcg_liveness_analysis(TCGContext *s)
@@ -1338,9 +1338,8 @@ static void tcg_liveness_analysis(TCGContext *s)
     int oi, oi_prev, nb_ops;
 
     nb_ops = s->gen_next_op_idx;
-    s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
-    s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
-    
+    s->op_arg_life = tcg_malloc(nb_ops * sizeof(TCGLifeData));
+
     dead_temps = tcg_malloc(s->nb_temps);
     mem_temps = tcg_malloc(s->nb_temps);
     tcg_la_func_end(s, dead_temps, mem_temps);

tcg/tcg.c static void tcg_liveness_analysis(TCGContext *s) · line 1351

Quoted 6 lines
tcg/tcg.c +1 -2
@@ -1349,8 +1348,7 @@ static void tcg_liveness_analysis(TCGContext *s)
         int i, nb_iargs, nb_oargs;
         TCGOpcode opc_new, opc_new2;
         bool have_opc_new2;
-        uint16_t dead_args;
-        uint8_t sync_args;
+        TCGLifeData arg_life = 0;

A small improvement, probably for later: we can zero the s->op_arg_life structure, and then access it directly instead of using the arg_life temporary variable.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#8 lore raw

PATCH v3 5/9 tcg: Reorg TCGOp chaining

In reply to Richard Henderson

Instead of using -1 as end of chain, use 0, and link through the 0 entry as a fully circular double-linked list.

---
 include/exec/gen-icount.h |  2 +-
 tcg/optimize.c            |  8 ++------
 tcg/tcg-op.c              |  2 +-
 tcg/tcg.c                 | 32 ++++++++++++--------------------
 tcg/tcg.h                 | 20 ++++++++++++--------
 5 files changed, 28 insertions(+), 36 deletions(-)
include/exec/gen-icount.h +1 -1
index a011324..5f16077 100644
@@ -59,7 +59,7 @@ static void gen_tb_end(TranslationBlock *tb, int num_insns)
     }
 
     /* Terminate the linked list.  */
-    tcg_ctx.gen_op_buf[tcg_ctx.gen_last_op_idx].next = -1;
+    tcg_ctx.gen_op_buf[tcg_ctx.gen_op_buf[0].prev].next = 0;
 }
 
 static inline void gen_io_start(void)
tcg/optimize.c +2 -6
index c0d975b..8df7fc7 100644
@@ -103,11 +103,7 @@ static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
         .prev = prev,
         .next = next
     };
-    if (prev >= 0) {
-        s->gen_op_buf[prev].next = oi;
-    } else {
-        s->gen_first_op_idx = oi;
-    }
+    s->gen_op_buf[prev].next = oi;
     old_op->prev = oi;
 
     return new_op;
@@ -583,7 +579,7 @@ void tcg_optimize(TCGContext *s)
     nb_globals = s->nb_globals;
     reset_all_temps(nb_temps);
 
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = oi_next) {
         tcg_target_ulong mask, partmask, affected;
         int nb_oargs, nb_iargs, i;
         TCGArg tmp;
tcg/tcg-op.c +1 -1
index 569cdc6..62d91b4 100644
@@ -52,7 +52,7 @@ static void tcg_emit_op(TCGContext *ctx, TCGOpcode opc, int args)
     int pi = oi - 1;
 
     tcg_debug_assert(oi < OPC_BUF_SIZE);
-    ctx->gen_last_op_idx = oi;
+    ctx->gen_op_buf[0].prev = oi;
     ctx->gen_next_op_idx = ni;
 
     ctx->gen_op_buf[oi] = (TCGOp){
tcg/tcg.c +12 -20
index 400e69c..bb1efe2 100644
@@ -437,9 +437,9 @@ void tcg_func_start(TCGContext *s)
     s->goto_tb_issue_mask = 0;
 #endif
 
-    s->gen_first_op_idx = 0;
-    s->gen_last_op_idx = -1;
-    s->gen_next_op_idx = 0;
+    s->gen_op_buf[0].next = 1;
+    s->gen_op_buf[0].prev = 0;
+    s->gen_next_op_idx = 1;
     s->gen_next_parm_idx = 0;
 
     s->be = tcg_malloc(sizeof(TCGBackendData));
@@ -868,7 +868,7 @@ void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
     /* Make sure the calli field didn't overflow.  */
     tcg_debug_assert(s->gen_op_buf[i].calli == real_args);
 
-    s->gen_last_op_idx = i;
+    s->gen_op_buf[0].prev = i;
     s->gen_next_op_idx = i + 1;
     s->gen_next_parm_idx = pi;
 
@@ -1004,7 +1004,7 @@ void tcg_dump_ops(TCGContext *s)
     TCGOp *op;
     int oi;
 
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = op->next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = op->next) {
         int i, k, nb_oargs, nb_iargs, nb_cargs;
         const TCGOpDef *def;
         const TCGArg *args;
@@ -1016,7 +1016,7 @@ void tcg_dump_ops(TCGContext *s)
         args = &s->gen_opparam_buf[op->args];
 
         if (c == INDEX_op_insn_start) {
-            qemu_log("%s ----", oi != s->gen_first_op_idx ? "\n" : "");
+            qemu_log("%s ----", oi != s->gen_op_buf[0].next ? "\n" : "");
 
             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
                 target_ulong a;
@@ -1287,18 +1287,10 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
     int next = op->next;
     int prev = op->prev;
 
-    if (next >= 0) {
-        s->gen_op_buf[next].prev = prev;
-    } else {
-        s->gen_last_op_idx = prev;
-    }
-    if (prev >= 0) {
-        s->gen_op_buf[prev].next = next;
-    } else {
-        s->gen_first_op_idx = next;
-    }
+    s->gen_op_buf[next].prev = prev;
+    s->gen_op_buf[prev].next = next;
 
-    memset(op, -1, sizeof(*op));
+    memset(op, 0, sizeof(*op));
 
 #ifdef CONFIG_PROFILER
     s->del_op_count++;
@@ -1344,7 +1336,7 @@ static void tcg_liveness_analysis(TCGContext *s)
     mem_temps = tcg_malloc(s->nb_temps);
     tcg_la_func_end(s, dead_temps, mem_temps);
 
-    for (oi = s->gen_last_op_idx; oi >= 0; oi = oi_prev) {
+    for (oi = s->gen_op_buf[0].prev; oi != 0; oi = oi_prev) {
         int i, nb_iargs, nb_oargs;
         TCGOpcode opc_new, opc_new2;
         bool have_opc_new2;
@@ -2321,7 +2313,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
     {
         int n;
 
-        n = s->gen_last_op_idx + 1;
+        n = s->gen_op_buf[0].prev + 1;
         s->op_count += n;
         if (n > s->op_count_max) {
             s->op_count_max = n;
@@ -2380,7 +2372,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
     tcg_out_tb_init(s);
 
     num_insns = -1;
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = oi_next) {
         TCGOp * const op = &s->gen_op_buf[oi];
         TCGArg * const args = &s->gen_opparam_buf[op->args];
         TCGOpcode opc = op->opc;
tcg/tcg.h +12 -8
index cc14560..49b396d 100644
@@ -520,17 +520,21 @@ typedef struct TCGOp {
     unsigned callo  : 2;
     unsigned calli  : 6;
 
-    /* Index of the arguments for this op, or -1 for zero-operand ops.  */
-    signed args     : 16;
+    /* Index of the arguments for this op, or 0 for zero-operand ops.  */
+    unsigned args   : 16;
 
-    /* Index of the prex/next op, or -1 for the end of the list.  */
-    signed prev     : 16;
-    signed next     : 16;
+    /* Index of the prex/next op, or 0 for the end of the list.  */
+    unsigned prev   : 16;
+    unsigned next   : 16;
 } TCGOp;
 
-QEMU_BUILD_BUG_ON(NB_OPS > 0xff);
-QEMU_BUILD_BUG_ON(OPC_BUF_SIZE >= 0x7fff);
-QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE >= 0x7fff);
+/* Make sure operands fit in the bitfields above.  */
+QEMU_BUILD_BUG_ON(NB_OPS > (1 << 8));
+QEMU_BUILD_BUG_ON(OPC_BUF_SIZE > (1 << 16));
+QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE > (1 << 16));
+
+/* Make sure that we don't overflow 64 bits without noticing.  */
+QEMU_BUILD_BUG_ON(sizeof(TCGOp) > 8);
 
 struct TCGContext {
     uint8_t *pool_cur, *pool_end;
Signature · 1 line
2.5.5
#9 lore raw

Re: [Qemu-devel] [PATCH v3 5/9] tcg: Reorg TCGOp chaining

Quoted 117 lines · Richard Henderson

Instead of using -1 as end of chain, use 0, and link through the 0 entry as a fully circular double-linked list.

---
 include/exec/gen-icount.h |  2 +-
 tcg/optimize.c            |  8 ++------
 tcg/tcg-op.c              |  2 +-
 tcg/tcg.c                 | 32 ++++++++++++--------------------
 tcg/tcg.h                 | 20 ++++++++++++--------
 5 files changed, 28 insertions(+), 36 deletions(-)
include/exec/gen-icount.h +1 -1
index a011324..5f16077 100644
@@ -59,7 +59,7 @@ static void gen_tb_end(TranslationBlock *tb, int num_insns)
     }
 
     /* Terminate the linked list.  */
-    tcg_ctx.gen_op_buf[tcg_ctx.gen_last_op_idx].next = -1;
+    tcg_ctx.gen_op_buf[tcg_ctx.gen_op_buf[0].prev].next = 0;
 }
 
 static inline void gen_io_start(void)
tcg/optimize.c +2 -6
index c0d975b..8df7fc7 100644
@@ -103,11 +103,7 @@ static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
         .prev = prev,
         .next = next
     };
-    if (prev >= 0) {
-        s->gen_op_buf[prev].next = oi;
-    } else {
-        s->gen_first_op_idx = oi;
-    }
+    s->gen_op_buf[prev].next = oi;
     old_op->prev = oi;
 
     return new_op;
@@ -583,7 +579,7 @@ void tcg_optimize(TCGContext *s)
     nb_globals = s->nb_globals;
     reset_all_temps(nb_temps);
 
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = oi_next) {
         tcg_target_ulong mask, partmask, affected;
         int nb_oargs, nb_iargs, i;
         TCGArg tmp;
tcg/tcg-op.c +1 -1
index 569cdc6..62d91b4 100644
@@ -52,7 +52,7 @@ static void tcg_emit_op(TCGContext *ctx, TCGOpcode opc, int args)
     int pi = oi - 1;
 
     tcg_debug_assert(oi < OPC_BUF_SIZE);
-    ctx->gen_last_op_idx = oi;
+    ctx->gen_op_buf[0].prev = oi;
     ctx->gen_next_op_idx = ni;
 
     ctx->gen_op_buf[oi] = (TCGOp){
tcg/tcg.c +6 -11
index 400e69c..bb1efe2 100644
@@ -437,9 +437,9 @@ void tcg_func_start(TCGContext *s)
     s->goto_tb_issue_mask = 0;
 #endif
 
-    s->gen_first_op_idx = 0;
-    s->gen_last_op_idx = -1;
-    s->gen_next_op_idx = 0;
+    s->gen_op_buf[0].next = 1;
+    s->gen_op_buf[0].prev = 0;
+    s->gen_next_op_idx = 1;
     s->gen_next_parm_idx = 0;
 
     s->be = tcg_malloc(sizeof(TCGBackendData));
@@ -868,7 +868,7 @@ void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
     /* Make sure the calli field didn't overflow.  */
     tcg_debug_assert(s->gen_op_buf[i].calli == real_args);
 
-    s->gen_last_op_idx = i;
+    s->gen_op_buf[0].prev = i;
     s->gen_next_op_idx = i + 1;
     s->gen_next_parm_idx = pi;
 
@@ -1004,7 +1004,7 @@ void tcg_dump_ops(TCGContext *s)
     TCGOp *op;
     int oi;
 
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = op->next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = op->next) {
         int i, k, nb_oargs, nb_iargs, nb_cargs;
         const TCGOpDef *def;
         const TCGArg *args;
@@ -1016,7 +1016,7 @@ void tcg_dump_ops(TCGContext *s)
         args = &s->gen_opparam_buf[op->args];
 
         if (c == INDEX_op_insn_start) {
-            qemu_log("%s ----", oi != s->gen_first_op_idx ? "\n" : "");
+            qemu_log("%s ----", oi != s->gen_op_buf[0].next ? "\n" : "");
 
             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
                 target_ulong a;
@@ -1287,18 +1287,10 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
     int next = op->next;
     int prev = op->prev;
 
-    if (next >= 0) {
-        s->gen_op_buf[next].prev = prev;
-    } else {
-        s->gen_last_op_idx = prev;
-    }

tcg/tcg.c void tcg_op_remove(TCGContext *s, TCGOp *op) · line 1293

Quoted 10 lines
tcg/tcg.c +3 -6
-    if (prev >= 0) {
-        s->gen_op_buf[prev].next = next;
-    } else {
-        s->gen_first_op_idx = next;
-    }
+    s->gen_op_buf[next].prev = prev;
+    s->gen_op_buf[prev].next = next;
 
-    memset(op, -1, sizeof(*op));
+    memset(op, 0, sizeof(*op));

Doing so means you can remove the op at gen_op_buf[0]. The doubled linked list is still valid, but then I guess you can't assume anymore that the first op is gen_op_buf[0], as it is done elsewhere your patch.

I guess it's unlikely to happen that we have to remove the first op. Maybe adding an assert there is enough?

Quoted 36 lines
tcg/tcg.c +3 -3
 #ifdef CONFIG_PROFILER
     s->del_op_count++;
@@ -1344,7 +1336,7 @@ static void tcg_liveness_analysis(TCGContext *s)
     mem_temps = tcg_malloc(s->nb_temps);
     tcg_la_func_end(s, dead_temps, mem_temps);
 
-    for (oi = s->gen_last_op_idx; oi >= 0; oi = oi_prev) {
+    for (oi = s->gen_op_buf[0].prev; oi != 0; oi = oi_prev) {
         int i, nb_iargs, nb_oargs;
         TCGOpcode opc_new, opc_new2;
         bool have_opc_new2;
@@ -2321,7 +2313,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
     {
         int n;
 
-        n = s->gen_last_op_idx + 1;
+        n = s->gen_op_buf[0].prev + 1;
         s->op_count += n;
         if (n > s->op_count_max) {
             s->op_count_max = n;
@@ -2380,7 +2372,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
     tcg_out_tb_init(s);
 
     num_insns = -1;
-    for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
+    for (oi = s->gen_op_buf[0].next; oi != 0; oi = oi_next) {
         TCGOp * const op = &s->gen_op_buf[oi];
         TCGArg * const args = &s->gen_opparam_buf[op->args];
         TCGOpcode opc = op->opc;
tcg/tcg.h +0 -0
index cc14560..49b396d 100644
@@ -520,17 +520,21 @@ typedef struct TCGOp {
     unsigned callo  : 2;
     unsigned calli  : 6;

tcg/tcg.h typedef struct TCGOp { · line 526

Quoted 10 lines
tcg/tcg.h +3 -5
 
-    /* Index of the arguments for this op, or -1 for zero-operand ops.  */
-    signed args     : 16;
+    /* Index of the arguments for this op, or 0 for zero-operand ops.  */
+    unsigned args   : 16;
 
-    /* Index of the prex/next op, or -1 for the end of the list.  */
-    signed prev     : 16;
-    signed next     : 16;
+    /* Index of the prex/next op, or 0 for the end of the list.  */

It's not introduced by your patch, but you might want to fix the typo prex -> prev.

Quoted 7 lines
tcg/tcg.h +2 -3
+    unsigned prev   : 16;
+    unsigned next   : 16;
 } TCGOp;
 
-QEMU_BUILD_BUG_ON(NB_OPS > 0xff);
-QEMU_BUILD_BUG_ON(OPC_BUF_SIZE >= 0x7fff);
-QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE >= 0x7fff);

tcg/tcg.h typedef struct TCGOp { · line 540

Quoted 10 lines
tcg/tcg.h +7 -0
+/* Make sure operands fit in the bitfields above.  */
+QEMU_BUILD_BUG_ON(NB_OPS > (1 << 8));
+QEMU_BUILD_BUG_ON(OPC_BUF_SIZE > (1 << 16));
+QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE > (1 << 16));
+
+/* Make sure that we don't overflow 64 bits without noticing.  */
+QEMU_BUILD_BUG_ON(sizeof(TCGOp) > 8);
 
 struct TCGContext {
     uint8_t *pool_cur, *pool_end;

It seems that gen_first_op_idx and gen_last_op_idx are now unused. Shouldn't they be removed?

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#10 lore raw

Re: [Qemu-devel] [PATCH v3 5/9] tcg: Reorg TCGOp chaining

Quoted 25 lines · Aurelien Jarno
Quoted 9 lines · Richard Henderson
tcg/tcg.c +0 -5
@@ -1287,18 +1287,10 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
     int next = op->next;
     int prev = op->prev;
 
-    if (next >= 0) {
-        s->gen_op_buf[next].prev = prev;
-    } else {
-        s->gen_last_op_idx = prev;
-    }

tcg/tcg.c void tcg_op_remove(TCGContext *s, TCGOp *op) · line 1293

Quoted 10 lines
tcg/tcg.c +3 -6
-    if (prev >= 0) {
-        s->gen_op_buf[prev].next = next;
-    } else {
-        s->gen_first_op_idx = next;
-    }
+    s->gen_op_buf[next].prev = prev;
+    s->gen_op_buf[prev].next = next;
 
-    memset(op, -1, sizeof(*op));
+    memset(op, 0, sizeof(*op));

Doing so means you can remove the op at gen_op_buf[0]. The doubled linked list is still valid, but then I guess you can't assume anymore that the first op is gen_op_buf[0], as it is done elsewhere your patch.

Quoted 2 lines

I guess it's unlikely to happen that we have to remove the first op. Maybe adding an assert there is enough?

Assert added.

Quoted 7 lines

tcg/tcg.h typedef struct TCGOp { · line 526

Quoted 4 lines
tcg/tcg.h +1 -3
-    /* Index of the prex/next op, or -1 for the end of the list.  */
-    signed prev     : 16;
-    signed next     : 16;
+    /* Index of the prex/next op, or 0 for the end of the list.  */

It's not introduced by your patch, but you might want to fix the typo prex -> prev.

Fixed.

Quoted 2 lines

It seems that gen_first_op_idx and gen_last_op_idx are now unused. Shouldn't they be removed?

Yep, fixed.

r~

#11 lore raw

PATCH v3 6/9 tcg: Fold life data into TCGOp

In reply to Richard Henderson

Reduce the size of other bitfields to make room.
This reduces the cache footprint of compilation.

---
 tcg/tcg.c |  9 +++------
 tcg/tcg.h | 26 ++++++++++++++------------
 2 files changed, 17 insertions(+), 18 deletions(-)
tcg/tcg.c +3 -6
index bb1efe2..c41640f 100644
@@ -1327,10 +1327,7 @@ static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
 static void tcg_liveness_analysis(TCGContext *s)
 {
     uint8_t *dead_temps, *mem_temps;
-    int oi, oi_prev, nb_ops;
-
-    nb_ops = s->gen_next_op_idx;
-    s->op_arg_life = tcg_malloc(nb_ops * sizeof(TCGLifeData));
+    int oi, oi_prev;
 
     dead_temps = tcg_malloc(s->nb_temps);
     mem_temps = tcg_malloc(s->nb_temps);
@@ -1553,7 +1550,7 @@ static void tcg_liveness_analysis(TCGContext *s)
             }
             break;
         }
-        s->op_arg_life[oi] = arg_life;
+        op->life = arg_life;
     }
 }
 
@@ -2377,7 +2374,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
         TCGArg * const args = &s->gen_opparam_buf[op->args];
         TCGOpcode opc = op->opc;
         const TCGOpDef *def = &tcg_op_defs[opc];
-        TCGLifeData arg_life = s->op_arg_life[oi];
+        TCGLifeData arg_life = op->life;
 
         oi_next = op->next;
 #ifdef CONFIG_PROFILER
tcg/tcg.h +14 -12
index 49b396d..2ff3ad2 100644
@@ -513,25 +513,30 @@ typedef struct TCGTempSet {
 #define SYNC_ARG  1
 typedef uint16_t TCGLifeData;
 
+/* The layout here is designed to avoid crossing of a 32-bit boundary.
+   If we do so, gcc adds padding, expanding the size to 12.  */
 typedef struct TCGOp {
-    TCGOpcode opc   : 8;
+    TCGOpcode opc   : 8;        /*  8 */
+
+    /* Index of the prex/next op, or 0 for the end of the list.  */
+    unsigned prev   : 10;       /* 18 */
+    unsigned next   : 10;       /* 28 */
 
     /* The number of out and in parameter for a call.  */
-    unsigned callo  : 2;
-    unsigned calli  : 6;
+    unsigned calli  : 4;        /* 32 */
+    unsigned callo  : 2;        /* 34 */
 
     /* Index of the arguments for this op, or 0 for zero-operand ops.  */
-    unsigned args   : 16;
+    unsigned args   : 14;       /* 48 */
 
-    /* Index of the prex/next op, or 0 for the end of the list.  */
-    unsigned prev   : 16;
-    unsigned next   : 16;
+    /* Lifetime data of the operands.  */
+    unsigned life   : 16;       /* 64 */
 } TCGOp;
 
 /* Make sure operands fit in the bitfields above.  */
 QEMU_BUILD_BUG_ON(NB_OPS > (1 << 8));
-QEMU_BUILD_BUG_ON(OPC_BUF_SIZE > (1 << 16));
-QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE > (1 << 16));
+QEMU_BUILD_BUG_ON(OPC_BUF_SIZE > (1 << 10));
+QEMU_BUILD_BUG_ON(OPPARAM_BUF_SIZE > (1 << 14));
 
 /* Make sure that we don't overflow 64 bits without noticing.  */
 QEMU_BUILD_BUG_ON(sizeof(TCGOp) > 8);
@@ -549,9 +554,6 @@ struct TCGContext {
     uint16_t *tb_jmp_insn_offset; /* tb->jmp_insn_offset if USE_DIRECT_JUMP */
     uintptr_t *tb_jmp_target_addr; /* tb->jmp_target_addr if !USE_DIRECT_JUMP */
 
-    /* liveness analysis */
-    TCGLifeData *op_arg_life;
-
     TCGRegSet reserved_regs;
     intptr_t current_frame_offset;
     intptr_t frame_start;
Signature · 1 line
2.5.5
Reviewed-by on v3 6/9
#12 lore raw

Re: [Qemu-devel] [PATCH v3 6/9] tcg: Fold life data into TCGOp

Quoted 8 lines · Richard Henderson

Reduce the size of other bitfields to make room.
This reduces the cache footprint of compilation.

---
 tcg/tcg.c |  9 +++------
 tcg/tcg.h | 26 ++++++++++++++------------
 2 files changed, 17 insertions(+), 18 deletions(-)

This looks fine and goes in the right direction of having all the data at the same location. In the long term it might be useful to have the TCG stream and the associated data together to implement more agressive optimisations.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#13 lore raw

PATCH v3 7/9 tcg: Compress dead_temps and mem_temps into a single array

In reply to Richard Henderson

We only need two bits per temporary. Fold the two bytes into one, and reduce the memory and cachelines required during compilation.

---
 tcg/tcg.c | 118 +++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 59 insertions(+), 59 deletions(-)
tcg/tcg.c +59 -59
index c41640f..fd92b06 100644
@@ -332,7 +332,7 @@ void tcg_context_init(TCGContext *s)
 
     memset(s, 0, sizeof(*s));
     s->nb_globals = 0;
-    
+
     /* Count total number of arguments and allocate the corresponding
        space */
     total_args = 0;
@@ -824,16 +824,16 @@ void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
                 real_args++;
             }
 #endif
-	    /* If stack grows up, then we will be placing successive
-	       arguments at lower addresses, which means we need to
-	       reverse the order compared to how we would normally
-	       treat either big or little-endian.  For those arguments
-	       that will wind up in registers, this still works for
-	       HPPA (the only current STACK_GROWSUP target) since the
-	       argument registers are *also* allocated in decreasing
-	       order.  If another such target is added, this logic may
-	       have to get more complicated to differentiate between
-	       stack arguments and register arguments.  */
+           /* If stack grows up, then we will be placing successive
+              arguments at lower addresses, which means we need to
+              reverse the order compared to how we would normally
+              treat either big or little-endian.  For those arguments
+              that will wind up in registers, this still works for
+              HPPA (the only current STACK_GROWSUP target) since the
+              argument registers are *also* allocated in decreasing
+              order.  If another such target is added, this logic may
+              have to get more complicated to differentiate between
+              stack arguments and register arguments.  */
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
             s->gen_opparam_buf[pi++] = args[i] + 1;
             s->gen_opparam_buf[pi++] = args[i];
@@ -1297,27 +1297,28 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
 #endif
 }
 
+#define TS_DEAD  1
+#define TS_SYNC  2
+
 /* liveness analysis: end of function: all temps are dead, and globals
    should be in memory. */
-static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
-                                   uint8_t *mem_temps)
+static inline void tcg_la_func_end(TCGContext *s, uint8_t *temp_state)
 {
-    memset(dead_temps, 1, s->nb_temps);
-    memset(mem_temps, 1, s->nb_globals);
-    memset(mem_temps + s->nb_globals, 0, s->nb_temps - s->nb_globals);
+    memset(temp_state, TS_DEAD | TS_SYNC, s->nb_globals);
+    memset(temp_state + s->nb_globals, TS_DEAD, s->nb_temps - s->nb_globals);
 }
 
 /* liveness analysis: end of basic block: all temps are dead, globals
    and local temps should be in memory. */
-static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
-                                 uint8_t *mem_temps)
+static inline void tcg_la_bb_end(TCGContext *s, uint8_t *temp_state)
 {
-    int i;
+    int i, n;
 
-    memset(dead_temps, 1, s->nb_temps);
-    memset(mem_temps, 1, s->nb_globals);
-    for(i = s->nb_globals; i < s->nb_temps; i++) {
-        mem_temps[i] = s->temps[i].temp_local;
+    tcg_la_func_end(s, temp_state);
+    for (i = s->nb_globals, n = s->nb_temps; i < n; i++) {
+        if (s->temps[i].temp_local) {
+            temp_state[i] |= TS_SYNC;
+        }
     }
 }
 
@@ -1326,12 +1327,12 @@ static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
    temporaries are removed. */
 static void tcg_liveness_analysis(TCGContext *s)
 {
-    uint8_t *dead_temps, *mem_temps;
+    uint8_t *temp_state;
     int oi, oi_prev;
+    int nb_globals = s->nb_globals;
 
-    dead_temps = tcg_malloc(s->nb_temps);
-    mem_temps = tcg_malloc(s->nb_temps);
-    tcg_la_func_end(s, dead_temps, mem_temps);
+    temp_state = tcg_malloc(s->nb_temps);
+    tcg_la_func_end(s, temp_state);
 
     for (oi = s->gen_op_buf[0].prev; oi != 0; oi = oi_prev) {
         int i, nb_iargs, nb_oargs;
@@ -1360,7 +1361,7 @@ static void tcg_liveness_analysis(TCGContext *s)
                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
                     for (i = 0; i < nb_oargs; i++) {
                         arg = args[i];
-                        if (!dead_temps[arg] || mem_temps[arg]) {
+                        if (temp_state[arg] != TS_DEAD) {
                             goto do_not_remove_call;
                         }
                     }
@@ -1371,39 +1372,41 @@ static void tcg_liveness_analysis(TCGContext *s)
                     /* output args are dead */
                     for (i = 0; i < nb_oargs; i++) {
                         arg = args[i];
-                        if (dead_temps[arg]) {
+                        if (temp_state[arg] & TS_DEAD) {
                             arg_life |= DEAD_ARG << i;
                         }
-                        if (mem_temps[arg]) {
+                        if (temp_state[arg] & TS_SYNC) {
                             arg_life |= SYNC_ARG << i;
                         }
-                        dead_temps[arg] = 1;
-                        mem_temps[arg] = 0;
+                        temp_state[arg] = TS_DEAD;
                     }
 
-                    if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
-                        /* globals should be synced to memory */
-                        memset(mem_temps, 1, s->nb_globals);
-                    }
                     if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
                                         TCG_CALL_NO_READ_GLOBALS))) {
                         /* globals should go back to memory */
-                        memset(dead_temps, 1, s->nb_globals);
+                        memset(temp_state, TS_DEAD | TS_SYNC, nb_globals);
+                    } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
+                        /* globals should be synced to memory */
+                        for (i = 0; i < nb_globals; i++) {
+                            temp_state[i] |= TS_SYNC;
+                        }
                     }
 
                     /* record arguments that die in this helper */
                     for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
                         arg = args[i];
                         if (arg != TCG_CALL_DUMMY_ARG) {
-                            if (dead_temps[arg]) {
+                            if (temp_state[arg] & TS_DEAD) {
                                 arg_life |= DEAD_ARG << i;
                             }
                         }
                     }
                     /* input arguments are live for preceding opcodes */
-                    for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
+                    for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
                         arg = args[i];
-                        dead_temps[arg] = 0;
+                        if (arg != TCG_CALL_DUMMY_ARG) {
+                            temp_state[arg] &= ~TS_DEAD;
+                        }
                     }
                 }
             }
@@ -1412,8 +1415,7 @@ static void tcg_liveness_analysis(TCGContext *s)
             break;
         case INDEX_op_discard:
             /* mark the temporary as dead */
-            dead_temps[args[0]] = 1;
-            mem_temps[args[0]] = 0;
+            temp_state[args[0]] = TS_DEAD;
             break;
 
         case INDEX_op_add2_i32:
@@ -1434,8 +1436,8 @@ static void tcg_liveness_analysis(TCGContext *s)
                the low part.  The result can be optimized to a simple
                add or sub.  This happens often for x86_64 guest when the
                cpu mode is set to 32 bit.  */
-            if (dead_temps[args[1]] && !mem_temps[args[1]]) {
-                if (dead_temps[args[0]] && !mem_temps[args[0]]) {
+            if (temp_state[args[1]] == TS_DEAD) {
+                if (temp_state[args[0]] == TS_DEAD) {
                     goto do_remove;
                 }
                 /* Replace the opcode and adjust the args in place,
@@ -1472,8 +1474,8 @@ static void tcg_liveness_analysis(TCGContext *s)
         do_mul2:
             nb_iargs = 2;
             nb_oargs = 2;
-            if (dead_temps[args[1]] && !mem_temps[args[1]]) {
-                if (dead_temps[args[0]] && !mem_temps[args[0]]) {
+            if (temp_state[args[1]] == TS_DEAD) {
+                if (temp_state[args[0]] == TS_DEAD) {
                     /* Both parts of the operation are dead.  */
                     goto do_remove;
                 }
@@ -1481,8 +1483,7 @@ static void tcg_liveness_analysis(TCGContext *s)
                 op->opc = opc = opc_new;
                 args[1] = args[2];
                 args[2] = args[3];
-            } else if (have_opc_new2 && dead_temps[args[0]]
-                       && !mem_temps[args[0]]) {
+            } else if (temp_state[args[0]] == TS_DEAD && have_opc_new2) {
                 /* The low part of the operation is dead; generate the high. */
                 op->opc = opc = opc_new2;
                 args[0] = args[1];
@@ -1505,8 +1506,7 @@ static void tcg_liveness_analysis(TCGContext *s)
                implies side effects */
             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
                 for (i = 0; i < nb_oargs; i++) {
-                    arg = args[i];
-                    if (!dead_temps[arg] || mem_temps[arg]) {
+                    if (temp_state[args[i]] != TS_DEAD) {
                         goto do_not_remove;
                     }
                 }
@@ -1517,35 +1517,35 @@ static void tcg_liveness_analysis(TCGContext *s)
                 /* output args are dead */
                 for (i = 0; i < nb_oargs; i++) {
                     arg = args[i];
-                    if (dead_temps[arg]) {
+                    if (temp_state[arg] & TS_DEAD) {
                         arg_life |= DEAD_ARG << i;
                     }
-                    if (mem_temps[arg]) {
+                    if (temp_state[arg] & TS_SYNC) {
                         arg_life |= SYNC_ARG << i;
                     }
-                    dead_temps[arg] = 1;
-                    mem_temps[arg] = 0;
+                    temp_state[arg] = TS_DEAD;
                 }
 
                 /* if end of basic block, update */
                 if (def->flags & TCG_OPF_BB_END) {
-                    tcg_la_bb_end(s, dead_temps, mem_temps);
+                    tcg_la_bb_end(s, temp_state);
                 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
                     /* globals should be synced to memory */
-                    memset(mem_temps, 1, s->nb_globals);
+                    for (i = 0; i < nb_globals; i++) {
+                        temp_state[i] |= TS_SYNC;
+                    }
                 }
 
                 /* record arguments that die in this opcode */
                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
                     arg = args[i];
-                    if (dead_temps[arg]) {
+                    if (temp_state[arg] & TS_DEAD) {
                         arg_life |= DEAD_ARG << i;
                     }
                 }
                 /* input arguments are live for preceding opcodes */
                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
-                    arg = args[i];
-                    dead_temps[arg] = 0;
+                    temp_state[args[i]] &= ~TS_DEAD;
                 }
             }
             break;
Signature · 1 line
2.5.5
Reviewed-by on v3 7/9
#14 lore raw

Re: [Qemu-devel] [PATCH v3 7/9] tcg: Compress dead_temps and mem_temps into a single array

Quoted 49 lines · Richard Henderson

We only need two bits per temporary. Fold the two bytes into one, and reduce the memory and cachelines required during compilation.

---
 tcg/tcg.c | 118 +++++++++++++++++++++++++++++++-------------------------------
 1 file changed, 59 insertions(+), 59 deletions(-)
tcg/tcg.c +11 -11
index c41640f..fd92b06 100644
@@ -332,7 +332,7 @@ void tcg_context_init(TCGContext *s)
 
     memset(s, 0, sizeof(*s));
     s->nb_globals = 0;
-    
+
     /* Count total number of arguments and allocate the corresponding
        space */
     total_args = 0;
@@ -824,16 +824,16 @@ void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
                 real_args++;
             }
 #endif
-	    /* If stack grows up, then we will be placing successive
-	       arguments at lower addresses, which means we need to
-	       reverse the order compared to how we would normally
-	       treat either big or little-endian.  For those arguments
-	       that will wind up in registers, this still works for
-	       HPPA (the only current STACK_GROWSUP target) since the
-	       argument registers are *also* allocated in decreasing
-	       order.  If another such target is added, this logic may
-	       have to get more complicated to differentiate between
-	       stack arguments and register arguments.  */
+           /* If stack grows up, then we will be placing successive
+              arguments at lower addresses, which means we need to
+              reverse the order compared to how we would normally
+              treat either big or little-endian.  For those arguments
+              that will wind up in registers, this still works for
+              HPPA (the only current STACK_GROWSUP target) since the
+              argument registers are *also* allocated in decreasing
+              order.  If another such target is added, this logic may
+              have to get more complicated to differentiate between
+              stack arguments and register arguments.  */
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
             s->gen_opparam_buf[pi++] = args[i] + 1;
             s->gen_opparam_buf[pi++] = args[i];

tcg/tcg.c void tcg_op_remove(TCGContext *s, TCGOp *op) · line 1301

Quoted 5 lines
tcg/tcg.c +2 -0
@@ -1297,27 +1297,28 @@ void tcg_op_remove(TCGContext *s, TCGOp *op)
 #endif
 }
 
+#define TS_DEAD  1
+#define TS_SYNC  2

I am not sure that TS_SYNC is the best name for that. There we really want to tell that at this moment in the TCG instruction stream the operand is in memory. It doesn't implied it is synced. Maybe TS_MEM?

The rest looks fine to me. The other alternative would have been to use the TCGTempSet with the bitmap functions like in optimize.c, and use only 2 bits per temp. That something that can be done later though.

All that said and provided you change the name:

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#15 lore raw

Re: [Qemu-devel] [PATCH v3 7/9] tcg: Compress dead_temps and mem_temps into a single array

Quoted 9 lines · Aurelien Jarno
Quoted 2 lines

tcg/tcg.c void tcg_op_remove(TCGContext *s, TCGOp *op) · line 1301

Quoted 2 lines
tcg/tcg.c +2 -0
+#define TS_DEAD  1
+#define TS_SYNC  2

I am not sure that TS_SYNC is the best name for that. There we really want to tell that at this moment in the TCG instruction stream the operand is in memory. It doesn't implied it is synced. Maybe TS_MEM?

The rest looks fine to me. The other alternative would have been to use the TCGTempSet with the bitmap functions like in optimize.c, and use only 2 bits per temp. That something that can be done later though.

I changed the name to TS_MEM as suggested.

r~

#16 lore raw

PATCH v3 8/9 tcg: Include liveness info in the dumps

In reply to Richard Henderson
---
 tcg/tcg.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tcg/tcg.c +26 -0
index fd92b06..3e4bc99 100644
@@ -1009,6 +1009,7 @@ void tcg_dump_ops(TCGContext *s)
         const TCGOpDef *def;
         const TCGArg *args;
         TCGOpcode c;
+        long pos = ftell(qemu_logfile);
 
         op = &s->gen_op_buf[oi];
         c = op->opc;
@@ -1133,6 +1134,31 @@ void tcg_dump_ops(TCGContext *s)
                 qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", args[k]);
             }
         }
+        if (op->life) {
+            unsigned life = op->life;
+
+            for (i = ftell(qemu_logfile) - pos; i < 48; ++i) {
+                putc(' ', qemu_logfile);
+            }
+
+            if (life & (SYNC_ARG * 3)) {
+                qemu_log("  sync:");
+                for (i = 0; i < 2; ++i) {
+                    if (life & (SYNC_ARG << i)) {
+                        qemu_log(" %d", i);
+                    }
+                }
+            }
+            life /= DEAD_ARG;
+            if (life) {
+                qemu_log("  dead:");
+                for (i = 0; life; ++i, life >>= 1) {
+                    if (life & 1) {
+                        qemu_log(" %d", i);
+                    }
+                }
+            }
+        }
         qemu_log("\n");
     }
 }
Signature · 1 line
2.5.5
#18 lore raw

Re: [Qemu-devel] [PATCH v3 8/9] tcg: Include liveness info in the dumps

In reply to Richard Henderson
Quoted 10 lines · Richard Henderson
---
 tcg/tcg.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tcg/tcg.c +0 -0
index fd92b06..3e4bc99 100644

tcg/tcg.c void tcg_dump_ops(TCGContext *s) · line 1013

Quoted 5 lines
tcg/tcg.c +1 -0
@@ -1009,6 +1009,7 @@ void tcg_dump_ops(TCGContext *s)
         const TCGOpDef *def;
         const TCGArg *args;
         TCGOpcode c;
+        long pos = ftell(qemu_logfile);
 

Small additional note: ftell() doesn't work well when the logfile is the terminal. As such when not dumping to the file, the alignment is wrong.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#19 lore raw

PATCH v3 9/9 tcg: Lower indirect registers in a separate pass

In reply to Richard Henderson

Rather than rely on recursion during the middle of register allocation, lower indirect registers to loads and stores off the indirect base into plain temps.

For an x86_64 host, with sufficient registers, this results in identical code, modulo the actual register assignments.

For an i686 host, with insufficient registers, this means that temps can be (temporarily) spilled to the stack in order to satisfy an allocation. This as opposed to the possibility of not being able to spill, to allocate a register for the indirect base, in order to perform a spill.

---
 include/qemu/log.h |   1 +
 tcg/optimize.c     |  31 +-----
 tcg/tcg.c          | 306 +++++++++++++++++++++++++++++++++++++++++++----------
 tcg/tcg.h          |   4 +
 util/log.c         |   5 +-
 5 files changed, 263 insertions(+), 84 deletions(-)
include/qemu/log.h +1 -0
index 8bec6b4..9c1cc38 100644
@@ -42,6 +42,7 @@ static inline bool qemu_log_separate(void)
 #define CPU_LOG_TB_NOCHAIN (1 << 13)
 #define CPU_LOG_PAGE       (1 << 14)
 #define LOG_TRACE          (1 << 15)
+#define CPU_LOG_TB_OP_IND  (1 << 16)
 
 /* Returns true if a bit is set in the current loglevel mask
  */
tcg/optimize.c +2 -29
index 8df7fc7..cffe89b 100644
@@ -82,33 +82,6 @@ static void init_temp_info(TCGArg temp)
     }
 }
 
-static TCGOp *insert_op_before(TCGContext *s, TCGOp *old_op,
-                                TCGOpcode opc, int nargs)
-{
-    int oi = s->gen_next_op_idx;
-    int pi = s->gen_next_parm_idx;
-    int prev = old_op->prev;
-    int next = old_op - s->gen_op_buf;
-    TCGOp *new_op;
-
-    tcg_debug_assert(oi < OPC_BUF_SIZE);
-    tcg_debug_assert(pi + nargs <= OPPARAM_BUF_SIZE);
-    s->gen_next_op_idx = oi + 1;
-    s->gen_next_parm_idx = pi + nargs;
-
-    new_op = &s->gen_op_buf[oi];
-    *new_op = (TCGOp){
-        .opc = opc,
-        .args = pi,
-        .prev = prev,
-        .next = next
-    };
-    s->gen_op_buf[prev].next = oi;
-    old_op->prev = oi;
-
-    return new_op;
-}
-
 static int op_bits(TCGOpcode op)
 {
     const TCGOpDef *def = &tcg_op_defs[op];
@@ -1116,7 +1089,7 @@ void tcg_optimize(TCGContext *s)
                 uint64_t a = ((uint64_t)ah << 32) | al;
                 uint64_t b = ((uint64_t)bh << 32) | bl;
                 TCGArg rl, rh;
-                TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
+                TCGOp *op2 = tcg_op_insert_before(s, op, INDEX_op_movi_i32, 2);
                 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
 
                 if (opc == INDEX_op_add2_i32) {
@@ -1142,7 +1115,7 @@ void tcg_optimize(TCGContext *s)
                 uint32_t b = temps[args[3]].val;
                 uint64_t r = (uint64_t)a * b;
                 TCGArg rl, rh;
-                TCGOp *op2 = insert_op_before(s, op, INDEX_op_movi_i32, 2);
+                TCGOp *op2 = tcg_op_insert_before(s, op, INDEX_op_movi_i32, 2);
                 TCGArg *args2 = &s->gen_opparam_buf[op2->args];
 
                 rl = args[0];
tcg/tcg.c +253 -53

385 lines not on this page · raw message

tcg/tcg.h +4 -0
index 2ff3ad2..c726355 100644
@@ -547,6 +547,7 @@ struct TCGContext {
     int nb_labels;
     int nb_globals;
     int nb_temps;
+    int nb_indirects;
 
     /* goto_tb support */
     tcg_insn_unit *code_buf;
@@ -839,6 +840,9 @@ void tcg_gen_callN(TCGContext *s, void *func,
                    TCGArg ret, int nargs, TCGArg *args);
 
 void tcg_op_remove(TCGContext *s, TCGOp *op);
+TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
+TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *op, TCGOpcode opc, int narg);
+
 void tcg_optimize(TCGContext *s);
 
 /* only used for debugging purposes */
util/log.c +3 -2
index 32e4160..7760d2a 100644
@@ -239,8 +239,9 @@ const QEMULogItem qemu_log_items[] = {
     { CPU_LOG_TB_OP, "op",
       "show micro ops for each compiled TB" },
     { CPU_LOG_TB_OP_OPT, "op_opt",
-      "show micro ops (x86 only: before eflags optimization) and\n"
-      "after liveness analysis" },
+      "show micro ops after optimization" },
+    { CPU_LOG_TB_OP_IND, "op_ind",
+      "show micro ops before indirect lowering" },
     { CPU_LOG_INT, "int",
       "show interrupts/exceptions in short format" },
     { CPU_LOG_EXEC, "exec",
Signature · 1 line
2.5.5
Reviewed-by on v3 9/9
#20 lore raw

Re: [Qemu-devel] [PATCH v3 9/9] tcg: Lower indirect registers in a separate pass

Quoted 12 lines · Richard Henderson

Rather than rely on recursion during the middle of register allocation, lower indirect registers to loads and stores off the indirect base into plain temps.

For an x86_64 host, with sufficient registers, this results in identical code, modulo the actual register assignments.

For an i686 host, with insufficient registers, this means that temps can be (temporarily) spilled to the stack in order to satisfy an allocation. This as opposed to the possibility of not being able to spill, to allocate a register for the indirect base, in order to perform a spill.

Quoted 8 lines
---
 include/qemu/log.h |   1 +
 tcg/optimize.c     |  31 +-----
 tcg/tcg.c          | 306 +++++++++++++++++++++++++++++++++++++++++++----------
 tcg/tcg.h          |   4 +
 util/log.c         |   5 +-
 5 files changed, 263 insertions(+), 84 deletions(-)

This patch is a difficult one to review... On the purely technical side it does what it is supposed to do and I haven't found any issue, though it's probably very easy to miss one in this kind of code. I have done tests with various sparc images and I haven't found any obvious regression on an x86_64 host.

Now on the less technical side, I really like the idea of being able to transform more or less in place the TCG instruction stream. Your more or less recent patches towards that direction are great. That said I am a bit worried that we loop many times on the various ops. We used to have one forward pass (optimizer) and one backward pass (liveness analysis). Your patch adds up to two additional passes (one forward and one backward), this clearly has a cost. Given that indirect registers bring a lot of performance I think it is worth it. Now I wonder if there is any way to do the lowering of registers earlier, I mean before the liveness analysis. This would probably generate plenty of useless ops, but that are later removed by the liveness analysis. Maybe you have already try that?

I think it also depends on which direction we want to go with TCG, either plenty of small independent optimization passes, or keep the number of passes limited which means more complex code. Contrary to a compiler we have to do a much more difficult trade-off between the optimization time and the level of optimization.

Nevertheless I think it's the correct way to go forward for now and this patch fixes real issues on hosts with limited registers. Maybe just add a note saying there *might* be better ways to do that.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net
#21 lore raw

Re: [Qemu-devel] [PATCH v3 9/9] tcg: Lower indirect registers in a separate pass

Quoted 4 lines · Aurelien Jarno

Now on the less technical side, I really like the idea of being able to transform more or less in place the TCG instruction stream. Your more or less recent patches towards that direction are great. That said I am a bit worried that we loop many times on the various ops. We used to have

Quoted 8 lines

one forward pass (optimizer) and one backward pass (liveness analysis). Your patch adds up to two additional passes (one forward and one backward), this clearly has a cost. Given that indirect registers bring a lot of performance I think it is worth it. Now I wonder if there is any way to do the lowering of registers earlier, I mean before the liveness analysis. This would probably generate plenty of useless ops, but that are later removed by the liveness analysis. Maybe you have already try that?

No, I did not try that, simply because we don't do liveness analysis of memory.
And that's what we have with lowering indirect registers earlier. Indeed, it
means we're right back where we were before introducing them.

We need liveness analysis on the tcg globals in order to know where to add the reads and writes. I see no way around that.

The one place where the code could be improved to remove a pass is to have the indirect lowering pass update liveness at the same time. We need accurate liveness in order to satisfy the asserts in the final code generation pass, so we have to do something. I simply thought it was easier to re-run the original liveness pass rather than complicating the indirect lowering pass.

Quoted 5 lines

I think it also depends on which direction we want to go with TCG, either plenty of small independent optimization passes, or keep the number of passes limited which means more complex code. Contrary to a compiler we have to do a much more difficult trade-off between the optimization time and the level of optimization.

Indeed. Fewer passes over large amounts of data is better, but I'm not sure we have "large" amounts of data for the average TB. On the other hand, smaller passes can reduce the code size of any one loop so that each fits in icache when one unified pass might not.

r~

#22 lore raw
In reply to Richard Henderson
Quoted 34 lines · Richard Henderson

I was unhappy about the complexity of the second try.

Better to convert to normal temps, allowing in rare occasions, spilling the "globals" to the stack in order to satisfy register allocation.

I can no longer provoke an allocation failure on i686. Hopefully this fixes the OpenBSD case that Mark mentioned re the second attempt.

r~

Richard Henderson (9):
tcg: Fix name for high-half register
tcg: Optimize spills of constants
tcg: Require liveness analysis
tcg: Compress liveness data to 16 bits
tcg: Reorg TCGOp chaining
tcg: Fold life data into TCGOp
tcg: Compress dead_temps and mem_temps into a single array
tcg: Include liveness info in the dumps
tcg: Lower indirect registers in a separate pass

 include/exec/gen-icount.h    |   2 +-
 include/qemu/log.h           |   1 +
 tcg/aarch64/tcg-target.inc.c |  10 +
 tcg/arm/tcg-target.inc.c     |   6 +
 tcg/i386/tcg-target.inc.c    |  21 +-
 tcg/ia64/tcg-target.inc.c    |  10 +
 tcg/mips/tcg-target.inc.c    |  10 +
 tcg/optimize.c               |  37 +--
 tcg/ppc/tcg-target.inc.c     |   6 +
Quoted 8 lines
 tcg/s390/tcg-target.inc.c    |   6 +
 tcg/sparc/tcg-target.inc.c   |  10 +
 tcg/tcg-op.c                 |   2 +-
 tcg/tcg.c                    | 690 ++++++++++++++++++++++++++++---------------
 tcg/tcg.h                    |  50 ++--
 tcg/tci/tcg-target.inc.c     |   6 +
 util/log.c                   |   5 +-
 16 files changed, 563 insertions(+), 309 deletions(-)

Hi Richard,

I gave this patchset a quick test, and I see no regressions with most of the images, however while the OpenBSD 5.5 boot gets further I still see (a different type) of panic:

$ ./qemu-system-sparc64 -cdrom /home/build/install55.iso -boot d -nographic
OpenBIOS for Sparc64
Configuration device id QEMU version 1 machine id 0
kernel cmdline
CPUs: 1 x SUNW,UltraSPARC-IIi
UUID: 00000000-0000-0000-0000-000000000000
Welcome to OpenBIOS v1.1 built on Apr 18 2016 08:20
  Type 'help' for detailed information
Trying cdrom:f...
Not a bootable ELF image
Not a bootable a.out image

Loading FCode image...
Loaded 4829 bytes
entry point is 0x4000
OpenBSD IEEE 1275 Bootblock 1.3
..
Jumping to entry point 0000000000100000 for type 0000000000000001... switching to new context: entry point 0x100000 stack 0x00000000ffe84a09

Quoted 1 line
Quoted 1 line

OpenBSD BOOT 1.6

Trying bsd...
open /pci@1fe,0/pci-ata@5/ide1@8200/cdrom@0:f/etc/random.seed: No such file or directory Booting /pci@1fe,0/pci-ata@5/ide1@8200/cdrom@0:f/bsd 3901336@0x1000000+6248@0x13b8798+3261984@0x1800000+932320@0x1b1c620 symbols @ 0xffc5a300 119 start=0x1000000

Unexpected client interface exception: -1
console is /pci@1fe,0/ebus@3/su
Copyright (c) 1982, 1986, 1989, 1991, 1993
The Regents of the University of California. All rights reserved.
Copyright (c) 1995-2014 OpenBSD. All rights reserved. http://www.OpenBSD.org

OpenBSD 5.5 (RAMDISK) #153: Tue Mar 4 15:12:10 MST 2014
[email protected]:/usr/src/sys/arch/sparc64/compile/RAMDISK
real mem = 134217728 (128MB)
avail mem = 122011648 (116MB)
mainbus0 at root: OpenBiosTeam,OpenBIOS
cpu0 at mainbus0: SUNW,UltraSPARC-IIi (rev 9.1) @ 100 MHz
cpu0: physical 256K instruction (64 b/l), 16K data (32 b/l), 256K external (64 b/l)
psycho0 at mainbus0: SUNW,sabre, impl 0, version 0, ign 7c0
psycho0: bus range 0-2, PCI bus 0
psycho0: dvma map c0000000-dfffffff
pci0 at psycho0
ppb0 at pci0 dev 1 function 0 "Sun Simba" rev 0x11
pci1 at ppb0 bus 1
ppb1 at pci0 dev 1 function 1 "Sun Simba" rev 0x11
pci2 at ppb1 bus 2
unknown vendor 0x1234 product 0x1111 (class display subclass VGA, rev 0x02) at pci0 dev 2 function 0 not configured ebus0 at pci0 dev 3 function 0 "Sun PCIO EBus2" rev 0x01
clock1 at ebus0 addr 2000-3fff: mk48t59
"fdthree" at ebus0 addr 0-ffffffff not configured
com0 at ebus0 addr 3f8-3ff ivec 0x2b: ns16550a, 16 byte fifo
com0: console
"kb_ps2" at ebus0 addr 60-67 not configured
"Realtek 8029" rev 0x00 at pci0 dev 4 function 0 not configured
pciide0 at pci0 dev 5 function 0 "CMD Technology PCI0646" rev 0x07: DMA, channel 0 configured to native-PCI, channel 1 configured to native-PCI
pciide0: using ivec 0x7d4 for native-PCI interrupt
pciide0: channel 0 disabled (no drives)
atapiscsi0 at pciide0 channel 1 drive 0
scsibus0 at atapiscsi0: 2 targets
cd0 at scsibus0 targ 0 lun 0: <QEMU, QEMU DVD-ROM, 2.5+> ATAPI 5/cdrom removable
cd0(pciide0:1:0): using PIO mode 4, Ultra-DMA mode 2
softraid0 at root
scsibus1 at softraid0: 256 targets
bootpath: /pci@1fe,0/pci-ata@5,0/ide1@8200,0/cdrom@0,0:f
root on rd0a swap on rd0b dump on rd0b
erase ^?, weraseqemu: fatal: Trap 0x0030 while trap level (5) >= MAXTL (5), Error state
pc: 0000000001011d9c npc: 0000000001011da0
%g0-3: 0000000000000000 fffffc00d9d49470 0000000000000000 0000000000000000
%g4-7: fffffc00d9d48000 00000400e0017619 00000400e0017619 0000000000000017
%o0-3: 00000400e0016000 0000000000000030 0000000000000000 0000000000000000
%o4-7: 00000400e0016000 000000000000082d 00000400062ce391 0000000001011d9c
%l0-3: 00000400026a9970 00000000008e7000 00000000008e7000 00000000008e7000
%l4-7: 00000400062cfbf0 0000000000000000 000004000269a770 0000000000767b80
%i0-3: 00000400e0016000 0000000000000030 0000000000000000 0000000000000000
%i4-7: 00000400e0016000 000000000000082d 00000400062ce571 0000000001011d9c

%f00:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f08:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f16:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f24:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f32:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f40:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f48:  0000000000000000 0000000000000000 0000000000000000 0000000000000000
%f56:  0000000000000000 0000000000000000 0000000000000000 0000000000000000

pstate: 00000015 ccr: 00 (icc: ---- xcc: ----) asi: 82 tl: 5 pil: 0
cansave: 6 canrestore: 0 otherwin: 0 wstate: 23 cleanwin: 6 cwp: 5
fsr: 0000000000000000 y: 00000000000000b3 fprs: 0000000000000004

Aborted

I'm starting to wonder if there is something wrong with my i386 VM I spun up to test this...

ATB,

Mark.

#23 lore raw
In reply to Richard Henderson
Quoted 9 lines · Richard Henderson

I was unhappy about the complexity of the second try.

Better to convert to normal temps, allowing in rare occasions, spilling the "globals" to the stack in order to satisfy register allocation.

I can no longer provoke an allocation failure on i686. Hopefully this fixes the OpenBSD case that Mark mentioned re the second attempt.

Ping for review. It would be nice to have this fixed for 2.7, but this is complex enough I'd prefer another set of eyes.

r~

#24 lore raw
Quoted 7 lines · Richard Henderson
Quoted 6 lines · Richard Henderson

I was unhappy about the complexity of the second try.

Better to convert to normal temps, allowing in rare occasions, spilling the "globals" to the stack in order to satisfy register allocation.

Quoted 6 lines
Quoted 3 lines

I can no longer provoke an allocation failure on i686. Hopefully this fixes the OpenBSD case that Mark mentioned re the second attempt.

Ping for review. It would be nice to have this fixed for 2.7, but this is complex enough I'd prefer another set of eyes.

I'll try to have a look during the week-end. Sorry about the delay.

Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                 http://www.aurel32.net