]> git.ipfire.org Git - thirdparty/gcc.git/commit
middle-end: Fold vec_cond into conditional ternary or binary operation when sharing...
authorTamar Christina <tamar.christina@arm.com>
Wed, 18 Oct 2023 08:32:55 +0000 (09:32 +0100)
committerTamar Christina <tamar.christina@arm.com>
Wed, 18 Oct 2023 08:53:47 +0000 (09:53 +0100)
commit4b39aeef594f311e2c1715f15608f1d7ebc2d868
treebc0f36b93c71bb595b54948fd82d3ecc4cc6bb7d
parentb588dcb77e96d77777eb5647cba9e8f454e314dc
middle-end: Fold vec_cond into conditional ternary or binary operation when sharing operand [PR109154]

When we have a vector conditional on a masked target which is doing a selection
on the result of a conditional operation where one of the operands of the
conditional operation is the other operand of the select, then we can fold the
vector conditional into the operation.

Concretely this transforms

  c = mask1 ? (masked_op mask2 a b) : b

into

  c = masked_op (mask1 & mask2) a b

The mask is then propagated upwards by the compiler.  In the SVE case we don't
end up needing a mask AND here since `mask2` will end up in the instruction
creating `mask` which gives us a natural &.

Such transformations are more common now in GCC 13+ as PRE has not started
unsharing of common code in case it can make one branch fully independent.

e.g. in this case `b` becomes a loop invariant value after PRE.

This transformation removes the extra select for masked architectures but
doesn't fix the general case.

gcc/ChangeLog:

PR tree-optimization/109154
* match.pd: Add new cond_op rule.

gcc/testsuite/ChangeLog:

PR tree-optimization/109154
* gcc.target/aarch64/sve/pre_cond_share_1.c: New test.
gcc/match.pd
gcc/testsuite/gcc.target/aarch64/sve/pre_cond_share_1.c [new file with mode: 0644]