[WinEH] Emit EH tables for __CxxFrameHandler3 on 32-bit x86
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-similar-catch-blocks.ll
1 ; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3 ; This test is based on the following code:
4 ;
5 ; int main(void) {
6 ;   try {
7 ;     try {
8 ;       throw 'a';
9 ;     } catch (char c) {
10 ;       printf("%c\n", c);
11 ;     }
12 ;     throw 1;
13 ;   } catch(int x) {
14 ;     printf("%d\n", x);
15 ;   } catch(...) {
16 ;     printf("...\n");
17 ;   }
18 ;   try {
19 ;     try {
20 ;       throw 'b';
21 ;     } catch (char c) {
22 ;       printf("%c\n", c);
23 ;     }
24 ;     throw 2;
25 ;   } catch(int x) {
26 ;     printf("%d\n", x);
27 ;   } catch (char c) {
28 ;     printf("%c\n", c);
29 ;   } catch(...) {
30 ;     printf("...\n");
31 ;   }
32 ;   return 0;
33 ; }
34
35 ; This test is just checking for failures in processing the IR.
36 ; Extensive handler matching is not required.
37
38 ; ModuleID = 'cppeh-similar-catch-blocks.cpp'
39 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
40 target triple = "x86_64-pc-windows-msvc"
41
42 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
43 %eh.CatchHandlerType = type { i32, i8* }
44 %eh.CatchableType = type { i32, i32, i32, i32, i32, i32, i32 }
45 %eh.CatchableTypeArray.1 = type { i32, [1 x i32] }
46 %eh.ThrowInfo = type { i32, i32, i32, i32 }
47
48 $"\01??_R0H@8" = comdat any
49
50 $"\01??_R0D@8" = comdat any
51
52 $"_CT??_R0D@81" = comdat any
53
54 $_CTA1D = comdat any
55
56 $_TI1D = comdat any
57
58 $"\01??_C@_03PJCJOCBM@?$CFc?6?$AA@" = comdat any
59
60 $"_CT??_R0H@84" = comdat any
61
62 $_CTA1H = comdat any
63
64 $_TI1H = comdat any
65
66 $"\01??_C@_04MPPNMCOK@?4?4?4?6?$AA@" = comdat any
67
68 $"\01??_C@_03PMGGPEJJ@?$CFd?6?$AA@" = comdat any
69
70 @"\01??_7type_info@@6B@" = external constant i8*
71 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
72 @llvm.eh.handlertype.H.0 = private unnamed_addr constant %eh.CatchHandlerType { i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) }, section "llvm.metadata"
73 @"\01??_R0D@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".D\00" }, comdat
74 @llvm.eh.handlertype.D.0 = private unnamed_addr constant %eh.CatchHandlerType { i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0D@8" to i8*) }, section "llvm.metadata"
75 @__ImageBase = external constant i8
76 @"_CT??_R0D@81" = linkonce_odr unnamed_addr constant %eh.CatchableType { i32 1, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%rtti.TypeDescriptor2* @"\01??_R0D@8" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32), i32 0, i32 -1, i32 0, i32 1, i32 0 }, section ".xdata", comdat
77 @_CTA1D = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x i32] [i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableType* @"_CT??_R0D@81" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32)] }, section ".xdata", comdat
78 @_TI1D = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i32 0, i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableTypeArray.1* @_CTA1D to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32) }, section ".xdata", comdat
79 @"\01??_C@_03PJCJOCBM@?$CFc?6?$AA@" = linkonce_odr unnamed_addr constant [4 x i8] c"%c\0A\00", comdat, align 1
80 @"_CT??_R0H@84" = linkonce_odr unnamed_addr constant %eh.CatchableType { i32 1, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32), i32 0, i32 -1, i32 0, i32 4, i32 0 }, section ".xdata", comdat
81 @_CTA1H = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x i32] [i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableType* @"_CT??_R0H@84" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32)] }, section ".xdata", comdat
82 @_TI1H = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i32 0, i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableTypeArray.1* @_CTA1H to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32) }, section ".xdata", comdat
83 @"\01??_C@_04MPPNMCOK@?4?4?4?6?$AA@" = linkonce_odr unnamed_addr constant [5 x i8] c"...\0A\00", comdat, align 1
84 @"\01??_C@_03PMGGPEJJ@?$CFd?6?$AA@" = linkonce_odr unnamed_addr constant [4 x i8] c"%d\0A\00", comdat, align 1
85
86 ; This is just a minimal check to verify that main was handled by WinEHPrepare.
87 ; CHECK: define i32 @main()
88 ; CHECK: entry:
89 ; CHECK:   call void (...) @llvm.frameescape(i32* [[X_PTR:\%.+]], i32* [[X2_PTR:\%.+]], i8* [[C2_PTR:\%.+]], i8* [[C3_PTR:\%.+]], i8* [[C_PTR:\%.+]])
90 ; CHECK:   invoke void @_CxxThrowException
91 ; CHECK: }
92
93 ; Function Attrs: uwtable
94 define i32 @main() #0 {
95 entry:
96   %retval = alloca i32, align 4
97   %tmp = alloca i8, align 1
98   %exn.slot = alloca i8*
99   %ehselector.slot = alloca i32
100   %c = alloca i8, align 1
101   %tmp3 = alloca i32, align 4
102   %x = alloca i32, align 4
103   %tmp20 = alloca i8, align 1
104   %c28 = alloca i8, align 1
105   %tmp34 = alloca i32, align 4
106   %c48 = alloca i8, align 1
107   %x56 = alloca i32, align 4
108   store i32 0, i32* %retval
109   store i8 97, i8* %tmp
110   invoke void @_CxxThrowException(i8* %tmp, %eh.ThrowInfo* @_TI1D) #4
111           to label %unreachable unwind label %lpad
112
113 lpad:                                             ; preds = %entry
114   %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
115           catch %eh.CatchHandlerType* @llvm.eh.handlertype.D.0
116           catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0
117           catch i8* null
118   %1 = extractvalue { i8*, i32 } %0, 0
119   store i8* %1, i8** %exn.slot
120   %2 = extractvalue { i8*, i32 } %0, 1
121   store i32 %2, i32* %ehselector.slot
122   br label %catch.dispatch
123
124 catch.dispatch:                                   ; preds = %lpad
125   %sel = load i32, i32* %ehselector.slot
126   %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)) #2
127   %matches = icmp eq i32 %sel, %3
128   br i1 %matches, label %catch, label %catch.dispatch5
129
130 catch:                                            ; preds = %catch.dispatch
131   %exn = load i8*, i8** %exn.slot
132   call void @llvm.eh.begincatch(i8* %exn, i8* %c) #2
133   %4 = load i8, i8* %c, align 1
134   %conv = sext i8 %4 to i32
135   %call = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01??_C@_03PJCJOCBM@?$CFc?6?$AA@", i32 0, i32 0), i32 %conv)
136           to label %invoke.cont unwind label %lpad2
137
138 invoke.cont:                                      ; preds = %catch
139   call void @llvm.eh.endcatch() #2
140   br label %try.cont
141
142 try.cont:                                         ; preds = %invoke.cont
143   store i32 1, i32* %tmp3
144   %5 = bitcast i32* %tmp3 to i8*
145   invoke void @_CxxThrowException(i8* %5, %eh.ThrowInfo* @_TI1H) #4
146           to label %unreachable unwind label %lpad4
147
148 lpad2:                                            ; preds = %catch
149   %6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
150           catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0
151           catch i8* null
152   %7 = extractvalue { i8*, i32 } %6, 0
153   store i8* %7, i8** %exn.slot
154   %8 = extractvalue { i8*, i32 } %6, 1
155   store i32 %8, i32* %ehselector.slot
156   call void @llvm.eh.endcatch() #2
157   br label %catch.dispatch5
158
159 lpad4:                                            ; preds = %try.cont
160   %9 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
161           catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0
162           catch i8* null
163   %10 = extractvalue { i8*, i32 } %9, 0
164   store i8* %10, i8** %exn.slot
165   %11 = extractvalue { i8*, i32 } %9, 1
166   store i32 %11, i32* %ehselector.slot
167   br label %catch.dispatch5
168
169 catch.dispatch5:                                  ; preds = %lpad4, %lpad2, %catch.dispatch
170   %sel6 = load i32, i32* %ehselector.slot
171   %12 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)) #2
172   %matches7 = icmp eq i32 %sel6, %12
173   br i1 %matches7, label %catch13, label %catch8
174
175 catch13:                                          ; preds = %catch.dispatch5
176   %exn14 = load i8*, i8** %exn.slot
177   %13 = bitcast i32* %x to i8*
178   call void @llvm.eh.begincatch(i8* %exn14, i8* %13) #2
179   %14 = load i32, i32* %x, align 4
180   %call18 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01??_C@_03PMGGPEJJ@?$CFd?6?$AA@", i32 0, i32 0), i32 %14)
181           to label %invoke.cont17 unwind label %lpad16
182
183 invoke.cont17:                                    ; preds = %catch13
184   call void @llvm.eh.endcatch() #2
185   br label %try.cont19
186
187 try.cont19:                                       ; preds = %invoke.cont17, %invoke.cont11
188   store i8 98, i8* %tmp20
189   invoke void @_CxxThrowException(i8* %tmp20, %eh.ThrowInfo* @_TI1D) #4
190           to label %unreachable unwind label %lpad21
191
192 catch8:                                           ; preds = %catch.dispatch5
193   %exn9 = load i8*, i8** %exn.slot
194   call void @llvm.eh.begincatch(i8* %exn9, i8* null) #2
195   %call12 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @"\01??_C@_04MPPNMCOK@?4?4?4?6?$AA@", i32 0, i32 0))
196           to label %invoke.cont11 unwind label %lpad10
197
198 invoke.cont11:                                    ; preds = %catch8
199   call void @llvm.eh.endcatch() #2
200   br label %try.cont19
201
202 lpad10:                                           ; preds = %catch8
203   %15 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
204           cleanup
205   %16 = extractvalue { i8*, i32 } %15, 0
206   store i8* %16, i8** %exn.slot
207   %17 = extractvalue { i8*, i32 } %15, 1
208   store i32 %17, i32* %ehselector.slot
209   call void @llvm.eh.endcatch() #2
210   br label %eh.resume
211
212 lpad16:                                           ; preds = %catch13
213   %18 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
214           cleanup
215   %19 = extractvalue { i8*, i32 } %18, 0
216   store i8* %19, i8** %exn.slot
217   %20 = extractvalue { i8*, i32 } %18, 1
218   store i32 %20, i32* %ehselector.slot
219   call void @llvm.eh.endcatch() #2
220   br label %eh.resume
221
222 lpad21:                                           ; preds = %try.cont19
223   %21 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
224           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)
225           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)
226           catch i8* null
227   %22 = extractvalue { i8*, i32 } %21, 0
228   store i8* %22, i8** %exn.slot
229   %23 = extractvalue { i8*, i32 } %21, 1
230   store i32 %23, i32* %ehselector.slot
231   br label %catch.dispatch22
232
233 catch.dispatch22:                                 ; preds = %lpad21
234   %sel23 = load i32, i32* %ehselector.slot
235   %24 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)) #2
236   %matches24 = icmp eq i32 %sel23, %24
237   br i1 %matches24, label %catch25, label %catch.dispatch36
238
239 catch25:                                          ; preds = %catch.dispatch22
240   %exn26 = load i8*, i8** %exn.slot
241   call void @llvm.eh.begincatch(i8* %exn26, i8* %c28) #2
242   %25 = load i8, i8* %c28, align 1
243   %conv29 = sext i8 %25 to i32
244   %call32 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01??_C@_03PJCJOCBM@?$CFc?6?$AA@", i32 0, i32 0), i32 %conv29)
245           to label %invoke.cont31 unwind label %lpad30
246
247 invoke.cont31:                                    ; preds = %catch25
248   call void @llvm.eh.endcatch() #2
249   br label %try.cont33
250
251 try.cont33:                                       ; preds = %invoke.cont31
252   store i32 2, i32* %tmp34
253   %26 = bitcast i32* %tmp34 to i8*
254   invoke void @_CxxThrowException(i8* %26, %eh.ThrowInfo* @_TI1H) #4
255           to label %unreachable unwind label %lpad35
256
257 lpad30:                                           ; preds = %catch25
258   %27 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
259           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)
260           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)
261           catch i8* null
262   %28 = extractvalue { i8*, i32 } %27, 0
263   store i8* %28, i8** %exn.slot
264   %29 = extractvalue { i8*, i32 } %27, 1
265   store i32 %29, i32* %ehselector.slot
266   call void @llvm.eh.endcatch() #2
267   br label %catch.dispatch36
268
269 lpad35:                                           ; preds = %try.cont33
270   %30 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
271           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)
272           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)
273           catch i8* null
274   %31 = extractvalue { i8*, i32 } %30, 0
275   store i8* %31, i8** %exn.slot
276   %32 = extractvalue { i8*, i32 } %30, 1
277   store i32 %32, i32* %ehselector.slot
278   br label %catch.dispatch36
279
280 catch.dispatch36:                                 ; preds = %lpad35, %lpad30, %catch.dispatch22
281   %sel37 = load i32, i32* %ehselector.slot
282   %33 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)) #2
283   %matches38 = icmp eq i32 %sel37, %33
284   br i1 %matches38, label %catch53, label %catch.fallthrough
285
286 catch53:                                          ; preds = %catch.dispatch36
287   %exn54 = load i8*, i8** %exn.slot
288   %34 = bitcast i32* %x56 to i8*
289   call void @llvm.eh.begincatch(i8* %exn54, i8* %34) #2
290   %35 = load i32, i32* %x56, align 4
291   %call59 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01??_C@_03PMGGPEJJ@?$CFd?6?$AA@", i32 0, i32 0), i32 %35)
292           to label %invoke.cont58 unwind label %lpad57
293
294 invoke.cont58:                                    ; preds = %catch53
295   call void @llvm.eh.endcatch() #2
296   br label %try.cont60
297
298 try.cont60:                                       ; preds = %invoke.cont58, %invoke.cont51, %invoke.cont43
299   ret i32 0
300
301 catch.fallthrough:                                ; preds = %catch.dispatch36
302   %36 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.D.0 to i8*)) #2
303   %matches39 = icmp eq i32 %sel37, %36
304   br i1 %matches39, label %catch45, label %catch40
305
306 catch45:                                          ; preds = %catch.fallthrough
307   %exn46 = load i8*, i8** %exn.slot
308   call void @llvm.eh.begincatch(i8* %exn46, i8* %c48) #2
309   %37 = load i8, i8* %c48, align 1
310   %conv49 = sext i8 %37 to i32
311   %call52 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @"\01??_C@_03PJCJOCBM@?$CFc?6?$AA@", i32 0, i32 0), i32 %conv49)
312           to label %invoke.cont51 unwind label %lpad50
313
314 invoke.cont51:                                    ; preds = %catch45
315   call void @llvm.eh.endcatch() #2
316   br label %try.cont60
317
318 catch40:                                          ; preds = %catch.fallthrough
319   %exn41 = load i8*, i8** %exn.slot
320   call void @llvm.eh.begincatch(i8* %exn41, i8* null) #2
321   %call44 = invoke i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @"\01??_C@_04MPPNMCOK@?4?4?4?6?$AA@", i32 0, i32 0))
322           to label %invoke.cont43 unwind label %lpad42
323
324 invoke.cont43:                                    ; preds = %catch40
325   call void @llvm.eh.endcatch() #2
326   br label %try.cont60
327
328 lpad42:                                           ; preds = %catch40
329   %38 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
330           cleanup
331   %39 = extractvalue { i8*, i32 } %38, 0
332   store i8* %39, i8** %exn.slot
333   %40 = extractvalue { i8*, i32 } %38, 1
334   store i32 %40, i32* %ehselector.slot
335   call void @llvm.eh.endcatch() #2
336   br label %eh.resume
337
338 lpad50:                                           ; preds = %catch45
339   %41 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
340           cleanup
341   %42 = extractvalue { i8*, i32 } %41, 0
342   store i8* %42, i8** %exn.slot
343   %43 = extractvalue { i8*, i32 } %41, 1
344   store i32 %43, i32* %ehselector.slot
345   call void @llvm.eh.endcatch() #2
346   br label %eh.resume
347
348 lpad57:                                           ; preds = %catch53
349   %44 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
350           cleanup
351   %45 = extractvalue { i8*, i32 } %44, 0
352   store i8* %45, i8** %exn.slot
353   %46 = extractvalue { i8*, i32 } %44, 1
354   store i32 %46, i32* %ehselector.slot
355   call void @llvm.eh.endcatch() #2
356   br label %eh.resume
357
358 eh.resume:                                        ; preds = %lpad57, %lpad50, %lpad42, %lpad16, %lpad10
359   %exn61 = load i8*, i8** %exn.slot
360   %sel62 = load i32, i32* %ehselector.slot
361   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn61, 0
362   %lpad.val63 = insertvalue { i8*, i32 } %lpad.val, i32 %sel62, 1
363   resume { i8*, i32 } %lpad.val63
364
365 unreachable:                                      ; preds = %try.cont33, %try.cont19, %try.cont, %entry
366   unreachable
367 }
368
369 declare void @_CxxThrowException(i8*, %eh.ThrowInfo*)
370
371 declare i32 @__CxxFrameHandler3(...)
372
373 ; Function Attrs: nounwind readnone
374 declare i32 @llvm.eh.typeid.for(i8*) #1
375
376 ; Function Attrs: nounwind
377 declare void @llvm.eh.begincatch(i8* nocapture, i8* nocapture) #2
378
379 declare i32 @printf(i8*, ...) #3
380
381 ; Function Attrs: nounwind
382 declare void @llvm.eh.endcatch() #2
383
384 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" "target-cpu"="x86-64" "unsafe-fp-math"="false" "use-soft-float"="false" }
385 attributes #1 = { nounwind readnone }
386 attributes #2 = { nounwind }
387 attributes #3 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "unsafe-fp-math"="false" "use-soft-float"="false" }
388 attributes #4 = { noreturn }
389
390 !llvm.module.flags = !{!0}
391 !llvm.ident = !{!1}
392
393 !0 = !{i32 1, !"PIC Level", i32 2}
394 !1 = !{!"clang version 3.7.0 (trunk 235214) (llvm/trunk 235213)"}