[PATCH] phi-opt: Add missed optimization for "(cond | (a != b)) ? b : a"
Jovan Vukic
Jovan.Vukic@rt-rk.com
Tue Oct 22 08:32:33 GMT 2024
Currently, within the phiopt pass, under value_replacement, we have the
option to replace the expression "(cond & (a == b)) ? a : b" with "b". It
checks whether there is a BIT_AND_EXPR and verifies if one of the operands
contains the expression "a == b".
However, the same result should also apply to the expression with the
inverted condition "(cond | (a != b)) ? b : a," so this patch makes slight
modifications to the existing code to cover this situation as well. The
patch adds a check for whether there is a BIT_IOR_EXPR and if it contains
"a != b".
I added a test for both optimizations, as I could not find an adequate test
in the testsuite for the first one (even though that optimization exists).
For example, for this C code:
int foo(int a, int b, int c) {
return ((a > c) | (a != b)) ? b : a;
}
Before the patch, the generated assembly for RISC-V is:
foo:
bgt a0, a2, .L4
bne a0, a1, .L4
ret
.L4:
mv a0, a1
ret
After the patch, the generated assembly becomes:
foo:
mv a0, a1
ret
2024-10-22 Jovan Vukic <Jovan.Vukic@rt-rk.com>
gcc/ChangeLog:
* tree-ssa-phiopt.cc
(rhs_is_fed_for_value_replacement): Add a new optimization opportunity
for BIT_IOR_EXPR and a != b.
(operand_equal_for_value_replacement): Ditto.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/phi-opt-same-3.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 --------------
A non-text attachment was scrubbed...
Name: patch.patch
Type: text/x-patch
Size: 5202 bytes
Desc: patch.patch
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20241022/fca09cba/attachment.bin>
More information about the Gcc-patches
mailing list