[WinEH] Run cleanup handlers when an exception is thrown
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-catch-unwind.ll
1 ; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3 ; This test was generated from the following source:
4 ;
5 ; void test() {
6 ;   try {
7 ;     SomeClass obj;
8 ;     may_throw();
9 ;     try {
10 ;       may_throw();
11 ;     } catch (int) {
12 ;       handle_exception();
13 ;     }
14 ;   } catch (int) {
15 ;     handle_exception();
16 ;   }
17 ; }
18 ;
19 ; The code above was compiled with the -O2 option.
20
21 ; ModuleID = 'catch-unwind.cpp'
22 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
23 target triple = "x86_64-pc-windows-msvc"
24
25 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
26 %class.SomeClass = type { i8 }
27
28 $"\01??_R0H@8" = comdat any
29
30 @"\01??_7type_info@@6B@" = external constant i8*
31 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
32
33
34 ; CHECK-LABEL: define void @"\01?test@@YAXXZ"() #0 {
35 ; CHECK: entry:
36 ; CHECK:   [[UNWIND_HELP:\%.+]] = alloca i64
37 ; CHECK:   [[OBJ_PTR:\%.+]] = alloca %class.SomeClass
38 ; CHECK:   [[TMP0:\%.+]] = alloca i32, align 4
39 ; CHECK:   [[TMP1:\%.+]] = alloca i32, align 4
40 ; CHECK:   call void (...)* @llvm.frameescape(i32* [[TMP1]], %class.SomeClass* [[OBJ_PTR]], i32* [[TMP0]])
41 ; CHECK:   [[UNWIND_HELP_i8:\%.+]] = bitcast i64* [[UNWIND_HELP]] to i8*
42 ; CHECK:   call void @llvm.eh.unwindhelp(i8* [[UNWIND_HELP_i8]])
43 ; CHECK:   %call = invoke %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* %obj)
44 ; CHECK:           to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
45
46 ; Function Attrs: uwtable
47 define void @"\01?test@@YAXXZ"() #0 {
48 entry:
49   %obj = alloca %class.SomeClass, align 1
50   %0 = alloca i32, align 4
51   %1 = alloca i32, align 4
52   %call = invoke %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* %obj)
53           to label %invoke.cont unwind label %lpad
54
55 ; CHECK: invoke.cont:
56 ; CHECK:   invoke void @"\01?may_throw@@YAXXZ"()
57 ; CHECK:           to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]+]]
58
59 invoke.cont:                                      ; preds = %entry
60   invoke void @"\01?may_throw@@YAXXZ"()
61           to label %invoke.cont2 unwind label %lpad1
62
63 ; CHECK: invoke.cont2:
64 ; CHECK:   invoke void @"\01?may_throw@@YAXXZ"()
65 ; CHECK:           to label %try.cont unwind label %[[LPAD3_LABEL:lpad[0-9]+]]
66
67 invoke.cont2:                                     ; preds = %invoke.cont
68   invoke void @"\01?may_throw@@YAXXZ"()
69           to label %try.cont unwind label %lpad3
70
71 ; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
72 ; CHECK:   [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
73 ; CHECK-NEXT:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
74 ; CHECK-NEXT:   [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[TMP1]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
75 ; CHECK-NEXT:   indirectbr i8* [[RECOVER]], [label %try.cont15]
76
77 lpad:                                             ; preds = %entry
78   %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
79           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
80   %3 = extractvalue { i8*, i32 } %2, 0
81   %4 = extractvalue { i8*, i32 } %2, 1
82   br label %catch.dispatch7
83
84 ; CHECK: [[LPAD1_LABEL]]:{{[ ]+}}; preds = %invoke.cont
85 ; CHECK:   [[LPAD1_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
86 ; CHECK-NEXT:           cleanup
87 ; CHECK-NEXT:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
88 ; CHECK-NEXT:   [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 0, void (i8*, i8*)* @"\01?test@@YAXXZ.cleanup", i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[TMP1]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
89 ; CHECK-NEXT:   indirectbr i8* [[RECOVER1]], [label %try.cont15]
90
91 lpad1:                                            ; preds = %invoke.cont
92   %5 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
93           cleanup
94           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
95   %6 = extractvalue { i8*, i32 } %5, 0
96   %7 = extractvalue { i8*, i32 } %5, 1
97   br label %ehcleanup
98
99 ; CHECK: [[LPAD3_LABEL]]:{{[ ]+}}; preds = %invoke.cont2
100 ; CHECK:   [[LPAD3_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
101 ; CHECK-NEXT:           cleanup
102 ; CHECK-NEXT:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
103 ; CHECK-NEXT:   [[RECOVER3:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[TMP0]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1", i32 0, void (i8*, i8*)* @"\01?test@@YAXXZ.cleanup")
104 ; CHECK-NEXT:   indirectbr i8* [[RECOVER3]], [label %try.cont]
105
106 lpad3:                                            ; preds = %invoke.cont2
107   %8 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
108           cleanup
109           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
110   %9 = extractvalue { i8*, i32 } %8, 0
111   %10 = extractvalue { i8*, i32 } %8, 1
112   %11 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
113   %matches = icmp eq i32 %10, %11
114   br i1 %matches, label %catch, label %ehcleanup
115
116 ; CHECK-NOT: catch:
117 catch:                                            ; preds = %lpad3
118   %12 = bitcast i32* %0 to i8*
119   call void @llvm.eh.begincatch(i8* %9, i8* %12) #3
120   invoke void @"\01?handle_exception@@YAXXZ"()
121           to label %invoke.cont6 unwind label %lpad5
122
123 ; CHECK-NOT: invoke.cont6:
124 invoke.cont6:                                     ; preds = %catch
125   call void @llvm.eh.endcatch() #3
126   br label %try.cont
127
128 try.cont:                                         ; preds = %invoke.cont2, %invoke.cont6
129   call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* %obj) #3
130   br label %try.cont15
131
132 ; CHECK-NOT: lpad5:
133 lpad5:                                            ; preds = %catch
134   %13 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
135           cleanup
136           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
137   %14 = extractvalue { i8*, i32 } %13, 0
138   %15 = extractvalue { i8*, i32 } %13, 1
139   call void @llvm.eh.endcatch() #3
140   br label %ehcleanup
141
142 ; CHECK-NOT: ehcleanup
143 ehcleanup:                                        ; preds = %lpad5, %lpad3, %lpad1
144   %exn.slot.0 = phi i8* [ %14, %lpad5 ], [ %9, %lpad3 ], [ %6, %lpad1 ]
145   %ehselector.slot.0 = phi i32 [ %15, %lpad5 ], [ %10, %lpad3 ], [ %7, %lpad1 ]
146   call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* %obj) #3
147   br label %catch.dispatch7
148
149 ; CHECK-NOT: catch.dispatch7:
150 catch.dispatch7:                                  ; preds = %ehcleanup, %lpad
151   %exn.slot.1 = phi i8* [ %exn.slot.0, %ehcleanup ], [ %3, %lpad ]
152   %ehselector.slot.1 = phi i32 [ %ehselector.slot.0, %ehcleanup ], [ %4, %lpad ]
153   %16 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
154   %matches9 = icmp eq i32 %ehselector.slot.1, %16
155   br i1 %matches9, label %catch10, label %eh.resume
156
157 ; CHECK-NOT: catch10:
158 catch10:                                          ; preds = %catch.dispatch7
159   %17 = bitcast i32* %1 to i8*
160   call void @llvm.eh.begincatch(i8* %exn.slot.1, i8* %17) #3
161   call void @"\01?handle_exception@@YAXXZ"()
162   br label %invoke.cont13
163
164 ; CHECK-NOT: invoke.cont13:
165 invoke.cont13:                                    ; preds = %catch10
166   call void @llvm.eh.endcatch() #3
167   br label %try.cont15
168
169 try.cont15:                                       ; preds = %invoke.cont13, %try.cont
170   ret void
171
172 ; CHECK-NOT: eh.resume
173 eh.resume:                                        ; preds = %catch.dispatch7
174   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn.slot.1, 0
175   %lpad.val18 = insertvalue { i8*, i32 } %lpad.val, i32 %ehselector.slot.1, 1
176   resume { i8*, i32 } %lpad.val18
177
178 ; CHECK: }
179 }
180
181 ; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*)
182 ; CHECK: entry:
183 ; CHECK:   [[PARENTFRAME:\%.+]] = alloca i8*
184 ; CHECK:   store volatile i8* %1, i8** [[PARENTFRAME]]
185 ; CHECK:   call void @llvm.eh.parentframe(i8** [[PARENTFRAME]])
186 ; CHECK:   [[RECOVER_TMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
187 ; CHECK:   [[TMP1_PTR:\%.+]] = bitcast i8* [[RECOVER_TMP1]] to i32*
188 ; CHECK:   call void @"\01?handle_exception@@YAXXZ"()
189 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont15)
190 ; CHECK: }
191
192 ; CHECK-LABEL: define internal void @"\01?test@@YAXXZ.cleanup"(i8*, i8*)
193 ; CHECK: entry:
194 ; CHECK:   [[RECOVER_OBJ:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
195 ; CHECK:   [[OBJ_PTR:\%.+]] = bitcast i8* %obj.i8 to %class.SomeClass*
196 ; CHECK:   call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* [[OBJ_PTR]])
197 ; CHECK:   ret void
198 ; CHECK: }
199
200 ; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch1"(i8*, i8*)
201 ; CHECK: entry:
202 ; CHECK:   [[PARENTFRAME:\%.+]] = alloca i8*
203 ; CHECK:   store volatile i8* %1, i8** [[PARENTFRAME]]
204 ; CHECK:   call void @llvm.eh.parentframe(i8** [[PARENTFRAME]])
205 ; CHECK:   [[RECOVER_TMP0:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
206 ; CHECK:   [[TMP0_PTR:\%.+]] = bitcast i8* [[RECOVER_TMP0]] to i32*
207 ; CHECK:   invoke void @"\01?handle_exception@@YAXXZ"()
208 ; CHECK:           to label %invoke.cont6 unwind label %[[LPAD5_LABEL:lpad[0-9]+]]
209 ;
210 ; CHECK: invoke.cont6:                                     ; preds = %entry
211 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
212 ;
213 ; CHECK: [[LPAD5_LABEL]]:{{[ ]+}}; preds = %entry
214 ; CHECK:   [[LPAD5_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
215 ; CHECK:           cleanup
216 ; CHECK:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
217 ; CHECK:   unreachable
218 ; CHECK: }
219
220 declare %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* returned) #1
221
222 declare i32 @__CxxFrameHandler3(...)
223
224 declare void @"\01?may_throw@@YAXXZ"() #1
225
226 ; Function Attrs: nounwind readnone
227 declare i32 @llvm.eh.typeid.for(i8*) #2
228
229 ; Function Attrs: nounwind
230 declare void @llvm.eh.begincatch(i8* nocapture, i8* nocapture) #3
231
232 declare void @"\01?handle_exception@@YAXXZ"() #1
233
234 ; Function Attrs: nounwind
235 declare void @llvm.eh.endcatch() #3
236
237 ; Function Attrs: nounwind
238 declare void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass*) #4
239
240 attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
241 attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
242 attributes #2 = { nounwind readnone }
243 attributes #3 = { nounwind }
244 attributes #4 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
245
246 !llvm.module.flags = !{!0}
247 !llvm.ident = !{!1}
248
249 !0 = !{i32 1, !"PIC Level", i32 2}
250 !1 = !{!"clang version 3.7.0 (trunk 232069) (llvm/trunk 232070)"}