blob: a5d2faf57222ca9e8c011c36ca56bcba9c3c7ba8 [file] [log] [blame]
Andrew Kaylor72029c62015-03-03 00:41:03 +00001; RUN: opt -mtriple=i386-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3; This test is built from the following code:
4; struct A {
5; A(int a);
6; A(const A &o);
7; ~A();
8; int a;
9; };
10;
11; void may_throw();
12;
13; int test(A a) {
14; try {
15; may_throw();
16; }
17; catch (int e) {
18; return a.a + e;
19; }
20; return 0;
21; }
22;
23; The test was built for a 32-bit Windows target and then the reference to
24; the inalloca instruction was manually sunk into the landingpad.
25
26; ModuleID = 'cppeh-inalloca.cpp'
27target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
28target triple = "i386-pc-windows-msvc"
29
30%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
31%struct.A = type { i32 }
32
33$"\01??_R0H@8" = comdat any
34
35@"\01??_7type_info@@6B@" = external constant i8*
36@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
37
38; The function entry should be rewritten like this.
39; CHECK: define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
40; CHECK: entry:
Andrew Kaylor6b67d422015-03-11 23:22:06 +000041; CHECK: [[TMP_REGMEM:\%.+]] = alloca <{ %struct.A }>*
42; CHECK: [[TMP:\%.+]] = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef
43; CHECK: store <{ %struct.A }>* [[TMP]], <{ %struct.A }>** [[TMP_REGMEM]]
44; CHECK: [[RETVAL:\%.+]] = alloca i32, align 4
45; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
46; CHECK: [[CLEANUP_SLOT:\%.+]] = alloca i32
47; CHECK: call void (...)* @llvm.frameescape(i32* %e, <{ %struct.A }>** [[TMP_REGMEM]], i32* [[RETVAL]], i32* [[CLEANUP_SLOT]])
Andrew Kaylor72029c62015-03-03 00:41:03 +000048; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
Andrew Kaylor6b67d422015-03-11 23:22:06 +000049; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
Andrew Kaylor72029c62015-03-03 00:41:03 +000050
51define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
52entry:
53 %retval = alloca i32, align 4
54 %exn.slot = alloca i8*
55 %ehselector.slot = alloca i32
56 %e = alloca i32, align 4
57 %cleanup.dest.slot = alloca i32
58 invoke void @"\01?may_throw@@YAXXZ"()
59 to label %invoke.cont unwind label %lpad
60
61invoke.cont: ; preds = %entry
62 br label %try.cont
63
Andrew Kaylor6b67d422015-03-11 23:22:06 +000064; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
65; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
66; CHECK: cleanup
67; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
68; CHECK-NOT: extractvalue { i8*, i32 }
69; CHECK-NOT: store i8*
70; CHECK-NOT: store i32
71; CHECK-NOT: br label %catch.dispatch
72; CHECK: [[RECOVER:\%recover.*]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD_VAL]], i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAHUA@@@Z.catch" to i8*), i32 1, i8* bitcast (void (i8*, i8*)* @"\01?test@@YAHUA@@@Z.cleanup" to i8*))
73; CHECK: indirectbr i8* [[RECOVER]], [label %cleanup]
74
Andrew Kaylor72029c62015-03-03 00:41:03 +000075lpad: ; preds = %entry
76 %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
77 cleanup
78 catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
79 %2 = extractvalue { i8*, i32 } %1, 0
80 store i8* %2, i8** %exn.slot
81 %3 = extractvalue { i8*, i32 } %1, 1
82 store i32 %3, i32* %ehselector.slot
83 br label %catch.dispatch
84
Andrew Kaylor6b67d422015-03-11 23:22:06 +000085; CHECK-NOT: catch.dispatch:
86
Andrew Kaylor72029c62015-03-03 00:41:03 +000087catch.dispatch: ; preds = %lpad
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000088 %sel = load i32, i32* %ehselector.slot
Andrew Kaylor72029c62015-03-03 00:41:03 +000089 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
90 %matches = icmp eq i32 %sel, %4
91 br i1 %matches, label %catch, label %ehcleanup
92
Andrew Kaylor6b67d422015-03-11 23:22:06 +000093; CHECK-NOT: catch:
94
Andrew Kaylor72029c62015-03-03 00:41:03 +000095catch: ; preds = %catch.dispatch
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000096 %exn = load i8*, i8** %exn.slot
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000097 %e.i8 = bitcast i32* %e to i8*
98 call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #3
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000099 %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
100 %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000101 %tmp8 = load i32, i32* %a1, align 4
102 %tmp9 = load i32, i32* %e, align 4
103 %add = add nsw i32 %tmp8, %tmp9
Andrew Kaylor72029c62015-03-03 00:41:03 +0000104 store i32 %add, i32* %retval
105 store i32 1, i32* %cleanup.dest.slot
106 call void @llvm.eh.endcatch() #3
107 br label %cleanup
108
109try.cont: ; preds = %invoke.cont
110 store i32 0, i32* %retval
111 store i32 1, i32* %cleanup.dest.slot
112 br label %cleanup
113
114; The cleanup block should be re-written like this.
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000115; CHECK: cleanup:{{[ ]+}}; preds = %[[LPAD_LABEL]], %try.cont
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000116; CHECK-NOT: %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000117; CHECK: [[TMP_RELOAD:\%.+]] = load volatile <{ %struct.A }>*, <{ %struct.A }>** [[TMP_REGMEM]]
118; CHECK: [[A2:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0
119; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A2]])
120; CHECK: [[TMP1:\%.+]] = load i32, i32* [[RETVAL]]
121; CHECK: ret i32 [[TMP1]]
Andrew Kaylor72029c62015-03-03 00:41:03 +0000122
123cleanup: ; preds = %try.cont, %catch
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000124 %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
Andrew Kaylor72029c62015-03-03 00:41:03 +0000125 call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #3
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000126 %tmp10 = load i32, i32* %retval
127 ret i32 %tmp10
Andrew Kaylor72029c62015-03-03 00:41:03 +0000128
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000129; CHECK-NOT: ehcleanup:
130
Andrew Kaylor72029c62015-03-03 00:41:03 +0000131ehcleanup: ; preds = %catch.dispatch
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000132 %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
Andrew Kaylor72029c62015-03-03 00:41:03 +0000133 call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3
134 br label %eh.resume
135
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000136; CHECK-NOT: eh.resume:
137
Andrew Kaylor72029c62015-03-03 00:41:03 +0000138eh.resume: ; preds = %ehcleanup
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000139 %exn2 = load i8*, i8** %exn.slot
140 %sel3 = load i32, i32* %ehselector.slot
Andrew Kaylor72029c62015-03-03 00:41:03 +0000141 %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0
142 %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1
143 resume { i8*, i32 } %lpad.val4
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000144
145; CHECK: }
Andrew Kaylor72029c62015-03-03 00:41:03 +0000146}
147
148; The following catch handler should be outlined.
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000149; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
150; CHECK: entry:
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000151; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0)
152; CHECK: [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
153; CHECK: [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
154; CHECK: [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
155; CHECK: [[TMP_RELOAD:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP]]
156; CHECK: [[RECOVER_RETVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2)
157; CHECK: [[RETVAL1:\%.+]] = bitcast i8* [[RECOVER_RETVAL]] to i32*
158; CHECK: [[RECOVER_CLEANUPSLOT:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3)
159; CHECK: [[CLEANUPSLOT1:\%.+]] = bitcast i8* [[RECOVER_CLEANUPSLOT]] to i32*
160; CHECK: [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8*
161; CHECK: [[RECOVER_A:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0
162; CHECK: [[A1:\%.+]] = getelementptr inbounds %struct.A, %struct.A* [[RECOVER_A]], i32 0, i32 0
163; CHECK: [[TMP2:\%.+]] = load i32, i32* [[A1]], align 4
164; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR]], align 4
165; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP2]], [[TMP3]]
166; CHECK: store i32 [[ADD]], i32* [[RETVAL1]]
167; CHECK: store i32 1, i32* [[CLEANUPSLOT1]]
Andrew Kaylor72029c62015-03-03 00:41:03 +0000168; CHECK: ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup)
169; CHECK: }
170
Andrew Kaylor6b67d422015-03-11 23:22:06 +0000171; The following cleanup handler should be outlined.
172; CHECK: define internal void @"\01?test@@YAHUA@@@Z.cleanup"(i8*, i8*) {
173; CHECK: entry:
174; CHECK: [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
175; CHECK: [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
176; CHECK: [[TMP_RELOAD1:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP1]]
177; CHECK: [[A3:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD1]], i32 0, i32 0
178; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A3]])
179; CHECK: ret void
180; CHECK: }
181
Andrew Kaylor72029c62015-03-03 00:41:03 +0000182declare void @"\01?may_throw@@YAXXZ"() #0
183
184declare i32 @__CxxFrameHandler3(...)
185
186; Function Attrs: nounwind readnone
187declare i32 @llvm.eh.typeid.for(i8*) #1
188
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000189declare void @llvm.eh.begincatch(i8*, i8*)
Andrew Kaylor72029c62015-03-03 00:41:03 +0000190
191declare void @llvm.eh.endcatch()
192
193; Function Attrs: nounwind
194declare x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A*) #2
195
196attributes #0 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
197attributes #1 = { nounwind readnone }
198attributes #2 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
199attributes #3 = { nounwind }
200
201!llvm.ident = !{!0}
202
203!0 = !{!"clang version 3.7.0 (trunk 228868)"}