Skip to contentSkip to conversation

tcg: Introduce INDEX_op_lea

PATCH Superseded Richard Henderson 3 participants 32 messages Aug 28 – Aug 31 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover tcg: Introduce INDEX_op_lea

Add a complex addressing mode primitive, and some trivial use cases.

This came up in conversation with Matt Turner re his inline jump cache expansion. In particular,

-    tcg_gen_shli_i64(h, h, 4);
-    tcg_gen_add_i64(ent, jc, ent);
+    tcg_gen_lea_i64(ent, jc, h, 4, 0);

With the use case in target/i386/tcg, we get nearly full coverage of all the possible use cases. The only thing I didn't immediately see is shifts > 3, which could be visible with loongarch64 guests.

There are plenty of other places in target/ that could use this, but the shift/add/imm extraction/generation are inconveniently separated. E.g. m68k address generation, arm add-shifted, etc.

It may not even be worth converting those, but instead adding some trivial peepholing along the lines of Matt's user-only guest displacement.

r~

Richard Henderson (16):
tcg: Sort all_outop[] by INDEX name
tcg: Add INDEX_op_lea
tcg: Add tcg_gen_lea_{i32,i64,tl} tcg/optimize: Handle INDEX_op_lea target/alpha: Use tcg_gen_lea_i64 for S{4,8}ADD{L,Q} target/loongarch: Use tcg_gen_lea_tl for ALSL target/riscv: Use tcg_gen_lea_tl for SH*ADD target/s390x: Use tcg_gen_lea_i64 in get_address target/i386: Use tcg_gen_lea_tl in gen_lea_modrm_1 tcg/x86_64: Introduce TCG_REG_NONE tcg/x86_64: Implement INDEX_op_lea tcg/loongarch64: Import SLADD insns tcg/loongarch64: Implement INDEX_op_lea tcg/aarch64: Implement INDEX_op_lea tcg/riscv64: Implement INDEX_op_lea tcg/s390x: Implement INDEX_op_lea

 include/tcg/tcg-op-common.h                   |  4 +
 include/tcg/tcg-op.h                          |  2 +
 include/tcg/tcg-opc.h                         |  1 +
 tcg/aarch64/tcg-target-has.h                  |  4 +
 tcg/loongarch64/tcg-target-has.h              |  3 +
 tcg/ppc64/tcg-target-has.h                    |  3 +
 tcg/riscv64/tcg-target-has.h                  | 11 +++
 tcg/s390x/tcg-target-has.h                    |  4 +
 tcg/sparc64/tcg-target-has.h                  |  3 +
 tcg/tci/tcg-target-has.h                      |  3 +
 tcg/x86_64/tcg-target-con-set.h               |  1 +
 tcg/x86_64/tcg-target-has.h                   |  3 +
 tcg/x86_64/tcg-target.h                       |  3 +
 target/alpha/translate.c                      | 20 ++---
 target/i386/tcg/translate.c                   | 27 +++---
 target/s390x/tcg/translate.c                  |  4 +-
 tcg/optimize.c                                | 65 ++++++++++++++
 tcg/tcg-op.c                                  | 90 +++++++++++++++++++
 tcg/tcg.c                                     | 61 ++++++++-----
 docs/devel/tcg-ops.rst                        |  4 +
 .../tcg/insn_trans/trans_arith.c.inc          |  4 +-
 target/riscv/tcg/insn_trans/trans_rvb.c.inc   |  5 +-
 tcg/aarch64/tcg-target.c.inc                  | 11 +++
 tcg/loongarch64/tcg-insn-defs.c.inc           | 30 ++++++-
 tcg/loongarch64/tcg-target.c.inc              | 15 ++++
 tcg/ppc64/tcg-target.c.inc                    |  3 +
 tcg/riscv64/tcg-target.c.inc                  | 17 ++++
 tcg/s390x/tcg-target.c.inc                    | 14 +++
 tcg/sparc64/tcg-target.c.inc                  |  3 +
 tcg/tci/tcg-target.c.inc                      |  4 +
 tcg/x86_64/tcg-target.c.inc                   | 79 +++++++++-------
 31 files changed, 413 insertions(+), 88 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/16 tcg: Sort all_outop[] by INDEX name

Most of the opcodes were already sorted, but then there were a set at the bottom that were left over from the split between 32 and 64-bit hosts.

---
 tcg/tcg.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
tcg/tcg.c +19 -20
index 489df0e7386..ecc1f54c1bf 100644
@@ -1158,32 +1158,39 @@ static const TCGOutOpLoad outop_ld = {
 /* Register allocation descriptions for every TCGOpcode. */
 static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_add, TCGOutOpBinary, outop_add),
+    /* addc1o is implemented with set_carry + addcio */
+    OUTOP(INDEX_op_addc1o, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_addci, TCGOutOpAddSubCarry, outop_addci),
     OUTOP(INDEX_op_addcio, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_addco, TCGOutOpBinary, outop_addco),
-    /* addc1o is implemented with set_carry + addcio */
-    OUTOP(INDEX_op_addc1o, TCGOutOpBinary, outop_addcio),
     OUTOP(INDEX_op_and, TCGOutOpBinary, outop_and),
     OUTOP(INDEX_op_andc, TCGOutOpBinary, outop_andc),
     OUTOP(INDEX_op_brcond, TCGOutOpBrcond, outop_brcond),
     OUTOP(INDEX_op_bswap16, TCGOutOpBswap, outop_bswap16),
     OUTOP(INDEX_op_bswap32, TCGOutOpBswap, outop_bswap32),
+    OUTOP(INDEX_op_bswap64, TCGOutOpUnary, outop_bswap64),
     OUTOP(INDEX_op_clz, TCGOutOpBinary, outop_clz),
     OUTOP(INDEX_op_ctpop, TCGOutOpUnary, outop_ctpop),
     OUTOP(INDEX_op_ctz, TCGOutOpBinary, outop_ctz),
     OUTOP(INDEX_op_deposit, TCGOutOpDeposit, outop_deposit),
     OUTOP(INDEX_op_divs, TCGOutOpBinary, outop_divs),
-    OUTOP(INDEX_op_divu, TCGOutOpBinary, outop_divu),
     OUTOP(INDEX_op_divs2, TCGOutOpDivRem, outop_divs2),
+    OUTOP(INDEX_op_divu, TCGOutOpBinary, outop_divu),
     OUTOP(INDEX_op_divu2, TCGOutOpDivRem, outop_divu2),
     OUTOP(INDEX_op_eqv, TCGOutOpBinary, outop_eqv),
     OUTOP(INDEX_op_extract, TCGOutOpExtract, outop_extract),
     OUTOP(INDEX_op_extract2, TCGOutOpExtract2, outop_extract2),
-    OUTOP(INDEX_op_ld8u, TCGOutOpLoad, outop_ld8u),
-    OUTOP(INDEX_op_ld8s, TCGOutOpLoad, outop_ld8s),
-    OUTOP(INDEX_op_ld16u, TCGOutOpLoad, outop_ld16u),
-    OUTOP(INDEX_op_ld16s, TCGOutOpLoad, outop_ld16s),
+    OUTOP(INDEX_op_ext_i32_i64, TCGOutOpUnary, outop_exts_i32_i64),
+    OUTOP(INDEX_op_extrh_i64_i32, TCGOutOpUnary, outop_extrh_i64_i32),
+    OUTOP(INDEX_op_extrl_i64_i32, TCGOutOpUnary, outop_extrl_i64_i32),
+    OUTOP(INDEX_op_extu_i32_i64, TCGOutOpUnary, outop_extu_i32_i64),
     OUTOP(INDEX_op_ld, TCGOutOpLoad, outop_ld),
+    OUTOP(INDEX_op_ld8s, TCGOutOpLoad, outop_ld8s),
+    OUTOP(INDEX_op_ld8u, TCGOutOpLoad, outop_ld8u),
+    OUTOP(INDEX_op_ld16s, TCGOutOpLoad, outop_ld16s),
+    OUTOP(INDEX_op_ld16u, TCGOutOpLoad, outop_ld16u),
+    OUTOP(INDEX_op_ld32s, TCGOutOpLoad, outop_ld32s),
+    OUTOP(INDEX_op_ld32u, TCGOutOpLoad, outop_ld32u),
     OUTOP(INDEX_op_movcond, TCGOutOpMovcond, outop_movcond),
     OUTOP(INDEX_op_mul, TCGOutOpBinary, outop_mul),
     OUTOP(INDEX_op_muls2, TCGOutOpMul2, outop_muls2),
