https://gcc.gnu.org/g:7fe2ca4c9333e0d023be08271a51c6769569da3f
commit r17-6160-g7fe2ca4c9333e0d023be08271a51c6769569da3f Author: Xi Ruoyao <[email protected]> Date: Fri Oct 23 11:20:38 2026 +0800 LoongArch: fix missed bstrins_di_for_ior_mask 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, as the cost model does not recognize the sum of two bitwise and results. Factor out the logic recognized the bitwise or of two bitwise and results into a helper and call it when handling the and expressions to adapt for the change and fix gcc.target/loongarch/la64/bstrins-3.c failure. gcc/ * config/loongarch/loongarch.cc (loongarch_cost_bstrins_for_any_or_plus): New static function. (loongarch_rtx_costs): Call loongarch_cost_bstrins_for_any_or_plus for IOR, XOR, and PLUS. Diff: --- gcc/config/loongarch/loongarch.cc | 85 ++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index bcfcf7835d4e..57d09584a16c 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -4062,6 +4062,43 @@ loongarch_address_cost (rtx addr, machine_mode mode, la_addr_reg_reg_cost); } +static bool +loongarch_cost_bstrins_for_any_or_plus (rtx x, machine_mode mode, + int *total, bool speed) +{ + rtx op[2] = {XEXP (x, 0), XEXP (x, 1)}; + + if (GET_CODE (op[0]) == AND && GET_CODE (op[1]) == AND + && (mode == SImode || (TARGET_64BIT && mode == DImode))) + { + rtx rtx_mask0 = XEXP (op[0], 1), rtx_mask1 = XEXP (op[1], 1); + if (CONST_INT_P (rtx_mask0) && CONST_INT_P (rtx_mask1)) + { + unsigned HOST_WIDE_INT mask0 = UINTVAL (rtx_mask0); + unsigned HOST_WIDE_INT mask1 = UINTVAL (rtx_mask1); + if (loongarch_use_bstrins_for_ior_with_mask_1 (mode, + mask0, + mask1)) + { + /* A bstrins instruction. */ + *total = COSTS_N_INSNS (1); + + /* A srai instruction. */ + if (low_bitmask_len (mode, mask0) == -1 + && low_bitmask_len (mode, mask1) == -1) + *total += COSTS_N_INSNS (1); + + for (int i = 0; i < 2; i++) + *total += set_src_cost (XEXP (op[i], 0), mode, speed); + + return true; + } + } + } + + return false; +} + /* Implement TARGET_RTX_COSTS. */ static bool @@ -4178,45 +4215,18 @@ loongarch_rtx_costs (rtx x, machine_mode mode, int outer_code, return true; } - /* Fall through. */ - + /* Double-word operations use two single-word operations. */ + *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), + COSTS_N_INSNS (2), speed); + return true; case IOR: - { - rtx op[2] = {XEXP (x, 0), XEXP (x, 1)}; - if (GET_CODE (op[0]) == AND && GET_CODE (op[1]) == AND - && (mode == SImode || (TARGET_64BIT && mode == DImode))) - { - rtx rtx_mask0 = XEXP (op[0], 1), rtx_mask1 = XEXP (op[1], 1); - if (CONST_INT_P (rtx_mask0) && CONST_INT_P (rtx_mask1)) - { - unsigned HOST_WIDE_INT mask0 = UINTVAL (rtx_mask0); - unsigned HOST_WIDE_INT mask1 = UINTVAL (rtx_mask1); - if (loongarch_use_bstrins_for_ior_with_mask_1 (mode, - mask0, - mask1)) - { - /* A bstrins instruction */ - *total = COSTS_N_INSNS (1); - - /* A srai instruction */ - if (low_bitmask_len (mode, mask0) == -1 - && low_bitmask_len (mode, mask1) == -1) - *total += COSTS_N_INSNS (1); - - for (int i = 0; i < 2; i++) - *total += set_src_cost (XEXP (op[i], 0), mode, speed); - - return true; - } - } - } - } - - /* Fall through. */ case XOR: + if (loongarch_cost_bstrins_for_any_or_plus (x, mode, total, speed)) + return true; + /* Double-word operations use two single-word operations. */ - *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2), - speed); + *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), + COSTS_N_INSNS (2), speed); return true; case LSHIFTRT: @@ -4328,6 +4338,9 @@ loongarch_rtx_costs (rtx x, machine_mode mode, int outer_code, return true; } } + if (loongarch_cost_bstrins_for_any_or_plus (x, mode, total, + speed)) + return true; break; case ASHIFT: if (CONST_INT_P (XEXP (lhs, 1)))
