1 ; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
3 ; This test is based on the following code:
11 ; void does_not_throw(int i);
12 ; void dump(int *, int, SomeData&);
15 ; int NumExceptions = 0;
16 ; int ExceptionVal[10];
17 ; SomeData Data = { 0, 0 };
19 ; for (int i = 0; i < 10; ++i) {
25 ; ExceptionVal[NumExceptions] = e;
32 ; does_not_throw(NumExceptions);
34 ; dump(ExceptionVal, NumExceptions, Data);
37 ; ModuleID = 'cppeh-frame-vars.cpp'
38 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
39 target triple = "x86_64-pc-windows-msvc"
41 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
42 %struct.SomeData = type { i32, i32 }
44 $"\01??_R0H@8" = comdat any
46 @"\01??_7type_info@@6B@" = external constant i8*
47 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
49 ; The function entry should be rewritten like this.
50 ; CHECK: define void @"\01?test@@YAXXZ"() #0 {
52 ; CHECK: [[NUMEXCEPTIONS_PTR:\%.+]] = alloca i32, align 4
53 ; CHECK: [[EXCEPTIONVAL_PTR:\%.+]] = alloca [10 x i32], align 16
54 ; CHECK: [[DATA_PTR:\%.+]] = alloca %struct.SomeData, align 4
55 ; CHECK: [[I_PTR:\%.+]] = alloca i32, align 4
56 ; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
57 ; CHECK: store i32 0, i32* [[NUMEXCEPTIONS_PTR]], align 4
58 ; CHECK: [[TMP:\%.+]] = bitcast %struct.SomeData* [[DATA_PTR]] to i8*
59 ; CHECK: call void @llvm.memset(i8* [[TMP]], i8 0, i64 8, i32 4, i1 false)
60 ; CHECK: store i32 0, i32* [[I_PTR]], align 4
61 ; CHECK: call void (...)* @llvm.frameescape(i32* [[E_PTR]], i32* [[NUMEXCEPTIONS_PTR]], [10 x i32]* [[EXCEPTIONVAL_PTR]], i32* [[I_PTR]], %struct.SomeData* [[DATA_PTR]])
62 ; CHECK: br label %for.cond
64 ; Function Attrs: uwtable
65 define void @"\01?test@@YAXXZ"() #0 {
67 %NumExceptions = alloca i32, align 4
68 %ExceptionVal = alloca [10 x i32], align 16
69 %Data = alloca %struct.SomeData, align 4
70 %i = alloca i32, align 4
71 %exn.slot = alloca i8*
72 %ehselector.slot = alloca i32
73 %e = alloca i32, align 4
74 store i32 0, i32* %NumExceptions, align 4
75 %tmp = bitcast %struct.SomeData* %Data to i8*
76 call void @llvm.memset(i8* %tmp, i8 0, i64 8, i32 4, i1 false)
77 store i32 0, i32* %i, align 4
80 for.cond: ; preds = %for.inc, %entry
81 %tmp1 = load i32, i32* %i, align 4
82 %cmp = icmp slt i32 %tmp1, 10
83 br i1 %cmp, label %for.body, label %for.end
86 ; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
87 ; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
89 for.body: ; preds = %for.cond
90 invoke void @"\01?may_throw@@YAXXZ"()
91 to label %invoke.cont unwind label %lpad
93 invoke.cont: ; preds = %for.body
94 %tmp2 = load i32, i32* %i, align 4
95 %a = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
96 %tmp3 = load i32, i32* %a, align 4
97 %add = add nsw i32 %tmp3, %tmp2
98 store i32 %add, i32* %a, align 4
101 ; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %for.body
102 ; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
103 ; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
104 ; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
105 ; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
107 lpad: ; preds = %for.body
108 %tmp4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
109 catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
110 %tmp5 = extractvalue { i8*, i32 } %tmp4, 0
111 store i8* %tmp5, i8** %exn.slot
112 %tmp6 = extractvalue { i8*, i32 } %tmp4, 1
113 store i32 %tmp6, i32* %ehselector.slot
114 br label %catch.dispatch
116 ; CHECK-NOT: catch.dispatch:
118 catch.dispatch: ; preds = %lpad
119 %sel = load i32, i32* %ehselector.slot
120 %tmp7 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
121 %matches = icmp eq i32 %sel, %tmp7
122 br i1 %matches, label %catch, label %eh.resume
126 catch: ; preds = %catch.dispatch
127 %exn = load i8*, i8** %exn.slot
128 %e.i8 = bitcast i32* %e to i8*
129 call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #1
130 %tmp11 = load i32, i32* %e, align 4
131 %tmp12 = load i32, i32* %NumExceptions, align 4
132 %idxprom = sext i32 %tmp12 to i64
133 %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i64 %idxprom
134 store i32 %tmp11, i32* %arrayidx, align 4
135 %tmp13 = load i32, i32* %NumExceptions, align 4
136 %inc = add nsw i32 %tmp13, 1
137 store i32 %inc, i32* %NumExceptions, align 4
138 %tmp14 = load i32, i32* %e, align 4
139 %tmp15 = load i32, i32* %i, align 4
140 %cmp1 = icmp eq i32 %tmp14, %tmp15
141 br i1 %cmp1, label %if.then, label %if.else
143 ; CHECK-NOT: if.then:
145 if.then: ; preds = %catch
146 %tmp16 = load i32, i32* %e, align 4
147 %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1
148 %tmp17 = load i32, i32* %b, align 4
149 %add2 = add nsw i32 %tmp17, %tmp16
150 store i32 %add2, i32* %b, align 4
153 ; CHECK-NOT: if.else:
155 if.else: ; preds = %catch
156 %tmp18 = load i32, i32* %e, align 4
157 %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
158 %tmp19 = load i32, i32* %a3, align 4
159 %add4 = add nsw i32 %tmp19, %tmp18
160 store i32 %add4, i32* %a3, align 4
165 if.end: ; preds = %if.else, %if.then
166 call void @llvm.eh.endcatch() #1
169 try.cont: ; preds = %if.end, %invoke.cont
170 %tmp20 = load i32, i32* %NumExceptions, align 4
171 call void @"\01?does_not_throw@@YAXH@Z"(i32 %tmp20)
174 for.inc: ; preds = %try.cont
175 %tmp21 = load i32, i32* %i, align 4
176 %inc5 = add nsw i32 %tmp21, 1
177 store i32 %inc5, i32* %i, align 4
180 for.end: ; preds = %for.cond
181 %tmp22 = load i32, i32* %NumExceptions, align 4
182 %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i32 0
183 call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %tmp22, %struct.SomeData* dereferenceable(8) %Data)
186 ; CHECK-NOT: eh.resume:
188 eh.resume: ; preds = %catch.dispatch
189 %exn6 = load i8*, i8** %exn.slot
190 %sel7 = load i32, i32* %ehselector.slot
191 %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn6, 0
192 %lpad.val8 = insertvalue { i8*, i32 } %lpad.val, i32 %sel7, 1
193 resume { i8*, i32 } %lpad.val8
198 ; The following catch handler should be outlined.
199 ; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
201 ; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
202 ; CHECK: [[E_PTR1:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
203 ; CHECK: [[RECOVER_NUMEXCEPTIONS:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
204 ; CHECK: [[NUMEXCEPTIONS_PTR1:\%.+]] = bitcast i8* [[RECOVER_NUMEXCEPTIONS]] to i32*
205 ; CHECK: [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
206 ; CHECK: [[EXCEPTIONVAL_PTR1:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]*
207 ; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3)
208 ; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
209 ; CHECK: [[RECOVER_DATA:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4)
210 ; CHECK: [[DATA_PTR1:\%.+]] = bitcast i8* [[RECOVER_DATA]] to %struct.SomeData*
211 ; CHECK: [[TMP:\%.+]] = load i32, i32* [[E_PTR1]], align 4
212 ; CHECK: [[TMP1:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR]], align 4
213 ; CHECK: [[IDXPROM:\%.+]] = sext i32 [[TMP1]] to i64
214 ; CHECK: [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL_PTR1]], i32 0, i64 [[IDXPROM]]
215 ; CHECK: store i32 [[TMP]], i32* [[ARRAYIDX]], align 4
216 ; CHECK: [[TMP2:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR1]], align 4
217 ; CHECK: [[INC:\%.+]] = add nsw i32 [[TMP2]], 1
218 ; CHECK: store i32 [[INC]], i32* [[NUMEXCEPTIONS_PTR]], align 4
219 ; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR1]], align 4
220 ; CHECK: [[TMP4:\%.+]] = load i32, i32* [[I_PTR1]], align 4
221 ; CHECK: [[CMP:\%.+]] = icmp eq i32 [[TMP3]], [[TMP4]]
222 ; CHECK: br i1 [[CMP]], label %if.then, label %if.else
224 ; CHECK: if.then: ; preds = %entry
225 ; CHECK: [[TMP5:\%.+]] = load i32, i32* [[E_PTR1]], align 4
226 ; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[DATA_PTR1]], i32 0, i32 1
227 ; CHECK: [[TMP6:\%.+]] = load i32, i32* [[B_PTR]], align 4
228 ; CHECK: %add2 = add nsw i32 [[TMP6]], [[TMP5]]
229 ; CHECK: store i32 [[ADD:\%.+]], i32* [[B_PTR]], align 4
230 ; CHECK: br label %if.end
232 ; CHECK: if.else: ; preds = %entry
233 ; CHECK: [[TMP7:\%.+]] = load i32, i32* %e, align 4
234 ; CHECK: [[A3:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
235 ; CHECK: [[TMP8:\%.+]] = load i32, i32* %a3, align 4
236 ; CHECK: [[ADD1:\%.+]] = add nsw i32 [[TMP8]], [[TMP7]]
237 ; CHECK: store i32 [[ADD1]], i32* [[A3]], align 4
238 ; CHECK: br label %if.end
240 ; CHECK: if.end: ; preds = %if.else, %if.then
241 ; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
245 ; Function Attrs: nounwind
246 declare void @llvm.memset(i8* nocapture, i8, i64, i32, i1) #1
248 declare void @"\01?may_throw@@YAXXZ"() #2
250 declare i32 @__CxxFrameHandler3(...)
252 ; Function Attrs: nounwind readnone
253 declare i32 @llvm.eh.typeid.for(i8*) #3
255 declare void @llvm.eh.begincatch(i8*, i8*)
257 declare void @llvm.eh.endcatch()
259 declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
261 declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
263 attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
264 attributes #1 = { nounwind }
265 attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
266 attributes #3 = { nounwind readnone }
268 !llvm.module.flags = !{!0}
271 !0 = !{i32 1, !"PIC Level", i32 2}
272 !1 = !{!"clang version 3.7.0 (trunk 228868)"}