1 ; RUN: opt < %s -loop-deletion -verify-dom-info -S | FileCheck %s
3 ; Checking that we can delete loops that are never executed.
4 ; We do not change the constant conditional branch statement (where the not-taken target
5 ; is the loop) to an unconditional one.
7 ; delete the infinite loop because it is never executed.
8 define void @test1(i64 %n, i64 %m) nounwind {
11 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
14 br i1 true, label %return, label %bb
17 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb ]
19 %t1 = icmp slt i64 %x.0, %n
20 %t3 = icmp sgt i64 %x.0, %m
22 br i1 true, label %bb, label %return
28 ; FIXME: We can delete this infinite loop. Currently we do not,
29 ; because the infinite loop has no exit block.
30 define void @test2(i64 %n, i64 %m) nounwind {
33 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
37 br i1 true, label %return, label %bb
40 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb ]
42 %t1 = icmp slt i64 %x.0, %n
43 %t3 = icmp sgt i64 %x.0, %m
51 ; There are multiple exiting blocks and a single exit block.
52 ; Since it is a never executed loop, we do not care about the values
53 ; from different exiting paths and we can
55 define i64 @test3(i64 %n, i64 %m, i64 %maybe_zero) nounwind {
60 ; CHECK-LABEL: return.loopexit:
61 ; CHECK-NEXT: %x.lcssa.ph = phi i64 [ undef, %bb.preheader ]
62 ; CHECK-NEXT: br label %return
63 ; CHECK-LABEL: return:
64 ; CHECK-NEXT: %x.lcssa = phi i64 [ 20, %entry ], [ %x.lcssa.ph, %return.loopexit ]
65 ; CHECK-NEXT: ret i64 %x.lcssa
67 br i1 false, label %bb, label %return
70 %x.0 = phi i64 [ 0, %entry ], [ %t0, %bb3 ]
72 %t1 = icmp slt i64 %x.0, %n
73 br i1 %t1, label %bb2, label %return
76 %t2 = icmp slt i64 %x.0, %m
77 %unused1 = udiv i64 42, %maybe_zero
78 br i1 %t2, label %bb3, label %return
81 %t3 = icmp slt i64 %x.0, %m
82 %unused2 = sdiv i64 42, %maybe_zero
83 br i1 %t3, label %bb, label %return
86 ; the only valid value fo x.lcssa is 20.
87 %x.lcssa = phi i64 [ 12, %bb ], [ 14, %bb2 ], [ 16, %bb3 ], [20, %entry ]
91 ; Cannot delete the loop, since it may be executed at runtime.
92 define void @test4(i64 %n, i64 %m, i1 %cond) {
96 br i1 %cond, label %looppred1, label %looppred2
99 br i1 true, label %return, label %bb
102 br i1 false, label %return, label %bb
105 %x.0 = phi i64 [ 0, %looppred1 ], [ 1, %looppred2 ], [ %t0, %bb ]
106 %t0 = add i64 %x.0, 1
107 %t1 = icmp slt i64 %x.0, %n
108 %t3 = icmp sgt i64 %x.0, %m
109 %t4 = and i1 %t1, %t3
110 br i1 true, label %bb, label %return
116 ; multiple constant conditional branches with loop not-taken in all cases.
117 define void @test5(i64 %n, i64 %m, i1 %cond) nounwind {
119 ; CHECK-LABEL: looppred1:
120 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
121 ; CHECK-LABEL: looppred2:
122 ; CHECK-NEXT: br i1 true, label %return, label %bb.preheader
125 br i1 %cond, label %looppred1, label %looppred2
128 br i1 true, label %return, label %bb
131 br i1 true, label %return, label %bb
134 %x.0 = phi i64 [ 0, %looppred1 ], [ 1, %looppred2 ], [ %t0, %bb ]
135 %t0 = add i64 %x.0, 1
136 %t1 = icmp slt i64 %x.0, %n
137 %t3 = icmp sgt i64 %x.0, %m
138 %t4 = and i1 %t1, %t3
139 br i1 true, label %bb, label %return
145 ; Don't delete this infinite loop because the loop
146 ; is executable at runtime.
147 define void @test6(i64 %n, i64 %m) nounwind {
149 ; CHECK-LABEL: entry:
150 ; CHECK-NEXT: br i1 true, label %bb.preheader, label %bb.preheader
153 br i1 true, label %bb, label %bb
156 %x.0 = phi i64 [ 0, %entry ], [ 0, %entry ], [ %t0, %bb ]
157 %t0 = add i64 %x.0, 1
158 %t1 = icmp slt i64 %x.0, %n
159 %t3 = icmp sgt i64 %x.0, %m
160 %t4 = and i1 %t1, %t3
161 br i1 true, label %bb, label %return
167 declare i64 @foo(i64)
168 ; The loop L2 is never executed and is a subloop, with an
169 ; exit block that branches back to parent loop.
170 ; Here we can delete loop L2, while L1 still exists.
171 define i64 @test7(i64 %n) {
174 ; CHECK: br i1 true, label %L1Latch, label %L2.preheader
175 ; CHECK-LABEL: L2.preheader:
176 ; CHECK-NEXT: br label %L1Latch.loopexit
177 ; CHECK-LABEL: L1Latch.loopexit:
178 ; CHECK: br label %L1Latch
179 ; CHECK-LABEL: L1Latch:
180 ; CHECK-NEXT: %y = phi i64 [ %y.next, %L1 ], [ %y.L2.lcssa, %L1Latch.loopexit ]
181 ; CHECK: br i1 %cond2, label %exit, label %L1
186 %y.next = phi i64 [ 0, %entry ], [ %y.add, %L1Latch ]
187 br i1 true, label %L1Latch, label %L2
190 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
191 %x.next = add i64 %x, 1
192 %y.L2 = call i64 @foo(i64 %x.next)
193 %cond = icmp slt i64 %x.next, %n
194 br i1 %cond, label %L2, label %L1Latch
197 %y = phi i64 [ %y.next, %L1 ], [ %y.L2, %L2 ]
198 %y.add = add i64 %y, %n
199 %cond2 = icmp eq i64 %y.add, 42
200 br i1 %cond2, label %exit, label %L1
207 ; Show recursive deletion of loops. Since we start with subloops and progress outward
208 ; to parent loop, we first delete the loop L2. Now loop L1 becomes a non-loop since it's backedge
209 ; from L2's preheader to L1's exit block is never taken. So, L1 gets deleted as well.
210 define void @test8(i64 %n) {
212 ; CHECK-LABEL: entry:
213 ; CHECK-NEXT: br label %exit
215 ; CHECK-NEXT: ret void
220 br i1 true, label %exit, label %L2
223 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
224 %x.next = add i64 %x, 1
225 %y.L2 = call i64 @foo(i64 %x.next)
226 %cond = icmp slt i64 %x.next, %n
227 br i1 %cond, label %L2, label %L1
234 ; Delete a loop (L2) which has subloop (L3).
235 ; Here we delete loop L2, but leave L3 as is.
236 ; FIXME: Can delete L3 as well, by iteratively going backward through the single
237 ; predecessor of L3 until we reach L1's block that guarantees L3 is never
239 define void @test9(i64 %n) {
241 ; CHECK-LABEL: L2.preheader:
242 ; CHECK-NEXT: br label %L3.preheader
244 ; CHECK-LABEL: L3.preheader:
245 ; CHECK-NEXT: %y.L2.lcssa = phi i64 [ undef, %L2.preheader ]
246 ; CHECK-NEXT: br label %L3
248 ; CHECK: br i1 %cond2, label %L3, label %L1.loopexit
253 br i1 true, label %exit, label %L2
256 %x = phi i64 [ 0, %L1 ], [ %x.next, %L2 ]
257 %x.next = add i64 %x, 1
258 %y.L2 = call i64 @foo(i64 %x.next)
259 %cond = icmp slt i64 %x.next, %n
260 br i1 %cond, label %L2, label %L3
263 %cond2 = icmp slt i64 %y.L2, %n
264 br i1 %cond2, label %L3, label %L1
270 ; We cannot delete L3 because of call within it.
271 ; Since L3 is not deleted, and entirely contained within L2, L2 is also not
273 ; FIXME: We can delete unexecutable loops having
274 ; subloops contained entirely within them.
275 define void @test10(i64 %n) {
276 ; CHECK-LABEL: test10
283 br i1 true, label %exit, label %L2
286 %x = phi i64 [ 0, %L1 ], [ %x.next, %L3 ]
287 %x.next = add i64 %x, 1
288 %y.L2 = call i64 @foo(i64 %x.next)
289 %cond = icmp slt i64 %x.next, %n
290 br i1 %cond, label %L1, label %L3
293 %y.L3 = phi i64 [ %y.L2, %L2 ], [ %y.L3.next, %L3 ]
294 %y.L3.next = add i64 %y.L3, 1
295 %dummy = call i64 @foo(i64 %y.L3.next)
296 %cond2 = icmp slt i64 %y.L3, %n
297 br i1 %cond2, label %L3, label %L2
303 ; same as test10, but L3 does not contain call.
304 ; So, in the first iteration, all statements of L3 are made invariant, and L3 is
306 ; In the next iteration, since L2 is never executed and has no subloops, we delete
307 ; L2 as well. Finally, the outermost loop L1 is deleted.
308 define void @test11(i64 %n) {
309 ; CHECK-LABEL: test11
310 ; CHECK-LABEL: entry:
311 ; CHECK-NEXT: br label %exit
313 ; CHECK-NEXT: ret void
318 br i1 true, label %exit, label %L2
321 %x = phi i64 [ 0, %L1 ], [ %x.next, %L3 ]
322 %x.next = add i64 %x, 1
323 %y.L2 = call i64 @foo(i64 %x.next)
324 %cond = icmp slt i64 %x.next, %n
325 br i1 %cond, label %L1, label %L3
328 %y.L3 = phi i64 [ %y.L2, %L2 ], [ %y.L3.next, %L3 ]
329 %y.L3.next = add i64 %y.L3, 1
330 %cond2 = icmp slt i64 %y.L3, %n
331 br i1 %cond2, label %L3, label %L2
338 ; 2 edges from a single exiting block to the exit block.
339 define i64 @test12(i64 %n){
340 ;CHECK-LABEL: @test12
342 ; CHECK-NOT: L1Latch:
343 ; CHECK-LABEL: L1.preheader:
344 ; CHECK-NEXT: br label %exit
346 ; CHECK-NEXT: %y.phi = phi i64 [ undef, %L1.preheader ]
347 ; CHECK-NEXT: ret i64 %y.phi
350 br i1 true, label %exit1, label %L1
355 L1: ; preds = %L1Latch, %entry
356 %y.next = phi i64 [ 0, %entry ], [ %y.add, %L1Latch ]
357 br i1 true, label %L1Latch, label %exit
359 L1Latch: ; preds = %L1
360 %y = phi i64 [ %y.next, %L1 ]
361 %y.add = add i64 %y, %n
362 %cond2 = icmp eq i64 %y.add, 42
363 switch i64 %n, label %L1 [
368 exit: ; preds = %L1Latch, %L1Latch
369 %y.phi = phi i64 [ 10, %L1Latch ], [ 10, %L1Latch ], [ %y.next, %L1]
373 ; multiple edges to exit block from the same exiting blocks
374 define i64 @test13(i64 %n) {
375 ; CHECK-LABEL: @test13
377 ; CHECK-NOT: L1Latch:
378 ; CHECK-LABEL: L1.preheader:
379 ; CHECK-NEXT: br label %exit
381 ; CHECK-NEXT: %y.phi = phi i64 [ undef, %L1.preheader ]
382 ; CHECK-NEXT: ret i64 %y.phi
385 br i1 true, label %exit1, label %L1
390 L1: ; preds = %L1Latch, %entry
391 %y.next = phi i64 [ 0, %entry ], [ %y.add, %L1Latch ]
392 br i1 true, label %L1Block, label %exit
394 L1Block: ; preds = %L1
395 %y = phi i64 [ %y.next, %L1 ]
396 %y.add = add i64 %y, %n
397 %cond2 = icmp eq i64 %y.add, 42
398 switch i64 %n, label %L1Latch [
404 switch i64 %n, label %L1 [
409 exit: ; preds = %L1Block, %L1, %L1Latch
410 %y.phi = phi i64 [ 10, %L1Block ], [ 10, %L1Block ], [ %y.next, %L1 ], [ 30, %L1Latch ], [ 30, %L1Latch ]