[WinEH] Split blocks at calls to llvm.eh.begincatch
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-inalloca.ll
1 ; 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'
27 target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
28 target 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)
40 ; CHECK: entry:
41 ; CHECK:   [[TMP_REGMEM:\%.+]] = alloca <{ %struct.A }>*
42 ; CHECK:   store <{ %struct.A }>* %0, <{ %struct.A }>** [[TMP_REGMEM]]
43 ; CHECK:   [[RETVAL:\%.+]] = alloca i32, align 4
44 ; CHECK:   [[E_PTR:\%.+]] = alloca i32, align 4
45 ; CHECK:   [[CLEANUP_SLOT:\%.+]] = alloca i32
46 ; CHECK:   call void (...) @llvm.frameescape(i32* %e, <{ %struct.A }>** [[TMP_REGMEM]], i32* [[RETVAL]], i32* [[CLEANUP_SLOT]])
47 ; CHECK:   invoke void @"\01?may_throw@@YAXXZ"()
48 ; CHECK:           to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
49
50 define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
51 entry:
52   %retval = alloca i32, align 4
53   %exn.slot = alloca i8*
54   %ehselector.slot = alloca i32
55   %e = alloca i32, align 4
56   %cleanup.dest.slot = alloca i32
57   invoke void @"\01?may_throw@@YAXXZ"()
58           to label %invoke.cont unwind label %lpad
59
60 invoke.cont:                                      ; preds = %entry
61   br label %try.cont
62
63 ; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
64 ; CHECK:   landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
65 ; CHECK-NEXT:           cleanup
66 ; CHECK-NEXT:           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
67 ; CHECK-NEXT:   [[RECOVER:\%recover.*]] = call i8* (...) @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 0, i8* (i8*, i8*)* @"\01?test@@YAHUA@@@Z.catch", i32 0, void (i8*, i8*)* @"\01?test@@YAHUA@@@Z.cleanup")
68 ; CHECK-NEXT:   indirectbr i8* [[RECOVER]], [label %cleanup]
69
70 lpad:                                             ; preds = %entry
71   %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
72           cleanup
73           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
74   %2 = extractvalue { i8*, i32 } %1, 0
75   store i8* %2, i8** %exn.slot
76   %3 = extractvalue { i8*, i32 } %1, 1
77   store i32 %3, i32* %ehselector.slot
78   br label %catch.dispatch
79
80 ; CHECK-NOT: catch.dispatch:
81
82 catch.dispatch:                                   ; preds = %lpad
83   %sel = load i32, i32* %ehselector.slot
84   %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
85   %matches = icmp eq i32 %sel, %4
86   br i1 %matches, label %catch, label %ehcleanup
87
88 ; CHECK-NOT: catch:
89
90 catch:                                            ; preds = %catch.dispatch
91   %exn = load i8*, i8** %exn.slot
92   %e.i8 = bitcast i32* %e to i8*
93   call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #3
94   %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
95   %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0
96   %tmp8 = load i32, i32* %a1, align 4
97   %tmp9 = load i32, i32* %e, align 4
98   %add = add nsw i32 %tmp8, %tmp9
99   store i32 %add, i32* %retval
100   store i32 1, i32* %cleanup.dest.slot
101   call void @llvm.eh.endcatch() #3
102   br label %cleanup
103
104 try.cont:                                         ; preds = %invoke.cont
105   store i32 0, i32* %retval
106   store i32 1, i32* %cleanup.dest.slot
107   br label %cleanup
108
109 ; The cleanup block should be re-written like this.
110 ; CHECK: cleanup:{{[ ]+}}; preds = %[[LPAD_LABEL]], %try.cont
111 ; CHECK:   %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
112 ; CHECK:   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2)
113 ; CHECK:   [[TMP1:\%.+]] = load i32, i32* [[RETVAL]]
114 ; CHECK:   ret i32 [[TMP1]]
115
116 cleanup:                                          ; preds = %try.cont, %catch
117   %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
118   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #3
119   %tmp10 = load i32, i32* %retval
120   ret i32 %tmp10
121
122 ; CHECK-NOT: ehcleanup:
123
124 ehcleanup:                                        ; preds = %catch.dispatch
125   %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
126   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3
127   br label %eh.resume
128
129 ; CHECK-NOT: eh.resume:
130
131 eh.resume:                                        ; preds = %ehcleanup
132   %exn2 = load i8*, i8** %exn.slot
133   %sel3 = load i32, i32* %ehselector.slot
134   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0
135   %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1
136   resume { i8*, i32 } %lpad.val4
137
138 ; CHECK: }
139 }
140
141 ; The following catch handler should be outlined.
142 ; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*)
143 ; CHECK: entry:
144 ; CHECK:   [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0)
145 ; CHECK:   [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
146 ; CHECK:   [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
147 ; CHECK:   [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
148 ; CHECK:   [[RECOVER_RETVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2)
149 ; CHECK:   [[RETVAL1:\%.+]] = bitcast i8* [[RECOVER_RETVAL]] to i32*
150 ; CHECK:   [[RECOVER_CLEANUPSLOT:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3)
151 ; CHECK:   [[CLEANUPSLOT1:\%.+]] = bitcast i8* [[RECOVER_CLEANUPSLOT]] to i32*
152 ; CHECK:   [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8*
153 ; CHECK:   [[TMP_RELOAD:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP]]
154 ; CHECK:   [[RECOVER_A:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0
155 ; CHECK:   [[A1:\%.+]] = getelementptr inbounds %struct.A, %struct.A* [[RECOVER_A]], i32 0, i32 0
156 ; CHECK:   [[TMP2:\%.+]] = load i32, i32* [[A1]], align 4
157 ; CHECK:   [[TMP3:\%.+]] = load i32, i32* [[E_PTR]], align 4
158 ; CHECK:   [[ADD:\%.+]] = add nsw i32 [[TMP2]], [[TMP3]]
159 ; CHECK:   store i32 [[ADD]], i32* [[RETVAL1]]
160 ; CHECK:   store i32 1, i32* [[CLEANUPSLOT1]]
161 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup)
162 ; CHECK: }
163
164 ; The following cleanup handler should be outlined.
165 ; CHECK: define internal void @"\01?test@@YAHUA@@@Z.cleanup"(i8*, i8*)
166 ; CHECK: entry:
167 ; CHECK:   [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
168 ; CHECK:   [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
169 ; CHECK:   [[TMP_RELOAD1:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP1]]
170 ; CHECK:   [[A3:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD1]], i32 0, i32 0
171 ; CHECK:   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A3]])
172 ; CHECK:   ret void
173 ; CHECK: }
174
175 declare void @"\01?may_throw@@YAXXZ"() #0
176
177 declare i32 @__CxxFrameHandler3(...)
178
179 ; Function Attrs: nounwind readnone
180 declare i32 @llvm.eh.typeid.for(i8*) #1
181
182 declare void @llvm.eh.begincatch(i8*, i8*)
183
184 declare void @llvm.eh.endcatch()
185
186 ; Function Attrs: nounwind
187 declare x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A*) #2
188
189 attributes #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" }
190 attributes #1 = { nounwind readnone }
191 attributes #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" }
192 attributes #3 = { nounwind }
193
194 !llvm.ident = !{!0}
195
196 !0 = !{!"clang version 3.7.0 (trunk 228868)"}