@@ -1203,7 +1210,9 @@ static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_qemu_st2, TCGOutOpQemuLdSt2, outop_qemu_st2),
     OUTOP(INDEX_op_rems, TCGOutOpBinary, outop_rems),
     OUTOP(INDEX_op_remu, TCGOutOpBinary, outop_remu),
+    OUTOP(INDEX_op_revbit8, TCGOutOpUnary, outop_revbit8),
     OUTOP(INDEX_op_revbit32, TCGOutOpBswap, outop_revbit32),
+    OUTOP(INDEX_op_revbit64, TCGOutOpUnary, outop_revbit64),
     OUTOP(INDEX_op_rotl, TCGOutOpBinary, outop_rotl),
     OUTOP(INDEX_op_rotr, TCGOutOpBinary, outop_rotr),
     OUTOP(INDEX_op_sar, TCGOutOpBinary, outop_sar),
@@ -1216,28 +1225,18 @@ static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_st, TCGOutOpStore, outop_st),
     OUTOP(INDEX_op_st8, TCGOutOpStore, outop_st8),
     OUTOP(INDEX_op_st16, TCGOutOpStore, outop_st16),
+    OUTOP(INDEX_op_st32, TCGOutOpStore, outop_st),
     OUTOP(INDEX_op_sub, TCGOutOpSubtract, outop_sub),
+    /* subb1o is implemented with set_borrow + subbio */
+    OUTOP(INDEX_op_subb1o, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_subbi, TCGOutOpAddSubCarry, outop_subbi),
     OUTOP(INDEX_op_subbio, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_subbo, TCGOutOpAddSubCarry, outop_subbo),
-    /* subb1o is implemented with set_borrow + subbio */
-    OUTOP(INDEX_op_subb1o, TCGOutOpAddSubCarry, outop_subbio),
     OUTOP(INDEX_op_umax, TCGOutOpBinary, outop_umax),
     OUTOP(INDEX_op_umin, TCGOutOpBinary, outop_umin),
     OUTOP(INDEX_op_xor, TCGOutOpBinary, outop_xor),
 
     [INDEX_op_goto_ptr] = &outop_goto_ptr,
-
-    OUTOP(INDEX_op_bswap64, TCGOutOpUnary, outop_bswap64),
-    OUTOP(INDEX_op_ext_i32_i64, TCGOutOpUnary, outop_exts_i32_i64),
-    OUTOP(INDEX_op_extu_i32_i64, TCGOutOpUnary, outop_extu_i32_i64),
-    OUTOP(INDEX_op_extrl_i64_i32, TCGOutOpUnary, outop_extrl_i64_i32),
-    OUTOP(INDEX_op_extrh_i64_i32, TCGOutOpUnary, outop_extrh_i64_i32),
-    OUTOP(INDEX_op_ld32u, TCGOutOpLoad, outop_ld32u),
-    OUTOP(INDEX_op_ld32s, TCGOutOpLoad, outop_ld32s),
-    OUTOP(INDEX_op_revbit8, TCGOutOpUnary, outop_revbit8),
-    OUTOP(INDEX_op_revbit64, TCGOutOpUnary, outop_revbit64),
-    OUTOP(INDEX_op_st32, TCGOutOpStore, outop_st),
 };
 
 #undef OUTOP
Signature · 1 line
2.53.0
Reviewed-by on v1 1/16
#2 lore raw

Re: [PATCH 01/16] tcg: Sort all_outop[] by INDEX name

Quoted 8 lines · Richard Henderson

Most of the opcodes were already sorted, but then there were a set at the bottom that were left over from the split between 32 and 64-bit hosts.

---
  tcg/tcg.c | 39 +++++++++++++++++++--------------------
  1 file changed, 19 insertions(+), 20 deletions(-)
#3 lore raw

PATCH 2/16 tcg: Add INDEX_op_lea

In reply to Richard Henderson

Add an opcode for compound address formation.

---
 include/tcg/tcg-opc.h            |  1 +
 tcg/aarch64/tcg-target-has.h     |  3 +++
 tcg/loongarch64/tcg-target-has.h |  3 +++
 tcg/ppc64/tcg-target-has.h       |  3 +++
 tcg/riscv64/tcg-target-has.h     |  3 +++
 tcg/s390x/tcg-target-has.h       |  3 +++
 tcg/sparc64/tcg-target-has.h     |  3 +++
 tcg/tci/tcg-target-has.h         |  3 +++
 tcg/x86_64/tcg-target-has.h      |  3 +++
 tcg/tcg.c                        | 22 ++++++++++++++++++++++
 docs/devel/tcg-ops.rst           |  4 ++++
 tcg/aarch64/tcg-target.c.inc     |  4 ++++
 tcg/loongarch64/tcg-target.c.inc |  4 ++++
 tcg/ppc64/tcg-target.c.inc       |  3 +++
 tcg/riscv64/tcg-target.c.inc     |  3 +++
 tcg/s390x/tcg-target.c.inc       |  3 +++
 tcg/sparc64/tcg-target.c.inc     |  3 +++
 tcg/tci/tcg-target.c.inc         |  4 ++++
 tcg/x86_64/tcg-target.c.inc      |  4 ++++
 19 files changed, 79 insertions(+)
include/tcg/tcg-opc.h +1 -0
index f3a81d5d7fb..884345afe56 100644
@@ -64,6 +64,7 @@ DEF(ld16s, 1, 1, 1, TCG_OPF_INT)
 DEF(ld32u, 1, 1, 1, TCG_OPF_INT)
 DEF(ld32s, 1, 1, 1, TCG_OPF_INT)
 DEF(ld, 1, 1, 1, TCG_OPF_INT)
+DEF(lea, 1, 2, 2, TCG_OPF_INT)
 DEF(movcond, 1, 4, 1, TCG_OPF_INT)
 DEF(mul, 1, 2, 0, TCG_OPF_INT)
 DEF(muls2, 2, 2, 0, TCG_OPF_INT)
tcg/aarch64/tcg-target-has.h +3 -0
index 69e83efb69d..86672b2eab4 100644
@@ -57,4 +57,7 @@
 #define TCG_TARGET_sextract_valid(type, ofs, len)  1
 #define TCG_TARGET_deposit_valid(type, ofs, len)   1
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/loongarch64/tcg-target-has.h +3 -0
index 32abc6f4574..31e6ea9632d 100644
@@ -54,4 +54,7 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/ppc64/tcg-target-has.h +3 -0
index a3711feeae2..d11d82f1844 100644
@@ -66,4 +66,7 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/riscv64/tcg-target-has.h +3 -0
index aef10c2d9d6..93f8baad09f 100644
@@ -69,4 +69,7 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 
 #define TCG_TARGET_deposit_valid(type, ofs, len)  0
 
+#define TCG_TARGET_lea_sh_valid(type, sh)         0
+#define TCG_TARGET_lea_imm_valid(type, imm)       0
+
 #endif
tcg/s390x/tcg-target-has.h +3 -0
index 0aeb5ba01a9..58e3afe5266 100644
@@ -77,4 +77,7 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/sparc64/tcg-target-has.h +3 -0
index b29fd177f69..4182a7c1aae 100644
@@ -19,4 +19,7 @@
 
 #define TCG_TARGET_deposit_valid(type, ofs, len) 0
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/tci/tcg-target-has.h +3 -0
index 64742cf0b71..f4f9ddf595f 100644
@@ -17,4 +17,7 @@
 #define TCG_TARGET_sextract_valid(type, ofs, len)  1
 #define TCG_TARGET_deposit_valid(type, ofs, len)   1
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/x86_64/tcg-target-has.h +3 -0
index d249c1b3e7f..6ffae51f923 100644
@@ -105,4 +105,7 @@ tcg_target_extract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_extract_valid  tcg_target_extract_valid
 
+#define TCG_TARGET_lea_sh_valid(type, sh)          0
+#define TCG_TARGET_lea_imm_valid(type, imm)        0
+
 #endif
tcg/tcg.c +22 -0
index ecc1f54c1bf..c0e149e7eab 100644
@@ -1095,6 +1095,12 @@ typedef struct TCGOutOpSubtract {
                     TCGReg a0, tcg_target_long a1, TCGReg a2);
 } TCGOutOpSubtract;
 
+typedef struct TCGOutOpLea {
+    TCGOutOp base;
+    void (*out)(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                TCGReg a2, unsigned sh, tcg_target_long imm);
+} TCGOutOpLea;
+
 #include "tcg-target.c.inc"
 
 #ifndef CONFIG_TCG_INTERPRETER
