Replace llvm.frameallocate with llvm.frameescape
[oota-llvm.git] / test / CodeGen / WinEH / cppeh-inalloca.ll
1 ; RUN: opt -mtriple=i386-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
2
3 ; This test is built from the following code:
4 ; struct A {
5 ;   A(int a);
6 ;   A(const A &o);
7 ;   ~A();
8 ;   int a;
9 ; };
10 ;
11 ; void may_throw();
12 ;
13 ; int test(A a) {
14 ;   try {
15 ;     may_throw();
16 ;   }
17 ;   catch (int e) {
18 ;     return a.a + e;
19 ;   }
20 ;   return 0;
21 ; }
22 ;
23 ; The test was built for a 32-bit Windows target and then the reference to
24 ; the inalloca instruction was manually sunk into the landingpad.
25
26 ; ModuleID = 'cppeh-inalloca.cpp'
27 target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
28 target triple = "i386-pc-windows-msvc"
29
30 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
31 %struct.A = type { i32 }
32
33 $"\01??_R0H@8" = comdat any
34
35 @"\01??_7type_info@@6B@" = external constant i8*
36 @"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
37
38 ; The function entry should be rewritten like this.
39 ; CHECK: define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
40 ; CHECK: entry:
41 ; CHECK:   %.tmp.reg2mem = alloca <{ %struct.A }>*
42 ; CHECK:   %.tmp = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef
43 ; CHECK:   store <{ %struct.A }>* %.tmp, <{ %struct.A }>** %.tmp.reg2mem
44 ; CHECK:   %retval = alloca i32, align 4
45 ; CHECK:   %exn.slot = alloca i8*
46 ; CHECK:   %ehselector.slot = alloca i32
47 ; CHECK:   %e = alloca i32, align 4
48 ; CHECK:   %cleanup.dest.slot = alloca i32
49 ; CHECK:   call void (...)* @llvm.frameescape(i32* %e, <{ %struct.A }>** %.tmp.reg2mem, i32* %retval, i32* %cleanup.dest.slot)
50 ; CHECK:   invoke void @"\01?may_throw@@YAXXZ"()
51 ; CHECK:           to label %invoke.cont unwind label %lpad
52
53 define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
54 entry:
55   %retval = alloca i32, align 4
56   %exn.slot = alloca i8*
57   %ehselector.slot = alloca i32
58   %e = alloca i32, align 4
59   %cleanup.dest.slot = alloca i32
60   invoke void @"\01?may_throw@@YAXXZ"()
61           to label %invoke.cont unwind label %lpad
62
63 invoke.cont:                                      ; preds = %entry
64   br label %try.cont
65
66 lpad:                                             ; preds = %entry
67   %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
68           cleanup
69           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
70   %2 = extractvalue { i8*, i32 } %1, 0
71   store i8* %2, i8** %exn.slot
72   %3 = extractvalue { i8*, i32 } %1, 1
73   store i32 %3, i32* %ehselector.slot
74   br label %catch.dispatch
75
76 catch.dispatch:                                   ; preds = %lpad
77   %sel = load i32, i32* %ehselector.slot
78   %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
79   %matches = icmp eq i32 %sel, %4
80   br i1 %matches, label %catch, label %ehcleanup
81
82 catch:                                            ; preds = %catch.dispatch
83   %exn = load i8*, i8** %exn.slot
84   %e.i8 = bitcast i32* %e to i8*
85   call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #3
86   %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
87   %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0
88   %tmp8 = load i32, i32* %a1, align 4
89   %tmp9 = load i32, i32* %e, align 4
90   %add = add nsw i32 %tmp8, %tmp9
91   store i32 %add, i32* %retval
92   store i32 1, i32* %cleanup.dest.slot
93   call void @llvm.eh.endcatch() #3
94   br label %cleanup
95
96 try.cont:                                         ; preds = %invoke.cont
97   store i32 0, i32* %retval
98   store i32 1, i32* %cleanup.dest.slot
99   br label %cleanup
100
101 ; The cleanup block should be re-written like this.
102 ; CHECK: cleanup:                                          ; preds = %try.cont, %catch
103 ; CHECK-NOT:  %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
104 ; CHECK:   %.tmp.reload1 = load volatile <{ %struct.A }>*, <{ %struct.A }>** %.tmp.reg2mem
105 ; CHECK:   %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.tmp.reload1, i32 0, i32 0
106 ; CHECK:   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #2
107 ; CHECK:   %tmp10 = load i32, i32* %retval
108 ; CHECK:   ret i32 %tmp10
109
110 cleanup:                                          ; preds = %try.cont, %catch
111   %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
112   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #3
113   %tmp10 = load i32, i32* %retval
114   ret i32 %tmp10
115
116 ehcleanup:                                        ; preds = %catch.dispatch
117   %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
118   call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3
119   br label %eh.resume
120
121 eh.resume:                                        ; preds = %ehcleanup
122   %exn2 = load i8*, i8** %exn.slot
123   %sel3 = load i32, i32* %ehselector.slot
124   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0
125   %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1
126   resume { i8*, i32 } %lpad.val4
127 }
128
129 ; The following catch handler should be outlined.
130 ; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
131 ; CHECK: entry:
132 ; CHECK:   %e.i81 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0)
133 ; CHECK:   %e = bitcast i8* %e.i81 to i32*
134 ; CHECK:   %eh.temp.alloca.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
135 ; CHECK:   %eh.temp.alloca = bitcast i8* %eh.temp.alloca.i8 to <{ %struct.A }>**
136 ; CHECK:   %.reload = load <{ %struct.A }>*, <{ %struct.A }>** %eh.temp.alloca
137 ; CHECK:   %retval.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2)
138 ; CHECK:   %retval = bitcast i8* %retval.i8 to i32*
139 ; CHECK:   %cleanup.dest.slot.i8 = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3)
140 ; CHECK:   %cleanup.dest.slot = bitcast i8* %cleanup.dest.slot.i8 to i32*
141 ; CHECK:   %e.i8 = bitcast i32* %e to i8*
142 ; CHECK:   %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %.reload, i32 0, i32 0
143 ; CHECK:   %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0
144 ; CHECK:   %tmp8 = load i32, i32* %a1, align 4
145 ; CHECK:   %tmp9 = load i32, i32* %e, align 4
146 ; CHECK:   %add = add nsw i32 %tmp8, %tmp9
147 ; CHECK:   store i32 %add, i32* %retval
148 ; CHECK:   store i32 1, i32* %cleanup.dest.slot
149 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup)
150 ; CHECK: }
151
152 declare void @"\01?may_throw@@YAXXZ"() #0
153
154 declare i32 @__CxxFrameHandler3(...)
155
156 ; Function Attrs: nounwind readnone
157 declare i32 @llvm.eh.typeid.for(i8*) #1
158
159 declare void @llvm.eh.begincatch(i8*, i8*)
160
161 declare void @llvm.eh.endcatch()
162
163 ; Function Attrs: nounwind
164 declare x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A*) #2
165
166 attributes #0 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
167 attributes #1 = { nounwind readnone }
168 attributes #2 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
169 attributes #3 = { nounwind }
170
171 !llvm.ident = !{!0}
172
173 !0 = !{!"clang version 3.7.0 (trunk 228868)"}