1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -loop-reduce -S | FileCheck %s
3
4target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
5target triple = "i686-pc-windows-msvc"
6
7declare i32 @_except_handler3(...)
8declare i32 @__CxxFrameHandler3(...)
9
10declare void @external(i32*)
11declare void @reserve()
12
13define void @f() personality i32 (...)* @_except_handler3 {
14; CHECK-LABEL: @f(
15; CHECK-NEXT:  entry:
16; CHECK-NEXT:    br label [[THROW:%.*]]
17; CHECK:       throw:
18; CHECK-NEXT:    [[TMP96:%.*]] = getelementptr inbounds i8, i8* undef, i32 1
19; CHECK-NEXT:    invoke void @reserve()
20; CHECK-NEXT:    to label [[THROW]] unwind label [[PAD:%.*]]
21; CHECK:       pad:
22; CHECK-NEXT:    [[PHI2:%.*]] = phi i8* [ [[TMP96]], [[THROW]] ]
23; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label %unreachable] unwind label [[BLAH2:%.*]]
24; CHECK:       unreachable:
25; CHECK-NEXT:    [[TMP0:%.*]] = catchpad within [[CS]] []
26; CHECK-NEXT:    unreachable
27; CHECK:       blah2:
28; CHECK-NEXT:    [[CLEANUPPADI4_I_I_I:%.*]] = cleanuppad within none []
29; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr i8, i8* [[PHI2]], i32 -1
30; CHECK-NEXT:    br label [[LOOP_BODY:%.*]]
31; CHECK:       loop_body:
32; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i8* [ [[SCEVGEP1:%.*]], [[ITER:%.*]] ], [ [[SCEVGEP]], [[BLAH2]] ]
33; CHECK-NEXT:    [[SCEVGEP1]] = getelementptr i8, i8* [[LSR_IV]], i32 1
34; CHECK-NEXT:    [[TMP100:%.*]] = icmp eq i8* [[SCEVGEP1]], undef
35; CHECK-NEXT:    br i1 [[TMP100]], label [[UNWIND_OUT:%.*]], label [[ITER]]
36; CHECK:       iter:
37; CHECK-NEXT:    br i1 true, label [[UNWIND_OUT]], label [[LOOP_BODY]]
38; CHECK:       unwind_out:
39; CHECK-NEXT:    cleanupret from [[CLEANUPPADI4_I_I_I]] unwind to caller
40;
41entry:
42  br label %throw
43
44throw:                                            ; preds = %throw, %entry
45  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
46  invoke void @reserve()
47  to label %throw unwind label %pad
48
49pad:                                              ; preds = %throw
50  %phi2 = phi i8* [ %tmp96, %throw ]
51  %cs = catchswitch within none [label %unreachable] unwind label %blah2
52
53unreachable:
54  catchpad within %cs []
55  unreachable
56
57blah2:
58  %cleanuppadi4.i.i.i = cleanuppad within none []
59  br label %loop_body
60
61loop_body:                                        ; preds = %iter, %pad
62  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blah2 ]
63  %tmp100 = icmp eq i8* %tmp99, undef
64  br i1 %tmp100, label %unwind_out, label %iter
65
66iter:                                             ; preds = %loop_body
67  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
68  br i1 undef, label %unwind_out, label %loop_body
69
70unwind_out:                                       ; preds = %iter, %loop_body
71  cleanupret from %cleanuppadi4.i.i.i unwind to caller
72}
73
74define void @g() personality i32 (...)* @_except_handler3 {
75; CHECK-LABEL: @g(
76; CHECK-NEXT:  entry:
77; CHECK-NEXT:    br label [[THROW:%.*]]
78; CHECK:       throw:
79; CHECK-NEXT:    [[TMP96:%.*]] = getelementptr inbounds i8, i8* undef, i32 1
80; CHECK-NEXT:    invoke void @reserve()
81; CHECK-NEXT:    to label [[THROW]] unwind label [[PAD:%.*]]
82; CHECK:       pad:
83; CHECK-NEXT:    [[PHI2:%.*]] = phi i8* [ [[TMP96]], [[THROW]] ]
84; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label [[UNREACHABLE:%.*]], label %blah] unwind to caller
85; CHECK:       unreachable:
86; CHECK-NEXT:    [[TMP0:%.*]] = catchpad within [[CS]] []
87; CHECK-NEXT:    unreachable
88; CHECK:       blah:
89; CHECK-NEXT:    [[CATCHPAD:%.*]] = catchpad within [[CS]] []
90; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr i8, i8* [[PHI2]], i32 -1
91; CHECK-NEXT:    br label [[LOOP_BODY:%.*]]
92; CHECK:       unwind_out:
93; CHECK-NEXT:    catchret from [[CATCHPAD]] to label [[LEAVE:%.*]]
94; CHECK:       leave:
95; CHECK-NEXT:    ret void
96; CHECK:       loop_body:
97; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i8* [ [[SCEVGEP1:%.*]], [[ITER:%.*]] ], [ [[SCEVGEP]], [[BLAH:%.*]] ]
98; CHECK-NEXT:    [[SCEVGEP1]] = getelementptr i8, i8* [[LSR_IV]], i32 1
99; CHECK-NEXT:    [[TMP100:%.*]] = icmp eq i8* [[SCEVGEP1]], undef
100; CHECK-NEXT:    br i1 [[TMP100]], label [[UNWIND_OUT:%.*]], label [[ITER]]
101; CHECK:       iter:
102; CHECK-NEXT:    br i1 true, label [[UNWIND_OUT]], label [[LOOP_BODY]]
103;
104entry:
105  br label %throw
106
107throw:                                            ; preds = %throw, %entry
108  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
109  invoke void @reserve()
110  to label %throw unwind label %pad
111
112pad:
113  %phi2 = phi i8* [ %tmp96, %throw ]
114  %cs = catchswitch within none [label %unreachable, label %blah] unwind to caller
115
116unreachable:
117  catchpad within %cs []
118  unreachable
119
120blah:
121  %catchpad = catchpad within %cs []
122  br label %loop_body
123
124unwind_out:
125  catchret from %catchpad to label %leave
126
127leave:
128  ret void
129
130loop_body:                                        ; preds = %iter, %pad
131  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blah ]
132  %tmp100 = icmp eq i8* %tmp99, undef
133  br i1 %tmp100, label %unwind_out, label %iter
134
135iter:                                             ; preds = %loop_body
136  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
137  br i1 undef, label %unwind_out, label %loop_body
138}
139
140define void @h() personality i32 (...)* @_except_handler3 {
141; CHECK-LABEL: @h(
142; CHECK-NEXT:  entry:
143; CHECK-NEXT:    br label [[THROW:%.*]]
144; CHECK:       throw:
145; CHECK-NEXT:    [[TMP96:%.*]] = getelementptr inbounds i8, i8* undef, i32 1
146; CHECK-NEXT:    invoke void @reserve()
147; CHECK-NEXT:    to label [[THROW]] unwind label [[PAD:%.*]]
148; CHECK:       pad:
149; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label [[UNREACHABLE:%.*]], label %blug] unwind to caller
150; CHECK:       unreachable:
151; CHECK-NEXT:    [[TMP0:%.*]] = catchpad within [[CS]] []
152; CHECK-NEXT:    unreachable
153; CHECK:       blug:
154; CHECK-NEXT:    [[PHI2:%.*]] = phi i8* [ [[TMP96]], [[PAD]] ]
155; CHECK-NEXT:    [[CATCHPAD:%.*]] = catchpad within [[CS]] []
156; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr i8, i8* [[PHI2]], i32 -1
157; CHECK-NEXT:    br label [[LOOP_BODY:%.*]]
158; CHECK:       unwind_out:
159; CHECK-NEXT:    catchret from [[CATCHPAD]] to label [[LEAVE:%.*]]
160; CHECK:       leave:
161; CHECK-NEXT:    ret void
162; CHECK:       loop_body:
163; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i8* [ [[SCEVGEP1:%.*]], [[ITER:%.*]] ], [ [[SCEVGEP]], [[BLUG:%.*]] ]
164; CHECK-NEXT:    [[SCEVGEP1]] = getelementptr i8, i8* [[LSR_IV]], i32 1
165; CHECK-NEXT:    [[TMP100:%.*]] = icmp eq i8* [[SCEVGEP1]], undef
166; CHECK-NEXT:    br i1 [[TMP100]], label [[UNWIND_OUT:%.*]], label [[ITER]]
167; CHECK:       iter:
168; CHECK-NEXT:    br i1 true, label [[UNWIND_OUT]], label [[LOOP_BODY]]
169;
170entry:
171  br label %throw
172
173throw:                                            ; preds = %throw, %entry
174  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
175  invoke void @reserve()
176  to label %throw unwind label %pad
177
178pad:
179  %cs = catchswitch within none [label %unreachable, label %blug] unwind to caller
180
181unreachable:
182  catchpad within %cs []
183  unreachable
184
185blug:
186  %phi2 = phi i8* [ %tmp96, %pad ]
187  %catchpad = catchpad within %cs []
188  br label %loop_body
189
190unwind_out:
191  catchret from %catchpad to label %leave
192
193leave:
194  ret void
195
196loop_body:                                        ; preds = %iter, %pad
197  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blug ]
198  %tmp100 = icmp eq i8* %tmp99, undef
199  br i1 %tmp100, label %unwind_out, label %iter
200
201iter:                                             ; preds = %loop_body
202  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
203  br i1 undef, label %unwind_out, label %loop_body
204}
205
206define void @i() personality i32 (...)* @_except_handler3 {
207; CHECK-LABEL: @i(
208; CHECK-NEXT:  entry:
209; CHECK-NEXT:    br label [[THROW:%.*]]
210; CHECK:       throw:
211; CHECK-NEXT:    [[TMP96:%.*]] = getelementptr inbounds i8, i8* undef, i32 1
212; CHECK-NEXT:    invoke void @reserve()
213; CHECK-NEXT:    to label [[THROW]] unwind label [[CATCHPAD:%.*]]
214; CHECK:       catchpad:
215; CHECK-NEXT:    [[PHI2:%.*]] = phi i8* [ [[TMP96]], [[THROW]] ]
216; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label %cp_body] unwind label [[CLEANUPPAD:%.*]]
217; CHECK:       cp_body:
218; CHECK-NEXT:    [[TMP0:%.*]] = catchpad within [[CS]] []
219; CHECK-NEXT:    br label [[LOOP_HEAD:%.*]]
220; CHECK:       cleanuppad:
221; CHECK-NEXT:    [[TMP1:%.*]] = cleanuppad within none []
222; CHECK-NEXT:    br label [[LOOP_HEAD]]
223; CHECK:       loop_head:
224; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr i8, i8* [[PHI2]], i32 -1
225; CHECK-NEXT:    br label [[LOOP_BODY:%.*]]
226; CHECK:       loop_body:
227; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i8* [ [[SCEVGEP1:%.*]], [[ITER:%.*]] ], [ [[SCEVGEP]], [[LOOP_HEAD]] ]
228; CHECK-NEXT:    [[SCEVGEP1]] = getelementptr i8, i8* [[LSR_IV]], i32 1
229; CHECK-NEXT:    [[TMP100:%.*]] = icmp eq i8* [[SCEVGEP1]], undef
230; CHECK-NEXT:    br i1 [[TMP100]], label [[UNWIND_OUT:%.*]], label [[ITER]]
231; CHECK:       iter:
232; CHECK-NEXT:    br i1 true, label [[UNWIND_OUT]], label [[LOOP_BODY]]
233; CHECK:       unwind_out:
234; CHECK-NEXT:    unreachable
235;
236entry:
237  br label %throw
238
239throw:                                            ; preds = %throw, %entry
240  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
241  invoke void @reserve()
242  to label %throw unwind label %catchpad
243
244catchpad:                                              ; preds = %throw
245  %phi2 = phi i8* [ %tmp96, %throw ]
246  %cs = catchswitch within none [label %cp_body] unwind label %cleanuppad
247
248cp_body:
249  catchpad within %cs []
250  br label %loop_head
251
252cleanuppad:
253  cleanuppad within none []
254  br label %loop_head
255
256loop_head:
257  br label %loop_body
258
259loop_body:                                        ; preds = %iter, %catchpad
260  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %loop_head ]
261  %tmp100 = icmp eq i8* %tmp99, undef
262  br i1 %tmp100, label %unwind_out, label %iter
263
264iter:                                             ; preds = %loop_body
265  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
266  br i1 undef, label %unwind_out, label %loop_body
267
268unwind_out:                                       ; preds = %iter, %loop_body
269  unreachable
270}
271
272define void @test1(i32* %b, i32* %c) personality i32 (...)* @__CxxFrameHandler3 {
273; CHECK-LABEL: @test1(
274; CHECK-NEXT:  entry:
275; CHECK-NEXT:    br label [[FOR_COND:%.*]]
276; CHECK:       for.cond:
277; CHECK-NEXT:    [[D_0:%.*]] = phi i32* [ [[B:%.*]], [[ENTRY:%.*]] ], [ [[INCDEC_PTR:%.*]], [[FOR_INC:%.*]] ]
278; CHECK-NEXT:    invoke void @external(i32* [[D_0]])
279; CHECK-NEXT:    to label [[FOR_INC]] unwind label [[CATCH_DISPATCH:%.*]]
280; CHECK:       for.inc:
281; CHECK-NEXT:    [[INCDEC_PTR]] = getelementptr inbounds i32, i32* [[D_0]], i32 1
282; CHECK-NEXT:    br label [[FOR_COND]]
283; CHECK:       catch.dispatch:
284; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label %catch] unwind label [[CATCH_DISPATCH_2:%.*]]
285; CHECK:       catch:
286; CHECK-NEXT:    [[TMP0:%.*]] = catchpad within [[CS]] [i8* null, i32 64, i8* null]
287; CHECK-NEXT:    catchret from [[TMP0]] to label [[TRY_CONT:%.*]]
288; CHECK:       try.cont:
289; CHECK-NEXT:    invoke void @external(i32* [[C:%.*]])
290; CHECK-NEXT:    to label [[TRY_CONT_7:%.*]] unwind label [[CATCH_DISPATCH_2]]
291; CHECK:       catch.dispatch.2:
292; CHECK-NEXT:    [[E_0:%.*]] = phi i32* [ [[C]], [[TRY_CONT]] ], [ [[B]], [[CATCH_DISPATCH]] ]
293; CHECK-NEXT:    [[CS2:%.*]] = catchswitch within none [label %catch.4] unwind to caller
294; CHECK:       catch.4:
295; CHECK-NEXT:    [[TMP1:%.*]] = catchpad within [[CS2]] [i8* null, i32 64, i8* null]
296; CHECK-NEXT:    unreachable
297; CHECK:       try.cont.7:
298; CHECK-NEXT:    ret void
299;
300entry:
301  br label %for.cond
302
303for.cond:                                         ; preds = %for.inc, %entry
304  %d.0 = phi i32* [ %b, %entry ], [ %incdec.ptr, %for.inc ]
305  invoke void @external(i32* %d.0)
306  to label %for.inc unwind label %catch.dispatch
307
308for.inc:                                          ; preds = %for.cond
309  %incdec.ptr = getelementptr inbounds i32, i32* %d.0, i32 1
310  br label %for.cond
311
312catch.dispatch:                                   ; preds = %for.cond
313  %cs = catchswitch within none [label %catch] unwind label %catch.dispatch.2
314
315catch:                                            ; preds = %catch.dispatch
316  %0 = catchpad within %cs [i8* null, i32 64, i8* null]
317  catchret from %0 to label %try.cont
318
319try.cont:                                         ; preds = %catch
320  invoke void @external(i32* %c)
321  to label %try.cont.7 unwind label %catch.dispatch.2
322
323catch.dispatch.2:                                 ; preds = %try.cont, %catchendblock
324  %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catch.dispatch ]
325  %cs2 = catchswitch within none [label %catch.4] unwind to caller
326
327catch.4:                                          ; preds = %catch.dispatch.2
328  catchpad within %cs2 [i8* null, i32 64, i8* null]
329  unreachable
330
331try.cont.7:                                       ; preds = %try.cont
332  ret void
333}
334
335define i32 @test2() personality i32 (...)* @_except_handler3 {
336; CHECK-LABEL: @test2(
337; CHECK-NEXT:  entry:
338; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
339; CHECK:       for.body:
340; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ [[INC:%.*]], [[FOR_INC:%.*]] ], [ 0, [[ENTRY:%.*]] ]
341; CHECK-NEXT:    invoke void @reserve()
342; CHECK-NEXT:    to label [[FOR_INC]] unwind label [[CATCH_DISPATCH:%.*]]
343; CHECK:       catch.dispatch:
344; CHECK-NEXT:    [[TMP18:%.*]] = catchswitch within none [label %catch.handler] unwind to caller
345; CHECK:       catch.handler:
346; CHECK-NEXT:    [[PHI_LCSSA:%.*]] = phi i32 [ [[PHI]], [[CATCH_DISPATCH]] ]
347; CHECK-NEXT:    [[TMP19:%.*]] = catchpad within [[TMP18]] [i8* null]
348; CHECK-NEXT:    catchret from [[TMP19]] to label [[DONE:%.*]]
349; CHECK:       done:
350; CHECK-NEXT:    ret i32 [[PHI_LCSSA]]
351; CHECK:       for.inc:
352; CHECK-NEXT:    [[INC]] = add i32 [[PHI]], 1
353; CHECK-NEXT:    br label [[FOR_BODY]]
354;
355entry:
356  br label %for.body
357
358for.body:                                         ; preds = %for.inc, %entry
359  %phi = phi i32 [ %inc, %for.inc ], [ 0, %entry ]
360  invoke void @reserve()
361  to label %for.inc unwind label %catch.dispatch
362
363catch.dispatch:                                   ; preds = %for.body
364  %tmp18 = catchswitch within none [label %catch.handler] unwind to caller
365
366catch.handler:                                    ; preds = %catch.dispatch
367  %phi.lcssa = phi i32 [ %phi, %catch.dispatch ]
368  %tmp19 = catchpad within %tmp18 [i8* null]
369  catchret from %tmp19 to label %done
370
371done:
372  ret i32 %phi.lcssa
373
374for.inc:                                          ; preds = %for.body
375  %inc = add i32 %phi, 1
376  br label %for.body
377}
378