1 // REQUIRES: arm-registered-target
2 // RUN: %clang_cc1 -Wall -Werror -triple thumbv7-linux-gnueabi -fno-signed-char -O3 -emit-llvm -o - %s | FileCheck %s
3 
4 // Make sure the canonical use works before going into smaller details:
5 int atomic_inc(int *addr) {
6   int Failure, OldVal;
7   do {
8     OldVal = __builtin_arm_ldrex(addr);
9     Failure = __builtin_arm_strex(OldVal + 1, addr);
10   } while (Failure);
11 
12   return OldVal;
13 }
14 
15 // CHECK: @atomic_inc
16 // CHECK:   [[OLDVAL:%.*]] = tail call i32 @llvm.arm.ldrex.p0i32(i32* %addr)
17 // CHECK:   [[INC:%.*]] = add nsw i32 [[OLDVAL]], 1
18 // CHECK:   [[FAILURE:%.*]] = tail call i32 @llvm.arm.strex.p0i32(i32 [[INC]], i32* %addr)
19 // CHECK:   [[TST:%.*]] = icmp eq i32 [[FAILURE]], 0
20 // CHECK:   br i1 [[TST]], label {{%[a-zA-Z0-9.]+}}, label {{%[a-zA-Z0-9.]+}}
21 
22 struct Simple {
23   char a, b;
24 };
25 
26 int test_ldrex(char *addr, long long *addr64, float *addrfloat) {
27 // CHECK: @test_ldrex
28   int sum = 0;
29   sum += __builtin_arm_ldrex(addr);
30 // CHECK: [[INTRES:%.*]] = tail call i32 @llvm.arm.ldrex.p0i8(i8* %addr)
31 // CHECK: and i32 [[INTRES]], 255
32 
33   sum += __builtin_arm_ldrex((short *)addr);
34 // CHECK: [[ADDR16:%.*]] = bitcast i8* %addr to i16*
35 // CHECK: [[INTRES:%.*]] = tail call i32 @llvm.arm.ldrex.p0i16(i16* [[ADDR16]])
36 // CHECK: [[TMPSEXT:%.*]] = shl i32 [[INTRES]], 16
37 // CHECK: ashr exact i32 [[TMPSEXT]], 16
38 
39   sum += __builtin_arm_ldrex((int *)addr);
40 // CHECK: [[ADDR32:%.*]] = bitcast i8* %addr to i32*
41 // CHECK:  call i32 @llvm.arm.ldrex.p0i32(i32* [[ADDR32]])
42 
43   sum += __builtin_arm_ldrex((long long *)addr);
44 // CHECK: call { i32, i32 } @llvm.arm.ldrexd(i8* %addr)
45 
46   sum += __builtin_arm_ldrex(addr64);
47 // CHECK: [[ADDR64_AS8:%.*]] = bitcast i64* %addr64 to i8*
48 // CHECK: call { i32, i32 } @llvm.arm.ldrexd(i8* [[ADDR64_AS8]])
49 
50   sum += __builtin_arm_ldrex(addrfloat);
51 // CHECK: [[INTADDR:%.*]] = bitcast float* %addrfloat to i32*
52 // CHECK: [[INTRES:%.*]] = tail call i32 @llvm.arm.ldrex.p0i32(i32* [[INTADDR]])
53 // CHECK: bitcast i32 [[INTRES]] to float
54 
55   sum += __builtin_arm_ldrex((double *)addr);
56 // CHECK: [[STRUCTRES:%.*]] = tail call { i32, i32 } @llvm.arm.ldrexd(i8* %addr)
57 // CHECK: [[RESHI:%.*]] = extractvalue { i32, i32 } [[STRUCTRES]], 1
58 // CHECK: [[RESLO:%.*]] = extractvalue { i32, i32 } [[STRUCTRES]], 0
59 // CHECK: [[RESHI64:%.*]] = zext i32 [[RESHI]] to i64
60 // CHECK: [[RESLO64:%.*]] = zext i32 [[RESLO]] to i64
61 // CHECK: [[RESHIHI:%.*]] = shl nuw i64 [[RESHI64]], 32
62 // CHECK: [[INTRES:%.*]] = or i64 [[RESHIHI]], [[RESLO64]]
63 // CHECK: bitcast i64 [[INTRES]] to double
64 
65   sum += *__builtin_arm_ldrex((int **)addr);
66 // CHECK: [[INTRES:%.*]] = tail call i32 @llvm.arm.ldrex.p0i32(i32* [[ADDR32]])
67 // CHECK: inttoptr i32 [[INTRES]] to i32*
68 
69   sum += __builtin_arm_ldrex((struct Simple **)addr)->a;
70 // CHECK: [[INTRES:%.*]] = tail call i32 @llvm.arm.ldrex.p0i32(i32* [[ADDR32]])
71 // CHECK: inttoptr i32 [[INTRES]] to %struct.Simple*
72 
73   return sum;
74 }
75 
76 int test_strex(char *addr) {
77 // CHECK: @test_strex
78   int res = 0;
79   struct Simple var = {0};
80   res |= __builtin_arm_strex(4, addr);
81 // CHECK: call i32 @llvm.arm.strex.p0i8(i32 4, i8* %addr)
82 
83   res |= __builtin_arm_strex(42, (short *)addr);
84 // CHECK: [[ADDR16:%.*]] = bitcast i8* %addr to i16*
85 // CHECK:  call i32 @llvm.arm.strex.p0i16(i32 42, i16* [[ADDR16]])
86 
87   res |= __builtin_arm_strex(42, (int *)addr);
88 // CHECK: [[ADDR32:%.*]] = bitcast i8* %addr to i32*
89 // CHECK: call i32 @llvm.arm.strex.p0i32(i32 42, i32* [[ADDR32]])
90 
91   res |= __builtin_arm_strex(42, (long long *)addr);
92 // CHECK: call i32 @llvm.arm.strexd(i32 42, i32 0, i8* %addr)
93 
94   res |= __builtin_arm_strex(2.71828f, (float *)addr);
95 // CHECK: call i32 @llvm.arm.strex.p0i32(i32 1076754509, i32* [[ADDR32]])
96 
97   res |= __builtin_arm_strex(3.14159, (double *)addr);
98 // CHECK: call i32 @llvm.arm.strexd(i32 -266631570, i32 1074340345, i8* %addr)
99 
100   res |= __builtin_arm_strex(&var, (struct Simple **)addr);
101 // CHECK: [[INTVAL:%.*]] = ptrtoint i16* %var to i32
102 // CHECK: call i32 @llvm.arm.strex.p0i32(i32 [[INTVAL]], i32* [[ADDR32]])
103 
104   return res;
105 }
106 
107 void test_clrex() {
108 // CHECK: @test_clrex
109 
110   __builtin_arm_clrex();
111 // CHECK: call void @llvm.arm.clrex()
112 }
113