[PATCH] PR target/125167 - More select improvements for RISC-V

Milan Tripkovic Milan.Tripkovic@rt-rk.com
Thu Oct 8 14:33:44 GMT 2026


This patch fixes PR125167, where two select cases generate
conditional branches on RISC-V even though better sequences exist:

long foo2 (long c, long a, long b) { return c < 0 ? a : b; }
long foo3 (long c) { return c < 0 ? 16383 : 0; }

Patch contains 2 changes: noce_try_sign_mask and noce_try_sign_bit_splat.

noce_try_sign_mask only handled selects where one arm is zero.  Extend
it to select between two registers as X = ((T ^ F) & M) ^ F, where M is
the sign mask. When M is all ones the expression evaluates to T, and when
M is zero it evaluates to F. Both arms are evaluated unconditionally,
so this is limited to registers.  As in the existing zero-arm case, the result
is committed without calling noce_conversion_profitable_p.

Foo2 before:
        bge a0,zero,.L7
        mv a2,a1
.L7:
        mv a0,a2
        ret

Foo2 after:
        srai    a0,a0,63
        xor     a1,a1,a2
        and     t0,a0,a1
        xor     a0,t0,a2
        ret

noce_try_sign_bit_splat already knows how to build 2^n-1 from the sign
mask, but 16383 takes two insns to synthesize, so the THEN block is not
simple and transformation was rejected early.  The value is still recorded in a
REG_EQUAL note on the last insn of the block, and
bb_valid_for_noce_process_p has already checked that such a block has
no side effects and that its temporaries are dead on exit.  So recover
constant arms from REG_EQUAL or REG_EQUIV notes and accept non-simple
blocks whose value was recovered that way.

Foo3 before:
        blt a0,zero,.L12
        li a0,0
        ret
.L12:
        li a0,16384
        addi a0,a0,-1
        ret

Foo3 after:
        srai    a0,a0,63
        srli    a0,a0,50
        ret

This patch also handles the MSB case in pr120553-5.c and pr120553-6.c
on both rv32 and rv64, which those tests noted as not yet handled, so
adjust the expected instruction counts accordingly.

Regression tested on riscv64 and x86_64.

2026-10-08  Milan Tripkovic  <Milan.Tripkovic@rt-rk.com>

      PR target/125167

gcc/ChangeLog:

      * ifcvt.cc (noce_try_sign_bit_splat): Recover synthesized constants
      (noce_try_sign_mask): Handle selects between two nonzero registers.


gcc/testsuite/ChangeLog:

      * gcc.target/riscv/pr120553-5.c: Adjust expected counts.
      * gcc.target/riscv/pr120553-6.c: Adjust expected counts.
      * gcc.target/riscv/pr125167-1.c: New test.
      * gcc.target/riscv/pr125167-2.c: New test.





CONFIDENTIALITY: The contents of this e-mail are confidential and intended only for the above addressee(s). If you are not the intended recipient, or the person responsible for delivering it to the intended recipient, copying or delivering it to anyone else or using it in any unauthorized manner is prohibited and may be unlawful. If you receive this e-mail by mistake, please notify the sender and the systems administrator at straymail@rt-rk.com immediately.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20261008/ffb45f7b/attachment-0001.htm>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: 0001-f4.patch.txt
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20261008/ffb45f7b/attachment-0001.txt>


More information about the Gcc-patches mailing list