LoongArch: Fix atomic_exchange expanding [PR107713]
authorJinyang He <hejinyang@loongson.cn>
Thu, 17 Nov 2022 06:38:52 +0000 (14:38 +0800)
committerLulu Cheng <chenglulu@loongson.cn>
Fri, 18 Nov 2022 07:02:27 +0000 (15:02 +0800)
commitf0024bfb228f94e60e06dc32a4983e40a9b90be5
tree8b7e6f36b177d2de6195c7d3bd2d292c542ba67e
parent7b3b2f50953c5143d4b14b59d322d8a793f411dd
LoongArch: Fix atomic_exchange expanding [PR107713]

We used to expand atomic_exchange_n(ptr, new, mem_order) for subword types
into something like:

    {
      __typeof__(*ptr) t = atomic_load_n(ptr, mem_order);
      atomic_compare_exchange_n(ptr, &t, new, true, mem_order, mem_order);
      return t;
    }

It's incorrect because another thread may store a different value into *ptr
after atomic_load_n.  Then atomic_compare_exchange_n will not store into
*ptr, but atomic_exchange_n should always perform the store.

gcc/ChangeLog:

PR target/107713
* config/loongarch/sync.md
(atomic_cas_value_exchange_7_<mode>): New define_insn.
(atomic_exchange): Use atomic_cas_value_exchange_7_si instead of
atomic_cas_value_cmp_and_7_si.

gcc/testsuite/ChangeLog:

PR target/107713
* gcc.target/loongarch/pr107713-1.c: New test.
* gcc.target/loongarch/pr107713-2.c: New test.
gcc/config/loongarch/sync.md
gcc/testsuite/gcc.target/loongarch/pr107713-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/loongarch/pr107713-2.c [new file with mode: 0644]