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