https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123313
--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:baa8b724674018e568c571b2b610658d8dc0223d commit r17-1060-gbaa8b724674018e568c571b2b610658d8dc0223d Author: Jeff Law <[email protected]> Date: Sun May 31 16:34:42 2026 -0600 [RISC-V][PR rtl-optimization/123313] Improve select between reg,-1 So this improves our ability to select across reg,-1. The early versions of this patch allowed const,-1, but those sequences weren't any better and occasionally ever-so-slightly worse, so those are rejected. I've spot checked spec2017 where it does show up, but it's not clear that it's showing up in any hot code. The basic idea is to use an sCC to generate 1,0, subtract 1 giving 0, -1. Then we can IOR that with the other input. Concretely: > int f(int a, int b, int c) > { > a = -1; > if (c < 10) a = b; > return a; > } Currently generates: > li a5,9 > addi a1,a1,1 > sgt a2,a2,a5 > czero.nez a2,a1,a2 > addi a0,a2,-1 > ret After this patch: > slti a0,a2,10 > addi a0,a0,-1 > or a0,a0,a1 > ret Probably the same performance on 4+ wide designs (and perhaps often on a 2 wide designs). But it encodes a lot more efficiently, 18 bytes for the first sequence, just 10 bytes for the second. That can be important on some designs, particularly since if-converted blocks are more likely to be large and/or cross cache line boundaries. This has been bootstrapped and regression tested on x86, and riscv64. The riscv64 bootstraps were on the Pioneer, K1 (early version of the patch) and K3 (most recent versions). It's also been tested on all the *-elf platforms in my tester as well as additional bootstraps on platforms like alpha, sh4, etc. I'll wait for a final confirmation from the pre-commit tester before moving forward. PR rtl-optimization/123313 gcc/ * ifcvt.cc (noce_try_store_flag_logical): New function. (noce_process_if_block): Call it. gcc/testsuite/ * gcc.target/riscv/pr123313.c: New test. * gcc.target/riscv/pr124009.c: Adjust expected output.
