blob: 6382a872a6401bf2aab6aa9f8547f3cdc4e42351 [file] [log] [blame]
Andrew Kaylor72029c62015-03-03 00:41:03 +00001; 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'
42target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
43target 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
Andrew Kaylor72029c62015-03-03 00:41:03 +000053; The function entry should be rewritten like this.
David Majnemercde33032015-03-30 22:58:10 +000054; CHECK: define void @"\01?test@@YAXXZ"()
Andrew Kaylor72029c62015-03-03 00:41:03 +000055; CHECK: entry:
Andrew Kaylor6b67d422015-03-11 23:22:06 +000056; CHECK: [[NUMEXCEPTIONS_REGMEM:\%.+]] = alloca i32
57; CHECK: [[I_REGMEM:\%.+]] = alloca i32
Andrew Kaylor67d3c032015-04-08 20:57:22 +000058; CHECK: [[A_REGMEM:\%.+]] = alloca i32*
59; CHECK: [[B_REGMEM:\%.+]] = alloca i32*
Andrew Kaylor6b67d422015-03-11 23:22:06 +000060; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
61; CHECK: [[EXCEPTIONVAL:\%.+]] = alloca [10 x i32], align 16
62; CHECK: [[DATA_PTR:\%.+]] = alloca i64, align 8
63; CHECK: [[TMPCAST:\%.+]] = bitcast i64* [[DATA_PTR]] to %struct.SomeData*
64; CHECK: [[TMP:\%.+]] = bitcast [10 x i32]* [[EXCEPTIONVAL]] to i8*
65; CHECK: call void @llvm.lifetime.start(i64 40, i8* [[TMP]])
66; CHECK: store i64 0, i64* [[DATA_PTR]], align 8
Andrew Kaylor6b67d422015-03-11 23:22:06 +000067; CHECK: [[A_PTR:\%.+]] = bitcast i64* [[DATA_PTR]] to i32*
68; CHECK: store i32* [[A_PTR]], i32** [[A_REGMEM]]
69; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[TMPCAST]], i64 0, i32 1
Andrew Kaylor6b67d422015-03-11 23:22:06 +000070; 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]])
Reid Klecknercfb9ce52015-03-05 18:26:34 +000074; CHECK: br label %for.body
Andrew Kaylor72029c62015-03-03 00:41:03 +000075
76; Function Attrs: uwtable
77define void @"\01?test@@YAXXZ"() #0 {
78entry:
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000079 %e = alloca i32, align 4
Andrew Kaylor72029c62015-03-03 00:41:03 +000080 %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*
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000087 %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
Andrew Kaylor72029c62015-03-03 00:41:03 +000088 br label %for.body
89
90; CHECK: for.body:
Andrew Kaylor6b67d422015-03-11 23:22:06 +000091; 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]]
Andrew Kaylor72029c62015-03-03 00:41:03 +000095for.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
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000102; 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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000107; CHECK: br label %try.cont
108invoke.cont: ; preds = %for.body
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000109 %1 = load i32, i32* %a, align 8, !tbaa !2
Andrew Kaylor72029c62015-03-03 00:41:03 +0000110 %add = add nsw i32 %1, %i.019
111 store i32 %add, i32* %a, align 8, !tbaa !2
112 br label %try.cont
113
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000114; CHECK: [[LPAD_LABEL:lpad[0-9]*]]:{{[ ]+}}; preds = %for.body
Reid Klecknerc759fe92015-03-19 22:31:02 +0000115; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
Reid Klecknere1b22ec2015-03-12 01:38:48 +0000116; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
Reid Kleckner3567d272015-04-02 21:13:31 +0000117; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 0, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
Reid Klecknere1b22ec2015-03-12 01:38:48 +0000118; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000119
Andrew Kaylor72029c62015-03-03 00:41:03 +0000120lpad: ; 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
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000128; CHECK-NOT: catch:
129
Andrew Kaylor72029c62015-03-03 00:41:03 +0000130catch: ; preds = %lpad
131 %5 = extractvalue { i8*, i32 } %2, 0
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000132 %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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000135 %idxprom = sext i32 %NumExceptions.020 to i64
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000136 %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000137 store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
Andrew Kaylor72029c62015-03-03 00:41:03 +0000138 %inc = add nsw i32 %NumExceptions.020, 1
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000139 %cmp1 = icmp eq i32 %tmp8, %i.019
Andrew Kaylor72029c62015-03-03 00:41:03 +0000140 br i1 %cmp1, label %if.then, label %if.else
141
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000142; CHECK-NOT: if.then:
143
Andrew Kaylor72029c62015-03-03 00:41:03 +0000144if.then: ; preds = %catch
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000145 %tmp9 = load i32, i32* %b, align 4, !tbaa !8
146 %add2 = add nsw i32 %tmp9, %i.019
Andrew Kaylor72029c62015-03-03 00:41:03 +0000147 store i32 %add2, i32* %b, align 4, !tbaa !8
148 br label %if.end
149
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000150; CHECK-NOT: if.else:
151
Andrew Kaylor72029c62015-03-03 00:41:03 +0000152if.else: ; preds = %catch
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000153 %tmp10 = load i32, i32* %a, align 8, !tbaa !2
154 %add4 = add nsw i32 %tmp10, %tmp8
Andrew Kaylor72029c62015-03-03 00:41:03 +0000155 store i32 %add4, i32* %a, align 8, !tbaa !2
156 br label %if.end
157
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000158; CHECK-NOT: if.end:
159
Andrew Kaylor72029c62015-03-03 00:41:03 +0000160if.end: ; preds = %if.else, %if.then
161 tail call void @llvm.eh.endcatch() #1
162 br label %try.cont
163
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000164; 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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000172
173try.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
180for.end: ; preds = %try.cont
181 %NumExceptions.1.lcssa = phi i32 [ %NumExceptions.1, %try.cont ]
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000182 %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 0
Andrew Kaylor72029c62015-03-03 00:41:03 +0000183 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
187eh.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.
David Majnemercde33032015-03-30 22:58:10 +0000193; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*)
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000194; CHECK: entry:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000195; 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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000219;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000220; 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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000224; CHECK: br label %if.end
225;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000226; 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
Andrew Kaylor72029c62015-03-03 00:41:03 +0000230; CHECK: br label %if.end
231;
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000232; CHECK: if.end:{{[ ]+}}; preds = %if.else, %if.then
Andrew Kaylor72029c62015-03-03 00:41:03 +0000233; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
234; CHECK: }
235
236; Function Attrs: nounwind
237declare void @llvm.lifetime.start(i64, i8* nocapture) #1
238
239declare void @"\01?may_throw@@YAXXZ"() #2
240
241declare i32 @__CxxFrameHandler3(...)
242
243; Function Attrs: nounwind readnone
244declare i32 @llvm.eh.typeid.for(i8*) #3
245
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000246declare void @llvm.eh.begincatch(i8*, i8*)
Andrew Kaylor72029c62015-03-03 00:41:03 +0000247
248declare void @llvm.eh.endcatch()
249
250declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
251
252declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
253
254; Function Attrs: nounwind
255declare void @llvm.lifetime.end(i64, i8* nocapture) #1
256
257attributes #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" }
258attributes #1 = { nounwind }
259attributes #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" }
260attributes #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}