@@ -1191,6 +1197,7 @@ static const TCGOutOp * const all_outop[NB_OPS] = {
     OUTOP(INDEX_op_ld16u, TCGOutOpLoad, outop_ld16u),
     OUTOP(INDEX_op_ld32s, TCGOutOpLoad, outop_ld32s),
     OUTOP(INDEX_op_ld32u, TCGOutOpLoad, outop_ld32u),
+    OUTOP(INDEX_op_lea, TCGOutOpLea, outop_lea),
     OUTOP(INDEX_op_movcond, TCGOutOpMovcond, outop_movcond),
     OUTOP(INDEX_op_mul, TCGOutOpBinary, outop_mul),
     OUTOP(INDEX_op_muls2, TCGOutOpMul2, outop_muls2),
@@ -5788,6 +5795,21 @@ static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
         }
         break;
 
+    case INDEX_op_lea:
+        {
+            const TCGOutOpLea *out = &outop_lea;
+            int sh = new_args[3];
+            tcg_target_long imm = new_args[4];
+
+            tcg_debug_assert(!const_args[1]);
+            tcg_debug_assert(!const_args[2]);
+            tcg_debug_assert(TCG_TARGET_lea_sh_valid(type, sh));
+            tcg_debug_assert(TCG_TARGET_lea_imm_valid(type, imm));
+
+            out->out(s, type, new_args[0], new_args[1], new_args[2], sh, imm);
+        }
+        break;
+
     case INDEX_op_goto_ptr:
         tcg_debug_assert(!const_args[0]);
         tcg_out_goto_ptr(s, new_args[0]);
docs/devel/tcg-ops.rst +4 -0
index 88d48a1f99a..51fa183e911 100644
@@ -524,6 +524,10 @@ Misc
 
      - | Reverse the 64 bits of input *t1* with output in *dest*.
 
+   * - lea *dest*, *t1*, *t2*, *sh*, *imm*
+
+     - | *dest* = *t1* + (*t2* << *sh*) + *imm*.
+
 Conditional moves
 -----------------
 
tcg/aarch64/tcg-target.c.inc +4 -0
index 4ebdef7e077..76adb2256f9 100644
@@ -3081,6 +3081,10 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
+
 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
                            unsigned vecl, unsigned vece,
                            const TCGArg args[TCG_MAX_OP_ARGS],
tcg/loongarch64/tcg-target.c.inc +4 -0
index 69e76807d6e..17784d8bbe2 100644
@@ -2098,6 +2098,10 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
+
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
                             TCGReg rd, TCGReg rs)
 {
tcg/ppc64/tcg-target.c.inc +3 -0
index cd1c2343672..99f4469f4b9 100644
@@ -3633,6 +3633,9 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
 
 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
 {
tcg/riscv64/tcg-target.c.inc +3 -0
index 2ce9d47a633..df4c4a9789b 100644
@@ -2725,6 +2725,9 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
 
 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
                            unsigned vecl, unsigned vece,
tcg/s390x/tcg-target.c.inc +3 -0
index 4d1a779c47c..2fd7012d047 100644
@@ -3187,6 +3187,9 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
 
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
                             TCGReg dst, TCGReg src)
tcg/sparc64/tcg-target.c.inc +3 -0
index 35cd14a5b65..cb457453ff9 100644
@@ -2120,6 +2120,9 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
 
 static TCGConstraintSetIndex
 tcg_target_op_def(TCGOpcode op, TCGType type, unsigned flags)
tcg/tci/tcg-target.c.inc +4 -0
index 4cd1c1431ce..6942fdbb4e0 100644
@@ -1220,6 +1220,10 @@ static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
     return false;
 }
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
+
 /* Test if a constant matches the constraint. */
 static bool tcg_target_const_match(int64_t val, int ct,
                                    TCGType type, TCGCond cond, int vece)
tcg/x86_64/tcg-target.c.inc +4 -0
index 2c8f1f3e580..660350b963d 100644
@@ -3327,6 +3327,10 @@ static const TCGOutOpStore outop_st = {
     .out_i = tgen_st_i,
 };
 
+static const TCGOutOpLea outop_lea = {
+    .base.static_constraint = C_NotImplemented,
+};
+
 static int const umin_insn[4] = {
     OPC_PMINUB, OPC_PMINUW, OPC_PMINUD, OPC_VPMINUQ
 };
Signature · 1 line
2.53.0
Reviewed-by on v1 2/16
#4 lore raw

Re: [PATCH 02/16] tcg: Add INDEX_op_lea

Quoted 16 lines · Richard Henderson

Add an opcode for compound address formation.

---
  include/tcg/tcg-opc.h            |  1 +
  tcg/aarch64/tcg-target-has.h     |  3 +++
  tcg/loongarch64/tcg-target-has.h |  3 +++
  tcg/ppc64/tcg-target-has.h       |  3 +++
  tcg/riscv64/tcg-target-has.h     |  3 +++
  tcg/s390x/tcg-target-has.h       |  3 +++
  tcg/sparc64/tcg-target-has.h     |  3 +++
  tcg/tci/tcg-target-has.h         |  3 +++
  tcg/x86_64/tcg-target-has.h      |  3 +++
  tcg/tcg.c                        | 22 ++++++++++++++++++++++
  docs/devel/tcg-ops.rst           |  4 ++++
  tcg/aarch64/tcg-target.c.inc     |  4 ++++
Quoted 8 lines
  tcg/loongarch64/tcg-target.c.inc |  4 ++++
  tcg/ppc64/tcg-target.c.inc       |  3 +++
  tcg/riscv64/tcg-target.c.inc     |  3 +++
  tcg/s390x/tcg-target.c.inc       |  3 +++
  tcg/sparc64/tcg-target.c.inc     |  3 +++
  tcg/tci/tcg-target.c.inc         |  4 ++++
  tcg/x86_64/tcg-target.c.inc      |  4 ++++
  19 files changed, 79 insertions(+)
#5 lore raw

PATCH 3/16 tcg: Add tcg_gen_lea_{i32,i64,tl}

In reply to Richard Henderson
---
 include/tcg/tcg-op-common.h |  4 ++
 include/tcg/tcg-op.h        |  2 +
 tcg/tcg-op.c                | 90 +++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)
include/tcg/tcg-op-common.h +4 -0
index 9b321f959c0..b06445b8031 100644
@@ -167,6 +167,8 @@ void tcg_gen_ussub_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_abs_i32(TCGv_i32, TCGv_i32);
 void tcg_gen_revbit8_i32(TCGv_i32 ret, TCGv_i32 arg);
 void tcg_gen_revbit32_i32(TCGv_i32 ret, TCGv_i32 arg);
+void tcg_gen_lea_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b,
+                     int32_t sh, int32_t imm);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
 void tcg_gen_dup_i32(unsigned vece, TCGv_i32 out, TCGv_i32 in);
@@ -282,6 +284,8 @@ void tcg_gen_abs_i64(TCGv_i64, TCGv_i64);
 void tcg_gen_revbit8_i64(TCGv_i64 ret, TCGv_i64 arg);
 void tcg_gen_revbit32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags);
 void tcg_gen_revbit64_i64(TCGv_i64 ret, TCGv_i64 arg);
+void tcg_gen_lea_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b,
+                     int64_t sh, int64_t imm);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
 void tcg_gen_dup_i64(unsigned vece, TCGv_i64 out, TCGv_i64 in);
include/tcg/tcg-op.h +2 -0
index 37211642365..a7d001c959c 100644
@@ -83,6 +83,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_shri_tl tcg_gen_shri_i64
 #define tcg_gen_sar_tl tcg_gen_sar_i64
 #define tcg_gen_sari_tl tcg_gen_sari_i64
+#define tcg_gen_lea_tl tcg_gen_lea_i64
 #define tcg_gen_brcond_tl tcg_gen_brcond_i64
 #define tcg_gen_brcondi_tl tcg_gen_brcondi_i64
 #define tcg_gen_setcond_tl tcg_gen_setcond_i64
@@ -209,6 +210,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_shri_tl tcg_gen_shri_i32
 #define tcg_gen_sar_tl tcg_gen_sar_i32
 #define tcg_gen_sari_tl tcg_gen_sari_i32
+#define tcg_gen_lea_tl tcg_gen_lea_i32
 #define tcg_gen_brcond_tl tcg_gen_brcond_i32
 #define tcg_gen_brcondi_tl tcg_gen_brcondi_i32
 #define tcg_gen_setcond_tl tcg_gen_setcond_i32
