blob: b1a6aae814936dddc9381092f8f82364fce2972a [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:
41; CHECK: %frame.alloc = call i8* @llvm.frameallocate(i32 24)
42; CHECK: %eh.data = bitcast i8* %frame.alloc to %"struct.\01?test@@YAHUA@@@Z.ehdata"*
43; CHECK: %.tmp.reg2mem = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 3
44; CHECK: %.tmp = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef
45; CHECK: store <{ %struct.A }>* %.tmp, <{ %struct.A }>** %.tmp.reg2mem
46; CHECK-NOT: %retval = alloca i32, align 4
47; CHECK: %retval = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 4
48; CHECK: %exn.slot = alloca i8*
49; CHECK: %ehselector.slot = alloca i32
50; CHECK-NOT: %e = alloca i32, align 4
51; CHECK: %e = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 2
52; CHECK: %cleanup.dest.slot = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 5
53; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
54; CHECK: to label %invoke.cont unwind label %lpad
55
56define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
57entry:
58 %retval = alloca i32, align 4
59 %exn.slot = alloca i8*
60 %ehselector.slot = alloca i32
61 %e = alloca i32, align 4
62 %cleanup.dest.slot = alloca i32
63 invoke void @"\01?may_throw@@YAXXZ"()
64 to label %invoke.cont unwind label %lpad
65
66invoke.cont: ; preds = %entry
67 br label %try.cont
68
69lpad: ; preds = %entry
70 %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
71 cleanup
72 catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
73 %2 = extractvalue { i8*, i32 } %1, 0
74 store i8* %2, i8** %exn.slot
75 %3 = extractvalue { i8*, i32 } %1, 1
76 store i32 %3, i32* %ehselector.slot
77 br label %catch.dispatch
78
79catch.dispatch: ; preds = %lpad
80 %sel = load i32* %ehselector.slot
81 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
82 %matches = icmp eq i32 %sel, %4
83 br i1 %matches, label %catch, label %ehcleanup
84
85catch: ; preds = %catch.dispatch
86 %exn = load i8** %exn.slot
87 %5 = call i8* @llvm.eh.begincatch(i8* %exn) #3
88 %6 = bitcast i8* %5 to i32*
89 %7 = load i32* %6, align 4
90 store i32 %7, i32* %e, align 4
91 %a = getelementptr inbounds <{ %struct.A }>* %0, i32 0, i32 0
92 %a1 = getelementptr inbounds %struct.A* %a, i32 0, i32 0
93 %8 = load i32* %a1, align 4
94 %9 = load i32* %e, align 4
95 %add = add nsw i32 %8, %9
96 store i32 %add, i32* %retval
97 store i32 1, i32* %cleanup.dest.slot
98 call void @llvm.eh.endcatch() #3
99 br label %cleanup
100
101try.cont: ; preds = %invoke.cont
102 store i32 0, i32* %retval
103 store i32 1, i32* %cleanup.dest.slot
104 br label %cleanup
105
106; The cleanup block should be re-written like this.
107; CHECK: cleanup: ; preds = %try.cont, %catch
108; CHECK-NOT: %a2 = getelementptr inbounds <{ %struct.A }>* %0, i32 0, i32 0
109; CHECK: %.tmp.reload1 = load volatile <{ %struct.A }>** %.tmp.reg2mem
110; CHECK: %a2 = getelementptr inbounds <{ %struct.A }>* %.tmp.reload1, i32 0, i32 0
111; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #2
112; CHECK: %10 = load i32* %retval
113; CHECK: ret i32 %10
114
115cleanup: ; preds = %try.cont, %catch
116 %a2 = getelementptr inbounds <{ %struct.A }>* %0, i32 0, i32 0
117 call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #3
118 %10 = load i32* %retval
119 ret i32 %10
120
121ehcleanup: ; preds = %catch.dispatch
122 %a3 = getelementptr inbounds <{ %struct.A }>* %0, i32 0, i32 0
123 call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3
124 br label %eh.resume
125
126eh.resume: ; preds = %ehcleanup
127 %exn2 = load i8** %exn.slot
128 %sel3 = load i32* %ehselector.slot
129 %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0
130 %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1
131 resume { i8*, i32 } %lpad.val4
132}
133
134; The following catch handler should be outlined.
135; CHECK: define i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
136; CHECK: catch.entry:
137; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1)
138; CHECK: %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAHUA@@@Z.ehdata"*
139; CHECK: %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 1
140; CHECK: %eh.obj = load i8** %eh.obj.ptr
141; CHECK: %e = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 2
142; CHECK: %eh.temp.alloca = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 3
143; CHECK: %.reload = load <{ %struct.A }>** %eh.temp.alloca
144; CHECK: %retval = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 4
145; CHECK: %cleanup.dest.slot = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 5
146; CHECK: %2 = bitcast i8* %eh.obj to i32*
147; CHECK: %3 = load i32* %2, align 4
148; CHECK: store i32 %3, i32* %e, align 4
149; CHECK: %a = getelementptr inbounds <{ %struct.A }>* %.reload, i32 0, i32 0
150; CHECK: %a1 = getelementptr inbounds %struct.A* %a, i32 0, i32 0
151; CHECK: %4 = load i32* %a1, align 4
152; CHECK: %5 = load i32* %e, align 4
153; CHECK: %add = add nsw i32 %4, %5
154; CHECK: store i32 %add, i32* %retval
155; CHECK: store i32 1, i32* %cleanup.dest.slot
156; CHECK: ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup)
157; CHECK: }
158
159
160declare void @"\01?may_throw@@YAXXZ"() #0
161
162declare i32 @__CxxFrameHandler3(...)
163
164; Function Attrs: nounwind readnone
165declare i32 @llvm.eh.typeid.for(i8*) #1
166
167declare i8* @llvm.eh.begincatch(i8*)
168
169declare void @llvm.eh.endcatch()
170
171; Function Attrs: nounwind
172declare x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A*) #2
173
174attributes #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" }
175attributes #1 = { nounwind readnone }
176attributes #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" }
177attributes #3 = { nounwind }
178
179!llvm.ident = !{!0}
180
181!0 = !{!"clang version 3.7.0 (trunk 228868)"}