lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 7/8] mips: Fill delay slots in jit_bgtr, jit_bgti, jit_bler, j


From: Paul Cercueil
Subject: [PATCH v2 7/8] mips: Fill delay slots in jit_bgtr, jit_bgti, jit_bler, jit_blei
Date: Mon, 9 Jan 2023 23:04:10 +0000

Fill the delay slots with the opcode that precedes the branch opcode, if
possible.

To simplify things, the code has also been factorized into a single
function.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 lib/jit_mips-cpu.c | 214 +++++++++++++++++----------------------------
 lib/jit_mips.c     |  16 ++--
 2 files changed, 87 insertions(+), 143 deletions(-)

diff --git a/lib/jit_mips-cpu.c b/lib/jit_mips-cpu.c
index 984a6f9..a9266ae 100644
--- a/lib/jit_mips-cpu.c
+++ b/lib/jit_mips-cpu.c
@@ -681,14 +681,10 @@ static jit_word_t 
_bltr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
 static jit_word_t _blti(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
 #define blti_u(i0,r0,i1)               _blti_u(_jit,i0,r0,i1)
 static jit_word_t _blti_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
-#define bler(i0,r0,r1)                 _bler(_jit,i0,r0,r1)
-static jit_word_t _bler(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
-#define bler_u(i0,r0,r1)               _bler_u(_jit,i0,r0,r1)
-static jit_word_t _bler_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
-#define blei(i0,r0,i1)                 _blei(_jit,i0,r0,i1)
-static jit_word_t _blei(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
-#define blei_u(i0,r0,i1)               _blei_u(_jit,i0,r0,i1)
-static jit_word_t _blei_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
+#define bler(i0,r0,r1,no_flag)         _bgtr(_jit,i0,r1,r0,0,1,no_flag)
+#define bler_u(i0,r0,r1,no_flag)       _bgtr(_jit,i0,r1,r0,1,1,no_flag)
+#define blei(i0,r0,i1,no_flag)         _bgti(_jit,i0,r0,i1,0,1,no_flag)
+#define blei_u(i0,r0,i1,no_flag)       _bgti(_jit,i0,r0,i1,1,1,no_flag)
 #define beqr(i0,r0,r1,no_flag)         _beqr(_jit,i0,r0,r1,no_flag)
 static jit_word_t 
_beqr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t,jit_bool_t);
 #define beqi(i0,r0,i1,no_flag)         _beqi(_jit,i0,r0,i1,no_flag)
@@ -701,14 +697,14 @@ static jit_word_t 
_bger_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
 static jit_word_t _bgei(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
 #define bgei_u(i0,r0,i1)               _bgei_u(_jit,i0,r0,i1)
 static jit_word_t _bgei_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
-#define bgtr(i0,r0,r1)                 _bgtr(_jit,i0,r0,r1)
-static jit_word_t _bgtr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
-#define bgtr_u(i0,r0,r1)               _bgtr_u(_jit,i0,r0,r1)
-static jit_word_t _bgtr_u(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t);
-#define bgti(i0,r0,i1)                 _bgti(_jit,i0,r0,i1)
-static jit_word_t _bgti(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
-#define bgti_u(i0,r0,i1)               _bgti_u(_jit,i0,r0,i1)
-static jit_word_t _bgti_u(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t);
+#define bgtr(i0,r0,r1,no_flag)         _bgtr(_jit,i0,r0,r1,0,0,no_flag)
+#define bgtr_u(i0,r0,r1,no_flag)       _bgtr(_jit,i0,r0,r1,1,0,no_flag)
+static jit_word_t _bgtr(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t,
+                       jit_bool_t,jit_bool_t,jit_bool_t);
+#define bgti(i0,r0,i1,no_flag)         _bgti(_jit,i0,r0,i1,0,0,no_flag)
+#define bgti_u(i0,r0,i1,no_flag)       _bgti(_jit,i0,r0,i1,1,0,no_flag)
+static jit_word_t _bgti(jit_state_t*,jit_word_t,jit_int32_t,jit_word_t,
+                       jit_bool_t,jit_bool_t,jit_bool_t);
 #define bner(i0,r0,r1,no_flag)         _bner(_jit,i0,r0,r1,no_flag)
 static jit_word_t 
_bner(jit_state_t*,jit_word_t,jit_int32_t,jit_int32_t,jit_bool_t);
 #define bnei(i0,r0,i1,no_flag)         _bnei(_jit,i0,r0,i1,no_flag)
@@ -2238,80 +2234,6 @@ _blti_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t 
r0, jit_word_t i1)
     return (w);
 }
 
-static jit_word_t
-_bler(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
-
-    reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-    SLT(rn(reg), r1, r0);
-    w = _jit->pc.w;
-    BEQ(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-    NOP(1);
-    jit_unget_reg(reg);
-
-    return (w);
-}
-
-static jit_word_t
-_bler_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
-
-    reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-    SLTU(rn(reg), r1, r0);
-    w = _jit->pc.w;
-    BEQ(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-    NOP(1);
-    jit_unget_reg(reg);
-
-    return (w);
-}
-
-static jit_word_t
-_blei(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
-
-    if (i1 == 0) {
-       w = _jit->pc.w;
-       BLEZ(r0, ((i0 - w) >> 2) - 1);
-       NOP(1);
-    }
-    else {
-       reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-       movi(rn(reg), i1);
-       w = bler(i0, r0, rn(reg));
-       jit_unget_reg(reg);
-    }
-
-    return (w);
-}
-
-static jit_word_t
-_blei_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
-
-    if (i1 == 0) {
-       w = _jit->pc.w;
-       BEQ(r0, _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-       NOP(1);
-    }
-    else {
-       reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-       movi(rn(reg), i1);
-       w = bler_u(i0, r0, rn(reg));
-       jit_unget_reg(reg);
-    }
-
-    return (w);
-}
-
 static jit_word_t
 _beqr(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1, 
jit_bool_t no_flag)
 {
@@ -2459,76 +2381,98 @@ _bgei_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t 
r0, jit_word_t i1)
 }
 
 static jit_word_t
-_bgtr(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
+_bgtr(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1,
+      jit_bool_t sltu, jit_bool_t inv, jit_bool_t no_flag)
 {
     jit_word_t         w;
-    jit_int32_t                reg;
+    jit_int32_t                reg, prev, offset;
+    jit_bool_t         swap_ds;
+
+    offset = ((jit_word_t)_jit->pc.ui - (jit_word_t)_jit->code.ptr) / 
sizeof(jit_instr_t);
+    swap_ds = no_flag
+        && (offset < 2 || !has_delay_slot((jit_instr_t)*(_jit->pc.ui - 2)))
+        && !op_writes_register((jit_instr_t)*(_jit->pc.ui - 1), r0)
+        && !op_writes_register((jit_instr_t)*(_jit->pc.ui - 1), r1);
 
     reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-    SLT(rn(reg), r1, r0);
-    w = _jit->pc.w;
-    BNE(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-    NOP(1);
-    jit_unget_reg(reg);
 
-    return (w);
-}
+    if (swap_ds)
+        prev = *--_jit->pc.ui;
 
-static jit_word_t
-_bgtr_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
+    if (sltu)
+        SLTU(rn(reg), r1, r0);
+    else
+        SLT(rn(reg), r1, r0);
 
-    reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-    SLTU(rn(reg), r1, r0);
     w = _jit->pc.w;
-    BNE(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-    NOP(1);
+    if (inv)
+        BEQ(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
+    else
+        BNE(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
+
+    if (swap_ds)
+        ii(prev);
+    else
+        NOP(1);
+
     jit_unget_reg(reg);
 
     return (w);
 }
 
 static jit_word_t
-_bgti(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1)
+_bgti(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1,
+      jit_bool_t sltiu, jit_bool_t inv, jit_bool_t no_flag)
 {
     jit_word_t         w;
-    jit_int32_t                reg;
+    jit_int32_t                reg, prev, offset;
+    jit_bool_t         swap_ds;
+
+    offset = ((jit_word_t)_jit->pc.ui - (jit_word_t)_jit->code.ptr) / 
sizeof(jit_instr_t);
+    swap_ds = no_flag
+        && (offset < 2 || !has_delay_slot((jit_instr_t)*(_jit->pc.ui - 2)))
+        && !op_writes_register((jit_instr_t)*(_jit->pc.ui - 1), r0);
+
+    if (swap_ds)
+        prev = *--_jit->pc.ui;
 
     if (i1 == 0) {
-       w = _jit->pc.w;
-       BGTZ(r0, ((i0 - w) >> 2) - 1);
-       NOP(1);
+        w = _jit->pc.w;
+        if (inv) {
+            if (sltiu)
+                BEQ(r0, _ZERO_REGNO, ((i0 - w) >> 2) - 1);
+            else
+                BLEZ(r0, ((i0 - w) >> 2) - 1);
+        } else {
+            if (sltiu)
+                BNE(r0, _ZERO_REGNO, ((i0 - w) >> 2) - 1);
+            else
+                BGTZ(r0, ((i0 - w) >> 2) - 1);
+        }
     }
     else {
-       reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-       movi(rn(reg), i1);
-       w = bgtr(i0, r0, rn(reg));
-       jit_unget_reg(reg);
-    }
+        reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
 
-    return (w);
-}
+        movi(rn(reg), i1);
+        if (sltiu)
+            SLTU(rn(reg), rn(reg), r0);
+        else
+            SLT(rn(reg), rn(reg), r0);
 
-static jit_word_t
-_bgti_u(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1)
-{
-    jit_word_t         w;
-    jit_int32_t                reg;
+        w = _jit->pc.w;
+        if (inv)
+            BEQ(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
+        else
+            BNE(rn(reg), _ZERO_REGNO, ((i0 - w) >> 2) - 1);
 
-    if (i1 == 0) {
-       w = _jit->pc.w;
-       BNE(r0, _ZERO_REGNO, ((i0 - w) >> 2) - 1);
-       NOP(1);
-    }
-    else {
-       reg = jit_get_reg(jit_class_gpr|jit_class_nospill);
-       movi(rn(reg), i1);
-       w = bgtr_u(i0, r0, rn(reg));
-       jit_unget_reg(reg);
+        jit_unget_reg(reg);
     }
 
+    if (swap_ds)
+        ii(prev);
+    else
+        NOP(1);
+
     return (w);
 }
 
diff --git a/lib/jit_mips.c b/lib/jit_mips.c
index 67df648..0b7f544 100644
--- a/lib/jit_mips.c
+++ b/lib/jit_mips.c
@@ -1515,20 +1515,20 @@ _emit_code(jit_state_t *_jit)
                case_brw(blt,);
                case_brr(blt, _u);
                case_brw(blt, _u);
-               case_brr(ble,);
-               case_brw(ble,);
-               case_brr(ble, _u);
-               case_brw(ble, _u);
+               case_brrn(ble,, no_flag);
+               case_brwn(ble,, no_flag);
+               case_brrn(ble, _u, no_flag);
+               case_brwn(ble, _u, no_flag);
                case_brrn(beq,, no_flag);
                case_brwn(beq,, no_flag);
                case_brr(bge,);
                case_brw(bge,);
                case_brr(bge, _u);
                case_brw(bge, _u);
-               case_brr(bgt,);
-               case_brw(bgt,);
-               case_brr(bgt, _u);
-               case_brw(bgt, _u);
+               case_brrn(bgt,, no_flag);
+               case_brwn(bgt,, no_flag);
+               case_brrn(bgt, _u, no_flag);
+               case_brwn(bgt, _u, no_flag);
                case_brrn(bne,, no_flag);
                case_brwn(bne,, no_flag);
                case_brr(boadd,);
-- 
2.39.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]