[Bug target/125765] `a ? CST0 : (CST0|CST1)` can be handled better for riscv
Milan.Tripkovic@rt-rk.com
gcc-bugzilla@gcc.gnu.org
Thu Jul 2 11:37:49 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125765
Milan Tripkovic <Milan.Tripkovic@rt-rk.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |Milan.Tripkovic@rt-rk.com
--- Comment #3 from Milan Tripkovic <Milan.Tripkovic@rt-rk.com> ---
Created attachment 64919
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64919&action=edit
patchv1
Hello, I added a patch that extends noce_try_cmove_arith and generate correct
ASM for current problem. However, it contains a hard-coded variable,
cost_ignore, because of a costing issue I encountered.
Without variable the optimization doesn't apply because the compiler thinks
this is too expensive at the moment. During the ce1 pass, the cost is
calculated based on RTL, and the compiler can't see future optimizations (like
the combine pass) that will later simplify the RTL, get rid of redundant
instructions, and eventually generate much better assembly.
FOR code:
>int src(int c, int b, int a) {
> int t = (a ? 0xff10 : 0xff00);
> return t;
>}
trunk rv64gcb_zicond has cost 20 in ce1
RTL after ce1 ASM
movdi_64bit li a0,65536
mvconst_internal addi a0,a0,-208
mvconst_internal czero.eqz a0,a0,a2
czero.eqz.did li a5,65536
czero.nez.didi addi a5,a5,-256
adddi3 czero.nez a2,a5,a2
movdi_64bit add a0,a2,a0
ret
trunk rv64gcb has cost 20 in ce1
RTL after ce1 ASM
movdi_64bit li a0,65536
mvconst_internal addi a0,a0,-256
branchdi beq a2,zero,.L2
mvconst_internal li a0,65536
movdi_64bit addi a0,a0,-208
ret
rv64gcb_zicond with my change has cost 32 in ce1
RTL after ce1 ASM
movdi_64bit li a5,48
movdi_64bit czero.nez a0,a5,a2
movdi_64bit li a5,65536
czero.eqz.didi addi a5,a5,-208
czero.nez.didi xor a0,a0,a5
adddi3 ret
movdi_64bit
adddi3
xordi3
movdi_64bit
movdi_64bit
in 2nd example my RTL has cost 60 but generates better code.
>long f(long a)
>{
> long t = 1234567890123;
> if (a) return t;
> return t+12;
>}
trunk rv64gcb_zicond
f:
bne a0,zero,.L6
li a0,18837504
addi a0,a0,507
slli a0,a0,16
addi a0,a0,1239
ret
.L6:
li a0,18837504
addi a0,a0,507
slli a0,a0,16
addi a0,a0,1227
ret
rv64gcb_zicond with my change
f:
li a5,28
czero.nez a0,a5,a0
li a5,18837504
addi a5,a5,507
slli a5,a5,16
addi a5,a5,1227
xor a0,a0,a5
ret
I have question about this problem is this right way to optimize out or should
i have different approach.
More information about the Gcc-bugs
mailing list