1// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -O0 %s -emit-llvm -o - | FileCheck %s
2
3extern "C" int printf(const char *, ...);
4
5static int N;
6struct S {
7  S()
8  __attribute__((nothrow)) { printf("%d: S()\n", ++N); }
9  ~S() __attribute__((nothrow)) { printf("%d: ~S()\n", N--); }
10  int n[17];
11};
12// CHECK: [[struct_S:%.+]] = type { [17 x i32] }
13void print(int n, int a, int b, int c, int d) {
14  printf("n=%d\n,sizeof(S)=%d\nsizeof(array_t[0][0])=%d\nsizeof(array_t[0])=%d\nsizeof(array_t)=%d\n",
15         n, a, b, c, d);
16  if (n == 2)
17    throw(n);
18}
19
20void test(int n) {
21  // CHECK: define void {{.*test.*}}(i32 [[n:%.+]]) #
22  // CHECK: [[n_addr:%.+]] = alloca
23  // CHECK-NEXT: [[saved_stack:%.+]] = alloca
24  // CHECK-NEXT: [[sizeof_S:%.+]] = alloca
25  // CHECK-NEXT: [[sizeof_array_t_0_0:%.+]] = alloca
26  // CHECK-NEXT: [[sizeof_array_t_0:%.+]] = alloca
27  // CHECK-NEXT: [[sizeof_array_t:%.+]] = alloca
28  // CHECK-NEXT: [[exn_slot:%.+]] = alloca i8*
29  // CHECK-NEXT: [[ehselector_slot:%.+]] = alloca i32
30  // CHECK-NEXT: store i32 [[n]], i32* [[n_addr]]
31  // CHECK-NEXT: [[t0:%.+]] = load i32, i32* [[n_addr]]
32  // CHECK-NEXT: [[t1:%.+]] = zext i32 [[t0]] to i64
33  // CHECK-NEXT: [[t2:%.+]] = load i32, i32* [[n_addr]]
34  // CHECK-NEXT: [[add:%.+]] = add nsw i32 [[t2]], 1
35  // CHECK-NEXT: [[t3:%.+]] = zext i32 [[add]] to i64
36  // CHECK-NEXT: [[t4:%.+]] = call i8* @llvm.stacksave()
37  // CHECK-NEXT: store i8* [[t4]], i8** [[saved_stack]]
38  // CHECK-NEXT: [[t5:%.+]] = mul nuw i64 [[t1]], [[t3]]
39  // CHECK-NEXT: [[vla:%.+]] = alloca [[struct_S]], i64 [[t5]]
40  // CHECK-NEXT: [[t6:%.+]] = mul nuw i64 [[t1]], [[t3]]
41  // CHECK-NEXT: [[isempty:%.+]] = icmp eq i64 [[t6]], 0
42  // CHECK-NEXT: br i1 [[isempty]], label %[[arrayctor_cont:.+]], label %[[new_ctorloop:.+]]
43
44  S array_t[n][n + 1];
45
46  // CHECK: [[new_ctorloop]]
47  // CHECK-NEXT: [[arrayctor_end:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t6]]
48  // CHECK-NEXT: br label %[[arrayctor_loop:.+]]
49
50  // CHECK: [[arrayctor_loop]]
51  // CHECK-NEXT: [[arrayctor_cur:%.+]] = phi [[struct_S]]* [ [[vla]], %[[new_ctorloop]] ], [ [[arrayctor_next:%.+]], %[[arrayctor_loop]] ]
52  // CHECK-NEXT: call void [[ctor:@.+]]([[struct_S]]* [[arrayctor_cur]])
53  // CHECK-NEXT: [[arrayctor_next]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arrayctor_cur]], i64 1
54  // CHECK-NEXT: [[arrayctor_done:%.+]] = icmp eq [[struct_S]]* [[arrayctor_next]], [[arrayctor_end]]
55  // CHECK-NEXT: br i1 [[arrayctor_done]], label %[[arrayctor_cont]], label %[[arrayctor_loop]]
56
57  int sizeof_S = sizeof(S);
58  int sizeof_array_t_0_0 = sizeof(array_t[0][0]);
59  int sizeof_array_t_0 = sizeof(array_t[0]);
60  int sizeof_array_t = sizeof(array_t);
61  print(n, sizeof_S, sizeof_array_t_0_0, sizeof_array_t_0, sizeof_array_t);
62
63  //  CHECK: [[arrayctor_cont]]
64  //  CHECK-NEXT: store i32 68, i32* [[sizeof_S]]
65  //  CHECK-NEXT: store i32 68, i32* [[sizeof_array_t_0_0]]
66  //  CHECK: [[t8:%.+]] = mul nuw i64 68, [[t3]]
67  //  CHECK-NEXT: [[conv:%.+]] = trunc i64 [[t8]] to i32
68  //  CHECK-NEXT: store i32 [[conv]], i32* [[sizeof_array_t_0]]
69  //  CHECK-NEXT: [[t9:%.+]] = mul nuw i64 [[t1]], [[t3]]
70  //  CHECK-NEXT: [[t10:%.+]] = mul nuw i64 68, [[t9]]
71  //  CHECK-NEXT: [[conv1:%.+]] = trunc i64 [[t10]] to i32
72  //  CHECK-NEXT: store i32 [[conv1]], i32* [[sizeof_array_t]]
73  //  CHECK-NEXT: [[t11:%.+]] = load i32, i32* [[n_addr:%.+]]
74  //  CHECK-NEXT: [[t12:%.+]] = load i32, i32* [[sizeof_S]]
75  //  CHECK-NEXT: [[t13:%.+]] = load i32, i32* [[sizeof_array_t_0_0]]
76  //  CHECK-NEXT: [[t14:%.+]] = load i32, i32* [[sizeof_array_t_0]]
77  //  CHECK-NEXT: [[t15:%.+]] = load i32, i32* [[sizeof_array_t]]
78  //  CHECK-NEXT: invoke void @{{.*print.*}}(i32 [[t11]], i32 [[t12]], i32 [[t13]], i32 [[t14]], i32 [[t15]])
79  //  CHECK-NEXT: to label %[[invoke_cont:.+]] unwind label %[[lpad:.+]]
80
81  //  CHECK: [[invoke_cont]]
82  //  CHECK-NEXT: [[t16:%.+]] = mul nuw i64 [[t1]], [[t3]]
83  //  CHECK-NEXT: [[t17:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t16]]
84  //  CHECK-NEXT: [[arraydestroy_isempty:%.+]] = icmp eq [[struct_S]]* [[vla]], [[t17]]
85  //  CHECK-NEXT: br i1 [[arraydestroy_isempty]], label %[[arraydestroy_done2:.+]], label %[[arraydestroy_body:.+]]
86
87  //  CHECK: [[arraydestroy_body]]
88  //  CHECK-NEXT: [[arraydestroy_elementPast:%.+]] = phi [[struct_S]]* [ [[t17]], %[[invoke_cont]] ], [ [[arraydestroy_element:%.+]], %[[arraydestroy_body]] ]
89  //  CHECK-NEXT: [[arraydestroy_element]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arraydestroy_elementPast]]
90  //  CHECK-NEXT: call void @[[dtor:.+]]([[struct_S]]* [[arraydestroy_element]])
91  //  CHECK-NEXT: [[arraydestroy_done:%.+]] = icmp eq [[struct_S]]* [[arraydestroy_element]], [[vla]]
92  //  CHECK-NEXT: br i1 [[arraydestroy_done]], label %[[arraydestroy_done2]], label %[[arraydestroy_body]]
93
94  //  CHECK: [[arraydestroy_done2]]
95  //  CHECK-NEXT: [[t17:%.+]] = load i8*, i8** [[saved_stack]]
96  //  CHECK-NEXT: call void @llvm.stackrestore(i8* [[t17]])
97  //  CHECK: ret void
98
99  //  CHECK: [[lpad]]
100  //  CHECK-NEXT: [[t19:%.+]] = landingpad { i8*, i32 }
101  //  CHECK: [[t20:%.+]] = extractvalue { i8*, i32 } [[t19]], 0
102  //  CHECK-NEXT: store i8* [[t20]], i8** [[exn_slot]]
103  //  CHECK-NEXT: [[t21:%.+]] = extractvalue { i8*, i32 } [[t19]], 1
104  //  CHECK-NEXT: store i32 [[t21]], i32* [[ehselector_slot]]
105  //  CHECK-NEXT: [[t22:%.+]] = mul nuw i64 [[t1]], [[t3]]
106  //  CHECK-NEXT: [[t23:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t22]]
107  //  CHECK-NEXT: [[arraydestroy_isempty3:%.+]] = icmp eq [[struct_S]]* [[vla]], [[t23]]
108  //  CHECK-NEXT: br i1 [[arraydestroy_isempty3]], label %[[arraydestroy_done8:.+]], label %[[arraydestroy_body4:.+]]
109
110  //  CHECK: [[arraydestroy_body4]]
111  //  CHECK: [[arraydestroy_elementPast5:%.+]] = phi [[struct_S]]* [ [[t23]], %[[lpad]] ], [ [[arraydestroy_element6:.+]], %[[arraydestroy_body4]] ]
112  //  CHECK-NEXT: [[arraydestroy_element6]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arraydestroy_elementPast5]], i64 -1
113  //  CHECK-NEXT: call void @[[dtor]]([[struct_S]]* [[arraydestroy_element6]])
114  //  CHECK-NEXT: [[arraydestroy_done7:%.+]] = icmp eq [[struct_S]]* [[arraydestroy_element6]], [[vla]]
115  //  CHECK-NEXT: br i1 [[arraydestroy_done7]], label %[[arraydestroy_done8]], label %[[arraydestroy_body4]]
116
117  //  CHECK: [[arraydestroy_done8]]
118  //  CHECK-NEXT: br label %[[eh_resume:.+]]
119
120  //  CHECK: [[eh_resume]]
121  //  CHECK-NEXT: [[exn:%.+]] = load i8*, i8** [[exn_slot]]
122  //  CHECK-NEXT: [[sel:%.+]] = load i32, i32* [[ehselector_slot]]
123  //  CHECK-NEXT: [[lpad_val:%.+]] = insertvalue { i8*, i32 } undef, i8* [[exn]], 0
124  //  CHECK-NEXT: [[lpad_val9:%.+]] = insertvalue { i8*, i32 } [[lpad_val]], i32 [[sel]], 1
125  //  CHECK-NEXT: resume { i8*, i32 } [[lpad_val9]]
126}
127
128int main() {
129  try {
130    test(2);
131  } catch (int e) {
132    printf("expeption %d\n", e);
133  }
134  try {
135    test(3);
136  } catch (int e) {
137    printf("expeption %d", e);
138  }
139}
140