On Tue, Oct 27, 2026 at 12:11 AM Takayuki 'January June' Suwa <[email protected]> wrote: > > For example, in the Xtensa ISA, there are no machine instructions for > bitwise inversion (1's complement) or immediate-form XOR. Consequently, > implementing the former requires either assigning -1 into a scratch register > and performing a reg-reg XOR, or adding 1 to the source value and inverting > its sign. The former implementation offers opportunities for CSE and loop- > invariant hoisting, while the latter results in lower register pressure. > Thus, when implementing an instruction that cannot be reduced to a single > machine instruction, there are trade-offs regarding optimization depending > on whether or not a scratch register containing a constant value is used. > > Now, looking at the optimization process after the RTL generation pass, we > see that loop optimization follows forward propagation and CSE, and then > instruction combination optimization. Following this fact, it would be > better to use constant-value scratch registers for insns that are material- > ized early during RTL passes, such as those following the define_expand MD > pattern, while such scratch registers should be avoided whenever possible > for non-standard name patterns (often described by define_insn_and_split) > found in the later instruction combination pass. Even more conveniently, > in the instruction combination pass, registers that have been subjected > to CSE or loop-invariant hoisting are not used in the search for instruc- > tions to combine. > > However, defining a insn(_and_split) MD pattern naturally means that the > instruction recognizer will begin to recognize that pattern. In practice, > because recog() is conveniently invoked during early RTL passes (such as in > forwprop), patterns that do not use constant-value scratch registers appear > before CSE or loop optimization passes, and the intended optimization may > not be achieved. > > This patch introduces a new target-specific predicate function that is false > until the instruction combination RTL pass is reached, thereby providing a > means to counteract the above disadvantage. > > /* example #1 */ > int test0(int a) { > return ~a; > } > int test1(int a[]) { > int i; > for (i = 0; i < 16; ++i) > a[i] = ~a[i]; > } > > /* after #1 (-O2) */ > test0: > entry sp, 32 > addi.n a2, a2, 1 ;; "*one_cmplsi2" insn_and_split > neg a2, a2 ;; before this patch, > ;; "movi.n a8, -1; xor a2, a2, a8" > retw.n > test1: > entry sp, 32 > movi.n a10, -1 ;; hoisted out > movi.n a9, 0x10 > loop a9, .L4_LEND > .L4: > l32i.n a8, a2, 0 > xor a8, a8, a10 ;; "one_cmplsi2" standard generation > s32i.n a8, a2, 0 > addi.n a2, a2, 4 > .L4_LEND: > retw.n > > /* example #2 */ > int test0(int a) { > return a & 0x0001FFFF; > } > int test1(int a[]) { > int i; > for (i = 0; i < 16; ++i) > a[i] &= 0x0001FFFF; > } > > /* after #2 (-O2) */ > test0: > entry sp, 32 > slli a2, a2, 15 ;; "*andsi3_const_pow2_minus_one" > srli a2, a2, 15 ;; insn_and_split > retw.n > test1: > entry sp, 32 > movi.n a10, -1 ;; hoisted out > srli a10, a10, 15 ;; > movi.n a9, 0x10 > loop a9, .L4_LEND > .L4: > l32i.n a8, a2, 0 > and a8, a8, a10 ;; "andsi3" standard generation > ;; before this patch, > ;; "slli a8, a8, 15; srli a8, a8, 15" > s32i.n a8, a2, 0 > addi.n a2, a2, 4 > .L4_LEND: > retw.n > > gcc/ChangeLog: > > * gcc/config/xtensa/xtensa-passes.def (pass_xtensa_before_combine): > New directive that inserts pass_xtensa_before_combine immediately > before pass_combine. > * config/xtensa/xtensa-protos.h > (xtensa_rtl_combine_reached_p, make_pass_xtensa_before_combine): > New function prototypes. > * config/xtensa/xtensa.cc (machine_function): > Add a new member "rtl_combine_reached". > (xtensa_rtl_combine_reached_p, make_pass_xtensa_before_combine): > New function definitions. > (pass_data_xtensa_before_combine, pass_xtensa_before_combine): > New target-specific hidden pass definition, whose sole purpose is > to set cfun->machine->rtl_combine_reached to true. > * config/xtensa/xtensa.md > (*subsi3_from_const, *andsi3_const_pow2_minus_one, > *andsi3_const_negative_pow2, *andsi3_const_shifted_mask): > Add the condition that xtensa_rtl_combine_reached_p() is true to > the "condition" of each MD definition. > (*one_cmplsi2): New insn_and_split pattern. > --- > gcc/config/xtensa/xtensa-passes.def | 1 + > gcc/config/xtensa/xtensa-protos.h | 2 ++ > gcc/config/xtensa/xtensa.cc | 54 +++++++++++++++++++++++++++++ > gcc/config/xtensa/xtensa.md | 33 +++++++++++++++--- > 4 files changed, 85 insertions(+), 5 deletions(-)
Regtested for target=xtensa-linux-uclibc, no new regressions. Committed both changes to master. -- Thanks. -- Max
