1; RUN: opt < %s -loop-reduce -S | FileCheck %s
2
3target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
4target triple = "i686-pc-windows-msvc"
5
6declare i32 @_except_handler3(...)
7declare i32 @__CxxFrameHandler3(...)
8
9declare void @external(i32*)
10declare void @reserve()
11
12define void @f() personality i32 (...)* @_except_handler3 {
13entry:
14  br label %throw
15
16throw:                                            ; preds = %throw, %entry
17  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
18  invoke void @reserve()
19          to label %throw unwind label %pad
20
21pad:                                              ; preds = %throw
22  %phi2 = phi i8* [ %tmp96, %throw ]
23  %cs = catchswitch within none [label %unreachable] unwind label %blah2
24
25unreachable:
26  catchpad within %cs []
27  unreachable
28
29blah2:
30  %cleanuppadi4.i.i.i = cleanuppad within none []
31  br label %loop_body
32
33loop_body:                                        ; preds = %iter, %pad
34  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blah2 ]
35  %tmp100 = icmp eq i8* %tmp99, undef
36  br i1 %tmp100, label %unwind_out, label %iter
37
38iter:                                             ; preds = %loop_body
39  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
40  br i1 undef, label %unwind_out, label %loop_body
41
42unwind_out:                                       ; preds = %iter, %loop_body
43  cleanupret from %cleanuppadi4.i.i.i unwind to caller
44}
45
46; CHECK-LABEL: define void @f(
47; CHECK: cleanuppad within none []
48; CHECK-NEXT: ptrtoint i8* %phi2 to i32
49
50define void @g() personality i32 (...)* @_except_handler3 {
51entry:
52  br label %throw
53
54throw:                                            ; preds = %throw, %entry
55  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
56  invoke void @reserve()
57          to label %throw unwind label %pad
58
59pad:
60  %phi2 = phi i8* [ %tmp96, %throw ]
61  %cs = catchswitch within none [label %unreachable, label %blah] unwind to caller
62
63unreachable:
64  catchpad within %cs []
65  unreachable
66
67blah:
68  %catchpad = catchpad within %cs []
69  br label %loop_body
70
71unwind_out:
72  catchret from %catchpad to label %leave
73
74leave:
75  ret void
76
77loop_body:                                        ; preds = %iter, %pad
78  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blah ]
79  %tmp100 = icmp eq i8* %tmp99, undef
80  br i1 %tmp100, label %unwind_out, label %iter
81
82iter:                                             ; preds = %loop_body
83  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
84  br i1 undef, label %unwind_out, label %loop_body
85}
86
87; CHECK-LABEL: define void @g(
88; CHECK: blah:
89; CHECK-NEXT: catchpad within %cs []
90; CHECK-NEXT: ptrtoint i8* %phi2 to i32
91
92
93define void @h() personality i32 (...)* @_except_handler3 {
94entry:
95  br label %throw
96
97throw:                                            ; preds = %throw, %entry
98  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
99  invoke void @reserve()
100          to label %throw unwind label %pad
101
102pad:
103  %cs = catchswitch within none [label %unreachable, label %blug] unwind to caller
104
105unreachable:
106  catchpad within %cs []
107  unreachable
108
109blug:
110  %phi2 = phi i8* [ %tmp96, %pad ]
111  %catchpad = catchpad within %cs []
112  br label %loop_body
113
114unwind_out:
115  catchret from %catchpad to label %leave
116
117leave:
118  ret void
119
120loop_body:                                        ; preds = %iter, %pad
121  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %blug ]
122  %tmp100 = icmp eq i8* %tmp99, undef
123  br i1 %tmp100, label %unwind_out, label %iter
124
125iter:                                             ; preds = %loop_body
126  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
127  br i1 undef, label %unwind_out, label %loop_body
128}
129
130; CHECK-LABEL: define void @h(
131; CHECK: blug:
132; CHECK: catchpad within %cs []
133; CHECK-NEXT: ptrtoint i8* %phi2 to i32
134
135define void @i() personality i32 (...)* @_except_handler3 {
136entry:
137  br label %throw
138
139throw:                                            ; preds = %throw, %entry
140  %tmp96 = getelementptr inbounds i8, i8* undef, i32 1
141  invoke void @reserve()
142          to label %throw unwind label %catchpad
143
144catchpad:                                              ; preds = %throw
145  %phi2 = phi i8* [ %tmp96, %throw ]
146  %cs = catchswitch within none [label %cp_body] unwind label %cleanuppad
147
148cp_body:
149  catchpad within %cs []
150  br label %loop_head
151
152cleanuppad:
153  cleanuppad within none []
154  br label %loop_head
155
156loop_head:
157  br label %loop_body
158
159loop_body:                                        ; preds = %iter, %catchpad
160  %tmp99 = phi i8* [ %tmp101, %iter ], [ %phi2, %loop_head ]
161  %tmp100 = icmp eq i8* %tmp99, undef
162  br i1 %tmp100, label %unwind_out, label %iter
163
164iter:                                             ; preds = %loop_body
165  %tmp101 = getelementptr inbounds i8, i8* %tmp99, i32 1
166  br i1 undef, label %unwind_out, label %loop_body
167
168unwind_out:                                       ; preds = %iter, %loop_body
169  unreachable
170}
171
172; CHECK-LABEL: define void @i(
173; CHECK: ptrtoint i8* %phi2 to i32
174
175define void @test1(i32* %b, i32* %c) personality i32 (...)* @__CxxFrameHandler3 {
176entry:
177  br label %for.cond
178
179for.cond:                                         ; preds = %for.inc, %entry
180  %d.0 = phi i32* [ %b, %entry ], [ %incdec.ptr, %for.inc ]
181  invoke void @external(i32* %d.0)
182          to label %for.inc unwind label %catch.dispatch
183
184for.inc:                                          ; preds = %for.cond
185  %incdec.ptr = getelementptr inbounds i32, i32* %d.0, i32 1
186  br label %for.cond
187
188catch.dispatch:                                   ; preds = %for.cond
189  %cs = catchswitch within none [label %catch] unwind label %catch.dispatch.2
190
191catch:                                            ; preds = %catch.dispatch
192  %0 = catchpad within %cs [i8* null, i32 64, i8* null]
193  catchret from %0 to label %try.cont
194
195try.cont:                                         ; preds = %catch
196  invoke void @external(i32* %c)
197          to label %try.cont.7 unwind label %catch.dispatch.2
198
199catch.dispatch.2:                                 ; preds = %try.cont, %catchendblock
200  %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catch.dispatch ]
201  %cs2 = catchswitch within none [label %catch.4] unwind to caller
202
203catch.4:                                          ; preds = %catch.dispatch.2
204  catchpad within %cs2 [i8* null, i32 64, i8* null]
205  unreachable
206
207try.cont.7:                                       ; preds = %try.cont
208  ret void
209}
210
211; CHECK-LABEL: define void @test1(
212; CHECK: for.cond:
213; CHECK:   %d.0 = phi i32* [ %b, %entry ], [ %incdec.ptr, %for.inc ]
214
215; CHECK: catch.dispatch.2:
216; CHECK: %e.0 = phi i32* [ %c, %try.cont ], [ %b, %catch.dispatch ]
217
218define i32 @test2() personality i32 (...)* @_except_handler3 {
219entry:
220  br label %for.body
221
222for.body:                                         ; preds = %for.inc, %entry
223  %phi = phi i32 [ %inc, %for.inc ], [ 0, %entry ]
224  invoke void @reserve()
225          to label %for.inc unwind label %catch.dispatch
226
227catch.dispatch:                                   ; preds = %for.body
228  %tmp18 = catchswitch within none [label %catch.handler] unwind to caller
229
230catch.handler:                                    ; preds = %catch.dispatch
231  %phi.lcssa = phi i32 [ %phi, %catch.dispatch ]
232  %tmp19 = catchpad within %tmp18 [i8* null]
233  catchret from %tmp19 to label %done
234
235done:
236  ret i32 %phi.lcssa
237
238for.inc:                                          ; preds = %for.body
239  %inc = add i32 %phi, 1
240  br label %for.body
241}
242
243; CHECK-LABEL: define i32 @test2(
244; CHECK:      %phi.lcssa = phi i32 [ %phi, %catch.dispatch ]
245; CHECK-NEXT: catchpad within
246