tcg/tcg-op.c +90 -0

102 lines not on this page · raw message

Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 03/16] tcg: Add tcg_gen_lea_{i32,i64,tl}

Quoted 55 lines · Richard Henderson
---
 include/tcg/tcg-op-common.h |  4 ++
 include/tcg/tcg-op.h        |  2 +
 tcg/tcg-op.c                | 90 +++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+)
include/tcg/tcg-op-common.h +4 -0
index 9b321f959c0..b06445b8031 100644
@@ -167,6 +167,8 @@ void tcg_gen_ussub_i32(TCGv_i32, TCGv_i32 arg1, TCGv_i32 arg2);
 void tcg_gen_abs_i32(TCGv_i32, TCGv_i32);
 void tcg_gen_revbit8_i32(TCGv_i32 ret, TCGv_i32 arg);
 void tcg_gen_revbit32_i32(TCGv_i32 ret, TCGv_i32 arg);
+void tcg_gen_lea_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b,
+                     int32_t sh, int32_t imm);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
 void tcg_gen_dup_i32(unsigned vece, TCGv_i32 out, TCGv_i32 in);
@@ -282,6 +284,8 @@ void tcg_gen_abs_i64(TCGv_i64, TCGv_i64);
 void tcg_gen_revbit8_i64(TCGv_i64 ret, TCGv_i64 arg);
 void tcg_gen_revbit32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags);
 void tcg_gen_revbit64_i64(TCGv_i64 ret, TCGv_i64 arg);
+void tcg_gen_lea_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b,
+                     int64_t sh, int64_t imm);
 
 /* Replicate a value of size @vece from @in to all the lanes in @out */
 void tcg_gen_dup_i64(unsigned vece, TCGv_i64 out, TCGv_i64 in);
include/tcg/tcg-op.h +2 -0
index 37211642365..a7d001c959c 100644
@@ -83,6 +83,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_shri_tl tcg_gen_shri_i64
 #define tcg_gen_sar_tl tcg_gen_sar_i64
 #define tcg_gen_sari_tl tcg_gen_sari_i64
+#define tcg_gen_lea_tl tcg_gen_lea_i64
 #define tcg_gen_brcond_tl tcg_gen_brcond_i64
 #define tcg_gen_brcondi_tl tcg_gen_brcondi_i64
 #define tcg_gen_setcond_tl tcg_gen_setcond_i64
@@ -209,6 +210,7 @@ typedef TCGv_i64 TCGv;
 #define tcg_gen_shri_tl tcg_gen_shri_i32
 #define tcg_gen_sar_tl tcg_gen_sar_i32
 #define tcg_gen_sari_tl tcg_gen_sari_i32
+#define tcg_gen_lea_tl tcg_gen_lea_i32
 #define tcg_gen_brcond_tl tcg_gen_brcond_i32
 #define tcg_gen_brcondi_tl tcg_gen_brcondi_i32
 #define tcg_gen_setcond_tl tcg_gen_setcond_i32
tcg/tcg-op.c +0 -0
index 2c88f4f7ca9..fb5fef9422d 100644

tcg/tcg-op.c void tcg_gen_sari_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2) · line 504

Quoted 7 lines
tcg/tcg-op.c +4 -0
@@ -498,6 +498,51 @@ void tcg_gen_sari_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
     }
 }
 
+void tcg_gen_lea_i32(TCGv_i32 ret, TCGv_i32 a1, TCGv_i32 a2,
+                     int32_t sh, int32_t imm)
+{
+    TCGv_i32 t = tcg_temp_ebb_new_i32();

Dunno if it's worth it, but we can sink this into (multiple) branches below and avoid emitting it in some cases. Same for tcg_gen_lea_i64.

#7 lore raw

PATCH 4/16 tcg/optimize: Handle INDEX_op_lea

In reply to Richard Henderson
---
 tcg/optimize.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tcg/optimize.c +65 -0
index d291c844ca8..83f95e26643 100644
@@ -2101,6 +2101,68 @@ static bool fold_extu(OptContext *ctx, TCGOp *op)
     return fold_masks_zo(ctx, op, z_mask, o_mask);
 }
 
+static bool fold_shift(OptContext *ctx, TCGOp *op);
+static bool fold_lea(OptContext *ctx, TCGOp *op)
+{
+    TCGArg sh = op->args[3];
+    TCGArg imm = op->args[4];
+    int var_mask = 0;
+
+    if (arg_is_const(op->args[1])) {
+        imm += arg_const_val(op->args[1]);
+    } else {
+        var_mask |= 1;
+    }
+    if (arg_is_const(op->args[2])) {
+        imm += arg_const_val(op->args[2]) << sh;
+    } else {
+        var_mask |= 2;
+    }
+
+    switch (var_mask) {
+    case 0:
+        return tcg_opt_gen_movi(ctx, op, op->args[0], imm);
+
+    case 1:
+        if (imm == 0) {
+            return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
+        }
+        op->opc = INDEX_op_add;
+        op->args[2] = arg_new_constant(ctx, imm);
+        break;
+
+    case 2:
+        if (sh == 0) {
+            if (imm == 0) {
+                return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
+            }
+            op->opc = INDEX_op_add;
+            op->args[1] = op->args[2];
+            op->args[2] = arg_new_constant(ctx, imm);
+            break;
+        }
+        if (imm == 0) {
+            op->opc = INDEX_op_shl;
+            op->args[1] = op->args[2];
+            op->args[2] = arg_new_constant(ctx, sh);
+            return fold_shift(ctx, op);
+        }
+
+        /* If arg1 was not already 0, fold the immediates. */
+        if (arg_const_val(op->args[1]) != 0) {
+            if (TCG_TARGET_lea_imm_valid(ctx->type, imm)) {
+                op->args[1] = arg_new_constant(ctx, 0);
+                op->args[4] = imm;
+            } else {
+                op->args[1] = arg_new_constant(ctx, imm);
+                op->args[4] = 0;
+            }
+        }
+        break;
+    }
+    return finish_folding(ctx, op);
+}
+
 static bool fold_mb(OptContext *ctx, TCGOp *op)
 {
     /* Eliminate duplicate and redundant fence instructions.  */
@@ -3209,6 +3271,9 @@ void tcg_optimize(TCGContext *s)
         case INDEX_op_st_vec:
             done = fold_tcg_st_memcopy(&ctx, op);
             break;
+        case INDEX_op_lea:
+            done = fold_lea(&ctx, op);
+            break;
         case INDEX_op_mb:
             done = fold_mb(&ctx, op);
             break;
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH 04/16] tcg/optimize: Handle INDEX_op_lea

Quoted 42 lines · Richard Henderson
---
 tcg/optimize.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
tcg/optimize.c +28 -0
index d291c844ca8..83f95e26643 100644
@@ -2101,6 +2101,68 @@ static bool fold_extu(OptContext *ctx, TCGOp *op)
     return fold_masks_zo(ctx, op, z_mask, o_mask);
 }
 
