https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128767
Bug ID: 128767
Summary: Improve simplification of scc sequences, particularly
for RISC-V
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: law at gcc dot gnu.org
Reporter: law at gcc dot gnu.org
Blocks: 26163
Target Milestone: ---
Target: riscv
The RISC-V ISA has a limited set of scc style insns. Concretely we can
directly implement x < y or x < C for small constants. However, we can not
implement something like x >= C.
To get the full complement of scc style insns we often have to invert another
sequence. The inversion can be done with seq/xor#1. Both will invert a
truthvalue.
If we look at gimple a chain of logical operations on truthvalues can look
optimal, but because of the limited scc forms on RISC-V, the generated code can
be pretty bad.
Interestingly enough this often comes up with the aliasing checks emitted by
the vectorizer to select across a scalar vs vector loop. pixel_avg from x264
being one warm example, but there's others.
With these sequences looking good in gimple, this needs to be a backend
improvement. Trying to capture with a target pattern looks like a bad choice
-- we're not often going to see enough insns to improve things. simplify-rtx
seems much better.
The key to cat this in simplify-rtx is to utilize nonzero_bits to detect when
we're using an xor to flip a truthvalue. With that trick we can apply
demorgan's rules and ultimately generate better code -- to the tune of
hundreds of millions fewer instructions in x264.
Here's a testcase that should show this issue. It's LLM generated:
/* { dg-do compile } */
/* { dg-options "-O2 -march=rv64gc_zicond -mabi=lp64d -fgimple" } */
/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
/* A select on (C ^ 1) where C is known to be 0 or 1 is a select on C == 0,
so the XOR is not needed and the operation can be computed
unconditionally ahead of a single czero. Each function below is
slt; op; czero. */
long
__GIMPLE (ssa,startwith ("optimized"))
f_add_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) + b_4(D);
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_add_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) + b_4(D);
t_10 = e_8 ? 0l : t_9;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_addi_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) + 5l;
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_addi_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) + 5l;
t_10 = e_8 ? 0l : t_9;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_sub_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) - b_4(D);
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_sub_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) - b_4(D);
t_10 = e_8 ? 0l : t_9;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_or_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) | b_4(D);
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_or_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) | b_4(D);
t_10 = e_8 ? 0l : t_9;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_xor_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) ^ b_4(D);
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_xor_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) ^ b_4(D);
t_10 = e_8 ? 0l : t_9;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_and_nez (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) & b_4(D);
t_10 = e_8 ? t_9 : 0l;
return t_10;
}
long
__GIMPLE (ssa,startwith ("optimized"))
f_and_eqz (long x, long y, long a, long b)
{
_Bool c;
long d;
_Bool e;
long t;
__BB(2):
c_5 = x_1(D) < y_2(D);
d_6 = (long) c_5;
d_7 = d_6 ^ 1l;
e_8 = d_7 != 0l;
t_9 = a_3(D) & b_4(D);
t_10 = e_8 ? 0l : t_9;
return t_10;
}
/* Every function is slt, one operation, one czero, with no XOR to invert the
condition. */
/* { dg-final { scan-assembler-times
"\tslt\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 12 } } */
/* { dg-final { scan-assembler-times "\tczero\.nez\t" 6 } } */
/* { dg-final { scan-assembler-times "\tczero\.eqz\t" 6 } } */
/* { dg-final { scan-assembler-not "\txori\t" } } */
/* { dg-final { scan-assembler-times
"\tadd\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 2 } } */
/* { dg-final { scan-assembler-times "\taddi\t\[axt\]\[0-9\],\[axt\]\[0-9\],5"
2 } } */
/* { dg-final { scan-assembler-times
"\tsub\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 2 } } */
/* { dg-final { scan-assembler-times
"\tor\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 2 } } */
/* { dg-final { scan-assembler-times
"\txor\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 2 } } */
/* { dg-final { scan-assembler-times
"\tand\t\[axt\]\[0-9\],\[axt\]\[0-9\],\[axt\]\[0-9\]" 2 } } */
I've got simplify-rtx.cc changes to fix this stuff, but it needs another test
with x264, verification that all three simplify-rtx fragments are really
needed, bootstrapping on other architectres and wider testing.
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=26163
[Bug 26163] [meta-bug] missed optimization in SPEC (2026, 2k17, 2k and 2k6 and
95)