blob: 1239f267bac8a3c55e68b09f1085c4ffea143992 [file] [log] [blame]
Andrew Kaylor72029c62015-03-03 00:41:03 +00001; 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'
42target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
43target 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; This structure should be declared for the frame allocation block.
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000054; CHECK: %"struct.\01?test@@YAXXZ.ehdata" = type { i32, i8*, i32, i32, [10 x i32], i32, i32*, i32* }
Andrew Kaylor72029c62015-03-03 00:41:03 +000055
56; The function entry should be rewritten like this.
57; CHECK: define void @"\01?test@@YAXXZ"() #0 {
58; CHECK: entry:
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000059; CHECK: %frame.alloc = call i8* @llvm.frameallocate(i32 88)
Andrew Kaylor72029c62015-03-03 00:41:03 +000060; CHECK: %eh.data = bitcast i8* %frame.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
61; CHECK-NOT: %ExceptionVal = alloca [10 x i32], align 16
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000062; CHECK: %NumExceptions.020.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
63; CHECK: %i.019.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 5
64; CHECK: %ExceptionVal = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 4
Andrew Kaylor72029c62015-03-03 00:41:03 +000065; CHECK: %Data = alloca i64, align 8
66; CHECK: %tmpcast = bitcast i64* %Data to %struct.SomeData*
67; CHECK: %0 = bitcast [10 x i32]* %ExceptionVal to i8*
68; CHECK: call void @llvm.lifetime.start(i64 40, i8* %0) #1
69; CHECK: store i64 0, i64* %Data, align 8
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000070; CHECK: %a.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 6
Andrew Kaylor72029c62015-03-03 00:41:03 +000071; CHECK: %a = bitcast i64* %Data to i32*
72; CHECK: store i32* %a, i32** %a.reg2mem
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000073; CHECK: %b.reg2mem = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 7
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000074; CHECK: %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
Andrew Kaylor72029c62015-03-03 00:41:03 +000075; CHECK: store i32* %b, i32** %b.reg2mem
76; CHECK: store i32 0, i32* %NumExceptions.020.reg2mem
77; CHECK: store i32 0, i32* %i.019.reg2mem
78; CHECK: br label %for.body
79
80; Function Attrs: uwtable
81define void @"\01?test@@YAXXZ"() #0 {
82entry:
Reid Kleckner2f05d4c2015-03-03 17:41:09 +000083 %e = alloca i32, align 4
Andrew Kaylor72029c62015-03-03 00:41:03 +000084 %ExceptionVal = alloca [10 x i32], align 16
85 %Data = alloca i64, align 8
86 %tmpcast = bitcast i64* %Data to %struct.SomeData*
87 %0 = bitcast [10 x i32]* %ExceptionVal to i8*
88 call void @llvm.lifetime.start(i64 40, i8* %0) #1
89 store i64 0, i64* %Data, align 8
90 %a = bitcast i64* %Data to i32*
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000091 %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
Andrew Kaylor72029c62015-03-03 00:41:03 +000092 br label %for.body
93
94; CHECK: for.body:
95; CHECK-NOT: %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
96; CHECK-NOT: %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
Reid Kleckner1d2c3f92015-03-03 01:04:39 +000097; CHECK: %i.019.reload = load i32, i32* %i.019.reg2mem
98; CHECK: %NumExceptions.020.reload = load i32, i32* %NumExceptions.020.reg2mem
Andrew Kaylor72029c62015-03-03 00:41:03 +000099for.body: ; preds = %entry, %try.cont
100 %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
101 %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
102 invoke void @"\01?may_throw@@YAXXZ"()
103 to label %invoke.cont unwind label %lpad
104
105; CHECK: invoke.cont: ; preds = %for.body
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000106; CHECK-NOT: %1 = load i32, i32* %a, align 8, !tbaa !2
Andrew Kaylor72029c62015-03-03 00:41:03 +0000107; CHECK-NOT: %add = add nsw i32 %1, %i.019
108; CHECK-NOT: store i32 %add, i32* %a, align 8, !tbaa !2
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000109; CHECK: %a.reload3 = load volatile i32*, i32** %a.reg2mem
110; CHECK: %1 = load i32, i32* %a.reload3, align 8, !tbaa !2
Andrew Kaylor72029c62015-03-03 00:41:03 +0000111; CHECK: %add = add nsw i32 %1, %i.019.reload
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000112; CHECK: %a.reload2 = load volatile i32*, i32** %a.reg2mem
Andrew Kaylor72029c62015-03-03 00:41:03 +0000113; CHECK: store i32 %add, i32* %a.reload2, align 8, !tbaa !2
114; CHECK: br label %try.cont
115invoke.cont: ; preds = %for.body
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000116 %1 = load i32, i32* %a, align 8, !tbaa !2
Andrew Kaylor72029c62015-03-03 00:41:03 +0000117 %add = add nsw i32 %1, %i.019
118 store i32 %add, i32* %a, align 8, !tbaa !2
119 br label %try.cont
120
121lpad: ; preds = %for.body
122 %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
123 catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
124 %3 = extractvalue { i8*, i32 } %2, 1
125 %4 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
126 %matches = icmp eq i32 %3, %4
127 br i1 %matches, label %catch, label %eh.resume
128
129catch: ; preds = %lpad
130 %5 = extractvalue { i8*, i32 } %2, 0
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000131 %e.i8 = bitcast i32* %e to i8*
132 call void @llvm.eh.begincatch(i8* %5, i8* %e.i8) #1
133 %tmp8 = load i32, i32* %e, align 4, !tbaa !7
Andrew Kaylor72029c62015-03-03 00:41:03 +0000134 %idxprom = sext i32 %NumExceptions.020 to i64
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000135 %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000136 store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
Andrew Kaylor72029c62015-03-03 00:41:03 +0000137 %inc = add nsw i32 %NumExceptions.020, 1
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000138 %cmp1 = icmp eq i32 %tmp8, %i.019
Andrew Kaylor72029c62015-03-03 00:41:03 +0000139 br i1 %cmp1, label %if.then, label %if.else
140
141if.then: ; preds = %catch
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000142 %tmp9 = load i32, i32* %b, align 4, !tbaa !8
143 %add2 = add nsw i32 %tmp9, %i.019
Andrew Kaylor72029c62015-03-03 00:41:03 +0000144 store i32 %add2, i32* %b, align 4, !tbaa !8
145 br label %if.end
146
147if.else: ; preds = %catch
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000148 %tmp10 = load i32, i32* %a, align 8, !tbaa !2
149 %add4 = add nsw i32 %tmp10, %tmp8
Andrew Kaylor72029c62015-03-03 00:41:03 +0000150 store i32 %add4, i32* %a, align 8, !tbaa !2
151 br label %if.end
152
153if.end: ; preds = %if.else, %if.then
154 tail call void @llvm.eh.endcatch() #1
155 br label %try.cont
156
157; CHECK: try.cont: ; preds = %if.end, %invoke.cont
158; CHECK-NOT: %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
159; CHECK: %NumExceptions.1 = phi i32 [ %NumExceptions.020.reload, %invoke.cont ], [ %inc, %if.end ]
160; CHECK: tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
161; CHECK-NOT: %inc5 = add nuw nsw i32 %i.019, 1
162; CHECK: %inc5 = add nuw nsw i32 %i.019.reload, 1
163; CHECK: %cmp = icmp slt i32 %inc5, 10
164; CHECK: store i32 %NumExceptions.1, i32* %NumExceptions.020.reg2mem
165; CHECK: store i32 %inc5, i32* %i.019.reg2mem
166; CHECK: br i1 %cmp, label %for.body, label %for.end
167
168try.cont: ; preds = %if.end, %invoke.cont
169 %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
170 tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
171 %inc5 = add nuw nsw i32 %i.019, 1
172 %cmp = icmp slt i32 %inc5, 10
173 br i1 %cmp, label %for.body, label %for.end
174
175for.end: ; preds = %try.cont
176 %NumExceptions.1.lcssa = phi i32 [ %NumExceptions.1, %try.cont ]
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000177 %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 0
Andrew Kaylor72029c62015-03-03 00:41:03 +0000178 call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %NumExceptions.1.lcssa, %struct.SomeData* dereferenceable(8) %tmpcast)
179 call void @llvm.lifetime.end(i64 40, i8* %0) #1
180 ret void
181
182eh.resume: ; preds = %lpad
183 %.lcssa = phi { i8*, i32 } [ %2, %lpad ]
184 resume { i8*, i32 } %.lcssa
185}
186
187; The following catch handler should be outlined.
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000188; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
189; CHECK: entry:
Andrew Kaylor72029c62015-03-03 00:41:03 +0000190; CHECK: %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
191; CHECK: %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000192; CHECK: %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
193; CHECK: %eh.obj = load i8*, i8** %eh.obj.ptr
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000194; CHECK: %e = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 2
195; CHECK: %eh.temp.alloca = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 3
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000196; CHECK: %NumExceptions.020.reload = load i32, i32* %eh.temp.alloca
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000197; CHECK: %ExceptionVal = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 4
198; CHECK: %eh.temp.alloca1 = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 5
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000199; CHECK: %i.019.reload = load i32, i32* %eh.temp.alloca1
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000200; CHECK: %eh.temp.alloca2 = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 6
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000201; CHECK: %a.reload = load i32*, i32** %eh.temp.alloca2
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000202; CHECK: %eh.temp.alloca3 = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 7
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000203; CHECK: %b.reload = load i32*, i32** %eh.temp.alloca3
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000204; CHECK: %e.i8 = bitcast i32* %e to i8*
205; CHECK: %tmp8 = load i32, i32* %e, align 4, !tbaa !7
Andrew Kaylor72029c62015-03-03 00:41:03 +0000206; CHECK: %idxprom = sext i32 %NumExceptions.020.reload to i64
Reid Kleckner1d2c3f92015-03-03 01:04:39 +0000207; CHECK: %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000208; CHECK: store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
Andrew Kaylor72029c62015-03-03 00:41:03 +0000209; CHECK: %inc = add nsw i32 %NumExceptions.020.reload, 1
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000210; CHECK: %cmp1 = icmp eq i32 %tmp8, %i.019.reload
Andrew Kaylor72029c62015-03-03 00:41:03 +0000211; CHECK: br i1 %cmp1, label %if.then, label %if.else
212;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000213; CHECK: if.then: ; preds = %entry
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000214; CHECK: %tmp9 = load i32, i32* %b.reload, align 4, !tbaa !8
215; CHECK: %add2 = add nsw i32 %tmp9, %i.019.reload
Andrew Kaylor72029c62015-03-03 00:41:03 +0000216; CHECK: store i32 %add2, i32* %b.reload, align 4, !tbaa !8
217; CHECK: br label %if.end
218;
Andrew Kaylorf0f5e462015-03-03 20:00:16 +0000219; CHECK: if.else: ; preds = %entry
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000220; CHECK: %tmp10 = load i32, i32* %a.reload, align 8, !tbaa !2
221; CHECK: %add4 = add nsw i32 %tmp10, %tmp8
Andrew Kaylor72029c62015-03-03 00:41:03 +0000222; CHECK: store i32 %add4, i32* %a.reload, align 8, !tbaa !2
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; Function Attrs: nounwind
230declare void @llvm.lifetime.start(i64, i8* nocapture) #1
231
232declare void @"\01?may_throw@@YAXXZ"() #2
233
234declare i32 @__CxxFrameHandler3(...)
235
236; Function Attrs: nounwind readnone
237declare i32 @llvm.eh.typeid.for(i8*) #3
238
Reid Kleckner2f05d4c2015-03-03 17:41:09 +0000239declare void @llvm.eh.begincatch(i8*, i8*)
Andrew Kaylor72029c62015-03-03 00:41:03 +0000240
241declare void @llvm.eh.endcatch()
242
243declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
244
245declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
246
247; Function Attrs: nounwind
248declare void @llvm.lifetime.end(i64, i8* nocapture) #1
249
250attributes #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" }
251attributes #1 = { nounwind }
252attributes #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" }
253attributes #3 = { nounwind readnone }
254
255!llvm.module.flags = !{!0}
256!llvm.ident = !{!1}
257
258!0 = !{i32 1, !"PIC Level", i32 2}
259!1 = !{!"clang version 3.7.0 (trunk 228868)"}
260!2 = !{!3, !4, i64 0}
261!3 = !{!"?AUSomeData@@", !4, i64 0, !4, i64 4}
262!4 = !{!"int", !5, i64 0}
263!5 = !{!"omnipotent char", !6, i64 0}
264!6 = !{!"Simple C/C++ TBAA"}
265!7 = !{!4, !4, i64 0}
266!8 = !{!3, !4, i64 4}