[opaque pointer type] Add textual IR support for explicit type parameter to getelemen...
[oota-llvm.git] / test / CodeGen / X86 / cppeh-frame-vars.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 ; ModuleID = 'cppeh-frame-vars.cpp'
38 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
39 target triple = "x86_64-pc-windows-msvc"
40
41 %rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
42 %struct.SomeData = type { i32, i32 }
43
44 ; This structure should be declared for the frame allocation block.
45 ; CHECK: %"struct.\01?test@@YAXXZ.ehdata" = type { i32, i8*, i32, i32, [10 x i32], i32, %struct.SomeData }
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
52 ; The function entry should be rewritten like this.
53 ; CHECK: define void @"\01?test@@YAXXZ"() #0 {
54 ; CHECK: entry:
55 ; CHECK:   %frame.alloc = call i8* @llvm.frameallocate(i32 80)
56 ; CHECK:   %eh.data = bitcast i8* %frame.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
57 ; CHECK-NOT:  %NumExceptions = alloca i32, align 4
58 ; CHECK:   %NumExceptions = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
59 ; CHECK-NOT:  %ExceptionVal = alloca [10 x i32], align 16
60 ; CHECK:   %ExceptionVal = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 4
61 ; CHECK-NOT:  %Data = alloca %struct.SomeData, align 4
62 ; CHECK:   %Data = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 6
63 ; CHECK:   %i = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 5
64 ; CHECK:   %exn.slot = alloca i8*
65 ; CHECK:   %ehselector.slot = alloca i32
66 ; CHECK-NOT:  %e = alloca i32, align 4
67 ; CHECK:   %e = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 2
68
69 ; Function Attrs: uwtable
70 define void @"\01?test@@YAXXZ"() #0 {
71 entry:
72   %NumExceptions = alloca i32, align 4
73   %ExceptionVal = alloca [10 x i32], align 16
74   %Data = alloca %struct.SomeData, align 4
75   %i = alloca i32, align 4
76   %exn.slot = alloca i8*
77   %ehselector.slot = alloca i32
78   %e = alloca i32, align 4
79   store i32 0, i32* %NumExceptions, align 4
80   %0 = bitcast %struct.SomeData* %Data to i8*
81   call void @llvm.memset(i8* %0, i8 0, i64 8, i32 4, i1 false)
82   store i32 0, i32* %i, align 4
83   br label %for.cond
84
85 for.cond:                                         ; preds = %for.inc, %entry
86   %1 = load i32* %i, align 4
87   %cmp = icmp slt i32 %1, 10
88   br i1 %cmp, label %for.body, label %for.end
89
90 for.body:                                         ; preds = %for.cond
91   invoke void @"\01?may_throw@@YAXXZ"()
92           to label %invoke.cont unwind label %lpad
93
94 invoke.cont:                                      ; preds = %for.body
95   %2 = load i32* %i, align 4
96   %a = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
97   %3 = load i32* %a, align 4
98   %add = add nsw i32 %3, %2
99   store i32 %add, i32* %a, align 4
100   br label %try.cont
101
102 lpad:                                             ; preds = %for.body
103   %4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
104           catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
105   %5 = extractvalue { i8*, i32 } %4, 0
106   store i8* %5, i8** %exn.slot
107   %6 = extractvalue { i8*, i32 } %4, 1
108   store i32 %6, i32* %ehselector.slot
109   br label %catch.dispatch
110
111 catch.dispatch:                                   ; preds = %lpad
112   %sel = load i32* %ehselector.slot
113   %7 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
114   %matches = icmp eq i32 %sel, %7
115   br i1 %matches, label %catch, label %eh.resume
116
117 catch:                                            ; preds = %catch.dispatch
118   %exn = load i8** %exn.slot
119   %8 = call i8* @llvm.eh.begincatch(i8* %exn) #1
120   %9 = bitcast i8* %8 to i32*
121   %10 = load i32* %9, align 4
122   store i32 %10, i32* %e, align 4
123   %11 = load i32* %e, align 4
124   %12 = load i32* %NumExceptions, align 4
125   %idxprom = sext i32 %12 to i64
126   %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i64 %idxprom
127   store i32 %11, i32* %arrayidx, align 4
128   %13 = load i32* %NumExceptions, align 4
129   %inc = add nsw i32 %13, 1
130   store i32 %inc, i32* %NumExceptions, align 4
131   %14 = load i32* %e, align 4
132   %15 = load i32* %i, align 4
133   %cmp1 = icmp eq i32 %14, %15
134   br i1 %cmp1, label %if.then, label %if.else
135
136 if.then:                                          ; preds = %catch
137   %16 = load i32* %e, align 4
138   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1
139   %17 = load i32* %b, align 4
140   %add2 = add nsw i32 %17, %16
141   store i32 %add2, i32* %b, align 4
142   br label %if.end
143
144 if.else:                                          ; preds = %catch
145   %18 = load i32* %e, align 4
146   %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
147   %19 = load i32* %a3, align 4
148   %add4 = add nsw i32 %19, %18
149   store i32 %add4, i32* %a3, align 4
150   br label %if.end
151
152 if.end:                                           ; preds = %if.else, %if.then
153   call void @llvm.eh.endcatch() #1
154   br label %try.cont
155
156 try.cont:                                         ; preds = %if.end, %invoke.cont
157   %20 = load i32* %NumExceptions, align 4
158   call void @"\01?does_not_throw@@YAXH@Z"(i32 %20)
159   br label %for.inc
160
161 for.inc:                                          ; preds = %try.cont
162   %21 = load i32* %i, align 4
163   %inc5 = add nsw i32 %21, 1
164   store i32 %inc5, i32* %i, align 4
165   br label %for.cond
166
167 for.end:                                          ; preds = %for.cond
168   %22 = load i32* %NumExceptions, align 4
169   %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i32 0
170   call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %22, %struct.SomeData* dereferenceable(8) %Data)
171   ret void
172
173 eh.resume:                                        ; preds = %catch.dispatch
174   %exn6 = load i8** %exn.slot
175   %sel7 = load i32* %ehselector.slot
176   %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn6, 0
177   %lpad.val8 = insertvalue { i8*, i32 } %lpad.val, i32 %sel7, 1
178   resume { i8*, i32 } %lpad.val8
179 }
180
181 ; The following catch handler should be outlined.
182 ; CHECK: define i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
183 ; CHECK: catch.entry:
184 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
185 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
186 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
187 ; CHECK:   %eh.obj = load i8** %eh.obj.ptr
188 ; CHECK:   %e = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 2
189 ; CHECK:   %NumExceptions = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
190 ; CHECK:   %ExceptionVal = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 4
191 ; CHECK:   %i = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 5
192 ; CHECK:   %Data = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 6
193 ; CHECK:   %2 = bitcast i8* %eh.obj to i32*
194 ; CHECK:   %3 = load i32* %2, align 4
195 ; CHECK:   store i32 %3, i32* %e, align 4
196 ; CHECK:   %4 = load i32* %e, align 4
197 ; CHECK:   %5 = load i32* %NumExceptions, align 4
198 ; CHECK:   %idxprom = sext i32 %5 to i64
199 ; CHECK:   %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i64 %idxprom
200 ; CHECK:   store i32 %4, i32* %arrayidx, align 4
201 ; CHECK:   %6 = load i32* %NumExceptions, align 4
202 ; CHECK:   %inc = add nsw i32 %6, 1
203 ; CHECK:   store i32 %inc, i32* %NumExceptions, align 4
204 ; CHECK:   %7 = load i32* %e, align 4
205 ; CHECK:   %8 = load i32* %i, align 4
206 ; CHECK:   %cmp1 = icmp eq i32 %7, %8
207 ; CHECK:   br i1 %cmp1, label %if.then, label %if.else
208 ;
209 ; CHECK: if.then:                                          ; preds = %catch.entry
210 ; CHECK:   %9 = load i32* %e, align 4
211 ; CHECK:   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1
212 ; CHECK:   %10 = load i32* %b, align 4
213 ; CHECK:   %add2 = add nsw i32 %10, %9
214 ; CHECK:   store i32 %add2, i32* %b, align 4
215 ; CHECK:   br label %if.end
216 ;
217 ; CHECK: if.else:                                          ; preds = %catch.entry
218 ; CHECK:   %11 = load i32* %e, align 4
219 ; CHECK:   %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
220 ; CHECK:   %12 = load i32* %a3, align 4
221 ; CHECK:   %add4 = add nsw i32 %12, %11
222 ; CHECK:   store i32 %add4, i32* %a3, align 4
223 ; CHECK:   br label %if.end
224 ;
225 ; CHECK: if.end:                                           ; preds = %if.else, %if.then
226 ; CHECK:   ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
227 ; CHECK: }
228
229
230
231
232
233
234 ; Function Attrs: nounwind
235 declare void @llvm.memset(i8* nocapture, i8, i64, i32, i1) #1
236
237 declare void @"\01?may_throw@@YAXXZ"() #2
238
239 declare i32 @__CxxFrameHandler3(...)
240
241 ; Function Attrs: nounwind readnone
242 declare i32 @llvm.eh.typeid.for(i8*) #3
243
244 declare i8* @llvm.eh.begincatch(i8*)
245
246 declare void @llvm.eh.endcatch()
247
248 declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
249
250 declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
251
252 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" }
253 attributes #1 = { nounwind }
254 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" }
255 attributes #3 = { nounwind readnone }
256
257 !llvm.module.flags = !{!0}
258 !llvm.ident = !{!1}
259
260 !0 = !{i32 1, !"PIC Level", i32 2}
261 !1 = !{!"clang version 3.7.0 (trunk 228868)"}