Replace llvm.frameallocate with llvm.frameescape
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-nonalloca-frame-values.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 ; struct SomeData {
6 ;   int a;
7 ;   int b;
8 ; };
9
10 ; void may_throw();
11 ; void does_not_throw(int i);
12 ; void dump(int *, int, SomeData&);
13
14 ; void test() {
15 ;   int NumExceptions = 0;
16 ;   int ExceptionVal[10];
17 ;   SomeData Data = { 0, 0 };
18
19 ;   for (int i = 0; i < 10; ++i) {
20 ;     try {
21 ;       may_throw();
22 ;       Data.a += i;
23 ;     }
24 ;     catch (int e) {
25 ;       ExceptionVal[NumExceptions] = e;
26 ;       ++NumExceptions;
27 ;       if (e == i)
28 ;         Data.b += e;
29 ;       else
30 ;         Data.a += e;
31 ;     }
32 ;     does_not_throw(NumExceptions);
33 ;   }
34 ;   dump(ExceptionVal, NumExceptions, Data);
35 ; }
36 ;
37 ; Unlike the cppeh-frame-vars.ll test, this test was generated using -O2
38 ; optimization, which results in non-alloca values being used in the
39 ; catch handler.
40
41 ; ModuleID = 'cppeh-frame-vars.cpp'
42 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
43 target triple = "x86_64-pc-windows-msvc"
44
45 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
46 %struct.SomeData = type { i32, i32 }
47
48 $"\01??_R0H@8" = comdat any
49
50 @"\01??_7type_info@@6B@" = external constant i8*
51 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
52
53 ; The function entry should be rewritten like this.
54 ; CHECK: define void @"\01?test@@YAXXZ"() #0 {
55 ; CHECK: entry:
56 ; CHECK:   %NumExceptions.020.reg2mem = alloca i32
57 ; CHECK:   %i.019.reg2mem = alloca i32
58 ; CHECK:   %e = alloca i32, align 4
59 ; CHECK:   %ExceptionVal = alloca [10 x i32], align 16
60 ; CHECK:   %Data = alloca i64, align 8
61 ; CHECK:   %tmpcast = bitcast i64* %Data to %struct.SomeData*
62 ; CHECK:   %0 = bitcast [10 x i32]* %ExceptionVal to i8*
63 ; CHECK:   call void @llvm.lifetime.start(i64 40, i8* %0) #1
64 ; CHECK:   store i64 0, i64* %Data, align 8
65 ; CHECK:   %a.reg2mem = alloca i32*
66 ; CHECK:   %a = bitcast i64* %Data to i32*
67 ; CHECK:   store i32* %a, i32** %a.reg2mem
68 ; CHECK:   %b.reg2mem = alloca i32*
69 ; CHECK:   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
70 ; CHECK:   store i32* %b, i32** %b.reg2mem
71 ; CHECK:   store i32 0, i32* %NumExceptions.020.reg2mem
72 ; CHECK:   store i32 0, i32* %i.019.reg2mem
73 ; CHECK:   call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions.020.reg2mem, [10 x i32]* %ExceptionVal, i32* %i.019.reg2mem, i32** %a.reg2mem, i32** %b.reg2mem)
74 ; CHECK:   br label %for.body
75
76 ; Function Attrs: uwtable
77 define void @"\01?test@@YAXXZ"() #0 {
78 entry:
79   %e = alloca i32, align 4
80   %ExceptionVal = alloca [10 x i32], align 16
81   %Data = alloca i64, align 8
82   %tmpcast = bitcast i64* %Data to %struct.SomeData*
83   %0 = bitcast [10 x i32]* %ExceptionVal to i8*
84   call void @llvm.lifetime.start(i64 40, i8* %0) #1
85   store i64 0, i64* %Data, align 8
86   %a = bitcast i64* %Data to i32*
87   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
88   br label %for.body
89
90 ; CHECK: for.body:
91 ; CHECK-NOT:  %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
92 ; CHECK-NOT:  %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
93 ; CHECK:  %i.019.reload = load i32, i32* %i.019.reg2mem
94 ; CHECK:  %NumExceptions.020.reload = load i32, i32* %NumExceptions.020.reg2mem
95 for.body:                                         ; preds = %entry, %try.cont
96   %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
97   %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
98   invoke void @"\01?may_throw@@YAXXZ"()
99           to label %invoke.cont unwind label %lpad
100
101 ; CHECK: invoke.cont:                                      ; preds = %for.body
102 ; CHECK-NOT:  %1 = load i32, i32* %a, align 8, !tbaa !2
103 ; CHECK-NOT:  %add = add nsw i32 %1, %i.019
104 ; CHECK-NOT:  store i32 %add, i32* %a, align 8, !tbaa !2
105 ; CHECK:   %a.reload3 = load volatile i32*, i32** %a.reg2mem
106 ; CHECK:   %1 = load i32, i32* %a.reload3, align 8, !tbaa !2
107 ; CHECK:   %add = add nsw i32 %1, %i.019.reload
108 ; CHECK:   %a.reload2 = load volatile i32*, i32** %a.reg2mem
109 ; CHECK:   store i32 %add, i32* %a.reload2, align 8, !tbaa !2
110 ; CHECK:   br label %try.cont
111 invoke.cont:                                      ; preds = %for.body
112   %1 = load i32, i32* %a, align 8, !tbaa !2
113   %add = add nsw i32 %1, %i.019
114   store i32 %add, i32* %a, align 8, !tbaa !2
115   br label %try.cont
116
117 lpad:                                             ; preds = %for.body
118   %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
119           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
120   %3 = extractvalue { i8*, i32 } %2, 1
121   %4 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
122   %matches = icmp eq i32 %3, %4
123   br i1 %matches, label %catch, label %eh.resume
124
125 catch:                                            ; preds = %lpad
126   %5 = extractvalue { i8*, i32 } %2, 0
127   %e.i8 = bitcast i32* %e to i8*
128   call void @llvm.eh.begincatch(i8* %5, i8* %e.i8) #1
129   %tmp8 = load i32, i32* %e, align 4, !tbaa !7
130   %idxprom = sext i32 %NumExceptions.020 to i64
131   %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
132   store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
133   %inc = add nsw i32 %NumExceptions.020, 1
134   %cmp1 = icmp eq i32 %tmp8, %i.019
135   br i1 %cmp1, label %if.then, label %if.else
136
137 if.then:                                          ; preds = %catch
138   %tmp9 = load i32, i32* %b, align 4, !tbaa !8
139   %add2 = add nsw i32 %tmp9, %i.019
140   store i32 %add2, i32* %b, align 4, !tbaa !8
141   br label %if.end
142
143 if.else:                                          ; preds = %catch
144   %tmp10 = load i32, i32* %a, align 8, !tbaa !2
145   %add4 = add nsw i32 %tmp10, %tmp8
146   store i32 %add4, i32* %a, align 8, !tbaa !2
147   br label %if.end
148
149 if.end:                                           ; preds = %if.else, %if.then
150   tail call void @llvm.eh.endcatch() #1
151   br label %try.cont
152
153 ; CHECK: try.cont:                                         ; preds = %if.end, %invoke.cont
154 ; CHECK-NOT:  %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
155 ; CHECK:   %NumExceptions.1 = phi i32 [ %NumExceptions.020.reload, %invoke.cont ], [ %inc, %if.end ]
156 ; CHECK:   tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
157 ; CHECK-NOT:  %inc5 = add nuw nsw i32 %i.019, 1
158 ; CHECK:   %inc5 = add nuw nsw i32 %i.019.reload, 1
159 ; CHECK:   %cmp = icmp slt i32 %inc5, 10
160 ; CHECK:   store i32 %NumExceptions.1, i32* %NumExceptions.020.reg2mem
161 ; CHECK:   store i32 %inc5, i32* %i.019.reg2mem
162 ; CHECK:   br i1 %cmp, label %for.body, label %for.end
163
164 try.cont:                                         ; preds = %if.end, %invoke.cont
165   %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
166   tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
167   %inc5 = add nuw nsw i32 %i.019, 1
168   %cmp = icmp slt i32 %inc5, 10
169   br i1 %cmp, label %for.body, label %for.end
170
171 for.end:                                          ; preds = %try.cont
172   %NumExceptions.1.lcssa = phi i32 [ %NumExceptions.1, %try.cont ]
173   %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 0
174   call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %NumExceptions.1.lcssa, %struct.SomeData* dereferenceable(8) %tmpcast)
175   call void @llvm.lifetime.end(i64 40, i8* %0) #1
176   ret void
177
178 eh.resume:                                        ; preds = %lpad
179   %.lcssa = phi { i8*, i32 } [ %2, %lpad ]
180   resume { i8*, i32 } %.lcssa
181 }
182
183 ; The following catch handler should be outlined.
184 ; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
185 ; CHECK: entry:
186 ; CHECK:   %e.i84 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
187 ; CHECK:   %e = bitcast i8* %e.i84 to i32*
188 ; CHECK:   %eh.temp.alloca.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
189 ; CHECK:   %eh.temp.alloca = bitcast i8* %eh.temp.alloca.i8 to i32*
190 ; CHECK:   %NumExceptions.020.reload = load i32, i32* %eh.temp.alloca
191 ; CHECK:   %ExceptionVal.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
192 ; CHECK:   %ExceptionVal = bitcast i8* %ExceptionVal.i8 to [10 x i32]*
193 ; CHECK:   %eh.temp.alloca1.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3)
194 ; CHECK:   %eh.temp.alloca1 = bitcast i8* %eh.temp.alloca1.i8 to i32*
195 ; CHECK:   %i.019.reload = load i32, i32* %eh.temp.alloca1
196 ; CHECK:   %eh.temp.alloca2.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4)
197 ; CHECK:   %eh.temp.alloca2 = bitcast i8* %eh.temp.alloca2.i8 to i32**
198 ; CHECK:   %a.reload = load i32*, i32** %eh.temp.alloca2
199 ; CHECK:   %eh.temp.alloca3.i8 = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 5)
200 ; CHECK:   %eh.temp.alloca3 = bitcast i8* %eh.temp.alloca3.i8 to i32**
201 ; CHECK:   %b.reload = load i32*, i32** %eh.temp.alloca3
202 ; CHECK:   %e.i8 = bitcast i32* %e to i8*
203 ; CHECK:   %tmp8 = load i32, i32* %e, align 4, !tbaa !7
204 ; CHECK:   %idxprom = sext i32 %NumExceptions.020.reload to i64
205 ; CHECK:   %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
206 ; CHECK:   store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
207 ; CHECK:   %inc = add nsw i32 %NumExceptions.020.reload, 1
208 ; CHECK:   %cmp1 = icmp eq i32 %tmp8, %i.019.reload
209 ; CHECK:   br i1 %cmp1, label %if.then, label %if.else
210 ;
211 ; CHECK: if.then:                                          ; preds = %entry
212 ; CHECK:   %tmp9 = load i32, i32* %b.reload, align 4, !tbaa !8
213 ; CHECK:   %add2 = add nsw i32 %tmp9, %i.019.reload
214 ; CHECK:   store i32 %add2, i32* %b.reload, align 4, !tbaa !8
215 ; CHECK:   br label %if.end
216 ;
217 ; CHECK: if.else:                                          ; preds = %entry
218 ; CHECK:   %tmp10 = load i32, i32* %a.reload, align 8, !tbaa !2
219 ; CHECK:   %add4 = add nsw i32 %tmp10, %tmp8
220 ; CHECK:   store i32 %add4, i32* %a.reload, align 8, !tbaa !2
221 ; CHECK:   br label %if.end
222 ;
223 ; CHECK: if.end:                                           ; preds = %if.else, %if.then
224 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
225 ; CHECK: }
226
227 ; Function Attrs: nounwind
228 declare void @llvm.lifetime.start(i64, i8* nocapture) #1
229
230 declare void @"\01?may_throw@@YAXXZ"() #2
231
232 declare i32 @__CxxFrameHandler3(...)
233
234 ; Function Attrs: nounwind readnone
235 declare i32 @llvm.eh.typeid.for(i8*) #3
236
237 declare void @llvm.eh.begincatch(i8*, i8*)
238
239 declare void @llvm.eh.endcatch()
240
241 declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
242
243 declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
244
245 ; Function Attrs: nounwind
246 declare void @llvm.lifetime.end(i64, i8* nocapture) #1
247
248 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" }
249 attributes #1 = { nounwind }
250 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" }
251 attributes #3 = { nounwind readnone }
252
253 !llvm.module.flags = !{!0}
254 !llvm.ident = !{!1}
255
256 !0 = !{i32 1, !"PIC Level", i32 2}
257 !1 = !{!"clang version 3.7.0 (trunk 228868)"}
258 !2 = !{!3, !4, i64 0}
259 !3 = !{!"?AUSomeData@@", !4, i64 0, !4, i64 4}
260 !4 = !{!"int", !5, i64 0}
261 !5 = !{!"omnipotent char", !6, i64 0}
262 !6 = !{!"Simple C/C++ TBAA"}
263 !7 = !{!4, !4, i64 0}
264 !8 = !{!3, !4, i64 4}