1 //===-- sync-ops.h - --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements outline macros for the __sync_fetch_and_*
10 // operations. Different instantiations will generate appropriate assembly for
11 // ARM and Thumb-2 versions of the functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "../assembly.h"
16 
17 #define SYNC_OP_4(op)                                                          \
18   .p2align 2;                                                                  \
19   .thumb;                                                                      \
20   .syntax unified;                                                             \
21   DEFINE_COMPILERRT_THUMB_FUNCTION(__sync_fetch_and_##op)                      \
22   dmb;                                                                         \
23   mov r12, r0;                                                                 \
24   LOCAL_LABEL(tryatomic_##op) : ldrex r0, [r12];                               \
25   op(r2, r0, r1);                                                              \
26   strex r3, r2, [r12];                                                         \
27   cmp r3, #0;                                                                  \
28   bne LOCAL_LABEL(tryatomic_##op);                                             \
29   dmb;                                                                         \
30   bx lr
31 
32 #define SYNC_OP_8(op)                                                          \
33   .p2align 2;                                                                  \
34   .thumb;                                                                      \
35   .syntax unified;                                                             \
36   DEFINE_COMPILERRT_THUMB_FUNCTION(__sync_fetch_and_##op)                      \
37   push {r4, r5, r6, lr};                                                       \
38   dmb;                                                                         \
39   mov r12, r0;                                                                 \
40   LOCAL_LABEL(tryatomic_##op) : ldrexd r0, r1, [r12];                          \
41   op(r4, r5, r0, r1, r2, r3);                                                  \
42   strexd r6, r4, r5, [r12];                                                    \
43   cmp r6, #0;                                                                  \
44   bne LOCAL_LABEL(tryatomic_##op);                                             \
45   dmb;                                                                         \
46   pop { r4, r5, r6, pc }
47 
48 #define MINMAX_4(rD, rN, rM, cmp_kind)                                         \
49   cmp rN, rM;                                                                  \
50   mov rD, rM;                                                                  \
51   it cmp_kind;                                                                 \
52   mov##cmp_kind rD, rN
53 
54 #define MINMAX_8(rD_LO, rD_HI, rN_LO, rN_HI, rM_LO, rM_HI, cmp_kind)           \
55   cmp rN_LO, rM_LO;                                                            \
56   sbcs rN_HI, rM_HI;                                                           \
57   mov rD_LO, rM_LO;                                                            \
58   mov rD_HI, rM_HI;                                                            \
59   itt cmp_kind;                                                                \
60   mov##cmp_kind rD_LO, rN_LO;                                                  \
61   mov##cmp_kind rD_HI, rN_HI
62