+static bool fold_shift(OptContext *ctx, TCGOp *op);
+static bool fold_lea(OptContext *ctx, TCGOp *op)
+{
+    TCGArg sh = op->args[3];
+    TCGArg imm = op->args[4];
+    int var_mask = 0;
+
+    if (arg_is_const(op->args[1])) {
+        imm += arg_const_val(op->args[1]);
+    } else {
+        var_mask |= 1;
+    }
+    if (arg_is_const(op->args[2])) {
+        imm += arg_const_val(op->args[2]) << sh;
+    } else {
+        var_mask |= 2;
+    }
+
+    switch (var_mask) {
+    case 0:
+        return tcg_opt_gen_movi(ctx, op, op->args[0], imm);
+
+    case 1:
+        if (imm == 0) {
+            return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
+        }
+        op->opc = INDEX_op_add;
+        op->args[2] = arg_new_constant(ctx, imm);

tcg/optimize.c static bool fold_extu(OptContext *ctx, TCGOp *op) · line 2141

Quoted 10 lines
tcg/optimize.c +10 -0
+        break;
+
+    case 2:
+        if (sh == 0) {
+            if (imm == 0) {
+                return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
+            }
+            op->opc = INDEX_op_add;
+            op->args[1] = op->args[2];
+            op->args[2] = arg_new_constant(ctx, imm);

If we use fold_add here, will that handle optimizing add dst, x, -x into mov dst, 0?

Quoted 12 lines
tcg/optimize.c +12 -0
+            break;
+        }
+        if (imm == 0) {
+            op->opc = INDEX_op_shl;
+            op->args[1] = op->args[2];
+            op->args[2] = arg_new_constant(ctx, sh);
+            return fold_shift(ctx, op);
+        }
+
+        /* If arg1 was not already 0, fold the immediates. */
+        if (arg_const_val(op->args[1]) != 0) {
+            if (TCG_TARGET_lea_imm_valid(ctx->type, imm)) {

tcg/optimize.c static bool fold_extu(OptContext *ctx, TCGOp *op) · line 2163

Quoted 10 lines
tcg/optimize.c +10 -0
+                op->args[1] = arg_new_constant(ctx, 0);
+                op->args[4] = imm;
+            } else {
+                op->args[1] = arg_new_constant(ctx, imm);
+                op->args[4] = 0;
+            }
+        }
+        break;
+    }
+    return finish_folding(ctx, op);

It looks to me like fold_shift and fold_add propagate {z,o,s}_mask information, but fold_lea doesn't. I think that might prevent folding a downstream operation?

#9 lore raw

Re: [PATCH 04/16] tcg/optimize: Handle INDEX_op_lea

Quoted 49 lines · Matt Turner
Quoted 47 lines · Richard Henderson
---
  tcg/optimize.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 65 insertions(+)
tcg/optimize.c +33 -0
index d291c844ca8..83f95e26643 100644
@@ -2101,6 +2101,68 @@ static bool fold_extu(OptContext *ctx, TCGOp *op)
      return fold_masks_zo(ctx, op, z_mask, o_mask);
  }
 
