1; RUN: llc < %s -mtriple=armv7-apple-darwin   | FileCheck %s
2; RUN: llc < %s -mtriple=thumbv7-apple-darwin > %t
3; RUN: FileCheck %s < %t
4; RUN: FileCheck %s < %t --check-prefix=CHECK-T2ADDRMODE
5
6%0 = type { i32, i32 }
7
8; CHECK-LABEL: f0:
9; CHECK: ldrexd
10define i64 @f0(i8* %p) nounwind readonly {
11entry:
12  %ldrexd = tail call %0 @llvm.arm.ldrexd(i8* %p)
13  %0 = extractvalue %0 %ldrexd, 1
14  %1 = extractvalue %0 %ldrexd, 0
15  %2 = zext i32 %0 to i64
16  %3 = zext i32 %1 to i64
17  %shl = shl nuw i64 %2, 32
18  %4 = or i64 %shl, %3
19  ret i64 %4
20}
21
22; CHECK-LABEL: f1:
23; CHECK: strexd
24define i32 @f1(i8* %ptr, i64 %val) nounwind {
25entry:
26  %tmp4 = trunc i64 %val to i32
27  %tmp6 = lshr i64 %val, 32
28  %tmp7 = trunc i64 %tmp6 to i32
29  %strexd = tail call i32 @llvm.arm.strexd(i32 %tmp4, i32 %tmp7, i8* %ptr)
30  ret i32 %strexd
31}
32
33declare %0 @llvm.arm.ldrexd(i8*) nounwind readonly
34declare i32 @llvm.arm.strexd(i32, i32, i8*) nounwind
35
36; CHECK-LABEL: test_load_i8:
37; CHECK: ldrexb r0, [r0]
38; CHECK-NOT: uxtb
39; CHECK-NOT: and
40define zeroext i8 @test_load_i8(i8* %addr) {
41  %val = call i32 @llvm.arm.ldrex.p0i8(i8* %addr)
42  %val8 = trunc i32 %val to i8
43  ret i8 %val8
44}
45
46; CHECK-LABEL: test_load_i16:
47; CHECK: ldrexh r0, [r0]
48; CHECK-NOT: uxth
49; CHECK-NOT: and
50define zeroext i16 @test_load_i16(i16* %addr) {
51  %val = call i32 @llvm.arm.ldrex.p0i16(i16* %addr)
52  %val16 = trunc i32 %val to i16
53  ret i16 %val16
54}
55
56; CHECK-LABEL: test_load_i32:
57; CHECK: ldrex r0, [r0]
58define i32 @test_load_i32(i32* %addr) {
59  %val = call i32 @llvm.arm.ldrex.p0i32(i32* %addr)
60  ret i32 %val
61}
62
63declare i32 @llvm.arm.ldrex.p0i8(i8*) nounwind readonly
64declare i32 @llvm.arm.ldrex.p0i16(i16*) nounwind readonly
65declare i32 @llvm.arm.ldrex.p0i32(i32*) nounwind readonly
66
67; CHECK-LABEL: test_store_i8:
68; CHECK-NOT: uxtb
69; CHECK: strexb r0, r1, [r2]
70define i32 @test_store_i8(i32, i8 %val, i8* %addr) {
71  %extval = zext i8 %val to i32
72  %res = call i32 @llvm.arm.strex.p0i8(i32 %extval, i8* %addr)
73  ret i32 %res
74}
75
76; CHECK-LABEL: test_store_i16:
77; CHECK-NOT: uxth
78; CHECK: strexh r0, r1, [r2]
79define i32 @test_store_i16(i32, i16 %val, i16* %addr) {
80  %extval = zext i16 %val to i32
81  %res = call i32 @llvm.arm.strex.p0i16(i32 %extval, i16* %addr)
82  ret i32 %res
83}
84
85; CHECK-LABEL: test_store_i32:
86; CHECK: strex r0, r1, [r2]
87define i32 @test_store_i32(i32, i32 %val, i32* %addr) {
88  %res = call i32 @llvm.arm.strex.p0i32(i32 %val, i32* %addr)
89  ret i32 %res
90}
91
92declare i32 @llvm.arm.strex.p0i8(i32, i8*) nounwind
93declare i32 @llvm.arm.strex.p0i16(i32, i16*) nounwind
94declare i32 @llvm.arm.strex.p0i32(i32, i32*) nounwind
95
96; CHECK-LABEL: test_clear:
97; CHECK: clrex
98define void @test_clear() {
99  call void @llvm.arm.clrex()
100  ret void
101}
102
103declare void @llvm.arm.clrex() nounwind
104
105@base = global i32* null
106
107define void @excl_addrmode() {
108; CHECK-T2ADDRMODE-LABEL: excl_addrmode:
109  %base1020 = load i32*, i32** @base
110  %offset1020 = getelementptr i32, i32* %base1020, i32 255
111  call i32 @llvm.arm.ldrex.p0i32(i32* %offset1020)
112  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %offset1020)
113; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [{{r[0-9]+}}, #1020]
114; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [{{r[0-9]+}}, #1020]
115
116  %base1024 = load i32*, i32** @base
117  %offset1024 = getelementptr i32, i32* %base1024, i32 256
118  call i32 @llvm.arm.ldrex.p0i32(i32* %offset1024)
119  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %offset1024)
120; CHECK-T2ADDRMODE: add.w r[[ADDR:[0-9]+]], {{r[0-9]+}}, #1024
121; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
122; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
123
124  %base1 = load i32*, i32** @base
125  %addr8 = bitcast i32* %base1 to i8*
126  %offset1_8 = getelementptr i8, i8* %addr8, i32 1
127  %offset1 = bitcast i8* %offset1_8 to i32*
128  call i32 @llvm.arm.ldrex.p0i32(i32* %offset1)
129  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %offset1)
130; CHECK-T2ADDRMODE: adds r[[ADDR:[0-9]+]], #1
131; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
132; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
133
134  %local = alloca i8, i32 1024
135  %local32 = bitcast i8* %local to i32*
136  call i32 @llvm.arm.ldrex.p0i32(i32* %local32)
137  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32)
138; CHECK-T2ADDRMODE: mov r[[ADDR:[0-9]+]], sp
139; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
140; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
141
142  ret void
143}
144
145define void @test_excl_addrmode_folded() {
146; CHECK-LABEL: test_excl_addrmode_folded:
147  %local = alloca i8, i32 4096
148
149  %local.0 = getelementptr i8, i8* %local, i32 4
150  %local32.0 = bitcast i8* %local.0 to i32*
151  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.0)
152  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.0)
153; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [sp, #4]
154; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [sp, #4]
155
156  %local.1 = getelementptr i8, i8* %local, i32 1020
157  %local32.1 = bitcast i8* %local.1 to i32*
158  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.1)
159  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.1)
160; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [sp, #1020]
161; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [sp, #1020]
162
163  ret void
164}
165
166define void @test_excl_addrmode_range() {
167; CHECK-LABEL: test_excl_addrmode_range:
168  %local = alloca i8, i32 4096
169
170  %local.0 = getelementptr i8, i8* %local, i32 1024
171  %local32.0 = bitcast i8* %local.0 to i32*
172  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.0)
173  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.0)
174; CHECK-T2ADDRMODE: mov r[[TMP:[0-9]+]], sp
175; CHECK-T2ADDRMODE: add.w r[[ADDR:[0-9]+]], r[[TMP]], #1024
176; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
177; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
178
179  ret void
180}
181
182define void @test_excl_addrmode_align() {
183; CHECK-LABEL: test_excl_addrmode_align:
184  %local = alloca i8, i32 4096
185
186  %local.0 = getelementptr i8, i8* %local, i32 2
187  %local32.0 = bitcast i8* %local.0 to i32*
188  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.0)
189  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.0)
190; CHECK-T2ADDRMODE: mov r[[ADDR:[0-9]+]], sp
191; CHECK-T2ADDRMODE: adds r[[ADDR:[0-9]+]], #2
192; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
193; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
194
195  ret void
196}
197
198define void @test_excl_addrmode_sign() {
199; CHECK-LABEL: test_excl_addrmode_sign:
200  %local = alloca i8, i32 4096
201
202  %local.0 = getelementptr i8, i8* %local, i32 -4
203  %local32.0 = bitcast i8* %local.0 to i32*
204  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.0)
205  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.0)
206; CHECK-T2ADDRMODE: mov r[[ADDR:[0-9]+]], sp
207; CHECK-T2ADDRMODE: subs r[[ADDR:[0-9]+]], #4
208; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [r[[ADDR]]]
209; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [r[[ADDR]]]
210
211  ret void
212}
213
214define void @test_excl_addrmode_combination() {
215; CHECK-LABEL: test_excl_addrmode_combination:
216  %local = alloca i8, i32 4096
217  %unused = alloca i8, i32 64
218
219  %local.0 = getelementptr i8, i8* %local, i32 4
220  %local32.0 = bitcast i8* %local.0 to i32*
221  call i32 @llvm.arm.ldrex.p0i32(i32* %local32.0)
222  call i32 @llvm.arm.strex.p0i32(i32 0, i32* %local32.0)
223; CHECK-T2ADDRMODE: ldrex {{r[0-9]+}}, [sp, #68]
224; CHECK-T2ADDRMODE: strex {{r[0-9]+}}, {{r[0-9]+}}, [sp, #68]
225
226  ret void
227}
228
229
230; LLVM should know, even across basic blocks, that ldrex is setting the high
231; bits of its i32 to 0. There should be no zero-extend operation.
232define zeroext i8 @test_cross_block_zext_i8(i1 %tst, i8* %addr) {
233; CHECK: test_cross_block_zext_i8:
234; CHECK-NOT: uxtb
235; CHECK-NOT: and
236; CHECK: bx lr
237  %val = call i32 @llvm.arm.ldrex.p0i8(i8* %addr)
238  br i1 %tst, label %end, label %mid
239mid:
240  ret i8 42
241end:
242  %val8 = trunc i32 %val to i8
243  ret i8 %val8
244}
245