https://gcc.gnu.org/g:d35883717679f486bb5b2b5ef546d0eda9878874
commit r17-6592-gd35883717679f486bb5b2b5ef546d0eda9878874 Author: Kyrylo Tkachov <[email protected]> Date: Fri Nov 20 03:04:53 2026 -0800 aarch64: Cost MOVK as a single instruction in aarch64_rtx_costs [PR128058] Since 53c3316a8c7 (PR tree-optimization/108477), expand emits a PLUS instead of an IOR or XOR for operands with disjoint bits, so (x & 0xffff) | 0x12340000 becomes (plus (and x 0xffff) 0x12340000). The *aarch64_movk<mode> pattern matches that form, but combine rejects the 3 to 2 insn combination of the constant move and the PLUS when the immediate is neither an ADD immediate nor a 24-bit ADD pair, because aarch64_rtx_costs charges a separate immediate move for it. This regressed gcc.target/aarch64/movk_2.c. Before: mov w1, 305397760 add w0, w1, w0, uxth ret After: movk w0, #0x1234, lsl 16 ret Add aarch64_movk_plus_p to recognise the (plus (and R MASK) IMM) shape accepted by aarch64_movk_shift. Check it at the start of the PLUS case of aarch64_rtx_costs, before the ADD immediate costing, and cost the operation as one logical instruction on R. The check is before the cost_plus label because the COMPARE case jumps there with OP0 and OP1 set to the operands of the COMPARE. The IOR and XOR forms need no change, because expand does not emit them for disjoint operands. Bootstrapped and tested on aarch64-linux-gnu. gcc/ChangeLog: PR target/128058 * config/aarch64/aarch64.cc (aarch64_movk_plus_p): New function. (aarch64_rtx_costs): Use it to cost MOVK patterns in the PLUS case. gcc/testsuite/ChangeLog: PR target/128058 * gcc.target/aarch64/movk_5.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/config/aarch64/aarch64.cc | 23 +++++++++++++++ gcc/testsuite/gcc.target/aarch64/movk_5.c | 49 +++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index 17b3fc730504..bdd0db3fce91 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -15138,6 +15138,19 @@ aarch64_branch_cost (bool speed_p, bool predictable_p) return branch_costs->unpredictable; } +/* Return true if OP0 and OP1, the operands of a PLUS in MODE, are + (and R MASK) and IMM where MASK and IMM form a single MOVK instruction. + See *aarch64_movk<mode>. MODE must be SImode or DImode. */ +static bool +aarch64_movk_plus_p (rtx op0, rtx op1, machine_mode mode) +{ + return (GET_CODE (op0) == AND + && CONST_INT_P (XEXP (op0, 1)) + && CONST_INT_P (op1) + && aarch64_movk_shift (rtx_mode_t (XEXP (op0, 1), mode), + rtx_mode_t (op1, mode)) >= 0); +} + /* Return true if X is a zero or sign extract usable in an ADD or SUB (extended register) instruction. */ static bool @@ -16067,6 +16080,16 @@ cost_minus: op0 = XEXP (x, 0); op1 = XEXP (x, 1); + /* MOVK. This is one instruction whatever the immediate is. */ + if ((mode == SImode || mode == DImode) + && aarch64_movk_plus_p (op0, op1, mode)) + { + *cost += rtx_cost (XEXP (op0, 0), mode, AND, 0, speed); + if (speed) + *cost += extra_cost->alu.logical; + return true; + } + cost_plus: if (VECTOR_MODE_P (mode)) { diff --git a/gcc/testsuite/gcc.target/aarch64/movk_5.c b/gcc/testsuite/gcc.target/aarch64/movk_5.c new file mode 100644 index 000000000000..a0ef02cd4c6f --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/movk_5.c @@ -0,0 +1,49 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +#include <stdint.h> + +/* +** f1: +** movk w0, #0x1234, lsl #?16 +** ret +*/ +uint32_t +f1 (uint32_t x) +{ + return (x & 0xffff) ^ 0x12340000; +} + +/* +** f2: +** movk w0, #0x9876, lsl #?16 +** ret +*/ +uint32_t +f2 (uint32_t x) +{ + return (x & 0xffff) | 0x98760000; +} + +/* +** f3: +** movk w0, #0x100, lsl #?16 +** ret +*/ +uint32_t +f3 (uint32_t x) +{ + return (x & 0xffff) | 0x01000000; +} + +/* +** g1: +** movk x0, #0x1234, lsl #?48 +** ret +*/ +uint64_t +g1 (uint64_t x) +{ + return (x & 0xffffffffffff) ^ 0x1234000000000000; +}
