https://gcc.gnu.org/g:5ff4d69897539786097091ed637f6e943ade9fc8

commit r17-6158-g5ff4d69897539786097091ed637f6e943ade9fc8
Author: Xi Ruoyao <[email protected]>
Date:   Fri Oct 23 11:20:36 2026 +0800

    LoongArch: fix missed bstrins_d_for_ior_ashift_and optimization
    
    The pattern already has any_or_plus instead of ior but it still fails
    after the target-independent code started to prefer plus for more cases.
    For example (a << 1) | (b & 1) is now transformed to (a << 1) + (b & 1),
    which can be obviously implemented with andi + alsl (2 insns) instead of
    the naive andi + slli + or (3 insns).  But the cost model did not cover
    this case so the cost of bstrins_d_for_ior_ashift_and was estimated to
    be 3 insns, more than 2 insns of andi + alsl so the compiler refuses to
    use the pattern.
    
    Add this case to code model to fix the failure of
    gcc.target/loongarch/bstrins-6.c.  Also improve the pattern itself a
    little by using the "0" constraint to make the LRA to move the value if
    necessary instead of hand-brew logic.
    
    Make it work on LA32S by the way.
    
    gcc/
    
            * config/loongarch/loongarch.cc (loongarch_rtx_costs): Add the
            case for bstrins_d_for_ior_ashift_and.
            * config/loongarch/loongarch.md
            (*bstrins_d_for_ior_ashift_and): Rename to ...
            (*bstrins_for_ior_ashift_and): ... here, use GPR instead of DI
            and allow it on LA32S; use "0" constraint for operand 1;
            remove redundant conditions.
    
    gcc/testsuite:
    
            * gcc.target/loongarch/bstrins-6-32s.c: New test.

Diff:
---
 gcc/config/loongarch/loongarch.cc                  | 17 ++++++++++++
 gcc/config/loongarch/loongarch.md                  | 30 ++++++++++------------
 gcc/testsuite/gcc.target/loongarch/bstrins-6-32s.c |  6 +++++
 3 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/gcc/config/loongarch/loongarch.cc 
b/gcc/config/loongarch/loongarch.cc
index bf4016023942..bcfcf7835d4e 100644
--- a/gcc/config/loongarch/loongarch.cc
+++ b/gcc/config/loongarch/loongarch.cc
@@ -4312,6 +4312,23 @@ loongarch_rtx_costs (rtx x, machine_mode mode, int 
outer_code,
 
          switch (code_lhs)
            {
+           case AND:
+             if (CONST_INT_P (XEXP (lhs, 1))
+                 && GET_CODE (XEXP (x, 1)) == ASHIFT)
+               {
+                 unsigned HOST_WIDE_INT mask = UINTVAL (XEXP (lhs, 1));
+                 rtx rhs = XEXP (x, 1);
+                 if (CONST_INT_P (XEXP (rhs, 1))
+                     && (HWIT_1U << INTVAL (XEXP (rhs, 1))) - 1 == mask)
+                   {
+                     /* This is a bstrins instruction.  */
+                     *total = COSTS_N_INSNS (1);
+                     *total += set_src_cost (XEXP (lhs, 0), mode, speed);
+                     *total += set_src_cost (XEXP (rhs, 0), mode, speed);
+                     return true;
+                   }
+               }
+             break;
            case ASHIFT:
              if (CONST_INT_P (XEXP (lhs, 1)))
                shamt = INTVAL (XEXP (lhs, 1));
diff --git a/gcc/config/loongarch/loongarch.md 
b/gcc/config/loongarch/loongarch.md
index 0fa3f46e8f29..78da42fc0320 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -1695,25 +1695,23 @@
 ;; Optimize (a << imm1) | (b & imm2) to use bstrins.d instruction, the size of
 ;; a and b are 8 bits, 16 bits or 64bits, imm2 value should be equal to
 ;; (1LL << imm1) - 1.
-(define_insn_and_split "*bstrins_d_for_ior_ashift_and"
-  [(set (match_operand:DI 0 "register_operand" "=r")
-       (any_or_plus:DI
-         (and:DI (match_operand:DI 1 "register_operand" "r")
-                 (match_operand:DI 2 "const_int_operand" "i"))
-         (ashift:DI
-             (match_operand:DI 3 "register_operand" "r")
-             (match_operand:DI 4 "const_uimm63_operand"))))]
-  "TARGET_64BIT && loongarch_pre_reload_split ()
-   && !reg_overlap_mentioned_p (operands[0], operands[3])
-   && INTVAL (operands[2]) != 0 && INTVAL (operands[4]) != 0
-   && INTVAL (operands[2]) == (1LL << INTVAL (operands[4])) - 1"
+(define_insn_and_split "*bstrins_for_ior_ashift_and"
+  [(set (match_operand:GPR 0 "register_operand" "=r")
+       (any_or_plus:GPR
+         (and:GPR (match_operand:GPR 1 "register_operand" "0")
+                  (match_operand     2 "const_int_operand"))
+         (ashift:GPR
+             (match_operand:GPR 3 "register_operand" "r")
+             (match_operand     4 "const_int_operand"))))]
+  "(TARGET_64BIT || TARGET_32BIT_S)
+   && IN_RANGE (INTVAL (operands[4]), 1, GET_MODE_BITSIZE (<MODE>mode) - 1)
+   && UINTVAL (operands[2]) == (HWIT_1U << INTVAL (operands[4])) - 1"
   "#"
-  "&& true"
-  [(set (match_dup 0) (match_dup 1))
-   (set (zero_extract:DI (match_dup 0) (match_dup 2) (match_dup 4))
+  "&& reload_completed"
+  [(set (zero_extract:GPR (match_dup 0) (match_dup 2) (match_dup 4))
        (match_dup 3))]
   {
-    operands[2] = GEN_INT (64 - INTVAL (operands[4]));
+    operands[2] = GEN_INT (GET_MODE_BITSIZE (<MODE>mode) - INTVAL 
(operands[4]));
   })
 
 (define_insn "and_load_zero_extend<mode>"
diff --git a/gcc/testsuite/gcc.target/loongarch/bstrins-6-32s.c 
b/gcc/testsuite/gcc.target/loongarch/bstrins-6-32s.c
new file mode 100644
index 000000000000..10962387fbc4
--- /dev/null
+++ b/gcc/testsuite/gcc.target/loongarch/bstrins-6-32s.c
@@ -0,0 +1,6 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=la32v1.0 -mabi=ilp32s" } */
+/* { dg-final { scan-assembler 
"foo:.*\tbstrins\\.w\t\\\$r\[0-9\]+,\\\$r\[0-9\]+,31,1.*foo" } } */
+/* { dg-final { scan-assembler 
"bar:.*\tbstrins\\.w\t\\\$r\[0-9\]+,\\\$r\[0-9\]+,31,15.*bar" } } */
+
+#include "bstrins-6.c"

Reply via email to