> -----Original Message----- > From: [email protected] <[email protected]> > Sent: 18 November 2026 11:48 > To: [email protected] > Cc: Kyrylo Tkachov <[email protected]>; Tamar Christina > <[email protected]>; Wilco Dijkstra <[email protected]> > Subject: [PATCH] aarch64: Recognise any-or-plus forms of BFI and cost them > as one insn > > From: Kyrylo Tkachov <[email protected]> > > The any-or-plus work lets the middle end emit PLUS instead of IOR when > the operands have disjoint nonzero bits. The *aarch64_bfi patterns > still only match IOR, and aarch64_rtx_costs prices a BFI-shaped > IOR/PLUS as separate AND, AND and ORR/ADD operations. For > > int f1 (int x, int y) > { > return (x & ~0x0ffff00) | ((y << 8) & 0x0ffff00); > } > > this gives > > ubfiz w1, w1, 8, 16 > and w0, w0, -16776961 > add w0, w1, w0 > > instead of the single > > bfi w0, w1, 8, 16
Nice, > > For (x & ~0xffff) + (y & 0xffff), combine also merges the zero_extend > into the PLUS first and then rejects the BFI form as more expensive > than the three-instruction sequence. > > Use any_or_plus in the six *aarch64_bfi patterns that matched IOR. > Add aarch64_bfi_rtx_p and use it in aarch64_rtx_costs for IOR and PLUS > so that these forms cost one BFI. > > This fixes the gcc.target/aarch64/combine_bfi_1.c, combine_bfi_2.c and > combine_bfxil.c scan-assembler-times failures. > > The sign-extension of the high part of _BitInt(65) and _BitInt(127) > arguments in bitint-args.c is a BFI-shaped PLUS. With the lower cost > combine now keeps it as a single SBFX instead of EXTR, AND and ADD, > so update the expected output. > > Bootstrapped and tested on aarch64-none-linux-gnu. > Ok for trunk? OK, though a question below > Thanks, > Kyrill > > Cc: Tamar Christina <[email protected]> > Cc: Wilco Dijkstra <[email protected]> > > gcc/ChangeLog: > > * config/aarch64/aarch64.cc (aarch64_bfi_operands_p) > (aarch64_bfi_rtx_p): New functions. > (aarch64_rtx_costs): Use aarch64_bfi_rtx_p for PLUS and IOR. > * config/aarch64/aarch64.md (*aarch64_bfi<GPI:mode>5_shift) > (*aarch64_bfi<GPI:mode>5_shift_alt, > *aarch64_bfi<GPI:mode>4_noand) > (*aarch64_bfi<GPI:mode>4_noand_alt, > *aarch64_bfi<GPI:mode>4_noshift) > (*aarch64_bfi<GPI:mode>4_noshift_alt): Use any_or_plus instead of > ior. > > gcc/testsuite/ChangeLog: > > * gcc.target/aarch64/bitint-args.c (f65, f127): Update expected > output. > > Signed-off-by: Kyrylo Tkachov <[email protected]> > --- > gcc/config/aarch64/aarch64.cc | 67 ++++++++++++++++++ > gcc/config/aarch64/aarch64.md | 68 ++++++++++--------- > .../gcc.target/aarch64/bitint-args.c | 14 ++-- > 3 files changed, 108 insertions(+), 41 deletions(-) > > diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc > index fa3f623e969..0f42094c6dc 100644 > --- a/gcc/config/aarch64/aarch64.cc > +++ b/gcc/config/aarch64/aarch64.cc > @@ -15197,6 +15197,57 @@ aarch64_extr_rtx_p (rtx x, rtx *res_op0, rtx > *res_op1) > return false; > } > > +/* Return true if BASE and INS, the operands of an IOR or PLUS in MODE, > + match the *aarch64_bfi patterns with BASE being the value that is > + updated. Set *SRC to the register whose bits are inserted. This must > + be kept in sync with the operand forms of those patterns. */ > + > +static bool > +aarch64_bfi_operands_p (scalar_int_mode mode, rtx base, rtx ins, rtx *src) > +{ > + if (GET_CODE (base) != AND || !CONST_INT_P (XEXP (base, 1))) > + return false; > + > + unsigned HOST_WIDE_INT shft_amnt = 0; > + unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_M1U; > + if (GET_CODE (ins) == AND && CONST_INT_P (XEXP (ins, 1))) > + { > + mask = UINTVAL (XEXP (ins, 1)); > + ins = XEXP (ins, 0); > + } > + if (GET_CODE (ins) == ASHIFT && CONST_INT_P (XEXP (ins, 1))) > + { > + shft_amnt = UINTVAL (XEXP (ins, 1)); > + if (shft_amnt >= GET_MODE_BITSIZE (mode)) > + return false; > + /* Without an AND, the shift clears the low bits of the insertion. */ > + if (mask == HOST_WIDE_INT_M1U) > + mask <<= shft_amnt; > + ins = XEXP (ins, 0); > + } > + else if (mask == HOST_WIDE_INT_M1U) > + return false; > + > + *src = ins; > + return aarch64_masks_and_shift_for_bfi_p (mode, UINTVAL (XEXP (base, > 1)), > + shft_amnt, mask); > +} > + > +/* Return true if X is an IOR or PLUS that matches a *aarch64_bfi > + pattern. Set *SRC to the register whose bits are inserted. */ > + > +static bool > +aarch64_bfi_rtx_p (rtx x, rtx *src) > +{ > + scalar_int_mode mode; > + if (!is_a <scalar_int_mode> (GET_MODE (x), &mode) > + || (GET_CODE (x) != IOR && GET_CODE (x) != PLUS)) > + return false; > + > + return aarch64_bfi_operands_p (mode, XEXP (x, 0), XEXP (x, 1), src) > + || aarch64_bfi_operands_p (mode, XEXP (x, 1), XEXP (x, 0), src); > +} > + > /* Calculate the cost of calculating (if_then_else (OP0) (OP1) (OP2)), > storing it in *COST. Result is true if the total cost of the operation > has now been calculated. */ > @@ -15982,6 +16033,14 @@ cost_minus: > { > rtx new_op0; > > + if (aarch64_bfi_rtx_p (x, &op0)) > + { > + *cost += rtx_cost (op0, mode, code, 0, speed); > + if (speed) If aarch64_bfi_rtx_p is true, it means something has created the full RTX for the sequence no? so in that regard do we want to limit the replacement only in BB which have !optimizing_for_size set? Since the new sequence is also always smaller in codesize too no? In other words, should we not always cost this as the bfi? Thanks, Tamar > + *cost += extra_cost->alu.bfi; > + return true; > + } > + > op0 = XEXP (x, 0); > op1 = XEXP (x, 1); > > @@ -16128,6 +16187,14 @@ cost_plus: > > return true; > } > + > + if (aarch64_bfi_rtx_p (x, &op0)) > + { > + *cost += rtx_cost (op0, mode, code, 0, speed); > + if (speed) > + *cost += extra_cost->alu.bfi; > + return true; > + } > /* Fall through. */ > case XOR: > case AND: > diff --git a/gcc/config/aarch64/aarch64.md > b/gcc/config/aarch64/aarch64.md > index 4d9a794e292..350175e13bb 100644 > --- a/gcc/config/aarch64/aarch64.md > +++ b/gcc/config/aarch64/aarch64.md > @@ -6810,12 +6810,13 @@ > > (define_insn "*aarch64_bfi<GPI:mode>5_shift" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (and:GPI (match_operand:GPI 1 "register_operand" "0") > - (match_operand:GPI 2 "const_int_operand" "n")) > - (and:GPI (ashift:GPI > - (match_operand:GPI 3 "register_operand" "r") > - (match_operand:GPI 4 > "aarch64_simd_shift_imm_<mode>" > "n")) > - (match_operand:GPI 5 "const_int_operand" "n"))))] > + (any_or_plus:GPI > + (and:GPI (match_operand:GPI 1 "register_operand" "0") > + (match_operand:GPI 2 "const_int_operand" "n")) > + (and:GPI (ashift:GPI > + (match_operand:GPI 3 "register_operand" "r") > + (match_operand:GPI 4 "aarch64_simd_shift_imm_<mode>" > "n")) > + (match_operand:GPI 5 "const_int_operand" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[2]), > UINTVAL (operands[4]), > UINTVAL(operands[5]))" > @@ -6825,12 +6826,13 @@ > > (define_insn "*aarch64_bfi<GPI:mode>5_shift_alt" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (and:GPI (ashift:GPI > - (match_operand:GPI 1 "register_operand" "r") > - (match_operand:GPI 2 > "aarch64_simd_shift_imm_<mode>" > "n")) > - (match_operand:GPI 3 "const_int_operand" "n")) > - (and:GPI (match_operand:GPI 4 "register_operand" "0") > - (match_operand:GPI 5 "const_int_operand" "n"))))] > + (any_or_plus:GPI > + (and:GPI (ashift:GPI > + (match_operand:GPI 1 "register_operand" "r") > + (match_operand:GPI 2 "aarch64_simd_shift_imm_<mode>" > "n")) > + (match_operand:GPI 3 "const_int_operand" "n")) > + (and:GPI (match_operand:GPI 4 "register_operand" "0") > + (match_operand:GPI 5 "const_int_operand" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[5]), > UINTVAL (operands[2]), > UINTVAL(operands[3]))" > @@ -6843,11 +6845,12 @@ > > (define_insn "*aarch64_bfi<GPI:mode>4_noand" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (and:GPI (match_operand:GPI 1 "register_operand" "0") > - (match_operand:GPI 2 "const_int_operand" "n")) > - (ashift:GPI > - (match_operand:GPI 3 "register_operand" "r") > - (match_operand:GPI 4 > "aarch64_simd_shift_imm_<mode>" > "n"))))] > + (any_or_plus:GPI > + (and:GPI (match_operand:GPI 1 "register_operand" "0") > + (match_operand:GPI 2 "const_int_operand" "n")) > + (ashift:GPI > + (match_operand:GPI 3 "register_operand" "r") > + (match_operand:GPI 4 "aarch64_simd_shift_imm_<mode>" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[2]), > UINTVAL (operands[4]), > HOST_WIDE_INT_M1U << UINTVAL > (operands[4]) )" > @@ -6860,11 +6863,12 @@ > > (define_insn "*aarch64_bfi<GPI:mode>4_noand_alt" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (ashift:GPI > - (match_operand:GPI 1 "register_operand" "r") > - (match_operand:GPI 2 > "aarch64_simd_shift_imm_<mode>" > "n")) > - (and:GPI (match_operand:GPI 3 "register_operand" "0") > - (match_operand:GPI 4 "const_int_operand" "n"))))] > + (any_or_plus:GPI > + (ashift:GPI > + (match_operand:GPI 1 "register_operand" "r") > + (match_operand:GPI 2 "aarch64_simd_shift_imm_<mode>" "n")) > + (and:GPI (match_operand:GPI 3 "register_operand" "0") > + (match_operand:GPI 4 "const_int_operand" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[4]), > UINTVAL (operands[2]), > HOST_WIDE_INT_M1U << UINTVAL > (operands[2]) )" > @@ -6881,10 +6885,11 @@ > > (define_insn "*aarch64_bfi<GPI:mode>4_noshift" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (and:GPI (match_operand:GPI 1 "register_operand" "0") > - (match_operand:GPI 2 "const_int_operand" "n")) > - (and:GPI (match_operand:GPI 3 "register_operand" "r") > - (match_operand:GPI 4 "const_int_operand" "n"))))] > + (any_or_plus:GPI > + (and:GPI (match_operand:GPI 1 "register_operand" "0") > + (match_operand:GPI 2 "const_int_operand" "n")) > + (and:GPI (match_operand:GPI 3 "register_operand" "r") > + (match_operand:GPI 4 "const_int_operand" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[2]), 0, > UINTVAL (operands[4]))" > "bfi\t%<GPI:w>0, %<GPI:w>3, 0, %P4" > @@ -6893,10 +6898,11 @@ > > (define_insn "*aarch64_bfi<GPI:mode>4_noshift_alt" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (ior:GPI (and:GPI (match_operand:GPI 3 "register_operand" "r") > - (match_operand:GPI 4 "const_int_operand" "n")) > - (and:GPI (match_operand:GPI 1 "register_operand" "0") > - (match_operand:GPI 2 "const_int_operand" "n"))))] > + (any_or_plus:GPI > + (and:GPI (match_operand:GPI 3 "register_operand" "r") > + (match_operand:GPI 4 "const_int_operand" "n")) > + (and:GPI (match_operand:GPI 1 "register_operand" "0") > + (match_operand:GPI 2 "const_int_operand" "n"))))] > "aarch64_masks_and_shift_for_bfi_p (<MODE>mode, UINTVAL > (operands[2]), 0, > UINTVAL (operands[4]))" > "bfi\t%<GPI:w>0, %<GPI:w>3, 0, %P4" > @@ -6905,7 +6911,7 @@ > > (define_insn "*aarch64_bfxil<mode>_extr" > [(set (match_operand:GPI 0 "register_operand" "=r") > - (any_or_plus:GPI > + (any_or_plus:GPI > (and:GPI (match_operand:GPI 1 "register_operand" "0") > (match_operand:GPI 2 "const_int_operand" "n")) > (zero_extract:GPI > diff --git a/gcc/testsuite/gcc.target/aarch64/bitint-args.c > b/gcc/testsuite/gcc.target/aarch64/bitint-args.c > index bc580849658..88e151baed1 100644 > --- a/gcc/testsuite/gcc.target/aarch64/bitint-args.c > +++ b/gcc/testsuite/gcc.target/aarch64/bitint-args.c > @@ -67,22 +67,16 @@ CHECK_ARG(64) > CHECK_ARG(65) > /* > ** f65: > -** extr (x[0-9]+), x3, x2, 1 > -** and (x[0-9]+), x2, 1 > -** add (x[0-9]+), \2, \1, lsl 1 > -** asr (x[0-9]+), \1, 63 > -** stp \3, \4, \[x0\] > +** sbfx (x[0-9]+), x3, 0, 1 > +** stp x2, \1, \[x0\] > ** ret > */ > > CHECK_ARG(127) > /* > ** f127: > -** extr (x[0-9]+), x3, x2, 63 > -** and (x[0-9]+), x2, 9223372036854775807 > -** add (x[0-9]+), \2, \1, lsl 63 > -** asr (x[0-9]+), \1, 1 > -** stp \3, \4, \[x0\] > +** sbfx (x[0-9]+), x3, 0, 63 > +** stp x2, \1, \[x0\] > ** ret > */ > > -- > 2.54.0 (Apple Git-157)
