xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/cxx11-initializer-array-new.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++11 %s -emit-llvm -o - | FileCheck %s
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc // PR10878
4*0a6a1f1dSLionel Sambuc 
5*0a6a1f1dSLionel Sambuc struct S { S(); S(int); ~S(); int n; };
6*0a6a1f1dSLionel Sambuc 
7*0a6a1f1dSLionel Sambuc void *p = new S[2][3]{ { 1, 2, 3 }, { 4, 5, 6 } };
8*0a6a1f1dSLionel Sambuc 
9*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define
10*0a6a1f1dSLionel Sambuc // CHECK: %[[ALLOC:.*]] = call noalias i8* @_Znam(i64 32)
11*0a6a1f1dSLionel Sambuc // CHECK: %[[COOKIE:.*]] = bitcast i8* %[[ALLOC]] to i64*
12*0a6a1f1dSLionel Sambuc // CHECK: store i64 6, i64* %[[COOKIE]]
13*0a6a1f1dSLionel Sambuc // CHECK: %[[START_AS_i8:.*]] = getelementptr inbounds i8* %[[ALLOC]], i64 8
14*0a6a1f1dSLionel Sambuc // CHECK: %[[START_AS_S:.*]] = bitcast i8* %[[START_AS_i8]] to %[[S:.*]]*
15*0a6a1f1dSLionel Sambuc //
16*0a6a1f1dSLionel Sambuc // Explicit initializers:
17*0a6a1f1dSLionel Sambuc //
18*0a6a1f1dSLionel Sambuc // { 1, 2, 3 }
19*0a6a1f1dSLionel Sambuc //
20*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0:.*]] = bitcast %[[S]]* %[[START_AS_S]] to [3 x %[[S]]]*
21*0a6a1f1dSLionel Sambuc //
22*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_0:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_0]], i64 0, i64 0
23*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_0]], i32 1)
24*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_1:.*]] = getelementptr inbounds %[[S]]* %[[S_0_0]], i64 1
25*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_1]], i32 2)
26*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_2:.*]] = getelementptr inbounds %[[S]]* %[[S_0_1]], i64 1
27*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_2]], i32 3)
28*0a6a1f1dSLionel Sambuc //
29*0a6a1f1dSLionel Sambuc // { 4, 5, 6 }
30*0a6a1f1dSLionel Sambuc //
31*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_0]], i32 1
32*0a6a1f1dSLionel Sambuc //
33*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_0:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_1]], i64 0, i64 0
34*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_0]], i32 4)
35*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_1:.*]] = getelementptr inbounds %[[S]]* %[[S_1_0]], i64 1
36*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_1]], i32 5)
37*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_2:.*]] = getelementptr inbounds %[[S]]* %[[S_1_1]], i64 1
38*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_2]], i32 6)
39*0a6a1f1dSLionel Sambuc //
40*0a6a1f1dSLionel Sambuc // CHECK-NOT: br i1
41*0a6a1f1dSLionel Sambuc // CHECK-NOT: call
42*0a6a1f1dSLionel Sambuc // CHECK: }
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc int n;
45*0a6a1f1dSLionel Sambuc void *q = new S[n][3]{ { 1, 2, 3 }, { 4, 5, 6 } };
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define
48*0a6a1f1dSLionel Sambuc //
49*0a6a1f1dSLionel Sambuc // CHECK: load i32* @n
50*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @llvm.umul.with.overflow.i64(i64 %[[N:.*]], i64 12)
51*0a6a1f1dSLionel Sambuc // CHECK: %[[ELTS:.*]] = mul i64 %[[N]], 3
52*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @llvm.uadd.with.overflow.i64(i64 %{{.*}}, i64 8)
53*0a6a1f1dSLionel Sambuc // CHECK: %[[ALLOC:.*]] = call noalias i8* @_Znam(i64 %{{.*}})
54*0a6a1f1dSLionel Sambuc //
55*0a6a1f1dSLionel Sambuc // CHECK: %[[COOKIE:.*]] = bitcast i8* %[[ALLOC]] to i64*
56*0a6a1f1dSLionel Sambuc // CHECK: store i64 %[[ELTS]], i64* %[[COOKIE]]
57*0a6a1f1dSLionel Sambuc // CHECK: %[[START_AS_i8:.*]] = getelementptr inbounds i8* %[[ALLOC]], i64 8
58*0a6a1f1dSLionel Sambuc // CHECK: %[[START_AS_S:.*]] = bitcast i8* %[[START_AS_i8]] to %[[S]]*
59*0a6a1f1dSLionel Sambuc //
60*0a6a1f1dSLionel Sambuc // Explicit initializers:
61*0a6a1f1dSLionel Sambuc //
62*0a6a1f1dSLionel Sambuc // { 1, 2, 3 }
63*0a6a1f1dSLionel Sambuc //
64*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0:.*]] = bitcast %[[S]]* %[[START_AS_S]] to [3 x %[[S]]]*
65*0a6a1f1dSLionel Sambuc //
66*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_0:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_0]], i64 0, i64 0
67*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_0]], i32 1)
68*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_1:.*]] = getelementptr inbounds %[[S]]* %[[S_0_0]], i64 1
69*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_1]], i32 2)
70*0a6a1f1dSLionel Sambuc // CHECK: %[[S_0_2:.*]] = getelementptr inbounds %[[S]]* %[[S_0_1]], i64 1
71*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_0_2]], i32 3)
72*0a6a1f1dSLionel Sambuc //
73*0a6a1f1dSLionel Sambuc // { 4, 5, 6 }
74*0a6a1f1dSLionel Sambuc //
75*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_0]], i32 1
76*0a6a1f1dSLionel Sambuc //
77*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_0:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_1]], i64 0, i64 0
78*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_0]], i32 4)
79*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_1:.*]] = getelementptr inbounds %[[S]]* %[[S_1_0]], i64 1
80*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_1]], i32 5)
81*0a6a1f1dSLionel Sambuc // CHECK: %[[S_1_2:.*]] = getelementptr inbounds %[[S]]* %[[S_1_1]], i64 1
82*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ei(%[[S]]* %[[S_1_2]], i32 6)
83*0a6a1f1dSLionel Sambuc //
84*0a6a1f1dSLionel Sambuc // And the rest.
85*0a6a1f1dSLionel Sambuc //
86*0a6a1f1dSLionel Sambuc // CHECK: %[[S_2:.*]] = getelementptr inbounds [3 x %[[S]]]* %[[S_1]], i32 1
87*0a6a1f1dSLionel Sambuc // CHECK: %[[S_2_AS_S:.*]] = bitcast [3 x %[[S]]]* %[[S_2]] to %[[S]]*
88*0a6a1f1dSLionel Sambuc //
89*0a6a1f1dSLionel Sambuc // CHECK: %[[REST:.*]] = sub i64 %[[ELTS]], 6
90*0a6a1f1dSLionel Sambuc // CHECK: icmp eq i64 %[[REST]], 0
91*0a6a1f1dSLionel Sambuc // CHECK: br i1
92*0a6a1f1dSLionel Sambuc //
93*0a6a1f1dSLionel Sambuc // CHECK: %[[END:.*]] = getelementptr inbounds %[[S]]* %[[S_2_AS_S]], i64 %[[REST]]
94*0a6a1f1dSLionel Sambuc // CHECK: br label
95*0a6a1f1dSLionel Sambuc //
96*0a6a1f1dSLionel Sambuc // CHECK: %[[CUR:.*]] = phi %[[S]]* [ %[[S_2_AS_S]], {{.*}} ], [ %[[NEXT:.*]], {{.*}} ]
97*0a6a1f1dSLionel Sambuc // CHECK: call void @_ZN1SC1Ev(%[[S]]* %[[CUR]])
98*0a6a1f1dSLionel Sambuc // CHECK: %[[NEXT]] = getelementptr inbounds %[[S]]* %[[CUR]], i64 1
99*0a6a1f1dSLionel Sambuc // CHECK: icmp eq %[[S]]* %[[NEXT]], %[[END]]
100*0a6a1f1dSLionel Sambuc // CHECK: br i1
101*0a6a1f1dSLionel Sambuc //
102*0a6a1f1dSLionel Sambuc // CHECK: }
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc struct T { int a; };
105*0a6a1f1dSLionel Sambuc void *r = new T[n][3]{ { 1, 2, 3 }, { 4, 5, 6 } };
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc // CHECK-LABEL: define
108*0a6a1f1dSLionel Sambuc //
109*0a6a1f1dSLionel Sambuc // CHECK: load i32* @n
110*0a6a1f1dSLionel Sambuc // CHECK: call {{.*}} @llvm.umul.with.overflow.i64(i64 %[[N:.*]], i64 12)
111*0a6a1f1dSLionel Sambuc // CHECK: %[[ELTS:.*]] = mul i64 %[[N]], 3
112*0a6a1f1dSLionel Sambuc //
113*0a6a1f1dSLionel Sambuc // No cookie.
114*0a6a1f1dSLionel Sambuc // CHECK-NOT: @llvm.uadd.with.overflow
115*0a6a1f1dSLionel Sambuc //
116*0a6a1f1dSLionel Sambuc // CHECK: %[[ALLOC:.*]] = call noalias i8* @_Znam(i64 %{{.*}})
117*0a6a1f1dSLionel Sambuc //
118*0a6a1f1dSLionel Sambuc // CHECK: %[[START_AS_T:.*]] = bitcast i8* %[[ALLOC]] to %[[T:.*]]*
119*0a6a1f1dSLionel Sambuc //
120*0a6a1f1dSLionel Sambuc // Explicit initializers:
121*0a6a1f1dSLionel Sambuc //
122*0a6a1f1dSLionel Sambuc // { 1, 2, 3 }
123*0a6a1f1dSLionel Sambuc //
124*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0:.*]] = bitcast %[[T]]* %[[START_AS_T]] to [3 x %[[T]]]*
125*0a6a1f1dSLionel Sambuc //
126*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_0:.*]] = getelementptr inbounds [3 x %[[T]]]* %[[T_0]], i64 0, i64 0
127*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_0_0:.*]] = getelementptr inbounds %[[T]]* %[[T_0_0]], i32 0, i32 0
128*0a6a1f1dSLionel Sambuc // CHECK: store i32 1, i32* %[[T_0_0_0]]
129*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_1:.*]] = getelementptr inbounds %[[T]]* %[[T_0_0]], i64 1
130*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_1_0:.*]] = getelementptr inbounds %[[T]]* %[[T_0_1]], i32 0, i32 0
131*0a6a1f1dSLionel Sambuc // CHECK: store i32 2, i32* %[[T_0_1_0]]
132*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_2:.*]] = getelementptr inbounds %[[T]]* %[[T_0_1]], i64 1
133*0a6a1f1dSLionel Sambuc // CHECK: %[[T_0_2_0:.*]] = getelementptr inbounds %[[T]]* %[[T_0_2]], i32 0, i32 0
134*0a6a1f1dSLionel Sambuc // CHECK: store i32 3, i32* %[[T_0_2_0]]
135*0a6a1f1dSLionel Sambuc //
136*0a6a1f1dSLionel Sambuc // { 4, 5, 6 }
137*0a6a1f1dSLionel Sambuc //
138*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1:.*]] = getelementptr inbounds [3 x %[[T]]]* %[[T_0]], i32 1
139*0a6a1f1dSLionel Sambuc //
140*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_0:.*]] = getelementptr inbounds [3 x %[[T]]]* %[[T_1]], i64 0, i64 0
141*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_0_0:.*]] = getelementptr inbounds %[[T]]* %[[T_1_0]], i32 0, i32 0
142*0a6a1f1dSLionel Sambuc // CHECK: store i32 4, i32* %[[T_1_0_0]]
143*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_1:.*]] = getelementptr inbounds %[[T]]* %[[T_1_0]], i64 1
144*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_1_0:.*]] = getelementptr inbounds %[[T]]* %[[T_1_1]], i32 0, i32 0
145*0a6a1f1dSLionel Sambuc // CHECK: store i32 5, i32* %[[T_1_1_0]]
146*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_2:.*]] = getelementptr inbounds %[[T]]* %[[T_1_1]], i64 1
147*0a6a1f1dSLionel Sambuc // CHECK: %[[T_1_2_0:.*]] = getelementptr inbounds %[[T]]* %[[T_1_2]], i32 0, i32 0
148*0a6a1f1dSLionel Sambuc // CHECK: store i32 6, i32* %[[T_1_2_0]]
149*0a6a1f1dSLionel Sambuc //
150*0a6a1f1dSLionel Sambuc // And the rest gets memset to 0.
151*0a6a1f1dSLionel Sambuc //
152*0a6a1f1dSLionel Sambuc // CHECK: %[[T_2:.*]] = getelementptr inbounds [3 x %[[T]]]* %[[T_1]], i32 1
153*0a6a1f1dSLionel Sambuc // CHECK: %[[T_2_AS_T:.*]] = bitcast [3 x %[[T]]]* %[[T_2]] to %[[T]]*
154*0a6a1f1dSLionel Sambuc //
155*0a6a1f1dSLionel Sambuc // CHECK: %[[SIZE:.*]] = sub i64 %{{.*}}, 24
156*0a6a1f1dSLionel Sambuc // CHECK: %[[REST:.*]] = bitcast %[[T]]* %[[T_2_AS_T]] to i8*
157*0a6a1f1dSLionel Sambuc // CHECK: call void @llvm.memset.p0i8.i64(i8* %[[REST]], i8 0, i64 %[[SIZE]], i32 4, i1 false)
158*0a6a1f1dSLionel Sambuc //
159*0a6a1f1dSLionel Sambuc // CHECK: }
160*0a6a1f1dSLionel Sambuc 
161