[WinEH] Teach AsmPrinter about funclets
[oota-llvm.git] / test / CodeGen / X86 / seh-except-finally.ll
1 ; RUN: llc < %s | FileCheck %s
2
3 ; Test case based on this source:
4 ; int puts(const char*);
5 ; __declspec(noinline) void crash() {
6 ;   *(volatile int*)0 = 42;
7 ; }
8 ; int filt();
9 ; void use_both() {
10 ;   __try {
11 ;     __try {
12 ;       crash();
13 ;     } __finally {
14 ;       puts("__finally");
15 ;     }
16 ;   } __except (filt()) {
17 ;     puts("__except");
18 ;   }
19 ; }
20
21 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
22 target triple = "x86_64-pc-windows-msvc"
23
24 $"\01??_C@_09KJEHOMHG@__finally?$AA@" = comdat any
25
26 $"\01??_C@_08MLCMLGHM@__except?$AA@" = comdat any
27
28 @"\01??_C@_09KJEHOMHG@__finally?$AA@" = linkonce_odr unnamed_addr constant [10 x i8] c"__finally\00", comdat, align 1
29 @"\01??_C@_08MLCMLGHM@__except?$AA@" = linkonce_odr unnamed_addr constant [9 x i8] c"__except\00", comdat, align 1
30
31 declare void @crash()
32
33 declare i32 @filt()
34
35 ; Function Attrs: nounwind uwtable
36 define void @use_both() #1 personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*) {
37 entry:
38   %exn.slot = alloca i8*
39   %ehselector.slot = alloca i32
40   invoke void @crash() #5
41           to label %invoke.cont unwind label %lpad
42
43 invoke.cont:                                      ; preds = %entry
44   %0 = call i8* @llvm.localaddress()
45   invoke void @"\01?fin$0@0@use_both@@"(i1 zeroext false, i8* %0) #5
46           to label %invoke.cont2 unwind label %lpad1
47
48 invoke.cont2:                                     ; preds = %invoke.cont
49   br label %__try.cont
50
51 lpad:                                             ; preds = %entry
52   %1 = landingpad { i8*, i32 }
53           cleanup
54           catch i8* bitcast (i32 (i8*, i8*)* @"\01?filt$0@0@use_both@@" to i8*)
55   %2 = extractvalue { i8*, i32 } %1, 0
56   store i8* %2, i8** %exn.slot
57   %3 = extractvalue { i8*, i32 } %1, 1
58   store i32 %3, i32* %ehselector.slot
59   %4 = call i8* @llvm.localaddress()
60   invoke void @"\01?fin$0@0@use_both@@"(i1 zeroext true, i8* %4) #5
61           to label %invoke.cont3 unwind label %lpad1
62
63 lpad1:                                            ; preds = %lpad, %invoke.cont
64   %5 = landingpad { i8*, i32 }
65           catch i8* bitcast (i32 (i8*, i8*)* @"\01?filt$0@0@use_both@@" to i8*)
66   %6 = extractvalue { i8*, i32 } %5, 0
67   store i8* %6, i8** %exn.slot
68   %7 = extractvalue { i8*, i32 } %5, 1
69   store i32 %7, i32* %ehselector.slot
70   br label %catch.dispatch
71
72 invoke.cont3:                                     ; preds = %lpad
73   br label %catch.dispatch
74
75 catch.dispatch:                                   ; preds = %invoke.cont3, %lpad1
76   %sel = load i32, i32* %ehselector.slot
77   %8 = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @"\01?filt$0@0@use_both@@" to i8*)) #6
78   %matches = icmp eq i32 %sel, %8
79   br i1 %matches, label %__except, label %eh.resume
80
81 __except:                                         ; preds = %catch.dispatch
82   %call = call i32 @puts(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @"\01??_C@_08MLCMLGHM@__except?$AA@", i32 0, i32 0))
83   br label %__try.cont
84
85 __try.cont:                                       ; preds = %__except, %invoke.cont2
86   ret void
87
88 eh.resume:                                        ; preds = %catch.dispatch
89   %exn = load i8*, i8** %exn.slot
90   %sel4 = load i32, i32* %ehselector.slot
91   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0
92   %lpad.val5 = insertvalue { i8*, i32 } %lpad.val, i32 %sel4, 1
93   resume { i8*, i32 } %lpad.val5
94 }
95
96 ; CHECK-LABEL: use_both:
97 ; CHECK: .Ltmp0
98 ; CHECK: callq crash
99 ; CHECK: .Ltmp1
100 ; CHECK: .Ltmp3
101 ; CHECK: callq "?fin$0@0@use_both@@"
102 ; CHECK: .Ltmp4
103 ; CHECK: retq
104 ;
105 ; CHECK: .seh_handlerdata
106 ; CHECK-NEXT: .text
107 ; CHECK-NEXT: .Ltmp{{[0-9]+}}
108 ; CHECK-NEXT: .seh_endproc
109 ; CHECK-NEXT: .section .xdata,"dr"
110 ; CHECK-NEXT: .long 3
111 ; CHECK-NEXT: .long .Ltmp0@IMGREL
112 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
113 ; CHECK-NEXT: .long "?fin$0@0@use_both@@"@IMGREL
114 ; CHECK-NEXT: .long 0
115 ; CHECK-NEXT: .long .Ltmp0@IMGREL
116 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
117 ; CHECK-NEXT: .long "?filt$0@0@use_both@@"@IMGREL
118 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}@IMGREL
119 ; CHECK-NEXT: .long .Ltmp3@IMGREL
120 ; CHECK-NEXT: .long .Ltmp4@IMGREL+1
121 ; CHECK-NEXT: .long "?filt$0@0@use_both@@"@IMGREL
122 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}@IMGREL
123
124 ; Function Attrs: noinline nounwind
125 define internal i32 @"\01?filt$0@0@use_both@@"(i8* %exception_pointers, i8* %frame_pointer) #2 {
126 entry:
127   %frame_pointer.addr = alloca i8*, align 8
128   %exception_pointers.addr = alloca i8*, align 8
129   %exn.slot = alloca i8*
130   store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
131   store i8* %exception_pointers, i8** %exception_pointers.addr, align 8
132   %0 = load i8*, i8** %exception_pointers.addr
133   %1 = bitcast i8* %0 to { i32*, i8* }*
134   %2 = getelementptr inbounds { i32*, i8* }, { i32*, i8* }* %1, i32 0, i32 0
135   %3 = load i32*, i32** %2
136   %4 = load i32, i32* %3
137   %5 = zext i32 %4 to i64
138   %6 = inttoptr i64 %5 to i8*
139   store i8* %6, i8** %exn.slot
140   %call = call i32 @filt()
141   ret i32 %call
142 }
143
144 define internal void @"\01?fin$0@0@use_both@@"(i1 zeroext %abnormal_termination, i8* %frame_pointer) #3 {
145 entry:
146   %frame_pointer.addr = alloca i8*, align 8
147   %abnormal_termination.addr = alloca i8, align 1
148   store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
149   %frombool = zext i1 %abnormal_termination to i8
150   store i8 %frombool, i8* %abnormal_termination.addr, align 1
151   %call = call i32 @puts(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @"\01??_C@_09KJEHOMHG@__finally?$AA@", i32 0, i32 0))
152   ret void
153 }
154
155 declare i32 @puts(i8*) #3
156
157 declare i32 @__C_specific_handler(...)
158
159 ; Function Attrs: nounwind readnone
160 declare i8* @llvm.localaddress() #4
161
162 ; Function Attrs: nounwind readnone
163 declare i32 @llvm.eh.typeid.for(i8*) #4
164
165 attributes #0 = { noinline nounwind 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" }
166 attributes #1 = { nounwind 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" }
167 attributes #2 = { noinline nounwind "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" }
168 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" }
169 attributes #4 = { nounwind readnone }
170 attributes #5 = { noinline }
171 attributes #6 = { nounwind }