+static bool fold_shift(OptContext *ctx, TCGOp *op);
+static bool fold_lea(OptContext *ctx, TCGOp *op)
+{
+    TCGArg sh = op->args[3];
+    TCGArg imm = op->args[4];
+    int var_mask = 0;
+
+    if (arg_is_const(op->args[1])) {
+        imm += arg_const_val(op->args[1]);
+    } else {
+        var_mask |= 1;
+    }
+    if (arg_is_const(op->args[2])) {
+        imm += arg_const_val(op->args[2]) << sh;
+    } else {
+        var_mask |= 2;
+    }
+
+    switch (var_mask) {
+    case 0:
+        return tcg_opt_gen_movi(ctx, op, op->args[0], imm);
+
+    case 1:
+        if (imm == 0) {
+            return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
+        }
+        op->opc = INDEX_op_add;
+        op->args[2] = arg_new_constant(ctx, imm);
+        break;
+
+    case 2:
+        if (sh == 0) {
+            if (imm == 0) {
Quoted 8 lines

tcg/optimize.c static bool fold_extu(OptContext *ctx, TCGOp *op) · line 2141

Quoted 5 lines
tcg/optimize.c +5 -0
+                return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
+            }
+            op->opc = INDEX_op_add;
+            op->args[1] = op->args[2];
+            op->args[2] = arg_new_constant(ctx, imm);

If we use fold_add here, will that handle optimizing add dst, x, -x into mov dst, 0?

Only if x is constant, which isn't the case here -- we've just checked for that in fact.

Quoted 3 lines

It looks to me like fold_shift and fold_add propagate {z,o,s}_mask information, but fold_lea doesn't. I think that might prevent folding a downstream operation?

fold_shift does, fold_add does not. Though I suppose we could go ahead and use it for completeness, just in case we do something there in future.

r~

#10 lore raw

PATCH 5/16 target/alpha: Use tcg_gen_lea_i64 for S{4,8}ADD{L,Q}

In reply to Richard Henderson
---
 target/alpha/translate.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
target/alpha/translate.c +6 -14
index c66e3f9c146..6f18b8d1af6 100644
@@ -1474,10 +1474,8 @@ static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
             break;
         case 0x02:
             /* S4ADDL */
-            tmp = tcg_temp_new_i64();
-            tcg_gen_shli_i64(tmp, va, 2);
-            tcg_gen_add_i64(tmp, tmp, vb);
-            tcg_gen_ext32s_i64(vc, tmp);
+            tcg_gen_lea_i64(vc, vb, va, 2, 0);
+            tcg_gen_ext32s_i64(vc, vc);
             break;
         case 0x09:
             /* SUBL */
@@ -1502,10 +1500,8 @@ static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
             break;
         case 0x12:
             /* S8ADDL */
-            tmp = tcg_temp_new_i64();
-            tcg_gen_shli_i64(tmp, va, 3);
-            tcg_gen_add_i64(tmp, tmp, vb);
-            tcg_gen_ext32s_i64(vc, tmp);
+            tcg_gen_lea_i64(vc, vb, va, 3, 0);
+            tcg_gen_ext32s_i64(vc, vc);
             break;
         case 0x1B:
             /* S8SUBL */
@@ -1524,9 +1520,7 @@ static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
             break;
         case 0x22:
             /* S4ADDQ */
-            tmp = tcg_temp_new_i64();
-            tcg_gen_shli_i64(tmp, va, 2);
-            tcg_gen_add_i64(vc, tmp, vb);
+            tcg_gen_lea_i64(vc, vb, va, 2, 0);
             break;
         case 0x29:
             /* SUBQ */
@@ -1544,9 +1538,7 @@ static DisasJumpType translate_one(DisasContext *ctx, uint32_t insn)
             break;
         case 0x32:
             /* S8ADDQ */
-            tmp = tcg_temp_new_i64();
-            tcg_gen_shli_i64(tmp, va, 3);
-            tcg_gen_add_i64(vc, tmp, vb);
+            tcg_gen_lea_i64(vc, vb, va, 3, 0);
             break;
         case 0x3B:
             /* S8SUBQ */
Signature · 1 line
2.53.0
#12 lore raw

PATCH 6/16 target/loongarch: Use tcg_gen_lea_tl for ALSL

In reply to Richard Henderson
---
 target/loongarch/tcg/insn_trans/trans_arith.c.inc | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
target/loongarch/tcg/insn_trans/trans_arith.c.inc +1 -3
index 2be057e9320..c358e642649 100644
@@ -188,9 +188,7 @@ static void gen_rem_w(TCGv dest, TCGv src1, TCGv src2)
 
 static void gen_alsl(TCGv dest, TCGv src1, TCGv src2, target_long sa)
 {
-    TCGv t0 = tcg_temp_new();
-    tcg_gen_shli_tl(t0, src1, sa);
-    tcg_gen_add_tl(dest, t0, src2);
+    tcg_gen_lea_tl(dest, src2, src1, sa, 0);
 }
 
 static bool trans_lu32i_d(DisasContext *ctx, arg_lu32i_d *a)
Signature · 1 line
2.53.0
#14 lore raw

PATCH 7/16 target/riscv: Use tcg_gen_lea_tl for SH*ADD

In reply to Richard Henderson
---
 target/riscv/tcg/insn_trans/trans_rvb.c.inc | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
target/riscv/tcg/insn_trans/trans_rvb.c.inc +1 -4
index 3e74be223b0..82e715c7b73 100644
@@ -336,10 +336,7 @@ static bool trans_orc_b(DisasContext *ctx, arg_orc_b *a)
 #define GEN_SHADD(SHAMT)                                       \
 static void gen_sh##SHAMT##add(TCGv ret, TCGv arg1, TCGv arg2) \
 {                                                              \
-    TCGv t = tcg_temp_new();                                   \
-                                                               \
-    tcg_gen_shli_tl(t, arg1, SHAMT);                           \
-    tcg_gen_add_tl(ret, t, arg2);                              \
+    tcg_gen_lea_tl(ret, arg2, arg1, SHAMT, 0);                 \
 }
 
 GEN_SHADD(1)
Signature · 1 line
2.53.0
#16 lore raw

PATCH 8/16 target/s390x: Use tcg_gen_lea_i64 in get_address

In reply to Richard Henderson

While there's no actual shift, use the compound function to add two registers and an immediate in one step.

---
 target/s390x/tcg/translate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/s390x/tcg/translate.c +2 -2
index 1b6023168bb..4eb2eba5e45 100644
@@ -460,8 +460,8 @@ static TCGv_i64 get_address(DisasContext *s, int x2, int b2, int d2)
      * displacements early we create larger immediate addends.
      */
     if (b2 && x2) {
-        tcg_gen_add_i64(tmp, regs[b2], regs[x2]);
-        gen_addi_and_wrap_i64(s, tmp, tmp, d2);
+        tcg_gen_lea_i64(tmp, regs[b2], regs[x2], 0, d2);
+        gen_addi_and_wrap_i64(s, tmp, tmp, 0);
     } else if (b2) {
         gen_addi_and_wrap_i64(s, tmp, regs[b2], d2);
     } else if (x2) {
Signature · 1 line
2.53.0
#18 lore raw

PATCH 9/16 target/i386: Use tcg_gen_lea_tl in gen_lea_modrm_1

In reply to Richard Henderson
---
 target/i386/tcg/translate.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)
target/i386/tcg/translate.c +14 -13
index d8de290acb6..4d0fff017c4 100644
@@ -1615,31 +1615,32 @@ static TCGv gen_shiftd_rm_T1(DisasContext *s, MemOp ot,
 /* Compute the address, with a minimum number of TCG ops.  */
 static TCGv gen_lea_modrm_1(DisasContext *s, AddressParts a, bool is_vsib)
 {
-    TCGv ea = NULL;
+    TCGv ea = (a.base >= 0 ? cpu_regs[a.base] : NULL);
 
     if (a.index >= 0 && !is_vsib) {
+        TCGv index = cpu_regs[a.index];
+
+        if (ea) {
+            tcg_gen_lea_tl(s->A0, ea, index, a.scale, a.disp);
+            return s->A0;
+        }
         if (a.scale == 0) {
-            ea = cpu_regs[a.index];
+            ea = index;
         } else {
-            tcg_gen_shli_tl(s->A0, cpu_regs[a.index], a.scale);
+            tcg_gen_shli_tl(s->A0, index, a.scale);
             ea = s->A0;
         }
-        if (a.base >= 0) {
-            tcg_gen_add_tl(s->A0, ea, cpu_regs[a.base]);
-            ea = s->A0;
-        }
-    } else if (a.base >= 0) {
-        ea = cpu_regs[a.base];
-    }
-    if (!ea) {
+    } else if (!ea) {
         if (tb_cflags(s->base.tb) & CF_PCREL && a.base == -2) {
             /* With cpu_eip ~= pc_save, the expression is pc-relative. */
             tcg_gen_addi_tl(s->A0, cpu_eip, a.disp - s->pc_save);
         } else {
             tcg_gen_movi_tl(s->A0, a.disp);
         }
-        ea = s->A0;
-    } else if (a.disp != 0) {
+        return s->A0;
+    }
+
+    if (a.disp != 0) {
         tcg_gen_addi_tl(s->A0, ea, a.disp);
         ea = s->A0;
     }
Signature · 1 line
2.53.0
#19 lore raw

PATCH 10/16 tcg/x86_64: Introduce TCG_REG_NONE

In reply to Richard Henderson

Rather than hard-code -1, or compare < 0, use something more descriptive. This also allows more use of TCGReg.

---
 tcg/x86_64/tcg-target.h     |  1 +
 tcg/x86_64/tcg-target.c.inc | 66 ++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 31 deletions(-)
tcg/x86_64/tcg-target.h +1 -0
index 7ebae56a7d7..7109722f240 100644
@@ -31,6 +31,7 @@
 #define MAX_CODE_GEN_BUFFER_SIZE  (2 * GiB)
 
 typedef enum {
+    TCG_REG_NONE = -1,
     TCG_REG_EAX = 0,
     TCG_REG_ECX,
     TCG_REG_EDX,
tcg/x86_64/tcg-target.c.inc +35 -31

179 lines not on this page · raw message

Signature · 1 line
2.53.0
#20 lore raw

Re: [PATCH 10/16] tcg/x86_64: Introduce TCG_REG_NONE

Quoted 27 lines · Richard Henderson

Rather than hard-code -1, or compare < 0, use something more descriptive. This also allows more use of TCGReg.

---
 tcg/x86_64/tcg-target.h     |  1 +
 tcg/x86_64/tcg-target.c.inc | 66 ++++++++++++++++++++-----------------
 2 files changed, 36 insertions(+), 31 deletions(-)
tcg/x86_64/tcg-target.h +1 -0
index 7ebae56a7d7..7109722f240 100644
@@ -31,6 +31,7 @@
 #define MAX_CODE_GEN_BUFFER_SIZE  (2 * GiB)
 
 typedef enum {
+    TCG_REG_NONE = -1,
     TCG_REG_EAX = 0,
     TCG_REG_ECX,
     TCG_REG_EDX,
tcg/x86_64/tcg-target.c.inc +0 -0
index 660350b963d..ec397e287c0 100644

tcg/x86_64/tcg-target.c.inc static bool tcg_target_const_match(int64_t val, int ct, · line 214

Quoted 6 lines
tcg/x86_64/tcg-target.c.inc +2 -1
@@ -210,7 +210,8 @@ static bool tcg_target_const_match(int64_t val, int ct,
     return 0;
 }
 
-# define LOWREGMASK(x) ((x) & 7)
+#define LOWREGMASK(x)   ((x) & 7)
+#define REGOR0(x)       ((x) == TCG_REG_NONE ? 0 : (x))

Could use a static inline to avoid evaluating x twice. Doesn't look to be a big deal.

#21 lore raw

PATCH 11/16 tcg/x86_64: Implement INDEX_op_lea

In reply to Richard Henderson
---
 tcg/x86_64/tcg-target-con-set.h |  1 +
 tcg/x86_64/tcg-target-has.h     |  4 ++--
 tcg/x86_64/tcg-target.h         |  2 ++
 tcg/x86_64/tcg-target.c.inc     | 11 ++++++++++-
 4 files changed, 15 insertions(+), 3 deletions(-)
tcg/x86_64/tcg-target-con-set.h +1 -0
index 458d69c3c07..5af8c1cb2cd 100644
@@ -46,6 +46,7 @@ C_O1_I2(r, r, r)
 C_O1_I2(r, r, re)
 C_O1_I2(r, r, ri)
 C_O1_I2(r, rO, re)
+C_O1_I2(r, rz, r)
 C_O1_I2(x, x, x)
 C_N1_I2(r, r, r)
 C_N1_I2(r, r, rW)
tcg/x86_64/tcg-target-has.h +2 -2
index 6ffae51f923..4f06b395de4 100644
@@ -105,7 +105,7 @@ tcg_target_extract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_extract_valid  tcg_target_extract_valid
 
-#define TCG_TARGET_lea_sh_valid(type, sh)          0
-#define TCG_TARGET_lea_imm_valid(type, imm)        0
+#define TCG_TARGET_lea_sh_valid(type, sh)          ((sh) >= 0 && (sh) <= 3)
+#define TCG_TARGET_lea_imm_valid(type, imm)        ((imm) == (int32_t)(imm))
 
 #endif
tcg/x86_64/tcg-target.h +2 -0
index 7109722f240..3444bcdca0a 100644
@@ -80,4 +80,6 @@ typedef enum {
     TCG_REG_CALL_STACK = TCG_REG_ESP
 } TCGReg;
 
+#define TCG_REG_ZERO  TCG_REG_NONE
+
 #endif
tcg/x86_64/tcg-target.c.inc +10 -1
index ec397e287c0..a41252782e1 100644
@@ -3331,8 +3331,17 @@ static const TCGOutOpStore outop_st = {
     .out_i = tgen_st_i,
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    int rexw = type == TCG_TYPE_I32 ? 0 : P_REXW;
+    tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, a1, a2, sh, imm);
+}
+
 static const TCGOutOpLea outop_lea = {
-    .base.static_constraint = C_NotImplemented,
+    /* Allow "lea imm(, a2, sh), a0" via TCG_REG_ZERO constraint */
+    .base.static_constraint = C_O1_I2(r, rz, r),
+    .out = tgen_lea,
 };
 
 static int const umin_insn[4] = {
Signature · 1 line
2.53.0
#22 lore raw

PATCH 12/16 tcg/loongarch64: Import SLADD insns

In reply to Richard Henderson
---
 tcg/loongarch64/tcg-insn-defs.c.inc | 30 ++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
tcg/loongarch64/tcg-insn-defs.c.inc +29 -1
index e907fc755f5..ececc6a3dd5 100644
@@ -4,7 +4,7 @@
  *
  * This file is auto-generated by genqemutcgdefs from
  * https://github.com/loongson-community/loongarch-opcodes,
- * from commit 1b95bb8c443ad71e266120fd97bd227df2d8ecfc.
+ * from commit 2d84c975e8894afc8b3ee9b8559f10308a658440.
  * DO NOT EDIT.
  */
 
@@ -24,6 +24,7 @@ typedef enum {
     OPC_REVBIT_D = 0x00005400,
     OPC_SEXT_H = 0x00005800,
     OPC_SEXT_B = 0x00005c00,
+    OPC_SLADD_W = 0x00040000,
     OPC_ADD_W = 0x00100000,
     OPC_ADD_D = 0x00108000,
     OPC_SUB_W = 0x00110000,
@@ -62,6 +63,7 @@ typedef enum {
     OPC_MOD_D = 0x00228000,
     OPC_DIV_DU = 0x00230000,
     OPC_MOD_DU = 0x00238000,
+    OPC_SLADD_D = 0x002c0000,
     OPC_SLLI_W = 0x00408000,
     OPC_SLLI_D = 0x00410000,
     OPC_SRLI_W = 0x00448000,
@@ -619,6 +621,18 @@ encode_djk_insn(LoongArchInsn opc, TCGReg d, TCGReg j, TCGReg k)
     return encode_djk_slots(opc, d, j, k);
 }
 
+static int32_t __attribute__((unused)) encode_djkua2_insn(LoongArchInsn opc,
+                                                          TCGReg d, TCGReg j,
+                                                          TCGReg k,
+                                                          uint32_t ua2)
+{
+    tcg_debug_assert(d >= 0 && d <= 0x1f);
+    tcg_debug_assert(j >= 0 && j <= 0x1f);
+    tcg_debug_assert(k >= 0 && k <= 0x1f);
+    tcg_debug_assert(ua2 <= 0x3);
+    return encode_djka_slots(opc, d, j, k, ua2);
+}
+
 static int32_t __attribute__((unused))
 encode_djsk12_insn(LoongArchInsn opc, TCGReg d, TCGReg j, int32_t sk12)
 {
@@ -1426,6 +1440,13 @@ static void __attribute__((unused)) tcg_out_opc_sext_b(TCGContext *s, TCGReg d,
     tcg_out32(s, encode_dj_insn(OPC_SEXT_B, d, j));
 }
 
+/* Emits the `sladd.w d, j, k, ua2` instruction.  */
+static void __attribute__((unused))
+tcg_out_opc_sladd_w(TCGContext *s, TCGReg d, TCGReg j, TCGReg k, uint32_t ua2)
+{
+    tcg_out32(s, encode_djkua2_insn(OPC_SLADD_W, d, j, k, ua2));
+}
+
 /* Emits the `add.w d, j, k` instruction.  */
 static void __attribute__((unused)) tcg_out_opc_add_w(TCGContext *s, TCGReg d,
                                                       TCGReg j, TCGReg k)
@@ -1692,6 +1713,13 @@ static void __attribute__((unused)) tcg_out_opc_mod_du(TCGContext *s, TCGReg d,
     tcg_out32(s, encode_djk_insn(OPC_MOD_DU, d, j, k));
 }
 
+/* Emits the `sladd.d d, j, k, ua2` instruction.  */
+static void __attribute__((unused))
+tcg_out_opc_sladd_d(TCGContext *s, TCGReg d, TCGReg j, TCGReg k, uint32_t ua2)
+{
+    tcg_out32(s, encode_djkua2_insn(OPC_SLADD_D, d, j, k, ua2));
+}
+
 /* Emits the `slli.w d, j, uk5` instruction.  */
 static void __attribute__((unused)) tcg_out_opc_slli_w(TCGContext *s, TCGReg d,
                                                        TCGReg j, uint32_t uk5)
Signature · 1 line
2.53.0
#23 lore raw

PATCH 13/16 tcg/loongarch64: Implement INDEX_op_lea

In reply to Richard Henderson
---
 tcg/loongarch64/tcg-target-has.h |  4 ++--
 tcg/loongarch64/tcg-target.c.inc | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 3 deletions(-)
tcg/loongarch64/tcg-target-has.h +2 -2
index 31e6ea9632d..53b7ca40571 100644
@@ -54,7 +54,7 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
-#define TCG_TARGET_lea_sh_valid(type, sh)          0
-#define TCG_TARGET_lea_imm_valid(type, imm)        0
+#define TCG_TARGET_lea_sh_valid(type, sh)          ((sh) >= 1 && (sh) <= 4)
+#define TCG_TARGET_lea_imm_valid(type, imm)        ((imm) == 0)
 
 #endif
tcg/loongarch64/tcg-target.c.inc +12 -1
index 17784d8bbe2..377cd9ec08d 100644
@@ -2098,8 +2098,19 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    if (type == TCG_TYPE_I32) {
+        tcg_out_opc_sladd_w(s, a0, a2, a1, sh - 1);
+    } else {
+        tcg_out_opc_sladd_d(s, a0, a2, a1, sh - 1);
+    }
+}
+
 static const TCGOutOpLea outop_lea = {
-    .base.static_constraint = C_NotImplemented,
+    .base.static_constraint = C_O1_I2(r, r, r),
+    .out = tgen_lea,
 };
 
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
Signature · 1 line
2.53.0
#24 lore raw

PATCH 14/16 tcg/aarch64: Implement INDEX_op_lea

In reply to Richard Henderson
---
 tcg/aarch64/tcg-target-has.h | 5 +++--
 tcg/aarch64/tcg-target.c.inc | 9 ++++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)
tcg/aarch64/tcg-target-has.h +3 -2
index 86672b2eab4..6327c5215a2 100644
@@ -57,7 +57,8 @@
 #define TCG_TARGET_sextract_valid(type, ofs, len)  1
 #define TCG_TARGET_deposit_valid(type, ofs, len)   1
 
-#define TCG_TARGET_lea_sh_valid(type, sh)          0
-#define TCG_TARGET_lea_imm_valid(type, imm)        0
+#define TCG_TARGET_lea_sh_valid(type, sh)          \
+    ((sh) >= 0 && (sh) < tcg_type_size(type) * 8)
+#define TCG_TARGET_lea_imm_valid(type, imm)        ((imm) == 0)
 
 #endif
tcg/aarch64/tcg-target.c.inc +8 -1
index 76adb2256f9..6836c0dd94f 100644
@@ -3081,8 +3081,15 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    tcg_out_insn(s, addsub_realshift, ADD_LSL, type, a0, a1, a2, sh);
+}
+
 static const TCGOutOpLea outop_lea = {
-    .base.static_constraint = C_NotImplemented,
+    .base.static_constraint = C_O1_I2(r, r, r),
+    .out = tgen_lea,
 };
 
 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
Signature · 1 line
2.53.0
#25 lore raw

PATCH 15/16 tcg/riscv64: Implement INDEX_op_lea

In reply to Richard Henderson
---
 tcg/riscv64/tcg-target-has.h | 12 ++++++++++--
 tcg/riscv64/tcg-target.c.inc | 16 +++++++++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)
tcg/riscv64/tcg-target-has.h +10 -2
index 93f8baad09f..3cdeb5c3900 100644
@@ -69,7 +69,15 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 
 #define TCG_TARGET_deposit_valid(type, ofs, len)  0
 
-#define TCG_TARGET_lea_sh_valid(type, sh)         0
-#define TCG_TARGET_lea_imm_valid(type, imm)       0
+/*
+ * The SH*ADD instructions only operate on full register width.
+ * To allow TCG_TYPE_I32, we'd need a subsequent extension,
+ * with the result no better than the separate shift+add insns.
+ */
+#define TCG_TARGET_lea_sh_valid(type, sh)         \
+    (cpuinfo & CPUINFO_ZBA &&                     \
+     (type) == TCG_TYPE_REG &&                    \
+     (sh) >= 1 && (sh) <= 3)
+#define TCG_TARGET_lea_imm_valid(type, imm)       ((imm) == 0)
 
 #endif
tcg/riscv64/tcg-target.c.inc +15 -1
index df4c4a9789b..ba7aab9246c 100644
@@ -224,6 +224,7 @@ typedef enum {
 
     /* Zba: Bit manipulation extension, address generation */
     OPC_ADD_UW = 0x0800003b,
+    OPC_SHxADD = 0x20000033,  /* plus sh << 13 */
 
     /* Zbb: Bit manipulation extension, basic bit manipulation */
     OPC_ANDN   = 0x40007033,
@@ -2725,8 +2726,21 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    tcg_out32(s, encode_r(OPC_SHxADD, a0, a2, a1) | (sh << 13));
+}
+
+static TCGConstraintSetIndex cset_zba_rrr(TCGType type, unsigned flags)
+{
+    return cpuinfo & CPUINFO_ZBA ? C_O1_I2(r, r, r) : C_NotImplemented;
+}
+
 static const TCGOutOpLea outop_lea = {
-    .base.static_constraint = C_NotImplemented,
+    .base.static_constraint = C_Dynamic,
+    .base.dynamic_constraint = cset_zba_rrr,
+    .out = tgen_lea,
 };
 
 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
Signature · 1 line
2.53.0
#26 lore raw

Re: [PATCH 15/16] tcg/riscv64: Implement INDEX_op_lea

Quoted 43 lines · Richard Henderson
---
 tcg/riscv64/tcg-target-has.h | 12 ++++++++++--
 tcg/riscv64/tcg-target.c.inc | 16 +++++++++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)
tcg/riscv64/tcg-target-has.h +10 -2
index 93f8baad09f..3cdeb5c3900 100644
@@ -69,7 +69,15 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 
 #define TCG_TARGET_deposit_valid(type, ofs, len)  0
 
-#define TCG_TARGET_lea_sh_valid(type, sh)         0
-#define TCG_TARGET_lea_imm_valid(type, imm)       0
+/*
+ * The SH*ADD instructions only operate on full register width.
+ * To allow TCG_TYPE_I32, we'd need a subsequent extension,
+ * with the result no better than the separate shift+add insns.
+ */
+#define TCG_TARGET_lea_sh_valid(type, sh)         \
+    (cpuinfo & CPUINFO_ZBA &&                     \
+     (type) == TCG_TYPE_REG &&                    \
+     (sh) >= 1 && (sh) <= 3)
+#define TCG_TARGET_lea_imm_valid(type, imm)       ((imm) == 0)
 
 #endif
tcg/riscv64/tcg-target.c.inc +1 -0
index df4c4a9789b..ba7aab9246c 100644
@@ -224,6 +224,7 @@ typedef enum {
 
     /* Zba: Bit manipulation extension, address generation */
     OPC_ADD_UW = 0x0800003b,
+    OPC_SHxADD = 0x20000033,  /* plus sh << 13 */
 
     /* Zbb: Bit manipulation extension, basic bit manipulation */
     OPC_ANDN   = 0x40007033,
@@ -2725,8 +2726,21 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,

tcg/riscv64/tcg-target.c.inc static const TCGOutOpStore outop_st = { · line 2736

Quoted 10 lines
tcg/riscv64/tcg-target.c.inc +8 -0
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    tcg_out32(s, encode_r(OPC_SHxADD, a0, a2, a1) | (sh << 13));
+}
+
+static TCGConstraintSetIndex cset_zba_rrr(TCGType type, unsigned flags)
+{

Assert type == TCG_TYPE_REG to mirror the macro?

Quoted 2 lines
tcg/riscv64/tcg-target.c.inc +2 -0
+    return cpuinfo & CPUINFO_ZBA ? C_O1_I2(r, r, r) : C_NotImplemented;
+}
#27 lore raw

Re: [PATCH 15/16] tcg/riscv64: Implement INDEX_op_lea

Quoted 10 lines · Matt Turner

tcg/riscv64/tcg-target.c.inc static const TCGOutOpStore outop_st = { · line 2736

Quoted 8 lines
tcg/riscv64/tcg-target.c.inc +8 -0
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    tcg_out32(s, encode_r(OPC_SHxADD, a0, a2, a1) | (sh << 13));
+}
+
+static TCGConstraintSetIndex cset_zba_rrr(TCGType type, unsigned flags)
+{

Assert type == TCG_TYPE_REG to mirror the macro?

We already have

Quoted 1 line

+ tcg_debug_assert(TCG_TARGET_lea_sh_valid(type, sh));

in the caller.

r~

#28 lore raw

PATCH 16/16 tcg/s390x: Implement INDEX_op_lea

In reply to Richard Henderson
---
 tcg/s390x/tcg-target-has.h |  5 +++--
 tcg/s390x/tcg-target.c.inc | 13 ++++++++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)
tcg/s390x/tcg-target-has.h +3 -2
index 58e3afe5266..33a3b72318e 100644
@@ -77,7 +77,8 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
-#define TCG_TARGET_lea_sh_valid(type, sh)          0
-#define TCG_TARGET_lea_imm_valid(type, imm)        0
+#define TCG_TARGET_lea_sh_valid(type, sh)          ((sh) == 0)
+#define TCG_TARGET_lea_imm_valid(type, imm)        \
+    ((imm) >= -0x80000 && (imm) < 0x80000)
 
 #endif
tcg/s390x/tcg-target.c.inc +12 -1
index 2fd7012d047..db3b2fc3c92 100644
@@ -3187,8 +3187,19 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    if (imm >= 0 && imm < 0x1000) {
+        tcg_out_insn(s, RX, LA, a0, a1, a2, imm);
+    } else {
+        tcg_out_insn(s, RXY, LAY, a0, a1, a2, imm);
+    }
+}
+
 static const TCGOutOpLea outop_lea = {
-    .base.static_constraint = C_NotImplemented,
+    .base.static_constraint = C_O1_I2(r, r, r),
+    .out = tgen_lea,
 };
 
 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
Signature · 1 line
2.53.0
#29 lore raw

Re: [PATCH 16/16] tcg/s390x: Implement INDEX_op_lea

Quoted 28 lines · Richard Henderson
---
 tcg/s390x/tcg-target-has.h |  5 +++--
 tcg/s390x/tcg-target.c.inc | 13 ++++++++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)
tcg/s390x/tcg-target-has.h +3 -2
index 58e3afe5266..33a3b72318e 100644
@@ -77,7 +77,8 @@ tcg_target_sextract_valid(TCGType type, unsigned ofs, unsigned len)
 }
 #define TCG_TARGET_sextract_valid  tcg_target_sextract_valid
 
-#define TCG_TARGET_lea_sh_valid(type, sh)          0
-#define TCG_TARGET_lea_imm_valid(type, imm)        0
+#define TCG_TARGET_lea_sh_valid(type, sh)          ((sh) == 0)
+#define TCG_TARGET_lea_imm_valid(type, imm)        \
+    ((imm) >= -0x80000 && (imm) < 0x80000)
 
 #endif
tcg/s390x/tcg-target.c.inc +0 -0
index 2fd7012d047..db3b2fc3c92 100644
@@ -3187,8 +3187,19 @@ static const TCGOutOpStore outop_st = {
     .out_r = tcg_out_st,

tcg/s390x/tcg-target.c.inc static const TCGOutOpStore outop_st = { · line 3197

Quoted 10 lines
tcg/s390x/tcg-target.c.inc +8 -0
 };
 
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    if (imm >= 0 && imm < 0x1000) {
+        tcg_out_insn(s, RX, LA, a0, a1, a2, imm);
+    } else {
+        tcg_out_insn(s, RXY, LAY, a0, a1, a2, imm);
+    }

Optional: could just be tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, a2, imm)

#30 lore raw

Re: [PATCH 16/16] tcg/s390x: Implement INDEX_op_lea

Quoted 10 lines · Matt Turner

tcg/s390x/tcg-target.c.inc static const TCGOutOpStore outop_st = { · line 3197

Quoted 8 lines
tcg/s390x/tcg-target.c.inc +8 -0
+static void tgen_lea(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1,
+                     TCGReg a2, unsigned sh, tcg_target_long imm)
+{
+    if (imm >= 0 && imm < 0x1000) {
+        tcg_out_insn(s, RX, LA, a0, a1, a2, imm);
+    } else {
+        tcg_out_insn(s, RXY, LAY, a0, a1, a2, imm);
+    }

Optional: could just be tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, a2, imm)

I considered that, but it does slightly more than I want here.

r~

Reviewed-by on the v1 series
#31 lore raw
In reply to Richard Henderson
Quoted 14 lines · Richard Henderson

Add a complex addressing mode primitive, and some trivial use cases.

This came up in conversation with Matt Turner re his inline jump cache expansion. In particular,

(quoted diff) +1 -2
-    tcg_gen_shli_i64(h, h, 4);
-    tcg_gen_add_i64(ent, jc, ent);
+    tcg_gen_lea_i64(ent, jc, h, 4, 0);

With the use case in target/i386/tcg, we get nearly full coverage of all the possible use cases. The only thing I didn't immediately see is shifts > 3, which could be visible with loongarch64 guests.

Quoted 6 lines

There are plenty of other places in target/ that could use this, but the shift/add/imm extraction/generation are inconveniently separated. E.g. m68k address generation, arm add-shifted, etc.

It may not even be worth converting those, but instead adding some trivial peepholing along the lines of Matt's user-only guest displacement.

This looks great to me, for what that's worth.