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