[WinEH] Don't sink allocas into child handlers
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-alloca-sink.ll
1 ; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3 ; This test describes two difficult cases in sinking allocas into child frames.
4 ; We don't currently do this optimization, but we'll need to tweak these tests
5 ; when we do.
6
7 ; This test is based on the following code:
8 ;
9 ; // In this case we can sink the alloca from the parent into the catch because
10 ; // the lifetime is limited to the catch.
11 ; extern "C" void may_throw();
12 ; extern "C" void sink_alloca_to_catch() {
13 ;   try {
14 ;     may_throw();
15 ;   } catch (int) {
16 ;     volatile int only_used_in_catch = 42;
17 ;   }
18 ; }
19 ;
20 ; // In this case we cannot. The variable should live as long as the parent
21 ; // frame lives.
22 ; extern "C" void use_catch_var(int *);
23 ; extern "C" void dont_sink_alloca_to_catch(int n) {
24 ;   int live_in_out_catch = 0;
25 ;   while (n > 0) {
26 ;     try {
27 ;       may_throw();
28 ;     } catch (int) {
29 ;       use_catch_var(&live_in_out_catch);
30 ;     }
31 ;     n--;
32 ;   }
33 ; }
34
35 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
36 target triple = "x86_64-pc-windows-msvc"
37
38 declare void @may_throw() #1
39 declare i32 @__CxxFrameHandler3(...)
40 declare i32 @llvm.eh.typeid.for(i8*) #2
41 declare void @llvm.eh.begincatch(i8* nocapture, i8* nocapture) #3
42 declare void @llvm.eh.endcatch() #3
43
44 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
45 %eh.CatchHandlerType = type { i32, i8* }
46
47 $"\01??_R0H@8" = comdat any
48
49 @"\01??_7type_info@@6B@" = external constant i8*
50 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
51 @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"
52
53 ; Function Attrs: uwtable
54 define void @sink_alloca_to_catch() #0 {
55 entry:
56   %0 = alloca i32
57   %only_used_in_catch = alloca i32, align 4
58   invoke void @may_throw()
59           to label %try.cont unwind label %lpad
60
61 lpad:                                             ; preds = %entry
62   %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
63           catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0
64   %2 = extractvalue { i8*, i32 } %1, 1
65   %3 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)) #3
66   %matches = icmp eq i32 %2, %3
67   br i1 %matches, label %catch, label %eh.resume
68
69 catch:                                            ; preds = %lpad
70   %4 = extractvalue { i8*, i32 } %1, 0
71   call void @llvm.eh.begincatch(i8* %4, i8* null) #3
72   store volatile i32 42, i32* %only_used_in_catch, align 4
73   tail call void @llvm.eh.endcatch() #3
74   br label %try.cont
75
76 try.cont:                                         ; preds = %entry, %catch
77   ret void
78
79 eh.resume:                                        ; preds = %lpad
80   resume { i8*, i32 } %1
81 }
82
83 ; CHECK-LABEL: define void @sink_alloca_to_catch()
84 ; CHECK: call void (...)* @llvm.frameescape(i32* %only_used_in_catch)
85
86 declare void @use_catch_var(i32*) #1
87
88 ; Function Attrs: uwtable
89 define void @dont_sink_alloca_to_catch(i32 %n) #0 {
90 entry:
91   %0 = alloca i32
92   %n.addr = alloca i32, align 4
93   %live_in_out_catch = alloca i32, align 4
94   %exn.slot = alloca i8*
95   %ehselector.slot = alloca i32
96   store i32 %n, i32* %n.addr, align 4
97   br label %while.cond
98
99 while.cond:                                       ; preds = %try.cont, %entry
100   %1 = load i32, i32* %n.addr, align 4
101   %cmp = icmp sgt i32 %1, 0
102   br i1 %cmp, label %while.body, label %while.end
103
104 while.body:                                       ; preds = %while.cond
105   invoke void @may_throw()
106           to label %invoke.cont unwind label %lpad
107
108 invoke.cont:                                      ; preds = %while.body
109   br label %try.cont
110
111 lpad:                                             ; preds = %while.body
112   %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
113           catch i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)
114   %3 = extractvalue { i8*, i32 } %2, 0
115   store i8* %3, i8** %exn.slot
116   %4 = extractvalue { i8*, i32 } %2, 1
117   store i32 %4, i32* %ehselector.slot
118   br label %catch.dispatch
119
120 catch.dispatch:                                   ; preds = %lpad
121   %sel = load i32, i32* %ehselector.slot
122   %5 = call i32 @llvm.eh.typeid.for(i8* bitcast (%eh.CatchHandlerType* @llvm.eh.handlertype.H.0 to i8*)) #3
123   %matches = icmp eq i32 %sel, %5
124   br i1 %matches, label %catch, label %eh.resume
125
126 catch:                                            ; preds = %catch.dispatch
127   %exn = load i8*, i8** %exn.slot
128   call void @llvm.eh.begincatch(i8* %exn, i8* null) #3
129   invoke void @use_catch_var(i32* %live_in_out_catch)
130           to label %invoke.cont2 unwind label %lpad1
131
132 invoke.cont2:                                     ; preds = %catch
133   call void @llvm.eh.endcatch() #3
134   br label %try.cont
135
136 try.cont:                                         ; preds = %invoke.cont2, %invoke.cont
137   %6 = load i32, i32* %0
138   %7 = load i32, i32* %n.addr, align 4
139   %dec = add nsw i32 %7, -1
140   store i32 %dec, i32* %n.addr, align 4
141   br label %while.cond
142
143 lpad1:                                            ; preds = %catch
144   %8 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
145           cleanup
146   %9 = extractvalue { i8*, i32 } %8, 0
147   store i8* %9, i8** %exn.slot
148   %10 = extractvalue { i8*, i32 } %8, 1
149   store i32 %10, i32* %ehselector.slot
150   call void @llvm.eh.endcatch() #3
151   br label %eh.resume
152
153 while.end:                                        ; preds = %while.cond
154   ret void
155
156 eh.resume:                                        ; preds = %lpad1, %catch.dispatch
157   %exn3 = load i8*, i8** %exn.slot
158   %sel4 = load i32, i32* %ehselector.slot
159   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn3, 0
160   %lpad.val5 = insertvalue { i8*, i32 } %lpad.val, i32 %sel4, 1
161   resume { i8*, i32 } %lpad.val5
162 }
163
164 ; CHECK-LABEL: define void @dont_sink_alloca_to_catch(i32 %n)
165 ; CHECK: call void (...)* @llvm.frameescape(i32* %live_in_out_catch)
166
167 ; CHECK-LABEL: define internal i8* @sink_alloca_to_catch.catch(i8*, i8*)
168 ; CHECK: %only_used_in_catch.i8 = call i8* @llvm.framerecover({{.*}}, i32 0)
169 ; CHECK: %only_used_in_catch = bitcast
170
171 ; CHECK-LABEL: define internal i8* @dont_sink_alloca_to_catch.catch(i8*, i8*)
172 ; CHECK: %live_in_out_catch.i8 = call i8* @llvm.framerecover({{.*}}, i32 0)
173 ; CHECK: %live_in_out_catch = bitcast
174
175
176
177 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" }
178 attributes #1 = { "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" }
179 attributes #2 = { nounwind readnone }
180 attributes #3 = { nounwind }