expand: Fix up IFN_ATOMIC_{BIT*,*CMP_0} expansion [PR105951]
authorJakub Jelinek <jakub@redhat.com>
Thu, 16 Jun 2022 08:58:58 +0000 (10:58 +0200)
committerJakub Jelinek <jakub@redhat.com>
Sun, 19 Jun 2022 10:08:35 +0000 (12:08 +0200)
commit86e10e471fddfcacb0c02d02f30ab5bb2038e504
tree77ce147df088a4b70ef6f9daefcbe524d1aa72ac
parentbf4ba940673b80961c5979078f9d37a7bef2f5ff
expand: Fix up IFN_ATOMIC_{BIT*,*CMP_0} expansion [PR105951]

Both IFN_ATOMIC_BIT_TEST_AND_* and IFN_ATOMIC_*_FETCH_CMP_0 ifns
are matched if their corresponding optab is implemented for the particular
mode.  The fact that those optabs are implemented doesn't guarantee
they will succeed though, they can just FAIL in their expansion.
The expansion in that case uses expand_atomic_fetch_op as fallback, but
as has been reported and and can be reproduced on the testcases,
even those can fail and we didn't have any fallback after that.
For IFN_ATOMIC_BIT_TEST_AND_* we actually have such calls.  One is
done whenever we lost lhs of the ifn at some point in between matching
it in tree-ssa-ccp.cc and expansion.  The following patch for that case
just falls through and expands as if there was a lhs, creates a temporary
for it.  For the other expand_atomic_fetch_op call in the same expander
and for the only expand_atomic_fetch_op call in the other, this falls
back the hard way, by constructing a CALL_EXPR to the call from which
the ifn has been matched and expanding that.  Either it is lucky and manages
to expand inline, or it emits a libatomic API call.
So that we don't have to rediscover which builtin function to call in the
fallback, we record at tree-ssa-ccp.cc time gimple_call_fn (call) in
an extra argument to the ifn.

2022-06-16  Jakub Jelinek  <jakub@redhat.com>

PR middle-end/105951
* tree-ssa-ccp.cc (optimize_atomic_bit_test_and,
optimize_atomic_op_fetch_cmp_0): Remember gimple_call_fn (call)
as last argument to the internal functions.
* builtins.cc (expand_ifn_atomic_bit_test_and): Adjust for the
extra call argument to ifns.  If expand_atomic_fetch_op fails for the
lhs == NULL_TREE case, fall through into the optab code with
gen_reg_rtx (mode) as target.  If second expand_atomic_fetch_op
fails, construct a CALL_EXPR and expand that.
(expand_ifn_atomic_op_fetch_cmp_0): Adjust for the extra call argument
to ifns.  If expand_atomic_fetch_op fails, construct a CALL_EXPR and
expand that.

* gcc.target/i386/pr105951-1.c: New test.
* gcc.target/i386/pr105951-2.c: New test.

(cherry picked from commit 6a27c430468cb85454b19cef881a1422580657ff)
gcc/builtins.cc
gcc/testsuite/gcc.target/i386/pr105951-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr105951-2.c [new file with mode: 0644]
gcc/tree-ssa-ccp.cc