[WinEH] Run cleanup handlers when an exception is thrown
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-nonalloca-frame-values.ll
1 ; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3 ; This test is based on the following code:
4 ;
5 ; struct SomeData {
6 ;   int a;
7 ;   int b;
8 ; };
9
10 ; void may_throw();
11 ; void does_not_throw(int i);
12 ; void dump(int *, int, SomeData&);
13
14 ; void test() {
15 ;   int NumExceptions = 0;
16 ;   int ExceptionVal[10];
17 ;   SomeData Data = { 0, 0 };
18
19 ;   for (int i = 0; i < 10; ++i) {
20 ;     try {
21 ;       may_throw();
22 ;       Data.a += i;
23 ;     }
24 ;     catch (int e) {
25 ;       ExceptionVal[NumExceptions] = e;
26 ;       ++NumExceptions;
27 ;       if (e == i)
28 ;         Data.b += e;
29 ;       else
30 ;         Data.a += e;
31 ;     }
32 ;     does_not_throw(NumExceptions);
33 ;   }
34 ;   dump(ExceptionVal, NumExceptions, Data);
35 ; }
36 ;
37 ; Unlike the cppeh-frame-vars.ll test, this test was generated using -O2
38 ; optimization, which results in non-alloca values being used in the
39 ; catch handler.
40
41 ; ModuleID = 'cppeh-frame-vars.cpp'
42 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
43 target triple = "x86_64-pc-windows-msvc"
44
45 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
46 %struct.SomeData = type { i32, i32 }
47
48 $"\01??_R0H@8" = comdat any
49
50 @"\01??_7type_info@@6B@" = external constant i8*
51 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
52
53 ; The function entry should be rewritten like this.
54 ; CHECK: define void @"\01?test@@YAXXZ"()
55 ; CHECK: entry:
56 ; CHECK:   [[NUMEXCEPTIONS_REGMEM:\%.+]] = alloca i32
57 ; CHECK:   [[I_REGMEM:\%.+]] = alloca i32
58 ; CHECK:   [[E_PTR:\%.+]] = alloca i32, align 4
59 ; CHECK:   [[EXCEPTIONVAL:\%.+]] = alloca [10 x i32], align 16
60 ; CHECK:   [[DATA_PTR:\%.+]] = alloca i64, align 8
61 ; CHECK:   [[TMPCAST:\%.+]] = bitcast i64* [[DATA_PTR]] to %struct.SomeData*
62 ; CHECK:   [[TMP:\%.+]] = bitcast [10 x i32]* [[EXCEPTIONVAL]] to i8*
63 ; CHECK:   call void @llvm.lifetime.start(i64 40, i8* [[TMP]])
64 ; CHECK:   store i64 0, i64* [[DATA_PTR]], align 8
65 ; CHECK:   [[A_REGMEM:\%.+]] = alloca i32*
66 ; CHECK:   [[A_PTR:\%.+]] = bitcast i64* [[DATA_PTR]] to i32*
67 ; CHECK:   store i32* [[A_PTR]], i32** [[A_REGMEM]]
68 ; CHECK:   [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[TMPCAST]], i64 0, i32 1
69 ; CHECK:   [[B_REGMEM:\%.+]] = alloca i32*
70 ; CHECK:   store i32* [[B_PTR]], i32** [[B_REGMEM]]
71 ; CHECK:   store i32 0, i32* [[NUMEXCEPTIONS_REGMEM]]
72 ; CHECK:   store i32 0, i32* [[I_REGMEM]]
73 ; CHECK:   call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions.020.reg2mem, [10 x i32]* [[EXCEPTIONVAL]], i32* [[I_REGMEM]], i32** [[A_REGMEM]], i32** [[B_REGMEM]])
74 ; CHECK:   br label %for.body
75
76 ; Function Attrs: uwtable
77 define void @"\01?test@@YAXXZ"() #0 {
78 entry:
79   %e = alloca i32, align 4
80   %ExceptionVal = alloca [10 x i32], align 16
81   %Data = alloca i64, align 8
82   %tmpcast = bitcast i64* %Data to %struct.SomeData*
83   %0 = bitcast [10 x i32]* %ExceptionVal to i8*
84   call void @llvm.lifetime.start(i64 40, i8* %0) #1
85   store i64 0, i64* %Data, align 8
86   %a = bitcast i64* %Data to i32*
87   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
88   br label %for.body
89
90 ; CHECK: for.body:
91 ; CHECK-NOT:  phi i32 [ 0, %entry ], [ {{\%NumExceptions.*}}, %try.cont ]
92 ; CHECK-NOT:  phi i32 [ 0, %entry ], [ {{\%inc.*}}, %try.cont ]
93 ; CHECK:  [[I_RELOAD:\%.+]] = load i32, i32* [[I_REGMEM]]
94 ; CHECK:  [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_REGMEM]]
95 for.body:                                         ; preds = %entry, %try.cont
96   %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
97   %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
98   invoke void @"\01?may_throw@@YAXXZ"()
99           to label %invoke.cont unwind label %lpad
100
101 ; CHECK: invoke.cont:                                      ; preds = %for.body
102 ; CHECK:   [[A_RELOAD:\%.+]] = load volatile i32*, i32** [[A_REGMEM]]
103 ; CHECK:   [[TMP1:\%.+]] = load i32, i32* [[A_RELOAD]], align 8
104 ; CHECK:   [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]]
105 ; CHECK:   [[A_RELOAD1:\%.+]] = load volatile i32*, i32** [[A_REGMEM]]
106 ; CHECK:   store i32 [[ADD]], i32* [[A_RELOAD1]], align 8
107 ; CHECK:   br label %try.cont
108 invoke.cont:                                      ; preds = %for.body
109   %1 = load i32, i32* %a, align 8, !tbaa !2
110   %add = add nsw i32 %1, %i.019
111   store i32 %add, i32* %a, align 8, !tbaa !2
112   br label %try.cont
113
114 ; CHECK: [[LPAD_LABEL:lpad[0-9]*]]:{{[ ]+}}; preds = %for.body
115 ; CHECK:   landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
116 ; CHECK-NEXT:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
117 ; CHECK-NEXT:   [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
118 ; CHECK-NEXT:   indirectbr i8* [[RECOVER]], [label %try.cont]
119
120 lpad:                                             ; preds = %for.body
121   %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
122           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
123   %3 = extractvalue { i8*, i32 } %2, 1
124   %4 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
125   %matches = icmp eq i32 %3, %4
126   br i1 %matches, label %catch, label %eh.resume
127
128 ; CHECK-NOT: catch:
129
130 catch:                                            ; preds = %lpad
131   %5 = extractvalue { i8*, i32 } %2, 0
132   %e.i8 = bitcast i32* %e to i8*
133   call void @llvm.eh.begincatch(i8* %5, i8* %e.i8) #1
134   %tmp8 = load i32, i32* %e, align 4, !tbaa !7
135   %idxprom = sext i32 %NumExceptions.020 to i64
136   %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
137   store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
138   %inc = add nsw i32 %NumExceptions.020, 1
139   %cmp1 = icmp eq i32 %tmp8, %i.019
140   br i1 %cmp1, label %if.then, label %if.else
141
142 ; CHECK-NOT: if.then:
143
144 if.then:                                          ; preds = %catch
145   %tmp9 = load i32, i32* %b, align 4, !tbaa !8
146   %add2 = add nsw i32 %tmp9, %i.019
147   store i32 %add2, i32* %b, align 4, !tbaa !8
148   br label %if.end
149
150 ; CHECK-NOT: if.else:
151
152 if.else:                                          ; preds = %catch
153   %tmp10 = load i32, i32* %a, align 8, !tbaa !2
154   %add4 = add nsw i32 %tmp10, %tmp8
155   store i32 %add4, i32* %a, align 8, !tbaa !2
156   br label %if.end
157
158 ; CHECK-NOT: if.end:
159
160 if.end:                                           ; preds = %if.else, %if.then
161   tail call void @llvm.eh.endcatch() #1
162   br label %try.cont
163
164 ; CHECK: try.cont:{{[ ]+}}; preds = %[[LPAD_LABEL]], %invoke.cont
165 ; CHECK-NOT:  phi i32
166 ; CHECK:   tail call void @"\01?does_not_throw@@YAXH@Z"(i32 [[NUMEXCEPTIONS_RELOAD]])
167 ; CHECK:   [[INC:\%.+]] = add nuw nsw i32 [[I_RELOAD]], 1
168 ; CHECK:   [[CMP:\%.+]] = icmp slt i32 [[INC]], 10
169 ; CHECK:   store i32 [[NUMEXCEPTIONS_RELOAD]], i32* [[NUMEXCEPTIONS_REGMEM]]
170 ; CHECK:   store i32 [[INC]], i32* [[I_REGMEM]]
171 ; CHECK:   br i1 [[CMP]], label %for.body, label %for.end
172
173 try.cont:                                         ; preds = %if.end, %invoke.cont
174   %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
175   tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
176   %inc5 = add nuw nsw i32 %i.019, 1
177   %cmp = icmp slt i32 %inc5, 10
178   br i1 %cmp, label %for.body, label %for.end
179
180 for.end:                                          ; preds = %try.cont
181   %NumExceptions.1.lcssa = phi i32 [ %NumExceptions.1, %try.cont ]
182   %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 0
183   call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %NumExceptions.1.lcssa, %struct.SomeData* dereferenceable(8) %tmpcast)
184   call void @llvm.lifetime.end(i64 40, i8* %0) #1
185   ret void
186
187 eh.resume:                                        ; preds = %lpad
188   %.lcssa = phi { i8*, i32 } [ %2, %lpad ]
189   resume { i8*, i32 } %.lcssa
190 }
191
192 ; The following catch handler should be outlined.
193 ; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*)
194 ; CHECK: entry:
195 ; CHECK:   [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
196 ; CHECK:   [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
197 ; CHECK:   [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
198 ; CHECK:   [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to i32*
199 ; CHECK:   [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP]]
200 ; CHECK:   [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
201 ; CHECK:   [[EXCEPTIONVAL:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]*
202 ; CHECK:   [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3)
203 ; CHECK:   [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP1]] to i32*
204 ; CHECK:   [[I_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP1]]
205 ; CHECK:   [[RECOVER_EH_TEMP2:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4)
206 ; CHECK:   [[EH_TEMP2:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP2]] to i32**
207 ; CHECK:   [[A_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP2]]
208 ; CHECK:   [[RECOVER_EH_TEMP3:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 5)
209 ; CHECK:   [[EH_TEMP3:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP3]] to i32**
210 ; CHECK:   [[B_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP3]]
211 ; CHECK:   [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8*
212 ; CHECK:   [[TMP:\%.+]] = load i32, i32* [[E_PTR]], align 4
213 ; CHECK:   [[IDXPROM:\%.+]] = sext i32 [[NUMEXCEPTIONS_RELOAD]] to i64
214 ; CHECK:   [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL]], i64 0, i64 [[IDXPROM]]
215 ; CHECK:   store i32 [[TMP]], i32* [[ARRAYIDX]], align 4
216 ; CHECK:   [[INC:\%.+]] = add nsw i32 [[NUMEXCEPTIONS_RELOAD]], 1
217 ; CHECK:   [[CMP:\%.+]] = icmp eq i32 [[TMP]], [[I_RELOAD]]
218 ; CHECK:   br i1 [[CMP]], label %if.then, label %if.else
219 ;
220 ; CHECK: if.then:{{[ ]+}}; preds = %entry
221 ; CHECK:   [[TMP1:\%.+]] = load i32, i32* [[B_RELOAD]], align 4
222 ; CHECK:   [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]]
223 ; CHECK:   store i32 [[ADD]], i32* [[B_RELOAD]], align 4
224 ; CHECK:   br label %if.end
225 ;
226 ; CHECK: if.else:{{[ ]+}}; preds = %entry
227 ; CHECK:   [[TMP2:\%.+]] = load i32, i32* [[A_RELOAD]], align 8
228 ; CHECK:   [[ADD2:\%.+]] = add nsw i32 [[TMP2]], [[TMP]]
229 ; CHECK:   store i32 [[ADD2]], i32* [[A_RELOAD]], align 8
230 ; CHECK:   br label %if.end
231 ;
232 ; CHECK: if.end:{{[ ]+}}; preds = %if.else, %if.then
233 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
234 ; CHECK: }
235
236 ; Function Attrs: nounwind
237 declare void @llvm.lifetime.start(i64, i8* nocapture) #1
238
239 declare void @"\01?may_throw@@YAXXZ"() #2
240
241 declare i32 @__CxxFrameHandler3(...)
242
243 ; Function Attrs: nounwind readnone
244 declare i32 @llvm.eh.typeid.for(i8*) #3
245
246 declare void @llvm.eh.begincatch(i8*, i8*)
247
248 declare void @llvm.eh.endcatch()
249
250 declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
251
252 declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
253
254 ; Function Attrs: nounwind
255 declare void @llvm.lifetime.end(i64, i8* nocapture) #1
256
257 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" }
258 attributes #1 = { nounwind }
259 attributes #2 = { "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" }
260 attributes #3 = { nounwind readnone }
261
262 !llvm.module.flags = !{!0}
263 !llvm.ident = !{!1}
264
265 !0 = !{i32 1, !"PIC Level", i32 2}
266 !1 = !{!"clang version 3.7.0 (trunk 228868)"}
267 !2 = !{!3, !4, i64 0}
268 !3 = !{!"?AUSomeData@@", !4, i64 0, !4, i64 4}
269 !4 = !{!"int", !5, i64 0}
270 !5 = !{!"omnipotent char", !6, i64 0}
271 !6 = !{!"Simple C/C++ TBAA"}
272 !7 = !{!4, !4, i64 0}
273 !8 = !{!3, !4, i64 4}