Commit 5de59c6
Jeff Law
[RISC-V][PR rtl-optimization/56096] Improve equality comparisons of a logical AND expressions
This BZ shows that we can improve certain comparisons for RISC-V. In
particular if we are testing the result of a logical AND for equality and one
operand of the AND requires synthesis, we may be able to do better if we right
shift away any trailing zeros from the constant and shift the other input as
well. This wins when the shifted constant does not require synthesis.
That may in turn allow improvement of a select of 0 and 2^n based on the
zero/nonzero status of a logical AND. Essentially we can rewrite the sequence
to remove a data dependency.
Concretely:
>
> unsigned f1 (unsigned x, unsigned m)
> {
> x >>= ((m & 0x008080) ? 8 : 0);
> return x;
> }
Compiles into:
> li a5,32768
> addi a5,a5,128
> and a1,a1,a5
> snez a1,a1
> slliw a1,a1,3
> srlw a0,a0,a1
> ret
But after this patch we generate this instead:
> srai a5,a1,7
> andi a5,a5,257
> li a4,8
> czero.eqz a1,a4,a5
> srlw a0,a0,a1
> ret
It's just one less instruction, but the li can issue whenever the uarch wants
before the srlw as it has no incoming dependency. So we're slight more dense
on encoding and slightly more efficient as well. Much like 57650, I'm focused
on the low level RISC-V codegen issues, not the broader issues that are raised
in the PR.
This has been in my tree for a while, so it's been tested on riscv32-elf,
riscv64-elf and bootstrapped on the BPI which has support for czero. Waiting
on pre-commit CI before moving forward.
PR rtl-optimization/56096
gcc/
* config/riscv/riscv.md: Add new patterns to optimize certain cases with
a logical AND feeding an equality test against zero.
gcc/testsuite/
* gcc.target/riscv/pr56096.c: New test.1 parent 7828030 commit 5de59c6
2 files changed
Lines changed: 75 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3201 | 3201 | | |
3202 | 3202 | | |
3203 | 3203 | | |
| 3204 | + | |
| 3205 | + | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
| 3209 | + | |
| 3210 | + | |
| 3211 | + | |
| 3212 | + | |
| 3213 | + | |
| 3214 | + | |
| 3215 | + | |
| 3216 | + | |
| 3217 | + | |
| 3218 | + | |
| 3219 | + | |
| 3220 | + | |
| 3221 | + | |
| 3222 | + | |
| 3223 | + | |
| 3224 | + | |
| 3225 | + | |
| 3226 | + | |
| 3227 | + | |
| 3228 | + | |
| 3229 | + | |
| 3230 | + | |
| 3231 | + | |
| 3232 | + | |
| 3233 | + | |
| 3234 | + | |
| 3235 | + | |
| 3236 | + | |
| 3237 | + | |
| 3238 | + | |
| 3239 | + | |
| 3240 | + | |
| 3241 | + | |
| 3242 | + | |
| 3243 | + | |
| 3244 | + | |
| 3245 | + | |
| 3246 | + | |
| 3247 | + | |
| 3248 | + | |
| 3249 | + | |
| 3250 | + | |
| 3251 | + | |
| 3252 | + | |
| 3253 | + | |
| 3254 | + | |
| 3255 | + | |
| 3256 | + | |
| 3257 | + | |
| 3258 | + | |
| 3259 | + | |
3204 | 3260 | | |
3205 | 3261 | | |
3206 | 3262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
0 commit comments