1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=arm-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=ARM %s
3 ; RUN: llc -mtriple=thumb-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=THUMB %s
4 ; RUN: llc -mtriple=thumb-eabi -arm-atomic-cfg-tidy=0 -mcpu=arm1156t2-s -mattr=+thumb2 %s -o - | FileCheck -check-prefix=T2 %s
5 ; RUN: llc -mtriple=thumbv8-eabi -arm-atomic-cfg-tidy=0 %s -o - | FileCheck -check-prefix=V8 %s
7 ; FIXME: The -mtriple=thumb test doesn't change if -disable-peephole is specified.
9 %struct.Foo = type { ptr }
14 define ptr @foo(ptr %this, i32 %acc) nounwind readonly align 2 {
15 ; ARM: @ %bb.0: @ %entry
16 ; ARM-NEXT: add r2, r0, #4
17 ; ARM-NEXT: mov r12, #1
19 ; ARM-NEXT: .LBB0_1: @ %tailrecurse.switch
20 ; ARM-NEXT: @ in Loop: Header=BB0_3 Depth=1
21 ; ARM-NEXT: cmp r3, #1
22 ; ARM-NEXT: movne pc, lr
23 ; ARM-NEXT: .LBB0_2: @ %sw.bb
24 ; ARM-NEXT: @ in Loop: Header=BB0_3 Depth=1
25 ; ARM-NEXT: orr r1, r3, r1, lsl #1
26 ; ARM-NEXT: add r2, r2, #4
27 ; ARM-NEXT: add r12, r12, #1
28 ; ARM-NEXT: .LBB0_3: @ %tailrecurse
29 ; ARM-NEXT: @ =>This Inner Loop Header: Depth=1
30 ; ARM-NEXT: ldr r3, [r2, #-4]
31 ; ARM-NEXT: ands r3, r3, #3
32 ; ARM-NEXT: beq .LBB0_2
33 ; ARM-NEXT: @ %bb.4: @ %tailrecurse.switch
34 ; ARM-NEXT: @ in Loop: Header=BB0_3 Depth=1
35 ; ARM-NEXT: cmp r3, #3
36 ; ARM-NEXT: moveq r0, r2
37 ; ARM-NEXT: moveq pc, lr
38 ; ARM-NEXT: .LBB0_5: @ %tailrecurse.switch
39 ; ARM-NEXT: @ in Loop: Header=BB0_3 Depth=1
40 ; ARM-NEXT: cmp r3, #2
41 ; ARM-NEXT: bne .LBB0_1
42 ; ARM-NEXT: @ %bb.6: @ %sw.bb8
43 ; ARM-NEXT: add r1, r1, r12
44 ; ARM-NEXT: add r0, r0, r1, lsl #2
45 ; ARM-NEXT: mov pc, lr
47 ; THUMB: @ %bb.0: @ %entry
48 ; THUMB-NEXT: .save {r4, r5, r7, lr}
49 ; THUMB-NEXT: push {r4, r5, r7, lr}
50 ; THUMB-NEXT: movs r2, #1
51 ; THUMB-NEXT: movs r3, r0
52 ; THUMB-NEXT: .LBB0_1: @ %tailrecurse
53 ; THUMB-NEXT: @ =>This Inner Loop Header: Depth=1
54 ; THUMB-NEXT: ldr r5, [r3]
55 ; THUMB-NEXT: movs r4, #3
56 ; THUMB-NEXT: ands r4, r5
57 ; THUMB-NEXT: beq .LBB0_5
58 ; THUMB-NEXT: @ %bb.2: @ %tailrecurse.switch
59 ; THUMB-NEXT: @ in Loop: Header=BB0_1 Depth=1
60 ; THUMB-NEXT: cmp r4, #3
61 ; THUMB-NEXT: beq .LBB0_6
62 ; THUMB-NEXT: @ %bb.3: @ %tailrecurse.switch
63 ; THUMB-NEXT: @ in Loop: Header=BB0_1 Depth=1
64 ; THUMB-NEXT: cmp r4, #2
65 ; THUMB-NEXT: beq .LBB0_7
66 ; THUMB-NEXT: @ %bb.4: @ %tailrecurse.switch
67 ; THUMB-NEXT: @ in Loop: Header=BB0_1 Depth=1
68 ; THUMB-NEXT: cmp r4, #1
69 ; THUMB-NEXT: bne .LBB0_9
70 ; THUMB-NEXT: .LBB0_5: @ %sw.bb
71 ; THUMB-NEXT: @ in Loop: Header=BB0_1 Depth=1
72 ; THUMB-NEXT: lsls r1, r1, #1
73 ; THUMB-NEXT: orrs r4, r1
74 ; THUMB-NEXT: adds r3, r3, #4
75 ; THUMB-NEXT: adds r2, r2, #1
76 ; THUMB-NEXT: movs r1, r4
77 ; THUMB-NEXT: b .LBB0_1
78 ; THUMB-NEXT: .LBB0_6: @ %sw.bb6
79 ; THUMB-NEXT: adds r0, r3, #4
80 ; THUMB-NEXT: b .LBB0_8
81 ; THUMB-NEXT: .LBB0_7: @ %sw.bb8
82 ; THUMB-NEXT: adds r1, r1, r2
83 ; THUMB-NEXT: lsls r1, r1, #2
84 ; THUMB-NEXT: adds r0, r0, r1
85 ; THUMB-NEXT: .LBB0_8: @ %sw.bb6
86 ; THUMB-NEXT: pop {r4, r5, r7}
87 ; THUMB-NEXT: pop {r1}
89 ; THUMB-NEXT: .LBB0_9: @ %sw.epilog
90 ; THUMB-NEXT: pop {r4, r5, r7}
91 ; THUMB-NEXT: pop {r0}
94 ; T2: @ %bb.0: @ %entry
95 ; T2-NEXT: adds r2, r0, #4
96 ; T2-NEXT: mov.w r12, #1
98 ; T2-NEXT: .LBB0_1: @ %tailrecurse.switch
99 ; T2-NEXT: @ in Loop: Header=BB0_3 Depth=1
100 ; T2-NEXT: cmp r3, #1
103 ; T2-NEXT: .LBB0_2: @ %sw.bb
104 ; T2-NEXT: @ in Loop: Header=BB0_3 Depth=1
105 ; T2-NEXT: orr.w r1, r3, r1, lsl #1
106 ; T2-NEXT: adds r2, #4
107 ; T2-NEXT: add.w r12, r12, #1
108 ; T2-NEXT: .LBB0_3: @ %tailrecurse
109 ; T2-NEXT: @ =>This Inner Loop Header: Depth=1
110 ; T2-NEXT: ldr r3, [r2, #-4]
111 ; T2-NEXT: ands r3, r3, #3
112 ; T2-NEXT: beq .LBB0_2
113 ; T2-NEXT: @ %bb.4: @ %tailrecurse.switch
114 ; T2-NEXT: @ in Loop: Header=BB0_3 Depth=1
115 ; T2-NEXT: cmp r3, #3
117 ; T2-NEXT: moveq r0, r2
119 ; T2-NEXT: .LBB0_5: @ %tailrecurse.switch
120 ; T2-NEXT: @ in Loop: Header=BB0_3 Depth=1
121 ; T2-NEXT: cmp r3, #2
122 ; T2-NEXT: bne .LBB0_1
123 ; T2-NEXT: @ %bb.6: @ %sw.bb8
124 ; T2-NEXT: add r1, r12
125 ; T2-NEXT: add.w r0, r0, r1, lsl #2
128 ; V8: @ %bb.0: @ %entry
129 ; V8-NEXT: adds r2, r0, #4
130 ; V8-NEXT: mov.w r12, #1
132 ; V8-NEXT: .LBB0_1: @ %tailrecurse.switch
133 ; V8-NEXT: @ in Loop: Header=BB0_3 Depth=1
134 ; V8-NEXT: cmp r3, #1
137 ; V8-NEXT: .LBB0_2: @ %sw.bb
138 ; V8-NEXT: @ in Loop: Header=BB0_3 Depth=1
139 ; V8-NEXT: orr.w r1, r3, r1, lsl #1
140 ; V8-NEXT: adds r2, #4
141 ; V8-NEXT: add.w r12, r12, #1
142 ; V8-NEXT: .LBB0_3: @ %tailrecurse
143 ; V8-NEXT: @ =>This Inner Loop Header: Depth=1
144 ; V8-NEXT: ldr r3, [r2, #-4]
145 ; V8-NEXT: ands r3, r3, #3
146 ; V8-NEXT: beq .LBB0_2
147 ; V8-NEXT: @ %bb.4: @ %tailrecurse.switch
148 ; V8-NEXT: @ in Loop: Header=BB0_3 Depth=1
149 ; V8-NEXT: cmp r3, #3
151 ; V8-NEXT: moveq r0, r2
153 ; V8-NEXT: .LBB0_5: @ %tailrecurse.switch
154 ; V8-NEXT: @ in Loop: Header=BB0_3 Depth=1
155 ; V8-NEXT: cmp r3, #2
156 ; V8-NEXT: bne .LBB0_1
157 ; V8-NEXT: @ %bb.6: @ %sw.bb8
158 ; V8-NEXT: add r1, r12
159 ; V8-NEXT: add.w r0, r0, r1, lsl #2
162 %scevgep = getelementptr %struct.Foo, ptr %this, i32 1
163 br label %tailrecurse
165 tailrecurse: ; preds = %sw.bb, %entry
166 %lsr.iv2 = phi ptr [ %scevgep3, %sw.bb ], [ %scevgep, %entry ]
167 %lsr.iv = phi i32 [ %lsr.iv.next, %sw.bb ], [ 1, %entry ]
168 %acc.tr = phi i32 [ %or, %sw.bb ], [ %acc, %entry ]
169 %scevgep5 = getelementptr ptr, ptr %lsr.iv2, i32 -1
170 %tmp2 = load ptr, ptr %scevgep5
171 %0 = ptrtoint ptr %tmp2 to i32
177 %tst = icmp eq i32 %and, 0
178 br i1 %tst, label %sw.bb, label %tailrecurse.switch
180 tailrecurse.switch: ; preds = %tailrecurse
182 switch i32 %and, label %sw.epilog [
188 sw.bb: ; preds = %tailrecurse.switch, %tailrecurse
189 %shl = shl i32 %acc.tr, 1
190 %or = or i32 %and, %shl
191 %lsr.iv.next = add i32 %lsr.iv, 1
192 %scevgep3 = getelementptr %struct.Foo, ptr %lsr.iv2, i32 1
193 br label %tailrecurse
195 sw.bb6: ; preds = %tailrecurse.switch
198 sw.bb8: ; preds = %tailrecurse.switch
199 %tmp1 = add i32 %acc.tr, %lsr.iv
200 %add.ptr11 = getelementptr inbounds %struct.Foo, ptr %this, i32 %tmp1
203 sw.epilog: ; preds = %tailrecurse.switch
207 ; Another test that exercises the AND/TST peephole optimization and also
208 ; generates a predicated ANDS instruction. Check that the predicate is printed
209 ; after the "S" modifier on the instruction.
211 %struct.S = type { ptr, [1 x i8] }
217 define internal zeroext i8 @bar(ptr %x, ptr nocapture %y) nounwind readonly {
218 ; ARM: @ %bb.0: @ %entry
219 ; ARM-NEXT: ldrb r2, [r0, #4]
220 ; ARM-NEXT: ands r2, r2, #112
221 ; ARM-NEXT: ldrbne r1, [r1, #4]
222 ; ARM-NEXT: andsne r1, r1, #112
223 ; ARM-NEXT: beq .LBB1_2
224 ; ARM-NEXT: @ %bb.1: @ %bb2
225 ; ARM-NEXT: cmp r2, #16
226 ; ARM-NEXT: cmpne r1, #16
227 ; ARM-NEXT: andeq r0, r0, #255
228 ; ARM-NEXT: moveq pc, lr
229 ; ARM-NEXT: .LBB1_2: @ %return
230 ; ARM-NEXT: mov r0, #1
231 ; ARM-NEXT: mov pc, lr
233 ; THUMB: @ %bb.0: @ %entry
234 ; THUMB-NEXT: ldrb r2, [r0, #4]
235 ; THUMB-NEXT: movs r3, #112
236 ; THUMB-NEXT: ands r2, r3
237 ; THUMB-NEXT: beq .LBB1_4
238 ; THUMB-NEXT: @ %bb.1: @ %bb
239 ; THUMB-NEXT: ldrb r1, [r1, #4]
240 ; THUMB-NEXT: ands r1, r3
241 ; THUMB-NEXT: beq .LBB1_4
242 ; THUMB-NEXT: @ %bb.2: @ %bb2
243 ; THUMB-NEXT: cmp r2, #16
244 ; THUMB-NEXT: beq .LBB1_5
245 ; THUMB-NEXT: @ %bb.3: @ %bb2
246 ; THUMB-NEXT: cmp r1, #16
247 ; THUMB-NEXT: beq .LBB1_5
248 ; THUMB-NEXT: .LBB1_4: @ %return
249 ; THUMB-NEXT: movs r0, #1
251 ; THUMB-NEXT: .LBB1_5: @ %bb4
252 ; THUMB-NEXT: movs r1, #255
253 ; THUMB-NEXT: ands r0, r1
256 ; T2: @ %bb.0: @ %entry
257 ; T2-NEXT: ldrb r2, [r0, #4]
258 ; T2-NEXT: ands r2, r2, #112
260 ; T2-NEXT: ldrbne r1, [r1, #4]
261 ; T2-NEXT: andsne r1, r1, #112
262 ; T2-NEXT: beq .LBB1_2
263 ; T2-NEXT: @ %bb.1: @ %bb2
264 ; T2-NEXT: cmp r2, #16
266 ; T2-NEXT: cmpne r1, #16
267 ; T2-NEXT: uxtbeq r0, r0
269 ; T2-NEXT: .LBB1_2: @ %return
270 ; T2-NEXT: movs r0, #1
273 ; V8: @ %bb.0: @ %entry
274 ; V8-NEXT: ldrb r2, [r0, #4]
275 ; V8-NEXT: ands r2, r2, #112
277 ; V8-NEXT: ldrbne r1, [r1, #4]
278 ; V8-NEXT: andsne r1, r1, #112
279 ; V8-NEXT: beq .LBB1_2
280 ; V8-NEXT: @ %bb.1: @ %bb2
281 ; V8-NEXT: cmp r2, #16
283 ; V8-NEXT: cmpne r1, #16
284 ; V8-NEXT: uxtbeq r0, r0
286 ; V8-NEXT: .LBB1_2: @ %return
287 ; V8-NEXT: movs r0, #1
290 %0 = getelementptr inbounds %struct.S, ptr %x, i32 0, i32 1, i32 0
291 %1 = load i8, ptr %0, align 1
292 %2 = zext i8 %1 to i32
294 %4 = icmp eq i32 %3, 0
295 br i1 %4, label %return, label %bb
298 %5 = getelementptr inbounds %struct.S, ptr %y, i32 0, i32 1, i32 0
299 %6 = load i8, ptr %5, align 1
300 %7 = zext i8 %6 to i32
302 %9 = icmp eq i32 %8, 0
303 br i1 %9, label %return, label %bb2
306 %10 = icmp eq i32 %3, 16
307 %11 = icmp eq i32 %8, 16
308 %or.cond = or i1 %10, %11
309 br i1 %or.cond, label %bb4, label %return
312 %12 = ptrtoint ptr %x to i32
313 %phitmp = trunc i32 %12 to i8
316 return: ; preds = %bb2, %bb, %entry
321 ; We were looking through multiple COPY instructions to find an AND we might
322 ; fold into a TST, but in doing so we changed the register being tested allowing
323 ; folding of unrelated tests (in this case, a TST against r1 was eliminated in
324 ; favour of an AND of r0).
326 define i32 @test_tst_assessment(i32 %a, i32 %b) {
327 ; ARM-LABEL: test_tst_assessment:
329 ; ARM-NEXT: and r0, r0, #1
330 ; ARM-NEXT: tst r1, #1
331 ; ARM-NEXT: subne r0, r0, #1
332 ; ARM-NEXT: mov pc, lr
334 ; THUMB-LABEL: test_tst_assessment:
336 ; THUMB-NEXT: movs r2, r0
337 ; THUMB-NEXT: movs r0, #1
338 ; THUMB-NEXT: ands r0, r2
339 ; THUMB-NEXT: lsls r1, r1, #31
340 ; THUMB-NEXT: beq .LBB2_2
341 ; THUMB-NEXT: @ %bb.1:
342 ; THUMB-NEXT: subs r0, r0, #1
343 ; THUMB-NEXT: .LBB2_2:
346 ; T2-LABEL: test_tst_assessment:
348 ; T2-NEXT: and r0, r0, #1
349 ; T2-NEXT: lsls r1, r1, #31
351 ; T2-NEXT: subne r0, #1
354 ; V8-LABEL: test_tst_assessment:
356 ; V8-NEXT: and r0, r0, #1
357 ; V8-NEXT: lsls r1, r1, #31
359 ; V8-NEXT: subne r0, #1
361 %and1 = and i32 %a, 1
362 %sub = sub i32 %and1, 1
363 %and2 = and i32 %b, 1
364 %cmp = icmp eq i32 %and2, 0
365 %sel = select i1 %cmp, i32 %and1, i32 %sub
369 !1 = !{!"branch_weights", i32 1, i32 1, i32 3, i32 2 }