Skip to contentSkip to conversation

tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

PATCH In review Philippe Mathieu-Daudé 2 participants 4 messages May 20

lore
Share & tools
lore
#0 lore raw

v11 cover tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

Respin of Richard patch split to ease review.

Richard Henderson (2):
tcg: Massage fold_multiply2()
tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

 tcg/optimize.c | 90 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 59 insertions(+), 31 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v11 1/2 tcg: Massage fold_multiply2()

From: Richard Henderson <[email protected]>

In order to ease next commit review, check arg2 constness in the inner loop.

[PMD: Split of following commit]
---
 tcg/optimize.c | 68 +++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 34 deletions(-)
tcg/optimize.c +34 -34
index b1abec69a5d..ef5eb2cf175 100644
@@ -2173,45 +2173,45 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op)
 {
     swap_commutative(op->args[0], &op->args[2], &op->args[3]);
 
-    if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
-        uint64_t a = arg_const_val(op->args[2]);
+    if (arg_is_const(op->args[3])) {
         uint64_t b = arg_const_val(op->args[3]);
-        uint64_t h, l;
-        TCGArg rl, rh;
+        TCGArg rl = op->args[0];
+        TCGArg rh = op->args[1];
         TCGOp *op2;
 
-        switch (op->opc) {
-        case INDEX_op_mulu2:
-            if (ctx->type == TCG_TYPE_I32) {
-                l = (uint64_t)(uint32_t)a * (uint32_t)b;
-                h = (int32_t)(l >> 32);
-                l = (int32_t)l;
-            } else {
-                mulu64(&l, &h, a, b);
+        if (arg_is_const(op->args[2])) {
+            uint64_t a = arg_const_val(op->args[2]);
+            uint64_t h, l;
+
+            switch (op->opc) {
+            case INDEX_op_mulu2:
+                if (ctx->type == TCG_TYPE_I32) {
+                    l = (uint64_t)(uint32_t)a * (uint32_t)b;
+                    h = (int32_t)(l >> 32);
+                    l = (int32_t)l;
+                } else {
+                    mulu64(&l, &h, a, b);
+                }
+                break;
+            case INDEX_op_muls2:
+                if (ctx->type == TCG_TYPE_I32) {
+                    l = (int64_t)(int32_t)a * (int32_t)b;
+                    h = l >> 32;
+                    l = (int32_t)l;
+                } else {
+                    muls64(&l, &h, a, b);
+                }
+                break;
+            default:
+                g_assert_not_reached();
             }
-            break;
-        case INDEX_op_muls2:
-            if (ctx->type == TCG_TYPE_I32) {
-                l = (int64_t)(int32_t)a * (int32_t)b;
-                h = l >> 32;
-                l = (int32_t)l;
-            } else {
-                muls64(&l, &h, a, b);
-            }
-            break;
-        default:
-            g_assert_not_reached();
+
+            /* The proper opcode is supplied by tcg_opt_gen_mov. */
+            op2 = opt_insert_before(ctx, op, 0, 2);
+            tcg_opt_gen_movi(ctx, op, rl, l);
+            tcg_opt_gen_movi(ctx, op2, rh, h);
+            return true;
         }
-
-        rl = op->args[0];
-        rh = op->args[1];
-
-        /* The proper opcode is supplied by tcg_opt_gen_mov. */
-        op2 = opt_insert_before(ctx, op, 0, 2);
-
-        tcg_opt_gen_movi(ctx, op, rl, l);
-        tcg_opt_gen_movi(ctx, op2, rh, h);
-        return true;
     }
     return finish_folding(ctx, op);
 }
Signature · 1 line
2.53.0
#2 lore raw

PATCH v11 2/2 tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

In reply to Philippe Mathieu-Daudé

From: Richard Henderson <[email protected]>

Zero operands produce a zero high and low product. One operands produce a copy of the other operand and a zero or sign extension in the high half.

Fold those cases during TCG optimization so wide-multiply idioms used by target translators can collapse before code generation.

---
 tcg/optimize.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
tcg/optimize.c +28 -0
index ef5eb2cf175..fcdef25beed 100644
@@ -2212,6 +2212,34 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op)
             tcg_opt_gen_movi(ctx, op2, rh, h);
             return true;
         }
+
+        if (b == 0) {
+            op2 = opt_insert_before(ctx, op, 0, 2);
+            tcg_opt_gen_movi(ctx, op2, rl, 0);
+            tcg_opt_gen_movi(ctx, op, rh, 0);
+            return true;
+        }
+        if (b == 1) {
+            op2 = opt_insert_before(ctx, op, 0, 2);
+            tcg_opt_gen_mov(ctx, op2, rl, op->args[2]);
+
+            switch (op->opc) {
+            case INDEX_op_mulu2:
+                tcg_opt_gen_movi(ctx, op, rh, 0);
+                break;
+            case INDEX_op_muls2:
+                op->opc = INDEX_op_sar;
+                op->args[0] = rh;
+                op->args[1] = rl;
+                op->args[2] =
+                    arg_new_constant(ctx, tcg_type_size(ctx->type) * 8 - 1);
+                break;
+            default:
+                g_assert_not_reached();
+            }
+
+            return true;
+        }
     }
     return finish_folding(ctx, op);
 }
Signature · 1 line
2.53.0
#3 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 9 lines · Philippe Mathieu-Daudé

Respin of Richard patch split to ease review.

Richard Henderson (2):
tcg: Massage fold_multiply2()
tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

  tcg/optimize.c | 90 +++++++++++++++++++++++++++++++++-----------------
  1 file changed, 59 insertions(+), 31 deletions(-)

Ok, queued.

r~