1; RUN: opt -S -winehprepare -sehprepare -mtriple=x86_64-windows-msvc < %s | FileCheck %s
2
3; Test case based on this code:
4;
5; extern "C" int _abnormal_termination();
6; #pragma intrinsic(_abnormal_termination)
7; extern "C" int printf(const char *, ...);
8; extern "C" void may_crash() {
9;   *(volatile int *)0 = 42;
10; }
11; int main() {
12;   int myres = 0;
13;   __try {
14;     __try {
15;       may_crash();
16;     } __finally {
17;       printf("inner finally %d\n", _abnormal_termination());
18;       may_crash();
19;     }
20;   } __finally {
21;     printf("outer finally %d\n", _abnormal_termination());
22;   }
23; }
24;
25; Note that if the inner finally crashes, the outer finally still runs. There
26; is nothing like a std::terminate call in this situation.
27
28target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
29target triple = "x86_64-pc-windows-msvc"
30
31@str_outer_finally = linkonce_odr unnamed_addr constant [18 x i8] c"outer finally %d\0A\00", align 1
32@str_inner_finally = linkonce_odr unnamed_addr constant [18 x i8] c"inner finally %d\0A\00", align 1
33
34; Function Attrs: nounwind uwtable
35define void @may_crash() #0 {
36entry:
37  store volatile i32 42, i32* null, align 4
38  ret void
39}
40
41; Function Attrs: uwtable
42define i32 @main() #1 {
43entry:
44  %myres = alloca i32, align 4
45  %exn.slot = alloca i8*
46  %ehselector.slot = alloca i32
47  store i32 0, i32* %myres, align 4
48  invoke void @may_crash() #4
49          to label %invoke.cont unwind label %lpad
50
51invoke.cont:                                      ; preds = %entry
52  %0 = call i8* @llvm.frameaddress(i32 0)
53  invoke void @"\01?fin$1@0@main@@"(i1 zeroext false, i8* %0) #4
54          to label %invoke.cont2 unwind label %lpad1
55
56invoke.cont2:                                     ; preds = %invoke.cont
57  %1 = call i8* @llvm.frameaddress(i32 0)
58  call void @"\01?fin$0@0@main@@"(i1 zeroext false, i8* %1)
59  ret i32 0
60
61lpad:                                             ; preds = %entry
62  %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*)
63          cleanup
64  %3 = extractvalue { i8*, i32 } %2, 0
65  store i8* %3, i8** %exn.slot
66  %4 = extractvalue { i8*, i32 } %2, 1
67  store i32 %4, i32* %ehselector.slot
68  %5 = call i8* @llvm.frameaddress(i32 0)
69  invoke void @"\01?fin$1@0@main@@"(i1 zeroext true, i8* %5) #4
70          to label %invoke.cont3 unwind label %lpad1
71
72lpad1:                                            ; preds = %lpad, %invoke.cont
73  %6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*)
74          cleanup
75  %7 = extractvalue { i8*, i32 } %6, 0
76  store i8* %7, i8** %exn.slot
77  %8 = extractvalue { i8*, i32 } %6, 1
78  store i32 %8, i32* %ehselector.slot
79  br label %ehcleanup
80
81invoke.cont3:                                     ; preds = %lpad
82  br label %ehcleanup
83
84ehcleanup:                                        ; preds = %invoke.cont3, %lpad1
85  %9 = call i8* @llvm.frameaddress(i32 0)
86  call void @"\01?fin$0@0@main@@"(i1 zeroext true, i8* %9)
87  br label %eh.resume
88
89eh.resume:                                        ; preds = %ehcleanup
90  %exn = load i8*, i8** %exn.slot
91  %sel = load i32, i32* %ehselector.slot
92  %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0
93  %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel, 1
94  resume { i8*, i32 } %lpad.val4
95}
96
97; CHECK-NOT: define internal void @
98
99; CHECK-LABEL: define i32 @main()
100; CHECK: invoke void @may_crash()
101;
102; CHECK: landingpad { i8*, i32 }
103; CHECK-NEXT: cleanup
104; CHECK-NEXT: call i8* (...) @llvm.eh.actions(i32 0, void (i1, i8*)* @"\01?fin$1@0@main@@", i32 0, void (i1, i8*)* @"\01?fin$0@0@main@@")
105; CHECK-NEXT: indirectbr
106;
107; CHECK: landingpad { i8*, i32 }
108; CHECK-NEXT: cleanup
109; CHECK-NEXT: call i8* (...) @llvm.eh.actions(i32 0, void (i1, i8*)* @"\01?fin$0@0@main@@")
110; CHECK-NEXT: indirectbr
111
112; There should not be any *new* cleanup helpers, just the existing ones.
113; CHECK-NOT: define internal void @
114; CHECK: define internal void @"\01?fin$0@0@main@@"(i1 zeroext %abnormal_termination, i8* %frame_pointer)
115; CHECK-NOT: define internal void @
116; CHECK: define internal void @"\01?fin$1@0@main@@"(i1 zeroext %abnormal_termination, i8* %frame_pointer)
117; CHECK-NOT: define internal void @
118
119define internal void @"\01?fin$0@0@main@@"(i1 zeroext %abnormal_termination, i8* %frame_pointer) #2 {
120entry:
121  %frame_pointer.addr = alloca i8*, align 8
122  %abnormal_termination.addr = alloca i8, align 1
123  store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
124  %frombool = zext i1 %abnormal_termination to i8
125  store i8 %frombool, i8* %abnormal_termination.addr, align 1
126  %0 = zext i1 %abnormal_termination to i32
127  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str_outer_finally, i32 0, i32 0), i32 %0)
128  ret void
129}
130
131declare i32 @printf(i8*, ...) #2
132
133define internal void @"\01?fin$1@0@main@@"(i1 zeroext %abnormal_termination, i8* %frame_pointer) #2 {
134entry:
135  %frame_pointer.addr = alloca i8*, align 8
136  %abnormal_termination.addr = alloca i8, align 1
137  store i8* %frame_pointer, i8** %frame_pointer.addr, align 8
138  %frombool = zext i1 %abnormal_termination to i8
139  store i8 %frombool, i8* %abnormal_termination.addr, align 1
140  %0 = zext i1 %abnormal_termination to i32
141  %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @str_inner_finally, i32 0, i32 0), i32 %0)
142  call void @may_crash()
143  ret void
144}
145
146declare i32 @__C_specific_handler(...)
147
148; Function Attrs: nounwind readnone
149declare i8* @llvm.frameaddress(i32) #3
150
151attributes #0 = { 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" }
152attributes #1 = { 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" }
153attributes #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" "target-cpu"="x86-64" "unsafe-fp-math"="false" "use-soft-float"="false" }
154attributes #3 = { nounwind readnone }
155attributes #4 = { noinline }
156